19 #include <linux/module.h>
20 #include <linux/device.h>
23 #include <linux/slab.h>
24 #include <linux/sched.h>
33 #define MAX_READY_WAIT_JIFFIES msecs_to_jiffies(3000)
35 #define SST25L_CMD_WRSR 0x01
36 #define SST25L_CMD_WRDI 0x04
37 #define SST25L_CMD_RDSR 0x05
38 #define SST25L_CMD_WREN 0x06
39 #define SST25L_CMD_READ 0x03
41 #define SST25L_CMD_EWSR 0x50
42 #define SST25L_CMD_SECTOR_ERASE 0x20
43 #define SST25L_CMD_READ_ID 0x90
44 #define SST25L_CMD_AAI_PROGRAM 0xaf
46 #define SST25L_STATUS_BUSY (1 << 0)
47 #define SST25L_STATUS_WREN (1 << 1)
48 #define SST25L_STATUS_BP0 (1 << 2)
49 #define SST25L_STATUS_BP1 (1 << 3)
65 #define to_sst25l_flash(x) container_of(x, struct sst25l_flash, mtd)
68 {
"sst25lf020a", 0xbf43, 256, 1024, 4096},
69 {
"sst25lf040a", 0xbf44, 256, 2048, 4096},
76 unsigned char cmd_resp[2];
86 t.len =
sizeof(cmd_resp);
87 spi_message_add_tail(&
t, &
m);
92 *status = cmd_resp[1];
102 err = spi_write(flash->
spi, command, 1);
107 err = spi_write(flash->
spi, command, 1);
113 err = spi_write(flash->
spi, command, 2);
118 err = sst25l_status(flash, &status);
128 static int sst25l_wait_till_ready(
struct sst25l_flash *flash)
130 unsigned long deadline;
135 err = sst25l_status(flash, &status);
149 unsigned char command[4];
152 err = sst25l_write_enable(flash, 1);
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);
185 end = addr + instr->
len;
189 err = sst25l_wait_till_ready(flash);
196 err = sst25l_erase_sector(flash, addr);
214 static int sst25l_read(
struct mtd_info *mtd, loff_t
from,
size_t len,
215 size_t *retlen,
unsigned char *
buf)
220 unsigned char command[4];
224 memset(&transfer, 0,
sizeof(transfer));
227 command[1] = from >> 16;
228 command[2] = from >> 8;
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);
242 ret = sst25l_wait_till_ready(flash);
250 if (retlen &&
message.actual_length >
sizeof(command))
257 static int sst25l_write(
struct mtd_info *mtd, loff_t to,
size_t len,
258 size_t *retlen,
const unsigned char *buf)
262 unsigned char command[5];
269 ret = sst25l_write_enable(flash, 1);
273 for (i = 0; i < len; i += mtd->
writesize) {
274 ret = sst25l_wait_till_ready(flash);
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);
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);
321 unsigned char cmd_resp[6];
325 spi_message_init(&
m);
336 t.len =
sizeof(cmd_resp);
337 spi_message_add_tail(&
t, &
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];
358 struct flash_info *flash_info;
363 flash_info = sst25l_match_device(spi);
375 data = spi->
dev.platform_data;
376 if (data && data->
name)
379 flash->
mtd.name = dev_name(&spi->
dev);
387 flash->
mtd._erase = sst25l_erase;
388 flash->
mtd._read = sst25l_read;
389 flash->
mtd._write = sst25l_write;
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);
430 .probe = sst25l_probe,