blob: fec41a42439fd78defd6c51a8fcefaac7f1bbcb1 [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>
18#include <spi.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090019#include <linux/errno.h>
Simon Glassd1c13772015-09-01 19:19:37 -060020#include <asm/io.h>
21#include <asm/arch/clock.h>
22#include <asm/arch/periph.h>
23#include <dm/pinctrl.h>
24#include "rk_spi.h"
25
Simon Glassd1c13772015-09-01 19:19:37 -060026/* Change to 1 to output registers at the start of each transaction */
27#define DEBUG_RK_SPI 0
28
29struct rockchip_spi_platdata {
Simon Glass32308d22016-11-13 14:22:02 -070030#if CONFIG_IS_ENABLED(OF_PLATDATA)
31 struct dtd_rockchip_rk3288_spi of_plat;
32#endif
Simon Glassd1c13772015-09-01 19:19:37 -060033 s32 frequency; /* Default clock frequency, -1 for none */
34 fdt_addr_t base;
35 uint deactivate_delay_us; /* Delay to wait after deactivate */
Simon Glass58a52e82016-01-21 19:44:10 -070036 uint activate_delay_us; /* Delay to wait after activate */
Simon Glassd1c13772015-09-01 19:19:37 -060037};
38
39struct rockchip_spi_priv {
40 struct rockchip_spi *regs;
Stephen Warrena9622432016-06-17 09:44:00 -060041 struct clk clk;
Simon Glassd1c13772015-09-01 19:19:37 -060042 unsigned int max_freq;
43 unsigned int mode;
Simon Glassd1c13772015-09-01 19:19:37 -060044 ulong last_transaction_us; /* Time of last transaction end */
Simon Glassd1c13772015-09-01 19:19:37 -060045 unsigned int speed_hz;
Simon Glass30508e92016-01-21 19:44:03 -070046 unsigned int last_speed_hz;
Simon Glassd1c13772015-09-01 19:19:37 -060047 uint input_rate;
48};
49
50#define SPI_FIFO_DEPTH 32
51
52static void rkspi_dump_regs(struct rockchip_spi *regs)
53{
54 debug("ctrl0: \t\t0x%08x\n", readl(&regs->ctrlr0));
55 debug("ctrl1: \t\t0x%08x\n", readl(&regs->ctrlr1));
56 debug("ssienr: \t\t0x%08x\n", readl(&regs->enr));
57 debug("ser: \t\t0x%08x\n", readl(&regs->ser));
58 debug("baudr: \t\t0x%08x\n", readl(&regs->baudr));
59 debug("txftlr: \t\t0x%08x\n", readl(&regs->txftlr));
60 debug("rxftlr: \t\t0x%08x\n", readl(&regs->rxftlr));
61 debug("txflr: \t\t0x%08x\n", readl(&regs->txflr));
62 debug("rxflr: \t\t0x%08x\n", readl(&regs->rxflr));
63 debug("sr: \t\t0x%08x\n", readl(&regs->sr));
64 debug("imr: \t\t0x%08x\n", readl(&regs->imr));
65 debug("isr: \t\t0x%08x\n", readl(&regs->isr));
66 debug("dmacr: \t\t0x%08x\n", readl(&regs->dmacr));
67 debug("dmatdlr: \t0x%08x\n", readl(&regs->dmatdlr));
68 debug("dmardlr: \t0x%08x\n", readl(&regs->dmardlr));
69}
70
71static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable)
72{
73 writel(enable ? 1 : 0, &regs->enr);
74}
75
76static void rkspi_set_clk(struct rockchip_spi_priv *priv, uint speed)
77{
Philipp Tomsich0f5b4c92017-04-20 22:05:52 +020078 /*
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.
81 */
82 uint clk_div = DIV_ROUND_UP(priv->input_rate, speed);
Simon Glassd1c13772015-09-01 19:19:37 -060083
Philipp Tomsich0f5b4c92017-04-20 22:05:52 +020084 /* 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:
87 *
88 * Fsclk_out = Fspi_clk/ SCKDV
89 * Where SCKDV is any even value between 2 and 65534.
90 */
91 if (clk_div > 0xfffe) {
92 clk_div = 0xfffe;
Heinrich Schuchardt348c8782017-11-12 20:59:44 +010093 debug("%s: can't divide down to %d Hz (actual will be %d Hz)\n",
Philipp Tomsich0f5b4c92017-04-20 22:05:52 +020094 __func__, speed, priv->input_rate / clk_div);
95 }
96
97 /* Round up to the next even 16bit number */
98 clk_div = (clk_div + 1) & 0xfffe;
99
Simon Glassd1c13772015-09-01 19:19:37 -0600100 debug("spi speed %u, div %u\n", speed, clk_div);
101
Philipp Tomsich0f5b4c92017-04-20 22:05:52 +0200102 clrsetbits_le32(&priv->regs->baudr, 0xffff, clk_div);
Simon Glass30508e92016-01-21 19:44:03 -0700103 priv->last_speed_hz = speed;
Simon Glassd1c13772015-09-01 19:19:37 -0600104}
105
106static int rkspi_wait_till_not_busy(struct rockchip_spi *regs)
107{
108 unsigned long start;
109
110 start = get_timer(0);
111 while (readl(&regs->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");
114 return -ETIMEDOUT;
115 }
116 }
117
118 return 0;
119}
120
Simon Glass58a52e82016-01-21 19:44:10 -0700121static void spi_cs_activate(struct udevice *dev, uint cs)
Simon Glassd1c13772015-09-01 19:19:37 -0600122{
Simon Glass58a52e82016-01-21 19:44:10 -0700123 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;
127
Simon Glasscf65b002016-11-13 14:22:03 -0700128 /* 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;
Philipp Tomsich8cb1d582019-02-03 16:17:26 +0100132 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);
138 }
Simon Glasscf65b002016-11-13 14:22:03 -0700139 }
140
Simon Glassd1c13772015-09-01 19:19:37 -0600141 debug("activate cs%u\n", cs);
142 writel(1 << cs, &regs->ser);
Simon Glass58a52e82016-01-21 19:44:10 -0700143 if (plat->activate_delay_us)
144 udelay(plat->activate_delay_us);
Simon Glassd1c13772015-09-01 19:19:37 -0600145}
146
Simon Glass58a52e82016-01-21 19:44:10 -0700147static void spi_cs_deactivate(struct udevice *dev, uint cs)
Simon Glassd1c13772015-09-01 19:19:37 -0600148{
Simon Glass58a52e82016-01-21 19:44:10 -0700149 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;
153
Simon Glassd1c13772015-09-01 19:19:37 -0600154 debug("deactivate cs%u\n", cs);
155 writel(0, &regs->ser);
Simon Glass58a52e82016-01-21 19:44:10 -0700156
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();
Simon Glassd1c13772015-09-01 19:19:37 -0600160}
161
Simon Glass32308d22016-11-13 14:22:02 -0700162#if CONFIG_IS_ENABLED(OF_PLATDATA)
163static int conv_of_platdata(struct udevice *dev)
164{
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);
168 int ret;
169
170 plat->base = dtplat->reg[0];
171 plat->frequency = 20000000;
172 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->clk);
173 if (ret < 0)
174 return ret;
175 dev->req_seq = 0;
176
177 return 0;
178}
179#endif
180
Simon Glassd1c13772015-09-01 19:19:37 -0600181static int rockchip_spi_ofdata_to_platdata(struct udevice *bus)
182{
Simon Glass32308d22016-11-13 14:22:02 -0700183#if !CONFIG_IS_ENABLED(OF_PLATDATA)
184 struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
Simon Glassa95049e2016-01-21 19:43:43 -0700185 struct rockchip_spi_priv *priv = dev_get_priv(bus);
Simon Glassd1c13772015-09-01 19:19:37 -0600186 int ret;
187
Philipp Tomsich828dbb52017-09-11 22:04:20 +0200188 plat->base = dev_read_addr(bus);
Simon Glassd1c13772015-09-01 19:19:37 -0600189
Simon Glassa95049e2016-01-21 19:43:43 -0700190 ret = clk_get_by_index(bus, 0, &priv->clk);
191 if (ret < 0) {
192 debug("%s: Could not get clock for %s: %d\n", __func__,
193 bus->name, ret);
194 return ret;
195 }
Simon Glassd1c13772015-09-01 19:19:37 -0600196
Philipp Tomsich778ec662017-06-07 18:45:58 +0200197 plat->frequency =
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);
203
Simon Glassc45aec92016-01-21 19:44:12 -0700204 debug("%s: base=%x, max-frequency=%d, deactivate_delay=%d\n",
205 __func__, (uint)plat->base, plat->frequency,
Simon Glassd1c13772015-09-01 19:19:37 -0600206 plat->deactivate_delay_us);
Simon Glass32308d22016-11-13 14:22:02 -0700207#endif
Simon Glassd1c13772015-09-01 19:19:37 -0600208
209 return 0;
210}
211
Philipp Tomsich45447882017-04-20 22:05:51 +0200212static int rockchip_spi_calc_modclk(ulong max_freq)
213{
Philipp Tomsichc720b892017-07-25 16:25:30 +0200214 /*
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.
220 */
221
Philipp Tomsich45447882017-04-20 22:05:51 +0200222 unsigned div;
223 const unsigned long gpll_hz = 594000000UL;
224
225 /*
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.
229 *
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.
234 */
235
236 div = DIV_ROUND_UP(gpll_hz, max_freq * 4);
237 return gpll_hz / div;
238}
239
Simon Glassd1c13772015-09-01 19:19:37 -0600240static int rockchip_spi_probe(struct udevice *bus)
241{
242 struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
243 struct rockchip_spi_priv *priv = dev_get_priv(bus);
244 int ret;
245
246 debug("%s: probe\n", __func__);
Simon Glass32308d22016-11-13 14:22:02 -0700247#if CONFIG_IS_ENABLED(OF_PLATDATA)
248 ret = conv_of_platdata(bus);
249 if (ret)
250 return ret;
251#endif
Simon Glassd1c13772015-09-01 19:19:37 -0600252 priv->regs = (struct rockchip_spi *)plat->base;
253
254 priv->last_transaction_us = timer_get_us();
255 priv->max_freq = plat->frequency;
Simon Glassd1c13772015-09-01 19:19:37 -0600256
Philipp Tomsich45447882017-04-20 22:05:51 +0200257 /* 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;
260
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));
Simon Glassd1c13772015-09-01 19:19:37 -0600264 if (ret < 0) {
265 debug("%s: Failed to set clock: %d\n", __func__, ret);
266 return ret;
267 }
268 priv->input_rate = ret;
269 debug("%s: rate = %u\n", __func__, priv->input_rate);
Simon Glassd1c13772015-09-01 19:19:37 -0600270
271 return 0;
272}
273
274static int rockchip_spi_claim_bus(struct udevice *dev)
275{
276 struct udevice *bus = dev->parent;
Simon Glassd1c13772015-09-01 19:19:37 -0600277 struct rockchip_spi_priv *priv = dev_get_priv(bus);
278 struct rockchip_spi *regs = priv->regs;
Simon Glassd1c13772015-09-01 19:19:37 -0600279 uint ctrlr0;
Simon Glassd1c13772015-09-01 19:19:37 -0600280
281 /* Disable the SPI hardware */
Philipp Tomsich5755fff2019-02-03 16:17:29 +0100282 rkspi_enable_chip(regs, false);
Simon Glassd1c13772015-09-01 19:19:37 -0600283
Simon Glass30508e92016-01-21 19:44:03 -0700284 if (priv->speed_hz != priv->last_speed_hz)
285 rkspi_set_clk(priv, priv->speed_hz);
Simon Glassd1c13772015-09-01 19:19:37 -0600286
287 /* Operation Mode */
288 ctrlr0 = OMOD_MASTER << OMOD_SHIFT;
289
290 /* Data Frame Size */
Philipp Tomsich1b35a512019-02-03 16:17:27 +0100291 ctrlr0 |= DFS_8BIT << DFS_SHIFT;
Simon Glassd1c13772015-09-01 19:19:37 -0600292
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;
298
299 /* Chip Select Mode */
300 ctrlr0 |= CSM_KEEP << CSM_SHIFT;
301
302 /* SSN to Sclk_out delay */
303 ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT;
304
305 /* Serial Endian Mode */
306 ctrlr0 |= SEM_LITTLE << SEM_SHIFT;
307
308 /* First Bit Mode */
309 ctrlr0 |= FBM_MSB << FBM_SHIFT;
310
311 /* Byte and Halfword Transform */
Philipp Tomsich1b35a512019-02-03 16:17:27 +0100312 ctrlr0 |= HALF_WORD_OFF << HALF_WORD_TX_SHIFT;
Simon Glassd1c13772015-09-01 19:19:37 -0600313
314 /* Rxd Sample Delay */
315 ctrlr0 |= 0 << RXDSD_SHIFT;
316
317 /* Frame Format */
318 ctrlr0 |= FRF_SPI << FRF_SHIFT;
319
320 /* Tx and Rx mode */
Philipp Tomsich1b35a512019-02-03 16:17:27 +0100321 ctrlr0 |= TMOD_TR << TMOD_SHIFT;
Simon Glassd1c13772015-09-01 19:19:37 -0600322
323 writel(ctrlr0, &regs->ctrlr0);
Simon Glassd1c13772015-09-01 19:19:37 -0600324
325 return 0;
326}
327
328static int rockchip_spi_release_bus(struct udevice *dev)
329{
Simon Glass2c9693c2016-01-21 19:44:11 -0700330 struct udevice *bus = dev->parent;
331 struct rockchip_spi_priv *priv = dev_get_priv(bus);
332
333 rkspi_enable_chip(priv->regs, false);
334
Philipp Tomsich8e453992019-02-03 16:17:31 +0100335 return 0;
336}
337
338static inline int rockchip_spi_16bit_reader(struct udevice *dev,
339 u8 **din, int *len)
340{
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(&regs->ctrlr0);
347#if defined(DEBUG)
348 u32 statistics_rxlevels[33] = { };
349#endif
350 u32 frames = *len / 2;
351 u16 *in16 = (u16 *)(*din);
352 u32 max_chunk_size = SPI_FIFO_DEPTH;
353
354 if (!frames)
355 return 0;
356
357 /*
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.
361 */
362 if ((uintptr_t)*din & 1) {
363 debug("%s: unaligned buffer, din = %p\n", __func__, *din);
364 return 0;
365 }
366
367 // rockchip_spi_configure(dev, mode, size)
368 rkspi_enable_chip(regs, false);
369 clrsetbits_le32(&regs->ctrlr0,
370 TMOD_MASK << TMOD_SHIFT,
371 TMOD_RO << TMOD_SHIFT);
372 /* 16bit data frame size */
373 clrsetbits_le32(&regs->ctrlr0, DFS_MASK, DFS_16BIT);
374
375 /* Update caller's context */
376 const u32 bytes_to_process = 2 * frames;
377 *din += bytes_to_process;
378 *len -= bytes_to_process;
379
380 /* Process our frames */
381 while (frames) {
382 u32 chunk_size = min(frames, max_chunk_size);
383
384 frames -= chunk_size;
385
386 writew(chunk_size - 1, &regs->ctrlr1);
387 rkspi_enable_chip(regs, true);
388
389 do {
390 u32 rx_level = readw(&regs->rxflr);
391#if defined(DEBUG)
392 statistics_rxlevels[rx_level]++;
393#endif
394 chunk_size -= rx_level;
395 while (rx_level--)
396 *in16++ = readw(regs->rxdr);
397 } while (chunk_size);
398
399 rkspi_enable_chip(regs, false);
400 }
401
402#if defined(DEBUG)
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]);
407#endif
408 /* Restore the original transfer setup and return error-free. */
409 writel(saved_ctrlr0, &regs->ctrlr0);
Simon Glassd1c13772015-09-01 19:19:37 -0600410 return 0;
411}
412
413static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen,
414 const void *dout, void *din, unsigned long flags)
415{
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;
422 u8 *in = din;
423 int toread, towrite;
Philipp Tomsich8e453992019-02-03 16:17:31 +0100424 int ret = 0;
Simon Glassd1c13772015-09-01 19:19:37 -0600425
426 debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
427 len, flags);
428 if (DEBUG_RK_SPI)
429 rkspi_dump_regs(regs);
430
431 /* Assert CS before transfer */
432 if (flags & SPI_XFER_BEGIN)
Simon Glass58a52e82016-01-21 19:44:10 -0700433 spi_cs_activate(dev, slave_plat->cs);
Simon Glassd1c13772015-09-01 19:19:37 -0600434
Philipp Tomsich8e453992019-02-03 16:17:31 +0100435 /*
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
439 * FIFO element.
440 */
441 if (!out)
442 ret = rockchip_spi_16bit_reader(dev, &in, &len);
443
444 /* This is the original 8bit reader/writer code */
Simon Glassd1c13772015-09-01 19:19:37 -0600445 while (len > 0) {
Philipp Tomsicha5a37162019-02-03 16:17:28 +0100446 int todo = min(len, 0x10000);
Simon Glassd1c13772015-09-01 19:19:37 -0600447
Simon Glass2c9693c2016-01-21 19:44:11 -0700448 rkspi_enable_chip(regs, false);
Simon Glassd1c13772015-09-01 19:19:37 -0600449 writel(todo - 1, &regs->ctrlr1);
450 rkspi_enable_chip(regs, true);
451
452 toread = todo;
453 towrite = todo;
454 while (toread || towrite) {
455 u32 status = readl(&regs->sr);
456
457 if (towrite && !(status & SR_TF_FULL)) {
458 writel(out ? *out++ : 0, regs->txdr);
459 towrite--;
460 }
461 if (toread && !(status & SR_RF_EMPT)) {
462 u32 byte = readl(regs->rxdr);
463
464 if (in)
465 *in++ = byte;
466 toread--;
467 }
468 }
Philipp Tomsiche3cc1a22019-02-03 16:17:30 +0100469
470 /*
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).
474 */
475 if (out) {
476 ret = rkspi_wait_till_not_busy(regs);
477 if (ret)
478 break;
479 }
480
Simon Glassd1c13772015-09-01 19:19:37 -0600481 len -= todo;
482 }
483
484 /* Deassert CS after transfer */
485 if (flags & SPI_XFER_END)
Simon Glass58a52e82016-01-21 19:44:10 -0700486 spi_cs_deactivate(dev, slave_plat->cs);
Simon Glassd1c13772015-09-01 19:19:37 -0600487
488 rkspi_enable_chip(regs, false);
489
490 return ret;
491}
492
493static int rockchip_spi_set_speed(struct udevice *bus, uint speed)
494{
495 struct rockchip_spi_priv *priv = dev_get_priv(bus);
496
Philipp Tomsich45447882017-04-20 22:05:51 +0200497 /* Clamp to the maximum frequency specified in the DTS */
Simon Glassd1c13772015-09-01 19:19:37 -0600498 if (speed > priv->max_freq)
499 speed = priv->max_freq;
Philipp Tomsich45447882017-04-20 22:05:51 +0200500
Simon Glassd1c13772015-09-01 19:19:37 -0600501 priv->speed_hz = speed;
502
503 return 0;
504}
505
506static int rockchip_spi_set_mode(struct udevice *bus, uint mode)
507{
508 struct rockchip_spi_priv *priv = dev_get_priv(bus);
509
510 priv->mode = mode;
511
512 return 0;
513}
514
515static 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,
521 /*
522 * cs_info is not needed, since we require all chip selects to be
523 * in the device tree explicitly
524 */
525};
526
527static const struct udevice_id rockchip_spi_ids[] = {
528 { .compatible = "rockchip,rk3288-spi" },
Philipp Tomsichc720b892017-07-25 16:25:30 +0200529 { .compatible = "rockchip,rk3368-spi" },
Jakob Unterwurzachera83478f2017-04-20 22:05:54 +0200530 { .compatible = "rockchip,rk3399-spi" },
Simon Glassd1c13772015-09-01 19:19:37 -0600531 { }
532};
533
534U_BOOT_DRIVER(rockchip_spi) = {
Simon Glass32308d22016-11-13 14:22:02 -0700535#if CONFIG_IS_ENABLED(OF_PLATDATA)
536 .name = "rockchip_rk3288_spi",
537#else
Simon Glassd1c13772015-09-01 19:19:37 -0600538 .name = "rockchip_spi",
Simon Glass32308d22016-11-13 14:22:02 -0700539#endif
Simon Glassd1c13772015-09-01 19:19:37 -0600540 .id = UCLASS_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,
547};