4 * Driver for SST25L SPI Flash chips
6 * Copyright © 2009 Bluewater Systems Ltd
7 * Author: Andre Renaud <andre@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;
65 #define to_sst25l_flash(x) container_of(x, struct sst25l_flash, mtd)
67 static struct flash_info sst25l_flash_info[] = {
68 {"sst25lf020a", 0xbf43, 256, 1024, 4096},
69 {"sst25lf040a", 0xbf44, 256, 2048, 4096},
72 static int sst25l_status(struct sst25l_flash *flash, int *status)
75 struct spi_transfer t;
76 unsigned char cmd_resp[2];
80 memset(&t, 0, sizeof(struct spi_transfer));
82 cmd_resp[0] = SST25L_CMD_RDSR;
86 t.len = sizeof(cmd_resp);
87 spi_message_add_tail(&t, &m);
88 err = spi_sync(flash->spi, &m);
92 *status = cmd_resp[1];
96 static int sst25l_write_enable(struct sst25l_flash *flash, int enable)
98 unsigned char command[2];
101 command[0] = enable ? SST25L_CMD_WREN : SST25L_CMD_WRDI;
102 err = spi_write(flash->spi, command, 1);
106 command[0] = SST25L_CMD_EWSR;
107 err = spi_write(flash->spi, command, 1);
111 command[0] = SST25L_CMD_WRSR;
112 command[1] = enable ? 0 : SST25L_STATUS_BP0 | SST25L_STATUS_BP1;
113 err = spi_write(flash->spi, command, 2);
118 err = sst25l_status(flash, &status);
121 if (!(status & SST25L_STATUS_WREN))
128 static int sst25l_wait_till_ready(struct sst25l_flash *flash)
130 unsigned long deadline;
133 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
135 err = sst25l_status(flash, &status);
138 if (!(status & SST25L_STATUS_BUSY))
142 } while (!time_after_eq(jiffies, deadline));
147 static int sst25l_erase_sector(struct sst25l_flash *flash, uint32_t offset)
149 unsigned char command[4];
152 err = sst25l_write_enable(flash, 1);
156 command[0] = SST25L_CMD_SECTOR_ERASE;
157 command[1] = offset >> 16;
158 command[2] = offset >> 8;
160 err = spi_write(flash->spi, command, 4);
164 err = sst25l_wait_till_ready(flash);
168 return sst25l_write_enable(flash, 0);
171 static int sst25l_erase(struct mtd_info *mtd, struct erase_info *instr)
173 struct sst25l_flash *flash = to_sst25l_flash(mtd);
178 if ((uint32_t)instr->len % mtd->erasesize)
181 if ((uint32_t)instr->addr % mtd->erasesize)
185 end = addr + instr->len;
187 mutex_lock(&flash->lock);
189 err = sst25l_wait_till_ready(flash);
191 mutex_unlock(&flash->lock);
196 err = sst25l_erase_sector(flash, addr);
198 mutex_unlock(&flash->lock);
199 instr->state = MTD_ERASE_FAILED;
200 dev_err(&flash->spi->dev, "Erase failed\n");
204 addr += mtd->erasesize;
207 mutex_unlock(&flash->lock);
209 instr->state = MTD_ERASE_DONE;
210 mtd_erase_callback(instr);
214 static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len,
215 size_t *retlen, unsigned char *buf)
217 struct sst25l_flash *flash = to_sst25l_flash(mtd);
218 struct spi_transfer transfer[2];
219 struct spi_message message;
220 unsigned char command[4];
223 spi_message_init(&message);
224 memset(&transfer, 0, sizeof(transfer));
226 command[0] = SST25L_CMD_READ;
227 command[1] = from >> 16;
228 command[2] = from >> 8;
231 transfer[0].tx_buf = command;
232 transfer[0].len = sizeof(command);
233 spi_message_add_tail(&transfer[0], &message);
235 transfer[1].rx_buf = buf;
236 transfer[1].len = len;
237 spi_message_add_tail(&transfer[1], &message);
239 mutex_lock(&flash->lock);
241 /* Wait for previous write/erase to complete */
242 ret = sst25l_wait_till_ready(flash);
244 mutex_unlock(&flash->lock);
248 spi_sync(flash->spi, &message);
250 if (retlen && message.actual_length > sizeof(command))
251 *retlen += message.actual_length - sizeof(command);
253 mutex_unlock(&flash->lock);
257 static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len,
258 size_t *retlen, const unsigned char *buf)
260 struct sst25l_flash *flash = to_sst25l_flash(mtd);
261 int i, j, ret, bytes, copied = 0;
262 unsigned char command[5];
264 if ((uint32_t)to % mtd->writesize)
267 mutex_lock(&flash->lock);
269 ret = sst25l_write_enable(flash, 1);
273 for (i = 0; i < len; i += mtd->writesize) {
274 ret = sst25l_wait_till_ready(flash);
278 /* Write the first byte of the page */
279 command[0] = SST25L_CMD_AAI_PROGRAM;
280 command[1] = (to + i) >> 16;
281 command[2] = (to + i) >> 8;
282 command[3] = (to + i);
284 ret = spi_write(flash->spi, command, 5);
290 * Write the remaining bytes using auto address
293 bytes = min_t(uint32_t, mtd->writesize, len - i);
294 for (j = 1; j < bytes; j++, copied++) {
295 ret = sst25l_wait_till_ready(flash);
299 command[1] = buf[i + j];
300 ret = spi_write(flash->spi, command, 2);
307 ret = sst25l_write_enable(flash, 0);
312 mutex_unlock(&flash->lock);
316 static struct flash_info *sst25l_match_device(struct spi_device *spi)
318 struct flash_info *flash_info = NULL;
319 struct spi_message m;
320 struct spi_transfer t;
321 unsigned char cmd_resp[6];
325 spi_message_init(&m);
326 memset(&t, 0, sizeof(struct spi_transfer));
328 cmd_resp[0] = SST25L_CMD_READ_ID;
336 t.len = sizeof(cmd_resp);
337 spi_message_add_tail(&t, &m);
338 err = spi_sync(spi, &m);
340 dev_err(&spi->dev, "error reading device id\n");
344 id = (cmd_resp[4] << 8) | cmd_resp[5];
346 for (i = 0; i < ARRAY_SIZE(sst25l_flash_info); i++)
347 if (sst25l_flash_info[i].device_id == id)
348 flash_info = &sst25l_flash_info[i];
351 dev_err(&spi->dev, "unknown id %.4x\n", id);
356 static int sst25l_probe(struct spi_device *spi)
358 struct flash_info *flash_info;
359 struct sst25l_flash *flash;
360 struct flash_platform_data *data;
363 flash_info = sst25l_match_device(spi);
367 flash = kzalloc(sizeof(struct sst25l_flash), GFP_KERNEL);
372 mutex_init(&flash->lock);
373 dev_set_drvdata(&spi->dev, flash);
375 data = spi->dev.platform_data;
376 if (data && data->name)
377 flash->mtd.name = data->name;
379 flash->mtd.name = dev_name(&spi->dev);
381 flash->mtd.type = MTD_NORFLASH;
382 flash->mtd.flags = MTD_CAP_NORFLASH;
383 flash->mtd.erasesize = flash_info->erase_size;
384 flash->mtd.writesize = flash_info->page_size;
385 flash->mtd.writebufsize = flash_info->page_size;
386 flash->mtd.size = flash_info->page_size * flash_info->nr_pages;
387 flash->mtd._erase = sst25l_erase;
388 flash->mtd._read = sst25l_read;
389 flash->mtd._write = sst25l_write;
391 dev_info(&spi->dev, "%s (%lld KiB)\n", flash_info->name,
392 (long long)flash->mtd.size >> 10);
394 pr_debug("mtd .name = %s, .size = 0x%llx (%lldMiB) "
395 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
397 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
398 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
399 flash->mtd.numeraseregions);
402 ret = mtd_device_parse_register(&flash->mtd, NULL, NULL,
403 data ? data->parts : NULL,
404 data ? data->nr_parts : 0);
407 dev_set_drvdata(&spi->dev, NULL);
414 static int sst25l_remove(struct spi_device *spi)
416 struct sst25l_flash *flash = dev_get_drvdata(&spi->dev);
419 ret = mtd_device_unregister(&flash->mtd);
425 static struct spi_driver sst25l_driver = {
428 .owner = THIS_MODULE,
430 .probe = sst25l_probe,
431 .remove = sst25l_remove,
434 module_spi_driver(sst25l_driver);
436 MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips");
437 MODULE_AUTHOR("Andre Renaud <andre@bluewatersys.com>, "
439 MODULE_LICENSE("GPL");