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>
23 #include <linux/math64.h>
24 #include <linux/mod_devicetable.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/partitions.h>
29 #include <linux/spi/spi.h>
30 #include <linux/spi/flash.h>
33 #define OPCODE_WREN 0x06 /* Write enable */
34 #define OPCODE_RDSR 0x05 /* Read status register */
35 #define OPCODE_WRSR 0x01 /* Write status register 1 byte */
36 #define OPCODE_NORM_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_CHIP_ERASE 0xc7 /* Erase whole flash chip */
42 #define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
43 #define OPCODE_RDID 0x9f /* Read JEDEC ID */
45 /* Used for SST flashes only. */
46 #define OPCODE_BP 0x02 /* Byte program */
47 #define OPCODE_WRDI 0x04 /* Write disable */
48 #define OPCODE_AAI_WP 0xad /* Auto address increment word program */
50 /* Status Register bits. */
51 #define SR_WIP 1 /* Write in progress */
52 #define SR_WEL 2 /* Write enable latch */
53 /* meaning of other SR_* bits may differ between vendors */
54 #define SR_BP0 4 /* Block protect 0 */
55 #define SR_BP1 8 /* Block protect 1 */
56 #define SR_BP2 0x10 /* Block protect 2 */
57 #define SR_SRWD 0x80 /* SR write protect */
59 /* Define max times to check status register before we give up. */
60 #define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
61 #define MAX_CMD_SIZE 4
63 #ifdef CONFIG_M25PXX_USE_FAST_READ
64 #define OPCODE_READ OPCODE_FAST_READ
65 #define FAST_READ_DUMMY_BYTE 1
67 #define OPCODE_READ OPCODE_NORM_READ
68 #define FAST_READ_DUMMY_BYTE 0
71 /****************************************************************************/
74 struct spi_device *spi;
77 unsigned partitioned:1;
84 static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
86 return container_of(mtd, struct m25p, mtd);
89 /****************************************************************************/
92 * Internal helper functions
96 * Read the status register, returning its value in the location
97 * Return the status register value.
98 * Returns negative if error occurred.
100 static int read_sr(struct m25p *flash)
103 u8 code = OPCODE_RDSR;
106 retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
109 dev_err(&flash->spi->dev, "error %d reading SR\n",
118 * Write status register 1 byte
119 * Returns negative if error occurred.
121 static int write_sr(struct m25p *flash, u8 val)
123 flash->command[0] = OPCODE_WRSR;
124 flash->command[1] = val;
126 return spi_write(flash->spi, flash->command, 2);
130 * Set write enable latch with Write Enable command.
131 * Returns negative if error occurred.
133 static inline int write_enable(struct m25p *flash)
135 u8 code = OPCODE_WREN;
137 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
141 * Send write disble instruction to the chip.
143 static inline int write_disable(struct m25p *flash)
145 u8 code = OPCODE_WRDI;
147 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
151 * Service routine to read status register until ready, or timeout occurs.
152 * Returns non-zero if error.
154 static int wait_till_ready(struct m25p *flash)
156 unsigned long deadline;
159 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
162 if ((sr = read_sr(flash)) < 0)
164 else if (!(sr & SR_WIP))
169 } while (!time_after_eq(jiffies, deadline));
175 * Erase the whole flash memory
177 * Returns 0 if successful, non-zero otherwise.
179 static int erase_chip(struct m25p *flash)
181 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %lldKiB\n",
182 dev_name(&flash->spi->dev), __func__,
183 (long long)(flash->mtd.size >> 10));
185 /* Wait until finished previous write command. */
186 if (wait_till_ready(flash))
189 /* Send write enable, then erase commands. */
192 /* Set up command buffer. */
193 flash->command[0] = OPCODE_CHIP_ERASE;
195 spi_write(flash->spi, flash->command, 1);
200 static void m25p_addr2cmd(struct m25p *flash, unsigned int addr, u8 *cmd)
202 /* opcode is in cmd[0] */
203 cmd[1] = addr >> (flash->addr_width * 8 - 8);
204 cmd[2] = addr >> (flash->addr_width * 8 - 16);
205 cmd[3] = addr >> (flash->addr_width * 8 - 24);
208 static int m25p_cmdsz(struct m25p *flash)
210 return 1 + flash->addr_width;
214 * Erase one sector of flash memory at offset ``offset'' which is any
215 * address within the sector which should be erased.
217 * Returns 0 if successful, non-zero otherwise.
219 static int erase_sector(struct m25p *flash, u32 offset)
221 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
222 dev_name(&flash->spi->dev), __func__,
223 flash->mtd.erasesize / 1024, offset);
225 /* Wait until finished previous write command. */
226 if (wait_till_ready(flash))
229 /* Send write enable, then erase commands. */
232 /* Set up command buffer. */
233 flash->command[0] = flash->erase_opcode;
234 m25p_addr2cmd(flash, offset, flash->command);
236 spi_write(flash->spi, flash->command, m25p_cmdsz(flash));
241 /****************************************************************************/
248 * Erase an address range on the flash chip. The address range may extend
249 * one or more erase sectors. Return an error is there is a problem erasing.
251 static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
253 struct m25p *flash = mtd_to_m25p(mtd);
257 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%llx, len %lld\n",
258 dev_name(&flash->spi->dev), __func__, "at",
259 (long long)instr->addr, (long long)instr->len);
262 if (instr->addr + instr->len > flash->mtd.size)
264 div_u64_rem(instr->len, mtd->erasesize, &rem);
271 mutex_lock(&flash->lock);
273 /* whole-chip erase? */
274 if (len == flash->mtd.size) {
275 if (erase_chip(flash)) {
276 instr->state = MTD_ERASE_FAILED;
277 mutex_unlock(&flash->lock);
281 /* REVISIT in some cases we could speed up erasing large regions
282 * by using OPCODE_SE instead of OPCODE_BE_4K. We may have set up
283 * to use "small sector erase", but that's not always optimal.
286 /* "sector"-at-a-time erase */
289 if (erase_sector(flash, addr)) {
290 instr->state = MTD_ERASE_FAILED;
291 mutex_unlock(&flash->lock);
295 addr += mtd->erasesize;
296 len -= mtd->erasesize;
300 mutex_unlock(&flash->lock);
302 instr->state = MTD_ERASE_DONE;
303 mtd_erase_callback(instr);
309 * Read an address range from the flash chip. The address range
310 * may be any size provided it is within the physical boundaries.
312 static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
313 size_t *retlen, u_char *buf)
315 struct m25p *flash = mtd_to_m25p(mtd);
316 struct spi_transfer t[2];
317 struct spi_message m;
319 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
320 dev_name(&flash->spi->dev), __func__, "from",
327 if (from + len > flash->mtd.size)
330 spi_message_init(&m);
331 memset(t, 0, (sizeof t));
334 * OPCODE_FAST_READ (if available) is faster.
335 * Should add 1 byte DUMMY_BYTE.
337 t[0].tx_buf = flash->command;
338 t[0].len = m25p_cmdsz(flash) + FAST_READ_DUMMY_BYTE;
339 spi_message_add_tail(&t[0], &m);
343 spi_message_add_tail(&t[1], &m);
345 /* Byte count starts at zero. */
349 mutex_lock(&flash->lock);
351 /* Wait till previous write/erase is done. */
352 if (wait_till_ready(flash)) {
353 /* REVISIT status return?? */
354 mutex_unlock(&flash->lock);
358 /* FIXME switch to OPCODE_FAST_READ. It's required for higher
359 * clocks; and at this writing, every chip this driver handles
360 * supports that opcode.
363 /* Set up the write data buffer. */
364 flash->command[0] = OPCODE_READ;
365 m25p_addr2cmd(flash, from, flash->command);
367 spi_sync(flash->spi, &m);
369 *retlen = m.actual_length - m25p_cmdsz(flash) - FAST_READ_DUMMY_BYTE;
371 mutex_unlock(&flash->lock);
377 * Write an address range to the flash chip. Data must be written in
378 * FLASH_PAGESIZE chunks. The address range may be any size provided
379 * it is within the physical boundaries.
381 static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
382 size_t *retlen, const u_char *buf)
384 struct m25p *flash = mtd_to_m25p(mtd);
385 u32 page_offset, page_size;
386 struct spi_transfer t[2];
387 struct spi_message m;
389 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
390 dev_name(&flash->spi->dev), __func__, "to",
400 if (to + len > flash->mtd.size)
403 spi_message_init(&m);
404 memset(t, 0, (sizeof t));
406 t[0].tx_buf = flash->command;
407 t[0].len = m25p_cmdsz(flash);
408 spi_message_add_tail(&t[0], &m);
411 spi_message_add_tail(&t[1], &m);
413 mutex_lock(&flash->lock);
415 /* Wait until finished previous write command. */
416 if (wait_till_ready(flash)) {
417 mutex_unlock(&flash->lock);
423 /* Set up the opcode in the write buffer. */
424 flash->command[0] = OPCODE_PP;
425 m25p_addr2cmd(flash, to, flash->command);
427 page_offset = to & (flash->page_size - 1);
429 /* do all the bytes fit onto one page? */
430 if (page_offset + len <= flash->page_size) {
433 spi_sync(flash->spi, &m);
435 *retlen = m.actual_length - m25p_cmdsz(flash);
439 /* the size of data remaining on the first page */
440 page_size = flash->page_size - page_offset;
442 t[1].len = page_size;
443 spi_sync(flash->spi, &m);
445 *retlen = m.actual_length - m25p_cmdsz(flash);
447 /* write everything in flash->page_size chunks */
448 for (i = page_size; i < len; i += page_size) {
450 if (page_size > flash->page_size)
451 page_size = flash->page_size;
453 /* write the next page to flash */
454 m25p_addr2cmd(flash, to + i, flash->command);
456 t[1].tx_buf = buf + i;
457 t[1].len = page_size;
459 wait_till_ready(flash);
463 spi_sync(flash->spi, &m);
466 *retlen += m.actual_length - m25p_cmdsz(flash);
470 mutex_unlock(&flash->lock);
475 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
476 size_t *retlen, const u_char *buf)
478 struct m25p *flash = mtd_to_m25p(mtd);
479 struct spi_transfer t[2];
480 struct spi_message m;
491 if (to + len > flash->mtd.size)
494 spi_message_init(&m);
495 memset(t, 0, (sizeof t));
497 t[0].tx_buf = flash->command;
498 t[0].len = m25p_cmdsz(flash);
499 spi_message_add_tail(&t[0], &m);
502 spi_message_add_tail(&t[1], &m);
504 mutex_lock(&flash->lock);
506 /* Wait until finished previous write command. */
507 ret = wait_till_ready(flash);
514 /* Start write from odd address. */
516 flash->command[0] = OPCODE_BP;
517 m25p_addr2cmd(flash, to, flash->command);
519 /* write one byte. */
521 spi_sync(flash->spi, &m);
522 ret = wait_till_ready(flash);
525 *retlen += m.actual_length - m25p_cmdsz(flash);
529 flash->command[0] = OPCODE_AAI_WP;
530 m25p_addr2cmd(flash, to, flash->command);
532 /* Write out most of the data here. */
533 cmd_sz = m25p_cmdsz(flash);
534 for (; actual < len - 1; actual += 2) {
536 /* write two bytes. */
538 t[1].tx_buf = buf + actual;
540 spi_sync(flash->spi, &m);
541 ret = wait_till_ready(flash);
544 *retlen += m.actual_length - cmd_sz;
548 write_disable(flash);
549 ret = wait_till_ready(flash);
553 /* Write out trailing byte if it exists. */
556 flash->command[0] = OPCODE_BP;
557 m25p_addr2cmd(flash, to, flash->command);
558 t[0].len = m25p_cmdsz(flash);
560 t[1].tx_buf = buf + actual;
562 spi_sync(flash->spi, &m);
563 ret = wait_till_ready(flash);
566 *retlen += m.actual_length - m25p_cmdsz(flash);
567 write_disable(flash);
571 mutex_unlock(&flash->lock);
575 /****************************************************************************/
578 * SPI device driver setup and teardown
582 /* JEDEC id zero means "no ID" (most older chips); otherwise it has
583 * a high byte of zero plus three data bytes: the manufacturer id,
584 * then a two byte device id.
589 /* The size listed here is what works with OPCODE_SE, which isn't
590 * necessarily called a "sector" by the vendor.
592 unsigned sector_size;
599 #define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
600 #define M25P_NO_ERASE 0x02 /* No erase command needed */
603 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
604 ((kernel_ulong_t)&(struct flash_info) { \
605 .jedec_id = (_jedec_id), \
606 .ext_id = (_ext_id), \
607 .sector_size = (_sector_size), \
608 .n_sectors = (_n_sectors), \
614 #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width) \
615 ((kernel_ulong_t)&(struct flash_info) { \
616 .sector_size = (_sector_size), \
617 .n_sectors = (_n_sectors), \
618 .page_size = (_page_size), \
619 .addr_width = (_addr_width), \
620 .flags = M25P_NO_ERASE, \
623 /* NOTE: double check command sets and memory organization when you add
624 * more flash chips. This current list focusses on newer chips, which
625 * have been converging on command sets which including JEDEC ID.
627 static const struct spi_device_id m25p_ids[] = {
628 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
629 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
630 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
632 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
633 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
635 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
636 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
637 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
638 { "at26df321", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
641 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
642 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
643 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
644 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
645 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
647 /* Spansion -- single (large) sector size only, at least
648 * for the chips listed here (without boot sectors).
650 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
651 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
652 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
653 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
654 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
655 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
656 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
657 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
658 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
660 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
661 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K) },
662 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K) },
663 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K) },
664 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K) },
665 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K) },
666 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K) },
667 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K) },
668 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K) },
670 /* ST Microelectronics -- newer production may have feature updates */
671 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
672 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
673 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
674 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
675 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
676 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
677 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
678 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
679 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
681 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
682 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
683 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
685 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
686 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
688 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
689 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
690 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
691 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
692 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
693 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
694 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
695 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
697 /* Catalyst / On Semiconductor -- non-JEDEC */
698 { "cat25c11", CAT25_INFO( 16, 8, 16, 1) },
699 { "cat25c03", CAT25_INFO( 32, 8, 16, 2) },
700 { "cat25c09", CAT25_INFO( 128, 8, 32, 2) },
701 { "cat25c17", CAT25_INFO( 256, 8, 32, 2) },
702 { "cat25128", CAT25_INFO(2048, 8, 64, 2) },
705 MODULE_DEVICE_TABLE(spi, m25p_ids);
707 static const struct spi_device_id *__devinit jedec_probe(struct spi_device *spi)
710 u8 code = OPCODE_RDID;
714 struct flash_info *info;
716 /* JEDEC also defines an optional "extended device information"
717 * string for after vendor-specific data, after the three bytes
718 * we use here. Supporting some chips might require using it.
720 tmp = spi_write_then_read(spi, &code, 1, id, 5);
722 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
723 dev_name(&spi->dev), tmp);
733 * Some chips (like Numonyx M25P80) have JEDEC and non-JEDEC variants,
734 * which depend on technology process. Officially RDID command doesn't
735 * exist for non-JEDEC chips, but for compatibility they return ID 0.
740 ext_jedec = id[3] << 8 | id[4];
742 for (tmp = 0; tmp < ARRAY_SIZE(m25p_ids) - 1; tmp++) {
743 info = (void *)m25p_ids[tmp].driver_data;
744 if (info->jedec_id == jedec) {
745 if (info->ext_id != 0 && info->ext_id != ext_jedec)
747 return &m25p_ids[tmp];
755 * board specific setup should have ensured the SPI clock used here
756 * matches what the READ command supports, at least until this driver
757 * understands FAST_READ (for clocks over 25 MHz).
759 static int __devinit m25p_probe(struct spi_device *spi)
761 const struct spi_device_id *id = spi_get_device_id(spi);
762 struct flash_platform_data *data;
764 struct flash_info *info;
767 /* Platform data helps sort out which chip type we have, as
768 * well as how this board partitions it. If we don't have
769 * a chip ID, try the JEDEC id commands; they'll work for most
770 * newer chips, even if we don't recognize the particular chip.
772 data = spi->dev.platform_data;
773 if (data && data->type) {
774 const struct spi_device_id *plat_id;
776 for (i = 0; i < ARRAY_SIZE(m25p_ids) - 1; i++) {
777 plat_id = &m25p_ids[i];
778 if (strcmp(data->type, plat_id->name))
786 dev_warn(&spi->dev, "unrecognized id %s\n", data->type);
789 info = (void *)id->driver_data;
791 if (info->jedec_id) {
792 const struct spi_device_id *jid;
794 jid = jedec_probe(spi);
796 dev_info(&spi->dev, "non-JEDEC variant of %s\n",
798 } else if (jid != id) {
800 * JEDEC knows better, so overwrite platform ID. We
801 * can't trust partitions any longer, but we'll let
802 * mtd apply them anyway, since some partitions may be
803 * marked read-only, and we don't want to lose that
804 * information, even if it's not 100% accurate.
806 dev_warn(&spi->dev, "found %s, expected %s\n",
807 jid->name, id->name);
809 info = (void *)jid->driver_data;
813 flash = kzalloc(sizeof *flash, GFP_KERNEL);
816 flash->command = kmalloc(MAX_CMD_SIZE + FAST_READ_DUMMY_BYTE, GFP_KERNEL);
817 if (!flash->command) {
823 mutex_init(&flash->lock);
824 dev_set_drvdata(&spi->dev, flash);
827 * Atmel and SST serial flash tend to power
828 * up with the software protection bits set
831 if (info->jedec_id >> 16 == 0x1f ||
832 info->jedec_id >> 16 == 0xbf) {
837 if (data && data->name)
838 flash->mtd.name = data->name;
840 flash->mtd.name = dev_name(&spi->dev);
842 flash->mtd.type = MTD_NORFLASH;
843 flash->mtd.writesize = 1;
844 flash->mtd.flags = MTD_CAP_NORFLASH;
845 flash->mtd.size = info->sector_size * info->n_sectors;
846 flash->mtd.erase = m25p80_erase;
847 flash->mtd.read = m25p80_read;
849 /* sst flash chips use AAI word program */
850 if (info->jedec_id >> 16 == 0xbf)
851 flash->mtd.write = sst_write;
853 flash->mtd.write = m25p80_write;
855 /* prefer "small sector" erase if possible */
856 if (info->flags & SECT_4K) {
857 flash->erase_opcode = OPCODE_BE_4K;
858 flash->mtd.erasesize = 4096;
860 flash->erase_opcode = OPCODE_SE;
861 flash->mtd.erasesize = info->sector_size;
864 if (info->flags & M25P_NO_ERASE)
865 flash->mtd.flags |= MTD_NO_ERASE;
867 flash->mtd.dev.parent = &spi->dev;
868 flash->page_size = info->page_size;
869 flash->addr_width = info->addr_width;
871 dev_info(&spi->dev, "%s (%lld Kbytes)\n", id->name,
872 (long long)flash->mtd.size >> 10);
874 DEBUG(MTD_DEBUG_LEVEL2,
875 "mtd .name = %s, .size = 0x%llx (%lldMiB) "
876 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
878 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
879 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
880 flash->mtd.numeraseregions);
882 if (flash->mtd.numeraseregions)
883 for (i = 0; i < flash->mtd.numeraseregions; i++)
884 DEBUG(MTD_DEBUG_LEVEL2,
885 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
886 ".erasesize = 0x%.8x (%uKiB), "
887 ".numblocks = %d }\n",
888 i, (long long)flash->mtd.eraseregions[i].offset,
889 flash->mtd.eraseregions[i].erasesize,
890 flash->mtd.eraseregions[i].erasesize / 1024,
891 flash->mtd.eraseregions[i].numblocks);
894 /* partitions should match sector boundaries; and it may be good to
895 * use readonly partitions for writeprotected sectors (BP2..BP0).
897 if (mtd_has_partitions()) {
898 struct mtd_partition *parts = NULL;
901 if (mtd_has_cmdlinepart()) {
902 static const char *part_probes[]
903 = { "cmdlinepart", NULL, };
905 nr_parts = parse_mtd_partitions(&flash->mtd,
906 part_probes, &parts, 0);
909 if (nr_parts <= 0 && data && data->parts) {
911 nr_parts = data->nr_parts;
915 for (i = 0; i < nr_parts; i++) {
916 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
917 "{.name = %s, .offset = 0x%llx, "
918 ".size = 0x%llx (%lldKiB) }\n",
920 (long long)parts[i].offset,
921 (long long)parts[i].size,
922 (long long)(parts[i].size >> 10));
924 flash->partitioned = 1;
925 return add_mtd_partitions(&flash->mtd, parts, nr_parts);
927 } else if (data && data->nr_parts)
928 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
929 data->nr_parts, data->name);
931 return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
935 static int __devexit m25p_remove(struct spi_device *spi)
937 struct m25p *flash = dev_get_drvdata(&spi->dev);
940 /* Clean up MTD stuff. */
941 if (mtd_has_partitions() && flash->partitioned)
942 status = del_mtd_partitions(&flash->mtd);
944 status = del_mtd_device(&flash->mtd);
946 kfree(flash->command);
953 static struct spi_driver m25p80_driver = {
956 .bus = &spi_bus_type,
957 .owner = THIS_MODULE,
959 .id_table = m25p_ids,
961 .remove = __devexit_p(m25p_remove),
963 /* REVISIT: many of these chips have deep power-down modes, which
964 * should clearly be entered on suspend() to minimize power use.
965 * And also when they're otherwise idle...
970 static int __init m25p80_init(void)
972 return spi_register_driver(&m25p80_driver);
976 static void __exit m25p80_exit(void)
978 spi_unregister_driver(&m25p80_driver);
982 module_init(m25p80_init);
983 module_exit(m25p80_exit);
985 MODULE_LICENSE("GPL");
986 MODULE_AUTHOR("Mike Lavender");
987 MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");