m25p80.c 9.2 KB

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