sst25l.c 9.8 KB

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