blob: 13725ee7a2dca6c014700b6e003406848308dadd [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
Jagan Teki97b3d5a2019-02-27 20:02:10 +053021#include <clk.h>
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020022#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060023#include <log.h>
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020024#include <spi.h>
25#include <errno.h>
26#include <fdt_support.h>
Jagan Tekif69b4252019-02-27 20:02:11 +053027#include <reset.h>
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020028#include <wait_bit.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060029#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -070030#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060031#include <linux/bitops.h>
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020032
33#include <asm/bitops.h>
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020034#include <asm/io.h>
35
Jagan Teki66220da2019-02-27 20:02:05 +053036#include <linux/iopoll.h>
37
Jagan Teki3f53a582019-02-27 20:02:12 +053038DECLARE_GLOBAL_DATA_PTR;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020039
Jagan Teki3f53a582019-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 Mavrodiev5d716042018-02-06 15:14:33 +020048
Jagan Tekif69b4252019-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 Teki3f53a582019-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
73
Andre Przywara4d3521ce2022-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 Przywara27835682022-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 Przywara4d3521ce2022-04-26 23:58:53 +010082#endif
Jagan Teki3f53a582019-02-27 20:02:12 +053083#define SUN4I_SPI_MIN_RATE 3000
84#define SUN4I_SPI_DEFAULT_RATE 1000000
Icenowy Zhenga244be62022-06-28 14:49:24 +080085#define SUN4I_SPI_TIMEOUT_MS 1000
Stefan Mavrodiev5d716042018-02-06 15:14:33 +020086
Jagan Teki3f53a582019-02-27 20:02:12 +053087#define SPI_REG(priv, reg) ((priv)->base + \
Jagan Tekic25058c2019-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 Tekif69b4252019-02-27 20:02:11 +0530110 SPI_GCR_SRST,
Jagan Tekic25058c2019-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 Kiselev4d9267e2023-11-11 16:33:08 +0300119 SPI_TCR_SDC,
120 SPI_TCR_SDM,
Jagan Tekic25058c2019-02-27 20:02:08 +0530121 SPI_FCR_TF_RST,
122 SPI_FCR_RF_RST,
123 SPI_FSR_RF_CNT_MASK,
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200124};
125
Jagan Tekic25058c2019-02-27 20:02:08 +0530126struct sun4i_spi_variant {
127 const unsigned long *regs;
128 const u32 *bits;
Jagan Tekic12eb6a2019-02-27 20:02:09 +0530129 u32 fifo_depth;
Jagan Tekif69b4252019-02-27 20:02:11 +0530130 bool has_soft_reset;
131 bool has_burst_ctl;
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300132 bool has_clk_ctl;
Jagan Tekic25058c2019-02-27 20:02:08 +0530133};
134
Simon Glassb75b15b2020-12-03 16:55:23 -0700135struct sun4i_spi_plat {
Jagan Tekic25058c2019-02-27 20:02:08 +0530136 struct sun4i_spi_variant *variant;
Jagan Teki3f53a582019-02-27 20:02:12 +0530137 u32 base;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200138 u32 max_hz;
139};
140
141struct sun4i_spi_priv {
Jagan Tekic25058c2019-02-27 20:02:08 +0530142 struct sun4i_spi_variant *variant;
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530143 struct clk clk_ahb, clk_mod;
Jagan Tekif69b4252019-02-27 20:02:11 +0530144 struct reset_ctl reset;
Jagan Teki3f53a582019-02-27 20:02:12 +0530145 u32 base;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200146 u32 freq;
147 u32 mode;
148
149 const u8 *tx_buf;
150 u8 *rx_buf;
151};
152
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200153static inline void sun4i_spi_drain_fifo(struct sun4i_spi_priv *priv, int len)
154{
155 u8 byte;
156
157 while (len--) {
Jagan Tekic25058c2019-02-27 20:02:08 +0530158 byte = readb(SPI_REG(priv, SPI_RXD));
Stefan Mavrodiev165db622018-12-05 14:27:57 +0200159 if (priv->rx_buf)
160 *priv->rx_buf++ = byte;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200161 }
162}
163
164static inline void sun4i_spi_fill_fifo(struct sun4i_spi_priv *priv, int len)
165{
166 u8 byte;
167
168 while (len--) {
169 byte = priv->tx_buf ? *priv->tx_buf++ : 0;
Jagan Tekic25058c2019-02-27 20:02:08 +0530170 writeb(byte, SPI_REG(priv, SPI_TXD));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200171 }
172}
173
174static void sun4i_spi_set_cs(struct udevice *bus, u8 cs, bool enable)
175{
176 struct sun4i_spi_priv *priv = dev_get_priv(bus);
177 u32 reg;
178
Jagan Tekic25058c2019-02-27 20:02:08 +0530179 reg = readl(SPI_REG(priv, SPI_TCR));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200180
Jagan Tekic25058c2019-02-27 20:02:08 +0530181 reg &= ~SPI_BIT(priv, SPI_TCR_CS_MASK);
182 reg |= SPI_CS(priv, cs);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200183
184 if (enable)
Jagan Tekic25058c2019-02-27 20:02:08 +0530185 reg &= ~SPI_BIT(priv, SPI_TCR_CS_LEVEL);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200186 else
Jagan Tekic25058c2019-02-27 20:02:08 +0530187 reg |= SPI_BIT(priv, SPI_TCR_CS_LEVEL);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200188
Jagan Tekic25058c2019-02-27 20:02:08 +0530189 writel(reg, SPI_REG(priv, SPI_TCR));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200190}
191
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530192static inline int sun4i_spi_set_clock(struct udevice *dev, bool enable)
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200193{
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530194 struct sun4i_spi_priv *priv = dev_get_priv(dev);
195 int ret;
196
197 if (!enable) {
198 clk_disable(&priv->clk_ahb);
199 clk_disable(&priv->clk_mod);
Jagan Tekif69b4252019-02-27 20:02:11 +0530200 if (reset_valid(&priv->reset))
201 reset_assert(&priv->reset);
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530202 return 0;
203 }
204
205 ret = clk_enable(&priv->clk_ahb);
206 if (ret) {
207 dev_err(dev, "failed to enable ahb clock (ret=%d)\n", ret);
208 return ret;
209 }
210
211 ret = clk_enable(&priv->clk_mod);
212 if (ret) {
213 dev_err(dev, "failed to enable mod clock (ret=%d)\n", ret);
214 goto err_ahb;
215 }
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200216
Jagan Tekif69b4252019-02-27 20:02:11 +0530217 if (reset_valid(&priv->reset)) {
218 ret = reset_deassert(&priv->reset);
219 if (ret) {
220 dev_err(dev, "failed to deassert reset\n");
221 goto err_mod;
222 }
223 }
224
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530225 return 0;
226
Jagan Tekif69b4252019-02-27 20:02:11 +0530227err_mod:
228 clk_disable(&priv->clk_mod);
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530229err_ahb:
230 clk_disable(&priv->clk_ahb);
231 return ret;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200232}
233
Andre Przywara62a24e12022-05-03 00:07:16 +0100234static void sun4i_spi_set_speed_mode(struct udevice *dev)
235{
236 struct sun4i_spi_priv *priv = dev_get_priv(dev);
237 unsigned int div;
238 u32 reg;
239
240 /*
241 * Setup clock divider.
242 *
243 * We have two choices there. Either we can use the clock
244 * divide rate 1, which is calculated thanks to this formula:
245 * SPI_CLK = MOD_CLK / (2 ^ (cdr + 1))
246 * Or we can use CDR2, which is calculated with the formula:
247 * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
248 * Whether we use the former or the latter is set through the
249 * DRS bit.
250 *
251 * First try CDR2, and if we can't reach the expected
252 * frequency, fall back to CDR1.
253 */
254
Andre Przywara27835682022-05-03 02:06:37 +0100255 div = DIV_ROUND_UP(SUNXI_INPUT_CLOCK, priv->freq);
Andre Przywara62a24e12022-05-03 00:07:16 +0100256 reg = readl(SPI_REG(priv, SPI_CCR));
257
Andre Przywara27835682022-05-03 02:06:37 +0100258 if ((div / 2) <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
259 div /= 2;
Andre Przywara62a24e12022-05-03 00:07:16 +0100260 if (div > 0)
261 div--;
262
263 reg &= ~(SUN4I_CLK_CTL_CDR2_MASK | SUN4I_CLK_CTL_DRS);
264 reg |= SUN4I_CLK_CTL_CDR2(div) | SUN4I_CLK_CTL_DRS;
265 } else {
Andre Przywara27835682022-05-03 02:06:37 +0100266 div = fls(div - 1);
Andre Przywara4d3521ce2022-04-26 23:58:53 +0100267 /* The F1C100s encodes the divider as 2^(n+1) */
268 if (IS_ENABLED(CONFIG_MACH_SUNIV))
269 div--;
Andre Przywara62a24e12022-05-03 00:07:16 +0100270 reg &= ~((SUN4I_CLK_CTL_CDR1_MASK << 8) | SUN4I_CLK_CTL_DRS);
271 reg |= SUN4I_CLK_CTL_CDR1(div);
272 }
273
274 writel(reg, SPI_REG(priv, SPI_CCR));
275
276 reg = readl(SPI_REG(priv, SPI_TCR));
277 reg &= ~(SPI_BIT(priv, SPI_TCR_CPOL) | SPI_BIT(priv, SPI_TCR_CPHA));
278
279 if (priv->mode & SPI_CPOL)
280 reg |= SPI_BIT(priv, SPI_TCR_CPOL);
281
282 if (priv->mode & SPI_CPHA)
283 reg |= SPI_BIT(priv, SPI_TCR_CPHA);
284
285 writel(reg, SPI_REG(priv, SPI_TCR));
286}
287
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200288static int sun4i_spi_claim_bus(struct udevice *dev)
289{
290 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530291 int ret;
292
293 ret = sun4i_spi_set_clock(dev->parent, true);
294 if (ret)
295 return ret;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200296
Jagan Tekic25058c2019-02-27 20:02:08 +0530297 setbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE |
298 SUN4I_CTL_MASTER | SPI_BIT(priv, SPI_GCR_TP));
299
Jagan Tekif69b4252019-02-27 20:02:11 +0530300 if (priv->variant->has_soft_reset)
301 setbits_le32(SPI_REG(priv, SPI_GCR),
302 SPI_BIT(priv, SPI_GCR_SRST));
303
Jagan Tekic25058c2019-02-27 20:02:08 +0530304 setbits_le32(SPI_REG(priv, SPI_TCR), SPI_BIT(priv, SPI_TCR_CS_MANUAL) |
305 SPI_BIT(priv, SPI_TCR_CS_ACTIVE_LOW));
Jagan Tekif9b70122019-02-27 20:02:07 +0530306
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300307 if (priv->variant->has_clk_ctl) {
308 sun4i_spi_set_speed_mode(dev->parent);
309 } else {
310 /*
311 * At this moment there is no ability to change input clock.
312 * Therefore, we can only use default HOSC@24MHz clock and
313 * set SPI sampling mode to normal
314 */
315 clrsetbits_le32(SPI_REG(priv, SPI_TCR),
316 SPI_BIT(priv, SPI_TCR_SDC) |
317 SPI_BIT(priv, SPI_TCR_SDM),
318 SPI_BIT(priv, SPI_TCR_SDM));
319 }
Andre Przywara62a24e12022-05-03 00:07:16 +0100320
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200321 return 0;
322}
323
324static int sun4i_spi_release_bus(struct udevice *dev)
325{
326 struct sun4i_spi_priv *priv = dev_get_priv(dev->parent);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200327
Jagan Tekic25058c2019-02-27 20:02:08 +0530328 clrbits_le32(SPI_REG(priv, SPI_GCR), SUN4I_CTL_ENABLE);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200329
Jagan Teki97b3d5a2019-02-27 20:02:10 +0530330 sun4i_spi_set_clock(dev->parent, false);
331
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200332 return 0;
333}
334
335static int sun4i_spi_xfer(struct udevice *dev, unsigned int bitlen,
336 const void *dout, void *din, unsigned long flags)
337{
338 struct udevice *bus = dev->parent;
339 struct sun4i_spi_priv *priv = dev_get_priv(bus);
Simon Glassb75b15b2020-12-03 16:55:23 -0700340 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200341
342 u32 len = bitlen / 8;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200343 u8 nbytes;
344 int ret;
345
346 priv->tx_buf = dout;
347 priv->rx_buf = din;
348
349 if (bitlen % 8) {
350 debug("%s: non byte-aligned SPI transfer.\n", __func__);
351 return -ENAVAIL;
352 }
353
354 if (flags & SPI_XFER_BEGIN)
355 sun4i_spi_set_cs(bus, slave_plat->cs, true);
356
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200357 /* Reset FIFOs */
Jagan Tekic25058c2019-02-27 20:02:08 +0530358 setbits_le32(SPI_REG(priv, SPI_FCR), SPI_BIT(priv, SPI_FCR_RF_RST) |
359 SPI_BIT(priv, SPI_FCR_TF_RST));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200360
361 while (len) {
362 /* Setup the transfer now... */
Jagan Tekic12eb6a2019-02-27 20:02:09 +0530363 nbytes = min(len, (priv->variant->fifo_depth - 1));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200364
365 /* Setup the counters */
Jagan Tekic25058c2019-02-27 20:02:08 +0530366 writel(SUN4I_BURST_CNT(nbytes), SPI_REG(priv, SPI_BC));
367 writel(SUN4I_XMIT_CNT(nbytes), SPI_REG(priv, SPI_TC));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200368
Jagan Tekif69b4252019-02-27 20:02:11 +0530369 if (priv->variant->has_burst_ctl)
370 writel(SUN4I_BURST_CNT(nbytes),
371 SPI_REG(priv, SPI_BCTL));
372
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200373 /* Fill the TX FIFO */
374 sun4i_spi_fill_fifo(priv, nbytes);
375
376 /* Start the transfer */
Jagan Tekic25058c2019-02-27 20:02:08 +0530377 setbits_le32(SPI_REG(priv, SPI_TCR),
378 SPI_BIT(priv, SPI_TCR_XCH));
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200379
Icenowy Zhenga244be62022-06-28 14:49:24 +0800380 /* Wait for the transfer to be done */
381 ret = wait_for_bit_le32((const void *)SPI_REG(priv, SPI_TCR),
382 SPI_BIT(priv, SPI_TCR_XCH),
383 false, SUN4I_SPI_TIMEOUT_MS, false);
Jagan Teki66220da2019-02-27 20:02:05 +0530384 if (ret < 0) {
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200385 printf("ERROR: sun4i_spi: Timeout transferring data\n");
386 sun4i_spi_set_cs(bus, slave_plat->cs, false);
387 return ret;
388 }
389
390 /* Drain the RX FIFO */
391 sun4i_spi_drain_fifo(priv, nbytes);
392
393 len -= nbytes;
394 }
395
396 if (flags & SPI_XFER_END)
397 sun4i_spi_set_cs(bus, slave_plat->cs, false);
398
399 return 0;
400}
401
402static int sun4i_spi_set_speed(struct udevice *dev, uint speed)
403{
Simon Glassb75b15b2020-12-03 16:55:23 -0700404 struct sun4i_spi_plat *plat = dev_get_plat(dev);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200405 struct sun4i_spi_priv *priv = dev_get_priv(dev);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200406
407 if (speed > plat->max_hz)
408 speed = plat->max_hz;
409
410 if (speed < SUN4I_SPI_MIN_RATE)
411 speed = SUN4I_SPI_MIN_RATE;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200412
413 priv->freq = speed;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200414
415 return 0;
416}
417
418static int sun4i_spi_set_mode(struct udevice *dev, uint mode)
419{
420 struct sun4i_spi_priv *priv = dev_get_priv(dev);
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200421
422 priv->mode = mode;
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200423
424 return 0;
425}
426
427static const struct dm_spi_ops sun4i_spi_ops = {
428 .claim_bus = sun4i_spi_claim_bus,
429 .release_bus = sun4i_spi_release_bus,
430 .xfer = sun4i_spi_xfer,
431 .set_speed = sun4i_spi_set_speed,
432 .set_mode = sun4i_spi_set_mode,
433};
434
Jagan Teki3f53a582019-02-27 20:02:12 +0530435static int sun4i_spi_probe(struct udevice *bus)
436{
Simon Glassb75b15b2020-12-03 16:55:23 -0700437 struct sun4i_spi_plat *plat = dev_get_plat(bus);
Jagan Teki3f53a582019-02-27 20:02:12 +0530438 struct sun4i_spi_priv *priv = dev_get_priv(bus);
439 int ret;
440
441 ret = clk_get_by_name(bus, "ahb", &priv->clk_ahb);
442 if (ret) {
Sean Anderson64474dd2020-09-15 10:45:11 -0400443 dev_err(bus, "failed to get ahb clock\n");
Jagan Teki3f53a582019-02-27 20:02:12 +0530444 return ret;
445 }
446
447 ret = clk_get_by_name(bus, "mod", &priv->clk_mod);
448 if (ret) {
Sean Anderson64474dd2020-09-15 10:45:11 -0400449 dev_err(bus, "failed to get mod clock\n");
Jagan Teki3f53a582019-02-27 20:02:12 +0530450 return ret;
451 }
452
453 ret = reset_get_by_index(bus, 0, &priv->reset);
454 if (ret && ret != -ENOENT) {
Sean Anderson64474dd2020-09-15 10:45:11 -0400455 dev_err(bus, "failed to get reset\n");
Jagan Teki3f53a582019-02-27 20:02:12 +0530456 return ret;
457 }
458
Jagan Teki3f53a582019-02-27 20:02:12 +0530459 priv->variant = plat->variant;
460 priv->base = plat->base;
461 priv->freq = plat->max_hz;
462
463 return 0;
464}
465
Simon Glassaad29ae2020-12-03 16:55:21 -0700466static int sun4i_spi_of_to_plat(struct udevice *bus)
Jagan Teki3f53a582019-02-27 20:02:12 +0530467{
Simon Glassb75b15b2020-12-03 16:55:23 -0700468 struct sun4i_spi_plat *plat = dev_get_plat(bus);
Jagan Teki3f53a582019-02-27 20:02:12 +0530469 int node = dev_of_offset(bus);
470
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900471 plat->base = dev_read_addr(bus);
Jagan Teki3f53a582019-02-27 20:02:12 +0530472 plat->variant = (struct sun4i_spi_variant *)dev_get_driver_data(bus);
473 plat->max_hz = fdtdec_get_int(gd->fdt_blob, node,
474 "spi-max-frequency",
475 SUN4I_SPI_DEFAULT_RATE);
476
477 if (plat->max_hz > SUN4I_SPI_MAX_RATE)
478 plat->max_hz = SUN4I_SPI_MAX_RATE;
479
480 return 0;
481}
482
Jagan Tekic25058c2019-02-27 20:02:08 +0530483static const unsigned long sun4i_spi_regs[] = {
484 [SPI_GCR] = SUN4I_CTL_REG,
485 [SPI_TCR] = SUN4I_CTL_REG,
486 [SPI_FCR] = SUN4I_CTL_REG,
487 [SPI_FSR] = SUN4I_FIFO_STA_REG,
488 [SPI_CCR] = SUN4I_CLK_CTL_REG,
489 [SPI_BC] = SUN4I_BURST_CNT_REG,
490 [SPI_TC] = SUN4I_XMIT_CNT_REG,
491 [SPI_TXD] = SUN4I_TXDATA_REG,
492 [SPI_RXD] = SUN4I_RXDATA_REG,
493};
494
495static const u32 sun4i_spi_bits[] = {
496 [SPI_GCR_TP] = BIT(18),
497 [SPI_TCR_CPHA] = BIT(2),
498 [SPI_TCR_CPOL] = BIT(3),
499 [SPI_TCR_CS_ACTIVE_LOW] = BIT(4),
500 [SPI_TCR_XCH] = BIT(10),
501 [SPI_TCR_CS_SEL] = 12,
502 [SPI_TCR_CS_MASK] = 0x3000,
503 [SPI_TCR_CS_MANUAL] = BIT(16),
504 [SPI_TCR_CS_LEVEL] = BIT(17),
505 [SPI_FCR_TF_RST] = BIT(8),
506 [SPI_FCR_RF_RST] = BIT(9),
507 [SPI_FSR_RF_CNT_MASK] = GENMASK(6, 0),
508};
509
Jagan Tekif69b4252019-02-27 20:02:11 +0530510static const unsigned long sun6i_spi_regs[] = {
511 [SPI_GCR] = SUN6I_GBL_CTL_REG,
512 [SPI_TCR] = SUN6I_TFR_CTL_REG,
513 [SPI_FCR] = SUN6I_FIFO_CTL_REG,
514 [SPI_FSR] = SUN6I_FIFO_STA_REG,
515 [SPI_CCR] = SUN6I_CLK_CTL_REG,
516 [SPI_BC] = SUN6I_BURST_CNT_REG,
517 [SPI_TC] = SUN6I_XMIT_CNT_REG,
518 [SPI_BCTL] = SUN6I_BURST_CTL_REG,
519 [SPI_TXD] = SUN6I_TXDATA_REG,
520 [SPI_RXD] = SUN6I_RXDATA_REG,
521};
522
523static const u32 sun6i_spi_bits[] = {
524 [SPI_GCR_TP] = BIT(7),
525 [SPI_GCR_SRST] = BIT(31),
526 [SPI_TCR_CPHA] = BIT(0),
527 [SPI_TCR_CPOL] = BIT(1),
528 [SPI_TCR_CS_ACTIVE_LOW] = BIT(2),
529 [SPI_TCR_CS_SEL] = 4,
530 [SPI_TCR_CS_MASK] = 0x30,
531 [SPI_TCR_CS_MANUAL] = BIT(6),
532 [SPI_TCR_CS_LEVEL] = BIT(7),
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300533 [SPI_TCR_SDC] = BIT(11),
534 [SPI_TCR_SDM] = BIT(13),
Jagan Tekif69b4252019-02-27 20:02:11 +0530535 [SPI_TCR_XCH] = BIT(31),
536 [SPI_FCR_RF_RST] = BIT(15),
537 [SPI_FCR_TF_RST] = BIT(31),
538 [SPI_FSR_RF_CNT_MASK] = GENMASK(7, 0),
539};
540
Jagan Tekic25058c2019-02-27 20:02:08 +0530541static const struct sun4i_spi_variant sun4i_a10_spi_variant = {
542 .regs = sun4i_spi_regs,
543 .bits = sun4i_spi_bits,
Jagan Tekic12eb6a2019-02-27 20:02:09 +0530544 .fifo_depth = 64,
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300545 .has_clk_ctl = true,
Jagan Tekif69b4252019-02-27 20:02:11 +0530546};
547
548static const struct sun4i_spi_variant sun6i_a31_spi_variant = {
549 .regs = sun6i_spi_regs,
550 .bits = sun6i_spi_bits,
551 .fifo_depth = 128,
552 .has_soft_reset = true,
553 .has_burst_ctl = true,
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300554 .has_clk_ctl = true,
Jagan Tekif69b4252019-02-27 20:02:11 +0530555};
556
557static const struct sun4i_spi_variant sun8i_h3_spi_variant = {
558 .regs = sun6i_spi_regs,
559 .bits = sun6i_spi_bits,
560 .fifo_depth = 64,
561 .has_soft_reset = true,
562 .has_burst_ctl = true,
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300563 .has_clk_ctl = true,
Jagan Tekic25058c2019-02-27 20:02:08 +0530564};
565
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300566static const struct sun4i_spi_variant sun50i_r329_spi_variant = {
567 .regs = sun6i_spi_regs,
568 .bits = sun6i_spi_bits,
569 .fifo_depth = 64,
570 .has_soft_reset = true,
571 .has_burst_ctl = true,
572};
573
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200574static const struct udevice_id sun4i_spi_ids[] = {
Jagan Tekic25058c2019-02-27 20:02:08 +0530575 {
576 .compatible = "allwinner,sun4i-a10-spi",
577 .data = (ulong)&sun4i_a10_spi_variant,
578 },
Jagan Tekif69b4252019-02-27 20:02:11 +0530579 {
580 .compatible = "allwinner,sun6i-a31-spi",
581 .data = (ulong)&sun6i_a31_spi_variant,
582 },
583 {
584 .compatible = "allwinner,sun8i-h3-spi",
585 .data = (ulong)&sun8i_h3_spi_variant,
586 },
Maksim Kiselev4d9267e2023-11-11 16:33:08 +0300587 {
588 .compatible = "allwinner,sun50i-r329-spi",
589 .data = (ulong)&sun50i_r329_spi_variant,
590 },
Jagan Teki3f53a582019-02-27 20:02:12 +0530591 { /* sentinel */ }
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200592};
593
594U_BOOT_DRIVER(sun4i_spi) = {
595 .name = "sun4i_spi",
596 .id = UCLASS_SPI,
597 .of_match = sun4i_spi_ids,
598 .ops = &sun4i_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700599 .of_to_plat = sun4i_spi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700600 .plat_auto = sizeof(struct sun4i_spi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700601 .priv_auto = sizeof(struct sun4i_spi_priv),
Stefan Mavrodiev5d716042018-02-06 15:14:33 +0200602 .probe = sun4i_spi_probe,
603};