hisi-sfc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HiSilicon FMC SPI NOR flash controller driver
  4. *
  5. * Copyright (c) 2015-2016 HiSilicon Technologies Co., Ltd.
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/clk.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/module.h>
  12. #include <linux/mtd/mtd.h>
  13. #include <linux/mtd/spi-nor.h>
  14. #include <linux/of.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. /* Hardware register offsets and field definitions */
  18. #define FMC_CFG 0x00
  19. #define FMC_CFG_OP_MODE_MASK BIT_MASK(0)
  20. #define FMC_CFG_OP_MODE_BOOT 0
  21. #define FMC_CFG_OP_MODE_NORMAL 1
  22. #define FMC_CFG_FLASH_SEL(type) (((type) & 0x3) << 1)
  23. #define FMC_CFG_FLASH_SEL_MASK 0x6
  24. #define FMC_ECC_TYPE(type) (((type) & 0x7) << 5)
  25. #define FMC_ECC_TYPE_MASK GENMASK(7, 5)
  26. #define SPI_NOR_ADDR_MODE_MASK BIT_MASK(10)
  27. #define SPI_NOR_ADDR_MODE_3BYTES (0x0 << 10)
  28. #define SPI_NOR_ADDR_MODE_4BYTES (0x1 << 10)
  29. #define FMC_GLOBAL_CFG 0x04
  30. #define FMC_GLOBAL_CFG_WP_ENABLE BIT(6)
  31. #define FMC_SPI_TIMING_CFG 0x08
  32. #define TIMING_CFG_TCSH(nr) (((nr) & 0xf) << 8)
  33. #define TIMING_CFG_TCSS(nr) (((nr) & 0xf) << 4)
  34. #define TIMING_CFG_TSHSL(nr) ((nr) & 0xf)
  35. #define CS_HOLD_TIME 0x6
  36. #define CS_SETUP_TIME 0x6
  37. #define CS_DESELECT_TIME 0xf
  38. #define FMC_INT 0x18
  39. #define FMC_INT_OP_DONE BIT(0)
  40. #define FMC_INT_CLR 0x20
  41. #define FMC_CMD 0x24
  42. #define FMC_CMD_CMD1(cmd) ((cmd) & 0xff)
  43. #define FMC_ADDRL 0x2c
  44. #define FMC_OP_CFG 0x30
  45. #define OP_CFG_FM_CS(cs) ((cs) << 11)
  46. #define OP_CFG_MEM_IF_TYPE(type) (((type) & 0x7) << 7)
  47. #define OP_CFG_ADDR_NUM(addr) (((addr) & 0x7) << 4)
  48. #define OP_CFG_DUMMY_NUM(dummy) ((dummy) & 0xf)
  49. #define FMC_DATA_NUM 0x38
  50. #define FMC_DATA_NUM_CNT(cnt) ((cnt) & GENMASK(13, 0))
  51. #define FMC_OP 0x3c
  52. #define FMC_OP_DUMMY_EN BIT(8)
  53. #define FMC_OP_CMD1_EN BIT(7)
  54. #define FMC_OP_ADDR_EN BIT(6)
  55. #define FMC_OP_WRITE_DATA_EN BIT(5)
  56. #define FMC_OP_READ_DATA_EN BIT(2)
  57. #define FMC_OP_READ_STATUS_EN BIT(1)
  58. #define FMC_OP_REG_OP_START BIT(0)
  59. #define FMC_DMA_LEN 0x40
  60. #define FMC_DMA_LEN_SET(len) ((len) & GENMASK(27, 0))
  61. #define FMC_DMA_SADDR_D0 0x4c
  62. #define HIFMC_DMA_MAX_LEN (4096)
  63. #define HIFMC_DMA_MASK (HIFMC_DMA_MAX_LEN - 1)
  64. #define FMC_OP_DMA 0x68
  65. #define OP_CTRL_RD_OPCODE(code) (((code) & 0xff) << 16)
  66. #define OP_CTRL_WR_OPCODE(code) (((code) & 0xff) << 8)
  67. #define OP_CTRL_RW_OP(op) ((op) << 1)
  68. #define OP_CTRL_DMA_OP_READY BIT(0)
  69. #define FMC_OP_READ 0x0
  70. #define FMC_OP_WRITE 0x1
  71. #define FMC_WAIT_TIMEOUT 1000000
  72. enum hifmc_iftype {
  73. IF_TYPE_STD,
  74. IF_TYPE_DUAL,
  75. IF_TYPE_DIO,
  76. IF_TYPE_QUAD,
  77. IF_TYPE_QIO,
  78. };
  79. struct hifmc_priv {
  80. u32 chipselect;
  81. u32 clkrate;
  82. struct hifmc_host *host;
  83. };
  84. #define HIFMC_MAX_CHIP_NUM 2
  85. struct hifmc_host {
  86. struct device *dev;
  87. struct mutex lock;
  88. void __iomem *regbase;
  89. void __iomem *iobase;
  90. struct clk *clk;
  91. void *buffer;
  92. dma_addr_t dma_buffer;
  93. struct spi_nor *nor[HIFMC_MAX_CHIP_NUM];
  94. u32 num_chip;
  95. };
  96. static inline int hisi_spi_nor_wait_op_finish(struct hifmc_host *host)
  97. {
  98. u32 reg;
  99. return readl_poll_timeout(host->regbase + FMC_INT, reg,
  100. (reg & FMC_INT_OP_DONE), 0, FMC_WAIT_TIMEOUT);
  101. }
  102. static int hisi_spi_nor_get_if_type(enum spi_nor_protocol proto)
  103. {
  104. enum hifmc_iftype if_type;
  105. switch (proto) {
  106. case SNOR_PROTO_1_1_2:
  107. if_type = IF_TYPE_DUAL;
  108. break;
  109. case SNOR_PROTO_1_2_2:
  110. if_type = IF_TYPE_DIO;
  111. break;
  112. case SNOR_PROTO_1_1_4:
  113. if_type = IF_TYPE_QUAD;
  114. break;
  115. case SNOR_PROTO_1_4_4:
  116. if_type = IF_TYPE_QIO;
  117. break;
  118. case SNOR_PROTO_1_1_1:
  119. default:
  120. if_type = IF_TYPE_STD;
  121. break;
  122. }
  123. return if_type;
  124. }
  125. static void hisi_spi_nor_init(struct hifmc_host *host)
  126. {
  127. u32 reg;
  128. reg = TIMING_CFG_TCSH(CS_HOLD_TIME)
  129. | TIMING_CFG_TCSS(CS_SETUP_TIME)
  130. | TIMING_CFG_TSHSL(CS_DESELECT_TIME);
  131. writel(reg, host->regbase + FMC_SPI_TIMING_CFG);
  132. }
  133. static int hisi_spi_nor_prep(struct spi_nor *nor)
  134. {
  135. struct hifmc_priv *priv = nor->priv;
  136. struct hifmc_host *host = priv->host;
  137. int ret;
  138. mutex_lock(&host->lock);
  139. ret = clk_set_rate(host->clk, priv->clkrate);
  140. if (ret)
  141. goto out;
  142. ret = clk_prepare_enable(host->clk);
  143. if (ret)
  144. goto out;
  145. return 0;
  146. out:
  147. mutex_unlock(&host->lock);
  148. return ret;
  149. }
  150. static void hisi_spi_nor_unprep(struct spi_nor *nor)
  151. {
  152. struct hifmc_priv *priv = nor->priv;
  153. struct hifmc_host *host = priv->host;
  154. clk_disable_unprepare(host->clk);
  155. mutex_unlock(&host->lock);
  156. }
  157. static int hisi_spi_nor_op_reg(struct spi_nor *nor,
  158. u8 opcode, size_t len, u8 optype)
  159. {
  160. struct hifmc_priv *priv = nor->priv;
  161. struct hifmc_host *host = priv->host;
  162. u32 reg;
  163. reg = FMC_CMD_CMD1(opcode);
  164. writel(reg, host->regbase + FMC_CMD);
  165. reg = FMC_DATA_NUM_CNT(len);
  166. writel(reg, host->regbase + FMC_DATA_NUM);
  167. reg = OP_CFG_FM_CS(priv->chipselect);
  168. writel(reg, host->regbase + FMC_OP_CFG);
  169. writel(0xff, host->regbase + FMC_INT_CLR);
  170. reg = FMC_OP_CMD1_EN | FMC_OP_REG_OP_START | optype;
  171. writel(reg, host->regbase + FMC_OP);
  172. return hisi_spi_nor_wait_op_finish(host);
  173. }
  174. static int hisi_spi_nor_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf,
  175. size_t len)
  176. {
  177. struct hifmc_priv *priv = nor->priv;
  178. struct hifmc_host *host = priv->host;
  179. int ret;
  180. ret = hisi_spi_nor_op_reg(nor, opcode, len, FMC_OP_READ_DATA_EN);
  181. if (ret)
  182. return ret;
  183. memcpy_fromio(buf, host->iobase, len);
  184. return 0;
  185. }
  186. static int hisi_spi_nor_write_reg(struct spi_nor *nor, u8 opcode,
  187. const u8 *buf, size_t len)
  188. {
  189. struct hifmc_priv *priv = nor->priv;
  190. struct hifmc_host *host = priv->host;
  191. if (len)
  192. memcpy_toio(host->iobase, buf, len);
  193. return hisi_spi_nor_op_reg(nor, opcode, len, FMC_OP_WRITE_DATA_EN);
  194. }
  195. static int hisi_spi_nor_dma_transfer(struct spi_nor *nor, loff_t start_off,
  196. dma_addr_t dma_buf, size_t len, u8 op_type)
  197. {
  198. struct hifmc_priv *priv = nor->priv;
  199. struct hifmc_host *host = priv->host;
  200. u8 if_type = 0;
  201. u32 reg;
  202. reg = readl(host->regbase + FMC_CFG);
  203. reg &= ~(FMC_CFG_OP_MODE_MASK | SPI_NOR_ADDR_MODE_MASK);
  204. reg |= FMC_CFG_OP_MODE_NORMAL;
  205. reg |= (nor->addr_nbytes == 4) ? SPI_NOR_ADDR_MODE_4BYTES
  206. : SPI_NOR_ADDR_MODE_3BYTES;
  207. writel(reg, host->regbase + FMC_CFG);
  208. writel(start_off, host->regbase + FMC_ADDRL);
  209. writel(dma_buf, host->regbase + FMC_DMA_SADDR_D0);
  210. writel(FMC_DMA_LEN_SET(len), host->regbase + FMC_DMA_LEN);
  211. reg = OP_CFG_FM_CS(priv->chipselect);
  212. if (op_type == FMC_OP_READ)
  213. if_type = hisi_spi_nor_get_if_type(nor->read_proto);
  214. else
  215. if_type = hisi_spi_nor_get_if_type(nor->write_proto);
  216. reg |= OP_CFG_MEM_IF_TYPE(if_type);
  217. if (op_type == FMC_OP_READ)
  218. reg |= OP_CFG_DUMMY_NUM(nor->read_dummy >> 3);
  219. writel(reg, host->regbase + FMC_OP_CFG);
  220. writel(0xff, host->regbase + FMC_INT_CLR);
  221. reg = OP_CTRL_RW_OP(op_type) | OP_CTRL_DMA_OP_READY;
  222. reg |= (op_type == FMC_OP_READ)
  223. ? OP_CTRL_RD_OPCODE(nor->read_opcode)
  224. : OP_CTRL_WR_OPCODE(nor->program_opcode);
  225. writel(reg, host->regbase + FMC_OP_DMA);
  226. return hisi_spi_nor_wait_op_finish(host);
  227. }
  228. static ssize_t hisi_spi_nor_read(struct spi_nor *nor, loff_t from, size_t len,
  229. u_char *read_buf)
  230. {
  231. struct hifmc_priv *priv = nor->priv;
  232. struct hifmc_host *host = priv->host;
  233. size_t offset;
  234. int ret;
  235. for (offset = 0; offset < len; offset += HIFMC_DMA_MAX_LEN) {
  236. size_t trans = min_t(size_t, HIFMC_DMA_MAX_LEN, len - offset);
  237. ret = hisi_spi_nor_dma_transfer(nor,
  238. from + offset, host->dma_buffer, trans, FMC_OP_READ);
  239. if (ret) {
  240. dev_warn(nor->dev, "DMA read timeout\n");
  241. return ret;
  242. }
  243. memcpy(read_buf + offset, host->buffer, trans);
  244. }
  245. return len;
  246. }
  247. static ssize_t hisi_spi_nor_write(struct spi_nor *nor, loff_t to,
  248. size_t len, const u_char *write_buf)
  249. {
  250. struct hifmc_priv *priv = nor->priv;
  251. struct hifmc_host *host = priv->host;
  252. size_t offset;
  253. int ret;
  254. for (offset = 0; offset < len; offset += HIFMC_DMA_MAX_LEN) {
  255. size_t trans = min_t(size_t, HIFMC_DMA_MAX_LEN, len - offset);
  256. memcpy(host->buffer, write_buf + offset, trans);
  257. ret = hisi_spi_nor_dma_transfer(nor,
  258. to + offset, host->dma_buffer, trans, FMC_OP_WRITE);
  259. if (ret) {
  260. dev_warn(nor->dev, "DMA write timeout\n");
  261. return ret;
  262. }
  263. }
  264. return len;
  265. }
  266. static const struct spi_nor_controller_ops hisi_controller_ops = {
  267. .prepare = hisi_spi_nor_prep,
  268. .unprepare = hisi_spi_nor_unprep,
  269. .read_reg = hisi_spi_nor_read_reg,
  270. .write_reg = hisi_spi_nor_write_reg,
  271. .read = hisi_spi_nor_read,
  272. .write = hisi_spi_nor_write,
  273. };
  274. /*
  275. * Get spi flash device information and register it as a mtd device.
  276. */
  277. static int hisi_spi_nor_register(struct device_node *np,
  278. struct hifmc_host *host)
  279. {
  280. const struct spi_nor_hwcaps hwcaps = {
  281. .mask = SNOR_HWCAPS_READ |
  282. SNOR_HWCAPS_READ_FAST |
  283. SNOR_HWCAPS_READ_1_1_2 |
  284. SNOR_HWCAPS_READ_1_1_4 |
  285. SNOR_HWCAPS_PP,
  286. };
  287. struct device *dev = host->dev;
  288. struct spi_nor *nor;
  289. struct hifmc_priv *priv;
  290. struct mtd_info *mtd;
  291. int ret;
  292. nor = devm_kzalloc(dev, sizeof(*nor), GFP_KERNEL);
  293. if (!nor)
  294. return -ENOMEM;
  295. nor->dev = dev;
  296. spi_nor_set_flash_node(nor, np);
  297. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  298. if (!priv)
  299. return -ENOMEM;
  300. ret = of_property_read_u32(np, "reg", &priv->chipselect);
  301. if (ret) {
  302. dev_err(dev, "There's no reg property for %pOF\n",
  303. np);
  304. return ret;
  305. }
  306. ret = of_property_read_u32(np, "spi-max-frequency",
  307. &priv->clkrate);
  308. if (ret) {
  309. dev_err(dev, "There's no spi-max-frequency property for %pOF\n",
  310. np);
  311. return ret;
  312. }
  313. priv->host = host;
  314. nor->priv = priv;
  315. nor->controller_ops = &hisi_controller_ops;
  316. ret = spi_nor_scan(nor, NULL, &hwcaps);
  317. if (ret)
  318. return ret;
  319. mtd = &nor->mtd;
  320. mtd->name = np->name;
  321. ret = mtd_device_register(mtd, NULL, 0);
  322. if (ret)
  323. return ret;
  324. host->nor[host->num_chip] = nor;
  325. host->num_chip++;
  326. return 0;
  327. }
  328. static void hisi_spi_nor_unregister_all(struct hifmc_host *host)
  329. {
  330. int i;
  331. for (i = 0; i < host->num_chip; i++)
  332. mtd_device_unregister(&host->nor[i]->mtd);
  333. }
  334. static int hisi_spi_nor_register_all(struct hifmc_host *host)
  335. {
  336. struct device *dev = host->dev;
  337. struct device_node *np;
  338. int ret;
  339. for_each_available_child_of_node(dev->of_node, np) {
  340. ret = hisi_spi_nor_register(np, host);
  341. if (ret) {
  342. of_node_put(np);
  343. goto fail;
  344. }
  345. if (host->num_chip == HIFMC_MAX_CHIP_NUM) {
  346. dev_warn(dev, "Flash device number exceeds the maximum chipselect number\n");
  347. of_node_put(np);
  348. break;
  349. }
  350. }
  351. return 0;
  352. fail:
  353. hisi_spi_nor_unregister_all(host);
  354. return ret;
  355. }
  356. static int hisi_spi_nor_probe(struct platform_device *pdev)
  357. {
  358. struct device *dev = &pdev->dev;
  359. struct hifmc_host *host;
  360. int ret;
  361. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  362. if (!host)
  363. return -ENOMEM;
  364. platform_set_drvdata(pdev, host);
  365. host->dev = dev;
  366. host->regbase = devm_platform_ioremap_resource_byname(pdev, "control");
  367. if (IS_ERR(host->regbase))
  368. return PTR_ERR(host->regbase);
  369. host->iobase = devm_platform_ioremap_resource_byname(pdev, "memory");
  370. if (IS_ERR(host->iobase))
  371. return PTR_ERR(host->iobase);
  372. host->clk = devm_clk_get(dev, NULL);
  373. if (IS_ERR(host->clk))
  374. return PTR_ERR(host->clk);
  375. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  376. if (ret) {
  377. dev_warn(dev, "Unable to set dma mask\n");
  378. return ret;
  379. }
  380. host->buffer = dmam_alloc_coherent(dev, HIFMC_DMA_MAX_LEN,
  381. &host->dma_buffer, GFP_KERNEL);
  382. if (!host->buffer)
  383. return -ENOMEM;
  384. ret = clk_prepare_enable(host->clk);
  385. if (ret)
  386. return ret;
  387. mutex_init(&host->lock);
  388. hisi_spi_nor_init(host);
  389. ret = hisi_spi_nor_register_all(host);
  390. if (ret)
  391. mutex_destroy(&host->lock);
  392. clk_disable_unprepare(host->clk);
  393. return ret;
  394. }
  395. static int hisi_spi_nor_remove(struct platform_device *pdev)
  396. {
  397. struct hifmc_host *host = platform_get_drvdata(pdev);
  398. hisi_spi_nor_unregister_all(host);
  399. mutex_destroy(&host->lock);
  400. return 0;
  401. }
  402. static const struct of_device_id hisi_spi_nor_dt_ids[] = {
  403. { .compatible = "hisilicon,fmc-spi-nor"},
  404. { /* sentinel */ }
  405. };
  406. MODULE_DEVICE_TABLE(of, hisi_spi_nor_dt_ids);
  407. static struct platform_driver hisi_spi_nor_driver = {
  408. .driver = {
  409. .name = "hisi-sfc",
  410. .of_match_table = hisi_spi_nor_dt_ids,
  411. },
  412. .probe = hisi_spi_nor_probe,
  413. .remove = hisi_spi_nor_remove,
  414. };
  415. module_platform_driver(hisi_spi_nor_driver);
  416. MODULE_LICENSE("GPL v2");
  417. MODULE_DESCRIPTION("HiSilicon SPI Nor Flash Controller Driver");