mtk-quadspi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. * Author: Bayi Cheng <bayi.cheng@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <linux/iopoll.h>
  20. #include <linux/ioport.h>
  21. #include <linux/math64.h>
  22. #include <linux/module.h>
  23. #include <linux/mutex.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/pinctrl/consumer.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/mtd/spi-nor.h>
  32. #define MTK_NOR_CMD_REG 0x00
  33. #define MTK_NOR_CNT_REG 0x04
  34. #define MTK_NOR_RDSR_REG 0x08
  35. #define MTK_NOR_RDATA_REG 0x0c
  36. #define MTK_NOR_RADR0_REG 0x10
  37. #define MTK_NOR_RADR1_REG 0x14
  38. #define MTK_NOR_RADR2_REG 0x18
  39. #define MTK_NOR_WDATA_REG 0x1c
  40. #define MTK_NOR_PRGDATA0_REG 0x20
  41. #define MTK_NOR_PRGDATA1_REG 0x24
  42. #define MTK_NOR_PRGDATA2_REG 0x28
  43. #define MTK_NOR_PRGDATA3_REG 0x2c
  44. #define MTK_NOR_PRGDATA4_REG 0x30
  45. #define MTK_NOR_PRGDATA5_REG 0x34
  46. #define MTK_NOR_SHREG0_REG 0x38
  47. #define MTK_NOR_SHREG1_REG 0x3c
  48. #define MTK_NOR_SHREG2_REG 0x40
  49. #define MTK_NOR_SHREG3_REG 0x44
  50. #define MTK_NOR_SHREG4_REG 0x48
  51. #define MTK_NOR_SHREG5_REG 0x4c
  52. #define MTK_NOR_SHREG6_REG 0x50
  53. #define MTK_NOR_SHREG7_REG 0x54
  54. #define MTK_NOR_SHREG8_REG 0x58
  55. #define MTK_NOR_SHREG9_REG 0x5c
  56. #define MTK_NOR_CFG1_REG 0x60
  57. #define MTK_NOR_CFG2_REG 0x64
  58. #define MTK_NOR_CFG3_REG 0x68
  59. #define MTK_NOR_STATUS0_REG 0x70
  60. #define MTK_NOR_STATUS1_REG 0x74
  61. #define MTK_NOR_STATUS2_REG 0x78
  62. #define MTK_NOR_STATUS3_REG 0x7c
  63. #define MTK_NOR_FLHCFG_REG 0x84
  64. #define MTK_NOR_TIME_REG 0x94
  65. #define MTK_NOR_PP_DATA_REG 0x98
  66. #define MTK_NOR_PREBUF_STUS_REG 0x9c
  67. #define MTK_NOR_DELSEL0_REG 0xa0
  68. #define MTK_NOR_DELSEL1_REG 0xa4
  69. #define MTK_NOR_INTRSTUS_REG 0xa8
  70. #define MTK_NOR_INTREN_REG 0xac
  71. #define MTK_NOR_CHKSUM_CTL_REG 0xb8
  72. #define MTK_NOR_CHKSUM_REG 0xbc
  73. #define MTK_NOR_CMD2_REG 0xc0
  74. #define MTK_NOR_WRPROT_REG 0xc4
  75. #define MTK_NOR_RADR3_REG 0xc8
  76. #define MTK_NOR_DUAL_REG 0xcc
  77. #define MTK_NOR_DELSEL2_REG 0xd0
  78. #define MTK_NOR_DELSEL3_REG 0xd4
  79. #define MTK_NOR_DELSEL4_REG 0xd8
  80. /* commands for mtk nor controller */
  81. #define MTK_NOR_READ_CMD 0x0
  82. #define MTK_NOR_RDSR_CMD 0x2
  83. #define MTK_NOR_PRG_CMD 0x4
  84. #define MTK_NOR_WR_CMD 0x10
  85. #define MTK_NOR_PIO_WR_CMD 0x90
  86. #define MTK_NOR_WRSR_CMD 0x20
  87. #define MTK_NOR_PIO_READ_CMD 0x81
  88. #define MTK_NOR_WR_BUF_ENABLE 0x1
  89. #define MTK_NOR_WR_BUF_DISABLE 0x0
  90. #define MTK_NOR_ENABLE_SF_CMD 0x30
  91. #define MTK_NOR_DUAD_ADDR_EN 0x8
  92. #define MTK_NOR_QUAD_READ_EN 0x4
  93. #define MTK_NOR_DUAL_ADDR_EN 0x2
  94. #define MTK_NOR_DUAL_READ_EN 0x1
  95. #define MTK_NOR_DUAL_DISABLE 0x0
  96. #define MTK_NOR_FAST_READ 0x1
  97. #define SFLASH_WRBUF_SIZE 128
  98. /* Can shift up to 48 bits (6 bytes) of TX/RX */
  99. #define MTK_NOR_MAX_RX_TX_SHIFT 6
  100. /* can shift up to 56 bits (7 bytes) transfer by MTK_NOR_PRG_CMD */
  101. #define MTK_NOR_MAX_SHIFT 7
  102. /* nor controller 4-byte address mode enable bit */
  103. #define MTK_NOR_4B_ADDR_EN BIT(4)
  104. /* Helpers for accessing the program data / shift data registers */
  105. #define MTK_NOR_PRG_REG(n) (MTK_NOR_PRGDATA0_REG + 4 * (n))
  106. #define MTK_NOR_SHREG(n) (MTK_NOR_SHREG0_REG + 4 * (n))
  107. struct mt8173_nor {
  108. struct spi_nor nor;
  109. struct device *dev;
  110. void __iomem *base; /* nor flash base address */
  111. struct clk *spi_clk;
  112. struct clk *nor_clk;
  113. };
  114. static void mt8173_nor_set_read_mode(struct mt8173_nor *mt8173_nor)
  115. {
  116. struct spi_nor *nor = &mt8173_nor->nor;
  117. switch (nor->flash_read) {
  118. case SPI_NOR_FAST:
  119. writeb(nor->read_opcode, mt8173_nor->base +
  120. MTK_NOR_PRGDATA3_REG);
  121. writeb(MTK_NOR_FAST_READ, mt8173_nor->base +
  122. MTK_NOR_CFG1_REG);
  123. break;
  124. case SPI_NOR_DUAL:
  125. writeb(nor->read_opcode, mt8173_nor->base +
  126. MTK_NOR_PRGDATA3_REG);
  127. writeb(MTK_NOR_DUAL_READ_EN, mt8173_nor->base +
  128. MTK_NOR_DUAL_REG);
  129. break;
  130. case SPI_NOR_QUAD:
  131. writeb(nor->read_opcode, mt8173_nor->base +
  132. MTK_NOR_PRGDATA4_REG);
  133. writeb(MTK_NOR_QUAD_READ_EN, mt8173_nor->base +
  134. MTK_NOR_DUAL_REG);
  135. break;
  136. default:
  137. writeb(MTK_NOR_DUAL_DISABLE, mt8173_nor->base +
  138. MTK_NOR_DUAL_REG);
  139. break;
  140. }
  141. }
  142. static int mt8173_nor_execute_cmd(struct mt8173_nor *mt8173_nor, u8 cmdval)
  143. {
  144. int reg;
  145. u8 val = cmdval & 0x1f;
  146. writeb(cmdval, mt8173_nor->base + MTK_NOR_CMD_REG);
  147. return readl_poll_timeout(mt8173_nor->base + MTK_NOR_CMD_REG, reg,
  148. !(reg & val), 100, 10000);
  149. }
  150. static int mt8173_nor_do_tx_rx(struct mt8173_nor *mt8173_nor, u8 op,
  151. u8 *tx, int txlen, u8 *rx, int rxlen)
  152. {
  153. int len = 1 + txlen + rxlen;
  154. int i, ret, idx;
  155. if (len > MTK_NOR_MAX_SHIFT)
  156. return -EINVAL;
  157. writeb(len * 8, mt8173_nor->base + MTK_NOR_CNT_REG);
  158. /* start at PRGDATA5, go down to PRGDATA0 */
  159. idx = MTK_NOR_MAX_RX_TX_SHIFT - 1;
  160. /* opcode */
  161. writeb(op, mt8173_nor->base + MTK_NOR_PRG_REG(idx));
  162. idx--;
  163. /* program TX data */
  164. for (i = 0; i < txlen; i++, idx--)
  165. writeb(tx[i], mt8173_nor->base + MTK_NOR_PRG_REG(idx));
  166. /* clear out rest of TX registers */
  167. while (idx >= 0) {
  168. writeb(0, mt8173_nor->base + MTK_NOR_PRG_REG(idx));
  169. idx--;
  170. }
  171. ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PRG_CMD);
  172. if (ret)
  173. return ret;
  174. /* restart at first RX byte */
  175. idx = rxlen - 1;
  176. /* read out RX data */
  177. for (i = 0; i < rxlen; i++, idx--)
  178. rx[i] = readb(mt8173_nor->base + MTK_NOR_SHREG(idx));
  179. return 0;
  180. }
  181. /* Do a WRSR (Write Status Register) command */
  182. static int mt8173_nor_wr_sr(struct mt8173_nor *mt8173_nor, u8 sr)
  183. {
  184. writeb(sr, mt8173_nor->base + MTK_NOR_PRGDATA5_REG);
  185. writeb(8, mt8173_nor->base + MTK_NOR_CNT_REG);
  186. return mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_WRSR_CMD);
  187. }
  188. static int mt8173_nor_write_buffer_enable(struct mt8173_nor *mt8173_nor)
  189. {
  190. u8 reg;
  191. /* the bit0 of MTK_NOR_CFG2_REG is pre-fetch buffer
  192. * 0: pre-fetch buffer use for read
  193. * 1: pre-fetch buffer use for page program
  194. */
  195. writel(MTK_NOR_WR_BUF_ENABLE, mt8173_nor->base + MTK_NOR_CFG2_REG);
  196. return readb_poll_timeout(mt8173_nor->base + MTK_NOR_CFG2_REG, reg,
  197. 0x01 == (reg & 0x01), 100, 10000);
  198. }
  199. static int mt8173_nor_write_buffer_disable(struct mt8173_nor *mt8173_nor)
  200. {
  201. u8 reg;
  202. writel(MTK_NOR_WR_BUF_DISABLE, mt8173_nor->base + MTK_NOR_CFG2_REG);
  203. return readb_poll_timeout(mt8173_nor->base + MTK_NOR_CFG2_REG, reg,
  204. MTK_NOR_WR_BUF_DISABLE == (reg & 0x1), 100,
  205. 10000);
  206. }
  207. static void mt8173_nor_set_addr_width(struct mt8173_nor *mt8173_nor)
  208. {
  209. u8 val;
  210. struct spi_nor *nor = &mt8173_nor->nor;
  211. val = readb(mt8173_nor->base + MTK_NOR_DUAL_REG);
  212. switch (nor->addr_width) {
  213. case 3:
  214. val &= ~MTK_NOR_4B_ADDR_EN;
  215. break;
  216. case 4:
  217. val |= MTK_NOR_4B_ADDR_EN;
  218. break;
  219. default:
  220. dev_warn(mt8173_nor->dev, "Unexpected address width %u.\n",
  221. nor->addr_width);
  222. break;
  223. }
  224. writeb(val, mt8173_nor->base + MTK_NOR_DUAL_REG);
  225. }
  226. static void mt8173_nor_set_addr(struct mt8173_nor *mt8173_nor, u32 addr)
  227. {
  228. int i;
  229. mt8173_nor_set_addr_width(mt8173_nor);
  230. for (i = 0; i < 3; i++) {
  231. writeb(addr & 0xff, mt8173_nor->base + MTK_NOR_RADR0_REG + i * 4);
  232. addr >>= 8;
  233. }
  234. /* Last register is non-contiguous */
  235. writeb(addr & 0xff, mt8173_nor->base + MTK_NOR_RADR3_REG);
  236. }
  237. static ssize_t mt8173_nor_read(struct spi_nor *nor, loff_t from, size_t length,
  238. u_char *buffer)
  239. {
  240. int i, ret;
  241. int addr = (int)from;
  242. u8 *buf = (u8 *)buffer;
  243. struct mt8173_nor *mt8173_nor = nor->priv;
  244. /* set mode for fast read mode ,dual mode or quad mode */
  245. mt8173_nor_set_read_mode(mt8173_nor);
  246. mt8173_nor_set_addr(mt8173_nor, addr);
  247. for (i = 0; i < length; i++) {
  248. ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PIO_READ_CMD);
  249. if (ret < 0)
  250. return ret;
  251. buf[i] = readb(mt8173_nor->base + MTK_NOR_RDATA_REG);
  252. }
  253. return length;
  254. }
  255. static int mt8173_nor_write_single_byte(struct mt8173_nor *mt8173_nor,
  256. int addr, int length, u8 *data)
  257. {
  258. int i, ret;
  259. mt8173_nor_set_addr(mt8173_nor, addr);
  260. for (i = 0; i < length; i++) {
  261. writeb(*data++, mt8173_nor->base + MTK_NOR_WDATA_REG);
  262. ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PIO_WR_CMD);
  263. if (ret < 0)
  264. return ret;
  265. }
  266. return 0;
  267. }
  268. static int mt8173_nor_write_buffer(struct mt8173_nor *mt8173_nor, int addr,
  269. const u8 *buf)
  270. {
  271. int i, bufidx, data;
  272. mt8173_nor_set_addr(mt8173_nor, addr);
  273. bufidx = 0;
  274. for (i = 0; i < SFLASH_WRBUF_SIZE; i += 4) {
  275. data = buf[bufidx + 3]<<24 | buf[bufidx + 2]<<16 |
  276. buf[bufidx + 1]<<8 | buf[bufidx];
  277. bufidx += 4;
  278. writel(data, mt8173_nor->base + MTK_NOR_PP_DATA_REG);
  279. }
  280. return mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_WR_CMD);
  281. }
  282. static ssize_t mt8173_nor_write(struct spi_nor *nor, loff_t to, size_t len,
  283. const u_char *buf)
  284. {
  285. int ret;
  286. struct mt8173_nor *mt8173_nor = nor->priv;
  287. size_t i;
  288. ret = mt8173_nor_write_buffer_enable(mt8173_nor);
  289. if (ret < 0) {
  290. dev_warn(mt8173_nor->dev, "write buffer enable failed!\n");
  291. return ret;
  292. }
  293. for (i = 0; i + SFLASH_WRBUF_SIZE <= len; i += SFLASH_WRBUF_SIZE) {
  294. ret = mt8173_nor_write_buffer(mt8173_nor, to, buf);
  295. if (ret < 0) {
  296. dev_err(mt8173_nor->dev, "write buffer failed!\n");
  297. return ret;
  298. }
  299. to += SFLASH_WRBUF_SIZE;
  300. buf += SFLASH_WRBUF_SIZE;
  301. }
  302. ret = mt8173_nor_write_buffer_disable(mt8173_nor);
  303. if (ret < 0) {
  304. dev_warn(mt8173_nor->dev, "write buffer disable failed!\n");
  305. return ret;
  306. }
  307. if (i < len) {
  308. ret = mt8173_nor_write_single_byte(mt8173_nor, to,
  309. (int)(len - i), (u8 *)buf);
  310. if (ret < 0) {
  311. dev_err(mt8173_nor->dev, "write single byte failed!\n");
  312. return ret;
  313. }
  314. }
  315. return len;
  316. }
  317. static int mt8173_nor_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  318. {
  319. int ret;
  320. struct mt8173_nor *mt8173_nor = nor->priv;
  321. switch (opcode) {
  322. case SPINOR_OP_RDSR:
  323. ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_RDSR_CMD);
  324. if (ret < 0)
  325. return ret;
  326. if (len == 1)
  327. *buf = readb(mt8173_nor->base + MTK_NOR_RDSR_REG);
  328. else
  329. dev_err(mt8173_nor->dev, "len should be 1 for read status!\n");
  330. break;
  331. default:
  332. ret = mt8173_nor_do_tx_rx(mt8173_nor, opcode, NULL, 0, buf, len);
  333. break;
  334. }
  335. return ret;
  336. }
  337. static int mt8173_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf,
  338. int len)
  339. {
  340. int ret;
  341. struct mt8173_nor *mt8173_nor = nor->priv;
  342. switch (opcode) {
  343. case SPINOR_OP_WRSR:
  344. /* We only handle 1 byte */
  345. ret = mt8173_nor_wr_sr(mt8173_nor, *buf);
  346. break;
  347. default:
  348. ret = mt8173_nor_do_tx_rx(mt8173_nor, opcode, buf, len, NULL, 0);
  349. if (ret)
  350. dev_warn(mt8173_nor->dev, "write reg failure!\n");
  351. break;
  352. }
  353. return ret;
  354. }
  355. static int mtk_nor_init(struct mt8173_nor *mt8173_nor,
  356. struct device_node *flash_node)
  357. {
  358. int ret;
  359. struct spi_nor *nor;
  360. /* initialize controller to accept commands */
  361. writel(MTK_NOR_ENABLE_SF_CMD, mt8173_nor->base + MTK_NOR_WRPROT_REG);
  362. nor = &mt8173_nor->nor;
  363. nor->dev = mt8173_nor->dev;
  364. nor->priv = mt8173_nor;
  365. spi_nor_set_flash_node(nor, flash_node);
  366. /* fill the hooks to spi nor */
  367. nor->read = mt8173_nor_read;
  368. nor->read_reg = mt8173_nor_read_reg;
  369. nor->write = mt8173_nor_write;
  370. nor->write_reg = mt8173_nor_write_reg;
  371. nor->mtd.name = "mtk_nor";
  372. /* initialized with NULL */
  373. ret = spi_nor_scan(nor, NULL, SPI_NOR_DUAL);
  374. if (ret)
  375. return ret;
  376. return mtd_device_register(&nor->mtd, NULL, 0);
  377. }
  378. static int mtk_nor_drv_probe(struct platform_device *pdev)
  379. {
  380. struct device_node *flash_np;
  381. struct resource *res;
  382. int ret;
  383. struct mt8173_nor *mt8173_nor;
  384. if (!pdev->dev.of_node) {
  385. dev_err(&pdev->dev, "No DT found\n");
  386. return -EINVAL;
  387. }
  388. mt8173_nor = devm_kzalloc(&pdev->dev, sizeof(*mt8173_nor), GFP_KERNEL);
  389. if (!mt8173_nor)
  390. return -ENOMEM;
  391. platform_set_drvdata(pdev, mt8173_nor);
  392. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  393. mt8173_nor->base = devm_ioremap_resource(&pdev->dev, res);
  394. if (IS_ERR(mt8173_nor->base))
  395. return PTR_ERR(mt8173_nor->base);
  396. mt8173_nor->spi_clk = devm_clk_get(&pdev->dev, "spi");
  397. if (IS_ERR(mt8173_nor->spi_clk))
  398. return PTR_ERR(mt8173_nor->spi_clk);
  399. mt8173_nor->nor_clk = devm_clk_get(&pdev->dev, "sf");
  400. if (IS_ERR(mt8173_nor->nor_clk))
  401. return PTR_ERR(mt8173_nor->nor_clk);
  402. mt8173_nor->dev = &pdev->dev;
  403. ret = clk_prepare_enable(mt8173_nor->spi_clk);
  404. if (ret)
  405. return ret;
  406. ret = clk_prepare_enable(mt8173_nor->nor_clk);
  407. if (ret) {
  408. clk_disable_unprepare(mt8173_nor->spi_clk);
  409. return ret;
  410. }
  411. /* only support one attached flash */
  412. flash_np = of_get_next_available_child(pdev->dev.of_node, NULL);
  413. if (!flash_np) {
  414. dev_err(&pdev->dev, "no SPI flash device to configure\n");
  415. ret = -ENODEV;
  416. goto nor_free;
  417. }
  418. ret = mtk_nor_init(mt8173_nor, flash_np);
  419. nor_free:
  420. if (ret) {
  421. clk_disable_unprepare(mt8173_nor->spi_clk);
  422. clk_disable_unprepare(mt8173_nor->nor_clk);
  423. }
  424. return ret;
  425. }
  426. static int mtk_nor_drv_remove(struct platform_device *pdev)
  427. {
  428. struct mt8173_nor *mt8173_nor = platform_get_drvdata(pdev);
  429. clk_disable_unprepare(mt8173_nor->spi_clk);
  430. clk_disable_unprepare(mt8173_nor->nor_clk);
  431. return 0;
  432. }
  433. static const struct of_device_id mtk_nor_of_ids[] = {
  434. { .compatible = "mediatek,mt8173-nor"},
  435. { /* sentinel */ }
  436. };
  437. MODULE_DEVICE_TABLE(of, mtk_nor_of_ids);
  438. static struct platform_driver mtk_nor_driver = {
  439. .probe = mtk_nor_drv_probe,
  440. .remove = mtk_nor_drv_remove,
  441. .driver = {
  442. .name = "mtk-nor",
  443. .of_match_table = mtk_nor_of_ids,
  444. },
  445. };
  446. module_platform_driver(mtk_nor_driver);
  447. MODULE_LICENSE("GPL v2");
  448. MODULE_DESCRIPTION("MediaTek SPI NOR Flash Driver");