mtd: spi-nor-core: Add support for Read/Write Any Register
[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 <log.h>
14 #include <watchdog.h>
15 #include <dm.h>
16 #include <dm/device_compat.h>
17 #include <dm/devres.h>
18 #include <linux/bitops.h>
19 #include <linux/err.h>
20 #include <linux/errno.h>
21 #include <linux/log2.h>
22 #include <linux/math64.h>
23 #include <linux/sizes.h>
24 #include <linux/bitfield.h>
25 #include <linux/delay.h>
26
27 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/spi-nor.h>
29 #include <spi-mem.h>
30 #include <spi.h>
31
32 #include "sf_internal.h"
33
34 /* Define max times to check status register before we give up. */
35
36 /*
37  * For everything but full-chip erase; probably could be much smaller, but kept
38  * around for safety for now
39  */
40
41 #define HZ                                      CONFIG_SYS_HZ
42
43 #define DEFAULT_READY_WAIT_JIFFIES              (40UL * HZ)
44
45 #define ROUND_UP_TO(x, y)       (((x) + (y) - 1) / (y) * (y))
46
47 struct sfdp_parameter_header {
48         u8              id_lsb;
49         u8              minor;
50         u8              major;
51         u8              length; /* in double words */
52         u8              parameter_table_pointer[3]; /* byte address */
53         u8              id_msb;
54 };
55
56 #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
57 #define SFDP_PARAM_HEADER_PTP(p) \
58         (((p)->parameter_table_pointer[2] << 16) | \
59          ((p)->parameter_table_pointer[1] <<  8) | \
60          ((p)->parameter_table_pointer[0] <<  0))
61
62 #define SFDP_BFPT_ID            0xff00  /* Basic Flash Parameter Table */
63 #define SFDP_SECTOR_MAP_ID      0xff81  /* Sector Map Table */
64 #define SFDP_SST_ID             0x01bf  /* Manufacturer specific Table */
65 #define SFDP_PROFILE1_ID        0xff05  /* xSPI Profile 1.0 Table */
66
67 #define SFDP_SIGNATURE          0x50444653U
68 #define SFDP_JESD216_MAJOR      1
69 #define SFDP_JESD216_MINOR      0
70 #define SFDP_JESD216A_MINOR     5
71 #define SFDP_JESD216B_MINOR     6
72
73 struct sfdp_header {
74         u32             signature; /* Ox50444653U <=> "SFDP" */
75         u8              minor;
76         u8              major;
77         u8              nph; /* 0-base number of parameter headers */
78         u8              unused;
79
80         /* Basic Flash Parameter Table. */
81         struct sfdp_parameter_header    bfpt_header;
82 };
83
84 /* Basic Flash Parameter Table */
85
86 /*
87  * JESD216 rev D defines a Basic Flash Parameter Table of 20 DWORDs.
88  * They are indexed from 1 but C arrays are indexed from 0.
89  */
90 #define BFPT_DWORD(i)           ((i) - 1)
91 #define BFPT_DWORD_MAX          20
92
93 /* The first version of JESB216 defined only 9 DWORDs. */
94 #define BFPT_DWORD_MAX_JESD216                  9
95 #define BFPT_DWORD_MAX_JESD216B                 16
96
97 /* 1st DWORD. */
98 #define BFPT_DWORD1_FAST_READ_1_1_2             BIT(16)
99 #define BFPT_DWORD1_ADDRESS_BYTES_MASK          GENMASK(18, 17)
100 #define BFPT_DWORD1_ADDRESS_BYTES_3_ONLY        (0x0UL << 17)
101 #define BFPT_DWORD1_ADDRESS_BYTES_3_OR_4        (0x1UL << 17)
102 #define BFPT_DWORD1_ADDRESS_BYTES_4_ONLY        (0x2UL << 17)
103 #define BFPT_DWORD1_DTR                         BIT(19)
104 #define BFPT_DWORD1_FAST_READ_1_2_2             BIT(20)
105 #define BFPT_DWORD1_FAST_READ_1_4_4             BIT(21)
106 #define BFPT_DWORD1_FAST_READ_1_1_4             BIT(22)
107
108 /* 5th DWORD. */
109 #define BFPT_DWORD5_FAST_READ_2_2_2             BIT(0)
110 #define BFPT_DWORD5_FAST_READ_4_4_4             BIT(4)
111
112 /* 11th DWORD. */
113 #define BFPT_DWORD11_PAGE_SIZE_SHIFT            4
114 #define BFPT_DWORD11_PAGE_SIZE_MASK             GENMASK(7, 4)
115
116 /* 15th DWORD. */
117
118 /*
119  * (from JESD216 rev B)
120  * Quad Enable Requirements (QER):
121  * - 000b: Device does not have a QE bit. Device detects 1-1-4 and 1-4-4
122  *         reads based on instruction. DQ3/HOLD# functions are hold during
123  *         instruction phase.
124  * - 001b: QE is bit 1 of status register 2. It is set via Write Status with
125  *         two data bytes where bit 1 of the second byte is one.
126  *         [...]
127  *         Writing only one byte to the status register has the side-effect of
128  *         clearing status register 2, including the QE bit. The 100b code is
129  *         used if writing one byte to the status register does not modify
130  *         status register 2.
131  * - 010b: QE is bit 6 of status register 1. It is set via Write Status with
132  *         one data byte where bit 6 is one.
133  *         [...]
134  * - 011b: QE is bit 7 of status register 2. It is set via Write status
135  *         register 2 instruction 3Eh with one data byte where bit 7 is one.
136  *         [...]
137  *         The status register 2 is read using instruction 3Fh.
138  * - 100b: QE is bit 1 of status register 2. It is set via Write Status with
139  *         two data bytes where bit 1 of the second byte is one.
140  *         [...]
141  *         In contrast to the 001b code, writing one byte to the status
142  *         register does not modify status register 2.
143  * - 101b: QE is bit 1 of status register 2. Status register 1 is read using
144  *         Read Status instruction 05h. Status register2 is read using
145  *         instruction 35h. QE is set via Writ Status instruction 01h with
146  *         two data bytes where bit 1 of the second byte is one.
147  *         [...]
148  */
149 #define BFPT_DWORD15_QER_MASK                   GENMASK(22, 20)
150 #define BFPT_DWORD15_QER_NONE                   (0x0UL << 20) /* Micron */
151 #define BFPT_DWORD15_QER_SR2_BIT1_BUGGY         (0x1UL << 20)
152 #define BFPT_DWORD15_QER_SR1_BIT6               (0x2UL << 20) /* Macronix */
153 #define BFPT_DWORD15_QER_SR2_BIT7               (0x3UL << 20)
154 #define BFPT_DWORD15_QER_SR2_BIT1_NO_RD         (0x4UL << 20)
155 #define BFPT_DWORD15_QER_SR2_BIT1               (0x5UL << 20) /* Spansion */
156
157 #define BFPT_DWORD16_SOFT_RST                   BIT(12)
158
159 #define BFPT_DWORD18_CMD_EXT_MASK               GENMASK(30, 29)
160 #define BFPT_DWORD18_CMD_EXT_REP                (0x0UL << 29) /* Repeat */
161 #define BFPT_DWORD18_CMD_EXT_INV                (0x1UL << 29) /* Invert */
162 #define BFPT_DWORD18_CMD_EXT_RES                (0x2UL << 29) /* Reserved */
163 #define BFPT_DWORD18_CMD_EXT_16B                (0x3UL << 29) /* 16-bit opcode */
164
165 /* xSPI Profile 1.0 table (from JESD216D.01). */
166 #define PROFILE1_DWORD1_RD_FAST_CMD             GENMASK(15, 8)
167 #define PROFILE1_DWORD1_RDSR_DUMMY              BIT(28)
168 #define PROFILE1_DWORD1_RDSR_ADDR_BYTES         BIT(29)
169 #define PROFILE1_DWORD4_DUMMY_200MHZ            GENMASK(11, 7)
170 #define PROFILE1_DWORD5_DUMMY_166MHZ            GENMASK(31, 27)
171 #define PROFILE1_DWORD5_DUMMY_133MHZ            GENMASK(21, 17)
172 #define PROFILE1_DWORD5_DUMMY_100MHZ            GENMASK(11, 7)
173 #define PROFILE1_DUMMY_DEFAULT                  20
174
175 struct sfdp_bfpt {
176         u32     dwords[BFPT_DWORD_MAX];
177 };
178
179 /**
180  * struct spi_nor_fixups - SPI NOR fixup hooks
181  * @default_init: called after default flash parameters init. Used to tweak
182  *                flash parameters when information provided by the flash_info
183  *                table is incomplete or wrong.
184  * @post_bfpt: called after the BFPT table has been parsed
185  * @post_sfdp: called after SFDP has been parsed (is also called for SPI NORs
186  *             that do not support RDSFDP). Typically used to tweak various
187  *             parameters that could not be extracted by other means (i.e.
188  *             when information provided by the SFDP/flash_info tables are
189  *             incomplete or wrong).
190  *
191  * Those hooks can be used to tweak the SPI NOR configuration when the SFDP
192  * table is broken or not available.
193  */
194 struct spi_nor_fixups {
195         void (*default_init)(struct spi_nor *nor);
196         int (*post_bfpt)(struct spi_nor *nor,
197                          const struct sfdp_parameter_header *bfpt_header,
198                          const struct sfdp_bfpt *bfpt,
199                          struct spi_nor_flash_parameter *params);
200         void (*post_sfdp)(struct spi_nor *nor,
201                           struct spi_nor_flash_parameter *params);
202 };
203
204 #define SPI_NOR_SRST_SLEEP_LEN                  200
205
206 /**
207  * spi_nor_get_cmd_ext() - Get the command opcode extension based on the
208  *                         extension type.
209  * @nor:                pointer to a 'struct spi_nor'
210  * @op:                 pointer to the 'struct spi_mem_op' whose properties
211  *                      need to be initialized.
212  *
213  * Right now, only "repeat" and "invert" are supported.
214  *
215  * Return: The opcode extension.
216  */
217 static u8 spi_nor_get_cmd_ext(const struct spi_nor *nor,
218                               const struct spi_mem_op *op)
219 {
220         switch (nor->cmd_ext_type) {
221         case SPI_NOR_EXT_INVERT:
222                 return ~op->cmd.opcode;
223
224         case SPI_NOR_EXT_REPEAT:
225                 return op->cmd.opcode;
226
227         default:
228                 dev_dbg(nor->dev, "Unknown command extension type\n");
229                 return 0;
230         }
231 }
232
233 /**
234  * spi_nor_setup_op() - Set up common properties of a spi-mem op.
235  * @nor:                pointer to a 'struct spi_nor'
236  * @op:                 pointer to the 'struct spi_mem_op' whose properties
237  *                      need to be initialized.
238  * @proto:              the protocol from which the properties need to be set.
239  */
240 static void spi_nor_setup_op(const struct spi_nor *nor,
241                              struct spi_mem_op *op,
242                              const enum spi_nor_protocol proto)
243 {
244         u8 ext;
245
246         op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(proto);
247
248         if (op->addr.nbytes)
249                 op->addr.buswidth = spi_nor_get_protocol_addr_nbits(proto);
250
251         if (op->dummy.nbytes)
252                 op->dummy.buswidth = spi_nor_get_protocol_addr_nbits(proto);
253
254         if (op->data.nbytes)
255                 op->data.buswidth = spi_nor_get_protocol_data_nbits(proto);
256
257         if (spi_nor_protocol_is_dtr(proto)) {
258                 /*
259                  * spi-mem supports mixed DTR modes, but right now we can only
260                  * have all phases either DTR or STR. IOW, spi-mem can have
261                  * something like 4S-4D-4D, but spi-nor can't. So, set all 4
262                  * phases to either DTR or STR.
263                  */
264                 op->cmd.dtr = op->addr.dtr = op->dummy.dtr =
265                         op->data.dtr = true;
266
267                 /* 2 bytes per clock cycle in DTR mode. */
268                 op->dummy.nbytes *= 2;
269
270                 ext = spi_nor_get_cmd_ext(nor, op);
271                 op->cmd.opcode = (op->cmd.opcode << 8) | ext;
272                 op->cmd.nbytes = 2;
273         }
274 }
275
276 static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op
277                 *op, void *buf)
278 {
279         if (op->data.dir == SPI_MEM_DATA_IN)
280                 op->data.buf.in = buf;
281         else
282                 op->data.buf.out = buf;
283         return spi_mem_exec_op(nor->spi, op);
284 }
285
286 static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
287 {
288         struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 0),
289                                           SPI_MEM_OP_NO_ADDR,
290                                           SPI_MEM_OP_NO_DUMMY,
291                                           SPI_MEM_OP_DATA_IN(len, NULL, 0));
292         int ret;
293
294         spi_nor_setup_op(nor, &op, nor->reg_proto);
295
296         ret = spi_nor_read_write_reg(nor, &op, val);
297         if (ret < 0)
298                 dev_dbg(nor->dev, "error %d reading %x\n", ret, code);
299
300         return ret;
301 }
302
303 static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
304 {
305         struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 0),
306                                           SPI_MEM_OP_NO_ADDR,
307                                           SPI_MEM_OP_NO_DUMMY,
308                                           SPI_MEM_OP_DATA_OUT(len, NULL, 0));
309
310         spi_nor_setup_op(nor, &op, nor->reg_proto);
311
312         if (len == 0)
313                 op.data.dir = SPI_MEM_NO_DATA;
314
315         return spi_nor_read_write_reg(nor, &op, buf);
316 }
317
318 #ifdef CONFIG_SPI_FLASH_SPANSION
319 static int spansion_read_any_reg(struct spi_nor *nor, u32 addr, u8 dummy,
320                                  u8 *val)
321 {
322         struct spi_mem_op op =
323                         SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDAR, 1),
324                                    SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
325                                    SPI_MEM_OP_DUMMY(dummy / 8, 1),
326                                    SPI_MEM_OP_DATA_IN(1, NULL, 1));
327
328         return spi_nor_read_write_reg(nor, &op, val);
329 }
330
331 static int spansion_write_any_reg(struct spi_nor *nor, u32 addr, u8 val)
332 {
333         struct spi_mem_op op =
334                         SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRAR, 1),
335                                    SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
336                                    SPI_MEM_OP_NO_DUMMY,
337                                    SPI_MEM_OP_DATA_OUT(1, NULL, 1));
338
339         return spi_nor_read_write_reg(nor, &op, &val);
340 }
341 #endif
342
343 static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
344                                  u_char *buf)
345 {
346         struct spi_mem_op op =
347                         SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 0),
348                                    SPI_MEM_OP_ADDR(nor->addr_width, from, 0),
349                                    SPI_MEM_OP_DUMMY(nor->read_dummy, 0),
350                                    SPI_MEM_OP_DATA_IN(len, buf, 0));
351         size_t remaining = len;
352         int ret;
353
354         spi_nor_setup_op(nor, &op, nor->read_proto);
355
356         /* convert the dummy cycles to the number of bytes */
357         op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
358         if (spi_nor_protocol_is_dtr(nor->read_proto))
359                 op.dummy.nbytes *= 2;
360
361         while (remaining) {
362                 op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
363                 ret = spi_mem_adjust_op_size(nor->spi, &op);
364                 if (ret)
365                         return ret;
366
367                 ret = spi_mem_exec_op(nor->spi, &op);
368                 if (ret)
369                         return ret;
370
371                 op.addr.val += op.data.nbytes;
372                 remaining -= op.data.nbytes;
373                 op.data.buf.in += op.data.nbytes;
374         }
375
376         return len;
377 }
378
379 static ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
380                                   const u_char *buf)
381 {
382         struct spi_mem_op op =
383                         SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 0),
384                                    SPI_MEM_OP_ADDR(nor->addr_width, to, 0),
385                                    SPI_MEM_OP_NO_DUMMY,
386                                    SPI_MEM_OP_DATA_OUT(len, buf, 0));
387         int ret;
388
389         if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
390                 op.addr.nbytes = 0;
391
392         spi_nor_setup_op(nor, &op, nor->write_proto);
393
394         ret = spi_mem_adjust_op_size(nor->spi, &op);
395         if (ret)
396                 return ret;
397         op.data.nbytes = len < op.data.nbytes ? len : op.data.nbytes;
398
399         ret = spi_mem_exec_op(nor->spi, &op);
400         if (ret)
401                 return ret;
402
403         return op.data.nbytes;
404 }
405
406 /*
407  * Read the status register, returning its value in the location
408  * Return the status register value.
409  * Returns negative if error occurred.
410  */
411 static int read_sr(struct spi_nor *nor)
412 {
413         struct spi_mem_op op;
414         int ret;
415         u8 val[2];
416         u8 addr_nbytes, dummy;
417
418         if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
419                 addr_nbytes = nor->rdsr_addr_nbytes;
420                 dummy = nor->rdsr_dummy;
421         } else {
422                 addr_nbytes = 0;
423                 dummy = 0;
424         }
425
426         op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR, 0),
427                                            SPI_MEM_OP_ADDR(addr_nbytes, 0, 0),
428                                            SPI_MEM_OP_DUMMY(dummy, 0),
429                                            SPI_MEM_OP_DATA_IN(1, NULL, 0));
430
431         spi_nor_setup_op(nor, &op, nor->reg_proto);
432
433         /*
434          * We don't want to read only one byte in DTR mode. So, read 2 and then
435          * discard the second byte.
436          */
437         if (spi_nor_protocol_is_dtr(nor->reg_proto))
438                 op.data.nbytes = 2;
439
440         ret = spi_nor_read_write_reg(nor, &op, val);
441         if (ret < 0) {
442                 pr_debug("error %d reading SR\n", (int)ret);
443                 return ret;
444         }
445
446         return *val;
447 }
448
449 /*
450  * Read the flag status register, returning its value in the location
451  * Return the status register value.
452  * Returns negative if error occurred.
453  */
454 static int read_fsr(struct spi_nor *nor)
455 {
456         struct spi_mem_op op;
457         int ret;
458         u8 val[2];
459         u8 addr_nbytes, dummy;
460
461         if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
462                 addr_nbytes = nor->rdsr_addr_nbytes;
463                 dummy = nor->rdsr_dummy;
464         } else {
465                 addr_nbytes = 0;
466                 dummy = 0;
467         }
468
469         op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDFSR, 0),
470                                            SPI_MEM_OP_ADDR(addr_nbytes, 0, 0),
471                                            SPI_MEM_OP_DUMMY(dummy, 0),
472                                            SPI_MEM_OP_DATA_IN(1, NULL, 0));
473
474         spi_nor_setup_op(nor, &op, nor->reg_proto);
475
476         /*
477          * We don't want to read only one byte in DTR mode. So, read 2 and then
478          * discard the second byte.
479          */
480         if (spi_nor_protocol_is_dtr(nor->reg_proto))
481                 op.data.nbytes = 2;
482
483         ret = spi_nor_read_write_reg(nor, &op, val);
484         if (ret < 0) {
485                 pr_debug("error %d reading FSR\n", ret);
486                 return ret;
487         }
488
489         return *val;
490 }
491
492 /*
493  * Read configuration register, returning its value in the
494  * location. Return the configuration register value.
495  * Returns negative if error occurred.
496  */
497 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
498 static int read_cr(struct spi_nor *nor)
499 {
500         int ret;
501         u8 val;
502
503         ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
504         if (ret < 0) {
505                 dev_dbg(nor->dev, "error %d reading CR\n", ret);
506                 return ret;
507         }
508
509         return val;
510 }
511 #endif
512
513 /*
514  * Write status register 1 byte
515  * Returns negative if error occurred.
516  */
517 static int write_sr(struct spi_nor *nor, u8 val)
518 {
519         nor->cmd_buf[0] = val;
520         return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
521 }
522
523 /*
524  * Set write enable latch with Write Enable command.
525  * Returns negative if error occurred.
526  */
527 static int write_enable(struct spi_nor *nor)
528 {
529         return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
530 }
531
532 /*
533  * Send write disable instruction to the chip.
534  */
535 static int write_disable(struct spi_nor *nor)
536 {
537         return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
538 }
539
540 static struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
541 {
542         return mtd->priv;
543 }
544
545 #ifndef CONFIG_SPI_FLASH_BAR
546 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
547 {
548         size_t i;
549
550         for (i = 0; i < size; i++)
551                 if (table[i][0] == opcode)
552                         return table[i][1];
553
554         /* No conversion found, keep input op code. */
555         return opcode;
556 }
557
558 static u8 spi_nor_convert_3to4_read(u8 opcode)
559 {
560         static const u8 spi_nor_3to4_read[][2] = {
561                 { SPINOR_OP_READ,       SPINOR_OP_READ_4B },
562                 { SPINOR_OP_READ_FAST,  SPINOR_OP_READ_FAST_4B },
563                 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
564                 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
565                 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
566                 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
567                 { SPINOR_OP_READ_1_1_8, SPINOR_OP_READ_1_1_8_4B },
568                 { SPINOR_OP_READ_1_8_8, SPINOR_OP_READ_1_8_8_4B },
569
570                 { SPINOR_OP_READ_1_1_1_DTR,     SPINOR_OP_READ_1_1_1_DTR_4B },
571                 { SPINOR_OP_READ_1_2_2_DTR,     SPINOR_OP_READ_1_2_2_DTR_4B },
572                 { SPINOR_OP_READ_1_4_4_DTR,     SPINOR_OP_READ_1_4_4_DTR_4B },
573         };
574
575         return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
576                                       ARRAY_SIZE(spi_nor_3to4_read));
577 }
578
579 static u8 spi_nor_convert_3to4_program(u8 opcode)
580 {
581         static const u8 spi_nor_3to4_program[][2] = {
582                 { SPINOR_OP_PP,         SPINOR_OP_PP_4B },
583                 { SPINOR_OP_PP_1_1_4,   SPINOR_OP_PP_1_1_4_4B },
584                 { SPINOR_OP_PP_1_4_4,   SPINOR_OP_PP_1_4_4_4B },
585                 { SPINOR_OP_PP_1_1_8,   SPINOR_OP_PP_1_1_8_4B },
586                 { SPINOR_OP_PP_1_8_8,   SPINOR_OP_PP_1_8_8_4B },
587         };
588
589         return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
590                                       ARRAY_SIZE(spi_nor_3to4_program));
591 }
592
593 static u8 spi_nor_convert_3to4_erase(u8 opcode)
594 {
595         static const u8 spi_nor_3to4_erase[][2] = {
596                 { SPINOR_OP_BE_4K,      SPINOR_OP_BE_4K_4B },
597                 { SPINOR_OP_BE_32K,     SPINOR_OP_BE_32K_4B },
598                 { SPINOR_OP_SE,         SPINOR_OP_SE_4B },
599         };
600
601         return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
602                                       ARRAY_SIZE(spi_nor_3to4_erase));
603 }
604
605 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
606                                       const struct flash_info *info)
607 {
608         /* Do some manufacturer fixups first */
609         switch (JEDEC_MFR(info)) {
610         case SNOR_MFR_SPANSION:
611                 /* No small sector erase for 4-byte command set */
612                 nor->erase_opcode = SPINOR_OP_SE;
613                 nor->mtd.erasesize = info->sector_size;
614                 break;
615
616         default:
617                 break;
618         }
619
620         nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
621         nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
622         nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
623 }
624 #endif /* !CONFIG_SPI_FLASH_BAR */
625
626 /* Enable/disable 4-byte addressing mode. */
627 static int set_4byte(struct spi_nor *nor, const struct flash_info *info,
628                      int enable)
629 {
630         int status;
631         bool need_wren = false;
632         u8 cmd;
633
634         switch (JEDEC_MFR(info)) {
635         case SNOR_MFR_ST:
636         case SNOR_MFR_MICRON:
637                 /* Some Micron need WREN command; all will accept it */
638                 need_wren = true;
639         case SNOR_MFR_ISSI:
640         case SNOR_MFR_MACRONIX:
641         case SNOR_MFR_WINBOND:
642                 if (need_wren)
643                         write_enable(nor);
644
645                 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
646                 status = nor->write_reg(nor, cmd, NULL, 0);
647                 if (need_wren)
648                         write_disable(nor);
649
650                 if (!status && !enable &&
651                     JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
652                         /*
653                          * On Winbond W25Q256FV, leaving 4byte mode causes
654                          * the Extended Address Register to be set to 1, so all
655                          * 3-byte-address reads come from the second 16M.
656                          * We must clear the register to enable normal behavior.
657                          */
658                         write_enable(nor);
659                         nor->cmd_buf[0] = 0;
660                         nor->write_reg(nor, SPINOR_OP_WREAR, nor->cmd_buf, 1);
661                         write_disable(nor);
662                 }
663
664                 return status;
665         default:
666                 /* Spansion style */
667                 nor->cmd_buf[0] = enable << 7;
668                 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
669         }
670 }
671
672 static int spi_nor_sr_ready(struct spi_nor *nor)
673 {
674         int sr = read_sr(nor);
675
676         if (sr < 0)
677                 return sr;
678
679         if (nor->flags & SNOR_F_USE_CLSR && sr & (SR_E_ERR | SR_P_ERR)) {
680                 if (sr & SR_E_ERR)
681                         dev_dbg(nor->dev, "Erase Error occurred\n");
682                 else
683                         dev_dbg(nor->dev, "Programming Error occurred\n");
684
685                 nor->write_reg(nor, SPINOR_OP_CLSR, NULL, 0);
686                 return -EIO;
687         }
688
689         return !(sr & SR_WIP);
690 }
691
692 static int spi_nor_fsr_ready(struct spi_nor *nor)
693 {
694         int fsr = read_fsr(nor);
695
696         if (fsr < 0)
697                 return fsr;
698
699         if (fsr & (FSR_E_ERR | FSR_P_ERR)) {
700                 if (fsr & FSR_E_ERR)
701                         dev_err(nor->dev, "Erase operation failed.\n");
702                 else
703                         dev_err(nor->dev, "Program operation failed.\n");
704
705                 if (fsr & FSR_PT_ERR)
706                         dev_err(nor->dev,
707                                 "Attempted to modify a protected sector.\n");
708
709                 nor->write_reg(nor, SPINOR_OP_CLFSR, NULL, 0);
710                 return -EIO;
711         }
712
713         return fsr & FSR_READY;
714 }
715
716 static int spi_nor_ready(struct spi_nor *nor)
717 {
718         int sr, fsr;
719
720         sr = spi_nor_sr_ready(nor);
721         if (sr < 0)
722                 return sr;
723         fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
724         if (fsr < 0)
725                 return fsr;
726         return sr && fsr;
727 }
728
729 /*
730  * Service routine to read status register until ready, or timeout occurs.
731  * Returns non-zero if error.
732  */
733 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
734                                                 unsigned long timeout)
735 {
736         unsigned long timebase;
737         int ret;
738
739         timebase = get_timer(0);
740
741         while (get_timer(timebase) < timeout) {
742                 ret = spi_nor_ready(nor);
743                 if (ret < 0)
744                         return ret;
745                 if (ret)
746                         return 0;
747         }
748
749         dev_err(nor->dev, "flash operation timed out\n");
750
751         return -ETIMEDOUT;
752 }
753
754 static int spi_nor_wait_till_ready(struct spi_nor *nor)
755 {
756         return spi_nor_wait_till_ready_with_timeout(nor,
757                                                     DEFAULT_READY_WAIT_JIFFIES);
758 }
759
760 #ifdef CONFIG_SPI_FLASH_BAR
761 /*
762  * This "clean_bar" is necessary in a situation when one was accessing
763  * spi flash memory > 16 MiB by using Bank Address Register's BA24 bit.
764  *
765  * After it the BA24 bit shall be cleared to allow access to correct
766  * memory region after SW reset (by calling "reset" command).
767  *
768  * Otherwise, the BA24 bit may be left set and then after reset, the
769  * ROM would read/write/erase SPL from 16 MiB * bank_sel address.
770  */
771 static int clean_bar(struct spi_nor *nor)
772 {
773         u8 cmd, bank_sel = 0;
774
775         if (nor->bank_curr == 0)
776                 return 0;
777         cmd = nor->bank_write_cmd;
778         nor->bank_curr = 0;
779         write_enable(nor);
780
781         return nor->write_reg(nor, cmd, &bank_sel, 1);
782 }
783
784 static int write_bar(struct spi_nor *nor, u32 offset)
785 {
786         u8 cmd, bank_sel;
787         int ret;
788
789         bank_sel = offset / SZ_16M;
790         if (bank_sel == nor->bank_curr)
791                 goto bar_end;
792
793         cmd = nor->bank_write_cmd;
794         write_enable(nor);
795         ret = nor->write_reg(nor, cmd, &bank_sel, 1);
796         if (ret < 0) {
797                 debug("SF: fail to write bank register\n");
798                 return ret;
799         }
800
801 bar_end:
802         nor->bank_curr = bank_sel;
803         return nor->bank_curr;
804 }
805
806 static int read_bar(struct spi_nor *nor, const struct flash_info *info)
807 {
808         u8 curr_bank = 0;
809         int ret;
810
811         switch (JEDEC_MFR(info)) {
812         case SNOR_MFR_SPANSION:
813                 nor->bank_read_cmd = SPINOR_OP_BRRD;
814                 nor->bank_write_cmd = SPINOR_OP_BRWR;
815                 break;
816         default:
817                 nor->bank_read_cmd = SPINOR_OP_RDEAR;
818                 nor->bank_write_cmd = SPINOR_OP_WREAR;
819         }
820
821         ret = nor->read_reg(nor, nor->bank_read_cmd,
822                                     &curr_bank, 1);
823         if (ret) {
824                 debug("SF: fail to read bank addr register\n");
825                 return ret;
826         }
827         nor->bank_curr = curr_bank;
828
829         return 0;
830 }
831 #endif
832
833 /*
834  * Initiate the erasure of a single sector. Returns the number of bytes erased
835  * on success, a negative error code on error.
836  */
837 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
838 {
839         struct spi_mem_op op =
840                 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 0),
841                            SPI_MEM_OP_ADDR(nor->addr_width, addr, 0),
842                            SPI_MEM_OP_NO_DUMMY,
843                            SPI_MEM_OP_NO_DATA);
844         int ret;
845
846         spi_nor_setup_op(nor, &op, nor->write_proto);
847
848         if (nor->erase)
849                 return nor->erase(nor, addr);
850
851         /*
852          * Default implementation, if driver doesn't have a specialized HW
853          * control
854          */
855         ret = spi_mem_exec_op(nor->spi, &op);
856         if (ret)
857                 return ret;
858
859         return nor->mtd.erasesize;
860 }
861
862 /*
863  * Erase an address range on the nor chip.  The address range may extend
864  * one or more erase sectors.  Return an error is there is a problem erasing.
865  */
866 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
867 {
868         struct spi_nor *nor = mtd_to_spi_nor(mtd);
869         u32 addr, len, rem;
870         int ret;
871
872         dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
873                 (long long)instr->len);
874
875         if (!instr->len)
876                 return 0;
877
878         div_u64_rem(instr->len, mtd->erasesize, &rem);
879         if (rem)
880                 return -EINVAL;
881
882         addr = instr->addr;
883         len = instr->len;
884
885         while (len) {
886                 WATCHDOG_RESET();
887 #ifdef CONFIG_SPI_FLASH_BAR
888                 ret = write_bar(nor, addr);
889                 if (ret < 0)
890                         return ret;
891 #endif
892                 write_enable(nor);
893
894                 ret = spi_nor_erase_sector(nor, addr);
895                 if (ret < 0)
896                         goto erase_err;
897
898                 addr += ret;
899                 len -= ret;
900
901                 ret = spi_nor_wait_till_ready(nor);
902                 if (ret)
903                         goto erase_err;
904         }
905
906 erase_err:
907 #ifdef CONFIG_SPI_FLASH_BAR
908         ret = clean_bar(nor);
909 #endif
910         write_disable(nor);
911
912         return ret;
913 }
914
915 #ifdef CONFIG_SPI_FLASH_S28HS512T
916 /**
917  * spansion_erase_non_uniform() - erase non-uniform sectors for Spansion/Cypress
918  *                                chips
919  * @nor:        pointer to a 'struct spi_nor'
920  * @addr:       address of the sector to erase
921  * @opcode_4k:  opcode for 4K sector erase
922  * @ovlsz_top:  size of overlaid portion at the top address
923  * @ovlsz_btm:  size of overlaid portion at the bottom address
924  *
925  * Erase an address range on the nor chip that can contain 4KB sectors overlaid
926  * on top and/or bottom. The appropriate erase opcode and size are chosen by
927  * address to erase and size of overlaid portion.
928  *
929  * Return: number of bytes erased on success, -errno otherwise.
930  */
931 static int spansion_erase_non_uniform(struct spi_nor *nor, u32 addr,
932                                       u8 opcode_4k, u32 ovlsz_top,
933                                       u32 ovlsz_btm)
934 {
935         struct spi_mem_op op =
936                 SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 0),
937                            SPI_MEM_OP_ADDR(nor->addr_width, addr, 0),
938                            SPI_MEM_OP_NO_DUMMY,
939                            SPI_MEM_OP_NO_DATA);
940         struct mtd_info *mtd = &nor->mtd;
941         u32 erasesize;
942         int ret;
943
944         /* 4KB sectors */
945         if (op.addr.val < ovlsz_btm ||
946             op.addr.val >= mtd->size - ovlsz_top) {
947                 op.cmd.opcode = opcode_4k;
948                 erasesize = SZ_4K;
949
950         /* Non-overlaid portion in the normal sector at the bottom */
951         } else if (op.addr.val == ovlsz_btm) {
952                 op.cmd.opcode = nor->erase_opcode;
953                 erasesize = mtd->erasesize - ovlsz_btm;
954
955         /* Non-overlaid portion in the normal sector at the top */
956         } else if (op.addr.val == mtd->size - mtd->erasesize) {
957                 op.cmd.opcode = nor->erase_opcode;
958                 erasesize = mtd->erasesize - ovlsz_top;
959
960         /* Normal sectors */
961         } else {
962                 op.cmd.opcode = nor->erase_opcode;
963                 erasesize = mtd->erasesize;
964         }
965
966         spi_nor_setup_op(nor, &op, nor->write_proto);
967
968         ret = spi_mem_exec_op(nor->spi, &op);
969         if (ret)
970                 return ret;
971
972         return erasesize;
973 }
974 #endif
975
976 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
977 /* Write status register and ensure bits in mask match written values */
978 static int write_sr_and_check(struct spi_nor *nor, u8 status_new, u8 mask)
979 {
980         int ret;
981
982         write_enable(nor);
983         ret = write_sr(nor, status_new);
984         if (ret)
985                 return ret;
986
987         ret = spi_nor_wait_till_ready(nor);
988         if (ret)
989                 return ret;
990
991         ret = read_sr(nor);
992         if (ret < 0)
993                 return ret;
994
995         return ((ret & mask) != (status_new & mask)) ? -EIO : 0;
996 }
997
998 static void stm_get_locked_range(struct spi_nor *nor, u8 sr, loff_t *ofs,
999                                  uint64_t *len)
1000 {
1001         struct mtd_info *mtd = &nor->mtd;
1002         u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1003         int shift = ffs(mask) - 1;
1004         int pow;
1005
1006         if (!(sr & mask)) {
1007                 /* No protection */
1008                 *ofs = 0;
1009                 *len = 0;
1010         } else {
1011                 pow = ((sr & mask) ^ mask) >> shift;
1012                 *len = mtd->size >> pow;
1013                 if (nor->flags & SNOR_F_HAS_SR_TB && sr & SR_TB)
1014                         *ofs = 0;
1015                 else
1016                         *ofs = mtd->size - *len;
1017         }
1018 }
1019
1020 /*
1021  * Return 1 if the entire region is locked (if @locked is true) or unlocked (if
1022  * @locked is false); 0 otherwise
1023  */
1024 static int stm_check_lock_status_sr(struct spi_nor *nor, loff_t ofs, u64 len,
1025                                     u8 sr, bool locked)
1026 {
1027         loff_t lock_offs;
1028         uint64_t lock_len;
1029
1030         if (!len)
1031                 return 1;
1032
1033         stm_get_locked_range(nor, sr, &lock_offs, &lock_len);
1034
1035         if (locked)
1036                 /* Requested range is a sub-range of locked range */
1037                 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
1038         else
1039                 /* Requested range does not overlap with locked range */
1040                 return (ofs >= lock_offs + lock_len) || (ofs + len <= lock_offs);
1041 }
1042
1043 static int stm_is_locked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
1044                             u8 sr)
1045 {
1046         return stm_check_lock_status_sr(nor, ofs, len, sr, true);
1047 }
1048
1049 static int stm_is_unlocked_sr(struct spi_nor *nor, loff_t ofs, uint64_t len,
1050                               u8 sr)
1051 {
1052         return stm_check_lock_status_sr(nor, ofs, len, sr, false);
1053 }
1054
1055 /*
1056  * Lock a region of the flash. Compatible with ST Micro and similar flash.
1057  * Supports the block protection bits BP{0,1,2} in the status register
1058  * (SR). Does not support these features found in newer SR bitfields:
1059  *   - SEC: sector/block protect - only handle SEC=0 (block protect)
1060  *   - CMP: complement protect - only support CMP=0 (range is not complemented)
1061  *
1062  * Support for the following is provided conditionally for some flash:
1063  *   - TB: top/bottom protect
1064  *
1065  * Sample table portion for 8MB flash (Winbond w25q64fw):
1066  *
1067  *   SEC  |  TB   |  BP2  |  BP1  |  BP0  |  Prot Length  | Protected Portion
1068  *  --------------------------------------------------------------------------
1069  *    X   |   X   |   0   |   0   |   0   |  NONE         | NONE
1070  *    0   |   0   |   0   |   0   |   1   |  128 KB       | Upper 1/64
1071  *    0   |   0   |   0   |   1   |   0   |  256 KB       | Upper 1/32
1072  *    0   |   0   |   0   |   1   |   1   |  512 KB       | Upper 1/16
1073  *    0   |   0   |   1   |   0   |   0   |  1 MB         | Upper 1/8
1074  *    0   |   0   |   1   |   0   |   1   |  2 MB         | Upper 1/4
1075  *    0   |   0   |   1   |   1   |   0   |  4 MB         | Upper 1/2
1076  *    X   |   X   |   1   |   1   |   1   |  8 MB         | ALL
1077  *  ------|-------|-------|-------|-------|---------------|-------------------
1078  *    0   |   1   |   0   |   0   |   1   |  128 KB       | Lower 1/64
1079  *    0   |   1   |   0   |   1   |   0   |  256 KB       | Lower 1/32
1080  *    0   |   1   |   0   |   1   |   1   |  512 KB       | Lower 1/16
1081  *    0   |   1   |   1   |   0   |   0   |  1 MB         | Lower 1/8
1082  *    0   |   1   |   1   |   0   |   1   |  2 MB         | Lower 1/4
1083  *    0   |   1   |   1   |   1   |   0   |  4 MB         | Lower 1/2
1084  *
1085  * Returns negative on errors, 0 on success.
1086  */
1087 static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1088 {
1089         struct mtd_info *mtd = &nor->mtd;
1090         int status_old, status_new;
1091         u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1092         u8 shift = ffs(mask) - 1, pow, val;
1093         loff_t lock_len;
1094         bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1095         bool use_top;
1096
1097         status_old = read_sr(nor);
1098         if (status_old < 0)
1099                 return status_old;
1100
1101         /* If nothing in our range is unlocked, we don't need to do anything */
1102         if (stm_is_locked_sr(nor, ofs, len, status_old))
1103                 return 0;
1104
1105         /* If anything below us is unlocked, we can't use 'bottom' protection */
1106         if (!stm_is_locked_sr(nor, 0, ofs, status_old))
1107                 can_be_bottom = false;
1108
1109         /* If anything above us is unlocked, we can't use 'top' protection */
1110         if (!stm_is_locked_sr(nor, ofs + len, mtd->size - (ofs + len),
1111                               status_old))
1112                 can_be_top = false;
1113
1114         if (!can_be_bottom && !can_be_top)
1115                 return -EINVAL;
1116
1117         /* Prefer top, if both are valid */
1118         use_top = can_be_top;
1119
1120         /* lock_len: length of region that should end up locked */
1121         if (use_top)
1122                 lock_len = mtd->size - ofs;
1123         else
1124                 lock_len = ofs + len;
1125
1126         /*
1127          * Need smallest pow such that:
1128          *
1129          *   1 / (2^pow) <= (len / size)
1130          *
1131          * so (assuming power-of-2 size) we do:
1132          *
1133          *   pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
1134          */
1135         pow = ilog2(mtd->size) - ilog2(lock_len);
1136         val = mask - (pow << shift);
1137         if (val & ~mask)
1138                 return -EINVAL;
1139         /* Don't "lock" with no region! */
1140         if (!(val & mask))
1141                 return -EINVAL;
1142
1143         status_new = (status_old & ~mask & ~SR_TB) | val;
1144
1145         /* Disallow further writes if WP pin is asserted */
1146         status_new |= SR_SRWD;
1147
1148         if (!use_top)
1149                 status_new |= SR_TB;
1150
1151         /* Don't bother if they're the same */
1152         if (status_new == status_old)
1153                 return 0;
1154
1155         /* Only modify protection if it will not unlock other areas */
1156         if ((status_new & mask) < (status_old & mask))
1157                 return -EINVAL;
1158
1159         return write_sr_and_check(nor, status_new, mask);
1160 }
1161
1162 /*
1163  * Unlock a region of the flash. See stm_lock() for more info
1164  *
1165  * Returns negative on errors, 0 on success.
1166  */
1167 static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1168 {
1169         struct mtd_info *mtd = &nor->mtd;
1170         int status_old, status_new;
1171         u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
1172         u8 shift = ffs(mask) - 1, pow, val;
1173         loff_t lock_len;
1174         bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
1175         bool use_top;
1176
1177         status_old = read_sr(nor);
1178         if (status_old < 0)
1179                 return status_old;
1180
1181         /* If nothing in our range is locked, we don't need to do anything */
1182         if (stm_is_unlocked_sr(nor, ofs, len, status_old))
1183                 return 0;
1184
1185         /* If anything below us is locked, we can't use 'top' protection */
1186         if (!stm_is_unlocked_sr(nor, 0, ofs, status_old))
1187                 can_be_top = false;
1188
1189         /* If anything above us is locked, we can't use 'bottom' protection */
1190         if (!stm_is_unlocked_sr(nor, ofs + len, mtd->size - (ofs + len),
1191                                 status_old))
1192                 can_be_bottom = false;
1193
1194         if (!can_be_bottom && !can_be_top)
1195                 return -EINVAL;
1196
1197         /* Prefer top, if both are valid */
1198         use_top = can_be_top;
1199
1200         /* lock_len: length of region that should remain locked */
1201         if (use_top)
1202                 lock_len = mtd->size - (ofs + len);
1203         else
1204                 lock_len = ofs;
1205
1206         /*
1207          * Need largest pow such that:
1208          *
1209          *   1 / (2^pow) >= (len / size)
1210          *
1211          * so (assuming power-of-2 size) we do:
1212          *
1213          *   pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
1214          */
1215         pow = ilog2(mtd->size) - order_base_2(lock_len);
1216         if (lock_len == 0) {
1217                 val = 0; /* fully unlocked */
1218         } else {
1219                 val = mask - (pow << shift);
1220                 /* Some power-of-two sizes are not supported */
1221                 if (val & ~mask)
1222                         return -EINVAL;
1223         }
1224
1225         status_new = (status_old & ~mask & ~SR_TB) | val;
1226
1227         /* Don't protect status register if we're fully unlocked */
1228         if (lock_len == 0)
1229                 status_new &= ~SR_SRWD;
1230
1231         if (!use_top)
1232                 status_new |= SR_TB;
1233
1234         /* Don't bother if they're the same */
1235         if (status_new == status_old)
1236                 return 0;
1237
1238         /* Only modify protection if it will not lock other areas */
1239         if ((status_new & mask) > (status_old & mask))
1240                 return -EINVAL;
1241
1242         return write_sr_and_check(nor, status_new, mask);
1243 }
1244
1245 /*
1246  * Check if a region of the flash is (completely) locked. See stm_lock() for
1247  * more info.
1248  *
1249  * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
1250  * negative on errors.
1251  */
1252 static int stm_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
1253 {
1254         int status;
1255
1256         status = read_sr(nor);
1257         if (status < 0)
1258                 return status;
1259
1260         return stm_is_locked_sr(nor, ofs, len, status);
1261 }
1262 #endif /* CONFIG_SPI_FLASH_STMICRO */
1263
1264 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
1265 {
1266         int                     tmp;
1267         u8                      id[SPI_NOR_MAX_ID_LEN];
1268         const struct flash_info *info;
1269
1270         tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
1271         if (tmp < 0) {
1272                 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
1273                 return ERR_PTR(tmp);
1274         }
1275
1276         info = spi_nor_ids;
1277         for (; info->name; info++) {
1278                 if (info->id_len) {
1279                         if (!memcmp(info->id, id, info->id_len))
1280                                 return info;
1281                 }
1282         }
1283
1284         dev_err(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
1285                 id[0], id[1], id[2]);
1286         return ERR_PTR(-ENODEV);
1287 }
1288
1289 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
1290                         size_t *retlen, u_char *buf)
1291 {
1292         struct spi_nor *nor = mtd_to_spi_nor(mtd);
1293         int ret;
1294
1295         dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
1296
1297         while (len) {
1298                 loff_t addr = from;
1299                 size_t read_len = len;
1300
1301 #ifdef CONFIG_SPI_FLASH_BAR
1302                 u32 remain_len;
1303
1304                 ret = write_bar(nor, addr);
1305                 if (ret < 0)
1306                         return log_ret(ret);
1307                 remain_len = (SZ_16M * (nor->bank_curr + 1)) - addr;
1308
1309                 if (len < remain_len)
1310                         read_len = len;
1311                 else
1312                         read_len = remain_len;
1313 #endif
1314
1315                 ret = nor->read(nor, addr, read_len, buf);
1316                 if (ret == 0) {
1317                         /* We shouldn't see 0-length reads */
1318                         ret = -EIO;
1319                         goto read_err;
1320                 }
1321                 if (ret < 0)
1322                         goto read_err;
1323
1324                 *retlen += ret;
1325                 buf += ret;
1326                 from += ret;
1327                 len -= ret;
1328         }
1329         ret = 0;
1330
1331 read_err:
1332 #ifdef CONFIG_SPI_FLASH_BAR
1333         ret = clean_bar(nor);
1334 #endif
1335         return ret;
1336 }
1337
1338 #ifdef CONFIG_SPI_FLASH_SST
1339 /*
1340  * sst26 flash series has its own block protection implementation:
1341  * 4x   - 8  KByte blocks - read & write protection bits - upper addresses
1342  * 1x   - 32 KByte blocks - write protection bits
1343  * rest - 64 KByte blocks - write protection bits
1344  * 1x   - 32 KByte blocks - write protection bits
1345  * 4x   - 8  KByte blocks - read & write protection bits - lower addresses
1346  *
1347  * We'll support only per 64k lock/unlock so lower and upper 64 KByte region
1348  * will be treated as single block.
1349  */
1350 #define SST26_BPR_8K_NUM                4
1351 #define SST26_MAX_BPR_REG_LEN           (18 + 1)
1352 #define SST26_BOUND_REG_SIZE            ((32 + SST26_BPR_8K_NUM * 8) * SZ_1K)
1353
1354 enum lock_ctl {
1355         SST26_CTL_LOCK,
1356         SST26_CTL_UNLOCK,
1357         SST26_CTL_CHECK
1358 };
1359
1360 static bool sst26_process_bpr(u32 bpr_size, u8 *cmd, u32 bit, enum lock_ctl ctl)
1361 {
1362         switch (ctl) {
1363         case SST26_CTL_LOCK:
1364                 cmd[bpr_size - (bit / 8) - 1] |= BIT(bit % 8);
1365                 break;
1366         case SST26_CTL_UNLOCK:
1367                 cmd[bpr_size - (bit / 8) - 1] &= ~BIT(bit % 8);
1368                 break;
1369         case SST26_CTL_CHECK:
1370                 return !!(cmd[bpr_size - (bit / 8) - 1] & BIT(bit % 8));
1371         }
1372
1373         return false;
1374 }
1375
1376 /*
1377  * Lock, unlock or check lock status of the flash region of the flash (depending
1378  * on the lock_ctl value)
1379  */
1380 static int sst26_lock_ctl(struct spi_nor *nor, loff_t ofs, uint64_t len, enum lock_ctl ctl)
1381 {
1382         struct mtd_info *mtd = &nor->mtd;
1383         u32 i, bpr_ptr, rptr_64k, lptr_64k, bpr_size;
1384         bool lower_64k = false, upper_64k = false;
1385         u8 bpr_buff[SST26_MAX_BPR_REG_LEN] = {};
1386         int ret;
1387
1388         /* Check length and offset for 64k alignment */
1389         if ((ofs & (SZ_64K - 1)) || (len & (SZ_64K - 1))) {
1390                 dev_err(nor->dev, "length or offset is not 64KiB allighned\n");
1391                 return -EINVAL;
1392         }
1393
1394         if (ofs + len > mtd->size) {
1395                 dev_err(nor->dev, "range is more than device size: %#llx + %#llx > %#llx\n",
1396                         ofs, len, mtd->size);
1397                 return -EINVAL;
1398         }
1399
1400         /* SST26 family has only 16 Mbit, 32 Mbit and 64 Mbit IC */
1401         if (mtd->size != SZ_2M &&
1402             mtd->size != SZ_4M &&
1403             mtd->size != SZ_8M)
1404                 return -EINVAL;
1405
1406         bpr_size = 2 + (mtd->size / SZ_64K / 8);
1407
1408         ret = nor->read_reg(nor, SPINOR_OP_READ_BPR, bpr_buff, bpr_size);
1409         if (ret < 0) {
1410                 dev_err(nor->dev, "fail to read block-protection register\n");
1411                 return ret;
1412         }
1413
1414         rptr_64k = min_t(u32, ofs + len, mtd->size - SST26_BOUND_REG_SIZE);
1415         lptr_64k = max_t(u32, ofs, SST26_BOUND_REG_SIZE);
1416
1417         upper_64k = ((ofs + len) > (mtd->size - SST26_BOUND_REG_SIZE));
1418         lower_64k = (ofs < SST26_BOUND_REG_SIZE);
1419
1420         /* Lower bits in block-protection register are about 64k region */
1421         bpr_ptr = lptr_64k / SZ_64K - 1;
1422
1423         /* Process 64K blocks region */
1424         while (lptr_64k < rptr_64k) {
1425                 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1426                         return EACCES;
1427
1428                 bpr_ptr++;
1429                 lptr_64k += SZ_64K;
1430         }
1431
1432         /* 32K and 8K region bits in BPR are after 64k region bits */
1433         bpr_ptr = (mtd->size - 2 * SST26_BOUND_REG_SIZE) / SZ_64K;
1434
1435         /* Process lower 32K block region */
1436         if (lower_64k)
1437                 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1438                         return EACCES;
1439
1440         bpr_ptr++;
1441
1442         /* Process upper 32K block region */
1443         if (upper_64k)
1444                 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1445                         return EACCES;
1446
1447         bpr_ptr++;
1448
1449         /* Process lower 8K block regions */
1450         for (i = 0; i < SST26_BPR_8K_NUM; i++) {
1451                 if (lower_64k)
1452                         if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1453                                 return EACCES;
1454
1455                 /* In 8K area BPR has both read and write protection bits */
1456                 bpr_ptr += 2;
1457         }
1458
1459         /* Process upper 8K block regions */
1460         for (i = 0; i < SST26_BPR_8K_NUM; i++) {
1461                 if (upper_64k)
1462                         if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
1463                                 return EACCES;
1464
1465                 /* In 8K area BPR has both read and write protection bits */
1466                 bpr_ptr += 2;
1467         }
1468
1469         /* If we check region status we don't need to write BPR back */
1470         if (ctl == SST26_CTL_CHECK)
1471                 return 0;
1472
1473         ret = nor->write_reg(nor, SPINOR_OP_WRITE_BPR, bpr_buff, bpr_size);
1474         if (ret < 0) {
1475                 dev_err(nor->dev, "fail to write block-protection register\n");
1476                 return ret;
1477         }
1478
1479         return 0;
1480 }
1481
1482 static int sst26_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1483 {
1484         return sst26_lock_ctl(nor, ofs, len, SST26_CTL_UNLOCK);
1485 }
1486
1487 static int sst26_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
1488 {
1489         return sst26_lock_ctl(nor, ofs, len, SST26_CTL_LOCK);
1490 }
1491
1492 /*
1493  * Returns EACCES (positive value) if region is locked, 0 if region is unlocked,
1494  * and negative on errors.
1495  */
1496 static int sst26_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
1497 {
1498         /*
1499          * is_locked function is used for check before reading or erasing flash
1500          * region, so offset and length might be not 64k allighned, so adjust
1501          * them to be 64k allighned as sst26_lock_ctl works only with 64k
1502          * allighned regions.
1503          */
1504         ofs -= ofs & (SZ_64K - 1);
1505         len = len & (SZ_64K - 1) ? (len & ~(SZ_64K - 1)) + SZ_64K : len;
1506
1507         return sst26_lock_ctl(nor, ofs, len, SST26_CTL_CHECK);
1508 }
1509
1510 static int sst_write_byteprogram(struct spi_nor *nor, loff_t to, size_t len,
1511                                  size_t *retlen, const u_char *buf)
1512 {
1513         size_t actual;
1514         int ret = 0;
1515
1516         for (actual = 0; actual < len; actual++) {
1517                 nor->program_opcode = SPINOR_OP_BP;
1518
1519                 write_enable(nor);
1520                 /* write one byte. */
1521                 ret = nor->write(nor, to, 1, buf + actual);
1522                 if (ret < 0)
1523                         goto sst_write_err;
1524                 ret = spi_nor_wait_till_ready(nor);
1525                 if (ret)
1526                         goto sst_write_err;
1527                 to++;
1528         }
1529
1530 sst_write_err:
1531         write_disable(nor);
1532         return ret;
1533 }
1534
1535 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
1536                      size_t *retlen, const u_char *buf)
1537 {
1538         struct spi_nor *nor = mtd_to_spi_nor(mtd);
1539         struct spi_slave *spi = nor->spi;
1540         size_t actual;
1541         int ret;
1542
1543         dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1544         if (spi->mode & SPI_TX_BYTE)
1545                 return sst_write_byteprogram(nor, to, len, retlen, buf);
1546
1547         write_enable(nor);
1548
1549         nor->sst_write_second = false;
1550
1551         actual = to % 2;
1552         /* Start write from odd address. */
1553         if (actual) {
1554                 nor->program_opcode = SPINOR_OP_BP;
1555
1556                 /* write one byte. */
1557                 ret = nor->write(nor, to, 1, buf);
1558                 if (ret < 0)
1559                         goto sst_write_err;
1560                 ret = spi_nor_wait_till_ready(nor);
1561                 if (ret)
1562                         goto sst_write_err;
1563         }
1564         to += actual;
1565
1566         /* Write out most of the data here. */
1567         for (; actual < len - 1; actual += 2) {
1568                 nor->program_opcode = SPINOR_OP_AAI_WP;
1569
1570                 /* write two bytes. */
1571                 ret = nor->write(nor, to, 2, buf + actual);
1572                 if (ret < 0)
1573                         goto sst_write_err;
1574                 ret = spi_nor_wait_till_ready(nor);
1575                 if (ret)
1576                         goto sst_write_err;
1577                 to += 2;
1578                 nor->sst_write_second = true;
1579         }
1580         nor->sst_write_second = false;
1581
1582         write_disable(nor);
1583         ret = spi_nor_wait_till_ready(nor);
1584         if (ret)
1585                 goto sst_write_err;
1586
1587         /* Write out trailing byte if it exists. */
1588         if (actual != len) {
1589                 write_enable(nor);
1590
1591                 nor->program_opcode = SPINOR_OP_BP;
1592                 ret = nor->write(nor, to, 1, buf + actual);
1593                 if (ret < 0)
1594                         goto sst_write_err;
1595                 ret = spi_nor_wait_till_ready(nor);
1596                 if (ret)
1597                         goto sst_write_err;
1598                 write_disable(nor);
1599                 actual += 1;
1600         }
1601 sst_write_err:
1602         *retlen += actual;
1603         return ret;
1604 }
1605 #endif
1606 /*
1607  * Write an address range to the nor chip.  Data must be written in
1608  * FLASH_PAGESIZE chunks.  The address range may be any size provided
1609  * it is within the physical boundaries.
1610  */
1611 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
1612         size_t *retlen, const u_char *buf)
1613 {
1614         struct spi_nor *nor = mtd_to_spi_nor(mtd);
1615         size_t page_offset, page_remain, i;
1616         ssize_t ret;
1617
1618 #ifdef CONFIG_SPI_FLASH_SST
1619         /* sst nor chips use AAI word program */
1620         if (nor->info->flags & SST_WRITE)
1621                 return sst_write(mtd, to, len, retlen, buf);
1622 #endif
1623
1624         dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
1625
1626         if (!len)
1627                 return 0;
1628
1629         for (i = 0; i < len; ) {
1630                 ssize_t written;
1631                 loff_t addr = to + i;
1632                 WATCHDOG_RESET();
1633
1634                 /*
1635                  * If page_size is a power of two, the offset can be quickly
1636                  * calculated with an AND operation. On the other cases we
1637                  * need to do a modulus operation (more expensive).
1638                  */
1639                 if (is_power_of_2(nor->page_size)) {
1640                         page_offset = addr & (nor->page_size - 1);
1641                 } else {
1642                         u64 aux = addr;
1643
1644                         page_offset = do_div(aux, nor->page_size);
1645                 }
1646                 /* the size of data remaining on the first page */
1647                 page_remain = min_t(size_t,
1648                                     nor->page_size - page_offset, len - i);
1649
1650 #ifdef CONFIG_SPI_FLASH_BAR
1651                 ret = write_bar(nor, addr);
1652                 if (ret < 0)
1653                         return ret;
1654 #endif
1655                 write_enable(nor);
1656                 ret = nor->write(nor, addr, page_remain, buf + i);
1657                 if (ret < 0)
1658                         goto write_err;
1659                 written = ret;
1660
1661                 ret = spi_nor_wait_till_ready(nor);
1662                 if (ret)
1663                         goto write_err;
1664                 *retlen += written;
1665                 i += written;
1666         }
1667
1668 write_err:
1669 #ifdef CONFIG_SPI_FLASH_BAR
1670         ret = clean_bar(nor);
1671 #endif
1672         return ret;
1673 }
1674
1675 #if defined(CONFIG_SPI_FLASH_MACRONIX) || defined(CONFIG_SPI_FLASH_ISSI)
1676 /**
1677  * macronix_quad_enable() - set QE bit in Status Register.
1678  * @nor:        pointer to a 'struct spi_nor'
1679  *
1680  * Set the Quad Enable (QE) bit in the Status Register.
1681  *
1682  * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
1683  *
1684  * Return: 0 on success, -errno otherwise.
1685  */
1686 static int macronix_quad_enable(struct spi_nor *nor)
1687 {
1688         int ret, val;
1689
1690         val = read_sr(nor);
1691         if (val < 0)
1692                 return val;
1693         if (val & SR_QUAD_EN_MX)
1694                 return 0;
1695
1696         write_enable(nor);
1697
1698         write_sr(nor, val | SR_QUAD_EN_MX);
1699
1700         ret = spi_nor_wait_till_ready(nor);
1701         if (ret)
1702                 return ret;
1703
1704         ret = read_sr(nor);
1705         if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
1706                 dev_err(nor->dev, "Macronix Quad bit not set\n");
1707                 return -EINVAL;
1708         }
1709
1710         return 0;
1711 }
1712 #endif
1713
1714 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
1715 /*
1716  * Write status Register and configuration register with 2 bytes
1717  * The first byte will be written to the status register, while the
1718  * second byte will be written to the configuration register.
1719  * Return negative if error occurred.
1720  */
1721 static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
1722 {
1723         int ret;
1724
1725         write_enable(nor);
1726
1727         ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
1728         if (ret < 0) {
1729                 dev_dbg(nor->dev,
1730                         "error while writing configuration register\n");
1731                 return -EINVAL;
1732         }
1733
1734         ret = spi_nor_wait_till_ready(nor);
1735         if (ret) {
1736                 dev_dbg(nor->dev,
1737                         "timeout while writing configuration register\n");
1738                 return ret;
1739         }
1740
1741         return 0;
1742 }
1743
1744 /**
1745  * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
1746  * @nor:        pointer to a 'struct spi_nor'
1747  *
1748  * Set the Quad Enable (QE) bit in the Configuration Register.
1749  * This function should be used with QSPI memories supporting the Read
1750  * Configuration Register (35h) instruction.
1751  *
1752  * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1753  * memories.
1754  *
1755  * Return: 0 on success, -errno otherwise.
1756  */
1757 static int spansion_read_cr_quad_enable(struct spi_nor *nor)
1758 {
1759         u8 sr_cr[2];
1760         int ret;
1761
1762         /* Check current Quad Enable bit value. */
1763         ret = read_cr(nor);
1764         if (ret < 0) {
1765                 dev_dbg(nor->dev,
1766                         "error while reading configuration register\n");
1767                 return -EINVAL;
1768         }
1769
1770         if (ret & CR_QUAD_EN_SPAN)
1771                 return 0;
1772
1773         sr_cr[1] = ret | CR_QUAD_EN_SPAN;
1774
1775         /* Keep the current value of the Status Register. */
1776         ret = read_sr(nor);
1777         if (ret < 0) {
1778                 dev_dbg(nor->dev, "error while reading status register\n");
1779                 return -EINVAL;
1780         }
1781         sr_cr[0] = ret;
1782
1783         ret = write_sr_cr(nor, sr_cr);
1784         if (ret)
1785                 return ret;
1786
1787         /* Read back and check it. */
1788         ret = read_cr(nor);
1789         if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
1790                 dev_dbg(nor->dev, "Spansion Quad bit not set\n");
1791                 return -EINVAL;
1792         }
1793
1794         return 0;
1795 }
1796
1797 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
1798 /**
1799  * spansion_no_read_cr_quad_enable() - set QE bit in Configuration Register.
1800  * @nor:        pointer to a 'struct spi_nor'
1801  *
1802  * Set the Quad Enable (QE) bit in the Configuration Register.
1803  * This function should be used with QSPI memories not supporting the Read
1804  * Configuration Register (35h) instruction.
1805  *
1806  * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
1807  * memories.
1808  *
1809  * Return: 0 on success, -errno otherwise.
1810  */
1811 static int spansion_no_read_cr_quad_enable(struct spi_nor *nor)
1812 {
1813         u8 sr_cr[2];
1814         int ret;
1815
1816         /* Keep the current value of the Status Register. */
1817         ret = read_sr(nor);
1818         if (ret < 0) {
1819                 dev_dbg(nor->dev, "error while reading status register\n");
1820                 return -EINVAL;
1821         }
1822         sr_cr[0] = ret;
1823         sr_cr[1] = CR_QUAD_EN_SPAN;
1824
1825         return write_sr_cr(nor, sr_cr);
1826 }
1827
1828 #endif /* CONFIG_SPI_FLASH_SFDP_SUPPORT */
1829 #endif /* CONFIG_SPI_FLASH_SPANSION */
1830
1831 static void
1832 spi_nor_set_read_settings(struct spi_nor_read_command *read,
1833                           u8 num_mode_clocks,
1834                           u8 num_wait_states,
1835                           u8 opcode,
1836                           enum spi_nor_protocol proto)
1837 {
1838         read->num_mode_clocks = num_mode_clocks;
1839         read->num_wait_states = num_wait_states;
1840         read->opcode = opcode;
1841         read->proto = proto;
1842 }
1843
1844 static void
1845 spi_nor_set_pp_settings(struct spi_nor_pp_command *pp,
1846                         u8 opcode,
1847                         enum spi_nor_protocol proto)
1848 {
1849         pp->opcode = opcode;
1850         pp->proto = proto;
1851 }
1852
1853 #if CONFIG_IS_ENABLED(SPI_FLASH_SFDP_SUPPORT)
1854 /*
1855  * Serial Flash Discoverable Parameters (SFDP) parsing.
1856  */
1857
1858 /**
1859  * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
1860  * @nor:        pointer to a 'struct spi_nor'
1861  * @addr:       offset in the SFDP area to start reading data from
1862  * @len:        number of bytes to read
1863  * @buf:        buffer where the SFDP data are copied into (dma-safe memory)
1864  *
1865  * Whatever the actual numbers of bytes for address and dummy cycles are
1866  * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
1867  * followed by a 3-byte address and 8 dummy clock cycles.
1868  *
1869  * Return: 0 on success, -errno otherwise.
1870  */
1871 static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
1872                              size_t len, void *buf)
1873 {
1874         u8 addr_width, read_opcode, read_dummy;
1875         int ret;
1876
1877         read_opcode = nor->read_opcode;
1878         addr_width = nor->addr_width;
1879         read_dummy = nor->read_dummy;
1880
1881         nor->read_opcode = SPINOR_OP_RDSFDP;
1882         nor->addr_width = 3;
1883         nor->read_dummy = 8;
1884
1885         while (len) {
1886                 ret = nor->read(nor, addr, len, (u8 *)buf);
1887                 if (!ret || ret > len) {
1888                         ret = -EIO;
1889                         goto read_err;
1890                 }
1891                 if (ret < 0)
1892                         goto read_err;
1893
1894                 buf += ret;
1895                 addr += ret;
1896                 len -= ret;
1897         }
1898         ret = 0;
1899
1900 read_err:
1901         nor->read_opcode = read_opcode;
1902         nor->addr_width = addr_width;
1903         nor->read_dummy = read_dummy;
1904
1905         return ret;
1906 }
1907
1908 /* Fast Read settings. */
1909
1910 static void
1911 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
1912                                     u16 half,
1913                                     enum spi_nor_protocol proto)
1914 {
1915         read->num_mode_clocks = (half >> 5) & 0x07;
1916         read->num_wait_states = (half >> 0) & 0x1f;
1917         read->opcode = (half >> 8) & 0xff;
1918         read->proto = proto;
1919 }
1920
1921 struct sfdp_bfpt_read {
1922         /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
1923         u32                     hwcaps;
1924
1925         /*
1926          * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
1927          * whether the Fast Read x-y-z command is supported.
1928          */
1929         u32                     supported_dword;
1930         u32                     supported_bit;
1931
1932         /*
1933          * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
1934          * encodes the op code, the number of mode clocks and the number of wait
1935          * states to be used by Fast Read x-y-z command.
1936          */
1937         u32                     settings_dword;
1938         u32                     settings_shift;
1939
1940         /* The SPI protocol for this Fast Read x-y-z command. */
1941         enum spi_nor_protocol   proto;
1942 };
1943
1944 static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
1945         /* Fast Read 1-1-2 */
1946         {
1947                 SNOR_HWCAPS_READ_1_1_2,
1948                 BFPT_DWORD(1), BIT(16), /* Supported bit */
1949                 BFPT_DWORD(4), 0,       /* Settings */
1950                 SNOR_PROTO_1_1_2,
1951         },
1952
1953         /* Fast Read 1-2-2 */
1954         {
1955                 SNOR_HWCAPS_READ_1_2_2,
1956                 BFPT_DWORD(1), BIT(20), /* Supported bit */
1957                 BFPT_DWORD(4), 16,      /* Settings */
1958                 SNOR_PROTO_1_2_2,
1959         },
1960
1961         /* Fast Read 2-2-2 */
1962         {
1963                 SNOR_HWCAPS_READ_2_2_2,
1964                 BFPT_DWORD(5),  BIT(0), /* Supported bit */
1965                 BFPT_DWORD(6), 16,      /* Settings */
1966                 SNOR_PROTO_2_2_2,
1967         },
1968
1969         /* Fast Read 1-1-4 */
1970         {
1971                 SNOR_HWCAPS_READ_1_1_4,
1972                 BFPT_DWORD(1), BIT(22), /* Supported bit */
1973                 BFPT_DWORD(3), 16,      /* Settings */
1974                 SNOR_PROTO_1_1_4,
1975         },
1976
1977         /* Fast Read 1-4-4 */
1978         {
1979                 SNOR_HWCAPS_READ_1_4_4,
1980                 BFPT_DWORD(1), BIT(21), /* Supported bit */
1981                 BFPT_DWORD(3), 0,       /* Settings */
1982                 SNOR_PROTO_1_4_4,
1983         },
1984
1985         /* Fast Read 4-4-4 */
1986         {
1987                 SNOR_HWCAPS_READ_4_4_4,
1988                 BFPT_DWORD(5), BIT(4),  /* Supported bit */
1989                 BFPT_DWORD(7), 16,      /* Settings */
1990                 SNOR_PROTO_4_4_4,
1991         },
1992 };
1993
1994 struct sfdp_bfpt_erase {
1995         /*
1996          * The half-word at offset <shift> in DWORD <dwoard> encodes the
1997          * op code and erase sector size to be used by Sector Erase commands.
1998          */
1999         u32                     dword;
2000         u32                     shift;
2001 };
2002
2003 static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
2004         /* Erase Type 1 in DWORD8 bits[15:0] */
2005         {BFPT_DWORD(8), 0},
2006
2007         /* Erase Type 2 in DWORD8 bits[31:16] */
2008         {BFPT_DWORD(8), 16},
2009
2010         /* Erase Type 3 in DWORD9 bits[15:0] */
2011         {BFPT_DWORD(9), 0},
2012
2013         /* Erase Type 4 in DWORD9 bits[31:16] */
2014         {BFPT_DWORD(9), 16},
2015 };
2016
2017 static int spi_nor_hwcaps_read2cmd(u32 hwcaps);
2018
2019 static int
2020 spi_nor_post_bfpt_fixups(struct spi_nor *nor,
2021                          const struct sfdp_parameter_header *bfpt_header,
2022                          const struct sfdp_bfpt *bfpt,
2023                          struct spi_nor_flash_parameter *params)
2024 {
2025         if (nor->fixups && nor->fixups->post_bfpt)
2026                 return nor->fixups->post_bfpt(nor, bfpt_header, bfpt, params);
2027
2028         return 0;
2029 }
2030
2031 /**
2032  * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
2033  * @nor:                pointer to a 'struct spi_nor'
2034  * @bfpt_header:        pointer to the 'struct sfdp_parameter_header' describing
2035  *                      the Basic Flash Parameter Table length and version
2036  * @params:             pointer to the 'struct spi_nor_flash_parameter' to be
2037  *                      filled
2038  *
2039  * The Basic Flash Parameter Table is the main and only mandatory table as
2040  * defined by the SFDP (JESD216) specification.
2041  * It provides us with the total size (memory density) of the data array and
2042  * the number of address bytes for Fast Read, Page Program and Sector Erase
2043  * commands.
2044  * For Fast READ commands, it also gives the number of mode clock cycles and
2045  * wait states (regrouped in the number of dummy clock cycles) for each
2046  * supported instruction op code.
2047  * For Page Program, the page size is now available since JESD216 rev A, however
2048  * the supported instruction op codes are still not provided.
2049  * For Sector Erase commands, this table stores the supported instruction op
2050  * codes and the associated sector sizes.
2051  * Finally, the Quad Enable Requirements (QER) are also available since JESD216
2052  * rev A. The QER bits encode the manufacturer dependent procedure to be
2053  * executed to set the Quad Enable (QE) bit in some internal register of the
2054  * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
2055  * sending any Quad SPI command to the memory. Actually, setting the QE bit
2056  * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
2057  * and IO3 hence enabling 4 (Quad) I/O lines.
2058  *
2059  * Return: 0 on success, -errno otherwise.
2060  */
2061 static int spi_nor_parse_bfpt(struct spi_nor *nor,
2062                               const struct sfdp_parameter_header *bfpt_header,
2063                               struct spi_nor_flash_parameter *params)
2064 {
2065         struct mtd_info *mtd = &nor->mtd;
2066         struct sfdp_bfpt bfpt;
2067         size_t len;
2068         int i, cmd, err;
2069         u32 addr;
2070         u16 half;
2071
2072         /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
2073         if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
2074                 return -EINVAL;
2075
2076         /* Read the Basic Flash Parameter Table. */
2077         len = min_t(size_t, sizeof(bfpt),
2078                     bfpt_header->length * sizeof(u32));
2079         addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
2080         memset(&bfpt, 0, sizeof(bfpt));
2081         err = spi_nor_read_sfdp(nor,  addr, len, &bfpt);
2082         if (err < 0)
2083                 return err;
2084
2085         /* Fix endianness of the BFPT DWORDs. */
2086         for (i = 0; i < BFPT_DWORD_MAX; i++)
2087                 bfpt.dwords[i] = le32_to_cpu(bfpt.dwords[i]);
2088
2089         /* Number of address bytes. */
2090         switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
2091         case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
2092                 nor->addr_width = 3;
2093                 break;
2094
2095         case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
2096                 nor->addr_width = 4;
2097                 break;
2098
2099         default:
2100                 break;
2101         }
2102
2103         /* Flash Memory Density (in bits). */
2104         params->size = bfpt.dwords[BFPT_DWORD(2)];
2105         if (params->size & BIT(31)) {
2106                 params->size &= ~BIT(31);
2107
2108                 /*
2109                  * Prevent overflows on params->size. Anyway, a NOR of 2^64
2110                  * bits is unlikely to exist so this error probably means
2111                  * the BFPT we are reading is corrupted/wrong.
2112                  */
2113                 if (params->size > 63)
2114                         return -EINVAL;
2115
2116                 params->size = 1ULL << params->size;
2117         } else {
2118                 params->size++;
2119         }
2120         params->size >>= 3; /* Convert to bytes. */
2121
2122         /* Fast Read settings. */
2123         for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
2124                 const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
2125                 struct spi_nor_read_command *read;
2126
2127                 if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
2128                         params->hwcaps.mask &= ~rd->hwcaps;
2129                         continue;
2130                 }
2131
2132                 params->hwcaps.mask |= rd->hwcaps;
2133                 cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
2134                 read = &params->reads[cmd];
2135                 half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
2136                 spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
2137         }
2138
2139         /* Sector Erase settings. */
2140         for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
2141                 const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
2142                 u32 erasesize;
2143                 u8 opcode;
2144
2145                 half = bfpt.dwords[er->dword] >> er->shift;
2146                 erasesize = half & 0xff;
2147
2148                 /* erasesize == 0 means this Erase Type is not supported. */
2149                 if (!erasesize)
2150                         continue;
2151
2152                 erasesize = 1U << erasesize;
2153                 opcode = (half >> 8) & 0xff;
2154 #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
2155                 if (erasesize == SZ_4K) {
2156                         nor->erase_opcode = opcode;
2157                         mtd->erasesize = erasesize;
2158                         break;
2159                 }
2160 #endif
2161                 if (!mtd->erasesize || mtd->erasesize < erasesize) {
2162                         nor->erase_opcode = opcode;
2163                         mtd->erasesize = erasesize;
2164                 }
2165         }
2166
2167         /* Stop here if not JESD216 rev A or later. */
2168         if (bfpt_header->length == BFPT_DWORD_MAX_JESD216)
2169                 return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt,
2170                                                 params);
2171
2172         /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
2173         params->page_size = bfpt.dwords[BFPT_DWORD(11)];
2174         params->page_size &= BFPT_DWORD11_PAGE_SIZE_MASK;
2175         params->page_size >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
2176         params->page_size = 1U << params->page_size;
2177
2178         /* Quad Enable Requirements. */
2179         switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
2180         case BFPT_DWORD15_QER_NONE:
2181                 params->quad_enable = NULL;
2182                 break;
2183 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2184         case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
2185         case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
2186                 params->quad_enable = spansion_no_read_cr_quad_enable;
2187                 break;
2188 #endif
2189 #if defined(CONFIG_SPI_FLASH_MACRONIX) || defined(CONFIG_SPI_FLASH_ISSI)
2190         case BFPT_DWORD15_QER_SR1_BIT6:
2191                 params->quad_enable = macronix_quad_enable;
2192                 break;
2193 #endif
2194 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2195         case BFPT_DWORD15_QER_SR2_BIT1:
2196                 params->quad_enable = spansion_read_cr_quad_enable;
2197                 break;
2198 #endif
2199         default:
2200                 dev_dbg(nor->dev, "BFPT QER reserved value used\n");
2201                 break;
2202         }
2203
2204         /* Soft Reset support. */
2205         if (bfpt.dwords[BFPT_DWORD(16)] & BFPT_DWORD16_SOFT_RST)
2206                 nor->flags |= SNOR_F_SOFT_RESET;
2207
2208         /* Stop here if JESD216 rev B. */
2209         if (bfpt_header->length == BFPT_DWORD_MAX_JESD216B)
2210                 return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt,
2211                                                 params);
2212
2213         /* 8D-8D-8D command extension. */
2214         switch (bfpt.dwords[BFPT_DWORD(18)] & BFPT_DWORD18_CMD_EXT_MASK) {
2215         case BFPT_DWORD18_CMD_EXT_REP:
2216                 nor->cmd_ext_type = SPI_NOR_EXT_REPEAT;
2217                 break;
2218
2219         case BFPT_DWORD18_CMD_EXT_INV:
2220                 nor->cmd_ext_type = SPI_NOR_EXT_INVERT;
2221                 break;
2222
2223         case BFPT_DWORD18_CMD_EXT_RES:
2224                 return -EINVAL;
2225
2226         case BFPT_DWORD18_CMD_EXT_16B:
2227                 dev_err(nor->dev, "16-bit opcodes not supported\n");
2228                 return -ENOTSUPP;
2229         }
2230
2231         return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt, params);
2232 }
2233
2234 /**
2235  * spi_nor_parse_microchip_sfdp() - parse the Microchip manufacturer specific
2236  * SFDP table.
2237  * @nor:                pointer to a 'struct spi_nor'.
2238  * @param_header:       pointer to the SFDP parameter header.
2239  *
2240  * Return: 0 on success, -errno otherwise.
2241  */
2242 static int
2243 spi_nor_parse_microchip_sfdp(struct spi_nor *nor,
2244                              const struct sfdp_parameter_header *param_header)
2245 {
2246         size_t size;
2247         u32 addr;
2248         int ret;
2249
2250         size = param_header->length * sizeof(u32);
2251         addr = SFDP_PARAM_HEADER_PTP(param_header);
2252
2253         nor->manufacturer_sfdp = devm_kmalloc(nor->dev, size, GFP_KERNEL);
2254         if (!nor->manufacturer_sfdp)
2255                 return -ENOMEM;
2256
2257         ret = spi_nor_read_sfdp(nor, addr, size, nor->manufacturer_sfdp);
2258
2259         return ret;
2260 }
2261
2262 /**
2263  * spi_nor_parse_profile1() - parse the xSPI Profile 1.0 table
2264  * @nor:                pointer to a 'struct spi_nor'
2265  * @profile1_header:    pointer to the 'struct sfdp_parameter_header' describing
2266  *                      the 4-Byte Address Instruction Table length and version.
2267  * @params:             pointer to the 'struct spi_nor_flash_parameter' to be.
2268  *
2269  * Return: 0 on success, -errno otherwise.
2270  */
2271 static int spi_nor_parse_profile1(struct spi_nor *nor,
2272                                   const struct sfdp_parameter_header *profile1_header,
2273                                   struct spi_nor_flash_parameter *params)
2274 {
2275         u32 *table, opcode, addr;
2276         size_t len;
2277         int ret, i;
2278         u8 dummy;
2279
2280         len = profile1_header->length * sizeof(*table);
2281         table = kmalloc(len, GFP_KERNEL);
2282         if (!table)
2283                 return -ENOMEM;
2284
2285         addr = SFDP_PARAM_HEADER_PTP(profile1_header);
2286         ret = spi_nor_read_sfdp(nor, addr, len, table);
2287         if (ret)
2288                 goto out;
2289
2290         /* Fix endianness of the table DWORDs. */
2291         for (i = 0; i < profile1_header->length; i++)
2292                 table[i] = le32_to_cpu(table[i]);
2293
2294         /* Get 8D-8D-8D fast read opcode and dummy cycles. */
2295         opcode = FIELD_GET(PROFILE1_DWORD1_RD_FAST_CMD, table[0]);
2296
2297         /*
2298          * We don't know what speed the controller is running at. Find the
2299          * dummy cycles for the fastest frequency the flash can run at to be
2300          * sure we are never short of dummy cycles. A value of 0 means the
2301          * frequency is not supported.
2302          *
2303          * Default to PROFILE1_DUMMY_DEFAULT if we don't find anything, and let
2304          * flashes set the correct value if needed in their fixup hooks.
2305          */
2306         dummy = FIELD_GET(PROFILE1_DWORD4_DUMMY_200MHZ, table[3]);
2307         if (!dummy)
2308                 dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_166MHZ, table[4]);
2309         if (!dummy)
2310                 dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_133MHZ, table[4]);
2311         if (!dummy)
2312                 dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_100MHZ, table[4]);
2313         if (!dummy)
2314                 dummy = PROFILE1_DUMMY_DEFAULT;
2315
2316         /* Round up to an even value to avoid tripping controllers up. */
2317         dummy = ROUND_UP_TO(dummy, 2);
2318
2319         /* Update the fast read settings. */
2320         spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_8_8_8_DTR],
2321                                   0, dummy, opcode,
2322                                   SNOR_PROTO_8_8_8_DTR);
2323
2324         /*
2325          * Set the Read Status Register dummy cycles and dummy address bytes.
2326          */
2327         if (table[0] & PROFILE1_DWORD1_RDSR_DUMMY)
2328                 params->rdsr_dummy = 8;
2329         else
2330                 params->rdsr_dummy = 4;
2331
2332         if (table[0] & PROFILE1_DWORD1_RDSR_ADDR_BYTES)
2333                 params->rdsr_addr_nbytes = 4;
2334         else
2335                 params->rdsr_addr_nbytes = 0;
2336
2337 out:
2338         kfree(table);
2339         return ret;
2340 }
2341
2342 /**
2343  * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
2344  * @nor:                pointer to a 'struct spi_nor'
2345  * @params:             pointer to the 'struct spi_nor_flash_parameter' to be
2346  *                      filled
2347  *
2348  * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
2349  * specification. This is a standard which tends to supported by almost all
2350  * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
2351  * runtime the main parameters needed to perform basic SPI flash operations such
2352  * as Fast Read, Page Program or Sector Erase commands.
2353  *
2354  * Return: 0 on success, -errno otherwise.
2355  */
2356 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2357                               struct spi_nor_flash_parameter *params)
2358 {
2359         const struct sfdp_parameter_header *param_header, *bfpt_header;
2360         struct sfdp_parameter_header *param_headers = NULL;
2361         struct sfdp_header header;
2362         size_t psize;
2363         int i, err;
2364
2365         /* Get the SFDP header. */
2366         err = spi_nor_read_sfdp(nor, 0, sizeof(header), &header);
2367         if (err < 0)
2368                 return err;
2369
2370         /* Check the SFDP header version. */
2371         if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
2372             header.major != SFDP_JESD216_MAJOR)
2373                 return -EINVAL;
2374
2375         /*
2376          * Verify that the first and only mandatory parameter header is a
2377          * Basic Flash Parameter Table header as specified in JESD216.
2378          */
2379         bfpt_header = &header.bfpt_header;
2380         if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
2381             bfpt_header->major != SFDP_JESD216_MAJOR)
2382                 return -EINVAL;
2383
2384         /*
2385          * Allocate memory then read all parameter headers with a single
2386          * Read SFDP command. These parameter headers will actually be parsed
2387          * twice: a first time to get the latest revision of the basic flash
2388          * parameter table, then a second time to handle the supported optional
2389          * tables.
2390          * Hence we read the parameter headers once for all to reduce the
2391          * processing time. Also we use kmalloc() instead of devm_kmalloc()
2392          * because we don't need to keep these parameter headers: the allocated
2393          * memory is always released with kfree() before exiting this function.
2394          */
2395         if (header.nph) {
2396                 psize = header.nph * sizeof(*param_headers);
2397
2398                 param_headers = kmalloc(psize, GFP_KERNEL);
2399                 if (!param_headers)
2400                         return -ENOMEM;
2401
2402                 err = spi_nor_read_sfdp(nor, sizeof(header),
2403                                         psize, param_headers);
2404                 if (err < 0) {
2405                         dev_err(nor->dev,
2406                                 "failed to read SFDP parameter headers\n");
2407                         goto exit;
2408                 }
2409         }
2410
2411         /*
2412          * Check other parameter headers to get the latest revision of
2413          * the basic flash parameter table.
2414          */
2415         for (i = 0; i < header.nph; i++) {
2416                 param_header = &param_headers[i];
2417
2418                 if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
2419                     param_header->major == SFDP_JESD216_MAJOR &&
2420                     (param_header->minor > bfpt_header->minor ||
2421                      (param_header->minor == bfpt_header->minor &&
2422                       param_header->length > bfpt_header->length)))
2423                         bfpt_header = param_header;
2424         }
2425
2426         err = spi_nor_parse_bfpt(nor, bfpt_header, params);
2427         if (err)
2428                 goto exit;
2429
2430         /* Parse other parameter headers. */
2431         for (i = 0; i < header.nph; i++) {
2432                 param_header = &param_headers[i];
2433
2434                 switch (SFDP_PARAM_HEADER_ID(param_header)) {
2435                 case SFDP_SECTOR_MAP_ID:
2436                         dev_info(nor->dev,
2437                                  "non-uniform erase sector maps are not supported yet.\n");
2438                         break;
2439
2440                 case SFDP_SST_ID:
2441                         err = spi_nor_parse_microchip_sfdp(nor, param_header);
2442                         break;
2443
2444                 case SFDP_PROFILE1_ID:
2445                         err = spi_nor_parse_profile1(nor, param_header, params);
2446                         break;
2447
2448                 default:
2449                         break;
2450                 }
2451
2452                 if (err) {
2453                         dev_warn(nor->dev,
2454                                  "Failed to parse optional parameter table: %04x\n",
2455                                  SFDP_PARAM_HEADER_ID(param_header));
2456                         /*
2457                          * Let's not drop all information we extracted so far
2458                          * if optional table parsers fail. In case of failing,
2459                          * each optional parser is responsible to roll back to
2460                          * the previously known spi_nor data.
2461                          */
2462                         err = 0;
2463                 }
2464         }
2465
2466 exit:
2467         kfree(param_headers);
2468         return err;
2469 }
2470 #else
2471 static int spi_nor_parse_sfdp(struct spi_nor *nor,
2472                               struct spi_nor_flash_parameter *params)
2473 {
2474         return -EINVAL;
2475 }
2476 #endif /* SPI_FLASH_SFDP_SUPPORT */
2477
2478 /**
2479  * spi_nor_post_sfdp_fixups() - Updates the flash's parameters and settings
2480  * after SFDP has been parsed (is also called for SPI NORs that do not
2481  * support RDSFDP).
2482  * @nor:        pointer to a 'struct spi_nor'
2483  *
2484  * Typically used to tweak various parameters that could not be extracted by
2485  * other means (i.e. when information provided by the SFDP/flash_info tables
2486  * are incomplete or wrong).
2487  */
2488 static void spi_nor_post_sfdp_fixups(struct spi_nor *nor,
2489                                      struct spi_nor_flash_parameter *params)
2490 {
2491         if (nor->fixups && nor->fixups->post_sfdp)
2492                 nor->fixups->post_sfdp(nor, params);
2493 }
2494
2495 static void spi_nor_default_init_fixups(struct spi_nor *nor)
2496 {
2497         if (nor->fixups && nor->fixups->default_init)
2498                 nor->fixups->default_init(nor);
2499 }
2500
2501 static int spi_nor_init_params(struct spi_nor *nor,
2502                                const struct flash_info *info,
2503                                struct spi_nor_flash_parameter *params)
2504 {
2505         /* Set legacy flash parameters as default. */
2506         memset(params, 0, sizeof(*params));
2507
2508         /* Set SPI NOR sizes. */
2509         params->size = info->sector_size * info->n_sectors;
2510         params->page_size = info->page_size;
2511
2512         /* (Fast) Read settings. */
2513         params->hwcaps.mask |= SNOR_HWCAPS_READ;
2514         spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
2515                                   0, 0, SPINOR_OP_READ,
2516                                   SNOR_PROTO_1_1_1);
2517
2518         if (!(info->flags & SPI_NOR_NO_FR)) {
2519                 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
2520                 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
2521                                           0, 8, SPINOR_OP_READ_FAST,
2522                                           SNOR_PROTO_1_1_1);
2523         }
2524
2525         if (info->flags & SPI_NOR_DUAL_READ) {
2526                 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
2527                 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_2],
2528                                           0, 8, SPINOR_OP_READ_1_1_2,
2529                                           SNOR_PROTO_1_1_2);
2530         }
2531
2532         if (info->flags & SPI_NOR_QUAD_READ) {
2533                 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
2534                 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
2535                                           0, 8, SPINOR_OP_READ_1_1_4,
2536                                           SNOR_PROTO_1_1_4);
2537         }
2538
2539         if (info->flags & SPI_NOR_OCTAL_READ) {
2540                 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
2541                 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_8],
2542                                           0, 8, SPINOR_OP_READ_1_1_8,
2543                                           SNOR_PROTO_1_1_8);
2544         }
2545
2546         if (info->flags & SPI_NOR_OCTAL_DTR_READ) {
2547                 params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR;
2548                 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_8_8_8_DTR],
2549                                           0, 20, SPINOR_OP_READ_FAST,
2550                                           SNOR_PROTO_8_8_8_DTR);
2551         }
2552
2553         /* Page Program settings. */
2554         params->hwcaps.mask |= SNOR_HWCAPS_PP;
2555         spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP],
2556                                 SPINOR_OP_PP, SNOR_PROTO_1_1_1);
2557
2558         /*
2559          * Since xSPI Page Program opcode is backward compatible with
2560          * Legacy SPI, use Legacy SPI opcode there as well.
2561          */
2562         spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_8_8_8_DTR],
2563                                 SPINOR_OP_PP, SNOR_PROTO_8_8_8_DTR);
2564
2565         if (info->flags & SPI_NOR_QUAD_READ) {
2566                 params->hwcaps.mask |= SNOR_HWCAPS_PP_1_1_4;
2567                 spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_1_1_4],
2568                                         SPINOR_OP_PP_1_1_4, SNOR_PROTO_1_1_4);
2569         }
2570
2571         /* Select the procedure to set the Quad Enable bit. */
2572         if (params->hwcaps.mask & (SNOR_HWCAPS_READ_QUAD |
2573                                    SNOR_HWCAPS_PP_QUAD)) {
2574                 switch (JEDEC_MFR(info)) {
2575 #if defined(CONFIG_SPI_FLASH_MACRONIX) || defined(CONFIG_SPI_FLASH_ISSI)
2576                 case SNOR_MFR_MACRONIX:
2577                 case SNOR_MFR_ISSI:
2578                         params->quad_enable = macronix_quad_enable;
2579                         break;
2580 #endif
2581                 case SNOR_MFR_ST:
2582                 case SNOR_MFR_MICRON:
2583                         break;
2584
2585                 default:
2586 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
2587                         /* Kept only for backward compatibility purpose. */
2588                         params->quad_enable = spansion_read_cr_quad_enable;
2589 #endif
2590                         break;
2591                 }
2592         }
2593
2594         spi_nor_default_init_fixups(nor);
2595
2596         /* Override the parameters with data read from SFDP tables. */
2597         nor->addr_width = 0;
2598         nor->mtd.erasesize = 0;
2599         if ((info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ |
2600              SPI_NOR_OCTAL_DTR_READ)) &&
2601             !(info->flags & SPI_NOR_SKIP_SFDP)) {
2602                 struct spi_nor_flash_parameter sfdp_params;
2603
2604                 memcpy(&sfdp_params, params, sizeof(sfdp_params));
2605                 if (spi_nor_parse_sfdp(nor, &sfdp_params)) {
2606                         nor->addr_width = 0;
2607                         nor->mtd.erasesize = 0;
2608                 } else {
2609                         memcpy(params, &sfdp_params, sizeof(*params));
2610                 }
2611         }
2612
2613         spi_nor_post_sfdp_fixups(nor, params);
2614
2615         return 0;
2616 }
2617
2618 static int spi_nor_hwcaps2cmd(u32 hwcaps, const int table[][2], size_t size)
2619 {
2620         size_t i;
2621
2622         for (i = 0; i < size; i++)
2623                 if (table[i][0] == (int)hwcaps)
2624                         return table[i][1];
2625
2626         return -EINVAL;
2627 }
2628
2629 static int spi_nor_hwcaps_read2cmd(u32 hwcaps)
2630 {
2631         static const int hwcaps_read2cmd[][2] = {
2632                 { SNOR_HWCAPS_READ,             SNOR_CMD_READ },
2633                 { SNOR_HWCAPS_READ_FAST,        SNOR_CMD_READ_FAST },
2634                 { SNOR_HWCAPS_READ_1_1_1_DTR,   SNOR_CMD_READ_1_1_1_DTR },
2635                 { SNOR_HWCAPS_READ_1_1_2,       SNOR_CMD_READ_1_1_2 },
2636                 { SNOR_HWCAPS_READ_1_2_2,       SNOR_CMD_READ_1_2_2 },
2637                 { SNOR_HWCAPS_READ_2_2_2,       SNOR_CMD_READ_2_2_2 },
2638                 { SNOR_HWCAPS_READ_1_2_2_DTR,   SNOR_CMD_READ_1_2_2_DTR },
2639                 { SNOR_HWCAPS_READ_1_1_4,       SNOR_CMD_READ_1_1_4 },
2640                 { SNOR_HWCAPS_READ_1_4_4,       SNOR_CMD_READ_1_4_4 },
2641                 { SNOR_HWCAPS_READ_4_4_4,       SNOR_CMD_READ_4_4_4 },
2642                 { SNOR_HWCAPS_READ_1_4_4_DTR,   SNOR_CMD_READ_1_4_4_DTR },
2643                 { SNOR_HWCAPS_READ_1_1_8,       SNOR_CMD_READ_1_1_8 },
2644                 { SNOR_HWCAPS_READ_1_8_8,       SNOR_CMD_READ_1_8_8 },
2645                 { SNOR_HWCAPS_READ_8_8_8,       SNOR_CMD_READ_8_8_8 },
2646                 { SNOR_HWCAPS_READ_1_8_8_DTR,   SNOR_CMD_READ_1_8_8_DTR },
2647                 { SNOR_HWCAPS_READ_8_8_8_DTR,   SNOR_CMD_READ_8_8_8_DTR },
2648         };
2649
2650         return spi_nor_hwcaps2cmd(hwcaps, hwcaps_read2cmd,
2651                                   ARRAY_SIZE(hwcaps_read2cmd));
2652 }
2653
2654 static int spi_nor_hwcaps_pp2cmd(u32 hwcaps)
2655 {
2656         static const int hwcaps_pp2cmd[][2] = {
2657                 { SNOR_HWCAPS_PP,               SNOR_CMD_PP },
2658                 { SNOR_HWCAPS_PP_1_1_4,         SNOR_CMD_PP_1_1_4 },
2659                 { SNOR_HWCAPS_PP_1_4_4,         SNOR_CMD_PP_1_4_4 },
2660                 { SNOR_HWCAPS_PP_4_4_4,         SNOR_CMD_PP_4_4_4 },
2661                 { SNOR_HWCAPS_PP_1_1_8,         SNOR_CMD_PP_1_1_8 },
2662                 { SNOR_HWCAPS_PP_1_8_8,         SNOR_CMD_PP_1_8_8 },
2663                 { SNOR_HWCAPS_PP_8_8_8,         SNOR_CMD_PP_8_8_8 },
2664                 { SNOR_HWCAPS_PP_8_8_8_DTR,     SNOR_CMD_PP_8_8_8_DTR },
2665         };
2666
2667         return spi_nor_hwcaps2cmd(hwcaps, hwcaps_pp2cmd,
2668                                   ARRAY_SIZE(hwcaps_pp2cmd));
2669 }
2670
2671 #ifdef CONFIG_SPI_FLASH_SMART_HWCAPS
2672 /**
2673  * spi_nor_check_op - check if the operation is supported by controller
2674  * @nor:        pointer to a 'struct spi_nor'
2675  * @op:         pointer to op template to be checked
2676  *
2677  * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2678  */
2679 static int spi_nor_check_op(struct spi_nor *nor,
2680                             struct spi_mem_op *op)
2681 {
2682         /*
2683          * First test with 4 address bytes. The opcode itself might be a 3B
2684          * addressing opcode but we don't care, because SPI controller
2685          * implementation should not check the opcode, but just the sequence.
2686          */
2687         op->addr.nbytes = 4;
2688         if (!spi_mem_supports_op(nor->spi, op)) {
2689                 if (nor->mtd.size > SZ_16M)
2690                         return -ENOTSUPP;
2691
2692                 /* If flash size <= 16MB, 3 address bytes are sufficient */
2693                 op->addr.nbytes = 3;
2694                 if (!spi_mem_supports_op(nor->spi, op))
2695                         return -ENOTSUPP;
2696         }
2697
2698         return 0;
2699 }
2700
2701 /**
2702  * spi_nor_check_readop - check if the read op is supported by controller
2703  * @nor:         pointer to a 'struct spi_nor'
2704  * @read:        pointer to op template to be checked
2705  *
2706  * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2707  */
2708 static int spi_nor_check_readop(struct spi_nor *nor,
2709                                 const struct spi_nor_read_command *read)
2710 {
2711         struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(read->opcode, 0),
2712                                           SPI_MEM_OP_ADDR(3, 0, 0),
2713                                           SPI_MEM_OP_DUMMY(1, 0),
2714                                           SPI_MEM_OP_DATA_IN(2, NULL, 0));
2715
2716         spi_nor_setup_op(nor, &op, read->proto);
2717
2718         op.dummy.nbytes = (read->num_mode_clocks + read->num_wait_states) *
2719                           op.dummy.buswidth / 8;
2720         if (spi_nor_protocol_is_dtr(nor->read_proto))
2721                 op.dummy.nbytes *= 2;
2722
2723         return spi_nor_check_op(nor, &op);
2724 }
2725
2726 /**
2727  * spi_nor_check_pp - check if the page program op is supported by controller
2728  * @nor:         pointer to a 'struct spi_nor'
2729  * @pp:          pointer to op template to be checked
2730  *
2731  * Returns 0 if operation is supported, -ENOTSUPP otherwise.
2732  */
2733 static int spi_nor_check_pp(struct spi_nor *nor,
2734                             const struct spi_nor_pp_command *pp)
2735 {
2736         struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(pp->opcode, 0),
2737                                           SPI_MEM_OP_ADDR(3, 0, 0),
2738                                           SPI_MEM_OP_NO_DUMMY,
2739                                           SPI_MEM_OP_DATA_OUT(2, NULL, 0));
2740
2741         spi_nor_setup_op(nor, &op, pp->proto);
2742
2743         return spi_nor_check_op(nor, &op);
2744 }
2745
2746 /**
2747  * spi_nor_adjust_hwcaps - Find optimal Read/Write protocol based on SPI
2748  *                         controller capabilities
2749  * @nor:        pointer to a 'struct spi_nor'
2750  * @params:     pointer to the 'struct spi_nor_flash_parameter'
2751  *              representing SPI NOR flash capabilities
2752  * @hwcaps:     pointer to resulting capabilities after adjusting
2753  *              according to controller and flash's capability
2754  *
2755  * Discard caps based on what the SPI controller actually supports (using
2756  * spi_mem_supports_op()).
2757  */
2758 static void
2759 spi_nor_adjust_hwcaps(struct spi_nor *nor,
2760                       const struct spi_nor_flash_parameter *params,
2761                       u32 *hwcaps)
2762 {
2763         unsigned int cap;
2764
2765         /*
2766          * Enable all caps by default. We will mask them after checking what's
2767          * really supported using spi_mem_supports_op().
2768          */
2769         *hwcaps = SNOR_HWCAPS_ALL;
2770
2771         /* X-X-X modes are not supported yet, mask them all. */
2772         *hwcaps &= ~SNOR_HWCAPS_X_X_X;
2773
2774         /*
2775          * If the reset line is broken, we do not want to enter a stateful
2776          * mode.
2777          */
2778         if (nor->flags & SNOR_F_BROKEN_RESET)
2779                 *hwcaps &= ~(SNOR_HWCAPS_X_X_X | SNOR_HWCAPS_X_X_X_DTR);
2780
2781         for (cap = 0; cap < sizeof(*hwcaps) * BITS_PER_BYTE; cap++) {
2782                 int rdidx, ppidx;
2783
2784                 if (!(*hwcaps & BIT(cap)))
2785                         continue;
2786
2787                 rdidx = spi_nor_hwcaps_read2cmd(BIT(cap));
2788                 if (rdidx >= 0 &&
2789                     spi_nor_check_readop(nor, &params->reads[rdidx]))
2790                         *hwcaps &= ~BIT(cap);
2791
2792                 ppidx = spi_nor_hwcaps_pp2cmd(BIT(cap));
2793                 if (ppidx < 0)
2794                         continue;
2795
2796                 if (spi_nor_check_pp(nor, &params->page_programs[ppidx]))
2797                         *hwcaps &= ~BIT(cap);
2798         }
2799 }
2800 #else
2801 /**
2802  * spi_nor_adjust_hwcaps - Find optimal Read/Write protocol based on SPI
2803  *                         controller capabilities
2804  * @nor:        pointer to a 'struct spi_nor'
2805  * @params:     pointer to the 'struct spi_nor_flash_parameter'
2806  *              representing SPI NOR flash capabilities
2807  * @hwcaps:     pointer to resulting capabilities after adjusting
2808  *              according to controller and flash's capability
2809  *
2810  * Select caps based on what the SPI controller and SPI flash both support.
2811  */
2812 static void
2813 spi_nor_adjust_hwcaps(struct spi_nor *nor,
2814                       const struct spi_nor_flash_parameter *params,
2815                       u32 *hwcaps)
2816 {
2817         struct spi_slave *spi = nor->spi;
2818         u32 ignored_mask = (SNOR_HWCAPS_READ_2_2_2 |
2819                             SNOR_HWCAPS_READ_4_4_4 |
2820                             SNOR_HWCAPS_READ_8_8_8 |
2821                             SNOR_HWCAPS_PP_4_4_4   |
2822                             SNOR_HWCAPS_PP_8_8_8);
2823         u32 spi_hwcaps = (SNOR_HWCAPS_READ | SNOR_HWCAPS_READ_FAST |
2824                           SNOR_HWCAPS_PP);
2825
2826         /* Get the hardware capabilities the SPI controller supports. */
2827         if (spi->mode & SPI_RX_OCTAL) {
2828                 spi_hwcaps |= SNOR_HWCAPS_READ_1_1_8;
2829
2830                 if (spi->mode & SPI_TX_OCTAL)
2831                         spi_hwcaps |= (SNOR_HWCAPS_READ_1_8_8 |
2832                                         SNOR_HWCAPS_PP_1_1_8 |
2833                                         SNOR_HWCAPS_PP_1_8_8);
2834         } else if (spi->mode & SPI_RX_QUAD) {
2835                 spi_hwcaps |= SNOR_HWCAPS_READ_1_1_4;
2836
2837                 if (spi->mode & SPI_TX_QUAD)
2838                         spi_hwcaps |= (SNOR_HWCAPS_READ_1_4_4 |
2839                                         SNOR_HWCAPS_PP_1_1_4 |
2840                                         SNOR_HWCAPS_PP_1_4_4);
2841         } else if (spi->mode & SPI_RX_DUAL) {
2842                 spi_hwcaps |= SNOR_HWCAPS_READ_1_1_2;
2843
2844                 if (spi->mode & SPI_TX_DUAL)
2845                         spi_hwcaps |= SNOR_HWCAPS_READ_1_2_2;
2846         }
2847
2848         /*
2849          * Keep only the hardware capabilities supported by both the SPI
2850          * controller and the SPI flash memory.
2851          */
2852         *hwcaps = spi_hwcaps & params->hwcaps.mask;
2853         if (*hwcaps & ignored_mask) {
2854                 dev_dbg(nor->dev,
2855                         "SPI n-n-n protocols are not supported yet.\n");
2856                 *hwcaps &= ~ignored_mask;
2857         }
2858 }
2859 #endif /* CONFIG_SPI_FLASH_SMART_HWCAPS */
2860
2861 static int spi_nor_select_read(struct spi_nor *nor,
2862                                const struct spi_nor_flash_parameter *params,
2863                                u32 shared_hwcaps)
2864 {
2865         int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_READ_MASK) - 1;
2866         const struct spi_nor_read_command *read;
2867
2868         if (best_match < 0)
2869                 return -EINVAL;
2870
2871         cmd = spi_nor_hwcaps_read2cmd(BIT(best_match));
2872         if (cmd < 0)
2873                 return -EINVAL;
2874
2875         read = &params->reads[cmd];
2876         nor->read_opcode = read->opcode;
2877         nor->read_proto = read->proto;
2878
2879         /*
2880          * In the spi-nor framework, we don't need to make the difference
2881          * between mode clock cycles and wait state clock cycles.
2882          * Indeed, the value of the mode clock cycles is used by a QSPI
2883          * flash memory to know whether it should enter or leave its 0-4-4
2884          * (Continuous Read / XIP) mode.
2885          * eXecution In Place is out of the scope of the mtd sub-system.
2886          * Hence we choose to merge both mode and wait state clock cycles
2887          * into the so called dummy clock cycles.
2888          */
2889         nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
2890         return 0;
2891 }
2892
2893 static int spi_nor_select_pp(struct spi_nor *nor,
2894                              const struct spi_nor_flash_parameter *params,
2895                              u32 shared_hwcaps)
2896 {
2897         int cmd, best_match = fls(shared_hwcaps & SNOR_HWCAPS_PP_MASK) - 1;
2898         const struct spi_nor_pp_command *pp;
2899
2900         if (best_match < 0)
2901                 return -EINVAL;
2902
2903         cmd = spi_nor_hwcaps_pp2cmd(BIT(best_match));
2904         if (cmd < 0)
2905                 return -EINVAL;
2906
2907         pp = &params->page_programs[cmd];
2908         nor->program_opcode = pp->opcode;
2909         nor->write_proto = pp->proto;
2910         return 0;
2911 }
2912
2913 static int spi_nor_select_erase(struct spi_nor *nor,
2914                                 const struct flash_info *info)
2915 {
2916         struct mtd_info *mtd = &nor->mtd;
2917
2918         /* Do nothing if already configured from SFDP. */
2919         if (mtd->erasesize)
2920                 return 0;
2921
2922 #ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
2923         /* prefer "small sector" erase if possible */
2924         if (info->flags & SECT_4K) {
2925                 nor->erase_opcode = SPINOR_OP_BE_4K;
2926                 mtd->erasesize = 4096;
2927         } else if (info->flags & SECT_4K_PMC) {
2928                 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
2929                 mtd->erasesize = 4096;
2930         } else
2931 #endif
2932         {
2933                 nor->erase_opcode = SPINOR_OP_SE;
2934                 mtd->erasesize = info->sector_size;
2935         }
2936         return 0;
2937 }
2938
2939 static int spi_nor_default_setup(struct spi_nor *nor,
2940                                  const struct flash_info *info,
2941                                  const struct spi_nor_flash_parameter *params)
2942 {
2943         u32 shared_mask;
2944         bool enable_quad_io;
2945         int err;
2946
2947         spi_nor_adjust_hwcaps(nor, params, &shared_mask);
2948
2949         /* Select the (Fast) Read command. */
2950         err = spi_nor_select_read(nor, params, shared_mask);
2951         if (err) {
2952                 dev_dbg(nor->dev,
2953                         "can't select read settings supported by both the SPI controller and memory.\n");
2954                 return err;
2955         }
2956
2957         /* Select the Page Program command. */
2958         err = spi_nor_select_pp(nor, params, shared_mask);
2959         if (err) {
2960                 dev_dbg(nor->dev,
2961                         "can't select write settings supported by both the SPI controller and memory.\n");
2962                 return err;
2963         }
2964
2965         /* Select the Sector Erase command. */
2966         err = spi_nor_select_erase(nor, info);
2967         if (err) {
2968                 dev_dbg(nor->dev,
2969                         "can't select erase settings supported by both the SPI controller and memory.\n");
2970                 return err;
2971         }
2972
2973         /* Enable Quad I/O if needed. */
2974         enable_quad_io = (spi_nor_get_protocol_width(nor->read_proto) == 4 ||
2975                           spi_nor_get_protocol_width(nor->write_proto) == 4);
2976         if (enable_quad_io && params->quad_enable)
2977                 nor->quad_enable = params->quad_enable;
2978         else
2979                 nor->quad_enable = NULL;
2980
2981         return 0;
2982 }
2983
2984 static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
2985                          const struct spi_nor_flash_parameter *params)
2986 {
2987         if (!nor->setup)
2988                 return 0;
2989
2990         return nor->setup(nor, info, params);
2991 }
2992
2993 #ifdef CONFIG_SPI_FLASH_S28HS512T
2994 /**
2995  * spi_nor_cypress_octal_dtr_enable() - Enable octal DTR on Cypress flashes.
2996  * @nor:                pointer to a 'struct spi_nor'
2997  *
2998  * This also sets the memory access latency cycles to 24 to allow the flash to
2999  * run at up to 200MHz.
3000  *
3001  * Return: 0 on success, -errno otherwise.
3002  */
3003 static int spi_nor_cypress_octal_dtr_enable(struct spi_nor *nor)
3004 {
3005         struct spi_mem_op op;
3006         u8 buf;
3007         u8 addr_width = 3;
3008         int ret;
3009
3010         /* Use 24 dummy cycles for memory array reads. */
3011         ret = write_enable(nor);
3012         if (ret)
3013                 return ret;
3014
3015         buf = SPINOR_REG_CYPRESS_CFR2V_MEMLAT_11_24;
3016         op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WR_ANY_REG, 1),
3017                         SPI_MEM_OP_ADDR(addr_width, SPINOR_REG_CYPRESS_CFR2V, 1),
3018                         SPI_MEM_OP_NO_DUMMY,
3019                         SPI_MEM_OP_DATA_OUT(1, &buf, 1));
3020         ret = spi_mem_exec_op(nor->spi, &op);
3021         if (ret) {
3022                 dev_warn(nor->dev,
3023                          "failed to set default memory latency value: %d\n",
3024                          ret);
3025                 return ret;
3026         }
3027         ret = spi_nor_wait_till_ready(nor);
3028         if (ret)
3029                 return ret;
3030
3031         nor->read_dummy = 24;
3032
3033         /* Set the octal and DTR enable bits. */
3034         ret = write_enable(nor);
3035         if (ret)
3036                 return ret;
3037
3038         buf = SPINOR_REG_CYPRESS_CFR5V_OCT_DTR_EN;
3039         op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WR_ANY_REG, 1),
3040                         SPI_MEM_OP_ADDR(addr_width, SPINOR_REG_CYPRESS_CFR5V, 1),
3041                         SPI_MEM_OP_NO_DUMMY,
3042                         SPI_MEM_OP_DATA_OUT(1, &buf, 1));
3043         ret = spi_mem_exec_op(nor->spi, &op);
3044         if (ret) {
3045                 dev_warn(nor->dev, "Failed to enable octal DTR mode\n");
3046                 return ret;
3047         }
3048
3049         return 0;
3050 }
3051
3052 static int s28hs512t_erase_non_uniform(struct spi_nor *nor, loff_t addr)
3053 {
3054         /* Factory default configuration: 32 x 4 KiB sectors at bottom. */
3055         return spansion_erase_non_uniform(nor, addr, SPINOR_OP_S28_SE_4K,
3056                                           0, SZ_128K);
3057 }
3058
3059 static int s28hs512t_setup(struct spi_nor *nor, const struct flash_info *info,
3060                            const struct spi_nor_flash_parameter *params)
3061 {
3062         struct spi_mem_op op;
3063         u8 buf;
3064         u8 addr_width = 3;
3065         int ret;
3066
3067         ret = spi_nor_wait_till_ready(nor);
3068         if (ret)
3069                 return ret;
3070
3071         /*
3072          * Check CFR3V to check if non-uniform sector mode is selected. If it
3073          * is, set the erase hook to the non-uniform erase procedure.
3074          */
3075         op = (struct spi_mem_op)
3076                 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RD_ANY_REG, 1),
3077                            SPI_MEM_OP_ADDR(addr_width,
3078                                            SPINOR_REG_CYPRESS_CFR3V, 1),
3079                            SPI_MEM_OP_NO_DUMMY,
3080                            SPI_MEM_OP_DATA_IN(1, &buf, 1));
3081
3082         ret = spi_mem_exec_op(nor->spi, &op);
3083         if (ret)
3084                 return ret;
3085
3086         if (!(buf & SPINOR_REG_CYPRESS_CFR3V_UNISECT))
3087                 nor->erase = s28hs512t_erase_non_uniform;
3088
3089         return spi_nor_default_setup(nor, info, params);
3090 }
3091
3092 static void s28hs512t_default_init(struct spi_nor *nor)
3093 {
3094         nor->octal_dtr_enable = spi_nor_cypress_octal_dtr_enable;
3095         nor->setup = s28hs512t_setup;
3096 }
3097
3098 static void s28hs512t_post_sfdp_fixup(struct spi_nor *nor,
3099                                       struct spi_nor_flash_parameter *params)
3100 {
3101         /*
3102          * On older versions of the flash the xSPI Profile 1.0 table has the
3103          * 8D-8D-8D Fast Read opcode as 0x00. But it actually should be 0xEE.
3104          */
3105         if (params->reads[SNOR_CMD_READ_8_8_8_DTR].opcode == 0)
3106                 params->reads[SNOR_CMD_READ_8_8_8_DTR].opcode =
3107                         SPINOR_OP_CYPRESS_RD_FAST;
3108
3109         params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR;
3110
3111         /* This flash is also missing the 4-byte Page Program opcode bit. */
3112         spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP],
3113                                 SPINOR_OP_PP_4B, SNOR_PROTO_1_1_1);
3114         /*
3115          * Since xSPI Page Program opcode is backward compatible with
3116          * Legacy SPI, use Legacy SPI opcode there as well.
3117          */
3118         spi_nor_set_pp_settings(&params->page_programs[SNOR_CMD_PP_8_8_8_DTR],
3119                                 SPINOR_OP_PP_4B, SNOR_PROTO_8_8_8_DTR);
3120
3121         /*
3122          * The xSPI Profile 1.0 table advertises the number of additional
3123          * address bytes needed for Read Status Register command as 0 but the
3124          * actual value for that is 4.
3125          */
3126         params->rdsr_addr_nbytes = 4;
3127 }
3128
3129 static int s28hs512t_post_bfpt_fixup(struct spi_nor *nor,
3130                                      const struct sfdp_parameter_header *bfpt_header,
3131                                      const struct sfdp_bfpt *bfpt,
3132                                      struct spi_nor_flash_parameter *params)
3133 {
3134         struct spi_mem_op op;
3135         u8 buf;
3136         u8 addr_width = 3;
3137         int ret;
3138
3139         /*
3140          * The BFPT table advertises a 512B page size but the page size is
3141          * actually configurable (with the default being 256B). Read from
3142          * CFR3V[4] and set the correct size.
3143          */
3144         op = (struct spi_mem_op)
3145                 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RD_ANY_REG, 1),
3146                            SPI_MEM_OP_ADDR(addr_width, SPINOR_REG_CYPRESS_CFR3V, 1),
3147                            SPI_MEM_OP_NO_DUMMY,
3148                            SPI_MEM_OP_DATA_IN(1, &buf, 1));
3149         ret = spi_mem_exec_op(nor->spi, &op);
3150         if (ret)
3151                 return ret;
3152
3153         if (buf & SPINOR_REG_CYPRESS_CFR3V_PGSZ)
3154                 params->page_size = 512;
3155         else
3156                 params->page_size = 256;
3157
3158         /*
3159          * The BFPT advertises that it supports 4k erases, and the datasheet
3160          * says the same. But 4k erases did not work when testing. So, use 256k
3161          * erases for now.
3162          */
3163         nor->erase_opcode = SPINOR_OP_SE_4B;
3164         nor->mtd.erasesize = 0x40000;
3165
3166         return 0;
3167 }
3168
3169 static struct spi_nor_fixups s28hs512t_fixups = {
3170         .default_init = s28hs512t_default_init,
3171         .post_sfdp = s28hs512t_post_sfdp_fixup,
3172         .post_bfpt = s28hs512t_post_bfpt_fixup,
3173 };
3174 #endif /* CONFIG_SPI_FLASH_S28HS512T */
3175
3176 #ifdef CONFIG_SPI_FLASH_MT35XU
3177 static int spi_nor_micron_octal_dtr_enable(struct spi_nor *nor)
3178 {
3179         struct spi_mem_op op;
3180         u8 buf;
3181         u8 addr_width = 3;
3182         int ret;
3183
3184         /* Set dummy cycles for Fast Read to the default of 20. */
3185         ret = write_enable(nor);
3186         if (ret)
3187                 return ret;
3188
3189         buf = 20;
3190         op = (struct spi_mem_op)
3191                 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_MT_WR_ANY_REG, 1),
3192                            SPI_MEM_OP_ADDR(addr_width, SPINOR_REG_MT_CFR1V, 1),
3193                            SPI_MEM_OP_NO_DUMMY,
3194                            SPI_MEM_OP_DATA_OUT(1, &buf, 1));
3195         ret = spi_mem_exec_op(nor->spi, &op);
3196         if (ret)
3197                 return ret;
3198
3199         ret = spi_nor_wait_till_ready(nor);
3200         if (ret)
3201                 return ret;
3202
3203         nor->read_dummy = 20;
3204
3205         ret = write_enable(nor);
3206         if (ret)
3207                 return ret;
3208
3209         buf = SPINOR_MT_OCT_DTR;
3210         op = (struct spi_mem_op)
3211                 SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_MT_WR_ANY_REG, 1),
3212                            SPI_MEM_OP_ADDR(addr_width, SPINOR_REG_MT_CFR0V, 1),
3213                            SPI_MEM_OP_NO_DUMMY,
3214                            SPI_MEM_OP_DATA_OUT(1, &buf, 1));
3215         ret = spi_mem_exec_op(nor->spi, &op);
3216         if (ret) {
3217                 dev_err(nor->dev, "Failed to enable octal DTR mode\n");
3218                 return ret;
3219         }
3220
3221         return 0;
3222 }
3223
3224 static void mt35xu512aba_default_init(struct spi_nor *nor)
3225 {
3226         nor->octal_dtr_enable = spi_nor_micron_octal_dtr_enable;
3227 }
3228
3229 static void mt35xu512aba_post_sfdp_fixup(struct spi_nor *nor,
3230                                          struct spi_nor_flash_parameter *params)
3231 {
3232         /* Set the Fast Read settings. */
3233         params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR;
3234         spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_8_8_8_DTR],
3235                                   0, 20, SPINOR_OP_MT_DTR_RD,
3236                                   SNOR_PROTO_8_8_8_DTR);
3237
3238         params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR;
3239
3240         nor->cmd_ext_type = SPI_NOR_EXT_REPEAT;
3241         params->rdsr_dummy = 8;
3242         params->rdsr_addr_nbytes = 0;
3243
3244         /*
3245          * The BFPT quad enable field is set to a reserved value so the quad
3246          * enable function is ignored by spi_nor_parse_bfpt(). Make sure we
3247          * disable it.
3248          */
3249         params->quad_enable = NULL;
3250 }
3251
3252 static struct spi_nor_fixups mt35xu512aba_fixups = {
3253         .default_init = mt35xu512aba_default_init,
3254         .post_sfdp = mt35xu512aba_post_sfdp_fixup,
3255 };
3256 #endif /* CONFIG_SPI_FLASH_MT35XU */
3257
3258 /** spi_nor_octal_dtr_enable() - enable Octal DTR I/O if needed
3259  * @nor:                 pointer to a 'struct spi_nor'
3260  *
3261  * Return: 0 on success, -errno otherwise.
3262  */
3263 static int spi_nor_octal_dtr_enable(struct spi_nor *nor)
3264 {
3265         int ret;
3266
3267         if (!nor->octal_dtr_enable)
3268                 return 0;
3269
3270         if (!(nor->read_proto == SNOR_PROTO_8_8_8_DTR &&
3271               nor->write_proto == SNOR_PROTO_8_8_8_DTR))
3272                 return 0;
3273
3274         ret = nor->octal_dtr_enable(nor);
3275         if (ret)
3276                 return ret;
3277
3278         nor->reg_proto = SNOR_PROTO_8_8_8_DTR;
3279
3280         return 0;
3281 }
3282
3283 static int spi_nor_init(struct spi_nor *nor)
3284 {
3285         int err;
3286
3287         err = spi_nor_octal_dtr_enable(nor);
3288         if (err) {
3289                 dev_dbg(nor->dev, "Octal DTR mode not supported\n");
3290                 return err;
3291         }
3292
3293         /*
3294          * Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up
3295          * with the software protection bits set
3296          */
3297         if (IS_ENABLED(CONFIG_SPI_FLASH_UNLOCK_ALL) &&
3298             (JEDEC_MFR(nor->info) == SNOR_MFR_ATMEL ||
3299              JEDEC_MFR(nor->info) == SNOR_MFR_INTEL ||
3300              JEDEC_MFR(nor->info) == SNOR_MFR_SST ||
3301              nor->info->flags & SPI_NOR_HAS_LOCK)) {
3302                 write_enable(nor);
3303                 write_sr(nor, 0);
3304                 spi_nor_wait_till_ready(nor);
3305         }
3306
3307         if (nor->quad_enable) {
3308                 err = nor->quad_enable(nor);
3309                 if (err) {
3310                         dev_dbg(nor->dev, "quad mode not supported\n");
3311                         return err;
3312                 }
3313         }
3314
3315         if (nor->addr_width == 4 &&
3316             !(nor->info->flags & SPI_NOR_OCTAL_DTR_READ) &&
3317             (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
3318             !(nor->info->flags & SPI_NOR_4B_OPCODES)) {
3319                 /*
3320                  * If the RESET# pin isn't hooked up properly, or the system
3321                  * otherwise doesn't perform a reset command in the boot
3322                  * sequence, it's impossible to 100% protect against unexpected
3323                  * reboots (e.g., crashes). Warn the user (or hopefully, system
3324                  * designer) that this is bad.
3325                  */
3326                 if (nor->flags & SNOR_F_BROKEN_RESET)
3327                         debug("enabling reset hack; may not recover from unexpected reboots\n");
3328                 set_4byte(nor, nor->info, 1);
3329         }
3330
3331         return 0;
3332 }
3333
3334 #ifdef CONFIG_SPI_FLASH_SOFT_RESET
3335 /**
3336  * spi_nor_soft_reset() - perform the JEDEC Software Reset sequence
3337  * @nor:        the spi_nor structure
3338  *
3339  * This function can be used to switch from Octal DTR mode to legacy mode on a
3340  * flash that supports it. The soft reset is executed in Octal DTR mode.
3341  *
3342  * Return: 0 for success, -errno for failure.
3343  */
3344 static int spi_nor_soft_reset(struct spi_nor *nor)
3345 {
3346         struct spi_mem_op op;
3347         int ret;
3348         enum spi_nor_cmd_ext ext;
3349
3350         ext = nor->cmd_ext_type;
3351         nor->cmd_ext_type = SPI_NOR_EXT_REPEAT;
3352
3353         op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_SRSTEN, 0),
3354                         SPI_MEM_OP_NO_DUMMY,
3355                         SPI_MEM_OP_NO_ADDR,
3356                         SPI_MEM_OP_NO_DATA);
3357         spi_nor_setup_op(nor, &op, SNOR_PROTO_8_8_8_DTR);
3358         ret = spi_mem_exec_op(nor->spi, &op);
3359         if (ret) {
3360                 dev_warn(nor->dev, "Software reset enable failed: %d\n", ret);
3361                 goto out;
3362         }
3363
3364         op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_SRST, 0),
3365                         SPI_MEM_OP_NO_DUMMY,
3366                         SPI_MEM_OP_NO_ADDR,
3367                         SPI_MEM_OP_NO_DATA);
3368         spi_nor_setup_op(nor, &op, SNOR_PROTO_8_8_8_DTR);
3369         ret = spi_mem_exec_op(nor->spi, &op);
3370         if (ret) {
3371                 dev_warn(nor->dev, "Software reset failed: %d\n", ret);
3372                 goto out;
3373         }
3374
3375         /*
3376          * Software Reset is not instant, and the delay varies from flash to
3377          * flash. Looking at a few flashes, most range somewhere below 100
3378          * microseconds. So, wait for 200ms just to be sure.
3379          */
3380         udelay(SPI_NOR_SRST_SLEEP_LEN);
3381
3382 out:
3383         nor->cmd_ext_type = ext;
3384         return ret;
3385 }
3386 #endif /* CONFIG_SPI_FLASH_SOFT_RESET */
3387
3388 int spi_nor_remove(struct spi_nor *nor)
3389 {
3390 #ifdef CONFIG_SPI_FLASH_SOFT_RESET
3391         if (nor->info->flags & SPI_NOR_OCTAL_DTR_READ &&
3392             nor->flags & SNOR_F_SOFT_RESET)
3393                 return spi_nor_soft_reset(nor);
3394 #endif
3395
3396         return 0;
3397 }
3398
3399 void spi_nor_set_fixups(struct spi_nor *nor)
3400 {
3401 #ifdef CONFIG_SPI_FLASH_S28HS512T
3402         if (!strcmp(nor->info->name, "s28hs512t"))
3403                 nor->fixups = &s28hs512t_fixups;
3404 #endif
3405
3406 #ifdef CONFIG_SPI_FLASH_MT35XU
3407         if (!strcmp(nor->info->name, "mt35xu512aba"))
3408                 nor->fixups = &mt35xu512aba_fixups;
3409 #endif
3410 }
3411
3412 int spi_nor_scan(struct spi_nor *nor)
3413 {
3414         struct spi_nor_flash_parameter params;
3415         const struct flash_info *info = NULL;
3416         struct mtd_info *mtd = &nor->mtd;
3417         struct spi_slave *spi = nor->spi;
3418         int ret;
3419
3420         /* Reset SPI protocol for all commands. */
3421         nor->reg_proto = SNOR_PROTO_1_1_1;
3422         nor->read_proto = SNOR_PROTO_1_1_1;
3423         nor->write_proto = SNOR_PROTO_1_1_1;
3424         nor->read = spi_nor_read_data;
3425         nor->write = spi_nor_write_data;
3426         nor->read_reg = spi_nor_read_reg;
3427         nor->write_reg = spi_nor_write_reg;
3428
3429         nor->setup = spi_nor_default_setup;
3430
3431 #ifdef CONFIG_SPI_FLASH_SOFT_RESET_ON_BOOT
3432         /*
3433          * When the flash is handed to us in a stateful mode like 8D-8D-8D, it
3434          * is difficult to detect the mode the flash is in. One option is to
3435          * read SFDP in all modes and see which one gives the correct "SFDP"
3436          * signature, but not all flashes support SFDP in 8D-8D-8D mode.
3437          *
3438          * Further, even if you detect the mode of the flash via SFDP, you
3439          * still have the problem of actually reading the ID. The Read ID
3440          * command is not standardized across flash vendors. Flashes can have
3441          * different dummy cycles needed for reading the ID. Some flashes even
3442          * expect a 4-byte dummy address with the Read ID command. All this
3443          * information cannot be obtained from the SFDP table.
3444          *
3445          * So, perform a Software Reset sequence before reading the ID and
3446          * initializing the flash. A Soft Reset will bring back the flash in
3447          * its default protocol mode assuming no non-volatile configuration was
3448          * set. This will let us detect the flash even if ROM hands it to us in
3449          * Octal DTR mode.
3450          *
3451          * To accommodate cases where there is more than one flash on a board,
3452          * and only one of them needs a soft reset, failure to reset is not
3453          * made fatal, and we still try to read ID if possible.
3454          */
3455         spi_nor_soft_reset(nor);
3456 #endif /* CONFIG_SPI_FLASH_SOFT_RESET_ON_BOOT */
3457
3458         info = spi_nor_read_id(nor);
3459         if (IS_ERR_OR_NULL(info))
3460                 return -ENOENT;
3461         nor->info = info;
3462
3463         spi_nor_set_fixups(nor);
3464
3465         /* Parse the Serial Flash Discoverable Parameters table. */
3466         ret = spi_nor_init_params(nor, info, &params);
3467         if (ret)
3468                 return ret;
3469
3470         if (!mtd->name)
3471                 mtd->name = info->name;
3472         mtd->dev = nor->dev;
3473         mtd->priv = nor;
3474         mtd->type = MTD_NORFLASH;
3475         mtd->writesize = 1;
3476         mtd->flags = MTD_CAP_NORFLASH;
3477         mtd->size = params.size;
3478         mtd->_erase = spi_nor_erase;
3479         mtd->_read = spi_nor_read;
3480         mtd->_write = spi_nor_write;
3481
3482 #if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
3483         /* NOR protection support for STmicro/Micron chips and similar */
3484         if (JEDEC_MFR(info) == SNOR_MFR_ST ||
3485             JEDEC_MFR(info) == SNOR_MFR_MICRON ||
3486             JEDEC_MFR(info) == SNOR_MFR_SST ||
3487                         info->flags & SPI_NOR_HAS_LOCK) {
3488                 nor->flash_lock = stm_lock;
3489                 nor->flash_unlock = stm_unlock;
3490                 nor->flash_is_locked = stm_is_locked;
3491         }
3492 #endif
3493
3494 #ifdef CONFIG_SPI_FLASH_SST
3495         /*
3496          * sst26 series block protection implementation differs from other
3497          * series.
3498          */
3499         if (info->flags & SPI_NOR_HAS_SST26LOCK) {
3500                 nor->flash_lock = sst26_lock;
3501                 nor->flash_unlock = sst26_unlock;
3502                 nor->flash_is_locked = sst26_is_locked;
3503         }
3504 #endif
3505
3506         if (info->flags & USE_FSR)
3507                 nor->flags |= SNOR_F_USE_FSR;
3508         if (info->flags & SPI_NOR_HAS_TB)
3509                 nor->flags |= SNOR_F_HAS_SR_TB;
3510         if (info->flags & NO_CHIP_ERASE)
3511                 nor->flags |= SNOR_F_NO_OP_CHIP_ERASE;
3512         if (info->flags & USE_CLSR)
3513                 nor->flags |= SNOR_F_USE_CLSR;
3514
3515         if (info->flags & SPI_NOR_NO_ERASE)
3516                 mtd->flags |= MTD_NO_ERASE;
3517
3518         nor->page_size = params.page_size;
3519         mtd->writebufsize = nor->page_size;
3520
3521         /* Some devices cannot do fast-read, no matter what DT tells us */
3522         if ((info->flags & SPI_NOR_NO_FR) || (spi->mode & SPI_RX_SLOW))
3523                 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
3524
3525         /*
3526          * Configure the SPI memory:
3527          * - select op codes for (Fast) Read, Page Program and Sector Erase.
3528          * - set the number of dummy cycles (mode cycles + wait states).
3529          * - set the SPI protocols for register and memory accesses.
3530          * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
3531          */
3532         ret = spi_nor_setup(nor, info, &params);
3533         if (ret)
3534                 return ret;
3535
3536         if (spi_nor_protocol_is_dtr(nor->read_proto)) {
3537                  /* Always use 4-byte addresses in DTR mode. */
3538                 nor->addr_width = 4;
3539         } else if (nor->addr_width) {
3540                 /* already configured from SFDP */
3541         } else if (info->addr_width) {
3542                 nor->addr_width = info->addr_width;
3543         } else {
3544                 nor->addr_width = 3;
3545         }
3546
3547         if (nor->addr_width == 3 && mtd->size > SZ_16M) {
3548 #ifndef CONFIG_SPI_FLASH_BAR
3549                 /* enable 4-byte addressing if the device exceeds 16MiB */
3550                 nor->addr_width = 4;
3551                 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
3552                     info->flags & SPI_NOR_4B_OPCODES)
3553                         spi_nor_set_4byte_opcodes(nor, info);
3554 #else
3555         /* Configure the BAR - discover bank cmds and read current bank */
3556         nor->addr_width = 3;
3557         ret = read_bar(nor, info);
3558         if (ret < 0)
3559                 return ret;
3560 #endif
3561         }
3562
3563         if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
3564                 dev_dbg(nor->dev, "address width is too large: %u\n",
3565                         nor->addr_width);
3566                 return -EINVAL;
3567         }
3568
3569         /* Send all the required SPI flash commands to initialize device */
3570         ret = spi_nor_init(nor);
3571         if (ret)
3572                 return ret;
3573
3574         nor->rdsr_dummy = params.rdsr_dummy;
3575         nor->rdsr_addr_nbytes = params.rdsr_addr_nbytes;
3576         nor->name = mtd->name;
3577         nor->size = mtd->size;
3578         nor->erase_size = mtd->erasesize;
3579         nor->sector_size = mtd->erasesize;
3580
3581 #ifndef CONFIG_SPL_BUILD
3582         printf("SF: Detected %s with page size ", nor->name);
3583         print_size(nor->page_size, ", erase size ");
3584         print_size(nor->erase_size, ", total ");
3585         print_size(nor->size, "");
3586         puts("\n");
3587 #endif
3588
3589         return 0;
3590 }
3591
3592 /* U-Boot specific functions, need to extend MTD to support these */
3593 int spi_flash_cmd_get_sw_write_prot(struct spi_nor *nor)
3594 {
3595         int sr = read_sr(nor);
3596
3597         if (sr < 0)
3598                 return sr;
3599
3600         return (sr >> 2) & 7;
3601 }