blob: 10d24efb46291e092ac96c9c62ccd0db322c5aac [file] [log] [blame]
Ryan Mallonec77e212009-09-18 12:51:40 -07001/*
2 * sst25l.c
3 *
4 * Driver for SST25L SPI Flash chips
5 *
6 * Copyright © 2009 Bluewater Systems Ltd
7 * Author: Andre Renaud <andre@bluewatersys.com>
Ryan Mallon1c5454e2011-06-15 14:45:36 +10008 * Author: Ryan Mallon
Ryan Mallonec77e212009-09-18 12:51:40 -07009 *
10 * Based on m25p80.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
Ryan Mallonec77e212009-09-18 12:51:40 -070018#include <linux/module.h>
19#include <linux/device.h>
20#include <linux/mutex.h>
21#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040023#include <linux/sched.h>
Ryan Mallonec77e212009-09-18 12:51:40 -070024
25#include <linux/mtd/mtd.h>
26#include <linux/mtd/partitions.h>
27
28#include <linux/spi/spi.h>
29#include <linux/spi/flash.h>
30
31/* Erases can take up to 3 seconds! */
32#define MAX_READY_WAIT_JIFFIES msecs_to_jiffies(3000)
33
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 */
39
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 */
44
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 */
49
50struct sst25l_flash {
51 struct spi_device *spi;
52 struct mutex lock;
53 struct mtd_info mtd;
Ryan Mallonec77e212009-09-18 12:51:40 -070054};
55
56struct flash_info {
57 const char *name;
58 uint16_t device_id;
59 unsigned page_size;
60 unsigned nr_pages;
61 unsigned erase_size;
62};
63
64#define to_sst25l_flash(x) container_of(x, struct sst25l_flash, mtd)
65
Bill Pemberton042a1902012-11-19 13:24:23 -050066static struct flash_info sst25l_flash_info[] = {
Ryan Mallonec77e212009-09-18 12:51:40 -070067 {"sst25lf020a", 0xbf43, 256, 1024, 4096},
68 {"sst25lf040a", 0xbf44, 256, 2048, 4096},
69};
70
71static int sst25l_status(struct sst25l_flash *flash, int *status)
72{
H Hartley Sweeten0ffe0ce2010-04-29 13:34:24 -050073 struct spi_message m;
74 struct spi_transfer t;
75 unsigned char cmd_resp[2];
Ryan Mallonec77e212009-09-18 12:51:40 -070076 int err;
77
H Hartley Sweeten0ffe0ce2010-04-29 13:34:24 -050078 spi_message_init(&m);
79 memset(&t, 0, sizeof(struct spi_transfer));
80
81 cmd_resp[0] = SST25L_CMD_RDSR;
82 cmd_resp[1] = 0xff;
83 t.tx_buf = cmd_resp;
84 t.rx_buf = cmd_resp;
85 t.len = sizeof(cmd_resp);
86 spi_message_add_tail(&t, &m);
87 err = spi_sync(flash->spi, &m);
Ryan Mallonec77e212009-09-18 12:51:40 -070088 if (err < 0)
89 return err;
90
H Hartley Sweeten0ffe0ce2010-04-29 13:34:24 -050091 *status = cmd_resp[1];
Ryan Mallonec77e212009-09-18 12:51:40 -070092 return 0;
93}
94
95static int sst25l_write_enable(struct sst25l_flash *flash, int enable)
96{
97 unsigned char command[2];
98 int status, err;
99
100 command[0] = enable ? SST25L_CMD_WREN : SST25L_CMD_WRDI;
101 err = spi_write(flash->spi, command, 1);
102 if (err)
103 return err;
104
105 command[0] = SST25L_CMD_EWSR;
106 err = spi_write(flash->spi, command, 1);
107 if (err)
108 return err;
109
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);
113 if (err)
114 return err;
115
116 if (enable) {
117 err = sst25l_status(flash, &status);
118 if (err)
119 return err;
120 if (!(status & SST25L_STATUS_WREN))
121 return -EROFS;
122 }
123
124 return 0;
125}
126
127static int sst25l_wait_till_ready(struct sst25l_flash *flash)
128{
129 unsigned long deadline;
130 int status, err;
131
132 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
133 do {
134 err = sst25l_status(flash, &status);
135 if (err)
136 return err;
137 if (!(status & SST25L_STATUS_BUSY))
138 return 0;
139
140 cond_resched();
141 } while (!time_after_eq(jiffies, deadline));
142
143 return -ETIMEDOUT;
144}
145
146static int sst25l_erase_sector(struct sst25l_flash *flash, uint32_t offset)
147{
148 unsigned char command[4];
149 int err;
150
151 err = sst25l_write_enable(flash, 1);
152 if (err)
153 return err;
154
155 command[0] = SST25L_CMD_SECTOR_ERASE;
156 command[1] = offset >> 16;
157 command[2] = offset >> 8;
158 command[3] = offset;
159 err = spi_write(flash->spi, command, 4);
160 if (err)
161 return err;
162
163 err = sst25l_wait_till_ready(flash);
164 if (err)
165 return err;
166
167 return sst25l_write_enable(flash, 0);
168}
169
170static int sst25l_erase(struct mtd_info *mtd, struct erase_info *instr)
171{
172 struct sst25l_flash *flash = to_sst25l_flash(mtd);
173 uint32_t addr, end;
174 int err;
175
176 /* Sanity checks */
Ryan Mallonec77e212009-09-18 12:51:40 -0700177 if ((uint32_t)instr->len % mtd->erasesize)
178 return -EINVAL;
179
180 if ((uint32_t)instr->addr % mtd->erasesize)
181 return -EINVAL;
182
183 addr = instr->addr;
184 end = addr + instr->len;
185
186 mutex_lock(&flash->lock);
187
188 err = sst25l_wait_till_ready(flash);
Jiri Slaby2eaaa5f2009-09-18 12:51:42 -0700189 if (err) {
190 mutex_unlock(&flash->lock);
Ryan Mallonec77e212009-09-18 12:51:40 -0700191 return err;
Jiri Slaby2eaaa5f2009-09-18 12:51:42 -0700192 }
Ryan Mallonec77e212009-09-18 12:51:40 -0700193
194 while (addr < end) {
195 err = sst25l_erase_sector(flash, addr);
196 if (err) {
197 mutex_unlock(&flash->lock);
Ryan Mallonec77e212009-09-18 12:51:40 -0700198 dev_err(&flash->spi->dev, "Erase failed\n");
199 return err;
200 }
201
202 addr += mtd->erasesize;
203 }
204
205 mutex_unlock(&flash->lock);
206
Ryan Mallonec77e212009-09-18 12:51:40 -0700207 return 0;
208}
209
210static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len,
211 size_t *retlen, unsigned char *buf)
212{
213 struct sst25l_flash *flash = to_sst25l_flash(mtd);
214 struct spi_transfer transfer[2];
215 struct spi_message message;
216 unsigned char command[4];
217 int ret;
218
Ryan Mallonec77e212009-09-18 12:51:40 -0700219 spi_message_init(&message);
220 memset(&transfer, 0, sizeof(transfer));
221
222 command[0] = SST25L_CMD_READ;
223 command[1] = from >> 16;
224 command[2] = from >> 8;
225 command[3] = from;
226
227 transfer[0].tx_buf = command;
228 transfer[0].len = sizeof(command);
229 spi_message_add_tail(&transfer[0], &message);
230
231 transfer[1].rx_buf = buf;
232 transfer[1].len = len;
233 spi_message_add_tail(&transfer[1], &message);
234
235 mutex_lock(&flash->lock);
236
237 /* Wait for previous write/erase to complete */
238 ret = sst25l_wait_till_ready(flash);
239 if (ret) {
240 mutex_unlock(&flash->lock);
241 return ret;
242 }
243
244 spi_sync(flash->spi, &message);
245
246 if (retlen && message.actual_length > sizeof(command))
247 *retlen += message.actual_length - sizeof(command);
248
249 mutex_unlock(&flash->lock);
250 return 0;
251}
252
253static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len,
254 size_t *retlen, const unsigned char *buf)
255{
256 struct sst25l_flash *flash = to_sst25l_flash(mtd);
257 int i, j, ret, bytes, copied = 0;
258 unsigned char command[5];
259
Ryan Mallonec77e212009-09-18 12:51:40 -0700260 if ((uint32_t)to % mtd->writesize)
261 return -EINVAL;
262
263 mutex_lock(&flash->lock);
264
265 ret = sst25l_write_enable(flash, 1);
266 if (ret)
267 goto out;
268
269 for (i = 0; i < len; i += mtd->writesize) {
270 ret = sst25l_wait_till_ready(flash);
271 if (ret)
272 goto out;
273
274 /* Write the first byte of the page */
275 command[0] = SST25L_CMD_AAI_PROGRAM;
276 command[1] = (to + i) >> 16;
277 command[2] = (to + i) >> 8;
278 command[3] = (to + i);
279 command[4] = buf[i];
280 ret = spi_write(flash->spi, command, 5);
281 if (ret < 0)
282 goto out;
283 copied++;
284
285 /*
286 * Write the remaining bytes using auto address
287 * increment mode
288 */
289 bytes = min_t(uint32_t, mtd->writesize, len - i);
290 for (j = 1; j < bytes; j++, copied++) {
291 ret = sst25l_wait_till_ready(flash);
292 if (ret)
293 goto out;
294
295 command[1] = buf[i + j];
296 ret = spi_write(flash->spi, command, 2);
297 if (ret)
298 goto out;
299 }
300 }
301
302out:
303 ret = sst25l_write_enable(flash, 0);
304
305 if (retlen)
306 *retlen = copied;
307
308 mutex_unlock(&flash->lock);
309 return ret;
310}
311
Bill Pemberton06f25512012-11-19 13:23:07 -0500312static struct flash_info *sst25l_match_device(struct spi_device *spi)
Ryan Mallonec77e212009-09-18 12:51:40 -0700313{
314 struct flash_info *flash_info = NULL;
H Hartley Sweeten0ffe0ce2010-04-29 13:34:24 -0500315 struct spi_message m;
316 struct spi_transfer t;
317 unsigned char cmd_resp[6];
Ryan Mallonec77e212009-09-18 12:51:40 -0700318 int i, err;
319 uint16_t id;
320
H Hartley Sweeten0ffe0ce2010-04-29 13:34:24 -0500321 spi_message_init(&m);
322 memset(&t, 0, sizeof(struct spi_transfer));
323
324 cmd_resp[0] = SST25L_CMD_READ_ID;
325 cmd_resp[1] = 0;
326 cmd_resp[2] = 0;
327 cmd_resp[3] = 0;
328 cmd_resp[4] = 0xff;
329 cmd_resp[5] = 0xff;
330 t.tx_buf = cmd_resp;
331 t.rx_buf = cmd_resp;
332 t.len = sizeof(cmd_resp);
333 spi_message_add_tail(&t, &m);
334 err = spi_sync(spi, &m);
Ryan Mallonec77e212009-09-18 12:51:40 -0700335 if (err < 0) {
H Hartley Sweeten0ffe0ce2010-04-29 13:34:24 -0500336 dev_err(&spi->dev, "error reading device id\n");
Ryan Mallonec77e212009-09-18 12:51:40 -0700337 return NULL;
338 }
339
H Hartley Sweeten0ffe0ce2010-04-29 13:34:24 -0500340 id = (cmd_resp[4] << 8) | cmd_resp[5];
Ryan Mallonec77e212009-09-18 12:51:40 -0700341
342 for (i = 0; i < ARRAY_SIZE(sst25l_flash_info); i++)
343 if (sst25l_flash_info[i].device_id == id)
344 flash_info = &sst25l_flash_info[i];
345
346 if (!flash_info)
347 dev_err(&spi->dev, "unknown id %.4x\n", id);
348
349 return flash_info;
350}
351
Bill Pemberton06f25512012-11-19 13:23:07 -0500352static int sst25l_probe(struct spi_device *spi)
Ryan Mallonec77e212009-09-18 12:51:40 -0700353{
354 struct flash_info *flash_info;
355 struct sst25l_flash *flash;
356 struct flash_platform_data *data;
Artem Bityutskiyd81a32f2011-12-29 18:06:01 +0200357 int ret;
Ryan Mallonec77e212009-09-18 12:51:40 -0700358
359 flash_info = sst25l_match_device(spi);
360 if (!flash_info)
361 return -ENODEV;
362
Sachin Kamatb38be282013-10-08 14:51:34 +0530363 flash = devm_kzalloc(&spi->dev, sizeof(*flash), GFP_KERNEL);
Ryan Mallonec77e212009-09-18 12:51:40 -0700364 if (!flash)
365 return -ENOMEM;
366
367 flash->spi = spi;
368 mutex_init(&flash->lock);
Jingoo Han15949082013-04-06 15:40:30 +0900369 spi_set_drvdata(spi, flash);
Ryan Mallonec77e212009-09-18 12:51:40 -0700370
Jingoo Han0278fd32013-07-30 17:17:44 +0900371 data = dev_get_platdata(&spi->dev);
Ryan Mallonec77e212009-09-18 12:51:40 -0700372 if (data && data->name)
373 flash->mtd.name = data->name;
Ryan Mallonec77e212009-09-18 12:51:40 -0700374
Frans Klaver90b99752015-06-10 22:38:22 +0200375 flash->mtd.dev.parent = &spi->dev;
Ryan Mallonec77e212009-09-18 12:51:40 -0700376 flash->mtd.type = MTD_NORFLASH;
377 flash->mtd.flags = MTD_CAP_NORFLASH;
378 flash->mtd.erasesize = flash_info->erase_size;
379 flash->mtd.writesize = flash_info->page_size;
Artem Bityutskiyc4cc6252012-02-03 10:16:50 +0200380 flash->mtd.writebufsize = flash_info->page_size;
Ryan Mallonec77e212009-09-18 12:51:40 -0700381 flash->mtd.size = flash_info->page_size * flash_info->nr_pages;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200382 flash->mtd._erase = sst25l_erase;
383 flash->mtd._read = sst25l_read;
384 flash->mtd._write = sst25l_write;
Ryan Mallonec77e212009-09-18 12:51:40 -0700385
386 dev_info(&spi->dev, "%s (%lld KiB)\n", flash_info->name,
387 (long long)flash->mtd.size >> 10);
388
Brian Norris289c0522011-07-19 10:06:09 -0700389 pr_debug("mtd .name = %s, .size = 0x%llx (%lldMiB) "
Ryan Mallonec77e212009-09-18 12:51:40 -0700390 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
391 flash->mtd.name,
392 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
393 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
394 flash->mtd.numeraseregions);
395
Ryan Mallonec77e212009-09-18 12:51:40 -0700396
Rafał Miłecki39675ca2018-07-13 11:27:34 +0200397 ret = mtd_device_register(&flash->mtd, data ? data->parts : NULL,
398 data ? data->nr_parts : 0);
Sachin Kamatb38be282013-10-08 14:51:34 +0530399 if (ret)
Ryan Mallonec77e212009-09-18 12:51:40 -0700400 return -ENODEV;
Ryan Mallonec77e212009-09-18 12:51:40 -0700401
402 return 0;
403}
404
Bill Pemberton810b7e02012-11-19 13:26:04 -0500405static int sst25l_remove(struct spi_device *spi)
Ryan Mallonec77e212009-09-18 12:51:40 -0700406{
Jingoo Han15949082013-04-06 15:40:30 +0900407 struct sst25l_flash *flash = spi_get_drvdata(spi);
Ryan Mallonec77e212009-09-18 12:51:40 -0700408
Sachin Kamatb38be282013-10-08 14:51:34 +0530409 return mtd_device_unregister(&flash->mtd);
Ryan Mallonec77e212009-09-18 12:51:40 -0700410}
411
412static struct spi_driver sst25l_driver = {
413 .driver = {
414 .name = "sst25l",
Ryan Mallonec77e212009-09-18 12:51:40 -0700415 },
416 .probe = sst25l_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500417 .remove = sst25l_remove,
Ryan Mallonec77e212009-09-18 12:51:40 -0700418};
419
Axel Linc9d1b752012-01-27 15:45:20 +0800420module_spi_driver(sst25l_driver);
Ryan Mallonec77e212009-09-18 12:51:40 -0700421
422MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips");
423MODULE_AUTHOR("Andre Renaud <andre@bluewatersys.com>, "
Ryan Mallon1c5454e2011-06-15 14:45:36 +1000424 "Ryan Mallon");
Ryan Mallonec77e212009-09-18 12:51:40 -0700425MODULE_LICENSE("GPL");