nxp-spifi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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.read_proto) {
  199. case SNOR_PROTO_1_1_1:
  200. spifi->mcmd = SPIFI_CMD_FIELDFORM_ALL_SERIAL;
  201. break;
  202. case SNOR_PROTO_1_1_2:
  203. case SNOR_PROTO_1_1_4:
  204. spifi->mcmd = SPIFI_CMD_FIELDFORM_QUAD_DUAL_DATA;
  205. break;
  206. default:
  207. dev_err(spifi->dev, "unsupported SPI read mode\n");
  208. return -EINVAL;
  209. }
  210. /* Memory mode supports address length between 1 and 4 */
  211. if (spifi->nor.addr_width < 1 || spifi->nor.addr_width > 4)
  212. return -EINVAL;
  213. spifi->mcmd |= SPIFI_CMD_OPCODE(spifi->nor.read_opcode) |
  214. SPIFI_CMD_INTLEN(spifi->nor.read_dummy / 8) |
  215. SPIFI_CMD_FRAMEFORM(spifi->nor.addr_width + 1);
  216. return 0;
  217. }
  218. static void nxp_spifi_dummy_id_read(struct spi_nor *nor)
  219. {
  220. u8 id[SPI_NOR_MAX_ID_LEN];
  221. nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
  222. }
  223. static int nxp_spifi_setup_flash(struct nxp_spifi *spifi,
  224. struct device_node *np)
  225. {
  226. struct spi_nor_hwcaps hwcaps = {
  227. .mask = SNOR_HWCAPS_READ |
  228. SNOR_HWCAPS_READ_FAST |
  229. SNOR_HWCAPS_PP,
  230. };
  231. u32 ctrl, property;
  232. u16 mode = 0;
  233. int ret;
  234. if (!of_property_read_u32(np, "spi-rx-bus-width", &property)) {
  235. switch (property) {
  236. case 1:
  237. break;
  238. case 2:
  239. mode |= SPI_RX_DUAL;
  240. break;
  241. case 4:
  242. mode |= SPI_RX_QUAD;
  243. break;
  244. default:
  245. dev_err(spifi->dev, "unsupported rx-bus-width\n");
  246. return -EINVAL;
  247. }
  248. }
  249. if (of_find_property(np, "spi-cpha", NULL))
  250. mode |= SPI_CPHA;
  251. if (of_find_property(np, "spi-cpol", NULL))
  252. mode |= SPI_CPOL;
  253. /* Setup control register defaults */
  254. ctrl = SPIFI_CTRL_TIMEOUT(1000) |
  255. SPIFI_CTRL_CSHIGH(15) |
  256. SPIFI_CTRL_FBCLK;
  257. if (mode & SPI_RX_DUAL) {
  258. ctrl |= SPIFI_CTRL_DUAL;
  259. hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
  260. } else if (mode & SPI_RX_QUAD) {
  261. ctrl &= ~SPIFI_CTRL_DUAL;
  262. hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
  263. } else {
  264. ctrl |= SPIFI_CTRL_DUAL;
  265. }
  266. switch (mode & (SPI_CPHA | SPI_CPOL)) {
  267. case SPI_MODE_0:
  268. ctrl &= ~SPIFI_CTRL_MODE3;
  269. break;
  270. case SPI_MODE_3:
  271. ctrl |= SPIFI_CTRL_MODE3;
  272. break;
  273. default:
  274. dev_err(spifi->dev, "only mode 0 and 3 supported\n");
  275. return -EINVAL;
  276. }
  277. writel(ctrl, spifi->io_base + SPIFI_CTRL);
  278. spifi->nor.dev = spifi->dev;
  279. spi_nor_set_flash_node(&spifi->nor, np);
  280. spifi->nor.priv = spifi;
  281. spifi->nor.read = nxp_spifi_read;
  282. spifi->nor.write = nxp_spifi_write;
  283. spifi->nor.erase = nxp_spifi_erase;
  284. spifi->nor.read_reg = nxp_spifi_read_reg;
  285. spifi->nor.write_reg = nxp_spifi_write_reg;
  286. /*
  287. * The first read on a hard reset isn't reliable so do a
  288. * dummy read of the id before calling spi_nor_scan().
  289. * The reason for this problem is unknown.
  290. *
  291. * The official NXP spifilib uses more or less the same
  292. * workaround that is applied here by reading the device
  293. * id multiple times.
  294. */
  295. nxp_spifi_dummy_id_read(&spifi->nor);
  296. ret = spi_nor_scan(&spifi->nor, NULL, &hwcaps);
  297. if (ret) {
  298. dev_err(spifi->dev, "device scan failed\n");
  299. return ret;
  300. }
  301. ret = nxp_spifi_setup_memory_cmd(spifi);
  302. if (ret) {
  303. dev_err(spifi->dev, "memory command setup failed\n");
  304. return ret;
  305. }
  306. ret = mtd_device_register(&spifi->nor.mtd, NULL, 0);
  307. if (ret) {
  308. dev_err(spifi->dev, "mtd device parse failed\n");
  309. return ret;
  310. }
  311. return 0;
  312. }
  313. static int nxp_spifi_probe(struct platform_device *pdev)
  314. {
  315. struct device_node *flash_np;
  316. struct nxp_spifi *spifi;
  317. struct resource *res;
  318. int ret;
  319. spifi = devm_kzalloc(&pdev->dev, sizeof(*spifi), GFP_KERNEL);
  320. if (!spifi)
  321. return -ENOMEM;
  322. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spifi");
  323. spifi->io_base = devm_ioremap_resource(&pdev->dev, res);
  324. if (IS_ERR(spifi->io_base))
  325. return PTR_ERR(spifi->io_base);
  326. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "flash");
  327. spifi->flash_base = devm_ioremap_resource(&pdev->dev, res);
  328. if (IS_ERR(spifi->flash_base))
  329. return PTR_ERR(spifi->flash_base);
  330. spifi->clk_spifi = devm_clk_get(&pdev->dev, "spifi");
  331. if (IS_ERR(spifi->clk_spifi)) {
  332. dev_err(&pdev->dev, "spifi clock not found\n");
  333. return PTR_ERR(spifi->clk_spifi);
  334. }
  335. spifi->clk_reg = devm_clk_get(&pdev->dev, "reg");
  336. if (IS_ERR(spifi->clk_reg)) {
  337. dev_err(&pdev->dev, "reg clock not found\n");
  338. return PTR_ERR(spifi->clk_reg);
  339. }
  340. ret = clk_prepare_enable(spifi->clk_reg);
  341. if (ret) {
  342. dev_err(&pdev->dev, "unable to enable reg clock\n");
  343. return ret;
  344. }
  345. ret = clk_prepare_enable(spifi->clk_spifi);
  346. if (ret) {
  347. dev_err(&pdev->dev, "unable to enable spifi clock\n");
  348. goto dis_clk_reg;
  349. }
  350. spifi->dev = &pdev->dev;
  351. platform_set_drvdata(pdev, spifi);
  352. /* Initialize and reset device */
  353. nxp_spifi_reset(spifi);
  354. writel(0, spifi->io_base + SPIFI_IDATA);
  355. writel(0, spifi->io_base + SPIFI_MCMD);
  356. nxp_spifi_reset(spifi);
  357. flash_np = of_get_next_available_child(pdev->dev.of_node, NULL);
  358. if (!flash_np) {
  359. dev_err(&pdev->dev, "no SPI flash device to configure\n");
  360. ret = -ENODEV;
  361. goto dis_clks;
  362. }
  363. ret = nxp_spifi_setup_flash(spifi, flash_np);
  364. of_node_put(flash_np);
  365. if (ret) {
  366. dev_err(&pdev->dev, "unable to setup flash chip\n");
  367. goto dis_clks;
  368. }
  369. return 0;
  370. dis_clks:
  371. clk_disable_unprepare(spifi->clk_spifi);
  372. dis_clk_reg:
  373. clk_disable_unprepare(spifi->clk_reg);
  374. return ret;
  375. }
  376. static int nxp_spifi_remove(struct platform_device *pdev)
  377. {
  378. struct nxp_spifi *spifi = platform_get_drvdata(pdev);
  379. mtd_device_unregister(&spifi->nor.mtd);
  380. clk_disable_unprepare(spifi->clk_spifi);
  381. clk_disable_unprepare(spifi->clk_reg);
  382. return 0;
  383. }
  384. static const struct of_device_id nxp_spifi_match[] = {
  385. {.compatible = "nxp,lpc1773-spifi"},
  386. { /* sentinel */ }
  387. };
  388. MODULE_DEVICE_TABLE(of, nxp_spifi_match);
  389. static struct platform_driver nxp_spifi_driver = {
  390. .probe = nxp_spifi_probe,
  391. .remove = nxp_spifi_remove,
  392. .driver = {
  393. .name = "nxp-spifi",
  394. .of_match_table = nxp_spifi_match,
  395. },
  396. };
  397. module_platform_driver(nxp_spifi_driver);
  398. MODULE_DESCRIPTION("NXP SPI Flash Interface driver");
  399. MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
  400. MODULE_LICENSE("GPL v2");