Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * sst25l.c
4 *
5 * Driver for SST25L SPI Flash chips
6 *
7 * Copyright © 2009 Bluewater Systems Ltd
8 * Author: Andre Renaud <andre@bluewatersys.com>
9 * Author: Ryan Mallon
10 *
11 * Based on m25p80.c
12 */
13
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>
20
21#include <linux/mtd/mtd.h>
22#include <linux/mtd/partitions.h>
23
24#include <linux/spi/spi.h>
25#include <linux/spi/flash.h>
26
27/* Erases can take up to 3 seconds! */
28#define MAX_READY_WAIT_JIFFIES msecs_to_jiffies(3000)
29
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 */
35
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 */
40
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 */
45
46struct sst25l_flash {
47 struct spi_device *spi;
48 struct mutex lock;
49 struct mtd_info mtd;
50};
51
52struct flash_info {
53 const char *name;
54 uint16_t device_id;
55 unsigned page_size;
56 unsigned nr_pages;
57 unsigned erase_size;
58};
59
60#define to_sst25l_flash(x) container_of(x, struct sst25l_flash, mtd)
61
62static struct flash_info sst25l_flash_info[] = {
63 {"sst25lf020a", 0xbf43, 256, 1024, 4096},
64 {"sst25lf040a", 0xbf44, 256, 2048, 4096},
65};
66
67static int sst25l_status(struct sst25l_flash *flash, int *status)
68{
69 struct spi_message m;
70 struct spi_transfer t;
71 unsigned char cmd_resp[2];
72 int err;
73
74 spi_message_init(&m);
75 memset(&t, 0, sizeof(struct spi_transfer));
76
77 cmd_resp[0] = SST25L_CMD_RDSR;
78 cmd_resp[1] = 0xff;
79 t.tx_buf = cmd_resp;
80 t.rx_buf = cmd_resp;
81 t.len = sizeof(cmd_resp);
82 spi_message_add_tail(&t, &m);
83 err = spi_sync(flash->spi, &m);
84 if (err < 0)
85 return err;
86
87 *status = cmd_resp[1];
88 return 0;
89}
90
91static int sst25l_write_enable(struct sst25l_flash *flash, int enable)
92{
93 unsigned char command[2];
94 int status, err;
95
96 command[0] = enable ? SST25L_CMD_WREN : SST25L_CMD_WRDI;
97 err = spi_write(flash->spi, command, 1);
98 if (err)
99 return err;
100
101 command[0] = SST25L_CMD_EWSR;
102 err = spi_write(flash->spi, command, 1);
103 if (err)
104 return err;
105
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);
109 if (err)
110 return err;
111
112 if (enable) {
113 err = sst25l_status(flash, &status);
114 if (err)
115 return err;
116 if (!(status & SST25L_STATUS_WREN))
117 return -EROFS;
118 }
119
120 return 0;
121}
122
123static int sst25l_wait_till_ready(struct sst25l_flash *flash)
124{
125 unsigned long deadline;
126 int status, err;
127
128 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
129 do {
130 err = sst25l_status(flash, &status);
131 if (err)
132 return err;
133 if (!(status & SST25L_STATUS_BUSY))
134 return 0;
135
136 cond_resched();
137 } while (!time_after_eq(jiffies, deadline));
138
139 return -ETIMEDOUT;
140}
141
142static int sst25l_erase_sector(struct sst25l_flash *flash, uint32_t offset)
143{
144 unsigned char command[4];
145 int err;
146
147 err = sst25l_write_enable(flash, 1);
148 if (err)
149 return err;
150
151 command[0] = SST25L_CMD_SECTOR_ERASE;
152 command[1] = offset >> 16;
153 command[2] = offset >> 8;
154 command[3] = offset;
155 err = spi_write(flash->spi, command, 4);
156 if (err)
157 return err;
158
159 err = sst25l_wait_till_ready(flash);
160 if (err)
161 return err;
162
163 return sst25l_write_enable(flash, 0);
164}
165
166static int sst25l_erase(struct mtd_info *mtd, struct erase_info *instr)
167{
168 struct sst25l_flash *flash = to_sst25l_flash(mtd);
169 uint32_t addr, end;
170 int err;
171
172 /* Sanity checks */
173 if ((uint32_t)instr->len % mtd->erasesize)
174 return -EINVAL;
175
176 if ((uint32_t)instr->addr % mtd->erasesize)
177 return -EINVAL;
178
179 addr = instr->addr;
180 end = addr + instr->len;
181
182 mutex_lock(&flash->lock);
183
184 err = sst25l_wait_till_ready(flash);
185 if (err) {
186 mutex_unlock(&flash->lock);
187 return err;
188 }
189
190 while (addr < end) {
191 err = sst25l_erase_sector(flash, addr);
192 if (err) {
193 mutex_unlock(&flash->lock);
194 dev_err(&flash->spi->dev, "Erase failed\n");
195 return err;
196 }
197
198 addr += mtd->erasesize;
199 }
200
201 mutex_unlock(&flash->lock);
202
203 return 0;
204}
205
206static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len,
207 size_t *retlen, unsigned char *buf)
208{
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];
213 int ret;
214
215 spi_message_init(&message);
216 memset(&transfer, 0, sizeof(transfer));
217
218 command[0] = SST25L_CMD_READ;
219 command[1] = from >> 16;
220 command[2] = from >> 8;
221 command[3] = from;
222
223 transfer[0].tx_buf = command;
224 transfer[0].len = sizeof(command);
225 spi_message_add_tail(&transfer[0], &message);
226
227 transfer[1].rx_buf = buf;
228 transfer[1].len = len;
229 spi_message_add_tail(&transfer[1], &message);
230
231 mutex_lock(&flash->lock);
232
233 /* Wait for previous write/erase to complete */
234 ret = sst25l_wait_till_ready(flash);
235 if (ret) {
236 mutex_unlock(&flash->lock);
237 return ret;
238 }
239
240 spi_sync(flash->spi, &message);
241
242 if (retlen && message.actual_length > sizeof(command))
243 *retlen += message.actual_length - sizeof(command);
244
245 mutex_unlock(&flash->lock);
246 return 0;
247}
248
249static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len,
250 size_t *retlen, const unsigned char *buf)
251{
252 struct sst25l_flash *flash = to_sst25l_flash(mtd);
253 int i, j, ret, bytes, copied = 0;
254 unsigned char command[5];
255
256 if ((uint32_t)to % mtd->writesize)
257 return -EINVAL;
258
259 mutex_lock(&flash->lock);
260
261 ret = sst25l_write_enable(flash, 1);
262 if (ret)
263 goto out;
264
265 for (i = 0; i < len; i += mtd->writesize) {
266 ret = sst25l_wait_till_ready(flash);
267 if (ret)
268 goto out;
269
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);
275 command[4] = buf[i];
276 ret = spi_write(flash->spi, command, 5);
277 if (ret < 0)
278 goto out;
279 copied++;
280
281 /*
282 * Write the remaining bytes using auto address
283 * increment mode
284 */
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);
288 if (ret)
289 goto out;
290
291 command[1] = buf[i + j];
292 ret = spi_write(flash->spi, command, 2);
293 if (ret)
294 goto out;
295 }
296 }
297
298out:
299 ret = sst25l_write_enable(flash, 0);
300
301 if (retlen)
302 *retlen = copied;
303
304 mutex_unlock(&flash->lock);
305 return ret;
306}
307
308static struct flash_info *sst25l_match_device(struct spi_device *spi)
309{
310 struct flash_info *flash_info = NULL;
311 struct spi_message m;
312 struct spi_transfer t;
313 unsigned char cmd_resp[6];
314 int i, err;
315 uint16_t id;
316
317 spi_message_init(&m);
318 memset(&t, 0, sizeof(struct spi_transfer));
319
320 cmd_resp[0] = SST25L_CMD_READ_ID;
321 cmd_resp[1] = 0;
322 cmd_resp[2] = 0;
323 cmd_resp[3] = 0;
324 cmd_resp[4] = 0xff;
325 cmd_resp[5] = 0xff;
326 t.tx_buf = cmd_resp;
327 t.rx_buf = cmd_resp;
328 t.len = sizeof(cmd_resp);
329 spi_message_add_tail(&t, &m);
330 err = spi_sync(spi, &m);
331 if (err < 0) {
332 dev_err(&spi->dev, "error reading device id\n");
333 return NULL;
334 }
335
336 id = (cmd_resp[4] << 8) | cmd_resp[5];
337
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];
341
342 if (!flash_info)
343 dev_err(&spi->dev, "unknown id %.4x\n", id);
344
345 return flash_info;
346}
347
348static int sst25l_probe(struct spi_device *spi)
349{
350 struct flash_info *flash_info;
351 struct sst25l_flash *flash;
352 struct flash_platform_data *data;
353 int ret;
354
355 flash_info = sst25l_match_device(spi);
356 if (!flash_info)
357 return -ENODEV;
358
359 flash = devm_kzalloc(&spi->dev, sizeof(*flash), GFP_KERNEL);
360 if (!flash)
361 return -ENOMEM;
362
363 flash->spi = spi;
364 mutex_init(&flash->lock);
365 spi_set_drvdata(spi, flash);
366
367 data = dev_get_platdata(&spi->dev);
368 if (data && data->name)
369 flash->mtd.name = data->name;
370
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;
381
382 dev_info(&spi->dev, "%s (%lld KiB)\n", flash_info->name,
383 (long long)flash->mtd.size >> 10);
384
385 pr_debug("mtd .name = %s, .size = 0x%llx (%lldMiB) "
386 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
387 flash->mtd.name,
388 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
389 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
390 flash->mtd.numeraseregions);
391
392
393 ret = mtd_device_register(&flash->mtd, data ? data->parts : NULL,
394 data ? data->nr_parts : 0);
395 if (ret)
396 return -ENODEV;
397
398 return 0;
399}
400
401static void sst25l_remove(struct spi_device *spi)
402{
403 struct sst25l_flash *flash = spi_get_drvdata(spi);
404
405 WARN_ON(mtd_device_unregister(&flash->mtd));
406}
407
408static struct spi_driver sst25l_driver = {
409 .driver = {
410 .name = "sst25l",
411 },
412 .probe = sst25l_probe,
413 .remove = sst25l_remove,
414};
415
416module_spi_driver(sst25l_driver);
417
418MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips");
419MODULE_AUTHOR("Andre Renaud <andre@bluewatersys.com>, "
420 "Ryan Mallon");
421MODULE_LICENSE("GPL");
1/*
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>
8 * Author: Ryan Mallon
9 *
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
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>
25
26#include <linux/mtd/mtd.h>
27#include <linux/mtd/partitions.h>
28
29#include <linux/spi/spi.h>
30#include <linux/spi/flash.h>
31
32/* Erases can take up to 3 seconds! */
33#define MAX_READY_WAIT_JIFFIES msecs_to_jiffies(3000)
34
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 */
40
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 */
45
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 */
50
51struct sst25l_flash {
52 struct spi_device *spi;
53 struct mutex lock;
54 struct mtd_info mtd;
55
56 int partitioned;
57};
58
59struct flash_info {
60 const char *name;
61 uint16_t device_id;
62 unsigned page_size;
63 unsigned nr_pages;
64 unsigned erase_size;
65};
66
67#define to_sst25l_flash(x) container_of(x, struct sst25l_flash, mtd)
68
69static struct flash_info __devinitdata sst25l_flash_info[] = {
70 {"sst25lf020a", 0xbf43, 256, 1024, 4096},
71 {"sst25lf040a", 0xbf44, 256, 2048, 4096},
72};
73
74static int sst25l_status(struct sst25l_flash *flash, int *status)
75{
76 struct spi_message m;
77 struct spi_transfer t;
78 unsigned char cmd_resp[2];
79 int err;
80
81 spi_message_init(&m);
82 memset(&t, 0, sizeof(struct spi_transfer));
83
84 cmd_resp[0] = SST25L_CMD_RDSR;
85 cmd_resp[1] = 0xff;
86 t.tx_buf = cmd_resp;
87 t.rx_buf = cmd_resp;
88 t.len = sizeof(cmd_resp);
89 spi_message_add_tail(&t, &m);
90 err = spi_sync(flash->spi, &m);
91 if (err < 0)
92 return err;
93
94 *status = cmd_resp[1];
95 return 0;
96}
97
98static int sst25l_write_enable(struct sst25l_flash *flash, int enable)
99{
100 unsigned char command[2];
101 int status, err;
102
103 command[0] = enable ? SST25L_CMD_WREN : SST25L_CMD_WRDI;
104 err = spi_write(flash->spi, command, 1);
105 if (err)
106 return err;
107
108 command[0] = SST25L_CMD_EWSR;
109 err = spi_write(flash->spi, command, 1);
110 if (err)
111 return err;
112
113 command[0] = SST25L_CMD_WRSR;
114 command[1] = enable ? 0 : SST25L_STATUS_BP0 | SST25L_STATUS_BP1;
115 err = spi_write(flash->spi, command, 2);
116 if (err)
117 return err;
118
119 if (enable) {
120 err = sst25l_status(flash, &status);
121 if (err)
122 return err;
123 if (!(status & SST25L_STATUS_WREN))
124 return -EROFS;
125 }
126
127 return 0;
128}
129
130static int sst25l_wait_till_ready(struct sst25l_flash *flash)
131{
132 unsigned long deadline;
133 int status, err;
134
135 deadline = jiffies + MAX_READY_WAIT_JIFFIES;
136 do {
137 err = sst25l_status(flash, &status);
138 if (err)
139 return err;
140 if (!(status & SST25L_STATUS_BUSY))
141 return 0;
142
143 cond_resched();
144 } while (!time_after_eq(jiffies, deadline));
145
146 return -ETIMEDOUT;
147}
148
149static int sst25l_erase_sector(struct sst25l_flash *flash, uint32_t offset)
150{
151 unsigned char command[4];
152 int err;
153
154 err = sst25l_write_enable(flash, 1);
155 if (err)
156 return err;
157
158 command[0] = SST25L_CMD_SECTOR_ERASE;
159 command[1] = offset >> 16;
160 command[2] = offset >> 8;
161 command[3] = offset;
162 err = spi_write(flash->spi, command, 4);
163 if (err)
164 return err;
165
166 err = sst25l_wait_till_ready(flash);
167 if (err)
168 return err;
169
170 return sst25l_write_enable(flash, 0);
171}
172
173static int sst25l_erase(struct mtd_info *mtd, struct erase_info *instr)
174{
175 struct sst25l_flash *flash = to_sst25l_flash(mtd);
176 uint32_t addr, end;
177 int err;
178
179 /* Sanity checks */
180 if (instr->addr + instr->len > flash->mtd.size)
181 return -EINVAL;
182
183 if ((uint32_t)instr->len % mtd->erasesize)
184 return -EINVAL;
185
186 if ((uint32_t)instr->addr % mtd->erasesize)
187 return -EINVAL;
188
189 addr = instr->addr;
190 end = addr + instr->len;
191
192 mutex_lock(&flash->lock);
193
194 err = sst25l_wait_till_ready(flash);
195 if (err) {
196 mutex_unlock(&flash->lock);
197 return err;
198 }
199
200 while (addr < end) {
201 err = sst25l_erase_sector(flash, addr);
202 if (err) {
203 mutex_unlock(&flash->lock);
204 instr->state = MTD_ERASE_FAILED;
205 dev_err(&flash->spi->dev, "Erase failed\n");
206 return err;
207 }
208
209 addr += mtd->erasesize;
210 }
211
212 mutex_unlock(&flash->lock);
213
214 instr->state = MTD_ERASE_DONE;
215 mtd_erase_callback(instr);
216 return 0;
217}
218
219static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len,
220 size_t *retlen, unsigned char *buf)
221{
222 struct sst25l_flash *flash = to_sst25l_flash(mtd);
223 struct spi_transfer transfer[2];
224 struct spi_message message;
225 unsigned char command[4];
226 int ret;
227
228 /* Sanity checking */
229 if (len == 0)
230 return 0;
231
232 if (from + len > flash->mtd.size)
233 return -EINVAL;
234
235 if (retlen)
236 *retlen = 0;
237
238 spi_message_init(&message);
239 memset(&transfer, 0, sizeof(transfer));
240
241 command[0] = SST25L_CMD_READ;
242 command[1] = from >> 16;
243 command[2] = from >> 8;
244 command[3] = from;
245
246 transfer[0].tx_buf = command;
247 transfer[0].len = sizeof(command);
248 spi_message_add_tail(&transfer[0], &message);
249
250 transfer[1].rx_buf = buf;
251 transfer[1].len = len;
252 spi_message_add_tail(&transfer[1], &message);
253
254 mutex_lock(&flash->lock);
255
256 /* Wait for previous write/erase to complete */
257 ret = sst25l_wait_till_ready(flash);
258 if (ret) {
259 mutex_unlock(&flash->lock);
260 return ret;
261 }
262
263 spi_sync(flash->spi, &message);
264
265 if (retlen && message.actual_length > sizeof(command))
266 *retlen += message.actual_length - sizeof(command);
267
268 mutex_unlock(&flash->lock);
269 return 0;
270}
271
272static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len,
273 size_t *retlen, const unsigned char *buf)
274{
275 struct sst25l_flash *flash = to_sst25l_flash(mtd);
276 int i, j, ret, bytes, copied = 0;
277 unsigned char command[5];
278
279 /* Sanity checks */
280 if (!len)
281 return 0;
282
283 if (to + len > flash->mtd.size)
284 return -EINVAL;
285
286 if ((uint32_t)to % mtd->writesize)
287 return -EINVAL;
288
289 mutex_lock(&flash->lock);
290
291 ret = sst25l_write_enable(flash, 1);
292 if (ret)
293 goto out;
294
295 for (i = 0; i < len; i += mtd->writesize) {
296 ret = sst25l_wait_till_ready(flash);
297 if (ret)
298 goto out;
299
300 /* Write the first byte of the page */
301 command[0] = SST25L_CMD_AAI_PROGRAM;
302 command[1] = (to + i) >> 16;
303 command[2] = (to + i) >> 8;
304 command[3] = (to + i);
305 command[4] = buf[i];
306 ret = spi_write(flash->spi, command, 5);
307 if (ret < 0)
308 goto out;
309 copied++;
310
311 /*
312 * Write the remaining bytes using auto address
313 * increment mode
314 */
315 bytes = min_t(uint32_t, mtd->writesize, len - i);
316 for (j = 1; j < bytes; j++, copied++) {
317 ret = sst25l_wait_till_ready(flash);
318 if (ret)
319 goto out;
320
321 command[1] = buf[i + j];
322 ret = spi_write(flash->spi, command, 2);
323 if (ret)
324 goto out;
325 }
326 }
327
328out:
329 ret = sst25l_write_enable(flash, 0);
330
331 if (retlen)
332 *retlen = copied;
333
334 mutex_unlock(&flash->lock);
335 return ret;
336}
337
338static struct flash_info *__devinit sst25l_match_device(struct spi_device *spi)
339{
340 struct flash_info *flash_info = NULL;
341 struct spi_message m;
342 struct spi_transfer t;
343 unsigned char cmd_resp[6];
344 int i, err;
345 uint16_t id;
346
347 spi_message_init(&m);
348 memset(&t, 0, sizeof(struct spi_transfer));
349
350 cmd_resp[0] = SST25L_CMD_READ_ID;
351 cmd_resp[1] = 0;
352 cmd_resp[2] = 0;
353 cmd_resp[3] = 0;
354 cmd_resp[4] = 0xff;
355 cmd_resp[5] = 0xff;
356 t.tx_buf = cmd_resp;
357 t.rx_buf = cmd_resp;
358 t.len = sizeof(cmd_resp);
359 spi_message_add_tail(&t, &m);
360 err = spi_sync(spi, &m);
361 if (err < 0) {
362 dev_err(&spi->dev, "error reading device id\n");
363 return NULL;
364 }
365
366 id = (cmd_resp[4] << 8) | cmd_resp[5];
367
368 for (i = 0; i < ARRAY_SIZE(sst25l_flash_info); i++)
369 if (sst25l_flash_info[i].device_id == id)
370 flash_info = &sst25l_flash_info[i];
371
372 if (!flash_info)
373 dev_err(&spi->dev, "unknown id %.4x\n", id);
374
375 return flash_info;
376}
377
378static int __devinit sst25l_probe(struct spi_device *spi)
379{
380 struct flash_info *flash_info;
381 struct sst25l_flash *flash;
382 struct flash_platform_data *data;
383 int ret, i;
384 struct mtd_partition *parts = NULL;
385 int nr_parts = 0;
386
387 flash_info = sst25l_match_device(spi);
388 if (!flash_info)
389 return -ENODEV;
390
391 flash = kzalloc(sizeof(struct sst25l_flash), GFP_KERNEL);
392 if (!flash)
393 return -ENOMEM;
394
395 flash->spi = spi;
396 mutex_init(&flash->lock);
397 dev_set_drvdata(&spi->dev, flash);
398
399 data = spi->dev.platform_data;
400 if (data && data->name)
401 flash->mtd.name = data->name;
402 else
403 flash->mtd.name = dev_name(&spi->dev);
404
405 flash->mtd.type = MTD_NORFLASH;
406 flash->mtd.flags = MTD_CAP_NORFLASH;
407 flash->mtd.erasesize = flash_info->erase_size;
408 flash->mtd.writesize = flash_info->page_size;
409 flash->mtd.size = flash_info->page_size * flash_info->nr_pages;
410 flash->mtd.erase = sst25l_erase;
411 flash->mtd.read = sst25l_read;
412 flash->mtd.write = sst25l_write;
413
414 dev_info(&spi->dev, "%s (%lld KiB)\n", flash_info->name,
415 (long long)flash->mtd.size >> 10);
416
417 DEBUG(MTD_DEBUG_LEVEL2,
418 "mtd .name = %s, .size = 0x%llx (%lldMiB) "
419 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
420 flash->mtd.name,
421 (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
422 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
423 flash->mtd.numeraseregions);
424
425
426 if (mtd_has_cmdlinepart()) {
427 static const char *part_probes[] = {"cmdlinepart", NULL};
428
429 nr_parts = parse_mtd_partitions(&flash->mtd,
430 part_probes,
431 &parts, 0);
432 }
433
434 if (nr_parts <= 0 && data && data->parts) {
435 parts = data->parts;
436 nr_parts = data->nr_parts;
437 }
438
439 if (nr_parts > 0) {
440 for (i = 0; i < nr_parts; i++) {
441 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
442 "{.name = %s, .offset = 0x%llx, "
443 ".size = 0x%llx (%lldKiB) }\n",
444 i, parts[i].name,
445 (long long)parts[i].offset,
446 (long long)parts[i].size,
447 (long long)(parts[i].size >> 10));
448 }
449
450 flash->partitioned = 1;
451 return mtd_device_register(&flash->mtd, parts,
452 nr_parts);
453 }
454
455 ret = mtd_device_register(&flash->mtd, NULL, 0);
456 if (ret == 1) {
457 kfree(flash);
458 dev_set_drvdata(&spi->dev, NULL);
459 return -ENODEV;
460 }
461
462 return 0;
463}
464
465static int __devexit sst25l_remove(struct spi_device *spi)
466{
467 struct sst25l_flash *flash = dev_get_drvdata(&spi->dev);
468 int ret;
469
470 ret = mtd_device_unregister(&flash->mtd);
471 if (ret == 0)
472 kfree(flash);
473 return ret;
474}
475
476static struct spi_driver sst25l_driver = {
477 .driver = {
478 .name = "sst25l",
479 .bus = &spi_bus_type,
480 .owner = THIS_MODULE,
481 },
482 .probe = sst25l_probe,
483 .remove = __devexit_p(sst25l_remove),
484};
485
486static int __init sst25l_init(void)
487{
488 return spi_register_driver(&sst25l_driver);
489}
490
491static void __exit sst25l_exit(void)
492{
493 spi_unregister_driver(&sst25l_driver);
494}
495
496module_init(sst25l_init);
497module_exit(sst25l_exit);
498
499MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips");
500MODULE_AUTHOR("Andre Renaud <andre@bluewatersys.com>, "
501 "Ryan Mallon");
502MODULE_LICENSE("GPL");