blob: 9b78869cd8ab76f7522e4b01275788654fceb888 [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
Graf Yang49aac4a2009-06-15 08:23:41 +000047/* Used for SST flashes only. */
48#define OPCODE_BP 0x02 /* Byte program */
49#define OPCODE_WRDI 0x04 /* Write disable */
50#define OPCODE_AAI_WP 0xad /* Auto address increment word program */
51
Mike Lavender2f9f7622006-01-08 13:34:27 -080052/* Status Register bits. */
53#define SR_WIP 1 /* Write in progress */
54#define SR_WEL 2 /* Write enable latch */
David Brownellfa0a8c72007-06-24 15:12:35 -070055/* meaning of other SR_* bits may differ between vendors */
Mike Lavender2f9f7622006-01-08 13:34:27 -080056#define SR_BP0 4 /* Block protect 0 */
57#define SR_BP1 8 /* Block protect 1 */
58#define SR_BP2 0x10 /* Block protect 2 */
59#define SR_SRWD 0x80 /* SR write protect */
60
61/* Define max times to check status register before we give up. */
Steven A. Falco89bb8712009-06-26 12:42:47 -040062#define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
Bryan Wu2230b762008-04-25 12:07:32 +080063#define CMD_SIZE 4
Mike Lavender2f9f7622006-01-08 13:34:27 -080064
Bryan Wu2230b762008-04-25 12:07:32 +080065#ifdef CONFIG_M25PXX_USE_FAST_READ
66#define OPCODE_READ OPCODE_FAST_READ
67#define FAST_READ_DUMMY_BYTE 1
68#else
69#define OPCODE_READ OPCODE_NORM_READ
70#define FAST_READ_DUMMY_BYTE 0
71#endif
Mike Lavender2f9f7622006-01-08 13:34:27 -080072
Mike Lavender2f9f7622006-01-08 13:34:27 -080073/****************************************************************************/
74
75struct m25p {
76 struct spi_device *spi;
David Brownell7d5230e2007-06-24 15:09:13 -070077 struct mutex lock;
Mike Lavender2f9f7622006-01-08 13:34:27 -080078 struct mtd_info mtd;
David Brownellfa0a8c72007-06-24 15:12:35 -070079 unsigned partitioned:1;
80 u8 erase_opcode;
Bryan Wu2230b762008-04-25 12:07:32 +080081 u8 command[CMD_SIZE + FAST_READ_DUMMY_BYTE];
Mike Lavender2f9f7622006-01-08 13:34:27 -080082};
83
84static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
85{
86 return container_of(mtd, struct m25p, mtd);
87}
88
89/****************************************************************************/
90
91/*
92 * Internal helper functions
93 */
94
95/*
96 * Read the status register, returning its value in the location
97 * Return the status register value.
98 * Returns negative if error occurred.
99 */
100static int read_sr(struct m25p *flash)
101{
102 ssize_t retval;
103 u8 code = OPCODE_RDSR;
104 u8 val;
105
106 retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
107
108 if (retval < 0) {
109 dev_err(&flash->spi->dev, "error %d reading SR\n",
110 (int) retval);
111 return retval;
112 }
113
114 return val;
115}
116
Michael Hennerich72289822008-07-03 23:54:42 -0700117/*
118 * Write status register 1 byte
119 * Returns negative if error occurred.
120 */
121static int write_sr(struct m25p *flash, u8 val)
122{
123 flash->command[0] = OPCODE_WRSR;
124 flash->command[1] = val;
125
126 return spi_write(flash->spi, flash->command, 2);
127}
Mike Lavender2f9f7622006-01-08 13:34:27 -0800128
129/*
130 * Set write enable latch with Write Enable command.
131 * Returns negative if error occurred.
132 */
133static inline int write_enable(struct m25p *flash)
134{
135 u8 code = OPCODE_WREN;
136
David Woodhouse8a1a6272008-10-20 09:26:16 +0100137 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800138}
139
Graf Yang49aac4a2009-06-15 08:23:41 +0000140/*
141 * Send write disble instruction to the chip.
142 */
143static inline int write_disable(struct m25p *flash)
144{
145 u8 code = OPCODE_WRDI;
146
147 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
148}
Mike Lavender2f9f7622006-01-08 13:34:27 -0800149
150/*
151 * Service routine to read status register until ready, or timeout occurs.
152 * Returns non-zero if error.
153 */
154static int wait_till_ready(struct m25p *flash)
155{
Peter Hortoncd1a6de2009-05-08 13:51:53 +0100156 unsigned long deadline;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800157 int sr;
158
Peter Hortoncd1a6de2009-05-08 13:51:53 +0100159 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
160
161 do {
Mike Lavender2f9f7622006-01-08 13:34:27 -0800162 if ((sr = read_sr(flash)) < 0)
163 break;
164 else if (!(sr & SR_WIP))
165 return 0;
166
Peter Hortoncd1a6de2009-05-08 13:51:53 +0100167 cond_resched();
168
169 } while (!time_after_eq(jiffies, deadline));
Mike Lavender2f9f7622006-01-08 13:34:27 -0800170
171 return 1;
172}
173
Chen Gongfaff3752008-08-11 16:59:13 +0800174/*
175 * Erase the whole flash memory
176 *
177 * Returns 0 if successful, non-zero otherwise.
178 */
Chen Gong78546432008-11-26 10:23:57 +0000179static int erase_chip(struct m25p *flash)
Chen Gongfaff3752008-08-11 16:59:13 +0800180{
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200181 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %lldKiB\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000182 dev_name(&flash->spi->dev), __func__,
183 (long long)(flash->mtd.size >> 10));
Chen Gongfaff3752008-08-11 16:59:13 +0800184
185 /* Wait until finished previous write command. */
186 if (wait_till_ready(flash))
187 return 1;
188
189 /* Send write enable, then erase commands. */
190 write_enable(flash);
191
192 /* Set up command buffer. */
Chen Gong78546432008-11-26 10:23:57 +0000193 flash->command[0] = OPCODE_CHIP_ERASE;
Chen Gongfaff3752008-08-11 16:59:13 +0800194
195 spi_write(flash->spi, flash->command, 1);
196
197 return 0;
198}
Mike Lavender2f9f7622006-01-08 13:34:27 -0800199
200/*
201 * Erase one sector of flash memory at offset ``offset'' which is any
202 * address within the sector which should be erased.
203 *
204 * Returns 0 if successful, non-zero otherwise.
205 */
206static int erase_sector(struct m25p *flash, u32 offset)
207{
David Woodhouse02d087d2007-06-28 22:38:38 +0100208 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000209 dev_name(&flash->spi->dev), __func__,
David Brownellfa0a8c72007-06-24 15:12:35 -0700210 flash->mtd.erasesize / 1024, offset);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800211
212 /* Wait until finished previous write command. */
213 if (wait_till_ready(flash))
214 return 1;
215
216 /* Send write enable, then erase commands. */
217 write_enable(flash);
218
219 /* Set up command buffer. */
David Brownellfa0a8c72007-06-24 15:12:35 -0700220 flash->command[0] = flash->erase_opcode;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800221 flash->command[1] = offset >> 16;
222 flash->command[2] = offset >> 8;
223 flash->command[3] = offset;
224
Bryan Wu2230b762008-04-25 12:07:32 +0800225 spi_write(flash->spi, flash->command, CMD_SIZE);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800226
227 return 0;
228}
229
230/****************************************************************************/
231
232/*
233 * MTD implementation
234 */
235
236/*
237 * Erase an address range on the flash chip. The address range may extend
238 * one or more erase sectors. Return an error is there is a problem erasing.
239 */
240static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
241{
242 struct m25p *flash = mtd_to_m25p(mtd);
243 u32 addr,len;
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200244 uint32_t rem;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800245
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200246 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%llx, len %lld\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000247 dev_name(&flash->spi->dev), __func__, "at",
248 (long long)instr->addr, (long long)instr->len);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800249
250 /* sanity checks */
251 if (instr->addr + instr->len > flash->mtd.size)
252 return -EINVAL;
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200253 div_u64_rem(instr->len, mtd->erasesize, &rem);
254 if (rem)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800255 return -EINVAL;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800256
257 addr = instr->addr;
258 len = instr->len;
259
David Brownell7d5230e2007-06-24 15:09:13 -0700260 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800261
Chen Gong78546432008-11-26 10:23:57 +0000262 /* whole-chip erase? */
Steven A. Falco3f33b0a2009-04-27 17:10:10 -0400263 if (len == flash->mtd.size) {
264 if (erase_chip(flash)) {
265 instr->state = MTD_ERASE_FAILED;
266 mutex_unlock(&flash->lock);
267 return -EIO;
268 }
Chen Gong78546432008-11-26 10:23:57 +0000269
270 /* REVISIT in some cases we could speed up erasing large regions
271 * by using OPCODE_SE instead of OPCODE_BE_4K. We may have set up
272 * to use "small sector erase", but that's not always optimal.
273 */
274
275 /* "sector"-at-a-time erase */
Chen Gongfaff3752008-08-11 16:59:13 +0800276 } else {
277 while (len) {
278 if (erase_sector(flash, addr)) {
279 instr->state = MTD_ERASE_FAILED;
280 mutex_unlock(&flash->lock);
281 return -EIO;
282 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800283
Chen Gongfaff3752008-08-11 16:59:13 +0800284 addr += mtd->erasesize;
285 len -= mtd->erasesize;
286 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800287 }
288
David Brownell7d5230e2007-06-24 15:09:13 -0700289 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800290
291 instr->state = MTD_ERASE_DONE;
292 mtd_erase_callback(instr);
293
294 return 0;
295}
296
297/*
298 * Read an address range from the flash chip. The address range
299 * may be any size provided it is within the physical boundaries.
300 */
301static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
302 size_t *retlen, u_char *buf)
303{
304 struct m25p *flash = mtd_to_m25p(mtd);
305 struct spi_transfer t[2];
306 struct spi_message m;
307
308 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000309 dev_name(&flash->spi->dev), __func__, "from",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800310 (u32)from, len);
311
312 /* sanity checks */
313 if (!len)
314 return 0;
315
316 if (from + len > flash->mtd.size)
317 return -EINVAL;
318
Vitaly Wool8275c642006-01-08 13:34:28 -0800319 spi_message_init(&m);
320 memset(t, 0, (sizeof t));
321
Bryan Wu2230b762008-04-25 12:07:32 +0800322 /* NOTE:
323 * OPCODE_FAST_READ (if available) is faster.
324 * Should add 1 byte DUMMY_BYTE.
325 */
Vitaly Wool8275c642006-01-08 13:34:28 -0800326 t[0].tx_buf = flash->command;
Bryan Wu2230b762008-04-25 12:07:32 +0800327 t[0].len = CMD_SIZE + FAST_READ_DUMMY_BYTE;
Vitaly Wool8275c642006-01-08 13:34:28 -0800328 spi_message_add_tail(&t[0], &m);
329
330 t[1].rx_buf = buf;
331 t[1].len = len;
332 spi_message_add_tail(&t[1], &m);
333
334 /* Byte count starts at zero. */
335 if (retlen)
336 *retlen = 0;
337
David Brownell7d5230e2007-06-24 15:09:13 -0700338 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800339
340 /* Wait till previous write/erase is done. */
341 if (wait_till_ready(flash)) {
342 /* REVISIT status return?? */
David Brownell7d5230e2007-06-24 15:09:13 -0700343 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800344 return 1;
345 }
346
David Brownellfa0a8c72007-06-24 15:12:35 -0700347 /* FIXME switch to OPCODE_FAST_READ. It's required for higher
348 * clocks; and at this writing, every chip this driver handles
349 * supports that opcode.
350 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800351
352 /* Set up the write data buffer. */
353 flash->command[0] = OPCODE_READ;
354 flash->command[1] = from >> 16;
355 flash->command[2] = from >> 8;
356 flash->command[3] = from;
357
Mike Lavender2f9f7622006-01-08 13:34:27 -0800358 spi_sync(flash->spi, &m);
359
Bryan Wu2230b762008-04-25 12:07:32 +0800360 *retlen = m.actual_length - CMD_SIZE - FAST_READ_DUMMY_BYTE;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800361
David Brownell7d5230e2007-06-24 15:09:13 -0700362 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800363
364 return 0;
365}
366
367/*
368 * Write an address range to the flash chip. Data must be written in
369 * FLASH_PAGESIZE chunks. The address range may be any size provided
370 * it is within the physical boundaries.
371 */
372static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
373 size_t *retlen, const u_char *buf)
374{
375 struct m25p *flash = mtd_to_m25p(mtd);
376 u32 page_offset, page_size;
377 struct spi_transfer t[2];
378 struct spi_message m;
379
380 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000381 dev_name(&flash->spi->dev), __func__, "to",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800382 (u32)to, len);
383
384 if (retlen)
385 *retlen = 0;
386
387 /* sanity checks */
388 if (!len)
389 return(0);
390
391 if (to + len > flash->mtd.size)
392 return -EINVAL;
393
Vitaly Wool8275c642006-01-08 13:34:28 -0800394 spi_message_init(&m);
395 memset(t, 0, (sizeof t));
396
397 t[0].tx_buf = flash->command;
Bryan Wu2230b762008-04-25 12:07:32 +0800398 t[0].len = CMD_SIZE;
Vitaly Wool8275c642006-01-08 13:34:28 -0800399 spi_message_add_tail(&t[0], &m);
400
401 t[1].tx_buf = buf;
402 spi_message_add_tail(&t[1], &m);
403
David Brownell7d5230e2007-06-24 15:09:13 -0700404 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800405
406 /* Wait until finished previous write command. */
Chen Gongbc018862008-06-05 21:50:04 +0800407 if (wait_till_ready(flash)) {
408 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800409 return 1;
Chen Gongbc018862008-06-05 21:50:04 +0800410 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800411
412 write_enable(flash);
413
Mike Lavender2f9f7622006-01-08 13:34:27 -0800414 /* Set up the opcode in the write buffer. */
415 flash->command[0] = OPCODE_PP;
416 flash->command[1] = to >> 16;
417 flash->command[2] = to >> 8;
418 flash->command[3] = to;
419
Mike Lavender2f9f7622006-01-08 13:34:27 -0800420 /* what page do we start with? */
421 page_offset = to % FLASH_PAGESIZE;
422
423 /* do all the bytes fit onto one page? */
424 if (page_offset + len <= FLASH_PAGESIZE) {
Mike Lavender2f9f7622006-01-08 13:34:27 -0800425 t[1].len = len;
426
427 spi_sync(flash->spi, &m);
428
Bryan Wu2230b762008-04-25 12:07:32 +0800429 *retlen = m.actual_length - CMD_SIZE;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800430 } else {
431 u32 i;
432
433 /* the size of data remaining on the first page */
434 page_size = FLASH_PAGESIZE - page_offset;
435
Mike Lavender2f9f7622006-01-08 13:34:27 -0800436 t[1].len = page_size;
437 spi_sync(flash->spi, &m);
438
Bryan Wu2230b762008-04-25 12:07:32 +0800439 *retlen = m.actual_length - CMD_SIZE;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800440
441 /* write everything in PAGESIZE chunks */
442 for (i = page_size; i < len; i += page_size) {
443 page_size = len - i;
444 if (page_size > FLASH_PAGESIZE)
445 page_size = FLASH_PAGESIZE;
446
447 /* write the next page to flash */
448 flash->command[1] = (to + i) >> 16;
449 flash->command[2] = (to + i) >> 8;
450 flash->command[3] = (to + i);
451
452 t[1].tx_buf = buf + i;
453 t[1].len = page_size;
454
455 wait_till_ready(flash);
456
457 write_enable(flash);
458
459 spi_sync(flash->spi, &m);
460
David Brownell71117632006-01-08 13:34:29 -0800461 if (retlen)
Bryan Wu2230b762008-04-25 12:07:32 +0800462 *retlen += m.actual_length - CMD_SIZE;
David Brownell7d5230e2007-06-24 15:09:13 -0700463 }
464 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800465
David Brownell7d5230e2007-06-24 15:09:13 -0700466 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800467
468 return 0;
469}
470
Graf Yang49aac4a2009-06-15 08:23:41 +0000471static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
472 size_t *retlen, const u_char *buf)
473{
474 struct m25p *flash = mtd_to_m25p(mtd);
475 struct spi_transfer t[2];
476 struct spi_message m;
477 size_t actual;
478 int cmd_sz, ret;
479
480 if (retlen)
481 *retlen = 0;
482
483 /* sanity checks */
484 if (!len)
485 return 0;
486
487 if (to + len > flash->mtd.size)
488 return -EINVAL;
489
490 spi_message_init(&m);
491 memset(t, 0, (sizeof t));
492
493 t[0].tx_buf = flash->command;
494 t[0].len = CMD_SIZE;
495 spi_message_add_tail(&t[0], &m);
496
497 t[1].tx_buf = buf;
498 spi_message_add_tail(&t[1], &m);
499
500 mutex_lock(&flash->lock);
501
502 /* Wait until finished previous write command. */
503 ret = wait_till_ready(flash);
504 if (ret)
505 goto time_out;
506
507 write_enable(flash);
508
509 actual = to % 2;
510 /* Start write from odd address. */
511 if (actual) {
512 flash->command[0] = OPCODE_BP;
513 flash->command[1] = to >> 16;
514 flash->command[2] = to >> 8;
515 flash->command[3] = to;
516
517 /* write one byte. */
518 t[1].len = 1;
519 spi_sync(flash->spi, &m);
520 ret = wait_till_ready(flash);
521 if (ret)
522 goto time_out;
523 *retlen += m.actual_length - CMD_SIZE;
524 }
525 to += actual;
526
527 flash->command[0] = OPCODE_AAI_WP;
528 flash->command[1] = to >> 16;
529 flash->command[2] = to >> 8;
530 flash->command[3] = to;
531
532 /* Write out most of the data here. */
533 cmd_sz = CMD_SIZE;
534 for (; actual < len - 1; actual += 2) {
535 t[0].len = cmd_sz;
536 /* write two bytes. */
537 t[1].len = 2;
538 t[1].tx_buf = buf + actual;
539
540 spi_sync(flash->spi, &m);
541 ret = wait_till_ready(flash);
542 if (ret)
543 goto time_out;
544 *retlen += m.actual_length - cmd_sz;
545 cmd_sz = 1;
546 to += 2;
547 }
548 write_disable(flash);
549 ret = wait_till_ready(flash);
550 if (ret)
551 goto time_out;
552
553 /* Write out trailing byte if it exists. */
554 if (actual != len) {
555 write_enable(flash);
556 flash->command[0] = OPCODE_BP;
557 flash->command[1] = to >> 16;
558 flash->command[2] = to >> 8;
559 flash->command[3] = to;
560 t[0].len = CMD_SIZE;
561 t[1].len = 1;
562 t[1].tx_buf = buf + actual;
563
564 spi_sync(flash->spi, &m);
565 ret = wait_till_ready(flash);
566 if (ret)
567 goto time_out;
568 *retlen += m.actual_length - CMD_SIZE;
569 write_disable(flash);
570 }
571
572time_out:
573 mutex_unlock(&flash->lock);
574 return ret;
575}
Mike Lavender2f9f7622006-01-08 13:34:27 -0800576
577/****************************************************************************/
578
579/*
580 * SPI device driver setup and teardown
581 */
582
583struct flash_info {
584 char *name;
David Brownellfa0a8c72007-06-24 15:12:35 -0700585
586 /* JEDEC id zero means "no ID" (most older chips); otherwise it has
587 * a high byte of zero plus three data bytes: the manufacturer id,
588 * then a two byte device id.
589 */
590 u32 jedec_id;
Chen Gongd0e8c472008-08-11 16:59:15 +0800591 u16 ext_id;
David Brownellfa0a8c72007-06-24 15:12:35 -0700592
593 /* The size listed here is what works with OPCODE_SE, which isn't
594 * necessarily called a "sector" by the vendor.
595 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800596 unsigned sector_size;
David Brownellfa0a8c72007-06-24 15:12:35 -0700597 u16 n_sectors;
598
599 u16 flags;
600#define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800601};
602
David Brownellfa0a8c72007-06-24 15:12:35 -0700603
604/* NOTE: double check command sets and memory organization when you add
605 * more flash chips. This current list focusses on newer chips, which
606 * have been converging on command sets which including JEDEC ID.
607 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800608static struct flash_info __devinitdata m25p_data [] = {
David Brownellfa0a8c72007-06-24 15:12:35 -0700609
610 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
Chen Gongd0e8c472008-08-11 16:59:15 +0800611 { "at25fs010", 0x1f6601, 0, 32 * 1024, 4, SECT_4K, },
612 { "at25fs040", 0x1f6604, 0, 64 * 1024, 8, SECT_4K, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700613
Chen Gongd0e8c472008-08-11 16:59:15 +0800614 { "at25df041a", 0x1f4401, 0, 64 * 1024, 8, SECT_4K, },
615 { "at25df641", 0x1f4800, 0, 64 * 1024, 128, SECT_4K, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700616
Chen Gongd0e8c472008-08-11 16:59:15 +0800617 { "at26f004", 0x1f0400, 0, 64 * 1024, 8, SECT_4K, },
618 { "at26df081a", 0x1f4501, 0, 64 * 1024, 16, SECT_4K, },
619 { "at26df161a", 0x1f4601, 0, 64 * 1024, 32, SECT_4K, },
620 { "at26df321", 0x1f4701, 0, 64 * 1024, 64, SECT_4K, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700621
Lennert Buytenhekab1ff212009-05-20 13:07:11 +0200622 /* Macronix */
Siddarth Goreb0469ea72009-08-04 08:42:08 +0530623 { "mx25l3205d", 0xc22016, 0, 64 * 1024, 64, },
624 { "mx25l6405d", 0xc22017, 0, 64 * 1024, 128, },
Lennert Buytenhekab1ff212009-05-20 13:07:11 +0200625 { "mx25l12805d", 0xc22018, 0, 64 * 1024, 256, },
Siddarth Goreb0469ea72009-08-04 08:42:08 +0530626 { "mx25l12855e", 0xc22618, 0, 64 * 1024, 256, },
Lennert Buytenhekab1ff212009-05-20 13:07:11 +0200627
David Brownellfa0a8c72007-06-24 15:12:35 -0700628 /* Spansion -- single (large) sector size only, at least
629 * for the chips listed here (without boot sectors).
630 */
Chen Gongd0e8c472008-08-11 16:59:15 +0800631 { "s25sl004a", 0x010212, 0, 64 * 1024, 8, },
632 { "s25sl008a", 0x010213, 0, 64 * 1024, 16, },
633 { "s25sl016a", 0x010214, 0, 64 * 1024, 32, },
634 { "s25sl032a", 0x010215, 0, 64 * 1024, 64, },
635 { "s25sl064a", 0x010216, 0, 64 * 1024, 128, },
636 { "s25sl12800", 0x012018, 0x0300, 256 * 1024, 64, },
637 { "s25sl12801", 0x012018, 0x0301, 64 * 1024, 256, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700638
639 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
Chen Gongd0e8c472008-08-11 16:59:15 +0800640 { "sst25vf040b", 0xbf258d, 0, 64 * 1024, 8, SECT_4K, },
641 { "sst25vf080b", 0xbf258e, 0, 64 * 1024, 16, SECT_4K, },
642 { "sst25vf016b", 0xbf2541, 0, 64 * 1024, 32, SECT_4K, },
643 { "sst25vf032b", 0xbf254a, 0, 64 * 1024, 64, SECT_4K, },
Graf Yangaa3651e2009-06-15 08:23:41 +0000644 { "sst25wf512", 0xbf2501, 0, 64 * 1024, 1, SECT_4K, },
645 { "sst25wf010", 0xbf2502, 0, 64 * 1024, 2, SECT_4K, },
646 { "sst25wf020", 0xbf2503, 0, 64 * 1024, 4, SECT_4K, },
647 { "sst25wf040", 0xbf2504, 0, 64 * 1024, 8, SECT_4K, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700648
649 /* ST Microelectronics -- newer production may have feature updates */
Chen Gongd0e8c472008-08-11 16:59:15 +0800650 { "m25p05", 0x202010, 0, 32 * 1024, 2, },
651 { "m25p10", 0x202011, 0, 32 * 1024, 4, },
652 { "m25p20", 0x202012, 0, 64 * 1024, 4, },
653 { "m25p40", 0x202013, 0, 64 * 1024, 8, },
654 { "m25p80", 0, 0, 64 * 1024, 16, },
655 { "m25p16", 0x202015, 0, 64 * 1024, 32, },
656 { "m25p32", 0x202016, 0, 64 * 1024, 64, },
657 { "m25p64", 0x202017, 0, 64 * 1024, 128, },
658 { "m25p128", 0x202018, 0, 256 * 1024, 64, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700659
Matthieu CASTET1e42d142009-04-28 18:15:31 +0200660 { "m45pe10", 0x204011, 0, 64 * 1024, 2, },
Chen Gongd0e8c472008-08-11 16:59:15 +0800661 { "m45pe80", 0x204014, 0, 64 * 1024, 16, },
662 { "m45pe16", 0x204015, 0, 64 * 1024, 32, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700663
Chen Gongd0e8c472008-08-11 16:59:15 +0800664 { "m25pe80", 0x208014, 0, 64 * 1024, 16, },
665 { "m25pe16", 0x208015, 0, 64 * 1024, 32, SECT_4K, },
David Brownellfa0a8c72007-06-24 15:12:35 -0700666
David Woodhouse02d087d2007-06-28 22:38:38 +0100667 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
Chen Gongd0e8c472008-08-11 16:59:15 +0800668 { "w25x10", 0xef3011, 0, 64 * 1024, 2, SECT_4K, },
669 { "w25x20", 0xef3012, 0, 64 * 1024, 4, SECT_4K, },
670 { "w25x40", 0xef3013, 0, 64 * 1024, 8, SECT_4K, },
671 { "w25x80", 0xef3014, 0, 64 * 1024, 16, SECT_4K, },
672 { "w25x16", 0xef3015, 0, 64 * 1024, 32, SECT_4K, },
673 { "w25x32", 0xef3016, 0, 64 * 1024, 64, SECT_4K, },
674 { "w25x64", 0xef3017, 0, 64 * 1024, 128, SECT_4K, },
Mike Lavender2f9f7622006-01-08 13:34:27 -0800675};
676
David Brownellfa0a8c72007-06-24 15:12:35 -0700677static struct flash_info *__devinit jedec_probe(struct spi_device *spi)
678{
679 int tmp;
680 u8 code = OPCODE_RDID;
Chen Gongdaa84732008-09-16 14:14:12 +0800681 u8 id[5];
David Brownellfa0a8c72007-06-24 15:12:35 -0700682 u32 jedec;
Chen Gongd0e8c472008-08-11 16:59:15 +0800683 u16 ext_jedec;
David Brownellfa0a8c72007-06-24 15:12:35 -0700684 struct flash_info *info;
685
686 /* JEDEC also defines an optional "extended device information"
687 * string for after vendor-specific data, after the three bytes
688 * we use here. Supporting some chips might require using it.
689 */
Chen Gongdaa84732008-09-16 14:14:12 +0800690 tmp = spi_write_then_read(spi, &code, 1, id, 5);
David Brownellfa0a8c72007-06-24 15:12:35 -0700691 if (tmp < 0) {
692 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000693 dev_name(&spi->dev), tmp);
David Brownellfa0a8c72007-06-24 15:12:35 -0700694 return NULL;
695 }
696 jedec = id[0];
697 jedec = jedec << 8;
698 jedec |= id[1];
699 jedec = jedec << 8;
700 jedec |= id[2];
701
Chen Gongd0e8c472008-08-11 16:59:15 +0800702 ext_jedec = id[3] << 8 | id[4];
703
David Brownellfa0a8c72007-06-24 15:12:35 -0700704 for (tmp = 0, info = m25p_data;
705 tmp < ARRAY_SIZE(m25p_data);
706 tmp++, info++) {
Mike Frysingera3d3f732008-11-26 10:23:25 +0000707 if (info->jedec_id == jedec) {
Mike Frysinger9168ab82008-11-26 10:23:35 +0000708 if (info->ext_id != 0 && info->ext_id != ext_jedec)
Chen Gongd0e8c472008-08-11 16:59:15 +0800709 continue;
David Brownellfa0a8c72007-06-24 15:12:35 -0700710 return info;
Mike Frysingera3d3f732008-11-26 10:23:25 +0000711 }
David Brownellfa0a8c72007-06-24 15:12:35 -0700712 }
713 dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
714 return NULL;
715}
716
717
Mike Lavender2f9f7622006-01-08 13:34:27 -0800718/*
719 * board specific setup should have ensured the SPI clock used here
720 * matches what the READ command supports, at least until this driver
721 * understands FAST_READ (for clocks over 25 MHz).
722 */
723static int __devinit m25p_probe(struct spi_device *spi)
724{
725 struct flash_platform_data *data;
726 struct m25p *flash;
727 struct flash_info *info;
728 unsigned i;
729
730 /* Platform data helps sort out which chip type we have, as
David Brownellfa0a8c72007-06-24 15:12:35 -0700731 * well as how this board partitions it. If we don't have
732 * a chip ID, try the JEDEC id commands; they'll work for most
733 * newer chips, even if we don't recognize the particular chip.
Mike Lavender2f9f7622006-01-08 13:34:27 -0800734 */
735 data = spi->dev.platform_data;
David Brownellfa0a8c72007-06-24 15:12:35 -0700736 if (data && data->type) {
737 for (i = 0, info = m25p_data;
738 i < ARRAY_SIZE(m25p_data);
739 i++, info++) {
740 if (strcmp(data->type, info->name) == 0)
741 break;
742 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800743
David Brownellfa0a8c72007-06-24 15:12:35 -0700744 /* unrecognized chip? */
745 if (i == ARRAY_SIZE(m25p_data)) {
746 DEBUG(MTD_DEBUG_LEVEL0, "%s: unrecognized id %s\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000747 dev_name(&spi->dev), data->type);
David Brownellfa0a8c72007-06-24 15:12:35 -0700748 info = NULL;
749
750 /* recognized; is that chip really what's there? */
751 } else if (info->jedec_id) {
752 struct flash_info *chip = jedec_probe(spi);
753
754 if (!chip || chip != info) {
755 dev_warn(&spi->dev, "found %s, expected %s\n",
756 chip ? chip->name : "UNKNOWN",
757 info->name);
758 info = NULL;
759 }
760 }
761 } else
762 info = jedec_probe(spi);
763
764 if (!info)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800765 return -ENODEV;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800766
Christoph Lametere94b1762006-12-06 20:33:17 -0800767 flash = kzalloc(sizeof *flash, GFP_KERNEL);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800768 if (!flash)
769 return -ENOMEM;
770
771 flash->spi = spi;
David Brownell7d5230e2007-06-24 15:09:13 -0700772 mutex_init(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800773 dev_set_drvdata(&spi->dev, flash);
774
Michael Hennerich72289822008-07-03 23:54:42 -0700775 /*
776 * Atmel serial flash tend to power up
777 * with the software protection bits set
778 */
779
780 if (info->jedec_id >> 16 == 0x1f) {
781 write_enable(flash);
782 write_sr(flash, 0);
783 }
784
David Brownellfa0a8c72007-06-24 15:12:35 -0700785 if (data && data->name)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800786 flash->mtd.name = data->name;
787 else
Kay Sievers160bbab2008-12-23 10:00:14 +0000788 flash->mtd.name = dev_name(&spi->dev);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800789
790 flash->mtd.type = MTD_NORFLASH;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400791 flash->mtd.writesize = 1;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800792 flash->mtd.flags = MTD_CAP_NORFLASH;
793 flash->mtd.size = info->sector_size * info->n_sectors;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800794 flash->mtd.erase = m25p80_erase;
795 flash->mtd.read = m25p80_read;
Graf Yang49aac4a2009-06-15 08:23:41 +0000796
797 /* sst flash chips use AAI word program */
798 if (info->jedec_id >> 16 == 0xbf)
799 flash->mtd.write = sst_write;
800 else
801 flash->mtd.write = m25p80_write;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800802
David Brownellfa0a8c72007-06-24 15:12:35 -0700803 /* prefer "small sector" erase if possible */
804 if (info->flags & SECT_4K) {
805 flash->erase_opcode = OPCODE_BE_4K;
806 flash->mtd.erasesize = 4096;
807 } else {
808 flash->erase_opcode = OPCODE_SE;
809 flash->mtd.erasesize = info->sector_size;
810 }
811
David Brownell87f39f02009-03-26 00:42:50 -0700812 flash->mtd.dev.parent = &spi->dev;
813
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200814 dev_info(&spi->dev, "%s (%lld Kbytes)\n", info->name,
815 (long long)flash->mtd.size >> 10);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800816
817 DEBUG(MTD_DEBUG_LEVEL2,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200818 "mtd .name = %s, .size = 0x%llx (%lldMiB) "
David Woodhouse02d087d2007-06-28 22:38:38 +0100819 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800820 flash->mtd.name,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200821 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
Mike Lavender2f9f7622006-01-08 13:34:27 -0800822 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
823 flash->mtd.numeraseregions);
824
825 if (flash->mtd.numeraseregions)
826 for (i = 0; i < flash->mtd.numeraseregions; i++)
827 DEBUG(MTD_DEBUG_LEVEL2,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200828 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
David Woodhouse02d087d2007-06-28 22:38:38 +0100829 ".erasesize = 0x%.8x (%uKiB), "
Mike Lavender2f9f7622006-01-08 13:34:27 -0800830 ".numblocks = %d }\n",
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200831 i, (long long)flash->mtd.eraseregions[i].offset,
Mike Lavender2f9f7622006-01-08 13:34:27 -0800832 flash->mtd.eraseregions[i].erasesize,
833 flash->mtd.eraseregions[i].erasesize / 1024,
834 flash->mtd.eraseregions[i].numblocks);
835
836
837 /* partitions should match sector boundaries; and it may be good to
838 * use readonly partitions for writeprotected sectors (BP2..BP0).
839 */
840 if (mtd_has_partitions()) {
841 struct mtd_partition *parts = NULL;
842 int nr_parts = 0;
843
David Brownella4b6d512009-03-04 12:01:41 -0800844 if (mtd_has_cmdlinepart()) {
845 static const char *part_probes[]
846 = { "cmdlinepart", NULL, };
Mike Lavender2f9f7622006-01-08 13:34:27 -0800847
David Brownella4b6d512009-03-04 12:01:41 -0800848 nr_parts = parse_mtd_partitions(&flash->mtd,
849 part_probes, &parts, 0);
850 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800851
852 if (nr_parts <= 0 && data && data->parts) {
853 parts = data->parts;
854 nr_parts = data->nr_parts;
855 }
856
857 if (nr_parts > 0) {
David Brownellfa0a8c72007-06-24 15:12:35 -0700858 for (i = 0; i < nr_parts; i++) {
Mike Lavender2f9f7622006-01-08 13:34:27 -0800859 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200860 "{.name = %s, .offset = 0x%llx, "
861 ".size = 0x%llx (%lldKiB) }\n",
David Brownellfa0a8c72007-06-24 15:12:35 -0700862 i, parts[i].name,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200863 (long long)parts[i].offset,
864 (long long)parts[i].size,
865 (long long)(parts[i].size >> 10));
Mike Lavender2f9f7622006-01-08 13:34:27 -0800866 }
867 flash->partitioned = 1;
868 return add_mtd_partitions(&flash->mtd, parts, nr_parts);
869 }
Anton Vorontsovedcb3b12009-08-06 15:18:37 -0700870 } else if (data && data->nr_parts)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800871 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
872 data->nr_parts, data->name);
873
874 return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
875}
876
877
878static int __devexit m25p_remove(struct spi_device *spi)
879{
880 struct m25p *flash = dev_get_drvdata(&spi->dev);
881 int status;
882
883 /* Clean up MTD stuff. */
884 if (mtd_has_partitions() && flash->partitioned)
885 status = del_mtd_partitions(&flash->mtd);
886 else
887 status = del_mtd_device(&flash->mtd);
888 if (status == 0)
889 kfree(flash);
890 return 0;
891}
892
893
894static struct spi_driver m25p80_driver = {
895 .driver = {
896 .name = "m25p80",
897 .bus = &spi_bus_type,
898 .owner = THIS_MODULE,
899 },
900 .probe = m25p_probe,
901 .remove = __devexit_p(m25p_remove),
David Brownellfa0a8c72007-06-24 15:12:35 -0700902
903 /* REVISIT: many of these chips have deep power-down modes, which
904 * should clearly be entered on suspend() to minimize power use.
905 * And also when they're otherwise idle...
906 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800907};
908
909
910static int m25p80_init(void)
911{
912 return spi_register_driver(&m25p80_driver);
913}
914
915
916static void m25p80_exit(void)
917{
918 spi_unregister_driver(&m25p80_driver);
919}
920
921
922module_init(m25p80_init);
923module_exit(m25p80_exit);
924
925MODULE_LICENSE("GPL");
926MODULE_AUTHOR("Mike Lavender");
927MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");