blob: f3f4768d6e18df89ba99e4efc5723c5e6593715d [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>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040024#include <linux/sched.h>
Anton Vorontsovb34bc032009-10-12 20:24:35 +040025#include <linux/mod_devicetable.h>
David Brownell7d5230e2007-06-24 15:09:13 -070026
Mike Lavender2f9f7622006-01-08 13:34:27 -080027#include <linux/mtd/mtd.h>
28#include <linux/mtd/partitions.h>
David Brownell7d5230e2007-06-24 15:09:13 -070029
Mike Lavender2f9f7622006-01-08 13:34:27 -080030#include <linux/spi/spi.h>
31#include <linux/spi/flash.h>
32
Mike Lavender2f9f7622006-01-08 13:34:27 -080033/* 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) */
Chen Gong78546432008-11-26 10:23:57 +000040#define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
David Woodhouse02d087d2007-06-28 22:38:38 +010041#define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
Chen Gong78546432008-11-26 10:23:57 +000042#define OPCODE_CHIP_ERASE 0xc7 /* Erase whole flash chip */
David Woodhouse02d087d2007-06-28 22:38:38 +010043#define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
Mike Lavender2f9f7622006-01-08 13:34:27 -080044#define OPCODE_RDID 0x9f /* Read JEDEC ID */
45
Graf Yang49aac4a2009-06-15 08:23:41 +000046/* Used for SST flashes only. */
47#define OPCODE_BP 0x02 /* Byte program */
48#define OPCODE_WRDI 0x04 /* Write disable */
49#define OPCODE_AAI_WP 0xad /* Auto address increment word program */
50
Mike Lavender2f9f7622006-01-08 13:34:27 -080051/* Status Register bits. */
52#define SR_WIP 1 /* Write in progress */
53#define SR_WEL 2 /* Write enable latch */
David Brownellfa0a8c72007-06-24 15:12:35 -070054/* meaning of other SR_* bits may differ between vendors */
Mike Lavender2f9f7622006-01-08 13:34:27 -080055#define SR_BP0 4 /* Block protect 0 */
56#define SR_BP1 8 /* Block protect 1 */
57#define SR_BP2 0x10 /* Block protect 2 */
58#define SR_SRWD 0x80 /* SR write protect */
59
60/* Define max times to check status register before we give up. */
Steven A. Falco89bb8712009-06-26 12:42:47 -040061#define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
Anton Vorontsov837479d2009-10-12 20:24:40 +040062#define MAX_CMD_SIZE 4
Mike Lavender2f9f7622006-01-08 13:34:27 -080063
Bryan Wu2230b762008-04-25 12:07:32 +080064#ifdef CONFIG_M25PXX_USE_FAST_READ
65#define OPCODE_READ OPCODE_FAST_READ
66#define FAST_READ_DUMMY_BYTE 1
67#else
68#define OPCODE_READ OPCODE_NORM_READ
69#define FAST_READ_DUMMY_BYTE 0
70#endif
Mike Lavender2f9f7622006-01-08 13:34:27 -080071
Mike Lavender2f9f7622006-01-08 13:34:27 -080072/****************************************************************************/
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;
Anton Vorontsov837479d2009-10-12 20:24:40 +040079 u16 page_size;
80 u16 addr_width;
David Brownellfa0a8c72007-06-24 15:12:35 -070081 u8 erase_opcode;
Johannes Stezenbach61c35062009-10-28 14:21:37 +010082 u8 *command;
Mike Lavender2f9f7622006-01-08 13:34:27 -080083};
84
85static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
86{
87 return container_of(mtd, struct m25p, mtd);
88}
89
90/****************************************************************************/
91
92/*
93 * Internal helper functions
94 */
95
96/*
97 * Read the status register, returning its value in the location
98 * Return the status register value.
99 * Returns negative if error occurred.
100 */
101static int read_sr(struct m25p *flash)
102{
103 ssize_t retval;
104 u8 code = OPCODE_RDSR;
105 u8 val;
106
107 retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
108
109 if (retval < 0) {
110 dev_err(&flash->spi->dev, "error %d reading SR\n",
111 (int) retval);
112 return retval;
113 }
114
115 return val;
116}
117
Michael Hennerich72289822008-07-03 23:54:42 -0700118/*
119 * Write status register 1 byte
120 * Returns negative if error occurred.
121 */
122static int write_sr(struct m25p *flash, u8 val)
123{
124 flash->command[0] = OPCODE_WRSR;
125 flash->command[1] = val;
126
127 return spi_write(flash->spi, flash->command, 2);
128}
Mike Lavender2f9f7622006-01-08 13:34:27 -0800129
130/*
131 * Set write enable latch with Write Enable command.
132 * Returns negative if error occurred.
133 */
134static inline int write_enable(struct m25p *flash)
135{
136 u8 code = OPCODE_WREN;
137
David Woodhouse8a1a6272008-10-20 09:26:16 +0100138 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800139}
140
Graf Yang49aac4a2009-06-15 08:23:41 +0000141/*
142 * Send write disble instruction to the chip.
143 */
144static inline int write_disable(struct m25p *flash)
145{
146 u8 code = OPCODE_WRDI;
147
148 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
149}
Mike Lavender2f9f7622006-01-08 13:34:27 -0800150
151/*
152 * Service routine to read status register until ready, or timeout occurs.
153 * Returns non-zero if error.
154 */
155static int wait_till_ready(struct m25p *flash)
156{
Peter Hortoncd1a6de2009-05-08 13:51:53 +0100157 unsigned long deadline;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800158 int sr;
159
Peter Hortoncd1a6de2009-05-08 13:51:53 +0100160 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
161
162 do {
Mike Lavender2f9f7622006-01-08 13:34:27 -0800163 if ((sr = read_sr(flash)) < 0)
164 break;
165 else if (!(sr & SR_WIP))
166 return 0;
167
Peter Hortoncd1a6de2009-05-08 13:51:53 +0100168 cond_resched();
169
170 } while (!time_after_eq(jiffies, deadline));
Mike Lavender2f9f7622006-01-08 13:34:27 -0800171
172 return 1;
173}
174
Chen Gongfaff3752008-08-11 16:59:13 +0800175/*
176 * Erase the whole flash memory
177 *
178 * Returns 0 if successful, non-zero otherwise.
179 */
Chen Gong78546432008-11-26 10:23:57 +0000180static int erase_chip(struct m25p *flash)
Chen Gongfaff3752008-08-11 16:59:13 +0800181{
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200182 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %lldKiB\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000183 dev_name(&flash->spi->dev), __func__,
184 (long long)(flash->mtd.size >> 10));
Chen Gongfaff3752008-08-11 16:59:13 +0800185
186 /* Wait until finished previous write command. */
187 if (wait_till_ready(flash))
188 return 1;
189
190 /* Send write enable, then erase commands. */
191 write_enable(flash);
192
193 /* Set up command buffer. */
Chen Gong78546432008-11-26 10:23:57 +0000194 flash->command[0] = OPCODE_CHIP_ERASE;
Chen Gongfaff3752008-08-11 16:59:13 +0800195
196 spi_write(flash->spi, flash->command, 1);
197
198 return 0;
199}
Mike Lavender2f9f7622006-01-08 13:34:27 -0800200
Anton Vorontsov837479d2009-10-12 20:24:40 +0400201static void m25p_addr2cmd(struct m25p *flash, unsigned int addr, u8 *cmd)
202{
203 /* opcode is in cmd[0] */
204 cmd[1] = addr >> (flash->addr_width * 8 - 8);
205 cmd[2] = addr >> (flash->addr_width * 8 - 16);
206 cmd[3] = addr >> (flash->addr_width * 8 - 24);
207}
208
209static int m25p_cmdsz(struct m25p *flash)
210{
211 return 1 + flash->addr_width;
212}
213
Mike Lavender2f9f7622006-01-08 13:34:27 -0800214/*
215 * Erase one sector of flash memory at offset ``offset'' which is any
216 * address within the sector which should be erased.
217 *
218 * Returns 0 if successful, non-zero otherwise.
219 */
220static int erase_sector(struct m25p *flash, u32 offset)
221{
David Woodhouse02d087d2007-06-28 22:38:38 +0100222 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000223 dev_name(&flash->spi->dev), __func__,
David Brownellfa0a8c72007-06-24 15:12:35 -0700224 flash->mtd.erasesize / 1024, offset);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800225
226 /* Wait until finished previous write command. */
227 if (wait_till_ready(flash))
228 return 1;
229
230 /* Send write enable, then erase commands. */
231 write_enable(flash);
232
233 /* Set up command buffer. */
David Brownellfa0a8c72007-06-24 15:12:35 -0700234 flash->command[0] = flash->erase_opcode;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400235 m25p_addr2cmd(flash, offset, flash->command);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800236
Anton Vorontsov837479d2009-10-12 20:24:40 +0400237 spi_write(flash->spi, flash->command, m25p_cmdsz(flash));
Mike Lavender2f9f7622006-01-08 13:34:27 -0800238
239 return 0;
240}
241
242/****************************************************************************/
243
244/*
245 * MTD implementation
246 */
247
248/*
249 * Erase an address range on the flash chip. The address range may extend
250 * one or more erase sectors. Return an error is there is a problem erasing.
251 */
252static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
253{
254 struct m25p *flash = mtd_to_m25p(mtd);
255 u32 addr,len;
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200256 uint32_t rem;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800257
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200258 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%llx, len %lld\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000259 dev_name(&flash->spi->dev), __func__, "at",
260 (long long)instr->addr, (long long)instr->len);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800261
262 /* sanity checks */
263 if (instr->addr + instr->len > flash->mtd.size)
264 return -EINVAL;
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200265 div_u64_rem(instr->len, mtd->erasesize, &rem);
266 if (rem)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800267 return -EINVAL;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800268
269 addr = instr->addr;
270 len = instr->len;
271
David Brownell7d5230e2007-06-24 15:09:13 -0700272 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800273
Chen Gong78546432008-11-26 10:23:57 +0000274 /* whole-chip erase? */
Steven A. Falco3f33b0a2009-04-27 17:10:10 -0400275 if (len == flash->mtd.size) {
276 if (erase_chip(flash)) {
277 instr->state = MTD_ERASE_FAILED;
278 mutex_unlock(&flash->lock);
279 return -EIO;
280 }
Chen Gong78546432008-11-26 10:23:57 +0000281
282 /* REVISIT in some cases we could speed up erasing large regions
283 * by using OPCODE_SE instead of OPCODE_BE_4K. We may have set up
284 * to use "small sector erase", but that's not always optimal.
285 */
286
287 /* "sector"-at-a-time erase */
Chen Gongfaff3752008-08-11 16:59:13 +0800288 } else {
289 while (len) {
290 if (erase_sector(flash, addr)) {
291 instr->state = MTD_ERASE_FAILED;
292 mutex_unlock(&flash->lock);
293 return -EIO;
294 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800295
Chen Gongfaff3752008-08-11 16:59:13 +0800296 addr += mtd->erasesize;
297 len -= mtd->erasesize;
298 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800299 }
300
David Brownell7d5230e2007-06-24 15:09:13 -0700301 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800302
303 instr->state = MTD_ERASE_DONE;
304 mtd_erase_callback(instr);
305
306 return 0;
307}
308
309/*
310 * Read an address range from the flash chip. The address range
311 * may be any size provided it is within the physical boundaries.
312 */
313static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
314 size_t *retlen, u_char *buf)
315{
316 struct m25p *flash = mtd_to_m25p(mtd);
317 struct spi_transfer t[2];
318 struct spi_message m;
319
320 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000321 dev_name(&flash->spi->dev), __func__, "from",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800322 (u32)from, len);
323
324 /* sanity checks */
325 if (!len)
326 return 0;
327
328 if (from + len > flash->mtd.size)
329 return -EINVAL;
330
Vitaly Wool8275c642006-01-08 13:34:28 -0800331 spi_message_init(&m);
332 memset(t, 0, (sizeof t));
333
Bryan Wu2230b762008-04-25 12:07:32 +0800334 /* NOTE:
335 * OPCODE_FAST_READ (if available) is faster.
336 * Should add 1 byte DUMMY_BYTE.
337 */
Vitaly Wool8275c642006-01-08 13:34:28 -0800338 t[0].tx_buf = flash->command;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400339 t[0].len = m25p_cmdsz(flash) + FAST_READ_DUMMY_BYTE;
Vitaly Wool8275c642006-01-08 13:34:28 -0800340 spi_message_add_tail(&t[0], &m);
341
342 t[1].rx_buf = buf;
343 t[1].len = len;
344 spi_message_add_tail(&t[1], &m);
345
346 /* Byte count starts at zero. */
347 if (retlen)
348 *retlen = 0;
349
David Brownell7d5230e2007-06-24 15:09:13 -0700350 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800351
352 /* Wait till previous write/erase is done. */
353 if (wait_till_ready(flash)) {
354 /* REVISIT status return?? */
David Brownell7d5230e2007-06-24 15:09:13 -0700355 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800356 return 1;
357 }
358
David Brownellfa0a8c72007-06-24 15:12:35 -0700359 /* FIXME switch to OPCODE_FAST_READ. It's required for higher
360 * clocks; and at this writing, every chip this driver handles
361 * supports that opcode.
362 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800363
364 /* Set up the write data buffer. */
365 flash->command[0] = OPCODE_READ;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400366 m25p_addr2cmd(flash, from, flash->command);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800367
Mike Lavender2f9f7622006-01-08 13:34:27 -0800368 spi_sync(flash->spi, &m);
369
Anton Vorontsov837479d2009-10-12 20:24:40 +0400370 *retlen = m.actual_length - m25p_cmdsz(flash) - FAST_READ_DUMMY_BYTE;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800371
David Brownell7d5230e2007-06-24 15:09:13 -0700372 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800373
374 return 0;
375}
376
377/*
378 * Write an address range to the flash chip. Data must be written in
379 * FLASH_PAGESIZE chunks. The address range may be any size provided
380 * it is within the physical boundaries.
381 */
382static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
383 size_t *retlen, const u_char *buf)
384{
385 struct m25p *flash = mtd_to_m25p(mtd);
386 u32 page_offset, page_size;
387 struct spi_transfer t[2];
388 struct spi_message m;
389
390 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000391 dev_name(&flash->spi->dev), __func__, "to",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800392 (u32)to, len);
393
394 if (retlen)
395 *retlen = 0;
396
397 /* sanity checks */
398 if (!len)
399 return(0);
400
401 if (to + len > flash->mtd.size)
402 return -EINVAL;
403
Vitaly Wool8275c642006-01-08 13:34:28 -0800404 spi_message_init(&m);
405 memset(t, 0, (sizeof t));
406
407 t[0].tx_buf = flash->command;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400408 t[0].len = m25p_cmdsz(flash);
Vitaly Wool8275c642006-01-08 13:34:28 -0800409 spi_message_add_tail(&t[0], &m);
410
411 t[1].tx_buf = buf;
412 spi_message_add_tail(&t[1], &m);
413
David Brownell7d5230e2007-06-24 15:09:13 -0700414 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800415
416 /* Wait until finished previous write command. */
Chen Gongbc018862008-06-05 21:50:04 +0800417 if (wait_till_ready(flash)) {
418 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800419 return 1;
Chen Gongbc018862008-06-05 21:50:04 +0800420 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800421
422 write_enable(flash);
423
Mike Lavender2f9f7622006-01-08 13:34:27 -0800424 /* Set up the opcode in the write buffer. */
425 flash->command[0] = OPCODE_PP;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400426 m25p_addr2cmd(flash, to, flash->command);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800427
Anton Vorontsov837479d2009-10-12 20:24:40 +0400428 page_offset = to & (flash->page_size - 1);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800429
430 /* do all the bytes fit onto one page? */
Anton Vorontsov837479d2009-10-12 20:24:40 +0400431 if (page_offset + len <= flash->page_size) {
Mike Lavender2f9f7622006-01-08 13:34:27 -0800432 t[1].len = len;
433
434 spi_sync(flash->spi, &m);
435
Anton Vorontsov837479d2009-10-12 20:24:40 +0400436 *retlen = m.actual_length - m25p_cmdsz(flash);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800437 } else {
438 u32 i;
439
440 /* the size of data remaining on the first page */
Anton Vorontsov837479d2009-10-12 20:24:40 +0400441 page_size = flash->page_size - page_offset;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800442
Mike Lavender2f9f7622006-01-08 13:34:27 -0800443 t[1].len = page_size;
444 spi_sync(flash->spi, &m);
445
Anton Vorontsov837479d2009-10-12 20:24:40 +0400446 *retlen = m.actual_length - m25p_cmdsz(flash);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800447
Anton Vorontsov837479d2009-10-12 20:24:40 +0400448 /* write everything in flash->page_size chunks */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800449 for (i = page_size; i < len; i += page_size) {
450 page_size = len - i;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400451 if (page_size > flash->page_size)
452 page_size = flash->page_size;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800453
454 /* write the next page to flash */
Anton Vorontsov837479d2009-10-12 20:24:40 +0400455 m25p_addr2cmd(flash, to + i, flash->command);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800456
457 t[1].tx_buf = buf + i;
458 t[1].len = page_size;
459
460 wait_till_ready(flash);
461
462 write_enable(flash);
463
464 spi_sync(flash->spi, &m);
465
David Brownell71117632006-01-08 13:34:29 -0800466 if (retlen)
Anton Vorontsov837479d2009-10-12 20:24:40 +0400467 *retlen += m.actual_length - m25p_cmdsz(flash);
David Brownell7d5230e2007-06-24 15:09:13 -0700468 }
469 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800470
David Brownell7d5230e2007-06-24 15:09:13 -0700471 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800472
473 return 0;
474}
475
Graf Yang49aac4a2009-06-15 08:23:41 +0000476static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
477 size_t *retlen, const u_char *buf)
478{
479 struct m25p *flash = mtd_to_m25p(mtd);
480 struct spi_transfer t[2];
481 struct spi_message m;
482 size_t actual;
483 int cmd_sz, ret;
484
485 if (retlen)
486 *retlen = 0;
487
488 /* sanity checks */
489 if (!len)
490 return 0;
491
492 if (to + len > flash->mtd.size)
493 return -EINVAL;
494
495 spi_message_init(&m);
496 memset(t, 0, (sizeof t));
497
498 t[0].tx_buf = flash->command;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400499 t[0].len = m25p_cmdsz(flash);
Graf Yang49aac4a2009-06-15 08:23:41 +0000500 spi_message_add_tail(&t[0], &m);
501
502 t[1].tx_buf = buf;
503 spi_message_add_tail(&t[1], &m);
504
505 mutex_lock(&flash->lock);
506
507 /* Wait until finished previous write command. */
508 ret = wait_till_ready(flash);
509 if (ret)
510 goto time_out;
511
512 write_enable(flash);
513
514 actual = to % 2;
515 /* Start write from odd address. */
516 if (actual) {
517 flash->command[0] = OPCODE_BP;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400518 m25p_addr2cmd(flash, to, flash->command);
Graf Yang49aac4a2009-06-15 08:23:41 +0000519
520 /* write one byte. */
521 t[1].len = 1;
522 spi_sync(flash->spi, &m);
523 ret = wait_till_ready(flash);
524 if (ret)
525 goto time_out;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400526 *retlen += m.actual_length - m25p_cmdsz(flash);
Graf Yang49aac4a2009-06-15 08:23:41 +0000527 }
528 to += actual;
529
530 flash->command[0] = OPCODE_AAI_WP;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400531 m25p_addr2cmd(flash, to, flash->command);
Graf Yang49aac4a2009-06-15 08:23:41 +0000532
533 /* Write out most of the data here. */
Anton Vorontsov837479d2009-10-12 20:24:40 +0400534 cmd_sz = m25p_cmdsz(flash);
Graf Yang49aac4a2009-06-15 08:23:41 +0000535 for (; actual < len - 1; actual += 2) {
536 t[0].len = cmd_sz;
537 /* write two bytes. */
538 t[1].len = 2;
539 t[1].tx_buf = buf + actual;
540
541 spi_sync(flash->spi, &m);
542 ret = wait_till_ready(flash);
543 if (ret)
544 goto time_out;
545 *retlen += m.actual_length - cmd_sz;
546 cmd_sz = 1;
547 to += 2;
548 }
549 write_disable(flash);
550 ret = wait_till_ready(flash);
551 if (ret)
552 goto time_out;
553
554 /* Write out trailing byte if it exists. */
555 if (actual != len) {
556 write_enable(flash);
557 flash->command[0] = OPCODE_BP;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400558 m25p_addr2cmd(flash, to, flash->command);
559 t[0].len = m25p_cmdsz(flash);
Graf Yang49aac4a2009-06-15 08:23:41 +0000560 t[1].len = 1;
561 t[1].tx_buf = buf + actual;
562
563 spi_sync(flash->spi, &m);
564 ret = wait_till_ready(flash);
565 if (ret)
566 goto time_out;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400567 *retlen += m.actual_length - m25p_cmdsz(flash);
Graf Yang49aac4a2009-06-15 08:23:41 +0000568 write_disable(flash);
569 }
570
571time_out:
572 mutex_unlock(&flash->lock);
573 return ret;
574}
Mike Lavender2f9f7622006-01-08 13:34:27 -0800575
576/****************************************************************************/
577
578/*
579 * SPI device driver setup and teardown
580 */
581
582struct flash_info {
David Brownellfa0a8c72007-06-24 15:12:35 -0700583 /* JEDEC id zero means "no ID" (most older chips); otherwise it has
584 * a high byte of zero plus three data bytes: the manufacturer id,
585 * then a two byte device id.
586 */
587 u32 jedec_id;
Chen Gongd0e8c472008-08-11 16:59:15 +0800588 u16 ext_id;
David Brownellfa0a8c72007-06-24 15:12:35 -0700589
590 /* The size listed here is what works with OPCODE_SE, which isn't
591 * necessarily called a "sector" by the vendor.
592 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800593 unsigned sector_size;
David Brownellfa0a8c72007-06-24 15:12:35 -0700594 u16 n_sectors;
595
Anton Vorontsov837479d2009-10-12 20:24:40 +0400596 u16 page_size;
597 u16 addr_width;
598
David Brownellfa0a8c72007-06-24 15:12:35 -0700599 u16 flags;
600#define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
Anton Vorontsov837479d2009-10-12 20:24:40 +0400601#define M25P_NO_ERASE 0x02 /* No erase command needed */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800602};
603
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400604#define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
605 ((kernel_ulong_t)&(struct flash_info) { \
606 .jedec_id = (_jedec_id), \
607 .ext_id = (_ext_id), \
608 .sector_size = (_sector_size), \
609 .n_sectors = (_n_sectors), \
Anton Vorontsov837479d2009-10-12 20:24:40 +0400610 .page_size = 256, \
611 .addr_width = 3, \
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400612 .flags = (_flags), \
613 })
David Brownellfa0a8c72007-06-24 15:12:35 -0700614
Anton Vorontsov837479d2009-10-12 20:24:40 +0400615#define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width) \
616 ((kernel_ulong_t)&(struct flash_info) { \
617 .sector_size = (_sector_size), \
618 .n_sectors = (_n_sectors), \
619 .page_size = (_page_size), \
620 .addr_width = (_addr_width), \
621 .flags = M25P_NO_ERASE, \
622 })
David Brownellfa0a8c72007-06-24 15:12:35 -0700623
624/* NOTE: double check command sets and memory organization when you add
625 * more flash chips. This current list focusses on newer chips, which
626 * have been converging on command sets which including JEDEC ID.
627 */
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400628static const struct spi_device_id m25p_ids[] = {
David Brownellfa0a8c72007-06-24 15:12:35 -0700629 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400630 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
631 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700632
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400633 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
634 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700635
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400636 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
637 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
638 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
639 { "at26df321", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700640
Lennert Buytenhekab1ff212009-05-20 13:07:11 +0200641 /* Macronix */
Simon Guinotdf0094d2009-12-05 15:28:00 +0100642 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400643 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
644 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
645 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
646 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
Lennert Buytenhekab1ff212009-05-20 13:07:11 +0200647
David Brownellfa0a8c72007-06-24 15:12:35 -0700648 /* Spansion -- single (large) sector size only, at least
649 * for the chips listed here (without boot sectors).
650 */
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400651 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
652 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
653 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
654 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
655 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
656 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
657 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
658 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
659 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700660
661 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400662 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K) },
663 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K) },
664 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K) },
665 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K) },
666 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K) },
667 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K) },
668 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K) },
669 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700670
671 /* ST Microelectronics -- newer production may have feature updates */
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400672 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
673 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
674 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
675 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
676 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
677 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
678 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
679 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
680 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700681
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400682 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
683 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
684 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700685
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400686 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
687 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700688
David Woodhouse02d087d2007-06-28 22:38:38 +0100689 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400690 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
691 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
692 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
693 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
694 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
695 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
696 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
Mike Lavender2f9f7622006-01-08 13:34:27 -0800697
Anton Vorontsov837479d2009-10-12 20:24:40 +0400698 /* Catalyst / On Semiconductor -- non-JEDEC */
699 { "cat25c11", CAT25_INFO( 16, 8, 16, 1) },
700 { "cat25c03", CAT25_INFO( 32, 8, 16, 2) },
701 { "cat25c09", CAT25_INFO( 128, 8, 32, 2) },
702 { "cat25c17", CAT25_INFO( 256, 8, 32, 2) },
703 { "cat25128", CAT25_INFO(2048, 8, 64, 2) },
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400704 { },
Mike Lavender2f9f7622006-01-08 13:34:27 -0800705};
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400706MODULE_DEVICE_TABLE(spi, m25p_ids);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800707
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400708static const struct spi_device_id *__devinit jedec_probe(struct spi_device *spi)
David Brownellfa0a8c72007-06-24 15:12:35 -0700709{
710 int tmp;
711 u8 code = OPCODE_RDID;
Chen Gongdaa84732008-09-16 14:14:12 +0800712 u8 id[5];
David Brownellfa0a8c72007-06-24 15:12:35 -0700713 u32 jedec;
Chen Gongd0e8c472008-08-11 16:59:15 +0800714 u16 ext_jedec;
David Brownellfa0a8c72007-06-24 15:12:35 -0700715 struct flash_info *info;
716
717 /* JEDEC also defines an optional "extended device information"
718 * string for after vendor-specific data, after the three bytes
719 * we use here. Supporting some chips might require using it.
720 */
Chen Gongdaa84732008-09-16 14:14:12 +0800721 tmp = spi_write_then_read(spi, &code, 1, id, 5);
David Brownellfa0a8c72007-06-24 15:12:35 -0700722 if (tmp < 0) {
723 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000724 dev_name(&spi->dev), tmp);
David Brownellfa0a8c72007-06-24 15:12:35 -0700725 return NULL;
726 }
727 jedec = id[0];
728 jedec = jedec << 8;
729 jedec |= id[1];
730 jedec = jedec << 8;
731 jedec |= id[2];
732
Anton Vorontsov18c61822009-10-12 20:24:38 +0400733 /*
734 * Some chips (like Numonyx M25P80) have JEDEC and non-JEDEC variants,
735 * which depend on technology process. Officially RDID command doesn't
736 * exist for non-JEDEC chips, but for compatibility they return ID 0.
737 */
738 if (jedec == 0)
739 return NULL;
740
Chen Gongd0e8c472008-08-11 16:59:15 +0800741 ext_jedec = id[3] << 8 | id[4];
742
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400743 for (tmp = 0; tmp < ARRAY_SIZE(m25p_ids) - 1; tmp++) {
744 info = (void *)m25p_ids[tmp].driver_data;
Mike Frysingera3d3f732008-11-26 10:23:25 +0000745 if (info->jedec_id == jedec) {
Mike Frysinger9168ab82008-11-26 10:23:35 +0000746 if (info->ext_id != 0 && info->ext_id != ext_jedec)
Chen Gongd0e8c472008-08-11 16:59:15 +0800747 continue;
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400748 return &m25p_ids[tmp];
Mike Frysingera3d3f732008-11-26 10:23:25 +0000749 }
David Brownellfa0a8c72007-06-24 15:12:35 -0700750 }
David Brownellfa0a8c72007-06-24 15:12:35 -0700751 return NULL;
752}
753
754
Mike Lavender2f9f7622006-01-08 13:34:27 -0800755/*
756 * board specific setup should have ensured the SPI clock used here
757 * matches what the READ command supports, at least until this driver
758 * understands FAST_READ (for clocks over 25 MHz).
759 */
760static int __devinit m25p_probe(struct spi_device *spi)
761{
Anton Vorontsov18c61822009-10-12 20:24:38 +0400762 const struct spi_device_id *id = spi_get_device_id(spi);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800763 struct flash_platform_data *data;
764 struct m25p *flash;
765 struct flash_info *info;
766 unsigned i;
767
768 /* Platform data helps sort out which chip type we have, as
David Brownellfa0a8c72007-06-24 15:12:35 -0700769 * well as how this board partitions it. If we don't have
770 * a chip ID, try the JEDEC id commands; they'll work for most
771 * newer chips, even if we don't recognize the particular chip.
Mike Lavender2f9f7622006-01-08 13:34:27 -0800772 */
773 data = spi->dev.platform_data;
David Brownellfa0a8c72007-06-24 15:12:35 -0700774 if (data && data->type) {
Anton Vorontsov18c61822009-10-12 20:24:38 +0400775 const struct spi_device_id *plat_id;
776
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400777 for (i = 0; i < ARRAY_SIZE(m25p_ids) - 1; i++) {
Anton Vorontsov18c61822009-10-12 20:24:38 +0400778 plat_id = &m25p_ids[i];
779 if (strcmp(data->type, plat_id->name))
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400780 continue;
781 break;
David Brownellfa0a8c72007-06-24 15:12:35 -0700782 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800783
Anton Vorontsov18c61822009-10-12 20:24:38 +0400784 if (plat_id)
785 id = plat_id;
786 else
787 dev_warn(&spi->dev, "unrecognized id %s\n", data->type);
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400788 }
David Brownellfa0a8c72007-06-24 15:12:35 -0700789
Anton Vorontsov18c61822009-10-12 20:24:38 +0400790 info = (void *)id->driver_data;
David Brownellfa0a8c72007-06-24 15:12:35 -0700791
Anton Vorontsov18c61822009-10-12 20:24:38 +0400792 if (info->jedec_id) {
793 const struct spi_device_id *jid;
794
795 jid = jedec_probe(spi);
796 if (!jid) {
797 dev_info(&spi->dev, "non-JEDEC variant of %s\n",
798 id->name);
799 } else if (jid != id) {
800 /*
801 * JEDEC knows better, so overwrite platform ID. We
802 * can't trust partitions any longer, but we'll let
803 * mtd apply them anyway, since some partitions may be
804 * marked read-only, and we don't want to lose that
805 * information, even if it's not 100% accurate.
806 */
807 dev_warn(&spi->dev, "found %s, expected %s\n",
808 jid->name, id->name);
809 id = jid;
810 info = (void *)jid->driver_data;
David Brownellfa0a8c72007-06-24 15:12:35 -0700811 }
Anton Vorontsov18c61822009-10-12 20:24:38 +0400812 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800813
Christoph Lametere94b1762006-12-06 20:33:17 -0800814 flash = kzalloc(sizeof *flash, GFP_KERNEL);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800815 if (!flash)
816 return -ENOMEM;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400817 flash->command = kmalloc(MAX_CMD_SIZE + FAST_READ_DUMMY_BYTE, GFP_KERNEL);
Johannes Stezenbach61c35062009-10-28 14:21:37 +0100818 if (!flash->command) {
819 kfree(flash);
820 return -ENOMEM;
821 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800822
823 flash->spi = spi;
David Brownell7d5230e2007-06-24 15:09:13 -0700824 mutex_init(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800825 dev_set_drvdata(&spi->dev, flash);
826
Michael Hennerich72289822008-07-03 23:54:42 -0700827 /*
Graf Yangea60658a2009-09-24 15:46:22 -0400828 * Atmel and SST serial flash tend to power
829 * up with the software protection bits set
Michael Hennerich72289822008-07-03 23:54:42 -0700830 */
831
Graf Yangea60658a2009-09-24 15:46:22 -0400832 if (info->jedec_id >> 16 == 0x1f ||
833 info->jedec_id >> 16 == 0xbf) {
Michael Hennerich72289822008-07-03 23:54:42 -0700834 write_enable(flash);
835 write_sr(flash, 0);
836 }
837
David Brownellfa0a8c72007-06-24 15:12:35 -0700838 if (data && data->name)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800839 flash->mtd.name = data->name;
840 else
Kay Sievers160bbab2008-12-23 10:00:14 +0000841 flash->mtd.name = dev_name(&spi->dev);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800842
843 flash->mtd.type = MTD_NORFLASH;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400844 flash->mtd.writesize = 1;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800845 flash->mtd.flags = MTD_CAP_NORFLASH;
846 flash->mtd.size = info->sector_size * info->n_sectors;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800847 flash->mtd.erase = m25p80_erase;
848 flash->mtd.read = m25p80_read;
Graf Yang49aac4a2009-06-15 08:23:41 +0000849
850 /* sst flash chips use AAI word program */
851 if (info->jedec_id >> 16 == 0xbf)
852 flash->mtd.write = sst_write;
853 else
854 flash->mtd.write = m25p80_write;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800855
David Brownellfa0a8c72007-06-24 15:12:35 -0700856 /* prefer "small sector" erase if possible */
857 if (info->flags & SECT_4K) {
858 flash->erase_opcode = OPCODE_BE_4K;
859 flash->mtd.erasesize = 4096;
860 } else {
861 flash->erase_opcode = OPCODE_SE;
862 flash->mtd.erasesize = info->sector_size;
863 }
864
Anton Vorontsov837479d2009-10-12 20:24:40 +0400865 if (info->flags & M25P_NO_ERASE)
866 flash->mtd.flags |= MTD_NO_ERASE;
David Brownell87f39f02009-03-26 00:42:50 -0700867
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200868 flash->mtd.dev.parent = &spi->dev;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400869 flash->page_size = info->page_size;
870 flash->addr_width = info->addr_width;
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200871
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400872 dev_info(&spi->dev, "%s (%lld Kbytes)\n", id->name,
Mike Lavender2f9f7622006-01-08 13:34:27 -0800873 (long long)flash->mtd.size >> 10);
874
875 DEBUG(MTD_DEBUG_LEVEL2,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200876 "mtd .name = %s, .size = 0x%llx (%lldMiB) "
David Woodhouse02d087d2007-06-28 22:38:38 +0100877 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800878 flash->mtd.name,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200879 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
Mike Lavender2f9f7622006-01-08 13:34:27 -0800880 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
881 flash->mtd.numeraseregions);
882
883 if (flash->mtd.numeraseregions)
884 for (i = 0; i < flash->mtd.numeraseregions; i++)
885 DEBUG(MTD_DEBUG_LEVEL2,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200886 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
David Woodhouse02d087d2007-06-28 22:38:38 +0100887 ".erasesize = 0x%.8x (%uKiB), "
Mike Lavender2f9f7622006-01-08 13:34:27 -0800888 ".numblocks = %d }\n",
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200889 i, (long long)flash->mtd.eraseregions[i].offset,
Mike Lavender2f9f7622006-01-08 13:34:27 -0800890 flash->mtd.eraseregions[i].erasesize,
891 flash->mtd.eraseregions[i].erasesize / 1024,
892 flash->mtd.eraseregions[i].numblocks);
893
894
895 /* partitions should match sector boundaries; and it may be good to
896 * use readonly partitions for writeprotected sectors (BP2..BP0).
897 */
898 if (mtd_has_partitions()) {
899 struct mtd_partition *parts = NULL;
900 int nr_parts = 0;
901
David Brownella4b6d512009-03-04 12:01:41 -0800902 if (mtd_has_cmdlinepart()) {
903 static const char *part_probes[]
904 = { "cmdlinepart", NULL, };
Mike Lavender2f9f7622006-01-08 13:34:27 -0800905
David Brownella4b6d512009-03-04 12:01:41 -0800906 nr_parts = parse_mtd_partitions(&flash->mtd,
907 part_probes, &parts, 0);
908 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800909
910 if (nr_parts <= 0 && data && data->parts) {
911 parts = data->parts;
912 nr_parts = data->nr_parts;
913 }
914
915 if (nr_parts > 0) {
David Brownellfa0a8c72007-06-24 15:12:35 -0700916 for (i = 0; i < nr_parts; i++) {
Mike Lavender2f9f7622006-01-08 13:34:27 -0800917 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200918 "{.name = %s, .offset = 0x%llx, "
919 ".size = 0x%llx (%lldKiB) }\n",
David Brownellfa0a8c72007-06-24 15:12:35 -0700920 i, parts[i].name,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200921 (long long)parts[i].offset,
922 (long long)parts[i].size,
923 (long long)(parts[i].size >> 10));
Mike Lavender2f9f7622006-01-08 13:34:27 -0800924 }
925 flash->partitioned = 1;
926 return add_mtd_partitions(&flash->mtd, parts, nr_parts);
927 }
Anton Vorontsovedcb3b12009-08-06 15:18:37 -0700928 } else if (data && data->nr_parts)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800929 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
930 data->nr_parts, data->name);
931
932 return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
933}
934
935
936static int __devexit m25p_remove(struct spi_device *spi)
937{
938 struct m25p *flash = dev_get_drvdata(&spi->dev);
939 int status;
940
941 /* Clean up MTD stuff. */
942 if (mtd_has_partitions() && flash->partitioned)
943 status = del_mtd_partitions(&flash->mtd);
944 else
945 status = del_mtd_device(&flash->mtd);
Johannes Stezenbach61c35062009-10-28 14:21:37 +0100946 if (status == 0) {
947 kfree(flash->command);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800948 kfree(flash);
Johannes Stezenbach61c35062009-10-28 14:21:37 +0100949 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800950 return 0;
951}
952
953
954static struct spi_driver m25p80_driver = {
955 .driver = {
956 .name = "m25p80",
957 .bus = &spi_bus_type,
958 .owner = THIS_MODULE,
959 },
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400960 .id_table = m25p_ids,
Mike Lavender2f9f7622006-01-08 13:34:27 -0800961 .probe = m25p_probe,
962 .remove = __devexit_p(m25p_remove),
David Brownellfa0a8c72007-06-24 15:12:35 -0700963
964 /* REVISIT: many of these chips have deep power-down modes, which
965 * should clearly be entered on suspend() to minimize power use.
966 * And also when they're otherwise idle...
967 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800968};
969
970
Peter Huewe627df232009-06-11 02:23:33 +0200971static int __init m25p80_init(void)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800972{
973 return spi_register_driver(&m25p80_driver);
974}
975
976
Peter Huewe627df232009-06-11 02:23:33 +0200977static void __exit m25p80_exit(void)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800978{
979 spi_unregister_driver(&m25p80_driver);
980}
981
982
983module_init(m25p80_init);
984module_exit(m25p80_exit);
985
986MODULE_LICENSE("GPL");
987MODULE_AUTHOR("Mike Lavender");
988MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");