sst25l.c 12 KB

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