spi-hisi-sfc-v3xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // HiSilicon SPI NOR V3XX Flash Controller Driver for hi16xx chipsets
  4. //
  5. // Copyright (c) 2019 HiSilicon Technologies Co., Ltd.
  6. // Author: John Garry <[email protected]>
  7. #include <linux/bitops.h>
  8. #include <linux/completion.h>
  9. #include <linux/dmi.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/module.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/spi/spi-mem.h>
  18. #define HISI_SFC_V3XX_VERSION (0x1f8)
  19. #define HISI_SFC_V3XX_GLB_CFG (0x100)
  20. #define HISI_SFC_V3XX_GLB_CFG_CS0_ADDR_MODE BIT(2)
  21. #define HISI_SFC_V3XX_RAW_INT_STAT (0x120)
  22. #define HISI_SFC_V3XX_INT_STAT (0x124)
  23. #define HISI_SFC_V3XX_INT_MASK (0x128)
  24. #define HISI_SFC_V3XX_INT_CLR (0x12c)
  25. #define HISI_SFC_V3XX_CMD_CFG (0x300)
  26. #define HISI_SFC_V3XX_CMD_CFG_DATA_CNT_OFF 9
  27. #define HISI_SFC_V3XX_CMD_CFG_RW_MSK BIT(8)
  28. #define HISI_SFC_V3XX_CMD_CFG_DATA_EN_MSK BIT(7)
  29. #define HISI_SFC_V3XX_CMD_CFG_DUMMY_CNT_OFF 4
  30. #define HISI_SFC_V3XX_CMD_CFG_ADDR_EN_MSK BIT(3)
  31. #define HISI_SFC_V3XX_CMD_CFG_CS_SEL_OFF 1
  32. #define HISI_SFC_V3XX_CMD_CFG_START_MSK BIT(0)
  33. #define HISI_SFC_V3XX_CMD_INS (0x308)
  34. #define HISI_SFC_V3XX_CMD_ADDR (0x30c)
  35. #define HISI_SFC_V3XX_CMD_DATABUF0 (0x400)
  36. /* Common definition of interrupt bit masks */
  37. #define HISI_SFC_V3XX_INT_MASK_ALL (0x1ff) /* all the masks */
  38. #define HISI_SFC_V3XX_INT_MASK_CPLT BIT(0) /* command execution complete */
  39. #define HISI_SFC_V3XX_INT_MASK_PP_ERR BIT(2) /* page progrom error */
  40. #define HISI_SFC_V3XX_INT_MASK_IACCES BIT(5) /* error visiting inaccessible/
  41. * protected address
  42. */
  43. /* IO Mode definition in HISI_SFC_V3XX_CMD_CFG */
  44. #define HISI_SFC_V3XX_STD (0 << 17)
  45. #define HISI_SFC_V3XX_DIDO (1 << 17)
  46. #define HISI_SFC_V3XX_DIO (2 << 17)
  47. #define HISI_SFC_V3XX_FULL_DIO (3 << 17)
  48. #define HISI_SFC_V3XX_QIQO (5 << 17)
  49. #define HISI_SFC_V3XX_QIO (6 << 17)
  50. #define HISI_SFC_V3XX_FULL_QIO (7 << 17)
  51. /*
  52. * The IO modes lookup table. hisi_sfc_v3xx_io_modes[(z - 1) / 2][y / 2][x / 2]
  53. * stands for x-y-z mode, as described in SFDP terminology. -EIO indicates
  54. * an invalid mode.
  55. */
  56. static const int hisi_sfc_v3xx_io_modes[2][3][3] = {
  57. {
  58. { HISI_SFC_V3XX_DIDO, HISI_SFC_V3XX_DIDO, HISI_SFC_V3XX_DIDO },
  59. { HISI_SFC_V3XX_DIO, HISI_SFC_V3XX_FULL_DIO, -EIO },
  60. { -EIO, -EIO, -EIO },
  61. },
  62. {
  63. { HISI_SFC_V3XX_QIQO, HISI_SFC_V3XX_QIQO, HISI_SFC_V3XX_QIQO },
  64. { -EIO, -EIO, -EIO },
  65. { HISI_SFC_V3XX_QIO, -EIO, HISI_SFC_V3XX_FULL_QIO },
  66. },
  67. };
  68. struct hisi_sfc_v3xx_host {
  69. struct device *dev;
  70. void __iomem *regbase;
  71. int max_cmd_dword;
  72. struct completion *completion;
  73. u8 address_mode;
  74. int irq;
  75. };
  76. static void hisi_sfc_v3xx_disable_int(struct hisi_sfc_v3xx_host *host)
  77. {
  78. writel(0, host->regbase + HISI_SFC_V3XX_INT_MASK);
  79. }
  80. static void hisi_sfc_v3xx_enable_int(struct hisi_sfc_v3xx_host *host)
  81. {
  82. writel(HISI_SFC_V3XX_INT_MASK_ALL, host->regbase + HISI_SFC_V3XX_INT_MASK);
  83. }
  84. static void hisi_sfc_v3xx_clear_int(struct hisi_sfc_v3xx_host *host)
  85. {
  86. writel(HISI_SFC_V3XX_INT_MASK_ALL, host->regbase + HISI_SFC_V3XX_INT_CLR);
  87. }
  88. /*
  89. * The interrupt status register indicates whether an error occurs
  90. * after per operation. Check it, and clear the interrupts for
  91. * next time judgement.
  92. */
  93. static int hisi_sfc_v3xx_handle_completion(struct hisi_sfc_v3xx_host *host)
  94. {
  95. u32 reg;
  96. reg = readl(host->regbase + HISI_SFC_V3XX_RAW_INT_STAT);
  97. hisi_sfc_v3xx_clear_int(host);
  98. if (reg & HISI_SFC_V3XX_INT_MASK_IACCES) {
  99. dev_err(host->dev, "fail to access protected address\n");
  100. return -EIO;
  101. }
  102. if (reg & HISI_SFC_V3XX_INT_MASK_PP_ERR) {
  103. dev_err(host->dev, "page program operation failed\n");
  104. return -EIO;
  105. }
  106. /*
  107. * The other bits of the interrupt registers is not currently
  108. * used and probably not be triggered in this driver. When it
  109. * happens, we regard it as an unsupported error here.
  110. */
  111. if (!(reg & HISI_SFC_V3XX_INT_MASK_CPLT)) {
  112. dev_err(host->dev, "unsupported error occurred, status=0x%x\n", reg);
  113. return -EIO;
  114. }
  115. return 0;
  116. }
  117. #define HISI_SFC_V3XX_WAIT_TIMEOUT_US 1000000
  118. #define HISI_SFC_V3XX_WAIT_POLL_INTERVAL_US 10
  119. static int hisi_sfc_v3xx_wait_cmd_idle(struct hisi_sfc_v3xx_host *host)
  120. {
  121. u32 reg;
  122. return readl_poll_timeout(host->regbase + HISI_SFC_V3XX_CMD_CFG, reg,
  123. !(reg & HISI_SFC_V3XX_CMD_CFG_START_MSK),
  124. HISI_SFC_V3XX_WAIT_POLL_INTERVAL_US,
  125. HISI_SFC_V3XX_WAIT_TIMEOUT_US);
  126. }
  127. static int hisi_sfc_v3xx_adjust_op_size(struct spi_mem *mem,
  128. struct spi_mem_op *op)
  129. {
  130. struct spi_device *spi = mem->spi;
  131. struct hisi_sfc_v3xx_host *host;
  132. uintptr_t addr = (uintptr_t)op->data.buf.in;
  133. int max_byte_count;
  134. host = spi_controller_get_devdata(spi->master);
  135. max_byte_count = host->max_cmd_dword * 4;
  136. if (!IS_ALIGNED(addr, 4) && op->data.nbytes >= 4)
  137. op->data.nbytes = 4 - (addr % 4);
  138. else if (op->data.nbytes > max_byte_count)
  139. op->data.nbytes = max_byte_count;
  140. return 0;
  141. }
  142. /*
  143. * The controller only supports Standard SPI mode, Duall mode and
  144. * Quad mode. Double sanitize the ops here to avoid OOB access.
  145. */
  146. static bool hisi_sfc_v3xx_supports_op(struct spi_mem *mem,
  147. const struct spi_mem_op *op)
  148. {
  149. struct spi_device *spi = mem->spi;
  150. struct hisi_sfc_v3xx_host *host;
  151. host = spi_controller_get_devdata(spi->master);
  152. if (op->data.buswidth > 4 || op->dummy.buswidth > 4 ||
  153. op->addr.buswidth > 4 || op->cmd.buswidth > 4)
  154. return false;
  155. if (op->addr.nbytes != host->address_mode && op->addr.nbytes)
  156. return false;
  157. return spi_mem_default_supports_op(mem, op);
  158. }
  159. /*
  160. * memcpy_{to,from}io doesn't gurantee 32b accesses - which we require for the
  161. * DATABUF registers -so use __io{read,write}32_copy when possible. For
  162. * trailing bytes, copy them byte-by-byte from the DATABUF register, as we
  163. * can't clobber outside the source/dest buffer.
  164. *
  165. * For efficient data read/write, we try to put any start 32b unaligned data
  166. * into a separate transaction in hisi_sfc_v3xx_adjust_op_size().
  167. */
  168. static void hisi_sfc_v3xx_read_databuf(struct hisi_sfc_v3xx_host *host,
  169. u8 *to, unsigned int len)
  170. {
  171. void __iomem *from;
  172. int i;
  173. from = host->regbase + HISI_SFC_V3XX_CMD_DATABUF0;
  174. if (IS_ALIGNED((uintptr_t)to, 4)) {
  175. int words = len / 4;
  176. __ioread32_copy(to, from, words);
  177. len -= words * 4;
  178. if (len) {
  179. u32 val;
  180. to += words * 4;
  181. from += words * 4;
  182. val = __raw_readl(from);
  183. for (i = 0; i < len; i++, val >>= 8, to++)
  184. *to = (u8)val;
  185. }
  186. } else {
  187. for (i = 0; i < DIV_ROUND_UP(len, 4); i++, from += 4) {
  188. u32 val = __raw_readl(from);
  189. int j;
  190. for (j = 0; j < 4 && (j + (i * 4) < len);
  191. to++, val >>= 8, j++)
  192. *to = (u8)val;
  193. }
  194. }
  195. }
  196. static void hisi_sfc_v3xx_write_databuf(struct hisi_sfc_v3xx_host *host,
  197. const u8 *from, unsigned int len)
  198. {
  199. void __iomem *to;
  200. int i;
  201. to = host->regbase + HISI_SFC_V3XX_CMD_DATABUF0;
  202. if (IS_ALIGNED((uintptr_t)from, 4)) {
  203. int words = len / 4;
  204. __iowrite32_copy(to, from, words);
  205. len -= words * 4;
  206. if (len) {
  207. u32 val = 0;
  208. to += words * 4;
  209. from += words * 4;
  210. for (i = 0; i < len; i++, from++)
  211. val |= *from << i * 8;
  212. __raw_writel(val, to);
  213. }
  214. } else {
  215. for (i = 0; i < DIV_ROUND_UP(len, 4); i++, to += 4) {
  216. u32 val = 0;
  217. int j;
  218. for (j = 0; j < 4 && (j + (i * 4) < len);
  219. from++, j++)
  220. val |= *from << j * 8;
  221. __raw_writel(val, to);
  222. }
  223. }
  224. }
  225. static int hisi_sfc_v3xx_start_bus(struct hisi_sfc_v3xx_host *host,
  226. const struct spi_mem_op *op,
  227. u8 chip_select)
  228. {
  229. int len = op->data.nbytes, buswidth_mode;
  230. u32 config = 0;
  231. if (op->addr.nbytes)
  232. config |= HISI_SFC_V3XX_CMD_CFG_ADDR_EN_MSK;
  233. if (op->data.buswidth == 0 || op->data.buswidth == 1) {
  234. buswidth_mode = HISI_SFC_V3XX_STD;
  235. } else {
  236. int data_idx, addr_idx, cmd_idx;
  237. data_idx = (op->data.buswidth - 1) / 2;
  238. addr_idx = op->addr.buswidth / 2;
  239. cmd_idx = op->cmd.buswidth / 2;
  240. buswidth_mode = hisi_sfc_v3xx_io_modes[data_idx][addr_idx][cmd_idx];
  241. }
  242. if (buswidth_mode < 0)
  243. return buswidth_mode;
  244. config |= buswidth_mode;
  245. if (op->data.dir != SPI_MEM_NO_DATA) {
  246. config |= (len - 1) << HISI_SFC_V3XX_CMD_CFG_DATA_CNT_OFF;
  247. config |= HISI_SFC_V3XX_CMD_CFG_DATA_EN_MSK;
  248. }
  249. if (op->data.dir == SPI_MEM_DATA_IN)
  250. config |= HISI_SFC_V3XX_CMD_CFG_RW_MSK;
  251. config |= op->dummy.nbytes << HISI_SFC_V3XX_CMD_CFG_DUMMY_CNT_OFF |
  252. chip_select << HISI_SFC_V3XX_CMD_CFG_CS_SEL_OFF |
  253. HISI_SFC_V3XX_CMD_CFG_START_MSK;
  254. writel(op->addr.val, host->regbase + HISI_SFC_V3XX_CMD_ADDR);
  255. writel(op->cmd.opcode, host->regbase + HISI_SFC_V3XX_CMD_INS);
  256. writel(config, host->regbase + HISI_SFC_V3XX_CMD_CFG);
  257. return 0;
  258. }
  259. static int hisi_sfc_v3xx_generic_exec_op(struct hisi_sfc_v3xx_host *host,
  260. const struct spi_mem_op *op,
  261. u8 chip_select)
  262. {
  263. DECLARE_COMPLETION_ONSTACK(done);
  264. int ret;
  265. if (host->irq) {
  266. host->completion = &done;
  267. hisi_sfc_v3xx_enable_int(host);
  268. }
  269. if (op->data.dir == SPI_MEM_DATA_OUT)
  270. hisi_sfc_v3xx_write_databuf(host, op->data.buf.out, op->data.nbytes);
  271. ret = hisi_sfc_v3xx_start_bus(host, op, chip_select);
  272. if (ret)
  273. return ret;
  274. if (host->irq) {
  275. ret = wait_for_completion_timeout(host->completion,
  276. usecs_to_jiffies(HISI_SFC_V3XX_WAIT_TIMEOUT_US));
  277. if (!ret)
  278. ret = -ETIMEDOUT;
  279. else
  280. ret = 0;
  281. hisi_sfc_v3xx_disable_int(host);
  282. synchronize_irq(host->irq);
  283. host->completion = NULL;
  284. } else {
  285. ret = hisi_sfc_v3xx_wait_cmd_idle(host);
  286. }
  287. if (hisi_sfc_v3xx_handle_completion(host) || ret)
  288. return -EIO;
  289. if (op->data.dir == SPI_MEM_DATA_IN)
  290. hisi_sfc_v3xx_read_databuf(host, op->data.buf.in, op->data.nbytes);
  291. return 0;
  292. }
  293. static int hisi_sfc_v3xx_exec_op(struct spi_mem *mem,
  294. const struct spi_mem_op *op)
  295. {
  296. struct hisi_sfc_v3xx_host *host;
  297. struct spi_device *spi = mem->spi;
  298. u8 chip_select = spi->chip_select;
  299. host = spi_controller_get_devdata(spi->master);
  300. return hisi_sfc_v3xx_generic_exec_op(host, op, chip_select);
  301. }
  302. static const struct spi_controller_mem_ops hisi_sfc_v3xx_mem_ops = {
  303. .adjust_op_size = hisi_sfc_v3xx_adjust_op_size,
  304. .supports_op = hisi_sfc_v3xx_supports_op,
  305. .exec_op = hisi_sfc_v3xx_exec_op,
  306. };
  307. static irqreturn_t hisi_sfc_v3xx_isr(int irq, void *data)
  308. {
  309. struct hisi_sfc_v3xx_host *host = data;
  310. hisi_sfc_v3xx_disable_int(host);
  311. complete(host->completion);
  312. return IRQ_HANDLED;
  313. }
  314. static int hisi_sfc_v3xx_buswidth_override_bits;
  315. /*
  316. * ACPI FW does not allow us to currently set the device buswidth, so quirk it
  317. * depending on the board.
  318. */
  319. static int __init hisi_sfc_v3xx_dmi_quirk(const struct dmi_system_id *d)
  320. {
  321. hisi_sfc_v3xx_buswidth_override_bits = SPI_RX_QUAD | SPI_TX_QUAD;
  322. return 0;
  323. }
  324. static const struct dmi_system_id hisi_sfc_v3xx_dmi_quirk_table[] = {
  325. {
  326. .callback = hisi_sfc_v3xx_dmi_quirk,
  327. .matches = {
  328. DMI_MATCH(DMI_SYS_VENDOR, "Huawei"),
  329. DMI_MATCH(DMI_PRODUCT_NAME, "D06"),
  330. },
  331. },
  332. {
  333. .callback = hisi_sfc_v3xx_dmi_quirk,
  334. .matches = {
  335. DMI_MATCH(DMI_SYS_VENDOR, "Huawei"),
  336. DMI_MATCH(DMI_PRODUCT_NAME, "TaiShan 2280 V2"),
  337. },
  338. },
  339. {
  340. .callback = hisi_sfc_v3xx_dmi_quirk,
  341. .matches = {
  342. DMI_MATCH(DMI_SYS_VENDOR, "Huawei"),
  343. DMI_MATCH(DMI_PRODUCT_NAME, "TaiShan 200 (Model 2280)"),
  344. },
  345. },
  346. {}
  347. };
  348. static int hisi_sfc_v3xx_probe(struct platform_device *pdev)
  349. {
  350. struct device *dev = &pdev->dev;
  351. struct hisi_sfc_v3xx_host *host;
  352. struct spi_controller *ctlr;
  353. u32 version, glb_config;
  354. int ret;
  355. ctlr = spi_alloc_master(&pdev->dev, sizeof(*host));
  356. if (!ctlr)
  357. return -ENOMEM;
  358. ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD |
  359. SPI_TX_DUAL | SPI_TX_QUAD;
  360. ctlr->buswidth_override_bits = hisi_sfc_v3xx_buswidth_override_bits;
  361. host = spi_controller_get_devdata(ctlr);
  362. host->dev = dev;
  363. platform_set_drvdata(pdev, host);
  364. host->regbase = devm_platform_ioremap_resource(pdev, 0);
  365. if (IS_ERR(host->regbase)) {
  366. ret = PTR_ERR(host->regbase);
  367. goto err_put_master;
  368. }
  369. host->irq = platform_get_irq_optional(pdev, 0);
  370. if (host->irq == -EPROBE_DEFER) {
  371. ret = -EPROBE_DEFER;
  372. goto err_put_master;
  373. }
  374. hisi_sfc_v3xx_disable_int(host);
  375. if (host->irq > 0) {
  376. ret = devm_request_irq(dev, host->irq, hisi_sfc_v3xx_isr, 0,
  377. "hisi-sfc-v3xx", host);
  378. if (ret) {
  379. dev_err(dev, "failed to request irq%d, ret = %d\n", host->irq, ret);
  380. host->irq = 0;
  381. }
  382. } else {
  383. host->irq = 0;
  384. }
  385. ctlr->bus_num = -1;
  386. ctlr->num_chipselect = 1;
  387. ctlr->mem_ops = &hisi_sfc_v3xx_mem_ops;
  388. /*
  389. * The address mode of the controller is either 3 or 4,
  390. * which is indicated by the address mode bit in
  391. * the global config register. The register is read only
  392. * for the OS driver.
  393. */
  394. glb_config = readl(host->regbase + HISI_SFC_V3XX_GLB_CFG);
  395. if (glb_config & HISI_SFC_V3XX_GLB_CFG_CS0_ADDR_MODE)
  396. host->address_mode = 4;
  397. else
  398. host->address_mode = 3;
  399. version = readl(host->regbase + HISI_SFC_V3XX_VERSION);
  400. if (version >= 0x351)
  401. host->max_cmd_dword = 64;
  402. else
  403. host->max_cmd_dword = 16;
  404. ret = devm_spi_register_controller(dev, ctlr);
  405. if (ret)
  406. goto err_put_master;
  407. dev_info(&pdev->dev, "hw version 0x%x, %s mode.\n",
  408. version, host->irq ? "irq" : "polling");
  409. return 0;
  410. err_put_master:
  411. spi_master_put(ctlr);
  412. return ret;
  413. }
  414. static const struct acpi_device_id hisi_sfc_v3xx_acpi_ids[] = {
  415. {"HISI0341", 0},
  416. {}
  417. };
  418. MODULE_DEVICE_TABLE(acpi, hisi_sfc_v3xx_acpi_ids);
  419. static struct platform_driver hisi_sfc_v3xx_spi_driver = {
  420. .driver = {
  421. .name = "hisi-sfc-v3xx",
  422. .acpi_match_table = hisi_sfc_v3xx_acpi_ids,
  423. },
  424. .probe = hisi_sfc_v3xx_probe,
  425. };
  426. static int __init hisi_sfc_v3xx_spi_init(void)
  427. {
  428. dmi_check_system(hisi_sfc_v3xx_dmi_quirk_table);
  429. return platform_driver_register(&hisi_sfc_v3xx_spi_driver);
  430. }
  431. static void __exit hisi_sfc_v3xx_spi_exit(void)
  432. {
  433. platform_driver_unregister(&hisi_sfc_v3xx_spi_driver);
  434. }
  435. module_init(hisi_sfc_v3xx_spi_init);
  436. module_exit(hisi_sfc_v3xx_spi_exit);
  437. MODULE_LICENSE("GPL");
  438. MODULE_AUTHOR("John Garry <[email protected]>");
  439. MODULE_DESCRIPTION("HiSilicon SPI NOR V3XX Flash Controller Driver for hi16xx chipsets");