mtd: m25p80: Add support for CAT25xxx serial EEPROMs
[safe/jmp/linux-2.6] / drivers / mtd / devices / m25p80.c
1 /*
2  * MTD SPI driver for ST M25Pxx (and similar) serial flash chips
3  *
4  * Author: Mike Lavender, mike@steroidmicros.com
5  *
6  * Copyright (c) 2005, Intec Automation Inc.
7  *
8  * Some parts are based on lart.c by Abraham Van Der Merwe
9  *
10  * Cleaned up and generalized based on mtd_dataflash.c
11  *
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.
15  *
16  */
17
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>
25
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/partitions.h>
28
29 #include <linux/spi/spi.h>
30 #include <linux/spi/flash.h>
31
32 /* Flash opcodes. */
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 */
44
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 */
49
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 */
58
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
62
63 #ifdef CONFIG_M25PXX_USE_FAST_READ
64 #define OPCODE_READ     OPCODE_FAST_READ
65 #define FAST_READ_DUMMY_BYTE 1
66 #else
67 #define OPCODE_READ     OPCODE_NORM_READ
68 #define FAST_READ_DUMMY_BYTE 0
69 #endif
70
71 /****************************************************************************/
72
73 struct m25p {
74         struct spi_device       *spi;
75         struct mutex            lock;
76         struct mtd_info         mtd;
77         unsigned                partitioned:1;
78         u16                     page_size;
79         u16                     addr_width;
80         u8                      erase_opcode;
81         u8                      *command;
82 };
83
84 static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
85 {
86         return container_of(mtd, struct m25p, mtd);
87 }
88
89 /****************************************************************************/
90
91 /*
92  * Internal helper functions
93  */
94
95 /*
96  * Read the status register, returning its value in the location
97  * Return the status register value.
98  * Returns negative if error occurred.
99  */
100 static int read_sr(struct m25p *flash)
101 {
102         ssize_t retval;
103         u8 code = OPCODE_RDSR;
104         u8 val;
105
106         retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
107
108         if (retval < 0) {
109                 dev_err(&flash->spi->dev, "error %d reading SR\n",
110                                 (int) retval);
111                 return retval;
112         }
113
114         return val;
115 }
116
117 /*
118  * Write status register 1 byte
119  * Returns negative if error occurred.
120  */
121 static int write_sr(struct m25p *flash, u8 val)
122 {
123         flash->command[0] = OPCODE_WRSR;
124         flash->command[1] = val;
125
126         return spi_write(flash->spi, flash->command, 2);
127 }
128
129 /*
130  * Set write enable latch with Write Enable command.
131  * Returns negative if error occurred.
132  */
133 static inline int write_enable(struct m25p *flash)
134 {
135         u8      code = OPCODE_WREN;
136
137         return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
138 }
139
140 /*
141  * Send write disble instruction to the chip.
142  */
143 static inline int write_disable(struct m25p *flash)
144 {
145         u8      code = OPCODE_WRDI;
146
147         return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
148 }
149
150 /*
151  * Service routine to read status register until ready, or timeout occurs.
152  * Returns non-zero if error.
153  */
154 static int wait_till_ready(struct m25p *flash)
155 {
156         unsigned long deadline;
157         int sr;
158
159         deadline = jiffies + MAX_READY_WAIT_JIFFIES;
160
161         do {
162                 if ((sr = read_sr(flash)) < 0)
163                         break;
164                 else if (!(sr & SR_WIP))
165                         return 0;
166
167                 cond_resched();
168
169         } while (!time_after_eq(jiffies, deadline));
170
171         return 1;
172 }
173
174 /*
175  * Erase the whole flash memory
176  *
177  * Returns 0 if successful, non-zero otherwise.
178  */
179 static int erase_chip(struct m25p *flash)
180 {
181         DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %lldKiB\n",
182               dev_name(&flash->spi->dev), __func__,
183               (long long)(flash->mtd.size >> 10));
184
185         /* Wait until finished previous write command. */
186         if (wait_till_ready(flash))
187                 return 1;
188
189         /* Send write enable, then erase commands. */
190         write_enable(flash);
191
192         /* Set up command buffer. */
193         flash->command[0] = OPCODE_CHIP_ERASE;
194
195         spi_write(flash->spi, flash->command, 1);
196
197         return 0;
198 }
199
200 static void m25p_addr2cmd(struct m25p *flash, unsigned int addr, u8 *cmd)
201 {
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);
206 }
207
208 static int m25p_cmdsz(struct m25p *flash)
209 {
210         return 1 + flash->addr_width;
211 }
212
213 /*
214  * Erase one sector of flash memory at offset ``offset'' which is any
215  * address within the sector which should be erased.
216  *
217  * Returns 0 if successful, non-zero otherwise.
218  */
219 static int erase_sector(struct m25p *flash, u32 offset)
220 {
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);
224
225         /* Wait until finished previous write command. */
226         if (wait_till_ready(flash))
227                 return 1;
228
229         /* Send write enable, then erase commands. */
230         write_enable(flash);
231
232         /* Set up command buffer. */
233         flash->command[0] = flash->erase_opcode;
234         m25p_addr2cmd(flash, offset, flash->command);
235
236         spi_write(flash->spi, flash->command, m25p_cmdsz(flash));
237
238         return 0;
239 }
240
241 /****************************************************************************/
242
243 /*
244  * MTD implementation
245  */
246
247 /*
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.
250  */
251 static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
252 {
253         struct m25p *flash = mtd_to_m25p(mtd);
254         u32 addr,len;
255         uint32_t rem;
256
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);
260
261         /* sanity checks */
262         if (instr->addr + instr->len > flash->mtd.size)
263                 return -EINVAL;
264         div_u64_rem(instr->len, mtd->erasesize, &rem);
265         if (rem)
266                 return -EINVAL;
267
268         addr = instr->addr;
269         len = instr->len;
270
271         mutex_lock(&flash->lock);
272
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);
278                         return -EIO;
279                 }
280
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.
284          */
285
286         /* "sector"-at-a-time erase */
287         } else {
288                 while (len) {
289                         if (erase_sector(flash, addr)) {
290                                 instr->state = MTD_ERASE_FAILED;
291                                 mutex_unlock(&flash->lock);
292                                 return -EIO;
293                         }
294
295                         addr += mtd->erasesize;
296                         len -= mtd->erasesize;
297                 }
298         }
299
300         mutex_unlock(&flash->lock);
301
302         instr->state = MTD_ERASE_DONE;
303         mtd_erase_callback(instr);
304
305         return 0;
306 }
307
308 /*
309  * Read an address range from the flash chip.  The address range
310  * may be any size provided it is within the physical boundaries.
311  */
312 static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
313         size_t *retlen, u_char *buf)
314 {
315         struct m25p *flash = mtd_to_m25p(mtd);
316         struct spi_transfer t[2];
317         struct spi_message m;
318
319         DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
320                         dev_name(&flash->spi->dev), __func__, "from",
321                         (u32)from, len);
322
323         /* sanity checks */
324         if (!len)
325                 return 0;
326
327         if (from + len > flash->mtd.size)
328                 return -EINVAL;
329
330         spi_message_init(&m);
331         memset(t, 0, (sizeof t));
332
333         /* NOTE:
334          * OPCODE_FAST_READ (if available) is faster.
335          * Should add 1 byte DUMMY_BYTE.
336          */
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);
340
341         t[1].rx_buf = buf;
342         t[1].len = len;
343         spi_message_add_tail(&t[1], &m);
344
345         /* Byte count starts at zero. */
346         if (retlen)
347                 *retlen = 0;
348
349         mutex_lock(&flash->lock);
350
351         /* Wait till previous write/erase is done. */
352         if (wait_till_ready(flash)) {
353                 /* REVISIT status return?? */
354                 mutex_unlock(&flash->lock);
355                 return 1;
356         }
357
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.
361          */
362
363         /* Set up the write data buffer. */
364         flash->command[0] = OPCODE_READ;
365         m25p_addr2cmd(flash, from, flash->command);
366
367         spi_sync(flash->spi, &m);
368
369         *retlen = m.actual_length - m25p_cmdsz(flash) - FAST_READ_DUMMY_BYTE;
370
371         mutex_unlock(&flash->lock);
372
373         return 0;
374 }
375
376 /*
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.
380  */
381 static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
382         size_t *retlen, const u_char *buf)
383 {
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;
388
389         DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
390                         dev_name(&flash->spi->dev), __func__, "to",
391                         (u32)to, len);
392
393         if (retlen)
394                 *retlen = 0;
395
396         /* sanity checks */
397         if (!len)
398                 return(0);
399
400         if (to + len > flash->mtd.size)
401                 return -EINVAL;
402
403         spi_message_init(&m);
404         memset(t, 0, (sizeof t));
405
406         t[0].tx_buf = flash->command;
407         t[0].len = m25p_cmdsz(flash);
408         spi_message_add_tail(&t[0], &m);
409
410         t[1].tx_buf = buf;
411         spi_message_add_tail(&t[1], &m);
412
413         mutex_lock(&flash->lock);
414
415         /* Wait until finished previous write command. */
416         if (wait_till_ready(flash)) {
417                 mutex_unlock(&flash->lock);
418                 return 1;
419         }
420
421         write_enable(flash);
422
423         /* Set up the opcode in the write buffer. */
424         flash->command[0] = OPCODE_PP;
425         m25p_addr2cmd(flash, to, flash->command);
426
427         page_offset = to & (flash->page_size - 1);
428
429         /* do all the bytes fit onto one page? */
430         if (page_offset + len <= flash->page_size) {
431                 t[1].len = len;
432
433                 spi_sync(flash->spi, &m);
434
435                 *retlen = m.actual_length - m25p_cmdsz(flash);
436         } else {
437                 u32 i;
438
439                 /* the size of data remaining on the first page */
440                 page_size = flash->page_size - page_offset;
441
442                 t[1].len = page_size;
443                 spi_sync(flash->spi, &m);
444
445                 *retlen = m.actual_length - m25p_cmdsz(flash);
446
447                 /* write everything in flash->page_size chunks */
448                 for (i = page_size; i < len; i += page_size) {
449                         page_size = len - i;
450                         if (page_size > flash->page_size)
451                                 page_size = flash->page_size;
452
453                         /* write the next page to flash */
454                         m25p_addr2cmd(flash, to + i, flash->command);
455
456                         t[1].tx_buf = buf + i;
457                         t[1].len = page_size;
458
459                         wait_till_ready(flash);
460
461                         write_enable(flash);
462
463                         spi_sync(flash->spi, &m);
464
465                         if (retlen)
466                                 *retlen += m.actual_length - m25p_cmdsz(flash);
467                 }
468         }
469
470         mutex_unlock(&flash->lock);
471
472         return 0;
473 }
474
475 static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
476                 size_t *retlen, const u_char *buf)
477 {
478         struct m25p *flash = mtd_to_m25p(mtd);
479         struct spi_transfer t[2];
480         struct spi_message m;
481         size_t actual;
482         int cmd_sz, ret;
483
484         if (retlen)
485                 *retlen = 0;
486
487         /* sanity checks */
488         if (!len)
489                 return 0;
490
491         if (to + len > flash->mtd.size)
492                 return -EINVAL;
493
494         spi_message_init(&m);
495         memset(t, 0, (sizeof t));
496
497         t[0].tx_buf = flash->command;
498         t[0].len = m25p_cmdsz(flash);
499         spi_message_add_tail(&t[0], &m);
500
501         t[1].tx_buf = buf;
502         spi_message_add_tail(&t[1], &m);
503
504         mutex_lock(&flash->lock);
505
506         /* Wait until finished previous write command. */
507         ret = wait_till_ready(flash);
508         if (ret)
509                 goto time_out;
510
511         write_enable(flash);
512
513         actual = to % 2;
514         /* Start write from odd address. */
515         if (actual) {
516                 flash->command[0] = OPCODE_BP;
517                 m25p_addr2cmd(flash, to, flash->command);
518
519                 /* write one byte. */
520                 t[1].len = 1;
521                 spi_sync(flash->spi, &m);
522                 ret = wait_till_ready(flash);
523                 if (ret)
524                         goto time_out;
525                 *retlen += m.actual_length - m25p_cmdsz(flash);
526         }
527         to += actual;
528
529         flash->command[0] = OPCODE_AAI_WP;
530         m25p_addr2cmd(flash, to, flash->command);
531
532         /* Write out most of the data here. */
533         cmd_sz = m25p_cmdsz(flash);
534         for (; actual < len - 1; actual += 2) {
535                 t[0].len = cmd_sz;
536                 /* write two bytes. */
537                 t[1].len = 2;
538                 t[1].tx_buf = buf + actual;
539
540                 spi_sync(flash->spi, &m);
541                 ret = wait_till_ready(flash);
542                 if (ret)
543                         goto time_out;
544                 *retlen += m.actual_length - cmd_sz;
545                 cmd_sz = 1;
546                 to += 2;
547         }
548         write_disable(flash);
549         ret = wait_till_ready(flash);
550         if (ret)
551                 goto time_out;
552
553         /* Write out trailing byte if it exists. */
554         if (actual != len) {
555                 write_enable(flash);
556                 flash->command[0] = OPCODE_BP;
557                 m25p_addr2cmd(flash, to, flash->command);
558                 t[0].len = m25p_cmdsz(flash);
559                 t[1].len = 1;
560                 t[1].tx_buf = buf + actual;
561
562                 spi_sync(flash->spi, &m);
563                 ret = wait_till_ready(flash);
564                 if (ret)
565                         goto time_out;
566                 *retlen += m.actual_length - m25p_cmdsz(flash);
567                 write_disable(flash);
568         }
569
570 time_out:
571         mutex_unlock(&flash->lock);
572         return ret;
573 }
574
575 /****************************************************************************/
576
577 /*
578  * SPI device driver setup and teardown
579  */
580
581 struct flash_info {
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.
585          */
586         u32             jedec_id;
587         u16             ext_id;
588
589         /* The size listed here is what works with OPCODE_SE, which isn't
590          * necessarily called a "sector" by the vendor.
591          */
592         unsigned        sector_size;
593         u16             n_sectors;
594
595         u16             page_size;
596         u16             addr_width;
597
598         u16             flags;
599 #define SECT_4K         0x01            /* OPCODE_BE_4K works uniformly */
600 #define M25P_NO_ERASE   0x02            /* No erase command needed */
601 };
602
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),                              \
609                 .page_size = 256,                                       \
610                 .addr_width = 3,                                        \
611                 .flags = (_flags),                                      \
612         })
613
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,                                 \
621         })
622
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.
626  */
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) },
631
632         { "at25df041a", INFO(0x1f4401, 0, 64 * 1024,   8, SECT_4K) },
633         { "at25df641",  INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
634
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) },
639
640         /* Macronix */
641         { "mx25l3205d",  INFO(0xc22016, 0, 64 * 1024,  64, 0) },
642         { "mx25l6405d",  INFO(0xc22017, 0, 64 * 1024, 128, 0) },
643         { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
644         { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
645
646         /* Spansion -- single (large) sector size only, at least
647          * for the chips listed here (without boot sectors).
648          */
649         { "s25sl004a",  INFO(0x010212,      0,  64 * 1024,   8, 0) },
650         { "s25sl008a",  INFO(0x010213,      0,  64 * 1024,  16, 0) },
651         { "s25sl016a",  INFO(0x010214,      0,  64 * 1024,  32, 0) },
652         { "s25sl032a",  INFO(0x010215,      0,  64 * 1024,  64, 0) },
653         { "s25sl064a",  INFO(0x010216,      0,  64 * 1024, 128, 0) },
654         { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024,  64, 0) },
655         { "s25sl12801", INFO(0x012018, 0x0301,  64 * 1024, 256, 0) },
656         { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024,  64, 0) },
657         { "s25fl129p1", INFO(0x012018, 0x4d01,  64 * 1024, 256, 0) },
658
659         /* SST -- large erase sizes are "overlays", "sectors" are 4K */
660         { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024,  8, SECT_4K) },
661         { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K) },
662         { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K) },
663         { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K) },
664         { "sst25wf512",  INFO(0xbf2501, 0, 64 * 1024,  1, SECT_4K) },
665         { "sst25wf010",  INFO(0xbf2502, 0, 64 * 1024,  2, SECT_4K) },
666         { "sst25wf020",  INFO(0xbf2503, 0, 64 * 1024,  4, SECT_4K) },
667         { "sst25wf040",  INFO(0xbf2504, 0, 64 * 1024,  8, SECT_4K) },
668
669         /* ST Microelectronics -- newer production may have feature updates */
670         { "m25p05",  INFO(0x202010,  0,  32 * 1024,   2, 0) },
671         { "m25p10",  INFO(0x202011,  0,  32 * 1024,   4, 0) },
672         { "m25p20",  INFO(0x202012,  0,  64 * 1024,   4, 0) },
673         { "m25p40",  INFO(0x202013,  0,  64 * 1024,   8, 0) },
674         { "m25p80",  INFO(0x202014,  0,  64 * 1024,  16, 0) },
675         { "m25p16",  INFO(0x202015,  0,  64 * 1024,  32, 0) },
676         { "m25p32",  INFO(0x202016,  0,  64 * 1024,  64, 0) },
677         { "m25p64",  INFO(0x202017,  0,  64 * 1024, 128, 0) },
678         { "m25p128", INFO(0x202018,  0, 256 * 1024,  64, 0) },
679
680         { "m45pe10", INFO(0x204011,  0, 64 * 1024,    2, 0) },
681         { "m45pe80", INFO(0x204014,  0, 64 * 1024,   16, 0) },
682         { "m45pe16", INFO(0x204015,  0, 64 * 1024,   32, 0) },
683
684         { "m25pe80", INFO(0x208014,  0, 64 * 1024, 16,       0) },
685         { "m25pe16", INFO(0x208015,  0, 64 * 1024, 32, SECT_4K) },
686
687         /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
688         { "w25x10", INFO(0xef3011, 0, 64 * 1024,  2,  SECT_4K) },
689         { "w25x20", INFO(0xef3012, 0, 64 * 1024,  4,  SECT_4K) },
690         { "w25x40", INFO(0xef3013, 0, 64 * 1024,  8,  SECT_4K) },
691         { "w25x80", INFO(0xef3014, 0, 64 * 1024,  16, SECT_4K) },
692         { "w25x16", INFO(0xef3015, 0, 64 * 1024,  32, SECT_4K) },
693         { "w25x32", INFO(0xef3016, 0, 64 * 1024,  64, SECT_4K) },
694         { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
695
696         /* Catalyst / On Semiconductor -- non-JEDEC */
697         { "cat25c11", CAT25_INFO(  16, 8, 16, 1) },
698         { "cat25c03", CAT25_INFO(  32, 8, 16, 2) },
699         { "cat25c09", CAT25_INFO( 128, 8, 32, 2) },
700         { "cat25c17", CAT25_INFO( 256, 8, 32, 2) },
701         { "cat25128", CAT25_INFO(2048, 8, 64, 2) },
702         { },
703 };
704 MODULE_DEVICE_TABLE(spi, m25p_ids);
705
706 static const struct spi_device_id *__devinit jedec_probe(struct spi_device *spi)
707 {
708         int                     tmp;
709         u8                      code = OPCODE_RDID;
710         u8                      id[5];
711         u32                     jedec;
712         u16                     ext_jedec;
713         struct flash_info       *info;
714
715         /* JEDEC also defines an optional "extended device information"
716          * string for after vendor-specific data, after the three bytes
717          * we use here.  Supporting some chips might require using it.
718          */
719         tmp = spi_write_then_read(spi, &code, 1, id, 5);
720         if (tmp < 0) {
721                 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
722                         dev_name(&spi->dev), tmp);
723                 return NULL;
724         }
725         jedec = id[0];
726         jedec = jedec << 8;
727         jedec |= id[1];
728         jedec = jedec << 8;
729         jedec |= id[2];
730
731         /*
732          * Some chips (like Numonyx M25P80) have JEDEC and non-JEDEC variants,
733          * which depend on technology process. Officially RDID command doesn't
734          * exist for non-JEDEC chips, but for compatibility they return ID 0.
735          */
736         if (jedec == 0)
737                 return NULL;
738
739         ext_jedec = id[3] << 8 | id[4];
740
741         for (tmp = 0; tmp < ARRAY_SIZE(m25p_ids) - 1; tmp++) {
742                 info = (void *)m25p_ids[tmp].driver_data;
743                 if (info->jedec_id == jedec) {
744                         if (info->ext_id != 0 && info->ext_id != ext_jedec)
745                                 continue;
746                         return &m25p_ids[tmp];
747                 }
748         }
749         return NULL;
750 }
751
752
753 /*
754  * board specific setup should have ensured the SPI clock used here
755  * matches what the READ command supports, at least until this driver
756  * understands FAST_READ (for clocks over 25 MHz).
757  */
758 static int __devinit m25p_probe(struct spi_device *spi)
759 {
760         const struct spi_device_id      *id = spi_get_device_id(spi);
761         struct flash_platform_data      *data;
762         struct m25p                     *flash;
763         struct flash_info               *info;
764         unsigned                        i;
765
766         /* Platform data helps sort out which chip type we have, as
767          * well as how this board partitions it.  If we don't have
768          * a chip ID, try the JEDEC id commands; they'll work for most
769          * newer chips, even if we don't recognize the particular chip.
770          */
771         data = spi->dev.platform_data;
772         if (data && data->type) {
773                 const struct spi_device_id *plat_id;
774
775                 for (i = 0; i < ARRAY_SIZE(m25p_ids) - 1; i++) {
776                         plat_id = &m25p_ids[i];
777                         if (strcmp(data->type, plat_id->name))
778                                 continue;
779                         break;
780                 }
781
782                 if (plat_id)
783                         id = plat_id;
784                 else
785                         dev_warn(&spi->dev, "unrecognized id %s\n", data->type);
786         }
787
788         info = (void *)id->driver_data;
789
790         if (info->jedec_id) {
791                 const struct spi_device_id *jid;
792
793                 jid = jedec_probe(spi);
794                 if (!jid) {
795                         dev_info(&spi->dev, "non-JEDEC variant of %s\n",
796                                  id->name);
797                 } else if (jid != id) {
798                         /*
799                          * JEDEC knows better, so overwrite platform ID. We
800                          * can't trust partitions any longer, but we'll let
801                          * mtd apply them anyway, since some partitions may be
802                          * marked read-only, and we don't want to lose that
803                          * information, even if it's not 100% accurate.
804                          */
805                         dev_warn(&spi->dev, "found %s, expected %s\n",
806                                  jid->name, id->name);
807                         id = jid;
808                         info = (void *)jid->driver_data;
809                 }
810         }
811
812         flash = kzalloc(sizeof *flash, GFP_KERNEL);
813         if (!flash)
814                 return -ENOMEM;
815         flash->command = kmalloc(MAX_CMD_SIZE + FAST_READ_DUMMY_BYTE, GFP_KERNEL);
816         if (!flash->command) {
817                 kfree(flash);
818                 return -ENOMEM;
819         }
820
821         flash->spi = spi;
822         mutex_init(&flash->lock);
823         dev_set_drvdata(&spi->dev, flash);
824
825         /*
826          * Atmel and SST serial flash tend to power
827          * up with the software protection bits set
828          */
829
830         if (info->jedec_id >> 16 == 0x1f ||
831             info->jedec_id >> 16 == 0xbf) {
832                 write_enable(flash);
833                 write_sr(flash, 0);
834         }
835
836         if (data && data->name)
837                 flash->mtd.name = data->name;
838         else
839                 flash->mtd.name = dev_name(&spi->dev);
840
841         flash->mtd.type = MTD_NORFLASH;
842         flash->mtd.writesize = 1;
843         flash->mtd.flags = MTD_CAP_NORFLASH;
844         flash->mtd.size = info->sector_size * info->n_sectors;
845         flash->mtd.erase = m25p80_erase;
846         flash->mtd.read = m25p80_read;
847
848         /* sst flash chips use AAI word program */
849         if (info->jedec_id >> 16 == 0xbf)
850                 flash->mtd.write = sst_write;
851         else
852                 flash->mtd.write = m25p80_write;
853
854         /* prefer "small sector" erase if possible */
855         if (info->flags & SECT_4K) {
856                 flash->erase_opcode = OPCODE_BE_4K;
857                 flash->mtd.erasesize = 4096;
858         } else {
859                 flash->erase_opcode = OPCODE_SE;
860                 flash->mtd.erasesize = info->sector_size;
861         }
862
863         if (info->flags & M25P_NO_ERASE)
864                 flash->mtd.flags |= MTD_NO_ERASE;
865
866         flash->mtd.dev.parent = &spi->dev;
867         flash->page_size = info->page_size;
868         flash->addr_width = info->addr_width;
869
870         dev_info(&spi->dev, "%s (%lld Kbytes)\n", id->name,
871                         (long long)flash->mtd.size >> 10);
872
873         DEBUG(MTD_DEBUG_LEVEL2,
874                 "mtd .name = %s, .size = 0x%llx (%lldMiB) "
875                         ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
876                 flash->mtd.name,
877                 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
878                 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
879                 flash->mtd.numeraseregions);
880
881         if (flash->mtd.numeraseregions)
882                 for (i = 0; i < flash->mtd.numeraseregions; i++)
883                         DEBUG(MTD_DEBUG_LEVEL2,
884                                 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
885                                 ".erasesize = 0x%.8x (%uKiB), "
886                                 ".numblocks = %d }\n",
887                                 i, (long long)flash->mtd.eraseregions[i].offset,
888                                 flash->mtd.eraseregions[i].erasesize,
889                                 flash->mtd.eraseregions[i].erasesize / 1024,
890                                 flash->mtd.eraseregions[i].numblocks);
891
892
893         /* partitions should match sector boundaries; and it may be good to
894          * use readonly partitions for writeprotected sectors (BP2..BP0).
895          */
896         if (mtd_has_partitions()) {
897                 struct mtd_partition    *parts = NULL;
898                 int                     nr_parts = 0;
899
900                 if (mtd_has_cmdlinepart()) {
901                         static const char *part_probes[]
902                                         = { "cmdlinepart", NULL, };
903
904                         nr_parts = parse_mtd_partitions(&flash->mtd,
905                                         part_probes, &parts, 0);
906                 }
907
908                 if (nr_parts <= 0 && data && data->parts) {
909                         parts = data->parts;
910                         nr_parts = data->nr_parts;
911                 }
912
913                 if (nr_parts > 0) {
914                         for (i = 0; i < nr_parts; i++) {
915                                 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
916                                         "{.name = %s, .offset = 0x%llx, "
917                                                 ".size = 0x%llx (%lldKiB) }\n",
918                                         i, parts[i].name,
919                                         (long long)parts[i].offset,
920                                         (long long)parts[i].size,
921                                         (long long)(parts[i].size >> 10));
922                         }
923                         flash->partitioned = 1;
924                         return add_mtd_partitions(&flash->mtd, parts, nr_parts);
925                 }
926         } else if (data && data->nr_parts)
927                 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
928                                 data->nr_parts, data->name);
929
930         return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
931 }
932
933
934 static int __devexit m25p_remove(struct spi_device *spi)
935 {
936         struct m25p     *flash = dev_get_drvdata(&spi->dev);
937         int             status;
938
939         /* Clean up MTD stuff. */
940         if (mtd_has_partitions() && flash->partitioned)
941                 status = del_mtd_partitions(&flash->mtd);
942         else
943                 status = del_mtd_device(&flash->mtd);
944         if (status == 0) {
945                 kfree(flash->command);
946                 kfree(flash);
947         }
948         return 0;
949 }
950
951
952 static struct spi_driver m25p80_driver = {
953         .driver = {
954                 .name   = "m25p80",
955                 .bus    = &spi_bus_type,
956                 .owner  = THIS_MODULE,
957         },
958         .id_table       = m25p_ids,
959         .probe  = m25p_probe,
960         .remove = __devexit_p(m25p_remove),
961
962         /* REVISIT: many of these chips have deep power-down modes, which
963          * should clearly be entered on suspend() to minimize power use.
964          * And also when they're otherwise idle...
965          */
966 };
967
968
969 static int __init m25p80_init(void)
970 {
971         return spi_register_driver(&m25p80_driver);
972 }
973
974
975 static void __exit m25p80_exit(void)
976 {
977         spi_unregister_driver(&m25p80_driver);
978 }
979
980
981 module_init(m25p80_init);
982 module_exit(m25p80_exit);
983
984 MODULE_LICENSE("GPL");
985 MODULE_AUTHOR("Mike Lavender");
986 MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");