mtd: spi: spi-nor-core: Add 4 Byte addressing support
[platform/kernel/u-boot.git] / drivers / mtd / spi / spi-nor-core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with
4  * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
5  *
6  * Copyright (C) 2005, Intec Automation Inc.
7  * Copyright (C) 2014, Freescale Semiconductor, Inc.
8  *
9  * Synced from Linux v4.19
10  */
11
12 #include <common.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/log2.h>
16 #include <linux/math64.h>
17 #include <linux/sizes.h>
18
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/spi-nor.h>
21 #include <spi-mem.h>
22 #include <spi.h>
23
24 /* Define max times to check status register before we give up. */
25
26 /*
27  * For everything but full-chip erase; probably could be much smaller, but kept
28  * around for safety for now
29  */
30
31 #define HZ                                      CONFIG_SYS_HZ
32
33 #define DEFAULT_READY_WAIT_JIFFIES              (40UL * HZ)
34
35 #define SPI_NOR_MAX_ID_LEN      6
36 #define SPI_NOR_MAX_ADDR_WIDTH  4
37
38 struct flash_info {
39         char            *name;
40
41         /*
42          * This array stores the ID bytes.
43          * The first three bytes are the JEDIC ID.
44          * JEDEC ID zero means "no ID" (mostly older chips).
45          */
46         u8              id[SPI_NOR_MAX_ID_LEN];
47         u8              id_len;
48
49         /* The size listed here is what works with SPINOR_OP_SE, which isn't
50          * necessarily called a "sector" by the vendor.
51          */
52         unsigned int    sector_size;
53         u16             n_sectors;
54
55         u16             page_size;
56         u16             addr_width;
57
58         u16             flags;
59 #define SECT_4K                 BIT(0)  /* SPINOR_OP_BE_4K works uniformly */
60 #define SPI_NOR_NO_ERASE        BIT(1)  /* No erase command needed */
61 #define SST_WRITE               BIT(2)  /* use SST byte programming */
62 #define SPI_NOR_NO_FR           BIT(3)  /* Can't do fastread */
63 #define SECT_4K_PMC             BIT(4)  /* SPINOR_OP_BE_4K_PMC works uniformly */
64 #define SPI_NOR_DUAL_READ       BIT(5)  /* Flash supports Dual Read */
65 #define SPI_NOR_QUAD_READ       BIT(6)  /* Flash supports Quad Read */
66 #define USE_FSR                 BIT(7)  /* use flag status register */
67 #define SPI_NOR_HAS_LOCK        BIT(8)  /* Flash supports lock/unlock via SR */
68 #define SPI_NOR_HAS_TB          BIT(9)  /*
69                                          * Flash SR has Top/Bottom (TB) protect
70                                          * bit. Must be used with
71                                          * SPI_NOR_HAS_LOCK.
72                                          */
73 #define SPI_S3AN                BIT(10) /*
74                                          * Xilinx Spartan 3AN In-System Flash
75                                          * (MFR cannot be used for probing
76                                          * because it has the same value as
77                                          * ATMEL flashes)
78                                          */
79 #define SPI_NOR_4B_OPCODES      BIT(11) /*
80                                          * Use dedicated 4byte address op codes
81                                          * to support memory size above 128Mib.
82                                          */
83 #define NO_CHIP_ERASE           BIT(12) /* Chip does not support chip erase */
84 #define USE_CLSR                BIT(14) /* use CLSR command */
85
86         int     (*quad_enable)(struct spi_nor *nor);
87 };
88
89 #define JEDEC_MFR(info) ((info)->id[0])
90
91 static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op
92                 *op, void *buf)
93 {
94         if (op->data.dir == SPI_MEM_DATA_IN)
95                 op->data.buf.in = buf;
96         else
97                 op->data.buf.out = buf;
98         return spi_mem_exec_op(nor->spi, op);
99 }
100
101 static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
102 {
103         struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1),
104                                           SPI_MEM_OP_NO_ADDR,
105                                           SPI_MEM_OP_NO_DUMMY,
106                                           SPI_MEM_OP_DATA_IN(len, NULL, 1));
107         int ret;
108
109         ret = spi_nor_read_write_reg(nor, &op, val);
110         if (ret < 0)
111                 dev_dbg(&flash->spimem->spi->dev, "error %d reading %x\n", ret,
112                         code);
113
114         return ret;
115 }
116
117 static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
118 {
119         struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1),
120                                           SPI_MEM_OP_NO_ADDR,
121                                           SPI_MEM_OP_NO_DUMMY,
122                                           SPI_MEM_OP_DATA_OUT(len, NULL, 1));
123
124         return spi_nor_read_write_reg(nor, &op, buf);
125 }
126
127 static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
128                                  u_char *buf)
129 {
130         struct spi_mem_op op =
131                         SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
132                                    SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
133                                    SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
134                                    SPI_MEM_OP_DATA_IN(len, buf, 1));
135         size_t remaining = len;
136         int ret;
137
138         /* get transfer protocols. */
139         op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
140         op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
141         op.dummy.buswidth = op.addr.buswidth;
142         op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
143
144         /* convert the dummy cycles to the number of bytes */
145         op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
146
147         while (remaining) {
148                 op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
149                 ret = spi_mem_adjust_op_size(nor->spi, &op);
150                 if (ret)
151                         return ret;
152
153                 ret = spi_mem_exec_op(nor->spi, &op);
154                 if (ret)
155                         return ret;
156
157                 op.addr.val += op.data.nbytes;
158                 remaining -= op.data.nbytes;
159                 op.data.buf.in += op.data.nbytes;
160         }
161
162         return len;
163 }
164
165 static ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
166                                   const u_char *buf)
167 {
168         struct spi_mem_op op =
169                         SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1),
170                                    SPI_MEM_OP_ADDR(nor->addr_width, to, 1),
171                                    SPI_MEM_OP_NO_DUMMY,
172                                    SPI_MEM_OP_DATA_OUT(len, buf, 1));
173         size_t remaining = len;
174         int ret;
175
176         /* get transfer protocols. */
177         op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto);
178         op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto);
179         op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
180
181         if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
182                 op.addr.nbytes = 0;
183
184         while (remaining) {
185                 op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
186                 ret = spi_mem_adjust_op_size(nor->spi, &op);
187                 if (ret)
188                         return ret;
189
190                 ret = spi_mem_exec_op(nor->spi, &op);
191                 if (ret)
192                         return ret;
193
194                 op.addr.val += op.data.nbytes;
195                 remaining -= op.data.nbytes;
196                 op.data.buf.out += op.data.nbytes;
197         }
198
199         return len;
200 }
201
202 /*
203  * Read the status register, returning its value in the location
204  * Return the status register value.
205  * Returns negative if error occurred.
206  */
207 static int read_sr(struct spi_nor *nor)
208 {
209         int ret;
210         u8 val;
211
212         ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
213         if (ret < 0) {
214                 pr_debug("error %d reading SR\n", (int)ret);
215                 return ret;
216         }
217
218         return val;
219 }
220
221 /*
222  * Read the flag status register, returning its value in the location
223  * Return the status register value.
224  * Returns negative if error occurred.
225  */
226 static int read_fsr(struct spi_nor *nor)
227 {
228         int ret;
229         u8 val;
230
231         ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
232         if (ret < 0) {
233                 pr_debug("error %d reading FSR\n", ret);
234                 return ret;
235         }
236
237         return val;
238 }
239
240 /*
241  * Read configuration register, returning its value in the
242  * location. Return the configuration register value.
243  * Returns negative if error occurred.
244  */
245 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
246 static int read_cr(struct spi_nor *nor)
247 {
248         int ret;
249         u8 val;
250
251         ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
252         if (ret < 0) {
253                 dev_dbg(nor->dev, "error %d reading CR\n", ret);
254                 return ret;
255         }
256
257         return val;
258 }
259 #endif
260
261 /*
262  * Write status register 1 byte
263  * Returns negative if error occurred.
264  */
265 static int write_sr(struct spi_nor *nor, u8 val)
266 {
267         nor->cmd_buf[0] = val;
268         return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
269 }
270
271 /*
272  * Set write enable latch with Write Enable command.
273  * Returns negative if error occurred.
274  */
275 static int write_enable(struct spi_nor *nor)
276 {
277         return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
278 }
279
280 /*
281  * Send write disable instruction to the chip.
282  */
283 static int write_disable(struct spi_nor *nor)
284 {
285         return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
286 }
287
288 static struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
289 {
290         return mtd->priv;
291 }
292
293 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
294 {
295         size_t i;
296
297         for (i = 0; i < size; i++)
298                 if (table[i][0] == opcode)
299                         return table[i][1];
300
301         /* No conversion found, keep input op code. */
302         return opcode;
303 }
304
305 static u8 spi_nor_convert_3to4_read(u8 opcode)
306 {
307         static const u8 spi_nor_3to4_read[][2] = {
308                 { SPINOR_OP_READ,       SPINOR_OP_READ_4B },
309                 { SPINOR_OP_READ_FAST,  SPINOR_OP_READ_FAST_4B },
310                 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
311                 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
312                 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
313                 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
314
315                 { SPINOR_OP_READ_1_1_1_DTR,     SPINOR_OP_READ_1_1_1_DTR_4B },
316                 { SPINOR_OP_READ_1_2_2_DTR,     SPINOR_OP_READ_1_2_2_DTR_4B },
317                 { SPINOR_OP_READ_1_4_4_DTR,     SPINOR_OP_READ_1_4_4_DTR_4B },
318         };
319
320         return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
321                                       ARRAY_SIZE(spi_nor_3to4_read));
322 }
323
324 static u8 spi_nor_convert_3to4_program(u8 opcode)
325 {
326         static const u8 spi_nor_3to4_program[][2] = {
327                 { SPINOR_OP_PP,         SPINOR_OP_PP_4B },
328                 { SPINOR_OP_PP_1_1_4,   SPINOR_OP_PP_1_1_4_4B },
329                 { SPINOR_OP_PP_1_4_4,   SPINOR_OP_PP_1_4_4_4B },
330         };
331
332         return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
333                                       ARRAY_SIZE(spi_nor_3to4_program));
334 }
335
336 static u8 spi_nor_convert_3to4_erase(u8 opcode)
337 {
338         static const u8 spi_nor_3to4_erase[][2] = {
339                 { SPINOR_OP_BE_4K,      SPINOR_OP_BE_4K_4B },
340                 { SPINOR_OP_BE_32K,     SPINOR_OP_BE_32K_4B },
341                 { SPINOR_OP_SE,         SPINOR_OP_SE_4B },
342         };
343
344         return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
345                                       ARRAY_SIZE(spi_nor_3to4_erase));
346 }
347
348 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
349                                       const struct flash_info *info)
350 {
351         /* Do some manufacturer fixups first */
352         switch (JEDEC_MFR(info)) {
353         case SNOR_MFR_SPANSION:
354                 /* No small sector erase for 4-byte command set */
355                 nor->erase_opcode = SPINOR_OP_SE;
356                 nor->mtd.erasesize = info->sector_size;
357                 break;
358
359         default:
360                 break;
361         }
362
363         nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
364         nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
365         nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
366 }
367
368 /* Enable/disable 4-byte addressing mode. */
369 static int set_4byte(struct spi_nor *nor, const struct flash_info *info,
370                      int enable)
371 {
372         int status;
373         bool need_wren = false;
374         u8 cmd;
375
376         switch (JEDEC_MFR(info)) {
377         case SNOR_MFR_ST:
378         case SNOR_MFR_MICRON:
379                 /* Some Micron need WREN command; all will accept it */
380                 need_wren = true;
381         case SNOR_MFR_MACRONIX:
382         case SNOR_MFR_WINBOND:
383                 if (need_wren)
384                         write_enable(nor);
385
386                 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
387                 status = nor->write_reg(nor, cmd, NULL, 0);
388                 if (need_wren)
389                         write_disable(nor);
390
391                 if (!status && !enable &&
392                     JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
393                         /*
394                          * On Winbond W25Q256FV, leaving 4byte mode causes
395                          * the Extended Address Register to be set to 1, so all
396                          * 3-byte-address reads come from the second 16M.
397                          * We must clear the register to enable normal behavior.
398                          */
399                         write_enable(nor);
400                         nor->cmd_buf[0] = 0;
401                         nor->write_reg(nor, SPINOR_OP_WREAR, nor->cmd_buf, 1);
402                         write_disable(nor);
403                 }
404
405                 return status;
406         default:
407                 /* Spansion style */
408                 nor->cmd_buf[0] = enable << 7;
409                 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
410         }
411 }
412
413 static int spi_nor_sr_ready(struct spi_nor *nor)
414 {
415         int sr = read_sr(nor);
416
417         if (sr < 0)
418                 return sr;
419
420         if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) {
421                 if (sr & SR_E_ERR)
422                         dev_dbg(nor->dev, "Erase Error occurred\n");
423                 else
424                         dev_dbg(nor->dev, "Programming Error occurred\n");
425
426                 nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
427                 return -EIO;
428         }
429
430         return !(sr & SR_WIP);
431 }
432
433 static int spi_nor_fsr_ready(struct spi_nor *nor)
434 {
435         int fsr = read_fsr(nor);
436
437         if (fsr < 0)
438                 return fsr;
439
440         if (fsr & (FSR_E_ERR | FSR_P_ERR)) {
441                 if (fsr & FSR_E_ERR)
442                         dev_dbg(nor->dev, "Erase operation failed.\n");
443                 else
444                         dev_dbg(nor->dev, "Program operation failed.\n");
445
446                 if (fsr & FSR_PT_ERR)
447                         dev_dbg(nor->dev,
448                                 "Attempted to modify a protected sector.\n");
449
450                 nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0);
451                 return -EIO;
452         }
453
454         return fsr & FSR_READY;
455 }
456
457 static int spi_nor_ready(struct spi_nor *nor)
458 {
459         int sr, fsr;
460
461         sr = spi_nor_sr_ready(nor);
462         if (sr < 0)
463                 return sr;
464         fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
465         if (fsr < 0)
466                 return fsr;
467         return sr && fsr;
468 }
469
470 /*
471  * Service routine to read status register until ready, or timeout occurs.
472  * Returns non-zero if error.
473  */
474 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
475                                                 unsigned long timeout)
476 {
477         unsigned long timebase;
478         int ret;
479
480         timebase = get_timer(0);
481
482         while (get_timer(timebase) < timeout) {
483                 ret = spi_nor_ready(nor);
484                 if (ret < 0)
485                         return ret;
486                 if (ret)
487                         return 0;
488         }
489
490         dev_err(nor->dev, "flash operation timed out\n");
491
492         return -ETIMEDOUT;
493 }
494
495 static int spi_nor_wait_till_ready(struct spi_nor *nor)
496 {
497         return spi_nor_wait_till_ready_with_timeout(nor,
498                                                     DEFAULT_READY_WAIT_JIFFIES);
499 }
500
501 /*
502  * Initiate the erasure of a single sector
503  */
504 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
505 {
506         u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
507         int i;
508
509         if (nor->erase)
510                 return nor->erase(nor, addr);
511
512         /*
513          * Default implementation, if driver doesn't have a specialized HW
514          * control
515          */
516         for (i = nor->addr_width - 1; i >= 0; i--) {
517                 buf[i] = addr & 0xff;
518                 addr >>= 8;
519         }
520
521         return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
522 }
523
524 /*
525  * Erase an address range on the nor chip.  The address range may extend
526  * one or more erase sectors.  Return an error is there is a problem erasing.
527  */
528 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
529 {
530         struct spi_nor *nor = mtd_to_spi_nor(mtd);
531         u32 addr, len, rem;
532         int ret;
533
534         dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
535                 (long long)instr->len);
536
537         div_u64_rem(instr->len, mtd->erasesize, &rem);
538         if (rem)
539                 return -EINVAL;
540
541         addr = instr->addr;
542         len = instr->len;
543
544         while (len) {
545                 write_enable(nor);
546
547                 ret = spi_nor_erase_sector(nor, addr);
548                 if (ret)
549                         goto erase_err;
550
551                 addr += mtd->erasesize;
552                 len -= mtd->erasesize;
553
554                 ret = spi_nor_wait_till_ready(nor);
555                 if (ret)
556                         goto erase_err;
557         }
558
559         write_disable(nor);
560
561 erase_err:
562         return ret;
563 }
564
565 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
566 /* Write status register and ensure bits in mask match written values */
567 static int write_sr_and_check(struct spi_nor *nor, u8 status_new, u8 mask)
568 {
569         int ret;
570
571         write_enable(nor);
572         ret = write_sr(nor, status_new);
573         if (ret)
574                 return ret;
575
576         ret = spi_nor_wait_till_ready(nor);
577         if (ret)
578                 return ret;
579
580         ret = read_sr(nor);
581         if (ret < 0)
582                 return ret;
583
584         return ((ret & mask) != (status_new & mask)) ? -EIO : 0;
585 }
586
587 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
588                                  uint64_t *len)
589 {
590         struct mtd_info *mtd = &nor->mtd;
591         u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
592         int shift = ffs(mask) - 1;
593         int pow;
594
595         if (!(sr & mask)) {
596                 /* No protection */
597                 *ofs = 0;
598                 *len = 0;
599         } else {
600                 pow = ((sr & mask) ^ mask) >> shift;
601                 *len = mtd->size >> pow;
602                 if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
603                         *ofs = 0;
604                 else
605                         *ofs = mtd->size - *len;
606         }
607 }
608
609 /*
610  * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
611  * @locked is false); 0 otherwise
612  */
613 static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, u64 len,
614                                     u8 sr, bool locked)
615 {
616         loff_t lock_offs;
617         uint64_t lock_len;
618
619         if (!len)
620                 return 1;
621
622         stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
623
624         if (locked)
625                 /* Requested range is a sub-range of locked range */
626                 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
627         else
628                 /* Requested range does not overlap with locked range */
629                 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
630 }
631
632 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
633                             u8 sr)
634 {
635         return stm_check_lock_status_sr(nor, ofs, len, sr, true);
636 }
637
638 static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
639                               u8 sr)
640 {
641         return stm_check_lock_status_sr(nor, ofs, len, sr, false);
642 }
643
644 /*
645  * Lock a region of the flash. Compatible with ST Micro and similar flash.
646  * Supports the block protection bits BP{0,1,2} in the status register
647  * (SR). Does not support these features found in newer SR bitfields:
648  *   - SEC: sector/block protect - only handle SEC=0 (block protect)
649  *   - CMP: complement protect - only support CMP=0 (range is not complemented)
650  *
651  * Support for the following is provided conditionally for some flash:
652  *   - TB: top/bottom protect
653  *
654  * Sample table portion for 8MB flash (Winbond w25q64fw):
655  *
656  *   SEC  |  TB   |  BP2  |  BP1  |  BP0  |  Prot Length  | Protected Portion
657  *  --------------------------------------------------------------------------
658  *    X   |   X   |   0   |   0   |   0   |  NONE         | NONE
659  *    0   |   0   |   0   |   0   |   1   |  128 KB       | Upper 1/64
660  *    0   |   0   |   0   |   1   |   0   |  256 KB       | Upper 1/32
661  *    0   |   0   |   0   |   1   |   1   |  512 KB       | Upper 1/16
662  *    0   |   0   |   1   |   0   |   0   |  1 MB         | Upper 1/8
663  *    0   |   0   |   1   |   0   |   1   |  2 MB         | Upper 1/4
664  *    0   |   0   |   1   |   1   |   0   |  4 MB         | Upper 1/2
665  *    X   |   X   |   1   |   1   |   1   |  8 MB         | ALL
666  *  ------|-------|-------|-------|-------|---------------|-------------------
667  *    0   |   1   |   0   |   0   |   1   |  128 KB       | Lower 1/64
668  *    0   |   1   |   0   |   1   |   0   |  256 KB       | Lower 1/32
669  *    0   |   1   |   0   |   1   |   1   |  512 KB       | Lower 1/16
670  *    0   |   1   |   1   |   0   |   0   |  1 MB         | Lower 1/8
671  *    0   |   1   |   1   |   0   |   1   |  2 MB         | Lower 1/4
672  *    0   |   1   |   1   |   1   |   0   |  4 MB         | Lower 1/2
673  *
674  * Returns negative on errors, 0 on success.
675  */
676 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
677 {
678         struct mtd_info *mtd = &nor->mtd;
679         int status_old, status_new;
680         u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
681         u8 shift = ffs(mask) - 1, pow, val;
682         loff_t lock_len;
683         bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
684         bool use_top;
685
686         status_old = read_sr(nor);
687         if (status_old < 0)
688                 return status_old;
689
690         /* If nothing in our range is unlocked, we don't need to do anything */
691         if (stm_is_locked_sr(nor, ofs, len, status_old))
692                 return 0;
693
694         /* If anything below us is unlocked, we can't use 'bottom' protection */
695         if (!stm_is_locked_sr(nor, 0, ofs, status_old))
696                 can_be_bottom = false;
697
698         /* If anything above us is unlocked, we can't use 'top' protection */
699         if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
700                               status_old))
701                 can_be_top = false;
702
703         if (!can_be_bottom && !can_be_top)
704                 return -EINVAL;
705
706         /* Prefer top, if both are valid */
707         use_top = can_be_top;
708
709         /* lock_len: length of region that should end up locked */
710         if (use_top)
711                 lock_len = mtd->size - ofs;
712         else
713                 lock_len = ofs + len;
714
715         /*
716          * Need smallest pow such that:
717          *
718          *   1 / (2^pow) <= (len / size)
719          *
720          * so (assuming power-of-2 size) we do:
721          *
722          *   pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
723          */
724         pow = ilog2(mtd->size) - ilog2(lock_len);
725         val = mask - (pow << shift);
726         if (val & ~mask)
727                 return -EINVAL;
728         /* Don't "lock" with no region! */
729         if (!(val & mask))
730                 return -EINVAL;
731
732         status_new = (status_old & ~mask & ~SR_TB) | val;
733
734         /* Disallow further writes if WP pin is asserted */
735         status_new |= SR_SRWD;
736
737         if (!use_top)
738                 status_new |= SR_TB;
739
740         /* Don't bother if they're the same */
741         if (status_new == status_old)
742                 return 0;
743
744         /* Only modify protection if it will not unlock other areas */
745         if ((status_new & mask) < (status_old & mask))
746                 return -EINVAL;
747
748         return write_sr_and_check(nor, status_new, mask);
749 }
750
751 /*
752  * Unlock a region of the flash. See stm_lock() for more info
753  *
754  * Returns negative on errors, 0 on success.
755  */
756 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
757 {
758         struct mtd_info *mtd = &nor->mtd;
759         int status_old, status_new;
760         u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
761         u8 shift = ffs(mask) - 1, pow, val;
762         loff_t lock_len;
763         bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
764         bool use_top;
765
766         status_old = read_sr(nor);
767         if (status_old < 0)
768                 return status_old;
769
770         /* If nothing in our range is locked, we don't need to do anything */
771         if (stm_is_unlocked_sr(nor, ofs, len, status_old))
772                 return 0;
773
774         /* If anything below us is locked, we can't use 'top' protection */
775         if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
776                 can_be_top = false;
777
778         /* If anything above us is locked, we can't use 'bottom' protection */
779         if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
780                                 status_old))
781                 can_be_bottom = false;
782
783         if (!can_be_bottom && !can_be_top)
784                 return -EINVAL;
785
786         /* Prefer top, if both are valid */
787         use_top = can_be_top;
788
789         /* lock_len: length of region that should remain locked */
790         if (use_top)
791                 lock_len = mtd->size - (ofs + len);
792         else
793                 lock_len = ofs;
794
795         /*
796          * Need largest pow such that:
797          *
798          *   1 / (2^pow) >= (len / size)
799          *
800          * so (assuming power-of-2 size) we do:
801          *
802          *   pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
803          */
804         pow = ilog2(mtd->size) - order_base_2(lock_len);
805         if (lock_len == 0) {
806                 val = 0; /* fully unlocked */
807         } else {
808                 val = mask - (pow << shift);
809                 /* Some power-of-two sizes are not supported */
810                 if (val & ~mask)
811                         return -EINVAL;
812         }
813
814         status_new = (status_old & ~mask & ~SR_TB) | val;
815
816         /* Don't protect status register if we're fully unlocked */
817         if (lock_len == 0)
818                 status_new &= ~SR_SRWD;
819
820         if (!use_top)
821                 status_new |= SR_TB;
822
823         /* Don't bother if they're the same */
824         if (status_new == status_old)
825                 return 0;
826
827         /* Only modify protection if it will not lock other areas */
828         if ((status_new & mask) > (status_old & mask))
829                 return -EINVAL;
830
831         return write_sr_and_check(nor, status_new, mask);
832 }
833
834 /*
835  * Check if a region of the flash is (completely) locked. See stm_lock() for
836  * more info.
837  *
838  * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
839  * negative on errors.
840  */
841 static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
842 {
843         int status;
844
845         status = read_sr(nor);
846         if (status < 0)
847                 return status;
848
849         return stm_is_locked_sr(nor, ofs, len, status);
850 }
851 #endif /* CONFIG_SPI_FLASH_STMICRO */
852
853 /* Used when the "_ext_id" is two bytes at most */
854 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags)      \
855                 .id = {                                                 \
856                         ((_jedec_id) >> 16) & 0xff,                     \
857                         ((_jedec_id) >> 8) & 0xff,                      \
858                         (_jedec_id) & 0xff,                             \
859                         ((_ext_id) >> 8) & 0xff,                        \
860                         (_ext_id) & 0xff,                               \
861                         },                                              \
862                 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))),       \
863                 .sector_size = (_sector_size),                          \
864                 .n_sectors = (_n_sectors),                              \
865                 .page_size = 256,                                       \
866                 .flags = (_flags),
867
868 #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags)     \
869                 .id = {                                                 \
870                         ((_jedec_id) >> 16) & 0xff,                     \
871                         ((_jedec_id) >> 8) & 0xff,                      \
872                         (_jedec_id) & 0xff,                             \
873                         ((_ext_id) >> 16) & 0xff,                       \
874                         ((_ext_id) >> 8) & 0xff,                        \
875                         (_ext_id) & 0xff,                               \
876                         },                                              \
877                 .id_len = 6,                                            \
878                 .sector_size = (_sector_size),                          \
879                 .n_sectors = (_n_sectors),                              \
880                 .page_size = 256,                                       \
881                 .flags = (_flags),
882
883 /* NOTE: double check command sets and memory organization when you add
884  * more nor chips.  This current list focusses on newer chips, which
885  * have been converging on command sets which including JEDEC ID.
886  *
887  * All newly added entries should describe *hardware* and should use SECT_4K
888  * (or SECT_4K_PMC) if hardware supports erasing 4 KiB sectors. For usage
889  * scenarios excluding small sectors there is config option that can be
890  * disabled: CONFIG_MTD_SPI_NOR_USE_4K_SECTORS.
891  * For historical (and compatibility) reasons (before we got above config) some
892  * old entries may be missing 4K flag.
893  */
894 const struct flash_info spi_nor_ids[] = {
895 #ifdef CONFIG_SPI_FLASH_ATMEL           /* ATMEL */
896         /* Atmel -- some are (confusingly) marketed as "DataFlash" */
897         { "at26df321",  INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
898         { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
899
900         { "at45db011d", INFO(0x1f2200, 0, 64 * 1024,   4, SECT_4K) },
901         { "at45db021d", INFO(0x1f2300, 0, 64 * 1024,   8, SECT_4K) },
902         { "at45db041d", INFO(0x1f2400, 0, 64 * 1024,   8, SECT_4K) },
903         { "at45db081d", INFO(0x1f2500, 0, 64 * 1024,  16, SECT_4K) },
904         { "at45db161d", INFO(0x1f2600, 0, 64 * 1024,  32, SECT_4K) },
905         { "at45db321d", INFO(0x1f2700, 0, 64 * 1024,  64, SECT_4K) },
906         { "at45db641d", INFO(0x1f2800, 0, 64 * 1024, 128, SECT_4K) },
907         { "at26df081a", INFO(0x1f4501, 0, 64 * 1024,  16, SECT_4K) },
908 #endif
909 #ifdef CONFIG_SPI_FLASH_EON             /* EON */
910         /* EON -- en25xxx */
911         { "en25q32b",   INFO(0x1c3016, 0, 64 * 1024,   64, 0) },
912         { "en25q64",    INFO(0x1c3017, 0, 64 * 1024,  128, SECT_4K) },
913         { "en25qh128",  INFO(0x1c7018, 0, 64 * 1024,  256, 0) },
914         { "en25s64",    INFO(0x1c3817, 0, 64 * 1024,  128, SECT_4K) },
915 #endif
916 #ifdef CONFIG_SPI_FLASH_GIGADEVICE      /* GIGADEVICE */
917         /* GigaDevice */
918         {
919                 "gd25q16", INFO(0xc84015, 0, 64 * 1024,  32,
920                         SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
921                         SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
922         },
923         {
924                 "gd25q32", INFO(0xc84016, 0, 64 * 1024,  64,
925                         SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
926                         SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
927         },
928         {
929                 "gd25lq32", INFO(0xc86016, 0, 64 * 1024, 64,
930                         SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
931                         SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
932         },
933         {
934                 "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128,
935                         SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
936                         SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
937         },
938 #endif
939 #ifdef CONFIG_SPI_FLASH_ISSI            /* ISSI */
940         /* ISSI */
941         { "is25lq040b", INFO(0x9d4013, 0, 64 * 1024,   8,
942                         SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
943         { "is25lp032",  INFO(0x9d6016, 0, 64 * 1024,  64, 0) },
944         { "is25lp064",  INFO(0x9d6017, 0, 64 * 1024, 128, 0) },
945         { "is25lp128",  INFO(0x9d6018, 0, 64 * 1024, 256,
946                         SECT_4K | SPI_NOR_DUAL_READ) },
947         { "is25lp256",  INFO(0x9d6019, 0, 64 * 1024, 512,
948                         SECT_4K | SPI_NOR_DUAL_READ) },
949         { "is25wp032",  INFO(0x9d7016, 0, 64 * 1024,  64,
950                         SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
951         { "is25wp064",  INFO(0x9d7017, 0, 64 * 1024, 128,
952                         SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
953         { "is25wp128",  INFO(0x9d7018, 0, 64 * 1024, 256,
954                         SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
955 #endif
956 #ifdef CONFIG_SPI_FLASH_MACRONIX        /* MACRONIX */
957         /* Macronix */
958         { "mx25l2005a",  INFO(0xc22012, 0, 64 * 1024,   4, SECT_4K) },
959         { "mx25l4005a",  INFO(0xc22013, 0, 64 * 1024,   8, SECT_4K) },
960         { "mx25l8005",   INFO(0xc22014, 0, 64 * 1024,  16, 0) },
961         { "mx25l1606e",  INFO(0xc22015, 0, 64 * 1024,  32, SECT_4K) },
962         { "mx25l3205d",  INFO(0xc22016, 0, 64 * 1024,  64, SECT_4K) },
963         { "mx25l6405d",  INFO(0xc22017, 0, 64 * 1024, 128, SECT_4K) },
964         { "mx25u2033e",  INFO(0xc22532, 0, 64 * 1024,   4, SECT_4K) },
965         { "mx25u1635e",  INFO(0xc22535, 0, 64 * 1024,  32, SECT_4K) },
966         { "mx25u6435f",  INFO(0xc22537, 0, 64 * 1024, 128, SECT_4K) },
967         { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
968         { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
969         { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
970         { "mx25u25635f", INFO(0xc22539, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_4B_OPCODES) },
971         { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
972         { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
973         { "mx66u51235f", INFO(0xc2253a, 0, 64 * 1024, 1024, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
974         { "mx66l1g45g",  INFO(0xc2201b, 0, 64 * 1024, 2048, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
975         { "mx25l1633e",  INFO(0xc22415, 0, 64 * 1024,   32, SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES | SECT_4K) },
976 #endif
977
978 #ifdef CONFIG_SPI_FLASH_STMICRO         /* STMICRO */
979         /* Micron */
980         { "n25q016a",    INFO(0x20bb15, 0, 64 * 1024,   32, SECT_4K | SPI_NOR_QUAD_READ) },
981         { "n25q032",     INFO(0x20ba16, 0, 64 * 1024,   64, SPI_NOR_QUAD_READ) },
982         { "n25q032a",    INFO(0x20bb16, 0, 64 * 1024,   64, SPI_NOR_QUAD_READ) },
983         { "n25q064",     INFO(0x20ba17, 0, 64 * 1024,  128, SECT_4K | SPI_NOR_QUAD_READ) },
984         { "n25q064a",    INFO(0x20bb17, 0, 64 * 1024,  128, SECT_4K | SPI_NOR_QUAD_READ) },
985         { "n25q128a11",  INFO(0x20bb18, 0, 64 * 1024,  256, SECT_4K | SPI_NOR_QUAD_READ) },
986         { "n25q128a13",  INFO(0x20ba18, 0, 64 * 1024,  256, SECT_4K | SPI_NOR_QUAD_READ) },
987         { "n25q256a",    INFO(0x20ba19, 0, 64 * 1024,  512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
988         { "n25q256ax1",  INFO(0x20bb19, 0, 64 * 1024,  512, SECT_4K | SPI_NOR_QUAD_READ) },
989         { "n25q512a",    INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
990         { "n25q512ax3",  INFO(0x20ba20, 0, 64 * 1024, 1024, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ) },
991         { "n25q00",      INFO(0x20ba21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
992         { "n25q00a",     INFO(0x20bb21, 0, 64 * 1024, 2048, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
993         { "mt25qu02g",   INFO(0x20bb22, 0, 64 * 1024, 4096, SECT_4K | USE_FSR | SPI_NOR_QUAD_READ | NO_CHIP_ERASE) },
994 #endif
995 #ifdef CONFIG_SPI_FLASH_SPANSION        /* SPANSION */
996         /* Spansion/Cypress -- single (large) sector size only, at least
997          * for the chips listed here (without boot sectors).
998          */
999         { "s25sl032p",  INFO(0x010215, 0x4d00,  64 * 1024,  64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1000         { "s25sl064p",  INFO(0x010216, 0x4d00,  64 * 1024, 128, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1001         { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, USE_CLSR) },
1002         { "s25fl256s1", INFO(0x010219, 0x4d01,  64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1003         { "s25fl512s",  INFO6(0x010220, 0x4d0081, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1004         { "s25fl512s_256k",  INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1005         { "s25fl512s_64k",  INFO(0x010220, 0x4d01, 64 * 1024, 1024, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1006         { "s25fl512s_512k",  INFO(0x010220, 0x4f00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1007         { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024,  64, 0) },
1008         { "s25sl12801", INFO(0x012018, 0x0301,  64 * 1024, 256, 0) },
1009         { "s25fl128s",  INFO6(0x012018, 0x4d0180, 64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1010         { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024,  64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1011         { "s25fl129p1", INFO(0x012018, 0x4d01,  64 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | USE_CLSR) },
1012         { "s25sl008a",  INFO(0x010213,      0,  64 * 1024,  16, 0) },
1013         { "s25sl016a",  INFO(0x010214,      0,  64 * 1024,  32, 0) },
1014         { "s25sl032a",  INFO(0x010215,      0,  64 * 1024,  64, 0) },
1015         { "s25sl064a",  INFO(0x010216,      0,  64 * 1024, 128, 0) },
1016         { "s25fl116k",  INFO(0x014015,      0,  64 * 1024,  32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1017         { "s25fl164k",  INFO(0x014017,      0,  64 * 1024, 128, SECT_4K) },
1018         { "s25fl208k",  INFO(0x014014,      0,  64 * 1024,  16, SECT_4K | SPI_NOR_DUAL_READ) },
1019         { "s25fl128l",  INFO(0x016018,      0,  64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ | SPI_NOR_4B_OPCODES) },
1020 #endif
1021 #ifdef CONFIG_SPI_FLASH_SST             /* SST */
1022         /* SST -- large erase sizes are "overlays", "sectors" are 4K */
1023         { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024,  8, SECT_4K | SST_WRITE) },
1024         { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
1025         { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
1026         { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
1027         { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
1028         { "sst25wf512",  INFO(0xbf2501, 0, 64 * 1024,  1, SECT_4K | SST_WRITE) },
1029         { "sst25wf010",  INFO(0xbf2502, 0, 64 * 1024,  2, SECT_4K | SST_WRITE) },
1030         { "sst25wf020",  INFO(0xbf2503, 0, 64 * 1024,  4, SECT_4K | SST_WRITE) },
1031         { "sst25wf020a", INFO(0x621612, 0, 64 * 1024,  4, SECT_4K) },
1032         { "sst25wf040b", INFO(0x621613, 0, 64 * 1024,  8, SECT_4K) },
1033         { "sst25wf040",  INFO(0xbf2504, 0, 64 * 1024,  8, SECT_4K | SST_WRITE) },
1034         { "sst25wf080",  INFO(0xbf2505, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
1035         { "sst26vf064b", INFO(0xbf2643, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1036         { "sst26wf016",  INFO(0xbf2651, 0, 64 * 1024,  32, SECT_4K) },
1037         { "sst26wf032",  INFO(0xbf2622, 0, 64 * 1024,  64, SECT_4K) },
1038         { "sst26wf064",  INFO(0xbf2643, 0, 64 * 1024, 128, SECT_4K) },
1039 #endif
1040 #ifdef CONFIG_SPI_FLASH_STMICRO         /* STMICRO */
1041         /* ST Microelectronics -- newer production may have feature updates */
1042         { "m25p10",  INFO(0x202011,  0,  32 * 1024,   4, 0) },
1043         { "m25p20",  INFO(0x202012,  0,  64 * 1024,   4, 0) },
1044         { "m25p40",  INFO(0x202013,  0,  64 * 1024,   8, 0) },
1045         { "m25p80",  INFO(0x202014,  0,  64 * 1024,  16, 0) },
1046         { "m25p16",  INFO(0x202015,  0,  64 * 1024,  32, 0) },
1047         { "m25p32",  INFO(0x202016,  0,  64 * 1024,  64, 0) },
1048         { "m25p64",  INFO(0x202017,  0,  64 * 1024, 128, 0) },
1049         { "m25p128", INFO(0x202018,  0, 256 * 1024,  64, 0) },
1050         { "m25pe16", INFO(0x208015,  0, 64 * 1024, 32, SECT_4K) },
1051         { "m25px16",    INFO(0x207115,  0, 64 * 1024, 32, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1052         { "m25px64",    INFO(0x207117,  0, 64 * 1024, 128, 0) },
1053 #endif
1054 #ifdef CONFIG_SPI_FLASH_WINBOND         /* WINBOND */
1055         /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
1056         { "w25x05", INFO(0xef3010, 0, 64 * 1024,  1,  SECT_4K) },
1057         { "w25x10", INFO(0xef3011, 0, 64 * 1024,  2,  SECT_4K) },
1058         { "w25x20", INFO(0xef3012, 0, 64 * 1024,  4,  SECT_4K) },
1059         { "w25x40", INFO(0xef3013, 0, 64 * 1024,  8,  SECT_4K) },
1060         { "w25x80", INFO(0xef3014, 0, 64 * 1024,  16, SECT_4K) },
1061         { "w25x16", INFO(0xef3015, 0, 64 * 1024,  32, SECT_4K) },
1062         {
1063                 "w25q16dw", INFO(0xef6015, 0, 64 * 1024,  32,
1064                         SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1065                         SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1066         },
1067         { "w25x32", INFO(0xef3016, 0, 64 * 1024,  64, SECT_4K) },
1068         { "w25q20cl", INFO(0xef4012, 0, 64 * 1024,  4, SECT_4K) },
1069         { "w25q20bw", INFO(0xef5012, 0, 64 * 1024,  4, SECT_4K) },
1070         { "w25q20ew", INFO(0xef6012, 0, 64 * 1024,  4, SECT_4K) },
1071         { "w25q32", INFO(0xef4016, 0, 64 * 1024,  64, SECT_4K) },
1072         {
1073                 "w25q32dw", INFO(0xef6016, 0, 64 * 1024,  64,
1074                         SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1075                         SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1076         },
1077         {
1078                 "w25q32jv", INFO(0xef7016, 0, 64 * 1024,  64,
1079                         SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1080                         SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1081         },
1082         { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
1083         { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
1084         {
1085                 "w25q64dw", INFO(0xef6017, 0, 64 * 1024, 128,
1086                         SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1087                         SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1088         },
1089         {
1090                 "w25q128fw", INFO(0xef6018, 0, 64 * 1024, 256,
1091                         SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
1092                         SPI_NOR_HAS_LOCK | SPI_NOR_HAS_TB)
1093         },
1094         { "w25q80", INFO(0xef5014, 0, 64 * 1024,  16, SECT_4K) },
1095         { "w25q80bl", INFO(0xef4014, 0, 64 * 1024,  16, SECT_4K) },
1096         { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
1097         { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1098         { "w25m512jv", INFO(0xef7119, 0, 64 * 1024, 1024,
1099                         SECT_4K | SPI_NOR_QUAD_READ | SPI_NOR_DUAL_READ) },
1100 #endif
1101 #ifdef CONFIG_SPI_FLASH_XMC
1102         /* XMC (Wuhan Xinxin Semiconductor Manufacturing Corp.) */
1103         { "XM25QH64A", INFO(0x207017, 0, 64 * 1024, 128, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1104         { "XM25QH128A", INFO(0x207018, 0, 64 * 1024, 256, SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
1105 #endif
1106         { },
1107 };
1108
1109 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
1110 {
1111         int                     tmp;
1112         u8                      id[SPI_NOR_MAX_ID_LEN];
1113         const struct flash_info *info;
1114
1115         if (!ARRAY_SIZE(spi_nor_ids))
1116                 return ERR_PTR(-ENODEV);
1117
1118         tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
1119         if (tmp < 0) {
1120                 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
1121                 return ERR_PTR(tmp);
1122         }
1123
1124         for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
1125                 info = &spi_nor_ids[tmp];
1126                 if (info->id_len) {
1127                         if (!memcmp(info->id, id, info->id_len))
1128                                 return &spi_nor_ids[tmp];
1129                 }
1130         }
1131         dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
1132                 id[0], id[1], id[2]);
1133         return ERR_PTR(-ENODEV);
1134 }
1135
1136 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
1137                         size_t *retlen, u_char *buf)
1138 {
1139         struct spi_nor *nor = mtd_to_spi_nor(mtd);
1140         int ret;
1141
1142         dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
1143
1144         while (len) {
1145                 loff_t addr = from;
1146
1147                 ret = nor->read(nor, addr, len, buf);
1148                 if (ret == 0) {
1149                         /* We shouldn't see 0-length reads */
1150                         ret = -EIO;
1151                         goto read_err;
1152                 }
1153                 if (ret < 0)
1154                         goto read_err;
1155
1156                 *retlen += ret;
1157                 buf += ret;
1158                 from += ret;
1159                 len -= ret;
1160         }
1161         ret = 0;
1162
1163 read_err:
1164         return ret;
1165 }
1166
1167 #ifdef CONFIG_SPI_FLASH_SST
1168 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
1169                      size_t *retlen, const u_char *buf)
1170 {
1171         struct spi_nor *nor = mtd_to_spi_nor(mtd);
1172         size_t actual;
1173         int ret;
1174
1175         dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1176
1177         write_enable(nor);
1178
1179         nor->sst_write_second = false;
1180
1181         actual = to % 2;
1182         /* Start write from odd address. */
1183         if (actual) {
1184                 nor->program_opcode = SPINOR_OP_BP;
1185
1186                 /* write one byte. */
1187                 ret = nor->write(nor, to, 1, buf);
1188                 if (ret < 0)
1189                         goto sst_write_err;
1190                 ret = spi_nor_wait_till_ready(nor);
1191                 if (ret)
1192                         goto sst_write_err;
1193         }
1194         to += actual;
1195
1196         /* Write out most of the data here. */
1197         for (; actual < len - 1; actual += 2) {
1198                 nor->program_opcode = SPINOR_OP_AAI_WP;
1199
1200                 /* write two bytes. */
1201                 ret = nor->write(nor, to, 2, buf + actual);
1202                 if (ret < 0)
1203                         goto sst_write_err;
1204                 ret = spi_nor_wait_till_ready(nor);
1205                 if (ret)
1206                         goto sst_write_err;
1207                 to += 2;
1208                 nor->sst_write_second = true;
1209         }
1210         nor->sst_write_second = false;
1211
1212         write_disable(nor);
1213         ret = spi_nor_wait_till_ready(nor);
1214         if (ret)
1215                 goto sst_write_err;
1216
1217         /* Write out trailing byte if it exists. */
1218         if (actual != len) {
1219                 write_enable(nor);
1220
1221                 nor->program_opcode = SPINOR_OP_BP;
1222                 ret = nor->write(nor, to, 1, buf + actual);
1223                 if (ret < 0)
1224                         goto sst_write_err;
1225                 ret = spi_nor_wait_till_ready(nor);
1226                 if (ret)
1227                         goto sst_write_err;
1228                 write_disable(nor);
1229                 actual += 1;
1230         }
1231 sst_write_err:
1232         *retlen += actual;
1233         return ret;
1234 }
1235 #endif
1236 /*
1237  * Write an address range to the nor chip.  Data must be written in
1238  * FLASH_PAGESIZE chunks.  The address range may be any size provided
1239  * it is within the physical boundaries.
1240  */
1241 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1242         size_t *retlen, const u_char *buf)
1243 {
1244         struct spi_nor *nor = mtd_to_spi_nor(mtd);
1245         size_t page_offset, page_remain, i;
1246         ssize_t ret;
1247
1248         dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1249
1250         for (i = 0; i < len; ) {
1251                 ssize_t written;
1252                 loff_t addr = to + i;
1253
1254                 /*
1255                  * If page_size is a power of two, the offset can be quickly
1256                  * calculated with an AND operation. On the other cases we
1257                  * need to do a modulus operation (more expensive).
1258                  * Power of two numbers have only one bit set and we can use
1259                  * the instruction hweight32 to detect if we need to do a
1260                  * modulus (do_div()) or not.
1261                  */
1262                 if (hweight32(nor->page_size) == 1) {
1263                         page_offset = addr & (nor->page_size - 1);
1264                 } else {
1265                         u64 aux = addr;
1266
1267                         page_offset = do_div(aux, nor->page_size);
1268                 }
1269                 /* the size of data remaining on the first page */
1270                 page_remain = min_t(size_t,
1271                                     nor->page_size - page_offset, len - i);
1272
1273                 write_enable(nor);
1274                 ret = nor->write(nor, addr, page_remain, buf + i);
1275                 if (ret < 0)
1276                         goto write_err;
1277                 written = ret;
1278
1279                 ret = spi_nor_wait_till_ready(nor);
1280                 if (ret)
1281                         goto write_err;
1282                 *retlen += written;
1283                 i += written;
1284                 if (written != page_remain) {
1285                         ret = -EIO;
1286                         goto write_err;
1287                 }
1288         }
1289
1290 write_err:
1291         return ret;
1292 }
1293
1294 #ifdef CONFIG_SPI_FLASH_MACRONIX
1295 /**
1296  * macronix_quad_enable() - set QE bit in Status Register.
1297  * @nor:        pointer to a 'struct spi_nor'
1298  *
1299  * Set the Quad Enable (QE) bit in the Status Register.
1300  *
1301  * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
1302  *
1303  * Return: 0 on success, -errno otherwise.
1304  */
1305 static int macronix_quad_enable(struct spi_nor *nor)
1306 {
1307         int ret, val;
1308
1309         val = read_sr(nor);
1310         if (val < 0)
1311                 return val;
1312         if (val & SR_QUAD_EN_MX)
1313                 return 0;
1314
1315         write_enable(nor);
1316
1317         write_sr(nor, val | SR_QUAD_EN_MX);
1318
1319         ret = spi_nor_wait_till_ready(nor);
1320         if (ret)
1321                 return ret;
1322
1323         ret = read_sr(nor);
1324         if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
1325                 dev_err(nor->dev, "Macronix Quad bit not set\n");
1326                 return -EINVAL;
1327         }
1328
1329         return 0;
1330 }
1331 #endif
1332
1333 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
1334 /*
1335  * Write status Register and configuration register with 2 bytes
1336  * The first byte will be written to the status register, while the
1337  * second byte will be written to the configuration register.
1338  * Return negative if error occurred.
1339  */
1340 static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
1341 {
1342         int ret;
1343
1344         write_enable(nor);
1345
1346         ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
1347         if (ret < 0) {
1348                 dev_dbg(nor->dev,
1349                         "error while writing configuration register\n");
1350                 return -EINVAL;
1351         }
1352
1353         ret = spi_nor_wait_till_ready(nor);
1354         if (ret) {
1355                 dev_dbg(nor->dev,
1356                         "timeout while writing configuration register\n");
1357                 return ret;
1358         }
1359
1360         return 0;
1361 }
1362
1363 /**
1364  * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
1365  * @nor:        pointer to a 'struct spi_nor'
1366  *
1367  * Set the Quad Enable (QE) bit in the Configuration Register.
1368  * This function should be used with QSPI memories supporting the Read
1369  * Configuration Register (35h) instruction.
1370  *
1371  * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1372  * memories.
1373  *
1374  * Return: 0 on success, -errno otherwise.
1375  */
1376 static int spansion_read_cr_quad_enable(struct spi_nor *nor)
1377 {
1378         u8 sr_cr[2];
1379         int ret;
1380
1381         /* Check current Quad Enable bit value. */
1382         ret = read_cr(nor);
1383         if (ret < 0) {
1384                 dev_dbg(dev, "error while reading configuration register\n");
1385                 return -EINVAL;
1386         }
1387
1388         if (ret & CR_QUAD_EN_SPAN)
1389                 return 0;
1390
1391         sr_cr[1] = ret | CR_QUAD_EN_SPAN;
1392
1393         /* Keep the current value of the Status Register. */
1394         ret = read_sr(nor);
1395         if (ret < 0) {
1396                 dev_dbg(dev, "error while reading status register\n");
1397                 return -EINVAL;
1398         }
1399         sr_cr[0] = ret;
1400
1401         ret = write_sr_cr(nor, sr_cr);
1402         if (ret)
1403                 return ret;
1404
1405         /* Read back and check it. */
1406         ret = read_cr(nor);
1407         if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1408                 dev_dbg(nor->dev, "Spansion Quad bit not set\n");
1409                 return -EINVAL;
1410         }
1411
1412         return 0;
1413 }
1414 #endif /* CONFIG_SPI_FLASH_SPANSION */
1415
1416 struct spi_nor_read_command {
1417         u8                      num_mode_clocks;
1418         u8                      num_wait_states;
1419         u8                      opcode;
1420         enum spi_nor_protocol   proto;
1421 };
1422
1423 struct spi_nor_pp_command {
1424         u8                      opcode;
1425         enum spi_nor_protocol   proto;
1426 };
1427
1428 enum spi_nor_read_command_index {
1429         SNOR_CMD_READ,
1430         SNOR_CMD_READ_FAST,
1431         SNOR_CMD_READ_1_1_1_DTR,
1432
1433         /* Dual SPI */
1434         SNOR_CMD_READ_1_1_2,
1435         SNOR_CMD_READ_1_2_2,
1436         SNOR_CMD_READ_2_2_2,
1437         SNOR_CMD_READ_1_2_2_DTR,
1438
1439         /* Quad SPI */
1440         SNOR_CMD_READ_1_1_4,
1441         SNOR_CMD_READ_1_4_4,
1442         SNOR_CMD_READ_4_4_4,
1443         SNOR_CMD_READ_1_4_4_DTR,
1444
1445         /* Octo SPI */
1446         SNOR_CMD_READ_1_1_8,
1447         SNOR_CMD_READ_1_8_8,
1448         SNOR_CMD_READ_8_8_8,
1449         SNOR_CMD_READ_1_8_8_DTR,
1450
1451         SNOR_CMD_READ_MAX
1452 };
1453
1454 enum spi_nor_pp_command_index {
1455         SNOR_CMD_PP,
1456
1457         /* Quad SPI */
1458         SNOR_CMD_PP_1_1_4,
1459         SNOR_CMD_PP_1_4_4,
1460         SNOR_CMD_PP_4_4_4,
1461
1462         /* Octo SPI */
1463         SNOR_CMD_PP_1_1_8,
1464         SNOR_CMD_PP_1_8_8,
1465         SNOR_CMD_PP_8_8_8,
1466
1467         SNOR_CMD_PP_MAX
1468 };
1469
1470 struct spi_nor_flash_parameter {
1471         u64                             size;
1472         u32                             page_size;
1473
1474         struct spi_nor_hwcaps           hwcaps;
1475         struct spi_nor_read_command     reads[SNOR_CMD_READ_MAX];
1476         struct spi_nor_pp_command       page_programs[SNOR_CMD_PP_MAX];
1477
1478         int (*quad_enable)(struct spi_nor *nor);
1479 };
1480
1481 static void
1482 spi_nor_set_read_settings(struct spi_nor_read_command *read,
1483                           u8 num_mode_clocks,
1484                           u8 num_wait_states,
1485                           u8 opcode,
1486                           enum spi_nor_protocol proto)
1487 {
1488         read->num_mode_clocks = num_mode_clocks;
1489         read->num_wait_states = num_wait_states;
1490         read->opcode = opcode;
1491         read->proto = proto;
1492 }
1493
1494 static void
1495 spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
1496                         u8 opcode,
1497                         enum spi_nor_protocol proto)
1498 {
1499         pp->opcode = opcode;
1500         pp->proto = proto;
1501 }
1502
1503 static int spi_nor_init_params(struct spi_nor *nor,
1504                                const struct flash_info *info,
1505                                struct spi_nor_flash_parameter *params)
1506 {
1507         /* Set legacy flash parameters as default. */
1508         memset(params, 0, sizeof(*params));
1509
1510         /* Set SPI NOR sizes. */
1511         params->size = info->sector_size * info->n_sectors;
1512         params->page_size = info->page_size;
1513
1514         /* (Fast) Read settings. */
1515         params->hwcaps.mask |= SNOR_HWCAPS_READ;
1516         spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
1517                                   0, 0, SPINOR_OP_READ,
1518                                   SNOR_PROTO_1_1_1);
1519
1520         if (!(info->flags & SPI_NOR_NO_FR)) {
1521                 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
1522                 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
1523                                           0, 8, SPINOR_OP_READ_FAST,
1524                                           SNOR_PROTO_1_1_1);
1525         }
1526
1527         if (info->flags & SPI_NOR_DUAL_READ) {
1528                 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
1529                 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_2],
1530                                           0, 8, SPINOR_OP_READ_1_1_2,
1531                                           SNOR_PROTO_1_1_2);
1532         }
1533
1534         if (info->flags & SPI_NOR_QUAD_READ) {
1535                 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
1536                 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
1537                                           0, 8, SPINOR_OP_READ_1_1_4,
1538                                           SNOR_PROTO_1_1_4);
1539         }
1540
1541         /* Page Program settings. */
1542         params->hwcaps.mask |= SNOR_HWCAPS_PP;
1543         spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP],
1544                                 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
1545
1546         if (info->flags & SPI_NOR_QUAD_READ) {
1547                 params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
1548                 spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_1_1_4],
1549                                         SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4);
1550         }
1551
1552         /* Select the procedure to set the Quad Enable bit. */
1553         if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
1554                                    SNOR_HWCAPS_PP_QUAD)) {
1555                 switch (JEDEC_MFR(info)) {
1556 #ifdef CONFIG_SPI_FLASH_MACRONIX
1557                 case SNOR_MFR_MACRONIX:
1558                         params->quad_enable = macronix_quad_enable;
1559                         break;
1560 #endif
1561                 case SNOR_MFR_ST:
1562                 case SNOR_MFR_MICRON:
1563                         break;
1564
1565                 default:
1566 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
1567                         /* Kept only for backward compatibility purpose. */
1568                         params->quad_enable = spansion_read_cr_quad_enable;
1569 #endif
1570                         break;
1571                 }
1572         }
1573 }
1574
1575 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
1576 {
1577         size_t i;
1578
1579         for (i = 0; i < size; i++)
1580                 if (table[i][0] == (int)hwcaps)
1581                         return table[i][1];
1582
1583         return -EINVAL;
1584 }
1585
1586 static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
1587 {
1588         static const int hwcaps_read2cmd[][2] = {
1589                 { SNOR_HWCAPS_READ,             SNOR_CMD_READ },
1590                 { SNOR_HWCAPS_READ_FAST,        SNOR_CMD_READ_FAST },
1591                 { SNOR_HWCAPS_READ_1_1_1_DTR,   SNOR_CMD_READ_1_1_1_DTR },
1592                 { SNOR_HWCAPS_READ_1_1_2,       SNOR_CMD_READ_1_1_2 },
1593                 { SNOR_HWCAPS_READ_1_2_2,       SNOR_CMD_READ_1_2_2 },
1594                 { SNOR_HWCAPS_READ_2_2_2,       SNOR_CMD_READ_2_2_2 },
1595                 { SNOR_HWCAPS_READ_1_2_2_DTR,   SNOR_CMD_READ_1_2_2_DTR },
1596                 { SNOR_HWCAPS_READ_1_1_4,       SNOR_CMD_READ_1_1_4 },
1597                 { SNOR_HWCAPS_READ_1_4_4,       SNOR_CMD_READ_1_4_4 },
1598                 { SNOR_HWCAPS_READ_4_4_4,       SNOR_CMD_READ_4_4_4 },
1599                 { SNOR_HWCAPS_READ_1_4_4_DTR,   SNOR_CMD_READ_1_4_4_DTR },
1600                 { SNOR_HWCAPS_READ_1_1_8,       SNOR_CMD_READ_1_1_8 },
1601                 { SNOR_HWCAPS_READ_1_8_8,       SNOR_CMD_READ_1_8_8 },
1602                 { SNOR_HWCAPS_READ_8_8_8,       SNOR_CMD_READ_8_8_8 },
1603                 { SNOR_HWCAPS_READ_1_8_8_DTR,   SNOR_CMD_READ_1_8_8_DTR },
1604         };
1605
1606         return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
1607                                   ARRAY_SIZE(hwcaps_read2cmd));
1608 }
1609
1610 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
1611 {
1612         static const int hwcaps_pp2cmd[][2] = {
1613                 { SNOR_HWCAPS_PP,               SNOR_CMD_PP },
1614                 { SNOR_HWCAPS_PP_1_1_4,         SNOR_CMD_PP_1_1_4 },
1615                 { SNOR_HWCAPS_PP_1_4_4,         SNOR_CMD_PP_1_4_4 },
1616                 { SNOR_HWCAPS_PP_4_4_4,         SNOR_CMD_PP_4_4_4 },
1617                 { SNOR_HWCAPS_PP_1_1_8,         SNOR_CMD_PP_1_1_8 },
1618                 { SNOR_HWCAPS_PP_1_8_8,         SNOR_CMD_PP_1_8_8 },
1619                 { SNOR_HWCAPS_PP_8_8_8,         SNOR_CMD_PP_8_8_8 },
1620         };
1621
1622         return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
1623                                   ARRAY_SIZE(hwcaps_pp2cmd));
1624 }
1625
1626 static int spi_nor_select_read(struct spi_nor *nor,
1627                                const struct spi_nor_flash_parameter *params,
1628                                u32 shared_hwcaps)
1629 {
1630         int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
1631         const struct spi_nor_read_command *read;
1632
1633         if (best_match < 0)
1634                 return -EINVAL;
1635
1636         cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
1637         if (cmd < 0)
1638                 return -EINVAL;
1639
1640         read = &params->reads[cmd];
1641         nor->read_opcode = read->opcode;
1642         nor->read_proto = read->proto;
1643
1644         /*
1645          * In the spi-nor framework, we don't need to make the difference
1646          * between mode clock cycles and wait state clock cycles.
1647          * Indeed, the value of the mode clock cycles is used by a QSPI
1648          * flash memory to know whether it should enter or leave its 0-4-4
1649          * (Continuous Read / XIP) mode.
1650          * eXecution In Place is out of the scope of the mtd sub-system.
1651          * Hence we choose to merge both mode and wait state clock cycles
1652          * into the so called dummy clock cycles.
1653          */
1654         nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
1655         return 0;
1656 }
1657
1658 static int spi_nor_select_pp(struct spi_nor *nor,
1659                              const struct spi_nor_flash_parameter *params,
1660                              u32 shared_hwcaps)
1661 {
1662         int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
1663         const struct spi_nor_pp_command *pp;
1664
1665         if (best_match < 0)
1666                 return -EINVAL;
1667
1668         cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
1669         if (cmd < 0)
1670                 return -EINVAL;
1671
1672         pp = &params->page_programs[cmd];
1673         nor->program_opcode = pp->opcode;
1674         nor->write_proto = pp->proto;
1675         return 0;
1676 }
1677
1678 static int spi_nor_select_erase(struct spi_nor *nor,
1679                                 const struct flash_info *info)
1680 {
1681         struct mtd_info *mtd = &nor->mtd;
1682
1683 #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
1684         /* prefer "small sector" erase if possible */
1685         if (info->flags & SECT_4K) {
1686                 nor->erase_opcode = SPINOR_OP_BE_4K;
1687                 mtd->erasesize = 4096;
1688         } else if (info->flags & SECT_4K_PMC) {
1689                 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
1690                 mtd->erasesize = 4096;
1691         } else
1692 #endif
1693         {
1694                 nor->erase_opcode = SPINOR_OP_SE;
1695                 mtd->erasesize = info->sector_size;
1696         }
1697         return 0;
1698 }
1699
1700 static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
1701                          const struct spi_nor_flash_parameter *params,
1702                          const struct spi_nor_hwcaps *hwcaps)
1703 {
1704         u32 ignored_mask, shared_mask;
1705         bool enable_quad_io;
1706         int err;
1707
1708         /*
1709          * Keep only the hardware capabilities supported by both the SPI
1710          * controller and the SPI flash memory.
1711          */
1712         shared_mask = hwcaps->mask & params->hwcaps.mask;
1713
1714         /* SPI n-n-n protocols are not supported yet. */
1715         ignored_mask = (SNOR_HWCAPS_READ_2_2_2 |
1716                         SNOR_HWCAPS_READ_4_4_4 |
1717                         SNOR_HWCAPS_READ_8_8_8 |
1718                         SNOR_HWCAPS_PP_4_4_4 |
1719                         SNOR_HWCAPS_PP_8_8_8);
1720         if (shared_mask & ignored_mask) {
1721                 dev_dbg(nor->dev,
1722                         "SPI n-n-n protocols are not supported yet.\n");
1723                 shared_mask &= ~ignored_mask;
1724         }
1725
1726         /* Select the (Fast) Read command. */
1727         err = spi_nor_select_read(nor, params, shared_mask);
1728         if (err) {
1729                 dev_dbg(nor->dev,
1730                         "can't select read settings supported by both the SPI controller and memory.\n");
1731                 return err;
1732         }
1733
1734         /* Select the Page Program command. */
1735         err = spi_nor_select_pp(nor, params, shared_mask);
1736         if (err) {
1737                 dev_dbg(nor->dev,
1738                         "can't select write settings supported by both the SPI controller and memory.\n");
1739                 return err;
1740         }
1741
1742         /* Select the Sector Erase command. */
1743         err = spi_nor_select_erase(nor, info);
1744         if (err) {
1745                 dev_dbg(nor->dev,
1746                         "can't select erase settings supported by both the SPI controller and memory.\n");
1747                 return err;
1748         }
1749
1750         /* Enable Quad I/O if needed. */
1751         enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
1752                           spi_nor_get_protocol_width(nor->write_proto) == 4);
1753         if (enable_quad_io && params->quad_enable)
1754                 nor->quad_enable = params->quad_enable;
1755         else
1756                 nor->quad_enable = NULL;
1757
1758         return 0;
1759 }
1760
1761 static int spi_nor_init(struct spi_nor *nor)
1762 {
1763         int err;
1764
1765         /*
1766          * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
1767          * with the software protection bits set
1768          */
1769         if (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
1770             JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
1771             JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
1772             nor->info->flags & SPI_NOR_HAS_LOCK) {
1773                 write_enable(nor);
1774                 write_sr(nor, 0);
1775                 spi_nor_wait_till_ready(nor);
1776         }
1777
1778         if (nor->quad_enable) {
1779                 err = nor->quad_enable(nor);
1780                 if (err) {
1781                         dev_dbg(nor->dev, "quad mode not supported\n");
1782                         return err;
1783                 }
1784         }
1785
1786         if (nor->addr_width == 4 &&
1787             (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
1788             !(nor->info->flags & SPI_NOR_4B_OPCODES)) {
1789                 /*
1790                  * If the RESET# pin isn't hooked up properly, or the system
1791                  * otherwise doesn't perform a reset command in the boot
1792                  * sequence, it's impossible to 100% protect against unexpected
1793                  * reboots (e.g., crashes). Warn the user (or hopefully, system
1794                  * designer) that this is bad.
1795                  */
1796                 if (nor->flags & SNOR_F_BROKEN_RESET)
1797                         printf("enabling reset hack; may not recover from unexpected reboots\n");
1798                 set_4byte(nor, nor->info, 1);
1799         }
1800
1801         return 0;
1802 }
1803
1804 int spi_nor_scan(struct spi_nor *nor)
1805 {
1806         struct spi_nor_flash_parameter params;
1807         const struct flash_info *info = NULL;
1808         struct mtd_info *mtd = &nor->mtd;
1809         struct spi_nor_hwcaps hwcaps = {
1810                 .mask = SNOR_HWCAPS_READ |
1811                         SNOR_HWCAPS_READ_FAST |
1812                         SNOR_HWCAPS_PP,
1813         };
1814         struct spi_slave *spi = nor->spi;
1815         int ret;
1816
1817         /* Reset SPI protocol for all commands. */
1818         nor->reg_proto = SNOR_PROTO_1_1_1;
1819         nor->read_proto = SNOR_PROTO_1_1_1;
1820         nor->write_proto = SNOR_PROTO_1_1_1;
1821         nor->read = spi_nor_read_data;
1822         nor->write = spi_nor_write_data;
1823         nor->read_reg = spi_nor_read_reg;
1824         nor->write_reg = spi_nor_write_reg;
1825
1826         if (spi->mode & SPI_RX_QUAD) {
1827                 hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
1828
1829                 if (spi->mode & SPI_TX_QUAD)
1830                         hwcaps.mask |= (SNOR_HWCAPS_READ_1_4_4 |
1831                                         SNOR_HWCAPS_PP_1_1_4 |
1832                                         SNOR_HWCAPS_PP_1_4_4);
1833         } else if (spi->mode & SPI_RX_DUAL) {
1834                 hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
1835
1836                 if (spi->mode & SPI_TX_DUAL)
1837                         hwcaps.mask |= SNOR_HWCAPS_READ_1_2_2;
1838         }
1839
1840         info = spi_nor_read_id(nor);
1841         if (IS_ERR_OR_NULL(info))
1842                 return -ENOENT;
1843
1844         ret = spi_nor_init_params(nor, info, &params);
1845         if (ret)
1846                 return ret;
1847
1848         if (!mtd->name)
1849                 mtd->name = info->name;
1850         mtd->priv = nor;
1851         mtd->type = MTD_NORFLASH;
1852         mtd->writesize = 1;
1853         mtd->flags = MTD_CAP_NORFLASH;
1854         mtd->size = params.size;
1855         mtd->_erase = spi_nor_erase;
1856         mtd->_read = spi_nor_read;
1857
1858 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
1859         /* NOR protection support for STmicro/Micron chips and similar */
1860         if (JEDEC_MFR(info) == SNOR_MFR_ST ||
1861             JEDEC_MFR(info) == SNOR_MFR_MICRON ||
1862             JEDEC_MFR(info) == SNOR_MFR_SST ||
1863                         info->flags & SPI_NOR_HAS_LOCK) {
1864                 nor->flash_lock = stm_lock;
1865                 nor->flash_unlock = stm_unlock;
1866                 nor->flash_is_locked = stm_is_locked;
1867         }
1868 #endif
1869
1870 #ifdef CONFIG_SPI_FLASH_SST
1871         /* sst nor chips use AAI word program */
1872         if (info->flags & SST_WRITE)
1873                 mtd->_write = sst_write;
1874         else
1875 #endif
1876                 mtd->_write = spi_nor_write;
1877
1878         if (info->flags & USE_FSR)
1879                 nor->flags |= SNOR_F_USE_FSR;
1880         if (info->flags & SPI_NOR_HAS_TB)
1881                 nor->flags |= SNOR_F_HAS_SR_TB;
1882         if (info->flags & NO_CHIP_ERASE)
1883                 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
1884         if (info->flags & USE_CLSR)
1885                 nor->flags |= SNOR_F_USE_CLSR;
1886
1887         if (info->flags & SPI_NOR_NO_ERASE)
1888                 mtd->flags |= MTD_NO_ERASE;
1889
1890         nor->page_size = params.page_size;
1891         mtd->writebufsize = nor->page_size;
1892
1893         /* Some devices cannot do fast-read, no matter what DT tells us */
1894         if ((info->flags & SPI_NOR_NO_FR) || (spi->mode & SPI_RX_SLOW))
1895                 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
1896
1897         /*
1898          * Configure the SPI memory:
1899          * - select op codes for (Fast) Read, Page Program and Sector Erase.
1900          * - set the number of dummy cycles (mode cycles + wait states).
1901          * - set the SPI protocols for register and memory accesses.
1902          * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
1903          */
1904         ret = spi_nor_setup(nor, info, &params, &hwcaps);
1905         if (ret)
1906                 return ret;
1907
1908         if (info->addr_width) {
1909                 nor->addr_width = info->addr_width;
1910         } else if (mtd->size > 0x1000000) {
1911                 /* enable 4-byte addressing if the device exceeds 16MiB */
1912                 nor->addr_width = 4;
1913                 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
1914                     info->flags & SPI_NOR_4B_OPCODES)
1915                         spi_nor_set_4byte_opcodes(nor, info);
1916         } else {
1917                 nor->addr_width = 3;
1918         }
1919
1920         if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
1921                 dev_dbg(dev, "address width is too large: %u\n",
1922                         nor->addr_width);
1923                 return -EINVAL;
1924         }
1925
1926         /* Send all the required SPI flash commands to initialize device */
1927         nor->info = info;
1928         ret = spi_nor_init(nor);
1929         if (ret)
1930                 return ret;
1931
1932         nor->name = mtd->name;
1933         nor->size = mtd->size;
1934         nor->erase_size = mtd->erasesize;
1935         nor->sector_size = mtd->erasesize;
1936
1937 #ifndef CONFIG_SPL_BUILD
1938         printf("SF: Detected %s with page size ", nor->name);
1939         print_size(nor->page_size, ", erase size ");
1940         print_size(nor->erase_size, ", total ");
1941         print_size(nor->size, "");
1942         puts("\n");
1943 #endif
1944
1945         return 0;
1946 }