blob: ae1bfb2ce1e0eace11ac697cfb1f446a05c62932 [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>
Anton Vorontsovb34bc032009-10-12 20:24:35 +040024#include <linux/mod_devicetable.h>
David Brownell7d5230e2007-06-24 15:09:13 -070025
Mike Lavender2f9f7622006-01-08 13:34:27 -080026#include <linux/mtd/mtd.h>
27#include <linux/mtd/partitions.h>
David Brownell7d5230e2007-06-24 15:09:13 -070028
Mike Lavender2f9f7622006-01-08 13:34:27 -080029#include <linux/spi/spi.h>
30#include <linux/spi/flash.h>
31
Mike Lavender2f9f7622006-01-08 13:34:27 -080032/* Flash opcodes. */
David Brownellfa0a8c72007-06-24 15:12:35 -070033#define OPCODE_WREN 0x06 /* Write enable */
34#define OPCODE_RDSR 0x05 /* Read status register */
Michael Hennerich72289822008-07-03 23:54:42 -070035#define OPCODE_WRSR 0x01 /* Write status register 1 byte */
Bryan Wu2230b762008-04-25 12:07:32 +080036#define OPCODE_NORM_READ 0x03 /* Read data bytes (low frequency) */
David Brownellfa0a8c72007-06-24 15:12:35 -070037#define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */
38#define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */
Chen Gong78546432008-11-26 10:23:57 +000039#define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
David Woodhouse02d087d2007-06-28 22:38:38 +010040#define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
Chen Gong78546432008-11-26 10:23:57 +000041#define OPCODE_CHIP_ERASE 0xc7 /* Erase whole flash chip */
David Woodhouse02d087d2007-06-28 22:38:38 +010042#define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
Mike Lavender2f9f7622006-01-08 13:34:27 -080043#define OPCODE_RDID 0x9f /* Read JEDEC ID */
44
Graf Yang49aac4a2009-06-15 08:23:41 +000045/* Used for SST flashes only. */
46#define OPCODE_BP 0x02 /* Byte program */
47#define OPCODE_WRDI 0x04 /* Write disable */
48#define OPCODE_AAI_WP 0xad /* Auto address increment word program */
49
Mike Lavender2f9f7622006-01-08 13:34:27 -080050/* Status Register bits. */
51#define SR_WIP 1 /* Write in progress */
52#define SR_WEL 2 /* Write enable latch */
David Brownellfa0a8c72007-06-24 15:12:35 -070053/* meaning of other SR_* bits may differ between vendors */
Mike Lavender2f9f7622006-01-08 13:34:27 -080054#define SR_BP0 4 /* Block protect 0 */
55#define SR_BP1 8 /* Block protect 1 */
56#define SR_BP2 0x10 /* Block protect 2 */
57#define SR_SRWD 0x80 /* SR write protect */
58
59/* Define max times to check status register before we give up. */
Steven A. Falco89bb8712009-06-26 12:42:47 -040060#define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
Anton Vorontsov837479d2009-10-12 20:24:40 +040061#define MAX_CMD_SIZE 4
Mike Lavender2f9f7622006-01-08 13:34:27 -080062
Bryan Wu2230b762008-04-25 12:07:32 +080063#ifdef CONFIG_M25PXX_USE_FAST_READ
64#define OPCODE_READ OPCODE_FAST_READ
65#define FAST_READ_DUMMY_BYTE 1
66#else
67#define OPCODE_READ OPCODE_NORM_READ
68#define FAST_READ_DUMMY_BYTE 0
69#endif
Mike Lavender2f9f7622006-01-08 13:34:27 -080070
Mike Lavender2f9f7622006-01-08 13:34:27 -080071/****************************************************************************/
72
73struct m25p {
74 struct spi_device *spi;
David Brownell7d5230e2007-06-24 15:09:13 -070075 struct mutex lock;
Mike Lavender2f9f7622006-01-08 13:34:27 -080076 struct mtd_info mtd;
David Brownellfa0a8c72007-06-24 15:12:35 -070077 unsigned partitioned:1;
Anton Vorontsov837479d2009-10-12 20:24:40 +040078 u16 page_size;
79 u16 addr_width;
David Brownellfa0a8c72007-06-24 15:12:35 -070080 u8 erase_opcode;
Johannes Stezenbach61c35062009-10-28 14:21:37 +010081 u8 *command;
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
Anton Vorontsov837479d2009-10-12 20:24:40 +0400200static void m25p_addr2cmd(struct m25p *flash, unsigned int addr, u8 *cmd)
201{
202 /* opcode is in cmd[0] */
203 cmd[1] = addr >> (flash->addr_width * 8 - 8);
204 cmd[2] = addr >> (flash->addr_width * 8 - 16);
205 cmd[3] = addr >> (flash->addr_width * 8 - 24);
206}
207
208static int m25p_cmdsz(struct m25p *flash)
209{
210 return 1 + flash->addr_width;
211}
212
Mike Lavender2f9f7622006-01-08 13:34:27 -0800213/*
214 * Erase one sector of flash memory at offset ``offset'' which is any
215 * address within the sector which should be erased.
216 *
217 * Returns 0 if successful, non-zero otherwise.
218 */
219static int erase_sector(struct m25p *flash, u32 offset)
220{
David Woodhouse02d087d2007-06-28 22:38:38 +0100221 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000222 dev_name(&flash->spi->dev), __func__,
David Brownellfa0a8c72007-06-24 15:12:35 -0700223 flash->mtd.erasesize / 1024, offset);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800224
225 /* Wait until finished previous write command. */
226 if (wait_till_ready(flash))
227 return 1;
228
229 /* Send write enable, then erase commands. */
230 write_enable(flash);
231
232 /* Set up command buffer. */
David Brownellfa0a8c72007-06-24 15:12:35 -0700233 flash->command[0] = flash->erase_opcode;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400234 m25p_addr2cmd(flash, offset, flash->command);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800235
Anton Vorontsov837479d2009-10-12 20:24:40 +0400236 spi_write(flash->spi, flash->command, m25p_cmdsz(flash));
Mike Lavender2f9f7622006-01-08 13:34:27 -0800237
238 return 0;
239}
240
241/****************************************************************************/
242
243/*
244 * MTD implementation
245 */
246
247/*
248 * Erase an address range on the flash chip. The address range may extend
249 * one or more erase sectors. Return an error is there is a problem erasing.
250 */
251static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
252{
253 struct m25p *flash = mtd_to_m25p(mtd);
254 u32 addr,len;
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200255 uint32_t rem;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800256
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200257 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%llx, len %lld\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000258 dev_name(&flash->spi->dev), __func__, "at",
259 (long long)instr->addr, (long long)instr->len);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800260
261 /* sanity checks */
262 if (instr->addr + instr->len > flash->mtd.size)
263 return -EINVAL;
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200264 div_u64_rem(instr->len, mtd->erasesize, &rem);
265 if (rem)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800266 return -EINVAL;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800267
268 addr = instr->addr;
269 len = instr->len;
270
David Brownell7d5230e2007-06-24 15:09:13 -0700271 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800272
Chen Gong78546432008-11-26 10:23:57 +0000273 /* whole-chip erase? */
Steven A. Falco3f33b0a2009-04-27 17:10:10 -0400274 if (len == flash->mtd.size) {
275 if (erase_chip(flash)) {
276 instr->state = MTD_ERASE_FAILED;
277 mutex_unlock(&flash->lock);
278 return -EIO;
279 }
Chen Gong78546432008-11-26 10:23:57 +0000280
281 /* REVISIT in some cases we could speed up erasing large regions
282 * by using OPCODE_SE instead of OPCODE_BE_4K. We may have set up
283 * to use "small sector erase", but that's not always optimal.
284 */
285
286 /* "sector"-at-a-time erase */
Chen Gongfaff3752008-08-11 16:59:13 +0800287 } else {
288 while (len) {
289 if (erase_sector(flash, addr)) {
290 instr->state = MTD_ERASE_FAILED;
291 mutex_unlock(&flash->lock);
292 return -EIO;
293 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800294
Chen Gongfaff3752008-08-11 16:59:13 +0800295 addr += mtd->erasesize;
296 len -= mtd->erasesize;
297 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800298 }
299
David Brownell7d5230e2007-06-24 15:09:13 -0700300 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800301
302 instr->state = MTD_ERASE_DONE;
303 mtd_erase_callback(instr);
304
305 return 0;
306}
307
308/*
309 * Read an address range from the flash chip. The address range
310 * may be any size provided it is within the physical boundaries.
311 */
312static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
313 size_t *retlen, u_char *buf)
314{
315 struct m25p *flash = mtd_to_m25p(mtd);
316 struct spi_transfer t[2];
317 struct spi_message m;
318
319 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000320 dev_name(&flash->spi->dev), __func__, "from",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800321 (u32)from, len);
322
323 /* sanity checks */
324 if (!len)
325 return 0;
326
327 if (from + len > flash->mtd.size)
328 return -EINVAL;
329
Vitaly Wool8275c642006-01-08 13:34:28 -0800330 spi_message_init(&m);
331 memset(t, 0, (sizeof t));
332
Bryan Wu2230b762008-04-25 12:07:32 +0800333 /* NOTE:
334 * OPCODE_FAST_READ (if available) is faster.
335 * Should add 1 byte DUMMY_BYTE.
336 */
Vitaly Wool8275c642006-01-08 13:34:28 -0800337 t[0].tx_buf = flash->command;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400338 t[0].len = m25p_cmdsz(flash) + FAST_READ_DUMMY_BYTE;
Vitaly Wool8275c642006-01-08 13:34:28 -0800339 spi_message_add_tail(&t[0], &m);
340
341 t[1].rx_buf = buf;
342 t[1].len = len;
343 spi_message_add_tail(&t[1], &m);
344
345 /* Byte count starts at zero. */
346 if (retlen)
347 *retlen = 0;
348
David Brownell7d5230e2007-06-24 15:09:13 -0700349 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800350
351 /* Wait till previous write/erase is done. */
352 if (wait_till_ready(flash)) {
353 /* REVISIT status return?? */
David Brownell7d5230e2007-06-24 15:09:13 -0700354 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800355 return 1;
356 }
357
David Brownellfa0a8c72007-06-24 15:12:35 -0700358 /* FIXME switch to OPCODE_FAST_READ. It's required for higher
359 * clocks; and at this writing, every chip this driver handles
360 * supports that opcode.
361 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800362
363 /* Set up the write data buffer. */
364 flash->command[0] = OPCODE_READ;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400365 m25p_addr2cmd(flash, from, flash->command);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800366
Mike Lavender2f9f7622006-01-08 13:34:27 -0800367 spi_sync(flash->spi, &m);
368
Anton Vorontsov837479d2009-10-12 20:24:40 +0400369 *retlen = m.actual_length - m25p_cmdsz(flash) - FAST_READ_DUMMY_BYTE;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800370
David Brownell7d5230e2007-06-24 15:09:13 -0700371 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800372
373 return 0;
374}
375
376/*
377 * Write an address range to the flash chip. Data must be written in
378 * FLASH_PAGESIZE chunks. The address range may be any size provided
379 * it is within the physical boundaries.
380 */
381static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
382 size_t *retlen, const u_char *buf)
383{
384 struct m25p *flash = mtd_to_m25p(mtd);
385 u32 page_offset, page_size;
386 struct spi_transfer t[2];
387 struct spi_message m;
388
389 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000390 dev_name(&flash->spi->dev), __func__, "to",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800391 (u32)to, len);
392
393 if (retlen)
394 *retlen = 0;
395
396 /* sanity checks */
397 if (!len)
398 return(0);
399
400 if (to + len > flash->mtd.size)
401 return -EINVAL;
402
Vitaly Wool8275c642006-01-08 13:34:28 -0800403 spi_message_init(&m);
404 memset(t, 0, (sizeof t));
405
406 t[0].tx_buf = flash->command;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400407 t[0].len = m25p_cmdsz(flash);
Vitaly Wool8275c642006-01-08 13:34:28 -0800408 spi_message_add_tail(&t[0], &m);
409
410 t[1].tx_buf = buf;
411 spi_message_add_tail(&t[1], &m);
412
David Brownell7d5230e2007-06-24 15:09:13 -0700413 mutex_lock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800414
415 /* Wait until finished previous write command. */
Chen Gongbc018862008-06-05 21:50:04 +0800416 if (wait_till_ready(flash)) {
417 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800418 return 1;
Chen Gongbc018862008-06-05 21:50:04 +0800419 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800420
421 write_enable(flash);
422
Mike Lavender2f9f7622006-01-08 13:34:27 -0800423 /* Set up the opcode in the write buffer. */
424 flash->command[0] = OPCODE_PP;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400425 m25p_addr2cmd(flash, to, flash->command);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800426
Anton Vorontsov837479d2009-10-12 20:24:40 +0400427 page_offset = to & (flash->page_size - 1);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800428
429 /* do all the bytes fit onto one page? */
Anton Vorontsov837479d2009-10-12 20:24:40 +0400430 if (page_offset + len <= flash->page_size) {
Mike Lavender2f9f7622006-01-08 13:34:27 -0800431 t[1].len = len;
432
433 spi_sync(flash->spi, &m);
434
Anton Vorontsov837479d2009-10-12 20:24:40 +0400435 *retlen = m.actual_length - m25p_cmdsz(flash);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800436 } else {
437 u32 i;
438
439 /* the size of data remaining on the first page */
Anton Vorontsov837479d2009-10-12 20:24:40 +0400440 page_size = flash->page_size - page_offset;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800441
Mike Lavender2f9f7622006-01-08 13:34:27 -0800442 t[1].len = page_size;
443 spi_sync(flash->spi, &m);
444
Anton Vorontsov837479d2009-10-12 20:24:40 +0400445 *retlen = m.actual_length - m25p_cmdsz(flash);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800446
Anton Vorontsov837479d2009-10-12 20:24:40 +0400447 /* write everything in flash->page_size chunks */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800448 for (i = page_size; i < len; i += page_size) {
449 page_size = len - i;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400450 if (page_size > flash->page_size)
451 page_size = flash->page_size;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800452
453 /* write the next page to flash */
Anton Vorontsov837479d2009-10-12 20:24:40 +0400454 m25p_addr2cmd(flash, to + i, flash->command);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800455
456 t[1].tx_buf = buf + i;
457 t[1].len = page_size;
458
459 wait_till_ready(flash);
460
461 write_enable(flash);
462
463 spi_sync(flash->spi, &m);
464
David Brownell7111763d2006-01-08 13:34:29 -0800465 if (retlen)
Anton Vorontsov837479d2009-10-12 20:24:40 +0400466 *retlen += m.actual_length - m25p_cmdsz(flash);
David Brownell7d5230e2007-06-24 15:09:13 -0700467 }
468 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800469
David Brownell7d5230e2007-06-24 15:09:13 -0700470 mutex_unlock(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800471
472 return 0;
473}
474
Graf Yang49aac4a2009-06-15 08:23:41 +0000475static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
476 size_t *retlen, const u_char *buf)
477{
478 struct m25p *flash = mtd_to_m25p(mtd);
479 struct spi_transfer t[2];
480 struct spi_message m;
481 size_t actual;
482 int cmd_sz, ret;
483
484 if (retlen)
485 *retlen = 0;
486
487 /* sanity checks */
488 if (!len)
489 return 0;
490
491 if (to + len > flash->mtd.size)
492 return -EINVAL;
493
494 spi_message_init(&m);
495 memset(t, 0, (sizeof t));
496
497 t[0].tx_buf = flash->command;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400498 t[0].len = m25p_cmdsz(flash);
Graf Yang49aac4a2009-06-15 08:23:41 +0000499 spi_message_add_tail(&t[0], &m);
500
501 t[1].tx_buf = buf;
502 spi_message_add_tail(&t[1], &m);
503
504 mutex_lock(&flash->lock);
505
506 /* Wait until finished previous write command. */
507 ret = wait_till_ready(flash);
508 if (ret)
509 goto time_out;
510
511 write_enable(flash);
512
513 actual = to % 2;
514 /* Start write from odd address. */
515 if (actual) {
516 flash->command[0] = OPCODE_BP;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400517 m25p_addr2cmd(flash, to, flash->command);
Graf Yang49aac4a2009-06-15 08:23:41 +0000518
519 /* write one byte. */
520 t[1].len = 1;
521 spi_sync(flash->spi, &m);
522 ret = wait_till_ready(flash);
523 if (ret)
524 goto time_out;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400525 *retlen += m.actual_length - m25p_cmdsz(flash);
Graf Yang49aac4a2009-06-15 08:23:41 +0000526 }
527 to += actual;
528
529 flash->command[0] = OPCODE_AAI_WP;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400530 m25p_addr2cmd(flash, to, flash->command);
Graf Yang49aac4a2009-06-15 08:23:41 +0000531
532 /* Write out most of the data here. */
Anton Vorontsov837479d2009-10-12 20:24:40 +0400533 cmd_sz = m25p_cmdsz(flash);
Graf Yang49aac4a2009-06-15 08:23:41 +0000534 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;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400557 m25p_addr2cmd(flash, to, flash->command);
558 t[0].len = m25p_cmdsz(flash);
Graf Yang49aac4a2009-06-15 08:23:41 +0000559 t[1].len = 1;
560 t[1].tx_buf = buf + actual;
561
562 spi_sync(flash->spi, &m);
563 ret = wait_till_ready(flash);
564 if (ret)
565 goto time_out;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400566 *retlen += m.actual_length - m25p_cmdsz(flash);
Graf Yang49aac4a2009-06-15 08:23:41 +0000567 write_disable(flash);
568 }
569
570time_out:
571 mutex_unlock(&flash->lock);
572 return ret;
573}
Mike Lavender2f9f7622006-01-08 13:34:27 -0800574
575/****************************************************************************/
576
577/*
578 * SPI device driver setup and teardown
579 */
580
581struct flash_info {
David Brownellfa0a8c72007-06-24 15:12:35 -0700582 /* JEDEC id zero means "no ID" (most older chips); otherwise it has
583 * a high byte of zero plus three data bytes: the manufacturer id,
584 * then a two byte device id.
585 */
586 u32 jedec_id;
Chen Gongd0e8c472008-08-11 16:59:15 +0800587 u16 ext_id;
David Brownellfa0a8c72007-06-24 15:12:35 -0700588
589 /* The size listed here is what works with OPCODE_SE, which isn't
590 * necessarily called a "sector" by the vendor.
591 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800592 unsigned sector_size;
David Brownellfa0a8c72007-06-24 15:12:35 -0700593 u16 n_sectors;
594
Anton Vorontsov837479d2009-10-12 20:24:40 +0400595 u16 page_size;
596 u16 addr_width;
597
David Brownellfa0a8c72007-06-24 15:12:35 -0700598 u16 flags;
599#define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
Anton Vorontsov837479d2009-10-12 20:24:40 +0400600#define M25P_NO_ERASE 0x02 /* No erase command needed */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800601};
602
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400603#define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
604 ((kernel_ulong_t)&(struct flash_info) { \
605 .jedec_id = (_jedec_id), \
606 .ext_id = (_ext_id), \
607 .sector_size = (_sector_size), \
608 .n_sectors = (_n_sectors), \
Anton Vorontsov837479d2009-10-12 20:24:40 +0400609 .page_size = 256, \
610 .addr_width = 3, \
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400611 .flags = (_flags), \
612 })
David Brownellfa0a8c72007-06-24 15:12:35 -0700613
Anton Vorontsov837479d2009-10-12 20:24:40 +0400614#define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width) \
615 ((kernel_ulong_t)&(struct flash_info) { \
616 .sector_size = (_sector_size), \
617 .n_sectors = (_n_sectors), \
618 .page_size = (_page_size), \
619 .addr_width = (_addr_width), \
620 .flags = M25P_NO_ERASE, \
621 })
622
David Brownellfa0a8c72007-06-24 15:12:35 -0700623/* NOTE: double check command sets and memory organization when you add
624 * more flash chips. This current list focusses on newer chips, which
625 * have been converging on command sets which including JEDEC ID.
626 */
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400627static const struct spi_device_id m25p_ids[] = {
David Brownellfa0a8c72007-06-24 15:12:35 -0700628 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400629 { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
630 { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700631
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400632 { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
633 { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700634
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400635 { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
636 { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
637 { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
638 { "at26df321", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700639
Lennert Buytenhekab1ff212009-05-20 13:07:11 +0200640 /* Macronix */
Simon Guinotdf0094d2009-12-05 15:28:00 +0100641 { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400642 { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
643 { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
644 { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
645 { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
Lennert Buytenhekab1ff212009-05-20 13:07:11 +0200646
David Brownellfa0a8c72007-06-24 15:12:35 -0700647 /* Spansion -- single (large) sector size only, at least
648 * for the chips listed here (without boot sectors).
649 */
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400650 { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
651 { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
652 { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
653 { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
654 { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
655 { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
656 { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
657 { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
658 { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700659
660 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400661 { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K) },
662 { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K) },
663 { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K) },
664 { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K) },
665 { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K) },
666 { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K) },
667 { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K) },
668 { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700669
670 /* ST Microelectronics -- newer production may have feature updates */
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400671 { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
672 { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
673 { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
674 { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
675 { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
676 { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
677 { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
678 { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
679 { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700680
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400681 { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
682 { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
683 { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700684
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400685 { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
686 { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
David Brownellfa0a8c72007-06-24 15:12:35 -0700687
David Woodhouse02d087d2007-06-28 22:38:38 +0100688 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400689 { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
690 { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
691 { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
692 { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
693 { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
694 { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
695 { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
Anton Vorontsov837479d2009-10-12 20:24:40 +0400696
697 /* Catalyst / On Semiconductor -- non-JEDEC */
698 { "cat25c11", CAT25_INFO( 16, 8, 16, 1) },
699 { "cat25c03", CAT25_INFO( 32, 8, 16, 2) },
700 { "cat25c09", CAT25_INFO( 128, 8, 32, 2) },
701 { "cat25c17", CAT25_INFO( 256, 8, 32, 2) },
702 { "cat25128", CAT25_INFO(2048, 8, 64, 2) },
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400703 { },
Mike Lavender2f9f7622006-01-08 13:34:27 -0800704};
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400705MODULE_DEVICE_TABLE(spi, m25p_ids);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800706
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400707static const struct spi_device_id *__devinit jedec_probe(struct spi_device *spi)
David Brownellfa0a8c72007-06-24 15:12:35 -0700708{
709 int tmp;
710 u8 code = OPCODE_RDID;
Chen Gongdaa84732008-09-16 14:14:12 +0800711 u8 id[5];
David Brownellfa0a8c72007-06-24 15:12:35 -0700712 u32 jedec;
Chen Gongd0e8c472008-08-11 16:59:15 +0800713 u16 ext_jedec;
David Brownellfa0a8c72007-06-24 15:12:35 -0700714 struct flash_info *info;
715
716 /* JEDEC also defines an optional "extended device information"
717 * string for after vendor-specific data, after the three bytes
718 * we use here. Supporting some chips might require using it.
719 */
Chen Gongdaa84732008-09-16 14:14:12 +0800720 tmp = spi_write_then_read(spi, &code, 1, id, 5);
David Brownellfa0a8c72007-06-24 15:12:35 -0700721 if (tmp < 0) {
722 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
Kay Sievers160bbab2008-12-23 10:00:14 +0000723 dev_name(&spi->dev), tmp);
David Brownellfa0a8c72007-06-24 15:12:35 -0700724 return NULL;
725 }
726 jedec = id[0];
727 jedec = jedec << 8;
728 jedec |= id[1];
729 jedec = jedec << 8;
730 jedec |= id[2];
731
Anton Vorontsov18c61822009-10-12 20:24:38 +0400732 /*
733 * Some chips (like Numonyx M25P80) have JEDEC and non-JEDEC variants,
734 * which depend on technology process. Officially RDID command doesn't
735 * exist for non-JEDEC chips, but for compatibility they return ID 0.
736 */
737 if (jedec == 0)
738 return NULL;
739
Chen Gongd0e8c472008-08-11 16:59:15 +0800740 ext_jedec = id[3] << 8 | id[4];
741
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400742 for (tmp = 0; tmp < ARRAY_SIZE(m25p_ids) - 1; tmp++) {
743 info = (void *)m25p_ids[tmp].driver_data;
Mike Frysingera3d3f732008-11-26 10:23:25 +0000744 if (info->jedec_id == jedec) {
Mike Frysinger9168ab82008-11-26 10:23:35 +0000745 if (info->ext_id != 0 && info->ext_id != ext_jedec)
Chen Gongd0e8c472008-08-11 16:59:15 +0800746 continue;
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400747 return &m25p_ids[tmp];
Mike Frysingera3d3f732008-11-26 10:23:25 +0000748 }
David Brownellfa0a8c72007-06-24 15:12:35 -0700749 }
David Brownellfa0a8c72007-06-24 15:12:35 -0700750 return NULL;
751}
752
753
Mike Lavender2f9f7622006-01-08 13:34:27 -0800754/*
755 * board specific setup should have ensured the SPI clock used here
756 * matches what the READ command supports, at least until this driver
757 * understands FAST_READ (for clocks over 25 MHz).
758 */
759static int __devinit m25p_probe(struct spi_device *spi)
760{
Anton Vorontsov18c61822009-10-12 20:24:38 +0400761 const struct spi_device_id *id = spi_get_device_id(spi);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800762 struct flash_platform_data *data;
763 struct m25p *flash;
764 struct flash_info *info;
765 unsigned i;
766
767 /* Platform data helps sort out which chip type we have, as
David Brownellfa0a8c72007-06-24 15:12:35 -0700768 * well as how this board partitions it. If we don't have
769 * a chip ID, try the JEDEC id commands; they'll work for most
770 * newer chips, even if we don't recognize the particular chip.
Mike Lavender2f9f7622006-01-08 13:34:27 -0800771 */
772 data = spi->dev.platform_data;
David Brownellfa0a8c72007-06-24 15:12:35 -0700773 if (data && data->type) {
Anton Vorontsov18c61822009-10-12 20:24:38 +0400774 const struct spi_device_id *plat_id;
775
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400776 for (i = 0; i < ARRAY_SIZE(m25p_ids) - 1; i++) {
Anton Vorontsov18c61822009-10-12 20:24:38 +0400777 plat_id = &m25p_ids[i];
778 if (strcmp(data->type, plat_id->name))
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400779 continue;
780 break;
David Brownellfa0a8c72007-06-24 15:12:35 -0700781 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800782
Anton Vorontsov18c61822009-10-12 20:24:38 +0400783 if (plat_id)
784 id = plat_id;
785 else
786 dev_warn(&spi->dev, "unrecognized id %s\n", data->type);
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400787 }
David Brownellfa0a8c72007-06-24 15:12:35 -0700788
Anton Vorontsov18c61822009-10-12 20:24:38 +0400789 info = (void *)id->driver_data;
790
791 if (info->jedec_id) {
792 const struct spi_device_id *jid;
793
794 jid = jedec_probe(spi);
795 if (!jid) {
796 dev_info(&spi->dev, "non-JEDEC variant of %s\n",
797 id->name);
798 } else if (jid != id) {
799 /*
800 * JEDEC knows better, so overwrite platform ID. We
801 * can't trust partitions any longer, but we'll let
802 * mtd apply them anyway, since some partitions may be
803 * marked read-only, and we don't want to lose that
804 * information, even if it's not 100% accurate.
805 */
806 dev_warn(&spi->dev, "found %s, expected %s\n",
807 jid->name, id->name);
808 id = jid;
809 info = (void *)jid->driver_data;
810 }
811 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800812
Christoph Lametere94b1762006-12-06 20:33:17 -0800813 flash = kzalloc(sizeof *flash, GFP_KERNEL);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800814 if (!flash)
815 return -ENOMEM;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400816 flash->command = kmalloc(MAX_CMD_SIZE + FAST_READ_DUMMY_BYTE, GFP_KERNEL);
Johannes Stezenbach61c35062009-10-28 14:21:37 +0100817 if (!flash->command) {
818 kfree(flash);
819 return -ENOMEM;
820 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800821
822 flash->spi = spi;
David Brownell7d5230e2007-06-24 15:09:13 -0700823 mutex_init(&flash->lock);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800824 dev_set_drvdata(&spi->dev, flash);
825
Michael Hennerich72289822008-07-03 23:54:42 -0700826 /*
Graf Yangea60658a2009-09-24 15:46:22 -0400827 * Atmel and SST serial flash tend to power
828 * up with the software protection bits set
Michael Hennerich72289822008-07-03 23:54:42 -0700829 */
830
Graf Yangea60658a2009-09-24 15:46:22 -0400831 if (info->jedec_id >> 16 == 0x1f ||
832 info->jedec_id >> 16 == 0xbf) {
Michael Hennerich72289822008-07-03 23:54:42 -0700833 write_enable(flash);
834 write_sr(flash, 0);
835 }
836
David Brownellfa0a8c72007-06-24 15:12:35 -0700837 if (data && data->name)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800838 flash->mtd.name = data->name;
839 else
Kay Sievers160bbab2008-12-23 10:00:14 +0000840 flash->mtd.name = dev_name(&spi->dev);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800841
842 flash->mtd.type = MTD_NORFLASH;
Artem B. Bityutskiy783ed812006-06-14 19:53:44 +0400843 flash->mtd.writesize = 1;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800844 flash->mtd.flags = MTD_CAP_NORFLASH;
845 flash->mtd.size = info->sector_size * info->n_sectors;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800846 flash->mtd.erase = m25p80_erase;
847 flash->mtd.read = m25p80_read;
Graf Yang49aac4a2009-06-15 08:23:41 +0000848
849 /* sst flash chips use AAI word program */
850 if (info->jedec_id >> 16 == 0xbf)
851 flash->mtd.write = sst_write;
852 else
853 flash->mtd.write = m25p80_write;
Mike Lavender2f9f7622006-01-08 13:34:27 -0800854
David Brownellfa0a8c72007-06-24 15:12:35 -0700855 /* prefer "small sector" erase if possible */
856 if (info->flags & SECT_4K) {
857 flash->erase_opcode = OPCODE_BE_4K;
858 flash->mtd.erasesize = 4096;
859 } else {
860 flash->erase_opcode = OPCODE_SE;
861 flash->mtd.erasesize = info->sector_size;
862 }
863
Anton Vorontsov837479d2009-10-12 20:24:40 +0400864 if (info->flags & M25P_NO_ERASE)
865 flash->mtd.flags |= MTD_NO_ERASE;
866
David Brownell87f39f02009-03-26 00:42:50 -0700867 flash->mtd.dev.parent = &spi->dev;
Anton Vorontsov837479d2009-10-12 20:24:40 +0400868 flash->page_size = info->page_size;
869 flash->addr_width = info->addr_width;
David Brownell87f39f02009-03-26 00:42:50 -0700870
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400871 dev_info(&spi->dev, "%s (%lld Kbytes)\n", id->name,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200872 (long long)flash->mtd.size >> 10);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800873
874 DEBUG(MTD_DEBUG_LEVEL2,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200875 "mtd .name = %s, .size = 0x%llx (%lldMiB) "
David Woodhouse02d087d2007-06-28 22:38:38 +0100876 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
Mike Lavender2f9f7622006-01-08 13:34:27 -0800877 flash->mtd.name,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200878 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
Mike Lavender2f9f7622006-01-08 13:34:27 -0800879 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
880 flash->mtd.numeraseregions);
881
882 if (flash->mtd.numeraseregions)
883 for (i = 0; i < flash->mtd.numeraseregions; i++)
884 DEBUG(MTD_DEBUG_LEVEL2,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200885 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
David Woodhouse02d087d2007-06-28 22:38:38 +0100886 ".erasesize = 0x%.8x (%uKiB), "
Mike Lavender2f9f7622006-01-08 13:34:27 -0800887 ".numblocks = %d }\n",
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200888 i, (long long)flash->mtd.eraseregions[i].offset,
Mike Lavender2f9f7622006-01-08 13:34:27 -0800889 flash->mtd.eraseregions[i].erasesize,
890 flash->mtd.eraseregions[i].erasesize / 1024,
891 flash->mtd.eraseregions[i].numblocks);
892
893
894 /* partitions should match sector boundaries; and it may be good to
895 * use readonly partitions for writeprotected sectors (BP2..BP0).
896 */
897 if (mtd_has_partitions()) {
898 struct mtd_partition *parts = NULL;
899 int nr_parts = 0;
900
David Brownella4b6d512009-03-04 12:01:41 -0800901 if (mtd_has_cmdlinepart()) {
902 static const char *part_probes[]
903 = { "cmdlinepart", NULL, };
Mike Lavender2f9f7622006-01-08 13:34:27 -0800904
David Brownella4b6d512009-03-04 12:01:41 -0800905 nr_parts = parse_mtd_partitions(&flash->mtd,
906 part_probes, &parts, 0);
907 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800908
909 if (nr_parts <= 0 && data && data->parts) {
910 parts = data->parts;
911 nr_parts = data->nr_parts;
912 }
913
914 if (nr_parts > 0) {
David Brownellfa0a8c72007-06-24 15:12:35 -0700915 for (i = 0; i < nr_parts; i++) {
Mike Lavender2f9f7622006-01-08 13:34:27 -0800916 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200917 "{.name = %s, .offset = 0x%llx, "
918 ".size = 0x%llx (%lldKiB) }\n",
David Brownellfa0a8c72007-06-24 15:12:35 -0700919 i, parts[i].name,
Artem Bityutskiyd85316a2008-12-18 14:10:05 +0200920 (long long)parts[i].offset,
921 (long long)parts[i].size,
922 (long long)(parts[i].size >> 10));
Mike Lavender2f9f7622006-01-08 13:34:27 -0800923 }
924 flash->partitioned = 1;
925 return add_mtd_partitions(&flash->mtd, parts, nr_parts);
926 }
Anton Vorontsovedcb3b12009-08-06 15:18:37 -0700927 } else if (data && data->nr_parts)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800928 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
929 data->nr_parts, data->name);
930
931 return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
932}
933
934
935static int __devexit m25p_remove(struct spi_device *spi)
936{
937 struct m25p *flash = dev_get_drvdata(&spi->dev);
938 int status;
939
940 /* Clean up MTD stuff. */
941 if (mtd_has_partitions() && flash->partitioned)
942 status = del_mtd_partitions(&flash->mtd);
943 else
944 status = del_mtd_device(&flash->mtd);
Johannes Stezenbach61c35062009-10-28 14:21:37 +0100945 if (status == 0) {
946 kfree(flash->command);
Mike Lavender2f9f7622006-01-08 13:34:27 -0800947 kfree(flash);
Johannes Stezenbach61c35062009-10-28 14:21:37 +0100948 }
Mike Lavender2f9f7622006-01-08 13:34:27 -0800949 return 0;
950}
951
952
953static struct spi_driver m25p80_driver = {
954 .driver = {
955 .name = "m25p80",
956 .bus = &spi_bus_type,
957 .owner = THIS_MODULE,
958 },
Anton Vorontsovb34bc032009-10-12 20:24:35 +0400959 .id_table = m25p_ids,
Mike Lavender2f9f7622006-01-08 13:34:27 -0800960 .probe = m25p_probe,
961 .remove = __devexit_p(m25p_remove),
David Brownellfa0a8c72007-06-24 15:12:35 -0700962
963 /* REVISIT: many of these chips have deep power-down modes, which
964 * should clearly be entered on suspend() to minimize power use.
965 * And also when they're otherwise idle...
966 */
Mike Lavender2f9f7622006-01-08 13:34:27 -0800967};
968
969
Peter Huewe627df232009-06-11 02:23:33 +0200970static int __init m25p80_init(void)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800971{
972 return spi_register_driver(&m25p80_driver);
973}
974
975
Peter Huewe627df232009-06-11 02:23:33 +0200976static void __exit m25p80_exit(void)
Mike Lavender2f9f7622006-01-08 13:34:27 -0800977{
978 spi_unregister_driver(&m25p80_driver);
979}
980
981
982module_init(m25p80_init);
983module_exit(m25p80_exit);
984
985MODULE_LICENSE("GPL");
986MODULE_AUTHOR("Mike Lavender");
987MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");