4 * Driver for SST25L SPI Flash chips
6 * Copyright © 2009 Bluewater Systems Ltd
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/module.h>
19 #include <linux/device.h>
20 #include <linux/mutex.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/partitions.h>
28 #include <linux/spi/spi.h>
29 #include <linux/spi/flash.h>
31 /* Erases can take up to 3 seconds! */
32 #define MAX_READY_WAIT_JIFFIES msecs_to_jiffies(3000)
34 #define SST25L_CMD_WRSR 0x01 /* Write status register */
35 #define SST25L_CMD_WRDI 0x04 /* Write disable */
36 #define SST25L_CMD_RDSR 0x05 /* Read status register */
37 #define SST25L_CMD_WREN 0x06 /* Write enable */
38 #define SST25L_CMD_READ 0x03 /* High speed read */
40 #define SST25L_CMD_EWSR 0x50 /* Enable write status register */
41 #define SST25L_CMD_SECTOR_ERASE 0x20 /* Erase sector */
42 #define SST25L_CMD_READ_ID 0x90 /* Read device ID */
43 #define SST25L_CMD_AAI_PROGRAM 0xaf /* Auto address increment */
45 #define SST25L_STATUS_BUSY (1 << 0) /* Chip is busy */
46 #define SST25L_STATUS_WREN (1 << 1) /* Write enabled */
47 #define SST25L_STATUS_BP0 (1 << 2) /* Block protection 0 */
48 #define SST25L_STATUS_BP1 (1 << 3) /* Block protection 1 */
51 struct spi_device *spi;
64 #define to_sst25l_flash(x) container_of(x, struct sst25l_flash, mtd)
66 static struct flash_info sst25l_flash_info[] = {
67 {"sst25lf020a", 0xbf43, 256, 1024, 4096},
68 {"sst25lf040a", 0xbf44, 256, 2048, 4096},
71 static int sst25l_status(struct sst25l_flash *flash, int *status)
74 struct spi_transfer t;
75 unsigned char cmd_resp[2];
79 memset(&t, 0, sizeof(struct spi_transfer));
81 cmd_resp[0] = SST25L_CMD_RDSR;
85 t.len = sizeof(cmd_resp);
86 spi_message_add_tail(&t, &m);
87 err = spi_sync(flash->spi, &m);
91 *status = cmd_resp[1];
95 static int sst25l_write_enable(struct sst25l_flash *flash, int enable)
97 unsigned char command[2];
100 command[0] = enable ? SST25L_CMD_WREN : SST25L_CMD_WRDI;
101 err = spi_write(flash->spi, command, 1);
105 command[0] = SST25L_CMD_EWSR;
106 err = spi_write(flash->spi, command, 1);
110 command[0] = SST25L_CMD_WRSR;
111 command[1] = enable ? 0 : SST25L_STATUS_BP0 | SST25L_STATUS_BP1;
112 err = spi_write(flash->spi, command, 2);
117 err = sst25l_status(flash, &status);
120 if (!(status & SST25L_STATUS_WREN))
127 static int sst25l_wait_till_ready(struct sst25l_flash *flash)
129 unsigned long deadline;
132 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
134 err = sst25l_status(flash, &status);
137 if (!(status & SST25L_STATUS_BUSY))
141 } while (!time_after_eq(jiffies, deadline));
146 static int sst25l_erase_sector(struct sst25l_flash *flash, uint32_t offset)
148 unsigned char command[4];
151 err = sst25l_write_enable(flash, 1);
155 command[0] = SST25L_CMD_SECTOR_ERASE;
156 command[1] = offset >> 16;
157 command[2] = offset >> 8;
159 err = spi_write(flash->spi, command, 4);
163 err = sst25l_wait_till_ready(flash);
167 return sst25l_write_enable(flash, 0);
170 static int sst25l_erase(struct mtd_info *mtd, struct erase_info *instr)
172 struct sst25l_flash *flash = to_sst25l_flash(mtd);
177 if ((uint32_t)instr->len % mtd->erasesize)
180 if ((uint32_t)instr->addr % mtd->erasesize)
184 end = addr + instr->len;
186 mutex_lock(&flash->lock);
188 err = sst25l_wait_till_ready(flash);
190 mutex_unlock(&flash->lock);
195 err = sst25l_erase_sector(flash, addr);
197 mutex_unlock(&flash->lock);
198 instr->state = MTD_ERASE_FAILED;
199 dev_err(&flash->spi->dev, "Erase failed\n");
203 addr += mtd->erasesize;
206 mutex_unlock(&flash->lock);
208 instr->state = MTD_ERASE_DONE;
209 mtd_erase_callback(instr);
213 static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len,
214 size_t *retlen, unsigned char *buf)
216 struct sst25l_flash *flash = to_sst25l_flash(mtd);
217 struct spi_transfer transfer[2];
218 struct spi_message message;
219 unsigned char command[4];
222 spi_message_init(&message);
223 memset(&transfer, 0, sizeof(transfer));
225 command[0] = SST25L_CMD_READ;
226 command[1] = from >> 16;
227 command[2] = from >> 8;
230 transfer[0].tx_buf = command;
231 transfer[0].len = sizeof(command);
232 spi_message_add_tail(&transfer[0], &message);
234 transfer[1].rx_buf = buf;
235 transfer[1].len = len;
236 spi_message_add_tail(&transfer[1], &message);
238 mutex_lock(&flash->lock);
240 /* Wait for previous write/erase to complete */
241 ret = sst25l_wait_till_ready(flash);
243 mutex_unlock(&flash->lock);
247 spi_sync(flash->spi, &message);
249 if (retlen && message.actual_length > sizeof(command))
250 *retlen += message.actual_length - sizeof(command);
252 mutex_unlock(&flash->lock);
256 static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len,
257 size_t *retlen, const unsigned char *buf)
259 struct sst25l_flash *flash = to_sst25l_flash(mtd);
260 int i, j, ret, bytes, copied = 0;
261 unsigned char command[5];
263 if ((uint32_t)to % mtd->writesize)
266 mutex_lock(&flash->lock);
268 ret = sst25l_write_enable(flash, 1);
272 for (i = 0; i < len; i += mtd->writesize) {
273 ret = sst25l_wait_till_ready(flash);
277 /* Write the first byte of the page */
278 command[0] = SST25L_CMD_AAI_PROGRAM;
279 command[1] = (to + i) >> 16;
280 command[2] = (to + i) >> 8;
281 command[3] = (to + i);
283 ret = spi_write(flash->spi, command, 5);
289 * Write the remaining bytes using auto address
292 bytes = min_t(uint32_t, mtd->writesize, len - i);
293 for (j = 1; j < bytes; j++, copied++) {
294 ret = sst25l_wait_till_ready(flash);
298 command[1] = buf[i + j];
299 ret = spi_write(flash->spi, command, 2);
306 ret = sst25l_write_enable(flash, 0);
311 mutex_unlock(&flash->lock);
315 static struct flash_info *sst25l_match_device(struct spi_device *spi)
317 struct flash_info *flash_info = NULL;
318 struct spi_message m;
319 struct spi_transfer t;
320 unsigned char cmd_resp[6];
324 spi_message_init(&m);
325 memset(&t, 0, sizeof(struct spi_transfer));
327 cmd_resp[0] = SST25L_CMD_READ_ID;
335 t.len = sizeof(cmd_resp);
336 spi_message_add_tail(&t, &m);
337 err = spi_sync(spi, &m);
339 dev_err(&spi->dev, "error reading device id\n");
343 id = (cmd_resp[4] << 8) | cmd_resp[5];
345 for (i = 0; i < ARRAY_SIZE(sst25l_flash_info); i++)
346 if (sst25l_flash_info[i].device_id == id)
347 flash_info = &sst25l_flash_info[i];
350 dev_err(&spi->dev, "unknown id %.4x\n", id);
355 static int sst25l_probe(struct spi_device *spi)
357 struct flash_info *flash_info;
358 struct sst25l_flash *flash;
359 struct flash_platform_data *data;
362 flash_info = sst25l_match_device(spi);
366 flash = devm_kzalloc(&spi->dev, sizeof(*flash), GFP_KERNEL);
371 mutex_init(&flash->lock);
372 spi_set_drvdata(spi, flash);
374 data = dev_get_platdata(&spi->dev);
375 if (data && data->name)
376 flash->mtd.name = data->name;
378 flash->mtd.name = dev_name(&spi->dev);
380 flash->mtd.type = MTD_NORFLASH;
381 flash->mtd.flags = MTD_CAP_NORFLASH;
382 flash->mtd.erasesize = flash_info->erase_size;
383 flash->mtd.writesize = flash_info->page_size;
384 flash->mtd.writebufsize = flash_info->page_size;
385 flash->mtd.size = flash_info->page_size * flash_info->nr_pages;
386 flash->mtd._erase = sst25l_erase;
387 flash->mtd._read = sst25l_read;
388 flash->mtd._write = sst25l_write;
390 dev_info(&spi->dev, "%s (%lld KiB)\n", flash_info->name,
391 (long long)flash->mtd.size >> 10);
393 pr_debug("mtd .name = %s, .size = 0x%llx (%lldMiB) "
394 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
396 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
397 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
398 flash->mtd.numeraseregions);
401 ret = mtd_device_parse_register(&flash->mtd, NULL, NULL,
402 data ? data->parts : NULL,
403 data ? data->nr_parts : 0);
410 static int sst25l_remove(struct spi_device *spi)
412 struct sst25l_flash *flash = spi_get_drvdata(spi);
414 return mtd_device_unregister(&flash->mtd);
417 static struct spi_driver sst25l_driver = {
420 .owner = THIS_MODULE,
422 .probe = sst25l_probe,
423 .remove = sst25l_remove,
426 module_spi_driver(sst25l_driver);
428 MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips");
431 MODULE_LICENSE("GPL");