412fa8ba768fa63b6ea7b9d24a29bfaf19941ddf
[oweals/u-boot.git] / drivers / spi / rk_spi.c
1 /*
2  * spi driver for rockchip
3  *
4  * (C) Copyright 2015 Google, Inc
5  *
6  * (C) Copyright 2008-2013 Rockchip Electronics
7  * Peter, Software Engineering, <superpeter.cai@gmail.com>.
8  *
9  * SPDX-License-Identifier:     GPL-2.0+
10  */
11
12 #include <common.h>
13 #include <clk.h>
14 #include <dm.h>
15 #include <errno.h>
16 #include <spi.h>
17 #include <asm/errno.h>
18 #include <asm/io.h>
19 #include <asm/arch/clock.h>
20 #include <asm/arch/periph.h>
21 #include <dm/pinctrl.h>
22 #include "rk_spi.h"
23
24 DECLARE_GLOBAL_DATA_PTR;
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         int periph_id;
31         struct udevice *pinctrl;
32         s32 frequency;          /* Default clock frequency, -1 for none */
33         fdt_addr_t base;
34         uint deactivate_delay_us;       /* Delay to wait after deactivate */
35         uint activate_delay_us;         /* Delay to wait after activate */
36 };
37
38 struct rockchip_spi_priv {
39         struct rockchip_spi *regs;
40         struct udevice *clk;
41         int clk_id;
42         unsigned int max_freq;
43         unsigned int mode;
44         ulong last_transaction_us;      /* Time of last transaction end */
45         u8 bits_per_word;               /* max 16 bits per word */
46         u8 n_bytes;
47         unsigned int speed_hz;
48         unsigned int last_speed_hz;
49         unsigned int tmode;
50         uint input_rate;
51 };
52
53 #define SPI_FIFO_DEPTH          32
54
55 static void rkspi_dump_regs(struct rockchip_spi *regs)
56 {
57         debug("ctrl0: \t\t0x%08x\n", readl(&regs->ctrlr0));
58         debug("ctrl1: \t\t0x%08x\n", readl(&regs->ctrlr1));
59         debug("ssienr: \t\t0x%08x\n", readl(&regs->enr));
60         debug("ser: \t\t0x%08x\n", readl(&regs->ser));
61         debug("baudr: \t\t0x%08x\n", readl(&regs->baudr));
62         debug("txftlr: \t\t0x%08x\n", readl(&regs->txftlr));
63         debug("rxftlr: \t\t0x%08x\n", readl(&regs->rxftlr));
64         debug("txflr: \t\t0x%08x\n", readl(&regs->txflr));
65         debug("rxflr: \t\t0x%08x\n", readl(&regs->rxflr));
66         debug("sr: \t\t0x%08x\n", readl(&regs->sr));
67         debug("imr: \t\t0x%08x\n", readl(&regs->imr));
68         debug("isr: \t\t0x%08x\n", readl(&regs->isr));
69         debug("dmacr: \t\t0x%08x\n", readl(&regs->dmacr));
70         debug("dmatdlr: \t0x%08x\n", readl(&regs->dmatdlr));
71         debug("dmardlr: \t0x%08x\n", readl(&regs->dmardlr));
72 }
73
74 static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable)
75 {
76         writel(enable ? 1 : 0, &regs->enr);
77 }
78
79 static void rkspi_set_clk(struct rockchip_spi_priv *priv, uint speed)
80 {
81         uint clk_div;
82
83         clk_div = clk_get_divisor(priv->input_rate, speed);
84         debug("spi speed %u, div %u\n", speed, clk_div);
85
86         writel(clk_div, &priv->regs->baudr);
87         priv->last_speed_hz = speed;
88 }
89
90 static int rkspi_wait_till_not_busy(struct rockchip_spi *regs)
91 {
92         unsigned long start;
93
94         start = get_timer(0);
95         while (readl(&regs->sr) & SR_BUSY) {
96                 if (get_timer(start) > ROCKCHIP_SPI_TIMEOUT_MS) {
97                         debug("RK SPI: Status keeps busy for 1000us after a read/write!\n");
98                         return -ETIMEDOUT;
99                 }
100         }
101
102         return 0;
103 }
104
105 static void spi_cs_activate(struct udevice *dev, uint cs)
106 {
107         struct udevice *bus = dev->parent;
108         struct rockchip_spi_platdata *plat = bus->platdata;
109         struct rockchip_spi_priv *priv = dev_get_priv(bus);
110         struct rockchip_spi *regs = priv->regs;
111
112         debug("activate cs%u\n", cs);
113         writel(1 << cs, &regs->ser);
114         if (plat->activate_delay_us)
115                 udelay(plat->activate_delay_us);
116 }
117
118 static void spi_cs_deactivate(struct udevice *dev, uint cs)
119 {
120         struct udevice *bus = dev->parent;
121         struct rockchip_spi_platdata *plat = bus->platdata;
122         struct rockchip_spi_priv *priv = dev_get_priv(bus);
123         struct rockchip_spi *regs = priv->regs;
124
125         debug("deactivate cs%u\n", cs);
126         writel(0, &regs->ser);
127
128         /* Remember time of this transaction so we can honour the bus delay */
129         if (plat->deactivate_delay_us)
130                 priv->last_transaction_us = timer_get_us();
131 }
132
133 static int rockchip_spi_ofdata_to_platdata(struct udevice *bus)
134 {
135         struct rockchip_spi_platdata *plat = bus->platdata;
136         struct rockchip_spi_priv *priv = dev_get_priv(bus);
137         const void *blob = gd->fdt_blob;
138         int node = bus->of_offset;
139         int ret;
140
141         plat->base = dev_get_addr(bus);
142         ret = uclass_get_device(UCLASS_PINCTRL, 0, &plat->pinctrl);
143         if (ret)
144                 return ret;
145         ret = pinctrl_get_periph_id(plat->pinctrl, bus);
146
147         if (ret < 0) {
148                 debug("%s: Could not get peripheral ID for %s: %d\n", __func__,
149                       bus->name, ret);
150                 return ret;
151         }
152         plat->periph_id = ret;
153         ret = clk_get_by_index(bus, 0, &priv->clk);
154         if (ret < 0) {
155                 debug("%s: Could not get clock for %s: %d\n", __func__,
156                       bus->name, ret);
157                 return ret;
158         }
159         priv->clk_id = ret;
160
161         plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
162                                          50000000);
163         plat->deactivate_delay_us = fdtdec_get_int(blob, node,
164                                         "spi-deactivate-delay", 0);
165         plat->activate_delay_us = fdtdec_get_int(blob, node,
166                                                  "spi-activate-delay", 0);
167         debug("%s: base=%x, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
168               __func__, (uint)plat->base, plat->periph_id, plat->frequency,
169               plat->deactivate_delay_us);
170
171         return 0;
172 }
173
174 static int rockchip_spi_probe(struct udevice *bus)
175 {
176         struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
177         struct rockchip_spi_priv *priv = dev_get_priv(bus);
178         int ret;
179
180         debug("%s: probe\n", __func__);
181         priv->regs = (struct rockchip_spi *)plat->base;
182
183         priv->last_transaction_us = timer_get_us();
184         priv->max_freq = plat->frequency;
185
186         /*
187          * Use 99 MHz as our clock since it divides nicely into 594 MHz which
188          * is the assumed speed for CLK_GENERAL.
189          */
190         ret = clk_set_periph_rate(priv->clk, priv->clk_id, 99000000);
191         if (ret < 0) {
192                 debug("%s: Failed to set clock: %d\n", __func__, ret);
193                 return ret;
194         }
195         priv->input_rate = ret;
196         debug("%s: rate = %u\n", __func__, priv->input_rate);
197         priv->bits_per_word = 8;
198         priv->tmode = TMOD_TR; /* Tx & Rx */
199
200         return 0;
201 }
202
203 static int rockchip_spi_claim_bus(struct udevice *dev)
204 {
205         struct udevice *bus = dev->parent;
206         struct rockchip_spi_priv *priv = dev_get_priv(bus);
207         struct rockchip_spi *regs = priv->regs;
208         u8 spi_dfs, spi_tf;
209         uint ctrlr0;
210 #if !CONFIG_IS_ENABLED(PINCTRL_FULL)
211         struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
212         struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
213         int ret;
214 #endif
215
216         /* Disable the SPI hardware */
217         rkspi_enable_chip(regs, 0);
218
219         switch (priv->bits_per_word) {
220         case 8:
221                 priv->n_bytes = 1;
222                 spi_dfs = DFS_8BIT;
223                 spi_tf = HALF_WORD_OFF;
224                 break;
225         case 16:
226                 priv->n_bytes = 2;
227                 spi_dfs = DFS_16BIT;
228                 spi_tf = HALF_WORD_ON;
229                 break;
230         default:
231                 debug("%s: unsupported bits: %dbits\n", __func__,
232                       priv->bits_per_word);
233                 return -EPROTONOSUPPORT;
234         }
235
236         if (priv->speed_hz != priv->last_speed_hz)
237                 rkspi_set_clk(priv, priv->speed_hz);
238
239         /* Operation Mode */
240         ctrlr0 = OMOD_MASTER << OMOD_SHIFT;
241
242         /* Data Frame Size */
243         ctrlr0 |= spi_dfs << DFS_SHIFT;
244
245         /* set SPI mode 0..3 */
246         if (priv->mode & SPI_CPOL)
247                 ctrlr0 |= SCOL_HIGH << SCOL_SHIFT;
248         if (priv->mode & SPI_CPHA)
249                 ctrlr0 |= SCPH_TOGSTA << SCPH_SHIFT;
250
251         /* Chip Select Mode */
252         ctrlr0 |= CSM_KEEP << CSM_SHIFT;
253
254         /* SSN to Sclk_out delay */
255         ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT;
256
257         /* Serial Endian Mode */
258         ctrlr0 |= SEM_LITTLE << SEM_SHIFT;
259
260         /* First Bit Mode */
261         ctrlr0 |= FBM_MSB << FBM_SHIFT;
262
263         /* Byte and Halfword Transform */
264         ctrlr0 |= spi_tf << HALF_WORD_TX_SHIFT;
265
266         /* Rxd Sample Delay */
267         ctrlr0 |= 0 << RXDSD_SHIFT;
268
269         /* Frame Format */
270         ctrlr0 |= FRF_SPI << FRF_SHIFT;
271
272         /* Tx and Rx mode */
273         ctrlr0 |= (priv->tmode & TMOD_MASK) << TMOD_SHIFT;
274
275         writel(ctrlr0, &regs->ctrlr0);
276 #if !CONFIG_IS_ENABLED(PINCTRL_FULL)
277         ret = pinctrl_request(plat->pinctrl, plat->periph_id, slave_plat->cs);
278         if (ret) {
279                 debug("%s: Cannot request pinctrl: %d\n", __func__, ret);
280                 return ret;
281         }
282 #endif
283
284         return 0;
285 }
286
287 static int rockchip_spi_release_bus(struct udevice *dev)
288 {
289         struct udevice *bus = dev->parent;
290         struct rockchip_spi_priv *priv = dev_get_priv(bus);
291
292         rkspi_enable_chip(priv->regs, false);
293
294         return 0;
295 }
296
297 static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen,
298                            const void *dout, void *din, unsigned long flags)
299 {
300         struct udevice *bus = dev->parent;
301         struct rockchip_spi_priv *priv = dev_get_priv(bus);
302         struct rockchip_spi *regs = priv->regs;
303         struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
304         int len = bitlen >> 3;
305         const u8 *out = dout;
306         u8 *in = din;
307         int toread, towrite;
308         int ret;
309
310         debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
311               len, flags);
312         if (DEBUG_RK_SPI)
313                 rkspi_dump_regs(regs);
314
315         /* Assert CS before transfer */
316         if (flags & SPI_XFER_BEGIN)
317                 spi_cs_activate(dev, slave_plat->cs);
318
319         while (len > 0) {
320                 int todo = min(len, 0xffff);
321
322                 rkspi_enable_chip(regs, false);
323                 writel(todo - 1, &regs->ctrlr1);
324                 rkspi_enable_chip(regs, true);
325
326                 toread = todo;
327                 towrite = todo;
328                 while (toread || towrite) {
329                         u32 status = readl(&regs->sr);
330
331                         if (towrite && !(status & SR_TF_FULL)) {
332                                 writel(out ? *out++ : 0, regs->txdr);
333                                 towrite--;
334                         }
335                         if (toread && !(status & SR_RF_EMPT)) {
336                                 u32 byte = readl(regs->rxdr);
337
338                                 if (in)
339                                         *in++ = byte;
340                                 toread--;
341                         }
342                 }
343                 ret = rkspi_wait_till_not_busy(regs);
344                 if (ret)
345                         break;
346                 len -= todo;
347         }
348
349         /* Deassert CS after transfer */
350         if (flags & SPI_XFER_END)
351                 spi_cs_deactivate(dev, slave_plat->cs);
352
353         rkspi_enable_chip(regs, false);
354
355         return ret;
356 }
357
358 static int rockchip_spi_set_speed(struct udevice *bus, uint speed)
359 {
360         struct rockchip_spi_priv *priv = dev_get_priv(bus);
361
362         if (speed > ROCKCHIP_SPI_MAX_RATE)
363                 return -EINVAL;
364         if (speed > priv->max_freq)
365                 speed = priv->max_freq;
366         priv->speed_hz = speed;
367
368         return 0;
369 }
370
371 static int rockchip_spi_set_mode(struct udevice *bus, uint mode)
372 {
373         struct rockchip_spi_priv *priv = dev_get_priv(bus);
374
375         priv->mode = mode;
376
377         return 0;
378 }
379
380 static const struct dm_spi_ops rockchip_spi_ops = {
381         .claim_bus      = rockchip_spi_claim_bus,
382         .release_bus    = rockchip_spi_release_bus,
383         .xfer           = rockchip_spi_xfer,
384         .set_speed      = rockchip_spi_set_speed,
385         .set_mode       = rockchip_spi_set_mode,
386         /*
387          * cs_info is not needed, since we require all chip selects to be
388          * in the device tree explicitly
389          */
390 };
391
392 static const struct udevice_id rockchip_spi_ids[] = {
393         { .compatible = "rockchip,rk3288-spi" },
394         { }
395 };
396
397 U_BOOT_DRIVER(rockchip_spi) = {
398         .name   = "rockchip_spi",
399         .id     = UCLASS_SPI,
400         .of_match = rockchip_spi_ids,
401         .ops    = &rockchip_spi_ops,
402         .ofdata_to_platdata = rockchip_spi_ofdata_to_platdata,
403         .platdata_auto_alloc_size = sizeof(struct rockchip_spi_platdata),
404         .priv_auto_alloc_size = sizeof(struct rockchip_spi_priv),
405         .probe  = rockchip_spi_probe,
406 };