nxp-spifi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 int nxp_spifi_read(struct spi_nor *nor, loff_t from, size_t len,
  147. size_t *retlen, 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. *retlen += len;
  156. return 0;
  157. }
  158. static void nxp_spifi_write(struct spi_nor *nor, loff_t to, size_t len,
  159. size_t *retlen, const u_char *buf)
  160. {
  161. struct nxp_spifi *spifi = nor->priv;
  162. u32 cmd;
  163. int ret;
  164. ret = nxp_spifi_set_memory_mode_off(spifi);
  165. if (ret)
  166. return;
  167. writel(to, spifi->io_base + SPIFI_ADDR);
  168. *retlen += len;
  169. cmd = SPIFI_CMD_DOUT |
  170. SPIFI_CMD_DATALEN(len) |
  171. SPIFI_CMD_FIELDFORM_ALL_SERIAL |
  172. SPIFI_CMD_OPCODE(nor->program_opcode) |
  173. SPIFI_CMD_FRAMEFORM(spifi->nor.addr_width + 1);
  174. writel(cmd, spifi->io_base + SPIFI_CMD);
  175. while (len--)
  176. writeb(*buf++, spifi->io_base + SPIFI_DATA);
  177. nxp_spifi_wait_for_cmd(spifi);
  178. }
  179. static int nxp_spifi_erase(struct spi_nor *nor, loff_t offs)
  180. {
  181. struct nxp_spifi *spifi = nor->priv;
  182. u32 cmd;
  183. int ret;
  184. ret = nxp_spifi_set_memory_mode_off(spifi);
  185. if (ret)
  186. return ret;
  187. writel(offs, spifi->io_base + SPIFI_ADDR);
  188. cmd = SPIFI_CMD_FIELDFORM_ALL_SERIAL |
  189. SPIFI_CMD_OPCODE(nor->erase_opcode) |
  190. SPIFI_CMD_FRAMEFORM(spifi->nor.addr_width + 1);
  191. writel(cmd, spifi->io_base + SPIFI_CMD);
  192. return nxp_spifi_wait_for_cmd(spifi);
  193. }
  194. static int nxp_spifi_setup_memory_cmd(struct nxp_spifi *spifi)
  195. {
  196. switch (spifi->nor.flash_read) {
  197. case SPI_NOR_NORMAL:
  198. case SPI_NOR_FAST:
  199. spifi->mcmd = SPIFI_CMD_FIELDFORM_ALL_SERIAL;
  200. break;
  201. case SPI_NOR_DUAL:
  202. case SPI_NOR_QUAD:
  203. spifi->mcmd = SPIFI_CMD_FIELDFORM_QUAD_DUAL_DATA;
  204. break;
  205. default:
  206. dev_err(spifi->dev, "unsupported SPI read mode\n");
  207. return -EINVAL;
  208. }
  209. /* Memory mode supports address length between 1 and 4 */
  210. if (spifi->nor.addr_width < 1 || spifi->nor.addr_width > 4)
  211. return -EINVAL;
  212. spifi->mcmd |= SPIFI_CMD_OPCODE(spifi->nor.read_opcode) |
  213. SPIFI_CMD_INTLEN(spifi->nor.read_dummy / 8) |
  214. SPIFI_CMD_FRAMEFORM(spifi->nor.addr_width + 1);
  215. return 0;
  216. }
  217. static void nxp_spifi_dummy_id_read(struct spi_nor *nor)
  218. {
  219. u8 id[SPI_NOR_MAX_ID_LEN];
  220. nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
  221. }
  222. static int nxp_spifi_setup_flash(struct nxp_spifi *spifi,
  223. struct device_node *np)
  224. {
  225. struct mtd_part_parser_data ppdata;
  226. enum read_mode flash_read;
  227. u32 ctrl, property;
  228. u16 mode = 0;
  229. int ret;
  230. if (!of_property_read_u32(np, "spi-rx-bus-width", &property)) {
  231. switch (property) {
  232. case 1:
  233. break;
  234. case 2:
  235. mode |= SPI_RX_DUAL;
  236. break;
  237. case 4:
  238. mode |= SPI_RX_QUAD;
  239. break;
  240. default:
  241. dev_err(spifi->dev, "unsupported rx-bus-width\n");
  242. return -EINVAL;
  243. }
  244. }
  245. if (of_find_property(np, "spi-cpha", NULL))
  246. mode |= SPI_CPHA;
  247. if (of_find_property(np, "spi-cpol", NULL))
  248. mode |= SPI_CPOL;
  249. /* Setup control register defaults */
  250. ctrl = SPIFI_CTRL_TIMEOUT(1000) |
  251. SPIFI_CTRL_CSHIGH(15) |
  252. SPIFI_CTRL_FBCLK;
  253. if (mode & SPI_RX_DUAL) {
  254. ctrl |= SPIFI_CTRL_DUAL;
  255. flash_read = SPI_NOR_DUAL;
  256. } else if (mode & SPI_RX_QUAD) {
  257. ctrl &= ~SPIFI_CTRL_DUAL;
  258. flash_read = SPI_NOR_QUAD;
  259. } else {
  260. ctrl |= SPIFI_CTRL_DUAL;
  261. flash_read = SPI_NOR_NORMAL;
  262. }
  263. switch (mode & (SPI_CPHA | SPI_CPOL)) {
  264. case SPI_MODE_0:
  265. ctrl &= ~SPIFI_CTRL_MODE3;
  266. break;
  267. case SPI_MODE_3:
  268. ctrl |= SPIFI_CTRL_MODE3;
  269. break;
  270. default:
  271. dev_err(spifi->dev, "only mode 0 and 3 supported\n");
  272. return -EINVAL;
  273. }
  274. writel(ctrl, spifi->io_base + SPIFI_CTRL);
  275. spifi->nor.dev = spifi->dev;
  276. spifi->nor.flash_node = np;
  277. spifi->nor.priv = spifi;
  278. spifi->nor.read = nxp_spifi_read;
  279. spifi->nor.write = nxp_spifi_write;
  280. spifi->nor.erase = nxp_spifi_erase;
  281. spifi->nor.read_reg = nxp_spifi_read_reg;
  282. spifi->nor.write_reg = nxp_spifi_write_reg;
  283. /*
  284. * The first read on a hard reset isn't reliable so do a
  285. * dummy read of the id before calling spi_nor_scan().
  286. * The reason for this problem is unknown.
  287. *
  288. * The official NXP spifilib uses more or less the same
  289. * workaround that is applied here by reading the device
  290. * id multiple times.
  291. */
  292. nxp_spifi_dummy_id_read(&spifi->nor);
  293. ret = spi_nor_scan(&spifi->nor, NULL, flash_read);
  294. if (ret) {
  295. dev_err(spifi->dev, "device scan failed\n");
  296. return ret;
  297. }
  298. ret = nxp_spifi_setup_memory_cmd(spifi);
  299. if (ret) {
  300. dev_err(spifi->dev, "memory command setup failed\n");
  301. return ret;
  302. }
  303. ppdata.of_node = np;
  304. ret = mtd_device_parse_register(&spifi->nor.mtd, NULL, &ppdata, 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");