blob: 1c7d0ca310b6e8d0c37b23bf41b2e9dd36192a62 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stefan Roese5bef6fd2014-11-07 13:50:31 +01002/*
3 * Designware master SPI core controller driver
4 *
5 * Copyright (C) 2014 Stefan Roese <sr@denx.de>
Sean Anderson58875792020-10-16 18:57:51 -04006 * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
Stefan Roese5bef6fd2014-11-07 13:50:31 +01007 *
Stefan Roesea72f8022014-11-16 12:47:01 +01008 * Very loosely based on the Linux driver:
9 * drivers/spi/spi-dw.c, which is:
Stefan Roese5bef6fd2014-11-07 13:50:31 +010010 * Copyright (c) 2009, Intel Corporation.
Stefan Roese5bef6fd2014-11-07 13:50:31 +010011 */
12
Sean Anderson1b3dd492020-10-16 18:57:44 -040013#define LOG_CATEGORY UCLASS_SPI
Stefan Roese5bef6fd2014-11-07 13:50:31 +010014#include <common.h>
Eugeniy Paltsev58c125b2017-12-28 15:09:03 +030015#include <clk.h>
Stefan Roese5bef6fd2014-11-07 13:50:31 +010016#include <dm.h>
Simon Glass336d4612020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Sean Andersonfec7bf02020-10-16 18:57:53 -040018#include <errno.h>
19#include <fdtdec.h>
20#include <log.h>
21#include <malloc.h>
22#include <reset.h>
23#include <spi.h>
24#include <spi-mem.h>
25#include <asm/io.h>
26#include <asm-generic/gpio.h>
Sean Anderson58875792020-10-16 18:57:51 -040027#include <linux/bitfield.h>
Simon Glasscd93d622020-05-10 11:40:13 -060028#include <linux/bitops.h>
Stefan Roese5bef6fd2014-11-07 13:50:31 +010029#include <linux/compat.h>
Eugeniy Paltsevc6b4f032018-03-22 13:50:43 +030030#include <linux/iopoll.h>
Sean Andersonfec7bf02020-10-16 18:57:53 -040031#include <linux/sizes.h>
Stefan Roese5bef6fd2014-11-07 13:50:31 +010032
Stefan Roese5bef6fd2014-11-07 13:50:31 +010033/* Register offsets */
Sean Anderson30040342020-10-16 18:57:47 -040034#define DW_SPI_CTRLR0 0x00
35#define DW_SPI_CTRLR1 0x04
Stefan Roese5bef6fd2014-11-07 13:50:31 +010036#define DW_SPI_SSIENR 0x08
37#define DW_SPI_MWCR 0x0c
38#define DW_SPI_SER 0x10
39#define DW_SPI_BAUDR 0x14
Sean Anderson30040342020-10-16 18:57:47 -040040#define DW_SPI_TXFTLR 0x18
41#define DW_SPI_RXFTLR 0x1c
Stefan Roese5bef6fd2014-11-07 13:50:31 +010042#define DW_SPI_TXFLR 0x20
43#define DW_SPI_RXFLR 0x24
44#define DW_SPI_SR 0x28
45#define DW_SPI_IMR 0x2c
46#define DW_SPI_ISR 0x30
47#define DW_SPI_RISR 0x34
48#define DW_SPI_TXOICR 0x38
49#define DW_SPI_RXOICR 0x3c
50#define DW_SPI_RXUICR 0x40
51#define DW_SPI_MSTICR 0x44
52#define DW_SPI_ICR 0x48
53#define DW_SPI_DMACR 0x4c
54#define DW_SPI_DMATDLR 0x50
55#define DW_SPI_DMARDLR 0x54
56#define DW_SPI_IDR 0x58
57#define DW_SPI_VERSION 0x5c
58#define DW_SPI_DR 0x60
59
60/* Bit fields in CTRLR0 */
Sean Anderson58875792020-10-16 18:57:51 -040061/*
62 * Only present when SSI_MAX_XFER_SIZE=16. This is the default, and the only
63 * option before version 3.23a.
64 */
65#define CTRLR0_DFS_MASK GENMASK(3, 0)
Stefan Roese5bef6fd2014-11-07 13:50:31 +010066
Sean Anderson58875792020-10-16 18:57:51 -040067#define CTRLR0_FRF_MASK GENMASK(5, 4)
68#define CTRLR0_FRF_SPI 0x0
69#define CTRLR0_FRF_SSP 0x1
70#define CTRLR0_FRF_MICROWIRE 0x2
71#define CTRLR0_FRF_RESV 0x3
Stefan Roese5bef6fd2014-11-07 13:50:31 +010072
Sean Anderson58875792020-10-16 18:57:51 -040073#define CTRLR0_MODE_MASK GENMASK(7, 6)
74#define CTRLR0_MODE_SCPH 0x1
75#define CTRLR0_MODE_SCPOL 0x2
Stefan Roese5bef6fd2014-11-07 13:50:31 +010076
Sean Anderson58875792020-10-16 18:57:51 -040077#define CTRLR0_TMOD_MASK GENMASK(9, 8)
78#define CTRLR0_TMOD_TR 0x0 /* xmit & recv */
79#define CTRLR0_TMOD_TO 0x1 /* xmit only */
80#define CTRLR0_TMOD_RO 0x2 /* recv only */
81#define CTRLR0_TMOD_EPROMREAD 0x3 /* eeprom read mode */
Stefan Roese5bef6fd2014-11-07 13:50:31 +010082
Sean Anderson58875792020-10-16 18:57:51 -040083#define CTRLR0_SLVOE_OFFSET 10
84#define CTRLR0_SRL_OFFSET 11
85#define CTRLR0_CFS_MASK GENMASK(15, 12)
86
87/* Only present when SSI_MAX_XFER_SIZE=32 */
88#define CTRLR0_DFS_32_MASK GENMASK(20, 16)
89
90/* The next field is only present on versions after 4.00a */
91#define CTRLR0_SPI_FRF_MASK GENMASK(22, 21)
92#define CTRLR0_SPI_FRF_BYTE 0x0
93#define CTRLR0_SPI_FRF_DUAL 0x1
94#define CTRLR0_SPI_FRF_QUAD 0x2
95
96/* Bit fields in CTRLR0 based on DWC_ssi_databook.pdf v1.01a */
97#define DWC_SSI_CTRLR0_DFS_MASK GENMASK(4, 0)
98#define DWC_SSI_CTRLR0_FRF_MASK GENMASK(7, 6)
99#define DWC_SSI_CTRLR0_MODE_MASK GENMASK(9, 8)
100#define DWC_SSI_CTRLR0_TMOD_MASK GENMASK(11, 10)
101#define DWC_SSI_CTRLR0_SRL_OFFSET 13
102#define DWC_SSI_CTRLR0_SPI_FRF_MASK GENMASK(23, 22)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100103
104/* Bit fields in SR, 7 bits */
Jagan Teki95e77d92015-10-23 01:01:36 +0530105#define SR_MASK GENMASK(6, 0) /* cover 7 bits */
Jagan Teki431a9f02015-10-23 01:36:23 +0530106#define SR_BUSY BIT(0)
107#define SR_TF_NOT_FULL BIT(1)
108#define SR_TF_EMPT BIT(2)
109#define SR_RF_NOT_EMPT BIT(3)
110#define SR_RF_FULL BIT(4)
111#define SR_TX_ERR BIT(5)
112#define SR_DCOL BIT(6)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100113
Stefan Roesea72f8022014-11-16 12:47:01 +0100114#define RX_TIMEOUT 1000 /* timeout in ms */
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100115
Simon Glass8a8d24b2020-12-03 16:55:23 -0700116struct dw_spi_plat {
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100117 s32 frequency; /* Default clock frequency, -1 for none */
118 void __iomem *regs;
119};
120
121struct dw_spi_priv {
Eugeniy Paltsev58c125b2017-12-28 15:09:03 +0300122 struct clk clk;
Sean Andersonddd34502020-10-16 18:57:49 -0400123 struct reset_ctl_bulk resets;
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300124 struct gpio_desc cs_gpio; /* External chip-select gpio */
125
Sean Anderson58875792020-10-16 18:57:51 -0400126 u32 (*update_cr0)(struct dw_spi_priv *priv);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100127
Sean Andersonddd34502020-10-16 18:57:49 -0400128 void __iomem *regs;
129 unsigned long bus_clk_rate;
130 unsigned int freq; /* Default frequency */
131 unsigned int mode;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100132
Sean Andersonddd34502020-10-16 18:57:49 -0400133 const void *tx;
134 const void *tx_end;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100135 void *rx;
136 void *rx_end;
Sean Andersonddd34502020-10-16 18:57:49 -0400137 u32 fifo_len; /* depth of the FIFO buffer */
Sean Anderson58875792020-10-16 18:57:51 -0400138 u32 max_xfer; /* Maximum transfer size (in bits) */
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800139
Sean Andersonddd34502020-10-16 18:57:49 -0400140 int bits_per_word;
141 int len;
142 u8 cs; /* chip select pin */
143 u8 tmode; /* TR/TO/RO/EEPROM */
144 u8 type; /* SPI/SSP/MicroWire */
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100145};
146
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300147static inline u32 dw_read(struct dw_spi_priv *priv, u32 offset)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100148{
149 return __raw_readl(priv->regs + offset);
150}
151
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300152static inline void dw_write(struct dw_spi_priv *priv, u32 offset, u32 val)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100153{
154 __raw_writel(val, priv->regs + offset);
155}
156
Sean Anderson58875792020-10-16 18:57:51 -0400157static u32 dw_spi_dw16_update_cr0(struct dw_spi_priv *priv)
158{
159 return FIELD_PREP(CTRLR0_DFS_MASK, priv->bits_per_word - 1)
160 | FIELD_PREP(CTRLR0_FRF_MASK, priv->type)
161 | FIELD_PREP(CTRLR0_MODE_MASK, priv->mode)
162 | FIELD_PREP(CTRLR0_TMOD_MASK, priv->tmode);
163}
164
165static u32 dw_spi_dw32_update_cr0(struct dw_spi_priv *priv)
166{
167 return FIELD_PREP(CTRLR0_DFS_32_MASK, priv->bits_per_word - 1)
168 | FIELD_PREP(CTRLR0_FRF_MASK, priv->type)
169 | FIELD_PREP(CTRLR0_MODE_MASK, priv->mode)
170 | FIELD_PREP(CTRLR0_TMOD_MASK, priv->tmode);
171}
172
173static u32 dw_spi_dwc_update_cr0(struct dw_spi_priv *priv)
174{
175 return FIELD_PREP(DWC_SSI_CTRLR0_DFS_MASK, priv->bits_per_word - 1)
176 | FIELD_PREP(DWC_SSI_CTRLR0_FRF_MASK, priv->type)
177 | FIELD_PREP(DWC_SSI_CTRLR0_MODE_MASK, priv->mode)
178 | FIELD_PREP(DWC_SSI_CTRLR0_TMOD_MASK, priv->tmode);
179}
180
181static int dw_spi_apb_init(struct udevice *bus, struct dw_spi_priv *priv)
182{
183 /* If we read zeros from DFS, then we need to use DFS_32 instead */
184 dw_write(priv, DW_SPI_SSIENR, 0);
185 dw_write(priv, DW_SPI_CTRLR0, 0xffffffff);
186 if (FIELD_GET(CTRLR0_DFS_MASK, dw_read(priv, DW_SPI_CTRLR0))) {
187 priv->max_xfer = 16;
188 priv->update_cr0 = dw_spi_dw16_update_cr0;
189 } else {
190 priv->max_xfer = 32;
191 priv->update_cr0 = dw_spi_dw32_update_cr0;
192 }
193
194 return 0;
195}
196
Damien Le Moal40b64352022-03-01 10:35:43 +0000197static int dw_spi_apb_k210_init(struct udevice *bus, struct dw_spi_priv *priv)
198{
199 /*
200 * The Canaan Kendryte K210 SoC DW apb_ssi v4 spi controller is
201 * documented to have a 32 word deep TX and RX FIFO, which
202 * spi_hw_init() detects. However, when the RX FIFO is filled up to
203 * 32 entries (RXFLR = 32), an RX FIFO overrun error occurs. Avoid
204 * this problem by force setting fifo_len to 31.
205 */
206 priv->fifo_len = 31;
207
208 return dw_spi_apb_init(bus, priv);
209}
210
Sean Anderson58875792020-10-16 18:57:51 -0400211static int dw_spi_dwc_init(struct udevice *bus, struct dw_spi_priv *priv)
212{
213 priv->max_xfer = 32;
214 priv->update_cr0 = dw_spi_dwc_update_cr0;
215 return 0;
216}
217
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300218static int request_gpio_cs(struct udevice *bus)
219{
Simon Glassbcee8d62019-12-06 21:41:35 -0700220#if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD)
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300221 struct dw_spi_priv *priv = dev_get_priv(bus);
222 int ret;
223
224 /* External chip select gpio line is optional */
Sean Anderson13fc44e2020-10-16 18:57:45 -0400225 ret = gpio_request_by_name(bus, "cs-gpios", 0, &priv->cs_gpio,
226 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300227 if (ret == -ENOENT)
228 return 0;
229
230 if (ret < 0) {
Sean Anderson1b3dd492020-10-16 18:57:44 -0400231 dev_err(bus, "Couldn't request gpio! (error %d)\n", ret);
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300232 return ret;
233 }
234
235 if (dm_gpio_is_valid(&priv->cs_gpio)) {
236 dm_gpio_set_dir_flags(&priv->cs_gpio,
237 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
238 }
239
Sean Anderson1b3dd492020-10-16 18:57:44 -0400240 dev_dbg(bus, "Using external gpio for CS management\n");
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300241#endif
242 return 0;
243}
244
Simon Glassd1998a92020-12-03 16:55:21 -0700245static int dw_spi_of_to_plat(struct udevice *bus)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100246{
Simon Glass0fd3d912020-12-22 19:30:28 -0700247 struct dw_spi_plat *plat = dev_get_plat(bus);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100248
Masahiro Yamada8613c8d2020-07-17 14:36:46 +0900249 plat->regs = dev_read_addr_ptr(bus);
Sean Andersonc785f432020-10-16 18:57:46 -0400250 if (!plat->regs)
251 return -EINVAL;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100252
253 /* Use 500KHz as a suitable default */
Simon Goldschmidt27c3e072019-05-09 22:11:57 +0200254 plat->frequency = dev_read_u32_default(bus, "spi-max-frequency",
255 500000);
Sean Anderson58875792020-10-16 18:57:51 -0400256
257 if (dev_read_bool(bus, "spi-slave"))
258 return -EINVAL;
259
Sean Anderson1b3dd492020-10-16 18:57:44 -0400260 dev_info(bus, "max-frequency=%d\n", plat->frequency);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100261
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300262 return request_gpio_cs(bus);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100263}
264
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100265/* Restart the controller, disable all interrupts, clean rx fifo */
Sean Anderson1b3dd492020-10-16 18:57:44 -0400266static void spi_hw_init(struct udevice *bus, struct dw_spi_priv *priv)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100267{
Sean Anderson934beab2020-10-16 18:57:48 -0400268 dw_write(priv, DW_SPI_SSIENR, 0);
Sean Andersonbae4d9f2022-03-01 10:35:43 +0000269 dw_write(priv, DW_SPI_IMR, 0);
Sean Anderson934beab2020-10-16 18:57:48 -0400270 dw_write(priv, DW_SPI_SSIENR, 1);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100271
272 /*
273 * Try to detect the FIFO depth if not set by interface driver,
274 * the depth could be from 2 to 256 from HW spec
275 */
276 if (!priv->fifo_len) {
277 u32 fifo;
278
Axel Lin52091ad2015-02-26 10:45:22 +0800279 for (fifo = 1; fifo < 256; fifo++) {
Sean Anderson30040342020-10-16 18:57:47 -0400280 dw_write(priv, DW_SPI_TXFTLR, fifo);
281 if (fifo != dw_read(priv, DW_SPI_TXFTLR))
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100282 break;
283 }
284
Axel Lin52091ad2015-02-26 10:45:22 +0800285 priv->fifo_len = (fifo == 1) ? 0 : fifo;
Sean Anderson30040342020-10-16 18:57:47 -0400286 dw_write(priv, DW_SPI_TXFTLR, 0);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100287 }
Sean Anderson1b3dd492020-10-16 18:57:44 -0400288 dev_dbg(bus, "fifo_len=%d\n", priv->fifo_len);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100289}
290
Eugeniy Paltsev58c125b2017-12-28 15:09:03 +0300291/*
292 * We define dw_spi_get_clk function as 'weak' as some targets
293 * (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) don't use standard clock API
294 * and implement dw_spi_get_clk their own way in their clock manager.
295 */
296__weak int dw_spi_get_clk(struct udevice *bus, ulong *rate)
297{
298 struct dw_spi_priv *priv = dev_get_priv(bus);
299 int ret;
300
301 ret = clk_get_by_index(bus, 0, &priv->clk);
302 if (ret)
303 return ret;
304
305 ret = clk_enable(&priv->clk);
306 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
307 return ret;
308
309 *rate = clk_get_rate(&priv->clk);
310 if (!*rate)
311 goto err_rate;
312
Sean Anderson1b3dd492020-10-16 18:57:44 -0400313 dev_dbg(bus, "Got clock via device tree: %lu Hz\n", *rate);
Eugeniy Paltsev58c125b2017-12-28 15:09:03 +0300314
315 return 0;
316
317err_rate:
318 clk_disable(&priv->clk);
319 clk_free(&priv->clk);
320
321 return -EINVAL;
322}
323
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800324static int dw_spi_reset(struct udevice *bus)
325{
326 int ret;
327 struct dw_spi_priv *priv = dev_get_priv(bus);
328
329 ret = reset_get_bulk(bus, &priv->resets);
330 if (ret) {
331 /*
332 * Return 0 if error due to !CONFIG_DM_RESET and reset
333 * DT property is not present.
334 */
335 if (ret == -ENOENT || ret == -ENOTSUPP)
336 return 0;
337
Sean Anderson1b3dd492020-10-16 18:57:44 -0400338 dev_warn(bus, "Couldn't find/assert reset device (error %d)\n",
339 ret);
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800340 return ret;
341 }
342
343 ret = reset_deassert_bulk(&priv->resets);
344 if (ret) {
345 reset_release_bulk(&priv->resets);
Sean Anderson1b3dd492020-10-16 18:57:44 -0400346 dev_err(bus, "Failed to de-assert reset for SPI (error %d)\n",
347 ret);
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800348 return ret;
349 }
350
351 return 0;
352}
353
Sean Anderson58875792020-10-16 18:57:51 -0400354typedef int (*dw_spi_init_t)(struct udevice *bus, struct dw_spi_priv *priv);
355
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100356static int dw_spi_probe(struct udevice *bus)
357{
Sean Anderson58875792020-10-16 18:57:51 -0400358 dw_spi_init_t init = (dw_spi_init_t)dev_get_driver_data(bus);
Simon Glass8a8d24b2020-12-03 16:55:23 -0700359 struct dw_spi_plat *plat = dev_get_plat(bus);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100360 struct dw_spi_priv *priv = dev_get_priv(bus);
Eugeniy Paltsev58c125b2017-12-28 15:09:03 +0300361 int ret;
Sean Anderson58875792020-10-16 18:57:51 -0400362 u32 version;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100363
364 priv->regs = plat->regs;
365 priv->freq = plat->frequency;
366
Eugeniy Paltsev58c125b2017-12-28 15:09:03 +0300367 ret = dw_spi_get_clk(bus, &priv->bus_clk_rate);
368 if (ret)
369 return ret;
370
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800371 ret = dw_spi_reset(bus);
372 if (ret)
373 return ret;
374
Sean Anderson58875792020-10-16 18:57:51 -0400375 if (!init)
376 return -EINVAL;
377 ret = init(bus, priv);
378 if (ret)
379 return ret;
380
381 version = dw_read(priv, DW_SPI_VERSION);
382 dev_dbg(bus, "ssi_version_id=%c.%c%c%c ssi_max_xfer_size=%u\n",
383 version >> 24, version >> 16, version >> 8, version,
384 priv->max_xfer);
385
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100386 /* Currently only bits_per_word == 8 supported */
387 priv->bits_per_word = 8;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100388
389 priv->tmode = 0; /* Tx & Rx */
390
391 /* Basic HW init */
Sean Anderson1b3dd492020-10-16 18:57:44 -0400392 spi_hw_init(bus, priv);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100393
394 return 0;
395}
396
397/* Return the max entries we can fill into tx fifo */
398static inline u32 tx_max(struct dw_spi_priv *priv)
399{
400 u32 tx_left, tx_room, rxtx_gap;
401
Stefan Roesea72f8022014-11-16 12:47:01 +0100402 tx_left = (priv->tx_end - priv->tx) / (priv->bits_per_word >> 3);
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300403 tx_room = priv->fifo_len - dw_read(priv, DW_SPI_TXFLR);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100404
405 /*
406 * Another concern is about the tx/rx mismatch, we
Stefan Roesea72f8022014-11-16 12:47:01 +0100407 * thought about using (priv->fifo_len - rxflr - txflr) as
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100408 * one maximum value for tx, but it doesn't cover the
409 * data which is out of tx/rx fifo and inside the
410 * shift registers. So a control from sw point of
411 * view is taken.
412 */
413 rxtx_gap = ((priv->rx_end - priv->rx) - (priv->tx_end - priv->tx)) /
Stefan Roesea72f8022014-11-16 12:47:01 +0100414 (priv->bits_per_word >> 3);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100415
416 return min3(tx_left, tx_room, (u32)(priv->fifo_len - rxtx_gap));
417}
418
419/* Return the max entries we should read out of rx fifo */
420static inline u32 rx_max(struct dw_spi_priv *priv)
421{
Stefan Roesea72f8022014-11-16 12:47:01 +0100422 u32 rx_left = (priv->rx_end - priv->rx) / (priv->bits_per_word >> 3);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100423
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300424 return min_t(u32, rx_left, dw_read(priv, DW_SPI_RXFLR));
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100425}
426
427static void dw_writer(struct dw_spi_priv *priv)
428{
429 u32 max = tx_max(priv);
Sean Anderson58875792020-10-16 18:57:51 -0400430 u32 txw = 0xFFFFFFFF;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100431
432 while (max--) {
433 /* Set the tx word if the transfer's original "tx" is not null */
434 if (priv->tx_end - priv->len) {
Stefan Roesea72f8022014-11-16 12:47:01 +0100435 if (priv->bits_per_word == 8)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100436 txw = *(u8 *)(priv->tx);
437 else
438 txw = *(u16 *)(priv->tx);
439 }
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300440 dw_write(priv, DW_SPI_DR, txw);
Sean Anderson1b3dd492020-10-16 18:57:44 -0400441 log_content("tx=0x%02x\n", txw);
Stefan Roesea72f8022014-11-16 12:47:01 +0100442 priv->tx += priv->bits_per_word >> 3;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100443 }
444}
445
Eugeniy Paltsevd3d8aae2018-03-22 13:50:45 +0300446static void dw_reader(struct dw_spi_priv *priv)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100447{
Eugeniy Paltsevd3d8aae2018-03-22 13:50:45 +0300448 u32 max = rx_max(priv);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100449 u16 rxw;
450
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100451 while (max--) {
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300452 rxw = dw_read(priv, DW_SPI_DR);
Sean Anderson1b3dd492020-10-16 18:57:44 -0400453 log_content("rx=0x%02x\n", rxw);
Stefan Roesea72f8022014-11-16 12:47:01 +0100454
Eugeniy Paltsevd3d8aae2018-03-22 13:50:45 +0300455 /* Care about rx if the transfer's original "rx" is not null */
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100456 if (priv->rx_end - priv->len) {
Stefan Roesea72f8022014-11-16 12:47:01 +0100457 if (priv->bits_per_word == 8)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100458 *(u8 *)(priv->rx) = rxw;
459 else
460 *(u16 *)(priv->rx) = rxw;
461 }
Stefan Roesea72f8022014-11-16 12:47:01 +0100462 priv->rx += priv->bits_per_word >> 3;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100463 }
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100464}
465
466static int poll_transfer(struct dw_spi_priv *priv)
467{
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100468 do {
469 dw_writer(priv);
Eugeniy Paltsevd3d8aae2018-03-22 13:50:45 +0300470 dw_reader(priv);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100471 } while (priv->rx_end > priv->rx);
472
473 return 0;
474}
475
Gregory CLEMENTbea91b02018-10-09 14:14:07 +0200476/*
477 * We define external_cs_manage function as 'weak' as some targets
478 * (like MSCC Ocelot) don't control the external CS pin using a GPIO
479 * controller. These SoCs use specific registers to control by
480 * software the SPI pins (and especially the CS).
481 */
482__weak void external_cs_manage(struct udevice *dev, bool on)
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300483{
Simon Glassbcee8d62019-12-06 21:41:35 -0700484#if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD)
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300485 struct dw_spi_priv *priv = dev_get_priv(dev->parent);
486
487 if (!dm_gpio_is_valid(&priv->cs_gpio))
488 return;
489
490 dm_gpio_set_value(&priv->cs_gpio, on ? 1 : 0);
491#endif
492}
493
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100494static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
495 const void *dout, void *din, unsigned long flags)
496{
497 struct udevice *bus = dev->parent;
498 struct dw_spi_priv *priv = dev_get_priv(bus);
499 const u8 *tx = dout;
500 u8 *rx = din;
501 int ret = 0;
502 u32 cr0 = 0;
Eugeniy Paltsevc6b4f032018-03-22 13:50:43 +0300503 u32 val;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100504 u32 cs;
505
506 /* spi core configured to do 8 bit transfers */
507 if (bitlen % 8) {
Sean Anderson1b3dd492020-10-16 18:57:44 -0400508 dev_err(dev, "Non byte aligned SPI transfer.\n");
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100509 return -1;
510 }
511
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300512 /* Start the transaction if necessary. */
513 if (flags & SPI_XFER_BEGIN)
514 external_cs_manage(dev, false);
515
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100516 if (rx && tx)
Sean Anderson58875792020-10-16 18:57:51 -0400517 priv->tmode = CTRLR0_TMOD_TR;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100518 else if (rx)
Sean Anderson58875792020-10-16 18:57:51 -0400519 priv->tmode = CTRLR0_TMOD_RO;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100520 else
Eugeniy Paltsevfc282c72018-03-22 13:50:44 +0300521 /*
Sean Anderson58875792020-10-16 18:57:51 -0400522 * In transmit only mode (CTRL0_TMOD_TO) input FIFO never gets
Eugeniy Paltsevfc282c72018-03-22 13:50:44 +0300523 * any data which breaks our logic in poll_transfer() above.
524 */
Sean Anderson58875792020-10-16 18:57:51 -0400525 priv->tmode = CTRLR0_TMOD_TR;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100526
Sean Anderson58875792020-10-16 18:57:51 -0400527 cr0 = priv->update_cr0(priv);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100528
Stefan Roesea72f8022014-11-16 12:47:01 +0100529 priv->len = bitlen >> 3;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100530
531 priv->tx = (void *)tx;
532 priv->tx_end = priv->tx + priv->len;
533 priv->rx = rx;
534 priv->rx_end = priv->rx + priv->len;
535
536 /* Disable controller before writing control registers */
Sean Anderson934beab2020-10-16 18:57:48 -0400537 dw_write(priv, DW_SPI_SSIENR, 0);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100538
Sean Anderson1b3dd492020-10-16 18:57:44 -0400539 dev_dbg(dev, "cr0=%08x rx=%p tx=%p len=%d [bytes]\n", cr0, rx, tx,
540 priv->len);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100541 /* Reprogram cr0 only if changed */
Sean Anderson30040342020-10-16 18:57:47 -0400542 if (dw_read(priv, DW_SPI_CTRLR0) != cr0)
543 dw_write(priv, DW_SPI_CTRLR0, cr0);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100544
545 /*
546 * Configure the desired SS (slave select 0...3) in the controller
547 * The DW SPI controller will activate and deactivate this CS
548 * automatically. So no cs_activate() etc is needed in this driver.
549 */
550 cs = spi_chip_select(dev);
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300551 dw_write(priv, DW_SPI_SER, 1 << cs);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100552
553 /* Enable controller after writing control registers */
Sean Anderson934beab2020-10-16 18:57:48 -0400554 dw_write(priv, DW_SPI_SSIENR, 1);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100555
556 /* Start transfer in a polling loop */
557 ret = poll_transfer(priv);
558
Eugeniy Paltsevc6b4f032018-03-22 13:50:43 +0300559 /*
560 * Wait for current transmit operation to complete.
561 * Otherwise if some data still exists in Tx FIFO it can be
562 * silently flushed, i.e. dropped on disabling of the controller,
563 * which happens when writing 0 to DW_SPI_SSIENR which happens
564 * in the beginning of new transfer.
565 */
566 if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
Eugeniy Paltsev9b14ac52018-04-19 17:47:41 +0300567 (val & SR_TF_EMPT) && !(val & SR_BUSY),
Eugeniy Paltsevc6b4f032018-03-22 13:50:43 +0300568 RX_TIMEOUT * 1000)) {
569 ret = -ETIMEDOUT;
570 }
571
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300572 /* Stop the transaction if necessary */
573 if (flags & SPI_XFER_END)
574 external_cs_manage(dev, true);
575
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100576 return ret;
577}
578
Sean Andersonfec7bf02020-10-16 18:57:53 -0400579/*
580 * This function is necessary for reading SPI flash with the native CS
581 * c.f. https://lkml.org/lkml/2015/12/23/132
582 */
583static int dw_spi_exec_op(struct spi_slave *slave, const struct spi_mem_op *op)
584{
585 bool read = op->data.dir == SPI_MEM_DATA_IN;
586 int pos, i, ret = 0;
587 struct udevice *bus = slave->dev->parent;
588 struct dw_spi_priv *priv = dev_get_priv(bus);
Niklas Casseld56dfc92022-02-08 22:52:43 +0000589 u8 op_len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
Sean Andersonfec7bf02020-10-16 18:57:53 -0400590 u8 op_buf[op_len];
591 u32 cr0;
592
593 if (read)
594 priv->tmode = CTRLR0_TMOD_EPROMREAD;
595 else
596 priv->tmode = CTRLR0_TMOD_TO;
597
598 cr0 = priv->update_cr0(priv);
599 dev_dbg(bus, "cr0=%08x buf=%p len=%u [bytes]\n", cr0, op->data.buf.in,
600 op->data.nbytes);
601
602 dw_write(priv, DW_SPI_SSIENR, 0);
603 dw_write(priv, DW_SPI_CTRLR0, cr0);
604 if (read)
605 dw_write(priv, DW_SPI_CTRLR1, op->data.nbytes - 1);
606 dw_write(priv, DW_SPI_SSIENR, 1);
607
608 /* From spi_mem_exec_op */
609 pos = 0;
610 op_buf[pos++] = op->cmd.opcode;
611 if (op->addr.nbytes) {
612 for (i = 0; i < op->addr.nbytes; i++)
613 op_buf[pos + i] = op->addr.val >>
614 (8 * (op->addr.nbytes - i - 1));
615
616 pos += op->addr.nbytes;
617 }
618 if (op->dummy.nbytes)
619 memset(op_buf + pos, 0xff, op->dummy.nbytes);
620
621 external_cs_manage(slave->dev, false);
622
623 priv->tx = &op_buf;
624 priv->tx_end = priv->tx + op_len;
625 priv->rx = NULL;
626 priv->rx_end = NULL;
627 while (priv->tx != priv->tx_end)
628 dw_writer(priv);
629
630 /*
631 * XXX: The following are tight loops! Enabling debug messages may cause
632 * them to fail because we are not reading/writing the fifo fast enough.
633 */
634 if (read) {
635 priv->rx = op->data.buf.in;
636 priv->rx_end = priv->rx + op->data.nbytes;
637
638 dw_write(priv, DW_SPI_SER, 1 << spi_chip_select(slave->dev));
639 while (priv->rx != priv->rx_end)
640 dw_reader(priv);
641 } else {
642 u32 val;
643
644 priv->tx = op->data.buf.out;
645 priv->tx_end = priv->tx + op->data.nbytes;
646
647 /* Fill up the write fifo before starting the transfer */
648 dw_writer(priv);
649 dw_write(priv, DW_SPI_SER, 1 << spi_chip_select(slave->dev));
650 while (priv->tx != priv->tx_end)
651 dw_writer(priv);
652
653 if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
654 (val & SR_TF_EMPT) && !(val & SR_BUSY),
655 RX_TIMEOUT * 1000)) {
656 ret = -ETIMEDOUT;
657 }
658 }
659
660 dw_write(priv, DW_SPI_SER, 0);
661 external_cs_manage(slave->dev, true);
662
663 dev_dbg(bus, "%u bytes xfered\n", op->data.nbytes);
664 return ret;
665}
666
667/* The size of ctrl1 limits data transfers to 64K */
668static int dw_spi_adjust_op_size(struct spi_slave *slave, struct spi_mem_op *op)
669{
670 op->data.nbytes = min(op->data.nbytes, (unsigned int)SZ_64K);
671
672 return 0;
673}
674
675static const struct spi_controller_mem_ops dw_spi_mem_ops = {
676 .exec_op = dw_spi_exec_op,
677 .adjust_op_size = dw_spi_adjust_op_size,
678};
679
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100680static int dw_spi_set_speed(struct udevice *bus, uint speed)
681{
Simon Glass0fd3d912020-12-22 19:30:28 -0700682 struct dw_spi_plat *plat = dev_get_plat(bus);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100683 struct dw_spi_priv *priv = dev_get_priv(bus);
684 u16 clk_div;
685
686 if (speed > plat->frequency)
687 speed = plat->frequency;
688
689 /* Disable controller before writing control registers */
Sean Anderson934beab2020-10-16 18:57:48 -0400690 dw_write(priv, DW_SPI_SSIENR, 0);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100691
692 /* clk_div doesn't support odd number */
Eugeniy Paltsev58c125b2017-12-28 15:09:03 +0300693 clk_div = priv->bus_clk_rate / speed;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100694 clk_div = (clk_div + 1) & 0xfffe;
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300695 dw_write(priv, DW_SPI_BAUDR, clk_div);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100696
697 /* Enable controller after writing control registers */
Sean Anderson934beab2020-10-16 18:57:48 -0400698 dw_write(priv, DW_SPI_SSIENR, 1);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100699
700 priv->freq = speed;
Sean Anderson1b3dd492020-10-16 18:57:44 -0400701 dev_dbg(bus, "speed=%d clk_div=%d\n", priv->freq, clk_div);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100702
703 return 0;
704}
705
706static int dw_spi_set_mode(struct udevice *bus, uint mode)
707{
708 struct dw_spi_priv *priv = dev_get_priv(bus);
709
710 /*
711 * Can't set mode yet. Since this depends on if rx, tx, or
712 * rx & tx is requested. So we have to defer this to the
713 * real transfer function.
714 */
715 priv->mode = mode;
Sean Anderson1b3dd492020-10-16 18:57:44 -0400716 dev_dbg(bus, "mode=%d\n", priv->mode);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100717
718 return 0;
719}
720
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800721static int dw_spi_remove(struct udevice *bus)
722{
723 struct dw_spi_priv *priv = dev_get_priv(bus);
Ley Foon Tane7e05fc2018-09-19 16:27:19 +0800724 int ret;
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800725
Ley Foon Tane7e05fc2018-09-19 16:27:19 +0800726 ret = reset_release_bulk(&priv->resets);
727 if (ret)
728 return ret;
729
730#if CONFIG_IS_ENABLED(CLK)
731 ret = clk_disable(&priv->clk);
732 if (ret)
733 return ret;
734
Sean Anderson3cbdd4c2022-01-15 17:25:03 -0500735 clk_free(&priv->clk);
Ley Foon Tane7e05fc2018-09-19 16:27:19 +0800736 if (ret)
737 return ret;
738#endif
739 return 0;
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800740}
741
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100742static const struct dm_spi_ops dw_spi_ops = {
743 .xfer = dw_spi_xfer,
Sean Andersonfec7bf02020-10-16 18:57:53 -0400744 .mem_ops = &dw_spi_mem_ops,
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100745 .set_speed = dw_spi_set_speed,
746 .set_mode = dw_spi_set_mode,
747 /*
748 * cs_info is not needed, since we require all chip selects to be
749 * in the device tree explicitly
750 */
751};
752
753static const struct udevice_id dw_spi_ids[] = {
Sean Anderson58875792020-10-16 18:57:51 -0400754 /* Generic compatible strings */
755
756 { .compatible = "snps,dw-apb-ssi", .data = (ulong)dw_spi_apb_init },
757 { .compatible = "snps,dw-apb-ssi-3.20a", .data = (ulong)dw_spi_apb_init },
758 { .compatible = "snps,dw-apb-ssi-3.22a", .data = (ulong)dw_spi_apb_init },
759 /* First version with SSI_MAX_XFER_SIZE */
760 { .compatible = "snps,dw-apb-ssi-3.23a", .data = (ulong)dw_spi_apb_init },
761 /* First version with Dual/Quad SPI; unused by this driver */
762 { .compatible = "snps,dw-apb-ssi-4.00a", .data = (ulong)dw_spi_apb_init },
763 { .compatible = "snps,dw-apb-ssi-4.01", .data = (ulong)dw_spi_apb_init },
764 { .compatible = "snps,dwc-ssi-1.01a", .data = (ulong)dw_spi_dwc_init },
765
766 /* Compatible strings for specific SoCs */
767
768 /*
769 * Both the Cyclone V and Arria V share a device tree and have the same
770 * version of this device. This compatible string is used for those
771 * devices, and is not used for sofpgas in general.
772 */
773 { .compatible = "altr,socfpga-spi", .data = (ulong)dw_spi_apb_init },
774 { .compatible = "altr,socfpga-arria10-spi", .data = (ulong)dw_spi_apb_init },
Damien Le Moal40b64352022-03-01 10:35:43 +0000775 { .compatible = "canaan,k210-spi", .data = (ulong)dw_spi_apb_k210_init},
Damien Le Moalfd426b32022-03-01 10:35:39 +0000776 { .compatible = "canaan,k210-ssi", .data = (ulong)dw_spi_dwc_init },
Sean Anderson58875792020-10-16 18:57:51 -0400777 { .compatible = "intel,stratix10-spi", .data = (ulong)dw_spi_apb_init },
778 { .compatible = "intel,agilex-spi", .data = (ulong)dw_spi_apb_init },
779 { .compatible = "mscc,ocelot-spi", .data = (ulong)dw_spi_apb_init },
780 { .compatible = "mscc,jaguar2-spi", .data = (ulong)dw_spi_apb_init },
781 { .compatible = "snps,axs10x-spi", .data = (ulong)dw_spi_apb_init },
782 { .compatible = "snps,hsdk-spi", .data = (ulong)dw_spi_apb_init },
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100783 { }
784};
785
786U_BOOT_DRIVER(dw_spi) = {
787 .name = "dw_spi",
788 .id = UCLASS_SPI,
789 .of_match = dw_spi_ids,
790 .ops = &dw_spi_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700791 .of_to_plat = dw_spi_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700792 .plat_auto = sizeof(struct dw_spi_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700793 .priv_auto = sizeof(struct dw_spi_priv),
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100794 .probe = dw_spi_probe,
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800795 .remove = dw_spi_remove,
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100796};