blob: 628294948b557d183fab8d44ec445bc43987efe3 [file] [log] [blame]
Stefan Mavrodiev5d716042018-02-06 15:14:33 +02001/*
2 * (C) Copyright 2017 Whitebox Systems / Northend Systems B.V.
3 * S.J.R. van Schaik <stephan@whiteboxsystems.nl>
4 * M.B.W. Wajer <merlijn@whiteboxsystems.nl>
5 *
6 * (C) Copyright 2017 Olimex Ltd..
7 * Stefan Mavrodiev <stefan@olimex.com>
8 *
9 * Based on linux spi driver. Original copyright follows:
10 * linux/drivers/spi/spi-sun4i.c
11 *
12 * Copyright (C) 2012 - 2014 Allwinner Tech
13 * Pan Nan <pannan@allwinnertech.com>
14 *
15 * Copyright (C) 2014 Maxime Ripard
16 * Maxime Ripard <maxime.ripard@free-electrons.com>
17 *
18 * SPDX-License-Identifier: GPL-2.0+
19 */
20
21#include <common.h>
Jagan Teki97b3d5a2019-02-27 20:02:10 +053022#include <clk.h>
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020023#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060024#include <log.h>
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020025#include <spi.h>
26#include <errno.h>
27#include <fdt_support.h>
Jagan Tekif69b4252019-02-27 20:02:11 +053028#include <reset.h>
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020029#include <wait_bit.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060030#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -070031#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060032#include <linux/bitops.h>
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020033
34#include <asm/bitops.h>
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020035#include <asm/io.h>
36
Jagan Teki66220da2019-02-27 20:02:05 +053037#include <linux/iopoll.h>
38
Jagan Teki3f53a582019-02-27 20:02:12 +053039DECLARE_GLOBAL_DATA_PTR;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020040
Jagan Teki3f53a582019-02-27 20:02:12 +053041/* sun4i spi registers */
42#define SUN4I_RXDATA_REG 0x00
43#define SUN4I_TXDATA_REG 0x04
44#define SUN4I_CTL_REG 0x08
45#define SUN4I_CLK_CTL_REG 0x1c
46#define SUN4I_BURST_CNT_REG 0x20
47#define SUN4I_XMIT_CNT_REG 0x24
48#define SUN4I_FIFO_STA_REG 0x28
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020049
Jagan Tekif69b4252019-02-27 20:02:11 +053050/* sun6i spi registers */
51#define SUN6I_GBL_CTL_REG 0x04
52#define SUN6I_TFR_CTL_REG 0x08
53#define SUN6I_FIFO_CTL_REG 0x18
54#define SUN6I_FIFO_STA_REG 0x1c
55#define SUN6I_CLK_CTL_REG 0x24
56#define SUN6I_BURST_CNT_REG 0x30
57#define SUN6I_XMIT_CNT_REG 0x34
58#define SUN6I_BURST_CTL_REG 0x38
59#define SUN6I_TXDATA_REG 0x200
60#define SUN6I_RXDATA_REG 0x300
61
Jagan Teki3f53a582019-02-27 20:02:12 +053062/* sun spi bits */
63#define SUN4I_CTL_ENABLE BIT(0)
64#define SUN4I_CTL_MASTER BIT(1)
65#define SUN4I_CLK_CTL_CDR2_MASK 0xff
66#define SUN4I_CLK_CTL_CDR2(div) ((div) & SUN4I_CLK_CTL_CDR2_MASK)
67#define SUN4I_CLK_CTL_CDR1_MASK 0xf
68#define SUN4I_CLK_CTL_CDR1(div) (((div) & SUN4I_CLK_CTL_CDR1_MASK) << 8)
69#define SUN4I_CLK_CTL_DRS BIT(12)
70#define SUN4I_MAX_XFER_SIZE 0xffffff
71#define SUN4I_BURST_CNT(cnt) ((cnt) & SUN4I_MAX_XFER_SIZE)
72#define SUN4I_XMIT_CNT(cnt) ((cnt) & SUN4I_MAX_XFER_SIZE)
73#define SUN4I_FIFO_STA_RF_CNT_BITS 0
74
Andre Przywara27835682022-05-03 02:06:37 +010075/* the SPI mod clock, defaulting to be 1/1 of the HOSC@24MHz */
76#define SUNXI_INPUT_CLOCK 24000000 /* 24 MHz */
77#define SUN4I_SPI_MAX_RATE SUNXI_INPUT_CLOCK
Jagan Teki3f53a582019-02-27 20:02:12 +053078#define SUN4I_SPI_MIN_RATE 3000
79#define SUN4I_SPI_DEFAULT_RATE 1000000
Icenowy Zhenga244be62022-06-28 14:49:24 +080080#define SUN4I_SPI_TIMEOUT_MS 1000
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020081
Jagan Teki3f53a582019-02-27 20:02:12 +053082#define SPI_REG(priv, reg) ((priv)->base + \
Jagan Tekic25058c2019-02-27 20:02:08 +053083 (priv)->variant->regs[reg])
84#define SPI_BIT(priv, bit) ((priv)->variant->bits[bit])
85#define SPI_CS(priv, cs) (((cs) << SPI_BIT(priv, SPI_TCR_CS_SEL)) & \
86 SPI_BIT(priv, SPI_TCR_CS_MASK))
87
88/* sun spi register set */
89enum sun4i_spi_regs {
90 SPI_GCR,
91 SPI_TCR,
92 SPI_FCR,
93 SPI_FSR,
94 SPI_CCR,
95 SPI_BC,
96 SPI_TC,
97 SPI_BCTL,
98 SPI_TXD,
99 SPI_RXD,
100};
101
102/* sun spi register bits */
103enum sun4i_spi_bits {
104 SPI_GCR_TP,
Jagan Tekif69b4252019-02-27 20:02:11 +0530105 SPI_GCR_SRST,
Jagan Tekic25058c2019-02-27 20:02:08 +0530106 SPI_TCR_CPHA,
107 SPI_TCR_CPOL,
108 SPI_TCR_CS_ACTIVE_LOW,
109 SPI_TCR_CS_SEL,
110 SPI_TCR_CS_MASK,
111 SPI_TCR_XCH,
112 SPI_TCR_CS_MANUAL,
113 SPI_TCR_CS_LEVEL,
114 SPI_FCR_TF_RST,
115 SPI_FCR_RF_RST,
116 SPI_FSR_RF_CNT_MASK,
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200117};
118
Jagan Tekic25058c2019-02-27 20:02:08 +0530119struct sun4i_spi_variant {
120 const unsigned long *regs;
121 const u32 *bits;
Jagan Tekic12eb6a2019-02-27 20:02:09 +0530122 u32 fifo_depth;
Jagan Tekif69b4252019-02-27 20:02:11 +0530123 bool has_soft_reset;
124 bool has_burst_ctl;
Jagan Tekic25058c2019-02-27 20:02:08 +0530125};
126
Simon Glassb75b15b2020-12-03 16:55:23 -0700127struct sun4i_spi_plat {
Jagan Tekic25058c2019-02-27 20:02:08 +0530128 struct sun4i_spi_variant *variant;
Jagan Teki3f53a582019-02-27 20:02:12 +0530129 u32 base;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200130 u32 max_hz;
131};
132
133struct sun4i_spi_priv {
Jagan Tekic25058c2019-02-27 20:02:08 +0530134 struct sun4i_spi_variant *variant;
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530135 struct clk clk_ahb, clk_mod;
Jagan Tekif69b4252019-02-27 20:02:11 +0530136 struct reset_ctl reset;
Jagan Teki3f53a582019-02-27 20:02:12 +0530137 u32 base;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200138 u32 freq;
139 u32 mode;
140
141 const u8 *tx_buf;
142 u8 *rx_buf;
143};
144
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200145static inline void sun4i_spi_drain_fifo(struct sun4i_spi_priv *priv, int len)
146{
147 u8 byte;
148
149 while (len--) {
Jagan Tekic25058c2019-02-27 20:02:08 +0530150 byte = readb(SPI_REG(priv, SPI_RXD));
Stefan Mavrodiev165db622018-12-05 14:27:57 +0200151 if (priv->rx_buf)
152 *priv->rx_buf++ = byte;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200153 }
154}
155
156static inline void sun4i_spi_fill_fifo(struct sun4i_spi_priv *priv, int len)
157{
158 u8 byte;
159
160 while (len--) {
161 byte = priv->tx_buf ? *priv->tx_buf++ : 0;
Jagan Tekic25058c2019-02-27 20:02:08 +0530162 writeb(byte, SPI_REG(priv, SPI_TXD));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200163 }
164}
165
166static void sun4i_spi_set_cs(struct udevice *bus, u8 cs, bool enable)
167{
168 struct sun4i_spi_priv *priv = dev_get_priv(bus);
169 u32 reg;
170
Jagan Tekic25058c2019-02-27 20:02:08 +0530171 reg = readl(SPI_REG(priv, SPI_TCR));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200172
Jagan Tekic25058c2019-02-27 20:02:08 +0530173 reg &= ~SPI_BIT(priv, SPI_TCR_CS_MASK);
174 reg |= SPI_CS(priv, cs);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200175
176 if (enable)
Jagan Tekic25058c2019-02-27 20:02:08 +0530177 reg &= ~SPI_BIT(priv, SPI_TCR_CS_LEVEL);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200178 else
Jagan Tekic25058c2019-02-27 20:02:08 +0530179 reg |= SPI_BIT(priv, SPI_TCR_CS_LEVEL);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200180
Jagan Tekic25058c2019-02-27 20:02:08 +0530181 writel(reg, SPI_REG(priv, SPI_TCR));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200182}
183
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530184static inline int sun4i_spi_set_clock(struct udevice *dev, bool enable)
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200185{
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530186 struct sun4i_spi_priv *priv = dev_get_priv(dev);
187 int ret;
188
189 if (!enable) {
190 clk_disable(&priv->clk_ahb);
191 clk_disable(&priv->clk_mod);
Jagan Tekif69b4252019-02-27 20:02:11 +0530192 if (reset_valid(&priv->reset))
193 reset_assert(&priv->reset);
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530194 return 0;
195 }
196
197 ret = clk_enable(&priv->clk_ahb);
198 if (ret) {
199 dev_err(dev, "failed to enable ahb clock (ret=%d)\n", ret);
200 return ret;
201 }
202
203 ret = clk_enable(&priv->clk_mod);
204 if (ret) {
205 dev_err(dev, "failed to enable mod clock (ret=%d)\n", ret);
206 goto err_ahb;
207 }
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200208
Jagan Tekif69b4252019-02-27 20:02:11 +0530209 if (reset_valid(&priv->reset)) {
210 ret = reset_deassert(&priv->reset);
211 if (ret) {
212 dev_err(dev, "failed to deassert reset\n");
213 goto err_mod;
214 }
215 }
216
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530217 return 0;
218
Jagan Tekif69b4252019-02-27 20:02:11 +0530219err_mod:
220 clk_disable(&priv->clk_mod);
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530221err_ahb:
222 clk_disable(&priv->clk_ahb);
223 return ret;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200224}
225
Andre Przywara62a24e12022-05-03 00:07:16 +0100226static void sun4i_spi_set_speed_mode(struct udevice *dev)
227{
228 struct sun4i_spi_priv *priv = dev_get_priv(dev);
229 unsigned int div;
230 u32 reg;
231
232 /*
233 * Setup clock divider.
234 *
235 * We have two choices there. Either we can use the clock
236 * divide rate 1, which is calculated thanks to this formula:
237 * SPI_CLK = MOD_CLK / (2 ^ (cdr + 1))
238 * Or we can use CDR2, which is calculated with the formula:
239 * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
240 * Whether we use the former or the latter is set through the
241 * DRS bit.
242 *
243 * First try CDR2, and if we can't reach the expected
244 * frequency, fall back to CDR1.
245 */
246
Andre Przywara27835682022-05-03 02:06:37 +0100247 div = DIV_ROUND_UP(SUNXI_INPUT_CLOCK, priv->freq);
Andre Przywara62a24e12022-05-03 00:07:16 +0100248 reg = readl(SPI_REG(priv, SPI_CCR));
249
Andre Przywara27835682022-05-03 02:06:37 +0100250 if ((div / 2) <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
251 div /= 2;
Andre Przywara62a24e12022-05-03 00:07:16 +0100252 if (div > 0)
253 div--;
254
255 reg &= ~(SUN4I_CLK_CTL_CDR2_MASK | SUN4I_CLK_CTL_DRS);
256 reg |= SUN4I_CLK_CTL_CDR2(div) | SUN4I_CLK_CTL_DRS;
257 } else {
Andre Przywara27835682022-05-03 02:06:37 +0100258 div = fls(div - 1);
Andre Przywara62a24e12022-05-03 00:07:16 +0100259 reg &= ~((SUN4I_CLK_CTL_CDR1_MASK << 8) | SUN4I_CLK_CTL_DRS);
260 reg |= SUN4I_CLK_CTL_CDR1(div);
261 }
262
263 writel(reg, SPI_REG(priv, SPI_CCR));
264
265 reg = readl(SPI_REG(priv, SPI_TCR));
266 reg &= ~(SPI_BIT(priv, SPI_TCR_CPOL) | SPI_BIT(priv, SPI_TCR_CPHA));
267
268 if (priv->mode & SPI_CPOL)
269 reg |= SPI_BIT(priv, SPI_TCR_CPOL);
270
271 if (priv->mode & SPI_CPHA)
272 reg |= SPI_BIT(priv, SPI_TCR_CPHA);
273
274 writel(reg, SPI_REG(priv, SPI_TCR));
275}
276
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200277static int sun4i_spi_claim_bus(struct udevice *dev)
278{
279 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530280 int ret;
281
282 ret = sun4i_spi_set_clock(dev->parent, true);
283 if (ret)
284 return ret;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200285
Jagan Tekic25058c2019-02-27 20:02:08 +0530286 setbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE |
287 SUN4I_CTL_MASTER | SPI_BIT(priv, SPI_GCR_TP));
288
Jagan Tekif69b4252019-02-27 20:02:11 +0530289 if (priv->variant->has_soft_reset)
290 setbits_le32(SPI_REG(priv, SPI_GCR),
291 SPI_BIT(priv, SPI_GCR_SRST));
292
Jagan Tekic25058c2019-02-27 20:02:08 +0530293 setbits_le32(SPI_REG(priv, SPI_TCR), SPI_BIT(priv, SPI_TCR_CS_MANUAL) |
294 SPI_BIT(priv, SPI_TCR_CS_ACTIVE_LOW));
Jagan Tekif9b70122019-02-27 20:02:07 +0530295
Andre Przywara62a24e12022-05-03 00:07:16 +0100296 sun4i_spi_set_speed_mode(dev->parent);
297
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200298 return 0;
299}
300
301static int sun4i_spi_release_bus(struct udevice *dev)
302{
303 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200304
Jagan Tekic25058c2019-02-27 20:02:08 +0530305 clrbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200306
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530307 sun4i_spi_set_clock(dev->parent, false);
308
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200309 return 0;
310}
311
312static int sun4i_spi_xfer(struct udevice *dev, unsigned int bitlen,
313 const void *dout, void *din, unsigned long flags)
314{
315 struct udevice *bus = dev->parent;
316 struct sun4i_spi_priv *priv = dev_get_priv(bus);
Simon Glassb75b15b2020-12-03 16:55:23 -0700317 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200318
319 u32 len = bitlen / 8;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200320 u8 nbytes;
321 int ret;
322
323 priv->tx_buf = dout;
324 priv->rx_buf = din;
325
326 if (bitlen % 8) {
327 debug("%s: non byte-aligned SPI transfer.\n", __func__);
328 return -ENAVAIL;
329 }
330
331 if (flags & SPI_XFER_BEGIN)
332 sun4i_spi_set_cs(bus, slave_plat->cs, true);
333
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200334 /* Reset FIFOs */
Jagan Tekic25058c2019-02-27 20:02:08 +0530335 setbits_le32(SPI_REG(priv, SPI_FCR), SPI_BIT(priv, SPI_FCR_RF_RST) |
336 SPI_BIT(priv, SPI_FCR_TF_RST));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200337
338 while (len) {
339 /* Setup the transfer now... */
Jagan Tekic12eb6a2019-02-27 20:02:09 +0530340 nbytes = min(len, (priv->variant->fifo_depth - 1));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200341
342 /* Setup the counters */
Jagan Tekic25058c2019-02-27 20:02:08 +0530343 writel(SUN4I_BURST_CNT(nbytes), SPI_REG(priv, SPI_BC));
344 writel(SUN4I_XMIT_CNT(nbytes), SPI_REG(priv, SPI_TC));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200345
Jagan Tekif69b4252019-02-27 20:02:11 +0530346 if (priv->variant->has_burst_ctl)
347 writel(SUN4I_BURST_CNT(nbytes),
348 SPI_REG(priv, SPI_BCTL));
349
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200350 /* Fill the TX FIFO */
351 sun4i_spi_fill_fifo(priv, nbytes);
352
353 /* Start the transfer */
Jagan Tekic25058c2019-02-27 20:02:08 +0530354 setbits_le32(SPI_REG(priv, SPI_TCR),
355 SPI_BIT(priv, SPI_TCR_XCH));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200356
Icenowy Zhenga244be62022-06-28 14:49:24 +0800357 /* Wait for the transfer to be done */
358 ret = wait_for_bit_le32((const void *)SPI_REG(priv, SPI_TCR),
359 SPI_BIT(priv, SPI_TCR_XCH),
360 false, SUN4I_SPI_TIMEOUT_MS, false);
Jagan Teki66220da2019-02-27 20:02:05 +0530361 if (ret < 0) {
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200362 printf("ERROR: sun4i_spi: Timeout transferring data\n");
363 sun4i_spi_set_cs(bus, slave_plat->cs, false);
364 return ret;
365 }
366
367 /* Drain the RX FIFO */
368 sun4i_spi_drain_fifo(priv, nbytes);
369
370 len -= nbytes;
371 }
372
373 if (flags & SPI_XFER_END)
374 sun4i_spi_set_cs(bus, slave_plat->cs, false);
375
376 return 0;
377}
378
379static int sun4i_spi_set_speed(struct udevice *dev, uint speed)
380{
Simon Glassb75b15b2020-12-03 16:55:23 -0700381 struct sun4i_spi_plat *plat = dev_get_plat(dev);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200382 struct sun4i_spi_priv *priv = dev_get_priv(dev);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200383
384 if (speed > plat->max_hz)
385 speed = plat->max_hz;
386
387 if (speed < SUN4I_SPI_MIN_RATE)
388 speed = SUN4I_SPI_MIN_RATE;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200389
390 priv->freq = speed;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200391
392 return 0;
393}
394
395static int sun4i_spi_set_mode(struct udevice *dev, uint mode)
396{
397 struct sun4i_spi_priv *priv = dev_get_priv(dev);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200398
399 priv->mode = mode;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200400
401 return 0;
402}
403
404static const struct dm_spi_ops sun4i_spi_ops = {
405 .claim_bus = sun4i_spi_claim_bus,
406 .release_bus = sun4i_spi_release_bus,
407 .xfer = sun4i_spi_xfer,
408 .set_speed = sun4i_spi_set_speed,
409 .set_mode = sun4i_spi_set_mode,
410};
411
Jagan Teki3f53a582019-02-27 20:02:12 +0530412static int sun4i_spi_probe(struct udevice *bus)
413{
Simon Glassb75b15b2020-12-03 16:55:23 -0700414 struct sun4i_spi_plat *plat = dev_get_plat(bus);
Jagan Teki3f53a582019-02-27 20:02:12 +0530415 struct sun4i_spi_priv *priv = dev_get_priv(bus);
416 int ret;
417
418 ret = clk_get_by_name(bus, "ahb", &priv->clk_ahb);
419 if (ret) {
Sean Anderson64474dd2020-09-15 10:45:11 -0400420 dev_err(bus, "failed to get ahb clock\n");
Jagan Teki3f53a582019-02-27 20:02:12 +0530421 return ret;
422 }
423
424 ret = clk_get_by_name(bus, "mod", &priv->clk_mod);
425 if (ret) {
Sean Anderson64474dd2020-09-15 10:45:11 -0400426 dev_err(bus, "failed to get mod clock\n");
Jagan Teki3f53a582019-02-27 20:02:12 +0530427 return ret;
428 }
429
430 ret = reset_get_by_index(bus, 0, &priv->reset);
431 if (ret && ret != -ENOENT) {
Sean Anderson64474dd2020-09-15 10:45:11 -0400432 dev_err(bus, "failed to get reset\n");
Jagan Teki3f53a582019-02-27 20:02:12 +0530433 return ret;
434 }
435
Jagan Teki3f53a582019-02-27 20:02:12 +0530436 priv->variant = plat->variant;
437 priv->base = plat->base;
438 priv->freq = plat->max_hz;
439
440 return 0;
441}
442
Simon Glassaad29ae2020-12-03 16:55:21 -0700443static int sun4i_spi_of_to_plat(struct udevice *bus)
Jagan Teki3f53a582019-02-27 20:02:12 +0530444{
Simon Glassb75b15b2020-12-03 16:55:23 -0700445 struct sun4i_spi_plat *plat = dev_get_plat(bus);
Jagan Teki3f53a582019-02-27 20:02:12 +0530446 int node = dev_of_offset(bus);
447
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900448 plat->base = dev_read_addr(bus);
Jagan Teki3f53a582019-02-27 20:02:12 +0530449 plat->variant = (struct sun4i_spi_variant *)dev_get_driver_data(bus);
450 plat->max_hz = fdtdec_get_int(gd->fdt_blob, node,
451 "spi-max-frequency",
452 SUN4I_SPI_DEFAULT_RATE);
453
454 if (plat->max_hz > SUN4I_SPI_MAX_RATE)
455 plat->max_hz = SUN4I_SPI_MAX_RATE;
456
457 return 0;
458}
459
Jagan Tekic25058c2019-02-27 20:02:08 +0530460static const unsigned long sun4i_spi_regs[] = {
461 [SPI_GCR] = SUN4I_CTL_REG,
462 [SPI_TCR] = SUN4I_CTL_REG,
463 [SPI_FCR] = SUN4I_CTL_REG,
464 [SPI_FSR] = SUN4I_FIFO_STA_REG,
465 [SPI_CCR] = SUN4I_CLK_CTL_REG,
466 [SPI_BC] = SUN4I_BURST_CNT_REG,
467 [SPI_TC] = SUN4I_XMIT_CNT_REG,
468 [SPI_TXD] = SUN4I_TXDATA_REG,
469 [SPI_RXD] = SUN4I_RXDATA_REG,
470};
471
472static const u32 sun4i_spi_bits[] = {
473 [SPI_GCR_TP] = BIT(18),
474 [SPI_TCR_CPHA] = BIT(2),
475 [SPI_TCR_CPOL] = BIT(3),
476 [SPI_TCR_CS_ACTIVE_LOW] = BIT(4),
477 [SPI_TCR_XCH] = BIT(10),
478 [SPI_TCR_CS_SEL] = 12,
479 [SPI_TCR_CS_MASK] = 0x3000,
480 [SPI_TCR_CS_MANUAL] = BIT(16),
481 [SPI_TCR_CS_LEVEL] = BIT(17),
482 [SPI_FCR_TF_RST] = BIT(8),
483 [SPI_FCR_RF_RST] = BIT(9),
484 [SPI_FSR_RF_CNT_MASK] = GENMASK(6, 0),
485};
486
Jagan Tekif69b4252019-02-27 20:02:11 +0530487static const unsigned long sun6i_spi_regs[] = {
488 [SPI_GCR] = SUN6I_GBL_CTL_REG,
489 [SPI_TCR] = SUN6I_TFR_CTL_REG,
490 [SPI_FCR] = SUN6I_FIFO_CTL_REG,
491 [SPI_FSR] = SUN6I_FIFO_STA_REG,
492 [SPI_CCR] = SUN6I_CLK_CTL_REG,
493 [SPI_BC] = SUN6I_BURST_CNT_REG,
494 [SPI_TC] = SUN6I_XMIT_CNT_REG,
495 [SPI_BCTL] = SUN6I_BURST_CTL_REG,
496 [SPI_TXD] = SUN6I_TXDATA_REG,
497 [SPI_RXD] = SUN6I_RXDATA_REG,
498};
499
500static const u32 sun6i_spi_bits[] = {
501 [SPI_GCR_TP] = BIT(7),
502 [SPI_GCR_SRST] = BIT(31),
503 [SPI_TCR_CPHA] = BIT(0),
504 [SPI_TCR_CPOL] = BIT(1),
505 [SPI_TCR_CS_ACTIVE_LOW] = BIT(2),
506 [SPI_TCR_CS_SEL] = 4,
507 [SPI_TCR_CS_MASK] = 0x30,
508 [SPI_TCR_CS_MANUAL] = BIT(6),
509 [SPI_TCR_CS_LEVEL] = BIT(7),
510 [SPI_TCR_XCH] = BIT(31),
511 [SPI_FCR_RF_RST] = BIT(15),
512 [SPI_FCR_TF_RST] = BIT(31),
513 [SPI_FSR_RF_CNT_MASK] = GENMASK(7, 0),
514};
515
Jagan Tekic25058c2019-02-27 20:02:08 +0530516static const struct sun4i_spi_variant sun4i_a10_spi_variant = {
517 .regs = sun4i_spi_regs,
518 .bits = sun4i_spi_bits,
Jagan Tekic12eb6a2019-02-27 20:02:09 +0530519 .fifo_depth = 64,
Jagan Tekif69b4252019-02-27 20:02:11 +0530520};
521
522static const struct sun4i_spi_variant sun6i_a31_spi_variant = {
523 .regs = sun6i_spi_regs,
524 .bits = sun6i_spi_bits,
525 .fifo_depth = 128,
526 .has_soft_reset = true,
527 .has_burst_ctl = true,
528};
529
530static const struct sun4i_spi_variant sun8i_h3_spi_variant = {
531 .regs = sun6i_spi_regs,
532 .bits = sun6i_spi_bits,
533 .fifo_depth = 64,
534 .has_soft_reset = true,
535 .has_burst_ctl = true,
Jagan Tekic25058c2019-02-27 20:02:08 +0530536};
537
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200538static const struct udevice_id sun4i_spi_ids[] = {
Jagan Tekic25058c2019-02-27 20:02:08 +0530539 {
540 .compatible = "allwinner,sun4i-a10-spi",
541 .data = (ulong)&sun4i_a10_spi_variant,
542 },
Jagan Tekif69b4252019-02-27 20:02:11 +0530543 {
544 .compatible = "allwinner,sun6i-a31-spi",
545 .data = (ulong)&sun6i_a31_spi_variant,
546 },
547 {
548 .compatible = "allwinner,sun8i-h3-spi",
549 .data = (ulong)&sun8i_h3_spi_variant,
550 },
Jagan Teki3f53a582019-02-27 20:02:12 +0530551 { /* sentinel */ }
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200552};
553
554U_BOOT_DRIVER(sun4i_spi) = {
555 .name = "sun4i_spi",
556 .id = UCLASS_SPI,
557 .of_match = sun4i_spi_ids,
558 .ops = &sun4i_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700559 .of_to_plat = sun4i_spi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700560 .plat_auto = sizeof(struct sun4i_spi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700561 .priv_auto = sizeof(struct sun4i_spi_priv),
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200562 .probe = sun4i_spi_probe,
563};