4 * Driver for SST25L SPI Flash chips
6 * Copyright © 2009 Bluewater Systems Ltd
7 * Author: Andre Renaud <andre@bluewatersys.com>
8 * Author: Ryan Mallon <ryan@bluewatersys.com>
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.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/mutex.h>
22 #include <linux/interrupt.h>
23 #include <linux/slab.h>
24 #include <linux/sched.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/partitions.h>
29 #include <linux/spi/spi.h>
30 #include <linux/spi/flash.h>
32 /* Erases can take up to 3 seconds! */
33 #define MAX_READY_WAIT_JIFFIES msecs_to_jiffies(3000)
35 #define SST25L_CMD_WRSR 0x01 /* Write status register */
36 #define SST25L_CMD_WRDI 0x04 /* Write disable */
37 #define SST25L_CMD_RDSR 0x05 /* Read status register */
38 #define SST25L_CMD_WREN 0x06 /* Write enable */
39 #define SST25L_CMD_READ 0x03 /* High speed read */
41 #define SST25L_CMD_EWSR 0x50 /* Enable write status register */
42 #define SST25L_CMD_SECTOR_ERASE 0x20 /* Erase sector */
43 #define SST25L_CMD_READ_ID 0x90 /* Read device ID */
44 #define SST25L_CMD_AAI_PROGRAM 0xaf /* Auto address increment */
46 #define SST25L_STATUS_BUSY (1 << 0) /* Chip is busy */
47 #define SST25L_STATUS_WREN (1 << 1) /* Write enabled */
48 #define SST25L_STATUS_BP0 (1 << 2) /* Block protection 0 */
49 #define SST25L_STATUS_BP1 (1 << 3) /* Block protection 1 */
52 struct spi_device *spi;
67 #define to_sst25l_flash(x) container_of(x, struct sst25l_flash, mtd)
69 static struct flash_info __initdata sst25l_flash_info[] = {
70 {"sst25lf020a", 0xbf43, 256, 1024, 4096},
71 {"sst25lf040a", 0xbf44, 256, 2048, 4096},
74 static int sst25l_status(struct sst25l_flash *flash, int *status)
76 unsigned char command, response;
79 command = SST25L_CMD_RDSR;
80 err = spi_write_then_read(flash->spi, &command, 1, &response, 1);
88 static int sst25l_write_enable(struct sst25l_flash *flash, int enable)
90 unsigned char command[2];
93 command[0] = enable ? SST25L_CMD_WREN : SST25L_CMD_WRDI;
94 err = spi_write(flash->spi, command, 1);
98 command[0] = SST25L_CMD_EWSR;
99 err = spi_write(flash->spi, command, 1);
103 command[0] = SST25L_CMD_WRSR;
104 command[1] = enable ? 0 : SST25L_STATUS_BP0 | SST25L_STATUS_BP1;
105 err = spi_write(flash->spi, command, 2);
110 err = sst25l_status(flash, &status);
113 if (!(status & SST25L_STATUS_WREN))
120 static int sst25l_wait_till_ready(struct sst25l_flash *flash)
122 unsigned long deadline;
125 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
127 err = sst25l_status(flash, &status);
130 if (!(status & SST25L_STATUS_BUSY))
134 } while (!time_after_eq(jiffies, deadline));
139 static int sst25l_erase_sector(struct sst25l_flash *flash, uint32_t offset)
141 unsigned char command[4];
144 err = sst25l_write_enable(flash, 1);
148 command[0] = SST25L_CMD_SECTOR_ERASE;
149 command[1] = offset >> 16;
150 command[2] = offset >> 8;
152 err = spi_write(flash->spi, command, 4);
156 err = sst25l_wait_till_ready(flash);
160 return sst25l_write_enable(flash, 0);
163 static int sst25l_erase(struct mtd_info *mtd, struct erase_info *instr)
165 struct sst25l_flash *flash = to_sst25l_flash(mtd);
170 if (instr->addr + instr->len > flash->mtd.size)
173 if ((uint32_t)instr->len % mtd->erasesize)
176 if ((uint32_t)instr->addr % mtd->erasesize)
180 end = addr + instr->len;
182 mutex_lock(&flash->lock);
184 err = sst25l_wait_till_ready(flash);
186 mutex_unlock(&flash->lock);
191 err = sst25l_erase_sector(flash, addr);
193 mutex_unlock(&flash->lock);
194 instr->state = MTD_ERASE_FAILED;
195 dev_err(&flash->spi->dev, "Erase failed\n");
199 addr += mtd->erasesize;
202 mutex_unlock(&flash->lock);
204 instr->state = MTD_ERASE_DONE;
205 mtd_erase_callback(instr);
209 static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len,
210 size_t *retlen, unsigned char *buf)
212 struct sst25l_flash *flash = to_sst25l_flash(mtd);
213 struct spi_transfer transfer[2];
214 struct spi_message message;
215 unsigned char command[4];
218 /* Sanity checking */
222 if (from + len > flash->mtd.size)
228 spi_message_init(&message);
229 memset(&transfer, 0, sizeof(transfer));
231 command[0] = SST25L_CMD_READ;
232 command[1] = from >> 16;
233 command[2] = from >> 8;
236 transfer[0].tx_buf = command;
237 transfer[0].len = sizeof(command);
238 spi_message_add_tail(&transfer[0], &message);
240 transfer[1].rx_buf = buf;
241 transfer[1].len = len;
242 spi_message_add_tail(&transfer[1], &message);
244 mutex_lock(&flash->lock);
246 /* Wait for previous write/erase to complete */
247 ret = sst25l_wait_till_ready(flash);
249 mutex_unlock(&flash->lock);
253 spi_sync(flash->spi, &message);
255 if (retlen && message.actual_length > sizeof(command))
256 *retlen += message.actual_length - sizeof(command);
258 mutex_unlock(&flash->lock);
262 static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len,
263 size_t *retlen, const unsigned char *buf)
265 struct sst25l_flash *flash = to_sst25l_flash(mtd);
266 int i, j, ret, bytes, copied = 0;
267 unsigned char command[5];
273 if (to + len > flash->mtd.size)
276 if ((uint32_t)to % mtd->writesize)
279 mutex_lock(&flash->lock);
281 ret = sst25l_write_enable(flash, 1);
285 for (i = 0; i < len; i += mtd->writesize) {
286 ret = sst25l_wait_till_ready(flash);
290 /* Write the first byte of the page */
291 command[0] = SST25L_CMD_AAI_PROGRAM;
292 command[1] = (to + i) >> 16;
293 command[2] = (to + i) >> 8;
294 command[3] = (to + i);
296 ret = spi_write(flash->spi, command, 5);
302 * Write the remaining bytes using auto address
305 bytes = min_t(uint32_t, mtd->writesize, len - i);
306 for (j = 1; j < bytes; j++, copied++) {
307 ret = sst25l_wait_till_ready(flash);
311 command[1] = buf[i + j];
312 ret = spi_write(flash->spi, command, 2);
319 ret = sst25l_write_enable(flash, 0);
324 mutex_unlock(&flash->lock);
328 static struct flash_info *__init sst25l_match_device(struct spi_device *spi)
330 struct flash_info *flash_info = NULL;
331 unsigned char command[4], response;
335 command[0] = SST25L_CMD_READ_ID;
339 err = spi_write_then_read(spi, command, sizeof(command), &response, 1);
341 dev_err(&spi->dev, "error reading device id msb\n");
347 command[0] = SST25L_CMD_READ_ID;
351 err = spi_write_then_read(spi, command, sizeof(command), &response, 1);
353 dev_err(&spi->dev, "error reading device id lsb\n");
359 for (i = 0; i < ARRAY_SIZE(sst25l_flash_info); i++)
360 if (sst25l_flash_info[i].device_id == id)
361 flash_info = &sst25l_flash_info[i];
364 dev_err(&spi->dev, "unknown id %.4x\n", id);
369 static int __init sst25l_probe(struct spi_device *spi)
371 struct flash_info *flash_info;
372 struct sst25l_flash *flash;
373 struct flash_platform_data *data;
376 flash_info = sst25l_match_device(spi);
380 flash = kzalloc(sizeof(struct sst25l_flash), GFP_KERNEL);
385 mutex_init(&flash->lock);
386 dev_set_drvdata(&spi->dev, flash);
388 data = spi->dev.platform_data;
389 if (data && data->name)
390 flash->mtd.name = data->name;
392 flash->mtd.name = dev_name(&spi->dev);
394 flash->mtd.type = MTD_NORFLASH;
395 flash->mtd.flags = MTD_CAP_NORFLASH;
396 flash->mtd.erasesize = flash_info->erase_size;
397 flash->mtd.writesize = flash_info->page_size;
398 flash->mtd.size = flash_info->page_size * flash_info->nr_pages;
399 flash->mtd.erase = sst25l_erase;
400 flash->mtd.read = sst25l_read;
401 flash->mtd.write = sst25l_write;
403 dev_info(&spi->dev, "%s (%lld KiB)\n", flash_info->name,
404 (long long)flash->mtd.size >> 10);
406 DEBUG(MTD_DEBUG_LEVEL2,
407 "mtd .name = %s, .size = 0x%llx (%lldMiB) "
408 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
410 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
411 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
412 flash->mtd.numeraseregions);
414 if (flash->mtd.numeraseregions)
415 for (i = 0; i < flash->mtd.numeraseregions; i++)
416 DEBUG(MTD_DEBUG_LEVEL2,
417 "mtd.eraseregions[%d] = { .offset = 0x%llx, "
418 ".erasesize = 0x%.8x (%uKiB), "
419 ".numblocks = %d }\n",
420 i, (long long)flash->mtd.eraseregions[i].offset,
421 flash->mtd.eraseregions[i].erasesize,
422 flash->mtd.eraseregions[i].erasesize / 1024,
423 flash->mtd.eraseregions[i].numblocks);
425 if (mtd_has_partitions()) {
426 struct mtd_partition *parts = NULL;
429 if (mtd_has_cmdlinepart()) {
430 static const char *part_probes[] =
431 {"cmdlinepart", NULL};
433 nr_parts = parse_mtd_partitions(&flash->mtd,
438 if (nr_parts <= 0 && data && data->parts) {
440 nr_parts = data->nr_parts;
444 for (i = 0; i < nr_parts; i++) {
445 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
446 "{.name = %s, .offset = 0x%llx, "
447 ".size = 0x%llx (%lldKiB) }\n",
449 (long long)parts[i].offset,
450 (long long)parts[i].size,
451 (long long)(parts[i].size >> 10));
454 flash->partitioned = 1;
455 return add_mtd_partitions(&flash->mtd,
459 } else if (data->nr_parts) {
460 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
461 data->nr_parts, data->name);
464 ret = add_mtd_device(&flash->mtd);
467 dev_set_drvdata(&spi->dev, NULL);
474 static int __exit sst25l_remove(struct spi_device *spi)
476 struct sst25l_flash *flash = dev_get_drvdata(&spi->dev);
479 if (mtd_has_partitions() && flash->partitioned)
480 ret = del_mtd_partitions(&flash->mtd);
482 ret = del_mtd_device(&flash->mtd);
488 static struct spi_driver sst25l_driver = {
491 .bus = &spi_bus_type,
492 .owner = THIS_MODULE,
494 .probe = sst25l_probe,
495 .remove = __exit_p(sst25l_remove),
498 static int __init sst25l_init(void)
500 return spi_register_driver(&sst25l_driver);
503 static void __exit sst25l_exit(void)
505 spi_unregister_driver(&sst25l_driver);
508 module_init(sst25l_init);
509 module_exit(sst25l_exit);
511 MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips");
512 MODULE_AUTHOR("Andre Renaud <andre@bluewatersys.com>, "
513 "Ryan Mallon <ryan@bluewatersys.com>");
514 MODULE_LICENSE("GPL");