2 * Based on m25p80.c, by Mike Lavender (mike@steroidmicros.com), with
3 * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
5 * Copyright (C) 2005, Intec Automation Inc.
6 * Copyright (C) 2014, Freescale Semiconductor, Inc.
8 * This code is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/mutex.h>
18 #include <linux/math64.h>
20 #include <linux/mtd/cfi.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/of_platform.h>
23 #include <linux/spi/flash.h>
24 #include <linux/mtd/spi-nor.h>
26 /* Define max times to check status register before we give up. */
27 #define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
29 #define JEDEC_MFR(_jedec_id) ((_jedec_id) >> 16)
32 * Read the status register, returning its value in the location
33 * Return the status register value.
34 * Returns negative if error occurred.
36 static int read_sr(struct spi_nor *nor)
41 ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
43 pr_err("error %d reading SR\n", (int) ret);
51 * Read configuration register, returning its value in the
52 * location. Return the configuration register value.
53 * Returns negative if error occured.
55 static int read_cr(struct spi_nor *nor)
60 ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
62 dev_err(nor->dev, "error %d reading CR\n", ret);
70 * Dummy Cycle calculation for different type of read.
71 * It can be used to support more commands with
72 * different dummy cycle requirements.
74 static inline int spi_nor_read_dummy_cycles(struct spi_nor *nor)
76 switch (nor->flash_read) {
88 * Write status register 1 byte
89 * Returns negative if error occurred.
91 static inline int write_sr(struct spi_nor *nor, u8 val)
93 nor->cmd_buf[0] = val;
94 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1, 0);
98 * Set write enable latch with Write Enable command.
99 * Returns negative if error occurred.
101 static inline int write_enable(struct spi_nor *nor)
103 return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0, 0);
107 * Send write disble instruction to the chip.
109 static inline int write_disable(struct spi_nor *nor)
111 return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0, 0);
114 static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
119 /* Enable/disable 4-byte addressing mode. */
120 static inline int set_4byte(struct spi_nor *nor, u32 jedec_id, int enable)
123 bool need_wren = false;
126 switch (JEDEC_MFR(jedec_id)) {
127 case CFI_MFR_ST: /* Micron, actually */
128 /* Some Micron need WREN command; all will accept it */
130 case CFI_MFR_MACRONIX:
131 case 0xEF /* winbond */:
135 cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
136 status = nor->write_reg(nor, cmd, NULL, 0, 0);
143 nor->cmd_buf[0] = enable << 7;
144 return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1, 0);
148 static int spi_nor_wait_till_ready(struct spi_nor *nor)
150 unsigned long deadline;
153 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
161 else if (!(sr & SR_WIP))
163 } while (!time_after_eq(jiffies, deadline));
169 * Service routine to read status register until ready, or timeout occurs.
170 * Returns non-zero if error.
172 static int wait_till_ready(struct spi_nor *nor)
174 return nor->wait_till_ready(nor);
178 * Erase the whole flash memory
180 * Returns 0 if successful, non-zero otherwise.
182 static int erase_chip(struct spi_nor *nor)
186 dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd->size >> 10));
188 /* Wait until finished previous write command. */
189 ret = wait_till_ready(nor);
193 /* Send write enable, then erase commands. */
196 return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0, 0);
199 static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
203 mutex_lock(&nor->lock);
206 ret = nor->prepare(nor, ops);
208 dev_err(nor->dev, "failed in the preparation.\n");
209 mutex_unlock(&nor->lock);
216 static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
219 nor->unprepare(nor, ops);
220 mutex_unlock(&nor->lock);
224 * Erase an address range on the nor chip. The address range may extend
225 * one or more erase sectors. Return an error is there is a problem erasing.
227 static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
229 struct spi_nor *nor = mtd_to_spi_nor(mtd);
234 dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
235 (long long)instr->len);
237 div_u64_rem(instr->len, mtd->erasesize, &rem);
244 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
248 /* whole-chip erase? */
249 if (len == mtd->size) {
250 if (erase_chip(nor)) {
255 /* REVISIT in some cases we could speed up erasing large regions
256 * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
257 * to use "small sector erase", but that's not always optimal.
260 /* "sector"-at-a-time erase */
263 if (nor->erase(nor, addr)) {
268 addr += mtd->erasesize;
269 len -= mtd->erasesize;
273 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
275 instr->state = MTD_ERASE_DONE;
276 mtd_erase_callback(instr);
281 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
282 instr->state = MTD_ERASE_FAILED;
286 static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
288 struct spi_nor *nor = mtd_to_spi_nor(mtd);
289 uint32_t offset = ofs;
290 uint8_t status_old, status_new;
293 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
297 /* Wait until finished previous command */
298 ret = wait_till_ready(nor);
302 status_old = read_sr(nor);
304 if (offset < mtd->size - (mtd->size / 2))
305 status_new = status_old | SR_BP2 | SR_BP1 | SR_BP0;
306 else if (offset < mtd->size - (mtd->size / 4))
307 status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
308 else if (offset < mtd->size - (mtd->size / 8))
309 status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
310 else if (offset < mtd->size - (mtd->size / 16))
311 status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
312 else if (offset < mtd->size - (mtd->size / 32))
313 status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
314 else if (offset < mtd->size - (mtd->size / 64))
315 status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
317 status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
319 /* Only modify protection if it will not unlock other areas */
320 if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) >
321 (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
323 ret = write_sr(nor, status_new);
329 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
333 static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
335 struct spi_nor *nor = mtd_to_spi_nor(mtd);
336 uint32_t offset = ofs;
337 uint8_t status_old, status_new;
340 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
344 /* Wait until finished previous command */
345 ret = wait_till_ready(nor);
349 status_old = read_sr(nor);
351 if (offset+len > mtd->size - (mtd->size / 64))
352 status_new = status_old & ~(SR_BP2 | SR_BP1 | SR_BP0);
353 else if (offset+len > mtd->size - (mtd->size / 32))
354 status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
355 else if (offset+len > mtd->size - (mtd->size / 16))
356 status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
357 else if (offset+len > mtd->size - (mtd->size / 8))
358 status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
359 else if (offset+len > mtd->size - (mtd->size / 4))
360 status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
361 else if (offset+len > mtd->size - (mtd->size / 2))
362 status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
364 status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
366 /* Only modify protection if it will not lock other areas */
367 if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) <
368 (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
370 ret = write_sr(nor, status_new);
376 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
381 /* JEDEC id zero means "no ID" (most older chips); otherwise it has
382 * a high byte of zero plus three data bytes: the manufacturer id,
383 * then a two byte device id.
388 /* The size listed here is what works with SPINOR_OP_SE, which isn't
389 * necessarily called a "sector" by the vendor.
391 unsigned sector_size;
398 #define SECT_4K 0x01 /* SPINOR_OP_BE_4K works uniformly */
399 #define SPI_NOR_NO_ERASE 0x02 /* No erase command needed */
400 #define SST_WRITE 0x04 /* use SST byte programming */
401 #define SPI_NOR_NO_FR 0x08 /* Can't do fastread */
402 #define SECT_4K_PMC 0x10 /* SPINOR_OP_BE_4K_PMC works uniformly */
403 #define SPI_NOR_DUAL_READ 0x20 /* Flash supports Dual Read */
404 #define SPI_NOR_QUAD_READ 0x40 /* Flash supports Quad Read */
407 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
408 ((kernel_ulong_t)&(struct flash_info) { \
409 .jedec_id = (_jedec_id), \
410 .ext_id = (_ext_id), \
411 .sector_size = (_sector_size), \
412 .n_sectors = (_n_sectors), \
417 #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
418 ((kernel_ulong_t)&(struct flash_info) { \
419 .sector_size = (_sector_size), \
420 .n_sectors = (_n_sectors), \
421 .page_size = (_page_size), \
422 .addr_width = (_addr_width), \
426 /* NOTE: double check command sets and memory organization when you add
427 * more nor chips. This current list focusses on newer chips, which
428 * have been converging on command sets which including JEDEC ID.
430 const struct spi_device_id spi_nor_ids[] = {
431 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
432 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
433 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
435 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
436 { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
437 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
439 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
440 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
441 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
442 { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
444 { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
447 { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
448 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
449 { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
450 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
451 { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
452 { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
455 { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K) },
458 { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
459 { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
462 { "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64, SECT_4K) },
463 { "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128, SECT_4K) },
465 /* Intel/Numonyx -- xxxs33b */
466 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
467 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
468 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
471 { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
472 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
473 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
474 { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
475 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
476 { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
477 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
478 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
479 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
480 { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
481 { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
482 { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_QUAD_READ) },
483 { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
486 { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, 0) },
487 { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, 0) },
488 { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, 0) },
489 { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K) },
490 { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K) },
493 { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
494 { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
495 { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
497 /* Spansion -- single (large) sector size only, at least
498 * for the chips listed here (without boot sectors).
500 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
501 { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, 0) },
502 { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
503 { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
504 { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
505 { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
506 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
507 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
508 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
509 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
510 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
511 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
512 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
513 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
514 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
515 { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
516 { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K) },
517 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
519 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
520 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
521 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
522 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
523 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
524 { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
525 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
526 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
527 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
528 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
530 /* ST Microelectronics -- newer production may have feature updates */
531 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
532 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
533 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
534 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
535 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
536 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
537 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
538 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
539 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
540 { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, 0) },
542 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
543 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
544 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
545 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
546 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
547 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
548 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
549 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
550 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
552 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
553 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
554 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
556 { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
557 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
558 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
560 { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
561 { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
562 { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
563 { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
564 { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
566 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
567 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
568 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
569 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
570 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
571 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
572 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
573 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
574 { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K) },
575 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
576 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
577 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
578 { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
579 { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
580 { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
581 { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) },
583 /* Catalyst / On Semiconductor -- non-JEDEC */
584 { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
585 { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
586 { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
587 { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
588 { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
591 EXPORT_SYMBOL_GPL(spi_nor_ids);
593 static const struct spi_device_id *spi_nor_read_id(struct spi_nor *nor)
599 struct flash_info *info;
601 tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, 5);
603 dev_dbg(nor->dev, " error %d reading JEDEC ID\n", tmp);
612 ext_jedec = id[3] << 8 | id[4];
614 for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
615 info = (void *)spi_nor_ids[tmp].driver_data;
616 if (info->jedec_id == jedec) {
617 if (info->ext_id == 0 || info->ext_id == ext_jedec)
618 return &spi_nor_ids[tmp];
621 dev_err(nor->dev, "unrecognized JEDEC id %06x\n", jedec);
622 return ERR_PTR(-ENODEV);
625 static const struct spi_device_id *jedec_probe(struct spi_nor *nor)
627 return nor->read_id(nor);
630 static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
631 size_t *retlen, u_char *buf)
633 struct spi_nor *nor = mtd_to_spi_nor(mtd);
636 dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
638 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
642 ret = nor->read(nor, from, len, retlen, buf);
644 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
648 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
649 size_t *retlen, const u_char *buf)
651 struct spi_nor *nor = mtd_to_spi_nor(mtd);
655 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
657 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
661 /* Wait until finished previous write command. */
662 ret = wait_till_ready(nor);
668 nor->sst_write_second = false;
671 /* Start write from odd address. */
673 nor->program_opcode = SPINOR_OP_BP;
675 /* write one byte. */
676 nor->write(nor, to, 1, retlen, buf);
677 ret = wait_till_ready(nor);
683 /* Write out most of the data here. */
684 for (; actual < len - 1; actual += 2) {
685 nor->program_opcode = SPINOR_OP_AAI_WP;
687 /* write two bytes. */
688 nor->write(nor, to, 2, retlen, buf + actual);
689 ret = wait_till_ready(nor);
693 nor->sst_write_second = true;
695 nor->sst_write_second = false;
698 ret = wait_till_ready(nor);
702 /* Write out trailing byte if it exists. */
706 nor->program_opcode = SPINOR_OP_BP;
707 nor->write(nor, to, 1, retlen, buf + actual);
709 ret = wait_till_ready(nor);
715 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
720 * Write an address range to the nor chip. Data must be written in
721 * FLASH_PAGESIZE chunks. The address range may be any size provided
722 * it is within the physical boundaries.
724 static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
725 size_t *retlen, const u_char *buf)
727 struct spi_nor *nor = mtd_to_spi_nor(mtd);
728 u32 page_offset, page_size, i;
731 dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
733 ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
737 /* Wait until finished previous write command. */
738 ret = wait_till_ready(nor);
744 page_offset = to & (nor->page_size - 1);
746 /* do all the bytes fit onto one page? */
747 if (page_offset + len <= nor->page_size) {
748 nor->write(nor, to, len, retlen, buf);
750 /* the size of data remaining on the first page */
751 page_size = nor->page_size - page_offset;
752 nor->write(nor, to, page_size, retlen, buf);
754 /* write everything in nor->page_size chunks */
755 for (i = page_size; i < len; i += page_size) {
757 if (page_size > nor->page_size)
758 page_size = nor->page_size;
760 wait_till_ready(nor);
763 nor->write(nor, to + i, page_size, retlen, buf + i);
768 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
772 static int macronix_quad_enable(struct spi_nor *nor)
779 nor->cmd_buf[0] = val | SR_QUAD_EN_MX;
780 nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1, 0);
782 if (wait_till_ready(nor))
786 if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
787 dev_err(nor->dev, "Macronix Quad bit not set\n");
795 * Write status Register and configuration register with 2 bytes
796 * The first byte will be written to the status register, while the
797 * second byte will be written to the configuration register.
798 * Return negative if error occured.
800 static int write_sr_cr(struct spi_nor *nor, u16 val)
802 nor->cmd_buf[0] = val & 0xff;
803 nor->cmd_buf[1] = (val >> 8);
805 return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2, 0);
808 static int spansion_quad_enable(struct spi_nor *nor)
811 int quad_en = CR_QUAD_EN_SPAN << 8;
815 ret = write_sr_cr(nor, quad_en);
818 "error while writing configuration register\n");
822 /* read back and check it */
824 if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
825 dev_err(nor->dev, "Spansion Quad bit not set\n");
832 static int set_quad_mode(struct spi_nor *nor, u32 jedec_id)
836 switch (JEDEC_MFR(jedec_id)) {
837 case CFI_MFR_MACRONIX:
838 status = macronix_quad_enable(nor);
840 dev_err(nor->dev, "Macronix quad-read not enabled\n");
845 status = spansion_quad_enable(nor);
847 dev_err(nor->dev, "Spansion quad-read not enabled\n");
854 static int spi_nor_check(struct spi_nor *nor)
856 if (!nor->dev || !nor->read || !nor->write ||
857 !nor->read_reg || !nor->write_reg || !nor->erase) {
858 pr_err("spi-nor: please fill all the necessary fields!\n");
863 nor->read_id = spi_nor_read_id;
864 if (!nor->wait_till_ready)
865 nor->wait_till_ready = spi_nor_wait_till_ready;
870 int spi_nor_scan(struct spi_nor *nor, const struct spi_device_id *id,
873 struct flash_info *info;
874 struct flash_platform_data *data;
875 struct device *dev = nor->dev;
876 struct mtd_info *mtd = nor->mtd;
877 struct device_node *np = dev->of_node;
881 ret = spi_nor_check(nor);
885 /* Platform data helps sort out which chip type we have, as
886 * well as how this board partitions it. If we don't have
887 * a chip ID, try the JEDEC id commands; they'll work for most
888 * newer chips, even if we don't recognize the particular chip.
890 data = dev_get_platdata(dev);
891 if (data && data->type) {
892 const struct spi_device_id *plat_id;
894 for (i = 0; i < ARRAY_SIZE(spi_nor_ids) - 1; i++) {
895 plat_id = &spi_nor_ids[i];
896 if (strcmp(data->type, plat_id->name))
901 if (i < ARRAY_SIZE(spi_nor_ids) - 1)
904 dev_warn(dev, "unrecognized id %s\n", data->type);
907 info = (void *)id->driver_data;
909 if (info->jedec_id) {
910 const struct spi_device_id *jid;
912 jid = jedec_probe(nor);
915 } else if (jid != id) {
917 * JEDEC knows better, so overwrite platform ID. We
918 * can't trust partitions any longer, but we'll let
919 * mtd apply them anyway, since some partitions may be
920 * marked read-only, and we don't want to lose that
921 * information, even if it's not 100% accurate.
923 dev_warn(dev, "found %s, expected %s\n",
924 jid->name, id->name);
926 info = (void *)jid->driver_data;
930 mutex_init(&nor->lock);
933 * Atmel, SST and Intel/Numonyx serial nor tend to power
934 * up with the software protection bits set
937 if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ATMEL ||
938 JEDEC_MFR(info->jedec_id) == CFI_MFR_INTEL ||
939 JEDEC_MFR(info->jedec_id) == CFI_MFR_SST) {
944 if (data && data->name)
945 mtd->name = data->name;
947 mtd->name = dev_name(dev);
949 mtd->type = MTD_NORFLASH;
951 mtd->flags = MTD_CAP_NORFLASH;
952 mtd->size = info->sector_size * info->n_sectors;
953 mtd->_erase = spi_nor_erase;
954 mtd->_read = spi_nor_read;
956 /* nor protection support for STmicro chips */
957 if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ST) {
958 mtd->_lock = spi_nor_lock;
959 mtd->_unlock = spi_nor_unlock;
962 /* sst nor chips use AAI word program */
963 if (info->flags & SST_WRITE)
964 mtd->_write = sst_write;
966 mtd->_write = spi_nor_write;
968 /* prefer "small sector" erase if possible */
969 if (info->flags & SECT_4K) {
970 nor->erase_opcode = SPINOR_OP_BE_4K;
971 mtd->erasesize = 4096;
972 } else if (info->flags & SECT_4K_PMC) {
973 nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
974 mtd->erasesize = 4096;
976 nor->erase_opcode = SPINOR_OP_SE;
977 mtd->erasesize = info->sector_size;
980 if (info->flags & SPI_NOR_NO_ERASE)
981 mtd->flags |= MTD_NO_ERASE;
983 mtd->dev.parent = dev;
984 nor->page_size = info->page_size;
985 mtd->writebufsize = nor->page_size;
988 /* If we were instantiated by DT, use it */
989 if (of_property_read_bool(np, "m25p,fast-read"))
990 nor->flash_read = SPI_NOR_FAST;
992 nor->flash_read = SPI_NOR_NORMAL;
994 /* If we weren't instantiated by DT, default to fast-read */
995 nor->flash_read = SPI_NOR_FAST;
998 /* Some devices cannot do fast-read, no matter what DT tells us */
999 if (info->flags & SPI_NOR_NO_FR)
1000 nor->flash_read = SPI_NOR_NORMAL;
1002 /* Quad/Dual-read mode takes precedence over fast/normal */
1003 if (mode == SPI_NOR_QUAD && info->flags & SPI_NOR_QUAD_READ) {
1004 ret = set_quad_mode(nor, info->jedec_id);
1006 dev_err(dev, "quad mode not supported\n");
1009 nor->flash_read = SPI_NOR_QUAD;
1010 } else if (mode == SPI_NOR_DUAL && info->flags & SPI_NOR_DUAL_READ) {
1011 nor->flash_read = SPI_NOR_DUAL;
1014 /* Default commands */
1015 switch (nor->flash_read) {
1017 nor->read_opcode = SPINOR_OP_READ_1_1_4;
1020 nor->read_opcode = SPINOR_OP_READ_1_1_2;
1023 nor->read_opcode = SPINOR_OP_READ_FAST;
1025 case SPI_NOR_NORMAL:
1026 nor->read_opcode = SPINOR_OP_READ;
1029 dev_err(dev, "No Read opcode defined\n");
1033 nor->program_opcode = SPINOR_OP_PP;
1035 if (info->addr_width)
1036 nor->addr_width = info->addr_width;
1037 else if (mtd->size > 0x1000000) {
1038 /* enable 4-byte addressing if the device exceeds 16MiB */
1039 nor->addr_width = 4;
1040 if (JEDEC_MFR(info->jedec_id) == CFI_MFR_AMD) {
1041 /* Dedicated 4-byte command set */
1042 switch (nor->flash_read) {
1044 nor->read_opcode = SPINOR_OP_READ4_1_1_4;
1047 nor->read_opcode = SPINOR_OP_READ4_1_1_2;
1050 nor->read_opcode = SPINOR_OP_READ4_FAST;
1052 case SPI_NOR_NORMAL:
1053 nor->read_opcode = SPINOR_OP_READ4;
1056 nor->program_opcode = SPINOR_OP_PP_4B;
1057 /* No small sector erase for 4-byte command set */
1058 nor->erase_opcode = SPINOR_OP_SE_4B;
1059 mtd->erasesize = info->sector_size;
1061 set_4byte(nor, info->jedec_id, 1);
1063 nor->addr_width = 3;
1066 nor->read_dummy = spi_nor_read_dummy_cycles(nor);
1068 dev_info(dev, "%s (%lld Kbytes)\n", id->name,
1069 (long long)mtd->size >> 10);
1072 "mtd .name = %s, .size = 0x%llx (%lldMiB), "
1073 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
1074 mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
1075 mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
1077 if (mtd->numeraseregions)
1078 for (i = 0; i < mtd->numeraseregions; i++)
1080 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
1081 ".erasesize = 0x%.8x (%uKiB), "
1082 ".numblocks = %d }\n",
1083 i, (long long)mtd->eraseregions[i].offset,
1084 mtd->eraseregions[i].erasesize,
1085 mtd->eraseregions[i].erasesize / 1024,
1086 mtd->eraseregions[i].numblocks);
1089 EXPORT_SYMBOL_GPL(spi_nor_scan);
1091 const struct spi_device_id *spi_nor_match_id(char *name)
1093 const struct spi_device_id *id = spi_nor_ids;
1095 while (id->name[0]) {
1096 if (!strcmp(name, id->name))
1102 EXPORT_SYMBOL_GPL(spi_nor_match_id);
1104 MODULE_LICENSE("GPL");
1105 MODULE_AUTHOR("Huang Shijie <shijie8@gmail.com>");
1106 MODULE_AUTHOR("Mike Lavender");
1107 MODULE_DESCRIPTION("framework for SPI NOR");