spi-mxic.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (C) 2018 Macronix International Co., Ltd.
  4. //
  5. // Authors:
  6. // Mason Yang <[email protected]>
  7. // zhengxunli <[email protected]>
  8. // Boris Brezillon <[email protected]>
  9. //
  10. #include <linux/clk.h>
  11. #include <linux/io.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/module.h>
  14. #include <linux/mtd/nand.h>
  15. #include <linux/mtd/nand-ecc-mxic.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/spi/spi-mem.h>
  20. #define HC_CFG 0x0
  21. #define HC_CFG_IF_CFG(x) ((x) << 27)
  22. #define HC_CFG_DUAL_SLAVE BIT(31)
  23. #define HC_CFG_INDIVIDUAL BIT(30)
  24. #define HC_CFG_NIO(x) (((x) / 4) << 27)
  25. #define HC_CFG_TYPE(s, t) ((t) << (23 + ((s) * 2)))
  26. #define HC_CFG_TYPE_SPI_NOR 0
  27. #define HC_CFG_TYPE_SPI_NAND 1
  28. #define HC_CFG_TYPE_SPI_RAM 2
  29. #define HC_CFG_TYPE_RAW_NAND 3
  30. #define HC_CFG_SLV_ACT(x) ((x) << 21)
  31. #define HC_CFG_CLK_PH_EN BIT(20)
  32. #define HC_CFG_CLK_POL_INV BIT(19)
  33. #define HC_CFG_BIG_ENDIAN BIT(18)
  34. #define HC_CFG_DATA_PASS BIT(17)
  35. #define HC_CFG_IDLE_SIO_LVL(x) ((x) << 16)
  36. #define HC_CFG_MAN_START_EN BIT(3)
  37. #define HC_CFG_MAN_START BIT(2)
  38. #define HC_CFG_MAN_CS_EN BIT(1)
  39. #define HC_CFG_MAN_CS_ASSERT BIT(0)
  40. #define INT_STS 0x4
  41. #define INT_STS_EN 0x8
  42. #define INT_SIG_EN 0xc
  43. #define INT_STS_ALL GENMASK(31, 0)
  44. #define INT_RDY_PIN BIT(26)
  45. #define INT_RDY_SR BIT(25)
  46. #define INT_LNR_SUSP BIT(24)
  47. #define INT_ECC_ERR BIT(17)
  48. #define INT_CRC_ERR BIT(16)
  49. #define INT_LWR_DIS BIT(12)
  50. #define INT_LRD_DIS BIT(11)
  51. #define INT_SDMA_INT BIT(10)
  52. #define INT_DMA_FINISH BIT(9)
  53. #define INT_RX_NOT_FULL BIT(3)
  54. #define INT_RX_NOT_EMPTY BIT(2)
  55. #define INT_TX_NOT_FULL BIT(1)
  56. #define INT_TX_EMPTY BIT(0)
  57. #define HC_EN 0x10
  58. #define HC_EN_BIT BIT(0)
  59. #define TXD(x) (0x14 + ((x) * 4))
  60. #define RXD 0x24
  61. #define SS_CTRL(s) (0x30 + ((s) * 4))
  62. #define LRD_CFG 0x44
  63. #define LWR_CFG 0x80
  64. #define RWW_CFG 0x70
  65. #define OP_READ BIT(23)
  66. #define OP_DUMMY_CYC(x) ((x) << 17)
  67. #define OP_ADDR_BYTES(x) ((x) << 14)
  68. #define OP_CMD_BYTES(x) (((x) - 1) << 13)
  69. #define OP_OCTA_CRC_EN BIT(12)
  70. #define OP_DQS_EN BIT(11)
  71. #define OP_ENHC_EN BIT(10)
  72. #define OP_PREAMBLE_EN BIT(9)
  73. #define OP_DATA_DDR BIT(8)
  74. #define OP_DATA_BUSW(x) ((x) << 6)
  75. #define OP_ADDR_DDR BIT(5)
  76. #define OP_ADDR_BUSW(x) ((x) << 3)
  77. #define OP_CMD_DDR BIT(2)
  78. #define OP_CMD_BUSW(x) (x)
  79. #define OP_BUSW_1 0
  80. #define OP_BUSW_2 1
  81. #define OP_BUSW_4 2
  82. #define OP_BUSW_8 3
  83. #define OCTA_CRC 0x38
  84. #define OCTA_CRC_IN_EN(s) BIT(3 + ((s) * 16))
  85. #define OCTA_CRC_CHUNK(s, x) ((fls((x) / 32)) << (1 + ((s) * 16)))
  86. #define OCTA_CRC_OUT_EN(s) BIT(0 + ((s) * 16))
  87. #define ONFI_DIN_CNT(s) (0x3c + (s))
  88. #define LRD_CTRL 0x48
  89. #define RWW_CTRL 0x74
  90. #define LWR_CTRL 0x84
  91. #define LMODE_EN BIT(31)
  92. #define LMODE_SLV_ACT(x) ((x) << 21)
  93. #define LMODE_CMD1(x) ((x) << 8)
  94. #define LMODE_CMD0(x) (x)
  95. #define LRD_ADDR 0x4c
  96. #define LWR_ADDR 0x88
  97. #define LRD_RANGE 0x50
  98. #define LWR_RANGE 0x8c
  99. #define AXI_SLV_ADDR 0x54
  100. #define DMAC_RD_CFG 0x58
  101. #define DMAC_WR_CFG 0x94
  102. #define DMAC_CFG_PERIPH_EN BIT(31)
  103. #define DMAC_CFG_ALLFLUSH_EN BIT(30)
  104. #define DMAC_CFG_LASTFLUSH_EN BIT(29)
  105. #define DMAC_CFG_QE(x) (((x) + 1) << 16)
  106. #define DMAC_CFG_BURST_LEN(x) (((x) + 1) << 12)
  107. #define DMAC_CFG_BURST_SZ(x) ((x) << 8)
  108. #define DMAC_CFG_DIR_READ BIT(1)
  109. #define DMAC_CFG_START BIT(0)
  110. #define DMAC_RD_CNT 0x5c
  111. #define DMAC_WR_CNT 0x98
  112. #define SDMA_ADDR 0x60
  113. #define DMAM_CFG 0x64
  114. #define DMAM_CFG_START BIT(31)
  115. #define DMAM_CFG_CONT BIT(30)
  116. #define DMAM_CFG_SDMA_GAP(x) (fls((x) / 8192) << 2)
  117. #define DMAM_CFG_DIR_READ BIT(1)
  118. #define DMAM_CFG_EN BIT(0)
  119. #define DMAM_CNT 0x68
  120. #define LNR_TIMER_TH 0x6c
  121. #define RDM_CFG0 0x78
  122. #define RDM_CFG0_POLY(x) (x)
  123. #define RDM_CFG1 0x7c
  124. #define RDM_CFG1_RDM_EN BIT(31)
  125. #define RDM_CFG1_SEED(x) (x)
  126. #define LWR_SUSP_CTRL 0x90
  127. #define LWR_SUSP_CTRL_EN BIT(31)
  128. #define DMAS_CTRL 0x9c
  129. #define DMAS_CTRL_EN BIT(31)
  130. #define DMAS_CTRL_DIR_READ BIT(30)
  131. #define DATA_STROB 0xa0
  132. #define DATA_STROB_EDO_EN BIT(2)
  133. #define DATA_STROB_INV_POL BIT(1)
  134. #define DATA_STROB_DELAY_2CYC BIT(0)
  135. #define IDLY_CODE(x) (0xa4 + ((x) * 4))
  136. #define IDLY_CODE_VAL(x, v) ((v) << (((x) % 4) * 8))
  137. #define GPIO 0xc4
  138. #define GPIO_PT(x) BIT(3 + ((x) * 16))
  139. #define GPIO_RESET(x) BIT(2 + ((x) * 16))
  140. #define GPIO_HOLDB(x) BIT(1 + ((x) * 16))
  141. #define GPIO_WPB(x) BIT((x) * 16)
  142. #define HC_VER 0xd0
  143. #define HW_TEST(x) (0xe0 + ((x) * 4))
  144. struct mxic_spi {
  145. struct device *dev;
  146. struct clk *ps_clk;
  147. struct clk *send_clk;
  148. struct clk *send_dly_clk;
  149. void __iomem *regs;
  150. u32 cur_speed_hz;
  151. struct {
  152. void __iomem *map;
  153. dma_addr_t dma;
  154. size_t size;
  155. } linear;
  156. struct {
  157. bool use_pipelined_conf;
  158. struct nand_ecc_engine *pipelined_engine;
  159. void *ctx;
  160. } ecc;
  161. };
  162. static int mxic_spi_clk_enable(struct mxic_spi *mxic)
  163. {
  164. int ret;
  165. ret = clk_prepare_enable(mxic->send_clk);
  166. if (ret)
  167. return ret;
  168. ret = clk_prepare_enable(mxic->send_dly_clk);
  169. if (ret)
  170. goto err_send_dly_clk;
  171. return ret;
  172. err_send_dly_clk:
  173. clk_disable_unprepare(mxic->send_clk);
  174. return ret;
  175. }
  176. static void mxic_spi_clk_disable(struct mxic_spi *mxic)
  177. {
  178. clk_disable_unprepare(mxic->send_clk);
  179. clk_disable_unprepare(mxic->send_dly_clk);
  180. }
  181. static void mxic_spi_set_input_delay_dqs(struct mxic_spi *mxic, u8 idly_code)
  182. {
  183. writel(IDLY_CODE_VAL(0, idly_code) |
  184. IDLY_CODE_VAL(1, idly_code) |
  185. IDLY_CODE_VAL(2, idly_code) |
  186. IDLY_CODE_VAL(3, idly_code),
  187. mxic->regs + IDLY_CODE(0));
  188. writel(IDLY_CODE_VAL(4, idly_code) |
  189. IDLY_CODE_VAL(5, idly_code) |
  190. IDLY_CODE_VAL(6, idly_code) |
  191. IDLY_CODE_VAL(7, idly_code),
  192. mxic->regs + IDLY_CODE(1));
  193. }
  194. static int mxic_spi_clk_setup(struct mxic_spi *mxic, unsigned long freq)
  195. {
  196. int ret;
  197. ret = clk_set_rate(mxic->send_clk, freq);
  198. if (ret)
  199. return ret;
  200. ret = clk_set_rate(mxic->send_dly_clk, freq);
  201. if (ret)
  202. return ret;
  203. /*
  204. * A constant delay range from 0x0 ~ 0x1F for input delay,
  205. * the unit is 78 ps, the max input delay is 2.418 ns.
  206. */
  207. mxic_spi_set_input_delay_dqs(mxic, 0xf);
  208. /*
  209. * Phase degree = 360 * freq * output-delay
  210. * where output-delay is a constant value 1 ns in FPGA.
  211. *
  212. * Get Phase degree = 360 * freq * 1 ns
  213. * = 360 * freq * 1 sec / 1000000000
  214. * = 9 * freq / 25000000
  215. */
  216. ret = clk_set_phase(mxic->send_dly_clk, 9 * freq / 25000000);
  217. if (ret)
  218. return ret;
  219. return 0;
  220. }
  221. static int mxic_spi_set_freq(struct mxic_spi *mxic, unsigned long freq)
  222. {
  223. int ret;
  224. if (mxic->cur_speed_hz == freq)
  225. return 0;
  226. mxic_spi_clk_disable(mxic);
  227. ret = mxic_spi_clk_setup(mxic, freq);
  228. if (ret)
  229. return ret;
  230. ret = mxic_spi_clk_enable(mxic);
  231. if (ret)
  232. return ret;
  233. mxic->cur_speed_hz = freq;
  234. return 0;
  235. }
  236. static void mxic_spi_hw_init(struct mxic_spi *mxic)
  237. {
  238. writel(0, mxic->regs + DATA_STROB);
  239. writel(INT_STS_ALL, mxic->regs + INT_STS_EN);
  240. writel(0, mxic->regs + HC_EN);
  241. writel(0, mxic->regs + LRD_CFG);
  242. writel(0, mxic->regs + LRD_CTRL);
  243. writel(HC_CFG_NIO(1) | HC_CFG_TYPE(0, HC_CFG_TYPE_SPI_NOR) |
  244. HC_CFG_SLV_ACT(0) | HC_CFG_MAN_CS_EN | HC_CFG_IDLE_SIO_LVL(1),
  245. mxic->regs + HC_CFG);
  246. }
  247. static u32 mxic_spi_prep_hc_cfg(struct spi_device *spi, u32 flags)
  248. {
  249. int nio = 1;
  250. if (spi->mode & (SPI_TX_OCTAL | SPI_RX_OCTAL))
  251. nio = 8;
  252. else if (spi->mode & (SPI_TX_QUAD | SPI_RX_QUAD))
  253. nio = 4;
  254. else if (spi->mode & (SPI_TX_DUAL | SPI_RX_DUAL))
  255. nio = 2;
  256. return flags | HC_CFG_NIO(nio) |
  257. HC_CFG_TYPE(spi->chip_select, HC_CFG_TYPE_SPI_NOR) |
  258. HC_CFG_SLV_ACT(spi->chip_select) | HC_CFG_IDLE_SIO_LVL(1);
  259. }
  260. static u32 mxic_spi_mem_prep_op_cfg(const struct spi_mem_op *op,
  261. unsigned int data_len)
  262. {
  263. u32 cfg = OP_CMD_BYTES(op->cmd.nbytes) |
  264. OP_CMD_BUSW(fls(op->cmd.buswidth) - 1) |
  265. (op->cmd.dtr ? OP_CMD_DDR : 0);
  266. if (op->addr.nbytes)
  267. cfg |= OP_ADDR_BYTES(op->addr.nbytes) |
  268. OP_ADDR_BUSW(fls(op->addr.buswidth) - 1) |
  269. (op->addr.dtr ? OP_ADDR_DDR : 0);
  270. if (op->dummy.nbytes)
  271. cfg |= OP_DUMMY_CYC(op->dummy.nbytes);
  272. /* Direct mapping data.nbytes field is not populated */
  273. if (data_len) {
  274. cfg |= OP_DATA_BUSW(fls(op->data.buswidth) - 1) |
  275. (op->data.dtr ? OP_DATA_DDR : 0);
  276. if (op->data.dir == SPI_MEM_DATA_IN) {
  277. cfg |= OP_READ;
  278. if (op->data.dtr)
  279. cfg |= OP_DQS_EN;
  280. }
  281. }
  282. return cfg;
  283. }
  284. static int mxic_spi_data_xfer(struct mxic_spi *mxic, const void *txbuf,
  285. void *rxbuf, unsigned int len)
  286. {
  287. unsigned int pos = 0;
  288. while (pos < len) {
  289. unsigned int nbytes = len - pos;
  290. u32 data = 0xffffffff;
  291. u32 sts;
  292. int ret;
  293. if (nbytes > 4)
  294. nbytes = 4;
  295. if (txbuf)
  296. memcpy(&data, txbuf + pos, nbytes);
  297. ret = readl_poll_timeout(mxic->regs + INT_STS, sts,
  298. sts & INT_TX_EMPTY, 0, USEC_PER_SEC);
  299. if (ret)
  300. return ret;
  301. writel(data, mxic->regs + TXD(nbytes % 4));
  302. ret = readl_poll_timeout(mxic->regs + INT_STS, sts,
  303. sts & INT_TX_EMPTY, 0, USEC_PER_SEC);
  304. if (ret)
  305. return ret;
  306. ret = readl_poll_timeout(mxic->regs + INT_STS, sts,
  307. sts & INT_RX_NOT_EMPTY, 0,
  308. USEC_PER_SEC);
  309. if (ret)
  310. return ret;
  311. data = readl(mxic->regs + RXD);
  312. if (rxbuf) {
  313. data >>= (8 * (4 - nbytes));
  314. memcpy(rxbuf + pos, &data, nbytes);
  315. }
  316. WARN_ON(readl(mxic->regs + INT_STS) & INT_RX_NOT_EMPTY);
  317. pos += nbytes;
  318. }
  319. return 0;
  320. }
  321. static ssize_t mxic_spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
  322. u64 offs, size_t len, void *buf)
  323. {
  324. struct mxic_spi *mxic = spi_master_get_devdata(desc->mem->spi->master);
  325. int ret;
  326. u32 sts;
  327. if (WARN_ON(offs + desc->info.offset + len > U32_MAX))
  328. return -EINVAL;
  329. writel(mxic_spi_prep_hc_cfg(desc->mem->spi, 0), mxic->regs + HC_CFG);
  330. writel(mxic_spi_mem_prep_op_cfg(&desc->info.op_tmpl, len),
  331. mxic->regs + LRD_CFG);
  332. writel(desc->info.offset + offs, mxic->regs + LRD_ADDR);
  333. len = min_t(size_t, len, mxic->linear.size);
  334. writel(len, mxic->regs + LRD_RANGE);
  335. writel(LMODE_CMD0(desc->info.op_tmpl.cmd.opcode) |
  336. LMODE_SLV_ACT(desc->mem->spi->chip_select) |
  337. LMODE_EN,
  338. mxic->regs + LRD_CTRL);
  339. if (mxic->ecc.use_pipelined_conf && desc->info.op_tmpl.data.ecc) {
  340. ret = mxic_ecc_process_data_pipelined(mxic->ecc.pipelined_engine,
  341. NAND_PAGE_READ,
  342. mxic->linear.dma + offs);
  343. if (ret)
  344. return ret;
  345. } else {
  346. memcpy_fromio(buf, mxic->linear.map, len);
  347. }
  348. writel(INT_LRD_DIS, mxic->regs + INT_STS);
  349. writel(0, mxic->regs + LRD_CTRL);
  350. ret = readl_poll_timeout(mxic->regs + INT_STS, sts,
  351. sts & INT_LRD_DIS, 0, USEC_PER_SEC);
  352. if (ret)
  353. return ret;
  354. return len;
  355. }
  356. static ssize_t mxic_spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
  357. u64 offs, size_t len,
  358. const void *buf)
  359. {
  360. struct mxic_spi *mxic = spi_master_get_devdata(desc->mem->spi->master);
  361. u32 sts;
  362. int ret;
  363. if (WARN_ON(offs + desc->info.offset + len > U32_MAX))
  364. return -EINVAL;
  365. writel(mxic_spi_prep_hc_cfg(desc->mem->spi, 0), mxic->regs + HC_CFG);
  366. writel(mxic_spi_mem_prep_op_cfg(&desc->info.op_tmpl, len),
  367. mxic->regs + LWR_CFG);
  368. writel(desc->info.offset + offs, mxic->regs + LWR_ADDR);
  369. len = min_t(size_t, len, mxic->linear.size);
  370. writel(len, mxic->regs + LWR_RANGE);
  371. writel(LMODE_CMD0(desc->info.op_tmpl.cmd.opcode) |
  372. LMODE_SLV_ACT(desc->mem->spi->chip_select) |
  373. LMODE_EN,
  374. mxic->regs + LWR_CTRL);
  375. if (mxic->ecc.use_pipelined_conf && desc->info.op_tmpl.data.ecc) {
  376. ret = mxic_ecc_process_data_pipelined(mxic->ecc.pipelined_engine,
  377. NAND_PAGE_WRITE,
  378. mxic->linear.dma + offs);
  379. if (ret)
  380. return ret;
  381. } else {
  382. memcpy_toio(mxic->linear.map, buf, len);
  383. }
  384. writel(INT_LWR_DIS, mxic->regs + INT_STS);
  385. writel(0, mxic->regs + LWR_CTRL);
  386. ret = readl_poll_timeout(mxic->regs + INT_STS, sts,
  387. sts & INT_LWR_DIS, 0, USEC_PER_SEC);
  388. if (ret)
  389. return ret;
  390. return len;
  391. }
  392. static bool mxic_spi_mem_supports_op(struct spi_mem *mem,
  393. const struct spi_mem_op *op)
  394. {
  395. if (op->data.buswidth > 8 || op->addr.buswidth > 8 ||
  396. op->dummy.buswidth > 8 || op->cmd.buswidth > 8)
  397. return false;
  398. if (op->data.nbytes && op->dummy.nbytes &&
  399. op->data.buswidth != op->dummy.buswidth)
  400. return false;
  401. if (op->addr.nbytes > 7)
  402. return false;
  403. return spi_mem_default_supports_op(mem, op);
  404. }
  405. static int mxic_spi_mem_dirmap_create(struct spi_mem_dirmap_desc *desc)
  406. {
  407. struct mxic_spi *mxic = spi_master_get_devdata(desc->mem->spi->master);
  408. if (!mxic->linear.map)
  409. return -EINVAL;
  410. if (desc->info.offset + desc->info.length > U32_MAX)
  411. return -EINVAL;
  412. if (!mxic_spi_mem_supports_op(desc->mem, &desc->info.op_tmpl))
  413. return -EOPNOTSUPP;
  414. return 0;
  415. }
  416. static int mxic_spi_mem_exec_op(struct spi_mem *mem,
  417. const struct spi_mem_op *op)
  418. {
  419. struct mxic_spi *mxic = spi_master_get_devdata(mem->spi->master);
  420. int i, ret;
  421. u8 addr[8], cmd[2];
  422. ret = mxic_spi_set_freq(mxic, mem->spi->max_speed_hz);
  423. if (ret)
  424. return ret;
  425. writel(mxic_spi_prep_hc_cfg(mem->spi, HC_CFG_MAN_CS_EN),
  426. mxic->regs + HC_CFG);
  427. writel(HC_EN_BIT, mxic->regs + HC_EN);
  428. writel(mxic_spi_mem_prep_op_cfg(op, op->data.nbytes),
  429. mxic->regs + SS_CTRL(mem->spi->chip_select));
  430. writel(readl(mxic->regs + HC_CFG) | HC_CFG_MAN_CS_ASSERT,
  431. mxic->regs + HC_CFG);
  432. for (i = 0; i < op->cmd.nbytes; i++)
  433. cmd[i] = op->cmd.opcode >> (8 * (op->cmd.nbytes - i - 1));
  434. ret = mxic_spi_data_xfer(mxic, cmd, NULL, op->cmd.nbytes);
  435. if (ret)
  436. goto out;
  437. for (i = 0; i < op->addr.nbytes; i++)
  438. addr[i] = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
  439. ret = mxic_spi_data_xfer(mxic, addr, NULL, op->addr.nbytes);
  440. if (ret)
  441. goto out;
  442. ret = mxic_spi_data_xfer(mxic, NULL, NULL, op->dummy.nbytes);
  443. if (ret)
  444. goto out;
  445. ret = mxic_spi_data_xfer(mxic,
  446. op->data.dir == SPI_MEM_DATA_OUT ?
  447. op->data.buf.out : NULL,
  448. op->data.dir == SPI_MEM_DATA_IN ?
  449. op->data.buf.in : NULL,
  450. op->data.nbytes);
  451. out:
  452. writel(readl(mxic->regs + HC_CFG) & ~HC_CFG_MAN_CS_ASSERT,
  453. mxic->regs + HC_CFG);
  454. writel(0, mxic->regs + HC_EN);
  455. return ret;
  456. }
  457. static const struct spi_controller_mem_ops mxic_spi_mem_ops = {
  458. .supports_op = mxic_spi_mem_supports_op,
  459. .exec_op = mxic_spi_mem_exec_op,
  460. .dirmap_create = mxic_spi_mem_dirmap_create,
  461. .dirmap_read = mxic_spi_mem_dirmap_read,
  462. .dirmap_write = mxic_spi_mem_dirmap_write,
  463. };
  464. static const struct spi_controller_mem_caps mxic_spi_mem_caps = {
  465. .dtr = true,
  466. .ecc = true,
  467. };
  468. static void mxic_spi_set_cs(struct spi_device *spi, bool lvl)
  469. {
  470. struct mxic_spi *mxic = spi_master_get_devdata(spi->master);
  471. if (!lvl) {
  472. writel(readl(mxic->regs + HC_CFG) | HC_CFG_MAN_CS_EN,
  473. mxic->regs + HC_CFG);
  474. writel(HC_EN_BIT, mxic->regs + HC_EN);
  475. writel(readl(mxic->regs + HC_CFG) | HC_CFG_MAN_CS_ASSERT,
  476. mxic->regs + HC_CFG);
  477. } else {
  478. writel(readl(mxic->regs + HC_CFG) & ~HC_CFG_MAN_CS_ASSERT,
  479. mxic->regs + HC_CFG);
  480. writel(0, mxic->regs + HC_EN);
  481. }
  482. }
  483. static int mxic_spi_transfer_one(struct spi_master *master,
  484. struct spi_device *spi,
  485. struct spi_transfer *t)
  486. {
  487. struct mxic_spi *mxic = spi_master_get_devdata(master);
  488. unsigned int busw = OP_BUSW_1;
  489. int ret;
  490. if (t->rx_buf && t->tx_buf) {
  491. if (((spi->mode & SPI_TX_QUAD) &&
  492. !(spi->mode & SPI_RX_QUAD)) ||
  493. ((spi->mode & SPI_TX_DUAL) &&
  494. !(spi->mode & SPI_RX_DUAL)))
  495. return -ENOTSUPP;
  496. }
  497. ret = mxic_spi_set_freq(mxic, t->speed_hz);
  498. if (ret)
  499. return ret;
  500. if (t->tx_buf) {
  501. if (spi->mode & SPI_TX_QUAD)
  502. busw = OP_BUSW_4;
  503. else if (spi->mode & SPI_TX_DUAL)
  504. busw = OP_BUSW_2;
  505. } else if (t->rx_buf) {
  506. if (spi->mode & SPI_RX_QUAD)
  507. busw = OP_BUSW_4;
  508. else if (spi->mode & SPI_RX_DUAL)
  509. busw = OP_BUSW_2;
  510. }
  511. writel(OP_CMD_BYTES(1) | OP_CMD_BUSW(busw) |
  512. OP_DATA_BUSW(busw) | (t->rx_buf ? OP_READ : 0),
  513. mxic->regs + SS_CTRL(0));
  514. ret = mxic_spi_data_xfer(mxic, t->tx_buf, t->rx_buf, t->len);
  515. if (ret)
  516. return ret;
  517. spi_finalize_current_transfer(master);
  518. return 0;
  519. }
  520. /* ECC wrapper */
  521. static int mxic_spi_mem_ecc_init_ctx(struct nand_device *nand)
  522. {
  523. struct nand_ecc_engine_ops *ops = mxic_ecc_get_pipelined_ops();
  524. struct mxic_spi *mxic = nand->ecc.engine->priv;
  525. mxic->ecc.use_pipelined_conf = true;
  526. return ops->init_ctx(nand);
  527. }
  528. static void mxic_spi_mem_ecc_cleanup_ctx(struct nand_device *nand)
  529. {
  530. struct nand_ecc_engine_ops *ops = mxic_ecc_get_pipelined_ops();
  531. struct mxic_spi *mxic = nand->ecc.engine->priv;
  532. mxic->ecc.use_pipelined_conf = false;
  533. ops->cleanup_ctx(nand);
  534. }
  535. static int mxic_spi_mem_ecc_prepare_io_req(struct nand_device *nand,
  536. struct nand_page_io_req *req)
  537. {
  538. struct nand_ecc_engine_ops *ops = mxic_ecc_get_pipelined_ops();
  539. return ops->prepare_io_req(nand, req);
  540. }
  541. static int mxic_spi_mem_ecc_finish_io_req(struct nand_device *nand,
  542. struct nand_page_io_req *req)
  543. {
  544. struct nand_ecc_engine_ops *ops = mxic_ecc_get_pipelined_ops();
  545. return ops->finish_io_req(nand, req);
  546. }
  547. static struct nand_ecc_engine_ops mxic_spi_mem_ecc_engine_pipelined_ops = {
  548. .init_ctx = mxic_spi_mem_ecc_init_ctx,
  549. .cleanup_ctx = mxic_spi_mem_ecc_cleanup_ctx,
  550. .prepare_io_req = mxic_spi_mem_ecc_prepare_io_req,
  551. .finish_io_req = mxic_spi_mem_ecc_finish_io_req,
  552. };
  553. static void mxic_spi_mem_ecc_remove(struct mxic_spi *mxic)
  554. {
  555. if (mxic->ecc.pipelined_engine) {
  556. mxic_ecc_put_pipelined_engine(mxic->ecc.pipelined_engine);
  557. nand_ecc_unregister_on_host_hw_engine(mxic->ecc.pipelined_engine);
  558. }
  559. }
  560. static int mxic_spi_mem_ecc_probe(struct platform_device *pdev,
  561. struct mxic_spi *mxic)
  562. {
  563. struct nand_ecc_engine *eng;
  564. if (!mxic_ecc_get_pipelined_ops())
  565. return -EOPNOTSUPP;
  566. eng = mxic_ecc_get_pipelined_engine(pdev);
  567. if (IS_ERR(eng))
  568. return PTR_ERR(eng);
  569. eng->dev = &pdev->dev;
  570. eng->integration = NAND_ECC_ENGINE_INTEGRATION_PIPELINED;
  571. eng->ops = &mxic_spi_mem_ecc_engine_pipelined_ops;
  572. eng->priv = mxic;
  573. mxic->ecc.pipelined_engine = eng;
  574. nand_ecc_register_on_host_hw_engine(eng);
  575. return 0;
  576. }
  577. static int __maybe_unused mxic_spi_runtime_suspend(struct device *dev)
  578. {
  579. struct spi_master *master = dev_get_drvdata(dev);
  580. struct mxic_spi *mxic = spi_master_get_devdata(master);
  581. mxic_spi_clk_disable(mxic);
  582. clk_disable_unprepare(mxic->ps_clk);
  583. return 0;
  584. }
  585. static int __maybe_unused mxic_spi_runtime_resume(struct device *dev)
  586. {
  587. struct spi_master *master = dev_get_drvdata(dev);
  588. struct mxic_spi *mxic = spi_master_get_devdata(master);
  589. int ret;
  590. ret = clk_prepare_enable(mxic->ps_clk);
  591. if (ret) {
  592. dev_err(dev, "Cannot enable ps_clock.\n");
  593. return ret;
  594. }
  595. return mxic_spi_clk_enable(mxic);
  596. }
  597. static const struct dev_pm_ops mxic_spi_dev_pm_ops = {
  598. SET_RUNTIME_PM_OPS(mxic_spi_runtime_suspend,
  599. mxic_spi_runtime_resume, NULL)
  600. };
  601. static int mxic_spi_probe(struct platform_device *pdev)
  602. {
  603. struct spi_master *master;
  604. struct resource *res;
  605. struct mxic_spi *mxic;
  606. int ret;
  607. master = devm_spi_alloc_master(&pdev->dev, sizeof(struct mxic_spi));
  608. if (!master)
  609. return -ENOMEM;
  610. platform_set_drvdata(pdev, master);
  611. mxic = spi_master_get_devdata(master);
  612. mxic->dev = &pdev->dev;
  613. master->dev.of_node = pdev->dev.of_node;
  614. mxic->ps_clk = devm_clk_get(&pdev->dev, "ps_clk");
  615. if (IS_ERR(mxic->ps_clk))
  616. return PTR_ERR(mxic->ps_clk);
  617. mxic->send_clk = devm_clk_get(&pdev->dev, "send_clk");
  618. if (IS_ERR(mxic->send_clk))
  619. return PTR_ERR(mxic->send_clk);
  620. mxic->send_dly_clk = devm_clk_get(&pdev->dev, "send_dly_clk");
  621. if (IS_ERR(mxic->send_dly_clk))
  622. return PTR_ERR(mxic->send_dly_clk);
  623. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  624. mxic->regs = devm_ioremap_resource(&pdev->dev, res);
  625. if (IS_ERR(mxic->regs))
  626. return PTR_ERR(mxic->regs);
  627. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dirmap");
  628. mxic->linear.map = devm_ioremap_resource(&pdev->dev, res);
  629. if (!IS_ERR(mxic->linear.map)) {
  630. mxic->linear.dma = res->start;
  631. mxic->linear.size = resource_size(res);
  632. } else {
  633. mxic->linear.map = NULL;
  634. }
  635. pm_runtime_enable(&pdev->dev);
  636. master->auto_runtime_pm = true;
  637. master->num_chipselect = 1;
  638. master->mem_ops = &mxic_spi_mem_ops;
  639. master->mem_caps = &mxic_spi_mem_caps;
  640. master->set_cs = mxic_spi_set_cs;
  641. master->transfer_one = mxic_spi_transfer_one;
  642. master->bits_per_word_mask = SPI_BPW_MASK(8);
  643. master->mode_bits = SPI_CPOL | SPI_CPHA |
  644. SPI_RX_DUAL | SPI_TX_DUAL |
  645. SPI_RX_QUAD | SPI_TX_QUAD |
  646. SPI_RX_OCTAL | SPI_TX_OCTAL;
  647. mxic_spi_hw_init(mxic);
  648. ret = mxic_spi_mem_ecc_probe(pdev, mxic);
  649. if (ret == -EPROBE_DEFER) {
  650. pm_runtime_disable(&pdev->dev);
  651. return ret;
  652. }
  653. ret = spi_register_master(master);
  654. if (ret) {
  655. dev_err(&pdev->dev, "spi_register_master failed\n");
  656. pm_runtime_disable(&pdev->dev);
  657. mxic_spi_mem_ecc_remove(mxic);
  658. }
  659. return ret;
  660. }
  661. static int mxic_spi_remove(struct platform_device *pdev)
  662. {
  663. struct spi_master *master = platform_get_drvdata(pdev);
  664. struct mxic_spi *mxic = spi_master_get_devdata(master);
  665. pm_runtime_disable(&pdev->dev);
  666. mxic_spi_mem_ecc_remove(mxic);
  667. spi_unregister_master(master);
  668. return 0;
  669. }
  670. static const struct of_device_id mxic_spi_of_ids[] = {
  671. { .compatible = "mxicy,mx25f0a-spi", },
  672. { /* sentinel */ }
  673. };
  674. MODULE_DEVICE_TABLE(of, mxic_spi_of_ids);
  675. static struct platform_driver mxic_spi_driver = {
  676. .probe = mxic_spi_probe,
  677. .remove = mxic_spi_remove,
  678. .driver = {
  679. .name = "mxic-spi",
  680. .of_match_table = mxic_spi_of_ids,
  681. .pm = &mxic_spi_dev_pm_ops,
  682. },
  683. };
  684. module_platform_driver(mxic_spi_driver);
  685. MODULE_AUTHOR("Mason Yang <[email protected]>");
  686. MODULE_DESCRIPTION("MX25F0A SPI controller driver");
  687. MODULE_LICENSE("GPL v2");