blob: cbc2c4c7b42df641c900c404702fe51cffc93d3b [file] [log] [blame]
Stefan Mavrodiev7f25d812018-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
Jagan Teki8d71a192019-02-27 20:02:10 +053021#include <clk.h>
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +020022#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060023#include <log.h>
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +020024#include <spi.h>
25#include <errno.h>
26#include <fdt_support.h>
Jagan Teki853f4512019-02-27 20:02:11 +053027#include <reset.h>
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +020028#include <wait_bit.h>
Simon Glass401d1c42020-10-30 21:38:53 -060029#include <asm/global_data.h>
Simon Glass336d4612020-02-03 07:36:16 -070030#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060031#include <linux/bitops.h>
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +020032
33#include <asm/bitops.h>
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +020034#include <asm/io.h>
35
Jagan Teki6cb6aa62019-02-27 20:02:05 +053036#include <linux/iopoll.h>
37
Jagan Teki903e7cf2019-02-27 20:02:12 +053038DECLARE_GLOBAL_DATA_PTR;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +020039
Jagan Teki903e7cf2019-02-27 20:02:12 +053040/* sun4i spi registers */
41#define SUN4I_RXDATA_REG 0x00
42#define SUN4I_TXDATA_REG 0x04
43#define SUN4I_CTL_REG 0x08
44#define SUN4I_CLK_CTL_REG 0x1c
45#define SUN4I_BURST_CNT_REG 0x20
46#define SUN4I_XMIT_CNT_REG 0x24
47#define SUN4I_FIFO_STA_REG 0x28
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +020048
Jagan Teki853f4512019-02-27 20:02:11 +053049/* sun6i spi registers */
50#define SUN6I_GBL_CTL_REG 0x04
51#define SUN6I_TFR_CTL_REG 0x08
52#define SUN6I_FIFO_CTL_REG 0x18
53#define SUN6I_FIFO_STA_REG 0x1c
54#define SUN6I_CLK_CTL_REG 0x24
55#define SUN6I_BURST_CNT_REG 0x30
56#define SUN6I_XMIT_CNT_REG 0x34
57#define SUN6I_BURST_CTL_REG 0x38
58#define SUN6I_TXDATA_REG 0x200
59#define SUN6I_RXDATA_REG 0x300
60
Jagan Teki903e7cf2019-02-27 20:02:12 +053061/* sun spi bits */
62#define SUN4I_CTL_ENABLE BIT(0)
63#define SUN4I_CTL_MASTER BIT(1)
64#define SUN4I_CLK_CTL_CDR2_MASK 0xff
65#define SUN4I_CLK_CTL_CDR2(div) ((div) & SUN4I_CLK_CTL_CDR2_MASK)
66#define SUN4I_CLK_CTL_CDR1_MASK 0xf
67#define SUN4I_CLK_CTL_CDR1(div) (((div) & SUN4I_CLK_CTL_CDR1_MASK) << 8)
68#define SUN4I_CLK_CTL_DRS BIT(12)
69#define SUN4I_MAX_XFER_SIZE 0xffffff
70#define SUN4I_BURST_CNT(cnt) ((cnt) & SUN4I_MAX_XFER_SIZE)
71#define SUN4I_XMIT_CNT(cnt) ((cnt) & SUN4I_MAX_XFER_SIZE)
72#define SUN4I_FIFO_STA_RF_CNT_BITS 0
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +020073
Andre Przywara86499952022-04-26 23:58:53 +010074#ifdef CONFIG_MACH_SUNIV
75/* the AHB clock, which we programmed to be 1/3 of PLL_PERIPH@600MHz */
76#define SUNXI_INPUT_CLOCK 200000000 /* 200 MHz */
77#define SUN4I_SPI_MAX_RATE (SUNXI_INPUT_CLOCK / 2)
78#else
Andre Przywarafcd6d932022-05-03 02:06:37 +010079/* the SPI mod clock, defaulting to be 1/1 of the HOSC@24MHz */
80#define SUNXI_INPUT_CLOCK 24000000 /* 24 MHz */
81#define SUN4I_SPI_MAX_RATE SUNXI_INPUT_CLOCK
Andre Przywara86499952022-04-26 23:58:53 +010082#endif
Jagan Teki903e7cf2019-02-27 20:02:12 +053083#define SUN4I_SPI_MIN_RATE 3000
84#define SUN4I_SPI_DEFAULT_RATE 1000000
Icenowy Zheng56e497e2022-06-28 14:49:24 +080085#define SUN4I_SPI_TIMEOUT_MS 1000
Jagan Teki903e7cf2019-02-27 20:02:12 +053086
87#define SPI_REG(priv, reg) ((priv)->base + \
Jagan Teki8d9bf462019-02-27 20:02:08 +053088 (priv)->variant->regs[reg])
89#define SPI_BIT(priv, bit) ((priv)->variant->bits[bit])
90#define SPI_CS(priv, cs) (((cs) << SPI_BIT(priv, SPI_TCR_CS_SEL)) & \
91 SPI_BIT(priv, SPI_TCR_CS_MASK))
92
93/* sun spi register set */
94enum sun4i_spi_regs {
95 SPI_GCR,
96 SPI_TCR,
97 SPI_FCR,
98 SPI_FSR,
99 SPI_CCR,
100 SPI_BC,
101 SPI_TC,
102 SPI_BCTL,
103 SPI_TXD,
104 SPI_RXD,
105};
106
107/* sun spi register bits */
108enum sun4i_spi_bits {
109 SPI_GCR_TP,
Jagan Teki853f4512019-02-27 20:02:11 +0530110 SPI_GCR_SRST,
Jagan Teki8d9bf462019-02-27 20:02:08 +0530111 SPI_TCR_CPHA,
112 SPI_TCR_CPOL,
113 SPI_TCR_CS_ACTIVE_LOW,
114 SPI_TCR_CS_SEL,
115 SPI_TCR_CS_MASK,
116 SPI_TCR_XCH,
117 SPI_TCR_CS_MANUAL,
118 SPI_TCR_CS_LEVEL,
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300119 SPI_TCR_SDC,
120 SPI_TCR_SDM,
Jagan Teki8d9bf462019-02-27 20:02:08 +0530121 SPI_FCR_TF_RST,
122 SPI_FCR_RF_RST,
123 SPI_FSR_RF_CNT_MASK,
124};
125
126struct sun4i_spi_variant {
127 const unsigned long *regs;
128 const u32 *bits;
Jagan Teki178fbd22019-02-27 20:02:09 +0530129 u32 fifo_depth;
Jagan Teki853f4512019-02-27 20:02:11 +0530130 bool has_soft_reset;
131 bool has_burst_ctl;
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300132 bool has_clk_ctl;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200133};
134
Simon Glass8a8d24b2020-12-03 16:55:23 -0700135struct sun4i_spi_plat {
Jagan Teki8d9bf462019-02-27 20:02:08 +0530136 struct sun4i_spi_variant *variant;
Jagan Teki903e7cf2019-02-27 20:02:12 +0530137 u32 base;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200138};
139
140struct sun4i_spi_priv {
Jagan Teki8d9bf462019-02-27 20:02:08 +0530141 struct sun4i_spi_variant *variant;
Jagan Teki8d71a192019-02-27 20:02:10 +0530142 struct clk clk_ahb, clk_mod;
Jagan Teki853f4512019-02-27 20:02:11 +0530143 struct reset_ctl reset;
Jagan Teki903e7cf2019-02-27 20:02:12 +0530144 u32 base;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200145 u32 freq;
146 u32 mode;
147
148 const u8 *tx_buf;
149 u8 *rx_buf;
150};
151
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200152static inline void sun4i_spi_drain_fifo(struct sun4i_spi_priv *priv, int len)
153{
154 u8 byte;
155
156 while (len--) {
Jagan Teki8d9bf462019-02-27 20:02:08 +0530157 byte = readb(SPI_REG(priv, SPI_RXD));
Stefan Mavrodiev5c1a87d2018-12-05 14:27:57 +0200158 if (priv->rx_buf)
159 *priv->rx_buf++ = byte;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200160 }
161}
162
163static inline void sun4i_spi_fill_fifo(struct sun4i_spi_priv *priv, int len)
164{
165 u8 byte;
166
167 while (len--) {
168 byte = priv->tx_buf ? *priv->tx_buf++ : 0;
Jagan Teki8d9bf462019-02-27 20:02:08 +0530169 writeb(byte, SPI_REG(priv, SPI_TXD));
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200170 }
171}
172
173static void sun4i_spi_set_cs(struct udevice *bus, u8 cs, bool enable)
174{
175 struct sun4i_spi_priv *priv = dev_get_priv(bus);
176 u32 reg;
177
Jagan Teki8d9bf462019-02-27 20:02:08 +0530178 reg = readl(SPI_REG(priv, SPI_TCR));
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200179
Jagan Teki8d9bf462019-02-27 20:02:08 +0530180 reg &= ~SPI_BIT(priv, SPI_TCR_CS_MASK);
181 reg |= SPI_CS(priv, cs);
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200182
183 if (enable)
Jagan Teki8d9bf462019-02-27 20:02:08 +0530184 reg &= ~SPI_BIT(priv, SPI_TCR_CS_LEVEL);
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200185 else
Jagan Teki8d9bf462019-02-27 20:02:08 +0530186 reg |= SPI_BIT(priv, SPI_TCR_CS_LEVEL);
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200187
Jagan Teki8d9bf462019-02-27 20:02:08 +0530188 writel(reg, SPI_REG(priv, SPI_TCR));
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200189}
190
Jagan Teki8d71a192019-02-27 20:02:10 +0530191static inline int sun4i_spi_set_clock(struct udevice *dev, bool enable)
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200192{
Jagan Teki8d71a192019-02-27 20:02:10 +0530193 struct sun4i_spi_priv *priv = dev_get_priv(dev);
194 int ret;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200195
Jagan Teki8d71a192019-02-27 20:02:10 +0530196 if (!enable) {
197 clk_disable(&priv->clk_ahb);
198 clk_disable(&priv->clk_mod);
Jagan Teki853f4512019-02-27 20:02:11 +0530199 if (reset_valid(&priv->reset))
200 reset_assert(&priv->reset);
Jagan Teki8d71a192019-02-27 20:02:10 +0530201 return 0;
202 }
203
204 ret = clk_enable(&priv->clk_ahb);
205 if (ret) {
206 dev_err(dev, "failed to enable ahb clock (ret=%d)\n", ret);
207 return ret;
208 }
209
210 ret = clk_enable(&priv->clk_mod);
211 if (ret) {
212 dev_err(dev, "failed to enable mod clock (ret=%d)\n", ret);
213 goto err_ahb;
214 }
215
Jagan Teki853f4512019-02-27 20:02:11 +0530216 if (reset_valid(&priv->reset)) {
217 ret = reset_deassert(&priv->reset);
218 if (ret) {
219 dev_err(dev, "failed to deassert reset\n");
220 goto err_mod;
221 }
222 }
223
Jagan Teki8d71a192019-02-27 20:02:10 +0530224 return 0;
225
Jagan Teki853f4512019-02-27 20:02:11 +0530226err_mod:
227 clk_disable(&priv->clk_mod);
Jagan Teki8d71a192019-02-27 20:02:10 +0530228err_ahb:
229 clk_disable(&priv->clk_ahb);
230 return ret;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200231}
232
Andre Przywara239dfd12022-05-03 00:07:16 +0100233static void sun4i_spi_set_speed_mode(struct udevice *dev)
234{
235 struct sun4i_spi_priv *priv = dev_get_priv(dev);
Michael Walle6aadcb82024-07-18 22:42:52 +0200236 unsigned int div, div_cdr2;
Andre Przywara239dfd12022-05-03 00:07:16 +0100237 u32 reg;
238
239 /*
Michael Walle674e4f92024-07-12 19:14:56 +0200240 * The uclass should take care that this won't happen. But anyway,
241 * avoid a div-by-zero exception.
242 */
243 if (!priv->freq)
244 return;
245
246 /*
Andre Przywara239dfd12022-05-03 00:07:16 +0100247 * Setup clock divider.
248 *
249 * We have two choices there. Either we can use the clock
250 * divide rate 1, which is calculated thanks to this formula:
251 * SPI_CLK = MOD_CLK / (2 ^ (cdr + 1))
252 * Or we can use CDR2, which is calculated with the formula:
253 * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
254 * Whether we use the former or the latter is set through the
255 * DRS bit.
256 *
257 * First try CDR2, and if we can't reach the expected
258 * frequency, fall back to CDR1.
259 */
260
Andre Przywarafcd6d932022-05-03 02:06:37 +0100261 div = DIV_ROUND_UP(SUNXI_INPUT_CLOCK, priv->freq);
Michael Walle6aadcb82024-07-18 22:42:52 +0200262 div_cdr2 = DIV_ROUND_UP(div, 2);
Andre Przywara239dfd12022-05-03 00:07:16 +0100263 reg = readl(SPI_REG(priv, SPI_CCR));
264
Michael Walle6aadcb82024-07-18 22:42:52 +0200265 if (div_cdr2 <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
Andre Przywara239dfd12022-05-03 00:07:16 +0100266 reg &= ~(SUN4I_CLK_CTL_CDR2_MASK | SUN4I_CLK_CTL_DRS);
Michael Walle6aadcb82024-07-18 22:42:52 +0200267 reg |= SUN4I_CLK_CTL_CDR2(div_cdr2 - 1) | SUN4I_CLK_CTL_DRS;
Andre Przywara239dfd12022-05-03 00:07:16 +0100268 } else {
Andre Przywarafcd6d932022-05-03 02:06:37 +0100269 div = fls(div - 1);
Andre Przywara86499952022-04-26 23:58:53 +0100270 /* The F1C100s encodes the divider as 2^(n+1) */
271 if (IS_ENABLED(CONFIG_MACH_SUNIV))
272 div--;
Andre Przywara239dfd12022-05-03 00:07:16 +0100273 reg &= ~((SUN4I_CLK_CTL_CDR1_MASK << 8) | SUN4I_CLK_CTL_DRS);
274 reg |= SUN4I_CLK_CTL_CDR1(div);
275 }
276
277 writel(reg, SPI_REG(priv, SPI_CCR));
278
279 reg = readl(SPI_REG(priv, SPI_TCR));
280 reg &= ~(SPI_BIT(priv, SPI_TCR_CPOL) | SPI_BIT(priv, SPI_TCR_CPHA));
281
282 if (priv->mode & SPI_CPOL)
283 reg |= SPI_BIT(priv, SPI_TCR_CPOL);
284
285 if (priv->mode & SPI_CPHA)
286 reg |= SPI_BIT(priv, SPI_TCR_CPHA);
287
288 writel(reg, SPI_REG(priv, SPI_TCR));
289}
290
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200291static int sun4i_spi_claim_bus(struct udevice *dev)
292{
293 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
Jagan Teki8d71a192019-02-27 20:02:10 +0530294 int ret;
295
296 ret = sun4i_spi_set_clock(dev->parent, true);
297 if (ret)
298 return ret;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200299
Jagan Teki8d9bf462019-02-27 20:02:08 +0530300 setbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE |
301 SUN4I_CTL_MASTER | SPI_BIT(priv, SPI_GCR_TP));
302
Jagan Teki853f4512019-02-27 20:02:11 +0530303 if (priv->variant->has_soft_reset)
304 setbits_le32(SPI_REG(priv, SPI_GCR),
305 SPI_BIT(priv, SPI_GCR_SRST));
306
Jagan Teki8d9bf462019-02-27 20:02:08 +0530307 setbits_le32(SPI_REG(priv, SPI_TCR), SPI_BIT(priv, SPI_TCR_CS_MANUAL) |
308 SPI_BIT(priv, SPI_TCR_CS_ACTIVE_LOW));
Jagan Teki8cbf09b2019-02-27 20:02:07 +0530309
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300310 if (priv->variant->has_clk_ctl) {
311 sun4i_spi_set_speed_mode(dev->parent);
312 } else {
313 /*
314 * At this moment there is no ability to change input clock.
315 * Therefore, we can only use default HOSC@24MHz clock and
316 * set SPI sampling mode to normal
317 */
318 clrsetbits_le32(SPI_REG(priv, SPI_TCR),
319 SPI_BIT(priv, SPI_TCR_SDC) |
320 SPI_BIT(priv, SPI_TCR_SDM),
321 SPI_BIT(priv, SPI_TCR_SDM));
322 }
Andre Przywara239dfd12022-05-03 00:07:16 +0100323
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200324 return 0;
325}
326
327static int sun4i_spi_release_bus(struct udevice *dev)
328{
329 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200330
Jagan Teki8d9bf462019-02-27 20:02:08 +0530331 clrbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE);
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200332
Jagan Teki8d71a192019-02-27 20:02:10 +0530333 sun4i_spi_set_clock(dev->parent, false);
334
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200335 return 0;
336}
337
338static int sun4i_spi_xfer(struct udevice *dev, unsigned int bitlen,
339 const void *dout, void *din, unsigned long flags)
340{
341 struct udevice *bus = dev->parent;
342 struct sun4i_spi_priv *priv = dev_get_priv(bus);
Simon Glass8a8d24b2020-12-03 16:55:23 -0700343 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200344
345 u32 len = bitlen / 8;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200346 u8 nbytes;
347 int ret;
348
349 priv->tx_buf = dout;
350 priv->rx_buf = din;
351
352 if (bitlen % 8) {
353 debug("%s: non byte-aligned SPI transfer.\n", __func__);
354 return -ENAVAIL;
355 }
356
357 if (flags & SPI_XFER_BEGIN)
358 sun4i_spi_set_cs(bus, slave_plat->cs, true);
359
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200360 /* Reset FIFOs */
Jagan Teki8d9bf462019-02-27 20:02:08 +0530361 setbits_le32(SPI_REG(priv, SPI_FCR), SPI_BIT(priv, SPI_FCR_RF_RST) |
362 SPI_BIT(priv, SPI_FCR_TF_RST));
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200363
364 while (len) {
365 /* Setup the transfer now... */
Jagan Teki178fbd22019-02-27 20:02:09 +0530366 nbytes = min(len, (priv->variant->fifo_depth - 1));
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200367
368 /* Setup the counters */
Jagan Teki8d9bf462019-02-27 20:02:08 +0530369 writel(SUN4I_BURST_CNT(nbytes), SPI_REG(priv, SPI_BC));
370 writel(SUN4I_XMIT_CNT(nbytes), SPI_REG(priv, SPI_TC));
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200371
Jagan Teki853f4512019-02-27 20:02:11 +0530372 if (priv->variant->has_burst_ctl)
373 writel(SUN4I_BURST_CNT(nbytes),
374 SPI_REG(priv, SPI_BCTL));
375
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200376 /* Fill the TX FIFO */
377 sun4i_spi_fill_fifo(priv, nbytes);
378
379 /* Start the transfer */
Jagan Teki8d9bf462019-02-27 20:02:08 +0530380 setbits_le32(SPI_REG(priv, SPI_TCR),
381 SPI_BIT(priv, SPI_TCR_XCH));
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200382
Icenowy Zheng56e497e2022-06-28 14:49:24 +0800383 /* Wait for the transfer to be done */
384 ret = wait_for_bit_le32((const void *)SPI_REG(priv, SPI_TCR),
385 SPI_BIT(priv, SPI_TCR_XCH),
386 false, SUN4I_SPI_TIMEOUT_MS, false);
Jagan Teki6cb6aa62019-02-27 20:02:05 +0530387 if (ret < 0) {
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200388 printf("ERROR: sun4i_spi: Timeout transferring data\n");
389 sun4i_spi_set_cs(bus, slave_plat->cs, false);
390 return ret;
391 }
392
393 /* Drain the RX FIFO */
394 sun4i_spi_drain_fifo(priv, nbytes);
395
396 len -= nbytes;
397 }
398
399 if (flags & SPI_XFER_END)
400 sun4i_spi_set_cs(bus, slave_plat->cs, false);
401
402 return 0;
403}
404
405static int sun4i_spi_set_speed(struct udevice *dev, uint speed)
406{
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200407 struct sun4i_spi_priv *priv = dev_get_priv(dev);
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200408
Michael Walle674e4f92024-07-12 19:14:56 +0200409 if (speed > SUN4I_SPI_MAX_RATE)
410 speed = SUN4I_SPI_MAX_RATE;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200411
412 if (speed < SUN4I_SPI_MIN_RATE)
413 speed = SUN4I_SPI_MIN_RATE;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200414
415 priv->freq = speed;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200416
417 return 0;
418}
419
420static int sun4i_spi_set_mode(struct udevice *dev, uint mode)
421{
422 struct sun4i_spi_priv *priv = dev_get_priv(dev);
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200423
424 priv->mode = mode;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200425
426 return 0;
427}
428
429static const struct dm_spi_ops sun4i_spi_ops = {
430 .claim_bus = sun4i_spi_claim_bus,
431 .release_bus = sun4i_spi_release_bus,
432 .xfer = sun4i_spi_xfer,
433 .set_speed = sun4i_spi_set_speed,
434 .set_mode = sun4i_spi_set_mode,
435};
436
Jagan Teki903e7cf2019-02-27 20:02:12 +0530437static int sun4i_spi_probe(struct udevice *bus)
438{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700439 struct sun4i_spi_plat *plat = dev_get_plat(bus);
Jagan Teki903e7cf2019-02-27 20:02:12 +0530440 struct sun4i_spi_priv *priv = dev_get_priv(bus);
441 int ret;
442
443 ret = clk_get_by_name(bus, "ahb", &priv->clk_ahb);
444 if (ret) {
Sean Anderson32bbe5b2020-09-15 10:45:11 -0400445 dev_err(bus, "failed to get ahb clock\n");
Jagan Teki903e7cf2019-02-27 20:02:12 +0530446 return ret;
447 }
448
449 ret = clk_get_by_name(bus, "mod", &priv->clk_mod);
450 if (ret) {
Sean Anderson32bbe5b2020-09-15 10:45:11 -0400451 dev_err(bus, "failed to get mod clock\n");
Jagan Teki903e7cf2019-02-27 20:02:12 +0530452 return ret;
453 }
454
455 ret = reset_get_by_index(bus, 0, &priv->reset);
456 if (ret && ret != -ENOENT) {
Sean Anderson32bbe5b2020-09-15 10:45:11 -0400457 dev_err(bus, "failed to get reset\n");
Jagan Teki903e7cf2019-02-27 20:02:12 +0530458 return ret;
459 }
460
Jagan Teki903e7cf2019-02-27 20:02:12 +0530461 priv->variant = plat->variant;
462 priv->base = plat->base;
Jagan Teki903e7cf2019-02-27 20:02:12 +0530463
464 return 0;
465}
466
Simon Glassd1998a92020-12-03 16:55:21 -0700467static int sun4i_spi_of_to_plat(struct udevice *bus)
Jagan Teki903e7cf2019-02-27 20:02:12 +0530468{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700469 struct sun4i_spi_plat *plat = dev_get_plat(bus);
Jagan Teki903e7cf2019-02-27 20:02:12 +0530470
Masahiro Yamada25484932020-07-17 14:36:48 +0900471 plat->base = dev_read_addr(bus);
Jagan Teki903e7cf2019-02-27 20:02:12 +0530472 plat->variant = (struct sun4i_spi_variant *)dev_get_driver_data(bus);
Jagan Teki903e7cf2019-02-27 20:02:12 +0530473
474 return 0;
475}
476
Jagan Teki8d9bf462019-02-27 20:02:08 +0530477static const unsigned long sun4i_spi_regs[] = {
478 [SPI_GCR] = SUN4I_CTL_REG,
479 [SPI_TCR] = SUN4I_CTL_REG,
480 [SPI_FCR] = SUN4I_CTL_REG,
481 [SPI_FSR] = SUN4I_FIFO_STA_REG,
482 [SPI_CCR] = SUN4I_CLK_CTL_REG,
483 [SPI_BC] = SUN4I_BURST_CNT_REG,
484 [SPI_TC] = SUN4I_XMIT_CNT_REG,
485 [SPI_TXD] = SUN4I_TXDATA_REG,
486 [SPI_RXD] = SUN4I_RXDATA_REG,
487};
488
489static const u32 sun4i_spi_bits[] = {
490 [SPI_GCR_TP] = BIT(18),
491 [SPI_TCR_CPHA] = BIT(2),
492 [SPI_TCR_CPOL] = BIT(3),
493 [SPI_TCR_CS_ACTIVE_LOW] = BIT(4),
494 [SPI_TCR_XCH] = BIT(10),
495 [SPI_TCR_CS_SEL] = 12,
496 [SPI_TCR_CS_MASK] = 0x3000,
497 [SPI_TCR_CS_MANUAL] = BIT(16),
498 [SPI_TCR_CS_LEVEL] = BIT(17),
499 [SPI_FCR_TF_RST] = BIT(8),
500 [SPI_FCR_RF_RST] = BIT(9),
501 [SPI_FSR_RF_CNT_MASK] = GENMASK(6, 0),
502};
503
Jagan Teki853f4512019-02-27 20:02:11 +0530504static const unsigned long sun6i_spi_regs[] = {
505 [SPI_GCR] = SUN6I_GBL_CTL_REG,
506 [SPI_TCR] = SUN6I_TFR_CTL_REG,
507 [SPI_FCR] = SUN6I_FIFO_CTL_REG,
508 [SPI_FSR] = SUN6I_FIFO_STA_REG,
509 [SPI_CCR] = SUN6I_CLK_CTL_REG,
510 [SPI_BC] = SUN6I_BURST_CNT_REG,
511 [SPI_TC] = SUN6I_XMIT_CNT_REG,
512 [SPI_BCTL] = SUN6I_BURST_CTL_REG,
513 [SPI_TXD] = SUN6I_TXDATA_REG,
514 [SPI_RXD] = SUN6I_RXDATA_REG,
515};
516
517static const u32 sun6i_spi_bits[] = {
518 [SPI_GCR_TP] = BIT(7),
519 [SPI_GCR_SRST] = BIT(31),
520 [SPI_TCR_CPHA] = BIT(0),
521 [SPI_TCR_CPOL] = BIT(1),
522 [SPI_TCR_CS_ACTIVE_LOW] = BIT(2),
523 [SPI_TCR_CS_SEL] = 4,
524 [SPI_TCR_CS_MASK] = 0x30,
525 [SPI_TCR_CS_MANUAL] = BIT(6),
526 [SPI_TCR_CS_LEVEL] = BIT(7),
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300527 [SPI_TCR_SDC] = BIT(11),
528 [SPI_TCR_SDM] = BIT(13),
Jagan Teki853f4512019-02-27 20:02:11 +0530529 [SPI_TCR_XCH] = BIT(31),
530 [SPI_FCR_RF_RST] = BIT(15),
531 [SPI_FCR_TF_RST] = BIT(31),
532 [SPI_FSR_RF_CNT_MASK] = GENMASK(7, 0),
533};
534
Jagan Teki8d9bf462019-02-27 20:02:08 +0530535static const struct sun4i_spi_variant sun4i_a10_spi_variant = {
536 .regs = sun4i_spi_regs,
537 .bits = sun4i_spi_bits,
Jagan Teki178fbd22019-02-27 20:02:09 +0530538 .fifo_depth = 64,
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300539 .has_clk_ctl = true,
Jagan Teki8d9bf462019-02-27 20:02:08 +0530540};
541
Jagan Teki853f4512019-02-27 20:02:11 +0530542static const struct sun4i_spi_variant sun6i_a31_spi_variant = {
543 .regs = sun6i_spi_regs,
544 .bits = sun6i_spi_bits,
545 .fifo_depth = 128,
546 .has_soft_reset = true,
547 .has_burst_ctl = true,
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300548 .has_clk_ctl = true,
Jagan Teki853f4512019-02-27 20:02:11 +0530549};
550
551static const struct sun4i_spi_variant sun8i_h3_spi_variant = {
552 .regs = sun6i_spi_regs,
553 .bits = sun6i_spi_bits,
554 .fifo_depth = 64,
555 .has_soft_reset = true,
556 .has_burst_ctl = true,
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300557 .has_clk_ctl = true,
558};
559
560static const struct sun4i_spi_variant sun50i_r329_spi_variant = {
561 .regs = sun6i_spi_regs,
562 .bits = sun6i_spi_bits,
563 .fifo_depth = 64,
564 .has_soft_reset = true,
565 .has_burst_ctl = true,
Jagan Teki853f4512019-02-27 20:02:11 +0530566};
567
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200568static const struct udevice_id sun4i_spi_ids[] = {
Jagan Teki8d9bf462019-02-27 20:02:08 +0530569 {
570 .compatible = "allwinner,sun4i-a10-spi",
571 .data = (ulong)&sun4i_a10_spi_variant,
572 },
Jagan Teki853f4512019-02-27 20:02:11 +0530573 {
574 .compatible = "allwinner,sun6i-a31-spi",
575 .data = (ulong)&sun6i_a31_spi_variant,
576 },
577 {
578 .compatible = "allwinner,sun8i-h3-spi",
579 .data = (ulong)&sun8i_h3_spi_variant,
580 },
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300581 {
582 .compatible = "allwinner,sun50i-r329-spi",
583 .data = (ulong)&sun50i_r329_spi_variant,
584 },
Jagan Teki903e7cf2019-02-27 20:02:12 +0530585 { /* sentinel */ }
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200586};
587
588U_BOOT_DRIVER(sun4i_spi) = {
589 .name = "sun4i_spi",
590 .id = UCLASS_SPI,
591 .of_match = sun4i_spi_ids,
592 .ops = &sun4i_spi_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700593 .of_to_plat = sun4i_spi_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700594 .plat_auto = sizeof(struct sun4i_spi_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700595 .priv_auto = sizeof(struct sun4i_spi_priv),
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200596 .probe = sun4i_spi_probe,
597};