]> Git Repo - linux.git/blame - drivers/mtd/spi-nor/spi-nor.c
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / drivers / mtd / spi-nor / spi-nor.c
CommitLineData
b199489d 1/*
8eabdd1e
HS
2 * Based on m25p80.c, by Mike Lavender ([email protected]), with
3 * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
4 *
5 * Copyright (C) 2005, Intec Automation Inc.
6 * Copyright (C) 2014, Freescale Semiconductor, Inc.
b199489d
HS
7 *
8 * This code is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/err.h>
14#include <linux/errno.h>
15#include <linux/module.h>
16#include <linux/device.h>
17#include <linux/mutex.h>
18#include <linux/math64.h>
09b6a377 19#include <linux/sizes.h>
f384b352 20#include <linux/slab.h>
5390a8df 21#include <linux/sort.h>
b199489d 22
b199489d
HS
23#include <linux/mtd/mtd.h>
24#include <linux/of_platform.h>
25#include <linux/spi/flash.h>
26#include <linux/mtd/spi-nor.h>
27
28/* Define max times to check status register before we give up. */
09b6a377
FS
29
30/*
31 * For everything but full-chip erase; probably could be much smaller, but kept
32 * around for safety for now
33 */
34#define DEFAULT_READY_WAIT_JIFFIES (40UL * HZ)
35
36/*
37 * For full-chip erase, calibrated to a 2MB flash (M25P16); should be scaled up
38 * for larger flash
39 */
40#define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
b199489d 41
d928a259 42#define SPI_NOR_MAX_ID_LEN 6
c67cbb83 43#define SPI_NOR_MAX_ADDR_WIDTH 4
d928a259
HS
44
45struct flash_info {
06bb6f5a
RM
46 char *name;
47
d928a259
HS
48 /*
49 * This array stores the ID bytes.
50 * The first three bytes are the JEDIC ID.
51 * JEDEC ID zero means "no ID" (mostly older chips).
52 */
53 u8 id[SPI_NOR_MAX_ID_LEN];
54 u8 id_len;
55
56 /* The size listed here is what works with SPINOR_OP_SE, which isn't
57 * necessarily called a "sector" by the vendor.
58 */
59 unsigned sector_size;
60 u16 n_sectors;
61
62 u16 page_size;
63 u16 addr_width;
64
65 u16 flags;
0618114e
BN
66#define SECT_4K BIT(0) /* SPINOR_OP_BE_4K works uniformly */
67#define SPI_NOR_NO_ERASE BIT(1) /* No erase command needed */
68#define SST_WRITE BIT(2) /* use SST byte programming */
69#define SPI_NOR_NO_FR BIT(3) /* Can't do fastread */
70#define SECT_4K_PMC BIT(4) /* SPINOR_OP_BE_4K_PMC works uniformly */
71#define SPI_NOR_DUAL_READ BIT(5) /* Flash supports Dual Read */
72#define SPI_NOR_QUAD_READ BIT(6) /* Flash supports Quad Read */
73#define USE_FSR BIT(7) /* use flag status register */
76a4707d 74#define SPI_NOR_HAS_LOCK BIT(8) /* Flash supports lock/unlock via SR */
3dd8012a
BN
75#define SPI_NOR_HAS_TB BIT(9) /*
76 * Flash SR has Top/Bottom (TB) protect
77 * bit. Must be used with
78 * SPI_NOR_HAS_LOCK.
79 */
e99ca98f
RR
80#define SPI_S3AN BIT(10) /*
81 * Xilinx Spartan 3AN In-System Flash
82 * (MFR cannot be used for probing
83 * because it has the same value as
84 * ATMEL flashes)
85 */
ba3ae6a1
CP
86#define SPI_NOR_4B_OPCODES BIT(11) /*
87 * Use dedicated 4byte address op codes
88 * to support memory size above 128Mib.
89 */
2f5ad7f0 90#define NO_CHIP_ERASE BIT(12) /* Chip does not support chip erase */
f384b352 91#define SPI_NOR_SKIP_SFDP BIT(13) /* Skip parsing of SFDP tables */
c4b3eacc 92#define USE_CLSR BIT(14) /* use CLSR command */
e2707285
AY
93
94 int (*quad_enable)(struct spi_nor *nor);
d928a259
HS
95};
96
97#define JEDEC_MFR(info) ((info)->id[0])
b199489d 98
06bb6f5a 99static const struct flash_info *spi_nor_match_id(const char *name);
70f3ce05 100
b199489d
HS
101/*
102 * Read the status register, returning its value in the location
103 * Return the status register value.
104 * Returns negative if error occurred.
105 */
106static int read_sr(struct spi_nor *nor)
107{
108 int ret;
109 u8 val;
110
b02e7f3e 111 ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
b199489d
HS
112 if (ret < 0) {
113 pr_err("error %d reading SR\n", (int) ret);
114 return ret;
115 }
116
117 return val;
118}
119
c14dedde 120/*
121 * Read the flag status register, returning its value in the location
122 * Return the status register value.
123 * Returns negative if error occurred.
124 */
125static int read_fsr(struct spi_nor *nor)
126{
127 int ret;
128 u8 val;
129
130 ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
131 if (ret < 0) {
132 pr_err("error %d reading FSR\n", ret);
133 return ret;
134 }
135
136 return val;
137}
138
b199489d
HS
139/*
140 * Read configuration register, returning its value in the
141 * location. Return the configuration register value.
5d708ecc 142 * Returns negative if error occurred.
b199489d
HS
143 */
144static int read_cr(struct spi_nor *nor)
145{
146 int ret;
147 u8 val;
148
b02e7f3e 149 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
b199489d
HS
150 if (ret < 0) {
151 dev_err(nor->dev, "error %d reading CR\n", ret);
152 return ret;
153 }
154
155 return val;
156}
157
b199489d
HS
158/*
159 * Write status register 1 byte
160 * Returns negative if error occurred.
161 */
162static inline int write_sr(struct spi_nor *nor, u8 val)
163{
164 nor->cmd_buf[0] = val;
f9f3ce83 165 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
b199489d
HS
166}
167
168/*
169 * Set write enable latch with Write Enable command.
170 * Returns negative if error occurred.
171 */
172static inline int write_enable(struct spi_nor *nor)
173{
f9f3ce83 174 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
b199489d
HS
175}
176
177/*
8a1115ff 178 * Send write disable instruction to the chip.
b199489d
HS
179 */
180static inline int write_disable(struct spi_nor *nor)
181{
f9f3ce83 182 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
b199489d
HS
183}
184
185static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
186{
187 return mtd->priv;
188}
189
ba3ae6a1
CP
190
191static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
192{
193 size_t i;
194
195 for (i = 0; i < size; i++)
196 if (table[i][0] == opcode)
197 return table[i][1];
198
199 /* No conversion found, keep input op code. */
200 return opcode;
201}
202
203static inline u8 spi_nor_convert_3to4_read(u8 opcode)
204{
205 static const u8 spi_nor_3to4_read[][2] = {
206 { SPINOR_OP_READ, SPINOR_OP_READ_4B },
207 { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
208 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
209 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
210 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
211 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
15f55331
CP
212
213 { SPINOR_OP_READ_1_1_1_DTR, SPINOR_OP_READ_1_1_1_DTR_4B },
214 { SPINOR_OP_READ_1_2_2_DTR, SPINOR_OP_READ_1_2_2_DTR_4B },
215 { SPINOR_OP_READ_1_4_4_DTR, SPINOR_OP_READ_1_4_4_DTR_4B },
ba3ae6a1
CP
216 };
217
218 return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
219 ARRAY_SIZE(spi_nor_3to4_read));
220}
221
222static inline u8 spi_nor_convert_3to4_program(u8 opcode)
223{
224 static const u8 spi_nor_3to4_program[][2] = {
225 { SPINOR_OP_PP, SPINOR_OP_PP_4B },
226 { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
227 { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
228 };
229
230 return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
231 ARRAY_SIZE(spi_nor_3to4_program));
232}
233
234static inline u8 spi_nor_convert_3to4_erase(u8 opcode)
235{
236 static const u8 spi_nor_3to4_erase[][2] = {
237 { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
238 { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
239 { SPINOR_OP_SE, SPINOR_OP_SE_4B },
240 };
241
242 return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
243 ARRAY_SIZE(spi_nor_3to4_erase));
244}
245
246static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
247 const struct flash_info *info)
248{
249 /* Do some manufacturer fixups first */
250 switch (JEDEC_MFR(info)) {
251 case SNOR_MFR_SPANSION:
252 /* No small sector erase for 4-byte command set */
253 nor->erase_opcode = SPINOR_OP_SE;
254 nor->mtd.erasesize = info->sector_size;
255 break;
256
257 default:
258 break;
259 }
260
261 nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
262 nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
263 nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
5390a8df
TA
264
265 if (!spi_nor_has_uniform_erase(nor)) {
266 struct spi_nor_erase_map *map = &nor->erase_map;
267 struct spi_nor_erase_type *erase;
268 int i;
269
270 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
271 erase = &map->erase_type[i];
272 erase->opcode =
273 spi_nor_convert_3to4_erase(erase->opcode);
274 }
275 }
ba3ae6a1
CP
276}
277
b199489d 278/* Enable/disable 4-byte addressing mode. */
06bb6f5a 279static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
d928a259 280 int enable)
b199489d
HS
281{
282 int status;
283 bool need_wren = false;
284 u8 cmd;
285
d928a259 286 switch (JEDEC_MFR(info)) {
f0d2448e 287 case SNOR_MFR_MICRON:
b199489d
HS
288 /* Some Micron need WREN command; all will accept it */
289 need_wren = true;
f0d2448e
BN
290 case SNOR_MFR_MACRONIX:
291 case SNOR_MFR_WINBOND:
b199489d
HS
292 if (need_wren)
293 write_enable(nor);
294
b02e7f3e 295 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
f9f3ce83 296 status = nor->write_reg(nor, cmd, NULL, 0);
b199489d
HS
297 if (need_wren)
298 write_disable(nor);
299
f134fbbb
N
300 if (!status && !enable &&
301 JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
302 /*
303 * On Winbond W25Q256FV, leaving 4byte mode causes
304 * the Extended Address Register to be set to 1, so all
305 * 3-byte-address reads come from the second 16M.
306 * We must clear the register to enable normal behavior.
307 */
308 write_enable(nor);
309 nor->cmd_buf[0] = 0;
310 nor->write_reg(nor, SPINOR_OP_WREAR, nor->cmd_buf, 1);
311 write_disable(nor);
312 }
313
b199489d
HS
314 return status;
315 default:
316 /* Spansion style */
317 nor->cmd_buf[0] = enable << 7;
f9f3ce83 318 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
b199489d
HS
319 }
320}
e99ca98f
RR
321
322static int s3an_sr_ready(struct spi_nor *nor)
323{
324 int ret;
325 u8 val;
326
327 ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
328 if (ret < 0) {
329 dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
330 return ret;
331 }
332
333 return !!(val & XSR_RDY);
334}
335
51983b7d 336static inline int spi_nor_sr_ready(struct spi_nor *nor)
b199489d 337{
51983b7d
BN
338 int sr = read_sr(nor);
339 if (sr < 0)
340 return sr;
c4b3eacc
AS
341
342 if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) {
343 if (sr & SR_E_ERR)
344 dev_err(nor->dev, "Erase Error occurred\n");
345 else
346 dev_err(nor->dev, "Programming Error occurred\n");
347
348 nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
349 return -EIO;
350 }
351
352 return !(sr & SR_WIP);
51983b7d 353}
b199489d 354
51983b7d
BN
355static inline int spi_nor_fsr_ready(struct spi_nor *nor)
356{
357 int fsr = read_fsr(nor);
358 if (fsr < 0)
359 return fsr;
20ccb993
BH
360
361 if (fsr & (FSR_E_ERR | FSR_P_ERR)) {
362 if (fsr & FSR_E_ERR)
363 dev_err(nor->dev, "Erase operation failed.\n");
364 else
365 dev_err(nor->dev, "Program operation failed.\n");
366
367 if (fsr & FSR_PT_ERR)
368 dev_err(nor->dev,
369 "Attempted to modify a protected sector.\n");
370
371 nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0);
372 return -EIO;
373 }
374
375 return fsr & FSR_READY;
51983b7d 376}
b199489d 377
51983b7d
BN
378static int spi_nor_ready(struct spi_nor *nor)
379{
380 int sr, fsr;
e99ca98f
RR
381
382 if (nor->flags & SNOR_F_READY_XSR_RDY)
383 sr = s3an_sr_ready(nor);
384 else
385 sr = spi_nor_sr_ready(nor);
51983b7d
BN
386 if (sr < 0)
387 return sr;
388 fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
389 if (fsr < 0)
390 return fsr;
391 return sr && fsr;
b199489d
HS
392}
393
b94ed087
BN
394/*
395 * Service routine to read status register until ready, or timeout occurs.
396 * Returns non-zero if error.
397 */
09b6a377
FS
398static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
399 unsigned long timeout_jiffies)
c14dedde 400{
401 unsigned long deadline;
a95ce92e 402 int timeout = 0, ret;
c14dedde 403
09b6a377 404 deadline = jiffies + timeout_jiffies;
c14dedde 405
a95ce92e
BN
406 while (!timeout) {
407 if (time_after_eq(jiffies, deadline))
408 timeout = 1;
c14dedde 409
51983b7d
BN
410 ret = spi_nor_ready(nor);
411 if (ret < 0)
412 return ret;
413 if (ret)
414 return 0;
a95ce92e
BN
415
416 cond_resched();
417 }
418
419 dev_err(nor->dev, "flash operation timed out\n");
c14dedde 420
421 return -ETIMEDOUT;
422}
423
09b6a377
FS
424static int spi_nor_wait_till_ready(struct spi_nor *nor)
425{
426 return spi_nor_wait_till_ready_with_timeout(nor,
427 DEFAULT_READY_WAIT_JIFFIES);
428}
429
b199489d
HS
430/*
431 * Erase the whole flash memory
432 *
433 * Returns 0 if successful, non-zero otherwise.
434 */
435static int erase_chip(struct spi_nor *nor)
436{
19763671 437 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd.size >> 10));
b199489d 438
f9f3ce83 439 return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0);
b199489d
HS
440}
441
442static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
443{
444 int ret = 0;
445
446 mutex_lock(&nor->lock);
447
448 if (nor->prepare) {
449 ret = nor->prepare(nor, ops);
450 if (ret) {
451 dev_err(nor->dev, "failed in the preparation.\n");
452 mutex_unlock(&nor->lock);
453 return ret;
454 }
455 }
456 return ret;
457}
458
459static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
460{
461 if (nor->unprepare)
462 nor->unprepare(nor, ops);
463 mutex_unlock(&nor->lock);
464}
465
e99ca98f
RR
466/*
467 * This code converts an address to the Default Address Mode, that has non
468 * power of two page sizes. We must support this mode because it is the default
469 * mode supported by Xilinx tools, it can access the whole flash area and
470 * changing over to the Power-of-two mode is irreversible and corrupts the
471 * original data.
472 * Addr can safely be unsigned int, the biggest S3AN device is smaller than
473 * 4 MiB.
474 */
475static loff_t spi_nor_s3an_addr_convert(struct spi_nor *nor, unsigned int addr)
476{
56c5c328
RR
477 unsigned int offset;
478 unsigned int page;
e99ca98f 479
56c5c328
RR
480 offset = addr % nor->page_size;
481 page = addr / nor->page_size;
482 page <<= (nor->page_size > 512) ? 10 : 9;
e99ca98f 483
56c5c328 484 return page | offset;
e99ca98f
RR
485}
486
c67cbb83
BN
487/*
488 * Initiate the erasure of a single sector
489 */
490static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
491{
492 u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
493 int i;
494
e99ca98f
RR
495 if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
496 addr = spi_nor_s3an_addr_convert(nor, addr);
497
c67cbb83
BN
498 if (nor->erase)
499 return nor->erase(nor, addr);
500
501 /*
502 * Default implementation, if driver doesn't have a specialized HW
503 * control
504 */
505 for (i = nor->addr_width - 1; i >= 0; i--) {
506 buf[i] = addr & 0xff;
507 addr >>= 8;
508 }
509
510 return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
511}
512
5390a8df
TA
513/**
514 * spi_nor_div_by_erase_size() - calculate remainder and update new dividend
515 * @erase: pointer to a structure that describes a SPI NOR erase type
516 * @dividend: dividend value
517 * @remainder: pointer to u32 remainder (will be updated)
518 *
519 * Return: the result of the division
520 */
521static u64 spi_nor_div_by_erase_size(const struct spi_nor_erase_type *erase,
522 u64 dividend, u32 *remainder)
523{
524 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
525 *remainder = (u32)dividend & erase->size_mask;
526 return dividend >> erase->size_shift;
527}
528
529/**
530 * spi_nor_find_best_erase_type() - find the best erase type for the given
531 * offset in the serial flash memory and the
532 * number of bytes to erase. The region in
533 * which the address fits is expected to be
534 * provided.
535 * @map: the erase map of the SPI NOR
536 * @region: pointer to a structure that describes a SPI NOR erase region
537 * @addr: offset in the serial flash memory
538 * @len: number of bytes to erase
539 *
540 * Return: a pointer to the best fitted erase type, NULL otherwise.
541 */
542static const struct spi_nor_erase_type *
543spi_nor_find_best_erase_type(const struct spi_nor_erase_map *map,
544 const struct spi_nor_erase_region *region,
545 u64 addr, u32 len)
546{
547 const struct spi_nor_erase_type *erase;
548 u32 rem;
549 int i;
550 u8 erase_mask = region->offset & SNOR_ERASE_TYPE_MASK;
551
552 /*
553 * Erase types are ordered by size, with the biggest erase type at
554 * index 0.
555 */
556 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
557 /* Does the erase region support the tested erase type? */
558 if (!(erase_mask & BIT(i)))
559 continue;
560
561 erase = &map->erase_type[i];
562
563 /* Don't erase more than what the user has asked for. */
564 if (erase->size > len)
565 continue;
566
567 /* Alignment is not mandatory for overlaid regions */
568 if (region->offset & SNOR_OVERLAID_REGION)
569 return erase;
570
571 spi_nor_div_by_erase_size(erase, addr, &rem);
572 if (rem)
573 continue;
574 else
575 return erase;
576 }
577
578 return NULL;
579}
580
581/**
582 * spi_nor_region_next() - get the next spi nor region
583 * @region: pointer to a structure that describes a SPI NOR erase region
584 *
585 * Return: the next spi nor region or NULL if last region.
586 */
587static struct spi_nor_erase_region *
588spi_nor_region_next(struct spi_nor_erase_region *region)
589{
590 if (spi_nor_region_is_last(region))
591 return NULL;
592 region++;
593 return region;
594}
595
596/**
597 * spi_nor_find_erase_region() - find the region of the serial flash memory in
598 * which the offset fits
599 * @map: the erase map of the SPI NOR
600 * @addr: offset in the serial flash memory
601 *
602 * Return: a pointer to the spi_nor_erase_region struct, ERR_PTR(-errno)
603 * otherwise.
604 */
605static struct spi_nor_erase_region *
606spi_nor_find_erase_region(const struct spi_nor_erase_map *map, u64 addr)
607{
608 struct spi_nor_erase_region *region = map->regions;
609 u64 region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
610 u64 region_end = region_start + region->size;
611
612 while (addr < region_start || addr >= region_end) {
613 region = spi_nor_region_next(region);
614 if (!region)
615 return ERR_PTR(-EINVAL);
616
617 region_start = region->offset & ~SNOR_ERASE_FLAGS_MASK;
618 region_end = region_start + region->size;
619 }
620
621 return region;
622}
623
624/**
625 * spi_nor_init_erase_cmd() - initialize an erase command
626 * @region: pointer to a structure that describes a SPI NOR erase region
627 * @erase: pointer to a structure that describes a SPI NOR erase type
628 *
629 * Return: the pointer to the allocated erase command, ERR_PTR(-errno)
630 * otherwise.
631 */
632static struct spi_nor_erase_command *
633spi_nor_init_erase_cmd(const struct spi_nor_erase_region *region,
634 const struct spi_nor_erase_type *erase)
635{
636 struct spi_nor_erase_command *cmd;
637
638 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
639 if (!cmd)
640 return ERR_PTR(-ENOMEM);
641
642 INIT_LIST_HEAD(&cmd->list);
643 cmd->opcode = erase->opcode;
644 cmd->count = 1;
645
646 if (region->offset & SNOR_OVERLAID_REGION)
647 cmd->size = region->size;
648 else
649 cmd->size = erase->size;
650
651 return cmd;
652}
653
654/**
655 * spi_nor_destroy_erase_cmd_list() - destroy erase command list
656 * @erase_list: list of erase commands
657 */
658static void spi_nor_destroy_erase_cmd_list(struct list_head *erase_list)
659{
660 struct spi_nor_erase_command *cmd, *next;
661
662 list_for_each_entry_safe(cmd, next, erase_list, list) {
663 list_del(&cmd->list);
664 kfree(cmd);
665 }
666}
667
668/**
669 * spi_nor_init_erase_cmd_list() - initialize erase command list
670 * @nor: pointer to a 'struct spi_nor'
671 * @erase_list: list of erase commands to be executed once we validate that the
672 * erase can be performed
673 * @addr: offset in the serial flash memory
674 * @len: number of bytes to erase
675 *
676 * Builds the list of best fitted erase commands and verifies if the erase can
677 * be performed.
678 *
679 * Return: 0 on success, -errno otherwise.
680 */
681static int spi_nor_init_erase_cmd_list(struct spi_nor *nor,
682 struct list_head *erase_list,
683 u64 addr, u32 len)
684{
685 const struct spi_nor_erase_map *map = &nor->erase_map;
686 const struct spi_nor_erase_type *erase, *prev_erase = NULL;
687 struct spi_nor_erase_region *region;
688 struct spi_nor_erase_command *cmd = NULL;
689 u64 region_end;
690 int ret = -EINVAL;
691
692 region = spi_nor_find_erase_region(map, addr);
693 if (IS_ERR(region))
694 return PTR_ERR(region);
695
696 region_end = spi_nor_region_end(region);
697
698 while (len) {
699 erase = spi_nor_find_best_erase_type(map, region, addr, len);
700 if (!erase)
701 goto destroy_erase_cmd_list;
702
703 if (prev_erase != erase ||
704 region->offset & SNOR_OVERLAID_REGION) {
705 cmd = spi_nor_init_erase_cmd(region, erase);
706 if (IS_ERR(cmd)) {
707 ret = PTR_ERR(cmd);
708 goto destroy_erase_cmd_list;
709 }
710
711 list_add_tail(&cmd->list, erase_list);
712 } else {
713 cmd->count++;
714 }
715
716 addr += cmd->size;
717 len -= cmd->size;
718
719 if (len && addr >= region_end) {
720 region = spi_nor_region_next(region);
721 if (!region)
722 goto destroy_erase_cmd_list;
723 region_end = spi_nor_region_end(region);
724 }
725
726 prev_erase = erase;
727 }
728
729 return 0;
730
731destroy_erase_cmd_list:
732 spi_nor_destroy_erase_cmd_list(erase_list);
733 return ret;
734}
735
736/**
737 * spi_nor_erase_multi_sectors() - perform a non-uniform erase
738 * @nor: pointer to a 'struct spi_nor'
739 * @addr: offset in the serial flash memory
740 * @len: number of bytes to erase
741 *
742 * Build a list of best fitted erase commands and execute it once we validate
743 * that the erase can be performed.
744 *
745 * Return: 0 on success, -errno otherwise.
746 */
747static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len)
748{
749 LIST_HEAD(erase_list);
750 struct spi_nor_erase_command *cmd, *next;
751 int ret;
752
753 ret = spi_nor_init_erase_cmd_list(nor, &erase_list, addr, len);
754 if (ret)
755 return ret;
756
757 list_for_each_entry_safe(cmd, next, &erase_list, list) {
758 nor->erase_opcode = cmd->opcode;
759 while (cmd->count) {
760 write_enable(nor);
761
762 ret = spi_nor_erase_sector(nor, addr);
763 if (ret)
764 goto destroy_erase_cmd_list;
765
766 addr += cmd->size;
767 cmd->count--;
768
769 ret = spi_nor_wait_till_ready(nor);
770 if (ret)
771 goto destroy_erase_cmd_list;
772 }
773 list_del(&cmd->list);
774 kfree(cmd);
775 }
776
777 return 0;
778
779destroy_erase_cmd_list:
780 spi_nor_destroy_erase_cmd_list(&erase_list);
781 return ret;
782}
783
b199489d
HS
784/*
785 * Erase an address range on the nor chip. The address range may extend
786 * one or more erase sectors. Return an error is there is a problem erasing.
787 */
788static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
789{
790 struct spi_nor *nor = mtd_to_spi_nor(mtd);
791 u32 addr, len;
792 uint32_t rem;
793 int ret;
794
795 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
796 (long long)instr->len);
797
5390a8df
TA
798 if (spi_nor_has_uniform_erase(nor)) {
799 div_u64_rem(instr->len, mtd->erasesize, &rem);
800 if (rem)
801 return -EINVAL;
802 }
b199489d
HS
803
804 addr = instr->addr;
805 len = instr->len;
806
807 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
808 if (ret)
809 return ret;
810
811 /* whole-chip erase? */
e99ca98f 812 if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
09b6a377
FS
813 unsigned long timeout;
814
05241aea
BN
815 write_enable(nor);
816
b199489d
HS
817 if (erase_chip(nor)) {
818 ret = -EIO;
819 goto erase_err;
820 }
821
09b6a377
FS
822 /*
823 * Scale the timeout linearly with the size of the flash, with
824 * a minimum calibrated to an old 2MB flash. We could try to
825 * pull these from CFI/SFDP, but these values should be good
826 * enough for now.
827 */
828 timeout = max(CHIP_ERASE_2MB_READY_WAIT_JIFFIES,
829 CHIP_ERASE_2MB_READY_WAIT_JIFFIES *
830 (unsigned long)(mtd->size / SZ_2M));
831 ret = spi_nor_wait_till_ready_with_timeout(nor, timeout);
dfa9c0cb
BN
832 if (ret)
833 goto erase_err;
834
b199489d 835 /* REVISIT in some cases we could speed up erasing large regions
b02e7f3e 836 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
b199489d
HS
837 * to use "small sector erase", but that's not always optimal.
838 */
839
840 /* "sector"-at-a-time erase */
5390a8df 841 } else if (spi_nor_has_uniform_erase(nor)) {
b199489d 842 while (len) {
05241aea
BN
843 write_enable(nor);
844
c67cbb83
BN
845 ret = spi_nor_erase_sector(nor, addr);
846 if (ret)
b199489d 847 goto erase_err;
b199489d
HS
848
849 addr += mtd->erasesize;
850 len -= mtd->erasesize;
dfa9c0cb
BN
851
852 ret = spi_nor_wait_till_ready(nor);
853 if (ret)
854 goto erase_err;
b199489d 855 }
5390a8df
TA
856
857 /* erase multiple sectors */
858 } else {
859 ret = spi_nor_erase_multi_sectors(nor, addr, len);
860 if (ret)
861 goto erase_err;
b199489d
HS
862 }
863
05241aea
BN
864 write_disable(nor);
865
d6af2694 866erase_err:
b199489d
HS
867 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
868
b199489d 869 return ret;
b199489d
HS
870}
871
2666067f
AS
872/* Write status register and ensure bits in mask match written values */
873static int write_sr_and_check(struct spi_nor *nor, u8 status_new, u8 mask)
874{
875 int ret;
876
877 write_enable(nor);
878 ret = write_sr(nor, status_new);
879 if (ret)
880 return ret;
881
882 ret = spi_nor_wait_till_ready(nor);
883 if (ret)
884 return ret;
885
886 ret = read_sr(nor);
887 if (ret < 0)
888 return ret;
889
890 return ((ret & mask) != (status_new & mask)) ? -EIO : 0;
891}
892
62593cf4
BN
893static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
894 uint64_t *len)
895{
896 struct mtd_info *mtd = &nor->mtd;
897 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
898 int shift = ffs(mask) - 1;
899 int pow;
900
901 if (!(sr & mask)) {
902 /* No protection */
903 *ofs = 0;
904 *len = 0;
905 } else {
906 pow = ((sr & mask) ^ mask) >> shift;
907 *len = mtd->size >> pow;
3dd8012a
BN
908 if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
909 *ofs = 0;
910 else
911 *ofs = mtd->size - *len;
62593cf4
BN
912 }
913}
914
915/*
f8860802
BN
916 * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
917 * @locked is false); 0 otherwise
62593cf4 918 */
f8860802
BN
919static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
920 u8 sr, bool locked)
62593cf4
BN
921{
922 loff_t lock_offs;
923 uint64_t lock_len;
924
f8860802
BN
925 if (!len)
926 return 1;
927
62593cf4
BN
928 stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
929
f8860802
BN
930 if (locked)
931 /* Requested range is a sub-range of locked range */
932 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
933 else
934 /* Requested range does not overlap with locked range */
935 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
936}
937
938static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
939 u8 sr)
940{
941 return stm_check_lock_status_sr(nor, ofs, len, sr, true);
942}
943
944static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
945 u8 sr)
946{
947 return stm_check_lock_status_sr(nor, ofs, len, sr, false);
62593cf4
BN
948}
949
950/*
951 * Lock a region of the flash. Compatible with ST Micro and similar flash.
3dd8012a 952 * Supports the block protection bits BP{0,1,2} in the status register
62593cf4 953 * (SR). Does not support these features found in newer SR bitfields:
62593cf4
BN
954 * - SEC: sector/block protect - only handle SEC=0 (block protect)
955 * - CMP: complement protect - only support CMP=0 (range is not complemented)
956 *
3dd8012a
BN
957 * Support for the following is provided conditionally for some flash:
958 * - TB: top/bottom protect
959 *
62593cf4
BN
960 * Sample table portion for 8MB flash (Winbond w25q64fw):
961 *
962 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
963 * --------------------------------------------------------------------------
964 * X | X | 0 | 0 | 0 | NONE | NONE
965 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
966 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
967 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
968 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
969 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
970 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
971 * X | X | 1 | 1 | 1 | 8 MB | ALL
3dd8012a
BN
972 * ------|-------|-------|-------|-------|---------------|-------------------
973 * 0 | 1 | 0 | 0 | 1 | 128 KB | Lower 1/64
974 * 0 | 1 | 0 | 1 | 0 | 256 KB | Lower 1/32
975 * 0 | 1 | 0 | 1 | 1 | 512 KB | Lower 1/16
976 * 0 | 1 | 1 | 0 | 0 | 1 MB | Lower 1/8
977 * 0 | 1 | 1 | 0 | 1 | 2 MB | Lower 1/4
978 * 0 | 1 | 1 | 1 | 0 | 4 MB | Lower 1/2
62593cf4
BN
979 *
980 * Returns negative on errors, 0 on success.
981 */
8cc7f33a 982static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
b199489d 983{
19763671 984 struct mtd_info *mtd = &nor->mtd;
f49289ce 985 int status_old, status_new;
62593cf4
BN
986 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
987 u8 shift = ffs(mask) - 1, pow, val;
f8860802 988 loff_t lock_len;
3dd8012a
BN
989 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
990 bool use_top;
b199489d 991
b199489d 992 status_old = read_sr(nor);
f49289ce
FE
993 if (status_old < 0)
994 return status_old;
b199489d 995
f8860802
BN
996 /* If nothing in our range is unlocked, we don't need to do anything */
997 if (stm_is_locked_sr(nor, ofs, len, status_old))
998 return 0;
999
3dd8012a
BN
1000 /* If anything below us is unlocked, we can't use 'bottom' protection */
1001 if (!stm_is_locked_sr(nor, 0, ofs, status_old))
1002 can_be_bottom = false;
1003
f8860802
BN
1004 /* If anything above us is unlocked, we can't use 'top' protection */
1005 if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
1006 status_old))
3dd8012a
BN
1007 can_be_top = false;
1008
1009 if (!can_be_bottom && !can_be_top)
f8860802
BN
1010 return -EINVAL;
1011
3dd8012a
BN
1012 /* Prefer top, if both are valid */
1013 use_top = can_be_top;
1014
f8860802 1015 /* lock_len: length of region that should end up locked */
3dd8012a
BN
1016 if (use_top)
1017 lock_len = mtd->size - ofs;
1018 else
1019 lock_len = ofs + len;
62593cf4
BN
1020
1021 /*
1022 * Need smallest pow such that:
1023 *
1024 * 1 / (2^pow) <= (len / size)
1025 *
1026 * so (assuming power-of-2 size) we do:
1027 *
1028 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
1029 */
f8860802 1030 pow = ilog2(mtd->size) - ilog2(lock_len);
62593cf4
BN
1031 val = mask - (pow << shift);
1032 if (val & ~mask)
1033 return -EINVAL;
1034 /* Don't "lock" with no region! */
1035 if (!(val & mask))
1036 return -EINVAL;
1037
3dd8012a 1038 status_new = (status_old & ~mask & ~SR_TB) | val;
b199489d 1039
47b8edbf
BN
1040 /* Disallow further writes if WP pin is asserted */
1041 status_new |= SR_SRWD;
1042
3dd8012a
BN
1043 if (!use_top)
1044 status_new |= SR_TB;
1045
4c0dba44
BN
1046 /* Don't bother if they're the same */
1047 if (status_new == status_old)
1048 return 0;
1049
b199489d 1050 /* Only modify protection if it will not unlock other areas */
4c0dba44 1051 if ((status_new & mask) < (status_old & mask))
62593cf4 1052 return -EINVAL;
b199489d 1053
2666067f 1054 return write_sr_and_check(nor, status_new, mask);
b199489d
HS
1055}
1056
62593cf4
BN
1057/*
1058 * Unlock a region of the flash. See stm_lock() for more info
1059 *
1060 * Returns negative on errors, 0 on success.
1061 */
8cc7f33a 1062static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
b199489d 1063{
19763671 1064 struct mtd_info *mtd = &nor->mtd;
f49289ce 1065 int status_old, status_new;
62593cf4
BN
1066 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1067 u8 shift = ffs(mask) - 1, pow, val;
f8860802 1068 loff_t lock_len;
3dd8012a
BN
1069 bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1070 bool use_top;
b199489d 1071
b199489d 1072 status_old = read_sr(nor);
f49289ce
FE
1073 if (status_old < 0)
1074 return status_old;
b199489d 1075
f8860802
BN
1076 /* If nothing in our range is locked, we don't need to do anything */
1077 if (stm_is_unlocked_sr(nor, ofs, len, status_old))
1078 return 0;
1079
1080 /* If anything below us is locked, we can't use 'top' protection */
1081 if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
3dd8012a
BN
1082 can_be_top = false;
1083
1084 /* If anything above us is locked, we can't use 'bottom' protection */
1085 if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
1086 status_old))
1087 can_be_bottom = false;
1088
1089 if (!can_be_bottom && !can_be_top)
62593cf4 1090 return -EINVAL;
b199489d 1091
3dd8012a
BN
1092 /* Prefer top, if both are valid */
1093 use_top = can_be_top;
1094
f8860802 1095 /* lock_len: length of region that should remain locked */
3dd8012a
BN
1096 if (use_top)
1097 lock_len = mtd->size - (ofs + len);
1098 else
1099 lock_len = ofs;
f8860802 1100
62593cf4
BN
1101 /*
1102 * Need largest pow such that:
1103 *
1104 * 1 / (2^pow) >= (len / size)
1105 *
1106 * so (assuming power-of-2 size) we do:
1107 *
1108 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
1109 */
f8860802
BN
1110 pow = ilog2(mtd->size) - order_base_2(lock_len);
1111 if (lock_len == 0) {
62593cf4
BN
1112 val = 0; /* fully unlocked */
1113 } else {
1114 val = mask - (pow << shift);
1115 /* Some power-of-two sizes are not supported */
1116 if (val & ~mask)
1117 return -EINVAL;
b199489d
HS
1118 }
1119
3dd8012a 1120 status_new = (status_old & ~mask & ~SR_TB) | val;
62593cf4 1121
47b8edbf 1122 /* Don't protect status register if we're fully unlocked */
06586204 1123 if (lock_len == 0)
47b8edbf
BN
1124 status_new &= ~SR_SRWD;
1125
3dd8012a
BN
1126 if (!use_top)
1127 status_new |= SR_TB;
1128
4c0dba44
BN
1129 /* Don't bother if they're the same */
1130 if (status_new == status_old)
1131 return 0;
1132
62593cf4 1133 /* Only modify protection if it will not lock other areas */
4c0dba44 1134 if ((status_new & mask) > (status_old & mask))
62593cf4
BN
1135 return -EINVAL;
1136
2666067f 1137 return write_sr_and_check(nor, status_new, mask);
8cc7f33a
BN
1138}
1139
5bf0e69b
BN
1140/*
1141 * Check if a region of the flash is (completely) locked. See stm_lock() for
1142 * more info.
1143 *
1144 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
1145 * negative on errors.
1146 */
1147static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
1148{
1149 int status;
1150
1151 status = read_sr(nor);
1152 if (status < 0)
1153 return status;
1154
1155 return stm_is_locked_sr(nor, ofs, len, status);
1156}
1157
8cc7f33a
BN
1158static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1159{
1160 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1161 int ret;
1162
1163 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
1164 if (ret)
1165 return ret;
1166
1167 ret = nor->flash_lock(nor, ofs, len);
1168
b199489d
HS
1169 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
1170 return ret;
1171}
1172
8cc7f33a
BN
1173static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1174{
1175 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1176 int ret;
1177
1178 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
1179 if (ret)
1180 return ret;
1181
1182 ret = nor->flash_unlock(nor, ofs, len);
1183
1184 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
1185 return ret;
1186}
1187
5bf0e69b
BN
1188static int spi_nor_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
1189{
1190 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1191 int ret;
1192
1193 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
1194 if (ret)
1195 return ret;
1196
1197 ret = nor->flash_is_locked(nor, ofs, len);
1198
1199 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
1200 return ret;
1201}
1202
65153846
AY
1203static int macronix_quad_enable(struct spi_nor *nor);
1204
09ffafb6 1205/* Used when the "_ext_id" is two bytes at most */
b199489d 1206#define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
09ffafb6
HS
1207 .id = { \
1208 ((_jedec_id) >> 16) & 0xff, \
1209 ((_jedec_id) >> 8) & 0xff, \
1210 (_jedec_id) & 0xff, \
1211 ((_ext_id) >> 8) & 0xff, \
1212 (_ext_id) & 0xff, \
1213 }, \
1214 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
b199489d
HS
1215 .sector_size = (_sector_size), \
1216 .n_sectors = (_n_sectors), \
1217 .page_size = 256, \
06bb6f5a 1218 .flags = (_flags),
b199489d 1219
6d7604e5 1220#define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
6d7604e5
HS
1221 .id = { \
1222 ((_jedec_id) >> 16) & 0xff, \
1223 ((_jedec_id) >> 8) & 0xff, \
1224 (_jedec_id) & 0xff, \
1225 ((_ext_id) >> 16) & 0xff, \
1226 ((_ext_id) >> 8) & 0xff, \
1227 (_ext_id) & 0xff, \
1228 }, \
1229 .id_len = 6, \
1230 .sector_size = (_sector_size), \
1231 .n_sectors = (_n_sectors), \
1232 .page_size = 256, \
06bb6f5a 1233 .flags = (_flags),
6d7604e5 1234
b199489d 1235#define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
b199489d
HS
1236 .sector_size = (_sector_size), \
1237 .n_sectors = (_n_sectors), \
1238 .page_size = (_page_size), \
1239 .addr_width = (_addr_width), \
06bb6f5a 1240 .flags = (_flags),
b199489d 1241
e99ca98f
RR
1242#define S3AN_INFO(_jedec_id, _n_sectors, _page_size) \
1243 .id = { \
1244 ((_jedec_id) >> 16) & 0xff, \
1245 ((_jedec_id) >> 8) & 0xff, \
1246 (_jedec_id) & 0xff \
1247 }, \
1248 .id_len = 3, \
1249 .sector_size = (8*_page_size), \
1250 .n_sectors = (_n_sectors), \
1251 .page_size = _page_size, \
1252 .addr_width = 3, \
1253 .flags = SPI_NOR_NO_FR | SPI_S3AN,
1254
b199489d
HS
1255/* NOTE: double check command sets and memory organization when you add
1256 * more nor chips. This current list focusses on newer chips, which
1257 * have been converging on command sets which including JEDEC ID.
c19900ed
RM
1258 *
1259 * All newly added entries should describe *hardware* and should use SECT_4K
1260 * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
1261 * scenarios excluding small sectors there is config option that can be
1262 * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
1263 * For historical (and compatibility) reasons (before we got above config) some
1264 * old entries may be missing 4K flag.
b199489d 1265 */
06bb6f5a 1266static const struct flash_info spi_nor_ids[] = {
b199489d
HS
1267 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
1268 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
1269 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
1270
1271 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
b08618c9 1272 { "at25df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
b199489d
HS
1273 { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
1274 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
1275
1276 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
1277 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
1278 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
1279 { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
1280
1281 { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
1282
1283 /* EON -- en25xxx */
1284 { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
1285 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
1286 { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
1287 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
1288 { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
771ff17e 1289 { "en25qh32", INFO(0x1c7016, 0, 64 * 1024, 64, 0) },
a41595b3 1290 { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
b199489d 1291 { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
c19900ed 1292 { "en25s64", INFO(0x1c3817, 0, 64 * 1024, 128, SECT_4K) },
b199489d
HS
1293
1294 /* ESMT */
fcf690a2 1295 { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_HAS_LOCK) },
ca1fa1a8
P
1296 { "f25l32qa", INFO(0x8c4116, 0, 64 * 1024, 64, SECT_4K | SPI_NOR_HAS_LOCK) },
1297 { "f25l64qa", INFO(0x8c4117, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_HAS_LOCK) },
b199489d
HS
1298
1299 /* Everspin */
282e45dc 1300 { "mr25h128", CAT25_INFO( 16 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
b199489d
HS
1301 { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1302 { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
edd0c8f4 1303 { "mr25h40", CAT25_INFO(512 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
b199489d 1304
ce56ce7d
RL
1305 /* Fujitsu */
1306 { "mb85rs1mt", INFO(0x047f27, 0, 128 * 1024, 1, SPI_NOR_NO_ERASE) },
1307
b199489d 1308 /* GigaDevice */
e9cf64de
KD
1309 {
1310 "gd25q16", INFO(0xc84015, 0, 64 * 1024, 32,
1311 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1312 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1313 },
595f0e10
BN
1314 {
1315 "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64,
1316 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1317 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1318 },
5a068283
KG
1319 {
1320 "gd25lq32", INFO(0xc86016, 0, 64 * 1024, 64,
1321 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1322 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1323 },
595f0e10
BN
1324 {
1325 "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128,
1326 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1327 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1328 },
1329 {
1330 "gd25lq64c", INFO(0xc86017, 0, 64 * 1024, 128,
1331 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1332 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1333 },
1334 {
1335 "gd25q128", INFO(0xc84018, 0, 64 * 1024, 256,
1336 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1337 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1338 },
65153846
AY
1339 {
1340 "gd25q256", INFO(0xc84019, 0, 64 * 1024, 512,
1341 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1342 SPI_NOR_4B_OPCODES | SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1343 .quad_enable = macronix_quad_enable,
1344 },
b199489d
HS
1345
1346 /* Intel/Numonyx -- xxxs33b */
1347 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
1348 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
1349 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
1350
b79c332f 1351 /* ISSI */
29d6b29f
SN
1352 { "is25cd512", INFO(0x7f9d20, 0, 32 * 1024, 2, SECT_4K) },
1353 { "is25lq040b", INFO(0x9d4013, 0, 64 * 1024, 8,
ded8a044 1354 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
17407ec3
RP
1355 { "is25lp080d", INFO(0x9d6014, 0, 64 * 1024, 16,
1356 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
29d6b29f 1357 { "is25lp128", INFO(0x9d6018, 0, 64 * 1024, 256,
34354d4b 1358 SECT_4K | SPI_NOR_DUAL_READ) },
c7aa1b77
MV
1359 { "is25lp256", INFO(0x9d6019, 0, 64 * 1024, 512,
1360 SECT_4K | SPI_NOR_DUAL_READ) },
d616f81c
KR
1361 { "is25wp032", INFO(0x9d7016, 0, 64 * 1024, 64,
1362 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1363 { "is25wp064", INFO(0x9d7017, 0, 64 * 1024, 128,
1364 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1365 { "is25wp128", INFO(0x9d7018, 0, 64 * 1024, 256,
1366 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
b79c332f 1367
b199489d 1368 /* Macronix */
660b5b07 1369 { "mx25l512e", INFO(0xc22010, 0, 64 * 1024, 1, SECT_4K) },
b199489d
HS
1370 { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
1371 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
1372 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
1373 { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
0501f2e5 1374 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, SECT_4K) },
b199489d 1375 { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
0501f2e5 1376 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K) },
9f3cd453
AK
1377 { "mx25u2033e", INFO(0xc22532, 0, 64 * 1024, 4, SECT_4K) },
1378 { "mx25u4035", INFO(0xc22533, 0, 64 * 1024, 8, SECT_4K) },
1379 { "mx25u8035", INFO(0xc22534, 0, 64 * 1024, 16, SECT_4K) },
81a1209c 1380 { "mx25u6435f", INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
b199489d
HS
1381 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
1382 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
d7c9ade2 1383 { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
b0fcb4b4 1384 { "mx25u25635f", INFO(0xc22539, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_4B_OPCODES) },
b199489d 1385 { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
d342b6a9 1386 { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
af18ba48 1387 { "mx66u51235f", INFO(0xc2253a, 0, 64 * 1024, 1024, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
ce398a81 1388 { "mx66l1g45g", INFO(0xc2201b, 0, 64 * 1024, 2048, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
b199489d
HS
1389 { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
1390
1391 /* Micron */
61e46118 1392 { "n25q016a", INFO(0x20bb15, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_QUAD_READ) },
548cd3ab 1393 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
f9bcb6dc 1394 { "n25q032a", INFO(0x20bb16, 0, 64 * 1024, 64, SPI_NOR_QUAD_READ) },
0db7fae2 1395 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
2a06c7b1 1396 { "n25q064a", INFO(0x20bb17, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_QUAD_READ) },
4607777c
EG
1397 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
1398 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_QUAD_READ) },
d7c9ade2 1399 { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
835ed7bf 1400 { "n25q256ax1", INFO(0x20bb19, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_QUAD_READ) },
548cd3ab
BH
1401 { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
1402 { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
193fb3c1 1403 { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
1404 { "n25q00a", INFO(0x20bb21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
56c6855c 1405 { "mt25qu02g", INFO(0x20bb22, 0, 64 * 1024, 4096, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
b199489d
HS
1406
1407 /* PMC */
1408 { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
1409 { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
1410 { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
1411
0074a8f3 1412 /* Spansion/Cypress -- single (large) sector size only, at least
b199489d
HS
1413 * for the chips listed here (without boot sectors).
1414 */
9ab86995 1415 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
0f12a27b 1416 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
c4b3eacc
AS
1417 { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, USE_CLSR) },
1418 { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1419 { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
b199489d
HS
1420 { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
1421 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
1422 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
c4b3eacc
AS
1423 { "s25fl128s", INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1424 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1425 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
b199489d
HS
1426 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
1427 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
1428 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
1429 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
1430 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
7c748f57 1431 { "s25fl004k", INFO(0xef4013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
adf508c3
JE
1432 { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1433 { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
b199489d 1434 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
c0826679 1435 { "s25fl116k", INFO(0x014015, 0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
c19900ed 1436 { "s25fl132k", INFO(0x014016, 0, 64 * 1024, 64, SECT_4K) },
413780d7 1437 { "s25fl164k", INFO(0x014017, 0, 64 * 1024, 128, SECT_4K) },
aada20cd 1438 { "s25fl204k", INFO(0x014013, 0, 64 * 1024, 8, SECT_4K | SPI_NOR_DUAL_READ) },
022a400f 1439 { "s25fl208k", INFO(0x014014, 0, 64 * 1024, 16, SECT_4K | SPI_NOR_DUAL_READ) },
d8b494a3 1440 { "s25fl064l", INFO(0x016017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
0074a8f3
RG
1441 { "s25fl128l", INFO(0x016018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
1442 { "s25fl256l", INFO(0x016019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
b199489d
HS
1443
1444 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
1445 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
1446 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
1447 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
1448 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
1449 { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
1450 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
1451 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
1452 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
a1d97ef9 1453 { "sst25wf020a", INFO(0x621612, 0, 64 * 1024, 4, SECT_4K) },
c887be71 1454 { "sst25wf040b", INFO(0x621613, 0, 64 * 1024, 8, SECT_4K) },
b199489d 1455 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
f02985b7 1456 { "sst25wf080", INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
18f7ce2f 1457 { "sst26vf064b", INFO(0xbf2643, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
b199489d
HS
1458
1459 /* ST Microelectronics -- newer production may have feature updates */
1460 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
1461 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
1462 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
1463 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
1464 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
1465 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
1466 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
1467 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
1468 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
b199489d
HS
1469
1470 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
1471 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
1472 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
1473 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
1474 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
1475 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
1476 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
1477 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
1478 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
1479
1480 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
1481 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
1482 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
1483
1484 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
1485 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
1486 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
1487
1488 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
1489 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
1490 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
1491 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
1492 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
f2fabe16 1493 { "m25px80", INFO(0x207114, 0, 64 * 1024, 16, 0) },
b199489d
HS
1494
1495 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
40d19ab6 1496 { "w25x05", INFO(0xef3010, 0, 64 * 1024, 1, SECT_4K) },
b199489d
HS
1497 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
1498 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
1499 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
1500 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
1501 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
ace3cbdd
NA
1502 {
1503 "w25q16dw", INFO(0xef6015, 0, 64 * 1024, 32,
1504 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1505 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1506 },
b199489d 1507 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
34fc99db
AK
1508 { "w25q20cl", INFO(0xef4012, 0, 64 * 1024, 4, SECT_4K) },
1509 { "w25q20bw", INFO(0xef5012, 0, 64 * 1024, 4, SECT_4K) },
1510 { "w25q20ew", INFO(0xef6012, 0, 64 * 1024, 4, SECT_4K) },
b199489d 1511 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
9648388f
BN
1512 {
1513 "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64,
1514 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1515 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1516 },
7fccf56e
SD
1517 {
1518 "w25q32jv", INFO(0xef7016, 0, 64 * 1024, 64,
1519 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1520 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1521 },
b199489d
HS
1522 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
1523 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
9648388f
BN
1524 {
1525 "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
1526 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1527 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1528 },
1529 {
1530 "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
1531 SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1532 SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1533 },
b199489d
HS
1534 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
1535 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
1536 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
d7c9ade2 1537 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
0cbef932
BH
1538 { "w25m512jv", INFO(0xef7119, 0, 64 * 1024, 1024,
1539 SECT_4K | SPI_NOR_QUAD_READ | SPI_NOR_DUAL_READ) },
b199489d
HS
1540
1541 /* Catalyst / On Semiconductor -- non-JEDEC */
1542 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1543 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1544 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1545 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
1546 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
e99ca98f
RR
1547
1548 /* Xilinx S3AN Internal Flash */
1549 { "3S50AN", S3AN_INFO(0x1f2200, 64, 264) },
1550 { "3S200AN", S3AN_INFO(0x1f2400, 256, 264) },
1551 { "3S400AN", S3AN_INFO(0x1f2400, 256, 264) },
1552 { "3S700AN", S3AN_INFO(0x1f2500, 512, 264) },
1553 { "3S1400AN", S3AN_INFO(0x1f2600, 512, 528) },
ce5013ff
HM
1554
1555 /* XMC (Wuhan Xinxin Semiconductor Manufacturing Corp.) */
1556 { "XM25QH64A", INFO(0x207017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1557 { "XM25QH128A", INFO(0x207018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
b199489d
HS
1558 { },
1559};
1560
06bb6f5a 1561static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
b199489d
HS
1562{
1563 int tmp;
09ffafb6 1564 u8 id[SPI_NOR_MAX_ID_LEN];
06bb6f5a 1565 const struct flash_info *info;
b199489d 1566
09ffafb6 1567 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
b199489d 1568 if (tmp < 0) {
20625dfe 1569 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
b199489d
HS
1570 return ERR_PTR(tmp);
1571 }
b199489d
HS
1572
1573 for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
06bb6f5a 1574 info = &spi_nor_ids[tmp];
09ffafb6
HS
1575 if (info->id_len) {
1576 if (!memcmp(info->id, id, info->id_len))
b199489d
HS
1577 return &spi_nor_ids[tmp];
1578 }
1579 }
9b9f1033 1580 dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
09ffafb6 1581 id[0], id[1], id[2]);
b199489d
HS
1582 return ERR_PTR(-ENODEV);
1583}
1584
b199489d
HS
1585static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
1586 size_t *retlen, u_char *buf)
1587{
1588 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1589 int ret;
1590
1591 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
1592
1593 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
1594 if (ret)
1595 return ret;
1596
26f9bcad 1597 while (len) {
e99ca98f
RR
1598 loff_t addr = from;
1599
1600 if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
1601 addr = spi_nor_s3an_addr_convert(nor, addr);
1602
1603 ret = nor->read(nor, addr, len, buf);
26f9bcad
MS
1604 if (ret == 0) {
1605 /* We shouldn't see 0-length reads */
1606 ret = -EIO;
1607 goto read_err;
1608 }
1609 if (ret < 0)
1610 goto read_err;
b199489d 1611
26f9bcad
MS
1612 WARN_ON(ret > len);
1613 *retlen += ret;
1614 buf += ret;
1615 from += ret;
1616 len -= ret;
1617 }
1618 ret = 0;
59451e12 1619
26f9bcad
MS
1620read_err:
1621 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
1622 return ret;
b199489d
HS
1623}
1624
1625static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
1626 size_t *retlen, const u_char *buf)
1627{
1628 struct spi_nor *nor = mtd_to_spi_nor(mtd);
1629 size_t actual;
1630 int ret;
1631
1632 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1633
1634 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
1635 if (ret)
1636 return ret;
1637
b199489d
HS
1638 write_enable(nor);
1639
1640 nor->sst_write_second = false;
1641
1642 actual = to % 2;
1643 /* Start write from odd address. */
1644 if (actual) {
b02e7f3e 1645 nor->program_opcode = SPINOR_OP_BP;
b199489d
HS
1646
1647 /* write one byte. */
2dd087b1 1648 ret = nor->write(nor, to, 1, buf);
0bad7b93
MS
1649 if (ret < 0)
1650 goto sst_write_err;
1651 WARN(ret != 1, "While writing 1 byte written %i bytes\n",
1652 (int)ret);
b94ed087 1653 ret = spi_nor_wait_till_ready(nor);
b199489d 1654 if (ret)
0bad7b93 1655 goto sst_write_err;
b199489d
HS
1656 }
1657 to += actual;
1658
1659 /* Write out most of the data here. */
1660 for (; actual < len - 1; actual += 2) {
b02e7f3e 1661 nor->program_opcode = SPINOR_OP_AAI_WP;
b199489d
HS
1662
1663 /* write two bytes. */
2dd087b1 1664 ret = nor->write(nor, to, 2, buf + actual);
0bad7b93
MS
1665 if (ret < 0)
1666 goto sst_write_err;
1667 WARN(ret != 2, "While writing 2 bytes written %i bytes\n",
1668 (int)ret);
b94ed087 1669 ret = spi_nor_wait_till_ready(nor);
b199489d 1670 if (ret)
0bad7b93 1671 goto sst_write_err;
b199489d
HS
1672 to += 2;
1673 nor->sst_write_second = true;
1674 }
1675 nor->sst_write_second = false;
1676
1677 write_disable(nor);
b94ed087 1678 ret = spi_nor_wait_till_ready(nor);
b199489d 1679 if (ret)
0bad7b93 1680 goto sst_write_err;
b199489d
HS
1681
1682 /* Write out trailing byte if it exists. */
1683 if (actual != len) {
1684 write_enable(nor);
1685
b02e7f3e 1686 nor->program_opcode = SPINOR_OP_BP;
2dd087b1 1687 ret = nor->write(nor, to, 1, buf + actual);
0bad7b93
MS
1688 if (ret < 0)
1689 goto sst_write_err;
1690 WARN(ret != 1, "While writing 1 byte written %i bytes\n",
1691 (int)ret);
b94ed087 1692 ret = spi_nor_wait_till_ready(nor);
b199489d 1693 if (ret)
0bad7b93 1694 goto sst_write_err;
b199489d 1695 write_disable(nor);
2dd087b1 1696 actual += 1;
b199489d 1697 }
0bad7b93 1698sst_write_err:
2dd087b1 1699 *retlen += actual;
b199489d
HS
1700 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
1701 return ret;
1702}
1703
1704/*
1705 * Write an address range to the nor chip. Data must be written in
1706 * FLASH_PAGESIZE chunks. The address range may be any size provided
1707 * it is within the physical boundaries.
1708 */
1709static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1710 size_t *retlen, const u_char *buf)
1711{
1712 struct spi_nor *nor = mtd_to_spi_nor(mtd);
e5d05cbd
MS
1713 size_t page_offset, page_remain, i;
1714 ssize_t ret;
b199489d
HS
1715
1716 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1717
1718 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
1719 if (ret)
1720 return ret;
1721
e5d05cbd
MS
1722 for (i = 0; i < len; ) {
1723 ssize_t written;
e99ca98f 1724 loff_t addr = to + i;
b199489d 1725
e99ca98f
RR
1726 /*
1727 * If page_size is a power of two, the offset can be quickly
1728 * calculated with an AND operation. On the other cases we
1729 * need to do a modulus operation (more expensive).
1730 * Power of two numbers have only one bit set and we can use
1731 * the instruction hweight32 to detect if we need to do a
1732 * modulus (do_div()) or not.
1733 */
1734 if (hweight32(nor->page_size) == 1) {
1735 page_offset = addr & (nor->page_size - 1);
1736 } else {
1737 uint64_t aux = addr;
b199489d 1738
e99ca98f
RR
1739 page_offset = do_div(aux, nor->page_size);
1740 }
b199489d 1741 /* the size of data remaining on the first page */
e5d05cbd
MS
1742 page_remain = min_t(size_t,
1743 nor->page_size - page_offset, len - i);
1744
e99ca98f
RR
1745 if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
1746 addr = spi_nor_s3an_addr_convert(nor, addr);
1747
e5d05cbd 1748 write_enable(nor);
e99ca98f 1749 ret = nor->write(nor, addr, page_remain, buf + i);
0bad7b93
MS
1750 if (ret < 0)
1751 goto write_err;
e5d05cbd 1752 written = ret;
1d61dcb3 1753
e5d05cbd
MS
1754 ret = spi_nor_wait_till_ready(nor);
1755 if (ret)
1756 goto write_err;
1757 *retlen += written;
1758 i += written;
b199489d
HS
1759 }
1760
1761write_err:
1762 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
1d61dcb3 1763 return ret;
b199489d
HS
1764}
1765
f384b352
CP
1766/**
1767 * macronix_quad_enable() - set QE bit in Status Register.
1768 * @nor: pointer to a 'struct spi_nor'
1769 *
1770 * Set the Quad Enable (QE) bit in the Status Register.
1771 *
1772 * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
1773 *
1774 * Return: 0 on success, -errno otherwise.
1775 */
b199489d
HS
1776static int macronix_quad_enable(struct spi_nor *nor)
1777{
1778 int ret, val;
1779
1780 val = read_sr(nor);
f49289ce
FE
1781 if (val < 0)
1782 return val;
32c90f16
CP
1783 if (val & SR_QUAD_EN_MX)
1784 return 0;
1785
b199489d
HS
1786 write_enable(nor);
1787
fd725234 1788 write_sr(nor, val | SR_QUAD_EN_MX);
b199489d 1789
05d090f0
DC
1790 ret = spi_nor_wait_till_ready(nor);
1791 if (ret)
1792 return ret;
b199489d
HS
1793
1794 ret = read_sr(nor);
1795 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
1796 dev_err(nor->dev, "Macronix Quad bit not set\n");
1797 return -EINVAL;
1798 }
1799
1800 return 0;
1801}
1802
1803/*
1804 * Write status Register and configuration register with 2 bytes
1805 * The first byte will be written to the status register, while the
1806 * second byte will be written to the configuration register.
5d708ecc 1807 * Return negative if error occurred.
b199489d 1808 */
f384b352 1809static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
b199489d
HS
1810{
1811 int ret;
b199489d
HS
1812
1813 write_enable(nor);
1814
f384b352 1815 ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
b199489d
HS
1816 if (ret < 0) {
1817 dev_err(nor->dev,
1818 "error while writing configuration register\n");
1819 return -EINVAL;
1820 }
1821
807c1625
JE
1822 ret = spi_nor_wait_till_ready(nor);
1823 if (ret) {
1824 dev_err(nor->dev,
1825 "timeout while writing configuration register\n");
1826 return ret;
1827 }
1828
f384b352
CP
1829 return 0;
1830}
1831
1832/**
1833 * spansion_quad_enable() - set QE bit in Configuraiton Register.
1834 * @nor: pointer to a 'struct spi_nor'
1835 *
1836 * Set the Quad Enable (QE) bit in the Configuration Register.
1837 * This function is kept for legacy purpose because it has been used for a
1838 * long time without anybody complaining but it should be considered as
1839 * deprecated and maybe buggy.
1840 * First, this function doesn't care about the previous values of the Status
1841 * and Configuration Registers when it sets the QE bit (bit 1) in the
1842 * Configuration Register: all other bits are cleared, which may have unwanted
1843 * side effects like removing some block protections.
1844 * Secondly, it uses the Read Configuration Register (35h) instruction though
1845 * some very old and few memories don't support this instruction. If a pull-up
1846 * resistor is present on the MISO/IO1 line, we might still be able to pass the
1847 * "read back" test because the QSPI memory doesn't recognize the command,
1848 * so leaves the MISO/IO1 line state unchanged, hence read_cr() returns 0xFF.
1849 *
1850 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1851 * memories.
1852 *
1853 * Return: 0 on success, -errno otherwise.
1854 */
1855static int spansion_quad_enable(struct spi_nor *nor)
1856{
1857 u8 sr_cr[2] = {0, CR_QUAD_EN_SPAN};
1858 int ret;
1859
1860 ret = write_sr_cr(nor, sr_cr);
1861 if (ret)
1862 return ret;
1863
b199489d
HS
1864 /* read back and check it */
1865 ret = read_cr(nor);
1866 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1867 dev_err(nor->dev, "Spansion Quad bit not set\n");
1868 return -EINVAL;
1869 }
1870
1871 return 0;
1872}
1873
f384b352
CP
1874/**
1875 * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
1876 * @nor: pointer to a 'struct spi_nor'
1877 *
1878 * Set the Quad Enable (QE) bit in the Configuration Register.
1879 * This function should be used with QSPI memories not supporting the Read
1880 * Configuration Register (35h) instruction.
1881 *
1882 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1883 * memories.
1884 *
1885 * Return: 0 on success, -errno otherwise.
1886 */
1887static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
1888{
1889 u8 sr_cr[2];
1890 int ret;
1891
1892 /* Keep the current value of the Status Register. */
1893 ret = read_sr(nor);
1894 if (ret < 0) {
1895 dev_err(nor->dev, "error while reading status register\n");
1896 return -EINVAL;
1897 }
1898 sr_cr[0] = ret;
1899 sr_cr[1] = CR_QUAD_EN_SPAN;
1900
1901 return write_sr_cr(nor, sr_cr);
1902}
1903
1904/**
1905 * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
1906 * @nor: pointer to a 'struct spi_nor'
1907 *
1908 * Set the Quad Enable (QE) bit in the Configuration Register.
1909 * This function should be used with QSPI memories supporting the Read
1910 * Configuration Register (35h) instruction.
1911 *
1912 * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1913 * memories.
1914 *
1915 * Return: 0 on success, -errno otherwise.
1916 */
1917static int spansion_read_cr_quad_enable(struct spi_nor *nor)
1918{
1919 struct device *dev = nor->dev;
1920 u8 sr_cr[2];
1921 int ret;
1922
1923 /* Check current Quad Enable bit value. */
1924 ret = read_cr(nor);
1925 if (ret < 0) {
1926 dev_err(dev, "error while reading configuration register\n");
1927 return -EINVAL;
1928 }
1929
1930 if (ret & CR_QUAD_EN_SPAN)
1931 return 0;
1932
1933 sr_cr[1] = ret | CR_QUAD_EN_SPAN;
1934
1935 /* Keep the current value of the Status Register. */
1936 ret = read_sr(nor);
1937 if (ret < 0) {
1938 dev_err(dev, "error while reading status register\n");
1939 return -EINVAL;
1940 }
1941 sr_cr[0] = ret;
1942
1943 ret = write_sr_cr(nor, sr_cr);
1944 if (ret)
1945 return ret;
1946
1947 /* Read back and check it. */
1948 ret = read_cr(nor);
1949 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1950 dev_err(nor->dev, "Spansion Quad bit not set\n");
1951 return -EINVAL;
1952 }
1953
1954 return 0;
1955}
1956
1957/**
1958 * sr2_bit7_quad_enable() - set QE bit in Status Register 2.
1959 * @nor: pointer to a 'struct spi_nor'
1960 *
1961 * Set the Quad Enable (QE) bit in the Status Register 2.
1962 *
1963 * This is one of the procedures to set the QE bit described in the SFDP
1964 * (JESD216 rev B) specification but no manufacturer using this procedure has
1965 * been identified yet, hence the name of the function.
1966 *
1967 * Return: 0 on success, -errno otherwise.
1968 */
1969static int sr2_bit7_quad_enable(struct spi_nor *nor)
1970{
1971 u8 sr2;
1972 int ret;
1973
1974 /* Check current Quad Enable bit value. */
1975 ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
1976 if (ret)
1977 return ret;
1978 if (sr2 & SR2_QUAD_EN_BIT7)
1979 return 0;
1980
1981 /* Update the Quad Enable bit. */
1982 sr2 |= SR2_QUAD_EN_BIT7;
1983
1984 write_enable(nor);
1985
1986 ret = nor->write_reg(nor, SPINOR_OP_WRSR2, &sr2, 1);
1987 if (ret < 0) {
1988 dev_err(nor->dev, "error while writing status register 2\n");
1989 return -EINVAL;
1990 }
1991
1992 ret = spi_nor_wait_till_ready(nor);
1993 if (ret < 0) {
1994 dev_err(nor->dev, "timeout while writing status register 2\n");
1995 return ret;
1996 }
1997
1998 /* Read back and check it. */
1999 ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
2000 if (!(ret > 0 && (sr2 & SR2_QUAD_EN_BIT7))) {
2001 dev_err(nor->dev, "SR2 Quad bit not set\n");
2002 return -EINVAL;
2003 }
2004
2005 return 0;
2006}
2007
b199489d
HS
2008static int spi_nor_check(struct spi_nor *nor)
2009{
2010 if (!nor->dev || !nor->read || !nor->write ||
c67cbb83 2011 !nor->read_reg || !nor->write_reg) {
b199489d
HS
2012 pr_err("spi-nor: please fill all the necessary fields!\n");
2013 return -EINVAL;
2014 }
2015
b199489d
HS
2016 return 0;
2017}
2018
e99ca98f
RR
2019static int s3an_nor_scan(const struct flash_info *info, struct spi_nor *nor)
2020{
2021 int ret;
2022 u8 val;
2023
2024 ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
2025 if (ret < 0) {
2026 dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
2027 return ret;
2028 }
2029
2030 nor->erase_opcode = SPINOR_OP_XSE;
2031 nor->program_opcode = SPINOR_OP_XPP;
2032 nor->read_opcode = SPINOR_OP_READ;
2033 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
2034
2035 /*
2036 * This flashes have a page size of 264 or 528 bytes (known as
2037 * Default addressing mode). It can be changed to a more standard
2038 * Power of two mode where the page size is 256/512. This comes
2039 * with a price: there is 3% less of space, the data is corrupted
2040 * and the page size cannot be changed back to default addressing
2041 * mode.
2042 *
2043 * The current addressing mode can be read from the XRDSR register
2044 * and should not be changed, because is a destructive operation.
2045 */
2046 if (val & XSR_PAGESIZE) {
2047 /* Flash in Power of 2 mode */
2048 nor->page_size = (nor->page_size == 264) ? 256 : 512;
2049 nor->mtd.writebufsize = nor->page_size;
2050 nor->mtd.size = 8 * nor->page_size * info->n_sectors;
2051 nor->mtd.erasesize = 8 * nor->page_size;
2052 } else {
2053 /* Flash in Default addressing mode */
2054 nor->flags |= SNOR_F_S3AN_ADDR_DEFAULT;
2055 }
2056
2057 return 0;
2058}
2059
cfc5604c
CP
2060struct spi_nor_read_command {
2061 u8 num_mode_clocks;
2062 u8 num_wait_states;
2063 u8 opcode;
2064 enum spi_nor_protocol proto;
2065};
2066
2067struct spi_nor_pp_command {
2068 u8 opcode;
2069 enum spi_nor_protocol proto;
2070};
2071
2072enum spi_nor_read_command_index {
2073 SNOR_CMD_READ,
2074 SNOR_CMD_READ_FAST,
15f55331 2075 SNOR_CMD_READ_1_1_1_DTR,
cfc5604c
CP
2076
2077 /* Dual SPI */
2078 SNOR_CMD_READ_1_1_2,
2079 SNOR_CMD_READ_1_2_2,
2080 SNOR_CMD_READ_2_2_2,
15f55331 2081 SNOR_CMD_READ_1_2_2_DTR,
cfc5604c
CP
2082
2083 /* Quad SPI */
2084 SNOR_CMD_READ_1_1_4,
2085 SNOR_CMD_READ_1_4_4,
2086 SNOR_CMD_READ_4_4_4,
15f55331 2087 SNOR_CMD_READ_1_4_4_DTR,
cfc5604c 2088
fe488a5e
CP
2089 /* Octo SPI */
2090 SNOR_CMD_READ_1_1_8,
2091 SNOR_CMD_READ_1_8_8,
2092 SNOR_CMD_READ_8_8_8,
2093 SNOR_CMD_READ_1_8_8_DTR,
2094
cfc5604c
CP
2095 SNOR_CMD_READ_MAX
2096};
2097
2098enum spi_nor_pp_command_index {
2099 SNOR_CMD_PP,
2100
2101 /* Quad SPI */
2102 SNOR_CMD_PP_1_1_4,
2103 SNOR_CMD_PP_1_4_4,
2104 SNOR_CMD_PP_4_4_4,
2105
fe488a5e
CP
2106 /* Octo SPI */
2107 SNOR_CMD_PP_1_1_8,
2108 SNOR_CMD_PP_1_8_8,
2109 SNOR_CMD_PP_8_8_8,
2110
cfc5604c
CP
2111 SNOR_CMD_PP_MAX
2112};
2113
2114struct spi_nor_flash_parameter {
2115 u64 size;
2116 u32 page_size;
2117
2118 struct spi_nor_hwcaps hwcaps;
2119 struct spi_nor_read_command reads[SNOR_CMD_READ_MAX];
2120 struct spi_nor_pp_command page_programs[SNOR_CMD_PP_MAX];
2121
2122 int (*quad_enable)(struct spi_nor *nor);
2123};
2124
2125static void
2126spi_nor_set_read_settings(struct spi_nor_read_command *read,
2127 u8 num_mode_clocks,
2128 u8 num_wait_states,
2129 u8 opcode,
2130 enum spi_nor_protocol proto)
2131{
2132 read->num_mode_clocks = num_mode_clocks;
2133 read->num_wait_states = num_wait_states;
2134 read->opcode = opcode;
2135 read->proto = proto;
2136}
2137
2138static void
2139spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
2140 u8 opcode,
2141 enum spi_nor_protocol proto)
2142{
2143 pp->opcode = opcode;
2144 pp->proto = proto;
2145}
2146
f384b352
CP
2147/*
2148 * Serial Flash Discoverable Parameters (SFDP) parsing.
2149 */
2150
b038e8e3
TA
2151/**
2152 * spi_nor_read_raw() - raw read of serial flash memory. read_opcode,
2153 * addr_width and read_dummy members of the struct spi_nor
2154 * should be previously
2155 * set.
2156 * @nor: pointer to a 'struct spi_nor'
2157 * @addr: offset in the serial flash memory
2158 * @len: number of bytes to read
1d5ceff2 2159 * @buf: buffer where the data is copied into (dma-safe memory)
b038e8e3
TA
2160 *
2161 * Return: 0 on success, -errno otherwise.
2162 */
2163static int spi_nor_read_raw(struct spi_nor *nor, u32 addr, size_t len, u8 *buf)
2164{
2165 int ret;
2166
2167 while (len) {
2168 ret = nor->read(nor, addr, len, buf);
2169 if (!ret || ret > len)
2170 return -EIO;
2171 if (ret < 0)
2172 return ret;
2173
2174 buf += ret;
2175 addr += ret;
2176 len -= ret;
2177 }
2178 return 0;
2179}
2180
f384b352
CP
2181/**
2182 * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
2183 * @nor: pointer to a 'struct spi_nor'
2184 * @addr: offset in the SFDP area to start reading data from
2185 * @len: number of bytes to read
bfa41337 2186 * @buf: buffer where the SFDP data are copied into (dma-safe memory)
f384b352
CP
2187 *
2188 * Whatever the actual numbers of bytes for address and dummy cycles are
2189 * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
2190 * followed by a 3-byte address and 8 dummy clock cycles.
2191 *
2192 * Return: 0 on success, -errno otherwise.
2193 */
2194static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
2195 size_t len, void *buf)
2196{
2197 u8 addr_width, read_opcode, read_dummy;
2198 int ret;
2199
2200 read_opcode = nor->read_opcode;
2201 addr_width = nor->addr_width;
2202 read_dummy = nor->read_dummy;
2203
2204 nor->read_opcode = SPINOR_OP_RDSFDP;
2205 nor->addr_width = 3;
2206 nor->read_dummy = 8;
2207
b038e8e3 2208 ret = spi_nor_read_raw(nor, addr, len, buf);
f384b352 2209
f384b352
CP
2210 nor->read_opcode = read_opcode;
2211 nor->addr_width = addr_width;
2212 nor->read_dummy = read_dummy;
2213
2214 return ret;
2215}
2216
bfa41337
CP
2217/**
2218 * spi_nor_read_sfdp_dma_unsafe() - read Serial Flash Discoverable Parameters.
2219 * @nor: pointer to a 'struct spi_nor'
2220 * @addr: offset in the SFDP area to start reading data from
2221 * @len: number of bytes to read
2222 * @buf: buffer where the SFDP data are copied into
2223 *
2224 * Wrap spi_nor_read_sfdp() using a kmalloc'ed bounce buffer as @buf is now not
2225 * guaranteed to be dma-safe.
2226 *
2227 * Return: -ENOMEM if kmalloc() fails, the return code of spi_nor_read_sfdp()
2228 * otherwise.
2229 */
2230static int spi_nor_read_sfdp_dma_unsafe(struct spi_nor *nor, u32 addr,
2231 size_t len, void *buf)
2232{
2233 void *dma_safe_buf;
2234 int ret;
2235
2236 dma_safe_buf = kmalloc(len, GFP_KERNEL);
2237 if (!dma_safe_buf)
2238 return -ENOMEM;
2239
2240 ret = spi_nor_read_sfdp(nor, addr, len, dma_safe_buf);
2241 memcpy(buf, dma_safe_buf, len);
2242 kfree(dma_safe_buf);
2243
2244 return ret;
2245}
2246
f384b352
CP
2247struct sfdp_parameter_header {
2248 u8 id_lsb;
2249 u8 minor;
2250 u8 major;
2251 u8 length; /* in double words */
2252 u8 parameter_table_pointer[3]; /* byte address */
2253 u8 id_msb;
2254};
2255
2256#define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
2257#define SFDP_PARAM_HEADER_PTP(p) \
2258 (((p)->parameter_table_pointer[2] << 16) | \
2259 ((p)->parameter_table_pointer[1] << 8) | \
2260 ((p)->parameter_table_pointer[0] << 0))
2261
2262#define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
2263#define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
2264
2265#define SFDP_SIGNATURE 0x50444653U
2266#define SFDP_JESD216_MAJOR 1
2267#define SFDP_JESD216_MINOR 0
2268#define SFDP_JESD216A_MINOR 5
2269#define SFDP_JESD216B_MINOR 6
2270
2271struct sfdp_header {
2272 u32 signature; /* Ox50444653U <=> "SFDP" */
2273 u8 minor;
2274 u8 major;
2275 u8 nph; /* 0-base number of parameter headers */
2276 u8 unused;
2277
2278 /* Basic Flash Parameter Table. */
2279 struct sfdp_parameter_header bfpt_header;
2280};
2281
2282/* Basic Flash Parameter Table */
2283
2284/*
2285 * JESD216 rev B defines a Basic Flash Parameter Table of 16 DWORDs.
2286 * They are indexed from 1 but C arrays are indexed from 0.
2287 */
2288#define BFPT_DWORD(i) ((i) - 1)
2289#define BFPT_DWORD_MAX 16
2290
2291/* The first version of JESB216 defined only 9 DWORDs. */
2292#define BFPT_DWORD_MAX_JESD216 9
2293
2294/* 1st DWORD. */
2295#define BFPT_DWORD1_FAST_READ_1_1_2 BIT(16)
2296#define BFPT_DWORD1_ADDRESS_BYTES_MASK GENMASK(18, 17)
2297#define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY (0x0UL << 17)
2298#define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4 (0x1UL << 17)
2299#define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY (0x2UL << 17)
2300#define BFPT_DWORD1_DTR BIT(19)
2301#define BFPT_DWORD1_FAST_READ_1_2_2 BIT(20)
2302#define BFPT_DWORD1_FAST_READ_1_4_4 BIT(21)
2303#define BFPT_DWORD1_FAST_READ_1_1_4 BIT(22)
2304
2305/* 5th DWORD. */
2306#define BFPT_DWORD5_FAST_READ_2_2_2 BIT(0)
2307#define BFPT_DWORD5_FAST_READ_4_4_4 BIT(4)
2308
2309/* 11th DWORD. */
2310#define BFPT_DWORD11_PAGE_SIZE_SHIFT 4
2311#define BFPT_DWORD11_PAGE_SIZE_MASK GENMASK(7, 4)
2312
2313/* 15th DWORD. */
2314
2315/*
2316 * (from JESD216 rev B)
2317 * Quad Enable Requirements (QER):
2318 * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
2319 * reads based on instruction. DQ3/HOLD# functions are hold during
2320 * instruction phase.
2321 * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
2322 * two data bytes where bit 1 of the second byte is one.
2323 * [...]
2324 * Writing only one byte to the status register has the side-effect of
2325 * clearing status register 2, including the QE bit. The 100b code is
2326 * used if writing one byte to the status register does not modify
2327 * status register 2.
2328 * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
2329 * one data byte where bit 6 is one.
2330 * [...]
2331 * - 011b: QE is bit 7 of status register 2. It is set via Write status
2332 * register 2 instruction 3Eh with one data byte where bit 7 is one.
2333 * [...]
2334 * The status register 2 is read using instruction 3Fh.
2335 * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
2336 * two data bytes where bit 1 of the second byte is one.
2337 * [...]
2338 * In contrast to the 001b code, writing one byte to the status
2339 * register does not modify status register 2.
2340 * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
2341 * Read Status instruction 05h. Status register2 is read using
2342 * instruction 35h. QE is set via Writ Status instruction 01h with
2343 * two data bytes where bit 1 of the second byte is one.
2344 * [...]
2345 */
2346#define BFPT_DWORD15_QER_MASK GENMASK(22, 20)
2347#define BFPT_DWORD15_QER_NONE (0x0UL << 20) /* Micron */
2348#define BFPT_DWORD15_QER_SR2_BIT1_BUGGY (0x1UL << 20)
2349#define BFPT_DWORD15_QER_SR1_BIT6 (0x2UL << 20) /* Macronix */
2350#define BFPT_DWORD15_QER_SR2_BIT7 (0x3UL << 20)
2351#define BFPT_DWORD15_QER_SR2_BIT1_NO_RD (0x4UL << 20)
2352#define BFPT_DWORD15_QER_SR2_BIT1 (0x5UL << 20) /* Spansion */
2353
2354struct sfdp_bfpt {
2355 u32 dwords[BFPT_DWORD_MAX];
2356};
2357
2358/* Fast Read settings. */
2359
2360static inline void
2361spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
2362 u16 half,
2363 enum spi_nor_protocol proto)
2364{
2365 read->num_mode_clocks = (half >> 5) & 0x07;
2366 read->num_wait_states = (half >> 0) & 0x1f;
2367 read->opcode = (half >> 8) & 0xff;
2368 read->proto = proto;
2369}
2370
2371struct sfdp_bfpt_read {
2372 /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
2373 u32 hwcaps;
2374
2375 /*
2376 * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
2377 * whether the Fast Read x-y-z command is supported.
2378 */
2379 u32 supported_dword;
2380 u32 supported_bit;
2381
2382 /*
2383 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
2384 * encodes the op code, the number of mode clocks and the number of wait
2385 * states to be used by Fast Read x-y-z command.
2386 */
2387 u32 settings_dword;
2388 u32 settings_shift;
2389
2390 /* The SPI protocol for this Fast Read x-y-z command. */
2391 enum spi_nor_protocol proto;
2392};
2393
2394static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
2395 /* Fast Read 1-1-2 */
2396 {
2397 SNOR_HWCAPS_READ_1_1_2,
2398 BFPT_DWORD(1), BIT(16), /* Supported bit */
2399 BFPT_DWORD(4), 0, /* Settings */
2400 SNOR_PROTO_1_1_2,
2401 },
2402
2403 /* Fast Read 1-2-2 */
2404 {
2405 SNOR_HWCAPS_READ_1_2_2,
2406 BFPT_DWORD(1), BIT(20), /* Supported bit */
2407 BFPT_DWORD(4), 16, /* Settings */
2408 SNOR_PROTO_1_2_2,
2409 },
2410
2411 /* Fast Read 2-2-2 */
2412 {
2413 SNOR_HWCAPS_READ_2_2_2,
2414 BFPT_DWORD(5), BIT(0), /* Supported bit */
2415 BFPT_DWORD(6), 16, /* Settings */
2416 SNOR_PROTO_2_2_2,
2417 },
2418
2419 /* Fast Read 1-1-4 */
2420 {
2421 SNOR_HWCAPS_READ_1_1_4,
2422 BFPT_DWORD(1), BIT(22), /* Supported bit */
2423 BFPT_DWORD(3), 16, /* Settings */
2424 SNOR_PROTO_1_1_4,
2425 },
2426
2427 /* Fast Read 1-4-4 */
2428 {
2429 SNOR_HWCAPS_READ_1_4_4,
2430 BFPT_DWORD(1), BIT(21), /* Supported bit */
2431 BFPT_DWORD(3), 0, /* Settings */
2432 SNOR_PROTO_1_4_4,
2433 },
2434
2435 /* Fast Read 4-4-4 */
2436 {
2437 SNOR_HWCAPS_READ_4_4_4,
2438 BFPT_DWORD(5), BIT(4), /* Supported bit */
2439 BFPT_DWORD(7), 16, /* Settings */
2440 SNOR_PROTO_4_4_4,
2441 },
2442};
2443
2444struct sfdp_bfpt_erase {
2445 /*
2446 * The half-word at offset <shift> in DWORD <dwoard> encodes the
2447 * op code and erase sector size to be used by Sector Erase commands.
2448 */
2449 u32 dword;
2450 u32 shift;
2451};
2452
2453static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
2454 /* Erase Type 1 in DWORD8 bits[15:0] */
2455 {BFPT_DWORD(8), 0},
2456
2457 /* Erase Type 2 in DWORD8 bits[31:16] */
2458 {BFPT_DWORD(8), 16},
2459
2460 /* Erase Type 3 in DWORD9 bits[15:0] */
2461 {BFPT_DWORD(9), 0},
2462
2463 /* Erase Type 4 in DWORD9 bits[31:16] */
2464 {BFPT_DWORD(9), 16},
2465};
2466
2467static int spi_nor_hwcaps_read2cmd(u32 hwcaps);
2468
5390a8df
TA
2469/**
2470 * spi_nor_set_erase_type() - set a SPI NOR erase type
2471 * @erase: pointer to a structure that describes a SPI NOR erase type
2472 * @size: the size of the sector/block erased by the erase type
2473 * @opcode: the SPI command op code to erase the sector/block
2474 */
2475static void spi_nor_set_erase_type(struct spi_nor_erase_type *erase,
2476 u32 size, u8 opcode)
2477{
2478 erase->size = size;
2479 erase->opcode = opcode;
2480 /* JEDEC JESD216B Standard imposes erase sizes to be power of 2. */
2481 erase->size_shift = ffs(erase->size) - 1;
2482 erase->size_mask = (1 << erase->size_shift) - 1;
2483}
2484
2485/**
2486 * spi_nor_set_erase_settings_from_bfpt() - set erase type settings from BFPT
2487 * @erase: pointer to a structure that describes a SPI NOR erase type
2488 * @size: the size of the sector/block erased by the erase type
2489 * @opcode: the SPI command op code to erase the sector/block
2490 * @i: erase type index as sorted in the Basic Flash Parameter Table
2491 *
2492 * The supported Erase Types will be sorted at init in ascending order, with
2493 * the smallest Erase Type size being the first member in the erase_type array
2494 * of the spi_nor_erase_map structure. Save the Erase Type index as sorted in
2495 * the Basic Flash Parameter Table since it will be used later on to
2496 * synchronize with the supported Erase Types defined in SFDP optional tables.
2497 */
2498static void
2499spi_nor_set_erase_settings_from_bfpt(struct spi_nor_erase_type *erase,
2500 u32 size, u8 opcode, u8 i)
2501{
2502 erase->idx = i;
2503 spi_nor_set_erase_type(erase, size, opcode);
2504}
2505
2506/**
2507 * spi_nor_map_cmp_erase_type() - compare the map's erase types by size
2508 * @l: member in the left half of the map's erase_type array
2509 * @r: member in the right half of the map's erase_type array
2510 *
2511 * Comparison function used in the sort() call to sort in ascending order the
2512 * map's erase types, the smallest erase type size being the first member in the
2513 * sorted erase_type array.
2514 *
2515 * Return: the result of @l->size - @r->size
2516 */
2517static int spi_nor_map_cmp_erase_type(const void *l, const void *r)
2518{
2519 const struct spi_nor_erase_type *left = l, *right = r;
2520
2521 return left->size - right->size;
2522}
2523
e8828ec1
TA
2524/**
2525 * spi_nor_sort_erase_mask() - sort erase mask
2526 * @map: the erase map of the SPI NOR
2527 * @erase_mask: the erase type mask to be sorted
2528 *
2529 * Replicate the sort done for the map's erase types in BFPT: sort the erase
2530 * mask in ascending order with the smallest erase type size starting from
2531 * BIT(0) in the sorted erase mask.
2532 *
2533 * Return: sorted erase mask.
2534 */
2535static u8 spi_nor_sort_erase_mask(struct spi_nor_erase_map *map, u8 erase_mask)
2536{
2537 struct spi_nor_erase_type *erase_type = map->erase_type;
2538 int i;
2539 u8 sorted_erase_mask = 0;
2540
2541 if (!erase_mask)
2542 return 0;
2543
2544 /* Replicate the sort done for the map's erase types. */
2545 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++)
2546 if (erase_type[i].size && erase_mask & BIT(erase_type[i].idx))
2547 sorted_erase_mask |= BIT(i);
2548
2549 return sorted_erase_mask;
2550}
2551
5390a8df
TA
2552/**
2553 * spi_nor_regions_sort_erase_types() - sort erase types in each region
2554 * @map: the erase map of the SPI NOR
2555 *
2556 * Function assumes that the erase types defined in the erase map are already
2557 * sorted in ascending order, with the smallest erase type size being the first
2558 * member in the erase_type array. It replicates the sort done for the map's
2559 * erase types. Each region's erase bitmask will indicate which erase types are
2560 * supported from the sorted erase types defined in the erase map.
2561 * Sort the all region's erase type at init in order to speed up the process of
2562 * finding the best erase command at runtime.
2563 */
2564static void spi_nor_regions_sort_erase_types(struct spi_nor_erase_map *map)
2565{
2566 struct spi_nor_erase_region *region = map->regions;
5390a8df
TA
2567 u8 region_erase_mask, sorted_erase_mask;
2568
2569 while (region) {
2570 region_erase_mask = region->offset & SNOR_ERASE_TYPE_MASK;
2571
e8828ec1
TA
2572 sorted_erase_mask = spi_nor_sort_erase_mask(map,
2573 region_erase_mask);
5390a8df
TA
2574
2575 /* Overwrite erase mask. */
2576 region->offset = (region->offset & ~SNOR_ERASE_TYPE_MASK) |
2577 sorted_erase_mask;
2578
2579 region = spi_nor_region_next(region);
2580 }
2581}
2582
2583/**
2584 * spi_nor_init_uniform_erase_map() - Initialize uniform erase map
2585 * @map: the erase map of the SPI NOR
2586 * @erase_mask: bitmask encoding erase types that can erase the entire
2587 * flash memory
2588 * @flash_size: the spi nor flash memory size
2589 */
2590static void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
2591 u8 erase_mask, u64 flash_size)
2592{
2593 /* Offset 0 with erase_mask and SNOR_LAST_REGION bit set */
2594 map->uniform_region.offset = (erase_mask & SNOR_ERASE_TYPE_MASK) |
2595 SNOR_LAST_REGION;
2596 map->uniform_region.size = flash_size;
2597 map->regions = &map->uniform_region;
2598 map->uniform_erase_type = erase_mask;
2599}
2600
f384b352
CP
2601/**
2602 * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
2603 * @nor: pointer to a 'struct spi_nor'
2604 * @bfpt_header: pointer to the 'struct sfdp_parameter_header' describing
2605 * the Basic Flash Parameter Table length and version
2606 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
2607 * filled
2608 *
2609 * The Basic Flash Parameter Table is the main and only mandatory table as
2610 * defined by the SFDP (JESD216) specification.
2611 * It provides us with the total size (memory density) of the data array and
2612 * the number of address bytes for Fast Read, Page Program and Sector Erase
2613 * commands.
2614 * For Fast READ commands, it also gives the number of mode clock cycles and
2615 * wait states (regrouped in the number of dummy clock cycles) for each
2616 * supported instruction op code.
2617 * For Page Program, the page size is now available since JESD216 rev A, however
2618 * the supported instruction op codes are still not provided.
2619 * For Sector Erase commands, this table stores the supported instruction op
2620 * codes and the associated sector sizes.
2621 * Finally, the Quad Enable Requirements (QER) are also available since JESD216
2622 * rev A. The QER bits encode the manufacturer dependent procedure to be
2623 * executed to set the Quad Enable (QE) bit in some internal register of the
2624 * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
2625 * sending any Quad SPI command to the memory. Actually, setting the QE bit
2626 * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
2627 * and IO3 hence enabling 4 (Quad) I/O lines.
2628 *
2629 * Return: 0 on success, -errno otherwise.
2630 */
2631static int spi_nor_parse_bfpt(struct spi_nor *nor,
2632 const struct sfdp_parameter_header *bfpt_header,
2633 struct spi_nor_flash_parameter *params)
2634{
5390a8df
TA
2635 struct spi_nor_erase_map *map = &nor->erase_map;
2636 struct spi_nor_erase_type *erase_type = map->erase_type;
f384b352
CP
2637 struct sfdp_bfpt bfpt;
2638 size_t len;
2639 int i, cmd, err;
2640 u32 addr;
2641 u16 half;
5390a8df 2642 u8 erase_mask;
f384b352
CP
2643
2644 /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
2645 if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
2646 return -EINVAL;
2647
2648 /* Read the Basic Flash Parameter Table. */
2649 len = min_t(size_t, sizeof(bfpt),
2650 bfpt_header->length * sizeof(u32));
2651 addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
2652 memset(&bfpt, 0, sizeof(bfpt));
bfa41337 2653 err = spi_nor_read_sfdp_dma_unsafe(nor, addr, len, &bfpt);
f384b352
CP
2654 if (err < 0)
2655 return err;
2656
2657 /* Fix endianness of the BFPT DWORDs. */
2658 for (i = 0; i < BFPT_DWORD_MAX; i++)
2659 bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]);
2660
2661 /* Number of address bytes. */
2662 switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
2663 case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
2664 nor->addr_width = 3;
2665 break;
2666
2667 case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
2668 nor->addr_width = 4;
2669 break;
2670
2671 default:
2672 break;
2673 }
2674
2675 /* Flash Memory Density (in bits). */
2676 params->size = bfpt.dwords[BFPT_DWORD(2)];
2677 if (params->size & BIT(31)) {
2678 params->size &= ~BIT(31);
b8f39116
BB
2679
2680 /*
2681 * Prevent overflows on params->size. Anyway, a NOR of 2^64
2682 * bits is unlikely to exist so this error probably means
2683 * the BFPT we are reading is corrupted/wrong.
2684 */
2685 if (params->size > 63)
2686 return -EINVAL;
2687
f384b352
CP
2688 params->size = 1ULL << params->size;
2689 } else {
2690 params->size++;
2691 }
2692 params->size >>= 3; /* Convert to bytes. */
2693
2694 /* Fast Read settings. */
2695 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
2696 const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
2697 struct spi_nor_read_command *read;
2698
2699 if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
2700 params->hwcaps.mask &= ~rd->hwcaps;
2701 continue;
2702 }
2703
2704 params->hwcaps.mask |= rd->hwcaps;
2705 cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
2706 read = &params->reads[cmd];
2707 half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
2708 spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
2709 }
2710
5390a8df
TA
2711 /*
2712 * Sector Erase settings. Reinitialize the uniform erase map using the
2713 * Erase Types defined in the bfpt table.
2714 */
2715 erase_mask = 0;
2716 memset(&nor->erase_map, 0, sizeof(nor->erase_map));
f384b352
CP
2717 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
2718 const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
2719 u32 erasesize;
2720 u8 opcode;
2721
2722 half = bfpt.dwords[er->dword] >> er->shift;
2723 erasesize = half & 0xff;
2724
2725 /* erasesize == 0 means this Erase Type is not supported. */
2726 if (!erasesize)
2727 continue;
2728
2729 erasesize = 1U << erasesize;
2730 opcode = (half >> 8) & 0xff;
5390a8df
TA
2731 erase_mask |= BIT(i);
2732 spi_nor_set_erase_settings_from_bfpt(&erase_type[i], erasesize,
2733 opcode, i);
f384b352 2734 }
5390a8df
TA
2735 spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
2736 /*
2737 * Sort all the map's Erase Types in ascending order with the smallest
2738 * erase size being the first member in the erase_type array.
2739 */
2740 sort(erase_type, SNOR_ERASE_TYPE_MAX, sizeof(erase_type[0]),
2741 spi_nor_map_cmp_erase_type, NULL);
2742 /*
2743 * Sort the erase types in the uniform region in order to update the
2744 * uniform_erase_type bitmask. The bitmask will be used later on when
2745 * selecting the uniform erase.
2746 */
2747 spi_nor_regions_sort_erase_types(map);
2748 map->uniform_erase_type = map->uniform_region.offset &
2749 SNOR_ERASE_TYPE_MASK;
f384b352
CP
2750
2751 /* Stop here if not JESD216 rev A or later. */
2752 if (bfpt_header->length < BFPT_DWORD_MAX)
2753 return 0;
2754
2755 /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
2756 params->page_size = bfpt.dwords[BFPT_DWORD(11)];
2757 params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK;
2758 params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
2759 params->page_size = 1U << params->page_size;
2760
2761 /* Quad Enable Requirements. */
2762 switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
2763 case BFPT_DWORD15_QER_NONE:
2764 params->quad_enable = NULL;
2765 break;
2766
2767 case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
2768 case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
2769 params->quad_enable = spansion_no_read_cr_quad_enable;
2770 break;
2771
2772 case BFPT_DWORD15_QER_SR1_BIT6:
2773 params->quad_enable = macronix_quad_enable;
2774 break;
2775
2776 case BFPT_DWORD15_QER_SR2_BIT7:
2777 params->quad_enable = sr2_bit7_quad_enable;
2778 break;
2779
2780 case BFPT_DWORD15_QER_SR2_BIT1:
2781 params->quad_enable = spansion_read_cr_quad_enable;
2782 break;
2783
2784 default:
2785 return -EINVAL;
2786 }
2787
2788 return 0;
2789}
2790
b038e8e3
TA
2791#define SMPT_CMD_ADDRESS_LEN_MASK GENMASK(23, 22)
2792#define SMPT_CMD_ADDRESS_LEN_0 (0x0UL << 22)
2793#define SMPT_CMD_ADDRESS_LEN_3 (0x1UL << 22)
2794#define SMPT_CMD_ADDRESS_LEN_4 (0x2UL << 22)
2795#define SMPT_CMD_ADDRESS_LEN_USE_CURRENT (0x3UL << 22)
2796
2797#define SMPT_CMD_READ_DUMMY_MASK GENMASK(19, 16)
2798#define SMPT_CMD_READ_DUMMY_SHIFT 16
2799#define SMPT_CMD_READ_DUMMY(_cmd) \
2800 (((_cmd) & SMPT_CMD_READ_DUMMY_MASK) >> SMPT_CMD_READ_DUMMY_SHIFT)
2801#define SMPT_CMD_READ_DUMMY_IS_VARIABLE 0xfUL
2802
2803#define SMPT_CMD_READ_DATA_MASK GENMASK(31, 24)
2804#define SMPT_CMD_READ_DATA_SHIFT 24
2805#define SMPT_CMD_READ_DATA(_cmd) \
2806 (((_cmd) & SMPT_CMD_READ_DATA_MASK) >> SMPT_CMD_READ_DATA_SHIFT)
2807
2808#define SMPT_CMD_OPCODE_MASK GENMASK(15, 8)
2809#define SMPT_CMD_OPCODE_SHIFT 8
2810#define SMPT_CMD_OPCODE(_cmd) \
2811 (((_cmd) & SMPT_CMD_OPCODE_MASK) >> SMPT_CMD_OPCODE_SHIFT)
2812
2813#define SMPT_MAP_REGION_COUNT_MASK GENMASK(23, 16)
2814#define SMPT_MAP_REGION_COUNT_SHIFT 16
2815#define SMPT_MAP_REGION_COUNT(_header) \
2816 ((((_header) & SMPT_MAP_REGION_COUNT_MASK) >> \
2817 SMPT_MAP_REGION_COUNT_SHIFT) + 1)
2818
2819#define SMPT_MAP_ID_MASK GENMASK(15, 8)
2820#define SMPT_MAP_ID_SHIFT 8
2821#define SMPT_MAP_ID(_header) \
2822 (((_header) & SMPT_MAP_ID_MASK) >> SMPT_MAP_ID_SHIFT)
2823
2824#define SMPT_MAP_REGION_SIZE_MASK GENMASK(31, 8)
2825#define SMPT_MAP_REGION_SIZE_SHIFT 8
2826#define SMPT_MAP_REGION_SIZE(_region) \
2827 (((((_region) & SMPT_MAP_REGION_SIZE_MASK) >> \
2828 SMPT_MAP_REGION_SIZE_SHIFT) + 1) * 256)
2829
2830#define SMPT_MAP_REGION_ERASE_TYPE_MASK GENMASK(3, 0)
2831#define SMPT_MAP_REGION_ERASE_TYPE(_region) \
2832 ((_region) & SMPT_MAP_REGION_ERASE_TYPE_MASK)
2833
2834#define SMPT_DESC_TYPE_MAP BIT(1)
2835#define SMPT_DESC_END BIT(0)
2836
2837/**
2838 * spi_nor_smpt_addr_width() - return the address width used in the
2839 * configuration detection command.
2840 * @nor: pointer to a 'struct spi_nor'
2841 * @settings: configuration detection command descriptor, dword1
2842 */
2843static u8 spi_nor_smpt_addr_width(const struct spi_nor *nor, const u32 settings)
2844{
2845 switch (settings & SMPT_CMD_ADDRESS_LEN_MASK) {
2846 case SMPT_CMD_ADDRESS_LEN_0:
2847 return 0;
2848 case SMPT_CMD_ADDRESS_LEN_3:
2849 return 3;
2850 case SMPT_CMD_ADDRESS_LEN_4:
2851 return 4;
2852 case SMPT_CMD_ADDRESS_LEN_USE_CURRENT:
2853 /* fall through */
2854 default:
2855 return nor->addr_width;
2856 }
2857}
2858
2859/**
2860 * spi_nor_smpt_read_dummy() - return the configuration detection command read
2861 * latency, in clock cycles.
2862 * @nor: pointer to a 'struct spi_nor'
2863 * @settings: configuration detection command descriptor, dword1
2864 *
2865 * Return: the number of dummy cycles for an SMPT read
2866 */
2867static u8 spi_nor_smpt_read_dummy(const struct spi_nor *nor, const u32 settings)
2868{
2869 u8 read_dummy = SMPT_CMD_READ_DUMMY(settings);
2870
2871 if (read_dummy == SMPT_CMD_READ_DUMMY_IS_VARIABLE)
2872 return nor->read_dummy;
2873 return read_dummy;
2874}
2875
2876/**
2877 * spi_nor_get_map_in_use() - get the configuration map in use
2878 * @nor: pointer to a 'struct spi_nor'
2879 * @smpt: pointer to the sector map parameter table
c797bd81 2880 * @smpt_len: sector map parameter table length
b9f07cc8
TA
2881 *
2882 * Return: pointer to the map in use, ERR_PTR(-errno) otherwise.
b038e8e3 2883 */
c797bd81
TA
2884static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt,
2885 u8 smpt_len)
b038e8e3 2886{
b9f07cc8 2887 const u32 *ret;
1d5ceff2 2888 u8 *buf;
c797bd81 2889 u32 addr;
b038e8e3 2890 int err;
c797bd81 2891 u8 i;
b038e8e3 2892 u8 addr_width, read_opcode, read_dummy;
1d5ceff2
TA
2893 u8 read_data_mask, map_id;
2894
2895 /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */
2896 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
2897 if (!buf)
2898 return ERR_PTR(-ENOMEM);
b038e8e3
TA
2899
2900 addr_width = nor->addr_width;
2901 read_dummy = nor->read_dummy;
2902 read_opcode = nor->read_opcode;
2903
2904 map_id = 0;
b038e8e3 2905 /* Determine if there are any optional Detection Command Descriptors */
c797bd81
TA
2906 for (i = 0; i < smpt_len; i += 2) {
2907 if (smpt[i] & SMPT_DESC_TYPE_MAP)
2908 break;
2909
b038e8e3
TA
2910 read_data_mask = SMPT_CMD_READ_DATA(smpt[i]);
2911 nor->addr_width = spi_nor_smpt_addr_width(nor, smpt[i]);
2912 nor->read_dummy = spi_nor_smpt_read_dummy(nor, smpt[i]);
2913 nor->read_opcode = SMPT_CMD_OPCODE(smpt[i]);
2914 addr = smpt[i + 1];
2915
1d5ceff2 2916 err = spi_nor_read_raw(nor, addr, 1, buf);
b9f07cc8
TA
2917 if (err) {
2918 ret = ERR_PTR(err);
b038e8e3 2919 goto out;
b9f07cc8 2920 }
b038e8e3
TA
2921
2922 /*
2923 * Build an index value that is used to select the Sector Map
2924 * Configuration that is currently in use.
2925 */
1d5ceff2 2926 map_id = map_id << 1 | !!(*buf & read_data_mask);
b038e8e3
TA
2927 }
2928
c797bd81
TA
2929 /*
2930 * If command descriptors are provided, they always precede map
2931 * descriptors in the table. There is no need to start the iteration
2932 * over smpt array all over again.
2933 *
2934 * Find the matching configuration map.
2935 */
b9f07cc8 2936 ret = ERR_PTR(-EINVAL);
c797bd81
TA
2937 while (i < smpt_len) {
2938 if (SMPT_MAP_ID(smpt[i]) == map_id) {
2939 ret = smpt + i;
2940 break;
2941 }
2942
2943 /*
2944 * If there are no more configuration map descriptors and no
2945 * configuration ID matched the configuration identifier, the
2946 * sector address map is unknown.
2947 */
b038e8e3 2948 if (smpt[i] & SMPT_DESC_END)
c797bd81
TA
2949 break;
2950
b038e8e3
TA
2951 /* increment the table index to the next map */
2952 i += SMPT_MAP_REGION_COUNT(smpt[i]) + 1;
2953 }
2954
b038e8e3
TA
2955 /* fall through */
2956out:
1d5ceff2 2957 kfree(buf);
b038e8e3
TA
2958 nor->addr_width = addr_width;
2959 nor->read_dummy = read_dummy;
2960 nor->read_opcode = read_opcode;
2961 return ret;
2962}
2963
2964/**
2965 * spi_nor_region_check_overlay() - set overlay bit when the region is overlaid
2966 * @region: pointer to a structure that describes a SPI NOR erase region
2967 * @erase: pointer to a structure that describes a SPI NOR erase type
2968 * @erase_type: erase type bitmask
2969 */
2970static void
2971spi_nor_region_check_overlay(struct spi_nor_erase_region *region,
2972 const struct spi_nor_erase_type *erase,
2973 const u8 erase_type)
2974{
2975 int i;
2976
2977 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
2978 if (!(erase_type & BIT(i)))
2979 continue;
2980 if (region->size & erase[i].size_mask) {
2981 spi_nor_region_mark_overlay(region);
2982 return;
2983 }
2984 }
2985}
2986
2987/**
2988 * spi_nor_init_non_uniform_erase_map() - initialize the non-uniform erase map
2989 * @nor: pointer to a 'struct spi_nor'
2990 * @smpt: pointer to the sector map parameter table
2991 *
2992 * Return: 0 on success, -errno otherwise.
2993 */
2994static int spi_nor_init_non_uniform_erase_map(struct spi_nor *nor,
2995 const u32 *smpt)
2996{
2997 struct spi_nor_erase_map *map = &nor->erase_map;
2998 const struct spi_nor_erase_type *erase = map->erase_type;
2999 struct spi_nor_erase_region *region;
3000 u64 offset;
3001 u32 region_count;
3002 int i, j;
e8828ec1 3003 u8 erase_type, uniform_erase_type;
b038e8e3
TA
3004
3005 region_count = SMPT_MAP_REGION_COUNT(*smpt);
3006 /*
3007 * The regions will be freed when the driver detaches from the
3008 * device.
3009 */
3010 region = devm_kcalloc(nor->dev, region_count, sizeof(*region),
3011 GFP_KERNEL);
3012 if (!region)
3013 return -ENOMEM;
3014 map->regions = region;
3015
e8828ec1 3016 uniform_erase_type = 0xff;
b038e8e3
TA
3017 offset = 0;
3018 /* Populate regions. */
3019 for (i = 0; i < region_count; i++) {
3020 j = i + 1; /* index for the region dword */
3021 region[i].size = SMPT_MAP_REGION_SIZE(smpt[j]);
3022 erase_type = SMPT_MAP_REGION_ERASE_TYPE(smpt[j]);
3023 region[i].offset = offset | erase_type;
3024
3025 spi_nor_region_check_overlay(&region[i], erase, erase_type);
3026
3027 /*
3028 * Save the erase types that are supported in all regions and
3029 * can erase the entire flash memory.
3030 */
e8828ec1 3031 uniform_erase_type &= erase_type;
b038e8e3
TA
3032
3033 offset = (region[i].offset & ~SNOR_ERASE_FLAGS_MASK) +
3034 region[i].size;
3035 }
3036
e8828ec1
TA
3037 map->uniform_erase_type = spi_nor_sort_erase_mask(map,
3038 uniform_erase_type);
3039
b038e8e3
TA
3040 spi_nor_region_mark_end(&region[i - 1]);
3041
3042 return 0;
3043}
3044
3045/**
3046 * spi_nor_parse_smpt() - parse Sector Map Parameter Table
3047 * @nor: pointer to a 'struct spi_nor'
3048 * @smpt_header: sector map parameter table header
3049 *
3050 * This table is optional, but when available, we parse it to identify the
3051 * location and size of sectors within the main data array of the flash memory
3052 * device and to identify which Erase Types are supported by each sector.
3053 *
3054 * Return: 0 on success, -errno otherwise.
3055 */
3056static int spi_nor_parse_smpt(struct spi_nor *nor,
3057 const struct sfdp_parameter_header *smpt_header)
3058{
3059 const u32 *sector_map;
3060 u32 *smpt;
3061 size_t len;
3062 u32 addr;
3063 int i, ret;
3064
3065 /* Read the Sector Map Parameter Table. */
3066 len = smpt_header->length * sizeof(*smpt);
3067 smpt = kzalloc(len, GFP_KERNEL);
3068 if (!smpt)
3069 return -ENOMEM;
3070
3071 addr = SFDP_PARAM_HEADER_PTP(smpt_header);
3072 ret = spi_nor_read_sfdp(nor, addr, len, smpt);
3073 if (ret)
3074 goto out;
3075
3076 /* Fix endianness of the SMPT DWORDs. */
3077 for (i = 0; i < smpt_header->length; i++)
3078 smpt[i] = le32_to_cpu(smpt[i]);
3079
c797bd81 3080 sector_map = spi_nor_get_map_in_use(nor, smpt, smpt_header->length);
b9f07cc8
TA
3081 if (IS_ERR(sector_map)) {
3082 ret = PTR_ERR(sector_map);
b038e8e3
TA
3083 goto out;
3084 }
3085
3086 ret = spi_nor_init_non_uniform_erase_map(nor, sector_map);
3087 if (ret)
3088 goto out;
3089
3090 spi_nor_regions_sort_erase_types(&nor->erase_map);
3091 /* fall through */
3092out:
3093 kfree(smpt);
3094 return ret;
3095}
3096
f384b352
CP
3097/**
3098 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
3099 * @nor: pointer to a 'struct spi_nor'
3100 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
3101 * filled
3102 *
3103 * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
3104 * specification. This is a standard which tends to supported by almost all
3105 * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
3106 * runtime the main parameters needed to perform basic SPI flash operations such
3107 * as Fast Read, Page Program or Sector Erase commands.
3108 *
3109 * Return: 0 on success, -errno otherwise.
3110 */
3111static int spi_nor_parse_sfdp(struct spi_nor *nor,
3112 struct spi_nor_flash_parameter *params)
3113{
3114 const struct sfdp_parameter_header *param_header, *bfpt_header;
3115 struct sfdp_parameter_header *param_headers = NULL;
3116 struct sfdp_header header;
3117 struct device *dev = nor->dev;
3118 size_t psize;
3119 int i, err;
3120
3121 /* Get the SFDP header. */
bfa41337 3122 err = spi_nor_read_sfdp_dma_unsafe(nor, 0, sizeof(header), &header);
f384b352
CP
3123 if (err < 0)
3124 return err;
3125
3126 /* Check the SFDP header version. */
3127 if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
90d4fa45 3128 header.major != SFDP_JESD216_MAJOR)
f384b352
CP
3129 return -EINVAL;
3130
3131 /*
3132 * Verify that the first and only mandatory parameter header is a
3133 * Basic Flash Parameter Table header as specified in JESD216.
3134 */
3135 bfpt_header = &header.bfpt_header;
3136 if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
3137 bfpt_header->major != SFDP_JESD216_MAJOR)
3138 return -EINVAL;
3139
3140 /*
3141 * Allocate memory then read all parameter headers with a single
3142 * Read SFDP command. These parameter headers will actually be parsed
3143 * twice: a first time to get the latest revision of the basic flash
3144 * parameter table, then a second time to handle the supported optional
3145 * tables.
3146 * Hence we read the parameter headers once for all to reduce the
3147 * processing time. Also we use kmalloc() instead of devm_kmalloc()
3148 * because we don't need to keep these parameter headers: the allocated
3149 * memory is always released with kfree() before exiting this function.
3150 */
3151 if (header.nph) {
3152 psize = header.nph * sizeof(*param_headers);
3153
3154 param_headers = kmalloc(psize, GFP_KERNEL);
3155 if (!param_headers)
3156 return -ENOMEM;
3157
3158 err = spi_nor_read_sfdp(nor, sizeof(header),
3159 psize, param_headers);
3160 if (err < 0) {
3161 dev_err(dev, "failed to read SFDP parameter headers\n");
3162 goto exit;
3163 }
3164 }
3165
3166 /*
3167 * Check other parameter headers to get the latest revision of
3168 * the basic flash parameter table.
3169 */
3170 for (i = 0; i < header.nph; i++) {
3171 param_header = &param_headers[i];
3172
3173 if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
3174 param_header->major == SFDP_JESD216_MAJOR &&
3175 (param_header->minor > bfpt_header->minor ||
3176 (param_header->minor == bfpt_header->minor &&
3177 param_header->length > bfpt_header->length)))
3178 bfpt_header = param_header;
3179 }
3180
3181 err = spi_nor_parse_bfpt(nor, bfpt_header, params);
3182 if (err)
3183 goto exit;
3184
cd5e6d79 3185 /* Parse optional parameter tables. */
f384b352
CP
3186 for (i = 0; i < header.nph; i++) {
3187 param_header = &param_headers[i];
3188
3189 switch (SFDP_PARAM_HEADER_ID(param_header)) {
3190 case SFDP_SECTOR_MAP_ID:
b038e8e3 3191 err = spi_nor_parse_smpt(nor, param_header);
f384b352
CP
3192 break;
3193
3194 default:
3195 break;
3196 }
3197
cd5e6d79
TA
3198 if (err) {
3199 dev_warn(dev, "Failed to parse optional parameter table: %04x\n",
3200 SFDP_PARAM_HEADER_ID(param_header));
3201 /*
3202 * Let's not drop all information we extracted so far
3203 * if optional table parsers fail. In case of failing,
3204 * each optional parser is responsible to roll back to
3205 * the previously known spi_nor data.
3206 */
3207 err = 0;
3208 }
f384b352
CP
3209 }
3210
3211exit:
3212 kfree(param_headers);
3213 return err;
3214}
3215
cfc5604c
CP
3216static int spi_nor_init_params(struct spi_nor *nor,
3217 const struct flash_info *info,
3218 struct spi_nor_flash_parameter *params)
3219{
5390a8df
TA
3220 struct spi_nor_erase_map *map = &nor->erase_map;
3221 u8 i, erase_mask;
3222
cfc5604c
CP
3223 /* Set legacy flash parameters as default. */
3224 memset(params, 0, sizeof(*params));
3225
3226 /* Set SPI NOR sizes. */
3227 params->size = info->sector_size * info->n_sectors;
3228 params->page_size = info->page_size;
3229
3230 /* (Fast) Read settings. */
3231 params->hwcaps.mask |= SNOR_HWCAPS_READ;
3232 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
3233 0, 0, SPINOR_OP_READ,
3234 SNOR_PROTO_1_1_1);
3235
3236 if (!(info->flags & SPI_NOR_NO_FR)) {
3237 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
3238 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
3239 0, 8, SPINOR_OP_READ_FAST,
3240 SNOR_PROTO_1_1_1);
3241 }
3242
3243 if (info->flags & SPI_NOR_DUAL_READ) {
3244 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
3245 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_2],
3246 0, 8, SPINOR_OP_READ_1_1_2,
3247 SNOR_PROTO_1_1_2);
3248 }
3249
3250 if (info->flags & SPI_NOR_QUAD_READ) {
3251 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
3252 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
3253 0, 8, SPINOR_OP_READ_1_1_4,
3254 SNOR_PROTO_1_1_4);
3255 }
3256
3257 /* Page Program settings. */
3258 params->hwcaps.mask |= SNOR_HWCAPS_PP;
3259 spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP],
3260 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
3261
5390a8df
TA
3262 /*
3263 * Sector Erase settings. Sort Erase Types in ascending order, with the
3264 * smallest erase size starting at BIT(0).
3265 */
3266 erase_mask = 0;
3267 i = 0;
3268 if (info->flags & SECT_4K_PMC) {
3269 erase_mask |= BIT(i);
3270 spi_nor_set_erase_type(&map->erase_type[i], 4096u,
3271 SPINOR_OP_BE_4K_PMC);
3272 i++;
3273 } else if (info->flags & SECT_4K) {
3274 erase_mask |= BIT(i);
3275 spi_nor_set_erase_type(&map->erase_type[i], 4096u,
3276 SPINOR_OP_BE_4K);
3277 i++;
3278 }
3279 erase_mask |= BIT(i);
3280 spi_nor_set_erase_type(&map->erase_type[i], info->sector_size,
3281 SPINOR_OP_SE);
3282 spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
3283
cfc5604c
CP
3284 /* Select the procedure to set the Quad Enable bit. */
3285 if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
3286 SNOR_HWCAPS_PP_QUAD)) {
3287 switch (JEDEC_MFR(info)) {
3288 case SNOR_MFR_MACRONIX:
3289 params->quad_enable = macronix_quad_enable;
3290 break;
3291
3292 case SNOR_MFR_MICRON:
3293 break;
3294
3295 default:
f384b352 3296 /* Kept only for backward compatibility purpose. */
cfc5604c
CP
3297 params->quad_enable = spansion_quad_enable;
3298 break;
3299 }
e2707285
AY
3300
3301 /*
3302 * Some manufacturer like GigaDevice may use different
3303 * bit to set QE on different memories, so the MFR can't
3304 * indicate the quad_enable method for this case, we need
3305 * set it in flash info list.
3306 */
3307 if (info->quad_enable)
3308 params->quad_enable = info->quad_enable;
cfc5604c
CP
3309 }
3310
f384b352
CP
3311 if ((info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) &&
3312 !(info->flags & SPI_NOR_SKIP_SFDP)) {
3313 struct spi_nor_flash_parameter sfdp_params;
5390a8df 3314 struct spi_nor_erase_map prev_map;
f384b352
CP
3315
3316 memcpy(&sfdp_params, params, sizeof(sfdp_params));
5390a8df
TA
3317 memcpy(&prev_map, &nor->erase_map, sizeof(prev_map));
3318
90c31cb9
BB
3319 if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
3320 nor->addr_width = 0;
5390a8df
TA
3321 /* restore previous erase map */
3322 memcpy(&nor->erase_map, &prev_map,
3323 sizeof(nor->erase_map));
90c31cb9 3324 } else {
f384b352 3325 memcpy(params, &sfdp_params, sizeof(*params));
90c31cb9 3326 }
f384b352
CP
3327 }
3328
cfc5604c
CP
3329 return 0;
3330}
3331
3332static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
3333{
3334 size_t i;
3335
3336 for (i = 0; i < size; i++)
3337 if (table[i][0] == (int)hwcaps)
3338 return table[i][1];
3339
3340 return -EINVAL;
3341}
3342
3343static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
3344{
3345 static const int hwcaps_read2cmd[][2] = {
3346 { SNOR_HWCAPS_READ, SNOR_CMD_READ },
3347 { SNOR_HWCAPS_READ_FAST, SNOR_CMD_READ_FAST },
15f55331 3348 { SNOR_HWCAPS_READ_1_1_1_DTR, SNOR_CMD_READ_1_1_1_DTR },
cfc5604c
CP
3349 { SNOR_HWCAPS_READ_1_1_2, SNOR_CMD_READ_1_1_2 },
3350 { SNOR_HWCAPS_READ_1_2_2, SNOR_CMD_READ_1_2_2 },
3351 { SNOR_HWCAPS_READ_2_2_2, SNOR_CMD_READ_2_2_2 },
15f55331 3352 { SNOR_HWCAPS_READ_1_2_2_DTR, SNOR_CMD_READ_1_2_2_DTR },
cfc5604c
CP
3353 { SNOR_HWCAPS_READ_1_1_4, SNOR_CMD_READ_1_1_4 },
3354 { SNOR_HWCAPS_READ_1_4_4, SNOR_CMD_READ_1_4_4 },
3355 { SNOR_HWCAPS_READ_4_4_4, SNOR_CMD_READ_4_4_4 },
15f55331 3356 { SNOR_HWCAPS_READ_1_4_4_DTR, SNOR_CMD_READ_1_4_4_DTR },
fe488a5e
CP
3357 { SNOR_HWCAPS_READ_1_1_8, SNOR_CMD_READ_1_1_8 },
3358 { SNOR_HWCAPS_READ_1_8_8, SNOR_CMD_READ_1_8_8 },
3359 { SNOR_HWCAPS_READ_8_8_8, SNOR_CMD_READ_8_8_8 },
3360 { SNOR_HWCAPS_READ_1_8_8_DTR, SNOR_CMD_READ_1_8_8_DTR },
cfc5604c
CP
3361 };
3362
3363 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
3364 ARRAY_SIZE(hwcaps_read2cmd));
3365}
3366
3367static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
3368{
3369 static const int hwcaps_pp2cmd[][2] = {
3370 { SNOR_HWCAPS_PP, SNOR_CMD_PP },
3371 { SNOR_HWCAPS_PP_1_1_4, SNOR_CMD_PP_1_1_4 },
3372 { SNOR_HWCAPS_PP_1_4_4, SNOR_CMD_PP_1_4_4 },
3373 { SNOR_HWCAPS_PP_4_4_4, SNOR_CMD_PP_4_4_4 },
fe488a5e
CP
3374 { SNOR_HWCAPS_PP_1_1_8, SNOR_CMD_PP_1_1_8 },
3375 { SNOR_HWCAPS_PP_1_8_8, SNOR_CMD_PP_1_8_8 },
3376 { SNOR_HWCAPS_PP_8_8_8, SNOR_CMD_PP_8_8_8 },
cfc5604c
CP
3377 };
3378
3379 return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
3380 ARRAY_SIZE(hwcaps_pp2cmd));
3381}
3382
3383static int spi_nor_select_read(struct spi_nor *nor,
3384 const struct spi_nor_flash_parameter *params,
3385 u32 shared_hwcaps)
3386{
3387 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
3388 const struct spi_nor_read_command *read;
3389
3390 if (best_match < 0)
3391 return -EINVAL;
3392
3393 cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
3394 if (cmd < 0)
3395 return -EINVAL;
3396
3397 read = &params->reads[cmd];
3398 nor->read_opcode = read->opcode;
3399 nor->read_proto = read->proto;
3400
3401 /*
3402 * In the spi-nor framework, we don't need to make the difference
3403 * between mode clock cycles and wait state clock cycles.
3404 * Indeed, the value of the mode clock cycles is used by a QSPI
3405 * flash memory to know whether it should enter or leave its 0-4-4
3406 * (Continuous Read / XIP) mode.
3407 * eXecution In Place is out of the scope of the mtd sub-system.
3408 * Hence we choose to merge both mode and wait state clock cycles
3409 * into the so called dummy clock cycles.
3410 */
3411 nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
3412 return 0;
3413}
3414
3415static int spi_nor_select_pp(struct spi_nor *nor,
3416 const struct spi_nor_flash_parameter *params,
3417 u32 shared_hwcaps)
3418{
3419 int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
3420 const struct spi_nor_pp_command *pp;
3421
3422 if (best_match < 0)
3423 return -EINVAL;
3424
3425 cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
3426 if (cmd < 0)
3427 return -EINVAL;
3428
3429 pp = &params->page_programs[cmd];
3430 nor->program_opcode = pp->opcode;
3431 nor->write_proto = pp->proto;
3432 return 0;
3433}
3434
5390a8df
TA
3435/**
3436 * spi_nor_select_uniform_erase() - select optimum uniform erase type
3437 * @map: the erase map of the SPI NOR
3438 * @wanted_size: the erase type size to search for. Contains the value of
3439 * info->sector_size or of the "small sector" size in case
3440 * CONFIG_MTD_SPI_NOR_USE_4K_SECTORS is defined.
3441 *
3442 * Once the optimum uniform sector erase command is found, disable all the
3443 * other.
3444 *
3445 * Return: pointer to erase type on success, NULL otherwise.
3446 */
3447static const struct spi_nor_erase_type *
3448spi_nor_select_uniform_erase(struct spi_nor_erase_map *map,
3449 const u32 wanted_size)
cfc5604c 3450{
5390a8df
TA
3451 const struct spi_nor_erase_type *tested_erase, *erase = NULL;
3452 int i;
3453 u8 uniform_erase_type = map->uniform_erase_type;
cfc5604c 3454
5390a8df
TA
3455 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
3456 if (!(uniform_erase_type & BIT(i)))
3457 continue;
3458
3459 tested_erase = &map->erase_type[i];
3460
3461 /*
3462 * If the current erase size is the one, stop here:
3463 * we have found the right uniform Sector Erase command.
3464 */
3465 if (tested_erase->size == wanted_size) {
3466 erase = tested_erase;
3467 break;
3468 }
f384b352 3469
5390a8df
TA
3470 /*
3471 * Otherwise, the current erase size is still a valid canditate.
3472 * Select the biggest valid candidate.
3473 */
3474 if (!erase && tested_erase->size)
3475 erase = tested_erase;
3476 /* keep iterating to find the wanted_size */
3477 }
3478
3479 if (!erase)
3480 return NULL;
3481
3482 /* Disable all other Sector Erase commands. */
3483 map->uniform_erase_type &= ~SNOR_ERASE_TYPE_MASK;
3484 map->uniform_erase_type |= BIT(erase - map->erase_type);
3485 return erase;
3486}
3487
3488static int spi_nor_select_erase(struct spi_nor *nor, u32 wanted_size)
3489{
3490 struct spi_nor_erase_map *map = &nor->erase_map;
3491 const struct spi_nor_erase_type *erase = NULL;
3492 struct mtd_info *mtd = &nor->mtd;
3493 int i;
3494
3495 /*
3496 * The previous implementation handling Sector Erase commands assumed
3497 * that the SPI flash memory has an uniform layout then used only one
3498 * of the supported erase sizes for all Sector Erase commands.
3499 * So to be backward compatible, the new implementation also tries to
3500 * manage the SPI flash memory as uniform with a single erase sector
3501 * size, when possible.
3502 */
cfc5604c
CP
3503#ifdef CONFIG_MTD_SPI_NOR_USE_4K_SECTORS
3504 /* prefer "small sector" erase if possible */
5390a8df 3505 wanted_size = 4096u;
cfc5604c 3506#endif
5390a8df
TA
3507
3508 if (spi_nor_has_uniform_erase(nor)) {
3509 erase = spi_nor_select_uniform_erase(map, wanted_size);
3510 if (!erase)
3511 return -EINVAL;
3512 nor->erase_opcode = erase->opcode;
3513 mtd->erasesize = erase->size;
3514 return 0;
cfc5604c 3515 }
5390a8df
TA
3516
3517 /*
3518 * For non-uniform SPI flash memory, set mtd->erasesize to the
3519 * maximum erase sector size. No need to set nor->erase_opcode.
3520 */
3521 for (i = SNOR_ERASE_TYPE_MAX - 1; i >= 0; i--) {
3522 if (map->erase_type[i].size) {
3523 erase = &map->erase_type[i];
3524 break;
3525 }
3526 }
3527
3528 if (!erase)
3529 return -EINVAL;
3530
3531 mtd->erasesize = erase->size;
cfc5604c
CP
3532 return 0;
3533}
3534
3535static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
3536 const struct spi_nor_flash_parameter *params,
3537 const struct spi_nor_hwcaps *hwcaps)
3538{
3539 u32 ignored_mask, shared_mask;
3540 bool enable_quad_io;
3541 int err;
3542
3543 /*
3544 * Keep only the hardware capabilities supported by both the SPI
3545 * controller and the SPI flash memory.
3546 */
3547 shared_mask = hwcaps->mask & params->hwcaps.mask;
3548
3549 /* SPI n-n-n protocols are not supported yet. */
3550 ignored_mask = (SNOR_HWCAPS_READ_2_2_2 |
3551 SNOR_HWCAPS_READ_4_4_4 |
fe488a5e
CP
3552 SNOR_HWCAPS_READ_8_8_8 |
3553 SNOR_HWCAPS_PP_4_4_4 |
3554 SNOR_HWCAPS_PP_8_8_8);
cfc5604c
CP
3555 if (shared_mask & ignored_mask) {
3556 dev_dbg(nor->dev,
3557 "SPI n-n-n protocols are not supported yet.\n");
3558 shared_mask &= ~ignored_mask;
3559 }
3560
3561 /* Select the (Fast) Read command. */
3562 err = spi_nor_select_read(nor, params, shared_mask);
3563 if (err) {
3564 dev_err(nor->dev,
3565 "can't select read settings supported by both the SPI controller and memory.\n");
3566 return err;
3567 }
3568
3569 /* Select the Page Program command. */
3570 err = spi_nor_select_pp(nor, params, shared_mask);
3571 if (err) {
3572 dev_err(nor->dev,
3573 "can't select write settings supported by both the SPI controller and memory.\n");
3574 return err;
3575 }
3576
3577 /* Select the Sector Erase command. */
5390a8df 3578 err = spi_nor_select_erase(nor, info->sector_size);
cfc5604c
CP
3579 if (err) {
3580 dev_err(nor->dev,
3581 "can't select erase settings supported by both the SPI controller and memory.\n");
3582 return err;
3583 }
3584
3585 /* Enable Quad I/O if needed. */
3586 enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
3587 spi_nor_get_protocol_width(nor->write_proto) == 4);
46dde01f
KD
3588 if (enable_quad_io && params->quad_enable)
3589 nor->quad_enable = params->quad_enable;
3590 else
3591 nor->quad_enable = NULL;
3592
3593 return 0;
3594}
3595
3596static int spi_nor_init(struct spi_nor *nor)
3597{
3598 int err;
3599
3600 /*
3601 * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
3602 * with the software protection bits set
3603 */
3604 if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
3605 JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
3606 JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
3607 nor->info->flags & SPI_NOR_HAS_LOCK) {
3608 write_enable(nor);
3609 write_sr(nor, 0);
3610 spi_nor_wait_till_ready(nor);
3611 }
3612
3613 if (nor->quad_enable) {
3614 err = nor->quad_enable(nor);
cfc5604c
CP
3615 if (err) {
3616 dev_err(nor->dev, "quad mode not supported\n");
3617 return err;
3618 }
3619 }
3620
46dde01f
KD
3621 if ((nor->addr_width == 4) &&
3622 (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
bb276262
BN
3623 !(nor->info->flags & SPI_NOR_4B_OPCODES)) {
3624 /*
3625 * If the RESET# pin isn't hooked up properly, or the system
3626 * otherwise doesn't perform a reset command in the boot
3627 * sequence, it's impossible to 100% protect against unexpected
3628 * reboots (e.g., crashes). Warn the user (or hopefully, system
3629 * designer) that this is bad.
3630 */
3631 WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET,
3632 "enabling reset hack; may not recover from unexpected reboots\n");
46dde01f 3633 set_4byte(nor, nor->info, 1);
bb276262 3634 }
46dde01f 3635
cfc5604c
CP
3636 return 0;
3637}
3638
d6084fc8
KD
3639/* mtd resume handler */
3640static void spi_nor_resume(struct mtd_info *mtd)
3641{
3642 struct spi_nor *nor = mtd_to_spi_nor(mtd);
3643 struct device *dev = nor->dev;
3644 int ret;
3645
3646 /* re-initialize the nor chip */
3647 ret = spi_nor_init(nor);
3648 if (ret)
3649 dev_err(dev, "resume() failed\n");
3650}
3651
8dee1d97
HZ
3652void spi_nor_restore(struct spi_nor *nor)
3653{
3654 /* restore the addressing mode */
3655 if ((nor->addr_width == 4) &&
3656 (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
bb276262
BN
3657 !(nor->info->flags & SPI_NOR_4B_OPCODES) &&
3658 (nor->flags & SNOR_F_BROKEN_RESET))
8dee1d97
HZ
3659 set_4byte(nor, nor->info, 0);
3660}
3661EXPORT_SYMBOL_GPL(spi_nor_restore);
3662
cfc5604c
CP
3663int spi_nor_scan(struct spi_nor *nor, const char *name,
3664 const struct spi_nor_hwcaps *hwcaps)
b199489d 3665{
cfc5604c 3666 struct spi_nor_flash_parameter params;
06bb6f5a 3667 const struct flash_info *info = NULL;
b199489d 3668 struct device *dev = nor->dev;
19763671 3669 struct mtd_info *mtd = &nor->mtd;
9c7d7875 3670 struct device_node *np = spi_nor_get_flash_node(nor);
b199489d
HS
3671 int ret;
3672 int i;
3673
3674 ret = spi_nor_check(nor);
3675 if (ret)
3676 return ret;
3677
cfc5604c
CP
3678 /* Reset SPI protocol for all commands. */
3679 nor->reg_proto = SNOR_PROTO_1_1_1;
3680 nor->read_proto = SNOR_PROTO_1_1_1;
3681 nor->write_proto = SNOR_PROTO_1_1_1;
3682
43163022 3683 if (name)
06bb6f5a 3684 info = spi_nor_match_id(name);
43163022 3685 /* Try to auto-detect if chip name wasn't specified or not found */
06bb6f5a
RM
3686 if (!info)
3687 info = spi_nor_read_id(nor);
3688 if (IS_ERR_OR_NULL(info))
70f3ce05
BH
3689 return -ENOENT;
3690
58c81957
RM
3691 /*
3692 * If caller has specified name of flash model that can normally be
3693 * detected using JEDEC, let's verify it.
3694 */
3695 if (name && info->id_len) {
06bb6f5a 3696 const struct flash_info *jinfo;
b199489d 3697
06bb6f5a
RM
3698 jinfo = spi_nor_read_id(nor);
3699 if (IS_ERR(jinfo)) {
3700 return PTR_ERR(jinfo);
3701 } else if (jinfo != info) {
b199489d
HS
3702 /*
3703 * JEDEC knows better, so overwrite platform ID. We
3704 * can't trust partitions any longer, but we'll let
3705 * mtd apply them anyway, since some partitions may be
3706 * marked read-only, and we don't want to lose that
3707 * information, even if it's not 100% accurate.
3708 */
3709 dev_warn(dev, "found %s, expected %s\n",
06bb6f5a
RM
3710 jinfo->name, info->name);
3711 info = jinfo;
b199489d
HS
3712 }
3713 }
3714
3715 mutex_init(&nor->lock);
3716
e99ca98f
RR
3717 /*
3718 * Make sure the XSR_RDY flag is set before calling
3719 * spi_nor_wait_till_ready(). Xilinx S3AN share MFR
3720 * with Atmel spi-nor
3721 */
3722 if (info->flags & SPI_S3AN)
3723 nor->flags |= SNOR_F_READY_XSR_RDY;
3724
cfc5604c
CP
3725 /* Parse the Serial Flash Discoverable Parameters table. */
3726 ret = spi_nor_init_params(nor, info, &params);
3727 if (ret)
3728 return ret;
3729
32f1b7c8 3730 if (!mtd->name)
b199489d 3731 mtd->name = dev_name(dev);
c9ec3900 3732 mtd->priv = nor;
b199489d
HS
3733 mtd->type = MTD_NORFLASH;
3734 mtd->writesize = 1;
3735 mtd->flags = MTD_CAP_NORFLASH;
cfc5604c 3736 mtd->size = params.size;
b199489d
HS
3737 mtd->_erase = spi_nor_erase;
3738 mtd->_read = spi_nor_read;
d6084fc8 3739 mtd->_resume = spi_nor_resume;
b199489d 3740
357ca38d 3741 /* NOR protection support for STmicro/Micron chips and similar */
76a4707d
BN
3742 if (JEDEC_MFR(info) == SNOR_MFR_MICRON ||
3743 info->flags & SPI_NOR_HAS_LOCK) {
8cc7f33a
BN
3744 nor->flash_lock = stm_lock;
3745 nor->flash_unlock = stm_unlock;
5bf0e69b 3746 nor->flash_is_locked = stm_is_locked;
8cc7f33a
BN
3747 }
3748
5bf0e69b 3749 if (nor->flash_lock && nor->flash_unlock && nor->flash_is_locked) {
b199489d
HS
3750 mtd->_lock = spi_nor_lock;
3751 mtd->_unlock = spi_nor_unlock;
5bf0e69b 3752 mtd->_is_locked = spi_nor_is_locked;
b199489d
HS
3753 }
3754
3755 /* sst nor chips use AAI word program */
3756 if (info->flags & SST_WRITE)
3757 mtd->_write = sst_write;
3758 else
3759 mtd->_write = spi_nor_write;
3760
51983b7d
BN
3761 if (info->flags & USE_FSR)
3762 nor->flags |= SNOR_F_USE_FSR;
3dd8012a
BN
3763 if (info->flags & SPI_NOR_HAS_TB)
3764 nor->flags |= SNOR_F_HAS_SR_TB;
2f5ad7f0 3765 if (info->flags & NO_CHIP_ERASE)
3766 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
c4b3eacc
AS
3767 if (info->flags & USE_CLSR)
3768 nor->flags |= SNOR_F_USE_CLSR;
c14dedde 3769
b199489d
HS
3770 if (info->flags & SPI_NOR_NO_ERASE)
3771 mtd->flags |= MTD_NO_ERASE;
3772
3773 mtd->dev.parent = dev;
cfc5604c 3774 nor->page_size = params.page_size;
b199489d
HS
3775 mtd->writebufsize = nor->page_size;
3776
3777 if (np) {
3778 /* If we were instantiated by DT, use it */
3779 if (of_property_read_bool(np, "m25p,fast-read"))
cfc5604c 3780 params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
b199489d 3781 else
cfc5604c 3782 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
b199489d
HS
3783 } else {
3784 /* If we weren't instantiated by DT, default to fast-read */
cfc5604c 3785 params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
b199489d
HS
3786 }
3787
bb276262
BN
3788 if (of_property_read_bool(np, "broken-flash-reset"))
3789 nor->flags |= SNOR_F_BROKEN_RESET;
3790
b199489d
HS
3791 /* Some devices cannot do fast-read, no matter what DT tells us */
3792 if (info->flags & SPI_NOR_NO_FR)
cfc5604c 3793 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
b199489d 3794
cfc5604c
CP
3795 /*
3796 * Configure the SPI memory:
3797 * - select op codes for (Fast) Read, Page Program and Sector Erase.
3798 * - set the number of dummy cycles (mode cycles + wait states).
3799 * - set the SPI protocols for register and memory accesses.
3800 * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
3801 */
3802 ret = spi_nor_setup(nor, info, &params, hwcaps);
3803 if (ret)
3804 return ret;
b199489d 3805
f384b352
CP
3806 if (nor->addr_width) {
3807 /* already configured from SFDP */
3808 } else if (info->addr_width) {
b199489d 3809 nor->addr_width = info->addr_width;
f384b352 3810 } else if (mtd->size > 0x1000000) {
b199489d
HS
3811 /* enable 4-byte addressing if the device exceeds 16MiB */
3812 nor->addr_width = 4;
ba3ae6a1
CP
3813 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
3814 info->flags & SPI_NOR_4B_OPCODES)
3815 spi_nor_set_4byte_opcodes(nor, info);
b199489d
HS
3816 } else {
3817 nor->addr_width = 3;
3818 }
3819
c67cbb83
BN
3820 if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
3821 dev_err(dev, "address width is too large: %u\n",
3822 nor->addr_width);
3823 return -EINVAL;
3824 }
3825
e99ca98f
RR
3826 if (info->flags & SPI_S3AN) {
3827 ret = s3an_nor_scan(info, nor);
3828 if (ret)
3829 return ret;
3830 }
3831
46dde01f
KD
3832 /* Send all the required SPI flash commands to initialize device */
3833 nor->info = info;
3834 ret = spi_nor_init(nor);
3835 if (ret)
3836 return ret;
3837
06bb6f5a 3838 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
b199489d
HS
3839 (long long)mtd->size >> 10);
3840
3841 dev_dbg(dev,
3842 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
3843 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
3844 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
3845 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
3846
3847 if (mtd->numeraseregions)
3848 for (i = 0; i < mtd->numeraseregions; i++)
3849 dev_dbg(dev,
3850 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
3851 ".erasesize = 0x%.8x (%uKiB), "
3852 ".numblocks = %d }\n",
3853 i, (long long)mtd->eraseregions[i].offset,
3854 mtd->eraseregions[i].erasesize,
3855 mtd->eraseregions[i].erasesize / 1024,
3856 mtd->eraseregions[i].numblocks);
3857 return 0;
3858}
b61834b0 3859EXPORT_SYMBOL_GPL(spi_nor_scan);
b199489d 3860
06bb6f5a 3861static const struct flash_info *spi_nor_match_id(const char *name)
0d8c11c0 3862{
06bb6f5a 3863 const struct flash_info *id = spi_nor_ids;
0d8c11c0 3864
2ff46e6f 3865 while (id->name) {
0d8c11c0
HS
3866 if (!strcmp(name, id->name))
3867 return id;
3868 id++;
3869 }
3870 return NULL;
3871}
3872
b199489d
HS
3873MODULE_LICENSE("GPL");
3874MODULE_AUTHOR("Huang Shijie <[email protected]>");
3875MODULE_AUTHOR("Mike Lavender");
3876MODULE_DESCRIPTION("framework for SPI NOR");
This page took 0.757895 seconds and 4 git commands to generate.