]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/mtd/devices/m25p80.c
mtd: fix build error in m25p80.c
[net-next-2.6.git] / drivers / mtd / devices / m25p80.c
CommitLineData
2f9f7628 1/*
fa0a8c71 2 * MTD SPI driver for ST M25Pxx (and similar) serial flash chips
2f9f7628
ML
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>
9d2c4f3f
AV
19#include <linux/err.h>
20#include <linux/errno.h>
2f9f7628
ML
21#include <linux/module.h>
22#include <linux/device.h>
23#include <linux/interrupt.h>
7d5230ea 24#include <linux/mutex.h>
d85316ac 25#include <linux/math64.h>
5a0e3ad6 26#include <linux/slab.h>
d43c36dc 27#include <linux/sched.h>
b34bc037 28#include <linux/mod_devicetable.h>
7d5230ea 29
2f9f7628
ML
30#include <linux/mtd/mtd.h>
31#include <linux/mtd/partitions.h>
7d5230ea 32
2f9f7628
ML
33#include <linux/spi/spi.h>
34#include <linux/spi/flash.h>
35
2f9f7628 36/* Flash opcodes. */
fa0a8c71
DB
37#define OPCODE_WREN 0x06 /* Write enable */
38#define OPCODE_RDSR 0x05 /* Read status register */
72289824 39#define OPCODE_WRSR 0x01 /* Write status register 1 byte */
2230b76b 40#define OPCODE_NORM_READ 0x03 /* Read data bytes (low frequency) */
fa0a8c71
DB
41#define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */
42#define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */
7854643a 43#define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
02d087db 44#define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
7854643a 45#define OPCODE_CHIP_ERASE 0xc7 /* Erase whole flash chip */
02d087db 46#define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
2f9f7628
ML
47#define OPCODE_RDID 0x9f /* Read JEDEC ID */
48
49aac4ae
GY
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 */
53
2f9f7628
ML
54/* Status Register bits. */
55#define SR_WIP 1 /* Write in progress */
56#define SR_WEL 2 /* Write enable latch */
fa0a8c71 57/* meaning of other SR_* bits may differ between vendors */
2f9f7628
ML
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 */
62
63/* Define max times to check status register before we give up. */
89bb871e 64#define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
837479d2 65#define MAX_CMD_SIZE 4
2f9f7628 66
2230b76b
BW
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
2f9f7628 74
2f9f7628
ML
75/****************************************************************************/
76
77struct m25p {
78 struct spi_device *spi;
7d5230ea 79 struct mutex lock;
2f9f7628 80 struct mtd_info mtd;
fa0a8c71 81 unsigned partitioned:1;
837479d2
AV
82 u16 page_size;
83 u16 addr_width;
fa0a8c71 84 u8 erase_opcode;
61c3506c 85 u8 *command;
2f9f7628
ML
86};
87
88static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
89{
90 return container_of(mtd, struct m25p, mtd);
91}
92
93/****************************************************************************/
94
95/*
96 * Internal helper functions
97 */
98
99/*
100 * Read the status register, returning its value in the location
101 * Return the status register value.
102 * Returns negative if error occurred.
103 */
104static int read_sr(struct m25p *flash)
105{
106 ssize_t retval;
107 u8 code = OPCODE_RDSR;
108 u8 val;
109
110 retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
111
112 if (retval < 0) {
113 dev_err(&flash->spi->dev, "error %d reading SR\n",
114 (int) retval);
115 return retval;
116 }
117
118 return val;
119}
120
72289824
MH
121/*
122 * Write status register 1 byte
123 * Returns negative if error occurred.
124 */
125static int write_sr(struct m25p *flash, u8 val)
126{
127 flash->command[0] = OPCODE_WRSR;
128 flash->command[1] = val;
129
130 return spi_write(flash->spi, flash->command, 2);
131}
2f9f7628
ML
132
133/*
134 * Set write enable latch with Write Enable command.
135 * Returns negative if error occurred.
136 */
137static inline int write_enable(struct m25p *flash)
138{
139 u8 code = OPCODE_WREN;
140
8a1a6272 141 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
2f9f7628
ML
142}
143
49aac4ae
GY
144/*
145 * Send write disble instruction to the chip.
146 */
147static inline int write_disable(struct m25p *flash)
148{
149 u8 code = OPCODE_WRDI;
150
151 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
152}
2f9f7628
ML
153
154/*
155 * Service routine to read status register until ready, or timeout occurs.
156 * Returns non-zero if error.
157 */
158static int wait_till_ready(struct m25p *flash)
159{
cd1a6de7 160 unsigned long deadline;
2f9f7628
ML
161 int sr;
162
cd1a6de7
PH
163 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
164
165 do {
2f9f7628
ML
166 if ((sr = read_sr(flash)) < 0)
167 break;
168 else if (!(sr & SR_WIP))
169 return 0;
170
cd1a6de7
PH
171 cond_resched();
172
173 } while (!time_after_eq(jiffies, deadline));
2f9f7628
ML
174
175 return 1;
176}
177
faff3750
CG
178/*
179 * Erase the whole flash memory
180 *
181 * Returns 0 if successful, non-zero otherwise.
182 */
7854643a 183static int erase_chip(struct m25p *flash)
faff3750 184{
d85316ac 185 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %lldKiB\n",
160bbab3
KS
186 dev_name(&flash->spi->dev), __func__,
187 (long long)(flash->mtd.size >> 10));
faff3750
CG
188
189 /* Wait until finished previous write command. */
190 if (wait_till_ready(flash))
191 return 1;
192
193 /* Send write enable, then erase commands. */
194 write_enable(flash);
195
196 /* Set up command buffer. */
7854643a 197 flash->command[0] = OPCODE_CHIP_ERASE;
faff3750
CG
198
199 spi_write(flash->spi, flash->command, 1);
200
201 return 0;
202}
2f9f7628 203
837479d2
AV
204static void m25p_addr2cmd(struct m25p *flash, unsigned int addr, u8 *cmd)
205{
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);
210}
211
212static int m25p_cmdsz(struct m25p *flash)
213{
214 return 1 + flash->addr_width;
215}
216
2f9f7628
ML
217/*
218 * Erase one sector of flash memory at offset ``offset'' which is any
219 * address within the sector which should be erased.
220 *
221 * Returns 0 if successful, non-zero otherwise.
222 */
223static int erase_sector(struct m25p *flash, u32 offset)
224{
02d087db 225 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
160bbab3 226 dev_name(&flash->spi->dev), __func__,
fa0a8c71 227 flash->mtd.erasesize / 1024, offset);
2f9f7628
ML
228
229 /* Wait until finished previous write command. */
230 if (wait_till_ready(flash))
231 return 1;
232
233 /* Send write enable, then erase commands. */
234 write_enable(flash);
235
236 /* Set up command buffer. */
fa0a8c71 237 flash->command[0] = flash->erase_opcode;
837479d2 238 m25p_addr2cmd(flash, offset, flash->command);
2f9f7628 239
837479d2 240 spi_write(flash->spi, flash->command, m25p_cmdsz(flash));
2f9f7628
ML
241
242 return 0;
243}
244
245/****************************************************************************/
246
247/*
248 * MTD implementation
249 */
250
251/*
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.
254 */
255static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
256{
257 struct m25p *flash = mtd_to_m25p(mtd);
258 u32 addr,len;
d85316ac 259 uint32_t rem;
2f9f7628 260
d85316ac 261 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%llx, len %lld\n",
160bbab3
KS
262 dev_name(&flash->spi->dev), __func__, "at",
263 (long long)instr->addr, (long long)instr->len);
2f9f7628
ML
264
265 /* sanity checks */
266 if (instr->addr + instr->len > flash->mtd.size)
267 return -EINVAL;
d85316ac
AB
268 div_u64_rem(instr->len, mtd->erasesize, &rem);
269 if (rem)
2f9f7628 270 return -EINVAL;
2f9f7628
ML
271
272 addr = instr->addr;
273 len = instr->len;
274
7d5230ea 275 mutex_lock(&flash->lock);
2f9f7628 276
7854643a 277 /* whole-chip erase? */
3f33b0aa
SF
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;
283 }
7854643a
CG
284
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.
288 */
289
290 /* "sector"-at-a-time erase */
faff3750
CG
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;
297 }
298
299 addr += mtd->erasesize;
300 len -= mtd->erasesize;
2f9f7628 301 }
2f9f7628
ML
302 }
303
7d5230ea 304 mutex_unlock(&flash->lock);
2f9f7628
ML
305
306 instr->state = MTD_ERASE_DONE;
307 mtd_erase_callback(instr);
308
309 return 0;
310}
311
312/*
313 * Read an address range from the flash chip. The address range
314 * may be any size provided it is within the physical boundaries.
315 */
316static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
317 size_t *retlen, u_char *buf)
318{
319 struct m25p *flash = mtd_to_m25p(mtd);
320 struct spi_transfer t[2];
321 struct spi_message m;
322
323 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
160bbab3 324 dev_name(&flash->spi->dev), __func__, "from",
2f9f7628
ML
325 (u32)from, len);
326
327 /* sanity checks */
328 if (!len)
329 return 0;
330
331 if (from + len > flash->mtd.size)
332 return -EINVAL;
333
8275c642
VW
334 spi_message_init(&m);
335 memset(t, 0, (sizeof t));
336
2230b76b
BW
337 /* NOTE:
338 * OPCODE_FAST_READ (if available) is faster.
339 * Should add 1 byte DUMMY_BYTE.
340 */
8275c642 341 t[0].tx_buf = flash->command;
837479d2 342 t[0].len = m25p_cmdsz(flash) + FAST_READ_DUMMY_BYTE;
8275c642
VW
343 spi_message_add_tail(&t[0], &m);
344
345 t[1].rx_buf = buf;
346 t[1].len = len;
347 spi_message_add_tail(&t[1], &m);
348
349 /* Byte count starts at zero. */
b06cd21e 350 *retlen = 0;
8275c642 351
7d5230ea 352 mutex_lock(&flash->lock);
2f9f7628
ML
353
354 /* Wait till previous write/erase is done. */
355 if (wait_till_ready(flash)) {
356 /* REVISIT status return?? */
7d5230ea 357 mutex_unlock(&flash->lock);
2f9f7628
ML
358 return 1;
359 }
360
fa0a8c71
DB
361 /* FIXME switch to OPCODE_FAST_READ. It's required for higher
362 * clocks; and at this writing, every chip this driver handles
363 * supports that opcode.
364 */
2f9f7628
ML
365
366 /* Set up the write data buffer. */
367 flash->command[0] = OPCODE_READ;
837479d2 368 m25p_addr2cmd(flash, from, flash->command);
2f9f7628 369
2f9f7628
ML
370 spi_sync(flash->spi, &m);
371
837479d2 372 *retlen = m.actual_length - m25p_cmdsz(flash) - FAST_READ_DUMMY_BYTE;
2f9f7628 373
7d5230ea 374 mutex_unlock(&flash->lock);
2f9f7628
ML
375
376 return 0;
377}
378
379/*
380 * Write an address range to the flash chip. Data must be written in
381 * FLASH_PAGESIZE chunks. The address range may be any size provided
382 * it is within the physical boundaries.
383 */
384static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
385 size_t *retlen, const u_char *buf)
386{
387 struct m25p *flash = mtd_to_m25p(mtd);
388 u32 page_offset, page_size;
389 struct spi_transfer t[2];
390 struct spi_message m;
391
392 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
160bbab3 393 dev_name(&flash->spi->dev), __func__, "to",
2f9f7628
ML
394 (u32)to, len);
395
b06cd21e 396 *retlen = 0;
2f9f7628
ML
397
398 /* sanity checks */
399 if (!len)
400 return(0);
401
402 if (to + len > flash->mtd.size)
403 return -EINVAL;
404
8275c642
VW
405 spi_message_init(&m);
406 memset(t, 0, (sizeof t));
407
408 t[0].tx_buf = flash->command;
837479d2 409 t[0].len = m25p_cmdsz(flash);
8275c642
VW
410 spi_message_add_tail(&t[0], &m);
411
412 t[1].tx_buf = buf;
413 spi_message_add_tail(&t[1], &m);
414
7d5230ea 415 mutex_lock(&flash->lock);
2f9f7628
ML
416
417 /* Wait until finished previous write command. */
bc018863
CG
418 if (wait_till_ready(flash)) {
419 mutex_unlock(&flash->lock);
2f9f7628 420 return 1;
bc018863 421 }
2f9f7628
ML
422
423 write_enable(flash);
424
2f9f7628
ML
425 /* Set up the opcode in the write buffer. */
426 flash->command[0] = OPCODE_PP;
837479d2 427 m25p_addr2cmd(flash, to, flash->command);
2f9f7628 428
837479d2 429 page_offset = to & (flash->page_size - 1);
2f9f7628
ML
430
431 /* do all the bytes fit onto one page? */
837479d2 432 if (page_offset + len <= flash->page_size) {
2f9f7628
ML
433 t[1].len = len;
434
435 spi_sync(flash->spi, &m);
436
837479d2 437 *retlen = m.actual_length - m25p_cmdsz(flash);
2f9f7628
ML
438 } else {
439 u32 i;
440
441 /* the size of data remaining on the first page */
837479d2 442 page_size = flash->page_size - page_offset;
2f9f7628 443
2f9f7628
ML
444 t[1].len = page_size;
445 spi_sync(flash->spi, &m);
446
837479d2 447 *retlen = m.actual_length - m25p_cmdsz(flash);
2f9f7628 448
837479d2 449 /* write everything in flash->page_size chunks */
2f9f7628
ML
450 for (i = page_size; i < len; i += page_size) {
451 page_size = len - i;
837479d2
AV
452 if (page_size > flash->page_size)
453 page_size = flash->page_size;
2f9f7628
ML
454
455 /* write the next page to flash */
837479d2 456 m25p_addr2cmd(flash, to + i, flash->command);
2f9f7628
ML
457
458 t[1].tx_buf = buf + i;
459 t[1].len = page_size;
460
461 wait_till_ready(flash);
462
463 write_enable(flash);
464
465 spi_sync(flash->spi, &m);
466
b06cd21e 467 *retlen += m.actual_length - m25p_cmdsz(flash);
7d5230ea
DB
468 }
469 }
2f9f7628 470
7d5230ea 471 mutex_unlock(&flash->lock);
2f9f7628
ML
472
473 return 0;
474}
475
49aac4ae
GY
476static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
477 size_t *retlen, const u_char *buf)
478{
479 struct m25p *flash = mtd_to_m25p(mtd);
480 struct spi_transfer t[2];
481 struct spi_message m;
482 size_t actual;
483 int cmd_sz, ret;
484
b06cd21e 485 *retlen = 0;
49aac4ae
GY
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;
837479d2 498 t[0].len = m25p_cmdsz(flash);
49aac4ae
GY
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;
837479d2 517 m25p_addr2cmd(flash, to, flash->command);
49aac4ae
GY
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;
837479d2 525 *retlen += m.actual_length - m25p_cmdsz(flash);
49aac4ae
GY
526 }
527 to += actual;
528
529 flash->command[0] = OPCODE_AAI_WP;
837479d2 530 m25p_addr2cmd(flash, to, flash->command);
49aac4ae
GY
531
532 /* Write out most of the data here. */
837479d2 533 cmd_sz = m25p_cmdsz(flash);
49aac4ae
GY
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;
837479d2
AV
557 m25p_addr2cmd(flash, to, flash->command);
558 t[0].len = m25p_cmdsz(flash);
49aac4ae
GY
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;
837479d2 566 *retlen += m.actual_length - m25p_cmdsz(flash);
49aac4ae
GY
567 write_disable(flash);
568 }
569
570time_out:
571 mutex_unlock(&flash->lock);
572 return ret;
573}
2f9f7628
ML
574
575/****************************************************************************/
576
577/*
578 * SPI device driver setup and teardown
579 */
580
581struct flash_info {
fa0a8c71
DB
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;
d0e8c47c 587 u16 ext_id;
fa0a8c71
DB
588
589 /* The size listed here is what works with OPCODE_SE, which isn't
590 * necessarily called a "sector" by the vendor.
591 */
2f9f7628 592 unsigned sector_size;
fa0a8c71
DB
593 u16 n_sectors;
594
837479d2
AV
595 u16 page_size;
596 u16 addr_width;
597
fa0a8c71
DB
598 u16 flags;
599#define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
837479d2 600#define M25P_NO_ERASE 0x02 /* No erase command needed */
2f9f7628
ML
601};
602
b34bc037
AV
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), \
837479d2
AV
609 .page_size = 256, \
610 .addr_width = 3, \
b34bc037
AV
611 .flags = (_flags), \
612 })
fa0a8c71 613
837479d2
AV
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 })
fa0a8c71
DB
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 */
b34bc037 627static const struct spi_device_id m25p_ids[] = {
fa0a8c71 628 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
b34bc037
AV
629 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
630 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
fa0a8c71 631
b34bc037
AV
632 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
633 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
fa0a8c71 634
b34bc037
AV
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) },
fa0a8c71 639
60845e72
GJ
640 /* EON -- en25pxx */
641 { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
642 { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
643
f80e521c
GJ
644 /* Intel/Numonyx -- xxxs33b */
645 { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
646 { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
647 { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
648
ab1ff210 649 /* Macronix */
df0094d7 650 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
6175f4a1 651 { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
b34bc037
AV
652 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
653 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
654 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
655 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
ab1ff210 656
fa0a8c71
DB
657 /* Spansion -- single (large) sector size only, at least
658 * for the chips listed here (without boot sectors).
659 */
b34bc037
AV
660 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
661 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
662 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
663 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
d86fbdb8 664 { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SECT_4K) },
b34bc037
AV
665 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
666 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
667 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
668 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
669 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
f2df1ae3
GH
670 { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K) },
671 { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
fa0a8c71
DB
672
673 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
b34bc037
AV
674 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K) },
675 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K) },
676 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K) },
677 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K) },
678 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K) },
679 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K) },
680 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K) },
681 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K) },
fa0a8c71
DB
682
683 /* ST Microelectronics -- newer production may have feature updates */
b34bc037
AV
684 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
685 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
686 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
687 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
688 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
689 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
690 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
691 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
692 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
693
f7b00090
AV
694 { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
695 { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
696 { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
697 { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
698 { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
699 { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
700 { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
701 { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
702 { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
703
b34bc037
AV
704 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
705 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
706 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
707
708 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
709 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
fa0a8c71 710
02d087db 711 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
b34bc037
AV
712 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
713 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
714 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
715 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
716 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
717 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
0af18d27 718 { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
b34bc037 719 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
d2ac467a 720 { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
837479d2
AV
721
722 /* Catalyst / On Semiconductor -- non-JEDEC */
723 { "cat25c11", CAT25_INFO( 16, 8, 16, 1) },
724 { "cat25c03", CAT25_INFO( 32, 8, 16, 2) },
725 { "cat25c09", CAT25_INFO( 128, 8, 32, 2) },
726 { "cat25c17", CAT25_INFO( 256, 8, 32, 2) },
727 { "cat25128", CAT25_INFO(2048, 8, 64, 2) },
b34bc037 728 { },
2f9f7628 729};
b34bc037 730MODULE_DEVICE_TABLE(spi, m25p_ids);
2f9f7628 731
b34bc037 732static const struct spi_device_id *__devinit jedec_probe(struct spi_device *spi)
fa0a8c71
DB
733{
734 int tmp;
735 u8 code = OPCODE_RDID;
daa84735 736 u8 id[5];
fa0a8c71 737 u32 jedec;
d0e8c47c 738 u16 ext_jedec;
fa0a8c71
DB
739 struct flash_info *info;
740
741 /* JEDEC also defines an optional "extended device information"
742 * string for after vendor-specific data, after the three bytes
743 * we use here. Supporting some chips might require using it.
744 */
daa84735 745 tmp = spi_write_then_read(spi, &code, 1, id, 5);
fa0a8c71
DB
746 if (tmp < 0) {
747 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
160bbab3 748 dev_name(&spi->dev), tmp);
9d2c4f3f 749 return ERR_PTR(tmp);
fa0a8c71
DB
750 }
751 jedec = id[0];
752 jedec = jedec << 8;
753 jedec |= id[1];
754 jedec = jedec << 8;
755 jedec |= id[2];
756
d0e8c47c
CG
757 ext_jedec = id[3] << 8 | id[4];
758
b34bc037
AV
759 for (tmp = 0; tmp < ARRAY_SIZE(m25p_ids) - 1; tmp++) {
760 info = (void *)m25p_ids[tmp].driver_data;
a3d3f73c 761 if (info->jedec_id == jedec) {
9168ab86 762 if (info->ext_id != 0 && info->ext_id != ext_jedec)
d0e8c47c 763 continue;
b34bc037 764 return &m25p_ids[tmp];
a3d3f73c 765 }
fa0a8c71 766 }
9d2c4f3f 767 return ERR_PTR(-ENODEV);
fa0a8c71
DB
768}
769
770
2f9f7628
ML
771/*
772 * board specific setup should have ensured the SPI clock used here
773 * matches what the READ command supports, at least until this driver
774 * understands FAST_READ (for clocks over 25 MHz).
775 */
776static int __devinit m25p_probe(struct spi_device *spi)
777{
18c6182b 778 const struct spi_device_id *id = spi_get_device_id(spi);
2f9f7628
ML
779 struct flash_platform_data *data;
780 struct m25p *flash;
781 struct flash_info *info;
782 unsigned i;
783
784 /* Platform data helps sort out which chip type we have, as
fa0a8c71
DB
785 * well as how this board partitions it. If we don't have
786 * a chip ID, try the JEDEC id commands; they'll work for most
787 * newer chips, even if we don't recognize the particular chip.
2f9f7628
ML
788 */
789 data = spi->dev.platform_data;
fa0a8c71 790 if (data && data->type) {
18c6182b 791 const struct spi_device_id *plat_id;
2f9f7628 792
b34bc037 793 for (i = 0; i < ARRAY_SIZE(m25p_ids) - 1; i++) {
18c6182b
AV
794 plat_id = &m25p_ids[i];
795 if (strcmp(data->type, plat_id->name))
b34bc037
AV
796 continue;
797 break;
fa0a8c71 798 }
fa0a8c71 799
f78ec6b2 800 if (i < ARRAY_SIZE(m25p_ids) - 1)
18c6182b
AV
801 id = plat_id;
802 else
803 dev_warn(&spi->dev, "unrecognized id %s\n", data->type);
b34bc037 804 }
fa0a8c71 805
18c6182b
AV
806 info = (void *)id->driver_data;
807
808 if (info->jedec_id) {
809 const struct spi_device_id *jid;
810
811 jid = jedec_probe(spi);
9d2c4f3f
AV
812 if (IS_ERR(jid)) {
813 return PTR_ERR(jid);
18c6182b
AV
814 } else if (jid != id) {
815 /*
816 * JEDEC knows better, so overwrite platform ID. We
817 * can't trust partitions any longer, but we'll let
818 * mtd apply them anyway, since some partitions may be
819 * marked read-only, and we don't want to lose that
820 * information, even if it's not 100% accurate.
821 */
822 dev_warn(&spi->dev, "found %s, expected %s\n",
823 jid->name, id->name);
824 id = jid;
825 info = (void *)jid->driver_data;
826 }
827 }
2f9f7628 828
e94b1766 829 flash = kzalloc(sizeof *flash, GFP_KERNEL);
2f9f7628
ML
830 if (!flash)
831 return -ENOMEM;
837479d2 832 flash->command = kmalloc(MAX_CMD_SIZE + FAST_READ_DUMMY_BYTE, GFP_KERNEL);
61c3506c
JS
833 if (!flash->command) {
834 kfree(flash);
835 return -ENOMEM;
836 }
2f9f7628
ML
837
838 flash->spi = spi;
7d5230ea 839 mutex_init(&flash->lock);
2f9f7628
ML
840 dev_set_drvdata(&spi->dev, flash);
841
72289824 842 /*
f80e521c 843 * Atmel, SST and Intel/Numonyx serial flash tend to power
ea60658a 844 * up with the software protection bits set
72289824
MH
845 */
846
ea60658a 847 if (info->jedec_id >> 16 == 0x1f ||
f80e521c 848 info->jedec_id >> 16 == 0x89 ||
ea60658a 849 info->jedec_id >> 16 == 0xbf) {
72289824
MH
850 write_enable(flash);
851 write_sr(flash, 0);
852 }
853
fa0a8c71 854 if (data && data->name)
2f9f7628
ML
855 flash->mtd.name = data->name;
856 else
160bbab3 857 flash->mtd.name = dev_name(&spi->dev);
2f9f7628
ML
858
859 flash->mtd.type = MTD_NORFLASH;
783ed81f 860 flash->mtd.writesize = 1;
2f9f7628
ML
861 flash->mtd.flags = MTD_CAP_NORFLASH;
862 flash->mtd.size = info->sector_size * info->n_sectors;
2f9f7628
ML
863 flash->mtd.erase = m25p80_erase;
864 flash->mtd.read = m25p80_read;
49aac4ae
GY
865
866 /* sst flash chips use AAI word program */
867 if (info->jedec_id >> 16 == 0xbf)
868 flash->mtd.write = sst_write;
869 else
870 flash->mtd.write = m25p80_write;
2f9f7628 871
fa0a8c71
DB
872 /* prefer "small sector" erase if possible */
873 if (info->flags & SECT_4K) {
874 flash->erase_opcode = OPCODE_BE_4K;
875 flash->mtd.erasesize = 4096;
876 } else {
877 flash->erase_opcode = OPCODE_SE;
878 flash->mtd.erasesize = info->sector_size;
879 }
880
837479d2
AV
881 if (info->flags & M25P_NO_ERASE)
882 flash->mtd.flags |= MTD_NO_ERASE;
883
87f39f04 884 flash->mtd.dev.parent = &spi->dev;
837479d2
AV
885 flash->page_size = info->page_size;
886 flash->addr_width = info->addr_width;
87f39f04 887
b34bc037 888 dev_info(&spi->dev, "%s (%lld Kbytes)\n", id->name,
d85316ac 889 (long long)flash->mtd.size >> 10);
2f9f7628
ML
890
891 DEBUG(MTD_DEBUG_LEVEL2,
d85316ac 892 "mtd .name = %s, .size = 0x%llx (%lldMiB) "
02d087db 893 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
2f9f7628 894 flash->mtd.name,
d85316ac 895 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
2f9f7628
ML
896 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
897 flash->mtd.numeraseregions);
898
899 if (flash->mtd.numeraseregions)
900 for (i = 0; i < flash->mtd.numeraseregions; i++)
901 DEBUG(MTD_DEBUG_LEVEL2,
d85316ac 902 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
02d087db 903 ".erasesize = 0x%.8x (%uKiB), "
2f9f7628 904 ".numblocks = %d }\n",
d85316ac 905 i, (long long)flash->mtd.eraseregions[i].offset,
2f9f7628
ML
906 flash->mtd.eraseregions[i].erasesize,
907 flash->mtd.eraseregions[i].erasesize / 1024,
908 flash->mtd.eraseregions[i].numblocks);
909
910
911 /* partitions should match sector boundaries; and it may be good to
912 * use readonly partitions for writeprotected sectors (BP2..BP0).
913 */
914 if (mtd_has_partitions()) {
915 struct mtd_partition *parts = NULL;
916 int nr_parts = 0;
917
a4b6d516
DB
918 if (mtd_has_cmdlinepart()) {
919 static const char *part_probes[]
920 = { "cmdlinepart", NULL, };
2f9f7628 921
a4b6d516
DB
922 nr_parts = parse_mtd_partitions(&flash->mtd,
923 part_probes, &parts, 0);
924 }
2f9f7628
ML
925
926 if (nr_parts <= 0 && data && data->parts) {
927 parts = data->parts;
928 nr_parts = data->nr_parts;
929 }
930
40847437 931#ifdef CONFIG_MTD_OF_PARTS
97ff46cb
MH
932 if (nr_parts <= 0 && spi->dev.of_node) {
933 nr_parts = of_mtd_parse_partitions(&spi->dev,
934 spi->dev.of_node, &parts);
935 }
936#endif
937
2f9f7628 938 if (nr_parts > 0) {
fa0a8c71 939 for (i = 0; i < nr_parts; i++) {
2f9f7628 940 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
d85316ac
AB
941 "{.name = %s, .offset = 0x%llx, "
942 ".size = 0x%llx (%lldKiB) }\n",
fa0a8c71 943 i, parts[i].name,
d85316ac
AB
944 (long long)parts[i].offset,
945 (long long)parts[i].size,
946 (long long)(parts[i].size >> 10));
2f9f7628
ML
947 }
948 flash->partitioned = 1;
949 return add_mtd_partitions(&flash->mtd, parts, nr_parts);
950 }
edcb3b14 951 } else if (data && data->nr_parts)
2f9f7628
ML
952 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
953 data->nr_parts, data->name);
954
955 return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
956}
957
958
959static int __devexit m25p_remove(struct spi_device *spi)
960{
961 struct m25p *flash = dev_get_drvdata(&spi->dev);
962 int status;
963
964 /* Clean up MTD stuff. */
965 if (mtd_has_partitions() && flash->partitioned)
966 status = del_mtd_partitions(&flash->mtd);
967 else
968 status = del_mtd_device(&flash->mtd);
61c3506c
JS
969 if (status == 0) {
970 kfree(flash->command);
2f9f7628 971 kfree(flash);
61c3506c 972 }
2f9f7628
ML
973 return 0;
974}
975
976
977static struct spi_driver m25p80_driver = {
978 .driver = {
979 .name = "m25p80",
980 .bus = &spi_bus_type,
981 .owner = THIS_MODULE,
982 },
b34bc037 983 .id_table = m25p_ids,
2f9f7628
ML
984 .probe = m25p_probe,
985 .remove = __devexit_p(m25p_remove),
fa0a8c71
DB
986
987 /* REVISIT: many of these chips have deep power-down modes, which
988 * should clearly be entered on suspend() to minimize power use.
989 * And also when they're otherwise idle...
990 */
2f9f7628
ML
991};
992
993
627df23c 994static int __init m25p80_init(void)
2f9f7628
ML
995{
996 return spi_register_driver(&m25p80_driver);
997}
998
999
627df23c 1000static void __exit m25p80_exit(void)
2f9f7628
ML
1001{
1002 spi_unregister_driver(&m25p80_driver);
1003}
1004
1005
1006module_init(m25p80_init);
1007module_exit(m25p80_exit);
1008
1009MODULE_LICENSE("GPL");
1010MODULE_AUTHOR("Mike Lavender");
1011MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");