rockchip: spi: add optimised receive-only implementation
[platform/kernel/u-boot.git] / drivers / spi / rk_spi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * spi driver for rockchip
4  *
5  * (C) 2019 Theobroma Systems Design und Consulting GmbH
6  *
7  * (C) Copyright 2015 Google, Inc
8  *
9  * (C) Copyright 2008-2013 Rockchip Electronics
10  * Peter, Software Engineering, <superpeter.cai@gmail.com>.
11  */
12
13 #include <common.h>
14 #include <clk.h>
15 #include <dm.h>
16 #include <dt-structs.h>
17 #include <errno.h>
18 #include <spi.h>
19 #include <linux/errno.h>
20 #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
26 /* Change to 1 to output registers at the start of each transaction */
27 #define DEBUG_RK_SPI    0
28
29 struct rockchip_spi_platdata {
30 #if CONFIG_IS_ENABLED(OF_PLATDATA)
31         struct dtd_rockchip_rk3288_spi of_plat;
32 #endif
33         s32 frequency;          /* Default clock frequency, -1 for none */
34         fdt_addr_t base;
35         uint deactivate_delay_us;       /* Delay to wait after deactivate */
36         uint activate_delay_us;         /* Delay to wait after activate */
37 };
38
39 struct rockchip_spi_priv {
40         struct rockchip_spi *regs;
41         struct clk clk;
42         unsigned int max_freq;
43         unsigned int mode;
44         ulong last_transaction_us;      /* Time of last transaction end */
45         unsigned int speed_hz;
46         unsigned int last_speed_hz;
47         uint input_rate;
48 };
49
50 #define SPI_FIFO_DEPTH          32
51
52 static 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
71 static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable)
72 {
73         writel(enable ? 1 : 0, &regs->enr);
74 }
75
76 static void rkspi_set_clk(struct rockchip_spi_priv *priv, uint speed)
77 {
78         /*
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);
83
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:
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;
93                 debug("%s: can't divide down to %d Hz (actual will be %d Hz)\n",
94                       __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
100         debug("spi speed %u, div %u\n", speed, clk_div);
101
102         clrsetbits_le32(&priv->regs->baudr, 0xffff, clk_div);
103         priv->last_speed_hz = speed;
104 }
105
106 static 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
121 static void spi_cs_activate(struct udevice *dev, uint cs)
122 {
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;
127
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);
138                 }
139         }
140
141         debug("activate cs%u\n", cs);
142         writel(1 << cs, &regs->ser);
143         if (plat->activate_delay_us)
144                 udelay(plat->activate_delay_us);
145 }
146
147 static void spi_cs_deactivate(struct udevice *dev, uint cs)
148 {
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;
153
154         debug("deactivate cs%u\n", cs);
155         writel(0, &regs->ser);
156
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();
160 }
161
162 #if CONFIG_IS_ENABLED(OF_PLATDATA)
163 static 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
181 static int rockchip_spi_ofdata_to_platdata(struct udevice *bus)
182 {
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);
186         int ret;
187
188         plat->base = dev_read_addr(bus);
189
190         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         }
196
197         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
204         debug("%s: base=%x, max-frequency=%d, deactivate_delay=%d\n",
205               __func__, (uint)plat->base, plat->frequency,
206               plat->deactivate_delay_us);
207 #endif
208
209         return 0;
210 }
211
212 static int rockchip_spi_calc_modclk(ulong max_freq)
213 {
214         /*
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
222         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
240 static 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__);
247 #if CONFIG_IS_ENABLED(OF_PLATDATA)
248         ret = conv_of_platdata(bus);
249         if (ret)
250                 return ret;
251 #endif
252         priv->regs = (struct rockchip_spi *)plat->base;
253
254         priv->last_transaction_us = timer_get_us();
255         priv->max_freq = plat->frequency;
256
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;
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));
264         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);
270
271         return 0;
272 }
273
274 static int rockchip_spi_claim_bus(struct udevice *dev)
275 {
276         struct udevice *bus = dev->parent;
277         struct rockchip_spi_priv *priv = dev_get_priv(bus);
278         struct rockchip_spi *regs = priv->regs;
279         uint ctrlr0;
280
281         /* Disable the SPI hardware */
282         rkspi_enable_chip(regs, false);
283
284         if (priv->speed_hz != priv->last_speed_hz)
285                 rkspi_set_clk(priv, priv->speed_hz);
286
287         /* Operation Mode */
288         ctrlr0 = OMOD_MASTER << OMOD_SHIFT;
289
290         /* Data Frame Size */
291         ctrlr0 |= DFS_8BIT << DFS_SHIFT;
292
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 */
312         ctrlr0 |= HALF_WORD_OFF << HALF_WORD_TX_SHIFT;
313
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 */
321         ctrlr0 |= TMOD_TR << TMOD_SHIFT;
322
323         writel(ctrlr0, &regs->ctrlr0);
324
325         return 0;
326 }
327
328 static int rockchip_spi_release_bus(struct udevice *dev)
329 {
330         struct udevice *bus = dev->parent;
331         struct rockchip_spi_priv *priv = dev_get_priv(bus);
332
333         rkspi_enable_chip(priv->regs, false);
334
335         return 0;
336 }
337
338 static 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);
410         return 0;
411 }
412
413 static 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;
424         int ret = 0;
425
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)
433                 spi_cs_activate(dev, slave_plat->cs);
434
435         /*
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 */
445         while (len > 0) {
446                 int todo = min(len, 0x10000);
447
448                 rkspi_enable_chip(regs, false);
449                 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                 }
469
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
481                 len -= todo;
482         }
483
484         /* Deassert CS after transfer */
485         if (flags & SPI_XFER_END)
486                 spi_cs_deactivate(dev, slave_plat->cs);
487
488         rkspi_enable_chip(regs, false);
489
490         return ret;
491 }
492
493 static int rockchip_spi_set_speed(struct udevice *bus, uint speed)
494 {
495         struct rockchip_spi_priv *priv = dev_get_priv(bus);
496
497         /* Clamp to the maximum frequency specified in the DTS */
498         if (speed > priv->max_freq)
499                 speed = priv->max_freq;
500
501         priv->speed_hz = speed;
502
503         return 0;
504 }
505
506 static 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
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,
521         /*
522          * cs_info is not needed, since we require all chip selects to be
523          * in the device tree explicitly
524          */
525 };
526
527 static const struct udevice_id rockchip_spi_ids[] = {
528         { .compatible = "rockchip,rk3288-spi" },
529         { .compatible = "rockchip,rk3368-spi" },
530         { .compatible = "rockchip,rk3399-spi" },
531         { }
532 };
533
534 U_BOOT_DRIVER(rockchip_spi) = {
535 #if CONFIG_IS_ENABLED(OF_PLATDATA)
536         .name   = "rockchip_rk3288_spi",
537 #else
538         .name   = "rockchip_spi",
539 #endif
540         .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 };