mtk-quadspi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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/mtd/mtd.h>
  24. #include <linux/mutex.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/pinctrl/consumer.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/slab.h>
  30. #include <linux/mtd/mtd.h>
  31. #include <linux/mtd/partitions.h>
  32. #include <linux/mtd/spi-nor.h>
  33. #define MTK_NOR_CMD_REG 0x00
  34. #define MTK_NOR_CNT_REG 0x04
  35. #define MTK_NOR_RDSR_REG 0x08
  36. #define MTK_NOR_RDATA_REG 0x0c
  37. #define MTK_NOR_RADR0_REG 0x10
  38. #define MTK_NOR_RADR1_REG 0x14
  39. #define MTK_NOR_RADR2_REG 0x18
  40. #define MTK_NOR_WDATA_REG 0x1c
  41. #define MTK_NOR_PRGDATA0_REG 0x20
  42. #define MTK_NOR_PRGDATA1_REG 0x24
  43. #define MTK_NOR_PRGDATA2_REG 0x28
  44. #define MTK_NOR_PRGDATA3_REG 0x2c
  45. #define MTK_NOR_PRGDATA4_REG 0x30
  46. #define MTK_NOR_PRGDATA5_REG 0x34
  47. #define MTK_NOR_SHREG0_REG 0x38
  48. #define MTK_NOR_SHREG1_REG 0x3c
  49. #define MTK_NOR_SHREG2_REG 0x40
  50. #define MTK_NOR_SHREG3_REG 0x44
  51. #define MTK_NOR_SHREG4_REG 0x48
  52. #define MTK_NOR_SHREG5_REG 0x4c
  53. #define MTK_NOR_SHREG6_REG 0x50
  54. #define MTK_NOR_SHREG7_REG 0x54
  55. #define MTK_NOR_SHREG8_REG 0x58
  56. #define MTK_NOR_SHREG9_REG 0x5c
  57. #define MTK_NOR_CFG1_REG 0x60
  58. #define MTK_NOR_CFG2_REG 0x64
  59. #define MTK_NOR_CFG3_REG 0x68
  60. #define MTK_NOR_STATUS0_REG 0x70
  61. #define MTK_NOR_STATUS1_REG 0x74
  62. #define MTK_NOR_STATUS2_REG 0x78
  63. #define MTK_NOR_STATUS3_REG 0x7c
  64. #define MTK_NOR_FLHCFG_REG 0x84
  65. #define MTK_NOR_TIME_REG 0x94
  66. #define MTK_NOR_PP_DATA_REG 0x98
  67. #define MTK_NOR_PREBUF_STUS_REG 0x9c
  68. #define MTK_NOR_DELSEL0_REG 0xa0
  69. #define MTK_NOR_DELSEL1_REG 0xa4
  70. #define MTK_NOR_INTRSTUS_REG 0xa8
  71. #define MTK_NOR_INTREN_REG 0xac
  72. #define MTK_NOR_CHKSUM_CTL_REG 0xb8
  73. #define MTK_NOR_CHKSUM_REG 0xbc
  74. #define MTK_NOR_CMD2_REG 0xc0
  75. #define MTK_NOR_WRPROT_REG 0xc4
  76. #define MTK_NOR_RADR3_REG 0xc8
  77. #define MTK_NOR_DUAL_REG 0xcc
  78. #define MTK_NOR_DELSEL2_REG 0xd0
  79. #define MTK_NOR_DELSEL3_REG 0xd4
  80. #define MTK_NOR_DELSEL4_REG 0xd8
  81. /* commands for mtk nor controller */
  82. #define MTK_NOR_READ_CMD 0x0
  83. #define MTK_NOR_RDSR_CMD 0x2
  84. #define MTK_NOR_PRG_CMD 0x4
  85. #define MTK_NOR_WR_CMD 0x10
  86. #define MTK_NOR_PIO_WR_CMD 0x90
  87. #define MTK_NOR_WRSR_CMD 0x20
  88. #define MTK_NOR_PIO_READ_CMD 0x81
  89. #define MTK_NOR_WR_BUF_ENABLE 0x1
  90. #define MTK_NOR_WR_BUF_DISABLE 0x0
  91. #define MTK_NOR_ENABLE_SF_CMD 0x30
  92. #define MTK_NOR_DUAD_ADDR_EN 0x8
  93. #define MTK_NOR_QUAD_READ_EN 0x4
  94. #define MTK_NOR_DUAL_ADDR_EN 0x2
  95. #define MTK_NOR_DUAL_READ_EN 0x1
  96. #define MTK_NOR_DUAL_DISABLE 0x0
  97. #define MTK_NOR_FAST_READ 0x1
  98. #define SFLASH_WRBUF_SIZE 128
  99. /* Can shift up to 48 bits (6 bytes) of TX/RX */
  100. #define MTK_NOR_MAX_RX_TX_SHIFT 6
  101. /* can shift up to 56 bits (7 bytes) transfer by MTK_NOR_PRG_CMD */
  102. #define MTK_NOR_MAX_SHIFT 7
  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->flash_read) {
  117. case SPI_NOR_FAST:
  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 SPI_NOR_DUAL:
  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 SPI_NOR_QUAD:
  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(struct mt8173_nor *mt8173_nor, u32 addr)
  207. {
  208. int i;
  209. for (i = 0; i < 3; i++) {
  210. writeb(addr & 0xff, mt8173_nor->base + MTK_NOR_RADR0_REG + i * 4);
  211. addr >>= 8;
  212. }
  213. /* Last register is non-contiguous */
  214. writeb(addr & 0xff, mt8173_nor->base + MTK_NOR_RADR3_REG);
  215. }
  216. static int mt8173_nor_read(struct spi_nor *nor, loff_t from, size_t length,
  217. size_t *retlen, u_char *buffer)
  218. {
  219. int i, ret;
  220. int addr = (int)from;
  221. u8 *buf = (u8 *)buffer;
  222. struct mt8173_nor *mt8173_nor = nor->priv;
  223. /* set mode for fast read mode ,dual mode or quad mode */
  224. mt8173_nor_set_read_mode(mt8173_nor);
  225. mt8173_nor_set_addr(mt8173_nor, addr);
  226. for (i = 0; i < length; i++, (*retlen)++) {
  227. ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PIO_READ_CMD);
  228. if (ret < 0)
  229. return ret;
  230. buf[i] = readb(mt8173_nor->base + MTK_NOR_RDATA_REG);
  231. }
  232. return 0;
  233. }
  234. static int mt8173_nor_write_single_byte(struct mt8173_nor *mt8173_nor,
  235. int addr, int length, u8 *data)
  236. {
  237. int i, ret;
  238. mt8173_nor_set_addr(mt8173_nor, addr);
  239. for (i = 0; i < length; i++) {
  240. writeb(*data++, mt8173_nor->base + MTK_NOR_WDATA_REG);
  241. ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PIO_WR_CMD);
  242. if (ret < 0)
  243. return ret;
  244. }
  245. return 0;
  246. }
  247. static int mt8173_nor_write_buffer(struct mt8173_nor *mt8173_nor, int addr,
  248. const u8 *buf)
  249. {
  250. int i, bufidx, data;
  251. mt8173_nor_set_addr(mt8173_nor, addr);
  252. bufidx = 0;
  253. for (i = 0; i < SFLASH_WRBUF_SIZE; i += 4) {
  254. data = buf[bufidx + 3]<<24 | buf[bufidx + 2]<<16 |
  255. buf[bufidx + 1]<<8 | buf[bufidx];
  256. bufidx += 4;
  257. writel(data, mt8173_nor->base + MTK_NOR_PP_DATA_REG);
  258. }
  259. return mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_WR_CMD);
  260. }
  261. static void mt8173_nor_write(struct spi_nor *nor, loff_t to, size_t len,
  262. size_t *retlen, const u_char *buf)
  263. {
  264. int ret;
  265. struct mt8173_nor *mt8173_nor = nor->priv;
  266. ret = mt8173_nor_write_buffer_enable(mt8173_nor);
  267. if (ret < 0)
  268. dev_warn(mt8173_nor->dev, "write buffer enable failed!\n");
  269. while (len >= SFLASH_WRBUF_SIZE) {
  270. ret = mt8173_nor_write_buffer(mt8173_nor, to, buf);
  271. if (ret < 0)
  272. dev_err(mt8173_nor->dev, "write buffer failed!\n");
  273. len -= SFLASH_WRBUF_SIZE;
  274. to += SFLASH_WRBUF_SIZE;
  275. buf += SFLASH_WRBUF_SIZE;
  276. (*retlen) += SFLASH_WRBUF_SIZE;
  277. }
  278. ret = mt8173_nor_write_buffer_disable(mt8173_nor);
  279. if (ret < 0)
  280. dev_warn(mt8173_nor->dev, "write buffer disable failed!\n");
  281. if (len) {
  282. ret = mt8173_nor_write_single_byte(mt8173_nor, to, (int)len,
  283. (u8 *)buf);
  284. if (ret < 0)
  285. dev_err(mt8173_nor->dev, "write single byte failed!\n");
  286. (*retlen) += len;
  287. }
  288. }
  289. static int mt8173_nor_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  290. {
  291. int ret;
  292. struct mt8173_nor *mt8173_nor = nor->priv;
  293. switch (opcode) {
  294. case SPINOR_OP_RDSR:
  295. ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_RDSR_CMD);
  296. if (ret < 0)
  297. return ret;
  298. if (len == 1)
  299. *buf = readb(mt8173_nor->base + MTK_NOR_RDSR_REG);
  300. else
  301. dev_err(mt8173_nor->dev, "len should be 1 for read status!\n");
  302. break;
  303. default:
  304. ret = mt8173_nor_do_tx_rx(mt8173_nor, opcode, NULL, 0, buf, len);
  305. break;
  306. }
  307. return ret;
  308. }
  309. static int mt8173_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf,
  310. int len)
  311. {
  312. int ret;
  313. struct mt8173_nor *mt8173_nor = nor->priv;
  314. switch (opcode) {
  315. case SPINOR_OP_WRSR:
  316. /* We only handle 1 byte */
  317. ret = mt8173_nor_wr_sr(mt8173_nor, *buf);
  318. break;
  319. default:
  320. ret = mt8173_nor_do_tx_rx(mt8173_nor, opcode, buf, len, NULL, 0);
  321. if (ret)
  322. dev_warn(mt8173_nor->dev, "write reg failure!\n");
  323. break;
  324. }
  325. return ret;
  326. }
  327. static int mtk_nor_init(struct mt8173_nor *mt8173_nor,
  328. struct device_node *flash_node)
  329. {
  330. int ret;
  331. struct spi_nor *nor;
  332. /* initialize controller to accept commands */
  333. writel(MTK_NOR_ENABLE_SF_CMD, mt8173_nor->base + MTK_NOR_WRPROT_REG);
  334. nor = &mt8173_nor->nor;
  335. nor->dev = mt8173_nor->dev;
  336. nor->priv = mt8173_nor;
  337. spi_nor_set_flash_node(nor, flash_node);
  338. /* fill the hooks to spi nor */
  339. nor->read = mt8173_nor_read;
  340. nor->read_reg = mt8173_nor_read_reg;
  341. nor->write = mt8173_nor_write;
  342. nor->write_reg = mt8173_nor_write_reg;
  343. nor->mtd.name = "mtk_nor";
  344. /* initialized with NULL */
  345. ret = spi_nor_scan(nor, NULL, SPI_NOR_DUAL);
  346. if (ret)
  347. return ret;
  348. return mtd_device_register(&nor->mtd, NULL, 0);
  349. }
  350. static int mtk_nor_drv_probe(struct platform_device *pdev)
  351. {
  352. struct device_node *flash_np;
  353. struct resource *res;
  354. int ret;
  355. struct mt8173_nor *mt8173_nor;
  356. if (!pdev->dev.of_node) {
  357. dev_err(&pdev->dev, "No DT found\n");
  358. return -EINVAL;
  359. }
  360. mt8173_nor = devm_kzalloc(&pdev->dev, sizeof(*mt8173_nor), GFP_KERNEL);
  361. if (!mt8173_nor)
  362. return -ENOMEM;
  363. platform_set_drvdata(pdev, mt8173_nor);
  364. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  365. mt8173_nor->base = devm_ioremap_resource(&pdev->dev, res);
  366. if (IS_ERR(mt8173_nor->base))
  367. return PTR_ERR(mt8173_nor->base);
  368. mt8173_nor->spi_clk = devm_clk_get(&pdev->dev, "spi");
  369. if (IS_ERR(mt8173_nor->spi_clk))
  370. return PTR_ERR(mt8173_nor->spi_clk);
  371. mt8173_nor->nor_clk = devm_clk_get(&pdev->dev, "sf");
  372. if (IS_ERR(mt8173_nor->nor_clk))
  373. return PTR_ERR(mt8173_nor->nor_clk);
  374. mt8173_nor->dev = &pdev->dev;
  375. ret = clk_prepare_enable(mt8173_nor->spi_clk);
  376. if (ret)
  377. return ret;
  378. ret = clk_prepare_enable(mt8173_nor->nor_clk);
  379. if (ret) {
  380. clk_disable_unprepare(mt8173_nor->spi_clk);
  381. return ret;
  382. }
  383. /* only support one attached flash */
  384. flash_np = of_get_next_available_child(pdev->dev.of_node, NULL);
  385. if (!flash_np) {
  386. dev_err(&pdev->dev, "no SPI flash device to configure\n");
  387. ret = -ENODEV;
  388. goto nor_free;
  389. }
  390. ret = mtk_nor_init(mt8173_nor, flash_np);
  391. nor_free:
  392. if (ret) {
  393. clk_disable_unprepare(mt8173_nor->spi_clk);
  394. clk_disable_unprepare(mt8173_nor->nor_clk);
  395. }
  396. return ret;
  397. }
  398. static int mtk_nor_drv_remove(struct platform_device *pdev)
  399. {
  400. struct mt8173_nor *mt8173_nor = platform_get_drvdata(pdev);
  401. clk_disable_unprepare(mt8173_nor->spi_clk);
  402. clk_disable_unprepare(mt8173_nor->nor_clk);
  403. return 0;
  404. }
  405. static const struct of_device_id mtk_nor_of_ids[] = {
  406. { .compatible = "mediatek,mt8173-nor"},
  407. { /* sentinel */ }
  408. };
  409. MODULE_DEVICE_TABLE(of, mtk_nor_of_ids);
  410. static struct platform_driver mtk_nor_driver = {
  411. .probe = mtk_nor_drv_probe,
  412. .remove = mtk_nor_drv_remove,
  413. .driver = {
  414. .name = "mtk-nor",
  415. .of_match_table = mtk_nor_of_ids,
  416. },
  417. };
  418. module_platform_driver(mtk_nor_driver);
  419. MODULE_LICENSE("GPL v2");
  420. MODULE_DESCRIPTION("MediaTek SPI NOR Flash Driver");