sst25l.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. return err;
  148. while (addr < end) {
  149. err = sst25l_erase_sector(flash, addr);
  150. if (err) {
  151. mutex_unlock(&flash->lock);
  152. instr->state = MTD_ERASE_FAILED;
  153. dev_err(&flash->spi->dev, "Erase failed\n");
  154. return err;
  155. }
  156. addr += mtd->erasesize;
  157. }
  158. mutex_unlock(&flash->lock);
  159. instr->state = MTD_ERASE_DONE;
  160. mtd_erase_callback(instr);
  161. return 0;
  162. }
  163. static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len,
  164. size_t *retlen, unsigned char *buf)
  165. {
  166. struct sst25l_flash *flash = to_sst25l_flash(mtd);
  167. struct spi_transfer transfer[2];
  168. struct spi_message message;
  169. unsigned char command[4];
  170. int ret;
  171. /* Sanity checking */
  172. if (len == 0)
  173. return 0;
  174. if (from + len > flash->mtd.size)
  175. return -EINVAL;
  176. if (retlen)
  177. *retlen = 0;
  178. spi_message_init(&message);
  179. memset(&transfer, 0, sizeof(transfer));
  180. command[0] = SST25L_CMD_READ;
  181. command[1] = from >> 16;
  182. command[2] = from >> 8;
  183. command[3] = from;
  184. transfer[0].tx_buf = command;
  185. transfer[0].len = sizeof(command);
  186. spi_message_add_tail(&transfer[0], &message);
  187. transfer[1].rx_buf = buf;
  188. transfer[1].len = len;
  189. spi_message_add_tail(&transfer[1], &message);
  190. mutex_lock(&flash->lock);
  191. /* Wait for previous write/erase to complete */
  192. ret = sst25l_wait_till_ready(flash);
  193. if (ret) {
  194. mutex_unlock(&flash->lock);
  195. return ret;
  196. }
  197. spi_sync(flash->spi, &message);
  198. if (retlen && message.actual_length > sizeof(command))
  199. *retlen += message.actual_length - sizeof(command);
  200. mutex_unlock(&flash->lock);
  201. return 0;
  202. }
  203. static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len,
  204. size_t *retlen, const unsigned char *buf)
  205. {
  206. struct sst25l_flash *flash = to_sst25l_flash(mtd);
  207. int i, j, ret, bytes, copied = 0;
  208. unsigned char command[5];
  209. /* Sanity checks */
  210. if (!len)
  211. return 0;
  212. if (to + len > flash->mtd.size)
  213. return -EINVAL;
  214. if ((uint32_t)to % mtd->writesize)
  215. return -EINVAL;
  216. mutex_lock(&flash->lock);
  217. ret = sst25l_write_enable(flash, 1);
  218. if (ret)
  219. goto out;
  220. for (i = 0; i < len; i += mtd->writesize) {
  221. ret = sst25l_wait_till_ready(flash);
  222. if (ret)
  223. goto out;
  224. /* Write the first byte of the page */
  225. command[0] = SST25L_CMD_AAI_PROGRAM;
  226. command[1] = (to + i) >> 16;
  227. command[2] = (to + i) >> 8;
  228. command[3] = (to + i);
  229. command[4] = buf[i];
  230. ret = spi_write(flash->spi, command, 5);
  231. if (ret < 0)
  232. goto out;
  233. copied++;
  234. /*
  235. * Write the remaining bytes using auto address
  236. * increment mode
  237. */
  238. bytes = min_t(uint32_t, mtd->writesize, len - i);
  239. for (j = 1; j < bytes; j++, copied++) {
  240. ret = sst25l_wait_till_ready(flash);
  241. if (ret)
  242. goto out;
  243. command[1] = buf[i + j];
  244. ret = spi_write(flash->spi, command, 2);
  245. if (ret)
  246. goto out;
  247. }
  248. }
  249. out:
  250. ret = sst25l_write_enable(flash, 0);
  251. if (retlen)
  252. *retlen = copied;
  253. mutex_unlock(&flash->lock);
  254. return ret;
  255. }
  256. static struct flash_info *__init sst25l_match_device(struct spi_device *spi)
  257. {
  258. struct flash_info *flash_info = NULL;
  259. unsigned char command[4], response;
  260. int i, err;
  261. uint16_t id;
  262. command[0] = SST25L_CMD_READ_ID;
  263. command[1] = 0;
  264. command[2] = 0;
  265. command[3] = 0;
  266. err = spi_write_then_read(spi, command, sizeof(command), &response, 1);
  267. if (err < 0) {
  268. dev_err(&spi->dev, "error reading device id msb\n");
  269. return NULL;
  270. }
  271. id = response << 8;
  272. command[0] = SST25L_CMD_READ_ID;
  273. command[1] = 0;
  274. command[2] = 0;
  275. command[3] = 1;
  276. err = spi_write_then_read(spi, command, sizeof(command), &response, 1);
  277. if (err < 0) {
  278. dev_err(&spi->dev, "error reading device id lsb\n");
  279. return NULL;
  280. }
  281. id |= response;
  282. for (i = 0; i < ARRAY_SIZE(sst25l_flash_info); i++)
  283. if (sst25l_flash_info[i].device_id == id)
  284. flash_info = &sst25l_flash_info[i];
  285. if (!flash_info)
  286. dev_err(&spi->dev, "unknown id %.4x\n", id);
  287. return flash_info;
  288. }
  289. static int __init sst25l_probe(struct spi_device *spi)
  290. {
  291. struct flash_info *flash_info;
  292. struct sst25l_flash *flash;
  293. struct flash_platform_data *data;
  294. int ret, i;
  295. flash_info = sst25l_match_device(spi);
  296. if (!flash_info)
  297. return -ENODEV;
  298. flash = kzalloc(sizeof(struct sst25l_flash), GFP_KERNEL);
  299. if (!flash)
  300. return -ENOMEM;
  301. flash->spi = spi;
  302. mutex_init(&flash->lock);
  303. dev_set_drvdata(&spi->dev, flash);
  304. data = spi->dev.platform_data;
  305. if (data && data->name)
  306. flash->mtd.name = data->name;
  307. else
  308. flash->mtd.name = dev_name(&spi->dev);
  309. flash->mtd.type = MTD_NORFLASH;
  310. flash->mtd.flags = MTD_CAP_NORFLASH;
  311. flash->mtd.erasesize = flash_info->erase_size;
  312. flash->mtd.writesize = flash_info->page_size;
  313. flash->mtd.size = flash_info->page_size * flash_info->nr_pages;
  314. flash->mtd.erase = sst25l_erase;
  315. flash->mtd.read = sst25l_read;
  316. flash->mtd.write = sst25l_write;
  317. dev_info(&spi->dev, "%s (%lld KiB)\n", flash_info->name,
  318. (long long)flash->mtd.size >> 10);
  319. DEBUG(MTD_DEBUG_LEVEL2,
  320. "mtd .name = %s, .size = 0x%llx (%lldMiB) "
  321. ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  322. flash->mtd.name,
  323. (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20),
  324. flash->mtd.erasesize, flash->mtd.erasesize / 1024,
  325. flash->mtd.numeraseregions);
  326. if (flash->mtd.numeraseregions)
  327. for (i = 0; i < flash->mtd.numeraseregions; i++)
  328. DEBUG(MTD_DEBUG_LEVEL2,
  329. "mtd.eraseregions[%d] = { .offset = 0x%llx, "
  330. ".erasesize = 0x%.8x (%uKiB), "
  331. ".numblocks = %d }\n",
  332. i, (long long)flash->mtd.eraseregions[i].offset,
  333. flash->mtd.eraseregions[i].erasesize,
  334. flash->mtd.eraseregions[i].erasesize / 1024,
  335. flash->mtd.eraseregions[i].numblocks);
  336. if (mtd_has_partitions()) {
  337. struct mtd_partition *parts = NULL;
  338. int nr_parts = 0;
  339. if (mtd_has_cmdlinepart()) {
  340. static const char *part_probes[] =
  341. {"cmdlinepart", NULL};
  342. nr_parts = parse_mtd_partitions(&flash->mtd,
  343. part_probes,
  344. &parts, 0);
  345. }
  346. if (nr_parts <= 0 && data && data->parts) {
  347. parts = data->parts;
  348. nr_parts = data->nr_parts;
  349. }
  350. if (nr_parts > 0) {
  351. for (i = 0; i < nr_parts; i++) {
  352. DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
  353. "{.name = %s, .offset = 0x%llx, "
  354. ".size = 0x%llx (%lldKiB) }\n",
  355. i, parts[i].name,
  356. (long long)parts[i].offset,
  357. (long long)parts[i].size,
  358. (long long)(parts[i].size >> 10));
  359. }
  360. flash->partitioned = 1;
  361. return add_mtd_partitions(&flash->mtd,
  362. parts, nr_parts);
  363. }
  364. } else if (data->nr_parts) {
  365. dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
  366. data->nr_parts, data->name);
  367. }
  368. ret = add_mtd_device(&flash->mtd);
  369. if (ret == 1) {
  370. kfree(flash);
  371. dev_set_drvdata(&spi->dev, NULL);
  372. return -ENODEV;
  373. }
  374. return 0;
  375. }
  376. static int __exit sst25l_remove(struct spi_device *spi)
  377. {
  378. struct sst25l_flash *flash = dev_get_drvdata(&spi->dev);
  379. int ret;
  380. if (mtd_has_partitions() && flash->partitioned)
  381. ret = del_mtd_partitions(&flash->mtd);
  382. else
  383. ret = del_mtd_device(&flash->mtd);
  384. if (ret == 0)
  385. kfree(flash);
  386. return ret;
  387. }
  388. static struct spi_driver sst25l_driver = {
  389. .driver = {
  390. .name = "sst25l",
  391. .bus = &spi_bus_type,
  392. .owner = THIS_MODULE,
  393. },
  394. .probe = sst25l_probe,
  395. .remove = __exit_p(sst25l_remove),
  396. };
  397. static int __init sst25l_init(void)
  398. {
  399. return spi_register_driver(&sst25l_driver);
  400. }
  401. static void __exit sst25l_exit(void)
  402. {
  403. spi_unregister_driver(&sst25l_driver);
  404. }
  405. module_init(sst25l_init);
  406. module_exit(sst25l_exit);
  407. MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips");
  408. MODULE_AUTHOR("Andre Renaud <andre@bluewatersys.com>, "
  409. "Ryan Mallon <ryan@bluewatersys.com>");
  410. MODULE_LICENSE("GPL");