m25p80.c 11 KB

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