]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/mtd/devices/m25p80.c
[MTD] we don't need no misc devices
[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>
19#include <linux/module.h>
20#include <linux/device.h>
21#include <linux/interrupt.h>
7d5230ea 22#include <linux/mutex.h>
d85316ac 23#include <linux/math64.h>
7d5230ea 24
2f9f7628
ML
25#include <linux/mtd/mtd.h>
26#include <linux/mtd/partitions.h>
7d5230ea 27
2f9f7628
ML
28#include <linux/spi/spi.h>
29#include <linux/spi/flash.h>
30
2f9f7628 31
2f9f7628
ML
32#define FLASH_PAGESIZE 256
33
34/* Flash opcodes. */
fa0a8c71
DB
35#define OPCODE_WREN 0x06 /* Write enable */
36#define OPCODE_RDSR 0x05 /* Read status register */
72289824 37#define OPCODE_WRSR 0x01 /* Write status register 1 byte */
2230b76b 38#define OPCODE_NORM_READ 0x03 /* Read data bytes (low frequency) */
fa0a8c71
DB
39#define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */
40#define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */
7854643a 41#define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
02d087db 42#define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
7854643a 43#define OPCODE_CHIP_ERASE 0xc7 /* Erase whole flash chip */
02d087db 44#define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
2f9f7628
ML
45#define OPCODE_RDID 0x9f /* Read JEDEC ID */
46
47/* Status Register bits. */
48#define SR_WIP 1 /* Write in progress */
49#define SR_WEL 2 /* Write enable latch */
fa0a8c71 50/* meaning of other SR_* bits may differ between vendors */
2f9f7628
ML
51#define SR_BP0 4 /* Block protect 0 */
52#define SR_BP1 8 /* Block protect 1 */
53#define SR_BP2 0x10 /* Block protect 2 */
54#define SR_SRWD 0x80 /* SR write protect */
55
56/* Define max times to check status register before we give up. */
57#define MAX_READY_WAIT_COUNT 100000
2230b76b 58#define CMD_SIZE 4
2f9f7628 59
2230b76b
BW
60#ifdef CONFIG_M25PXX_USE_FAST_READ
61#define OPCODE_READ OPCODE_FAST_READ
62#define FAST_READ_DUMMY_BYTE 1
63#else
64#define OPCODE_READ OPCODE_NORM_READ
65#define FAST_READ_DUMMY_BYTE 0
66#endif
2f9f7628
ML
67
68#ifdef CONFIG_MTD_PARTITIONS
69#define mtd_has_partitions() (1)
70#else
71#define mtd_has_partitions() (0)
72#endif
73
74/****************************************************************************/
75
76struct m25p {
77 struct spi_device *spi;
7d5230ea 78 struct mutex lock;
2f9f7628 79 struct mtd_info mtd;
fa0a8c71
DB
80 unsigned partitioned:1;
81 u8 erase_opcode;
2230b76b 82 u8 command[CMD_SIZE + FAST_READ_DUMMY_BYTE];
2f9f7628
ML
83};
84
85static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
86{
87 return container_of(mtd, struct m25p, mtd);
88}
89
90/****************************************************************************/
91
92/*
93 * Internal helper functions
94 */
95
96/*
97 * Read the status register, returning its value in the location
98 * Return the status register value.
99 * Returns negative if error occurred.
100 */
101static int read_sr(struct m25p *flash)
102{
103 ssize_t retval;
104 u8 code = OPCODE_RDSR;
105 u8 val;
106
107 retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
108
109 if (retval < 0) {
110 dev_err(&flash->spi->dev, "error %d reading SR\n",
111 (int) retval);
112 return retval;
113 }
114
115 return val;
116}
117
72289824
MH
118/*
119 * Write status register 1 byte
120 * Returns negative if error occurred.
121 */
122static int write_sr(struct m25p *flash, u8 val)
123{
124 flash->command[0] = OPCODE_WRSR;
125 flash->command[1] = val;
126
127 return spi_write(flash->spi, flash->command, 2);
128}
2f9f7628
ML
129
130/*
131 * Set write enable latch with Write Enable command.
132 * Returns negative if error occurred.
133 */
134static inline int write_enable(struct m25p *flash)
135{
136 u8 code = OPCODE_WREN;
137
8a1a6272 138 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
2f9f7628
ML
139}
140
141
142/*
143 * Service routine to read status register until ready, or timeout occurs.
144 * Returns non-zero if error.
145 */
146static int wait_till_ready(struct m25p *flash)
147{
148 int count;
149 int sr;
150
151 /* one chip guarantees max 5 msec wait here after page writes,
152 * but potentially three seconds (!) after page erase.
153 */
154 for (count = 0; count < MAX_READY_WAIT_COUNT; count++) {
155 if ((sr = read_sr(flash)) < 0)
156 break;
157 else if (!(sr & SR_WIP))
158 return 0;
159
160 /* REVISIT sometimes sleeping would be best */
161 }
162
163 return 1;
164}
165
faff3750
CG
166/*
167 * Erase the whole flash memory
168 *
169 * Returns 0 if successful, non-zero otherwise.
170 */
7854643a 171static int erase_chip(struct m25p *flash)
faff3750 172{
d85316ac 173 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %lldKiB\n",
160bbab3
KS
174 dev_name(&flash->spi->dev), __func__,
175 (long long)(flash->mtd.size >> 10));
faff3750
CG
176
177 /* Wait until finished previous write command. */
178 if (wait_till_ready(flash))
179 return 1;
180
181 /* Send write enable, then erase commands. */
182 write_enable(flash);
183
184 /* Set up command buffer. */
7854643a 185 flash->command[0] = OPCODE_CHIP_ERASE;
faff3750
CG
186
187 spi_write(flash->spi, flash->command, 1);
188
189 return 0;
190}
2f9f7628
ML
191
192/*
193 * Erase one sector of flash memory at offset ``offset'' which is any
194 * address within the sector which should be erased.
195 *
196 * Returns 0 if successful, non-zero otherwise.
197 */
198static int erase_sector(struct m25p *flash, u32 offset)
199{
02d087db 200 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
160bbab3 201 dev_name(&flash->spi->dev), __func__,
fa0a8c71 202 flash->mtd.erasesize / 1024, offset);
2f9f7628
ML
203
204 /* Wait until finished previous write command. */
205 if (wait_till_ready(flash))
206 return 1;
207
208 /* Send write enable, then erase commands. */
209 write_enable(flash);
210
211 /* Set up command buffer. */
fa0a8c71 212 flash->command[0] = flash->erase_opcode;
2f9f7628
ML
213 flash->command[1] = offset >> 16;
214 flash->command[2] = offset >> 8;
215 flash->command[3] = offset;
216
2230b76b 217 spi_write(flash->spi, flash->command, CMD_SIZE);
2f9f7628
ML
218
219 return 0;
220}
221
222/****************************************************************************/
223
224/*
225 * MTD implementation
226 */
227
228/*
229 * Erase an address range on the flash chip. The address range may extend
230 * one or more erase sectors. Return an error is there is a problem erasing.
231 */
232static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
233{
234 struct m25p *flash = mtd_to_m25p(mtd);
235 u32 addr,len;
d85316ac 236 uint32_t rem;
2f9f7628 237
d85316ac 238 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%llx, len %lld\n",
160bbab3
KS
239 dev_name(&flash->spi->dev), __func__, "at",
240 (long long)instr->addr, (long long)instr->len);
2f9f7628
ML
241
242 /* sanity checks */
243 if (instr->addr + instr->len > flash->mtd.size)
244 return -EINVAL;
d85316ac
AB
245 div_u64_rem(instr->len, mtd->erasesize, &rem);
246 if (rem)
2f9f7628 247 return -EINVAL;
2f9f7628
ML
248
249 addr = instr->addr;
250 len = instr->len;
251
7d5230ea 252 mutex_lock(&flash->lock);
2f9f7628 253
7854643a
CG
254 /* whole-chip erase? */
255 if (len == flash->mtd.size && erase_chip(flash)) {
faff3750
CG
256 instr->state = MTD_ERASE_FAILED;
257 mutex_unlock(&flash->lock);
258 return -EIO;
7854643a
CG
259
260 /* REVISIT in some cases we could speed up erasing large regions
261 * by using OPCODE_SE instead of OPCODE_BE_4K. We may have set up
262 * to use "small sector erase", but that's not always optimal.
263 */
264
265 /* "sector"-at-a-time erase */
faff3750
CG
266 } else {
267 while (len) {
268 if (erase_sector(flash, addr)) {
269 instr->state = MTD_ERASE_FAILED;
270 mutex_unlock(&flash->lock);
271 return -EIO;
272 }
273
274 addr += mtd->erasesize;
275 len -= mtd->erasesize;
2f9f7628 276 }
2f9f7628
ML
277 }
278
7d5230ea 279 mutex_unlock(&flash->lock);
2f9f7628
ML
280
281 instr->state = MTD_ERASE_DONE;
282 mtd_erase_callback(instr);
283
284 return 0;
285}
286
287/*
288 * Read an address range from the flash chip. The address range
289 * may be any size provided it is within the physical boundaries.
290 */
291static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
292 size_t *retlen, u_char *buf)
293{
294 struct m25p *flash = mtd_to_m25p(mtd);
295 struct spi_transfer t[2];
296 struct spi_message m;
297
298 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
160bbab3 299 dev_name(&flash->spi->dev), __func__, "from",
2f9f7628
ML
300 (u32)from, len);
301
302 /* sanity checks */
303 if (!len)
304 return 0;
305
306 if (from + len > flash->mtd.size)
307 return -EINVAL;
308
8275c642
VW
309 spi_message_init(&m);
310 memset(t, 0, (sizeof t));
311
2230b76b
BW
312 /* NOTE:
313 * OPCODE_FAST_READ (if available) is faster.
314 * Should add 1 byte DUMMY_BYTE.
315 */
8275c642 316 t[0].tx_buf = flash->command;
2230b76b 317 t[0].len = CMD_SIZE + FAST_READ_DUMMY_BYTE;
8275c642
VW
318 spi_message_add_tail(&t[0], &m);
319
320 t[1].rx_buf = buf;
321 t[1].len = len;
322 spi_message_add_tail(&t[1], &m);
323
324 /* Byte count starts at zero. */
325 if (retlen)
326 *retlen = 0;
327
7d5230ea 328 mutex_lock(&flash->lock);
2f9f7628
ML
329
330 /* Wait till previous write/erase is done. */
331 if (wait_till_ready(flash)) {
332 /* REVISIT status return?? */
7d5230ea 333 mutex_unlock(&flash->lock);
2f9f7628
ML
334 return 1;
335 }
336
fa0a8c71
DB
337 /* FIXME switch to OPCODE_FAST_READ. It's required for higher
338 * clocks; and at this writing, every chip this driver handles
339 * supports that opcode.
340 */
2f9f7628
ML
341
342 /* Set up the write data buffer. */
343 flash->command[0] = OPCODE_READ;
344 flash->command[1] = from >> 16;
345 flash->command[2] = from >> 8;
346 flash->command[3] = from;
347
2f9f7628
ML
348 spi_sync(flash->spi, &m);
349
2230b76b 350 *retlen = m.actual_length - CMD_SIZE - FAST_READ_DUMMY_BYTE;
2f9f7628 351
7d5230ea 352 mutex_unlock(&flash->lock);
2f9f7628
ML
353
354 return 0;
355}
356
357/*
358 * Write an address range to the flash chip. Data must be written in
359 * FLASH_PAGESIZE chunks. The address range may be any size provided
360 * it is within the physical boundaries.
361 */
362static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
363 size_t *retlen, const u_char *buf)
364{
365 struct m25p *flash = mtd_to_m25p(mtd);
366 u32 page_offset, page_size;
367 struct spi_transfer t[2];
368 struct spi_message m;
369
370 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
160bbab3 371 dev_name(&flash->spi->dev), __func__, "to",
2f9f7628
ML
372 (u32)to, len);
373
374 if (retlen)
375 *retlen = 0;
376
377 /* sanity checks */
378 if (!len)
379 return(0);
380
381 if (to + len > flash->mtd.size)
382 return -EINVAL;
383
8275c642
VW
384 spi_message_init(&m);
385 memset(t, 0, (sizeof t));
386
387 t[0].tx_buf = flash->command;
2230b76b 388 t[0].len = CMD_SIZE;
8275c642
VW
389 spi_message_add_tail(&t[0], &m);
390
391 t[1].tx_buf = buf;
392 spi_message_add_tail(&t[1], &m);
393
7d5230ea 394 mutex_lock(&flash->lock);
2f9f7628
ML
395
396 /* Wait until finished previous write command. */
bc018863
CG
397 if (wait_till_ready(flash)) {
398 mutex_unlock(&flash->lock);
2f9f7628 399 return 1;
bc018863 400 }
2f9f7628
ML
401
402 write_enable(flash);
403
2f9f7628
ML
404 /* Set up the opcode in the write buffer. */
405 flash->command[0] = OPCODE_PP;
406 flash->command[1] = to >> 16;
407 flash->command[2] = to >> 8;
408 flash->command[3] = to;
409
2f9f7628
ML
410 /* what page do we start with? */
411 page_offset = to % FLASH_PAGESIZE;
412
413 /* do all the bytes fit onto one page? */
414 if (page_offset + len <= FLASH_PAGESIZE) {
2f9f7628
ML
415 t[1].len = len;
416
417 spi_sync(flash->spi, &m);
418
2230b76b 419 *retlen = m.actual_length - CMD_SIZE;
2f9f7628
ML
420 } else {
421 u32 i;
422
423 /* the size of data remaining on the first page */
424 page_size = FLASH_PAGESIZE - page_offset;
425
2f9f7628
ML
426 t[1].len = page_size;
427 spi_sync(flash->spi, &m);
428
2230b76b 429 *retlen = m.actual_length - CMD_SIZE;
2f9f7628
ML
430
431 /* write everything in PAGESIZE chunks */
432 for (i = page_size; i < len; i += page_size) {
433 page_size = len - i;
434 if (page_size > FLASH_PAGESIZE)
435 page_size = FLASH_PAGESIZE;
436
437 /* write the next page to flash */
438 flash->command[1] = (to + i) >> 16;
439 flash->command[2] = (to + i) >> 8;
440 flash->command[3] = (to + i);
441
442 t[1].tx_buf = buf + i;
443 t[1].len = page_size;
444
445 wait_till_ready(flash);
446
447 write_enable(flash);
448
449 spi_sync(flash->spi, &m);
450
7111763d 451 if (retlen)
2230b76b 452 *retlen += m.actual_length - CMD_SIZE;
7d5230ea
DB
453 }
454 }
2f9f7628 455
7d5230ea 456 mutex_unlock(&flash->lock);
2f9f7628
ML
457
458 return 0;
459}
460
461
462/****************************************************************************/
463
464/*
465 * SPI device driver setup and teardown
466 */
467
468struct flash_info {
469 char *name;
fa0a8c71
DB
470
471 /* JEDEC id zero means "no ID" (most older chips); otherwise it has
472 * a high byte of zero plus three data bytes: the manufacturer id,
473 * then a two byte device id.
474 */
475 u32 jedec_id;
d0e8c47c 476 u16 ext_id;
fa0a8c71
DB
477
478 /* The size listed here is what works with OPCODE_SE, which isn't
479 * necessarily called a "sector" by the vendor.
480 */
2f9f7628 481 unsigned sector_size;
fa0a8c71
DB
482 u16 n_sectors;
483
484 u16 flags;
485#define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
2f9f7628
ML
486};
487
fa0a8c71
DB
488
489/* NOTE: double check command sets and memory organization when you add
490 * more flash chips. This current list focusses on newer chips, which
491 * have been converging on command sets which including JEDEC ID.
492 */
2f9f7628 493static struct flash_info __devinitdata m25p_data [] = {
fa0a8c71
DB
494
495 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
d0e8c47c
CG
496 { "at25fs010", 0x1f6601, 0, 32 * 1024, 4, SECT_4K, },
497 { "at25fs040", 0x1f6604, 0, 64 * 1024, 8, SECT_4K, },
fa0a8c71 498
d0e8c47c
CG
499 { "at25df041a", 0x1f4401, 0, 64 * 1024, 8, SECT_4K, },
500 { "at25df641", 0x1f4800, 0, 64 * 1024, 128, SECT_4K, },
fa0a8c71 501
d0e8c47c
CG
502 { "at26f004", 0x1f0400, 0, 64 * 1024, 8, SECT_4K, },
503 { "at26df081a", 0x1f4501, 0, 64 * 1024, 16, SECT_4K, },
504 { "at26df161a", 0x1f4601, 0, 64 * 1024, 32, SECT_4K, },
505 { "at26df321", 0x1f4701, 0, 64 * 1024, 64, SECT_4K, },
fa0a8c71
DB
506
507 /* Spansion -- single (large) sector size only, at least
508 * for the chips listed here (without boot sectors).
509 */
d0e8c47c
CG
510 { "s25sl004a", 0x010212, 0, 64 * 1024, 8, },
511 { "s25sl008a", 0x010213, 0, 64 * 1024, 16, },
512 { "s25sl016a", 0x010214, 0, 64 * 1024, 32, },
513 { "s25sl032a", 0x010215, 0, 64 * 1024, 64, },
514 { "s25sl064a", 0x010216, 0, 64 * 1024, 128, },
515 { "s25sl12800", 0x012018, 0x0300, 256 * 1024, 64, },
516 { "s25sl12801", 0x012018, 0x0301, 64 * 1024, 256, },
fa0a8c71
DB
517
518 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
d0e8c47c
CG
519 { "sst25vf040b", 0xbf258d, 0, 64 * 1024, 8, SECT_4K, },
520 { "sst25vf080b", 0xbf258e, 0, 64 * 1024, 16, SECT_4K, },
521 { "sst25vf016b", 0xbf2541, 0, 64 * 1024, 32, SECT_4K, },
522 { "sst25vf032b", 0xbf254a, 0, 64 * 1024, 64, SECT_4K, },
fa0a8c71
DB
523
524 /* ST Microelectronics -- newer production may have feature updates */
d0e8c47c
CG
525 { "m25p05", 0x202010, 0, 32 * 1024, 2, },
526 { "m25p10", 0x202011, 0, 32 * 1024, 4, },
527 { "m25p20", 0x202012, 0, 64 * 1024, 4, },
528 { "m25p40", 0x202013, 0, 64 * 1024, 8, },
529 { "m25p80", 0, 0, 64 * 1024, 16, },
530 { "m25p16", 0x202015, 0, 64 * 1024, 32, },
531 { "m25p32", 0x202016, 0, 64 * 1024, 64, },
532 { "m25p64", 0x202017, 0, 64 * 1024, 128, },
533 { "m25p128", 0x202018, 0, 256 * 1024, 64, },
534
535 { "m45pe80", 0x204014, 0, 64 * 1024, 16, },
536 { "m45pe16", 0x204015, 0, 64 * 1024, 32, },
537
538 { "m25pe80", 0x208014, 0, 64 * 1024, 16, },
539 { "m25pe16", 0x208015, 0, 64 * 1024, 32, SECT_4K, },
fa0a8c71 540
02d087db 541 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
d0e8c47c
CG
542 { "w25x10", 0xef3011, 0, 64 * 1024, 2, SECT_4K, },
543 { "w25x20", 0xef3012, 0, 64 * 1024, 4, SECT_4K, },
544 { "w25x40", 0xef3013, 0, 64 * 1024, 8, SECT_4K, },
545 { "w25x80", 0xef3014, 0, 64 * 1024, 16, SECT_4K, },
546 { "w25x16", 0xef3015, 0, 64 * 1024, 32, SECT_4K, },
547 { "w25x32", 0xef3016, 0, 64 * 1024, 64, SECT_4K, },
548 { "w25x64", 0xef3017, 0, 64 * 1024, 128, SECT_4K, },
2f9f7628
ML
549};
550
fa0a8c71
DB
551static struct flash_info *__devinit jedec_probe(struct spi_device *spi)
552{
553 int tmp;
554 u8 code = OPCODE_RDID;
daa84735 555 u8 id[5];
fa0a8c71 556 u32 jedec;
d0e8c47c 557 u16 ext_jedec;
fa0a8c71
DB
558 struct flash_info *info;
559
560 /* JEDEC also defines an optional "extended device information"
561 * string for after vendor-specific data, after the three bytes
562 * we use here. Supporting some chips might require using it.
563 */
daa84735 564 tmp = spi_write_then_read(spi, &code, 1, id, 5);
fa0a8c71
DB
565 if (tmp < 0) {
566 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
160bbab3 567 dev_name(&spi->dev), tmp);
fa0a8c71
DB
568 return NULL;
569 }
570 jedec = id[0];
571 jedec = jedec << 8;
572 jedec |= id[1];
573 jedec = jedec << 8;
574 jedec |= id[2];
575
d0e8c47c
CG
576 ext_jedec = id[3] << 8 | id[4];
577
fa0a8c71
DB
578 for (tmp = 0, info = m25p_data;
579 tmp < ARRAY_SIZE(m25p_data);
580 tmp++, info++) {
a3d3f73c 581 if (info->jedec_id == jedec) {
9168ab86 582 if (info->ext_id != 0 && info->ext_id != ext_jedec)
d0e8c47c 583 continue;
fa0a8c71 584 return info;
a3d3f73c 585 }
fa0a8c71
DB
586 }
587 dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
588 return NULL;
589}
590
591
2f9f7628
ML
592/*
593 * board specific setup should have ensured the SPI clock used here
594 * matches what the READ command supports, at least until this driver
595 * understands FAST_READ (for clocks over 25 MHz).
596 */
597static int __devinit m25p_probe(struct spi_device *spi)
598{
599 struct flash_platform_data *data;
600 struct m25p *flash;
601 struct flash_info *info;
602 unsigned i;
603
604 /* Platform data helps sort out which chip type we have, as
fa0a8c71
DB
605 * well as how this board partitions it. If we don't have
606 * a chip ID, try the JEDEC id commands; they'll work for most
607 * newer chips, even if we don't recognize the particular chip.
2f9f7628
ML
608 */
609 data = spi->dev.platform_data;
fa0a8c71
DB
610 if (data && data->type) {
611 for (i = 0, info = m25p_data;
612 i < ARRAY_SIZE(m25p_data);
613 i++, info++) {
614 if (strcmp(data->type, info->name) == 0)
615 break;
616 }
2f9f7628 617
fa0a8c71
DB
618 /* unrecognized chip? */
619 if (i == ARRAY_SIZE(m25p_data)) {
620 DEBUG(MTD_DEBUG_LEVEL0, "%s: unrecognized id %s\n",
160bbab3 621 dev_name(&spi->dev), data->type);
fa0a8c71
DB
622 info = NULL;
623
624 /* recognized; is that chip really what's there? */
625 } else if (info->jedec_id) {
626 struct flash_info *chip = jedec_probe(spi);
627
628 if (!chip || chip != info) {
629 dev_warn(&spi->dev, "found %s, expected %s\n",
630 chip ? chip->name : "UNKNOWN",
631 info->name);
632 info = NULL;
633 }
634 }
635 } else
636 info = jedec_probe(spi);
637
638 if (!info)
2f9f7628 639 return -ENODEV;
2f9f7628 640
e94b1766 641 flash = kzalloc(sizeof *flash, GFP_KERNEL);
2f9f7628
ML
642 if (!flash)
643 return -ENOMEM;
644
645 flash->spi = spi;
7d5230ea 646 mutex_init(&flash->lock);
2f9f7628
ML
647 dev_set_drvdata(&spi->dev, flash);
648
72289824
MH
649 /*
650 * Atmel serial flash tend to power up
651 * with the software protection bits set
652 */
653
654 if (info->jedec_id >> 16 == 0x1f) {
655 write_enable(flash);
656 write_sr(flash, 0);
657 }
658
fa0a8c71 659 if (data && data->name)
2f9f7628
ML
660 flash->mtd.name = data->name;
661 else
160bbab3 662 flash->mtd.name = dev_name(&spi->dev);
2f9f7628
ML
663
664 flash->mtd.type = MTD_NORFLASH;
783ed81f 665 flash->mtd.writesize = 1;
2f9f7628
ML
666 flash->mtd.flags = MTD_CAP_NORFLASH;
667 flash->mtd.size = info->sector_size * info->n_sectors;
2f9f7628
ML
668 flash->mtd.erase = m25p80_erase;
669 flash->mtd.read = m25p80_read;
670 flash->mtd.write = m25p80_write;
671
fa0a8c71
DB
672 /* prefer "small sector" erase if possible */
673 if (info->flags & SECT_4K) {
674 flash->erase_opcode = OPCODE_BE_4K;
675 flash->mtd.erasesize = 4096;
676 } else {
677 flash->erase_opcode = OPCODE_SE;
678 flash->mtd.erasesize = info->sector_size;
679 }
680
d85316ac
AB
681 dev_info(&spi->dev, "%s (%lld Kbytes)\n", info->name,
682 (long long)flash->mtd.size >> 10);
2f9f7628
ML
683
684 DEBUG(MTD_DEBUG_LEVEL2,
d85316ac 685 "mtd .name = %s, .size = 0x%llx (%lldMiB) "
02d087db 686 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
2f9f7628 687 flash->mtd.name,
d85316ac 688 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
2f9f7628
ML
689 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
690 flash->mtd.numeraseregions);
691
692 if (flash->mtd.numeraseregions)
693 for (i = 0; i < flash->mtd.numeraseregions; i++)
694 DEBUG(MTD_DEBUG_LEVEL2,
d85316ac 695 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
02d087db 696 ".erasesize = 0x%.8x (%uKiB), "
2f9f7628 697 ".numblocks = %d }\n",
d85316ac 698 i, (long long)flash->mtd.eraseregions[i].offset,
2f9f7628
ML
699 flash->mtd.eraseregions[i].erasesize,
700 flash->mtd.eraseregions[i].erasesize / 1024,
701 flash->mtd.eraseregions[i].numblocks);
702
703
704 /* partitions should match sector boundaries; and it may be good to
705 * use readonly partitions for writeprotected sectors (BP2..BP0).
706 */
707 if (mtd_has_partitions()) {
708 struct mtd_partition *parts = NULL;
709 int nr_parts = 0;
710
711#ifdef CONFIG_MTD_CMDLINE_PARTS
712 static const char *part_probes[] = { "cmdlinepart", NULL, };
713
714 nr_parts = parse_mtd_partitions(&flash->mtd,
715 part_probes, &parts, 0);
716#endif
717
718 if (nr_parts <= 0 && data && data->parts) {
719 parts = data->parts;
720 nr_parts = data->nr_parts;
721 }
722
723 if (nr_parts > 0) {
fa0a8c71 724 for (i = 0; i < nr_parts; i++) {
2f9f7628 725 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
d85316ac
AB
726 "{.name = %s, .offset = 0x%llx, "
727 ".size = 0x%llx (%lldKiB) }\n",
fa0a8c71 728 i, parts[i].name,
d85316ac
AB
729 (long long)parts[i].offset,
730 (long long)parts[i].size,
731 (long long)(parts[i].size >> 10));
2f9f7628
ML
732 }
733 flash->partitioned = 1;
734 return add_mtd_partitions(&flash->mtd, parts, nr_parts);
735 }
736 } else if (data->nr_parts)
737 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
738 data->nr_parts, data->name);
739
740 return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
741}
742
743
744static int __devexit m25p_remove(struct spi_device *spi)
745{
746 struct m25p *flash = dev_get_drvdata(&spi->dev);
747 int status;
748
749 /* Clean up MTD stuff. */
750 if (mtd_has_partitions() && flash->partitioned)
751 status = del_mtd_partitions(&flash->mtd);
752 else
753 status = del_mtd_device(&flash->mtd);
754 if (status == 0)
755 kfree(flash);
756 return 0;
757}
758
759
760static struct spi_driver m25p80_driver = {
761 .driver = {
762 .name = "m25p80",
763 .bus = &spi_bus_type,
764 .owner = THIS_MODULE,
765 },
766 .probe = m25p_probe,
767 .remove = __devexit_p(m25p_remove),
fa0a8c71
DB
768
769 /* REVISIT: many of these chips have deep power-down modes, which
770 * should clearly be entered on suspend() to minimize power use.
771 * And also when they're otherwise idle...
772 */
2f9f7628
ML
773};
774
775
776static int m25p80_init(void)
777{
778 return spi_register_driver(&m25p80_driver);
779}
780
781
782static void m25p80_exit(void)
783{
784 spi_unregister_driver(&m25p80_driver);
785}
786
787
788module_init(m25p80_init);
789module_exit(m25p80_exit);
790
791MODULE_LICENSE("GPL");
792MODULE_AUTHOR("Mike Lavender");
793MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");