1 // SPDX-License-Identifier: GPL-2.0-only
5 * Driver for SST25L SPI Flash chips
7 * Copyright © 2009 Bluewater Systems Ltd
8 * Author: Andre Renaud <andre@bluewatersys.com>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/mutex.h>
17 #include <linux/interrupt.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/partitions.h>
24 #include <linux/spi/spi.h>
25 #include <linux/spi/flash.h>
27 /* Erases can take up to 3 seconds! */
28 #define MAX_READY_WAIT_JIFFIES msecs_to_jiffies(3000)
30 #define SST25L_CMD_WRSR 0x01 /* Write status register */
31 #define SST25L_CMD_WRDI 0x04 /* Write disable */
32 #define SST25L_CMD_RDSR 0x05 /* Read status register */
33 #define SST25L_CMD_WREN 0x06 /* Write enable */
34 #define SST25L_CMD_READ 0x03 /* High speed read */
36 #define SST25L_CMD_EWSR 0x50 /* Enable write status register */
37 #define SST25L_CMD_SECTOR_ERASE 0x20 /* Erase sector */
38 #define SST25L_CMD_READ_ID 0x90 /* Read device ID */
39 #define SST25L_CMD_AAI_PROGRAM 0xaf /* Auto address increment */
41 #define SST25L_STATUS_BUSY (1 << 0) /* Chip is busy */
42 #define SST25L_STATUS_WREN (1 << 1) /* Write enabled */
43 #define SST25L_STATUS_BP0 (1 << 2) /* Block protection 0 */
44 #define SST25L_STATUS_BP1 (1 << 3) /* Block protection 1 */
47 struct spi_device
*spi
;
60 #define to_sst25l_flash(x) container_of(x, struct sst25l_flash, mtd)
62 static struct flash_info sst25l_flash_info
[] = {
63 {"sst25lf020a", 0xbf43, 256, 1024, 4096},
64 {"sst25lf040a", 0xbf44, 256, 2048, 4096},
67 static int sst25l_status(struct sst25l_flash
*flash
, int *status
)
70 struct spi_transfer t
;
71 unsigned char cmd_resp
[2];
75 memset(&t
, 0, sizeof(struct spi_transfer
));
77 cmd_resp
[0] = SST25L_CMD_RDSR
;
81 t
.len
= sizeof(cmd_resp
);
82 spi_message_add_tail(&t
, &m
);
83 err
= spi_sync(flash
->spi
, &m
);
87 *status
= cmd_resp
[1];
91 static int sst25l_write_enable(struct sst25l_flash
*flash
, int enable
)
93 unsigned char command
[2];
96 command
[0] = enable
? SST25L_CMD_WREN
: SST25L_CMD_WRDI
;
97 err
= spi_write(flash
->spi
, command
, 1);
101 command
[0] = SST25L_CMD_EWSR
;
102 err
= spi_write(flash
->spi
, command
, 1);
106 command
[0] = SST25L_CMD_WRSR
;
107 command
[1] = enable
? 0 : SST25L_STATUS_BP0
| SST25L_STATUS_BP1
;
108 err
= spi_write(flash
->spi
, command
, 2);
113 err
= sst25l_status(flash
, &status
);
116 if (!(status
& SST25L_STATUS_WREN
))
123 static int sst25l_wait_till_ready(struct sst25l_flash
*flash
)
125 unsigned long deadline
;
128 deadline
= jiffies
+ MAX_READY_WAIT_JIFFIES
;
130 err
= sst25l_status(flash
, &status
);
133 if (!(status
& SST25L_STATUS_BUSY
))
137 } while (!time_after_eq(jiffies
, deadline
));
142 static int sst25l_erase_sector(struct sst25l_flash
*flash
, uint32_t offset
)
144 unsigned char command
[4];
147 err
= sst25l_write_enable(flash
, 1);
151 command
[0] = SST25L_CMD_SECTOR_ERASE
;
152 command
[1] = offset
>> 16;
153 command
[2] = offset
>> 8;
155 err
= spi_write(flash
->spi
, command
, 4);
159 err
= sst25l_wait_till_ready(flash
);
163 return sst25l_write_enable(flash
, 0);
166 static int sst25l_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
168 struct sst25l_flash
*flash
= to_sst25l_flash(mtd
);
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 dev_err(&flash
->spi
->dev
, "Erase failed\n");
198 addr
+= mtd
->erasesize
;
201 mutex_unlock(&flash
->lock
);
206 static int sst25l_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
207 size_t *retlen
, unsigned char *buf
)
209 struct sst25l_flash
*flash
= to_sst25l_flash(mtd
);
210 struct spi_transfer transfer
[2];
211 struct spi_message message
;
212 unsigned char command
[4];
215 spi_message_init(&message
);
216 memset(&transfer
, 0, sizeof(transfer
));
218 command
[0] = SST25L_CMD_READ
;
219 command
[1] = from
>> 16;
220 command
[2] = from
>> 8;
223 transfer
[0].tx_buf
= command
;
224 transfer
[0].len
= sizeof(command
);
225 spi_message_add_tail(&transfer
[0], &message
);
227 transfer
[1].rx_buf
= buf
;
228 transfer
[1].len
= len
;
229 spi_message_add_tail(&transfer
[1], &message
);
231 mutex_lock(&flash
->lock
);
233 /* Wait for previous write/erase to complete */
234 ret
= sst25l_wait_till_ready(flash
);
236 mutex_unlock(&flash
->lock
);
240 spi_sync(flash
->spi
, &message
);
242 if (retlen
&& message
.actual_length
> sizeof(command
))
243 *retlen
+= message
.actual_length
- sizeof(command
);
245 mutex_unlock(&flash
->lock
);
249 static int sst25l_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
250 size_t *retlen
, const unsigned char *buf
)
252 struct sst25l_flash
*flash
= to_sst25l_flash(mtd
);
253 int i
, j
, ret
, bytes
, copied
= 0;
254 unsigned char command
[5];
256 if ((uint32_t)to
% mtd
->writesize
)
259 mutex_lock(&flash
->lock
);
261 ret
= sst25l_write_enable(flash
, 1);
265 for (i
= 0; i
< len
; i
+= mtd
->writesize
) {
266 ret
= sst25l_wait_till_ready(flash
);
270 /* Write the first byte of the page */
271 command
[0] = SST25L_CMD_AAI_PROGRAM
;
272 command
[1] = (to
+ i
) >> 16;
273 command
[2] = (to
+ i
) >> 8;
274 command
[3] = (to
+ i
);
276 ret
= spi_write(flash
->spi
, command
, 5);
282 * Write the remaining bytes using auto address
285 bytes
= min_t(uint32_t, mtd
->writesize
, len
- i
);
286 for (j
= 1; j
< bytes
; j
++, copied
++) {
287 ret
= sst25l_wait_till_ready(flash
);
291 command
[1] = buf
[i
+ j
];
292 ret
= spi_write(flash
->spi
, command
, 2);
299 ret
= sst25l_write_enable(flash
, 0);
304 mutex_unlock(&flash
->lock
);
308 static struct flash_info
*sst25l_match_device(struct spi_device
*spi
)
310 struct flash_info
*flash_info
= NULL
;
311 struct spi_message m
;
312 struct spi_transfer t
;
313 unsigned char cmd_resp
[6];
317 spi_message_init(&m
);
318 memset(&t
, 0, sizeof(struct spi_transfer
));
320 cmd_resp
[0] = SST25L_CMD_READ_ID
;
328 t
.len
= sizeof(cmd_resp
);
329 spi_message_add_tail(&t
, &m
);
330 err
= spi_sync(spi
, &m
);
332 dev_err(&spi
->dev
, "error reading device id\n");
336 id
= (cmd_resp
[4] << 8) | cmd_resp
[5];
338 for (i
= 0; i
< ARRAY_SIZE(sst25l_flash_info
); i
++)
339 if (sst25l_flash_info
[i
].device_id
== id
)
340 flash_info
= &sst25l_flash_info
[i
];
343 dev_err(&spi
->dev
, "unknown id %.4x\n", id
);
348 static int sst25l_probe(struct spi_device
*spi
)
350 struct flash_info
*flash_info
;
351 struct sst25l_flash
*flash
;
352 struct flash_platform_data
*data
;
355 flash_info
= sst25l_match_device(spi
);
359 flash
= devm_kzalloc(&spi
->dev
, sizeof(*flash
), GFP_KERNEL
);
364 mutex_init(&flash
->lock
);
365 spi_set_drvdata(spi
, flash
);
367 data
= dev_get_platdata(&spi
->dev
);
368 if (data
&& data
->name
)
369 flash
->mtd
.name
= data
->name
;
371 flash
->mtd
.dev
.parent
= &spi
->dev
;
372 flash
->mtd
.type
= MTD_NORFLASH
;
373 flash
->mtd
.flags
= MTD_CAP_NORFLASH
;
374 flash
->mtd
.erasesize
= flash_info
->erase_size
;
375 flash
->mtd
.writesize
= flash_info
->page_size
;
376 flash
->mtd
.writebufsize
= flash_info
->page_size
;
377 flash
->mtd
.size
= flash_info
->page_size
* flash_info
->nr_pages
;
378 flash
->mtd
._erase
= sst25l_erase
;
379 flash
->mtd
._read
= sst25l_read
;
380 flash
->mtd
._write
= sst25l_write
;
382 dev_info(&spi
->dev
, "%s (%lld KiB)\n", flash_info
->name
,
383 (long long)flash
->mtd
.size
>> 10);
385 pr_debug("mtd .name = %s, .size = 0x%llx (%lldMiB) "
386 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
388 (long long)flash
->mtd
.size
, (long long)(flash
->mtd
.size
>> 20),
389 flash
->mtd
.erasesize
, flash
->mtd
.erasesize
/ 1024,
390 flash
->mtd
.numeraseregions
);
393 ret
= mtd_device_register(&flash
->mtd
, data
? data
->parts
: NULL
,
394 data
? data
->nr_parts
: 0);
401 static int sst25l_remove(struct spi_device
*spi
)
403 struct sst25l_flash
*flash
= spi_get_drvdata(spi
);
405 return mtd_device_unregister(&flash
->mtd
);
408 static struct spi_driver sst25l_driver
= {
412 .probe
= sst25l_probe
,
413 .remove
= sst25l_remove
,
416 module_spi_driver(sst25l_driver
);
418 MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips");
419 MODULE_AUTHOR("Andre Renaud <andre@bluewatersys.com>, "
421 MODULE_LICENSE("GPL");