sst25l.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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/module.h>
  18. #include <linux/device.h>
  19. #include <linux/mutex.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/slab.h>
  22. #include <linux/sched.h>
  23. #include <linux/mtd/mtd.h>
  24. #include <linux/mtd/partitions.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/spi/flash.h>
  27. /* Erases can take up to 3 seconds! */
  28. #define MAX_READY_WAIT_JIFFIES msecs_to_jiffies(3000)
  29. #define SST25L_CMD_WRSR 0x01 /* Write status register */
  30. #define SST25L_CMD_WRDI 0x04 /* Write disable */
  31. #define SST25L_CMD_RDSR 0x05 /* Read status register */
  32. #define SST25L_CMD_WREN 0x06 /* Write enable */
  33. #define SST25L_CMD_READ 0x03 /* High speed read */
  34. #define SST25L_CMD_EWSR 0x50 /* Enable write status register */
  35. #define SST25L_CMD_SECTOR_ERASE 0x20 /* Erase sector */
  36. #define SST25L_CMD_READ_ID 0x90 /* Read device ID */
  37. #define SST25L_CMD_AAI_PROGRAM 0xaf /* Auto address increment */
  38. #define SST25L_STATUS_BUSY (1 << 0) /* Chip is busy */
  39. #define SST25L_STATUS_WREN (1 << 1) /* Write enabled */
  40. #define SST25L_STATUS_BP0 (1 << 2) /* Block protection 0 */
  41. #define SST25L_STATUS_BP1 (1 << 3) /* Block protection 1 */
  42. struct sst25l_flash {
  43. struct spi_device *spi;
  44. struct mutex lock;
  45. struct mtd_info mtd;
  46. };
  47. struct flash_info {
  48. const char *name;
  49. uint16_t device_id;
  50. unsigned page_size;
  51. unsigned nr_pages;
  52. unsigned erase_size;
  53. };
  54. #define to_sst25l_flash(x) container_of(x, struct sst25l_flash, mtd)
  55. static struct flash_info sst25l_flash_info[] = {
  56. {"sst25lf020a", 0xbf43, 256, 1024, 4096},
  57. {"sst25lf040a", 0xbf44, 256, 2048, 4096},
  58. };
  59. static int sst25l_status(struct sst25l_flash *flash, int *status)
  60. {
  61. struct spi_message m;
  62. struct spi_transfer t;
  63. unsigned char cmd_resp[2];
  64. int err;
  65. spi_message_init(&m);
  66. memset(&t, 0, sizeof(struct spi_transfer));
  67. cmd_resp[0] = SST25L_CMD_RDSR;
  68. cmd_resp[1] = 0xff;
  69. t.tx_buf = cmd_resp;
  70. t.rx_buf = cmd_resp;
  71. t.len = sizeof(cmd_resp);
  72. spi_message_add_tail(&t, &m);
  73. err = spi_sync(flash->spi, &m);
  74. if (err < 0)
  75. return err;
  76. *status = cmd_resp[1];
  77. return 0;
  78. }
  79. static int sst25l_write_enable(struct sst25l_flash *flash, int enable)
  80. {
  81. unsigned char command[2];
  82. int status, err;
  83. command[0] = enable ? SST25L_CMD_WREN : SST25L_CMD_WRDI;
  84. err = spi_write(flash->spi, command, 1);
  85. if (err)
  86. return err;
  87. command[0] = SST25L_CMD_EWSR;
  88. err = spi_write(flash->spi, command, 1);
  89. if (err)
  90. return err;
  91. command[0] = SST25L_CMD_WRSR;
  92. command[1] = enable ? 0 : SST25L_STATUS_BP0 | SST25L_STATUS_BP1;
  93. err = spi_write(flash->spi, command, 2);
  94. if (err)
  95. return err;
  96. if (enable) {
  97. err = sst25l_status(flash, &status);
  98. if (err)
  99. return err;
  100. if (!(status & SST25L_STATUS_WREN))
  101. return -EROFS;
  102. }
  103. return 0;
  104. }
  105. static int sst25l_wait_till_ready(struct sst25l_flash *flash)
  106. {
  107. unsigned long deadline;
  108. int status, err;
  109. deadline = jiffies + MAX_READY_WAIT_JIFFIES;
  110. do {
  111. err = sst25l_status(flash, &status);
  112. if (err)
  113. return err;
  114. if (!(status & SST25L_STATUS_BUSY))
  115. return 0;
  116. cond_resched();
  117. } while (!time_after_eq(jiffies, deadline));
  118. return -ETIMEDOUT;
  119. }
  120. static int sst25l_erase_sector(struct sst25l_flash *flash, uint32_t offset)
  121. {
  122. unsigned char command[4];
  123. int err;
  124. err = sst25l_write_enable(flash, 1);
  125. if (err)
  126. return err;
  127. command[0] = SST25L_CMD_SECTOR_ERASE;
  128. command[1] = offset >> 16;
  129. command[2] = offset >> 8;
  130. command[3] = offset;
  131. err = spi_write(flash->spi, command, 4);
  132. if (err)
  133. return err;
  134. err = sst25l_wait_till_ready(flash);
  135. if (err)
  136. return err;
  137. return sst25l_write_enable(flash, 0);
  138. }
  139. static int sst25l_erase(struct mtd_info *mtd, struct erase_info *instr)
  140. {
  141. struct sst25l_flash *flash = to_sst25l_flash(mtd);
  142. uint32_t addr, end;
  143. int err;
  144. /* Sanity checks */
  145. if ((uint32_t)instr->len % mtd->erasesize)
  146. return -EINVAL;
  147. if ((uint32_t)instr->addr % mtd->erasesize)
  148. return -EINVAL;
  149. addr = instr->addr;
  150. end = addr + instr->len;
  151. mutex_lock(&flash->lock);
  152. err = sst25l_wait_till_ready(flash);
  153. if (err) {
  154. mutex_unlock(&flash->lock);
  155. return err;
  156. }
  157. while (addr < end) {
  158. err = sst25l_erase_sector(flash, addr);
  159. if (err) {
  160. mutex_unlock(&flash->lock);
  161. instr->state = MTD_ERASE_FAILED;
  162. dev_err(&flash->spi->dev, "Erase failed\n");
  163. return err;
  164. }
  165. addr += mtd->erasesize;
  166. }
  167. mutex_unlock(&flash->lock);
  168. instr->state = MTD_ERASE_DONE;
  169. mtd_erase_callback(instr);
  170. return 0;
  171. }
  172. static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len,
  173. size_t *retlen, unsigned char *buf)
  174. {
  175. struct sst25l_flash *flash = to_sst25l_flash(mtd);
  176. struct spi_transfer transfer[2];
  177. struct spi_message message;
  178. unsigned char command[4];
  179. int ret;
  180. spi_message_init(&message);
  181. memset(&transfer, 0, sizeof(transfer));
  182. command[0] = SST25L_CMD_READ;
  183. command[1] = from >> 16;
  184. command[2] = from >> 8;
  185. command[3] = from;
  186. transfer[0].tx_buf = command;
  187. transfer[0].len = sizeof(command);
  188. spi_message_add_tail(&transfer[0], &message);
  189. transfer[1].rx_buf = buf;
  190. transfer[1].len = len;
  191. spi_message_add_tail(&transfer[1], &message);
  192. mutex_lock(&flash->lock);
  193. /* Wait for previous write/erase to complete */
  194. ret = sst25l_wait_till_ready(flash);
  195. if (ret) {
  196. mutex_unlock(&flash->lock);
  197. return ret;
  198. }
  199. spi_sync(flash->spi, &message);
  200. if (retlen && message.actual_length > sizeof(command))
  201. *retlen += message.actual_length - sizeof(command);
  202. mutex_unlock(&flash->lock);
  203. return 0;
  204. }
  205. static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len,
  206. size_t *retlen, const unsigned char *buf)
  207. {
  208. struct sst25l_flash *flash = to_sst25l_flash(mtd);
  209. int i, j, ret, bytes, copied = 0;
  210. unsigned char command[5];
  211. if ((uint32_t)to % mtd->writesize)
  212. return -EINVAL;
  213. mutex_lock(&flash->lock);
  214. ret = sst25l_write_enable(flash, 1);
  215. if (ret)
  216. goto out;
  217. for (i = 0; i < len; i += mtd->writesize) {
  218. ret = sst25l_wait_till_ready(flash);
  219. if (ret)
  220. goto out;
  221. /* Write the first byte of the page */
  222. command[0] = SST25L_CMD_AAI_PROGRAM;
  223. command[1] = (to + i) >> 16;
  224. command[2] = (to + i) >> 8;
  225. command[3] = (to + i);
  226. command[4] = buf[i];
  227. ret = spi_write(flash->spi, command, 5);
  228. if (ret < 0)
  229. goto out;
  230. copied++;
  231. /*
  232. * Write the remaining bytes using auto address
  233. * increment mode
  234. */
  235. bytes = min_t(uint32_t, mtd->writesize, len - i);
  236. for (j = 1; j < bytes; j++, copied++) {
  237. ret = sst25l_wait_till_ready(flash);
  238. if (ret)
  239. goto out;
  240. command[1] = buf[i + j];
  241. ret = spi_write(flash->spi, command, 2);
  242. if (ret)
  243. goto out;
  244. }
  245. }
  246. out:
  247. ret = sst25l_write_enable(flash, 0);
  248. if (retlen)
  249. *retlen = copied;
  250. mutex_unlock(&flash->lock);
  251. return ret;
  252. }
  253. static struct flash_info *sst25l_match_device(struct spi_device *spi)
  254. {
  255. struct flash_info *flash_info = NULL;
  256. struct spi_message m;
  257. struct spi_transfer t;
  258. unsigned char cmd_resp[6];
  259. int i, err;
  260. uint16_t id;
  261. spi_message_init(&m);
  262. memset(&t, 0, sizeof(struct spi_transfer));
  263. cmd_resp[0] = SST25L_CMD_READ_ID;
  264. cmd_resp[1] = 0;
  265. cmd_resp[2] = 0;
  266. cmd_resp[3] = 0;
  267. cmd_resp[4] = 0xff;
  268. cmd_resp[5] = 0xff;
  269. t.tx_buf = cmd_resp;
  270. t.rx_buf = cmd_resp;
  271. t.len = sizeof(cmd_resp);
  272. spi_message_add_tail(&t, &m);
  273. err = spi_sync(spi, &m);
  274. if (err < 0) {
  275. dev_err(&spi->dev, "error reading device id\n");
  276. return NULL;
  277. }
  278. id = (cmd_resp[4] << 8) | cmd_resp[5];
  279. for (i = 0; i < ARRAY_SIZE(sst25l_flash_info); i++)
  280. if (sst25l_flash_info[i].device_id == id)
  281. flash_info = &sst25l_flash_info[i];
  282. if (!flash_info)
  283. dev_err(&spi->dev, "unknown id %.4x\n", id);
  284. return flash_info;
  285. }
  286. static int sst25l_probe(struct spi_device *spi)
  287. {
  288. struct flash_info *flash_info;
  289. struct sst25l_flash *flash;
  290. struct flash_platform_data *data;
  291. int ret;
  292. flash_info = sst25l_match_device(spi);
  293. if (!flash_info)
  294. return -ENODEV;
  295. flash = devm_kzalloc(&spi->dev, sizeof(*flash), GFP_KERNEL);
  296. if (!flash)
  297. return -ENOMEM;
  298. flash->spi = spi;
  299. mutex_init(&flash->lock);
  300. spi_set_drvdata(spi, flash);
  301. data = dev_get_platdata(&spi->dev);
  302. if (data && data->name)
  303. flash->mtd.name = data->name;
  304. flash->mtd.dev.parent = &spi->dev;
  305. flash->mtd.type = MTD_NORFLASH;
  306. flash->mtd.flags = MTD_CAP_NORFLASH;
  307. flash->mtd.erasesize = flash_info->erase_size;
  308. flash->mtd.writesize = flash_info->page_size;
  309. flash->mtd.writebufsize = flash_info->page_size;
  310. flash->mtd.size = flash_info->page_size * flash_info->nr_pages;
  311. flash->mtd._erase = sst25l_erase;
  312. flash->mtd._read = sst25l_read;
  313. flash->mtd._write = sst25l_write;
  314. dev_info(&spi->dev, "%s (%lld KiB)\n", flash_info->name,
  315. (long long)flash->mtd.size >> 10);
  316. pr_debug("mtd .name = %s, .size = 0x%llx (%lldMiB) "
  317. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  318. flash->mtd.name,
  319. (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
  320. flash->mtd.erasesize, flash->mtd.erasesize / 1024,
  321. flash->mtd.numeraseregions);
  322. ret = mtd_device_parse_register(&flash->mtd, NULL, NULL,
  323. data ? data->parts : NULL,
  324. data ? data->nr_parts : 0);
  325. if (ret)
  326. return -ENODEV;
  327. return 0;
  328. }
  329. static int sst25l_remove(struct spi_device *spi)
  330. {
  331. struct sst25l_flash *flash = spi_get_drvdata(spi);
  332. return mtd_device_unregister(&flash->mtd);
  333. }
  334. static struct spi_driver sst25l_driver = {
  335. .driver = {
  336. .name = "sst25l",
  337. },
  338. .probe = sst25l_probe,
  339. .remove = sst25l_remove,
  340. };
  341. module_spi_driver(sst25l_driver);
  342. MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips");
  343. MODULE_AUTHOR("Andre Renaud <andre@bluewatersys.com>, "
  344. "Ryan Mallon");
  345. MODULE_LICENSE("GPL");