mtk-quadspi.c 14 KB

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