[MTD] m25p80.c code cleanup
[deliverable/linux.git] / drivers / mtd / devices / m25p80.c
CommitLineData
2f9f7628 1/*
fa0a8c71 2 * MTD SPI driver for ST M25Pxx (and similar) serial flash chips
2f9f7628
ML
3 *
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>
7d5230ea
DB
22#include <linux/mutex.h>
23
2f9f7628
ML
24#include <linux/mtd/mtd.h>
25#include <linux/mtd/partitions.h>
7d5230ea 26
2f9f7628
ML
27#include <linux/spi/spi.h>
28#include <linux/spi/flash.h>
29
2f9f7628 30
2f9f7628
ML
31#define FLASH_PAGESIZE 256
32
33/* Flash opcodes. */
fa0a8c71
DB
34#define OPCODE_WREN 0x06 /* Write enable */
35#define OPCODE_RDSR 0x05 /* Read status register */
72289824 36#define OPCODE_WRSR 0x01 /* Write status register 1 byte */
2230b76b 37#define OPCODE_NORM_READ 0x03 /* Read data bytes (low frequency) */
fa0a8c71
DB
38#define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */
39#define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */
02d087db
DW
40#define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
41#define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
faff3750 42#define OPCODE_BE 0xc7 /* Erase whole flash block */
02d087db 43#define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
2f9f7628
ML
44#define OPCODE_RDID 0x9f /* Read JEDEC ID */
45
46/* Status Register bits. */
47#define SR_WIP 1 /* Write in progress */
48#define SR_WEL 2 /* Write enable latch */
fa0a8c71 49/* meaning of other SR_* bits may differ between vendors */
2f9f7628
ML
50#define SR_BP0 4 /* Block protect 0 */
51#define SR_BP1 8 /* Block protect 1 */
52#define SR_BP2 0x10 /* Block protect 2 */
53#define SR_SRWD 0x80 /* SR write protect */
54
55/* Define max times to check status register before we give up. */
56#define MAX_READY_WAIT_COUNT 100000
2230b76b 57#define CMD_SIZE 4
2f9f7628 58
2230b76b
BW
59#ifdef CONFIG_M25PXX_USE_FAST_READ
60#define OPCODE_READ OPCODE_FAST_READ
61#define FAST_READ_DUMMY_BYTE 1
62#else
63#define OPCODE_READ OPCODE_NORM_READ
64#define FAST_READ_DUMMY_BYTE 0
65#endif
2f9f7628
ML
66
67#ifdef CONFIG_MTD_PARTITIONS
68#define mtd_has_partitions() (1)
69#else
70#define mtd_has_partitions() (0)
71#endif
72
73/****************************************************************************/
74
75struct m25p {
76 struct spi_device *spi;
7d5230ea 77 struct mutex lock;
2f9f7628 78 struct mtd_info mtd;
fa0a8c71
DB
79 unsigned partitioned:1;
80 u8 erase_opcode;
2230b76b 81 u8 command[CMD_SIZE + FAST_READ_DUMMY_BYTE];
2f9f7628
ML
82};
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
72289824
MH
117/*
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}
2f9f7628
ML
128
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
75d0ee22 137 return spi_write(flash->spi, &code, 1);
2f9f7628
ML
138}
139
140
141/*
142 * Service routine to read status register until ready, or timeout occurs.
143 * Returns non-zero if error.
144 */
145static int wait_till_ready(struct m25p *flash)
146{
147 int count;
148 int sr;
149
150 /* one chip guarantees max 5 msec wait here after page writes,
151 * but potentially three seconds (!) after page erase.
152 */
153 for (count = 0; count < MAX_READY_WAIT_COUNT; count++) {
154 if ((sr = read_sr(flash)) < 0)
155 break;
156 else if (!(sr & SR_WIP))
157 return 0;
158
159 /* REVISIT sometimes sleeping would be best */
160 }
161
162 return 1;
163}
164
faff3750
CG
165/*
166 * Erase the whole flash memory
167 *
168 * Returns 0 if successful, non-zero otherwise.
169 */
170static int erase_block(struct m25p *flash)
171{
172 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB\n",
173 flash->spi->dev.bus_id, __func__,
174 flash->mtd.size / 1024);
175
176 /* Wait until finished previous write command. */
177 if (wait_till_ready(flash))
178 return 1;
179
180 /* Send write enable, then erase commands. */
181 write_enable(flash);
182
183 /* Set up command buffer. */
184 flash->command[0] = OPCODE_BE;
185
186 spi_write(flash->spi, flash->command, 1);
187
188 return 0;
189}
2f9f7628
ML
190
191/*
192 * Erase one sector of flash memory at offset ``offset'' which is any
193 * address within the sector which should be erased.
194 *
195 * Returns 0 if successful, non-zero otherwise.
196 */
197static int erase_sector(struct m25p *flash, u32 offset)
198{
02d087db 199 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
cb53b3b9 200 flash->spi->dev.bus_id, __func__,
fa0a8c71 201 flash->mtd.erasesize / 1024, offset);
2f9f7628
ML
202
203 /* Wait until finished previous write command. */
204 if (wait_till_ready(flash))
205 return 1;
206
207 /* Send write enable, then erase commands. */
208 write_enable(flash);
209
210 /* Set up command buffer. */
fa0a8c71 211 flash->command[0] = flash->erase_opcode;
2f9f7628
ML
212 flash->command[1] = offset >> 16;
213 flash->command[2] = offset >> 8;
214 flash->command[3] = offset;
215
2230b76b 216 spi_write(flash->spi, flash->command, CMD_SIZE);
2f9f7628
ML
217
218 return 0;
219}
220
221/****************************************************************************/
222
223/*
224 * MTD implementation
225 */
226
227/*
228 * Erase an address range on the flash chip. The address range may extend
229 * one or more erase sectors. Return an error is there is a problem erasing.
230 */
231static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
232{
233 struct m25p *flash = mtd_to_m25p(mtd);
234 u32 addr,len;
235
19676ff0 236 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %d\n",
cb53b3b9 237 flash->spi->dev.bus_id, __func__, "at",
2f9f7628
ML
238 (u32)instr->addr, instr->len);
239
240 /* sanity checks */
241 if (instr->addr + instr->len > flash->mtd.size)
242 return -EINVAL;
243 if ((instr->addr % mtd->erasesize) != 0
244 || (instr->len % mtd->erasesize) != 0) {
245 return -EINVAL;
246 }
247
248 addr = instr->addr;
249 len = instr->len;
250
7d5230ea 251 mutex_lock(&flash->lock);
2f9f7628 252
fa0a8c71
DB
253 /* REVISIT in some cases we could speed up erasing large regions
254 * by using OPCODE_SE instead of OPCODE_BE_4K
255 */
256
2f9f7628 257 /* now erase those sectors */
faff3750
CG
258 if (len == flash->mtd.size && erase_block(flash)) {
259 instr->state = MTD_ERASE_FAILED;
260 mutex_unlock(&flash->lock);
261 return -EIO;
262 } else {
263 while (len) {
264 if (erase_sector(flash, addr)) {
265 instr->state = MTD_ERASE_FAILED;
266 mutex_unlock(&flash->lock);
267 return -EIO;
268 }
269
270 addr += mtd->erasesize;
271 len -= mtd->erasesize;
2f9f7628 272 }
2f9f7628
ML
273 }
274
7d5230ea 275 mutex_unlock(&flash->lock);
2f9f7628
ML
276
277 instr->state = MTD_ERASE_DONE;
278 mtd_erase_callback(instr);
279
280 return 0;
281}
282
283/*
284 * Read an address range from the flash chip. The address range
285 * may be any size provided it is within the physical boundaries.
286 */
287static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
288 size_t *retlen, u_char *buf)
289{
290 struct m25p *flash = mtd_to_m25p(mtd);
291 struct spi_transfer t[2];
292 struct spi_message m;
293
294 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
cb53b3b9 295 flash->spi->dev.bus_id, __func__, "from",
2f9f7628
ML
296 (u32)from, len);
297
298 /* sanity checks */
299 if (!len)
300 return 0;
301
302 if (from + len > flash->mtd.size)
303 return -EINVAL;
304
8275c642
VW
305 spi_message_init(&m);
306 memset(t, 0, (sizeof t));
307
2230b76b
BW
308 /* NOTE:
309 * OPCODE_FAST_READ (if available) is faster.
310 * Should add 1 byte DUMMY_BYTE.
311 */
8275c642 312 t[0].tx_buf = flash->command;
2230b76b 313 t[0].len = CMD_SIZE + FAST_READ_DUMMY_BYTE;
8275c642
VW
314 spi_message_add_tail(&t[0], &m);
315
316 t[1].rx_buf = buf;
317 t[1].len = len;
318 spi_message_add_tail(&t[1], &m);
319
320 /* Byte count starts at zero. */
321 if (retlen)
322 *retlen = 0;
323
7d5230ea 324 mutex_lock(&flash->lock);
2f9f7628
ML
325
326 /* Wait till previous write/erase is done. */
327 if (wait_till_ready(flash)) {
328 /* REVISIT status return?? */
7d5230ea 329 mutex_unlock(&flash->lock);
2f9f7628
ML
330 return 1;
331 }
332
fa0a8c71
DB
333 /* FIXME switch to OPCODE_FAST_READ. It's required for higher
334 * clocks; and at this writing, every chip this driver handles
335 * supports that opcode.
336 */
2f9f7628
ML
337
338 /* Set up the write data buffer. */
339 flash->command[0] = OPCODE_READ;
340 flash->command[1] = from >> 16;
341 flash->command[2] = from >> 8;
342 flash->command[3] = from;
343
2f9f7628
ML
344 spi_sync(flash->spi, &m);
345
2230b76b 346 *retlen = m.actual_length - CMD_SIZE - FAST_READ_DUMMY_BYTE;
2f9f7628 347
7d5230ea 348 mutex_unlock(&flash->lock);
2f9f7628
ML
349
350 return 0;
351}
352
353/*
354 * Write an address range to the flash chip. Data must be written in
355 * FLASH_PAGESIZE chunks. The address range may be any size provided
356 * it is within the physical boundaries.
357 */
358static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
359 size_t *retlen, const u_char *buf)
360{
361 struct m25p *flash = mtd_to_m25p(mtd);
362 u32 page_offset, page_size;
363 struct spi_transfer t[2];
364 struct spi_message m;
365
366 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
cb53b3b9 367 flash->spi->dev.bus_id, __func__, "to",
2f9f7628
ML
368 (u32)to, len);
369
370 if (retlen)
371 *retlen = 0;
372
373 /* sanity checks */
374 if (!len)
375 return(0);
376
377 if (to + len > flash->mtd.size)
378 return -EINVAL;
379
8275c642
VW
380 spi_message_init(&m);
381 memset(t, 0, (sizeof t));
382
383 t[0].tx_buf = flash->command;
2230b76b 384 t[0].len = CMD_SIZE;
8275c642
VW
385 spi_message_add_tail(&t[0], &m);
386
387 t[1].tx_buf = buf;
388 spi_message_add_tail(&t[1], &m);
389
7d5230ea 390 mutex_lock(&flash->lock);
2f9f7628
ML
391
392 /* Wait until finished previous write command. */
bc018863
CG
393 if (wait_till_ready(flash)) {
394 mutex_unlock(&flash->lock);
2f9f7628 395 return 1;
bc018863 396 }
2f9f7628
ML
397
398 write_enable(flash);
399
2f9f7628
ML
400 /* Set up the opcode in the write buffer. */
401 flash->command[0] = OPCODE_PP;
402 flash->command[1] = to >> 16;
403 flash->command[2] = to >> 8;
404 flash->command[3] = to;
405
2f9f7628
ML
406 /* what page do we start with? */
407 page_offset = to % FLASH_PAGESIZE;
408
409 /* do all the bytes fit onto one page? */
410 if (page_offset + len <= FLASH_PAGESIZE) {
2f9f7628
ML
411 t[1].len = len;
412
413 spi_sync(flash->spi, &m);
414
2230b76b 415 *retlen = m.actual_length - CMD_SIZE;
2f9f7628
ML
416 } else {
417 u32 i;
418
419 /* the size of data remaining on the first page */
420 page_size = FLASH_PAGESIZE - page_offset;
421
2f9f7628
ML
422 t[1].len = page_size;
423 spi_sync(flash->spi, &m);
424
2230b76b 425 *retlen = m.actual_length - CMD_SIZE;
2f9f7628
ML
426
427 /* write everything in PAGESIZE chunks */
428 for (i = page_size; i < len; i += page_size) {
429 page_size = len - i;
430 if (page_size > FLASH_PAGESIZE)
431 page_size = FLASH_PAGESIZE;
432
433 /* write the next page to flash */
434 flash->command[1] = (to + i) >> 16;
435 flash->command[2] = (to + i) >> 8;
436 flash->command[3] = (to + i);
437
438 t[1].tx_buf = buf + i;
439 t[1].len = page_size;
440
441 wait_till_ready(flash);
442
443 write_enable(flash);
444
445 spi_sync(flash->spi, &m);
446
7111763d 447 if (retlen)
2230b76b 448 *retlen += m.actual_length - CMD_SIZE;
7d5230ea
DB
449 }
450 }
2f9f7628 451
7d5230ea 452 mutex_unlock(&flash->lock);
2f9f7628
ML
453
454 return 0;
455}
456
457
458/****************************************************************************/
459
460/*
461 * SPI device driver setup and teardown
462 */
463
464struct flash_info {
465 char *name;
fa0a8c71
DB
466
467 /* JEDEC id zero means "no ID" (most older chips); otherwise it has
468 * a high byte of zero plus three data bytes: the manufacturer id,
469 * then a two byte device id.
470 */
471 u32 jedec_id;
472
473 /* The size listed here is what works with OPCODE_SE, which isn't
474 * necessarily called a "sector" by the vendor.
475 */
2f9f7628 476 unsigned sector_size;
fa0a8c71
DB
477 u16 n_sectors;
478
479 u16 flags;
480#define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
2f9f7628
ML
481};
482
fa0a8c71
DB
483
484/* NOTE: double check command sets and memory organization when you add
485 * more flash chips. This current list focusses on newer chips, which
486 * have been converging on command sets which including JEDEC ID.
487 */
2f9f7628 488static struct flash_info __devinitdata m25p_data [] = {
fa0a8c71
DB
489
490 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
491 { "at25fs010", 0x1f6601, 32 * 1024, 4, SECT_4K, },
492 { "at25fs040", 0x1f6604, 64 * 1024, 8, SECT_4K, },
493
494 { "at25df041a", 0x1f4401, 64 * 1024, 8, SECT_4K, },
3887ed52 495 { "at25df641", 0x1f4800, 64 * 1024, 128, SECT_4K, },
fa0a8c71
DB
496
497 { "at26f004", 0x1f0400, 64 * 1024, 8, SECT_4K, },
498 { "at26df081a", 0x1f4501, 64 * 1024, 16, SECT_4K, },
499 { "at26df161a", 0x1f4601, 64 * 1024, 32, SECT_4K, },
500 { "at26df321", 0x1f4701, 64 * 1024, 64, SECT_4K, },
501
502 /* Spansion -- single (large) sector size only, at least
503 * for the chips listed here (without boot sectors).
504 */
505 { "s25sl004a", 0x010212, 64 * 1024, 8, },
506 { "s25sl008a", 0x010213, 64 * 1024, 16, },
507 { "s25sl016a", 0x010214, 64 * 1024, 32, },
508 { "s25sl032a", 0x010215, 64 * 1024, 64, },
509 { "s25sl064a", 0x010216, 64 * 1024, 128, },
510
511 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
512 { "sst25vf040b", 0xbf258d, 64 * 1024, 8, SECT_4K, },
513 { "sst25vf080b", 0xbf258e, 64 * 1024, 16, SECT_4K, },
514 { "sst25vf016b", 0xbf2541, 64 * 1024, 32, SECT_4K, },
515 { "sst25vf032b", 0xbf254a, 64 * 1024, 64, SECT_4K, },
516
517 /* ST Microelectronics -- newer production may have feature updates */
518 { "m25p05", 0x202010, 32 * 1024, 2, },
519 { "m25p10", 0x202011, 32 * 1024, 4, },
520 { "m25p20", 0x202012, 64 * 1024, 4, },
521 { "m25p40", 0x202013, 64 * 1024, 8, },
522 { "m25p80", 0, 64 * 1024, 16, },
523 { "m25p16", 0x202015, 64 * 1024, 32, },
524 { "m25p32", 0x202016, 64 * 1024, 64, },
525 { "m25p64", 0x202017, 64 * 1024, 128, },
526 { "m25p128", 0x202018, 256 * 1024, 64, },
527
528 { "m45pe80", 0x204014, 64 * 1024, 16, },
529 { "m45pe16", 0x204015, 64 * 1024, 32, },
530
531 { "m25pe80", 0x208014, 64 * 1024, 16, },
532 { "m25pe16", 0x208015, 64 * 1024, 32, SECT_4K, },
533
02d087db 534 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
fa0a8c71
DB
535 { "w25x10", 0xef3011, 64 * 1024, 2, SECT_4K, },
536 { "w25x20", 0xef3012, 64 * 1024, 4, SECT_4K, },
537 { "w25x40", 0xef3013, 64 * 1024, 8, SECT_4K, },
538 { "w25x80", 0xef3014, 64 * 1024, 16, SECT_4K, },
539 { "w25x16", 0xef3015, 64 * 1024, 32, SECT_4K, },
540 { "w25x32", 0xef3016, 64 * 1024, 64, SECT_4K, },
541 { "w25x64", 0xef3017, 64 * 1024, 128, SECT_4K, },
2f9f7628
ML
542};
543
fa0a8c71
DB
544static struct flash_info *__devinit jedec_probe(struct spi_device *spi)
545{
546 int tmp;
547 u8 code = OPCODE_RDID;
548 u8 id[3];
549 u32 jedec;
550 struct flash_info *info;
551
552 /* JEDEC also defines an optional "extended device information"
553 * string for after vendor-specific data, after the three bytes
554 * we use here. Supporting some chips might require using it.
555 */
556 tmp = spi_write_then_read(spi, &code, 1, id, 3);
557 if (tmp < 0) {
558 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
559 spi->dev.bus_id, tmp);
560 return NULL;
561 }
562 jedec = id[0];
563 jedec = jedec << 8;
564 jedec |= id[1];
565 jedec = jedec << 8;
566 jedec |= id[2];
567
568 for (tmp = 0, info = m25p_data;
569 tmp < ARRAY_SIZE(m25p_data);
570 tmp++, info++) {
571 if (info->jedec_id == jedec)
572 return info;
573 }
574 dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
575 return NULL;
576}
577
578
2f9f7628
ML
579/*
580 * board specific setup should have ensured the SPI clock used here
581 * matches what the READ command supports, at least until this driver
582 * understands FAST_READ (for clocks over 25 MHz).
583 */
584static int __devinit m25p_probe(struct spi_device *spi)
585{
586 struct flash_platform_data *data;
587 struct m25p *flash;
588 struct flash_info *info;
589 unsigned i;
590
591 /* Platform data helps sort out which chip type we have, as
fa0a8c71
DB
592 * well as how this board partitions it. If we don't have
593 * a chip ID, try the JEDEC id commands; they'll work for most
594 * newer chips, even if we don't recognize the particular chip.
2f9f7628
ML
595 */
596 data = spi->dev.platform_data;
fa0a8c71
DB
597 if (data && data->type) {
598 for (i = 0, info = m25p_data;
599 i < ARRAY_SIZE(m25p_data);
600 i++, info++) {
601 if (strcmp(data->type, info->name) == 0)
602 break;
603 }
2f9f7628 604
fa0a8c71
DB
605 /* unrecognized chip? */
606 if (i == ARRAY_SIZE(m25p_data)) {
607 DEBUG(MTD_DEBUG_LEVEL0, "%s: unrecognized id %s\n",
608 spi->dev.bus_id, data->type);
609 info = NULL;
610
611 /* recognized; is that chip really what's there? */
612 } else if (info->jedec_id) {
613 struct flash_info *chip = jedec_probe(spi);
614
615 if (!chip || chip != info) {
616 dev_warn(&spi->dev, "found %s, expected %s\n",
617 chip ? chip->name : "UNKNOWN",
618 info->name);
619 info = NULL;
620 }
621 }
622 } else
623 info = jedec_probe(spi);
624
625 if (!info)
2f9f7628 626 return -ENODEV;
2f9f7628 627
e94b1766 628 flash = kzalloc(sizeof *flash, GFP_KERNEL);
2f9f7628
ML
629 if (!flash)
630 return -ENOMEM;
631
632 flash->spi = spi;
7d5230ea 633 mutex_init(&flash->lock);
2f9f7628
ML
634 dev_set_drvdata(&spi->dev, flash);
635
72289824
MH
636 /*
637 * Atmel serial flash tend to power up
638 * with the software protection bits set
639 */
640
641 if (info->jedec_id >> 16 == 0x1f) {
642 write_enable(flash);
643 write_sr(flash, 0);
644 }
645
fa0a8c71 646 if (data && data->name)
2f9f7628
ML
647 flash->mtd.name = data->name;
648 else
649 flash->mtd.name = spi->dev.bus_id;
650
651 flash->mtd.type = MTD_NORFLASH;
783ed81f 652 flash->mtd.writesize = 1;
2f9f7628
ML
653 flash->mtd.flags = MTD_CAP_NORFLASH;
654 flash->mtd.size = info->sector_size * info->n_sectors;
2f9f7628
ML
655 flash->mtd.erase = m25p80_erase;
656 flash->mtd.read = m25p80_read;
657 flash->mtd.write = m25p80_write;
658
fa0a8c71
DB
659 /* prefer "small sector" erase if possible */
660 if (info->flags & SECT_4K) {
661 flash->erase_opcode = OPCODE_BE_4K;
662 flash->mtd.erasesize = 4096;
663 } else {
664 flash->erase_opcode = OPCODE_SE;
665 flash->mtd.erasesize = info->sector_size;
666 }
667
2f9f7628
ML
668 dev_info(&spi->dev, "%s (%d Kbytes)\n", info->name,
669 flash->mtd.size / 1024);
670
671 DEBUG(MTD_DEBUG_LEVEL2,
02d087db
DW
672 "mtd .name = %s, .size = 0x%.8x (%uMiB) "
673 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
2f9f7628
ML
674 flash->mtd.name,
675 flash->mtd.size, flash->mtd.size / (1024*1024),
676 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
677 flash->mtd.numeraseregions);
678
679 if (flash->mtd.numeraseregions)
680 for (i = 0; i < flash->mtd.numeraseregions; i++)
681 DEBUG(MTD_DEBUG_LEVEL2,
682 "mtd.eraseregions[%d] = { .offset = 0x%.8x, "
02d087db 683 ".erasesize = 0x%.8x (%uKiB), "
2f9f7628
ML
684 ".numblocks = %d }\n",
685 i, flash->mtd.eraseregions[i].offset,
686 flash->mtd.eraseregions[i].erasesize,
687 flash->mtd.eraseregions[i].erasesize / 1024,
688 flash->mtd.eraseregions[i].numblocks);
689
690
691 /* partitions should match sector boundaries; and it may be good to
692 * use readonly partitions for writeprotected sectors (BP2..BP0).
693 */
694 if (mtd_has_partitions()) {
695 struct mtd_partition *parts = NULL;
696 int nr_parts = 0;
697
698#ifdef CONFIG_MTD_CMDLINE_PARTS
699 static const char *part_probes[] = { "cmdlinepart", NULL, };
700
701 nr_parts = parse_mtd_partitions(&flash->mtd,
702 part_probes, &parts, 0);
703#endif
704
705 if (nr_parts <= 0 && data && data->parts) {
706 parts = data->parts;
707 nr_parts = data->nr_parts;
708 }
709
710 if (nr_parts > 0) {
fa0a8c71 711 for (i = 0; i < nr_parts; i++) {
2f9f7628
ML
712 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
713 "{.name = %s, .offset = 0x%.8x, "
02d087db 714 ".size = 0x%.8x (%uKiB) }\n",
fa0a8c71
DB
715 i, parts[i].name,
716 parts[i].offset,
717 parts[i].size,
718 parts[i].size / 1024);
2f9f7628
ML
719 }
720 flash->partitioned = 1;
721 return add_mtd_partitions(&flash->mtd, parts, nr_parts);
722 }
723 } else if (data->nr_parts)
724 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
725 data->nr_parts, data->name);
726
727 return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
728}
729
730
731static int __devexit m25p_remove(struct spi_device *spi)
732{
733 struct m25p *flash = dev_get_drvdata(&spi->dev);
734 int status;
735
736 /* Clean up MTD stuff. */
737 if (mtd_has_partitions() && flash->partitioned)
738 status = del_mtd_partitions(&flash->mtd);
739 else
740 status = del_mtd_device(&flash->mtd);
741 if (status == 0)
742 kfree(flash);
743 return 0;
744}
745
746
747static struct spi_driver m25p80_driver = {
748 .driver = {
749 .name = "m25p80",
750 .bus = &spi_bus_type,
751 .owner = THIS_MODULE,
752 },
753 .probe = m25p_probe,
754 .remove = __devexit_p(m25p_remove),
fa0a8c71
DB
755
756 /* REVISIT: many of these chips have deep power-down modes, which
757 * should clearly be entered on suspend() to minimize power use.
758 * And also when they're otherwise idle...
759 */
2f9f7628
ML
760};
761
762
763static int m25p80_init(void)
764{
765 return spi_register_driver(&m25p80_driver);
766}
767
768
769static void m25p80_exit(void)
770{
771 spi_unregister_driver(&m25p80_driver);
772}
773
774
775module_init(m25p80_init);
776module_exit(m25p80_exit);
777
778MODULE_LICENSE("GPL");
779MODULE_AUTHOR("Mike Lavender");
780MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");
This page took 0.282757 seconds and 5 git commands to generate.