nxp-spifi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * SPI-NOR driver for NXP SPI Flash Interface (SPIFI)
  3. *
  4. * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
  5. *
  6. * Based on Freescale QuadSPI driver:
  7. * Copyright (C) 2013 Freescale Semiconductor, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/module.h>
  19. #include <linux/mtd/mtd.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/mtd/spi-nor.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/spi/spi.h>
  26. /* NXP SPIFI registers, bits and macros */
  27. #define SPIFI_CTRL 0x000
  28. #define SPIFI_CTRL_TIMEOUT(timeout) (timeout)
  29. #define SPIFI_CTRL_CSHIGH(cshigh) ((cshigh) << 16)
  30. #define SPIFI_CTRL_MODE3 BIT(23)
  31. #define SPIFI_CTRL_DUAL BIT(28)
  32. #define SPIFI_CTRL_FBCLK BIT(30)
  33. #define SPIFI_CMD 0x004
  34. #define SPIFI_CMD_DATALEN(dlen) ((dlen) & 0x3fff)
  35. #define SPIFI_CMD_DOUT BIT(15)
  36. #define SPIFI_CMD_INTLEN(ilen) ((ilen) << 16)
  37. #define SPIFI_CMD_FIELDFORM(field) ((field) << 19)
  38. #define SPIFI_CMD_FIELDFORM_ALL_SERIAL SPIFI_CMD_FIELDFORM(0x0)
  39. #define SPIFI_CMD_FIELDFORM_QUAD_DUAL_DATA SPIFI_CMD_FIELDFORM(0x1)
  40. #define SPIFI_CMD_FRAMEFORM(frame) ((frame) << 21)
  41. #define SPIFI_CMD_FRAMEFORM_OPCODE_ONLY SPIFI_CMD_FRAMEFORM(0x1)
  42. #define SPIFI_CMD_OPCODE(op) ((op) << 24)
  43. #define SPIFI_ADDR 0x008
  44. #define SPIFI_IDATA 0x00c
  45. #define SPIFI_CLIMIT 0x010
  46. #define SPIFI_DATA 0x014
  47. #define SPIFI_MCMD 0x018
  48. #define SPIFI_STAT 0x01c
  49. #define SPIFI_STAT_MCINIT BIT(0)
  50. #define SPIFI_STAT_CMD BIT(1)
  51. #define SPIFI_STAT_RESET BIT(4)
  52. #define SPI_NOR_MAX_ID_LEN 6
  53. struct nxp_spifi {
  54. struct device *dev;
  55. struct clk *clk_spifi;
  56. struct clk *clk_reg;
  57. void __iomem *io_base;
  58. void __iomem *flash_base;
  59. struct spi_nor nor;
  60. bool memory_mode;
  61. u32 mcmd;
  62. };
  63. static int nxp_spifi_wait_for_cmd(struct nxp_spifi *spifi)
  64. {
  65. u8 stat;
  66. int ret;
  67. ret = readb_poll_timeout(spifi->io_base + SPIFI_STAT, stat,
  68. !(stat & SPIFI_STAT_CMD), 10, 30);
  69. if (ret)
  70. dev_warn(spifi->dev, "command timed out\n");
  71. return ret;
  72. }
  73. static int nxp_spifi_reset(struct nxp_spifi *spifi)
  74. {
  75. u8 stat;
  76. int ret;
  77. writel(SPIFI_STAT_RESET, spifi->io_base + SPIFI_STAT);
  78. ret = readb_poll_timeout(spifi->io_base + SPIFI_STAT, stat,
  79. !(stat & SPIFI_STAT_RESET), 10, 30);
  80. if (ret)
  81. dev_warn(spifi->dev, "state reset timed out\n");
  82. return ret;
  83. }
  84. static int nxp_spifi_set_memory_mode_off(struct nxp_spifi *spifi)
  85. {
  86. int ret;
  87. if (!spifi->memory_mode)
  88. return 0;
  89. ret = nxp_spifi_reset(spifi);
  90. if (ret)
  91. dev_err(spifi->dev, "unable to enter command mode\n");
  92. else
  93. spifi->memory_mode = false;
  94. return ret;
  95. }
  96. static int nxp_spifi_set_memory_mode_on(struct nxp_spifi *spifi)
  97. {
  98. u8 stat;
  99. int ret;
  100. if (spifi->memory_mode)
  101. return 0;
  102. writel(spifi->mcmd, spifi->io_base + SPIFI_MCMD);
  103. ret = readb_poll_timeout(spifi->io_base + SPIFI_STAT, stat,
  104. stat & SPIFI_STAT_MCINIT, 10, 30);
  105. if (ret)
  106. dev_err(spifi->dev, "unable to enter memory mode\n");
  107. else
  108. spifi->memory_mode = true;
  109. return ret;
  110. }
  111. static int nxp_spifi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  112. {
  113. struct nxp_spifi *spifi = nor->priv;
  114. u32 cmd;
  115. int ret;
  116. ret = nxp_spifi_set_memory_mode_off(spifi);
  117. if (ret)
  118. return ret;
  119. cmd = SPIFI_CMD_DATALEN(len) |
  120. SPIFI_CMD_OPCODE(opcode) |
  121. SPIFI_CMD_FIELDFORM_ALL_SERIAL |
  122. SPIFI_CMD_FRAMEFORM_OPCODE_ONLY;
  123. writel(cmd, spifi->io_base + SPIFI_CMD);
  124. while (len--)
  125. *buf++ = readb(spifi->io_base + SPIFI_DATA);
  126. return nxp_spifi_wait_for_cmd(spifi);
  127. }
  128. static int nxp_spifi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  129. {
  130. struct nxp_spifi *spifi = nor->priv;
  131. u32 cmd;
  132. int ret;
  133. ret = nxp_spifi_set_memory_mode_off(spifi);
  134. if (ret)
  135. return ret;
  136. cmd = SPIFI_CMD_DOUT |
  137. SPIFI_CMD_DATALEN(len) |
  138. SPIFI_CMD_OPCODE(opcode) |
  139. SPIFI_CMD_FIELDFORM_ALL_SERIAL |
  140. SPIFI_CMD_FRAMEFORM_OPCODE_ONLY;
  141. writel(cmd, spifi->io_base + SPIFI_CMD);
  142. while (len--)
  143. writeb(*buf++, spifi->io_base + SPIFI_DATA);
  144. return nxp_spifi_wait_for_cmd(spifi);
  145. }
  146. static ssize_t nxp_spifi_read(struct spi_nor *nor, loff_t from, size_t len,
  147. u_char *buf)
  148. {
  149. struct nxp_spifi *spifi = nor->priv;
  150. int ret;
  151. ret = nxp_spifi_set_memory_mode_on(spifi);
  152. if (ret)
  153. return ret;
  154. memcpy_fromio(buf, spifi->flash_base + from, len);
  155. return len;
  156. }
  157. static ssize_t nxp_spifi_write(struct spi_nor *nor, loff_t to, size_t len,
  158. const u_char *buf)
  159. {
  160. struct nxp_spifi *spifi = nor->priv;
  161. u32 cmd;
  162. int ret;
  163. size_t i;
  164. ret = nxp_spifi_set_memory_mode_off(spifi);
  165. if (ret)
  166. return ret;
  167. writel(to, spifi->io_base + SPIFI_ADDR);
  168. cmd = SPIFI_CMD_DOUT |
  169. SPIFI_CMD_DATALEN(len) |
  170. SPIFI_CMD_FIELDFORM_ALL_SERIAL |
  171. SPIFI_CMD_OPCODE(nor->program_opcode) |
  172. SPIFI_CMD_FRAMEFORM(spifi->nor.addr_width + 1);
  173. writel(cmd, spifi->io_base + SPIFI_CMD);
  174. for (i = 0; i < len; i++)
  175. writeb(buf[i], spifi->io_base + SPIFI_DATA);
  176. ret = nxp_spifi_wait_for_cmd(spifi);
  177. if (ret)
  178. return ret;
  179. return len;
  180. }
  181. static int nxp_spifi_erase(struct spi_nor *nor, loff_t offs)
  182. {
  183. struct nxp_spifi *spifi = nor->priv;
  184. u32 cmd;
  185. int ret;
  186. ret = nxp_spifi_set_memory_mode_off(spifi);
  187. if (ret)
  188. return ret;
  189. writel(offs, spifi->io_base + SPIFI_ADDR);
  190. cmd = SPIFI_CMD_FIELDFORM_ALL_SERIAL |
  191. SPIFI_CMD_OPCODE(nor->erase_opcode) |
  192. SPIFI_CMD_FRAMEFORM(spifi->nor.addr_width + 1);
  193. writel(cmd, spifi->io_base + SPIFI_CMD);
  194. return nxp_spifi_wait_for_cmd(spifi);
  195. }
  196. static int nxp_spifi_setup_memory_cmd(struct nxp_spifi *spifi)
  197. {
  198. switch (spifi->nor.flash_read) {
  199. case SPI_NOR_NORMAL:
  200. case SPI_NOR_FAST:
  201. spifi->mcmd = SPIFI_CMD_FIELDFORM_ALL_SERIAL;
  202. break;
  203. case SPI_NOR_DUAL:
  204. case SPI_NOR_QUAD:
  205. spifi->mcmd = SPIFI_CMD_FIELDFORM_QUAD_DUAL_DATA;
  206. break;
  207. default:
  208. dev_err(spifi->dev, "unsupported SPI read mode\n");
  209. return -EINVAL;
  210. }
  211. /* Memory mode supports address length between 1 and 4 */
  212. if (spifi->nor.addr_width < 1 || spifi->nor.addr_width > 4)
  213. return -EINVAL;
  214. spifi->mcmd |= SPIFI_CMD_OPCODE(spifi->nor.read_opcode) |
  215. SPIFI_CMD_INTLEN(spifi->nor.read_dummy / 8) |
  216. SPIFI_CMD_FRAMEFORM(spifi->nor.addr_width + 1);
  217. return 0;
  218. }
  219. static void nxp_spifi_dummy_id_read(struct spi_nor *nor)
  220. {
  221. u8 id[SPI_NOR_MAX_ID_LEN];
  222. nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
  223. }
  224. static int nxp_spifi_setup_flash(struct nxp_spifi *spifi,
  225. struct device_node *np)
  226. {
  227. enum read_mode flash_read;
  228. u32 ctrl, property;
  229. u16 mode = 0;
  230. int ret;
  231. if (!of_property_read_u32(np, "spi-rx-bus-width", &property)) {
  232. switch (property) {
  233. case 1:
  234. break;
  235. case 2:
  236. mode |= SPI_RX_DUAL;
  237. break;
  238. case 4:
  239. mode |= SPI_RX_QUAD;
  240. break;
  241. default:
  242. dev_err(spifi->dev, "unsupported rx-bus-width\n");
  243. return -EINVAL;
  244. }
  245. }
  246. if (of_find_property(np, "spi-cpha", NULL))
  247. mode |= SPI_CPHA;
  248. if (of_find_property(np, "spi-cpol", NULL))
  249. mode |= SPI_CPOL;
  250. /* Setup control register defaults */
  251. ctrl = SPIFI_CTRL_TIMEOUT(1000) |
  252. SPIFI_CTRL_CSHIGH(15) |
  253. SPIFI_CTRL_FBCLK;
  254. if (mode & SPI_RX_DUAL) {
  255. ctrl |= SPIFI_CTRL_DUAL;
  256. flash_read = SPI_NOR_DUAL;
  257. } else if (mode & SPI_RX_QUAD) {
  258. ctrl &= ~SPIFI_CTRL_DUAL;
  259. flash_read = SPI_NOR_QUAD;
  260. } else {
  261. ctrl |= SPIFI_CTRL_DUAL;
  262. flash_read = SPI_NOR_NORMAL;
  263. }
  264. switch (mode & (SPI_CPHA | SPI_CPOL)) {
  265. case SPI_MODE_0:
  266. ctrl &= ~SPIFI_CTRL_MODE3;
  267. break;
  268. case SPI_MODE_3:
  269. ctrl |= SPIFI_CTRL_MODE3;
  270. break;
  271. default:
  272. dev_err(spifi->dev, "only mode 0 and 3 supported\n");
  273. return -EINVAL;
  274. }
  275. writel(ctrl, spifi->io_base + SPIFI_CTRL);
  276. spifi->nor.dev = spifi->dev;
  277. spi_nor_set_flash_node(&spifi->nor, np);
  278. spifi->nor.priv = spifi;
  279. spifi->nor.read = nxp_spifi_read;
  280. spifi->nor.write = nxp_spifi_write;
  281. spifi->nor.erase = nxp_spifi_erase;
  282. spifi->nor.read_reg = nxp_spifi_read_reg;
  283. spifi->nor.write_reg = nxp_spifi_write_reg;
  284. /*
  285. * The first read on a hard reset isn't reliable so do a
  286. * dummy read of the id before calling spi_nor_scan().
  287. * The reason for this problem is unknown.
  288. *
  289. * The official NXP spifilib uses more or less the same
  290. * workaround that is applied here by reading the device
  291. * id multiple times.
  292. */
  293. nxp_spifi_dummy_id_read(&spifi->nor);
  294. ret = spi_nor_scan(&spifi->nor, NULL, flash_read);
  295. if (ret) {
  296. dev_err(spifi->dev, "device scan failed\n");
  297. return ret;
  298. }
  299. ret = nxp_spifi_setup_memory_cmd(spifi);
  300. if (ret) {
  301. dev_err(spifi->dev, "memory command setup failed\n");
  302. return ret;
  303. }
  304. ret = mtd_device_register(&spifi->nor.mtd, NULL, 0);
  305. if (ret) {
  306. dev_err(spifi->dev, "mtd device parse failed\n");
  307. return ret;
  308. }
  309. return 0;
  310. }
  311. static int nxp_spifi_probe(struct platform_device *pdev)
  312. {
  313. struct device_node *flash_np;
  314. struct nxp_spifi *spifi;
  315. struct resource *res;
  316. int ret;
  317. spifi = devm_kzalloc(&pdev->dev, sizeof(*spifi), GFP_KERNEL);
  318. if (!spifi)
  319. return -ENOMEM;
  320. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spifi");
  321. spifi->io_base = devm_ioremap_resource(&pdev->dev, res);
  322. if (IS_ERR(spifi->io_base))
  323. return PTR_ERR(spifi->io_base);
  324. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "flash");
  325. spifi->flash_base = devm_ioremap_resource(&pdev->dev, res);
  326. if (IS_ERR(spifi->flash_base))
  327. return PTR_ERR(spifi->flash_base);
  328. spifi->clk_spifi = devm_clk_get(&pdev->dev, "spifi");
  329. if (IS_ERR(spifi->clk_spifi)) {
  330. dev_err(&pdev->dev, "spifi clock not found\n");
  331. return PTR_ERR(spifi->clk_spifi);
  332. }
  333. spifi->clk_reg = devm_clk_get(&pdev->dev, "reg");
  334. if (IS_ERR(spifi->clk_reg)) {
  335. dev_err(&pdev->dev, "reg clock not found\n");
  336. return PTR_ERR(spifi->clk_reg);
  337. }
  338. ret = clk_prepare_enable(spifi->clk_reg);
  339. if (ret) {
  340. dev_err(&pdev->dev, "unable to enable reg clock\n");
  341. return ret;
  342. }
  343. ret = clk_prepare_enable(spifi->clk_spifi);
  344. if (ret) {
  345. dev_err(&pdev->dev, "unable to enable spifi clock\n");
  346. goto dis_clk_reg;
  347. }
  348. spifi->dev = &pdev->dev;
  349. platform_set_drvdata(pdev, spifi);
  350. /* Initialize and reset device */
  351. nxp_spifi_reset(spifi);
  352. writel(0, spifi->io_base + SPIFI_IDATA);
  353. writel(0, spifi->io_base + SPIFI_MCMD);
  354. nxp_spifi_reset(spifi);
  355. flash_np = of_get_next_available_child(pdev->dev.of_node, NULL);
  356. if (!flash_np) {
  357. dev_err(&pdev->dev, "no SPI flash device to configure\n");
  358. ret = -ENODEV;
  359. goto dis_clks;
  360. }
  361. ret = nxp_spifi_setup_flash(spifi, flash_np);
  362. if (ret) {
  363. dev_err(&pdev->dev, "unable to setup flash chip\n");
  364. goto dis_clks;
  365. }
  366. return 0;
  367. dis_clks:
  368. clk_disable_unprepare(spifi->clk_spifi);
  369. dis_clk_reg:
  370. clk_disable_unprepare(spifi->clk_reg);
  371. return ret;
  372. }
  373. static int nxp_spifi_remove(struct platform_device *pdev)
  374. {
  375. struct nxp_spifi *spifi = platform_get_drvdata(pdev);
  376. mtd_device_unregister(&spifi->nor.mtd);
  377. clk_disable_unprepare(spifi->clk_spifi);
  378. clk_disable_unprepare(spifi->clk_reg);
  379. return 0;
  380. }
  381. static const struct of_device_id nxp_spifi_match[] = {
  382. {.compatible = "nxp,lpc1773-spifi"},
  383. { /* sentinel */ }
  384. };
  385. MODULE_DEVICE_TABLE(of, nxp_spifi_match);
  386. static struct platform_driver nxp_spifi_driver = {
  387. .probe = nxp_spifi_probe,
  388. .remove = nxp_spifi_remove,
  389. .driver = {
  390. .name = "nxp-spifi",
  391. .of_match_table = nxp_spifi_match,
  392. },
  393. };
  394. module_platform_driver(nxp_spifi_driver);
  395. MODULE_DESCRIPTION("NXP SPI Flash Interface driver");
  396. MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
  397. MODULE_LICENSE("GPL v2");