blob: 412fa8ba768fa63b6ea7b9d24a29bfaf19941ddf [file] [log] [blame]
Simon Glassd1c13772015-09-01 19:19:37 -06001/*
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
24DECLARE_GLOBAL_DATA_PTR;
25
26/* Change to 1 to output registers at the start of each transaction */
27#define DEBUG_RK_SPI 0
28
29struct rockchip_spi_platdata {
Simon Glassa95049e2016-01-21 19:43:43 -070030 int periph_id;
Simon Glassd1c13772015-09-01 19:19:37 -060031 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 */
Simon Glass58a52e82016-01-21 19:44:10 -070035 uint activate_delay_us; /* Delay to wait after activate */
Simon Glassd1c13772015-09-01 19:19:37 -060036};
37
38struct rockchip_spi_priv {
39 struct rockchip_spi *regs;
Simon Glassa95049e2016-01-21 19:43:43 -070040 struct udevice *clk;
41 int clk_id;
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 */
45 u8 bits_per_word; /* max 16 bits per word */
46 u8 n_bytes;
47 unsigned int speed_hz;
Simon Glass30508e92016-01-21 19:44:03 -070048 unsigned int last_speed_hz;
Simon Glassd1c13772015-09-01 19:19:37 -060049 unsigned int tmode;
50 uint input_rate;
51};
52
53#define SPI_FIFO_DEPTH 32
54
55static 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
74static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable)
75{
76 writel(enable ? 1 : 0, &regs->enr);
77}
78
79static 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);
Simon Glass30508e92016-01-21 19:44:03 -070087 priv->last_speed_hz = speed;
Simon Glassd1c13772015-09-01 19:19:37 -060088}
89
90static 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
Simon Glass58a52e82016-01-21 19:44:10 -0700105static void spi_cs_activate(struct udevice *dev, uint cs)
Simon Glassd1c13772015-09-01 19:19:37 -0600106{
Simon Glass58a52e82016-01-21 19:44:10 -0700107 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
Simon Glassd1c13772015-09-01 19:19:37 -0600112 debug("activate cs%u\n", cs);
113 writel(1 << cs, &regs->ser);
Simon Glass58a52e82016-01-21 19:44:10 -0700114 if (plat->activate_delay_us)
115 udelay(plat->activate_delay_us);
Simon Glassd1c13772015-09-01 19:19:37 -0600116}
117
Simon Glass58a52e82016-01-21 19:44:10 -0700118static void spi_cs_deactivate(struct udevice *dev, uint cs)
Simon Glassd1c13772015-09-01 19:19:37 -0600119{
Simon Glass58a52e82016-01-21 19:44:10 -0700120 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
Simon Glassd1c13772015-09-01 19:19:37 -0600125 debug("deactivate cs%u\n", cs);
126 writel(0, &regs->ser);
Simon Glass58a52e82016-01-21 19:44:10 -0700127
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();
Simon Glassd1c13772015-09-01 19:19:37 -0600131}
132
133static int rockchip_spi_ofdata_to_platdata(struct udevice *bus)
134{
135 struct rockchip_spi_platdata *plat = bus->platdata;
Simon Glassa95049e2016-01-21 19:43:43 -0700136 struct rockchip_spi_priv *priv = dev_get_priv(bus);
Simon Glassd1c13772015-09-01 19:19:37 -0600137 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);
Simon Glassa95049e2016-01-21 19:43:43 -0700150 return ret;
Simon Glassd1c13772015-09-01 19:19:37 -0600151 }
152 plat->periph_id = ret;
Simon Glassa95049e2016-01-21 19:43:43 -0700153 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;
Simon Glassd1c13772015-09-01 19:19:37 -0600160
161 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
Simon Glassa95049e2016-01-21 19:43:43 -0700162 50000000);
Simon Glassd1c13772015-09-01 19:19:37 -0600163 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
164 "spi-deactivate-delay", 0);
Simon Glass58a52e82016-01-21 19:44:10 -0700165 plat->activate_delay_us = fdtdec_get_int(blob, node,
166 "spi-activate-delay", 0);
Simon Glassa95049e2016-01-21 19:43:43 -0700167 debug("%s: base=%x, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
168 __func__, (uint)plat->base, plat->periph_id, plat->frequency,
Simon Glassd1c13772015-09-01 19:19:37 -0600169 plat->deactivate_delay_us);
170
171 return 0;
172}
173
174static 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;
Simon Glassd1c13772015-09-01 19:19:37 -0600185
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 */
Simon Glassa95049e2016-01-21 19:43:43 -0700190 ret = clk_set_periph_rate(priv->clk, priv->clk_id, 99000000);
Simon Glassd1c13772015-09-01 19:19:37 -0600191 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
203static int rockchip_spi_claim_bus(struct udevice *dev)
204{
205 struct udevice *bus = dev->parent;
Simon Glassd1c13772015-09-01 19:19:37 -0600206 struct rockchip_spi_priv *priv = dev_get_priv(bus);
207 struct rockchip_spi *regs = priv->regs;
Simon Glassd1c13772015-09-01 19:19:37 -0600208 u8 spi_dfs, spi_tf;
209 uint ctrlr0;
Simon Glassf2e033f2016-01-21 19:43:44 -0700210#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);
Simon Glassd1c13772015-09-01 19:19:37 -0600213 int ret;
Simon Glassf2e033f2016-01-21 19:43:44 -0700214#endif
Simon Glassd1c13772015-09-01 19:19:37 -0600215
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
Simon Glass30508e92016-01-21 19:44:03 -0700236 if (priv->speed_hz != priv->last_speed_hz)
237 rkspi_set_clk(priv, priv->speed_hz);
Simon Glassd1c13772015-09-01 19:19:37 -0600238
239 /* Operation Mode */
240 ctrlr0 = OMOD_MASTER << OMOD_SHIFT;
241
242 /* Data Frame Size */
Simon Glassf9ed8182016-01-21 19:44:04 -0700243 ctrlr0 |= spi_dfs << DFS_SHIFT;
Simon Glassd1c13772015-09-01 19:19:37 -0600244
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 */
Simon Glassf9ed8182016-01-21 19:44:04 -0700264 ctrlr0 |= spi_tf << HALF_WORD_TX_SHIFT;
Simon Glassd1c13772015-09-01 19:19:37 -0600265
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);
Simon Glassf2e033f2016-01-21 19:43:44 -0700276#if !CONFIG_IS_ENABLED(PINCTRL_FULL)
Simon Glassa95049e2016-01-21 19:43:43 -0700277 ret = pinctrl_request(plat->pinctrl, plat->periph_id, slave_plat->cs);
Simon Glassd1c13772015-09-01 19:19:37 -0600278 if (ret) {
279 debug("%s: Cannot request pinctrl: %d\n", __func__, ret);
280 return ret;
281 }
Simon Glassf2e033f2016-01-21 19:43:44 -0700282#endif
Simon Glassd1c13772015-09-01 19:19:37 -0600283
284 return 0;
285}
286
287static int rockchip_spi_release_bus(struct udevice *dev)
288{
Simon Glass2c9693c2016-01-21 19:44:11 -0700289 struct udevice *bus = dev->parent;
290 struct rockchip_spi_priv *priv = dev_get_priv(bus);
291
292 rkspi_enable_chip(priv->regs, false);
293
Simon Glassd1c13772015-09-01 19:19:37 -0600294 return 0;
295}
296
297static 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)
Simon Glass58a52e82016-01-21 19:44:10 -0700317 spi_cs_activate(dev, slave_plat->cs);
Simon Glassd1c13772015-09-01 19:19:37 -0600318
319 while (len > 0) {
320 int todo = min(len, 0xffff);
321
Simon Glass2c9693c2016-01-21 19:44:11 -0700322 rkspi_enable_chip(regs, false);
Simon Glassd1c13772015-09-01 19:19:37 -0600323 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)
Simon Glass58a52e82016-01-21 19:44:10 -0700351 spi_cs_deactivate(dev, slave_plat->cs);
Simon Glassd1c13772015-09-01 19:19:37 -0600352
353 rkspi_enable_chip(regs, false);
354
355 return ret;
356}
357
358static 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
371static 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
380static 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
392static const struct udevice_id rockchip_spi_ids[] = {
393 { .compatible = "rockchip,rk3288-spi" },
394 { }
395};
396
397U_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};