blob: b520c727900d847dbbc01c214e9d703c428b58b0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stefan Roesed987ea62014-11-07 13:50:31 +01002/*
3 * Designware master SPI core controller driver
4 *
5 * Copyright (C) 2014 Stefan Roese <sr@denx.de>
Sean Andersondebbff82020-10-16 18:57:51 -04006 * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
Stefan Roesed987ea62014-11-07 13:50:31 +01007 *
Stefan Roese571e2a42014-11-16 12:47:01 +01008 * Very loosely based on the Linux driver:
9 * drivers/spi/spi-dw.c, which is:
Stefan Roesed987ea62014-11-07 13:50:31 +010010 * Copyright (c) 2009, Intel Corporation.
Stefan Roesed987ea62014-11-07 13:50:31 +010011 */
12
Sean Anderson0dfb3ac2020-10-16 18:57:44 -040013#define LOG_CATEGORY UCLASS_SPI
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +030014#include <clk.h>
Stefan Roesed987ea62014-11-07 13:50:31 +010015#include <dm.h>
Sean Anderson16edba32020-10-16 18:57:53 -040016#include <dm/device_compat.h>
Stefan Roesed987ea62014-11-07 13:50:31 +010017#include <errno.h>
Stefan Roesed987ea62014-11-07 13:50:31 +010018#include <fdtdec.h>
Sean Anderson16edba32020-10-16 18:57:53 -040019#include <log.h>
20#include <malloc.h>
Ley Foon Tanfc3382d2018-09-07 14:25:29 +080021#include <reset.h>
Sean Anderson16edba32020-10-16 18:57:53 -040022#include <spi.h>
23#include <spi-mem.h>
24#include <asm/io.h>
25#include <asm-generic/gpio.h>
Sean Andersondebbff82020-10-16 18:57:51 -040026#include <linux/bitfield.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060027#include <linux/bitops.h>
Stefan Roesed987ea62014-11-07 13:50:31 +010028#include <linux/compat.h>
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +030029#include <linux/iopoll.h>
Sean Anderson16edba32020-10-16 18:57:53 -040030#include <linux/sizes.h>
Stefan Roesed987ea62014-11-07 13:50:31 +010031
Stefan Roesed987ea62014-11-07 13:50:31 +010032/* Register offsets */
Sean Anderson75ea2f62020-10-16 18:57:47 -040033#define DW_SPI_CTRLR0 0x00
34#define DW_SPI_CTRLR1 0x04
Stefan Roesed987ea62014-11-07 13:50:31 +010035#define DW_SPI_SSIENR 0x08
36#define DW_SPI_MWCR 0x0c
37#define DW_SPI_SER 0x10
38#define DW_SPI_BAUDR 0x14
Sean Anderson75ea2f62020-10-16 18:57:47 -040039#define DW_SPI_TXFTLR 0x18
40#define DW_SPI_RXFTLR 0x1c
Stefan Roesed987ea62014-11-07 13:50:31 +010041#define DW_SPI_TXFLR 0x20
42#define DW_SPI_RXFLR 0x24
43#define DW_SPI_SR 0x28
44#define DW_SPI_IMR 0x2c
45#define DW_SPI_ISR 0x30
46#define DW_SPI_RISR 0x34
47#define DW_SPI_TXOICR 0x38
48#define DW_SPI_RXOICR 0x3c
49#define DW_SPI_RXUICR 0x40
50#define DW_SPI_MSTICR 0x44
51#define DW_SPI_ICR 0x48
52#define DW_SPI_DMACR 0x4c
53#define DW_SPI_DMATDLR 0x50
54#define DW_SPI_DMARDLR 0x54
55#define DW_SPI_IDR 0x58
56#define DW_SPI_VERSION 0x5c
57#define DW_SPI_DR 0x60
58
59/* Bit fields in CTRLR0 */
Sean Andersondebbff82020-10-16 18:57:51 -040060/*
61 * Only present when SSI_MAX_XFER_SIZE=16. This is the default, and the only
62 * option before version 3.23a.
63 */
64#define CTRLR0_DFS_MASK GENMASK(3, 0)
65
66#define CTRLR0_FRF_MASK GENMASK(5, 4)
67#define CTRLR0_FRF_SPI 0x0
68#define CTRLR0_FRF_SSP 0x1
69#define CTRLR0_FRF_MICROWIRE 0x2
70#define CTRLR0_FRF_RESV 0x3
Stefan Roesed987ea62014-11-07 13:50:31 +010071
Sean Andersondebbff82020-10-16 18:57:51 -040072#define CTRLR0_MODE_MASK GENMASK(7, 6)
73#define CTRLR0_MODE_SCPH 0x1
74#define CTRLR0_MODE_SCPOL 0x2
Stefan Roesed987ea62014-11-07 13:50:31 +010075
Sean Andersondebbff82020-10-16 18:57:51 -040076#define CTRLR0_TMOD_MASK GENMASK(9, 8)
77#define CTRLR0_TMOD_TR 0x0 /* xmit & recv */
78#define CTRLR0_TMOD_TO 0x1 /* xmit only */
79#define CTRLR0_TMOD_RO 0x2 /* recv only */
80#define CTRLR0_TMOD_EPROMREAD 0x3 /* eeprom read mode */
Stefan Roesed987ea62014-11-07 13:50:31 +010081
Sean Andersondebbff82020-10-16 18:57:51 -040082#define CTRLR0_SLVOE_OFFSET 10
83#define CTRLR0_SRL_OFFSET 11
84#define CTRLR0_CFS_MASK GENMASK(15, 12)
Stefan Roesed987ea62014-11-07 13:50:31 +010085
Sean Andersondebbff82020-10-16 18:57:51 -040086/* Only present when SSI_MAX_XFER_SIZE=32 */
87#define CTRLR0_DFS_32_MASK GENMASK(20, 16)
Stefan Roesed987ea62014-11-07 13:50:31 +010088
Sean Andersondebbff82020-10-16 18:57:51 -040089/* The next field is only present on versions after 4.00a */
90#define CTRLR0_SPI_FRF_MASK GENMASK(22, 21)
91#define CTRLR0_SPI_FRF_BYTE 0x0
92#define CTRLR0_SPI_FRF_DUAL 0x1
93#define CTRLR0_SPI_FRF_QUAD 0x2
Stefan Roesed987ea62014-11-07 13:50:31 +010094
Sean Andersondebbff82020-10-16 18:57:51 -040095/* Bit fields in CTRLR0 based on DWC_ssi_databook.pdf v1.01a */
96#define DWC_SSI_CTRLR0_DFS_MASK GENMASK(4, 0)
97#define DWC_SSI_CTRLR0_FRF_MASK GENMASK(7, 6)
98#define DWC_SSI_CTRLR0_MODE_MASK GENMASK(9, 8)
99#define DWC_SSI_CTRLR0_TMOD_MASK GENMASK(11, 10)
100#define DWC_SSI_CTRLR0_SRL_OFFSET 13
101#define DWC_SSI_CTRLR0_SPI_FRF_MASK GENMASK(23, 22)
Stefan Roesed987ea62014-11-07 13:50:31 +0100102
103/* Bit fields in SR, 7 bits */
Jagan Tekifac44912015-10-23 01:01:36 +0530104#define SR_MASK GENMASK(6, 0) /* cover 7 bits */
Jagan Tekib17746d2015-10-23 01:36:23 +0530105#define SR_BUSY BIT(0)
106#define SR_TF_NOT_FULL BIT(1)
107#define SR_TF_EMPT BIT(2)
108#define SR_RF_NOT_EMPT BIT(3)
109#define SR_RF_FULL BIT(4)
110#define SR_TX_ERR BIT(5)
111#define SR_DCOL BIT(6)
Stefan Roesed987ea62014-11-07 13:50:31 +0100112
Maksim Kiselev6cf56182023-12-21 13:13:30 +0300113/* Bit field in RISR */
114#define RISR_INT_RXOI BIT(3)
115
Stefan Roese571e2a42014-11-16 12:47:01 +0100116#define RX_TIMEOUT 1000 /* timeout in ms */
Stefan Roesed987ea62014-11-07 13:50:31 +0100117
Simon Glassb75b15b2020-12-03 16:55:23 -0700118struct dw_spi_plat {
Stefan Roesed987ea62014-11-07 13:50:31 +0100119 s32 frequency; /* Default clock frequency, -1 for none */
120 void __iomem *regs;
121};
122
123struct dw_spi_priv {
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300124 struct clk clk;
Sean Andersone11c0b12020-10-16 18:57:49 -0400125 struct reset_ctl_bulk resets;
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300126 struct gpio_desc cs_gpio; /* External chip-select gpio */
127
Sean Andersondebbff82020-10-16 18:57:51 -0400128 u32 (*update_cr0)(struct dw_spi_priv *priv);
Stefan Roesed987ea62014-11-07 13:50:31 +0100129
Sean Andersone11c0b12020-10-16 18:57:49 -0400130 void __iomem *regs;
131 unsigned long bus_clk_rate;
132 unsigned int freq; /* Default frequency */
133 unsigned int mode;
Stefan Roesed987ea62014-11-07 13:50:31 +0100134
Sean Andersone11c0b12020-10-16 18:57:49 -0400135 const void *tx;
136 const void *tx_end;
Stefan Roesed987ea62014-11-07 13:50:31 +0100137 void *rx;
138 void *rx_end;
Sean Andersone11c0b12020-10-16 18:57:49 -0400139 u32 fifo_len; /* depth of the FIFO buffer */
Sean Andersondebbff82020-10-16 18:57:51 -0400140 u32 max_xfer; /* Maximum transfer size (in bits) */
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800141
Sean Andersone11c0b12020-10-16 18:57:49 -0400142 int bits_per_word;
143 int len;
144 u8 cs; /* chip select pin */
145 u8 tmode; /* TR/TO/RO/EEPROM */
146 u8 type; /* SPI/SSP/MicroWire */
Stefan Roesed987ea62014-11-07 13:50:31 +0100147};
148
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300149static inline u32 dw_read(struct dw_spi_priv *priv, u32 offset)
Stefan Roesed987ea62014-11-07 13:50:31 +0100150{
151 return __raw_readl(priv->regs + offset);
152}
153
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300154static inline void dw_write(struct dw_spi_priv *priv, u32 offset, u32 val)
Stefan Roesed987ea62014-11-07 13:50:31 +0100155{
156 __raw_writel(val, priv->regs + offset);
157}
158
Sean Andersondebbff82020-10-16 18:57:51 -0400159static u32 dw_spi_dw16_update_cr0(struct dw_spi_priv *priv)
160{
161 return FIELD_PREP(CTRLR0_DFS_MASK, priv->bits_per_word - 1)
162 | FIELD_PREP(CTRLR0_FRF_MASK, priv->type)
163 | FIELD_PREP(CTRLR0_MODE_MASK, priv->mode)
164 | FIELD_PREP(CTRLR0_TMOD_MASK, priv->tmode);
165}
166
167static u32 dw_spi_dw32_update_cr0(struct dw_spi_priv *priv)
168{
169 return FIELD_PREP(CTRLR0_DFS_32_MASK, priv->bits_per_word - 1)
170 | FIELD_PREP(CTRLR0_FRF_MASK, priv->type)
171 | FIELD_PREP(CTRLR0_MODE_MASK, priv->mode)
172 | FIELD_PREP(CTRLR0_TMOD_MASK, priv->tmode);
173}
174
175static u32 dw_spi_dwc_update_cr0(struct dw_spi_priv *priv)
176{
177 return FIELD_PREP(DWC_SSI_CTRLR0_DFS_MASK, priv->bits_per_word - 1)
178 | FIELD_PREP(DWC_SSI_CTRLR0_FRF_MASK, priv->type)
179 | FIELD_PREP(DWC_SSI_CTRLR0_MODE_MASK, priv->mode)
180 | FIELD_PREP(DWC_SSI_CTRLR0_TMOD_MASK, priv->tmode);
181}
182
183static int dw_spi_apb_init(struct udevice *bus, struct dw_spi_priv *priv)
184{
185 /* If we read zeros from DFS, then we need to use DFS_32 instead */
186 dw_write(priv, DW_SPI_SSIENR, 0);
187 dw_write(priv, DW_SPI_CTRLR0, 0xffffffff);
188 if (FIELD_GET(CTRLR0_DFS_MASK, dw_read(priv, DW_SPI_CTRLR0))) {
189 priv->max_xfer = 16;
190 priv->update_cr0 = dw_spi_dw16_update_cr0;
191 } else {
192 priv->max_xfer = 32;
193 priv->update_cr0 = dw_spi_dw32_update_cr0;
194 }
195
196 return 0;
197}
198
Damien Le Moal96b2bdd2022-03-01 10:35:43 +0000199static int dw_spi_apb_k210_init(struct udevice *bus, struct dw_spi_priv *priv)
200{
201 /*
202 * The Canaan Kendryte K210 SoC DW apb_ssi v4 spi controller is
203 * documented to have a 32 word deep TX and RX FIFO, which
204 * spi_hw_init() detects. However, when the RX FIFO is filled up to
205 * 32 entries (RXFLR = 32), an RX FIFO overrun error occurs. Avoid
206 * this problem by force setting fifo_len to 31.
207 */
208 priv->fifo_len = 31;
209
210 return dw_spi_apb_init(bus, priv);
211}
212
Sean Andersondebbff82020-10-16 18:57:51 -0400213static int dw_spi_dwc_init(struct udevice *bus, struct dw_spi_priv *priv)
214{
215 priv->max_xfer = 32;
216 priv->update_cr0 = dw_spi_dwc_update_cr0;
217 return 0;
218}
219
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300220static int request_gpio_cs(struct udevice *bus)
221{
Simon Glass7ec24132024-09-29 19:49:48 -0600222#if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_XPL_BUILD)
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300223 struct dw_spi_priv *priv = dev_get_priv(bus);
224 int ret;
225
226 /* External chip select gpio line is optional */
Sean Anderson17f69fb2020-10-16 18:57:45 -0400227 ret = gpio_request_by_name(bus, "cs-gpios", 0, &priv->cs_gpio,
228 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300229 if (ret == -ENOENT)
230 return 0;
231
232 if (ret < 0) {
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400233 dev_err(bus, "Couldn't request gpio! (error %d)\n", ret);
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300234 return ret;
235 }
236
237 if (dm_gpio_is_valid(&priv->cs_gpio)) {
238 dm_gpio_set_dir_flags(&priv->cs_gpio,
239 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
240 }
241
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400242 dev_dbg(bus, "Using external gpio for CS management\n");
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300243#endif
244 return 0;
245}
246
Simon Glassaad29ae2020-12-03 16:55:21 -0700247static int dw_spi_of_to_plat(struct udevice *bus)
Stefan Roesed987ea62014-11-07 13:50:31 +0100248{
Simon Glass95588622020-12-22 19:30:28 -0700249 struct dw_spi_plat *plat = dev_get_plat(bus);
Stefan Roesed987ea62014-11-07 13:50:31 +0100250
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900251 plat->regs = dev_read_addr_ptr(bus);
Sean Anderson72097a72020-10-16 18:57:46 -0400252 if (!plat->regs)
253 return -EINVAL;
Stefan Roesed987ea62014-11-07 13:50:31 +0100254
255 /* Use 500KHz as a suitable default */
Simon Goldschmidt70cd31b2019-05-09 22:11:57 +0200256 plat->frequency = dev_read_u32_default(bus, "spi-max-frequency",
257 500000);
Stefan Roesed987ea62014-11-07 13:50:31 +0100258
Sean Andersondebbff82020-10-16 18:57:51 -0400259 if (dev_read_bool(bus, "spi-slave"))
260 return -EINVAL;
Stefan Roesed987ea62014-11-07 13:50:31 +0100261
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400262 dev_info(bus, "max-frequency=%d\n", plat->frequency);
Stefan Roesed987ea62014-11-07 13:50:31 +0100263
264 return request_gpio_cs(bus);
Stefan Roesed987ea62014-11-07 13:50:31 +0100265}
266
267/* Restart the controller, disable all interrupts, clean rx fifo */
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400268static void spi_hw_init(struct udevice *bus, struct dw_spi_priv *priv)
Stefan Roesed987ea62014-11-07 13:50:31 +0100269{
Sean Andersonf2520822020-10-16 18:57:48 -0400270 dw_write(priv, DW_SPI_SSIENR, 0);
Sean Anderson5a6f4552022-03-01 10:35:43 +0000271 dw_write(priv, DW_SPI_IMR, 0);
Sean Andersonf2520822020-10-16 18:57:48 -0400272 dw_write(priv, DW_SPI_SSIENR, 1);
Stefan Roesed987ea62014-11-07 13:50:31 +0100273
274 /*
275 * Try to detect the FIFO depth if not set by interface driver,
276 * the depth could be from 2 to 256 from HW spec
277 */
278 if (!priv->fifo_len) {
279 u32 fifo;
280
Axel Lin83cfd372015-02-26 10:45:22 +0800281 for (fifo = 1; fifo < 256; fifo++) {
Sean Anderson75ea2f62020-10-16 18:57:47 -0400282 dw_write(priv, DW_SPI_TXFTLR, fifo);
283 if (fifo != dw_read(priv, DW_SPI_TXFTLR))
Stefan Roesed987ea62014-11-07 13:50:31 +0100284 break;
285 }
286
Axel Lin83cfd372015-02-26 10:45:22 +0800287 priv->fifo_len = (fifo == 1) ? 0 : fifo;
Sean Anderson75ea2f62020-10-16 18:57:47 -0400288 dw_write(priv, DW_SPI_TXFTLR, 0);
Stefan Roesed987ea62014-11-07 13:50:31 +0100289 }
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400290 dev_dbg(bus, "fifo_len=%d\n", priv->fifo_len);
Stefan Roesed987ea62014-11-07 13:50:31 +0100291}
292
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300293/*
294 * We define dw_spi_get_clk function as 'weak' as some targets
295 * (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) don't use standard clock API
296 * and implement dw_spi_get_clk their own way in their clock manager.
297 */
298__weak int dw_spi_get_clk(struct udevice *bus, ulong *rate)
299{
300 struct dw_spi_priv *priv = dev_get_priv(bus);
301 int ret;
302
303 ret = clk_get_by_index(bus, 0, &priv->clk);
304 if (ret)
305 return ret;
306
307 ret = clk_enable(&priv->clk);
308 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
309 return ret;
310
311 *rate = clk_get_rate(&priv->clk);
312 if (!*rate)
313 goto err_rate;
314
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400315 dev_dbg(bus, "Got clock via device tree: %lu Hz\n", *rate);
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300316
317 return 0;
318
319err_rate:
320 clk_disable(&priv->clk);
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300321
322 return -EINVAL;
323}
324
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800325static int dw_spi_reset(struct udevice *bus)
326{
327 int ret;
328 struct dw_spi_priv *priv = dev_get_priv(bus);
329
330 ret = reset_get_bulk(bus, &priv->resets);
331 if (ret) {
332 /*
333 * Return 0 if error due to !CONFIG_DM_RESET and reset
334 * DT property is not present.
335 */
336 if (ret == -ENOENT || ret == -ENOTSUPP)
337 return 0;
338
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400339 dev_warn(bus, "Couldn't find/assert reset device (error %d)\n",
340 ret);
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800341 return ret;
342 }
343
344 ret = reset_deassert_bulk(&priv->resets);
345 if (ret) {
346 reset_release_bulk(&priv->resets);
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400347 dev_err(bus, "Failed to de-assert reset for SPI (error %d)\n",
348 ret);
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800349 return ret;
350 }
351
352 return 0;
353}
354
Sean Andersondebbff82020-10-16 18:57:51 -0400355typedef int (*dw_spi_init_t)(struct udevice *bus, struct dw_spi_priv *priv);
356
Stefan Roesed987ea62014-11-07 13:50:31 +0100357static int dw_spi_probe(struct udevice *bus)
358{
Sean Andersondebbff82020-10-16 18:57:51 -0400359 dw_spi_init_t init = (dw_spi_init_t)dev_get_driver_data(bus);
Simon Glassb75b15b2020-12-03 16:55:23 -0700360 struct dw_spi_plat *plat = dev_get_plat(bus);
Stefan Roesed987ea62014-11-07 13:50:31 +0100361 struct dw_spi_priv *priv = dev_get_priv(bus);
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300362 int ret;
Sean Andersondebbff82020-10-16 18:57:51 -0400363 u32 version;
Stefan Roesed987ea62014-11-07 13:50:31 +0100364
365 priv->regs = plat->regs;
366 priv->freq = plat->frequency;
367
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300368 ret = dw_spi_get_clk(bus, &priv->bus_clk_rate);
369 if (ret)
370 return ret;
371
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800372 ret = dw_spi_reset(bus);
373 if (ret)
374 return ret;
375
Sean Andersondebbff82020-10-16 18:57:51 -0400376 if (!init)
377 return -EINVAL;
378 ret = init(bus, priv);
379 if (ret)
380 return ret;
381
382 version = dw_read(priv, DW_SPI_VERSION);
383 dev_dbg(bus, "ssi_version_id=%c.%c%c%c ssi_max_xfer_size=%u\n",
384 version >> 24, version >> 16, version >> 8, version,
385 priv->max_xfer);
386
Stefan Roesed987ea62014-11-07 13:50:31 +0100387 /* Currently only bits_per_word == 8 supported */
388 priv->bits_per_word = 8;
Stefan Roesed987ea62014-11-07 13:50:31 +0100389
390 priv->tmode = 0; /* Tx & Rx */
391
392 /* Basic HW init */
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400393 spi_hw_init(bus, priv);
Stefan Roesed987ea62014-11-07 13:50:31 +0100394
395 return 0;
396}
397
398/* Return the max entries we can fill into tx fifo */
399static inline u32 tx_max(struct dw_spi_priv *priv)
400{
401 u32 tx_left, tx_room, rxtx_gap;
402
Stefan Roese571e2a42014-11-16 12:47:01 +0100403 tx_left = (priv->tx_end - priv->tx) / (priv->bits_per_word >> 3);
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300404 tx_room = priv->fifo_len - dw_read(priv, DW_SPI_TXFLR);
Stefan Roesed987ea62014-11-07 13:50:31 +0100405
406 /*
407 * Another concern is about the tx/rx mismatch, we
Stefan Roese571e2a42014-11-16 12:47:01 +0100408 * thought about using (priv->fifo_len - rxflr - txflr) as
Stefan Roesed987ea62014-11-07 13:50:31 +0100409 * one maximum value for tx, but it doesn't cover the
410 * data which is out of tx/rx fifo and inside the
411 * shift registers. So a control from sw point of
412 * view is taken.
413 */
414 rxtx_gap = ((priv->rx_end - priv->rx) - (priv->tx_end - priv->tx)) /
Stefan Roese571e2a42014-11-16 12:47:01 +0100415 (priv->bits_per_word >> 3);
Stefan Roesed987ea62014-11-07 13:50:31 +0100416
417 return min3(tx_left, tx_room, (u32)(priv->fifo_len - rxtx_gap));
418}
419
420/* Return the max entries we should read out of rx fifo */
421static inline u32 rx_max(struct dw_spi_priv *priv)
422{
Stefan Roese571e2a42014-11-16 12:47:01 +0100423 u32 rx_left = (priv->rx_end - priv->rx) / (priv->bits_per_word >> 3);
Stefan Roesed987ea62014-11-07 13:50:31 +0100424
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300425 return min_t(u32, rx_left, dw_read(priv, DW_SPI_RXFLR));
Stefan Roesed987ea62014-11-07 13:50:31 +0100426}
427
428static void dw_writer(struct dw_spi_priv *priv)
429{
430 u32 max = tx_max(priv);
Sean Andersondebbff82020-10-16 18:57:51 -0400431 u32 txw = 0xFFFFFFFF;
Stefan Roesed987ea62014-11-07 13:50:31 +0100432
433 while (max--) {
434 /* Set the tx word if the transfer's original "tx" is not null */
435 if (priv->tx_end - priv->len) {
Stefan Roese571e2a42014-11-16 12:47:01 +0100436 if (priv->bits_per_word == 8)
Stefan Roesed987ea62014-11-07 13:50:31 +0100437 txw = *(u8 *)(priv->tx);
438 else
439 txw = *(u16 *)(priv->tx);
440 }
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300441 dw_write(priv, DW_SPI_DR, txw);
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400442 log_content("tx=0x%02x\n", txw);
Stefan Roese571e2a42014-11-16 12:47:01 +0100443 priv->tx += priv->bits_per_word >> 3;
Stefan Roesed987ea62014-11-07 13:50:31 +0100444 }
445}
446
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300447static void dw_reader(struct dw_spi_priv *priv)
Stefan Roesed987ea62014-11-07 13:50:31 +0100448{
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300449 u32 max = rx_max(priv);
Stefan Roesed987ea62014-11-07 13:50:31 +0100450 u16 rxw;
451
Stefan Roesed987ea62014-11-07 13:50:31 +0100452 while (max--) {
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300453 rxw = dw_read(priv, DW_SPI_DR);
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400454 log_content("rx=0x%02x\n", rxw);
Stefan Roese571e2a42014-11-16 12:47:01 +0100455
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300456 /* Care about rx if the transfer's original "rx" is not null */
Stefan Roesed987ea62014-11-07 13:50:31 +0100457 if (priv->rx_end - priv->len) {
Stefan Roese571e2a42014-11-16 12:47:01 +0100458 if (priv->bits_per_word == 8)
Stefan Roesed987ea62014-11-07 13:50:31 +0100459 *(u8 *)(priv->rx) = rxw;
460 else
461 *(u16 *)(priv->rx) = rxw;
462 }
Stefan Roese571e2a42014-11-16 12:47:01 +0100463 priv->rx += priv->bits_per_word >> 3;
Stefan Roesed987ea62014-11-07 13:50:31 +0100464 }
Stefan Roesed987ea62014-11-07 13:50:31 +0100465}
466
467static int poll_transfer(struct dw_spi_priv *priv)
468{
Stefan Roesed987ea62014-11-07 13:50:31 +0100469 do {
470 dw_writer(priv);
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300471 dw_reader(priv);
Stefan Roesed987ea62014-11-07 13:50:31 +0100472 } while (priv->rx_end > priv->rx);
473
474 return 0;
475}
476
Gregory CLEMENTf2893372018-10-09 14:14:07 +0200477/*
478 * We define external_cs_manage function as 'weak' as some targets
479 * (like MSCC Ocelot) don't control the external CS pin using a GPIO
480 * controller. These SoCs use specific registers to control by
481 * software the SPI pins (and especially the CS).
482 */
483__weak void external_cs_manage(struct udevice *dev, bool on)
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300484{
Simon Glass7ec24132024-09-29 19:49:48 -0600485#if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_XPL_BUILD)
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300486 struct dw_spi_priv *priv = dev_get_priv(dev->parent);
487
488 if (!dm_gpio_is_valid(&priv->cs_gpio))
489 return;
490
491 dm_gpio_set_value(&priv->cs_gpio, on ? 1 : 0);
492#endif
493}
494
Stefan Roesed987ea62014-11-07 13:50:31 +0100495static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
496 const void *dout, void *din, unsigned long flags)
497{
498 struct udevice *bus = dev->parent;
499 struct dw_spi_priv *priv = dev_get_priv(bus);
500 const u8 *tx = dout;
501 u8 *rx = din;
502 int ret = 0;
503 u32 cr0 = 0;
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +0300504 u32 val;
Stefan Roesed987ea62014-11-07 13:50:31 +0100505 u32 cs;
506
507 /* spi core configured to do 8 bit transfers */
508 if (bitlen % 8) {
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400509 dev_err(dev, "Non byte aligned SPI transfer.\n");
Stefan Roesed987ea62014-11-07 13:50:31 +0100510 return -1;
511 }
512
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300513 /* Start the transaction if necessary. */
514 if (flags & SPI_XFER_BEGIN)
515 external_cs_manage(dev, false);
516
Stefan Roesed987ea62014-11-07 13:50:31 +0100517 if (rx && tx)
Sean Andersondebbff82020-10-16 18:57:51 -0400518 priv->tmode = CTRLR0_TMOD_TR;
Stefan Roesed987ea62014-11-07 13:50:31 +0100519 else if (rx)
Sean Andersondebbff82020-10-16 18:57:51 -0400520 priv->tmode = CTRLR0_TMOD_RO;
Stefan Roesed987ea62014-11-07 13:50:31 +0100521 else
Eugeniy Paltsev31f50132018-03-22 13:50:44 +0300522 /*
Sean Andersondebbff82020-10-16 18:57:51 -0400523 * In transmit only mode (CTRL0_TMOD_TO) input FIFO never gets
Eugeniy Paltsev31f50132018-03-22 13:50:44 +0300524 * any data which breaks our logic in poll_transfer() above.
525 */
Sean Andersondebbff82020-10-16 18:57:51 -0400526 priv->tmode = CTRLR0_TMOD_TR;
Stefan Roesed987ea62014-11-07 13:50:31 +0100527
Sean Andersondebbff82020-10-16 18:57:51 -0400528 cr0 = priv->update_cr0(priv);
Stefan Roesed987ea62014-11-07 13:50:31 +0100529
Stefan Roese571e2a42014-11-16 12:47:01 +0100530 priv->len = bitlen >> 3;
Stefan Roesed987ea62014-11-07 13:50:31 +0100531
532 priv->tx = (void *)tx;
533 priv->tx_end = priv->tx + priv->len;
534 priv->rx = rx;
535 priv->rx_end = priv->rx + priv->len;
536
537 /* Disable controller before writing control registers */
Sean Andersonf2520822020-10-16 18:57:48 -0400538 dw_write(priv, DW_SPI_SSIENR, 0);
Stefan Roesed987ea62014-11-07 13:50:31 +0100539
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400540 dev_dbg(dev, "cr0=%08x rx=%p tx=%p len=%d [bytes]\n", cr0, rx, tx,
541 priv->len);
Stefan Roesed987ea62014-11-07 13:50:31 +0100542 /* Reprogram cr0 only if changed */
Sean Anderson75ea2f62020-10-16 18:57:47 -0400543 if (dw_read(priv, DW_SPI_CTRLR0) != cr0)
544 dw_write(priv, DW_SPI_CTRLR0, cr0);
Stefan Roesed987ea62014-11-07 13:50:31 +0100545
546 /*
547 * Configure the desired SS (slave select 0...3) in the controller
548 * The DW SPI controller will activate and deactivate this CS
549 * automatically. So no cs_activate() etc is needed in this driver.
550 */
551 cs = spi_chip_select(dev);
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300552 dw_write(priv, DW_SPI_SER, 1 << cs);
Stefan Roesed987ea62014-11-07 13:50:31 +0100553
554 /* Enable controller after writing control registers */
Sean Andersonf2520822020-10-16 18:57:48 -0400555 dw_write(priv, DW_SPI_SSIENR, 1);
Stefan Roesed987ea62014-11-07 13:50:31 +0100556
557 /* Start transfer in a polling loop */
558 ret = poll_transfer(priv);
559
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +0300560 /*
561 * Wait for current transmit operation to complete.
562 * Otherwise if some data still exists in Tx FIFO it can be
563 * silently flushed, i.e. dropped on disabling of the controller,
564 * which happens when writing 0 to DW_SPI_SSIENR which happens
565 * in the beginning of new transfer.
566 */
567 if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
Eugeniy Paltsev208be8f2018-04-19 17:47:41 +0300568 (val & SR_TF_EMPT) && !(val & SR_BUSY),
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +0300569 RX_TIMEOUT * 1000)) {
570 ret = -ETIMEDOUT;
571 }
572
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300573 /* Stop the transaction if necessary */
574 if (flags & SPI_XFER_END)
575 external_cs_manage(dev, true);
576
Stefan Roesed987ea62014-11-07 13:50:31 +0100577 return ret;
578}
579
Sean Anderson16edba32020-10-16 18:57:53 -0400580/*
581 * This function is necessary for reading SPI flash with the native CS
582 * c.f. https://lkml.org/lkml/2015/12/23/132
583 */
584static int dw_spi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
585{
586 bool read = op->data.dir == SPI_MEM_DATA_IN;
587 int pos, i, ret = 0;
588 struct udevice *bus = slave->dev->parent;
589 struct dw_spi_priv *priv = dev_get_priv(bus);
Niklas Casselb7e92072022-02-08 22:52:43 +0000590 u8 op_len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
Sean Anderson16edba32020-10-16 18:57:53 -0400591 u8 op_buf[op_len];
Maksim Kiselev6cf56182023-12-21 13:13:30 +0300592 u32 cr0, sts;
Sean Anderson16edba32020-10-16 18:57:53 -0400593
594 if (read)
595 priv->tmode = CTRLR0_TMOD_EPROMREAD;
596 else
597 priv->tmode = CTRLR0_TMOD_TO;
598
599 cr0 = priv->update_cr0(priv);
600 dev_dbg(bus, "cr0=%08x buf=%p len=%u [bytes]\n", cr0, op->data.buf.in,
601 op->data.nbytes);
602
603 dw_write(priv, DW_SPI_SSIENR, 0);
604 dw_write(priv, DW_SPI_CTRLR0, cr0);
605 if (read)
606 dw_write(priv, DW_SPI_CTRLR1, op->data.nbytes - 1);
607 dw_write(priv, DW_SPI_SSIENR, 1);
608
609 /* From spi_mem_exec_op */
610 pos = 0;
611 op_buf[pos++] = op->cmd.opcode;
612 if (op->addr.nbytes) {
613 for (i = 0; i < op->addr.nbytes; i++)
614 op_buf[pos + i] = op->addr.val >>
615 (8 * (op->addr.nbytes - i - 1));
616
617 pos += op->addr.nbytes;
618 }
619 if (op->dummy.nbytes)
620 memset(op_buf + pos, 0xff, op->dummy.nbytes);
621
622 external_cs_manage(slave->dev, false);
623
624 priv->tx = &op_buf;
625 priv->tx_end = priv->tx + op_len;
626 priv->rx = NULL;
627 priv->rx_end = NULL;
628 while (priv->tx != priv->tx_end)
629 dw_writer(priv);
630
631 /*
632 * XXX: The following are tight loops! Enabling debug messages may cause
633 * them to fail because we are not reading/writing the fifo fast enough.
634 */
635 if (read) {
Maksim Kiselev6cf56182023-12-21 13:13:30 +0300636 void *prev_rx = priv->rx = op->data.buf.in;
Sean Anderson16edba32020-10-16 18:57:53 -0400637 priv->rx_end = priv->rx + op->data.nbytes;
638
639 dw_write(priv, DW_SPI_SER, 1 << spi_chip_select(slave->dev));
Maksim Kiselev6cf56182023-12-21 13:13:30 +0300640 while (priv->rx != priv->rx_end) {
Sean Anderson16edba32020-10-16 18:57:53 -0400641 dw_reader(priv);
Maksim Kiselev6cf56182023-12-21 13:13:30 +0300642 if (prev_rx == priv->rx) {
643 sts = dw_read(priv, DW_SPI_RISR);
644 if (sts & RISR_INT_RXOI) {
645 dev_err(bus, "FIFO overflow on Rx\n");
646 return -EIO;
647 }
648 }
649 prev_rx = priv->rx;
650 }
Sean Anderson16edba32020-10-16 18:57:53 -0400651 } else {
652 u32 val;
653
654 priv->tx = op->data.buf.out;
655 priv->tx_end = priv->tx + op->data.nbytes;
656
657 /* Fill up the write fifo before starting the transfer */
658 dw_writer(priv);
659 dw_write(priv, DW_SPI_SER, 1 << spi_chip_select(slave->dev));
660 while (priv->tx != priv->tx_end)
661 dw_writer(priv);
662
663 if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
664 (val & SR_TF_EMPT) && !(val & SR_BUSY),
665 RX_TIMEOUT * 1000)) {
666 ret = -ETIMEDOUT;
667 }
668 }
669
670 dw_write(priv, DW_SPI_SER, 0);
671 external_cs_manage(slave->dev, true);
672
673 dev_dbg(bus, "%u bytes xfered\n", op->data.nbytes);
Stefan Roesed987ea62014-11-07 13:50:31 +0100674 return ret;
675}
676
Sean Anderson16edba32020-10-16 18:57:53 -0400677/* The size of ctrl1 limits data transfers to 64K */
678static int dw_spi_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op)
679{
680 op->data.nbytes = min(op->data.nbytes, (unsigned int)SZ_64K);
681
682 return 0;
683}
684
685static const struct spi_controller_mem_ops dw_spi_mem_ops = {
686 .exec_op = dw_spi_exec_op,
687 .adjust_op_size = dw_spi_adjust_op_size,
688};
689
Stefan Roesed987ea62014-11-07 13:50:31 +0100690static int dw_spi_set_speed(struct udevice *bus, uint speed)
691{
Simon Glass95588622020-12-22 19:30:28 -0700692 struct dw_spi_plat *plat = dev_get_plat(bus);
Stefan Roesed987ea62014-11-07 13:50:31 +0100693 struct dw_spi_priv *priv = dev_get_priv(bus);
694 u16 clk_div;
695
696 if (speed > plat->frequency)
697 speed = plat->frequency;
698
699 /* Disable controller before writing control registers */
Sean Andersonf2520822020-10-16 18:57:48 -0400700 dw_write(priv, DW_SPI_SSIENR, 0);
Stefan Roesed987ea62014-11-07 13:50:31 +0100701
702 /* clk_div doesn't support odd number */
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300703 clk_div = priv->bus_clk_rate / speed;
Stefan Roesed987ea62014-11-07 13:50:31 +0100704 clk_div = (clk_div + 1) & 0xfffe;
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300705 dw_write(priv, DW_SPI_BAUDR, clk_div);
Stefan Roesed987ea62014-11-07 13:50:31 +0100706
707 /* Enable controller after writing control registers */
Sean Andersonf2520822020-10-16 18:57:48 -0400708 dw_write(priv, DW_SPI_SSIENR, 1);
Stefan Roesed987ea62014-11-07 13:50:31 +0100709
710 priv->freq = speed;
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400711 dev_dbg(bus, "speed=%d clk_div=%d\n", priv->freq, clk_div);
Stefan Roesed987ea62014-11-07 13:50:31 +0100712
713 return 0;
714}
715
716static int dw_spi_set_mode(struct udevice *bus, uint mode)
717{
718 struct dw_spi_priv *priv = dev_get_priv(bus);
719
720 /*
721 * Can't set mode yet. Since this depends on if rx, tx, or
722 * rx & tx is requested. So we have to defer this to the
723 * real transfer function.
724 */
725 priv->mode = mode;
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400726 dev_dbg(bus, "mode=%d\n", priv->mode);
Stefan Roesed987ea62014-11-07 13:50:31 +0100727
728 return 0;
729}
730
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800731static int dw_spi_remove(struct udevice *bus)
732{
733 struct dw_spi_priv *priv = dev_get_priv(bus);
Ley Foon Tand95ab402018-09-19 16:27:19 +0800734 int ret;
735
736 ret = reset_release_bulk(&priv->resets);
737 if (ret)
738 return ret;
739
740#if CONFIG_IS_ENABLED(CLK)
741 ret = clk_disable(&priv->clk);
742 if (ret)
743 return ret;
Ley Foon Tand95ab402018-09-19 16:27:19 +0800744#endif
745 return 0;
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800746}
747
Stefan Roesed987ea62014-11-07 13:50:31 +0100748static const struct dm_spi_ops dw_spi_ops = {
749 .xfer = dw_spi_xfer,
Sean Anderson16edba32020-10-16 18:57:53 -0400750 .mem_ops = &dw_spi_mem_ops,
Stefan Roesed987ea62014-11-07 13:50:31 +0100751 .set_speed = dw_spi_set_speed,
752 .set_mode = dw_spi_set_mode,
753 /*
754 * cs_info is not needed, since we require all chip selects to be
755 * in the device tree explicitly
756 */
757};
758
759static const struct udevice_id dw_spi_ids[] = {
Sean Andersondebbff82020-10-16 18:57:51 -0400760 /* Generic compatible strings */
761
762 { .compatible = "snps,dw-apb-ssi", .data = (ulong)dw_spi_apb_init },
763 { .compatible = "snps,dw-apb-ssi-3.20a", .data = (ulong)dw_spi_apb_init },
764 { .compatible = "snps,dw-apb-ssi-3.22a", .data = (ulong)dw_spi_apb_init },
765 /* First version with SSI_MAX_XFER_SIZE */
766 { .compatible = "snps,dw-apb-ssi-3.23a", .data = (ulong)dw_spi_apb_init },
767 /* First version with Dual/Quad SPI; unused by this driver */
768 { .compatible = "snps,dw-apb-ssi-4.00a", .data = (ulong)dw_spi_apb_init },
769 { .compatible = "snps,dw-apb-ssi-4.01", .data = (ulong)dw_spi_apb_init },
770 { .compatible = "snps,dwc-ssi-1.01a", .data = (ulong)dw_spi_dwc_init },
771
772 /* Compatible strings for specific SoCs */
773
774 /*
775 * Both the Cyclone V and Arria V share a device tree and have the same
776 * version of this device. This compatible string is used for those
777 * devices, and is not used for sofpgas in general.
778 */
779 { .compatible = "altr,socfpga-spi", .data = (ulong)dw_spi_apb_init },
780 { .compatible = "altr,socfpga-arria10-spi", .data = (ulong)dw_spi_apb_init },
Damien Le Moal96b2bdd2022-03-01 10:35:43 +0000781 { .compatible = "canaan,k210-spi", .data = (ulong)dw_spi_apb_k210_init},
Damien Le Moal6e5a8b72022-03-01 10:35:39 +0000782 { .compatible = "canaan,k210-ssi", .data = (ulong)dw_spi_dwc_init },
Sean Andersondebbff82020-10-16 18:57:51 -0400783 { .compatible = "intel,stratix10-spi", .data = (ulong)dw_spi_apb_init },
784 { .compatible = "intel,agilex-spi", .data = (ulong)dw_spi_apb_init },
785 { .compatible = "mscc,ocelot-spi", .data = (ulong)dw_spi_apb_init },
786 { .compatible = "mscc,jaguar2-spi", .data = (ulong)dw_spi_apb_init },
787 { .compatible = "snps,axs10x-spi", .data = (ulong)dw_spi_apb_init },
788 { .compatible = "snps,hsdk-spi", .data = (ulong)dw_spi_apb_init },
Stefan Roesed987ea62014-11-07 13:50:31 +0100789 { }
790};
791
792U_BOOT_DRIVER(dw_spi) = {
793 .name = "dw_spi",
794 .id = UCLASS_SPI,
795 .of_match = dw_spi_ids,
796 .ops = &dw_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700797 .of_to_plat = dw_spi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700798 .plat_auto = sizeof(struct dw_spi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700799 .priv_auto = sizeof(struct dw_spi_priv),
Stefan Roesed987ea62014-11-07 13:50:31 +0100800 .probe = dw_spi_probe,
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800801 .remove = dw_spi_remove,
Stefan Roesed987ea62014-11-07 13:50:31 +0100802};