hisi-sfc.c 13 KB

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