blob: 4571dc9f9b697be404bbb23c8690bdb53c1ca58b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd1c13772015-09-01 19:19:37 -06002/*
3 * spi driver for rockchip
4 *
Philipp Tomsich8e453992019-02-03 16:17:31 +01005 * (C) 2019 Theobroma Systems Design und Consulting GmbH
6 *
Simon Glassd1c13772015-09-01 19:19:37 -06007 * (C) Copyright 2015 Google, Inc
8 *
9 * (C) Copyright 2008-2013 Rockchip Electronics
10 * Peter, Software Engineering, <superpeter.cai@gmail.com>.
Simon Glassd1c13772015-09-01 19:19:37 -060011 */
12
Simon Glassd1c13772015-09-01 19:19:37 -060013#include <clk.h>
14#include <dm.h>
Simon Glass32308d22016-11-13 14:22:02 -070015#include <dt-structs.h>
Simon Glassd1c13772015-09-01 19:19:37 -060016#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Simon Glassd1c13772015-09-01 19:19:37 -060018#include <spi.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070019#include <time.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090021#include <linux/errno.h>
Simon Glassd1c13772015-09-01 19:19:37 -060022#include <asm/io.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080023#include <asm/arch-rockchip/clock.h>
24#include <asm/arch-rockchip/periph.h>
Simon Glassd1c13772015-09-01 19:19:37 -060025#include <dm/pinctrl.h>
26#include "rk_spi.h"
27
Simon Glassd1c13772015-09-01 19:19:37 -060028/* Change to 1 to output registers at the start of each transaction */
29#define DEBUG_RK_SPI 0
30
Jagan Teki46bbff12019-12-21 13:24:30 +053031/*
32 * ctrlr1 is 16-bits, so we should support lengths of 0xffff + 1. However,
33 * the controller seems to hang when given 0x10000, so stick with this for now.
34 */
35#define ROCKCHIP_SPI_MAX_TRANLEN 0xffff
36
Philipp Tomsich8d1b09c2019-02-03 16:17:32 +010037struct rockchip_spi_params {
38 /* RXFIFO overruns and TXFIFO underruns stop the master clock */
39 bool master_manages_fifo;
40};
41
Simon Glassb75b15b2020-12-03 16:55:23 -070042struct rockchip_spi_plat {
Simon Glass32308d22016-11-13 14:22:02 -070043#if CONFIG_IS_ENABLED(OF_PLATDATA)
44 struct dtd_rockchip_rk3288_spi of_plat;
45#endif
Simon Glassd1c13772015-09-01 19:19:37 -060046 s32 frequency; /* Default clock frequency, -1 for none */
Johan Jonker625eff92023-03-13 01:30:20 +010047 uintptr_t base;
Simon Glassd1c13772015-09-01 19:19:37 -060048 uint deactivate_delay_us; /* Delay to wait after deactivate */
Simon Glass58a52e82016-01-21 19:44:10 -070049 uint activate_delay_us; /* Delay to wait after activate */
Simon Glassd1c13772015-09-01 19:19:37 -060050};
51
52struct rockchip_spi_priv {
53 struct rockchip_spi *regs;
Stephen Warrena9622432016-06-17 09:44:00 -060054 struct clk clk;
Simon Glassd1c13772015-09-01 19:19:37 -060055 unsigned int max_freq;
56 unsigned int mode;
Simon Glassd1c13772015-09-01 19:19:37 -060057 ulong last_transaction_us; /* Time of last transaction end */
Simon Glassd1c13772015-09-01 19:19:37 -060058 unsigned int speed_hz;
Simon Glass30508e92016-01-21 19:44:03 -070059 unsigned int last_speed_hz;
Simon Glassd1c13772015-09-01 19:19:37 -060060 uint input_rate;
61};
62
63#define SPI_FIFO_DEPTH 32
64
65static void rkspi_dump_regs(struct rockchip_spi *regs)
66{
67 debug("ctrl0: \t\t0x%08x\n", readl(&regs->ctrlr0));
68 debug("ctrl1: \t\t0x%08x\n", readl(&regs->ctrlr1));
69 debug("ssienr: \t\t0x%08x\n", readl(&regs->enr));
70 debug("ser: \t\t0x%08x\n", readl(&regs->ser));
71 debug("baudr: \t\t0x%08x\n", readl(&regs->baudr));
72 debug("txftlr: \t\t0x%08x\n", readl(&regs->txftlr));
73 debug("rxftlr: \t\t0x%08x\n", readl(&regs->rxftlr));
74 debug("txflr: \t\t0x%08x\n", readl(&regs->txflr));
75 debug("rxflr: \t\t0x%08x\n", readl(&regs->rxflr));
76 debug("sr: \t\t0x%08x\n", readl(&regs->sr));
77 debug("imr: \t\t0x%08x\n", readl(&regs->imr));
78 debug("isr: \t\t0x%08x\n", readl(&regs->isr));
79 debug("dmacr: \t\t0x%08x\n", readl(&regs->dmacr));
80 debug("dmatdlr: \t0x%08x\n", readl(&regs->dmatdlr));
81 debug("dmardlr: \t0x%08x\n", readl(&regs->dmardlr));
82}
83
84static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable)
85{
86 writel(enable ? 1 : 0, &regs->enr);
87}
88
89static void rkspi_set_clk(struct rockchip_spi_priv *priv, uint speed)
90{
Philipp Tomsich0f5b4c92017-04-20 22:05:52 +020091 /*
92 * We should try not to exceed the speed requested by the caller:
93 * when selecting a divider, we need to make sure we round up.
94 */
95 uint clk_div = DIV_ROUND_UP(priv->input_rate, speed);
Simon Glassd1c13772015-09-01 19:19:37 -060096
Philipp Tomsich0f5b4c92017-04-20 22:05:52 +020097 /* The baudrate register (BAUDR) is defined as a 32bit register where
98 * the upper 16bit are reserved and having 'Fsclk_out' in the lower
99 * 16bits with 'Fsclk_out' defined as follows:
100 *
101 * Fsclk_out = Fspi_clk/ SCKDV
102 * Where SCKDV is any even value between 2 and 65534.
103 */
104 if (clk_div > 0xfffe) {
105 clk_div = 0xfffe;
Heinrich Schuchardt348c8782017-11-12 20:59:44 +0100106 debug("%s: can't divide down to %d Hz (actual will be %d Hz)\n",
Philipp Tomsich0f5b4c92017-04-20 22:05:52 +0200107 __func__, speed, priv->input_rate / clk_div);
108 }
109
110 /* Round up to the next even 16bit number */
111 clk_div = (clk_div + 1) & 0xfffe;
112
Simon Glassd1c13772015-09-01 19:19:37 -0600113 debug("spi speed %u, div %u\n", speed, clk_div);
114
Philipp Tomsich0f5b4c92017-04-20 22:05:52 +0200115 clrsetbits_le32(&priv->regs->baudr, 0xffff, clk_div);
Simon Glass30508e92016-01-21 19:44:03 -0700116 priv->last_speed_hz = speed;
Simon Glassd1c13772015-09-01 19:19:37 -0600117}
118
119static int rkspi_wait_till_not_busy(struct rockchip_spi *regs)
120{
121 unsigned long start;
122
123 start = get_timer(0);
124 while (readl(&regs->sr) & SR_BUSY) {
125 if (get_timer(start) > ROCKCHIP_SPI_TIMEOUT_MS) {
126 debug("RK SPI: Status keeps busy for 1000us after a read/write!\n");
127 return -ETIMEDOUT;
128 }
129 }
130
131 return 0;
132}
133
Simon Glass58a52e82016-01-21 19:44:10 -0700134static void spi_cs_activate(struct udevice *dev, uint cs)
Simon Glassd1c13772015-09-01 19:19:37 -0600135{
Simon Glass58a52e82016-01-21 19:44:10 -0700136 struct udevice *bus = dev->parent;
Simon Glass95588622020-12-22 19:30:28 -0700137 struct rockchip_spi_plat *plat = dev_get_plat(bus);
Simon Glass58a52e82016-01-21 19:44:10 -0700138 struct rockchip_spi_priv *priv = dev_get_priv(bus);
139 struct rockchip_spi *regs = priv->regs;
140
Simon Glasscf65b002016-11-13 14:22:03 -0700141 /* If it's too soon to do another transaction, wait */
142 if (plat->deactivate_delay_us && priv->last_transaction_us) {
143 ulong delay_us; /* The delay completed so far */
144 delay_us = timer_get_us() - priv->last_transaction_us;
Philipp Tomsich8cb1d582019-02-03 16:17:26 +0100145 if (delay_us < plat->deactivate_delay_us) {
146 ulong additional_delay_us =
147 plat->deactivate_delay_us - delay_us;
148 debug("%s: delaying by %ld us\n",
149 __func__, additional_delay_us);
150 udelay(additional_delay_us);
151 }
Simon Glasscf65b002016-11-13 14:22:03 -0700152 }
153
Simon Glassd1c13772015-09-01 19:19:37 -0600154 debug("activate cs%u\n", cs);
155 writel(1 << cs, &regs->ser);
Simon Glass58a52e82016-01-21 19:44:10 -0700156 if (plat->activate_delay_us)
157 udelay(plat->activate_delay_us);
Simon Glassd1c13772015-09-01 19:19:37 -0600158}
159
Simon Glass58a52e82016-01-21 19:44:10 -0700160static void spi_cs_deactivate(struct udevice *dev, uint cs)
Simon Glassd1c13772015-09-01 19:19:37 -0600161{
Simon Glass58a52e82016-01-21 19:44:10 -0700162 struct udevice *bus = dev->parent;
Simon Glass95588622020-12-22 19:30:28 -0700163 struct rockchip_spi_plat *plat = dev_get_plat(bus);
Simon Glass58a52e82016-01-21 19:44:10 -0700164 struct rockchip_spi_priv *priv = dev_get_priv(bus);
165 struct rockchip_spi *regs = priv->regs;
166
Simon Glassd1c13772015-09-01 19:19:37 -0600167 debug("deactivate cs%u\n", cs);
168 writel(0, &regs->ser);
Simon Glass58a52e82016-01-21 19:44:10 -0700169
170 /* Remember time of this transaction so we can honour the bus delay */
171 if (plat->deactivate_delay_us)
172 priv->last_transaction_us = timer_get_us();
Simon Glassd1c13772015-09-01 19:19:37 -0600173}
174
Simon Glass32308d22016-11-13 14:22:02 -0700175#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -0700176static int conv_of_plat(struct udevice *dev)
Simon Glass32308d22016-11-13 14:22:02 -0700177{
Simon Glass95588622020-12-22 19:30:28 -0700178 struct rockchip_spi_plat *plat = dev_get_plat(dev);
Simon Glass32308d22016-11-13 14:22:02 -0700179 struct dtd_rockchip_rk3288_spi *dtplat = &plat->of_plat;
180 struct rockchip_spi_priv *priv = dev_get_priv(dev);
181 int ret;
182
183 plat->base = dtplat->reg[0];
184 plat->frequency = 20000000;
Simon Glass1257efc2021-08-07 07:24:09 -0600185 ret = clk_get_by_phandle(dev, dtplat->clocks, &priv->clk);
Simon Glass32308d22016-11-13 14:22:02 -0700186 if (ret < 0)
187 return ret;
Simon Glass32308d22016-11-13 14:22:02 -0700188
189 return 0;
190}
191#endif
192
Simon Glassaad29ae2020-12-03 16:55:21 -0700193static int rockchip_spi_of_to_plat(struct udevice *bus)
Simon Glassd1c13772015-09-01 19:19:37 -0600194{
Simon Glassb75b15b2020-12-03 16:55:23 -0700195 struct rockchip_spi_plat *plat = dev_get_plat(bus);
Simon Glassa95049e2016-01-21 19:43:43 -0700196 struct rockchip_spi_priv *priv = dev_get_priv(bus);
Simon Glassd1c13772015-09-01 19:19:37 -0600197 int ret;
198
Simon Glass6d70ba02021-08-07 07:24:06 -0600199 if (CONFIG_IS_ENABLED(OF_REAL)) {
200 plat->base = dev_read_addr(bus);
Simon Glassd1c13772015-09-01 19:19:37 -0600201
Simon Glass6d70ba02021-08-07 07:24:06 -0600202 ret = clk_get_by_index(bus, 0, &priv->clk);
203 if (ret < 0) {
204 debug("%s: Could not get clock for %s: %d\n", __func__,
205 bus->name, ret);
206 return ret;
207 }
Simon Glassd1c13772015-09-01 19:19:37 -0600208
Simon Glass6d70ba02021-08-07 07:24:06 -0600209 plat->frequency = dev_read_u32_default(bus, "spi-max-frequency",
210 50000000);
211 plat->deactivate_delay_us =
212 dev_read_u32_default(bus, "spi-deactivate-delay", 0);
213 plat->activate_delay_us =
214 dev_read_u32_default(bus, "spi-activate-delay", 0);
Philipp Tomsich778ec662017-06-07 18:45:58 +0200215
Simon Glass6d70ba02021-08-07 07:24:06 -0600216 debug("%s: base=%x, max-frequency=%d, deactivate_delay=%d\n",
217 __func__, (uint)plat->base, plat->frequency,
218 plat->deactivate_delay_us);
219 }
Simon Glassd1c13772015-09-01 19:19:37 -0600220
221 return 0;
222}
223
Philipp Tomsich45447882017-04-20 22:05:51 +0200224static int rockchip_spi_calc_modclk(ulong max_freq)
225{
Philipp Tomsichc720b892017-07-25 16:25:30 +0200226 /*
227 * While this is not strictly correct for the RK3368, as the
228 * GPLL will be 576MHz, things will still work, as the
229 * clk_set_rate(...) implementation in our clock-driver will
230 * chose the next closest rate not exceeding what we request
231 * based on the output of this function.
232 */
233
Philipp Tomsich45447882017-04-20 22:05:51 +0200234 unsigned div;
235 const unsigned long gpll_hz = 594000000UL;
236
237 /*
238 * We need to find an input clock that provides at least twice
239 * the maximum frequency and can be generated from the assumed
240 * speed of GPLL (594MHz) using an integer divider.
241 *
242 * To give us more achievable bitrates at higher speeds (these
243 * are generated by dividing by an even 16-bit integer from
244 * this frequency), we try to have an input frequency of at
245 * least 4x our max_freq.
246 */
247
248 div = DIV_ROUND_UP(gpll_hz, max_freq * 4);
249 return gpll_hz / div;
250}
251
Simon Glassd1c13772015-09-01 19:19:37 -0600252static int rockchip_spi_probe(struct udevice *bus)
253{
Simon Glassb75b15b2020-12-03 16:55:23 -0700254 struct rockchip_spi_plat *plat = dev_get_plat(bus);
Simon Glassd1c13772015-09-01 19:19:37 -0600255 struct rockchip_spi_priv *priv = dev_get_priv(bus);
256 int ret;
257
258 debug("%s: probe\n", __func__);
Simon Glass32308d22016-11-13 14:22:02 -0700259#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -0700260 ret = conv_of_plat(bus);
Simon Glass32308d22016-11-13 14:22:02 -0700261 if (ret)
262 return ret;
263#endif
Simon Glassd1c13772015-09-01 19:19:37 -0600264 priv->regs = (struct rockchip_spi *)plat->base;
265
266 priv->last_transaction_us = timer_get_us();
267 priv->max_freq = plat->frequency;
Simon Glassd1c13772015-09-01 19:19:37 -0600268
Philipp Tomsich45447882017-04-20 22:05:51 +0200269 /* Clamp the value from the DTS against any hardware limits */
270 if (priv->max_freq > ROCKCHIP_SPI_MAX_RATE)
271 priv->max_freq = ROCKCHIP_SPI_MAX_RATE;
272
273 /* Find a module-input clock that fits with the max_freq setting */
274 ret = clk_set_rate(&priv->clk,
275 rockchip_spi_calc_modclk(priv->max_freq));
Simon Glassd1c13772015-09-01 19:19:37 -0600276 if (ret < 0) {
277 debug("%s: Failed to set clock: %d\n", __func__, ret);
278 return ret;
279 }
280 priv->input_rate = ret;
281 debug("%s: rate = %u\n", __func__, priv->input_rate);
Simon Glassd1c13772015-09-01 19:19:37 -0600282
283 return 0;
284}
285
286static int rockchip_spi_claim_bus(struct udevice *dev)
287{
288 struct udevice *bus = dev->parent;
Simon Glassd1c13772015-09-01 19:19:37 -0600289 struct rockchip_spi_priv *priv = dev_get_priv(bus);
290 struct rockchip_spi *regs = priv->regs;
Simon Glassd1c13772015-09-01 19:19:37 -0600291 uint ctrlr0;
Simon Glassd1c13772015-09-01 19:19:37 -0600292
293 /* Disable the SPI hardware */
Philipp Tomsich5755fff2019-02-03 16:17:29 +0100294 rkspi_enable_chip(regs, false);
Simon Glassd1c13772015-09-01 19:19:37 -0600295
Simon Glass30508e92016-01-21 19:44:03 -0700296 if (priv->speed_hz != priv->last_speed_hz)
297 rkspi_set_clk(priv, priv->speed_hz);
Simon Glassd1c13772015-09-01 19:19:37 -0600298
299 /* Operation Mode */
300 ctrlr0 = OMOD_MASTER << OMOD_SHIFT;
301
302 /* Data Frame Size */
Philipp Tomsich1b35a512019-02-03 16:17:27 +0100303 ctrlr0 |= DFS_8BIT << DFS_SHIFT;
Simon Glassd1c13772015-09-01 19:19:37 -0600304
305 /* set SPI mode 0..3 */
306 if (priv->mode & SPI_CPOL)
307 ctrlr0 |= SCOL_HIGH << SCOL_SHIFT;
308 if (priv->mode & SPI_CPHA)
309 ctrlr0 |= SCPH_TOGSTA << SCPH_SHIFT;
310
311 /* Chip Select Mode */
312 ctrlr0 |= CSM_KEEP << CSM_SHIFT;
313
314 /* SSN to Sclk_out delay */
315 ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT;
316
317 /* Serial Endian Mode */
318 ctrlr0 |= SEM_LITTLE << SEM_SHIFT;
319
320 /* First Bit Mode */
321 ctrlr0 |= FBM_MSB << FBM_SHIFT;
322
323 /* Byte and Halfword Transform */
Philipp Tomsich1b35a512019-02-03 16:17:27 +0100324 ctrlr0 |= HALF_WORD_OFF << HALF_WORD_TX_SHIFT;
Simon Glassd1c13772015-09-01 19:19:37 -0600325
326 /* Rxd Sample Delay */
327 ctrlr0 |= 0 << RXDSD_SHIFT;
328
329 /* Frame Format */
330 ctrlr0 |= FRF_SPI << FRF_SHIFT;
331
332 /* Tx and Rx mode */
Philipp Tomsich1b35a512019-02-03 16:17:27 +0100333 ctrlr0 |= TMOD_TR << TMOD_SHIFT;
Simon Glassd1c13772015-09-01 19:19:37 -0600334
335 writel(ctrlr0, &regs->ctrlr0);
Simon Glassd1c13772015-09-01 19:19:37 -0600336
337 return 0;
338}
339
340static int rockchip_spi_release_bus(struct udevice *dev)
341{
Simon Glass2c9693c2016-01-21 19:44:11 -0700342 struct udevice *bus = dev->parent;
343 struct rockchip_spi_priv *priv = dev_get_priv(bus);
344
345 rkspi_enable_chip(priv->regs, false);
346
Philipp Tomsich8e453992019-02-03 16:17:31 +0100347 return 0;
348}
349
350static inline int rockchip_spi_16bit_reader(struct udevice *dev,
351 u8 **din, int *len)
352{
353 struct udevice *bus = dev->parent;
354 const struct rockchip_spi_params * const data =
355 (void *)dev_get_driver_data(bus);
356 struct rockchip_spi_priv *priv = dev_get_priv(bus);
357 struct rockchip_spi *regs = priv->regs;
358 const u32 saved_ctrlr0 = readl(&regs->ctrlr0);
359#if defined(DEBUG)
360 u32 statistics_rxlevels[33] = { };
361#endif
362 u32 frames = *len / 2;
Philipp Tomsich09258c92019-02-03 16:17:33 +0100363 u8 *in = (u8 *)(*din);
Philipp Tomsich8e453992019-02-03 16:17:31 +0100364 u32 max_chunk_size = SPI_FIFO_DEPTH;
365
366 if (!frames)
367 return 0;
368
369 /*
Philipp Tomsich8d1b09c2019-02-03 16:17:32 +0100370 * If we know that the hardware will manage RXFIFO overruns
371 * (i.e. stop the SPI clock until there's space in the FIFO),
372 * we the allow largest possible chunk size that can be
373 * represented in CTRLR1.
374 */
375 if (data && data->master_manages_fifo)
Jagan Teki46bbff12019-12-21 13:24:30 +0530376 max_chunk_size = ROCKCHIP_SPI_MAX_TRANLEN;
Philipp Tomsich8d1b09c2019-02-03 16:17:32 +0100377
Philipp Tomsich8e453992019-02-03 16:17:31 +0100378 // rockchip_spi_configure(dev, mode, size)
379 rkspi_enable_chip(regs, false);
380 clrsetbits_le32(&regs->ctrlr0,
381 TMOD_MASK << TMOD_SHIFT,
382 TMOD_RO << TMOD_SHIFT);
383 /* 16bit data frame size */
384 clrsetbits_le32(&regs->ctrlr0, DFS_MASK, DFS_16BIT);
385
386 /* Update caller's context */
387 const u32 bytes_to_process = 2 * frames;
388 *din += bytes_to_process;
389 *len -= bytes_to_process;
390
391 /* Process our frames */
392 while (frames) {
393 u32 chunk_size = min(frames, max_chunk_size);
394
395 frames -= chunk_size;
396
397 writew(chunk_size - 1, &regs->ctrlr1);
398 rkspi_enable_chip(regs, true);
399
400 do {
401 u32 rx_level = readw(&regs->rxflr);
402#if defined(DEBUG)
403 statistics_rxlevels[rx_level]++;
404#endif
405 chunk_size -= rx_level;
Philipp Tomsich09258c92019-02-03 16:17:33 +0100406 while (rx_level--) {
407 u16 val = readw(regs->rxdr);
408 *in++ = val & 0xff;
409 *in++ = val >> 8;
410 }
Philipp Tomsich8e453992019-02-03 16:17:31 +0100411 } while (chunk_size);
412
413 rkspi_enable_chip(regs, false);
414 }
415
416#if defined(DEBUG)
417 debug("%s: observed rx_level during processing:\n", __func__);
418 for (int i = 0; i <= 32; ++i)
419 if (statistics_rxlevels[i])
420 debug("\t%2d: %d\n", i, statistics_rxlevels[i]);
421#endif
422 /* Restore the original transfer setup and return error-free. */
423 writel(saved_ctrlr0, &regs->ctrlr0);
Simon Glassd1c13772015-09-01 19:19:37 -0600424 return 0;
425}
426
427static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen,
428 const void *dout, void *din, unsigned long flags)
429{
430 struct udevice *bus = dev->parent;
431 struct rockchip_spi_priv *priv = dev_get_priv(bus);
432 struct rockchip_spi *regs = priv->regs;
Simon Glassb75b15b2020-12-03 16:55:23 -0700433 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Simon Glassd1c13772015-09-01 19:19:37 -0600434 int len = bitlen >> 3;
435 const u8 *out = dout;
436 u8 *in = din;
437 int toread, towrite;
Philipp Tomsich8e453992019-02-03 16:17:31 +0100438 int ret = 0;
Simon Glassd1c13772015-09-01 19:19:37 -0600439
440 debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
441 len, flags);
442 if (DEBUG_RK_SPI)
443 rkspi_dump_regs(regs);
444
445 /* Assert CS before transfer */
446 if (flags & SPI_XFER_BEGIN)
Simon Glass58a52e82016-01-21 19:44:10 -0700447 spi_cs_activate(dev, slave_plat->cs);
Simon Glassd1c13772015-09-01 19:19:37 -0600448
Philipp Tomsich8e453992019-02-03 16:17:31 +0100449 /*
450 * To ensure fast loading of firmware images (e.g. full U-Boot
451 * stage, ATF, Linux kernel) from SPI flash, we optimise the
452 * case of read-only transfers by using the full 16bits of each
453 * FIFO element.
454 */
Quentin Schulzc0301882024-03-14 10:36:14 +0100455 if (!out) {
Philipp Tomsich8e453992019-02-03 16:17:31 +0100456 ret = rockchip_spi_16bit_reader(dev, &in, &len);
Quentin Schulzc0301882024-03-14 10:36:14 +0100457 /*
458 * If "in" isn't 16b-aligned, we need to send the last byte
459 * ourselves. We however need to have the controller in RO mode
460 * which differs from the default.
461 */
462 clrsetbits_le32(&regs->ctrlr0,
463 TMOD_MASK << TMOD_SHIFT,
464 TMOD_RO << TMOD_SHIFT);
465 }
Philipp Tomsich8e453992019-02-03 16:17:31 +0100466
467 /* This is the original 8bit reader/writer code */
Simon Glassd1c13772015-09-01 19:19:37 -0600468 while (len > 0) {
Jagan Teki46bbff12019-12-21 13:24:30 +0530469 int todo = min(len, ROCKCHIP_SPI_MAX_TRANLEN);
Simon Glassd1c13772015-09-01 19:19:37 -0600470
Simon Glass2c9693c2016-01-21 19:44:11 -0700471 rkspi_enable_chip(regs, false);
Simon Glassd1c13772015-09-01 19:19:37 -0600472 writel(todo - 1, &regs->ctrlr1);
473 rkspi_enable_chip(regs, true);
474
475 toread = todo;
Quentin Schulzc0301882024-03-14 10:36:14 +0100476 /* Only write if we have something to write */
477 towrite = out ? todo : 0;
Simon Glassd1c13772015-09-01 19:19:37 -0600478 while (toread || towrite) {
479 u32 status = readl(&regs->sr);
480
481 if (towrite && !(status & SR_TF_FULL)) {
Quentin Schulzc0301882024-03-14 10:36:14 +0100482 writel(*out++, regs->txdr);
Simon Glassd1c13772015-09-01 19:19:37 -0600483 towrite--;
484 }
485 if (toread && !(status & SR_RF_EMPT)) {
486 u32 byte = readl(regs->rxdr);
487
488 if (in)
489 *in++ = byte;
490 toread--;
491 }
492 }
Philipp Tomsiche3cc1a22019-02-03 16:17:30 +0100493
494 /*
495 * In case that there's a transmit-component, we need to wait
496 * until the control goes idle before we can disable the SPI
Pengfei Fan746271d2022-12-09 09:39:50 +0800497 * control logic (as this will implicitly flush the FIFOs).
Philipp Tomsiche3cc1a22019-02-03 16:17:30 +0100498 */
499 if (out) {
500 ret = rkspi_wait_till_not_busy(regs);
501 if (ret)
502 break;
503 }
504
Simon Glassd1c13772015-09-01 19:19:37 -0600505 len -= todo;
506 }
507
508 /* Deassert CS after transfer */
509 if (flags & SPI_XFER_END)
Simon Glass58a52e82016-01-21 19:44:10 -0700510 spi_cs_deactivate(dev, slave_plat->cs);
Simon Glassd1c13772015-09-01 19:19:37 -0600511
512 rkspi_enable_chip(regs, false);
Quentin Schulzc0301882024-03-14 10:36:14 +0100513 if (!out)
514 clrsetbits_le32(&regs->ctrlr0,
515 TMOD_MASK << TMOD_SHIFT,
516 TMOD_TR << TMOD_SHIFT);
Simon Glassd1c13772015-09-01 19:19:37 -0600517
518 return ret;
519}
520
521static int rockchip_spi_set_speed(struct udevice *bus, uint speed)
522{
523 struct rockchip_spi_priv *priv = dev_get_priv(bus);
524
Philipp Tomsich45447882017-04-20 22:05:51 +0200525 /* Clamp to the maximum frequency specified in the DTS */
Simon Glassd1c13772015-09-01 19:19:37 -0600526 if (speed > priv->max_freq)
527 speed = priv->max_freq;
Philipp Tomsich45447882017-04-20 22:05:51 +0200528
Simon Glassd1c13772015-09-01 19:19:37 -0600529 priv->speed_hz = speed;
530
531 return 0;
532}
533
534static int rockchip_spi_set_mode(struct udevice *bus, uint mode)
535{
536 struct rockchip_spi_priv *priv = dev_get_priv(bus);
537
538 priv->mode = mode;
539
540 return 0;
541}
542
543static const struct dm_spi_ops rockchip_spi_ops = {
544 .claim_bus = rockchip_spi_claim_bus,
545 .release_bus = rockchip_spi_release_bus,
546 .xfer = rockchip_spi_xfer,
547 .set_speed = rockchip_spi_set_speed,
548 .set_mode = rockchip_spi_set_mode,
549 /*
550 * cs_info is not needed, since we require all chip selects to be
551 * in the device tree explicitly
552 */
553};
554
Philipp Tomsich8d1b09c2019-02-03 16:17:32 +0100555const struct rockchip_spi_params rk3399_spi_params = {
556 .master_manages_fifo = true,
557};
558
Simon Glassd1c13772015-09-01 19:19:37 -0600559static const struct udevice_id rockchip_spi_ids[] = {
Johannes Krottmayera601d5b2020-07-08 23:57:39 +0200560 { .compatible = "rockchip,rk3066-spi" },
Simon Glassd1c13772015-09-01 19:19:37 -0600561 { .compatible = "rockchip,rk3288-spi" },
Johannes Krottmayera601d5b2020-07-08 23:57:39 +0200562 { .compatible = "rockchip,rk3328-spi" },
Philipp Tomsich8d1b09c2019-02-03 16:17:32 +0100563 { .compatible = "rockchip,rk3368-spi",
564 .data = (ulong)&rk3399_spi_params },
565 { .compatible = "rockchip,rk3399-spi",
566 .data = (ulong)&rk3399_spi_params },
Simon Glassd1c13772015-09-01 19:19:37 -0600567 { }
568};
569
Walter Lozano2901ac62020-06-25 01:10:04 -0300570U_BOOT_DRIVER(rockchip_rk3288_spi) = {
Simon Glass32308d22016-11-13 14:22:02 -0700571 .name = "rockchip_rk3288_spi",
Simon Glassd1c13772015-09-01 19:19:37 -0600572 .id = UCLASS_SPI,
573 .of_match = rockchip_spi_ids,
574 .ops = &rockchip_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700575 .of_to_plat = rockchip_spi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700576 .plat_auto = sizeof(struct rockchip_spi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700577 .priv_auto = sizeof(struct rockchip_spi_priv),
Simon Glassd1c13772015-09-01 19:19:37 -0600578 .probe = rockchip_spi_probe,
579};
Walter Lozano48e5b042020-06-25 01:10:06 -0300580
Simon Glassdf65db82020-12-28 20:34:57 -0700581DM_DRIVER_ALIAS(rockchip_rk3288_spi, rockchip_rk3368_spi)