blob: 3835865ea7d3517b803d746eeb106e4a1fead5f4 [file] [log] [blame]
Zhengxunc93136b2021-06-23 17:15:15 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2021 Macronix International Co., Ltd.
4 *
5 * Authors:
6 * zhengxunli <zhengxunli@mxic.com.tw>
7 */
8
Zhengxunc93136b2021-06-23 17:15:15 +00009#include <clk.h>
10#include <dm.h>
11#include <errno.h>
12#include <asm/io.h>
13#include <malloc.h>
14#include <spi.h>
15#include <spi-mem.h>
16#include <linux/bug.h>
17#include <linux/iopoll.h>
18
19#define HC_CFG 0x0
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)
39
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
58#define HC_EN 0x10
59#define HC_EN_BIT BIT(0)
60
61#define TXD(x) (0x14 + ((x) * 4))
62#define RXD 0x24
63
64#define SS_CTRL(s) (0x30 + ((s) * 4))
65#define LRD_CFG 0x44
66#define LWR_CFG 0x80
67#define RWW_CFG 0x70
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)
82#define OP_BUSW_1 0
83#define OP_BUSW_2 1
84#define OP_BUSW_4 2
85#define OP_BUSW_8 3
86
87#define OCTA_CRC 0x38
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))
91
92#define ONFI_DIN_CNT(s) (0x3c + (s))
93
94#define LRD_CTRL 0x48
95#define RWW_CTRL 0x74
96#define LWR_CTRL 0x84
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)
101
102#define LRD_ADDR 0x4c
103#define LWR_ADDR 0x88
104#define LRD_RANGE 0x50
105#define LWR_RANGE 0x8c
106
107#define AXI_SLV_ADDR 0x54
108
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)
119
120#define DMAC_RD_CNT 0x5c
121#define DMAC_WR_CNT 0x98
122
123#define SDMA_ADDR 0x60
124
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)
131
132#define DMAM_CNT 0x68
133
134#define LNR_TIMER_TH 0x6c
135
136#define RDM_CFG0 0x78
137#define RDM_CFG0_POLY(x) (x)
138
139#define RDM_CFG1 0x7c
140#define RDM_CFG1_RDM_EN BIT(31)
141#define RDM_CFG1_SEED(x) (x)
142
143#define LWR_SUSP_CTRL 0x90
144#define LWR_SUSP_CTRL_EN BIT(31)
145
146#define DMAS_CTRL 0x9c
147#define DMAS_CTRL_EN BIT(31)
148#define DMAS_CTRL_DIR_READ BIT(30)
149
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)
154
155#define IDLY_CODE(x) (0xa4 + ((x) * 4))
156#define IDLY_CODE_VAL(x, v) ((v) << (((x) % 4) * 8))
157
158#define GPIO 0xc4
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)
163
164#define HC_VER 0xd0
165
166#define HW_TEST(x) (0xe0 + ((x) * 4))
167
168struct mxic_spi_priv {
169 struct clk *send_clk;
170 struct clk *send_dly_clk;
171 void __iomem *regs;
172 u32 cur_speed_hz;
173};
174
175static int mxic_spi_clk_enable(struct mxic_spi_priv *priv)
176{
177 int ret;
178
179 ret = clk_prepare_enable(priv->send_clk);
180 if (ret)
181 return ret;
182
183 ret = clk_prepare_enable(priv->send_dly_clk);
184 if (ret)
185 goto err_send_dly_clk;
186
187 return ret;
188
189err_send_dly_clk:
190 clk_disable_unprepare(priv->send_clk);
191
192 return ret;
193}
194
195static void mxic_spi_clk_disable(struct mxic_spi_priv *priv)
196{
197 clk_disable_unprepare(priv->send_clk);
198 clk_disable_unprepare(priv->send_dly_clk);
199}
200
201static void mxic_spi_set_input_delay_dqs(struct mxic_spi_priv *priv,
202 u8 idly_code)
203{
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));
214}
215
216static int mxic_spi_clk_setup(struct mxic_spi_priv *priv, uint freq)
217{
218 int ret;
219
220 ret = clk_set_rate(priv->send_clk, freq);
221 if (ret)
222 return ret;
223
224 ret = clk_set_rate(priv->send_dly_clk, freq);
225 if (ret)
226 return ret;
227
228 /*
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.
231 */
232 mxic_spi_set_input_delay_dqs(priv, 0xf);
233
234 return 0;
235}
236
237static int mxic_spi_set_speed(struct udevice *bus, uint freq)
238{
239 struct mxic_spi_priv *priv = dev_get_priv(bus);
240 int ret;
241
242 if (priv->cur_speed_hz == freq)
243 return 0;
244
245 mxic_spi_clk_disable(priv);
246 ret = mxic_spi_clk_setup(priv, freq);
247 if (ret)
248 return ret;
249
250 ret = mxic_spi_clk_enable(priv);
251 if (ret)
252 return ret;
253
254 priv->cur_speed_hz = freq;
255
256 return 0;
257}
258
259static int mxic_spi_set_mode(struct udevice *bus, uint mode)
260{
261 struct mxic_spi_priv *priv = dev_get_priv(bus);
262 u32 hc_config = 0;
263
264 if (mode & SPI_CPHA)
265 hc_config |= HC_CFG_CLK_PH_EN;
266 if (mode & SPI_CPOL)
267 hc_config |= HC_CFG_CLK_POL_INV;
268
269 writel(hc_config, priv->regs + HC_CFG);
270
271 return 0;
272}
273
274static void mxic_spi_hw_init(struct mxic_spi_priv *priv)
275{
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);
284}
285
286static int mxic_spi_data_xfer(struct mxic_spi_priv *priv, const void *txbuf,
287 void *rxbuf, unsigned int len)
288{
289 unsigned int pos = 0;
290
291 while (pos < len) {
292 unsigned int nbytes = len - pos;
293 u32 data = 0xffffffff;
294 u32 sts;
295 int ret;
296
297 if (nbytes > 4)
298 nbytes = 4;
299
300 if (txbuf)
301 memcpy(&data, txbuf + pos, nbytes);
302
303 ret = readl_poll_timeout(priv->regs + INT_STS, sts,
304 sts & INT_TX_EMPTY, 1000000);
305 if (ret)
306 return ret;
307
308 writel(data, priv->regs + TXD(nbytes % 4));
309
310 if (rxbuf) {
311 ret = readl_poll_timeout(priv->regs + INT_STS, sts,
312 sts & INT_TX_EMPTY,
313 1000000);
314 if (ret)
315 return ret;
316
317 ret = readl_poll_timeout(priv->regs + INT_STS, sts,
318 sts & INT_RX_NOT_EMPTY,
319 1000000);
320 if (ret)
321 return ret;
322
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);
327 } else {
328 readl(priv->regs + RXD);
329 }
330 WARN_ON(readl(priv->regs + INT_STS) & INT_RX_NOT_EMPTY);
331
332 pos += nbytes;
333 }
334
335 return 0;
336}
337
338static bool mxic_spi_mem_supports_op(struct spi_slave *slave,
339 const struct spi_mem_op *op)
340{
341 if (op->data.buswidth > 8 || op->addr.buswidth > 8 ||
342 op->dummy.buswidth > 8 || op->cmd.buswidth > 8)
343 return false;
344
345 if (op->addr.nbytes > 7)
346 return false;
347
348 return spi_mem_default_supports_op(slave, op);
349}
350
351static int mxic_spi_mem_exec_op(struct spi_slave *slave,
352 const struct spi_mem_op *op)
353{
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);
357 int nio = 1, i, ret;
358 u32 ss_ctrl;
359 u8 addr[8], dummy_bytes = 0;
360
361 if (slave->mode & (SPI_TX_OCTAL | SPI_RX_OCTAL))
362 nio = 8;
363 else if (slave->mode & (SPI_TX_QUAD | SPI_RX_QUAD))
364 nio = 4;
365 else if (slave->mode & (SPI_TX_DUAL | SPI_RX_DUAL))
366 nio = 2;
367
368 writel(HC_CFG_NIO(nio) |
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530369 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) |
Zhengxunc93136b2021-06-23 17:15:15 +0000371 HC_CFG_MAN_CS_EN,
372 priv->regs + HC_CFG);
373 writel(HC_EN_BIT, priv->regs + HC_EN);
374
375 ss_ctrl = OP_CMD_BYTES(1) | OP_CMD_BUSW(fls(op->cmd.buswidth) - 1);
376
377 if (op->addr.nbytes)
378 ss_ctrl |= OP_ADDR_BYTES(op->addr.nbytes) |
379 OP_ADDR_BUSW(fls(op->addr.buswidth) - 1);
380
381 /*
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
384 * dummy cycle.
385 */
386 if (op->dummy.nbytes) {
387 dummy_bytes = op->dummy.nbytes /
388 op->addr.buswidth *
389 op->data.buswidth;
390 ss_ctrl |= OP_DUMMY_CYC(dummy_bytes);
391 }
392
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)
396 ss_ctrl |= OP_READ;
397 }
398
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530399 writel(ss_ctrl, priv->regs + SS_CTRL(slave_plat->cs[0]));
Zhengxunc93136b2021-06-23 17:15:15 +0000400
401 writel(readl(priv->regs + HC_CFG) | HC_CFG_MAN_CS_ASSERT,
402 priv->regs + HC_CFG);
403
404 ret = mxic_spi_data_xfer(priv, &op->cmd.opcode, NULL, 1);
405 if (ret)
406 goto out;
407
408 for (i = 0; i < op->addr.nbytes; i++)
409 addr[i] = op->addr.val >> (8 * (op->addr.nbytes - i - 1));
410
411 ret = mxic_spi_data_xfer(priv, addr, NULL, op->addr.nbytes);
412 if (ret)
413 goto out;
414
415 ret = mxic_spi_data_xfer(priv, NULL, NULL, dummy_bytes);
416 if (ret)
417 goto out;
418
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,
424 op->data.nbytes);
425
426out:
427 writel(readl(priv->regs + HC_CFG) & ~HC_CFG_MAN_CS_ASSERT,
428 priv->regs + HC_CFG);
429 writel(0, priv->regs + HC_EN);
430
431 return ret;
432}
433
434static 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,
437};
438
439static int mxic_spi_claim_bus(struct udevice *dev)
440{
441 struct udevice *bus = dev_get_parent(dev);
442 struct mxic_spi_priv *priv = dev_get_priv(bus);
443
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);
449
450 return 0;
451}
452
453static int mxic_spi_release_bus(struct udevice *dev)
454{
455 struct udevice *bus = dev_get_parent(dev);
456 struct mxic_spi_priv *priv = dev_get_priv(bus);
457
458 writel(readl(priv->regs + HC_CFG) & ~HC_CFG_MAN_CS_ASSERT,
459 priv->regs + HC_CFG);
460 writel(0, priv->regs + HC_EN);
461
462 return 0;
463}
464
465static int mxic_spi_xfer(struct udevice *dev, unsigned int bitlen,
466 const void *dout, void *din, unsigned long flags)
467{
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;
473 int ret;
474
475 if (dout && din) {
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)))
480 return -ENOTSUPP;
481 }
482
483 if (din) {
484 if (slave->mode & SPI_TX_QUAD)
485 busw = OP_BUSW_4;
486 else if (slave->mode & SPI_TX_DUAL)
487 busw = OP_BUSW_2;
488 } else if (dout) {
489 if (slave->mode & SPI_RX_QUAD)
490 busw = OP_BUSW_4;
491 else if (slave->mode & SPI_RX_DUAL)
492 busw = OP_BUSW_2;
493 }
494
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));
498
499 ret = mxic_spi_data_xfer(priv, dout, din, len);
500 if (ret)
501 return ret;
502
503 return 0;
504}
505
506static int mxic_spi_probe(struct udevice *bus)
507{
508 struct mxic_spi_priv *priv = dev_get_priv(bus);
509
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100510 priv->regs = dev_read_addr_ptr(bus);
Zhengxunc93136b2021-06-23 17:15:15 +0000511
512 priv->send_clk = devm_clk_get(bus, "send_clk");
513 if (IS_ERR(priv->send_clk))
514 return PTR_ERR(priv->send_clk);
515
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);
519
520 mxic_spi_hw_init(priv);
521
522 return 0;
523}
524
525static 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,
532};
533
534static const struct udevice_id mxic_spi_ids[] = {
535 { .compatible = "mxicy,mx25f0a-spi", },
536 { }
537};
538
539U_BOOT_DRIVER(mxic_spi) = {
540 .name = "mxic_spi",
541 .id = UCLASS_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,
546};