sst25l.c 12 KB

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