]> err.no Git - linux-2.6/blob - drivers/mtd/devices/m25p80.c
[MTD] m25p80 converted to mutex
[linux-2.6] / drivers / mtd / devices / m25p80.c
1 /*
2  * MTD SPI driver for ST M25Pxx flash chips
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>
22 #include <linux/mutex.h>
23
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/partitions.h>
26
27 #include <linux/spi/spi.h>
28 #include <linux/spi/flash.h>
29
30
31 /* NOTE: AT 25F and SST 25LF series are very similar,
32  * as are other newer Atmel dataflash chips (AT26),
33  * but commands for sector erase and chip id differ...
34  */
35
36 #define FLASH_PAGESIZE          256
37
38 /* Flash opcodes. */
39 #define OPCODE_WREN             6       /* Write enable */
40 #define OPCODE_RDSR             5       /* Read status register */
41 #define OPCODE_READ             3       /* Read data bytes */
42 #define OPCODE_PP               2       /* Page program */
43 #define OPCODE_SE               0xd8    /* Sector erase */
44 #define OPCODE_RES              0xab    /* Read Electronic Signature */
45 #define OPCODE_RDID             0x9f    /* Read JEDEC ID */
46
47 /* Status Register bits. */
48 #define SR_WIP                  1       /* Write in progress */
49 #define SR_WEL                  2       /* Write enable latch */
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
57
58
59 #ifdef CONFIG_MTD_PARTITIONS
60 #define mtd_has_partitions()    (1)
61 #else
62 #define mtd_has_partitions()    (0)
63 #endif
64
65 /****************************************************************************/
66
67 struct m25p {
68         struct spi_device       *spi;
69         struct mutex            lock;
70         struct mtd_info         mtd;
71         unsigned                partitioned;
72         u8                      command[4];
73 };
74
75 static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
76 {
77         return container_of(mtd, struct m25p, mtd);
78 }
79
80 /****************************************************************************/
81
82 /*
83  * Internal helper functions
84  */
85
86 /*
87  * Read the status register, returning its value in the location
88  * Return the status register value.
89  * Returns negative if error occurred.
90  */
91 static int read_sr(struct m25p *flash)
92 {
93         ssize_t retval;
94         u8 code = OPCODE_RDSR;
95         u8 val;
96
97         retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
98
99         if (retval < 0) {
100                 dev_err(&flash->spi->dev, "error %d reading SR\n",
101                                 (int) retval);
102                 return retval;
103         }
104
105         return val;
106 }
107
108
109 /*
110  * Set write enable latch with Write Enable command.
111  * Returns negative if error occurred.
112  */
113 static inline int write_enable(struct m25p *flash)
114 {
115         u8      code = OPCODE_WREN;
116
117         return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
118 }
119
120
121 /*
122  * Service routine to read status register until ready, or timeout occurs.
123  * Returns non-zero if error.
124  */
125 static int wait_till_ready(struct m25p *flash)
126 {
127         int count;
128         int sr;
129
130         /* one chip guarantees max 5 msec wait here after page writes,
131          * but potentially three seconds (!) after page erase.
132          */
133         for (count = 0; count < MAX_READY_WAIT_COUNT; count++) {
134                 if ((sr = read_sr(flash)) < 0)
135                         break;
136                 else if (!(sr & SR_WIP))
137                         return 0;
138
139                 /* REVISIT sometimes sleeping would be best */
140         }
141
142         return 1;
143 }
144
145
146 /*
147  * Erase one sector of flash memory at offset ``offset'' which is any
148  * address within the sector which should be erased.
149  *
150  * Returns 0 if successful, non-zero otherwise.
151  */
152 static int erase_sector(struct m25p *flash, u32 offset)
153 {
154         DEBUG(MTD_DEBUG_LEVEL3, "%s: %s at 0x%08x\n", flash->spi->dev.bus_id,
155                         __FUNCTION__, offset);
156
157         /* Wait until finished previous write command. */
158         if (wait_till_ready(flash))
159                 return 1;
160
161         /* Send write enable, then erase commands. */
162         write_enable(flash);
163
164         /* Set up command buffer. */
165         flash->command[0] = OPCODE_SE;
166         flash->command[1] = offset >> 16;
167         flash->command[2] = offset >> 8;
168         flash->command[3] = offset;
169
170         spi_write(flash->spi, flash->command, sizeof(flash->command));
171
172         return 0;
173 }
174
175 /****************************************************************************/
176
177 /*
178  * MTD implementation
179  */
180
181 /*
182  * Erase an address range on the flash chip.  The address range may extend
183  * one or more erase sectors.  Return an error is there is a problem erasing.
184  */
185 static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
186 {
187         struct m25p *flash = mtd_to_m25p(mtd);
188         u32 addr,len;
189
190         DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %d\n",
191                         flash->spi->dev.bus_id, __FUNCTION__, "at",
192                         (u32)instr->addr, instr->len);
193
194         /* sanity checks */
195         if (instr->addr + instr->len > flash->mtd.size)
196                 return -EINVAL;
197         if ((instr->addr % mtd->erasesize) != 0
198                         || (instr->len % mtd->erasesize) != 0) {
199                 return -EINVAL;
200         }
201
202         addr = instr->addr;
203         len = instr->len;
204
205         mutex_lock(&flash->lock);
206
207         /* now erase those sectors */
208         while (len) {
209                 if (erase_sector(flash, addr)) {
210                         instr->state = MTD_ERASE_FAILED;
211                         mutex_unlock(&flash->lock);
212                         return -EIO;
213                 }
214
215                 addr += mtd->erasesize;
216                 len -= mtd->erasesize;
217         }
218
219         mutex_unlock(&flash->lock);
220
221         instr->state = MTD_ERASE_DONE;
222         mtd_erase_callback(instr);
223
224         return 0;
225 }
226
227 /*
228  * Read an address range from the flash chip.  The address range
229  * may be any size provided it is within the physical boundaries.
230  */
231 static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
232         size_t *retlen, u_char *buf)
233 {
234         struct m25p *flash = mtd_to_m25p(mtd);
235         struct spi_transfer t[2];
236         struct spi_message m;
237
238         DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
239                         flash->spi->dev.bus_id, __FUNCTION__, "from",
240                         (u32)from, len);
241
242         /* sanity checks */
243         if (!len)
244                 return 0;
245
246         if (from + len > flash->mtd.size)
247                 return -EINVAL;
248
249         spi_message_init(&m);
250         memset(t, 0, (sizeof t));
251
252         t[0].tx_buf = flash->command;
253         t[0].len = sizeof(flash->command);
254         spi_message_add_tail(&t[0], &m);
255
256         t[1].rx_buf = buf;
257         t[1].len = len;
258         spi_message_add_tail(&t[1], &m);
259
260         /* Byte count starts at zero. */
261         if (retlen)
262                 *retlen = 0;
263
264         mutex_lock(&flash->lock);
265
266         /* Wait till previous write/erase is done. */
267         if (wait_till_ready(flash)) {
268                 /* REVISIT status return?? */
269                 mutex_unlock(&flash->lock);
270                 return 1;
271         }
272
273         /* NOTE:  OPCODE_FAST_READ (if available) is faster... */
274
275         /* Set up the write data buffer. */
276         flash->command[0] = OPCODE_READ;
277         flash->command[1] = from >> 16;
278         flash->command[2] = from >> 8;
279         flash->command[3] = from;
280
281         spi_sync(flash->spi, &m);
282
283         *retlen = m.actual_length - sizeof(flash->command);
284
285         mutex_unlock(&flash->lock);
286
287         return 0;
288 }
289
290 /*
291  * Write an address range to the flash chip.  Data must be written in
292  * FLASH_PAGESIZE chunks.  The address range may be any size provided
293  * it is within the physical boundaries.
294  */
295 static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
296         size_t *retlen, const u_char *buf)
297 {
298         struct m25p *flash = mtd_to_m25p(mtd);
299         u32 page_offset, page_size;
300         struct spi_transfer t[2];
301         struct spi_message m;
302
303         DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
304                         flash->spi->dev.bus_id, __FUNCTION__, "to",
305                         (u32)to, len);
306
307         if (retlen)
308                 *retlen = 0;
309
310         /* sanity checks */
311         if (!len)
312                 return(0);
313
314         if (to + len > flash->mtd.size)
315                 return -EINVAL;
316
317         spi_message_init(&m);
318         memset(t, 0, (sizeof t));
319
320         t[0].tx_buf = flash->command;
321         t[0].len = sizeof(flash->command);
322         spi_message_add_tail(&t[0], &m);
323
324         t[1].tx_buf = buf;
325         spi_message_add_tail(&t[1], &m);
326
327         mutex_lock(&flash->lock);
328
329         /* Wait until finished previous write command. */
330         if (wait_till_ready(flash))
331                 return 1;
332
333         write_enable(flash);
334
335         /* Set up the opcode in the write buffer. */
336         flash->command[0] = OPCODE_PP;
337         flash->command[1] = to >> 16;
338         flash->command[2] = to >> 8;
339         flash->command[3] = to;
340
341         /* what page do we start with? */
342         page_offset = to % FLASH_PAGESIZE;
343
344         /* do all the bytes fit onto one page? */
345         if (page_offset + len <= FLASH_PAGESIZE) {
346                 t[1].len = len;
347
348                 spi_sync(flash->spi, &m);
349
350                 *retlen = m.actual_length - sizeof(flash->command);
351         } else {
352                 u32 i;
353
354                 /* the size of data remaining on the first page */
355                 page_size = FLASH_PAGESIZE - page_offset;
356
357                 t[1].len = page_size;
358                 spi_sync(flash->spi, &m);
359
360                 *retlen = m.actual_length - sizeof(flash->command);
361
362                 /* write everything in PAGESIZE chunks */
363                 for (i = page_size; i < len; i += page_size) {
364                         page_size = len - i;
365                         if (page_size > FLASH_PAGESIZE)
366                                 page_size = FLASH_PAGESIZE;
367
368                         /* write the next page to flash */
369                         flash->command[1] = (to + i) >> 16;
370                         flash->command[2] = (to + i) >> 8;
371                         flash->command[3] = (to + i);
372
373                         t[1].tx_buf = buf + i;
374                         t[1].len = page_size;
375
376                         wait_till_ready(flash);
377
378                         write_enable(flash);
379
380                         spi_sync(flash->spi, &m);
381
382                         if (retlen)
383                                 *retlen += m.actual_length
384                                         - sizeof(flash->command);
385                 }
386         }
387
388         mutex_unlock(&flash->lock);
389
390         return 0;
391 }
392
393
394 /****************************************************************************/
395
396 /*
397  * SPI device driver setup and teardown
398  */
399
400 struct flash_info {
401         char            *name;
402         u8              id;
403         u16             jedec_id;
404         unsigned        sector_size;
405         unsigned        n_sectors;
406 };
407
408 static struct flash_info __devinitdata m25p_data [] = {
409         /* JEDEC id zero means "has no ID" */
410         { "m25p05", 0x05, 0x2010, 32 * 1024, 2 },
411         { "m25p10", 0x10, 0x2011, 32 * 1024, 4 },
412         { "m25p20", 0x11, 0x2012, 64 * 1024, 4 },
413         { "m25p40", 0x12, 0x2013, 64 * 1024, 8 },
414         { "m25p80", 0x13, 0x0000, 64 * 1024, 16 },
415         { "m25p16", 0x14, 0x2015, 64 * 1024, 32 },
416         { "m25p32", 0x15, 0x2016, 64 * 1024, 64 },
417         { "m25p64", 0x16, 0x2017, 64 * 1024, 128 },
418 };
419
420 /*
421  * board specific setup should have ensured the SPI clock used here
422  * matches what the READ command supports, at least until this driver
423  * understands FAST_READ (for clocks over 25 MHz).
424  */
425 static int __devinit m25p_probe(struct spi_device *spi)
426 {
427         struct flash_platform_data      *data;
428         struct m25p                     *flash;
429         struct flash_info               *info;
430         unsigned                        i;
431
432         /* Platform data helps sort out which chip type we have, as
433          * well as how this board partitions it.
434          */
435         data = spi->dev.platform_data;
436         if (!data || !data->type) {
437                 /* FIXME some chips can identify themselves with RES
438                  * or JEDEC get-id commands.  Try them ...
439                  */
440                 DEBUG(MTD_DEBUG_LEVEL1, "%s: no chip id\n",
441                                 spi->dev.bus_id);
442                 return -ENODEV;
443         }
444
445         for (i = 0, info = m25p_data; i < ARRAY_SIZE(m25p_data); i++, info++) {
446                 if (strcmp(data->type, info->name) == 0)
447                         break;
448         }
449         if (i == ARRAY_SIZE(m25p_data)) {
450                 DEBUG(MTD_DEBUG_LEVEL1, "%s: unrecognized id %s\n",
451                                 spi->dev.bus_id, data->type);
452                 return -ENODEV;
453         }
454
455         flash = kzalloc(sizeof *flash, GFP_KERNEL);
456         if (!flash)
457                 return -ENOMEM;
458
459         flash->spi = spi;
460         mutex_init(&flash->lock);
461         dev_set_drvdata(&spi->dev, flash);
462
463         if (data->name)
464                 flash->mtd.name = data->name;
465         else
466                 flash->mtd.name = spi->dev.bus_id;
467
468         flash->mtd.type = MTD_NORFLASH;
469         flash->mtd.writesize = 1;
470         flash->mtd.flags = MTD_CAP_NORFLASH;
471         flash->mtd.size = info->sector_size * info->n_sectors;
472         flash->mtd.erasesize = info->sector_size;
473         flash->mtd.erase = m25p80_erase;
474         flash->mtd.read = m25p80_read;
475         flash->mtd.write = m25p80_write;
476
477         dev_info(&spi->dev, "%s (%d Kbytes)\n", info->name,
478                         flash->mtd.size / 1024);
479
480         DEBUG(MTD_DEBUG_LEVEL2,
481                 "mtd .name = %s, .size = 0x%.8x (%uM) "
482                         ".erasesize = 0x%.8x (%uK) .numeraseregions = %d\n",
483                 flash->mtd.name,
484                 flash->mtd.size, flash->mtd.size / (1024*1024),
485                 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
486                 flash->mtd.numeraseregions);
487
488         if (flash->mtd.numeraseregions)
489                 for (i = 0; i < flash->mtd.numeraseregions; i++)
490                         DEBUG(MTD_DEBUG_LEVEL2,
491                                 "mtd.eraseregions[%d] = { .offset = 0x%.8x, "
492                                 ".erasesize = 0x%.8x (%uK), "
493                                 ".numblocks = %d }\n",
494                                 i, flash->mtd.eraseregions[i].offset,
495                                 flash->mtd.eraseregions[i].erasesize,
496                                 flash->mtd.eraseregions[i].erasesize / 1024,
497                                 flash->mtd.eraseregions[i].numblocks);
498
499
500         /* partitions should match sector boundaries; and it may be good to
501          * use readonly partitions for writeprotected sectors (BP2..BP0).
502          */
503         if (mtd_has_partitions()) {
504                 struct mtd_partition    *parts = NULL;
505                 int                     nr_parts = 0;
506
507 #ifdef CONFIG_MTD_CMDLINE_PARTS
508                 static const char *part_probes[] = { "cmdlinepart", NULL, };
509
510                 nr_parts = parse_mtd_partitions(&flash->mtd,
511                                 part_probes, &parts, 0);
512 #endif
513
514                 if (nr_parts <= 0 && data && data->parts) {
515                         parts = data->parts;
516                         nr_parts = data->nr_parts;
517                 }
518
519                 if (nr_parts > 0) {
520                         for (i = 0; i < data->nr_parts; i++) {
521                                 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
522                                         "{.name = %s, .offset = 0x%.8x, "
523                                                 ".size = 0x%.8x (%uK) }\n",
524                                         i, data->parts[i].name,
525                                         data->parts[i].offset,
526                                         data->parts[i].size,
527                                         data->parts[i].size / 1024);
528                         }
529                         flash->partitioned = 1;
530                         return add_mtd_partitions(&flash->mtd, parts, nr_parts);
531                 }
532         } else if (data->nr_parts)
533                 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
534                                 data->nr_parts, data->name);
535
536         return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
537 }
538
539
540 static int __devexit m25p_remove(struct spi_device *spi)
541 {
542         struct m25p     *flash = dev_get_drvdata(&spi->dev);
543         int             status;
544
545         /* Clean up MTD stuff. */
546         if (mtd_has_partitions() && flash->partitioned)
547                 status = del_mtd_partitions(&flash->mtd);
548         else
549                 status = del_mtd_device(&flash->mtd);
550         if (status == 0)
551                 kfree(flash);
552         return 0;
553 }
554
555
556 static struct spi_driver m25p80_driver = {
557         .driver = {
558                 .name   = "m25p80",
559                 .bus    = &spi_bus_type,
560                 .owner  = THIS_MODULE,
561         },
562         .probe  = m25p_probe,
563         .remove = __devexit_p(m25p_remove),
564 };
565
566
567 static int m25p80_init(void)
568 {
569         return spi_register_driver(&m25p80_driver);
570 }
571
572
573 static void m25p80_exit(void)
574 {
575         spi_unregister_driver(&m25p80_driver);
576 }
577
578
579 module_init(m25p80_init);
580 module_exit(m25p80_exit);
581
582 MODULE_LICENSE("GPL");
583 MODULE_AUTHOR("Mike Lavender");
584 MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");