blob: 596c22aa01086aa229d68eb16046e6ceb5a1ffdd [file] [log] [blame]
Chris Morgan3afbc762021-08-05 16:26:38 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Rockchip Serial Flash Controller Driver
4 *
5 * Copyright (c) 2017-2021, Rockchip Inc.
6 * Author: Shawn Lin <shawn.lin@rock-chips.com>
7 * Chris Morgan <macromorgan@hotmail.com>
8 * Jon Lin <Jon.lin@rock-chips.com>
9 */
10
11#include <asm/io.h>
12#include <bouncebuf.h>
13#include <clk.h>
14#include <dm.h>
Chris Morganffa33eb2022-03-25 10:40:35 -050015#include <dm/device_compat.h>
Chris Morgan3afbc762021-08-05 16:26:38 +080016#include <linux/bitops.h>
17#include <linux/delay.h>
18#include <linux/iopoll.h>
19#include <spi.h>
20#include <spi-mem.h>
21
22/* System control */
23#define SFC_CTRL 0x0
24#define SFC_CTRL_PHASE_SEL_NEGETIVE BIT(1)
25#define SFC_CTRL_CMD_BITS_SHIFT 8
26#define SFC_CTRL_ADDR_BITS_SHIFT 10
27#define SFC_CTRL_DATA_BITS_SHIFT 12
28
29/* Interrupt mask */
30#define SFC_IMR 0x4
31#define SFC_IMR_RX_FULL BIT(0)
32#define SFC_IMR_RX_UFLOW BIT(1)
33#define SFC_IMR_TX_OFLOW BIT(2)
34#define SFC_IMR_TX_EMPTY BIT(3)
35#define SFC_IMR_TRAN_FINISH BIT(4)
36#define SFC_IMR_BUS_ERR BIT(5)
37#define SFC_IMR_NSPI_ERR BIT(6)
38#define SFC_IMR_DMA BIT(7)
39
40/* Interrupt clear */
41#define SFC_ICLR 0x8
42#define SFC_ICLR_RX_FULL BIT(0)
43#define SFC_ICLR_RX_UFLOW BIT(1)
44#define SFC_ICLR_TX_OFLOW BIT(2)
45#define SFC_ICLR_TX_EMPTY BIT(3)
46#define SFC_ICLR_TRAN_FINISH BIT(4)
47#define SFC_ICLR_BUS_ERR BIT(5)
48#define SFC_ICLR_NSPI_ERR BIT(6)
49#define SFC_ICLR_DMA BIT(7)
50
51/* FIFO threshold level */
52#define SFC_FTLR 0xc
53#define SFC_FTLR_TX_SHIFT 0
54#define SFC_FTLR_TX_MASK 0x1f
55#define SFC_FTLR_RX_SHIFT 8
56#define SFC_FTLR_RX_MASK 0x1f
57
58/* Reset FSM and FIFO */
59#define SFC_RCVR 0x10
60#define SFC_RCVR_RESET BIT(0)
61
62/* Enhanced mode */
63#define SFC_AX 0x14
64
65/* Address Bit number */
66#define SFC_ABIT 0x18
67
68/* Interrupt status */
69#define SFC_ISR 0x1c
70#define SFC_ISR_RX_FULL_SHIFT BIT(0)
71#define SFC_ISR_RX_UFLOW_SHIFT BIT(1)
72#define SFC_ISR_TX_OFLOW_SHIFT BIT(2)
73#define SFC_ISR_TX_EMPTY_SHIFT BIT(3)
74#define SFC_ISR_TX_FINISH_SHIFT BIT(4)
75#define SFC_ISR_BUS_ERR_SHIFT BIT(5)
76#define SFC_ISR_NSPI_ERR_SHIFT BIT(6)
77#define SFC_ISR_DMA_SHIFT BIT(7)
78
79/* FIFO status */
80#define SFC_FSR 0x20
81#define SFC_FSR_TX_IS_FULL BIT(0)
82#define SFC_FSR_TX_IS_EMPTY BIT(1)
83#define SFC_FSR_RX_IS_EMPTY BIT(2)
84#define SFC_FSR_RX_IS_FULL BIT(3)
85#define SFC_FSR_TXLV_MASK GENMASK(12, 8)
86#define SFC_FSR_TXLV_SHIFT 8
87#define SFC_FSR_RXLV_MASK GENMASK(20, 16)
88#define SFC_FSR_RXLV_SHIFT 16
89
90/* FSM status */
91#define SFC_SR 0x24
92#define SFC_SR_IS_IDLE 0x0
93#define SFC_SR_IS_BUSY 0x1
94
95/* Raw interrupt status */
96#define SFC_RISR 0x28
97#define SFC_RISR_RX_FULL BIT(0)
98#define SFC_RISR_RX_UNDERFLOW BIT(1)
99#define SFC_RISR_TX_OVERFLOW BIT(2)
100#define SFC_RISR_TX_EMPTY BIT(3)
101#define SFC_RISR_TRAN_FINISH BIT(4)
102#define SFC_RISR_BUS_ERR BIT(5)
103#define SFC_RISR_NSPI_ERR BIT(6)
104#define SFC_RISR_DMA BIT(7)
105
106/* Version */
107#define SFC_VER 0x2C
108#define SFC_VER_3 0x3
109#define SFC_VER_4 0x4
110#define SFC_VER_5 0x5
111
112/* Delay line controller resiter */
113#define SFC_DLL_CTRL0 0x3C
114#define SFC_DLL_CTRL0_SCLK_SMP_DLL BIT(15)
115#define SFC_DLL_CTRL0_DLL_MAX_VER4 0xFFU
116#define SFC_DLL_CTRL0_DLL_MAX_VER5 0x1FFU
117
118/* Master trigger */
119#define SFC_DMA_TRIGGER 0x80
Jon Lin0accc652021-09-17 21:14:58 +0800120#define SFC_DMA_TRIGGER_START 1
Chris Morgan3afbc762021-08-05 16:26:38 +0800121
122/* Src or Dst addr for master */
123#define SFC_DMA_ADDR 0x84
124
125/* Length control register extension 32GB */
126#define SFC_LEN_CTRL 0x88
127#define SFC_LEN_CTRL_TRB_SEL 1
128#define SFC_LEN_EXT 0x8C
129
130/* Command */
131#define SFC_CMD 0x100
132#define SFC_CMD_IDX_SHIFT 0
133#define SFC_CMD_DUMMY_SHIFT 8
134#define SFC_CMD_DIR_SHIFT 12
135#define SFC_CMD_DIR_RD 0
136#define SFC_CMD_DIR_WR 1
137#define SFC_CMD_ADDR_SHIFT 14
138#define SFC_CMD_ADDR_0BITS 0
139#define SFC_CMD_ADDR_24BITS 1
140#define SFC_CMD_ADDR_32BITS 2
141#define SFC_CMD_ADDR_XBITS 3
142#define SFC_CMD_TRAN_BYTES_SHIFT 16
143#define SFC_CMD_CS_SHIFT 30
144
145/* Address */
146#define SFC_ADDR 0x104
147
148/* Data */
149#define SFC_DATA 0x108
150
151/* The controller and documentation reports that it supports up to 4 CS
152 * devices (0-3), however I have only been able to test a single CS (CS 0)
153 * due to the configuration of my device.
154 */
155#define SFC_MAX_CHIPSELECT_NUM 4
156
157/* The SFC can transfer max 16KB - 1 at one time
158 * we set it to 15.5KB here for alignment.
159 */
160#define SFC_MAX_IOSIZE_VER3 (512 * 31)
161
162#define SFC_MAX_IOSIZE_VER4 (0xFFFFFFFFU)
163
164/* DMA is only enabled for large data transmission */
165#define SFC_DMA_TRANS_THRETHOLD (0x40)
166
167/* Maximum clock values from datasheet suggest keeping clock value under
Jon Lin0accc652021-09-17 21:14:58 +0800168 * 150MHz. No minimum or average value is suggested.
Chris Morgan3afbc762021-08-05 16:26:38 +0800169 */
Jon Lin0accc652021-09-17 21:14:58 +0800170#define SFC_MAX_SPEED (150 * 1000 * 1000)
Chris Morgan3afbc762021-08-05 16:26:38 +0800171
172struct rockchip_sfc {
Jon Lin0accc652021-09-17 21:14:58 +0800173 struct udevice *dev;
Chris Morgan3afbc762021-08-05 16:26:38 +0800174 void __iomem *regbase;
175 struct clk hclk;
176 struct clk clk;
177 u32 max_freq;
178 u32 speed;
179 bool use_dma;
180 u32 max_iosize;
181 u16 version;
182};
183
184static int rockchip_sfc_reset(struct rockchip_sfc *sfc)
185{
186 int err;
187 u32 status;
188
189 writel(SFC_RCVR_RESET, sfc->regbase + SFC_RCVR);
190
191 err = readl_poll_timeout(sfc->regbase + SFC_RCVR, status,
192 !(status & SFC_RCVR_RESET),
193 1000000);
194 if (err)
195 printf("SFC reset never finished\n");
196
197 /* Still need to clear the masked interrupt from RISR */
198 writel(0xFFFFFFFF, sfc->regbase + SFC_ICLR);
199
Chris Morgan3afbc762021-08-05 16:26:38 +0800200 return err;
201}
202
203static u16 rockchip_sfc_get_version(struct rockchip_sfc *sfc)
204{
205 return (u16)(readl(sfc->regbase + SFC_VER) & 0xffff);
206}
207
208static u32 rockchip_sfc_get_max_iosize(struct rockchip_sfc *sfc)
209{
210 if (rockchip_sfc_get_version(sfc) >= SFC_VER_4)
211 return SFC_MAX_IOSIZE_VER4;
212
213 return SFC_MAX_IOSIZE_VER3;
214}
215
216static int rockchip_sfc_init(struct rockchip_sfc *sfc)
217{
218 writel(0, sfc->regbase + SFC_CTRL);
219 if (rockchip_sfc_get_version(sfc) >= SFC_VER_4)
220 writel(SFC_LEN_CTRL_TRB_SEL, sfc->regbase + SFC_LEN_CTRL);
221
222 return 0;
223}
224
225static int rockchip_sfc_ofdata_to_platdata(struct udevice *bus)
226{
227 struct rockchip_sfc *sfc = dev_get_plat(bus);
228
229 sfc->regbase = dev_read_addr_ptr(bus);
Jonas Karlman29bc6052023-05-17 18:26:28 +0000230 sfc->use_dma = !dev_read_bool(bus, "rockchip,sfc-no-dma");
231
232 if (IS_ENABLED(CONFIG_SPL_BUILD) && sfc->use_dma)
233 sfc->use_dma = !dev_read_bool(bus, "u-boot,spl-sfc-no-dma");
Chris Morgan3afbc762021-08-05 16:26:38 +0800234
235#if CONFIG_IS_ENABLED(CLK)
236 int ret;
237
238 ret = clk_get_by_index(bus, 0, &sfc->clk);
239 if (ret < 0) {
240 printf("Could not get clock for %s: %d\n", bus->name, ret);
241 return ret;
242 }
243
244 ret = clk_get_by_index(bus, 1, &sfc->hclk);
245 if (ret < 0) {
246 printf("Could not get ahb clock for %s: %d\n", bus->name, ret);
247 return ret;
248 }
249#endif
250
251 return 0;
252}
253
254static int rockchip_sfc_probe(struct udevice *bus)
255{
256 struct rockchip_sfc *sfc = dev_get_plat(bus);
257 int ret;
258
259#if CONFIG_IS_ENABLED(CLK)
260 ret = clk_enable(&sfc->hclk);
261 if (ret)
Jon Lin0accc652021-09-17 21:14:58 +0800262 dev_dbg(sfc->dev, "sfc Enable ahb clock fail %s: %d\n", bus->name, ret);
Chris Morgan3afbc762021-08-05 16:26:38 +0800263
264 ret = clk_enable(&sfc->clk);
265 if (ret)
Jon Lin0accc652021-09-17 21:14:58 +0800266 dev_dbg(sfc->dev, "sfc Enable clock fail for %s: %d\n", bus->name, ret);
Chris Morgan3afbc762021-08-05 16:26:38 +0800267#endif
268
269 ret = rockchip_sfc_init(sfc);
270 if (ret)
271 goto err_init;
272
273 sfc->max_iosize = rockchip_sfc_get_max_iosize(sfc);
274 sfc->version = rockchip_sfc_get_version(sfc);
Jon Lin0accc652021-09-17 21:14:58 +0800275 sfc->max_freq = SFC_MAX_SPEED;
276 sfc->dev = bus;
Chris Morgan3afbc762021-08-05 16:26:38 +0800277
278 return 0;
279
280err_init:
281#if CONFIG_IS_ENABLED(CLK)
282 clk_disable(&sfc->clk);
283 clk_disable(&sfc->hclk);
284#endif
285
286 return ret;
287}
288
Jon Line792db12021-09-17 21:14:59 +0800289static int rockchip_sfc_wait_txfifo_ready(struct rockchip_sfc *sfc, u32 timeout_us)
Chris Morgan3afbc762021-08-05 16:26:38 +0800290{
Jon Line792db12021-09-17 21:14:59 +0800291 int ret = 0;
292 u32 status;
Chris Morgan3afbc762021-08-05 16:26:38 +0800293
Jon Line792db12021-09-17 21:14:59 +0800294 ret = readl_poll_timeout(sfc->regbase + SFC_FSR, status,
295 status & SFC_FSR_TXLV_MASK,
296 timeout_us);
297 if (ret) {
298 dev_dbg(sfc->dev, "sfc wait tx fifo timeout\n");
299
300 return -ETIMEDOUT;
301 }
Chris Morgan3afbc762021-08-05 16:26:38 +0800302
Jon Line792db12021-09-17 21:14:59 +0800303 return (status & SFC_FSR_TXLV_MASK) >> SFC_FSR_TXLV_SHIFT;
Chris Morgan3afbc762021-08-05 16:26:38 +0800304}
305
Jon Line792db12021-09-17 21:14:59 +0800306static int rockchip_sfc_wait_rxfifo_ready(struct rockchip_sfc *sfc, u32 timeout_us)
Chris Morgan3afbc762021-08-05 16:26:38 +0800307{
Jon Line792db12021-09-17 21:14:59 +0800308 int ret = 0;
309 u32 status;
Chris Morgan3afbc762021-08-05 16:26:38 +0800310
Jon Line792db12021-09-17 21:14:59 +0800311 ret = readl_poll_timeout(sfc->regbase + SFC_FSR, status,
312 status & SFC_FSR_RXLV_MASK,
313 timeout_us);
314 if (ret) {
315 dev_dbg(sfc->dev, "sfc wait rx fifo timeout\n");
316
317 return -ETIMEDOUT;
Chris Morgan3afbc762021-08-05 16:26:38 +0800318 }
319
Jon Line792db12021-09-17 21:14:59 +0800320 return (status & SFC_FSR_RXLV_MASK) >> SFC_FSR_RXLV_SHIFT;
Chris Morgan3afbc762021-08-05 16:26:38 +0800321}
322
323static void rockchip_sfc_adjust_op_work(struct spi_mem_op *op)
324{
325 if (unlikely(op->dummy.nbytes && !op->addr.nbytes)) {
326 /*
327 * SFC not support output DUMMY cycles right after CMD cycles, so
328 * treat it as ADDR cycles.
329 */
330 op->addr.nbytes = op->dummy.nbytes;
331 op->addr.buswidth = op->dummy.buswidth;
332 op->addr.val = 0xFFFFFFFFF;
333
334 op->dummy.nbytes = 0;
335 }
336}
337
338static int rockchip_sfc_wait_for_dma_finished(struct rockchip_sfc *sfc, int timeout)
339{
340 unsigned long tbase;
341
342 /* Wait for the DMA interrupt status */
343 tbase = get_timer(0);
344 while (!(readl(sfc->regbase + SFC_RISR) & SFC_RISR_DMA)) {
345 if (get_timer(tbase) > timeout) {
346 printf("dma timeout\n");
347 rockchip_sfc_reset(sfc);
348
349 return -ETIMEDOUT;
350 }
351
352 udelay(1);
353 }
354
355 writel(0xFFFFFFFF, sfc->regbase + SFC_ICLR);
356
357 return 0;
358}
359
360static int rockchip_sfc_xfer_setup(struct rockchip_sfc *sfc,
361 struct spi_slave *mem,
362 const struct spi_mem_op *op,
363 u32 len)
364{
365 struct dm_spi_slave_plat *plat = dev_get_parent_plat(mem->dev);
366 u32 ctrl = 0, cmd = 0;
367
368 /* set CMD */
369 cmd = op->cmd.opcode;
370 ctrl |= ((op->cmd.buswidth >> 1) << SFC_CTRL_CMD_BITS_SHIFT);
371
372 /* set ADDR */
373 if (op->addr.nbytes) {
374 if (op->addr.nbytes == 4) {
375 cmd |= SFC_CMD_ADDR_32BITS << SFC_CMD_ADDR_SHIFT;
376 } else if (op->addr.nbytes == 3) {
377 cmd |= SFC_CMD_ADDR_24BITS << SFC_CMD_ADDR_SHIFT;
378 } else {
379 cmd |= SFC_CMD_ADDR_XBITS << SFC_CMD_ADDR_SHIFT;
380 writel(op->addr.nbytes * 8 - 1, sfc->regbase + SFC_ABIT);
381 }
382
383 ctrl |= ((op->addr.buswidth >> 1) << SFC_CTRL_ADDR_BITS_SHIFT);
384 }
385
386 /* set DUMMY */
387 if (op->dummy.nbytes) {
388 if (op->dummy.buswidth == 4)
389 cmd |= op->dummy.nbytes * 2 << SFC_CMD_DUMMY_SHIFT;
390 else if (op->dummy.buswidth == 2)
391 cmd |= op->dummy.nbytes * 4 << SFC_CMD_DUMMY_SHIFT;
392 else
393 cmd |= op->dummy.nbytes * 8 << SFC_CMD_DUMMY_SHIFT;
394 }
395
396 /* set DATA */
397 if (sfc->version >= SFC_VER_4) /* Clear it if no data to transfer */
398 writel(len, sfc->regbase + SFC_LEN_EXT);
399 else
400 cmd |= len << SFC_CMD_TRAN_BYTES_SHIFT;
401 if (len) {
402 if (op->data.dir == SPI_MEM_DATA_OUT)
403 cmd |= SFC_CMD_DIR_WR << SFC_CMD_DIR_SHIFT;
404
405 ctrl |= ((op->data.buswidth >> 1) << SFC_CTRL_DATA_BITS_SHIFT);
406 }
407 if (!len && op->addr.nbytes)
408 cmd |= SFC_CMD_DIR_WR << SFC_CMD_DIR_SHIFT;
409
410 /* set the Controller */
411 ctrl |= SFC_CTRL_PHASE_SEL_NEGETIVE;
412 cmd |= plat->cs << SFC_CMD_CS_SHIFT;
413
Jon Lin0accc652021-09-17 21:14:58 +0800414 dev_dbg(sfc->dev, "sfc addr.nbytes=%x(x%d) dummy.nbytes=%x(x%d)\n",
415 op->addr.nbytes, op->addr.buswidth,
416 op->dummy.nbytes, op->dummy.buswidth);
417 dev_dbg(sfc->dev, "sfc ctrl=%x cmd=%x addr=%llx len=%x\n",
418 ctrl, cmd, op->addr.val, len);
Chris Morgan3afbc762021-08-05 16:26:38 +0800419
420 writel(ctrl, sfc->regbase + SFC_CTRL);
421 writel(cmd, sfc->regbase + SFC_CMD);
422 if (op->addr.nbytes)
423 writel(op->addr.val, sfc->regbase + SFC_ADDR);
424
425 return 0;
426}
427
428static int rockchip_sfc_write_fifo(struct rockchip_sfc *sfc, const u8 *buf, int len)
429{
430 u8 bytes = len & 0x3;
431 u32 dwords;
432 int tx_level;
433 u32 write_words;
434 u32 tmp = 0;
435
436 dwords = len >> 2;
437 while (dwords) {
Jon Line792db12021-09-17 21:14:59 +0800438 tx_level = rockchip_sfc_wait_txfifo_ready(sfc, 1000);
Chris Morgan3afbc762021-08-05 16:26:38 +0800439 if (tx_level < 0)
440 return tx_level;
441 write_words = min_t(u32, tx_level, dwords);
442 writesl(sfc->regbase + SFC_DATA, buf, write_words);
443 buf += write_words << 2;
444 dwords -= write_words;
445 }
446
447 /* write the rest non word aligned bytes */
448 if (bytes) {
Jon Line792db12021-09-17 21:14:59 +0800449 tx_level = rockchip_sfc_wait_txfifo_ready(sfc, 1000);
Chris Morgan3afbc762021-08-05 16:26:38 +0800450 if (tx_level < 0)
451 return tx_level;
452 memcpy(&tmp, buf, bytes);
453 writel(tmp, sfc->regbase + SFC_DATA);
454 }
455
456 return len;
457}
458
459static int rockchip_sfc_read_fifo(struct rockchip_sfc *sfc, u8 *buf, int len)
460{
461 u8 bytes = len & 0x3;
462 u32 dwords;
463 u8 read_words;
464 int rx_level;
465 int tmp;
466
467 /* word aligned access only */
468 dwords = len >> 2;
469 while (dwords) {
Jon Line792db12021-09-17 21:14:59 +0800470 rx_level = rockchip_sfc_wait_rxfifo_ready(sfc, 1000);
Chris Morgan3afbc762021-08-05 16:26:38 +0800471 if (rx_level < 0)
472 return rx_level;
473 read_words = min_t(u32, rx_level, dwords);
474 readsl(sfc->regbase + SFC_DATA, buf, read_words);
475 buf += read_words << 2;
476 dwords -= read_words;
477 }
478
479 /* read the rest non word aligned bytes */
480 if (bytes) {
Jon Line792db12021-09-17 21:14:59 +0800481 rx_level = rockchip_sfc_wait_rxfifo_ready(sfc, 1000);
Chris Morgan3afbc762021-08-05 16:26:38 +0800482 if (rx_level < 0)
483 return rx_level;
484 tmp = readl(sfc->regbase + SFC_DATA);
485 memcpy(buf, &tmp, bytes);
486 }
487
488 return len;
489}
490
491static int rockchip_sfc_fifo_transfer_dma(struct rockchip_sfc *sfc, dma_addr_t dma_buf, size_t len)
492{
493 writel(0xFFFFFFFF, sfc->regbase + SFC_ICLR);
494 writel((u32)dma_buf, sfc->regbase + SFC_DMA_ADDR);
Jon Lin0accc652021-09-17 21:14:58 +0800495 writel(SFC_DMA_TRIGGER_START, sfc->regbase + SFC_DMA_TRIGGER);
Chris Morgan3afbc762021-08-05 16:26:38 +0800496
497 return len;
498}
499
500static int rockchip_sfc_xfer_data_poll(struct rockchip_sfc *sfc,
501 const struct spi_mem_op *op, u32 len)
502{
Jon Lin0accc652021-09-17 21:14:58 +0800503 dev_dbg(sfc->dev, "sfc xfer_poll len=%x\n", len);
Chris Morgan3afbc762021-08-05 16:26:38 +0800504
505 if (op->data.dir == SPI_MEM_DATA_OUT)
506 return rockchip_sfc_write_fifo(sfc, op->data.buf.out, len);
507 else
508 return rockchip_sfc_read_fifo(sfc, op->data.buf.in, len);
509}
510
511static int rockchip_sfc_xfer_data_dma(struct rockchip_sfc *sfc,
512 const struct spi_mem_op *op, u32 len)
513{
514 struct bounce_buffer bb;
515 unsigned int bb_flags;
516 void *dma_buf;
517 int ret;
518
Jon Lin0accc652021-09-17 21:14:58 +0800519 dev_dbg(sfc->dev, "sfc xfer_dma len=%x\n", len);
Chris Morgan3afbc762021-08-05 16:26:38 +0800520
521 if (op->data.dir == SPI_MEM_DATA_OUT) {
522 dma_buf = (void *)op->data.buf.out;
523 bb_flags = GEN_BB_READ;
524 } else {
525 dma_buf = (void *)op->data.buf.in;
526 bb_flags = GEN_BB_WRITE;
527 }
528
529 ret = bounce_buffer_start(&bb, dma_buf, len, bb_flags);
530 if (ret)
531 return ret;
532
533 ret = rockchip_sfc_fifo_transfer_dma(sfc, (dma_addr_t)bb.bounce_buffer, len);
534 rockchip_sfc_wait_for_dma_finished(sfc, len * 10);
535 bounce_buffer_stop(&bb);
536
537 return ret;
538}
539
540static int rockchip_sfc_xfer_done(struct rockchip_sfc *sfc, u32 timeout_us)
541{
Chris Morgan3afbc762021-08-05 16:26:38 +0800542 int ret = 0;
Jon Line792db12021-09-17 21:14:59 +0800543 u32 status;
Chris Morgan3afbc762021-08-05 16:26:38 +0800544
Jon Line792db12021-09-17 21:14:59 +0800545 ret = readl_poll_timeout(sfc->regbase + SFC_SR, status,
546 !(status & SFC_SR_IS_BUSY),
547 timeout_us);
548 if (ret) {
549 dev_err(sfc->dev, "wait sfc idle timeout\n");
550 rockchip_sfc_reset(sfc);
Chris Morgan3afbc762021-08-05 16:26:38 +0800551
Jon Line792db12021-09-17 21:14:59 +0800552 ret = -EIO;
Chris Morgan3afbc762021-08-05 16:26:38 +0800553 }
554
555 return ret;
556}
557
558static int rockchip_sfc_exec_op(struct spi_slave *mem,
559 const struct spi_mem_op *op)
560{
561 struct rockchip_sfc *sfc = dev_get_plat(mem->dev->parent);
562 u32 len = min_t(u32, op->data.nbytes, sfc->max_iosize);
563 int ret;
564
Chris Morgan3afbc762021-08-05 16:26:38 +0800565 rockchip_sfc_adjust_op_work((struct spi_mem_op *)op);
Chris Morgan3afbc762021-08-05 16:26:38 +0800566 rockchip_sfc_xfer_setup(sfc, mem, op, len);
567 if (len) {
Jon Lin0accc652021-09-17 21:14:58 +0800568 if (likely(sfc->use_dma) && len >= SFC_DMA_TRANS_THRETHOLD)
Chris Morgan3afbc762021-08-05 16:26:38 +0800569 ret = rockchip_sfc_xfer_data_dma(sfc, op, len);
570 else
571 ret = rockchip_sfc_xfer_data_poll(sfc, op, len);
572
573 if (ret != len) {
Jon Lin0accc652021-09-17 21:14:58 +0800574 dev_err(sfc->dev, "xfer data failed ret %d dir %d\n", ret, op->data.dir);
Chris Morgan3afbc762021-08-05 16:26:38 +0800575
576 return -EIO;
577 }
578 }
579
580 return rockchip_sfc_xfer_done(sfc, 100000);
581}
582
583static int rockchip_sfc_adjust_op_size(struct spi_slave *mem, struct spi_mem_op *op)
584{
585 struct rockchip_sfc *sfc = dev_get_plat(mem->dev->parent);
586
587 op->data.nbytes = min(op->data.nbytes, sfc->max_iosize);
Jon Lin0accc652021-09-17 21:14:58 +0800588
Chris Morgan3afbc762021-08-05 16:26:38 +0800589 return 0;
590}
591
592static int rockchip_sfc_set_speed(struct udevice *bus, uint speed)
593{
Jon Lin0accc652021-09-17 21:14:58 +0800594 struct rockchip_sfc *sfc = dev_get_plat(bus);
595
596 if (speed > sfc->max_freq)
597 speed = sfc->max_freq;
598
599 if (speed == sfc->speed)
600 return 0;
601
602#if CONFIG_IS_ENABLED(CLK)
603 int ret = clk_set_rate(&sfc->clk, speed);
604
605 if (ret < 0) {
606 dev_err(sfc->dev, "set_freq=%dHz fail, check if it's the cru support level\n",
607 speed);
608 return ret;
609 }
610 sfc->speed = speed;
611#else
612 dev_dbg(sfc->dev, "sfc failed, CLK not support\n");
613#endif
Chris Morgan3afbc762021-08-05 16:26:38 +0800614 return 0;
615}
616
617static int rockchip_sfc_set_mode(struct udevice *bus, uint mode)
618{
619 return 0;
620}
621
622static const struct spi_controller_mem_ops rockchip_sfc_mem_ops = {
623 .adjust_op_size = rockchip_sfc_adjust_op_size,
624 .exec_op = rockchip_sfc_exec_op,
625};
626
627static const struct dm_spi_ops rockchip_sfc_ops = {
628 .mem_ops = &rockchip_sfc_mem_ops,
629 .set_speed = rockchip_sfc_set_speed,
630 .set_mode = rockchip_sfc_set_mode,
631};
632
633static const struct udevice_id rockchip_sfc_ids[] = {
634 { .compatible = "rockchip,sfc"},
635 {},
636};
637
638U_BOOT_DRIVER(rockchip_sfc_driver) = {
639 .name = "rockchip_sfc",
640 .id = UCLASS_SPI,
641 .of_match = rockchip_sfc_ids,
642 .ops = &rockchip_sfc_ops,
643 .of_to_plat = rockchip_sfc_ofdata_to_platdata,
644 .plat_auto = sizeof(struct rockchip_sfc),
645 .probe = rockchip_sfc_probe,
646};