1 // SPDX-License-Identifier: GPL-2.0+
3 * spi driver for rockchip
5 * (C) 2019 Theobroma Systems Design und Consulting GmbH
7 * (C) Copyright 2015 Google, Inc
9 * (C) Copyright 2008-2013 Rockchip Electronics
10 * Peter, Software Engineering, <superpeter.cai@gmail.com>.
16 #include <dt-structs.h>
19 #include <linux/errno.h>
21 #include <asm/arch/clock.h>
22 #include <asm/arch/periph.h>
23 #include <dm/pinctrl.h>
26 /* Change to 1 to output registers at the start of each transaction */
27 #define DEBUG_RK_SPI 0
29 struct rockchip_spi_platdata {
30 #if CONFIG_IS_ENABLED(OF_PLATDATA)
31 struct dtd_rockchip_rk3288_spi of_plat;
33 s32 frequency; /* Default clock frequency, -1 for none */
35 uint deactivate_delay_us; /* Delay to wait after deactivate */
36 uint activate_delay_us; /* Delay to wait after activate */
39 struct rockchip_spi_priv {
40 struct rockchip_spi *regs;
42 unsigned int max_freq;
44 ulong last_transaction_us; /* Time of last transaction end */
45 unsigned int speed_hz;
46 unsigned int last_speed_hz;
50 #define SPI_FIFO_DEPTH 32
52 static void rkspi_dump_regs(struct rockchip_spi *regs)
54 debug("ctrl0: \t\t0x%08x\n", readl(®s->ctrlr0));
55 debug("ctrl1: \t\t0x%08x\n", readl(®s->ctrlr1));
56 debug("ssienr: \t\t0x%08x\n", readl(®s->enr));
57 debug("ser: \t\t0x%08x\n", readl(®s->ser));
58 debug("baudr: \t\t0x%08x\n", readl(®s->baudr));
59 debug("txftlr: \t\t0x%08x\n", readl(®s->txftlr));
60 debug("rxftlr: \t\t0x%08x\n", readl(®s->rxftlr));
61 debug("txflr: \t\t0x%08x\n", readl(®s->txflr));
62 debug("rxflr: \t\t0x%08x\n", readl(®s->rxflr));
63 debug("sr: \t\t0x%08x\n", readl(®s->sr));
64 debug("imr: \t\t0x%08x\n", readl(®s->imr));
65 debug("isr: \t\t0x%08x\n", readl(®s->isr));
66 debug("dmacr: \t\t0x%08x\n", readl(®s->dmacr));
67 debug("dmatdlr: \t0x%08x\n", readl(®s->dmatdlr));
68 debug("dmardlr: \t0x%08x\n", readl(®s->dmardlr));
71 static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable)
73 writel(enable ? 1 : 0, ®s->enr);
76 static void rkspi_set_clk(struct rockchip_spi_priv *priv, uint speed)
79 * We should try not to exceed the speed requested by the caller:
80 * when selecting a divider, we need to make sure we round up.
82 uint clk_div = DIV_ROUND_UP(priv->input_rate, speed);
84 /* The baudrate register (BAUDR) is defined as a 32bit register where
85 * the upper 16bit are reserved and having 'Fsclk_out' in the lower
86 * 16bits with 'Fsclk_out' defined as follows:
88 * Fsclk_out = Fspi_clk/ SCKDV
89 * Where SCKDV is any even value between 2 and 65534.
91 if (clk_div > 0xfffe) {
93 debug("%s: can't divide down to %d Hz (actual will be %d Hz)\n",
94 __func__, speed, priv->input_rate / clk_div);
97 /* Round up to the next even 16bit number */
98 clk_div = (clk_div + 1) & 0xfffe;
100 debug("spi speed %u, div %u\n", speed, clk_div);
102 clrsetbits_le32(&priv->regs->baudr, 0xffff, clk_div);
103 priv->last_speed_hz = speed;
106 static int rkspi_wait_till_not_busy(struct rockchip_spi *regs)
110 start = get_timer(0);
111 while (readl(®s->sr) & SR_BUSY) {
112 if (get_timer(start) > ROCKCHIP_SPI_TIMEOUT_MS) {
113 debug("RK SPI: Status keeps busy for 1000us after a read/write!\n");
121 static void spi_cs_activate(struct udevice *dev, uint cs)
123 struct udevice *bus = dev->parent;
124 struct rockchip_spi_platdata *plat = bus->platdata;
125 struct rockchip_spi_priv *priv = dev_get_priv(bus);
126 struct rockchip_spi *regs = priv->regs;
128 /* If it's too soon to do another transaction, wait */
129 if (plat->deactivate_delay_us && priv->last_transaction_us) {
130 ulong delay_us; /* The delay completed so far */
131 delay_us = timer_get_us() - priv->last_transaction_us;
132 if (delay_us < plat->deactivate_delay_us) {
133 ulong additional_delay_us =
134 plat->deactivate_delay_us - delay_us;
135 debug("%s: delaying by %ld us\n",
136 __func__, additional_delay_us);
137 udelay(additional_delay_us);
141 debug("activate cs%u\n", cs);
142 writel(1 << cs, ®s->ser);
143 if (plat->activate_delay_us)
144 udelay(plat->activate_delay_us);
147 static void spi_cs_deactivate(struct udevice *dev, uint cs)
149 struct udevice *bus = dev->parent;
150 struct rockchip_spi_platdata *plat = bus->platdata;
151 struct rockchip_spi_priv *priv = dev_get_priv(bus);
152 struct rockchip_spi *regs = priv->regs;
154 debug("deactivate cs%u\n", cs);
155 writel(0, ®s->ser);
157 /* Remember time of this transaction so we can honour the bus delay */
158 if (plat->deactivate_delay_us)
159 priv->last_transaction_us = timer_get_us();
162 #if CONFIG_IS_ENABLED(OF_PLATDATA)
163 static int conv_of_platdata(struct udevice *dev)
165 struct rockchip_spi_platdata *plat = dev->platdata;
166 struct dtd_rockchip_rk3288_spi *dtplat = &plat->of_plat;
167 struct rockchip_spi_priv *priv = dev_get_priv(dev);
170 plat->base = dtplat->reg[0];
171 plat->frequency = 20000000;
172 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->clk);
181 static int rockchip_spi_ofdata_to_platdata(struct udevice *bus)
183 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
184 struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
185 struct rockchip_spi_priv *priv = dev_get_priv(bus);
188 plat->base = dev_read_addr(bus);
190 ret = clk_get_by_index(bus, 0, &priv->clk);
192 debug("%s: Could not get clock for %s: %d\n", __func__,
198 dev_read_u32_default(bus, "spi-max-frequency", 50000000);
199 plat->deactivate_delay_us =
200 dev_read_u32_default(bus, "spi-deactivate-delay", 0);
201 plat->activate_delay_us =
202 dev_read_u32_default(bus, "spi-activate-delay", 0);
204 debug("%s: base=%x, max-frequency=%d, deactivate_delay=%d\n",
205 __func__, (uint)plat->base, plat->frequency,
206 plat->deactivate_delay_us);
212 static int rockchip_spi_calc_modclk(ulong max_freq)
215 * While this is not strictly correct for the RK3368, as the
216 * GPLL will be 576MHz, things will still work, as the
217 * clk_set_rate(...) implementation in our clock-driver will
218 * chose the next closest rate not exceeding what we request
219 * based on the output of this function.
223 const unsigned long gpll_hz = 594000000UL;
226 * We need to find an input clock that provides at least twice
227 * the maximum frequency and can be generated from the assumed
228 * speed of GPLL (594MHz) using an integer divider.
230 * To give us more achievable bitrates at higher speeds (these
231 * are generated by dividing by an even 16-bit integer from
232 * this frequency), we try to have an input frequency of at
233 * least 4x our max_freq.
236 div = DIV_ROUND_UP(gpll_hz, max_freq * 4);
237 return gpll_hz / div;
240 static int rockchip_spi_probe(struct udevice *bus)
242 struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
243 struct rockchip_spi_priv *priv = dev_get_priv(bus);
246 debug("%s: probe\n", __func__);
247 #if CONFIG_IS_ENABLED(OF_PLATDATA)
248 ret = conv_of_platdata(bus);
252 priv->regs = (struct rockchip_spi *)plat->base;
254 priv->last_transaction_us = timer_get_us();
255 priv->max_freq = plat->frequency;
257 /* Clamp the value from the DTS against any hardware limits */
258 if (priv->max_freq > ROCKCHIP_SPI_MAX_RATE)
259 priv->max_freq = ROCKCHIP_SPI_MAX_RATE;
261 /* Find a module-input clock that fits with the max_freq setting */
262 ret = clk_set_rate(&priv->clk,
263 rockchip_spi_calc_modclk(priv->max_freq));
265 debug("%s: Failed to set clock: %d\n", __func__, ret);
268 priv->input_rate = ret;
269 debug("%s: rate = %u\n", __func__, priv->input_rate);
274 static int rockchip_spi_claim_bus(struct udevice *dev)
276 struct udevice *bus = dev->parent;
277 struct rockchip_spi_priv *priv = dev_get_priv(bus);
278 struct rockchip_spi *regs = priv->regs;
281 /* Disable the SPI hardware */
282 rkspi_enable_chip(regs, false);
284 if (priv->speed_hz != priv->last_speed_hz)
285 rkspi_set_clk(priv, priv->speed_hz);
288 ctrlr0 = OMOD_MASTER << OMOD_SHIFT;
290 /* Data Frame Size */
291 ctrlr0 |= DFS_8BIT << DFS_SHIFT;
293 /* set SPI mode 0..3 */
294 if (priv->mode & SPI_CPOL)
295 ctrlr0 |= SCOL_HIGH << SCOL_SHIFT;
296 if (priv->mode & SPI_CPHA)
297 ctrlr0 |= SCPH_TOGSTA << SCPH_SHIFT;
299 /* Chip Select Mode */
300 ctrlr0 |= CSM_KEEP << CSM_SHIFT;
302 /* SSN to Sclk_out delay */
303 ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT;
305 /* Serial Endian Mode */
306 ctrlr0 |= SEM_LITTLE << SEM_SHIFT;
309 ctrlr0 |= FBM_MSB << FBM_SHIFT;
311 /* Byte and Halfword Transform */
312 ctrlr0 |= HALF_WORD_OFF << HALF_WORD_TX_SHIFT;
314 /* Rxd Sample Delay */
315 ctrlr0 |= 0 << RXDSD_SHIFT;
318 ctrlr0 |= FRF_SPI << FRF_SHIFT;
321 ctrlr0 |= TMOD_TR << TMOD_SHIFT;
323 writel(ctrlr0, ®s->ctrlr0);
328 static int rockchip_spi_release_bus(struct udevice *dev)
330 struct udevice *bus = dev->parent;
331 struct rockchip_spi_priv *priv = dev_get_priv(bus);
333 rkspi_enable_chip(priv->regs, false);
338 static inline int rockchip_spi_16bit_reader(struct udevice *dev,
341 struct udevice *bus = dev->parent;
342 const struct rockchip_spi_params * const data =
343 (void *)dev_get_driver_data(bus);
344 struct rockchip_spi_priv *priv = dev_get_priv(bus);
345 struct rockchip_spi *regs = priv->regs;
346 const u32 saved_ctrlr0 = readl(®s->ctrlr0);
348 u32 statistics_rxlevels[33] = { };
350 u32 frames = *len / 2;
351 u16 *in16 = (u16 *)(*din);
352 u32 max_chunk_size = SPI_FIFO_DEPTH;
358 * If the destination buffer is unaligned, we'd run into a problem
359 * on ARMv8. Given that this doesn't seem to be a real issue, we
360 * just chicken out and fall back to the unoptimised implementation.
362 if ((uintptr_t)*din & 1) {
363 debug("%s: unaligned buffer, din = %p\n", __func__, *din);
367 // rockchip_spi_configure(dev, mode, size)
368 rkspi_enable_chip(regs, false);
369 clrsetbits_le32(®s->ctrlr0,
370 TMOD_MASK << TMOD_SHIFT,
371 TMOD_RO << TMOD_SHIFT);
372 /* 16bit data frame size */
373 clrsetbits_le32(®s->ctrlr0, DFS_MASK, DFS_16BIT);
375 /* Update caller's context */
376 const u32 bytes_to_process = 2 * frames;
377 *din += bytes_to_process;
378 *len -= bytes_to_process;
380 /* Process our frames */
382 u32 chunk_size = min(frames, max_chunk_size);
384 frames -= chunk_size;
386 writew(chunk_size - 1, ®s->ctrlr1);
387 rkspi_enable_chip(regs, true);
390 u32 rx_level = readw(®s->rxflr);
392 statistics_rxlevels[rx_level]++;
394 chunk_size -= rx_level;
396 *in16++ = readw(regs->rxdr);
397 } while (chunk_size);
399 rkspi_enable_chip(regs, false);
403 debug("%s: observed rx_level during processing:\n", __func__);
404 for (int i = 0; i <= 32; ++i)
405 if (statistics_rxlevels[i])
406 debug("\t%2d: %d\n", i, statistics_rxlevels[i]);
408 /* Restore the original transfer setup and return error-free. */
409 writel(saved_ctrlr0, ®s->ctrlr0);
413 static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen,
414 const void *dout, void *din, unsigned long flags)
416 struct udevice *bus = dev->parent;
417 struct rockchip_spi_priv *priv = dev_get_priv(bus);
418 struct rockchip_spi *regs = priv->regs;
419 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
420 int len = bitlen >> 3;
421 const u8 *out = dout;
426 debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
429 rkspi_dump_regs(regs);
431 /* Assert CS before transfer */
432 if (flags & SPI_XFER_BEGIN)
433 spi_cs_activate(dev, slave_plat->cs);
436 * To ensure fast loading of firmware images (e.g. full U-Boot
437 * stage, ATF, Linux kernel) from SPI flash, we optimise the
438 * case of read-only transfers by using the full 16bits of each
442 ret = rockchip_spi_16bit_reader(dev, &in, &len);
444 /* This is the original 8bit reader/writer code */
446 int todo = min(len, 0x10000);
448 rkspi_enable_chip(regs, false);
449 writel(todo - 1, ®s->ctrlr1);
450 rkspi_enable_chip(regs, true);
454 while (toread || towrite) {
455 u32 status = readl(®s->sr);
457 if (towrite && !(status & SR_TF_FULL)) {
458 writel(out ? *out++ : 0, regs->txdr);
461 if (toread && !(status & SR_RF_EMPT)) {
462 u32 byte = readl(regs->rxdr);
471 * In case that there's a transmit-component, we need to wait
472 * until the control goes idle before we can disable the SPI
473 * control logic (as this will implictly flush the FIFOs).
476 ret = rkspi_wait_till_not_busy(regs);
484 /* Deassert CS after transfer */
485 if (flags & SPI_XFER_END)
486 spi_cs_deactivate(dev, slave_plat->cs);
488 rkspi_enable_chip(regs, false);
493 static int rockchip_spi_set_speed(struct udevice *bus, uint speed)
495 struct rockchip_spi_priv *priv = dev_get_priv(bus);
497 /* Clamp to the maximum frequency specified in the DTS */
498 if (speed > priv->max_freq)
499 speed = priv->max_freq;
501 priv->speed_hz = speed;
506 static int rockchip_spi_set_mode(struct udevice *bus, uint mode)
508 struct rockchip_spi_priv *priv = dev_get_priv(bus);
515 static const struct dm_spi_ops rockchip_spi_ops = {
516 .claim_bus = rockchip_spi_claim_bus,
517 .release_bus = rockchip_spi_release_bus,
518 .xfer = rockchip_spi_xfer,
519 .set_speed = rockchip_spi_set_speed,
520 .set_mode = rockchip_spi_set_mode,
522 * cs_info is not needed, since we require all chip selects to be
523 * in the device tree explicitly
527 static const struct udevice_id rockchip_spi_ids[] = {
528 { .compatible = "rockchip,rk3288-spi" },
529 { .compatible = "rockchip,rk3368-spi" },
530 { .compatible = "rockchip,rk3399-spi" },
534 U_BOOT_DRIVER(rockchip_spi) = {
535 #if CONFIG_IS_ENABLED(OF_PLATDATA)
536 .name = "rockchip_rk3288_spi",
538 .name = "rockchip_spi",
541 .of_match = rockchip_spi_ids,
542 .ops = &rockchip_spi_ops,
543 .ofdata_to_platdata = rockchip_spi_ofdata_to_platdata,
544 .platdata_auto_alloc_size = sizeof(struct rockchip_spi_platdata),
545 .priv_auto_alloc_size = sizeof(struct rockchip_spi_priv),
546 .probe = rockchip_spi_probe,