mtk-quadspi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. /* Helpers for accessing the program data / shift data registers */
  103. #define MTK_NOR_PRG_REG(n) (MTK_NOR_PRGDATA0_REG + 4 * (n))
  104. #define MTK_NOR_SHREG(n) (MTK_NOR_SHREG0_REG + 4 * (n))
  105. struct mt8173_nor {
  106. struct spi_nor nor;
  107. struct device *dev;
  108. void __iomem *base; /* nor flash base address */
  109. struct clk *spi_clk;
  110. struct clk *nor_clk;
  111. };
  112. static void mt8173_nor_set_read_mode(struct mt8173_nor *mt8173_nor)
  113. {
  114. struct spi_nor *nor = &mt8173_nor->nor;
  115. switch (nor->flash_read) {
  116. case SPI_NOR_FAST:
  117. writeb(nor->read_opcode, mt8173_nor->base +
  118. MTK_NOR_PRGDATA3_REG);
  119. writeb(MTK_NOR_FAST_READ, mt8173_nor->base +
  120. MTK_NOR_CFG1_REG);
  121. break;
  122. case SPI_NOR_DUAL:
  123. writeb(nor->read_opcode, mt8173_nor->base +
  124. MTK_NOR_PRGDATA3_REG);
  125. writeb(MTK_NOR_DUAL_READ_EN, mt8173_nor->base +
  126. MTK_NOR_DUAL_REG);
  127. break;
  128. case SPI_NOR_QUAD:
  129. writeb(nor->read_opcode, mt8173_nor->base +
  130. MTK_NOR_PRGDATA4_REG);
  131. writeb(MTK_NOR_QUAD_READ_EN, mt8173_nor->base +
  132. MTK_NOR_DUAL_REG);
  133. break;
  134. default:
  135. writeb(MTK_NOR_DUAL_DISABLE, mt8173_nor->base +
  136. MTK_NOR_DUAL_REG);
  137. break;
  138. }
  139. }
  140. static int mt8173_nor_execute_cmd(struct mt8173_nor *mt8173_nor, u8 cmdval)
  141. {
  142. int reg;
  143. u8 val = cmdval & 0x1f;
  144. writeb(cmdval, mt8173_nor->base + MTK_NOR_CMD_REG);
  145. return readl_poll_timeout(mt8173_nor->base + MTK_NOR_CMD_REG, reg,
  146. !(reg & val), 100, 10000);
  147. }
  148. static int mt8173_nor_do_tx_rx(struct mt8173_nor *mt8173_nor, u8 op,
  149. u8 *tx, int txlen, u8 *rx, int rxlen)
  150. {
  151. int len = 1 + txlen + rxlen;
  152. int i, ret, idx;
  153. if (len > MTK_NOR_MAX_SHIFT)
  154. return -EINVAL;
  155. writeb(len * 8, mt8173_nor->base + MTK_NOR_CNT_REG);
  156. /* start at PRGDATA5, go down to PRGDATA0 */
  157. idx = MTK_NOR_MAX_RX_TX_SHIFT - 1;
  158. /* opcode */
  159. writeb(op, mt8173_nor->base + MTK_NOR_PRG_REG(idx));
  160. idx--;
  161. /* program TX data */
  162. for (i = 0; i < txlen; i++, idx--)
  163. writeb(tx[i], mt8173_nor->base + MTK_NOR_PRG_REG(idx));
  164. /* clear out rest of TX registers */
  165. while (idx >= 0) {
  166. writeb(0, mt8173_nor->base + MTK_NOR_PRG_REG(idx));
  167. idx--;
  168. }
  169. ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PRG_CMD);
  170. if (ret)
  171. return ret;
  172. /* restart at first RX byte */
  173. idx = rxlen - 1;
  174. /* read out RX data */
  175. for (i = 0; i < rxlen; i++, idx--)
  176. rx[i] = readb(mt8173_nor->base + MTK_NOR_SHREG(idx));
  177. return 0;
  178. }
  179. /* Do a WRSR (Write Status Register) command */
  180. static int mt8173_nor_wr_sr(struct mt8173_nor *mt8173_nor, u8 sr)
  181. {
  182. writeb(sr, mt8173_nor->base + MTK_NOR_PRGDATA5_REG);
  183. writeb(8, mt8173_nor->base + MTK_NOR_CNT_REG);
  184. return mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_WRSR_CMD);
  185. }
  186. static int mt8173_nor_write_buffer_enable(struct mt8173_nor *mt8173_nor)
  187. {
  188. u8 reg;
  189. /* the bit0 of MTK_NOR_CFG2_REG is pre-fetch buffer
  190. * 0: pre-fetch buffer use for read
  191. * 1: pre-fetch buffer use for page program
  192. */
  193. writel(MTK_NOR_WR_BUF_ENABLE, mt8173_nor->base + MTK_NOR_CFG2_REG);
  194. return readb_poll_timeout(mt8173_nor->base + MTK_NOR_CFG2_REG, reg,
  195. 0x01 == (reg & 0x01), 100, 10000);
  196. }
  197. static int mt8173_nor_write_buffer_disable(struct mt8173_nor *mt8173_nor)
  198. {
  199. u8 reg;
  200. writel(MTK_NOR_WR_BUF_DISABLE, mt8173_nor->base + MTK_NOR_CFG2_REG);
  201. return readb_poll_timeout(mt8173_nor->base + MTK_NOR_CFG2_REG, reg,
  202. MTK_NOR_WR_BUF_DISABLE == (reg & 0x1), 100,
  203. 10000);
  204. }
  205. static void mt8173_nor_set_addr(struct mt8173_nor *mt8173_nor, u32 addr)
  206. {
  207. int i;
  208. for (i = 0; i < 3; i++) {
  209. writeb(addr & 0xff, mt8173_nor->base + MTK_NOR_RADR0_REG + i * 4);
  210. addr >>= 8;
  211. }
  212. /* Last register is non-contiguous */
  213. writeb(addr & 0xff, mt8173_nor->base + MTK_NOR_RADR3_REG);
  214. }
  215. static ssize_t mt8173_nor_read(struct spi_nor *nor, loff_t from, size_t length,
  216. u_char *buffer)
  217. {
  218. int i, ret;
  219. int addr = (int)from;
  220. u8 *buf = (u8 *)buffer;
  221. struct mt8173_nor *mt8173_nor = nor->priv;
  222. /* set mode for fast read mode ,dual mode or quad mode */
  223. mt8173_nor_set_read_mode(mt8173_nor);
  224. mt8173_nor_set_addr(mt8173_nor, addr);
  225. for (i = 0; i < length; i++) {
  226. ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PIO_READ_CMD);
  227. if (ret < 0)
  228. return ret;
  229. buf[i] = readb(mt8173_nor->base + MTK_NOR_RDATA_REG);
  230. }
  231. return length;
  232. }
  233. static int mt8173_nor_write_single_byte(struct mt8173_nor *mt8173_nor,
  234. int addr, int length, u8 *data)
  235. {
  236. int i, ret;
  237. mt8173_nor_set_addr(mt8173_nor, addr);
  238. for (i = 0; i < length; i++) {
  239. writeb(*data++, mt8173_nor->base + MTK_NOR_WDATA_REG);
  240. ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_PIO_WR_CMD);
  241. if (ret < 0)
  242. return ret;
  243. }
  244. return 0;
  245. }
  246. static int mt8173_nor_write_buffer(struct mt8173_nor *mt8173_nor, int addr,
  247. const u8 *buf)
  248. {
  249. int i, bufidx, data;
  250. mt8173_nor_set_addr(mt8173_nor, addr);
  251. bufidx = 0;
  252. for (i = 0; i < SFLASH_WRBUF_SIZE; i += 4) {
  253. data = buf[bufidx + 3]<<24 | buf[bufidx + 2]<<16 |
  254. buf[bufidx + 1]<<8 | buf[bufidx];
  255. bufidx += 4;
  256. writel(data, mt8173_nor->base + MTK_NOR_PP_DATA_REG);
  257. }
  258. return mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_WR_CMD);
  259. }
  260. static ssize_t mt8173_nor_write(struct spi_nor *nor, loff_t to, size_t len,
  261. const u_char *buf)
  262. {
  263. int ret;
  264. struct mt8173_nor *mt8173_nor = nor->priv;
  265. size_t i;
  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. return ret;
  270. }
  271. for (i = 0; i + SFLASH_WRBUF_SIZE <= len; i += SFLASH_WRBUF_SIZE) {
  272. ret = mt8173_nor_write_buffer(mt8173_nor, to, buf);
  273. if (ret < 0) {
  274. dev_err(mt8173_nor->dev, "write buffer failed!\n");
  275. return ret;
  276. }
  277. to += SFLASH_WRBUF_SIZE;
  278. buf += SFLASH_WRBUF_SIZE;
  279. }
  280. ret = mt8173_nor_write_buffer_disable(mt8173_nor);
  281. if (ret < 0) {
  282. dev_warn(mt8173_nor->dev, "write buffer disable failed!\n");
  283. return ret;
  284. }
  285. if (i < len) {
  286. ret = mt8173_nor_write_single_byte(mt8173_nor, to,
  287. (int)(len - i), (u8 *)buf);
  288. if (ret < 0) {
  289. dev_err(mt8173_nor->dev, "write single byte failed!\n");
  290. return ret;
  291. }
  292. }
  293. return len;
  294. }
  295. static int mt8173_nor_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  296. {
  297. int ret;
  298. struct mt8173_nor *mt8173_nor = nor->priv;
  299. switch (opcode) {
  300. case SPINOR_OP_RDSR:
  301. ret = mt8173_nor_execute_cmd(mt8173_nor, MTK_NOR_RDSR_CMD);
  302. if (ret < 0)
  303. return ret;
  304. if (len == 1)
  305. *buf = readb(mt8173_nor->base + MTK_NOR_RDSR_REG);
  306. else
  307. dev_err(mt8173_nor->dev, "len should be 1 for read status!\n");
  308. break;
  309. default:
  310. ret = mt8173_nor_do_tx_rx(mt8173_nor, opcode, NULL, 0, buf, len);
  311. break;
  312. }
  313. return ret;
  314. }
  315. static int mt8173_nor_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf,
  316. int len)
  317. {
  318. int ret;
  319. struct mt8173_nor *mt8173_nor = nor->priv;
  320. switch (opcode) {
  321. case SPINOR_OP_WRSR:
  322. /* We only handle 1 byte */
  323. ret = mt8173_nor_wr_sr(mt8173_nor, *buf);
  324. break;
  325. default:
  326. ret = mt8173_nor_do_tx_rx(mt8173_nor, opcode, buf, len, NULL, 0);
  327. if (ret)
  328. dev_warn(mt8173_nor->dev, "write reg failure!\n");
  329. break;
  330. }
  331. return ret;
  332. }
  333. static int mtk_nor_init(struct mt8173_nor *mt8173_nor,
  334. struct device_node *flash_node)
  335. {
  336. int ret;
  337. struct spi_nor *nor;
  338. /* initialize controller to accept commands */
  339. writel(MTK_NOR_ENABLE_SF_CMD, mt8173_nor->base + MTK_NOR_WRPROT_REG);
  340. nor = &mt8173_nor->nor;
  341. nor->dev = mt8173_nor->dev;
  342. nor->priv = mt8173_nor;
  343. spi_nor_set_flash_node(nor, flash_node);
  344. /* fill the hooks to spi nor */
  345. nor->read = mt8173_nor_read;
  346. nor->read_reg = mt8173_nor_read_reg;
  347. nor->write = mt8173_nor_write;
  348. nor->write_reg = mt8173_nor_write_reg;
  349. nor->mtd.name = "mtk_nor";
  350. /* initialized with NULL */
  351. ret = spi_nor_scan(nor, NULL, SPI_NOR_DUAL);
  352. if (ret)
  353. return ret;
  354. return mtd_device_register(&nor->mtd, NULL, 0);
  355. }
  356. static int mtk_nor_drv_probe(struct platform_device *pdev)
  357. {
  358. struct device_node *flash_np;
  359. struct resource *res;
  360. int ret;
  361. struct mt8173_nor *mt8173_nor;
  362. if (!pdev->dev.of_node) {
  363. dev_err(&pdev->dev, "No DT found\n");
  364. return -EINVAL;
  365. }
  366. mt8173_nor = devm_kzalloc(&pdev->dev, sizeof(*mt8173_nor), GFP_KERNEL);
  367. if (!mt8173_nor)
  368. return -ENOMEM;
  369. platform_set_drvdata(pdev, mt8173_nor);
  370. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  371. mt8173_nor->base = devm_ioremap_resource(&pdev->dev, res);
  372. if (IS_ERR(mt8173_nor->base))
  373. return PTR_ERR(mt8173_nor->base);
  374. mt8173_nor->spi_clk = devm_clk_get(&pdev->dev, "spi");
  375. if (IS_ERR(mt8173_nor->spi_clk))
  376. return PTR_ERR(mt8173_nor->spi_clk);
  377. mt8173_nor->nor_clk = devm_clk_get(&pdev->dev, "sf");
  378. if (IS_ERR(mt8173_nor->nor_clk))
  379. return PTR_ERR(mt8173_nor->nor_clk);
  380. mt8173_nor->dev = &pdev->dev;
  381. ret = clk_prepare_enable(mt8173_nor->spi_clk);
  382. if (ret)
  383. return ret;
  384. ret = clk_prepare_enable(mt8173_nor->nor_clk);
  385. if (ret) {
  386. clk_disable_unprepare(mt8173_nor->spi_clk);
  387. return ret;
  388. }
  389. /* only support one attached flash */
  390. flash_np = of_get_next_available_child(pdev->dev.of_node, NULL);
  391. if (!flash_np) {
  392. dev_err(&pdev->dev, "no SPI flash device to configure\n");
  393. ret = -ENODEV;
  394. goto nor_free;
  395. }
  396. ret = mtk_nor_init(mt8173_nor, flash_np);
  397. nor_free:
  398. if (ret) {
  399. clk_disable_unprepare(mt8173_nor->spi_clk);
  400. clk_disable_unprepare(mt8173_nor->nor_clk);
  401. }
  402. return ret;
  403. }
  404. static int mtk_nor_drv_remove(struct platform_device *pdev)
  405. {
  406. struct mt8173_nor *mt8173_nor = platform_get_drvdata(pdev);
  407. clk_disable_unprepare(mt8173_nor->spi_clk);
  408. clk_disable_unprepare(mt8173_nor->nor_clk);
  409. return 0;
  410. }
  411. static const struct of_device_id mtk_nor_of_ids[] = {
  412. { .compatible = "mediatek,mt8173-nor"},
  413. { /* sentinel */ }
  414. };
  415. MODULE_DEVICE_TABLE(of, mtk_nor_of_ids);
  416. static struct platform_driver mtk_nor_driver = {
  417. .probe = mtk_nor_drv_probe,
  418. .remove = mtk_nor_drv_remove,
  419. .driver = {
  420. .name = "mtk-nor",
  421. .of_match_table = mtk_nor_of_ids,
  422. },
  423. };
  424. module_platform_driver(mtk_nor_driver);
  425. MODULE_LICENSE("GPL v2");
  426. MODULE_DESCRIPTION("MediaTek SPI NOR Flash Driver");