blob: c8694fdff954c90c86147f087ae1e4cd89af700b [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
13#include <common.h>
14#include <clk.h>
15#include <dm.h>
Simon Glass32308d22016-11-13 14:22:02 -070016#include <dt-structs.h>
Simon Glassd1c13772015-09-01 19:19:37 -060017#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Simon Glassd1c13772015-09-01 19:19:37 -060019#include <spi.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070020#include <time.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090022#include <linux/errno.h>
Simon Glassd1c13772015-09-01 19:19:37 -060023#include <asm/io.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080024#include <asm/arch-rockchip/clock.h>
25#include <asm/arch-rockchip/periph.h>
Simon Glassd1c13772015-09-01 19:19:37 -060026#include <dm/pinctrl.h>
27#include "rk_spi.h"
28
Simon Glassd1c13772015-09-01 19:19:37 -060029/* Change to 1 to output registers at the start of each transaction */
30#define DEBUG_RK_SPI 0
31
Jagan Teki46bbff12019-12-21 13:24:30 +053032/*
33 * ctrlr1 is 16-bits, so we should support lengths of 0xffff + 1. However,
34 * the controller seems to hang when given 0x10000, so stick with this for now.
35 */
36#define ROCKCHIP_SPI_MAX_TRANLEN 0xffff
37
Philipp Tomsich8d1b09c2019-02-03 16:17:32 +010038struct rockchip_spi_params {
39 /* RXFIFO overruns and TXFIFO underruns stop the master clock */
40 bool master_manages_fifo;
41};
42
Simon Glassb75b15b2020-12-03 16:55:23 -070043struct rockchip_spi_plat {
Simon Glass32308d22016-11-13 14:22:02 -070044#if CONFIG_IS_ENABLED(OF_PLATDATA)
45 struct dtd_rockchip_rk3288_spi of_plat;
46#endif
Simon Glassd1c13772015-09-01 19:19:37 -060047 s32 frequency; /* Default clock frequency, -1 for none */
Johan Jonker625eff92023-03-13 01:30:20 +010048 uintptr_t base;
Simon Glassd1c13772015-09-01 19:19:37 -060049 uint deactivate_delay_us; /* Delay to wait after deactivate */
Simon Glass58a52e82016-01-21 19:44:10 -070050 uint activate_delay_us; /* Delay to wait after activate */
Simon Glassd1c13772015-09-01 19:19:37 -060051};
52
53struct rockchip_spi_priv {
54 struct rockchip_spi *regs;
Stephen Warrena9622432016-06-17 09:44:00 -060055 struct clk clk;
Simon Glassd1c13772015-09-01 19:19:37 -060056 unsigned int max_freq;
57 unsigned int mode;
Simon Glassd1c13772015-09-01 19:19:37 -060058 ulong last_transaction_us; /* Time of last transaction end */
Simon Glassd1c13772015-09-01 19:19:37 -060059 unsigned int speed_hz;
Simon Glass30508e92016-01-21 19:44:03 -070060 unsigned int last_speed_hz;
Simon Glassd1c13772015-09-01 19:19:37 -060061 uint input_rate;
62};
63
64#define SPI_FIFO_DEPTH 32
65
66static void rkspi_dump_regs(struct rockchip_spi *regs)
67{
68 debug("ctrl0: \t\t0x%08x\n", readl(&regs->ctrlr0));
69 debug("ctrl1: \t\t0x%08x\n", readl(&regs->ctrlr1));
70 debug("ssienr: \t\t0x%08x\n", readl(&regs->enr));
71 debug("ser: \t\t0x%08x\n", readl(&regs->ser));
72 debug("baudr: \t\t0x%08x\n", readl(&regs->baudr));
73 debug("txftlr: \t\t0x%08x\n", readl(&regs->txftlr));
74 debug("rxftlr: \t\t0x%08x\n", readl(&regs->rxftlr));
75 debug("txflr: \t\t0x%08x\n", readl(&regs->txflr));
76 debug("rxflr: \t\t0x%08x\n", readl(&regs->rxflr));
77 debug("sr: \t\t0x%08x\n", readl(&regs->sr));
78 debug("imr: \t\t0x%08x\n", readl(&regs->imr));
79 debug("isr: \t\t0x%08x\n", readl(&regs->isr));
80 debug("dmacr: \t\t0x%08x\n", readl(&regs->dmacr));
81 debug("dmatdlr: \t0x%08x\n", readl(&regs->dmatdlr));
82 debug("dmardlr: \t0x%08x\n", readl(&regs->dmardlr));
83}
84
85static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable)
86{
87 writel(enable ? 1 : 0, &regs->enr);
88}
89
90static void rkspi_set_clk(struct rockchip_spi_priv *priv, uint speed)
91{
Philipp Tomsich0f5b4c92017-04-20 22:05:52 +020092 /*
93 * We should try not to exceed the speed requested by the caller:
94 * when selecting a divider, we need to make sure we round up.
95 */
96 uint clk_div = DIV_ROUND_UP(priv->input_rate, speed);
Simon Glassd1c13772015-09-01 19:19:37 -060097
Philipp Tomsich0f5b4c92017-04-20 22:05:52 +020098 /* The baudrate register (BAUDR) is defined as a 32bit register where
99 * the upper 16bit are reserved and having 'Fsclk_out' in the lower
100 * 16bits with 'Fsclk_out' defined as follows:
101 *
102 * Fsclk_out = Fspi_clk/ SCKDV
103 * Where SCKDV is any even value between 2 and 65534.
104 */
105 if (clk_div > 0xfffe) {
106 clk_div = 0xfffe;
Heinrich Schuchardt348c8782017-11-12 20:59:44 +0100107 debug("%s: can't divide down to %d Hz (actual will be %d Hz)\n",
Philipp Tomsich0f5b4c92017-04-20 22:05:52 +0200108 __func__, speed, priv->input_rate / clk_div);
109 }
110
111 /* Round up to the next even 16bit number */
112 clk_div = (clk_div + 1) & 0xfffe;
113
Simon Glassd1c13772015-09-01 19:19:37 -0600114 debug("spi speed %u, div %u\n", speed, clk_div);
115
Philipp Tomsich0f5b4c92017-04-20 22:05:52 +0200116 clrsetbits_le32(&priv->regs->baudr, 0xffff, clk_div);
Simon Glass30508e92016-01-21 19:44:03 -0700117 priv->last_speed_hz = speed;
Simon Glassd1c13772015-09-01 19:19:37 -0600118}
119
120static int rkspi_wait_till_not_busy(struct rockchip_spi *regs)
121{
122 unsigned long start;
123
124 start = get_timer(0);
125 while (readl(&regs->sr) & SR_BUSY) {
126 if (get_timer(start) > ROCKCHIP_SPI_TIMEOUT_MS) {
127 debug("RK SPI: Status keeps busy for 1000us after a read/write!\n");
128 return -ETIMEDOUT;
129 }
130 }
131
132 return 0;
133}
134
Simon Glass58a52e82016-01-21 19:44:10 -0700135static void spi_cs_activate(struct udevice *dev, uint cs)
Simon Glassd1c13772015-09-01 19:19:37 -0600136{
Simon Glass58a52e82016-01-21 19:44:10 -0700137 struct udevice *bus = dev->parent;
Simon Glass95588622020-12-22 19:30:28 -0700138 struct rockchip_spi_plat *plat = dev_get_plat(bus);
Simon Glass58a52e82016-01-21 19:44:10 -0700139 struct rockchip_spi_priv *priv = dev_get_priv(bus);
140 struct rockchip_spi *regs = priv->regs;
141
Simon Glasscf65b002016-11-13 14:22:03 -0700142 /* If it's too soon to do another transaction, wait */
143 if (plat->deactivate_delay_us && priv->last_transaction_us) {
144 ulong delay_us; /* The delay completed so far */
145 delay_us = timer_get_us() - priv->last_transaction_us;
Philipp Tomsich8cb1d582019-02-03 16:17:26 +0100146 if (delay_us < plat->deactivate_delay_us) {
147 ulong additional_delay_us =
148 plat->deactivate_delay_us - delay_us;
149 debug("%s: delaying by %ld us\n",
150 __func__, additional_delay_us);
151 udelay(additional_delay_us);
152 }
Simon Glasscf65b002016-11-13 14:22:03 -0700153 }
154
Simon Glassd1c13772015-09-01 19:19:37 -0600155 debug("activate cs%u\n", cs);
156 writel(1 << cs, &regs->ser);
Simon Glass58a52e82016-01-21 19:44:10 -0700157 if (plat->activate_delay_us)
158 udelay(plat->activate_delay_us);
Simon Glassd1c13772015-09-01 19:19:37 -0600159}
160
Simon Glass58a52e82016-01-21 19:44:10 -0700161static void spi_cs_deactivate(struct udevice *dev, uint cs)
Simon Glassd1c13772015-09-01 19:19:37 -0600162{
Simon Glass58a52e82016-01-21 19:44:10 -0700163 struct udevice *bus = dev->parent;
Simon Glass95588622020-12-22 19:30:28 -0700164 struct rockchip_spi_plat *plat = dev_get_plat(bus);
Simon Glass58a52e82016-01-21 19:44:10 -0700165 struct rockchip_spi_priv *priv = dev_get_priv(bus);
166 struct rockchip_spi *regs = priv->regs;
167
Simon Glassd1c13772015-09-01 19:19:37 -0600168 debug("deactivate cs%u\n", cs);
169 writel(0, &regs->ser);
Simon Glass58a52e82016-01-21 19:44:10 -0700170
171 /* Remember time of this transaction so we can honour the bus delay */
172 if (plat->deactivate_delay_us)
173 priv->last_transaction_us = timer_get_us();
Simon Glassd1c13772015-09-01 19:19:37 -0600174}
175
Simon Glass32308d22016-11-13 14:22:02 -0700176#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -0700177static int conv_of_plat(struct udevice *dev)
Simon Glass32308d22016-11-13 14:22:02 -0700178{
Simon Glass95588622020-12-22 19:30:28 -0700179 struct rockchip_spi_plat *plat = dev_get_plat(dev);
Simon Glass32308d22016-11-13 14:22:02 -0700180 struct dtd_rockchip_rk3288_spi *dtplat = &plat->of_plat;
181 struct rockchip_spi_priv *priv = dev_get_priv(dev);
182 int ret;
183
184 plat->base = dtplat->reg[0];
185 plat->frequency = 20000000;
Simon Glass1257efc2021-08-07 07:24:09 -0600186 ret = clk_get_by_phandle(dev, dtplat->clocks, &priv->clk);
Simon Glass32308d22016-11-13 14:22:02 -0700187 if (ret < 0)
188 return ret;
Simon Glass32308d22016-11-13 14:22:02 -0700189
190 return 0;
191}
192#endif
193
Simon Glassaad29ae2020-12-03 16:55:21 -0700194static int rockchip_spi_of_to_plat(struct udevice *bus)
Simon Glassd1c13772015-09-01 19:19:37 -0600195{
Simon Glassb75b15b2020-12-03 16:55:23 -0700196 struct rockchip_spi_plat *plat = dev_get_plat(bus);
Simon Glassa95049e2016-01-21 19:43:43 -0700197 struct rockchip_spi_priv *priv = dev_get_priv(bus);
Simon Glassd1c13772015-09-01 19:19:37 -0600198 int ret;
199
Simon Glass6d70ba02021-08-07 07:24:06 -0600200 if (CONFIG_IS_ENABLED(OF_REAL)) {
201 plat->base = dev_read_addr(bus);
Simon Glassd1c13772015-09-01 19:19:37 -0600202
Simon Glass6d70ba02021-08-07 07:24:06 -0600203 ret = clk_get_by_index(bus, 0, &priv->clk);
204 if (ret < 0) {
205 debug("%s: Could not get clock for %s: %d\n", __func__,
206 bus->name, ret);
207 return ret;
208 }
Simon Glassd1c13772015-09-01 19:19:37 -0600209
Simon Glass6d70ba02021-08-07 07:24:06 -0600210 plat->frequency = dev_read_u32_default(bus, "spi-max-frequency",
211 50000000);
212 plat->deactivate_delay_us =
213 dev_read_u32_default(bus, "spi-deactivate-delay", 0);
214 plat->activate_delay_us =
215 dev_read_u32_default(bus, "spi-activate-delay", 0);
Philipp Tomsich778ec662017-06-07 18:45:58 +0200216
Simon Glass6d70ba02021-08-07 07:24:06 -0600217 debug("%s: base=%x, max-frequency=%d, deactivate_delay=%d\n",
218 __func__, (uint)plat->base, plat->frequency,
219 plat->deactivate_delay_us);
220 }
Simon Glassd1c13772015-09-01 19:19:37 -0600221
222 return 0;
223}
224
Philipp Tomsich45447882017-04-20 22:05:51 +0200225static int rockchip_spi_calc_modclk(ulong max_freq)
226{
Philipp Tomsichc720b892017-07-25 16:25:30 +0200227 /*
228 * While this is not strictly correct for the RK3368, as the
229 * GPLL will be 576MHz, things will still work, as the
230 * clk_set_rate(...) implementation in our clock-driver will
231 * chose the next closest rate not exceeding what we request
232 * based on the output of this function.
233 */
234
Philipp Tomsich45447882017-04-20 22:05:51 +0200235 unsigned div;
236 const unsigned long gpll_hz = 594000000UL;
237
238 /*
239 * We need to find an input clock that provides at least twice
240 * the maximum frequency and can be generated from the assumed
241 * speed of GPLL (594MHz) using an integer divider.
242 *
243 * To give us more achievable bitrates at higher speeds (these
244 * are generated by dividing by an even 16-bit integer from
245 * this frequency), we try to have an input frequency of at
246 * least 4x our max_freq.
247 */
248
249 div = DIV_ROUND_UP(gpll_hz, max_freq * 4);
250 return gpll_hz / div;
251}
252
Simon Glassd1c13772015-09-01 19:19:37 -0600253static int rockchip_spi_probe(struct udevice *bus)
254{
Simon Glassb75b15b2020-12-03 16:55:23 -0700255 struct rockchip_spi_plat *plat = dev_get_plat(bus);
Simon Glassd1c13772015-09-01 19:19:37 -0600256 struct rockchip_spi_priv *priv = dev_get_priv(bus);
257 int ret;
258
259 debug("%s: probe\n", __func__);
Simon Glass32308d22016-11-13 14:22:02 -0700260#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -0700261 ret = conv_of_plat(bus);
Simon Glass32308d22016-11-13 14:22:02 -0700262 if (ret)
263 return ret;
264#endif
Simon Glassd1c13772015-09-01 19:19:37 -0600265 priv->regs = (struct rockchip_spi *)plat->base;
266
267 priv->last_transaction_us = timer_get_us();
268 priv->max_freq = plat->frequency;
Simon Glassd1c13772015-09-01 19:19:37 -0600269
Philipp Tomsich45447882017-04-20 22:05:51 +0200270 /* Clamp the value from the DTS against any hardware limits */
271 if (priv->max_freq > ROCKCHIP_SPI_MAX_RATE)
272 priv->max_freq = ROCKCHIP_SPI_MAX_RATE;
273
274 /* Find a module-input clock that fits with the max_freq setting */
275 ret = clk_set_rate(&priv->clk,
276 rockchip_spi_calc_modclk(priv->max_freq));
Simon Glassd1c13772015-09-01 19:19:37 -0600277 if (ret < 0) {
278 debug("%s: Failed to set clock: %d\n", __func__, ret);
279 return ret;
280 }
281 priv->input_rate = ret;
282 debug("%s: rate = %u\n", __func__, priv->input_rate);
Simon Glassd1c13772015-09-01 19:19:37 -0600283
284 return 0;
285}
286
287static int rockchip_spi_claim_bus(struct udevice *dev)
288{
289 struct udevice *bus = dev->parent;
Simon Glassd1c13772015-09-01 19:19:37 -0600290 struct rockchip_spi_priv *priv = dev_get_priv(bus);
291 struct rockchip_spi *regs = priv->regs;
Simon Glassd1c13772015-09-01 19:19:37 -0600292 uint ctrlr0;
Simon Glassd1c13772015-09-01 19:19:37 -0600293
294 /* Disable the SPI hardware */
Philipp Tomsich5755fff2019-02-03 16:17:29 +0100295 rkspi_enable_chip(regs, false);
Simon Glassd1c13772015-09-01 19:19:37 -0600296
Simon Glass30508e92016-01-21 19:44:03 -0700297 if (priv->speed_hz != priv->last_speed_hz)
298 rkspi_set_clk(priv, priv->speed_hz);
Simon Glassd1c13772015-09-01 19:19:37 -0600299
300 /* Operation Mode */
301 ctrlr0 = OMOD_MASTER << OMOD_SHIFT;
302
303 /* Data Frame Size */
Philipp Tomsich1b35a512019-02-03 16:17:27 +0100304 ctrlr0 |= DFS_8BIT << DFS_SHIFT;
Simon Glassd1c13772015-09-01 19:19:37 -0600305
306 /* set SPI mode 0..3 */
307 if (priv->mode & SPI_CPOL)
308 ctrlr0 |= SCOL_HIGH << SCOL_SHIFT;
309 if (priv->mode & SPI_CPHA)
310 ctrlr0 |= SCPH_TOGSTA << SCPH_SHIFT;
311
312 /* Chip Select Mode */
313 ctrlr0 |= CSM_KEEP << CSM_SHIFT;
314
315 /* SSN to Sclk_out delay */
316 ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT;
317
318 /* Serial Endian Mode */
319 ctrlr0 |= SEM_LITTLE << SEM_SHIFT;
320
321 /* First Bit Mode */
322 ctrlr0 |= FBM_MSB << FBM_SHIFT;
323
324 /* Byte and Halfword Transform */
Philipp Tomsich1b35a512019-02-03 16:17:27 +0100325 ctrlr0 |= HALF_WORD_OFF << HALF_WORD_TX_SHIFT;
Simon Glassd1c13772015-09-01 19:19:37 -0600326
327 /* Rxd Sample Delay */
328 ctrlr0 |= 0 << RXDSD_SHIFT;
329
330 /* Frame Format */
331 ctrlr0 |= FRF_SPI << FRF_SHIFT;
332
333 /* Tx and Rx mode */
Philipp Tomsich1b35a512019-02-03 16:17:27 +0100334 ctrlr0 |= TMOD_TR << TMOD_SHIFT;
Simon Glassd1c13772015-09-01 19:19:37 -0600335
336 writel(ctrlr0, &regs->ctrlr0);
Simon Glassd1c13772015-09-01 19:19:37 -0600337
338 return 0;
339}
340
341static int rockchip_spi_release_bus(struct udevice *dev)
342{
Simon Glass2c9693c2016-01-21 19:44:11 -0700343 struct udevice *bus = dev->parent;
344 struct rockchip_spi_priv *priv = dev_get_priv(bus);
345
346 rkspi_enable_chip(priv->regs, false);
347
Philipp Tomsich8e453992019-02-03 16:17:31 +0100348 return 0;
349}
350
351static inline int rockchip_spi_16bit_reader(struct udevice *dev,
352 u8 **din, int *len)
353{
354 struct udevice *bus = dev->parent;
355 const struct rockchip_spi_params * const data =
356 (void *)dev_get_driver_data(bus);
357 struct rockchip_spi_priv *priv = dev_get_priv(bus);
358 struct rockchip_spi *regs = priv->regs;
359 const u32 saved_ctrlr0 = readl(&regs->ctrlr0);
360#if defined(DEBUG)
361 u32 statistics_rxlevels[33] = { };
362#endif
363 u32 frames = *len / 2;
Philipp Tomsich09258c92019-02-03 16:17:33 +0100364 u8 *in = (u8 *)(*din);
Philipp Tomsich8e453992019-02-03 16:17:31 +0100365 u32 max_chunk_size = SPI_FIFO_DEPTH;
366
367 if (!frames)
368 return 0;
369
370 /*
Philipp Tomsich8d1b09c2019-02-03 16:17:32 +0100371 * If we know that the hardware will manage RXFIFO overruns
372 * (i.e. stop the SPI clock until there's space in the FIFO),
373 * we the allow largest possible chunk size that can be
374 * represented in CTRLR1.
375 */
376 if (data && data->master_manages_fifo)
Jagan Teki46bbff12019-12-21 13:24:30 +0530377 max_chunk_size = ROCKCHIP_SPI_MAX_TRANLEN;
Philipp Tomsich8d1b09c2019-02-03 16:17:32 +0100378
Philipp Tomsich8e453992019-02-03 16:17:31 +0100379 // rockchip_spi_configure(dev, mode, size)
380 rkspi_enable_chip(regs, false);
381 clrsetbits_le32(&regs->ctrlr0,
382 TMOD_MASK << TMOD_SHIFT,
383 TMOD_RO << TMOD_SHIFT);
384 /* 16bit data frame size */
385 clrsetbits_le32(&regs->ctrlr0, DFS_MASK, DFS_16BIT);
386
387 /* Update caller's context */
388 const u32 bytes_to_process = 2 * frames;
389 *din += bytes_to_process;
390 *len -= bytes_to_process;
391
392 /* Process our frames */
393 while (frames) {
394 u32 chunk_size = min(frames, max_chunk_size);
395
396 frames -= chunk_size;
397
398 writew(chunk_size - 1, &regs->ctrlr1);
399 rkspi_enable_chip(regs, true);
400
401 do {
402 u32 rx_level = readw(&regs->rxflr);
403#if defined(DEBUG)
404 statistics_rxlevels[rx_level]++;
405#endif
406 chunk_size -= rx_level;
Philipp Tomsich09258c92019-02-03 16:17:33 +0100407 while (rx_level--) {
408 u16 val = readw(regs->rxdr);
409 *in++ = val & 0xff;
410 *in++ = val >> 8;
411 }
Philipp Tomsich8e453992019-02-03 16:17:31 +0100412 } while (chunk_size);
413
414 rkspi_enable_chip(regs, false);
415 }
416
417#if defined(DEBUG)
418 debug("%s: observed rx_level during processing:\n", __func__);
419 for (int i = 0; i <= 32; ++i)
420 if (statistics_rxlevels[i])
421 debug("\t%2d: %d\n", i, statistics_rxlevels[i]);
422#endif
423 /* Restore the original transfer setup and return error-free. */
424 writel(saved_ctrlr0, &regs->ctrlr0);
Simon Glassd1c13772015-09-01 19:19:37 -0600425 return 0;
426}
427
428static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen,
429 const void *dout, void *din, unsigned long flags)
430{
431 struct udevice *bus = dev->parent;
432 struct rockchip_spi_priv *priv = dev_get_priv(bus);
433 struct rockchip_spi *regs = priv->regs;
Simon Glassb75b15b2020-12-03 16:55:23 -0700434 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Simon Glassd1c13772015-09-01 19:19:37 -0600435 int len = bitlen >> 3;
436 const u8 *out = dout;
437 u8 *in = din;
438 int toread, towrite;
Philipp Tomsich8e453992019-02-03 16:17:31 +0100439 int ret = 0;
Simon Glassd1c13772015-09-01 19:19:37 -0600440
441 debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
442 len, flags);
443 if (DEBUG_RK_SPI)
444 rkspi_dump_regs(regs);
445
446 /* Assert CS before transfer */
447 if (flags & SPI_XFER_BEGIN)
Simon Glass58a52e82016-01-21 19:44:10 -0700448 spi_cs_activate(dev, slave_plat->cs);
Simon Glassd1c13772015-09-01 19:19:37 -0600449
Philipp Tomsich8e453992019-02-03 16:17:31 +0100450 /*
451 * To ensure fast loading of firmware images (e.g. full U-Boot
452 * stage, ATF, Linux kernel) from SPI flash, we optimise the
453 * case of read-only transfers by using the full 16bits of each
454 * FIFO element.
455 */
Quentin Schulzc0301882024-03-14 10:36:14 +0100456 if (!out) {
Philipp Tomsich8e453992019-02-03 16:17:31 +0100457 ret = rockchip_spi_16bit_reader(dev, &in, &len);
Quentin Schulzc0301882024-03-14 10:36:14 +0100458 /*
459 * If "in" isn't 16b-aligned, we need to send the last byte
460 * ourselves. We however need to have the controller in RO mode
461 * which differs from the default.
462 */
463 clrsetbits_le32(&regs->ctrlr0,
464 TMOD_MASK << TMOD_SHIFT,
465 TMOD_RO << TMOD_SHIFT);
466 }
Philipp Tomsich8e453992019-02-03 16:17:31 +0100467
468 /* This is the original 8bit reader/writer code */
Simon Glassd1c13772015-09-01 19:19:37 -0600469 while (len > 0) {
Jagan Teki46bbff12019-12-21 13:24:30 +0530470 int todo = min(len, ROCKCHIP_SPI_MAX_TRANLEN);
Simon Glassd1c13772015-09-01 19:19:37 -0600471
Simon Glass2c9693c2016-01-21 19:44:11 -0700472 rkspi_enable_chip(regs, false);
Simon Glassd1c13772015-09-01 19:19:37 -0600473 writel(todo - 1, &regs->ctrlr1);
474 rkspi_enable_chip(regs, true);
475
476 toread = todo;
Quentin Schulzc0301882024-03-14 10:36:14 +0100477 /* Only write if we have something to write */
478 towrite = out ? todo : 0;
Simon Glassd1c13772015-09-01 19:19:37 -0600479 while (toread || towrite) {
480 u32 status = readl(&regs->sr);
481
482 if (towrite && !(status & SR_TF_FULL)) {
Quentin Schulzc0301882024-03-14 10:36:14 +0100483 writel(*out++, regs->txdr);
Simon Glassd1c13772015-09-01 19:19:37 -0600484 towrite--;
485 }
486 if (toread && !(status & SR_RF_EMPT)) {
487 u32 byte = readl(regs->rxdr);
488
489 if (in)
490 *in++ = byte;
491 toread--;
492 }
493 }
Philipp Tomsiche3cc1a22019-02-03 16:17:30 +0100494
495 /*
496 * In case that there's a transmit-component, we need to wait
497 * until the control goes idle before we can disable the SPI
Pengfei Fan746271d2022-12-09 09:39:50 +0800498 * control logic (as this will implicitly flush the FIFOs).
Philipp Tomsiche3cc1a22019-02-03 16:17:30 +0100499 */
500 if (out) {
501 ret = rkspi_wait_till_not_busy(regs);
502 if (ret)
503 break;
504 }
505
Simon Glassd1c13772015-09-01 19:19:37 -0600506 len -= todo;
507 }
508
509 /* Deassert CS after transfer */
510 if (flags & SPI_XFER_END)
Simon Glass58a52e82016-01-21 19:44:10 -0700511 spi_cs_deactivate(dev, slave_plat->cs);
Simon Glassd1c13772015-09-01 19:19:37 -0600512
513 rkspi_enable_chip(regs, false);
Quentin Schulzc0301882024-03-14 10:36:14 +0100514 if (!out)
515 clrsetbits_le32(&regs->ctrlr0,
516 TMOD_MASK << TMOD_SHIFT,
517 TMOD_TR << TMOD_SHIFT);
Simon Glassd1c13772015-09-01 19:19:37 -0600518
519 return ret;
520}
521
522static int rockchip_spi_set_speed(struct udevice *bus, uint speed)
523{
524 struct rockchip_spi_priv *priv = dev_get_priv(bus);
525
Philipp Tomsich45447882017-04-20 22:05:51 +0200526 /* Clamp to the maximum frequency specified in the DTS */
Simon Glassd1c13772015-09-01 19:19:37 -0600527 if (speed > priv->max_freq)
528 speed = priv->max_freq;
Philipp Tomsich45447882017-04-20 22:05:51 +0200529
Simon Glassd1c13772015-09-01 19:19:37 -0600530 priv->speed_hz = speed;
531
532 return 0;
533}
534
535static int rockchip_spi_set_mode(struct udevice *bus, uint mode)
536{
537 struct rockchip_spi_priv *priv = dev_get_priv(bus);
538
539 priv->mode = mode;
540
541 return 0;
542}
543
544static const struct dm_spi_ops rockchip_spi_ops = {
545 .claim_bus = rockchip_spi_claim_bus,
546 .release_bus = rockchip_spi_release_bus,
547 .xfer = rockchip_spi_xfer,
548 .set_speed = rockchip_spi_set_speed,
549 .set_mode = rockchip_spi_set_mode,
550 /*
551 * cs_info is not needed, since we require all chip selects to be
552 * in the device tree explicitly
553 */
554};
555
Philipp Tomsich8d1b09c2019-02-03 16:17:32 +0100556const struct rockchip_spi_params rk3399_spi_params = {
557 .master_manages_fifo = true,
558};
559
Simon Glassd1c13772015-09-01 19:19:37 -0600560static const struct udevice_id rockchip_spi_ids[] = {
Johannes Krottmayera601d5b2020-07-08 23:57:39 +0200561 { .compatible = "rockchip,rk3066-spi" },
Simon Glassd1c13772015-09-01 19:19:37 -0600562 { .compatible = "rockchip,rk3288-spi" },
Johannes Krottmayera601d5b2020-07-08 23:57:39 +0200563 { .compatible = "rockchip,rk3328-spi" },
Philipp Tomsich8d1b09c2019-02-03 16:17:32 +0100564 { .compatible = "rockchip,rk3368-spi",
565 .data = (ulong)&rk3399_spi_params },
566 { .compatible = "rockchip,rk3399-spi",
567 .data = (ulong)&rk3399_spi_params },
Simon Glassd1c13772015-09-01 19:19:37 -0600568 { }
569};
570
Walter Lozano2901ac62020-06-25 01:10:04 -0300571U_BOOT_DRIVER(rockchip_rk3288_spi) = {
Simon Glass32308d22016-11-13 14:22:02 -0700572 .name = "rockchip_rk3288_spi",
Simon Glassd1c13772015-09-01 19:19:37 -0600573 .id = UCLASS_SPI,
574 .of_match = rockchip_spi_ids,
575 .ops = &rockchip_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700576 .of_to_plat = rockchip_spi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700577 .plat_auto = sizeof(struct rockchip_spi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700578 .priv_auto = sizeof(struct rockchip_spi_priv),
Simon Glassd1c13772015-09-01 19:19:37 -0600579 .probe = rockchip_spi_probe,
580};
Walter Lozano48e5b042020-06-25 01:10:06 -0300581
Simon Glassdf65db82020-12-28 20:34:57 -0700582DM_DRIVER_ALIAS(rockchip_rk3288_spi, rockchip_rk3368_spi)