common: Drop log.h from common header
[pandora-u-boot.git] / drivers / mtd / spi / spi-nor-tiny.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 <dm/device_compat.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
17 #include <linux/log2.h>
18 #include <linux/math64.h>
19 #include <linux/sizes.h>
20
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/spi-nor.h>
23 #include <spi-mem.h>
24 #include <spi.h>
25
26 #include "sf_internal.h"
27
28 /* Define max times to check status register before we give up. */
29
30 /*
31  * For everything but full-chip erase; probably could be much smaller, but kept
32  * around for safety for now
33  */
34
35 #define HZ                                      CONFIG_SYS_HZ
36
37 #define DEFAULT_READY_WAIT_JIFFIES              (40UL * HZ)
38
39 static int spi_nor_read_write_reg(struct spi_nor *nor, struct spi_mem_op
40                 *op, void *buf)
41 {
42         if (op->data.dir == SPI_MEM_DATA_IN)
43                 op->data.buf.in = buf;
44         else
45                 op->data.buf.out = buf;
46         return spi_mem_exec_op(nor->spi, op);
47 }
48
49 static int spi_nor_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
50 {
51         struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1),
52                                           SPI_MEM_OP_NO_ADDR,
53                                           SPI_MEM_OP_NO_DUMMY,
54                                           SPI_MEM_OP_DATA_IN(len, NULL, 1));
55         int ret;
56
57         ret = spi_nor_read_write_reg(nor, &op, val);
58         if (ret < 0)
59                 dev_dbg(&flash->spimem->spi->dev, "error %d reading %x\n", ret,
60                         code);
61
62         return ret;
63 }
64
65 static int spi_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
66 {
67         struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1),
68                                           SPI_MEM_OP_NO_ADDR,
69                                           SPI_MEM_OP_NO_DUMMY,
70                                           SPI_MEM_OP_DATA_OUT(len, NULL, 1));
71
72         return spi_nor_read_write_reg(nor, &op, buf);
73 }
74
75 static ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
76                                  u_char *buf)
77 {
78         struct spi_mem_op op =
79                         SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
80                                    SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
81                                    SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
82                                    SPI_MEM_OP_DATA_IN(len, buf, 1));
83         size_t remaining = len;
84         int ret;
85
86         /* get transfer protocols. */
87         op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
88         op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
89         op.dummy.buswidth = op.addr.buswidth;
90         op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
91
92         /* convert the dummy cycles to the number of bytes */
93         op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
94
95         while (remaining) {
96                 op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
97                 ret = spi_mem_adjust_op_size(nor->spi, &op);
98                 if (ret)
99                         return ret;
100
101                 ret = spi_mem_exec_op(nor->spi, &op);
102                 if (ret)
103                         return ret;
104
105                 op.addr.val += op.data.nbytes;
106                 remaining -= op.data.nbytes;
107                 op.data.buf.in += op.data.nbytes;
108         }
109
110         return len;
111 }
112
113 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
114 /*
115  * Read configuration register, returning its value in the
116  * location. Return the configuration register value.
117  * Returns negative if error occurred.
118  */
119 static int read_cr(struct spi_nor *nor)
120 {
121         int ret;
122         u8 val;
123
124         ret = spi_nor_read_reg(nor, SPINOR_OP_RDCR, &val, 1);
125         if (ret < 0) {
126                 dev_dbg(nor->dev, "error %d reading CR\n", ret);
127                 return ret;
128         }
129
130         return val;
131 }
132 #endif
133
134 /*
135  * Write status register 1 byte
136  * Returns negative if error occurred.
137  */
138 static inline int write_sr(struct spi_nor *nor, u8 val)
139 {
140         nor->cmd_buf[0] = val;
141         return spi_nor_write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
142 }
143
144 /*
145  * Set write enable latch with Write Enable command.
146  * Returns negative if error occurred.
147  */
148 static inline int write_enable(struct spi_nor *nor)
149 {
150         return spi_nor_write_reg(nor, SPINOR_OP_WREN, NULL, 0);
151 }
152
153 /*
154  * Send write disable instruction to the chip.
155  */
156 static inline int write_disable(struct spi_nor *nor)
157 {
158         return spi_nor_write_reg(nor, SPINOR_OP_WRDI, NULL, 0);
159 }
160
161 static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
162 {
163         return mtd->priv;
164 }
165
166 static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
167 {
168         size_t i;
169
170         for (i = 0; i < size; i++)
171                 if (table[i][0] == opcode)
172                         return table[i][1];
173
174         /* No conversion found, keep input op code. */
175         return opcode;
176 }
177
178 static inline u8 spi_nor_convert_3to4_read(u8 opcode)
179 {
180         static const u8 spi_nor_3to4_read[][2] = {
181                 { SPINOR_OP_READ,       SPINOR_OP_READ_4B },
182                 { SPINOR_OP_READ_FAST,  SPINOR_OP_READ_FAST_4B },
183                 { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
184                 { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
185                 { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
186                 { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
187         };
188
189         return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
190                                       ARRAY_SIZE(spi_nor_3to4_read));
191 }
192
193 static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
194                                       const struct flash_info *info)
195 {
196         nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
197 }
198
199 /* Enable/disable 4-byte addressing mode. */
200 static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
201                             int enable)
202 {
203         int status;
204         bool need_wren = false;
205         u8 cmd;
206
207         switch (JEDEC_MFR(info)) {
208         case SNOR_MFR_ST:
209         case SNOR_MFR_MICRON:
210                 /* Some Micron need WREN command; all will accept it */
211                 need_wren = true;
212         case SNOR_MFR_MACRONIX:
213         case SNOR_MFR_WINBOND:
214                 if (need_wren)
215                         write_enable(nor);
216
217                 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
218                 status = spi_nor_write_reg(nor, cmd, NULL, 0);
219                 if (need_wren)
220                         write_disable(nor);
221
222                 if (!status && !enable &&
223                     JEDEC_MFR(info) == SNOR_MFR_WINBOND) {
224                         /*
225                          * On Winbond W25Q256FV, leaving 4byte mode causes
226                          * the Extended Address Register to be set to 1, so all
227                          * 3-byte-address reads come from the second 16M.
228                          * We must clear the register to enable normal behavior.
229                          */
230                         write_enable(nor);
231                         nor->cmd_buf[0] = 0;
232                         spi_nor_write_reg(nor, SPINOR_OP_WREAR,
233                                           nor->cmd_buf, 1);
234                         write_disable(nor);
235                 }
236
237                 return status;
238         default:
239                 /* Spansion style */
240                 nor->cmd_buf[0] = enable << 7;
241                 return spi_nor_write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
242         }
243 }
244
245 #if defined(CONFIG_SPI_FLASH_SPANSION) ||       \
246         defined(CONFIG_SPI_FLASH_WINBOND) ||    \
247         defined(CONFIG_SPI_FLASH_MACRONIX)
248 /*
249  * Read the status register, returning its value in the location
250  * Return the status register value.
251  * Returns negative if error occurred.
252  */
253 static int read_sr(struct spi_nor *nor)
254 {
255         int ret;
256         u8 val;
257
258         ret = spi_nor_read_reg(nor, SPINOR_OP_RDSR, &val, 1);
259         if (ret < 0) {
260                 pr_debug("error %d reading SR\n", (int)ret);
261                 return ret;
262         }
263
264         return val;
265 }
266
267 /*
268  * Read the flag status register, returning its value in the location
269  * Return the status register value.
270  * Returns negative if error occurred.
271  */
272 static int read_fsr(struct spi_nor *nor)
273 {
274         int ret;
275         u8 val;
276
277         ret = spi_nor_read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
278         if (ret < 0) {
279                 pr_debug("error %d reading FSR\n", ret);
280                 return ret;
281         }
282
283         return val;
284 }
285
286 static int spi_nor_sr_ready(struct spi_nor *nor)
287 {
288         int sr = read_sr(nor);
289
290         if (sr < 0)
291                 return sr;
292
293         return !(sr & SR_WIP);
294 }
295
296 static int spi_nor_fsr_ready(struct spi_nor *nor)
297 {
298         int fsr = read_fsr(nor);
299
300         if (fsr < 0)
301                 return fsr;
302         return fsr & FSR_READY;
303 }
304
305 static int spi_nor_ready(struct spi_nor *nor)
306 {
307         int sr, fsr;
308
309         sr = spi_nor_sr_ready(nor);
310         if (sr < 0)
311                 return sr;
312         fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
313         if (fsr < 0)
314                 return fsr;
315         return sr && fsr;
316 }
317
318 /*
319  * Service routine to read status register until ready, or timeout occurs.
320  * Returns non-zero if error.
321  */
322 static int spi_nor_wait_till_ready_with_timeout(struct spi_nor *nor,
323                                                 unsigned long timeout)
324 {
325         unsigned long timebase;
326         int ret;
327
328         timebase = get_timer(0);
329
330         while (get_timer(timebase) < timeout) {
331                 ret = spi_nor_ready(nor);
332                 if (ret < 0)
333                         return ret;
334                 if (ret)
335                         return 0;
336         }
337
338         dev_err(nor->dev, "flash operation timed out\n");
339
340         return -ETIMEDOUT;
341 }
342
343 static int spi_nor_wait_till_ready(struct spi_nor *nor)
344 {
345         return spi_nor_wait_till_ready_with_timeout(nor,
346                                                     DEFAULT_READY_WAIT_JIFFIES);
347 }
348 #endif /* CONFIG_SPI_FLASH_SPANSION */
349
350 /*
351  * Erase an address range on the nor chip.  The address range may extend
352  * one or more erase sectors.  Return an error is there is a problem erasing.
353  */
354 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
355 {
356         return -ENOTSUPP;
357 }
358
359 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
360 {
361         int                     tmp;
362         u8                      id[SPI_NOR_MAX_ID_LEN];
363         const struct flash_info *info;
364
365         tmp = spi_nor_read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
366         if (tmp < 0) {
367                 dev_dbg(nor->dev, "error %d reading JEDEC ID\n", tmp);
368                 return ERR_PTR(tmp);
369         }
370
371         info = spi_nor_ids;
372         for (; info->sector_size != 0; info++) {
373                 if (info->id_len) {
374                         if (!memcmp(info->id, id, info->id_len))
375                                 return info;
376                 }
377         }
378         dev_dbg(nor->dev, "unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
379                 id[0], id[1], id[2]);
380         return ERR_PTR(-ENODEV);
381 }
382
383 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
384                         size_t *retlen, u_char *buf)
385 {
386         struct spi_nor *nor = mtd_to_spi_nor(mtd);
387         int ret;
388
389         dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
390
391         while (len) {
392                 loff_t addr = from;
393
394                 ret = spi_nor_read_data(nor, addr, len, buf);
395                 if (ret == 0) {
396                         /* We shouldn't see 0-length reads */
397                         ret = -EIO;
398                         goto read_err;
399                 }
400                 if (ret < 0)
401                         goto read_err;
402
403                 *retlen += ret;
404                 buf += ret;
405                 from += ret;
406                 len -= ret;
407         }
408         ret = 0;
409
410 read_err:
411         return ret;
412 }
413
414 /*
415  * Write an address range to the nor chip.  Data must be written in
416  * FLASH_PAGESIZE chunks.  The address range may be any size provided
417  * it is within the physical boundaries.
418  */
419 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
420                          size_t *retlen, const u_char *buf)
421 {
422         return -ENOTSUPP;
423 }
424
425 #ifdef CONFIG_SPI_FLASH_MACRONIX
426 /**
427  * macronix_quad_enable() - set QE bit in Status Register.
428  * @nor:        pointer to a 'struct spi_nor'
429  *
430  * Set the Quad Enable (QE) bit in the Status Register.
431  *
432  * bit 6 of the Status Register is the QE bit for Macronix like QSPI memories.
433  *
434  * Return: 0 on success, -errno otherwise.
435  */
436 static int macronix_quad_enable(struct spi_nor *nor)
437 {
438         int ret, val;
439
440         val = read_sr(nor);
441         if (val < 0)
442                 return val;
443         if (val & SR_QUAD_EN_MX)
444                 return 0;
445
446         write_enable(nor);
447
448         write_sr(nor, val | SR_QUAD_EN_MX);
449
450         ret = spi_nor_wait_till_ready(nor);
451         if (ret)
452                 return ret;
453
454         ret = read_sr(nor);
455         if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
456                 dev_err(nor->dev, "Macronix Quad bit not set\n");
457                 return -EINVAL;
458         }
459
460         return 0;
461 }
462 #endif
463
464 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
465 /*
466  * Write status Register and configuration register with 2 bytes
467  * The first byte will be written to the status register, while the
468  * second byte will be written to the configuration register.
469  * Return negative if error occurred.
470  */
471 static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
472 {
473         int ret;
474
475         write_enable(nor);
476
477         ret = spi_nor_write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
478         if (ret < 0) {
479                 dev_dbg(nor->dev,
480                         "error while writing configuration register\n");
481                 return -EINVAL;
482         }
483
484         ret = spi_nor_wait_till_ready(nor);
485         if (ret) {
486                 dev_dbg(nor->dev,
487                         "timeout while writing configuration register\n");
488                 return ret;
489         }
490
491         return 0;
492 }
493
494 /**
495  * spansion_read_cr_quad_enable() - set QE bit in Configuration Register.
496  * @nor:        pointer to a 'struct spi_nor'
497  *
498  * Set the Quad Enable (QE) bit in the Configuration Register.
499  * This function should be used with QSPI memories supporting the Read
500  * Configuration Register (35h) instruction.
501  *
502  * bit 1 of the Configuration Register is the QE bit for Spansion like QSPI
503  * memories.
504  *
505  * Return: 0 on success, -errno otherwise.
506  */
507 static int spansion_read_cr_quad_enable(struct spi_nor *nor)
508 {
509         u8 sr_cr[2];
510         int ret;
511
512         /* Check current Quad Enable bit value. */
513         ret = read_cr(nor);
514         if (ret < 0) {
515                 dev_dbg(dev, "error while reading configuration register\n");
516                 return -EINVAL;
517         }
518
519         if (ret & CR_QUAD_EN_SPAN)
520                 return 0;
521
522         sr_cr[1] = ret | CR_QUAD_EN_SPAN;
523
524         /* Keep the current value of the Status Register. */
525         ret = read_sr(nor);
526         if (ret < 0) {
527                 dev_dbg(dev, "error while reading status register\n");
528                 return -EINVAL;
529         }
530         sr_cr[0] = ret;
531
532         ret = write_sr_cr(nor, sr_cr);
533         if (ret)
534                 return ret;
535
536         /* Read back and check it. */
537         ret = read_cr(nor);
538         if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
539                 dev_dbg(nor->dev, "Spansion Quad bit not set\n");
540                 return -EINVAL;
541         }
542
543         return 0;
544 }
545 #endif /* CONFIG_SPI_FLASH_SPANSION */
546
547 struct spi_nor_read_command {
548         u8                      num_mode_clocks;
549         u8                      num_wait_states;
550         u8                      opcode;
551         enum spi_nor_protocol   proto;
552 };
553
554 enum spi_nor_read_command_index {
555         SNOR_CMD_READ,
556         SNOR_CMD_READ_FAST,
557
558         /* Quad SPI */
559         SNOR_CMD_READ_1_1_4,
560
561         SNOR_CMD_READ_MAX
562 };
563
564 struct spi_nor_flash_parameter {
565         struct spi_nor_hwcaps           hwcaps;
566         struct spi_nor_read_command     reads[SNOR_CMD_READ_MAX];
567 };
568
569 static void
570 spi_nor_set_read_settings(struct spi_nor_read_command *read,
571                           u8 num_mode_clocks,
572                           u8 num_wait_states,
573                           u8 opcode,
574                           enum spi_nor_protocol proto)
575 {
576         read->num_mode_clocks = num_mode_clocks;
577         read->num_wait_states = num_wait_states;
578         read->opcode = opcode;
579         read->proto = proto;
580 }
581
582 static int spi_nor_init_params(struct spi_nor *nor,
583                                const struct flash_info *info,
584                                struct spi_nor_flash_parameter *params)
585 {
586         /* (Fast) Read settings. */
587         params->hwcaps.mask = SNOR_HWCAPS_READ;
588         spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
589                                   0, 0, SPINOR_OP_READ,
590                                   SNOR_PROTO_1_1_1);
591
592         if (!(info->flags & SPI_NOR_NO_FR)) {
593                 params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
594                 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
595                                           0, 8, SPINOR_OP_READ_FAST,
596                                           SNOR_PROTO_1_1_1);
597         }
598
599         if (info->flags & SPI_NOR_QUAD_READ) {
600                 params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
601                 spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_4],
602                                           0, 8, SPINOR_OP_READ_1_1_4,
603                                           SNOR_PROTO_1_1_4);
604         }
605
606         return 0;
607 }
608
609 static int spi_nor_select_read(struct spi_nor *nor,
610                                const struct spi_nor_flash_parameter *params,
611                                u32 shared_hwcaps)
612 {
613         int best_match = shared_hwcaps & SNOR_HWCAPS_READ_MASK;
614         int cmd;
615         const struct spi_nor_read_command *read;
616
617         if (best_match < 0)
618                 return -EINVAL;
619
620         if (best_match & SNOR_HWCAPS_READ_1_1_4)
621                 cmd = SNOR_CMD_READ_1_1_4;
622         else if (best_match & SNOR_HWCAPS_READ_FAST)
623                 cmd = SNOR_CMD_READ_FAST;
624         else
625                 cmd = SNOR_CMD_READ;
626
627         read = &params->reads[cmd];
628         nor->read_opcode = read->opcode;
629         nor->read_proto = read->proto;
630
631         /*
632          * In the spi-nor framework, we don't need to make the difference
633          * between mode clock cycles and wait state clock cycles.
634          * Indeed, the value of the mode clock cycles is used by a QSPI
635          * flash memory to know whether it should enter or leave its 0-4-4
636          * (Continuous Read / XIP) mode.
637          * eXecution In Place is out of the scope of the mtd sub-system.
638          * Hence we choose to merge both mode and wait state clock cycles
639          * into the so called dummy clock cycles.
640          */
641         nor->read_dummy = read->num_mode_clocks + read->num_wait_states;
642         return 0;
643 }
644
645 static int spi_nor_setup(struct spi_nor *nor, const struct flash_info *info,
646                          const struct spi_nor_flash_parameter *params,
647                          const struct spi_nor_hwcaps *hwcaps)
648 {
649         u32 shared_mask;
650         int err;
651
652         /*
653          * Keep only the hardware capabilities supported by both the SPI
654          * controller and the SPI flash memory.
655          */
656         shared_mask = hwcaps->mask & params->hwcaps.mask;
657
658         /* Select the (Fast) Read command. */
659         err = spi_nor_select_read(nor, params, shared_mask);
660         if (err) {
661                 dev_dbg(nor->dev,
662                         "can't select read settings supported by both the SPI controller and memory.\n");
663                 return err;
664         }
665
666         /* Enable Quad I/O if needed. */
667         if (spi_nor_get_protocol_width(nor->read_proto) == 4) {
668                 switch (JEDEC_MFR(info)) {
669 #ifdef CONFIG_SPI_FLASH_MACRONIX
670                 case SNOR_MFR_MACRONIX:
671                         err = macronix_quad_enable(nor);
672                         break;
673 #endif
674                 case SNOR_MFR_ST:
675                 case SNOR_MFR_MICRON:
676                         break;
677
678                 default:
679 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
680                         /* Kept only for backward compatibility purpose. */
681                         err = spansion_read_cr_quad_enable(nor);
682 #endif
683                         break;
684                 }
685         }
686         if (err) {
687                 dev_dbg(nor->dev, "quad mode not supported\n");
688                 return err;
689         }
690
691         return 0;
692 }
693
694 static int spi_nor_init(struct spi_nor *nor)
695 {
696         if (nor->addr_width == 4 &&
697             (JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
698             !(nor->info->flags & SPI_NOR_4B_OPCODES)) {
699                 /*
700                  * If the RESET# pin isn't hooked up properly, or the system
701                  * otherwise doesn't perform a reset command in the boot
702                  * sequence, it's impossible to 100% protect against unexpected
703                  * reboots (e.g., crashes). Warn the user (or hopefully, system
704                  * designer) that this is bad.
705                  */
706                 if (nor->flags & SNOR_F_BROKEN_RESET)
707                         printf("enabling reset hack; may not recover from unexpected reboots\n");
708                 set_4byte(nor, nor->info, 1);
709         }
710
711         return 0;
712 }
713
714 int spi_nor_scan(struct spi_nor *nor)
715 {
716         struct spi_nor_flash_parameter params;
717         const struct flash_info *info = NULL;
718         struct mtd_info *mtd = &nor->mtd;
719         struct spi_nor_hwcaps hwcaps = {
720                 .mask = SNOR_HWCAPS_READ |
721                         SNOR_HWCAPS_READ_FAST
722         };
723         struct spi_slave *spi = nor->spi;
724         int ret;
725
726         /* Reset SPI protocol for all commands. */
727         nor->reg_proto = SNOR_PROTO_1_1_1;
728         nor->read_proto = SNOR_PROTO_1_1_1;
729         nor->write_proto = SNOR_PROTO_1_1_1;
730
731         if (spi->mode & SPI_RX_QUAD)
732                 hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
733
734         info = spi_nor_read_id(nor);
735         if (IS_ERR_OR_NULL(info))
736                 return -ENOENT;
737         /* Parse the Serial Flash Discoverable Parameters table. */
738         ret = spi_nor_init_params(nor, info, &params);
739         if (ret)
740                 return ret;
741
742         mtd->name = "spi-flash";
743         mtd->priv = nor;
744         mtd->type = MTD_NORFLASH;
745         mtd->writesize = 1;
746         mtd->flags = MTD_CAP_NORFLASH;
747         mtd->size = info->sector_size * info->n_sectors;
748         mtd->_erase = spi_nor_erase;
749         mtd->_read = spi_nor_read;
750         mtd->_write = spi_nor_write;
751
752         nor->size = mtd->size;
753
754         if (info->flags & USE_FSR)
755                 nor->flags |= SNOR_F_USE_FSR;
756         if (info->flags & USE_CLSR)
757                 nor->flags |= SNOR_F_USE_CLSR;
758
759         if (info->flags & SPI_NOR_NO_FR)
760                 params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
761
762         /*
763          * Configure the SPI memory:
764          * - select op codes for (Fast) Read, Page Program and Sector Erase.
765          * - set the number of dummy cycles (mode cycles + wait states).
766          * - set the SPI protocols for register and memory accesses.
767          * - set the Quad Enable bit if needed (required by SPI x-y-4 protos).
768          */
769         ret = spi_nor_setup(nor, info, &params, &hwcaps);
770         if (ret)
771                 return ret;
772
773         if (nor->addr_width) {
774                 /* already configured from SFDP */
775         } else if (info->addr_width) {
776                 nor->addr_width = info->addr_width;
777         } else if (mtd->size > 0x1000000) {
778                 /* enable 4-byte addressing if the device exceeds 16MiB */
779                 nor->addr_width = 4;
780                 if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
781                     info->flags & SPI_NOR_4B_OPCODES)
782                         spi_nor_set_4byte_opcodes(nor, info);
783         } else {
784                 nor->addr_width = 3;
785         }
786
787         if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
788                 dev_dbg(dev, "address width is too large: %u\n",
789                         nor->addr_width);
790                 return -EINVAL;
791         }
792
793         /* Send all the required SPI flash commands to initialize device */
794         nor->info = info;
795         ret = spi_nor_init(nor);
796         if (ret)
797                 return ret;
798
799         return 0;
800 }
801
802 /* U-Boot specific functions, need to extend MTD to support these */
803 int spi_flash_cmd_get_sw_write_prot(struct spi_nor *nor)
804 {
805         return -ENOTSUPP;
806 }