blob: 98b0faf6696d6308f21331270780e219367b95b3 [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>
Artem Bityutskiyd85316a2008-12-18 14:10:05 +020023#include <linux/math64.h>
David Brownell7d5230e2007-06-24 15:09:13 -070024
Mike Lavender2f9f7622006-01-08 13:34:27 -080025#include <linux/mtd/mtd.h>
26#include <linux/mtd/partitions.h>
David Brownell7d5230e2007-06-24 15:09:13 -070027
Mike Lavender2f9f7622006-01-08 13:34:27 -080028#include <linux/spi/spi.h>
29#include <linux/spi/flash.h>
30
Mike Lavender2f9f7622006-01-08 13:34:27 -080031
Mike Lavender2f9f7622006-01-08 13:34:27 -080032#define FLASH_PAGESIZE 256
33
34/* Flash opcodes. */
David Brownellfa0a8c72007-06-24 15:12:35 -070035#define OPCODE_WREN 0x06 /* Write enable */
36#define OPCODE_RDSR 0x05 /* Read status register */
Michael Hennerich72289822008-07-03 23:54:42 -070037#define OPCODE_WRSR 0x01 /* Write status register 1 byte */
Bryan Wu2230b762008-04-25 12:07:32 +080038#define OPCODE_NORM_READ 0x03 /* Read data bytes (low frequency) */
David Brownellfa0a8c72007-06-24 15:12:35 -070039#define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */
40#define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */
Chen Gong78546432008-11-26 10:23:57 +000041#define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
David Woodhouse02d087d2007-06-28 22:38:38 +010042#define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
Chen Gong78546432008-11-26 10:23:57 +000043#define OPCODE_CHIP_ERASE 0xc7 /* Erase whole flash chip */
David Woodhouse02d087d2007-06-28 22:38:38 +010044#define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
Mike Lavender2f9f7622006-01-08 13:34:27 -080045#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 */
David Brownellfa0a8c72007-06-24 15:12:35 -070050/* meaning of other SR_* bits may differ between vendors */
Mike Lavender2f9f7622006-01-08 13:34:27 -080051#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
Bryan Wu2230b762008-04-25 12:07:32 +080058#define CMD_SIZE 4
Mike Lavender2f9f7622006-01-08 13:34:27 -080059
Bryan Wu2230b762008-04-25 12:07:32 +080060#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
Mike Lavender2f9f7622006-01-08 13:34:27 -080067
Mike Lavender2f9f7622006-01-08 13:34:27 -080068/****************************************************************************/
69
70struct m25p {
71 struct spi_device *spi;
David Brownell7d5230e2007-06-24 15:09:13 -070072 struct mutex lock;
Mike Lavender2f9f7622006-01-08 13:34:27 -080073 struct mtd_info mtd;
David Brownellfa0a8c72007-06-24 15:12:35 -070074 unsigned partitioned:1;
75 u8 erase_opcode;
Bryan Wu2230b762008-04-25 12:07:32 +080076 u8 command[CMD_SIZE + FAST_READ_DUMMY_BYTE];
Mike Lavender2f9f7622006-01-08 13:34:27 -080077};
78
79static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
80{
81 return container_of(mtd, struct m25p, mtd);
82}
83
84/****************************************************************************/
85
86/*
87 * Internal helper functions
88 */
89
90/*
91 * Read the status register, returning its value in the location
92 * Return the status register value.
93 * Returns negative if error occurred.
94 */
95static int read_sr(struct m25p *flash)
96{
97 ssize_t retval;
98 u8 code = OPCODE_RDSR;
99 u8 val;
100
101 retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
102
103 if (retval < 0) {
104 dev_err(&flash->spi->dev, "error %d reading SR\n",
105 (int) retval);
106 return retval;
107 }
108
109 return val;
110}
111
Michael Hennerich72289822008-07-03 23:54:42 -0700112/*
113 * Write status register 1 byte
114 * Returns negative if error occurred.
115 */
116static int write_sr(struct m25p *flash, u8 val)
117{
118 flash->command[0] = OPCODE_WRSR;
119 flash->command[1] = val;
120
121 return spi_write(flash->spi, flash->command, 2);
122}
Mike Lavender2f9f7622006-01-08 13:34:27 -0800123
124/*
125 * Set write enable latch with Write Enable command.
126 * Returns negative if error occurred.
127 */
128static inline int write_enable(struct m25p *flash)
129{
130 u8 code = OPCODE_WREN;
131
David Woodhouse8a1a6272008-10-20 09:26:16 +0100132 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800133}
134
135
136/*
137 * Service routine to read status register until ready, or timeout occurs.
138 * Returns non-zero if error.
139 */
140static int wait_till_ready(struct m25p *flash)
141{
142 int count;
143 int sr;
144
145 /* one chip guarantees max 5 msec wait here after page writes,
146 * but potentially three seconds (!) after page erase.
147 */
148 for (count = 0; count < MAX_READY_WAIT_COUNT; count++) {
149 if ((sr = read_sr(flash)) < 0)
150 break;
151 else if (!(sr & SR_WIP))
152 return 0;
153
154 /* REVISIT sometimes sleeping would be best */
155 }
156
157 return 1;
158}
159
Chen Gongfaff3752008-08-11 16:59:13 +0800160/*
161 * Erase the whole flash memory
162 *
163 * Returns 0 if successful, non-zero otherwise.
164 */
Chen Gong78546432008-11-26 10:23:57 +0000165static int erase_chip(struct m25p *flash)
Chen Gongfaff3752008-08-11 16:59:13 +0800166{
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200167 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %lldKiB\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000168 dev_name(&flash->spi->dev), __func__,
169 (long long)(flash->mtd.size >> 10));
Chen Gongfaff3752008-08-11 16:59:13 +0800170
171 /* Wait until finished previous write command. */
172 if (wait_till_ready(flash))
173 return 1;
174
175 /* Send write enable, then erase commands. */
176 write_enable(flash);
177
178 /* Set up command buffer. */
Chen Gong78546432008-11-26 10:23:57 +0000179 flash->command[0] = OPCODE_CHIP_ERASE;
Chen Gongfaff3752008-08-11 16:59:13 +0800180
181 spi_write(flash->spi, flash->command, 1);
182
183 return 0;
184}
Mike Lavender2f9f7622006-01-08 13:34:27 -0800185
186/*
187 * Erase one sector of flash memory at offset ``offset'' which is any
188 * address within the sector which should be erased.
189 *
190 * Returns 0 if successful, non-zero otherwise.
191 */
192static int erase_sector(struct m25p *flash, u32 offset)
193{
David Woodhouse02d087d2007-06-28 22:38:38 +0100194 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000195 dev_name(&flash->spi->dev), __func__,
David Brownellfa0a8c72007-06-24 15:12:35 -0700196 flash->mtd.erasesize / 1024, offset);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800197
198 /* Wait until finished previous write command. */
199 if (wait_till_ready(flash))
200 return 1;
201
202 /* Send write enable, then erase commands. */
203 write_enable(flash);
204
205 /* Set up command buffer. */
David Brownellfa0a8c72007-06-24 15:12:35 -0700206 flash->command[0] = flash->erase_opcode;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800207 flash->command[1] = offset >> 16;
208 flash->command[2] = offset >> 8;
209 flash->command[3] = offset;
210
Bryan Wu2230b762008-04-25 12:07:32 +0800211 spi_write(flash->spi, flash->command, CMD_SIZE);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800212
213 return 0;
214}
215
216/****************************************************************************/
217
218/*
219 * MTD implementation
220 */
221
222/*
223 * Erase an address range on the flash chip. The address range may extend
224 * one or more erase sectors. Return an error is there is a problem erasing.
225 */
226static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
227{
228 struct m25p *flash = mtd_to_m25p(mtd);
229 u32 addr,len;
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200230 uint32_t rem;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800231
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200232 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%llx, len %lld\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000233 dev_name(&flash->spi->dev), __func__, "at",
234 (long long)instr->addr, (long long)instr->len);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800235
236 /* sanity checks */
237 if (instr->addr + instr->len > flash->mtd.size)
238 return -EINVAL;
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200239 div_u64_rem(instr->len, mtd->erasesize, &rem);
240 if (rem)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800241 return -EINVAL;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800242
243 addr = instr->addr;
244 len = instr->len;
245
David Brownell7d5230e2007-06-24 15:09:13 -0700246 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800247
Chen Gong78546432008-11-26 10:23:57 +0000248 /* whole-chip erase? */
249 if (len == flash->mtd.size && erase_chip(flash)) {
Chen Gongfaff3752008-08-11 16:59:13 +0800250 instr->state = MTD_ERASE_FAILED;
251 mutex_unlock(&flash->lock);
252 return -EIO;
Chen Gong78546432008-11-26 10:23:57 +0000253
254 /* REVISIT in some cases we could speed up erasing large regions
255 * by using OPCODE_SE instead of OPCODE_BE_4K. We may have set up
256 * to use "small sector erase", but that's not always optimal.
257 */
258
259 /* "sector"-at-a-time erase */
Chen Gongfaff3752008-08-11 16:59:13 +0800260 } else {
261 while (len) {
262 if (erase_sector(flash, addr)) {
263 instr->state = MTD_ERASE_FAILED;
264 mutex_unlock(&flash->lock);
265 return -EIO;
266 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800267
Chen Gongfaff3752008-08-11 16:59:13 +0800268 addr += mtd->erasesize;
269 len -= mtd->erasesize;
270 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800271 }
272
David Brownell7d5230e2007-06-24 15:09:13 -0700273 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800274
275 instr->state = MTD_ERASE_DONE;
276 mtd_erase_callback(instr);
277
278 return 0;
279}
280
281/*
282 * Read an address range from the flash chip. The address range
283 * may be any size provided it is within the physical boundaries.
284 */
285static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
286 size_t *retlen, u_char *buf)
287{
288 struct m25p *flash = mtd_to_m25p(mtd);
289 struct spi_transfer t[2];
290 struct spi_message m;
291
292 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000293 dev_name(&flash->spi->dev), __func__, "from",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800294 (u32)from, len);
295
296 /* sanity checks */
297 if (!len)
298 return 0;
299
300 if (from + len > flash->mtd.size)
301 return -EINVAL;
302
Vitaly Wool8275c642006-01-08 13:34:28 -0800303 spi_message_init(&m);
304 memset(t, 0, (sizeof t));
305
Bryan Wu2230b762008-04-25 12:07:32 +0800306 /* NOTE:
307 * OPCODE_FAST_READ (if available) is faster.
308 * Should add 1 byte DUMMY_BYTE.
309 */
Vitaly Wool8275c642006-01-08 13:34:28 -0800310 t[0].tx_buf = flash->command;
Bryan Wu2230b762008-04-25 12:07:32 +0800311 t[0].len = CMD_SIZE + FAST_READ_DUMMY_BYTE;
Vitaly Wool8275c642006-01-08 13:34:28 -0800312 spi_message_add_tail(&t[0], &m);
313
314 t[1].rx_buf = buf;
315 t[1].len = len;
316 spi_message_add_tail(&t[1], &m);
317
318 /* Byte count starts at zero. */
319 if (retlen)
320 *retlen = 0;
321
David Brownell7d5230e2007-06-24 15:09:13 -0700322 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800323
324 /* Wait till previous write/erase is done. */
325 if (wait_till_ready(flash)) {
326 /* REVISIT status return?? */
David Brownell7d5230e2007-06-24 15:09:13 -0700327 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800328 return 1;
329 }
330
David Brownellfa0a8c72007-06-24 15:12:35 -0700331 /* FIXME switch to OPCODE_FAST_READ. It's required for higher
332 * clocks; and at this writing, every chip this driver handles
333 * supports that opcode.
334 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800335
336 /* Set up the write data buffer. */
337 flash->command[0] = OPCODE_READ;
338 flash->command[1] = from >> 16;
339 flash->command[2] = from >> 8;
340 flash->command[3] = from;
341
Mike Lavender2f9f7622006-01-08 13:34:27 -0800342 spi_sync(flash->spi, &m);
343
Bryan Wu2230b762008-04-25 12:07:32 +0800344 *retlen = m.actual_length - CMD_SIZE - FAST_READ_DUMMY_BYTE;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800345
David Brownell7d5230e2007-06-24 15:09:13 -0700346 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800347
348 return 0;
349}
350
351/*
352 * Write an address range to the flash chip. Data must be written in
353 * FLASH_PAGESIZE chunks. The address range may be any size provided
354 * it is within the physical boundaries.
355 */
356static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
357 size_t *retlen, const u_char *buf)
358{
359 struct m25p *flash = mtd_to_m25p(mtd);
360 u32 page_offset, page_size;
361 struct spi_transfer t[2];
362 struct spi_message m;
363
364 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000365 dev_name(&flash->spi->dev), __func__, "to",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800366 (u32)to, len);
367
368 if (retlen)
369 *retlen = 0;
370
371 /* sanity checks */
372 if (!len)
373 return(0);
374
375 if (to + len > flash->mtd.size)
376 return -EINVAL;
377
Vitaly Wool8275c642006-01-08 13:34:28 -0800378 spi_message_init(&m);
379 memset(t, 0, (sizeof t));
380
381 t[0].tx_buf = flash->command;
Bryan Wu2230b762008-04-25 12:07:32 +0800382 t[0].len = CMD_SIZE;
Vitaly Wool8275c642006-01-08 13:34:28 -0800383 spi_message_add_tail(&t[0], &m);
384
385 t[1].tx_buf = buf;
386 spi_message_add_tail(&t[1], &m);
387
David Brownell7d5230e2007-06-24 15:09:13 -0700388 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800389
390 /* Wait until finished previous write command. */
Chen Gongbc018862008-06-05 21:50:04 +0800391 if (wait_till_ready(flash)) {
392 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800393 return 1;
Chen Gongbc018862008-06-05 21:50:04 +0800394 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800395
396 write_enable(flash);
397
Mike Lavender2f9f7622006-01-08 13:34:27 -0800398 /* Set up the opcode in the write buffer. */
399 flash->command[0] = OPCODE_PP;
400 flash->command[1] = to >> 16;
401 flash->command[2] = to >> 8;
402 flash->command[3] = to;
403
Mike Lavender2f9f7622006-01-08 13:34:27 -0800404 /* what page do we start with? */
405 page_offset = to % FLASH_PAGESIZE;
406
407 /* do all the bytes fit onto one page? */
408 if (page_offset + len <= FLASH_PAGESIZE) {
Mike Lavender2f9f7622006-01-08 13:34:27 -0800409 t[1].len = len;
410
411 spi_sync(flash->spi, &m);
412
Bryan Wu2230b762008-04-25 12:07:32 +0800413 *retlen = m.actual_length - CMD_SIZE;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800414 } else {
415 u32 i;
416
417 /* the size of data remaining on the first page */
418 page_size = FLASH_PAGESIZE - page_offset;
419
Mike Lavender2f9f7622006-01-08 13:34:27 -0800420 t[1].len = page_size;
421 spi_sync(flash->spi, &m);
422
Bryan Wu2230b762008-04-25 12:07:32 +0800423 *retlen = m.actual_length - CMD_SIZE;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800424
425 /* write everything in PAGESIZE chunks */
426 for (i = page_size; i < len; i += page_size) {
427 page_size = len - i;
428 if (page_size > FLASH_PAGESIZE)
429 page_size = FLASH_PAGESIZE;
430
431 /* write the next page to flash */
432 flash->command[1] = (to + i) >> 16;
433 flash->command[2] = (to + i) >> 8;
434 flash->command[3] = (to + i);
435
436 t[1].tx_buf = buf + i;
437 t[1].len = page_size;
438
439 wait_till_ready(flash);
440
441 write_enable(flash);
442
443 spi_sync(flash->spi, &m);
444
David Brownell71117632006-01-08 13:34:29 -0800445 if (retlen)
Bryan Wu2230b762008-04-25 12:07:32 +0800446 *retlen += m.actual_length - CMD_SIZE;
David Brownell7d5230e2007-06-24 15:09:13 -0700447 }
448 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800449
David Brownell7d5230e2007-06-24 15:09:13 -0700450 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800451
452 return 0;
453}
454
455
456/****************************************************************************/
457
458/*
459 * SPI device driver setup and teardown
460 */
461
462struct flash_info {
463 char *name;
David Brownellfa0a8c72007-06-24 15:12:35 -0700464
465 /* JEDEC id zero means "no ID" (most older chips); otherwise it has
466 * a high byte of zero plus three data bytes: the manufacturer id,
467 * then a two byte device id.
468 */
469 u32 jedec_id;
Chen Gongd0e8c472008-08-11 16:59:15 +0800470 u16 ext_id;
David Brownellfa0a8c72007-06-24 15:12:35 -0700471
472 /* The size listed here is what works with OPCODE_SE, which isn't
473 * necessarily called a "sector" by the vendor.
474 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800475 unsigned sector_size;
David Brownellfa0a8c72007-06-24 15:12:35 -0700476 u16 n_sectors;
477
478 u16 flags;
479#define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800480};
481
David Brownellfa0a8c72007-06-24 15:12:35 -0700482
483/* NOTE: double check command sets and memory organization when you add
484 * more flash chips. This current list focusses on newer chips, which
485 * have been converging on command sets which including JEDEC ID.
486 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800487static struct flash_info __devinitdata m25p_data [] = {
David Brownellfa0a8c72007-06-24 15:12:35 -0700488
489 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
Chen Gongd0e8c472008-08-11 16:59:15 +0800490 { "at25fs010", 0x1f6601, 0, 32 * 1024, 4, SECT_4K, },
491 { "at25fs040", 0x1f6604, 0, 64 * 1024, 8, SECT_4K, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700492
Chen Gongd0e8c472008-08-11 16:59:15 +0800493 { "at25df041a", 0x1f4401, 0, 64 * 1024, 8, SECT_4K, },
494 { "at25df641", 0x1f4800, 0, 64 * 1024, 128, SECT_4K, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700495
Chen Gongd0e8c472008-08-11 16:59:15 +0800496 { "at26f004", 0x1f0400, 0, 64 * 1024, 8, SECT_4K, },
497 { "at26df081a", 0x1f4501, 0, 64 * 1024, 16, SECT_4K, },
498 { "at26df161a", 0x1f4601, 0, 64 * 1024, 32, SECT_4K, },
499 { "at26df321", 0x1f4701, 0, 64 * 1024, 64, SECT_4K, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700500
501 /* Spansion -- single (large) sector size only, at least
502 * for the chips listed here (without boot sectors).
503 */
Chen Gongd0e8c472008-08-11 16:59:15 +0800504 { "s25sl004a", 0x010212, 0, 64 * 1024, 8, },
505 { "s25sl008a", 0x010213, 0, 64 * 1024, 16, },
506 { "s25sl016a", 0x010214, 0, 64 * 1024, 32, },
507 { "s25sl032a", 0x010215, 0, 64 * 1024, 64, },
508 { "s25sl064a", 0x010216, 0, 64 * 1024, 128, },
509 { "s25sl12800", 0x012018, 0x0300, 256 * 1024, 64, },
510 { "s25sl12801", 0x012018, 0x0301, 64 * 1024, 256, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700511
512 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
Chen Gongd0e8c472008-08-11 16:59:15 +0800513 { "sst25vf040b", 0xbf258d, 0, 64 * 1024, 8, SECT_4K, },
514 { "sst25vf080b", 0xbf258e, 0, 64 * 1024, 16, SECT_4K, },
515 { "sst25vf016b", 0xbf2541, 0, 64 * 1024, 32, SECT_4K, },
516 { "sst25vf032b", 0xbf254a, 0, 64 * 1024, 64, SECT_4K, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700517
518 /* ST Microelectronics -- newer production may have feature updates */
Chen Gongd0e8c472008-08-11 16:59:15 +0800519 { "m25p05", 0x202010, 0, 32 * 1024, 2, },
520 { "m25p10", 0x202011, 0, 32 * 1024, 4, },
521 { "m25p20", 0x202012, 0, 64 * 1024, 4, },
522 { "m25p40", 0x202013, 0, 64 * 1024, 8, },
523 { "m25p80", 0, 0, 64 * 1024, 16, },
524 { "m25p16", 0x202015, 0, 64 * 1024, 32, },
525 { "m25p32", 0x202016, 0, 64 * 1024, 64, },
526 { "m25p64", 0x202017, 0, 64 * 1024, 128, },
527 { "m25p128", 0x202018, 0, 256 * 1024, 64, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700528
Chen Gongd0e8c472008-08-11 16:59:15 +0800529 { "m45pe80", 0x204014, 0, 64 * 1024, 16, },
530 { "m45pe16", 0x204015, 0, 64 * 1024, 32, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700531
Chen Gongd0e8c472008-08-11 16:59:15 +0800532 { "m25pe80", 0x208014, 0, 64 * 1024, 16, },
533 { "m25pe16", 0x208015, 0, 64 * 1024, 32, SECT_4K, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700534
David Woodhouse02d087d2007-06-28 22:38:38 +0100535 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
Chen Gongd0e8c472008-08-11 16:59:15 +0800536 { "w25x10", 0xef3011, 0, 64 * 1024, 2, SECT_4K, },
537 { "w25x20", 0xef3012, 0, 64 * 1024, 4, SECT_4K, },
538 { "w25x40", 0xef3013, 0, 64 * 1024, 8, SECT_4K, },
539 { "w25x80", 0xef3014, 0, 64 * 1024, 16, SECT_4K, },
540 { "w25x16", 0xef3015, 0, 64 * 1024, 32, SECT_4K, },
541 { "w25x32", 0xef3016, 0, 64 * 1024, 64, SECT_4K, },
542 { "w25x64", 0xef3017, 0, 64 * 1024, 128, SECT_4K, },
Mike Lavender2f9f7622006-01-08 13:34:27 -0800543};
544
David Brownellfa0a8c72007-06-24 15:12:35 -0700545static struct flash_info *__devinit jedec_probe(struct spi_device *spi)
546{
547 int tmp;
548 u8 code = OPCODE_RDID;
Chen Gongdaa84732008-09-16 14:14:12 +0800549 u8 id[5];
David Brownellfa0a8c72007-06-24 15:12:35 -0700550 u32 jedec;
Chen Gongd0e8c472008-08-11 16:59:15 +0800551 u16 ext_jedec;
David Brownellfa0a8c72007-06-24 15:12:35 -0700552 struct flash_info *info;
553
554 /* JEDEC also defines an optional "extended device information"
555 * string for after vendor-specific data, after the three bytes
556 * we use here. Supporting some chips might require using it.
557 */
Chen Gongdaa84732008-09-16 14:14:12 +0800558 tmp = spi_write_then_read(spi, &code, 1, id, 5);
David Brownellfa0a8c72007-06-24 15:12:35 -0700559 if (tmp < 0) {
560 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000561 dev_name(&spi->dev), tmp);
David Brownellfa0a8c72007-06-24 15:12:35 -0700562 return NULL;
563 }
564 jedec = id[0];
565 jedec = jedec << 8;
566 jedec |= id[1];
567 jedec = jedec << 8;
568 jedec |= id[2];
569
Chen Gongd0e8c472008-08-11 16:59:15 +0800570 ext_jedec = id[3] << 8 | id[4];
571
David Brownellfa0a8c72007-06-24 15:12:35 -0700572 for (tmp = 0, info = m25p_data;
573 tmp < ARRAY_SIZE(m25p_data);
574 tmp++, info++) {
Mike Frysingera3d3f732008-11-26 10:23:25 +0000575 if (info->jedec_id == jedec) {
Mike Frysinger9168ab82008-11-26 10:23:35 +0000576 if (info->ext_id != 0 && info->ext_id != ext_jedec)
Chen Gongd0e8c472008-08-11 16:59:15 +0800577 continue;
David Brownellfa0a8c72007-06-24 15:12:35 -0700578 return info;
Mike Frysingera3d3f732008-11-26 10:23:25 +0000579 }
David Brownellfa0a8c72007-06-24 15:12:35 -0700580 }
581 dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
582 return NULL;
583}
584
585
Mike Lavender2f9f7622006-01-08 13:34:27 -0800586/*
587 * board specific setup should have ensured the SPI clock used here
588 * matches what the READ command supports, at least until this driver
589 * understands FAST_READ (for clocks over 25 MHz).
590 */
591static int __devinit m25p_probe(struct spi_device *spi)
592{
593 struct flash_platform_data *data;
594 struct m25p *flash;
595 struct flash_info *info;
596 unsigned i;
597
598 /* Platform data helps sort out which chip type we have, as
David Brownellfa0a8c72007-06-24 15:12:35 -0700599 * well as how this board partitions it. If we don't have
600 * a chip ID, try the JEDEC id commands; they'll work for most
601 * newer chips, even if we don't recognize the particular chip.
Mike Lavender2f9f7622006-01-08 13:34:27 -0800602 */
603 data = spi->dev.platform_data;
David Brownellfa0a8c72007-06-24 15:12:35 -0700604 if (data && data->type) {
605 for (i = 0, info = m25p_data;
606 i < ARRAY_SIZE(m25p_data);
607 i++, info++) {
608 if (strcmp(data->type, info->name) == 0)
609 break;
610 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800611
David Brownellfa0a8c72007-06-24 15:12:35 -0700612 /* unrecognized chip? */
613 if (i == ARRAY_SIZE(m25p_data)) {
614 DEBUG(MTD_DEBUG_LEVEL0, "%s: unrecognized id %s\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000615 dev_name(&spi->dev), data->type);
David Brownellfa0a8c72007-06-24 15:12:35 -0700616 info = NULL;
617
618 /* recognized; is that chip really what's there? */
619 } else if (info->jedec_id) {
620 struct flash_info *chip = jedec_probe(spi);
621
622 if (!chip || chip != info) {
623 dev_warn(&spi->dev, "found %s, expected %s\n",
624 chip ? chip->name : "UNKNOWN",
625 info->name);
626 info = NULL;
627 }
628 }
629 } else
630 info = jedec_probe(spi);
631
632 if (!info)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800633 return -ENODEV;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800634
Christoph Lametere94b1762006-12-06 20:33:17 -0800635 flash = kzalloc(sizeof *flash, GFP_KERNEL);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800636 if (!flash)
637 return -ENOMEM;
638
639 flash->spi = spi;
David Brownell7d5230e2007-06-24 15:09:13 -0700640 mutex_init(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800641 dev_set_drvdata(&spi->dev, flash);
642
Michael Hennerich72289822008-07-03 23:54:42 -0700643 /*
644 * Atmel serial flash tend to power up
645 * with the software protection bits set
646 */
647
648 if (info->jedec_id >> 16 == 0x1f) {
649 write_enable(flash);
650 write_sr(flash, 0);
651 }
652
David Brownellfa0a8c72007-06-24 15:12:35 -0700653 if (data && data->name)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800654 flash->mtd.name = data->name;
655 else
Kay Sievers160bbab2008-12-23 10:00:14 +0000656 flash->mtd.name = dev_name(&spi->dev);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800657
658 flash->mtd.type = MTD_NORFLASH;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400659 flash->mtd.writesize = 1;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800660 flash->mtd.flags = MTD_CAP_NORFLASH;
661 flash->mtd.size = info->sector_size * info->n_sectors;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800662 flash->mtd.erase = m25p80_erase;
663 flash->mtd.read = m25p80_read;
664 flash->mtd.write = m25p80_write;
665
David Brownellfa0a8c72007-06-24 15:12:35 -0700666 /* prefer "small sector" erase if possible */
667 if (info->flags & SECT_4K) {
668 flash->erase_opcode = OPCODE_BE_4K;
669 flash->mtd.erasesize = 4096;
670 } else {
671 flash->erase_opcode = OPCODE_SE;
672 flash->mtd.erasesize = info->sector_size;
673 }
674
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200675 dev_info(&spi->dev, "%s (%lld Kbytes)\n", info->name,
676 (long long)flash->mtd.size >> 10);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800677
678 DEBUG(MTD_DEBUG_LEVEL2,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200679 "mtd .name = %s, .size = 0x%llx (%lldMiB) "
David Woodhouse02d087d2007-06-28 22:38:38 +0100680 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800681 flash->mtd.name,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200682 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
Mike Lavender2f9f7622006-01-08 13:34:27 -0800683 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
684 flash->mtd.numeraseregions);
685
686 if (flash->mtd.numeraseregions)
687 for (i = 0; i < flash->mtd.numeraseregions; i++)
688 DEBUG(MTD_DEBUG_LEVEL2,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200689 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
David Woodhouse02d087d2007-06-28 22:38:38 +0100690 ".erasesize = 0x%.8x (%uKiB), "
Mike Lavender2f9f7622006-01-08 13:34:27 -0800691 ".numblocks = %d }\n",
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200692 i, (long long)flash->mtd.eraseregions[i].offset,
Mike Lavender2f9f7622006-01-08 13:34:27 -0800693 flash->mtd.eraseregions[i].erasesize,
694 flash->mtd.eraseregions[i].erasesize / 1024,
695 flash->mtd.eraseregions[i].numblocks);
696
697
698 /* partitions should match sector boundaries; and it may be good to
699 * use readonly partitions for writeprotected sectors (BP2..BP0).
700 */
701 if (mtd_has_partitions()) {
702 struct mtd_partition *parts = NULL;
703 int nr_parts = 0;
704
David Brownella4b6d512009-03-04 12:01:41 -0800705 if (mtd_has_cmdlinepart()) {
706 static const char *part_probes[]
707 = { "cmdlinepart", NULL, };
Mike Lavender2f9f7622006-01-08 13:34:27 -0800708
David Brownella4b6d512009-03-04 12:01:41 -0800709 nr_parts = parse_mtd_partitions(&flash->mtd,
710 part_probes, &parts, 0);
711 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800712
713 if (nr_parts <= 0 && data && data->parts) {
714 parts = data->parts;
715 nr_parts = data->nr_parts;
716 }
717
718 if (nr_parts > 0) {
David Brownellfa0a8c72007-06-24 15:12:35 -0700719 for (i = 0; i < nr_parts; i++) {
Mike Lavender2f9f7622006-01-08 13:34:27 -0800720 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200721 "{.name = %s, .offset = 0x%llx, "
722 ".size = 0x%llx (%lldKiB) }\n",
David Brownellfa0a8c72007-06-24 15:12:35 -0700723 i, parts[i].name,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200724 (long long)parts[i].offset,
725 (long long)parts[i].size,
726 (long long)(parts[i].size >> 10));
Mike Lavender2f9f7622006-01-08 13:34:27 -0800727 }
728 flash->partitioned = 1;
729 return add_mtd_partitions(&flash->mtd, parts, nr_parts);
730 }
731 } else if (data->nr_parts)
732 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
733 data->nr_parts, data->name);
734
735 return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
736}
737
738
739static int __devexit m25p_remove(struct spi_device *spi)
740{
741 struct m25p *flash = dev_get_drvdata(&spi->dev);
742 int status;
743
744 /* Clean up MTD stuff. */
745 if (mtd_has_partitions() && flash->partitioned)
746 status = del_mtd_partitions(&flash->mtd);
747 else
748 status = del_mtd_device(&flash->mtd);
749 if (status == 0)
750 kfree(flash);
751 return 0;
752}
753
754
755static struct spi_driver m25p80_driver = {
756 .driver = {
757 .name = "m25p80",
758 .bus = &spi_bus_type,
759 .owner = THIS_MODULE,
760 },
761 .probe = m25p_probe,
762 .remove = __devexit_p(m25p_remove),
David Brownellfa0a8c72007-06-24 15:12:35 -0700763
764 /* REVISIT: many of these chips have deep power-down modes, which
765 * should clearly be entered on suspend() to minimize power use.
766 * And also when they're otherwise idle...
767 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800768};
769
770
771static int m25p80_init(void)
772{
773 return spi_register_driver(&m25p80_driver);
774}
775
776
777static void m25p80_exit(void)
778{
779 spi_unregister_driver(&m25p80_driver);
780}
781
782
783module_init(m25p80_init);
784module_exit(m25p80_exit);
785
786MODULE_LICENSE("GPL");
787MODULE_AUTHOR("Mike Lavender");
788MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");