2 * MTD SPI driver for ST M25Pxx (and similar) serial flash chips
4 * Author: Mike Lavender, mike@steroidmicros.com
6 * Copyright (c) 2005, Intec Automation Inc.
8 * Some parts are based on lart.c by Abraham Van Der Merwe
10 * Cleaned up and generalized based on mtd_dataflash.c
12 * This code is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/interrupt.h>
22 #include <linux/mutex.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/partitions.h>
27 #include <linux/spi/spi.h>
28 #include <linux/spi/flash.h>
31 #define FLASH_PAGESIZE 256
34 #define OPCODE_WREN 0x06 /* Write enable */
35 #define OPCODE_RDSR 0x05 /* Read status register */
36 #define OPCODE_READ 0x03 /* Read data bytes (low frequency) */
37 #define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */
38 #define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */
39 #define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
40 #define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
41 #define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
42 #define OPCODE_RDID 0x9f /* Read JEDEC ID */
44 /* Status Register bits. */
45 #define SR_WIP 1 /* Write in progress */
46 #define SR_WEL 2 /* Write enable latch */
47 /* meaning of other SR_* bits may differ between vendors */
48 #define SR_BP0 4 /* Block protect 0 */
49 #define SR_BP1 8 /* Block protect 1 */
50 #define SR_BP2 0x10 /* Block protect 2 */
51 #define SR_SRWD 0x80 /* SR write protect */
53 /* Define max times to check status register before we give up. */
54 #define MAX_READY_WAIT_COUNT 100000
57 #ifdef CONFIG_MTD_PARTITIONS
58 #define mtd_has_partitions() (1)
60 #define mtd_has_partitions() (0)
63 /****************************************************************************/
66 struct spi_device *spi;
69 unsigned partitioned:1;
74 static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
76 return container_of(mtd, struct m25p, mtd);
79 /****************************************************************************/
82 * Internal helper functions
86 * Read the status register, returning its value in the location
87 * Return the status register value.
88 * Returns negative if error occurred.
90 static int read_sr(struct m25p *flash)
93 u8 code = OPCODE_RDSR;
96 retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
99 dev_err(&flash->spi->dev, "error %d reading SR\n",
109 * Set write enable latch with Write Enable command.
110 * Returns negative if error occurred.
112 static inline int write_enable(struct m25p *flash)
114 u8 code = OPCODE_WREN;
116 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
121 * Service routine to read status register until ready, or timeout occurs.
122 * Returns non-zero if error.
124 static int wait_till_ready(struct m25p *flash)
129 /* one chip guarantees max 5 msec wait here after page writes,
130 * but potentially three seconds (!) after page erase.
132 for (count = 0; count < MAX_READY_WAIT_COUNT; count++) {
133 if ((sr = read_sr(flash)) < 0)
135 else if (!(sr & SR_WIP))
138 /* REVISIT sometimes sleeping would be best */
146 * Erase one sector of flash memory at offset ``offset'' which is any
147 * address within the sector which should be erased.
149 * Returns 0 if successful, non-zero otherwise.
151 static int erase_sector(struct m25p *flash, u32 offset)
153 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
154 flash->spi->dev.bus_id, __FUNCTION__,
155 flash->mtd.erasesize / 1024, offset);
157 /* Wait until finished previous write command. */
158 if (wait_till_ready(flash))
161 /* Send write enable, then erase commands. */
164 /* Set up command buffer. */
165 flash->command[0] = flash->erase_opcode;
166 flash->command[1] = offset >> 16;
167 flash->command[2] = offset >> 8;
168 flash->command[3] = offset;
170 spi_write(flash->spi, flash->command, sizeof(flash->command));
175 /****************************************************************************/
182 * Erase an address range on the flash chip. The address range may extend
183 * one or more erase sectors. Return an error is there is a problem erasing.
185 static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
187 struct m25p *flash = mtd_to_m25p(mtd);
190 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %d\n",
191 flash->spi->dev.bus_id, __FUNCTION__, "at",
192 (u32)instr->addr, instr->len);
195 if (instr->addr + instr->len > flash->mtd.size)
197 if ((instr->addr % mtd->erasesize) != 0
198 || (instr->len % mtd->erasesize) != 0) {
205 mutex_lock(&flash->lock);
207 /* REVISIT in some cases we could speed up erasing large regions
208 * by using OPCODE_SE instead of OPCODE_BE_4K
211 /* now erase those sectors */
213 if (erase_sector(flash, addr)) {
214 instr->state = MTD_ERASE_FAILED;
215 mutex_unlock(&flash->lock);
219 addr += mtd->erasesize;
220 len -= mtd->erasesize;
223 mutex_unlock(&flash->lock);
225 instr->state = MTD_ERASE_DONE;
226 mtd_erase_callback(instr);
232 * Read an address range from the flash chip. The address range
233 * may be any size provided it is within the physical boundaries.
235 static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
236 size_t *retlen, u_char *buf)
238 struct m25p *flash = mtd_to_m25p(mtd);
239 struct spi_transfer t[2];
240 struct spi_message m;
242 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
243 flash->spi->dev.bus_id, __FUNCTION__, "from",
250 if (from + len > flash->mtd.size)
253 spi_message_init(&m);
254 memset(t, 0, (sizeof t));
256 t[0].tx_buf = flash->command;
257 t[0].len = sizeof(flash->command);
258 spi_message_add_tail(&t[0], &m);
262 spi_message_add_tail(&t[1], &m);
264 /* Byte count starts at zero. */
268 mutex_lock(&flash->lock);
270 /* Wait till previous write/erase is done. */
271 if (wait_till_ready(flash)) {
272 /* REVISIT status return?? */
273 mutex_unlock(&flash->lock);
277 /* FIXME switch to OPCODE_FAST_READ. It's required for higher
278 * clocks; and at this writing, every chip this driver handles
279 * supports that opcode.
282 /* Set up the write data buffer. */
283 flash->command[0] = OPCODE_READ;
284 flash->command[1] = from >> 16;
285 flash->command[2] = from >> 8;
286 flash->command[3] = from;
288 spi_sync(flash->spi, &m);
290 *retlen = m.actual_length - sizeof(flash->command);
292 mutex_unlock(&flash->lock);
298 * Write an address range to the flash chip. Data must be written in
299 * FLASH_PAGESIZE chunks. The address range may be any size provided
300 * it is within the physical boundaries.
302 static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
303 size_t *retlen, const u_char *buf)
305 struct m25p *flash = mtd_to_m25p(mtd);
306 u32 page_offset, page_size;
307 struct spi_transfer t[2];
308 struct spi_message m;
310 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
311 flash->spi->dev.bus_id, __FUNCTION__, "to",
321 if (to + len > flash->mtd.size)
324 spi_message_init(&m);
325 memset(t, 0, (sizeof t));
327 t[0].tx_buf = flash->command;
328 t[0].len = sizeof(flash->command);
329 spi_message_add_tail(&t[0], &m);
332 spi_message_add_tail(&t[1], &m);
334 mutex_lock(&flash->lock);
336 /* Wait until finished previous write command. */
337 if (wait_till_ready(flash))
342 /* Set up the opcode in the write buffer. */
343 flash->command[0] = OPCODE_PP;
344 flash->command[1] = to >> 16;
345 flash->command[2] = to >> 8;
346 flash->command[3] = to;
348 /* what page do we start with? */
349 page_offset = to % FLASH_PAGESIZE;
351 /* do all the bytes fit onto one page? */
352 if (page_offset + len <= FLASH_PAGESIZE) {
355 spi_sync(flash->spi, &m);
357 *retlen = m.actual_length - sizeof(flash->command);
361 /* the size of data remaining on the first page */
362 page_size = FLASH_PAGESIZE - page_offset;
364 t[1].len = page_size;
365 spi_sync(flash->spi, &m);
367 *retlen = m.actual_length - sizeof(flash->command);
369 /* write everything in PAGESIZE chunks */
370 for (i = page_size; i < len; i += page_size) {
372 if (page_size > FLASH_PAGESIZE)
373 page_size = FLASH_PAGESIZE;
375 /* write the next page to flash */
376 flash->command[1] = (to + i) >> 16;
377 flash->command[2] = (to + i) >> 8;
378 flash->command[3] = (to + i);
380 t[1].tx_buf = buf + i;
381 t[1].len = page_size;
383 wait_till_ready(flash);
387 spi_sync(flash->spi, &m);
390 *retlen += m.actual_length
391 - sizeof(flash->command);
395 mutex_unlock(&flash->lock);
401 /****************************************************************************/
404 * SPI device driver setup and teardown
410 /* JEDEC id zero means "no ID" (most older chips); otherwise it has
411 * a high byte of zero plus three data bytes: the manufacturer id,
412 * then a two byte device id.
416 /* The size listed here is what works with OPCODE_SE, which isn't
417 * necessarily called a "sector" by the vendor.
419 unsigned sector_size;
423 #define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
427 /* NOTE: double check command sets and memory organization when you add
428 * more flash chips. This current list focusses on newer chips, which
429 * have been converging on command sets which including JEDEC ID.
431 static struct flash_info __devinitdata m25p_data [] = {
433 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
434 { "at25fs010", 0x1f6601, 32 * 1024, 4, SECT_4K, },
435 { "at25fs040", 0x1f6604, 64 * 1024, 8, SECT_4K, },
437 { "at25df041a", 0x1f4401, 64 * 1024, 8, SECT_4K, },
439 { "at26f004", 0x1f0400, 64 * 1024, 8, SECT_4K, },
440 { "at26df081a", 0x1f4501, 64 * 1024, 16, SECT_4K, },
441 { "at26df161a", 0x1f4601, 64 * 1024, 32, SECT_4K, },
442 { "at26df321", 0x1f4701, 64 * 1024, 64, SECT_4K, },
444 /* Spansion -- single (large) sector size only, at least
445 * for the chips listed here (without boot sectors).
447 { "s25sl004a", 0x010212, 64 * 1024, 8, },
448 { "s25sl008a", 0x010213, 64 * 1024, 16, },
449 { "s25sl016a", 0x010214, 64 * 1024, 32, },
450 { "s25sl032a", 0x010215, 64 * 1024, 64, },
451 { "s25sl064a", 0x010216, 64 * 1024, 128, },
453 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
454 { "sst25vf040b", 0xbf258d, 64 * 1024, 8, SECT_4K, },
455 { "sst25vf080b", 0xbf258e, 64 * 1024, 16, SECT_4K, },
456 { "sst25vf016b", 0xbf2541, 64 * 1024, 32, SECT_4K, },
457 { "sst25vf032b", 0xbf254a, 64 * 1024, 64, SECT_4K, },
459 /* ST Microelectronics -- newer production may have feature updates */
460 { "m25p05", 0x202010, 32 * 1024, 2, },
461 { "m25p10", 0x202011, 32 * 1024, 4, },
462 { "m25p20", 0x202012, 64 * 1024, 4, },
463 { "m25p40", 0x202013, 64 * 1024, 8, },
464 { "m25p80", 0, 64 * 1024, 16, },
465 { "m25p16", 0x202015, 64 * 1024, 32, },
466 { "m25p32", 0x202016, 64 * 1024, 64, },
467 { "m25p64", 0x202017, 64 * 1024, 128, },
468 { "m25p128", 0x202018, 256 * 1024, 64, },
470 { "m45pe80", 0x204014, 64 * 1024, 16, },
471 { "m45pe16", 0x204015, 64 * 1024, 32, },
473 { "m25pe80", 0x208014, 64 * 1024, 16, },
474 { "m25pe16", 0x208015, 64 * 1024, 32, SECT_4K, },
476 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
477 { "w25x10", 0xef3011, 64 * 1024, 2, SECT_4K, },
478 { "w25x20", 0xef3012, 64 * 1024, 4, SECT_4K, },
479 { "w25x40", 0xef3013, 64 * 1024, 8, SECT_4K, },
480 { "w25x80", 0xef3014, 64 * 1024, 16, SECT_4K, },
481 { "w25x16", 0xef3015, 64 * 1024, 32, SECT_4K, },
482 { "w25x32", 0xef3016, 64 * 1024, 64, SECT_4K, },
483 { "w25x64", 0xef3017, 64 * 1024, 128, SECT_4K, },
486 static struct flash_info *__devinit jedec_probe(struct spi_device *spi)
489 u8 code = OPCODE_RDID;
492 struct flash_info *info;
494 /* JEDEC also defines an optional "extended device information"
495 * string for after vendor-specific data, after the three bytes
496 * we use here. Supporting some chips might require using it.
498 tmp = spi_write_then_read(spi, &code, 1, id, 3);
500 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
501 spi->dev.bus_id, tmp);
510 for (tmp = 0, info = m25p_data;
511 tmp < ARRAY_SIZE(m25p_data);
513 if (info->jedec_id == jedec)
516 dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
522 * board specific setup should have ensured the SPI clock used here
523 * matches what the READ command supports, at least until this driver
524 * understands FAST_READ (for clocks over 25 MHz).
526 static int __devinit m25p_probe(struct spi_device *spi)
528 struct flash_platform_data *data;
530 struct flash_info *info;
533 /* Platform data helps sort out which chip type we have, as
534 * well as how this board partitions it. If we don't have
535 * a chip ID, try the JEDEC id commands; they'll work for most
536 * newer chips, even if we don't recognize the particular chip.
538 data = spi->dev.platform_data;
539 if (data && data->type) {
540 for (i = 0, info = m25p_data;
541 i < ARRAY_SIZE(m25p_data);
543 if (strcmp(data->type, info->name) == 0)
547 /* unrecognized chip? */
548 if (i == ARRAY_SIZE(m25p_data)) {
549 DEBUG(MTD_DEBUG_LEVEL0, "%s: unrecognized id %s\n",
550 spi->dev.bus_id, data->type);
553 /* recognized; is that chip really what's there? */
554 } else if (info->jedec_id) {
555 struct flash_info *chip = jedec_probe(spi);
557 if (!chip || chip != info) {
558 dev_warn(&spi->dev, "found %s, expected %s\n",
559 chip ? chip->name : "UNKNOWN",
565 info = jedec_probe(spi);
570 flash = kzalloc(sizeof *flash, GFP_KERNEL);
575 mutex_init(&flash->lock);
576 dev_set_drvdata(&spi->dev, flash);
578 if (data && data->name)
579 flash->mtd.name = data->name;
581 flash->mtd.name = spi->dev.bus_id;
583 flash->mtd.type = MTD_NORFLASH;
584 flash->mtd.writesize = 1;
585 flash->mtd.flags = MTD_CAP_NORFLASH;
586 flash->mtd.size = info->sector_size * info->n_sectors;
587 flash->mtd.erase = m25p80_erase;
588 flash->mtd.read = m25p80_read;
589 flash->mtd.write = m25p80_write;
591 /* prefer "small sector" erase if possible */
592 if (info->flags & SECT_4K) {
593 flash->erase_opcode = OPCODE_BE_4K;
594 flash->mtd.erasesize = 4096;
596 flash->erase_opcode = OPCODE_SE;
597 flash->mtd.erasesize = info->sector_size;
600 dev_info(&spi->dev, "%s (%d Kbytes)\n", info->name,
601 flash->mtd.size / 1024);
603 DEBUG(MTD_DEBUG_LEVEL2,
604 "mtd .name = %s, .size = 0x%.8x (%uMiB) "
605 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
607 flash->mtd.size, flash->mtd.size / (1024*1024),
608 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
609 flash->mtd.numeraseregions);
611 if (flash->mtd.numeraseregions)
612 for (i = 0; i < flash->mtd.numeraseregions; i++)
613 DEBUG(MTD_DEBUG_LEVEL2,
614 "mtd.eraseregions[%d] = { .offset = 0x%.8x, "
615 ".erasesize = 0x%.8x (%uKiB), "
616 ".numblocks = %d }\n",
617 i, flash->mtd.eraseregions[i].offset,
618 flash->mtd.eraseregions[i].erasesize,
619 flash->mtd.eraseregions[i].erasesize / 1024,
620 flash->mtd.eraseregions[i].numblocks);
623 /* partitions should match sector boundaries; and it may be good to
624 * use readonly partitions for writeprotected sectors (BP2..BP0).
626 if (mtd_has_partitions()) {
627 struct mtd_partition *parts = NULL;
630 #ifdef CONFIG_MTD_CMDLINE_PARTS
631 static const char *part_probes[] = { "cmdlinepart", NULL, };
633 nr_parts = parse_mtd_partitions(&flash->mtd,
634 part_probes, &parts, 0);
637 if (nr_parts <= 0 && data && data->parts) {
639 nr_parts = data->nr_parts;
643 for (i = 0; i < nr_parts; i++) {
644 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
645 "{.name = %s, .offset = 0x%.8x, "
646 ".size = 0x%.8x (%uKiB) }\n",
650 parts[i].size / 1024);
652 flash->partitioned = 1;
653 return add_mtd_partitions(&flash->mtd, parts, nr_parts);
655 } else if (data->nr_parts)
656 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
657 data->nr_parts, data->name);
659 return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
663 static int __devexit m25p_remove(struct spi_device *spi)
665 struct m25p *flash = dev_get_drvdata(&spi->dev);
668 /* Clean up MTD stuff. */
669 if (mtd_has_partitions() && flash->partitioned)
670 status = del_mtd_partitions(&flash->mtd);
672 status = del_mtd_device(&flash->mtd);
679 static struct spi_driver m25p80_driver = {
682 .bus = &spi_bus_type,
683 .owner = THIS_MODULE,
686 .remove = __devexit_p(m25p_remove),
688 /* REVISIT: many of these chips have deep power-down modes, which
689 * should clearly be entered on suspend() to minimize power use.
690 * And also when they're otherwise idle...
695 static int m25p80_init(void)
697 return spi_register_driver(&m25p80_driver);
701 static void m25p80_exit(void)
703 spi_unregister_driver(&m25p80_driver);
707 module_init(m25p80_init);
708 module_exit(m25p80_exit);
710 MODULE_LICENSE("GPL");
711 MODULE_AUTHOR("Mike Lavender");
712 MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");