hisi-sfc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 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 get_if_type(enum read_mode flash_read)
  115. {
  116. enum hifmc_iftype if_type;
  117. switch (flash_read) {
  118. case SPI_NOR_DUAL:
  119. if_type = IF_TYPE_DUAL;
  120. break;
  121. case SPI_NOR_QUAD:
  122. if_type = IF_TYPE_QUAD;
  123. break;
  124. case SPI_NOR_NORMAL:
  125. case SPI_NOR_FAST:
  126. default:
  127. if_type = IF_TYPE_STD;
  128. break;
  129. }
  130. return if_type;
  131. }
  132. static void hisi_spi_nor_init(struct hifmc_host *host)
  133. {
  134. u32 reg;
  135. reg = TIMING_CFG_TCSH(CS_HOLD_TIME)
  136. | TIMING_CFG_TCSS(CS_SETUP_TIME)
  137. | TIMING_CFG_TSHSL(CS_DESELECT_TIME);
  138. writel(reg, host->regbase + FMC_SPI_TIMING_CFG);
  139. }
  140. static int hisi_spi_nor_prep(struct spi_nor *nor, enum spi_nor_ops ops)
  141. {
  142. struct hifmc_priv *priv = nor->priv;
  143. struct hifmc_host *host = priv->host;
  144. int ret;
  145. mutex_lock(&host->lock);
  146. ret = clk_set_rate(host->clk, priv->clkrate);
  147. if (ret)
  148. goto out;
  149. ret = clk_prepare_enable(host->clk);
  150. if (ret)
  151. goto out;
  152. return 0;
  153. out:
  154. mutex_unlock(&host->lock);
  155. return ret;
  156. }
  157. static void hisi_spi_nor_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
  158. {
  159. struct hifmc_priv *priv = nor->priv;
  160. struct hifmc_host *host = priv->host;
  161. clk_disable_unprepare(host->clk);
  162. mutex_unlock(&host->lock);
  163. }
  164. static int hisi_spi_nor_op_reg(struct spi_nor *nor,
  165. u8 opcode, int len, u8 optype)
  166. {
  167. struct hifmc_priv *priv = nor->priv;
  168. struct hifmc_host *host = priv->host;
  169. u32 reg;
  170. reg = FMC_CMD_CMD1(opcode);
  171. writel(reg, host->regbase + FMC_CMD);
  172. reg = FMC_DATA_NUM_CNT(len);
  173. writel(reg, host->regbase + FMC_DATA_NUM);
  174. reg = OP_CFG_FM_CS(priv->chipselect);
  175. writel(reg, host->regbase + FMC_OP_CFG);
  176. writel(0xff, host->regbase + FMC_INT_CLR);
  177. reg = FMC_OP_CMD1_EN | FMC_OP_REG_OP_START | optype;
  178. writel(reg, host->regbase + FMC_OP);
  179. return wait_op_finish(host);
  180. }
  181. static int hisi_spi_nor_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf,
  182. int len)
  183. {
  184. struct hifmc_priv *priv = nor->priv;
  185. struct hifmc_host *host = priv->host;
  186. int ret;
  187. ret = hisi_spi_nor_op_reg(nor, opcode, len, FMC_OP_READ_DATA_EN);
  188. if (ret)
  189. return ret;
  190. memcpy_fromio(buf, host->iobase, len);
  191. return 0;
  192. }
  193. static int hisi_spi_nor_write_reg(struct spi_nor *nor, u8 opcode,
  194. u8 *buf, int len)
  195. {
  196. struct hifmc_priv *priv = nor->priv;
  197. struct hifmc_host *host = priv->host;
  198. if (len)
  199. memcpy_toio(host->iobase, buf, len);
  200. return hisi_spi_nor_op_reg(nor, opcode, len, FMC_OP_WRITE_DATA_EN);
  201. }
  202. static int hisi_spi_nor_dma_transfer(struct spi_nor *nor, loff_t start_off,
  203. dma_addr_t dma_buf, size_t len, u8 op_type)
  204. {
  205. struct hifmc_priv *priv = nor->priv;
  206. struct hifmc_host *host = priv->host;
  207. u8 if_type = 0;
  208. u32 reg;
  209. reg = readl(host->regbase + FMC_CFG);
  210. reg &= ~(FMC_CFG_OP_MODE_MASK | SPI_NOR_ADDR_MODE_MASK);
  211. reg |= FMC_CFG_OP_MODE_NORMAL;
  212. reg |= (nor->addr_width == 4) ? SPI_NOR_ADDR_MODE_4BYTES
  213. : SPI_NOR_ADDR_MODE_3BYTES;
  214. writel(reg, host->regbase + FMC_CFG);
  215. writel(start_off, host->regbase + FMC_ADDRL);
  216. writel(dma_buf, host->regbase + FMC_DMA_SADDR_D0);
  217. writel(FMC_DMA_LEN_SET(len), host->regbase + FMC_DMA_LEN);
  218. reg = OP_CFG_FM_CS(priv->chipselect);
  219. if_type = get_if_type(nor->flash_read);
  220. reg |= OP_CFG_MEM_IF_TYPE(if_type);
  221. if (op_type == FMC_OP_READ)
  222. reg |= OP_CFG_DUMMY_NUM(nor->read_dummy >> 3);
  223. writel(reg, host->regbase + FMC_OP_CFG);
  224. writel(0xff, host->regbase + FMC_INT_CLR);
  225. reg = OP_CTRL_RW_OP(op_type) | OP_CTRL_DMA_OP_READY;
  226. reg |= (op_type == FMC_OP_READ)
  227. ? OP_CTRL_RD_OPCODE(nor->read_opcode)
  228. : OP_CTRL_WR_OPCODE(nor->program_opcode);
  229. writel(reg, host->regbase + FMC_OP_DMA);
  230. return wait_op_finish(host);
  231. }
  232. static ssize_t hisi_spi_nor_read(struct spi_nor *nor, loff_t from, size_t len,
  233. u_char *read_buf)
  234. {
  235. struct hifmc_priv *priv = nor->priv;
  236. struct hifmc_host *host = priv->host;
  237. size_t offset;
  238. int ret;
  239. for (offset = 0; offset < len; offset += HIFMC_DMA_MAX_LEN) {
  240. size_t trans = min_t(size_t, HIFMC_DMA_MAX_LEN, len - offset);
  241. ret = hisi_spi_nor_dma_transfer(nor,
  242. from + offset, host->dma_buffer, trans, FMC_OP_READ);
  243. if (ret) {
  244. dev_warn(nor->dev, "DMA read timeout\n");
  245. return ret;
  246. }
  247. memcpy(read_buf + offset, host->buffer, trans);
  248. }
  249. return len;
  250. }
  251. static ssize_t hisi_spi_nor_write(struct spi_nor *nor, loff_t to,
  252. size_t len, const u_char *write_buf)
  253. {
  254. struct hifmc_priv *priv = nor->priv;
  255. struct hifmc_host *host = priv->host;
  256. size_t offset;
  257. int ret;
  258. for (offset = 0; offset < len; offset += HIFMC_DMA_MAX_LEN) {
  259. size_t trans = min_t(size_t, HIFMC_DMA_MAX_LEN, len - offset);
  260. memcpy(host->buffer, write_buf + offset, trans);
  261. ret = hisi_spi_nor_dma_transfer(nor,
  262. to + offset, host->dma_buffer, trans, FMC_OP_WRITE);
  263. if (ret) {
  264. dev_warn(nor->dev, "DMA write timeout\n");
  265. return ret;
  266. }
  267. }
  268. return len;
  269. }
  270. /**
  271. * Get spi flash device information and register it as a mtd device.
  272. */
  273. static int hisi_spi_nor_register(struct device_node *np,
  274. struct hifmc_host *host)
  275. {
  276. struct device *dev = host->dev;
  277. struct spi_nor *nor;
  278. struct hifmc_priv *priv;
  279. struct mtd_info *mtd;
  280. int ret;
  281. nor = devm_kzalloc(dev, sizeof(*nor), GFP_KERNEL);
  282. if (!nor)
  283. return -ENOMEM;
  284. nor->dev = dev;
  285. spi_nor_set_flash_node(nor, np);
  286. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  287. if (!priv)
  288. return -ENOMEM;
  289. ret = of_property_read_u32(np, "reg", &priv->chipselect);
  290. if (ret) {
  291. dev_err(dev, "There's no reg property for %s\n",
  292. np->full_name);
  293. return ret;
  294. }
  295. ret = of_property_read_u32(np, "spi-max-frequency",
  296. &priv->clkrate);
  297. if (ret) {
  298. dev_err(dev, "There's no spi-max-frequency property for %s\n",
  299. np->full_name);
  300. return ret;
  301. }
  302. priv->host = host;
  303. nor->priv = priv;
  304. nor->prepare = hisi_spi_nor_prep;
  305. nor->unprepare = hisi_spi_nor_unprep;
  306. nor->read_reg = hisi_spi_nor_read_reg;
  307. nor->write_reg = hisi_spi_nor_write_reg;
  308. nor->read = hisi_spi_nor_read;
  309. nor->write = hisi_spi_nor_write;
  310. nor->erase = NULL;
  311. ret = spi_nor_scan(nor, NULL, SPI_NOR_QUAD);
  312. if (ret)
  313. return ret;
  314. mtd = &nor->mtd;
  315. mtd->name = np->name;
  316. ret = mtd_device_register(mtd, NULL, 0);
  317. if (ret)
  318. return ret;
  319. host->nor[host->num_chip] = nor;
  320. host->num_chip++;
  321. return 0;
  322. }
  323. static void hisi_spi_nor_unregister_all(struct hifmc_host *host)
  324. {
  325. int i;
  326. for (i = 0; i < host->num_chip; i++)
  327. mtd_device_unregister(&host->nor[i]->mtd);
  328. }
  329. static int hisi_spi_nor_register_all(struct hifmc_host *host)
  330. {
  331. struct device *dev = host->dev;
  332. struct device_node *np;
  333. int ret;
  334. for_each_available_child_of_node(dev->of_node, np) {
  335. ret = hisi_spi_nor_register(np, host);
  336. if (ret)
  337. goto fail;
  338. if (host->num_chip == HIFMC_MAX_CHIP_NUM) {
  339. dev_warn(dev, "Flash device number exceeds the maximum chipselect number\n");
  340. break;
  341. }
  342. }
  343. return 0;
  344. fail:
  345. hisi_spi_nor_unregister_all(host);
  346. return ret;
  347. }
  348. static int hisi_spi_nor_probe(struct platform_device *pdev)
  349. {
  350. struct device *dev = &pdev->dev;
  351. struct resource *res;
  352. struct hifmc_host *host;
  353. int ret;
  354. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  355. if (!host)
  356. return -ENOMEM;
  357. platform_set_drvdata(pdev, host);
  358. host->dev = dev;
  359. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "control");
  360. host->regbase = devm_ioremap_resource(dev, res);
  361. if (IS_ERR(host->regbase))
  362. return PTR_ERR(host->regbase);
  363. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "memory");
  364. host->iobase = devm_ioremap_resource(dev, res);
  365. if (IS_ERR(host->iobase))
  366. return PTR_ERR(host->iobase);
  367. host->clk = devm_clk_get(dev, NULL);
  368. if (IS_ERR(host->clk))
  369. return PTR_ERR(host->clk);
  370. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  371. if (ret) {
  372. dev_warn(dev, "Unable to set dma mask\n");
  373. return ret;
  374. }
  375. host->buffer = dmam_alloc_coherent(dev, HIFMC_DMA_MAX_LEN,
  376. &host->dma_buffer, GFP_KERNEL);
  377. if (!host->buffer)
  378. return -ENOMEM;
  379. mutex_init(&host->lock);
  380. clk_prepare_enable(host->clk);
  381. hisi_spi_nor_init(host);
  382. ret = hisi_spi_nor_register_all(host);
  383. if (ret)
  384. mutex_destroy(&host->lock);
  385. clk_disable_unprepare(host->clk);
  386. return ret;
  387. }
  388. static int hisi_spi_nor_remove(struct platform_device *pdev)
  389. {
  390. struct hifmc_host *host = platform_get_drvdata(pdev);
  391. hisi_spi_nor_unregister_all(host);
  392. mutex_destroy(&host->lock);
  393. clk_disable_unprepare(host->clk);
  394. return 0;
  395. }
  396. static const struct of_device_id hisi_spi_nor_dt_ids[] = {
  397. { .compatible = "hisilicon,fmc-spi-nor"},
  398. { /* sentinel */ }
  399. };
  400. MODULE_DEVICE_TABLE(of, hisi_spi_nor_dt_ids);
  401. static struct platform_driver hisi_spi_nor_driver = {
  402. .driver = {
  403. .name = "hisi-sfc",
  404. .of_match_table = hisi_spi_nor_dt_ids,
  405. },
  406. .probe = hisi_spi_nor_probe,
  407. .remove = hisi_spi_nor_remove,
  408. };
  409. module_platform_driver(hisi_spi_nor_driver);
  410. MODULE_LICENSE("GPL v2");
  411. MODULE_DESCRIPTION("HiSilicon SPI Nor Flash Controller Driver");