blob: b35c3333e210b61878493d3fd052e1228b38d6db [file] [log] [blame]
Mike Lavender2f9f7622006-01-08 13:34:27 -08001/*
David Brownellfa0a8c72007-06-24 15:12:35 -07002 * MTD SPI driver for ST M25Pxx (and similar) serial flash chips
Mike Lavender2f9f7622006-01-08 13:34:27 -08003 *
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>
David Brownell7d5230e2007-06-24 15:09:13 -070022#include <linux/mutex.h>
23
Mike Lavender2f9f7622006-01-08 13:34:27 -080024#include <linux/mtd/mtd.h>
25#include <linux/mtd/partitions.h>
David Brownell7d5230e2007-06-24 15:09:13 -070026
Mike Lavender2f9f7622006-01-08 13:34:27 -080027#include <linux/spi/spi.h>
28#include <linux/spi/flash.h>
29
Mike Lavender2f9f7622006-01-08 13:34:27 -080030
Mike Lavender2f9f7622006-01-08 13:34:27 -080031#define FLASH_PAGESIZE 256
32
33/* Flash opcodes. */
David Brownellfa0a8c72007-06-24 15:12:35 -070034#define OPCODE_WREN 0x06 /* Write enable */
35#define OPCODE_RDSR 0x05 /* Read status register */
Michael Hennerich72289822008-07-03 23:54:42 -070036#define OPCODE_WRSR 0x01 /* Write status register 1 byte */
Bryan Wu2230b762008-04-25 12:07:32 +080037#define OPCODE_NORM_READ 0x03 /* Read data bytes (low frequency) */
David Brownellfa0a8c72007-06-24 15:12:35 -070038#define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */
39#define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */
David Woodhouse02d087d2007-06-28 22:38:38 +010040#define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
41#define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
42#define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
Mike Lavender2f9f7622006-01-08 13:34:27 -080043#define OPCODE_RDID 0x9f /* Read JEDEC ID */
44
45/* Status Register bits. */
46#define SR_WIP 1 /* Write in progress */
47#define SR_WEL 2 /* Write enable latch */
David Brownellfa0a8c72007-06-24 15:12:35 -070048/* meaning of other SR_* bits may differ between vendors */
Mike Lavender2f9f7622006-01-08 13:34:27 -080049#define SR_BP0 4 /* Block protect 0 */
50#define SR_BP1 8 /* Block protect 1 */
51#define SR_BP2 0x10 /* Block protect 2 */
52#define SR_SRWD 0x80 /* SR write protect */
53
54/* Define max times to check status register before we give up. */
55#define MAX_READY_WAIT_COUNT 100000
Bryan Wu2230b762008-04-25 12:07:32 +080056#define CMD_SIZE 4
Mike Lavender2f9f7622006-01-08 13:34:27 -080057
Bryan Wu2230b762008-04-25 12:07:32 +080058#ifdef CONFIG_M25PXX_USE_FAST_READ
59#define OPCODE_READ OPCODE_FAST_READ
60#define FAST_READ_DUMMY_BYTE 1
61#else
62#define OPCODE_READ OPCODE_NORM_READ
63#define FAST_READ_DUMMY_BYTE 0
64#endif
Mike Lavender2f9f7622006-01-08 13:34:27 -080065
66#ifdef CONFIG_MTD_PARTITIONS
67#define mtd_has_partitions() (1)
68#else
69#define mtd_has_partitions() (0)
70#endif
71
72/****************************************************************************/
73
74struct m25p {
75 struct spi_device *spi;
David Brownell7d5230e2007-06-24 15:09:13 -070076 struct mutex lock;
Mike Lavender2f9f7622006-01-08 13:34:27 -080077 struct mtd_info mtd;
David Brownellfa0a8c72007-06-24 15:12:35 -070078 unsigned partitioned:1;
79 u8 erase_opcode;
Bryan Wu2230b762008-04-25 12:07:32 +080080 u8 command[CMD_SIZE + FAST_READ_DUMMY_BYTE];
Mike Lavender2f9f7622006-01-08 13:34:27 -080081};
82
83static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
84{
85 return container_of(mtd, struct m25p, mtd);
86}
87
88/****************************************************************************/
89
90/*
91 * Internal helper functions
92 */
93
94/*
95 * Read the status register, returning its value in the location
96 * Return the status register value.
97 * Returns negative if error occurred.
98 */
99static int read_sr(struct m25p *flash)
100{
101 ssize_t retval;
102 u8 code = OPCODE_RDSR;
103 u8 val;
104
105 retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
106
107 if (retval < 0) {
108 dev_err(&flash->spi->dev, "error %d reading SR\n",
109 (int) retval);
110 return retval;
111 }
112
113 return val;
114}
115
Michael Hennerich72289822008-07-03 23:54:42 -0700116/*
117 * Write status register 1 byte
118 * Returns negative if error occurred.
119 */
120static int write_sr(struct m25p *flash, u8 val)
121{
122 flash->command[0] = OPCODE_WRSR;
123 flash->command[1] = val;
124
125 return spi_write(flash->spi, flash->command, 2);
126}
Mike Lavender2f9f7622006-01-08 13:34:27 -0800127
128/*
129 * Set write enable latch with Write Enable command.
130 * Returns negative if error occurred.
131 */
132static inline int write_enable(struct m25p *flash)
133{
134 u8 code = OPCODE_WREN;
135
136 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
137}
138
139
140/*
141 * Service routine to read status register until ready, or timeout occurs.
142 * Returns non-zero if error.
143 */
144static int wait_till_ready(struct m25p *flash)
145{
146 int count;
147 int sr;
148
149 /* one chip guarantees max 5 msec wait here after page writes,
150 * but potentially three seconds (!) after page erase.
151 */
152 for (count = 0; count < MAX_READY_WAIT_COUNT; count++) {
153 if ((sr = read_sr(flash)) < 0)
154 break;
155 else if (!(sr & SR_WIP))
156 return 0;
157
158 /* REVISIT sometimes sleeping would be best */
159 }
160
161 return 1;
162}
163
164
165/*
166 * Erase one sector of flash memory at offset ``offset'' which is any
167 * address within the sector which should be erased.
168 *
169 * Returns 0 if successful, non-zero otherwise.
170 */
171static int erase_sector(struct m25p *flash, u32 offset)
172{
David Woodhouse02d087d2007-06-28 22:38:38 +0100173 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700174 flash->spi->dev.bus_id, __func__,
David Brownellfa0a8c72007-06-24 15:12:35 -0700175 flash->mtd.erasesize / 1024, offset);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800176
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. */
David Brownellfa0a8c72007-06-24 15:12:35 -0700185 flash->command[0] = flash->erase_opcode;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800186 flash->command[1] = offset >> 16;
187 flash->command[2] = offset >> 8;
188 flash->command[3] = offset;
189
Bryan Wu2230b762008-04-25 12:07:32 +0800190 spi_write(flash->spi, flash->command, CMD_SIZE);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800191
192 return 0;
193}
194
195/****************************************************************************/
196
197/*
198 * MTD implementation
199 */
200
201/*
202 * Erase an address range on the flash chip. The address range may extend
203 * one or more erase sectors. Return an error is there is a problem erasing.
204 */
205static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
206{
207 struct m25p *flash = mtd_to_m25p(mtd);
208 u32 addr,len;
209
Andrew Morton19676ff2006-05-29 11:33:33 +0100210 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %d\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700211 flash->spi->dev.bus_id, __func__, "at",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800212 (u32)instr->addr, instr->len);
213
214 /* sanity checks */
215 if (instr->addr + instr->len > flash->mtd.size)
216 return -EINVAL;
217 if ((instr->addr % mtd->erasesize) != 0
218 || (instr->len % mtd->erasesize) != 0) {
219 return -EINVAL;
220 }
221
222 addr = instr->addr;
223 len = instr->len;
224
David Brownell7d5230e2007-06-24 15:09:13 -0700225 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800226
David Brownellfa0a8c72007-06-24 15:12:35 -0700227 /* REVISIT in some cases we could speed up erasing large regions
228 * by using OPCODE_SE instead of OPCODE_BE_4K
229 */
230
Mike Lavender2f9f7622006-01-08 13:34:27 -0800231 /* now erase those sectors */
232 while (len) {
233 if (erase_sector(flash, addr)) {
234 instr->state = MTD_ERASE_FAILED;
David Brownell7d5230e2007-06-24 15:09:13 -0700235 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800236 return -EIO;
237 }
238
239 addr += mtd->erasesize;
240 len -= mtd->erasesize;
241 }
242
David Brownell7d5230e2007-06-24 15:09:13 -0700243 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800244
245 instr->state = MTD_ERASE_DONE;
246 mtd_erase_callback(instr);
247
248 return 0;
249}
250
251/*
252 * Read an address range from the flash chip. The address range
253 * may be any size provided it is within the physical boundaries.
254 */
255static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
256 size_t *retlen, u_char *buf)
257{
258 struct m25p *flash = mtd_to_m25p(mtd);
259 struct spi_transfer t[2];
260 struct spi_message m;
261
262 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700263 flash->spi->dev.bus_id, __func__, "from",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800264 (u32)from, len);
265
266 /* sanity checks */
267 if (!len)
268 return 0;
269
270 if (from + len > flash->mtd.size)
271 return -EINVAL;
272
Vitaly Wool8275c642006-01-08 13:34:28 -0800273 spi_message_init(&m);
274 memset(t, 0, (sizeof t));
275
Bryan Wu2230b762008-04-25 12:07:32 +0800276 /* NOTE:
277 * OPCODE_FAST_READ (if available) is faster.
278 * Should add 1 byte DUMMY_BYTE.
279 */
Vitaly Wool8275c642006-01-08 13:34:28 -0800280 t[0].tx_buf = flash->command;
Bryan Wu2230b762008-04-25 12:07:32 +0800281 t[0].len = CMD_SIZE + FAST_READ_DUMMY_BYTE;
Vitaly Wool8275c642006-01-08 13:34:28 -0800282 spi_message_add_tail(&t[0], &m);
283
284 t[1].rx_buf = buf;
285 t[1].len = len;
286 spi_message_add_tail(&t[1], &m);
287
288 /* Byte count starts at zero. */
289 if (retlen)
290 *retlen = 0;
291
David Brownell7d5230e2007-06-24 15:09:13 -0700292 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800293
294 /* Wait till previous write/erase is done. */
295 if (wait_till_ready(flash)) {
296 /* REVISIT status return?? */
David Brownell7d5230e2007-06-24 15:09:13 -0700297 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800298 return 1;
299 }
300
David Brownellfa0a8c72007-06-24 15:12:35 -0700301 /* FIXME switch to OPCODE_FAST_READ. It's required for higher
302 * clocks; and at this writing, every chip this driver handles
303 * supports that opcode.
304 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800305
306 /* Set up the write data buffer. */
307 flash->command[0] = OPCODE_READ;
308 flash->command[1] = from >> 16;
309 flash->command[2] = from >> 8;
310 flash->command[3] = from;
311
Mike Lavender2f9f7622006-01-08 13:34:27 -0800312 spi_sync(flash->spi, &m);
313
Bryan Wu2230b762008-04-25 12:07:32 +0800314 *retlen = m.actual_length - CMD_SIZE - FAST_READ_DUMMY_BYTE;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800315
David Brownell7d5230e2007-06-24 15:09:13 -0700316 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800317
318 return 0;
319}
320
321/*
322 * Write an address range to the flash chip. Data must be written in
323 * FLASH_PAGESIZE chunks. The address range may be any size provided
324 * it is within the physical boundaries.
325 */
326static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
327 size_t *retlen, const u_char *buf)
328{
329 struct m25p *flash = mtd_to_m25p(mtd);
330 u32 page_offset, page_size;
331 struct spi_transfer t[2];
332 struct spi_message m;
333
334 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700335 flash->spi->dev.bus_id, __func__, "to",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800336 (u32)to, len);
337
338 if (retlen)
339 *retlen = 0;
340
341 /* sanity checks */
342 if (!len)
343 return(0);
344
345 if (to + len > flash->mtd.size)
346 return -EINVAL;
347
Vitaly Wool8275c642006-01-08 13:34:28 -0800348 spi_message_init(&m);
349 memset(t, 0, (sizeof t));
350
351 t[0].tx_buf = flash->command;
Bryan Wu2230b762008-04-25 12:07:32 +0800352 t[0].len = CMD_SIZE;
Vitaly Wool8275c642006-01-08 13:34:28 -0800353 spi_message_add_tail(&t[0], &m);
354
355 t[1].tx_buf = buf;
356 spi_message_add_tail(&t[1], &m);
357
David Brownell7d5230e2007-06-24 15:09:13 -0700358 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800359
360 /* Wait until finished previous write command. */
Chen Gongbc018862008-06-05 21:50:04 +0800361 if (wait_till_ready(flash)) {
362 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800363 return 1;
Chen Gongbc018862008-06-05 21:50:04 +0800364 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800365
366 write_enable(flash);
367
Mike Lavender2f9f7622006-01-08 13:34:27 -0800368 /* Set up the opcode in the write buffer. */
369 flash->command[0] = OPCODE_PP;
370 flash->command[1] = to >> 16;
371 flash->command[2] = to >> 8;
372 flash->command[3] = to;
373
Mike Lavender2f9f7622006-01-08 13:34:27 -0800374 /* what page do we start with? */
375 page_offset = to % FLASH_PAGESIZE;
376
377 /* do all the bytes fit onto one page? */
378 if (page_offset + len <= FLASH_PAGESIZE) {
Mike Lavender2f9f7622006-01-08 13:34:27 -0800379 t[1].len = len;
380
381 spi_sync(flash->spi, &m);
382
Bryan Wu2230b762008-04-25 12:07:32 +0800383 *retlen = m.actual_length - CMD_SIZE;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800384 } else {
385 u32 i;
386
387 /* the size of data remaining on the first page */
388 page_size = FLASH_PAGESIZE - page_offset;
389
Mike Lavender2f9f7622006-01-08 13:34:27 -0800390 t[1].len = page_size;
391 spi_sync(flash->spi, &m);
392
Bryan Wu2230b762008-04-25 12:07:32 +0800393 *retlen = m.actual_length - CMD_SIZE;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800394
395 /* write everything in PAGESIZE chunks */
396 for (i = page_size; i < len; i += page_size) {
397 page_size = len - i;
398 if (page_size > FLASH_PAGESIZE)
399 page_size = FLASH_PAGESIZE;
400
401 /* write the next page to flash */
402 flash->command[1] = (to + i) >> 16;
403 flash->command[2] = (to + i) >> 8;
404 flash->command[3] = (to + i);
405
406 t[1].tx_buf = buf + i;
407 t[1].len = page_size;
408
409 wait_till_ready(flash);
410
411 write_enable(flash);
412
413 spi_sync(flash->spi, &m);
414
David Brownell71117632006-01-08 13:34:29 -0800415 if (retlen)
Bryan Wu2230b762008-04-25 12:07:32 +0800416 *retlen += m.actual_length - CMD_SIZE;
David Brownell7d5230e2007-06-24 15:09:13 -0700417 }
418 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800419
David Brownell7d5230e2007-06-24 15:09:13 -0700420 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800421
422 return 0;
423}
424
425
426/****************************************************************************/
427
428/*
429 * SPI device driver setup and teardown
430 */
431
432struct flash_info {
433 char *name;
David Brownellfa0a8c72007-06-24 15:12:35 -0700434
435 /* JEDEC id zero means "no ID" (most older chips); otherwise it has
436 * a high byte of zero plus three data bytes: the manufacturer id,
437 * then a two byte device id.
438 */
439 u32 jedec_id;
440
441 /* The size listed here is what works with OPCODE_SE, which isn't
442 * necessarily called a "sector" by the vendor.
443 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800444 unsigned sector_size;
David Brownellfa0a8c72007-06-24 15:12:35 -0700445 u16 n_sectors;
446
447 u16 flags;
448#define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800449};
450
David Brownellfa0a8c72007-06-24 15:12:35 -0700451
452/* NOTE: double check command sets and memory organization when you add
453 * more flash chips. This current list focusses on newer chips, which
454 * have been converging on command sets which including JEDEC ID.
455 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800456static struct flash_info __devinitdata m25p_data [] = {
David Brownellfa0a8c72007-06-24 15:12:35 -0700457
458 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
459 { "at25fs010", 0x1f6601, 32 * 1024, 4, SECT_4K, },
460 { "at25fs040", 0x1f6604, 64 * 1024, 8, SECT_4K, },
461
462 { "at25df041a", 0x1f4401, 64 * 1024, 8, SECT_4K, },
Michael Hennerich3887ed52008-04-25 12:07:33 +0800463 { "at25df641", 0x1f4800, 64 * 1024, 128, SECT_4K, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700464
465 { "at26f004", 0x1f0400, 64 * 1024, 8, SECT_4K, },
466 { "at26df081a", 0x1f4501, 64 * 1024, 16, SECT_4K, },
467 { "at26df161a", 0x1f4601, 64 * 1024, 32, SECT_4K, },
468 { "at26df321", 0x1f4701, 64 * 1024, 64, SECT_4K, },
469
470 /* Spansion -- single (large) sector size only, at least
471 * for the chips listed here (without boot sectors).
472 */
473 { "s25sl004a", 0x010212, 64 * 1024, 8, },
474 { "s25sl008a", 0x010213, 64 * 1024, 16, },
475 { "s25sl016a", 0x010214, 64 * 1024, 32, },
476 { "s25sl032a", 0x010215, 64 * 1024, 64, },
477 { "s25sl064a", 0x010216, 64 * 1024, 128, },
478
479 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
480 { "sst25vf040b", 0xbf258d, 64 * 1024, 8, SECT_4K, },
481 { "sst25vf080b", 0xbf258e, 64 * 1024, 16, SECT_4K, },
482 { "sst25vf016b", 0xbf2541, 64 * 1024, 32, SECT_4K, },
483 { "sst25vf032b", 0xbf254a, 64 * 1024, 64, SECT_4K, },
484
485 /* ST Microelectronics -- newer production may have feature updates */
486 { "m25p05", 0x202010, 32 * 1024, 2, },
487 { "m25p10", 0x202011, 32 * 1024, 4, },
488 { "m25p20", 0x202012, 64 * 1024, 4, },
489 { "m25p40", 0x202013, 64 * 1024, 8, },
490 { "m25p80", 0, 64 * 1024, 16, },
491 { "m25p16", 0x202015, 64 * 1024, 32, },
492 { "m25p32", 0x202016, 64 * 1024, 64, },
493 { "m25p64", 0x202017, 64 * 1024, 128, },
494 { "m25p128", 0x202018, 256 * 1024, 64, },
495
496 { "m45pe80", 0x204014, 64 * 1024, 16, },
497 { "m45pe16", 0x204015, 64 * 1024, 32, },
498
499 { "m25pe80", 0x208014, 64 * 1024, 16, },
500 { "m25pe16", 0x208015, 64 * 1024, 32, SECT_4K, },
501
David Woodhouse02d087d2007-06-28 22:38:38 +0100502 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
David Brownellfa0a8c72007-06-24 15:12:35 -0700503 { "w25x10", 0xef3011, 64 * 1024, 2, SECT_4K, },
504 { "w25x20", 0xef3012, 64 * 1024, 4, SECT_4K, },
505 { "w25x40", 0xef3013, 64 * 1024, 8, SECT_4K, },
506 { "w25x80", 0xef3014, 64 * 1024, 16, SECT_4K, },
507 { "w25x16", 0xef3015, 64 * 1024, 32, SECT_4K, },
508 { "w25x32", 0xef3016, 64 * 1024, 64, SECT_4K, },
509 { "w25x64", 0xef3017, 64 * 1024, 128, SECT_4K, },
Mike Lavender2f9f7622006-01-08 13:34:27 -0800510};
511
David Brownellfa0a8c72007-06-24 15:12:35 -0700512static struct flash_info *__devinit jedec_probe(struct spi_device *spi)
513{
514 int tmp;
515 u8 code = OPCODE_RDID;
516 u8 id[3];
517 u32 jedec;
518 struct flash_info *info;
519
520 /* JEDEC also defines an optional "extended device information"
521 * string for after vendor-specific data, after the three bytes
522 * we use here. Supporting some chips might require using it.
523 */
524 tmp = spi_write_then_read(spi, &code, 1, id, 3);
525 if (tmp < 0) {
526 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
527 spi->dev.bus_id, tmp);
528 return NULL;
529 }
530 jedec = id[0];
531 jedec = jedec << 8;
532 jedec |= id[1];
533 jedec = jedec << 8;
534 jedec |= id[2];
535
536 for (tmp = 0, info = m25p_data;
537 tmp < ARRAY_SIZE(m25p_data);
538 tmp++, info++) {
539 if (info->jedec_id == jedec)
540 return info;
541 }
542 dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
543 return NULL;
544}
545
546
Mike Lavender2f9f7622006-01-08 13:34:27 -0800547/*
548 * board specific setup should have ensured the SPI clock used here
549 * matches what the READ command supports, at least until this driver
550 * understands FAST_READ (for clocks over 25 MHz).
551 */
552static int __devinit m25p_probe(struct spi_device *spi)
553{
554 struct flash_platform_data *data;
555 struct m25p *flash;
556 struct flash_info *info;
557 unsigned i;
558
559 /* Platform data helps sort out which chip type we have, as
David Brownellfa0a8c72007-06-24 15:12:35 -0700560 * well as how this board partitions it. If we don't have
561 * a chip ID, try the JEDEC id commands; they'll work for most
562 * newer chips, even if we don't recognize the particular chip.
Mike Lavender2f9f7622006-01-08 13:34:27 -0800563 */
564 data = spi->dev.platform_data;
David Brownellfa0a8c72007-06-24 15:12:35 -0700565 if (data && data->type) {
566 for (i = 0, info = m25p_data;
567 i < ARRAY_SIZE(m25p_data);
568 i++, info++) {
569 if (strcmp(data->type, info->name) == 0)
570 break;
571 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800572
David Brownellfa0a8c72007-06-24 15:12:35 -0700573 /* unrecognized chip? */
574 if (i == ARRAY_SIZE(m25p_data)) {
575 DEBUG(MTD_DEBUG_LEVEL0, "%s: unrecognized id %s\n",
576 spi->dev.bus_id, data->type);
577 info = NULL;
578
579 /* recognized; is that chip really what's there? */
580 } else if (info->jedec_id) {
581 struct flash_info *chip = jedec_probe(spi);
582
583 if (!chip || chip != info) {
584 dev_warn(&spi->dev, "found %s, expected %s\n",
585 chip ? chip->name : "UNKNOWN",
586 info->name);
587 info = NULL;
588 }
589 }
590 } else
591 info = jedec_probe(spi);
592
593 if (!info)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800594 return -ENODEV;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800595
Christoph Lametere94b1762006-12-06 20:33:17 -0800596 flash = kzalloc(sizeof *flash, GFP_KERNEL);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800597 if (!flash)
598 return -ENOMEM;
599
600 flash->spi = spi;
David Brownell7d5230e2007-06-24 15:09:13 -0700601 mutex_init(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800602 dev_set_drvdata(&spi->dev, flash);
603
Michael Hennerich72289822008-07-03 23:54:42 -0700604 /*
605 * Atmel serial flash tend to power up
606 * with the software protection bits set
607 */
608
609 if (info->jedec_id >> 16 == 0x1f) {
610 write_enable(flash);
611 write_sr(flash, 0);
612 }
613
David Brownellfa0a8c72007-06-24 15:12:35 -0700614 if (data && data->name)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800615 flash->mtd.name = data->name;
616 else
617 flash->mtd.name = spi->dev.bus_id;
618
619 flash->mtd.type = MTD_NORFLASH;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400620 flash->mtd.writesize = 1;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800621 flash->mtd.flags = MTD_CAP_NORFLASH;
622 flash->mtd.size = info->sector_size * info->n_sectors;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800623 flash->mtd.erase = m25p80_erase;
624 flash->mtd.read = m25p80_read;
625 flash->mtd.write = m25p80_write;
626
David Brownellfa0a8c72007-06-24 15:12:35 -0700627 /* prefer "small sector" erase if possible */
628 if (info->flags & SECT_4K) {
629 flash->erase_opcode = OPCODE_BE_4K;
630 flash->mtd.erasesize = 4096;
631 } else {
632 flash->erase_opcode = OPCODE_SE;
633 flash->mtd.erasesize = info->sector_size;
634 }
635
Mike Lavender2f9f7622006-01-08 13:34:27 -0800636 dev_info(&spi->dev, "%s (%d Kbytes)\n", info->name,
637 flash->mtd.size / 1024);
638
639 DEBUG(MTD_DEBUG_LEVEL2,
David Woodhouse02d087d2007-06-28 22:38:38 +0100640 "mtd .name = %s, .size = 0x%.8x (%uMiB) "
641 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800642 flash->mtd.name,
643 flash->mtd.size, flash->mtd.size / (1024*1024),
644 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
645 flash->mtd.numeraseregions);
646
647 if (flash->mtd.numeraseregions)
648 for (i = 0; i < flash->mtd.numeraseregions; i++)
649 DEBUG(MTD_DEBUG_LEVEL2,
650 "mtd.eraseregions[%d] = { .offset = 0x%.8x, "
David Woodhouse02d087d2007-06-28 22:38:38 +0100651 ".erasesize = 0x%.8x (%uKiB), "
Mike Lavender2f9f7622006-01-08 13:34:27 -0800652 ".numblocks = %d }\n",
653 i, flash->mtd.eraseregions[i].offset,
654 flash->mtd.eraseregions[i].erasesize,
655 flash->mtd.eraseregions[i].erasesize / 1024,
656 flash->mtd.eraseregions[i].numblocks);
657
658
659 /* partitions should match sector boundaries; and it may be good to
660 * use readonly partitions for writeprotected sectors (BP2..BP0).
661 */
662 if (mtd_has_partitions()) {
663 struct mtd_partition *parts = NULL;
664 int nr_parts = 0;
665
666#ifdef CONFIG_MTD_CMDLINE_PARTS
667 static const char *part_probes[] = { "cmdlinepart", NULL, };
668
669 nr_parts = parse_mtd_partitions(&flash->mtd,
670 part_probes, &parts, 0);
671#endif
672
673 if (nr_parts <= 0 && data && data->parts) {
674 parts = data->parts;
675 nr_parts = data->nr_parts;
676 }
677
678 if (nr_parts > 0) {
David Brownellfa0a8c72007-06-24 15:12:35 -0700679 for (i = 0; i < nr_parts; i++) {
Mike Lavender2f9f7622006-01-08 13:34:27 -0800680 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
681 "{.name = %s, .offset = 0x%.8x, "
David Woodhouse02d087d2007-06-28 22:38:38 +0100682 ".size = 0x%.8x (%uKiB) }\n",
David Brownellfa0a8c72007-06-24 15:12:35 -0700683 i, parts[i].name,
684 parts[i].offset,
685 parts[i].size,
686 parts[i].size / 1024);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800687 }
688 flash->partitioned = 1;
689 return add_mtd_partitions(&flash->mtd, parts, nr_parts);
690 }
691 } else if (data->nr_parts)
692 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
693 data->nr_parts, data->name);
694
695 return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
696}
697
698
699static int __devexit m25p_remove(struct spi_device *spi)
700{
701 struct m25p *flash = dev_get_drvdata(&spi->dev);
702 int status;
703
704 /* Clean up MTD stuff. */
705 if (mtd_has_partitions() && flash->partitioned)
706 status = del_mtd_partitions(&flash->mtd);
707 else
708 status = del_mtd_device(&flash->mtd);
709 if (status == 0)
710 kfree(flash);
711 return 0;
712}
713
714
715static struct spi_driver m25p80_driver = {
716 .driver = {
717 .name = "m25p80",
718 .bus = &spi_bus_type,
719 .owner = THIS_MODULE,
720 },
721 .probe = m25p_probe,
722 .remove = __devexit_p(m25p_remove),
David Brownellfa0a8c72007-06-24 15:12:35 -0700723
724 /* REVISIT: many of these chips have deep power-down modes, which
725 * should clearly be entered on suspend() to minimize power use.
726 * And also when they're otherwise idle...
727 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800728};
729
730
731static int m25p80_init(void)
732{
733 return spi_register_driver(&m25p80_driver);
734}
735
736
737static void m25p80_exit(void)
738{
739 spi_unregister_driver(&m25p80_driver);
740}
741
742
743module_init(m25p80_init);
744module_exit(m25p80_exit);
745
746MODULE_LICENSE("GPL");
747MODULE_AUTHOR("Mike Lavender");
748MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");