1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021 Macronix International Co., Ltd.
16 #include <linux/bug.h>
17 #include <linux/iopoll.h>
20 #define HC_CFG_IF_CFG(x) ((x) << 27)
21 #define HC_CFG_DUAL_SLAVE BIT(31)
22 #define HC_CFG_INDIVIDUAL BIT(30)
23 #define HC_CFG_NIO(x) (((x) / 4) << 27)
24 #define HC_CFG_TYPE(s, t) ((t) << (23 + ((s) * 2)))
25 #define HC_CFG_TYPE_SPI_NOR 0
26 #define HC_CFG_TYPE_SPI_NAND 1
27 #define HC_CFG_TYPE_SPI_RAM 2
28 #define HC_CFG_TYPE_RAW_NAND 3
29 #define HC_CFG_SLV_ACT(x) ((x) << 21)
30 #define HC_CFG_CLK_PH_EN BIT(20)
31 #define HC_CFG_CLK_POL_INV BIT(19)
32 #define HC_CFG_BIG_ENDIAN BIT(18)
33 #define HC_CFG_DATA_PASS BIT(17)
34 #define HC_CFG_IDLE_SIO_LVL(x) ((x) << 16)
35 #define HC_CFG_MAN_START_EN BIT(3)
36 #define HC_CFG_MAN_START BIT(2)
37 #define HC_CFG_MAN_CS_EN BIT(1)
38 #define HC_CFG_MAN_CS_ASSERT BIT(0)
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)
59 #define HC_EN_BIT BIT(0)
61 #define TXD(x) (0x14 + ((x) * 4))
64 #define SS_CTRL(s) (0x30 + ((s) * 4))
68 #define OP_READ BIT(23)
69 #define OP_DUMMY_CYC(x) ((x) << 17)
70 #define OP_ADDR_BYTES(x) ((x) << 14)
71 #define OP_CMD_BYTES(x) (((x) - 1) << 13)
72 #define OP_OCTA_CRC_EN BIT(12)
73 #define OP_DQS_EN BIT(11)
74 #define OP_ENHC_EN BIT(10)
75 #define OP_PREAMBLE_EN BIT(9)
76 #define OP_DATA_DDR BIT(8)
77 #define OP_DATA_BUSW(x) ((x) << 6)
78 #define OP_ADDR_DDR BIT(5)
79 #define OP_ADDR_BUSW(x) ((x) << 3)
80 #define OP_CMD_DDR BIT(2)
81 #define OP_CMD_BUSW(x) (x)
88 #define OCTA_CRC_IN_EN(s) BIT(3 + ((s) * 16))
89 #define OCTA_CRC_CHUNK(s, x) ((fls((x) / 32)) << (1 + ((s) * 16)))
90 #define OCTA_CRC_OUT_EN(s) BIT(0 + ((s) * 16))
92 #define ONFI_DIN_CNT(s) (0x3c + (s))
97 #define LMODE_EN BIT(31)
98 #define LMODE_SLV_ACT(x) ((x) << 21)
99 #define LMODE_CMD1(x) ((x) << 8)
100 #define LMODE_CMD0(x) (x)
102 #define LRD_ADDR 0x4c
103 #define LWR_ADDR 0x88
104 #define LRD_RANGE 0x50
105 #define LWR_RANGE 0x8c
107 #define AXI_SLV_ADDR 0x54
109 #define DMAC_RD_CFG 0x58
110 #define DMAC_WR_CFG 0x94
111 #define DMAC_CFG_PERIPH_EN BIT(31)
112 #define DMAC_CFG_ALLFLUSH_EN BIT(30)
113 #define DMAC_CFG_LASTFLUSH_EN BIT(29)
114 #define DMAC_CFG_QE(x) (((x) + 1) << 16)
115 #define DMAC_CFG_BURST_LEN(x) (((x) + 1) << 12)
116 #define DMAC_CFG_BURST_SZ(x) ((x) << 8)
117 #define DMAC_CFG_DIR_READ BIT(1)
118 #define DMAC_CFG_START BIT(0)
120 #define DMAC_RD_CNT 0x5c
121 #define DMAC_WR_CNT 0x98
123 #define SDMA_ADDR 0x60
125 #define DMAM_CFG 0x64
126 #define DMAM_CFG_START BIT(31)
127 #define DMAM_CFG_CONT BIT(30)
128 #define DMAM_CFG_SDMA_GAP(x) (fls((x) / 8192) << 2)
129 #define DMAM_CFG_DIR_READ BIT(1)
130 #define DMAM_CFG_EN BIT(0)
132 #define DMAM_CNT 0x68
134 #define LNR_TIMER_TH 0x6c
136 #define RDM_CFG0 0x78
137 #define RDM_CFG0_POLY(x) (x)
139 #define RDM_CFG1 0x7c
140 #define RDM_CFG1_RDM_EN BIT(31)
141 #define RDM_CFG1_SEED(x) (x)
143 #define LWR_SUSP_CTRL 0x90
144 #define LWR_SUSP_CTRL_EN BIT(31)
146 #define DMAS_CTRL 0x9c
147 #define DMAS_CTRL_EN BIT(31)
148 #define DMAS_CTRL_DIR_READ BIT(30)
150 #define DATA_STROB 0xa0
151 #define DATA_STROB_EDO_EN BIT(2)
152 #define DATA_STROB_INV_POL BIT(1)
153 #define DATA_STROB_DELAY_2CYC BIT(0)
155 #define IDLY_CODE(x) (0xa4 + ((x) * 4))
156 #define IDLY_CODE_VAL(x, v) ((v) << (((x) % 4) * 8))
159 #define GPIO_PT(x) BIT(3 + ((x) * 16))
160 #define GPIO_RESET(x) BIT(2 + ((x) * 16))
161 #define GPIO_HOLDB(x) BIT(1 + ((x) * 16))
162 #define GPIO_WPB(x) BIT((x) * 16)
166 #define HW_TEST(x) (0xe0 + ((x) * 4))
168 struct mxic_spi_priv {
169 struct clk *send_clk;
170 struct clk *send_dly_clk;
175 static int mxic_spi_clk_enable(struct mxic_spi_priv *priv)
179 ret = clk_prepare_enable(priv->send_clk);
183 ret = clk_prepare_enable(priv->send_dly_clk);
185 goto err_send_dly_clk;
190 clk_disable_unprepare(priv->send_clk);
195 static void mxic_spi_clk_disable(struct mxic_spi_priv *priv)
197 clk_disable_unprepare(priv->send_clk);
198 clk_disable_unprepare(priv->send_dly_clk);
201 static void mxic_spi_set_input_delay_dqs(struct mxic_spi_priv *priv,
204 writel(IDLY_CODE_VAL(0, idly_code) |
205 IDLY_CODE_VAL(1, idly_code) |
206 IDLY_CODE_VAL(2, idly_code) |
207 IDLY_CODE_VAL(3, idly_code),
208 priv->regs + IDLY_CODE(0));
209 writel(IDLY_CODE_VAL(4, idly_code) |
210 IDLY_CODE_VAL(5, idly_code) |
211 IDLY_CODE_VAL(6, idly_code) |
212 IDLY_CODE_VAL(7, idly_code),
213 priv->regs + IDLY_CODE(1));
216 static int mxic_spi_clk_setup(struct mxic_spi_priv *priv, uint freq)
220 ret = clk_set_rate(priv->send_clk, freq);
224 ret = clk_set_rate(priv->send_dly_clk, freq);
229 * A constant delay range from 0x0 ~ 0x1F for input delay,
230 * the unit is 78 ps, the max input delay is 2.418 ns.
232 mxic_spi_set_input_delay_dqs(priv, 0xf);
237 static int mxic_spi_set_speed(struct udevice *bus, uint freq)
239 struct mxic_spi_priv *priv = dev_get_priv(bus);
242 if (priv->cur_speed_hz == freq)
245 mxic_spi_clk_disable(priv);
246 ret = mxic_spi_clk_setup(priv, freq);
250 ret = mxic_spi_clk_enable(priv);
254 priv->cur_speed_hz = freq;
259 static int mxic_spi_set_mode(struct udevice *bus, uint mode)
261 struct mxic_spi_priv *priv = dev_get_priv(bus);
265 hc_config |= HC_CFG_CLK_PH_EN;
267 hc_config |= HC_CFG_CLK_POL_INV;
269 writel(hc_config, priv->regs + HC_CFG);
274 static void mxic_spi_hw_init(struct mxic_spi_priv *priv)
276 writel(0, priv->regs + DATA_STROB);
277 writel(INT_STS_ALL, priv->regs + INT_STS_EN);
278 writel(0, priv->regs + HC_EN);
279 writel(0, priv->regs + LRD_CFG);
280 writel(0, priv->regs + LRD_CTRL);
281 writel(HC_CFG_NIO(1) | HC_CFG_TYPE(0, HC_CFG_TYPE_SPI_NOR) |
282 HC_CFG_SLV_ACT(0) | HC_CFG_MAN_CS_EN | HC_CFG_IDLE_SIO_LVL(1),
283 priv->regs + HC_CFG);
286 static int mxic_spi_data_xfer(struct mxic_spi_priv *priv, const void *txbuf,
287 void *rxbuf, unsigned int len)
289 unsigned int pos = 0;
292 unsigned int nbytes = len - pos;
293 u32 data = 0xffffffff;
301 memcpy(&data, txbuf + pos, nbytes);
303 ret = readl_poll_timeout(priv->regs + INT_STS, sts,
304 sts & INT_TX_EMPTY, 1000000);
308 writel(data, priv->regs + TXD(nbytes % 4));
311 ret = readl_poll_timeout(priv->regs + INT_STS, sts,
317 ret = readl_poll_timeout(priv->regs + INT_STS, sts,
318 sts & INT_RX_NOT_EMPTY,
323 data = readl(priv->regs + RXD);
324 data >>= (8 * (4 - nbytes));
325 memcpy(rxbuf + pos, &data, nbytes);
326 WARN_ON(readl(priv->regs + INT_STS) & INT_RX_NOT_EMPTY);
328 readl(priv->regs + RXD);
330 WARN_ON(readl(priv->regs + INT_STS) & INT_RX_NOT_EMPTY);
338 static bool mxic_spi_mem_supports_op(struct spi_slave *slave,
339 const struct spi_mem_op *op)
341 if (op->data.buswidth > 8 || op->addr.buswidth > 8 ||
342 op->dummy.buswidth > 8 || op->cmd.buswidth > 8)
345 if (op->addr.nbytes > 7)
348 return spi_mem_default_supports_op(slave, op);
351 static int mxic_spi_mem_exec_op(struct spi_slave *slave,
352 const struct spi_mem_op *op)
354 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(slave->dev);
355 struct udevice *bus = slave->dev->parent;
356 struct mxic_spi_priv *priv = dev_get_priv(bus);
359 u8 addr[8], dummy_bytes = 0;
361 if (slave->mode & (SPI_TX_OCTAL | SPI_RX_OCTAL))
363 else if (slave->mode & (SPI_TX_QUAD | SPI_RX_QUAD))
365 else if (slave->mode & (SPI_TX_DUAL | SPI_RX_DUAL))
368 writel(HC_CFG_NIO(nio) |
369 HC_CFG_TYPE(slave_plat->cs[0], HC_CFG_TYPE_SPI_NOR) |
370 HC_CFG_SLV_ACT(slave_plat->cs[0]) | HC_CFG_IDLE_SIO_LVL(1) |
372 priv->regs + HC_CFG);
373 writel(HC_EN_BIT, priv->regs + HC_EN);
375 ss_ctrl = OP_CMD_BYTES(1) | OP_CMD_BUSW(fls(op->cmd.buswidth) - 1);
378 ss_ctrl |= OP_ADDR_BYTES(op->addr.nbytes) |
379 OP_ADDR_BUSW(fls(op->addr.buswidth) - 1);
382 * Since the SPI MXIC dummy buswidth is aligned with the data buswidth,
383 * the dummy byte needs to be recalculated to send out the correct
386 if (op->dummy.nbytes) {
387 dummy_bytes = op->dummy.nbytes /
390 ss_ctrl |= OP_DUMMY_CYC(dummy_bytes);
393 if (op->data.nbytes) {
394 ss_ctrl |= OP_DATA_BUSW(fls(op->data.buswidth) - 1);
395 if (op->data.dir == SPI_MEM_DATA_IN)
399 writel(ss_ctrl, priv->regs + SS_CTRL(slave_plat->cs[0]));
401 writel(readl(priv->regs + HC_CFG) | HC_CFG_MAN_CS_ASSERT,
402 priv->regs + HC_CFG);
404 ret = mxic_spi_data_xfer(priv, &op->cmd.opcode, NULL, 1);
408 for (i = 0; i < op->addr.nbytes; i++)
409 addr[i] = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
411 ret = mxic_spi_data_xfer(priv, addr, NULL, op->addr.nbytes);
415 ret = mxic_spi_data_xfer(priv, NULL, NULL, dummy_bytes);
419 ret = mxic_spi_data_xfer(priv,
420 op->data.dir == SPI_MEM_DATA_OUT ?
421 op->data.buf.out : NULL,
422 op->data.dir == SPI_MEM_DATA_IN ?
423 op->data.buf.in : NULL,
427 writel(readl(priv->regs + HC_CFG) & ~HC_CFG_MAN_CS_ASSERT,
428 priv->regs + HC_CFG);
429 writel(0, priv->regs + HC_EN);
434 static const struct spi_controller_mem_ops mxic_spi_mem_ops = {
435 .supports_op = mxic_spi_mem_supports_op,
436 .exec_op = mxic_spi_mem_exec_op,
439 static int mxic_spi_claim_bus(struct udevice *dev)
441 struct udevice *bus = dev_get_parent(dev);
442 struct mxic_spi_priv *priv = dev_get_priv(bus);
444 writel(readl(priv->regs + HC_CFG) | HC_CFG_MAN_CS_EN,
445 priv->regs + HC_CFG);
446 writel(HC_EN_BIT, priv->regs + HC_EN);
447 writel(readl(priv->regs + HC_CFG) | HC_CFG_MAN_CS_ASSERT,
448 priv->regs + HC_CFG);
453 static int mxic_spi_release_bus(struct udevice *dev)
455 struct udevice *bus = dev_get_parent(dev);
456 struct mxic_spi_priv *priv = dev_get_priv(bus);
458 writel(readl(priv->regs + HC_CFG) & ~HC_CFG_MAN_CS_ASSERT,
459 priv->regs + HC_CFG);
460 writel(0, priv->regs + HC_EN);
465 static int mxic_spi_xfer(struct udevice *dev, unsigned int bitlen,
466 const void *dout, void *din, unsigned long flags)
468 struct udevice *bus = dev_get_parent(dev);
469 struct mxic_spi_priv *priv = dev_get_priv(bus);
470 struct spi_slave *slave = dev_get_parent_priv(dev);
471 unsigned int busw = OP_BUSW_1;
472 unsigned int len = bitlen / 8;
476 if (((slave->mode & SPI_TX_QUAD) &&
477 !(slave->mode & SPI_RX_QUAD)) ||
478 ((slave->mode & SPI_TX_DUAL) &&
479 !(slave->mode & SPI_RX_DUAL)))
484 if (slave->mode & SPI_TX_QUAD)
486 else if (slave->mode & SPI_TX_DUAL)
489 if (slave->mode & SPI_RX_QUAD)
491 else if (slave->mode & SPI_RX_DUAL)
495 writel(OP_CMD_BYTES(1) | OP_CMD_BUSW(busw) |
496 OP_DATA_BUSW(busw) | (din ? OP_READ : 0),
497 priv->regs + SS_CTRL(0));
499 ret = mxic_spi_data_xfer(priv, dout, din, len);
506 static int mxic_spi_probe(struct udevice *bus)
508 struct mxic_spi_priv *priv = dev_get_priv(bus);
510 priv->regs = dev_read_addr_ptr(bus);
512 priv->send_clk = devm_clk_get(bus, "send_clk");
513 if (IS_ERR(priv->send_clk))
514 return PTR_ERR(priv->send_clk);
516 priv->send_dly_clk = devm_clk_get(bus, "send_dly_clk");
517 if (IS_ERR(priv->send_dly_clk))
518 return PTR_ERR(priv->send_dly_clk);
520 mxic_spi_hw_init(priv);
525 static const struct dm_spi_ops mxic_spi_ops = {
526 .claim_bus = mxic_spi_claim_bus,
527 .release_bus = mxic_spi_release_bus,
528 .xfer = mxic_spi_xfer,
529 .set_speed = mxic_spi_set_speed,
530 .set_mode = mxic_spi_set_mode,
531 .mem_ops = &mxic_spi_mem_ops,
534 static const struct udevice_id mxic_spi_ids[] = {
535 { .compatible = "mxicy,mx25f0a-spi", },
539 U_BOOT_DRIVER(mxic_spi) = {
542 .of_match = mxic_spi_ids,
543 .ops = &mxic_spi_ops,
544 .priv_auto = sizeof(struct mxic_spi_priv),
545 .probe = mxic_spi_probe,