m25p80.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * MTD SPI driver for ST M25Pxx (and similar) serial flash chips
  3. *
  4. * Author: Mike Lavender, mike@steroidmicros.com
  5. *
  6. * Copyright (c) 2005, Intec Automation Inc.
  7. *
  8. * Some parts are based on lart.c by Abraham Van Der Merwe
  9. *
  10. * Cleaned up and generalized based on mtd_dataflash.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/err.h>
  18. #include <linux/errno.h>
  19. #include <linux/module.h>
  20. #include <linux/device.h>
  21. #include <linux/mtd/mtd.h>
  22. #include <linux/mtd/partitions.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/spi/spi-mem.h>
  25. #include <linux/spi/flash.h>
  26. #include <linux/mtd/spi-nor.h>
  27. struct m25p {
  28. struct spi_mem *spimem;
  29. struct spi_nor spi_nor;
  30. };
  31. static int m25p80_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
  32. {
  33. struct m25p *flash = nor->priv;
  34. struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(code, 1),
  35. SPI_MEM_OP_NO_ADDR,
  36. SPI_MEM_OP_NO_DUMMY,
  37. SPI_MEM_OP_DATA_IN(len, NULL, 1));
  38. void *scratchbuf;
  39. int ret;
  40. scratchbuf = kmalloc(len, GFP_KERNEL);
  41. if (!scratchbuf)
  42. return -ENOMEM;
  43. op.data.buf.in = scratchbuf;
  44. ret = spi_mem_exec_op(flash->spimem, &op);
  45. if (ret < 0)
  46. dev_err(&flash->spimem->spi->dev, "error %d reading %x\n", ret,
  47. code);
  48. else
  49. memcpy(val, scratchbuf, len);
  50. kfree(scratchbuf);
  51. return ret;
  52. }
  53. static int m25p80_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  54. {
  55. struct m25p *flash = nor->priv;
  56. struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 1),
  57. SPI_MEM_OP_NO_ADDR,
  58. SPI_MEM_OP_NO_DUMMY,
  59. SPI_MEM_OP_DATA_OUT(len, NULL, 1));
  60. void *scratchbuf;
  61. int ret;
  62. scratchbuf = kmemdup(buf, len, GFP_KERNEL);
  63. if (!scratchbuf)
  64. return -ENOMEM;
  65. op.data.buf.out = scratchbuf;
  66. ret = spi_mem_exec_op(flash->spimem, &op);
  67. kfree(scratchbuf);
  68. return ret;
  69. }
  70. static ssize_t m25p80_write(struct spi_nor *nor, loff_t to, size_t len,
  71. const u_char *buf)
  72. {
  73. struct m25p *flash = nor->priv;
  74. struct spi_mem_op op =
  75. SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1),
  76. SPI_MEM_OP_ADDR(nor->addr_width, to, 1),
  77. SPI_MEM_OP_NO_DUMMY,
  78. SPI_MEM_OP_DATA_OUT(len, buf, 1));
  79. int ret;
  80. /* get transfer protocols. */
  81. op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto);
  82. op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto);
  83. op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
  84. if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
  85. op.addr.nbytes = 0;
  86. ret = spi_mem_adjust_op_size(flash->spimem, &op);
  87. if (ret)
  88. return ret;
  89. op.data.nbytes = len < op.data.nbytes ? len : op.data.nbytes;
  90. ret = spi_mem_exec_op(flash->spimem, &op);
  91. if (ret)
  92. return ret;
  93. return op.data.nbytes;
  94. }
  95. /*
  96. * Read an address range from the nor chip. The address range
  97. * may be any size provided it is within the physical boundaries.
  98. */
  99. static ssize_t m25p80_read(struct spi_nor *nor, loff_t from, size_t len,
  100. u_char *buf)
  101. {
  102. struct m25p *flash = nor->priv;
  103. struct spi_mem_op op =
  104. SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
  105. SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
  106. SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
  107. SPI_MEM_OP_DATA_IN(len, buf, 1));
  108. size_t remaining = len;
  109. int ret;
  110. /* get transfer protocols. */
  111. op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
  112. op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
  113. op.dummy.buswidth = op.addr.buswidth;
  114. op.data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
  115. /* convert the dummy cycles to the number of bytes */
  116. op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8;
  117. while (remaining) {
  118. op.data.nbytes = remaining < UINT_MAX ? remaining : UINT_MAX;
  119. ret = spi_mem_adjust_op_size(flash->spimem, &op);
  120. if (ret)
  121. return ret;
  122. ret = spi_mem_exec_op(flash->spimem, &op);
  123. if (ret)
  124. return ret;
  125. op.addr.val += op.data.nbytes;
  126. remaining -= op.data.nbytes;
  127. op.data.buf.in += op.data.nbytes;
  128. }
  129. return len;
  130. }
  131. /*
  132. * board specific setup should have ensured the SPI clock used here
  133. * matches what the READ command supports, at least until this driver
  134. * understands FAST_READ (for clocks over 25 MHz).
  135. */
  136. static int m25p_probe(struct spi_mem *spimem)
  137. {
  138. struct spi_device *spi = spimem->spi;
  139. struct flash_platform_data *data;
  140. struct m25p *flash;
  141. struct spi_nor *nor;
  142. struct spi_nor_hwcaps hwcaps = {
  143. .mask = SNOR_HWCAPS_READ |
  144. SNOR_HWCAPS_READ_FAST |
  145. SNOR_HWCAPS_PP,
  146. };
  147. char *flash_name;
  148. int ret;
  149. data = dev_get_platdata(&spimem->spi->dev);
  150. flash = devm_kzalloc(&spimem->spi->dev, sizeof(*flash), GFP_KERNEL);
  151. if (!flash)
  152. return -ENOMEM;
  153. nor = &flash->spi_nor;
  154. /* install the hooks */
  155. nor->read = m25p80_read;
  156. nor->write = m25p80_write;
  157. nor->write_reg = m25p80_write_reg;
  158. nor->read_reg = m25p80_read_reg;
  159. nor->dev = &spimem->spi->dev;
  160. spi_nor_set_flash_node(nor, spi->dev.of_node);
  161. nor->priv = flash;
  162. spi_mem_set_drvdata(spimem, flash);
  163. flash->spimem = spimem;
  164. if (spi->mode & SPI_RX_QUAD) {
  165. hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
  166. if (spi->mode & SPI_TX_QUAD)
  167. hwcaps.mask |= (SNOR_HWCAPS_READ_1_4_4 |
  168. SNOR_HWCAPS_PP_1_1_4 |
  169. SNOR_HWCAPS_PP_1_4_4);
  170. } else if (spi->mode & SPI_RX_DUAL) {
  171. hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
  172. if (spi->mode & SPI_TX_DUAL)
  173. hwcaps.mask |= SNOR_HWCAPS_READ_1_2_2;
  174. }
  175. if (data && data->name)
  176. nor->mtd.name = data->name;
  177. if (!nor->mtd.name)
  178. nor->mtd.name = spi_mem_get_name(spimem);
  179. /* For some (historical?) reason many platforms provide two different
  180. * names in flash_platform_data: "name" and "type". Quite often name is
  181. * set to "m25p80" and then "type" provides a real chip name.
  182. * If that's the case, respect "type" and ignore a "name".
  183. */
  184. if (data && data->type)
  185. flash_name = data->type;
  186. else if (!strcmp(spi->modalias, "spi-nor"))
  187. flash_name = NULL; /* auto-detect */
  188. else
  189. flash_name = spi->modalias;
  190. ret = spi_nor_scan(nor, flash_name, &hwcaps);
  191. if (ret)
  192. return ret;
  193. return mtd_device_register(&nor->mtd, data ? data->parts : NULL,
  194. data ? data->nr_parts : 0);
  195. }
  196. static int m25p_remove(struct spi_mem *spimem)
  197. {
  198. struct m25p *flash = spi_mem_get_drvdata(spimem);
  199. spi_nor_restore(&flash->spi_nor);
  200. /* Clean up MTD stuff. */
  201. return mtd_device_unregister(&flash->spi_nor.mtd);
  202. }
  203. static void m25p_shutdown(struct spi_mem *spimem)
  204. {
  205. struct m25p *flash = spi_mem_get_drvdata(spimem);
  206. spi_nor_restore(&flash->spi_nor);
  207. }
  208. /*
  209. * Do NOT add to this array without reading the following:
  210. *
  211. * Historically, many flash devices are bound to this driver by their name. But
  212. * since most of these flash are compatible to some extent, and their
  213. * differences can often be differentiated by the JEDEC read-ID command, we
  214. * encourage new users to add support to the spi-nor library, and simply bind
  215. * against a generic string here (e.g., "jedec,spi-nor").
  216. *
  217. * Many flash names are kept here in this list (as well as in spi-nor.c) to
  218. * keep them available as module aliases for existing platforms.
  219. */
  220. static const struct spi_device_id m25p_ids[] = {
  221. /*
  222. * Allow non-DT platform devices to bind to the "spi-nor" modalias, and
  223. * hack around the fact that the SPI core does not provide uevent
  224. * matching for .of_match_table
  225. */
  226. {"spi-nor"},
  227. /*
  228. * Entries not used in DTs that should be safe to drop after replacing
  229. * them with "spi-nor" in platform data.
  230. */
  231. {"s25sl064a"}, {"w25x16"}, {"m25p10"}, {"m25px64"},
  232. /*
  233. * Entries that were used in DTs without "jedec,spi-nor" fallback and
  234. * should be kept for backward compatibility.
  235. */
  236. {"at25df321a"}, {"at25df641"}, {"at26df081a"},
  237. {"mx25l4005a"}, {"mx25l1606e"}, {"mx25l6405d"}, {"mx25l12805d"},
  238. {"mx25l25635e"},{"mx66l51235l"},
  239. {"n25q064"}, {"n25q128a11"}, {"n25q128a13"}, {"n25q512a"},
  240. {"s25fl256s1"}, {"s25fl512s"}, {"s25sl12801"}, {"s25fl008k"},
  241. {"s25fl064k"},
  242. {"sst25vf040b"},{"sst25vf016b"},{"sst25vf032b"},{"sst25wf040"},
  243. {"m25p40"}, {"m25p80"}, {"m25p16"}, {"m25p32"},
  244. {"m25p64"}, {"m25p128"},
  245. {"w25x80"}, {"w25x32"}, {"w25q32"}, {"w25q32dw"},
  246. {"w25q80bl"}, {"w25q128"}, {"w25q256"},
  247. /* Flashes that can't be detected using JEDEC */
  248. {"m25p05-nonjedec"}, {"m25p10-nonjedec"}, {"m25p20-nonjedec"},
  249. {"m25p40-nonjedec"}, {"m25p80-nonjedec"}, {"m25p16-nonjedec"},
  250. {"m25p32-nonjedec"}, {"m25p64-nonjedec"}, {"m25p128-nonjedec"},
  251. /* Everspin MRAMs (non-JEDEC) */
  252. { "mr25h128" }, /* 128 Kib, 40 MHz */
  253. { "mr25h256" }, /* 256 Kib, 40 MHz */
  254. { "mr25h10" }, /* 1 Mib, 40 MHz */
  255. { "mr25h40" }, /* 4 Mib, 40 MHz */
  256. { },
  257. };
  258. MODULE_DEVICE_TABLE(spi, m25p_ids);
  259. static const struct of_device_id m25p_of_table[] = {
  260. /*
  261. * Generic compatibility for SPI NOR that can be identified by the
  262. * JEDEC READ ID opcode (0x9F). Use this, if possible.
  263. */
  264. { .compatible = "jedec,spi-nor" },
  265. {}
  266. };
  267. MODULE_DEVICE_TABLE(of, m25p_of_table);
  268. static struct spi_mem_driver m25p80_driver = {
  269. .spidrv = {
  270. .driver = {
  271. .name = "m25p80",
  272. .of_match_table = m25p_of_table,
  273. },
  274. .id_table = m25p_ids,
  275. },
  276. .probe = m25p_probe,
  277. .remove = m25p_remove,
  278. .shutdown = m25p_shutdown,
  279. /* REVISIT: many of these chips have deep power-down modes, which
  280. * should clearly be entered on suspend() to minimize power use.
  281. * And also when they're otherwise idle...
  282. */
  283. };
  284. module_spi_mem_driver(m25p80_driver);
  285. MODULE_LICENSE("GPL");
  286. MODULE_AUTHOR("Mike Lavender");
  287. MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");