sst25l.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. dev_err(&flash->spi->dev, "Erase failed\n");
  162. return err;
  163. }
  164. addr += mtd->erasesize;
  165. }
  166. mutex_unlock(&flash->lock);
  167. return 0;
  168. }
  169. static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len,
  170. size_t *retlen, unsigned char *buf)
  171. {
  172. struct sst25l_flash *flash = to_sst25l_flash(mtd);
  173. struct spi_transfer transfer[2];
  174. struct spi_message message;
  175. unsigned char command[4];
  176. int ret;
  177. spi_message_init(&message);
  178. memset(&transfer, 0, sizeof(transfer));
  179. command[0] = SST25L_CMD_READ;
  180. command[1] = from >> 16;
  181. command[2] = from >> 8;
  182. command[3] = from;
  183. transfer[0].tx_buf = command;
  184. transfer[0].len = sizeof(command);
  185. spi_message_add_tail(&transfer[0], &message);
  186. transfer[1].rx_buf = buf;
  187. transfer[1].len = len;
  188. spi_message_add_tail(&transfer[1], &message);
  189. mutex_lock(&flash->lock);
  190. /* Wait for previous write/erase to complete */
  191. ret = sst25l_wait_till_ready(flash);
  192. if (ret) {
  193. mutex_unlock(&flash->lock);
  194. return ret;
  195. }
  196. spi_sync(flash->spi, &message);
  197. if (retlen && message.actual_length > sizeof(command))
  198. *retlen += message.actual_length - sizeof(command);
  199. mutex_unlock(&flash->lock);
  200. return 0;
  201. }
  202. static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len,
  203. size_t *retlen, const unsigned char *buf)
  204. {
  205. struct sst25l_flash *flash = to_sst25l_flash(mtd);
  206. int i, j, ret, bytes, copied = 0;
  207. unsigned char command[5];
  208. if ((uint32_t)to % mtd->writesize)
  209. return -EINVAL;
  210. mutex_lock(&flash->lock);
  211. ret = sst25l_write_enable(flash, 1);
  212. if (ret)
  213. goto out;
  214. for (i = 0; i < len; i += mtd->writesize) {
  215. ret = sst25l_wait_till_ready(flash);
  216. if (ret)
  217. goto out;
  218. /* Write the first byte of the page */
  219. command[0] = SST25L_CMD_AAI_PROGRAM;
  220. command[1] = (to + i) >> 16;
  221. command[2] = (to + i) >> 8;
  222. command[3] = (to + i);
  223. command[4] = buf[i];
  224. ret = spi_write(flash->spi, command, 5);
  225. if (ret < 0)
  226. goto out;
  227. copied++;
  228. /*
  229. * Write the remaining bytes using auto address
  230. * increment mode
  231. */
  232. bytes = min_t(uint32_t, mtd->writesize, len - i);
  233. for (j = 1; j < bytes; j++, copied++) {
  234. ret = sst25l_wait_till_ready(flash);
  235. if (ret)
  236. goto out;
  237. command[1] = buf[i + j];
  238. ret = spi_write(flash->spi, command, 2);
  239. if (ret)
  240. goto out;
  241. }
  242. }
  243. out:
  244. ret = sst25l_write_enable(flash, 0);
  245. if (retlen)
  246. *retlen = copied;
  247. mutex_unlock(&flash->lock);
  248. return ret;
  249. }
  250. static struct flash_info *sst25l_match_device(struct spi_device *spi)
  251. {
  252. struct flash_info *flash_info = NULL;
  253. struct spi_message m;
  254. struct spi_transfer t;
  255. unsigned char cmd_resp[6];
  256. int i, err;
  257. uint16_t id;
  258. spi_message_init(&m);
  259. memset(&t, 0, sizeof(struct spi_transfer));
  260. cmd_resp[0] = SST25L_CMD_READ_ID;
  261. cmd_resp[1] = 0;
  262. cmd_resp[2] = 0;
  263. cmd_resp[3] = 0;
  264. cmd_resp[4] = 0xff;
  265. cmd_resp[5] = 0xff;
  266. t.tx_buf = cmd_resp;
  267. t.rx_buf = cmd_resp;
  268. t.len = sizeof(cmd_resp);
  269. spi_message_add_tail(&t, &m);
  270. err = spi_sync(spi, &m);
  271. if (err < 0) {
  272. dev_err(&spi->dev, "error reading device id\n");
  273. return NULL;
  274. }
  275. id = (cmd_resp[4] << 8) | cmd_resp[5];
  276. for (i = 0; i < ARRAY_SIZE(sst25l_flash_info); i++)
  277. if (sst25l_flash_info[i].device_id == id)
  278. flash_info = &sst25l_flash_info[i];
  279. if (!flash_info)
  280. dev_err(&spi->dev, "unknown id %.4x\n", id);
  281. return flash_info;
  282. }
  283. static int sst25l_probe(struct spi_device *spi)
  284. {
  285. struct flash_info *flash_info;
  286. struct sst25l_flash *flash;
  287. struct flash_platform_data *data;
  288. int ret;
  289. flash_info = sst25l_match_device(spi);
  290. if (!flash_info)
  291. return -ENODEV;
  292. flash = devm_kzalloc(&spi->dev, sizeof(*flash), GFP_KERNEL);
  293. if (!flash)
  294. return -ENOMEM;
  295. flash->spi = spi;
  296. mutex_init(&flash->lock);
  297. spi_set_drvdata(spi, flash);
  298. data = dev_get_platdata(&spi->dev);
  299. if (data && data->name)
  300. flash->mtd.name = data->name;
  301. flash->mtd.dev.parent = &spi->dev;
  302. flash->mtd.type = MTD_NORFLASH;
  303. flash->mtd.flags = MTD_CAP_NORFLASH;
  304. flash->mtd.erasesize = flash_info->erase_size;
  305. flash->mtd.writesize = flash_info->page_size;
  306. flash->mtd.writebufsize = flash_info->page_size;
  307. flash->mtd.size = flash_info->page_size * flash_info->nr_pages;
  308. flash->mtd._erase = sst25l_erase;
  309. flash->mtd._read = sst25l_read;
  310. flash->mtd._write = sst25l_write;
  311. dev_info(&spi->dev, "%s (%lld KiB)\n", flash_info->name,
  312. (long long)flash->mtd.size >> 10);
  313. pr_debug("mtd .name = %s, .size = 0x%llx (%lldMiB) "
  314. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  315. flash->mtd.name,
  316. (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
  317. flash->mtd.erasesize, flash->mtd.erasesize / 1024,
  318. flash->mtd.numeraseregions);
  319. ret = mtd_device_register(&flash->mtd, data ? data->parts : NULL,
  320. data ? data->nr_parts : 0);
  321. if (ret)
  322. return -ENODEV;
  323. return 0;
  324. }
  325. static int sst25l_remove(struct spi_device *spi)
  326. {
  327. struct sst25l_flash *flash = spi_get_drvdata(spi);
  328. return mtd_device_unregister(&flash->mtd);
  329. }
  330. static struct spi_driver sst25l_driver = {
  331. .driver = {
  332. .name = "sst25l",
  333. },
  334. .probe = sst25l_probe,
  335. .remove = sst25l_remove,
  336. };
  337. module_spi_driver(sst25l_driver);
  338. MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips");
  339. MODULE_AUTHOR("Andre Renaud <andre@bluewatersys.com>, "
  340. "Ryan Mallon");
  341. MODULE_LICENSE("GPL");