nxp-spifi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 mtd_info mtd;
  60. struct spi_nor nor;
  61. bool memory_mode;
  62. u32 mcmd;
  63. };
  64. static int nxp_spifi_wait_for_cmd(struct nxp_spifi *spifi)
  65. {
  66. u8 stat;
  67. int ret;
  68. ret = readb_poll_timeout(spifi->io_base + SPIFI_STAT, stat,
  69. !(stat & SPIFI_STAT_CMD), 10, 30);
  70. if (ret)
  71. dev_warn(spifi->dev, "command timed out\n");
  72. return ret;
  73. }
  74. static int nxp_spifi_reset(struct nxp_spifi *spifi)
  75. {
  76. u8 stat;
  77. int ret;
  78. writel(SPIFI_STAT_RESET, spifi->io_base + SPIFI_STAT);
  79. ret = readb_poll_timeout(spifi->io_base + SPIFI_STAT, stat,
  80. !(stat & SPIFI_STAT_RESET), 10, 30);
  81. if (ret)
  82. dev_warn(spifi->dev, "state reset timed out\n");
  83. return ret;
  84. }
  85. static int nxp_spifi_set_memory_mode_off(struct nxp_spifi *spifi)
  86. {
  87. int ret;
  88. if (!spifi->memory_mode)
  89. return 0;
  90. ret = nxp_spifi_reset(spifi);
  91. if (ret)
  92. dev_err(spifi->dev, "unable to enter command mode\n");
  93. else
  94. spifi->memory_mode = false;
  95. return ret;
  96. }
  97. static int nxp_spifi_set_memory_mode_on(struct nxp_spifi *spifi)
  98. {
  99. u8 stat;
  100. int ret;
  101. if (spifi->memory_mode)
  102. return 0;
  103. writel(spifi->mcmd, spifi->io_base + SPIFI_MCMD);
  104. ret = readb_poll_timeout(spifi->io_base + SPIFI_STAT, stat,
  105. stat & SPIFI_STAT_MCINIT, 10, 30);
  106. if (ret)
  107. dev_err(spifi->dev, "unable to enter memory mode\n");
  108. else
  109. spifi->memory_mode = true;
  110. return ret;
  111. }
  112. static int nxp_spifi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  113. {
  114. struct nxp_spifi *spifi = nor->priv;
  115. u32 cmd;
  116. int ret;
  117. ret = nxp_spifi_set_memory_mode_off(spifi);
  118. if (ret)
  119. return ret;
  120. cmd = SPIFI_CMD_DATALEN(len) |
  121. SPIFI_CMD_OPCODE(opcode) |
  122. SPIFI_CMD_FIELDFORM_ALL_SERIAL |
  123. SPIFI_CMD_FRAMEFORM_OPCODE_ONLY;
  124. writel(cmd, spifi->io_base + SPIFI_CMD);
  125. while (len--)
  126. *buf++ = readb(spifi->io_base + SPIFI_DATA);
  127. return nxp_spifi_wait_for_cmd(spifi);
  128. }
  129. static int nxp_spifi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf,
  130. int len, int write_enable)
  131. {
  132. struct nxp_spifi *spifi = nor->priv;
  133. u32 cmd;
  134. int ret;
  135. ret = nxp_spifi_set_memory_mode_off(spifi);
  136. if (ret)
  137. return ret;
  138. cmd = SPIFI_CMD_DOUT |
  139. SPIFI_CMD_DATALEN(len) |
  140. SPIFI_CMD_OPCODE(opcode) |
  141. SPIFI_CMD_FIELDFORM_ALL_SERIAL |
  142. SPIFI_CMD_FRAMEFORM_OPCODE_ONLY;
  143. writel(cmd, spifi->io_base + SPIFI_CMD);
  144. while (len--)
  145. writeb(*buf++, spifi->io_base + SPIFI_DATA);
  146. return nxp_spifi_wait_for_cmd(spifi);
  147. }
  148. static int nxp_spifi_read(struct spi_nor *nor, loff_t from, size_t len,
  149. size_t *retlen, u_char *buf)
  150. {
  151. struct nxp_spifi *spifi = nor->priv;
  152. int ret;
  153. ret = nxp_spifi_set_memory_mode_on(spifi);
  154. if (ret)
  155. return ret;
  156. memcpy_fromio(buf, spifi->flash_base + from, len);
  157. *retlen += len;
  158. return 0;
  159. }
  160. static void nxp_spifi_write(struct spi_nor *nor, loff_t to, size_t len,
  161. size_t *retlen, const u_char *buf)
  162. {
  163. struct nxp_spifi *spifi = nor->priv;
  164. u32 cmd;
  165. int ret;
  166. ret = nxp_spifi_set_memory_mode_off(spifi);
  167. if (ret)
  168. return;
  169. writel(to, spifi->io_base + SPIFI_ADDR);
  170. *retlen += len;
  171. cmd = SPIFI_CMD_DOUT |
  172. SPIFI_CMD_DATALEN(len) |
  173. SPIFI_CMD_FIELDFORM_ALL_SERIAL |
  174. SPIFI_CMD_OPCODE(nor->program_opcode) |
  175. SPIFI_CMD_FRAMEFORM(spifi->nor.addr_width + 1);
  176. writel(cmd, spifi->io_base + SPIFI_CMD);
  177. while (len--)
  178. writeb(*buf++, spifi->io_base + SPIFI_DATA);
  179. nxp_spifi_wait_for_cmd(spifi);
  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. struct mtd_part_parser_data ppdata;
  228. enum read_mode flash_read;
  229. u32 ctrl, property;
  230. u16 mode = 0;
  231. int ret;
  232. if (!of_property_read_u32(np, "spi-rx-bus-width", &property)) {
  233. switch (property) {
  234. case 1:
  235. break;
  236. case 2:
  237. mode |= SPI_RX_DUAL;
  238. break;
  239. case 4:
  240. mode |= SPI_RX_QUAD;
  241. break;
  242. default:
  243. dev_err(spifi->dev, "unsupported rx-bus-width\n");
  244. return -EINVAL;
  245. }
  246. }
  247. if (of_find_property(np, "spi-cpha", NULL))
  248. mode |= SPI_CPHA;
  249. if (of_find_property(np, "spi-cpol", NULL))
  250. mode |= SPI_CPOL;
  251. /* Setup control register defaults */
  252. ctrl = SPIFI_CTRL_TIMEOUT(1000) |
  253. SPIFI_CTRL_CSHIGH(15) |
  254. SPIFI_CTRL_FBCLK;
  255. if (mode & SPI_RX_DUAL) {
  256. ctrl |= SPIFI_CTRL_DUAL;
  257. flash_read = SPI_NOR_DUAL;
  258. } else if (mode & SPI_RX_QUAD) {
  259. ctrl &= ~SPIFI_CTRL_DUAL;
  260. flash_read = SPI_NOR_QUAD;
  261. } else {
  262. ctrl |= SPIFI_CTRL_DUAL;
  263. flash_read = SPI_NOR_NORMAL;
  264. }
  265. switch (mode & (SPI_CPHA | SPI_CPOL)) {
  266. case SPI_MODE_0:
  267. ctrl &= ~SPIFI_CTRL_MODE3;
  268. break;
  269. case SPI_MODE_3:
  270. ctrl |= SPIFI_CTRL_MODE3;
  271. break;
  272. default:
  273. dev_err(spifi->dev, "only mode 0 and 3 supported\n");
  274. return -EINVAL;
  275. }
  276. writel(ctrl, spifi->io_base + SPIFI_CTRL);
  277. spifi->mtd.priv = &spifi->nor;
  278. spifi->nor.mtd = &spifi->mtd;
  279. spifi->nor.dev = spifi->dev;
  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, flash_read);
  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. ppdata.of_node = np;
  307. ret = mtd_device_parse_register(&spifi->mtd, NULL, &ppdata, NULL, 0);
  308. if (ret) {
  309. dev_err(spifi->dev, "mtd device parse failed\n");
  310. return ret;
  311. }
  312. return 0;
  313. }
  314. static int nxp_spifi_probe(struct platform_device *pdev)
  315. {
  316. struct device_node *flash_np;
  317. struct nxp_spifi *spifi;
  318. struct resource *res;
  319. int ret;
  320. spifi = devm_kzalloc(&pdev->dev, sizeof(*spifi), GFP_KERNEL);
  321. if (!spifi)
  322. return -ENOMEM;
  323. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spifi");
  324. spifi->io_base = devm_ioremap_resource(&pdev->dev, res);
  325. if (IS_ERR(spifi->io_base))
  326. return PTR_ERR(spifi->io_base);
  327. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "flash");
  328. spifi->flash_base = devm_ioremap_resource(&pdev->dev, res);
  329. if (IS_ERR(spifi->flash_base))
  330. return PTR_ERR(spifi->flash_base);
  331. spifi->clk_spifi = devm_clk_get(&pdev->dev, "spifi");
  332. if (IS_ERR(spifi->clk_spifi)) {
  333. dev_err(&pdev->dev, "spifi clock not found\n");
  334. return PTR_ERR(spifi->clk_spifi);
  335. }
  336. spifi->clk_reg = devm_clk_get(&pdev->dev, "reg");
  337. if (IS_ERR(spifi->clk_reg)) {
  338. dev_err(&pdev->dev, "reg clock not found\n");
  339. return PTR_ERR(spifi->clk_reg);
  340. }
  341. ret = clk_prepare_enable(spifi->clk_reg);
  342. if (ret) {
  343. dev_err(&pdev->dev, "unable to enable reg clock\n");
  344. return ret;
  345. }
  346. ret = clk_prepare_enable(spifi->clk_spifi);
  347. if (ret) {
  348. dev_err(&pdev->dev, "unable to enable spifi clock\n");
  349. goto dis_clk_reg;
  350. }
  351. spifi->dev = &pdev->dev;
  352. platform_set_drvdata(pdev, spifi);
  353. /* Initialize and reset device */
  354. nxp_spifi_reset(spifi);
  355. writel(0, spifi->io_base + SPIFI_IDATA);
  356. writel(0, spifi->io_base + SPIFI_MCMD);
  357. nxp_spifi_reset(spifi);
  358. flash_np = of_get_next_available_child(pdev->dev.of_node, NULL);
  359. if (!flash_np) {
  360. dev_err(&pdev->dev, "no SPI flash device to configure\n");
  361. ret = -ENODEV;
  362. goto dis_clks;
  363. }
  364. ret = nxp_spifi_setup_flash(spifi, 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->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");