GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / mtd / devices / m25p80.c
blobe3eead9963f32ce48811d8be20548496a40bf47d
1 /*
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/err.h>
20 #include <linux/errno.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/interrupt.h>
24 #include <linux/mutex.h>
25 #include <linux/math64.h>
26 #include <linux/slab.h>
27 #include <linux/sched.h>
28 #include <linux/mod_devicetable.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/partitions.h>
33 #include <linux/spi/spi.h>
34 #include <linux/spi/flash.h>
36 /* Flash opcodes. */
37 #define OPCODE_WREN 0x06 /* Write enable */
38 #define OPCODE_RDSR 0x05 /* Read status register */
39 #define OPCODE_WRSR 0x01 /* Write status register 1 byte */
40 #define OPCODE_NORM_READ 0x03 /* Read data bytes (low frequency) */
41 #define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */
42 #define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */
43 #define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
44 #define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
45 #define OPCODE_CHIP_ERASE 0xc7 /* Erase whole flash chip */
46 #define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
47 #define OPCODE_RDID 0x9f /* Read JEDEC ID */
49 /* Used for SST flashes only. */
50 #define OPCODE_BP 0x02 /* Byte program */
51 #define OPCODE_WRDI 0x04 /* Write disable */
52 #define OPCODE_AAI_WP 0xad /* Auto address increment word program */
54 /* Status Register bits. */
55 #define SR_WIP 1 /* Write in progress */
56 #define SR_WEL 2 /* Write enable latch */
57 /* meaning of other SR_* bits may differ between vendors */
58 #define SR_BP0 4 /* Block protect 0 */
59 #define SR_BP1 8 /* Block protect 1 */
60 #define SR_BP2 0x10 /* Block protect 2 */
61 #define SR_SRWD 0x80 /* SR write protect */
63 /* Define max times to check status register before we give up. */
64 #define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
65 #define MAX_CMD_SIZE 4
67 #ifdef CONFIG_M25PXX_USE_FAST_READ
68 #define OPCODE_READ OPCODE_FAST_READ
69 #define FAST_READ_DUMMY_BYTE 1
70 #else
71 #define OPCODE_READ OPCODE_NORM_READ
72 #define FAST_READ_DUMMY_BYTE 0
73 #endif
75 /****************************************************************************/
77 struct m25p {
78 struct spi_device *spi;
79 struct mutex lock;
80 struct mtd_info mtd;
81 unsigned partitioned:1;
82 u16 page_size;
83 u16 addr_width;
84 u8 erase_opcode;
85 u8 *command;
88 static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
90 return container_of(mtd, struct m25p, mtd);
93 /****************************************************************************/
96 * Internal helper functions
100 * Read the status register, returning its value in the location
101 * Return the status register value.
102 * Returns negative if error occurred.
104 static int read_sr(struct m25p *flash)
106 ssize_t retval;
107 u8 code = OPCODE_RDSR;
108 u8 val;
110 retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
112 if (retval < 0) {
113 dev_err(&flash->spi->dev, "error %d reading SR\n",
114 (int) retval);
115 return retval;
118 return val;
122 * Write status register 1 byte
123 * Returns negative if error occurred.
125 static int write_sr(struct m25p *flash, u8 val)
127 flash->command[0] = OPCODE_WRSR;
128 flash->command[1] = val;
130 return spi_write(flash->spi, flash->command, 2);
134 * Set write enable latch with Write Enable command.
135 * Returns negative if error occurred.
137 static inline int write_enable(struct m25p *flash)
139 u8 code = OPCODE_WREN;
141 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
145 * Send write disble instruction to the chip.
147 static inline int write_disable(struct m25p *flash)
149 u8 code = OPCODE_WRDI;
151 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
155 * Service routine to read status register until ready, or timeout occurs.
156 * Returns non-zero if error.
158 static int wait_till_ready(struct m25p *flash)
160 unsigned long deadline;
161 int sr;
163 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
165 do {
166 if ((sr = read_sr(flash)) < 0)
167 break;
168 else if (!(sr & SR_WIP))
169 return 0;
171 cond_resched();
173 } while (!time_after_eq(jiffies, deadline));
175 return 1;
179 * Erase the whole flash memory
181 * Returns 0 if successful, non-zero otherwise.
183 static int erase_chip(struct m25p *flash)
185 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %lldKiB\n",
186 dev_name(&flash->spi->dev), __func__,
187 (long long)(flash->mtd.size >> 10));
189 /* Wait until finished previous write command. */
190 if (wait_till_ready(flash))
191 return 1;
193 /* Send write enable, then erase commands. */
194 write_enable(flash);
196 /* Set up command buffer. */
197 flash->command[0] = OPCODE_CHIP_ERASE;
199 spi_write(flash->spi, flash->command, 1);
201 return 0;
204 static void m25p_addr2cmd(struct m25p *flash, unsigned int addr, u8 *cmd)
206 /* opcode is in cmd[0] */
207 cmd[1] = addr >> (flash->addr_width * 8 - 8);
208 cmd[2] = addr >> (flash->addr_width * 8 - 16);
209 cmd[3] = addr >> (flash->addr_width * 8 - 24);
212 static int m25p_cmdsz(struct m25p *flash)
214 return 1 + flash->addr_width;
218 * Erase one sector of flash memory at offset ``offset'' which is any
219 * address within the sector which should be erased.
221 * Returns 0 if successful, non-zero otherwise.
223 static int erase_sector(struct m25p *flash, u32 offset)
225 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
226 dev_name(&flash->spi->dev), __func__,
227 flash->mtd.erasesize / 1024, offset);
229 /* Wait until finished previous write command. */
230 if (wait_till_ready(flash))
231 return 1;
233 /* Send write enable, then erase commands. */
234 write_enable(flash);
236 /* Set up command buffer. */
237 flash->command[0] = flash->erase_opcode;
238 m25p_addr2cmd(flash, offset, flash->command);
240 spi_write(flash->spi, flash->command, m25p_cmdsz(flash));
242 return 0;
245 /****************************************************************************/
248 * MTD implementation
252 * Erase an address range on the flash chip. The address range may extend
253 * one or more erase sectors. Return an error is there is a problem erasing.
255 static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
257 struct m25p *flash = mtd_to_m25p(mtd);
258 u32 addr,len;
259 uint32_t rem;
261 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%llx, len %lld\n",
262 dev_name(&flash->spi->dev), __func__, "at",
263 (long long)instr->addr, (long long)instr->len);
265 /* sanity checks */
266 if (instr->addr + instr->len > flash->mtd.size)
267 return -EINVAL;
268 div_u64_rem(instr->len, mtd->erasesize, &rem);
269 if (rem)
270 return -EINVAL;
272 addr = instr->addr;
273 len = instr->len;
275 mutex_lock(&flash->lock);
277 /* whole-chip erase? */
278 if (len == flash->mtd.size) {
279 if (erase_chip(flash)) {
280 instr->state = MTD_ERASE_FAILED;
281 mutex_unlock(&flash->lock);
282 return -EIO;
285 /* REVISIT in some cases we could speed up erasing large regions
286 * by using OPCODE_SE instead of OPCODE_BE_4K. We may have set up
287 * to use "small sector erase", but that's not always optimal.
290 /* "sector"-at-a-time erase */
291 } else {
292 while (len) {
293 if (erase_sector(flash, addr)) {
294 instr->state = MTD_ERASE_FAILED;
295 mutex_unlock(&flash->lock);
296 return -EIO;
299 addr += mtd->erasesize;
300 len -= mtd->erasesize;
304 mutex_unlock(&flash->lock);
306 instr->state = MTD_ERASE_DONE;
307 mtd_erase_callback(instr);
309 return 0;
313 * Read an address range from the flash chip. The address range
314 * may be any size provided it is within the physical boundaries.
316 static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
317 size_t *retlen, u_char *buf)
319 struct m25p *flash = mtd_to_m25p(mtd);
320 struct spi_transfer t[2];
321 struct spi_message m;
323 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
324 dev_name(&flash->spi->dev), __func__, "from",
325 (u32)from, len);
327 /* sanity checks */
328 if (!len)
329 return 0;
331 if (from + len > flash->mtd.size)
332 return -EINVAL;
334 spi_message_init(&m);
335 memset(t, 0, (sizeof t));
337 /* NOTE:
338 * OPCODE_FAST_READ (if available) is faster.
339 * Should add 1 byte DUMMY_BYTE.
341 t[0].tx_buf = flash->command;
342 t[0].len = m25p_cmdsz(flash) + FAST_READ_DUMMY_BYTE;
343 spi_message_add_tail(&t[0], &m);
345 t[1].rx_buf = buf;
346 t[1].len = len;
347 spi_message_add_tail(&t[1], &m);
349 /* Byte count starts at zero. */
350 *retlen = 0;
352 mutex_lock(&flash->lock);
354 /* Wait till previous write/erase is done. */
355 if (wait_till_ready(flash)) {
356 /* REVISIT status return?? */
357 mutex_unlock(&flash->lock);
358 return 1;
362 /* Set up the write data buffer. */
363 flash->command[0] = OPCODE_READ;
364 m25p_addr2cmd(flash, from, flash->command);
366 spi_sync(flash->spi, &m);
368 *retlen = m.actual_length - m25p_cmdsz(flash) - FAST_READ_DUMMY_BYTE;
370 mutex_unlock(&flash->lock);
372 return 0;
376 * Write an address range to the flash chip. Data must be written in
377 * FLASH_PAGESIZE chunks. The address range may be any size provided
378 * it is within the physical boundaries.
380 static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
381 size_t *retlen, const u_char *buf)
383 struct m25p *flash = mtd_to_m25p(mtd);
384 u32 page_offset, page_size;
385 struct spi_transfer t[2];
386 struct spi_message m;
388 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
389 dev_name(&flash->spi->dev), __func__, "to",
390 (u32)to, len);
392 *retlen = 0;
394 /* sanity checks */
395 if (!len)
396 return(0);
398 if (to + len > flash->mtd.size)
399 return -EINVAL;
401 spi_message_init(&m);
402 memset(t, 0, (sizeof t));
404 t[0].tx_buf = flash->command;
405 t[0].len = m25p_cmdsz(flash);
406 spi_message_add_tail(&t[0], &m);
408 t[1].tx_buf = buf;
409 spi_message_add_tail(&t[1], &m);
411 mutex_lock(&flash->lock);
413 /* Wait until finished previous write command. */
414 if (wait_till_ready(flash)) {
415 mutex_unlock(&flash->lock);
416 return 1;
419 write_enable(flash);
421 /* Set up the opcode in the write buffer. */
422 flash->command[0] = OPCODE_PP;
423 m25p_addr2cmd(flash, to, flash->command);
425 page_offset = to & (flash->page_size - 1);
427 /* do all the bytes fit onto one page? */
428 if (page_offset + len <= flash->page_size) {
429 t[1].len = len;
431 spi_sync(flash->spi, &m);
433 *retlen = m.actual_length - m25p_cmdsz(flash);
434 } else {
435 u32 i;
437 /* the size of data remaining on the first page */
438 page_size = flash->page_size - page_offset;
440 t[1].len = page_size;
441 spi_sync(flash->spi, &m);
443 *retlen = m.actual_length - m25p_cmdsz(flash);
445 /* write everything in flash->page_size chunks */
446 for (i = page_size; i < len; i += page_size) {
447 page_size = len - i;
448 if (page_size > flash->page_size)
449 page_size = flash->page_size;
451 /* write the next page to flash */
452 m25p_addr2cmd(flash, to + i, flash->command);
454 t[1].tx_buf = buf + i;
455 t[1].len = page_size;
457 wait_till_ready(flash);
459 write_enable(flash);
461 spi_sync(flash->spi, &m);
463 *retlen += m.actual_length - m25p_cmdsz(flash);
467 mutex_unlock(&flash->lock);
469 return 0;
472 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
473 size_t *retlen, const u_char *buf)
475 struct m25p *flash = mtd_to_m25p(mtd);
476 struct spi_transfer t[2];
477 struct spi_message m;
478 size_t actual;
479 int cmd_sz, ret;
481 *retlen = 0;
483 /* sanity checks */
484 if (!len)
485 return 0;
487 if (to + len > flash->mtd.size)
488 return -EINVAL;
490 spi_message_init(&m);
491 memset(t, 0, (sizeof t));
493 t[0].tx_buf = flash->command;
494 t[0].len = m25p_cmdsz(flash);
495 spi_message_add_tail(&t[0], &m);
497 t[1].tx_buf = buf;
498 spi_message_add_tail(&t[1], &m);
500 mutex_lock(&flash->lock);
502 /* Wait until finished previous write command. */
503 ret = wait_till_ready(flash);
504 if (ret)
505 goto time_out;
507 write_enable(flash);
509 actual = to % 2;
510 /* Start write from odd address. */
511 if (actual) {
512 flash->command[0] = OPCODE_BP;
513 m25p_addr2cmd(flash, to, flash->command);
515 /* write one byte. */
516 t[1].len = 1;
517 spi_sync(flash->spi, &m);
518 ret = wait_till_ready(flash);
519 if (ret)
520 goto time_out;
521 *retlen += m.actual_length - m25p_cmdsz(flash);
523 to += actual;
525 flash->command[0] = OPCODE_AAI_WP;
526 m25p_addr2cmd(flash, to, flash->command);
528 /* Write out most of the data here. */
529 cmd_sz = m25p_cmdsz(flash);
530 for (; actual < len - 1; actual += 2) {
531 t[0].len = cmd_sz;
532 /* write two bytes. */
533 t[1].len = 2;
534 t[1].tx_buf = buf + actual;
536 spi_sync(flash->spi, &m);
537 ret = wait_till_ready(flash);
538 if (ret)
539 goto time_out;
540 *retlen += m.actual_length - cmd_sz;
541 cmd_sz = 1;
542 to += 2;
544 write_disable(flash);
545 ret = wait_till_ready(flash);
546 if (ret)
547 goto time_out;
549 /* Write out trailing byte if it exists. */
550 if (actual != len) {
551 write_enable(flash);
552 flash->command[0] = OPCODE_BP;
553 m25p_addr2cmd(flash, to, flash->command);
554 t[0].len = m25p_cmdsz(flash);
555 t[1].len = 1;
556 t[1].tx_buf = buf + actual;
558 spi_sync(flash->spi, &m);
559 ret = wait_till_ready(flash);
560 if (ret)
561 goto time_out;
562 *retlen += m.actual_length - m25p_cmdsz(flash);
563 write_disable(flash);
566 time_out:
567 mutex_unlock(&flash->lock);
568 return ret;
571 /****************************************************************************/
574 * SPI device driver setup and teardown
577 struct flash_info {
578 /* JEDEC id zero means "no ID" (most older chips); otherwise it has
579 * a high byte of zero plus three data bytes: the manufacturer id,
580 * then a two byte device id.
582 u32 jedec_id;
583 u16 ext_id;
585 /* The size listed here is what works with OPCODE_SE, which isn't
586 * necessarily called a "sector" by the vendor.
588 unsigned sector_size;
589 u16 n_sectors;
591 u16 page_size;
592 u16 addr_width;
594 u16 flags;
595 #define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
596 #define M25P_NO_ERASE 0x02 /* No erase command needed */
599 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
600 ((kernel_ulong_t)&(struct flash_info) { \
601 .jedec_id = (_jedec_id), \
602 .ext_id = (_ext_id), \
603 .sector_size = (_sector_size), \
604 .n_sectors = (_n_sectors), \
605 .page_size = 256, \
606 .addr_width = 3, \
607 .flags = (_flags), \
610 #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width) \
611 ((kernel_ulong_t)&(struct flash_info) { \
612 .sector_size = (_sector_size), \
613 .n_sectors = (_n_sectors), \
614 .page_size = (_page_size), \
615 .addr_width = (_addr_width), \
616 .flags = M25P_NO_ERASE, \
619 /* NOTE: double check command sets and memory organization when you add
620 * more flash chips. This current list focusses on newer chips, which
621 * have been converging on command sets which including JEDEC ID.
623 static const struct spi_device_id m25p_ids[] = {
624 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
625 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
626 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
628 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
629 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
631 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
632 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
633 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
634 { "at26df321", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
636 /* EON -- en25pxx */
637 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
638 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
640 /* Intel/Numonyx -- xxxs33b */
641 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
642 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
643 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
645 /* Macronix */
646 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
647 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
648 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
649 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
650 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
651 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
653 /* Spansion -- single (large) sector size only, at least
654 * for the chips listed here (without boot sectors).
656 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
657 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
658 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
659 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
660 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
661 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
662 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
663 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
664 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
666 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
667 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K) },
668 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K) },
669 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K) },
670 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K) },
671 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K) },
672 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K) },
673 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K) },
674 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K) },
676 /* ST Microelectronics -- newer production may have feature updates */
677 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
678 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
679 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
680 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
681 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
682 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
683 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
684 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
685 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
687 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
688 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
689 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
690 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
691 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
692 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
693 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
694 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
695 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
697 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
698 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
699 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
701 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
702 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
704 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
705 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
706 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
707 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
708 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
709 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
710 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
711 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
712 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
714 /* Catalyst / On Semiconductor -- non-JEDEC */
715 { "cat25c11", CAT25_INFO( 16, 8, 16, 1) },
716 { "cat25c03", CAT25_INFO( 32, 8, 16, 2) },
717 { "cat25c09", CAT25_INFO( 128, 8, 32, 2) },
718 { "cat25c17", CAT25_INFO( 256, 8, 32, 2) },
719 { "cat25128", CAT25_INFO(2048, 8, 64, 2) },
720 { },
722 MODULE_DEVICE_TABLE(spi, m25p_ids);
724 static const struct spi_device_id *__devinit jedec_probe(struct spi_device *spi)
726 int tmp;
727 u8 code = OPCODE_RDID;
728 u8 id[5];
729 u32 jedec;
730 u16 ext_jedec;
731 struct flash_info *info;
733 /* JEDEC also defines an optional "extended device information"
734 * string for after vendor-specific data, after the three bytes
735 * we use here. Supporting some chips might require using it.
737 tmp = spi_write_then_read(spi, &code, 1, id, 5);
738 if (tmp < 0) {
739 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
740 dev_name(&spi->dev), tmp);
741 return ERR_PTR(tmp);
743 jedec = id[0];
744 jedec = jedec << 8;
745 jedec |= id[1];
746 jedec = jedec << 8;
747 jedec |= id[2];
749 ext_jedec = id[3] << 8 | id[4];
751 for (tmp = 0; tmp < ARRAY_SIZE(m25p_ids) - 1; tmp++) {
752 info = (void *)m25p_ids[tmp].driver_data;
753 if (info->jedec_id == jedec) {
754 if (info->ext_id != 0 && info->ext_id != ext_jedec)
755 continue;
756 return &m25p_ids[tmp];
759 return ERR_PTR(-ENODEV);
764 * board specific setup should have ensured the SPI clock used here
765 * matches what the READ command supports, at least until this driver
766 * understands FAST_READ (for clocks over 25 MHz).
768 static int __devinit m25p_probe(struct spi_device *spi)
770 const struct spi_device_id *id = spi_get_device_id(spi);
771 struct flash_platform_data *data;
772 struct m25p *flash;
773 struct flash_info *info;
774 unsigned i;
776 /* Platform data helps sort out which chip type we have, as
777 * well as how this board partitions it. If we don't have
778 * a chip ID, try the JEDEC id commands; they'll work for most
779 * newer chips, even if we don't recognize the particular chip.
781 data = spi->dev.platform_data;
782 if (data && data->type) {
783 const struct spi_device_id *plat_id;
785 for (i = 0; i < ARRAY_SIZE(m25p_ids) - 1; i++) {
786 plat_id = &m25p_ids[i];
787 if (strcmp(data->type, plat_id->name))
788 continue;
789 break;
792 if (i < ARRAY_SIZE(m25p_ids) - 1)
793 id = plat_id;
794 else
795 dev_warn(&spi->dev, "unrecognized id %s\n", data->type);
798 info = (void *)id->driver_data;
800 if (info->jedec_id) {
801 const struct spi_device_id *jid;
803 jid = jedec_probe(spi);
804 if (IS_ERR(jid)) {
805 return PTR_ERR(jid);
806 } else if (jid != id) {
808 * JEDEC knows better, so overwrite platform ID. We
809 * can't trust partitions any longer, but we'll let
810 * mtd apply them anyway, since some partitions may be
811 * marked read-only, and we don't want to lose that
812 * information, even if it's not 100% accurate.
814 dev_warn(&spi->dev, "found %s, expected %s\n",
815 jid->name, id->name);
816 id = jid;
817 info = (void *)jid->driver_data;
821 flash = kzalloc(sizeof *flash, GFP_KERNEL);
822 if (!flash)
823 return -ENOMEM;
824 flash->command = kmalloc(MAX_CMD_SIZE + FAST_READ_DUMMY_BYTE, GFP_KERNEL);
825 if (!flash->command) {
826 kfree(flash);
827 return -ENOMEM;
830 flash->spi = spi;
831 mutex_init(&flash->lock);
832 dev_set_drvdata(&spi->dev, flash);
835 * Atmel, SST and Intel/Numonyx serial flash tend to power
836 * up with the software protection bits set
839 if (info->jedec_id >> 16 == 0x1f ||
840 info->jedec_id >> 16 == 0x89 ||
841 info->jedec_id >> 16 == 0xbf) {
842 write_enable(flash);
843 write_sr(flash, 0);
846 if (data && data->name)
847 flash->mtd.name = data->name;
848 else
849 flash->mtd.name = dev_name(&spi->dev);
851 flash->mtd.type = MTD_NORFLASH;
852 flash->mtd.writesize = 1;
853 flash->mtd.flags = MTD_CAP_NORFLASH;
854 flash->mtd.size = info->sector_size * info->n_sectors;
855 flash->mtd.erase = m25p80_erase;
856 flash->mtd.read = m25p80_read;
858 /* sst flash chips use AAI word program */
859 if (info->jedec_id >> 16 == 0xbf)
860 flash->mtd.write = sst_write;
861 else
862 flash->mtd.write = m25p80_write;
864 /* prefer "small sector" erase if possible */
865 if (info->flags & SECT_4K) {
866 flash->erase_opcode = OPCODE_BE_4K;
867 flash->mtd.erasesize = 4096;
868 } else {
869 flash->erase_opcode = OPCODE_SE;
870 flash->mtd.erasesize = info->sector_size;
873 if (info->flags & M25P_NO_ERASE)
874 flash->mtd.flags |= MTD_NO_ERASE;
876 flash->mtd.dev.parent = &spi->dev;
877 flash->page_size = info->page_size;
878 flash->addr_width = info->addr_width;
880 dev_info(&spi->dev, "%s (%lld Kbytes)\n", id->name,
881 (long long)flash->mtd.size >> 10);
883 DEBUG(MTD_DEBUG_LEVEL2,
884 "mtd .name = %s, .size = 0x%llx (%lldMiB) "
885 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
886 flash->mtd.name,
887 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
888 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
889 flash->mtd.numeraseregions);
891 if (flash->mtd.numeraseregions)
892 for (i = 0; i < flash->mtd.numeraseregions; i++)
893 DEBUG(MTD_DEBUG_LEVEL2,
894 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
895 ".erasesize = 0x%.8x (%uKiB), "
896 ".numblocks = %d }\n",
897 i, (long long)flash->mtd.eraseregions[i].offset,
898 flash->mtd.eraseregions[i].erasesize,
899 flash->mtd.eraseregions[i].erasesize / 1024,
900 flash->mtd.eraseregions[i].numblocks);
903 /* partitions should match sector boundaries; and it may be good to
904 * use readonly partitions for writeprotected sectors (BP2..BP0).
906 if (mtd_has_partitions()) {
907 struct mtd_partition *parts = NULL;
908 int nr_parts = 0;
910 if (mtd_has_cmdlinepart()) {
911 static const char *part_probes[]
912 = { "cmdlinepart", NULL, };
914 nr_parts = parse_mtd_partitions(&flash->mtd,
915 part_probes, &parts, 0);
918 if (nr_parts <= 0 && data && data->parts) {
919 parts = data->parts;
920 nr_parts = data->nr_parts;
923 if (nr_parts > 0) {
924 for (i = 0; i < nr_parts; i++) {
925 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
926 "{.name = %s, .offset = 0x%llx, "
927 ".size = 0x%llx (%lldKiB) }\n",
928 i, parts[i].name,
929 (long long)parts[i].offset,
930 (long long)parts[i].size,
931 (long long)(parts[i].size >> 10));
933 flash->partitioned = 1;
934 return add_mtd_partitions(&flash->mtd, parts, nr_parts);
936 } else if (data && data->nr_parts)
937 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
938 data->nr_parts, data->name);
940 return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
944 static int __devexit m25p_remove(struct spi_device *spi)
946 struct m25p *flash = dev_get_drvdata(&spi->dev);
947 int status;
949 /* Clean up MTD stuff. */
950 if (mtd_has_partitions() && flash->partitioned)
951 status = del_mtd_partitions(&flash->mtd);
952 else
953 status = del_mtd_device(&flash->mtd);
954 if (status == 0) {
955 kfree(flash->command);
956 kfree(flash);
958 return 0;
962 static struct spi_driver m25p80_driver = {
963 .driver = {
964 .name = "m25p80",
965 .bus = &spi_bus_type,
966 .owner = THIS_MODULE,
968 .id_table = m25p_ids,
969 .probe = m25p_probe,
970 .remove = __devexit_p(m25p_remove),
972 /* REVISIT: many of these chips have deep power-down modes, which
973 * should clearly be entered on suspend() to minimize power use.
974 * And also when they're otherwise idle...
979 static int __init m25p80_init(void)
981 return spi_register_driver(&m25p80_driver);
985 static void __exit m25p80_exit(void)
987 spi_unregister_driver(&m25p80_driver);
991 module_init(m25p80_init);
992 module_exit(m25p80_exit);
994 MODULE_LICENSE("GPL");
995 MODULE_AUTHOR("Mike Lavender");
996 MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");