blob: a7333d8d9c0c0f3435e22e1a50870614725b6f0b [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);
236 unsigned int div;
237 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);
Andre Przywara239dfd12022-05-03 00:07:16 +0100262 reg = readl(SPI_REG(priv, SPI_CCR));
263
Andre Przywarafcd6d932022-05-03 02:06:37 +0100264 if ((div / 2) <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
265 div /= 2;
Andre Przywara239dfd12022-05-03 00:07:16 +0100266 if (div > 0)
267 div--;
268
269 reg &= ~(SUN4I_CLK_CTL_CDR2_MASK | SUN4I_CLK_CTL_DRS);
270 reg |= SUN4I_CLK_CTL_CDR2(div) | SUN4I_CLK_CTL_DRS;
271 } else {
Andre Przywarafcd6d932022-05-03 02:06:37 +0100272 div = fls(div - 1);
Andre Przywara86499952022-04-26 23:58:53 +0100273 /* The F1C100s encodes the divider as 2^(n+1) */
274 if (IS_ENABLED(CONFIG_MACH_SUNIV))
275 div--;
Andre Przywara239dfd12022-05-03 00:07:16 +0100276 reg &= ~((SUN4I_CLK_CTL_CDR1_MASK << 8) | SUN4I_CLK_CTL_DRS);
277 reg |= SUN4I_CLK_CTL_CDR1(div);
278 }
279
280 writel(reg, SPI_REG(priv, SPI_CCR));
281
282 reg = readl(SPI_REG(priv, SPI_TCR));
283 reg &= ~(SPI_BIT(priv, SPI_TCR_CPOL) | SPI_BIT(priv, SPI_TCR_CPHA));
284
285 if (priv->mode & SPI_CPOL)
286 reg |= SPI_BIT(priv, SPI_TCR_CPOL);
287
288 if (priv->mode & SPI_CPHA)
289 reg |= SPI_BIT(priv, SPI_TCR_CPHA);
290
291 writel(reg, SPI_REG(priv, SPI_TCR));
292}
293
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200294static int sun4i_spi_claim_bus(struct udevice *dev)
295{
296 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
Jagan Teki8d71a192019-02-27 20:02:10 +0530297 int ret;
298
299 ret = sun4i_spi_set_clock(dev->parent, true);
300 if (ret)
301 return ret;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200302
Jagan Teki8d9bf462019-02-27 20:02:08 +0530303 setbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE |
304 SUN4I_CTL_MASTER | SPI_BIT(priv, SPI_GCR_TP));
305
Jagan Teki853f4512019-02-27 20:02:11 +0530306 if (priv->variant->has_soft_reset)
307 setbits_le32(SPI_REG(priv, SPI_GCR),
308 SPI_BIT(priv, SPI_GCR_SRST));
309
Jagan Teki8d9bf462019-02-27 20:02:08 +0530310 setbits_le32(SPI_REG(priv, SPI_TCR), SPI_BIT(priv, SPI_TCR_CS_MANUAL) |
311 SPI_BIT(priv, SPI_TCR_CS_ACTIVE_LOW));
Jagan Teki8cbf09b2019-02-27 20:02:07 +0530312
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300313 if (priv->variant->has_clk_ctl) {
314 sun4i_spi_set_speed_mode(dev->parent);
315 } else {
316 /*
317 * At this moment there is no ability to change input clock.
318 * Therefore, we can only use default HOSC@24MHz clock and
319 * set SPI sampling mode to normal
320 */
321 clrsetbits_le32(SPI_REG(priv, SPI_TCR),
322 SPI_BIT(priv, SPI_TCR_SDC) |
323 SPI_BIT(priv, SPI_TCR_SDM),
324 SPI_BIT(priv, SPI_TCR_SDM));
325 }
Andre Przywara239dfd12022-05-03 00:07:16 +0100326
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200327 return 0;
328}
329
330static int sun4i_spi_release_bus(struct udevice *dev)
331{
332 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200333
Jagan Teki8d9bf462019-02-27 20:02:08 +0530334 clrbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE);
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200335
Jagan Teki8d71a192019-02-27 20:02:10 +0530336 sun4i_spi_set_clock(dev->parent, false);
337
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200338 return 0;
339}
340
341static int sun4i_spi_xfer(struct udevice *dev, unsigned int bitlen,
342 const void *dout, void *din, unsigned long flags)
343{
344 struct udevice *bus = dev->parent;
345 struct sun4i_spi_priv *priv = dev_get_priv(bus);
Simon Glass8a8d24b2020-12-03 16:55:23 -0700346 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200347
348 u32 len = bitlen / 8;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200349 u8 nbytes;
350 int ret;
351
352 priv->tx_buf = dout;
353 priv->rx_buf = din;
354
355 if (bitlen % 8) {
356 debug("%s: non byte-aligned SPI transfer.\n", __func__);
357 return -ENAVAIL;
358 }
359
360 if (flags & SPI_XFER_BEGIN)
361 sun4i_spi_set_cs(bus, slave_plat->cs, true);
362
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200363 /* Reset FIFOs */
Jagan Teki8d9bf462019-02-27 20:02:08 +0530364 setbits_le32(SPI_REG(priv, SPI_FCR), SPI_BIT(priv, SPI_FCR_RF_RST) |
365 SPI_BIT(priv, SPI_FCR_TF_RST));
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200366
367 while (len) {
368 /* Setup the transfer now... */
Jagan Teki178fbd22019-02-27 20:02:09 +0530369 nbytes = min(len, (priv->variant->fifo_depth - 1));
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200370
371 /* Setup the counters */
Jagan Teki8d9bf462019-02-27 20:02:08 +0530372 writel(SUN4I_BURST_CNT(nbytes), SPI_REG(priv, SPI_BC));
373 writel(SUN4I_XMIT_CNT(nbytes), SPI_REG(priv, SPI_TC));
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200374
Jagan Teki853f4512019-02-27 20:02:11 +0530375 if (priv->variant->has_burst_ctl)
376 writel(SUN4I_BURST_CNT(nbytes),
377 SPI_REG(priv, SPI_BCTL));
378
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200379 /* Fill the TX FIFO */
380 sun4i_spi_fill_fifo(priv, nbytes);
381
382 /* Start the transfer */
Jagan Teki8d9bf462019-02-27 20:02:08 +0530383 setbits_le32(SPI_REG(priv, SPI_TCR),
384 SPI_BIT(priv, SPI_TCR_XCH));
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200385
Icenowy Zheng56e497e2022-06-28 14:49:24 +0800386 /* Wait for the transfer to be done */
387 ret = wait_for_bit_le32((const void *)SPI_REG(priv, SPI_TCR),
388 SPI_BIT(priv, SPI_TCR_XCH),
389 false, SUN4I_SPI_TIMEOUT_MS, false);
Jagan Teki6cb6aa62019-02-27 20:02:05 +0530390 if (ret < 0) {
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200391 printf("ERROR: sun4i_spi: Timeout transferring data\n");
392 sun4i_spi_set_cs(bus, slave_plat->cs, false);
393 return ret;
394 }
395
396 /* Drain the RX FIFO */
397 sun4i_spi_drain_fifo(priv, nbytes);
398
399 len -= nbytes;
400 }
401
402 if (flags & SPI_XFER_END)
403 sun4i_spi_set_cs(bus, slave_plat->cs, false);
404
405 return 0;
406}
407
408static int sun4i_spi_set_speed(struct udevice *dev, uint speed)
409{
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200410 struct sun4i_spi_priv *priv = dev_get_priv(dev);
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200411
Michael Walle674e4f92024-07-12 19:14:56 +0200412 if (speed > SUN4I_SPI_MAX_RATE)
413 speed = SUN4I_SPI_MAX_RATE;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200414
415 if (speed < SUN4I_SPI_MIN_RATE)
416 speed = SUN4I_SPI_MIN_RATE;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200417
418 priv->freq = speed;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200419
420 return 0;
421}
422
423static int sun4i_spi_set_mode(struct udevice *dev, uint mode)
424{
425 struct sun4i_spi_priv *priv = dev_get_priv(dev);
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200426
427 priv->mode = mode;
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200428
429 return 0;
430}
431
432static const struct dm_spi_ops sun4i_spi_ops = {
433 .claim_bus = sun4i_spi_claim_bus,
434 .release_bus = sun4i_spi_release_bus,
435 .xfer = sun4i_spi_xfer,
436 .set_speed = sun4i_spi_set_speed,
437 .set_mode = sun4i_spi_set_mode,
438};
439
Jagan Teki903e7cf2019-02-27 20:02:12 +0530440static int sun4i_spi_probe(struct udevice *bus)
441{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700442 struct sun4i_spi_plat *plat = dev_get_plat(bus);
Jagan Teki903e7cf2019-02-27 20:02:12 +0530443 struct sun4i_spi_priv *priv = dev_get_priv(bus);
444 int ret;
445
446 ret = clk_get_by_name(bus, "ahb", &priv->clk_ahb);
447 if (ret) {
Sean Anderson32bbe5b2020-09-15 10:45:11 -0400448 dev_err(bus, "failed to get ahb clock\n");
Jagan Teki903e7cf2019-02-27 20:02:12 +0530449 return ret;
450 }
451
452 ret = clk_get_by_name(bus, "mod", &priv->clk_mod);
453 if (ret) {
Sean Anderson32bbe5b2020-09-15 10:45:11 -0400454 dev_err(bus, "failed to get mod clock\n");
Jagan Teki903e7cf2019-02-27 20:02:12 +0530455 return ret;
456 }
457
458 ret = reset_get_by_index(bus, 0, &priv->reset);
459 if (ret && ret != -ENOENT) {
Sean Anderson32bbe5b2020-09-15 10:45:11 -0400460 dev_err(bus, "failed to get reset\n");
Jagan Teki903e7cf2019-02-27 20:02:12 +0530461 return ret;
462 }
463
Jagan Teki903e7cf2019-02-27 20:02:12 +0530464 priv->variant = plat->variant;
465 priv->base = plat->base;
Jagan Teki903e7cf2019-02-27 20:02:12 +0530466
467 return 0;
468}
469
Simon Glassd1998a92020-12-03 16:55:21 -0700470static int sun4i_spi_of_to_plat(struct udevice *bus)
Jagan Teki903e7cf2019-02-27 20:02:12 +0530471{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700472 struct sun4i_spi_plat *plat = dev_get_plat(bus);
Jagan Teki903e7cf2019-02-27 20:02:12 +0530473
Masahiro Yamada25484932020-07-17 14:36:48 +0900474 plat->base = dev_read_addr(bus);
Jagan Teki903e7cf2019-02-27 20:02:12 +0530475 plat->variant = (struct sun4i_spi_variant *)dev_get_driver_data(bus);
Jagan Teki903e7cf2019-02-27 20:02:12 +0530476
477 return 0;
478}
479
Jagan Teki8d9bf462019-02-27 20:02:08 +0530480static const unsigned long sun4i_spi_regs[] = {
481 [SPI_GCR] = SUN4I_CTL_REG,
482 [SPI_TCR] = SUN4I_CTL_REG,
483 [SPI_FCR] = SUN4I_CTL_REG,
484 [SPI_FSR] = SUN4I_FIFO_STA_REG,
485 [SPI_CCR] = SUN4I_CLK_CTL_REG,
486 [SPI_BC] = SUN4I_BURST_CNT_REG,
487 [SPI_TC] = SUN4I_XMIT_CNT_REG,
488 [SPI_TXD] = SUN4I_TXDATA_REG,
489 [SPI_RXD] = SUN4I_RXDATA_REG,
490};
491
492static const u32 sun4i_spi_bits[] = {
493 [SPI_GCR_TP] = BIT(18),
494 [SPI_TCR_CPHA] = BIT(2),
495 [SPI_TCR_CPOL] = BIT(3),
496 [SPI_TCR_CS_ACTIVE_LOW] = BIT(4),
497 [SPI_TCR_XCH] = BIT(10),
498 [SPI_TCR_CS_SEL] = 12,
499 [SPI_TCR_CS_MASK] = 0x3000,
500 [SPI_TCR_CS_MANUAL] = BIT(16),
501 [SPI_TCR_CS_LEVEL] = BIT(17),
502 [SPI_FCR_TF_RST] = BIT(8),
503 [SPI_FCR_RF_RST] = BIT(9),
504 [SPI_FSR_RF_CNT_MASK] = GENMASK(6, 0),
505};
506
Jagan Teki853f4512019-02-27 20:02:11 +0530507static const unsigned long sun6i_spi_regs[] = {
508 [SPI_GCR] = SUN6I_GBL_CTL_REG,
509 [SPI_TCR] = SUN6I_TFR_CTL_REG,
510 [SPI_FCR] = SUN6I_FIFO_CTL_REG,
511 [SPI_FSR] = SUN6I_FIFO_STA_REG,
512 [SPI_CCR] = SUN6I_CLK_CTL_REG,
513 [SPI_BC] = SUN6I_BURST_CNT_REG,
514 [SPI_TC] = SUN6I_XMIT_CNT_REG,
515 [SPI_BCTL] = SUN6I_BURST_CTL_REG,
516 [SPI_TXD] = SUN6I_TXDATA_REG,
517 [SPI_RXD] = SUN6I_RXDATA_REG,
518};
519
520static const u32 sun6i_spi_bits[] = {
521 [SPI_GCR_TP] = BIT(7),
522 [SPI_GCR_SRST] = BIT(31),
523 [SPI_TCR_CPHA] = BIT(0),
524 [SPI_TCR_CPOL] = BIT(1),
525 [SPI_TCR_CS_ACTIVE_LOW] = BIT(2),
526 [SPI_TCR_CS_SEL] = 4,
527 [SPI_TCR_CS_MASK] = 0x30,
528 [SPI_TCR_CS_MANUAL] = BIT(6),
529 [SPI_TCR_CS_LEVEL] = BIT(7),
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300530 [SPI_TCR_SDC] = BIT(11),
531 [SPI_TCR_SDM] = BIT(13),
Jagan Teki853f4512019-02-27 20:02:11 +0530532 [SPI_TCR_XCH] = BIT(31),
533 [SPI_FCR_RF_RST] = BIT(15),
534 [SPI_FCR_TF_RST] = BIT(31),
535 [SPI_FSR_RF_CNT_MASK] = GENMASK(7, 0),
536};
537
Jagan Teki8d9bf462019-02-27 20:02:08 +0530538static const struct sun4i_spi_variant sun4i_a10_spi_variant = {
539 .regs = sun4i_spi_regs,
540 .bits = sun4i_spi_bits,
Jagan Teki178fbd22019-02-27 20:02:09 +0530541 .fifo_depth = 64,
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300542 .has_clk_ctl = true,
Jagan Teki8d9bf462019-02-27 20:02:08 +0530543};
544
Jagan Teki853f4512019-02-27 20:02:11 +0530545static const struct sun4i_spi_variant sun6i_a31_spi_variant = {
546 .regs = sun6i_spi_regs,
547 .bits = sun6i_spi_bits,
548 .fifo_depth = 128,
549 .has_soft_reset = true,
550 .has_burst_ctl = true,
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300551 .has_clk_ctl = true,
Jagan Teki853f4512019-02-27 20:02:11 +0530552};
553
554static const struct sun4i_spi_variant sun8i_h3_spi_variant = {
555 .regs = sun6i_spi_regs,
556 .bits = sun6i_spi_bits,
557 .fifo_depth = 64,
558 .has_soft_reset = true,
559 .has_burst_ctl = true,
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300560 .has_clk_ctl = true,
561};
562
563static const struct sun4i_spi_variant sun50i_r329_spi_variant = {
564 .regs = sun6i_spi_regs,
565 .bits = sun6i_spi_bits,
566 .fifo_depth = 64,
567 .has_soft_reset = true,
568 .has_burst_ctl = true,
Jagan Teki853f4512019-02-27 20:02:11 +0530569};
570
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200571static const struct udevice_id sun4i_spi_ids[] = {
Jagan Teki8d9bf462019-02-27 20:02:08 +0530572 {
573 .compatible = "allwinner,sun4i-a10-spi",
574 .data = (ulong)&sun4i_a10_spi_variant,
575 },
Jagan Teki853f4512019-02-27 20:02:11 +0530576 {
577 .compatible = "allwinner,sun6i-a31-spi",
578 .data = (ulong)&sun6i_a31_spi_variant,
579 },
580 {
581 .compatible = "allwinner,sun8i-h3-spi",
582 .data = (ulong)&sun8i_h3_spi_variant,
583 },
Maksim Kiselev2b9d6a12023-11-11 16:33:08 +0300584 {
585 .compatible = "allwinner,sun50i-r329-spi",
586 .data = (ulong)&sun50i_r329_spi_variant,
587 },
Jagan Teki903e7cf2019-02-27 20:02:12 +0530588 { /* sentinel */ }
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200589};
590
591U_BOOT_DRIVER(sun4i_spi) = {
592 .name = "sun4i_spi",
593 .id = UCLASS_SPI,
594 .of_match = sun4i_spi_ids,
595 .ops = &sun4i_spi_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700596 .of_to_plat = sun4i_spi_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700597 .plat_auto = sizeof(struct sun4i_spi_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700598 .priv_auto = sizeof(struct sun4i_spi_priv),
Stefan Mavrodiev7f25d812018-02-06 15:14:33 +0200599 .probe = sun4i_spi_probe,
600};