blob: 9ec6b359e227e1c78c7dd55e50de95d0f6f66a5a [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 Przywara4d3521ce2022-04-26 23:58:53 +010075#ifdef CONFIG_MACH_SUNIV
76/* the AHB clock, which we programmed to be 1/3 of PLL_PERIPH@600MHz */
77#define SUNXI_INPUT_CLOCK 200000000 /* 200 MHz */
78#define SUN4I_SPI_MAX_RATE (SUNXI_INPUT_CLOCK / 2)
79#else
Andre Przywara27835682022-05-03 02:06:37 +010080/* the SPI mod clock, defaulting to be 1/1 of the HOSC@24MHz */
81#define SUNXI_INPUT_CLOCK 24000000 /* 24 MHz */
82#define SUN4I_SPI_MAX_RATE SUNXI_INPUT_CLOCK
Andre Przywara4d3521ce2022-04-26 23:58:53 +010083#endif
Jagan Teki3f53a582019-02-27 20:02:12 +053084#define SUN4I_SPI_MIN_RATE 3000
85#define SUN4I_SPI_DEFAULT_RATE 1000000
Icenowy Zhenga244be62022-06-28 14:49:24 +080086#define SUN4I_SPI_TIMEOUT_MS 1000
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020087
Jagan Teki3f53a582019-02-27 20:02:12 +053088#define SPI_REG(priv, reg) ((priv)->base + \
Jagan Tekic25058c2019-02-27 20:02:08 +053089 (priv)->variant->regs[reg])
90#define SPI_BIT(priv, bit) ((priv)->variant->bits[bit])
91#define SPI_CS(priv, cs) (((cs) << SPI_BIT(priv, SPI_TCR_CS_SEL)) & \
92 SPI_BIT(priv, SPI_TCR_CS_MASK))
93
94/* sun spi register set */
95enum sun4i_spi_regs {
96 SPI_GCR,
97 SPI_TCR,
98 SPI_FCR,
99 SPI_FSR,
100 SPI_CCR,
101 SPI_BC,
102 SPI_TC,
103 SPI_BCTL,
104 SPI_TXD,
105 SPI_RXD,
106};
107
108/* sun spi register bits */
109enum sun4i_spi_bits {
110 SPI_GCR_TP,
Jagan Tekif69b4252019-02-27 20:02:11 +0530111 SPI_GCR_SRST,
Jagan Tekic25058c2019-02-27 20:02:08 +0530112 SPI_TCR_CPHA,
113 SPI_TCR_CPOL,
114 SPI_TCR_CS_ACTIVE_LOW,
115 SPI_TCR_CS_SEL,
116 SPI_TCR_CS_MASK,
117 SPI_TCR_XCH,
118 SPI_TCR_CS_MANUAL,
119 SPI_TCR_CS_LEVEL,
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300120 SPI_TCR_SDC,
121 SPI_TCR_SDM,
Jagan Tekic25058c2019-02-27 20:02:08 +0530122 SPI_FCR_TF_RST,
123 SPI_FCR_RF_RST,
124 SPI_FSR_RF_CNT_MASK,
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200125};
126
Jagan Tekic25058c2019-02-27 20:02:08 +0530127struct sun4i_spi_variant {
128 const unsigned long *regs;
129 const u32 *bits;
Jagan Tekic12eb6a2019-02-27 20:02:09 +0530130 u32 fifo_depth;
Jagan Tekif69b4252019-02-27 20:02:11 +0530131 bool has_soft_reset;
132 bool has_burst_ctl;
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300133 bool has_clk_ctl;
Jagan Tekic25058c2019-02-27 20:02:08 +0530134};
135
Simon Glassb75b15b2020-12-03 16:55:23 -0700136struct sun4i_spi_plat {
Jagan Tekic25058c2019-02-27 20:02:08 +0530137 struct sun4i_spi_variant *variant;
Jagan Teki3f53a582019-02-27 20:02:12 +0530138 u32 base;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200139 u32 max_hz;
140};
141
142struct sun4i_spi_priv {
Jagan Tekic25058c2019-02-27 20:02:08 +0530143 struct sun4i_spi_variant *variant;
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530144 struct clk clk_ahb, clk_mod;
Jagan Tekif69b4252019-02-27 20:02:11 +0530145 struct reset_ctl reset;
Jagan Teki3f53a582019-02-27 20:02:12 +0530146 u32 base;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200147 u32 freq;
148 u32 mode;
149
150 const u8 *tx_buf;
151 u8 *rx_buf;
152};
153
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200154static inline void sun4i_spi_drain_fifo(struct sun4i_spi_priv *priv, int len)
155{
156 u8 byte;
157
158 while (len--) {
Jagan Tekic25058c2019-02-27 20:02:08 +0530159 byte = readb(SPI_REG(priv, SPI_RXD));
Stefan Mavrodiev165db622018-12-05 14:27:57 +0200160 if (priv->rx_buf)
161 *priv->rx_buf++ = byte;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200162 }
163}
164
165static inline void sun4i_spi_fill_fifo(struct sun4i_spi_priv *priv, int len)
166{
167 u8 byte;
168
169 while (len--) {
170 byte = priv->tx_buf ? *priv->tx_buf++ : 0;
Jagan Tekic25058c2019-02-27 20:02:08 +0530171 writeb(byte, SPI_REG(priv, SPI_TXD));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200172 }
173}
174
175static void sun4i_spi_set_cs(struct udevice *bus, u8 cs, bool enable)
176{
177 struct sun4i_spi_priv *priv = dev_get_priv(bus);
178 u32 reg;
179
Jagan Tekic25058c2019-02-27 20:02:08 +0530180 reg = readl(SPI_REG(priv, SPI_TCR));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200181
Jagan Tekic25058c2019-02-27 20:02:08 +0530182 reg &= ~SPI_BIT(priv, SPI_TCR_CS_MASK);
183 reg |= SPI_CS(priv, cs);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200184
185 if (enable)
Jagan Tekic25058c2019-02-27 20:02:08 +0530186 reg &= ~SPI_BIT(priv, SPI_TCR_CS_LEVEL);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200187 else
Jagan Tekic25058c2019-02-27 20:02:08 +0530188 reg |= SPI_BIT(priv, SPI_TCR_CS_LEVEL);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200189
Jagan Tekic25058c2019-02-27 20:02:08 +0530190 writel(reg, SPI_REG(priv, SPI_TCR));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200191}
192
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530193static inline int sun4i_spi_set_clock(struct udevice *dev, bool enable)
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200194{
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530195 struct sun4i_spi_priv *priv = dev_get_priv(dev);
196 int ret;
197
198 if (!enable) {
199 clk_disable(&priv->clk_ahb);
200 clk_disable(&priv->clk_mod);
Jagan Tekif69b4252019-02-27 20:02:11 +0530201 if (reset_valid(&priv->reset))
202 reset_assert(&priv->reset);
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530203 return 0;
204 }
205
206 ret = clk_enable(&priv->clk_ahb);
207 if (ret) {
208 dev_err(dev, "failed to enable ahb clock (ret=%d)\n", ret);
209 return ret;
210 }
211
212 ret = clk_enable(&priv->clk_mod);
213 if (ret) {
214 dev_err(dev, "failed to enable mod clock (ret=%d)\n", ret);
215 goto err_ahb;
216 }
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200217
Jagan Tekif69b4252019-02-27 20:02:11 +0530218 if (reset_valid(&priv->reset)) {
219 ret = reset_deassert(&priv->reset);
220 if (ret) {
221 dev_err(dev, "failed to deassert reset\n");
222 goto err_mod;
223 }
224 }
225
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530226 return 0;
227
Jagan Tekif69b4252019-02-27 20:02:11 +0530228err_mod:
229 clk_disable(&priv->clk_mod);
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530230err_ahb:
231 clk_disable(&priv->clk_ahb);
232 return ret;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200233}
234
Andre Przywara62a24e12022-05-03 00:07:16 +0100235static void sun4i_spi_set_speed_mode(struct udevice *dev)
236{
237 struct sun4i_spi_priv *priv = dev_get_priv(dev);
238 unsigned int div;
239 u32 reg;
240
241 /*
242 * Setup clock divider.
243 *
244 * We have two choices there. Either we can use the clock
245 * divide rate 1, which is calculated thanks to this formula:
246 * SPI_CLK = MOD_CLK / (2 ^ (cdr + 1))
247 * Or we can use CDR2, which is calculated with the formula:
248 * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
249 * Whether we use the former or the latter is set through the
250 * DRS bit.
251 *
252 * First try CDR2, and if we can't reach the expected
253 * frequency, fall back to CDR1.
254 */
255
Andre Przywara27835682022-05-03 02:06:37 +0100256 div = DIV_ROUND_UP(SUNXI_INPUT_CLOCK, priv->freq);
Andre Przywara62a24e12022-05-03 00:07:16 +0100257 reg = readl(SPI_REG(priv, SPI_CCR));
258
Andre Przywara27835682022-05-03 02:06:37 +0100259 if ((div / 2) <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
260 div /= 2;
Andre Przywara62a24e12022-05-03 00:07:16 +0100261 if (div > 0)
262 div--;
263
264 reg &= ~(SUN4I_CLK_CTL_CDR2_MASK | SUN4I_CLK_CTL_DRS);
265 reg |= SUN4I_CLK_CTL_CDR2(div) | SUN4I_CLK_CTL_DRS;
266 } else {
Andre Przywara27835682022-05-03 02:06:37 +0100267 div = fls(div - 1);
Andre Przywara4d3521ce2022-04-26 23:58:53 +0100268 /* The F1C100s encodes the divider as 2^(n+1) */
269 if (IS_ENABLED(CONFIG_MACH_SUNIV))
270 div--;
Andre Przywara62a24e12022-05-03 00:07:16 +0100271 reg &= ~((SUN4I_CLK_CTL_CDR1_MASK << 8) | SUN4I_CLK_CTL_DRS);
272 reg |= SUN4I_CLK_CTL_CDR1(div);
273 }
274
275 writel(reg, SPI_REG(priv, SPI_CCR));
276
277 reg = readl(SPI_REG(priv, SPI_TCR));
278 reg &= ~(SPI_BIT(priv, SPI_TCR_CPOL) | SPI_BIT(priv, SPI_TCR_CPHA));
279
280 if (priv->mode & SPI_CPOL)
281 reg |= SPI_BIT(priv, SPI_TCR_CPOL);
282
283 if (priv->mode & SPI_CPHA)
284 reg |= SPI_BIT(priv, SPI_TCR_CPHA);
285
286 writel(reg, SPI_REG(priv, SPI_TCR));
287}
288
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200289static int sun4i_spi_claim_bus(struct udevice *dev)
290{
291 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530292 int ret;
293
294 ret = sun4i_spi_set_clock(dev->parent, true);
295 if (ret)
296 return ret;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200297
Jagan Tekic25058c2019-02-27 20:02:08 +0530298 setbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE |
299 SUN4I_CTL_MASTER | SPI_BIT(priv, SPI_GCR_TP));
300
Jagan Tekif69b4252019-02-27 20:02:11 +0530301 if (priv->variant->has_soft_reset)
302 setbits_le32(SPI_REG(priv, SPI_GCR),
303 SPI_BIT(priv, SPI_GCR_SRST));
304
Jagan Tekic25058c2019-02-27 20:02:08 +0530305 setbits_le32(SPI_REG(priv, SPI_TCR), SPI_BIT(priv, SPI_TCR_CS_MANUAL) |
306 SPI_BIT(priv, SPI_TCR_CS_ACTIVE_LOW));
Jagan Tekif9b70122019-02-27 20:02:07 +0530307
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300308 if (priv->variant->has_clk_ctl) {
309 sun4i_spi_set_speed_mode(dev->parent);
310 } else {
311 /*
312 * At this moment there is no ability to change input clock.
313 * Therefore, we can only use default HOSC@24MHz clock and
314 * set SPI sampling mode to normal
315 */
316 clrsetbits_le32(SPI_REG(priv, SPI_TCR),
317 SPI_BIT(priv, SPI_TCR_SDC) |
318 SPI_BIT(priv, SPI_TCR_SDM),
319 SPI_BIT(priv, SPI_TCR_SDM));
320 }
Andre Przywara62a24e12022-05-03 00:07:16 +0100321
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200322 return 0;
323}
324
325static int sun4i_spi_release_bus(struct udevice *dev)
326{
327 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200328
Jagan Tekic25058c2019-02-27 20:02:08 +0530329 clrbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200330
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530331 sun4i_spi_set_clock(dev->parent, false);
332
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200333 return 0;
334}
335
336static int sun4i_spi_xfer(struct udevice *dev, unsigned int bitlen,
337 const void *dout, void *din, unsigned long flags)
338{
339 struct udevice *bus = dev->parent;
340 struct sun4i_spi_priv *priv = dev_get_priv(bus);
Simon Glassb75b15b2020-12-03 16:55:23 -0700341 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200342
343 u32 len = bitlen / 8;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200344 u8 nbytes;
345 int ret;
346
347 priv->tx_buf = dout;
348 priv->rx_buf = din;
349
350 if (bitlen % 8) {
351 debug("%s: non byte-aligned SPI transfer.\n", __func__);
352 return -ENAVAIL;
353 }
354
355 if (flags & SPI_XFER_BEGIN)
356 sun4i_spi_set_cs(bus, slave_plat->cs, true);
357
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200358 /* Reset FIFOs */
Jagan Tekic25058c2019-02-27 20:02:08 +0530359 setbits_le32(SPI_REG(priv, SPI_FCR), SPI_BIT(priv, SPI_FCR_RF_RST) |
360 SPI_BIT(priv, SPI_FCR_TF_RST));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200361
362 while (len) {
363 /* Setup the transfer now... */
Jagan Tekic12eb6a2019-02-27 20:02:09 +0530364 nbytes = min(len, (priv->variant->fifo_depth - 1));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200365
366 /* Setup the counters */
Jagan Tekic25058c2019-02-27 20:02:08 +0530367 writel(SUN4I_BURST_CNT(nbytes), SPI_REG(priv, SPI_BC));
368 writel(SUN4I_XMIT_CNT(nbytes), SPI_REG(priv, SPI_TC));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200369
Jagan Tekif69b4252019-02-27 20:02:11 +0530370 if (priv->variant->has_burst_ctl)
371 writel(SUN4I_BURST_CNT(nbytes),
372 SPI_REG(priv, SPI_BCTL));
373
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200374 /* Fill the TX FIFO */
375 sun4i_spi_fill_fifo(priv, nbytes);
376
377 /* Start the transfer */
Jagan Tekic25058c2019-02-27 20:02:08 +0530378 setbits_le32(SPI_REG(priv, SPI_TCR),
379 SPI_BIT(priv, SPI_TCR_XCH));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200380
Icenowy Zhenga244be62022-06-28 14:49:24 +0800381 /* Wait for the transfer to be done */
382 ret = wait_for_bit_le32((const void *)SPI_REG(priv, SPI_TCR),
383 SPI_BIT(priv, SPI_TCR_XCH),
384 false, SUN4I_SPI_TIMEOUT_MS, false);
Jagan Teki66220da2019-02-27 20:02:05 +0530385 if (ret < 0) {
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200386 printf("ERROR: sun4i_spi: Timeout transferring data\n");
387 sun4i_spi_set_cs(bus, slave_plat->cs, false);
388 return ret;
389 }
390
391 /* Drain the RX FIFO */
392 sun4i_spi_drain_fifo(priv, nbytes);
393
394 len -= nbytes;
395 }
396
397 if (flags & SPI_XFER_END)
398 sun4i_spi_set_cs(bus, slave_plat->cs, false);
399
400 return 0;
401}
402
403static int sun4i_spi_set_speed(struct udevice *dev, uint speed)
404{
Simon Glassb75b15b2020-12-03 16:55:23 -0700405 struct sun4i_spi_plat *plat = dev_get_plat(dev);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200406 struct sun4i_spi_priv *priv = dev_get_priv(dev);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200407
408 if (speed > plat->max_hz)
409 speed = plat->max_hz;
410
411 if (speed < SUN4I_SPI_MIN_RATE)
412 speed = SUN4I_SPI_MIN_RATE;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200413
414 priv->freq = speed;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200415
416 return 0;
417}
418
419static int sun4i_spi_set_mode(struct udevice *dev, uint mode)
420{
421 struct sun4i_spi_priv *priv = dev_get_priv(dev);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200422
423 priv->mode = mode;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200424
425 return 0;
426}
427
428static const struct dm_spi_ops sun4i_spi_ops = {
429 .claim_bus = sun4i_spi_claim_bus,
430 .release_bus = sun4i_spi_release_bus,
431 .xfer = sun4i_spi_xfer,
432 .set_speed = sun4i_spi_set_speed,
433 .set_mode = sun4i_spi_set_mode,
434};
435
Jagan Teki3f53a582019-02-27 20:02:12 +0530436static int sun4i_spi_probe(struct udevice *bus)
437{
Simon Glassb75b15b2020-12-03 16:55:23 -0700438 struct sun4i_spi_plat *plat = dev_get_plat(bus);
Jagan Teki3f53a582019-02-27 20:02:12 +0530439 struct sun4i_spi_priv *priv = dev_get_priv(bus);
440 int ret;
441
442 ret = clk_get_by_name(bus, "ahb", &priv->clk_ahb);
443 if (ret) {
Sean Anderson64474dd2020-09-15 10:45:11 -0400444 dev_err(bus, "failed to get ahb clock\n");
Jagan Teki3f53a582019-02-27 20:02:12 +0530445 return ret;
446 }
447
448 ret = clk_get_by_name(bus, "mod", &priv->clk_mod);
449 if (ret) {
Sean Anderson64474dd2020-09-15 10:45:11 -0400450 dev_err(bus, "failed to get mod clock\n");
Jagan Teki3f53a582019-02-27 20:02:12 +0530451 return ret;
452 }
453
454 ret = reset_get_by_index(bus, 0, &priv->reset);
455 if (ret && ret != -ENOENT) {
Sean Anderson64474dd2020-09-15 10:45:11 -0400456 dev_err(bus, "failed to get reset\n");
Jagan Teki3f53a582019-02-27 20:02:12 +0530457 return ret;
458 }
459
Jagan Teki3f53a582019-02-27 20:02:12 +0530460 priv->variant = plat->variant;
461 priv->base = plat->base;
462 priv->freq = plat->max_hz;
463
464 return 0;
465}
466
Simon Glassaad29ae2020-12-03 16:55:21 -0700467static int sun4i_spi_of_to_plat(struct udevice *bus)
Jagan Teki3f53a582019-02-27 20:02:12 +0530468{
Simon Glassb75b15b2020-12-03 16:55:23 -0700469 struct sun4i_spi_plat *plat = dev_get_plat(bus);
Jagan Teki3f53a582019-02-27 20:02:12 +0530470 int node = dev_of_offset(bus);
471
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900472 plat->base = dev_read_addr(bus);
Jagan Teki3f53a582019-02-27 20:02:12 +0530473 plat->variant = (struct sun4i_spi_variant *)dev_get_driver_data(bus);
474 plat->max_hz = fdtdec_get_int(gd->fdt_blob, node,
475 "spi-max-frequency",
476 SUN4I_SPI_DEFAULT_RATE);
477
478 if (plat->max_hz > SUN4I_SPI_MAX_RATE)
479 plat->max_hz = SUN4I_SPI_MAX_RATE;
480
481 return 0;
482}
483
Jagan Tekic25058c2019-02-27 20:02:08 +0530484static const unsigned long sun4i_spi_regs[] = {
485 [SPI_GCR] = SUN4I_CTL_REG,
486 [SPI_TCR] = SUN4I_CTL_REG,
487 [SPI_FCR] = SUN4I_CTL_REG,
488 [SPI_FSR] = SUN4I_FIFO_STA_REG,
489 [SPI_CCR] = SUN4I_CLK_CTL_REG,
490 [SPI_BC] = SUN4I_BURST_CNT_REG,
491 [SPI_TC] = SUN4I_XMIT_CNT_REG,
492 [SPI_TXD] = SUN4I_TXDATA_REG,
493 [SPI_RXD] = SUN4I_RXDATA_REG,
494};
495
496static const u32 sun4i_spi_bits[] = {
497 [SPI_GCR_TP] = BIT(18),
498 [SPI_TCR_CPHA] = BIT(2),
499 [SPI_TCR_CPOL] = BIT(3),
500 [SPI_TCR_CS_ACTIVE_LOW] = BIT(4),
501 [SPI_TCR_XCH] = BIT(10),
502 [SPI_TCR_CS_SEL] = 12,
503 [SPI_TCR_CS_MASK] = 0x3000,
504 [SPI_TCR_CS_MANUAL] = BIT(16),
505 [SPI_TCR_CS_LEVEL] = BIT(17),
506 [SPI_FCR_TF_RST] = BIT(8),
507 [SPI_FCR_RF_RST] = BIT(9),
508 [SPI_FSR_RF_CNT_MASK] = GENMASK(6, 0),
509};
510
Jagan Tekif69b4252019-02-27 20:02:11 +0530511static const unsigned long sun6i_spi_regs[] = {
512 [SPI_GCR] = SUN6I_GBL_CTL_REG,
513 [SPI_TCR] = SUN6I_TFR_CTL_REG,
514 [SPI_FCR] = SUN6I_FIFO_CTL_REG,
515 [SPI_FSR] = SUN6I_FIFO_STA_REG,
516 [SPI_CCR] = SUN6I_CLK_CTL_REG,
517 [SPI_BC] = SUN6I_BURST_CNT_REG,
518 [SPI_TC] = SUN6I_XMIT_CNT_REG,
519 [SPI_BCTL] = SUN6I_BURST_CTL_REG,
520 [SPI_TXD] = SUN6I_TXDATA_REG,
521 [SPI_RXD] = SUN6I_RXDATA_REG,
522};
523
524static const u32 sun6i_spi_bits[] = {
525 [SPI_GCR_TP] = BIT(7),
526 [SPI_GCR_SRST] = BIT(31),
527 [SPI_TCR_CPHA] = BIT(0),
528 [SPI_TCR_CPOL] = BIT(1),
529 [SPI_TCR_CS_ACTIVE_LOW] = BIT(2),
530 [SPI_TCR_CS_SEL] = 4,
531 [SPI_TCR_CS_MASK] = 0x30,
532 [SPI_TCR_CS_MANUAL] = BIT(6),
533 [SPI_TCR_CS_LEVEL] = BIT(7),
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300534 [SPI_TCR_SDC] = BIT(11),
535 [SPI_TCR_SDM] = BIT(13),
Jagan Tekif69b4252019-02-27 20:02:11 +0530536 [SPI_TCR_XCH] = BIT(31),
537 [SPI_FCR_RF_RST] = BIT(15),
538 [SPI_FCR_TF_RST] = BIT(31),
539 [SPI_FSR_RF_CNT_MASK] = GENMASK(7, 0),
540};
541
Jagan Tekic25058c2019-02-27 20:02:08 +0530542static const struct sun4i_spi_variant sun4i_a10_spi_variant = {
543 .regs = sun4i_spi_regs,
544 .bits = sun4i_spi_bits,
Jagan Tekic12eb6a2019-02-27 20:02:09 +0530545 .fifo_depth = 64,
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300546 .has_clk_ctl = true,
Jagan Tekif69b4252019-02-27 20:02:11 +0530547};
548
549static const struct sun4i_spi_variant sun6i_a31_spi_variant = {
550 .regs = sun6i_spi_regs,
551 .bits = sun6i_spi_bits,
552 .fifo_depth = 128,
553 .has_soft_reset = true,
554 .has_burst_ctl = true,
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300555 .has_clk_ctl = true,
Jagan Tekif69b4252019-02-27 20:02:11 +0530556};
557
558static const struct sun4i_spi_variant sun8i_h3_spi_variant = {
559 .regs = sun6i_spi_regs,
560 .bits = sun6i_spi_bits,
561 .fifo_depth = 64,
562 .has_soft_reset = true,
563 .has_burst_ctl = true,
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300564 .has_clk_ctl = true,
Jagan Tekic25058c2019-02-27 20:02:08 +0530565};
566
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300567static const struct sun4i_spi_variant sun50i_r329_spi_variant = {
568 .regs = sun6i_spi_regs,
569 .bits = sun6i_spi_bits,
570 .fifo_depth = 64,
571 .has_soft_reset = true,
572 .has_burst_ctl = true,
573};
574
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200575static const struct udevice_id sun4i_spi_ids[] = {
Jagan Tekic25058c2019-02-27 20:02:08 +0530576 {
577 .compatible = "allwinner,sun4i-a10-spi",
578 .data = (ulong)&sun4i_a10_spi_variant,
579 },
Jagan Tekif69b4252019-02-27 20:02:11 +0530580 {
581 .compatible = "allwinner,sun6i-a31-spi",
582 .data = (ulong)&sun6i_a31_spi_variant,
583 },
584 {
585 .compatible = "allwinner,sun8i-h3-spi",
586 .data = (ulong)&sun8i_h3_spi_variant,
587 },
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300588 {
589 .compatible = "allwinner,sun50i-r329-spi",
590 .data = (ulong)&sun50i_r329_spi_variant,
591 },
Jagan Teki3f53a582019-02-27 20:02:12 +0530592 { /* sentinel */ }
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200593};
594
595U_BOOT_DRIVER(sun4i_spi) = {
596 .name = "sun4i_spi",
597 .id = UCLASS_SPI,
598 .of_match = sun4i_spi_ids,
599 .ops = &sun4i_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700600 .of_to_plat = sun4i_spi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700601 .plat_auto = sizeof(struct sun4i_spi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700602 .priv_auto = sizeof(struct sun4i_spi_priv),
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200603 .probe = sun4i_spi_probe,
604};