blob: 8d64249385380ac7e3edf1ba2b40b28957d9ff5f [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>
Simon Glass32308d22016-11-13 14:22:02 -070015#include <dt-structs.h>
Simon Glassd1c13772015-09-01 19:19:37 -060016#include <errno.h>
17#include <spi.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090018#include <linux/errno.h>
Simon Glassd1c13772015-09-01 19:19:37 -060019#include <asm/io.h>
20#include <asm/arch/clock.h>
21#include <asm/arch/periph.h>
22#include <dm/pinctrl.h>
23#include "rk_spi.h"
24
25DECLARE_GLOBAL_DATA_PTR;
26
27/* Change to 1 to output registers at the start of each transaction */
28#define DEBUG_RK_SPI 0
29
30struct rockchip_spi_platdata {
Simon Glass32308d22016-11-13 14:22:02 -070031#if CONFIG_IS_ENABLED(OF_PLATDATA)
32 struct dtd_rockchip_rk3288_spi of_plat;
33#endif
Simon Glassd1c13772015-09-01 19:19:37 -060034 s32 frequency; /* Default clock frequency, -1 for none */
35 fdt_addr_t base;
36 uint deactivate_delay_us; /* Delay to wait after deactivate */
Simon Glass58a52e82016-01-21 19:44:10 -070037 uint activate_delay_us; /* Delay to wait after activate */
Simon Glassd1c13772015-09-01 19:19:37 -060038};
39
40struct rockchip_spi_priv {
41 struct rockchip_spi *regs;
Stephen Warrena9622432016-06-17 09:44:00 -060042 struct clk clk;
Simon Glassd1c13772015-09-01 19:19:37 -060043 unsigned int max_freq;
44 unsigned int mode;
Simon Glassd1c13772015-09-01 19:19:37 -060045 ulong last_transaction_us; /* Time of last transaction end */
46 u8 bits_per_word; /* max 16 bits per word */
47 u8 n_bytes;
48 unsigned int speed_hz;
Simon Glass30508e92016-01-21 19:44:03 -070049 unsigned int last_speed_hz;
Simon Glassd1c13772015-09-01 19:19:37 -060050 unsigned int tmode;
51 uint input_rate;
52};
53
54#define SPI_FIFO_DEPTH 32
55
56static void rkspi_dump_regs(struct rockchip_spi *regs)
57{
58 debug("ctrl0: \t\t0x%08x\n", readl(&regs->ctrlr0));
59 debug("ctrl1: \t\t0x%08x\n", readl(&regs->ctrlr1));
60 debug("ssienr: \t\t0x%08x\n", readl(&regs->enr));
61 debug("ser: \t\t0x%08x\n", readl(&regs->ser));
62 debug("baudr: \t\t0x%08x\n", readl(&regs->baudr));
63 debug("txftlr: \t\t0x%08x\n", readl(&regs->txftlr));
64 debug("rxftlr: \t\t0x%08x\n", readl(&regs->rxftlr));
65 debug("txflr: \t\t0x%08x\n", readl(&regs->txflr));
66 debug("rxflr: \t\t0x%08x\n", readl(&regs->rxflr));
67 debug("sr: \t\t0x%08x\n", readl(&regs->sr));
68 debug("imr: \t\t0x%08x\n", readl(&regs->imr));
69 debug("isr: \t\t0x%08x\n", readl(&regs->isr));
70 debug("dmacr: \t\t0x%08x\n", readl(&regs->dmacr));
71 debug("dmatdlr: \t0x%08x\n", readl(&regs->dmatdlr));
72 debug("dmardlr: \t0x%08x\n", readl(&regs->dmardlr));
73}
74
75static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable)
76{
77 writel(enable ? 1 : 0, &regs->enr);
78}
79
80static void rkspi_set_clk(struct rockchip_spi_priv *priv, uint speed)
81{
82 uint clk_div;
83
84 clk_div = clk_get_divisor(priv->input_rate, speed);
85 debug("spi speed %u, div %u\n", speed, clk_div);
86
87 writel(clk_div, &priv->regs->baudr);
Simon Glass30508e92016-01-21 19:44:03 -070088 priv->last_speed_hz = speed;
Simon Glassd1c13772015-09-01 19:19:37 -060089}
90
91static int rkspi_wait_till_not_busy(struct rockchip_spi *regs)
92{
93 unsigned long start;
94
95 start = get_timer(0);
96 while (readl(&regs->sr) & SR_BUSY) {
97 if (get_timer(start) > ROCKCHIP_SPI_TIMEOUT_MS) {
98 debug("RK SPI: Status keeps busy for 1000us after a read/write!\n");
99 return -ETIMEDOUT;
100 }
101 }
102
103 return 0;
104}
105
Simon Glass58a52e82016-01-21 19:44:10 -0700106static void spi_cs_activate(struct udevice *dev, uint cs)
Simon Glassd1c13772015-09-01 19:19:37 -0600107{
Simon Glass58a52e82016-01-21 19:44:10 -0700108 struct udevice *bus = dev->parent;
109 struct rockchip_spi_platdata *plat = bus->platdata;
110 struct rockchip_spi_priv *priv = dev_get_priv(bus);
111 struct rockchip_spi *regs = priv->regs;
112
Simon Glassd1c13772015-09-01 19:19:37 -0600113 debug("activate cs%u\n", cs);
114 writel(1 << cs, &regs->ser);
Simon Glass58a52e82016-01-21 19:44:10 -0700115 if (plat->activate_delay_us)
116 udelay(plat->activate_delay_us);
Simon Glassd1c13772015-09-01 19:19:37 -0600117}
118
Simon Glass58a52e82016-01-21 19:44:10 -0700119static void spi_cs_deactivate(struct udevice *dev, uint cs)
Simon Glassd1c13772015-09-01 19:19:37 -0600120{
Simon Glass58a52e82016-01-21 19:44:10 -0700121 struct udevice *bus = dev->parent;
122 struct rockchip_spi_platdata *plat = bus->platdata;
123 struct rockchip_spi_priv *priv = dev_get_priv(bus);
124 struct rockchip_spi *regs = priv->regs;
125
Simon Glassd1c13772015-09-01 19:19:37 -0600126 debug("deactivate cs%u\n", cs);
127 writel(0, &regs->ser);
Simon Glass58a52e82016-01-21 19:44:10 -0700128
129 /* Remember time of this transaction so we can honour the bus delay */
130 if (plat->deactivate_delay_us)
131 priv->last_transaction_us = timer_get_us();
Simon Glassd1c13772015-09-01 19:19:37 -0600132}
133
Simon Glass32308d22016-11-13 14:22:02 -0700134#if CONFIG_IS_ENABLED(OF_PLATDATA)
135static int conv_of_platdata(struct udevice *dev)
136{
137 struct rockchip_spi_platdata *plat = dev->platdata;
138 struct dtd_rockchip_rk3288_spi *dtplat = &plat->of_plat;
139 struct rockchip_spi_priv *priv = dev_get_priv(dev);
140 int ret;
141
142 plat->base = dtplat->reg[0];
143 plat->frequency = 20000000;
144 ret = clk_get_by_index_platdata(dev, 0, dtplat->clocks, &priv->clk);
145 if (ret < 0)
146 return ret;
147 dev->req_seq = 0;
148
149 return 0;
150}
151#endif
152
Simon Glassd1c13772015-09-01 19:19:37 -0600153static int rockchip_spi_ofdata_to_platdata(struct udevice *bus)
154{
Simon Glass32308d22016-11-13 14:22:02 -0700155#if !CONFIG_IS_ENABLED(OF_PLATDATA)
156 struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
Simon Glassa95049e2016-01-21 19:43:43 -0700157 struct rockchip_spi_priv *priv = dev_get_priv(bus);
Simon Glassd1c13772015-09-01 19:19:37 -0600158 const void *blob = gd->fdt_blob;
159 int node = bus->of_offset;
160 int ret;
161
162 plat->base = dev_get_addr(bus);
Simon Glassd1c13772015-09-01 19:19:37 -0600163
Simon Glassa95049e2016-01-21 19:43:43 -0700164 ret = clk_get_by_index(bus, 0, &priv->clk);
165 if (ret < 0) {
166 debug("%s: Could not get clock for %s: %d\n", __func__,
167 bus->name, ret);
168 return ret;
169 }
Simon Glassd1c13772015-09-01 19:19:37 -0600170
171 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
Simon Glassa95049e2016-01-21 19:43:43 -0700172 50000000);
Simon Glassd1c13772015-09-01 19:19:37 -0600173 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
174 "spi-deactivate-delay", 0);
Simon Glass58a52e82016-01-21 19:44:10 -0700175 plat->activate_delay_us = fdtdec_get_int(blob, node,
176 "spi-activate-delay", 0);
Simon Glassc45aec92016-01-21 19:44:12 -0700177 debug("%s: base=%x, max-frequency=%d, deactivate_delay=%d\n",
178 __func__, (uint)plat->base, plat->frequency,
Simon Glassd1c13772015-09-01 19:19:37 -0600179 plat->deactivate_delay_us);
Simon Glass32308d22016-11-13 14:22:02 -0700180#endif
Simon Glassd1c13772015-09-01 19:19:37 -0600181
182 return 0;
183}
184
185static int rockchip_spi_probe(struct udevice *bus)
186{
187 struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
188 struct rockchip_spi_priv *priv = dev_get_priv(bus);
189 int ret;
190
191 debug("%s: probe\n", __func__);
Simon Glass32308d22016-11-13 14:22:02 -0700192#if CONFIG_IS_ENABLED(OF_PLATDATA)
193 ret = conv_of_platdata(bus);
194 if (ret)
195 return ret;
196#endif
Simon Glassd1c13772015-09-01 19:19:37 -0600197 priv->regs = (struct rockchip_spi *)plat->base;
198
199 priv->last_transaction_us = timer_get_us();
200 priv->max_freq = plat->frequency;
Simon Glassd1c13772015-09-01 19:19:37 -0600201
202 /*
203 * Use 99 MHz as our clock since it divides nicely into 594 MHz which
204 * is the assumed speed for CLK_GENERAL.
205 */
Stephen Warrena9622432016-06-17 09:44:00 -0600206 ret = clk_set_rate(&priv->clk, 99000000);
Simon Glassd1c13772015-09-01 19:19:37 -0600207 if (ret < 0) {
208 debug("%s: Failed to set clock: %d\n", __func__, ret);
209 return ret;
210 }
211 priv->input_rate = ret;
212 debug("%s: rate = %u\n", __func__, priv->input_rate);
213 priv->bits_per_word = 8;
214 priv->tmode = TMOD_TR; /* Tx & Rx */
215
216 return 0;
217}
218
219static int rockchip_spi_claim_bus(struct udevice *dev)
220{
221 struct udevice *bus = dev->parent;
Simon Glassd1c13772015-09-01 19:19:37 -0600222 struct rockchip_spi_priv *priv = dev_get_priv(bus);
223 struct rockchip_spi *regs = priv->regs;
Simon Glassd1c13772015-09-01 19:19:37 -0600224 u8 spi_dfs, spi_tf;
225 uint ctrlr0;
Simon Glassd1c13772015-09-01 19:19:37 -0600226
227 /* Disable the SPI hardware */
228 rkspi_enable_chip(regs, 0);
229
230 switch (priv->bits_per_word) {
231 case 8:
232 priv->n_bytes = 1;
233 spi_dfs = DFS_8BIT;
234 spi_tf = HALF_WORD_OFF;
235 break;
236 case 16:
237 priv->n_bytes = 2;
238 spi_dfs = DFS_16BIT;
239 spi_tf = HALF_WORD_ON;
240 break;
241 default:
242 debug("%s: unsupported bits: %dbits\n", __func__,
243 priv->bits_per_word);
244 return -EPROTONOSUPPORT;
245 }
246
Simon Glass30508e92016-01-21 19:44:03 -0700247 if (priv->speed_hz != priv->last_speed_hz)
248 rkspi_set_clk(priv, priv->speed_hz);
Simon Glassd1c13772015-09-01 19:19:37 -0600249
250 /* Operation Mode */
251 ctrlr0 = OMOD_MASTER << OMOD_SHIFT;
252
253 /* Data Frame Size */
Simon Glassf9ed8182016-01-21 19:44:04 -0700254 ctrlr0 |= spi_dfs << DFS_SHIFT;
Simon Glassd1c13772015-09-01 19:19:37 -0600255
256 /* set SPI mode 0..3 */
257 if (priv->mode & SPI_CPOL)
258 ctrlr0 |= SCOL_HIGH << SCOL_SHIFT;
259 if (priv->mode & SPI_CPHA)
260 ctrlr0 |= SCPH_TOGSTA << SCPH_SHIFT;
261
262 /* Chip Select Mode */
263 ctrlr0 |= CSM_KEEP << CSM_SHIFT;
264
265 /* SSN to Sclk_out delay */
266 ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT;
267
268 /* Serial Endian Mode */
269 ctrlr0 |= SEM_LITTLE << SEM_SHIFT;
270
271 /* First Bit Mode */
272 ctrlr0 |= FBM_MSB << FBM_SHIFT;
273
274 /* Byte and Halfword Transform */
Simon Glassf9ed8182016-01-21 19:44:04 -0700275 ctrlr0 |= spi_tf << HALF_WORD_TX_SHIFT;
Simon Glassd1c13772015-09-01 19:19:37 -0600276
277 /* Rxd Sample Delay */
278 ctrlr0 |= 0 << RXDSD_SHIFT;
279
280 /* Frame Format */
281 ctrlr0 |= FRF_SPI << FRF_SHIFT;
282
283 /* Tx and Rx mode */
284 ctrlr0 |= (priv->tmode & TMOD_MASK) << TMOD_SHIFT;
285
286 writel(ctrlr0, &regs->ctrlr0);
Simon Glassd1c13772015-09-01 19:19:37 -0600287
288 return 0;
289}
290
291static int rockchip_spi_release_bus(struct udevice *dev)
292{
Simon Glass2c9693c2016-01-21 19:44:11 -0700293 struct udevice *bus = dev->parent;
294 struct rockchip_spi_priv *priv = dev_get_priv(bus);
295
296 rkspi_enable_chip(priv->regs, false);
297
Simon Glassd1c13772015-09-01 19:19:37 -0600298 return 0;
299}
300
301static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen,
302 const void *dout, void *din, unsigned long flags)
303{
304 struct udevice *bus = dev->parent;
305 struct rockchip_spi_priv *priv = dev_get_priv(bus);
306 struct rockchip_spi *regs = priv->regs;
307 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
308 int len = bitlen >> 3;
309 const u8 *out = dout;
310 u8 *in = din;
311 int toread, towrite;
312 int ret;
313
314 debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
315 len, flags);
316 if (DEBUG_RK_SPI)
317 rkspi_dump_regs(regs);
318
319 /* Assert CS before transfer */
320 if (flags & SPI_XFER_BEGIN)
Simon Glass58a52e82016-01-21 19:44:10 -0700321 spi_cs_activate(dev, slave_plat->cs);
Simon Glassd1c13772015-09-01 19:19:37 -0600322
323 while (len > 0) {
324 int todo = min(len, 0xffff);
325
Simon Glass2c9693c2016-01-21 19:44:11 -0700326 rkspi_enable_chip(regs, false);
Simon Glassd1c13772015-09-01 19:19:37 -0600327 writel(todo - 1, &regs->ctrlr1);
328 rkspi_enable_chip(regs, true);
329
330 toread = todo;
331 towrite = todo;
332 while (toread || towrite) {
333 u32 status = readl(&regs->sr);
334
335 if (towrite && !(status & SR_TF_FULL)) {
336 writel(out ? *out++ : 0, regs->txdr);
337 towrite--;
338 }
339 if (toread && !(status & SR_RF_EMPT)) {
340 u32 byte = readl(regs->rxdr);
341
342 if (in)
343 *in++ = byte;
344 toread--;
345 }
346 }
347 ret = rkspi_wait_till_not_busy(regs);
348 if (ret)
349 break;
350 len -= todo;
351 }
352
353 /* Deassert CS after transfer */
354 if (flags & SPI_XFER_END)
Simon Glass58a52e82016-01-21 19:44:10 -0700355 spi_cs_deactivate(dev, slave_plat->cs);
Simon Glassd1c13772015-09-01 19:19:37 -0600356
357 rkspi_enable_chip(regs, false);
358
359 return ret;
360}
361
362static int rockchip_spi_set_speed(struct udevice *bus, uint speed)
363{
364 struct rockchip_spi_priv *priv = dev_get_priv(bus);
365
366 if (speed > ROCKCHIP_SPI_MAX_RATE)
367 return -EINVAL;
368 if (speed > priv->max_freq)
369 speed = priv->max_freq;
370 priv->speed_hz = speed;
371
372 return 0;
373}
374
375static int rockchip_spi_set_mode(struct udevice *bus, uint mode)
376{
377 struct rockchip_spi_priv *priv = dev_get_priv(bus);
378
379 priv->mode = mode;
380
381 return 0;
382}
383
384static const struct dm_spi_ops rockchip_spi_ops = {
385 .claim_bus = rockchip_spi_claim_bus,
386 .release_bus = rockchip_spi_release_bus,
387 .xfer = rockchip_spi_xfer,
388 .set_speed = rockchip_spi_set_speed,
389 .set_mode = rockchip_spi_set_mode,
390 /*
391 * cs_info is not needed, since we require all chip selects to be
392 * in the device tree explicitly
393 */
394};
395
396static const struct udevice_id rockchip_spi_ids[] = {
397 { .compatible = "rockchip,rk3288-spi" },
398 { }
399};
400
401U_BOOT_DRIVER(rockchip_spi) = {
Simon Glass32308d22016-11-13 14:22:02 -0700402#if CONFIG_IS_ENABLED(OF_PLATDATA)
403 .name = "rockchip_rk3288_spi",
404#else
Simon Glassd1c13772015-09-01 19:19:37 -0600405 .name = "rockchip_spi",
Simon Glass32308d22016-11-13 14:22:02 -0700406#endif
Simon Glassd1c13772015-09-01 19:19:37 -0600407 .id = UCLASS_SPI,
408 .of_match = rockchip_spi_ids,
409 .ops = &rockchip_spi_ops,
410 .ofdata_to_platdata = rockchip_spi_ofdata_to_platdata,
411 .platdata_auto_alloc_size = sizeof(struct rockchip_spi_platdata),
412 .priv_auto_alloc_size = sizeof(struct rockchip_spi_priv),
413 .probe = rockchip_spi_probe,
414};