2 * spi driver for rockchip
4 * (C) Copyright 2015 Google, Inc
6 * (C) Copyright 2008-2013 Rockchip Electronics
7 * Peter, Software Engineering, <superpeter.cai@gmail.com>.
9 * SPDX-License-Identifier: GPL-2.0+
17 #include <asm/errno.h>
19 #include <asm/arch/clock.h>
20 #include <asm/arch/periph.h>
21 #include <dm/pinctrl.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 /* Change to 1 to output registers at the start of each transaction */
27 #define DEBUG_RK_SPI 0
29 struct rockchip_spi_platdata {
31 struct udevice *pinctrl;
32 s32 frequency; /* Default clock frequency, -1 for none */
34 uint deactivate_delay_us; /* Delay to wait after deactivate */
37 struct rockchip_spi_priv {
38 struct rockchip_spi *regs;
41 unsigned int max_freq;
43 ulong last_transaction_us; /* Time of last transaction end */
44 u8 bits_per_word; /* max 16 bits per word */
46 unsigned int speed_hz;
47 unsigned int last_speed_hz;
52 #define SPI_FIFO_DEPTH 32
54 static void rkspi_dump_regs(struct rockchip_spi *regs)
56 debug("ctrl0: \t\t0x%08x\n", readl(®s->ctrlr0));
57 debug("ctrl1: \t\t0x%08x\n", readl(®s->ctrlr1));
58 debug("ssienr: \t\t0x%08x\n", readl(®s->enr));
59 debug("ser: \t\t0x%08x\n", readl(®s->ser));
60 debug("baudr: \t\t0x%08x\n", readl(®s->baudr));
61 debug("txftlr: \t\t0x%08x\n", readl(®s->txftlr));
62 debug("rxftlr: \t\t0x%08x\n", readl(®s->rxftlr));
63 debug("txflr: \t\t0x%08x\n", readl(®s->txflr));
64 debug("rxflr: \t\t0x%08x\n", readl(®s->rxflr));
65 debug("sr: \t\t0x%08x\n", readl(®s->sr));
66 debug("imr: \t\t0x%08x\n", readl(®s->imr));
67 debug("isr: \t\t0x%08x\n", readl(®s->isr));
68 debug("dmacr: \t\t0x%08x\n", readl(®s->dmacr));
69 debug("dmatdlr: \t0x%08x\n", readl(®s->dmatdlr));
70 debug("dmardlr: \t0x%08x\n", readl(®s->dmardlr));
73 static void rkspi_enable_chip(struct rockchip_spi *regs, bool enable)
75 writel(enable ? 1 : 0, ®s->enr);
78 static void rkspi_set_clk(struct rockchip_spi_priv *priv, uint speed)
82 clk_div = clk_get_divisor(priv->input_rate, speed);
83 debug("spi speed %u, div %u\n", speed, clk_div);
85 writel(clk_div, &priv->regs->baudr);
86 priv->last_speed_hz = speed;
89 static int rkspi_wait_till_not_busy(struct rockchip_spi *regs)
94 while (readl(®s->sr) & SR_BUSY) {
95 if (get_timer(start) > ROCKCHIP_SPI_TIMEOUT_MS) {
96 debug("RK SPI: Status keeps busy for 1000us after a read/write!\n");
104 static void spi_cs_activate(struct rockchip_spi *regs, uint cs)
106 debug("activate cs%u\n", cs);
107 writel(1 << cs, ®s->ser);
110 static void spi_cs_deactivate(struct rockchip_spi *regs, uint cs)
112 debug("deactivate cs%u\n", cs);
113 writel(0, ®s->ser);
116 static int rockchip_spi_ofdata_to_platdata(struct udevice *bus)
118 struct rockchip_spi_platdata *plat = bus->platdata;
119 struct rockchip_spi_priv *priv = dev_get_priv(bus);
120 const void *blob = gd->fdt_blob;
121 int node = bus->of_offset;
124 plat->base = dev_get_addr(bus);
125 ret = uclass_get_device(UCLASS_PINCTRL, 0, &plat->pinctrl);
128 ret = pinctrl_get_periph_id(plat->pinctrl, bus);
131 debug("%s: Could not get peripheral ID for %s: %d\n", __func__,
135 plat->periph_id = ret;
136 ret = clk_get_by_index(bus, 0, &priv->clk);
138 debug("%s: Could not get clock for %s: %d\n", __func__,
144 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
146 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
147 "spi-deactivate-delay", 0);
148 debug("%s: base=%x, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
149 __func__, (uint)plat->base, plat->periph_id, plat->frequency,
150 plat->deactivate_delay_us);
155 static int rockchip_spi_probe(struct udevice *bus)
157 struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
158 struct rockchip_spi_priv *priv = dev_get_priv(bus);
161 debug("%s: probe\n", __func__);
162 priv->regs = (struct rockchip_spi *)plat->base;
164 priv->last_transaction_us = timer_get_us();
165 priv->max_freq = plat->frequency;
168 * Use 99 MHz as our clock since it divides nicely into 594 MHz which
169 * is the assumed speed for CLK_GENERAL.
171 ret = clk_set_periph_rate(priv->clk, priv->clk_id, 99000000);
173 debug("%s: Failed to set clock: %d\n", __func__, ret);
176 priv->input_rate = ret;
177 debug("%s: rate = %u\n", __func__, priv->input_rate);
178 priv->bits_per_word = 8;
179 priv->tmode = TMOD_TR; /* Tx & Rx */
184 static int rockchip_spi_claim_bus(struct udevice *dev)
186 struct udevice *bus = dev->parent;
187 struct rockchip_spi_priv *priv = dev_get_priv(bus);
188 struct rockchip_spi *regs = priv->regs;
191 #if !CONFIG_IS_ENABLED(PINCTRL_FULL)
192 struct rockchip_spi_platdata *plat = dev_get_platdata(bus);
193 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
197 /* Disable the SPI hardware */
198 rkspi_enable_chip(regs, 0);
200 switch (priv->bits_per_word) {
204 spi_tf = HALF_WORD_OFF;
209 spi_tf = HALF_WORD_ON;
212 debug("%s: unsupported bits: %dbits\n", __func__,
213 priv->bits_per_word);
214 return -EPROTONOSUPPORT;
217 if (priv->speed_hz != priv->last_speed_hz)
218 rkspi_set_clk(priv, priv->speed_hz);
221 ctrlr0 = OMOD_MASTER << OMOD_SHIFT;
223 /* Data Frame Size */
224 ctrlr0 |= spi_dfs << DFS_SHIFT;
226 /* set SPI mode 0..3 */
227 if (priv->mode & SPI_CPOL)
228 ctrlr0 |= SCOL_HIGH << SCOL_SHIFT;
229 if (priv->mode & SPI_CPHA)
230 ctrlr0 |= SCPH_TOGSTA << SCPH_SHIFT;
232 /* Chip Select Mode */
233 ctrlr0 |= CSM_KEEP << CSM_SHIFT;
235 /* SSN to Sclk_out delay */
236 ctrlr0 |= SSN_DELAY_ONE << SSN_DELAY_SHIFT;
238 /* Serial Endian Mode */
239 ctrlr0 |= SEM_LITTLE << SEM_SHIFT;
242 ctrlr0 |= FBM_MSB << FBM_SHIFT;
244 /* Byte and Halfword Transform */
245 ctrlr0 |= spi_tf << HALF_WORD_TX_SHIFT;
247 /* Rxd Sample Delay */
248 ctrlr0 |= 0 << RXDSD_SHIFT;
251 ctrlr0 |= FRF_SPI << FRF_SHIFT;
254 ctrlr0 |= (priv->tmode & TMOD_MASK) << TMOD_SHIFT;
256 writel(ctrlr0, ®s->ctrlr0);
257 #if !CONFIG_IS_ENABLED(PINCTRL_FULL)
258 ret = pinctrl_request(plat->pinctrl, plat->periph_id, slave_plat->cs);
260 debug("%s: Cannot request pinctrl: %d\n", __func__, ret);
268 static int rockchip_spi_release_bus(struct udevice *dev)
273 static int rockchip_spi_xfer(struct udevice *dev, unsigned int bitlen,
274 const void *dout, void *din, unsigned long flags)
276 struct udevice *bus = dev->parent;
277 struct rockchip_spi_priv *priv = dev_get_priv(bus);
278 struct rockchip_spi *regs = priv->regs;
279 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
280 int len = bitlen >> 3;
281 const u8 *out = dout;
286 debug("%s: dout=%p, din=%p, len=%x, flags=%lx\n", __func__, dout, din,
289 rkspi_dump_regs(regs);
291 /* Assert CS before transfer */
292 if (flags & SPI_XFER_BEGIN)
293 spi_cs_activate(regs, slave_plat->cs);
296 int todo = min(len, 0xffff);
298 rkspi_enable_chip(regs, true);
299 writel(todo - 1, ®s->ctrlr1);
300 rkspi_enable_chip(regs, true);
304 while (toread || towrite) {
305 u32 status = readl(®s->sr);
307 if (towrite && !(status & SR_TF_FULL)) {
308 writel(out ? *out++ : 0, regs->txdr);
311 if (toread && !(status & SR_RF_EMPT)) {
312 u32 byte = readl(regs->rxdr);
319 ret = rkspi_wait_till_not_busy(regs);
325 /* Deassert CS after transfer */
326 if (flags & SPI_XFER_END)
327 spi_cs_deactivate(regs, slave_plat->cs);
329 rkspi_enable_chip(regs, false);
334 static int rockchip_spi_set_speed(struct udevice *bus, uint speed)
336 struct rockchip_spi_priv *priv = dev_get_priv(bus);
338 if (speed > ROCKCHIP_SPI_MAX_RATE)
340 if (speed > priv->max_freq)
341 speed = priv->max_freq;
342 priv->speed_hz = speed;
347 static int rockchip_spi_set_mode(struct udevice *bus, uint mode)
349 struct rockchip_spi_priv *priv = dev_get_priv(bus);
356 static const struct dm_spi_ops rockchip_spi_ops = {
357 .claim_bus = rockchip_spi_claim_bus,
358 .release_bus = rockchip_spi_release_bus,
359 .xfer = rockchip_spi_xfer,
360 .set_speed = rockchip_spi_set_speed,
361 .set_mode = rockchip_spi_set_mode,
363 * cs_info is not needed, since we require all chip selects to be
364 * in the device tree explicitly
368 static const struct udevice_id rockchip_spi_ids[] = {
369 { .compatible = "rockchip,rk3288-spi" },
373 U_BOOT_DRIVER(rockchip_spi) = {
374 .name = "rockchip_spi",
376 .of_match = rockchip_spi_ids,
377 .ops = &rockchip_spi_ops,
378 .ofdata_to_platdata = rockchip_spi_ofdata_to_platdata,
379 .platdata_auto_alloc_size = sizeof(struct rockchip_spi_platdata),
380 .priv_auto_alloc_size = sizeof(struct rockchip_spi_priv),
381 .probe = rockchip_spi_probe,