sst25l.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 <ryan@bluewatersys.com>
  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. #include <linux/init.h>
  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>
  24. #include <linux/mtd/mtd.h>
  25. #include <linux/mtd/partitions.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/spi/flash.h>
  28. /* Erases can take up to 3 seconds! */
  29. #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 */
  35. #define SST25L_CMD_EWSR 0x50 /* Enable write status register */
  36. #define SST25L_CMD_SECTOR_ERASE 0x20 /* Erase sector */
  37. #define SST25L_CMD_READ_ID 0x90 /* Read device ID */
  38. #define SST25L_CMD_AAI_PROGRAM 0xaf /* Auto address increment */
  39. #define SST25L_STATUS_BUSY (1 << 0) /* Chip is busy */
  40. #define SST25L_STATUS_WREN (1 << 1) /* Write enabled */
  41. #define SST25L_STATUS_BP0 (1 << 2) /* Block protection 0 */
  42. #define SST25L_STATUS_BP1 (1 << 3) /* Block protection 1 */
  43. struct sst25l_flash {
  44. struct spi_device *spi;
  45. struct mutex lock;
  46. struct mtd_info mtd;
  47. int partitioned;
  48. };
  49. struct flash_info {
  50. const char *name;
  51. uint16_t device_id;
  52. unsigned page_size;
  53. unsigned nr_pages;
  54. unsigned erase_size;
  55. };
  56. #define to_sst25l_flash(x) container_of(x, struct sst25l_flash, mtd)
  57. static struct flash_info __initdata sst25l_flash_info[] = {
  58. {"sst25lf020a", 0xbf43, 256, 1024, 4096},
  59. {"sst25lf040a", 0xbf44, 256, 2048, 4096},
  60. };
  61. static int sst25l_status(struct sst25l_flash *flash, int *status)
  62. {
  63. unsigned char command, response;
  64. int err;
  65. command = SST25L_CMD_RDSR;
  66. err = spi_write_then_read(flash->spi, &command, 1, &response, 1);
  67. if (err < 0)
  68. return err;
  69. *status = response;
  70. return 0;
  71. }
  72. static int sst25l_write_enable(struct sst25l_flash *flash, int enable)
  73. {
  74. unsigned char command[2];
  75. int status, err;
  76. command[0] = enable ? SST25L_CMD_WREN : SST25L_CMD_WRDI;
  77. err = spi_write(flash->spi, command, 1);
  78. if (err)
  79. return err;
  80. command[0] = SST25L_CMD_EWSR;
  81. err = spi_write(flash->spi, command, 1);
  82. if (err)
  83. return err;
  84. command[0] = SST25L_CMD_WRSR;
  85. command[1] = enable ? 0 : SST25L_STATUS_BP0 | SST25L_STATUS_BP1;
  86. err = spi_write(flash->spi, command, 2);
  87. if (err)
  88. return err;
  89. if (enable) {
  90. err = sst25l_status(flash, &status);
  91. if (err)
  92. return err;
  93. if (!(status & SST25L_STATUS_WREN))
  94. return -EROFS;
  95. }
  96. return 0;
  97. }
  98. static int sst25l_wait_till_ready(struct sst25l_flash *flash)
  99. {
  100. unsigned long deadline;
  101. int status, err;
  102. deadline = jiffies + MAX_READY_WAIT_JIFFIES;
  103. do {
  104. err = sst25l_status(flash, &status);
  105. if (err)
  106. return err;
  107. if (!(status & SST25L_STATUS_BUSY))
  108. return 0;
  109. cond_resched();
  110. } while (!time_after_eq(jiffies, deadline));
  111. return -ETIMEDOUT;
  112. }
  113. static int sst25l_erase_sector(struct sst25l_flash *flash, uint32_t offset)
  114. {
  115. unsigned char command[4];
  116. int err;
  117. err = sst25l_write_enable(flash, 1);
  118. if (err)
  119. return err;
  120. command[0] = SST25L_CMD_SECTOR_ERASE;
  121. command[1] = offset >> 16;
  122. command[2] = offset >> 8;
  123. command[3] = offset;
  124. err = spi_write(flash->spi, command, 4);
  125. if (err)
  126. return err;
  127. err = sst25l_wait_till_ready(flash);
  128. if (err)
  129. return err;
  130. return sst25l_write_enable(flash, 0);
  131. }
  132. static int sst25l_erase(struct mtd_info *mtd, struct erase_info *instr)
  133. {
  134. struct sst25l_flash *flash = to_sst25l_flash(mtd);
  135. uint32_t addr, end;
  136. int err;
  137. /* Sanity checks */
  138. if (instr->addr + instr->len > flash->mtd.size)
  139. return -EINVAL;
  140. if ((uint32_t)instr->len % mtd->erasesize)
  141. return -EINVAL;
  142. if ((uint32_t)instr->addr % mtd->erasesize)
  143. return -EINVAL;
  144. addr = instr->addr;
  145. end = addr + instr->len;
  146. mutex_lock(&flash->lock);
  147. err = sst25l_wait_till_ready(flash);
  148. if (err) {
  149. mutex_unlock(&flash->lock);
  150. return err;
  151. }
  152. while (addr < end) {
  153. err = sst25l_erase_sector(flash, addr);
  154. if (err) {
  155. mutex_unlock(&flash->lock);
  156. instr->state = MTD_ERASE_FAILED;
  157. dev_err(&flash->spi->dev, "Erase failed\n");
  158. return err;
  159. }
  160. addr += mtd->erasesize;
  161. }
  162. mutex_unlock(&flash->lock);
  163. instr->state = MTD_ERASE_DONE;
  164. mtd_erase_callback(instr);
  165. return 0;
  166. }
  167. static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len,
  168. size_t *retlen, unsigned char *buf)
  169. {
  170. struct sst25l_flash *flash = to_sst25l_flash(mtd);
  171. struct spi_transfer transfer[2];
  172. struct spi_message message;
  173. unsigned char command[4];
  174. int ret;
  175. /* Sanity checking */
  176. if (len == 0)
  177. return 0;
  178. if (from + len > flash->mtd.size)
  179. return -EINVAL;
  180. if (retlen)
  181. *retlen = 0;
  182. spi_message_init(&message);
  183. memset(&transfer, 0, sizeof(transfer));
  184. command[0] = SST25L_CMD_READ;
  185. command[1] = from >> 16;
  186. command[2] = from >> 8;
  187. command[3] = from;
  188. transfer[0].tx_buf = command;
  189. transfer[0].len = sizeof(command);
  190. spi_message_add_tail(&transfer[0], &message);
  191. transfer[1].rx_buf = buf;
  192. transfer[1].len = len;
  193. spi_message_add_tail(&transfer[1], &message);
  194. mutex_lock(&flash->lock);
  195. /* Wait for previous write/erase to complete */
  196. ret = sst25l_wait_till_ready(flash);
  197. if (ret) {
  198. mutex_unlock(&flash->lock);
  199. return ret;
  200. }
  201. spi_sync(flash->spi, &message);
  202. if (retlen && message.actual_length > sizeof(command))
  203. *retlen += message.actual_length - sizeof(command);
  204. mutex_unlock(&flash->lock);
  205. return 0;
  206. }
  207. static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len,
  208. size_t *retlen, const unsigned char *buf)
  209. {
  210. struct sst25l_flash *flash = to_sst25l_flash(mtd);
  211. int i, j, ret, bytes, copied = 0;
  212. unsigned char command[5];
  213. /* Sanity checks */
  214. if (!len)
  215. return 0;
  216. if (to + len > flash->mtd.size)
  217. return -EINVAL;
  218. if ((uint32_t)to % mtd->writesize)
  219. return -EINVAL;
  220. mutex_lock(&flash->lock);
  221. ret = sst25l_write_enable(flash, 1);
  222. if (ret)
  223. goto out;
  224. for (i = 0; i < len; i += mtd->writesize) {
  225. ret = sst25l_wait_till_ready(flash);
  226. if (ret)
  227. goto out;
  228. /* Write the first byte of the page */
  229. command[0] = SST25L_CMD_AAI_PROGRAM;
  230. command[1] = (to + i) >> 16;
  231. command[2] = (to + i) >> 8;
  232. command[3] = (to + i);
  233. command[4] = buf[i];
  234. ret = spi_write(flash->spi, command, 5);
  235. if (ret < 0)
  236. goto out;
  237. copied++;
  238. /*
  239. * Write the remaining bytes using auto address
  240. * increment mode
  241. */
  242. bytes = min_t(uint32_t, mtd->writesize, len - i);
  243. for (j = 1; j < bytes; j++, copied++) {
  244. ret = sst25l_wait_till_ready(flash);
  245. if (ret)
  246. goto out;
  247. command[1] = buf[i + j];
  248. ret = spi_write(flash->spi, command, 2);
  249. if (ret)
  250. goto out;
  251. }
  252. }
  253. out:
  254. ret = sst25l_write_enable(flash, 0);
  255. if (retlen)
  256. *retlen = copied;
  257. mutex_unlock(&flash->lock);
  258. return ret;
  259. }
  260. static struct flash_info *__init sst25l_match_device(struct spi_device *spi)
  261. {
  262. struct flash_info *flash_info = NULL;
  263. unsigned char command[4], response;
  264. int i, err;
  265. uint16_t id;
  266. command[0] = SST25L_CMD_READ_ID;
  267. command[1] = 0;
  268. command[2] = 0;
  269. command[3] = 0;
  270. err = spi_write_then_read(spi, command, sizeof(command), &response, 1);
  271. if (err < 0) {
  272. dev_err(&spi->dev, "error reading device id msb\n");
  273. return NULL;
  274. }
  275. id = response << 8;
  276. command[0] = SST25L_CMD_READ_ID;
  277. command[1] = 0;
  278. command[2] = 0;
  279. command[3] = 1;
  280. err = spi_write_then_read(spi, command, sizeof(command), &response, 1);
  281. if (err < 0) {
  282. dev_err(&spi->dev, "error reading device id lsb\n");
  283. return NULL;
  284. }
  285. id |= response;
  286. for (i = 0; i < ARRAY_SIZE(sst25l_flash_info); i++)
  287. if (sst25l_flash_info[i].device_id == id)
  288. flash_info = &sst25l_flash_info[i];
  289. if (!flash_info)
  290. dev_err(&spi->dev, "unknown id %.4x\n", id);
  291. return flash_info;
  292. }
  293. static int __init sst25l_probe(struct spi_device *spi)
  294. {
  295. struct flash_info *flash_info;
  296. struct sst25l_flash *flash;
  297. struct flash_platform_data *data;
  298. int ret, i;
  299. flash_info = sst25l_match_device(spi);
  300. if (!flash_info)
  301. return -ENODEV;
  302. flash = kzalloc(sizeof(struct sst25l_flash), GFP_KERNEL);
  303. if (!flash)
  304. return -ENOMEM;
  305. flash->spi = spi;
  306. mutex_init(&flash->lock);
  307. dev_set_drvdata(&spi->dev, flash);
  308. data = spi->dev.platform_data;
  309. if (data && data->name)
  310. flash->mtd.name = data->name;
  311. else
  312. flash->mtd.name = dev_name(&spi->dev);
  313. flash->mtd.type = MTD_NORFLASH;
  314. flash->mtd.flags = MTD_CAP_NORFLASH;
  315. flash->mtd.erasesize = flash_info->erase_size;
  316. flash->mtd.writesize = flash_info->page_size;
  317. flash->mtd.size = flash_info->page_size * flash_info->nr_pages;
  318. flash->mtd.erase = sst25l_erase;
  319. flash->mtd.read = sst25l_read;
  320. flash->mtd.write = sst25l_write;
  321. dev_info(&spi->dev, "%s (%lld KiB)\n", flash_info->name,
  322. (long long)flash->mtd.size >> 10);
  323. DEBUG(MTD_DEBUG_LEVEL2,
  324. "mtd .name = %s, .size = 0x%llx (%lldMiB) "
  325. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  326. flash->mtd.name,
  327. (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
  328. flash->mtd.erasesize, flash->mtd.erasesize / 1024,
  329. flash->mtd.numeraseregions);
  330. if (flash->mtd.numeraseregions)
  331. for (i = 0; i < flash->mtd.numeraseregions; i++)
  332. DEBUG(MTD_DEBUG_LEVEL2,
  333. "mtd.eraseregions[%d] = { .offset = 0x%llx, "
  334. ".erasesize = 0x%.8x (%uKiB), "
  335. ".numblocks = %d }\n",
  336. i, (long long)flash->mtd.eraseregions[i].offset,
  337. flash->mtd.eraseregions[i].erasesize,
  338. flash->mtd.eraseregions[i].erasesize / 1024,
  339. flash->mtd.eraseregions[i].numblocks);
  340. if (mtd_has_partitions()) {
  341. struct mtd_partition *parts = NULL;
  342. int nr_parts = 0;
  343. if (mtd_has_cmdlinepart()) {
  344. static const char *part_probes[] =
  345. {"cmdlinepart", NULL};
  346. nr_parts = parse_mtd_partitions(&flash->mtd,
  347. part_probes,
  348. &parts, 0);
  349. }
  350. if (nr_parts <= 0 && data && data->parts) {
  351. parts = data->parts;
  352. nr_parts = data->nr_parts;
  353. }
  354. if (nr_parts > 0) {
  355. for (i = 0; i < nr_parts; i++) {
  356. DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
  357. "{.name = %s, .offset = 0x%llx, "
  358. ".size = 0x%llx (%lldKiB) }\n",
  359. i, parts[i].name,
  360. (long long)parts[i].offset,
  361. (long long)parts[i].size,
  362. (long long)(parts[i].size >> 10));
  363. }
  364. flash->partitioned = 1;
  365. return add_mtd_partitions(&flash->mtd,
  366. parts, nr_parts);
  367. }
  368. } else if (data->nr_parts) {
  369. dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
  370. data->nr_parts, data->name);
  371. }
  372. ret = add_mtd_device(&flash->mtd);
  373. if (ret == 1) {
  374. kfree(flash);
  375. dev_set_drvdata(&spi->dev, NULL);
  376. return -ENODEV;
  377. }
  378. return 0;
  379. }
  380. static int __exit sst25l_remove(struct spi_device *spi)
  381. {
  382. struct sst25l_flash *flash = dev_get_drvdata(&spi->dev);
  383. int ret;
  384. if (mtd_has_partitions() && flash->partitioned)
  385. ret = del_mtd_partitions(&flash->mtd);
  386. else
  387. ret = del_mtd_device(&flash->mtd);
  388. if (ret == 0)
  389. kfree(flash);
  390. return ret;
  391. }
  392. static struct spi_driver sst25l_driver = {
  393. .driver = {
  394. .name = "sst25l",
  395. .bus = &spi_bus_type,
  396. .owner = THIS_MODULE,
  397. },
  398. .probe = sst25l_probe,
  399. .remove = __exit_p(sst25l_remove),
  400. };
  401. static int __init sst25l_init(void)
  402. {
  403. return spi_register_driver(&sst25l_driver);
  404. }
  405. static void __exit sst25l_exit(void)
  406. {
  407. spi_unregister_driver(&sst25l_driver);
  408. }
  409. module_init(sst25l_init);
  410. module_exit(sst25l_exit);
  411. MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips");
  412. MODULE_AUTHOR("Andre Renaud <andre@bluewatersys.com>, "
  413. "Ryan Mallon <ryan@bluewatersys.com>");
  414. MODULE_LICENSE("GPL");