sst25l.c 10 KB

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