blob: 9e02fce6c611831763c78604b828d0de59851bd5 [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
Stefan Roesed987ea62014-11-07 13:50:31 +010014#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Horatiu.Vultur@microchip.com340e5b32019-02-25 10:59:54 +000016#include <asm-generic/gpio.h>
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +030017#include <clk.h>
Stefan Roesed987ea62014-11-07 13:50:31 +010018#include <dm.h>
19#include <errno.h>
20#include <malloc.h>
21#include <spi.h>
22#include <fdtdec.h>
Ley Foon Tanfc3382d2018-09-07 14:25:29 +080023#include <reset.h>
Simon Glass9bc15642020-02-03 07:36:16 -070024#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#include <linux/bitops.h>
Sean Andersondebbff82020-10-16 18:57:51 -040026#include <linux/bitfield.h>
Stefan Roesed987ea62014-11-07 13:50:31 +010027#include <linux/compat.h>
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +030028#include <linux/iopoll.h>
Stefan Roesed987ea62014-11-07 13:50:31 +010029#include <asm/io.h>
30
Stefan Roesed987ea62014-11-07 13:50:31 +010031/* Register offsets */
Sean Anderson75ea2f62020-10-16 18:57:47 -040032#define DW_SPI_CTRLR0 0x00
33#define DW_SPI_CTRLR1 0x04
Stefan Roesed987ea62014-11-07 13:50:31 +010034#define DW_SPI_SSIENR 0x08
35#define DW_SPI_MWCR 0x0c
36#define DW_SPI_SER 0x10
37#define DW_SPI_BAUDR 0x14
Sean Anderson75ea2f62020-10-16 18:57:47 -040038#define DW_SPI_TXFTLR 0x18
39#define DW_SPI_RXFTLR 0x1c
Stefan Roesed987ea62014-11-07 13:50:31 +010040#define DW_SPI_TXFLR 0x20
41#define DW_SPI_RXFLR 0x24
42#define DW_SPI_SR 0x28
43#define DW_SPI_IMR 0x2c
44#define DW_SPI_ISR 0x30
45#define DW_SPI_RISR 0x34
46#define DW_SPI_TXOICR 0x38
47#define DW_SPI_RXOICR 0x3c
48#define DW_SPI_RXUICR 0x40
49#define DW_SPI_MSTICR 0x44
50#define DW_SPI_ICR 0x48
51#define DW_SPI_DMACR 0x4c
52#define DW_SPI_DMATDLR 0x50
53#define DW_SPI_DMARDLR 0x54
54#define DW_SPI_IDR 0x58
55#define DW_SPI_VERSION 0x5c
56#define DW_SPI_DR 0x60
57
58/* Bit fields in CTRLR0 */
Sean Andersondebbff82020-10-16 18:57:51 -040059/*
60 * Only present when SSI_MAX_XFER_SIZE=16. This is the default, and the only
61 * option before version 3.23a.
62 */
63#define CTRLR0_DFS_MASK GENMASK(3, 0)
64
65#define CTRLR0_FRF_MASK GENMASK(5, 4)
66#define CTRLR0_FRF_SPI 0x0
67#define CTRLR0_FRF_SSP 0x1
68#define CTRLR0_FRF_MICROWIRE 0x2
69#define CTRLR0_FRF_RESV 0x3
Stefan Roesed987ea62014-11-07 13:50:31 +010070
Sean Andersondebbff82020-10-16 18:57:51 -040071#define CTRLR0_MODE_MASK GENMASK(7, 6)
72#define CTRLR0_MODE_SCPH 0x1
73#define CTRLR0_MODE_SCPOL 0x2
Stefan Roesed987ea62014-11-07 13:50:31 +010074
Sean Andersondebbff82020-10-16 18:57:51 -040075#define CTRLR0_TMOD_MASK GENMASK(9, 8)
76#define CTRLR0_TMOD_TR 0x0 /* xmit & recv */
77#define CTRLR0_TMOD_TO 0x1 /* xmit only */
78#define CTRLR0_TMOD_RO 0x2 /* recv only */
79#define CTRLR0_TMOD_EPROMREAD 0x3 /* eeprom read mode */
Stefan Roesed987ea62014-11-07 13:50:31 +010080
Sean Andersondebbff82020-10-16 18:57:51 -040081#define CTRLR0_SLVOE_OFFSET 10
82#define CTRLR0_SRL_OFFSET 11
83#define CTRLR0_CFS_MASK GENMASK(15, 12)
Stefan Roesed987ea62014-11-07 13:50:31 +010084
Sean Andersondebbff82020-10-16 18:57:51 -040085/* Only present when SSI_MAX_XFER_SIZE=32 */
86#define CTRLR0_DFS_32_MASK GENMASK(20, 16)
87
88/* The next field is only present on versions after 4.00a */
89#define CTRLR0_SPI_FRF_MASK GENMASK(22, 21)
90#define CTRLR0_SPI_FRF_BYTE 0x0
91#define CTRLR0_SPI_FRF_DUAL 0x1
92#define CTRLR0_SPI_FRF_QUAD 0x2
93
94/* Bit fields in CTRLR0 based on DWC_ssi_databook.pdf v1.01a */
95#define DWC_SSI_CTRLR0_DFS_MASK GENMASK(4, 0)
96#define DWC_SSI_CTRLR0_FRF_MASK GENMASK(7, 6)
97#define DWC_SSI_CTRLR0_MODE_MASK GENMASK(9, 8)
98#define DWC_SSI_CTRLR0_TMOD_MASK GENMASK(11, 10)
99#define DWC_SSI_CTRLR0_SRL_OFFSET 13
100#define DWC_SSI_CTRLR0_SPI_FRF_MASK GENMASK(23, 22)
Stefan Roesed987ea62014-11-07 13:50:31 +0100101
102/* Bit fields in SR, 7 bits */
Jagan Tekifac44912015-10-23 01:01:36 +0530103#define SR_MASK GENMASK(6, 0) /* cover 7 bits */
Jagan Tekib17746d2015-10-23 01:36:23 +0530104#define SR_BUSY BIT(0)
105#define SR_TF_NOT_FULL BIT(1)
106#define SR_TF_EMPT BIT(2)
107#define SR_RF_NOT_EMPT BIT(3)
108#define SR_RF_FULL BIT(4)
109#define SR_TX_ERR BIT(5)
110#define SR_DCOL BIT(6)
Stefan Roesed987ea62014-11-07 13:50:31 +0100111
Stefan Roese571e2a42014-11-16 12:47:01 +0100112#define RX_TIMEOUT 1000 /* timeout in ms */
Stefan Roesed987ea62014-11-07 13:50:31 +0100113
114struct dw_spi_platdata {
115 s32 frequency; /* Default clock frequency, -1 for none */
116 void __iomem *regs;
117};
118
119struct dw_spi_priv {
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300120 struct clk clk;
Sean Andersone11c0b12020-10-16 18:57:49 -0400121 struct reset_ctl_bulk resets;
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300122 struct gpio_desc cs_gpio; /* External chip-select gpio */
123
Sean Andersondebbff82020-10-16 18:57:51 -0400124 u32 (*update_cr0)(struct dw_spi_priv *priv);
125
Sean Andersone11c0b12020-10-16 18:57:49 -0400126 void __iomem *regs;
127 unsigned long bus_clk_rate;
128 unsigned int freq; /* Default frequency */
129 unsigned int mode;
Stefan Roesed987ea62014-11-07 13:50:31 +0100130
Sean Andersone11c0b12020-10-16 18:57:49 -0400131 const void *tx;
132 const void *tx_end;
Stefan Roesed987ea62014-11-07 13:50:31 +0100133 void *rx;
134 void *rx_end;
Sean Andersone11c0b12020-10-16 18:57:49 -0400135 u32 fifo_len; /* depth of the FIFO buffer */
Sean Andersondebbff82020-10-16 18:57:51 -0400136 u32 max_xfer; /* Maximum transfer size (in bits) */
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800137
Sean Andersone11c0b12020-10-16 18:57:49 -0400138 int bits_per_word;
139 int len;
140 u8 cs; /* chip select pin */
141 u8 tmode; /* TR/TO/RO/EEPROM */
142 u8 type; /* SPI/SSP/MicroWire */
Stefan Roesed987ea62014-11-07 13:50:31 +0100143};
144
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300145static inline u32 dw_read(struct dw_spi_priv *priv, u32 offset)
Stefan Roesed987ea62014-11-07 13:50:31 +0100146{
147 return __raw_readl(priv->regs + offset);
148}
149
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300150static inline void dw_write(struct dw_spi_priv *priv, u32 offset, u32 val)
Stefan Roesed987ea62014-11-07 13:50:31 +0100151{
152 __raw_writel(val, priv->regs + offset);
153}
154
Sean Andersondebbff82020-10-16 18:57:51 -0400155static u32 dw_spi_dw16_update_cr0(struct dw_spi_priv *priv)
156{
157 return FIELD_PREP(CTRLR0_DFS_MASK, priv->bits_per_word - 1)
158 | FIELD_PREP(CTRLR0_FRF_MASK, priv->type)
159 | FIELD_PREP(CTRLR0_MODE_MASK, priv->mode)
160 | FIELD_PREP(CTRLR0_TMOD_MASK, priv->tmode);
161}
162
163static u32 dw_spi_dw32_update_cr0(struct dw_spi_priv *priv)
164{
165 return FIELD_PREP(CTRLR0_DFS_32_MASK, priv->bits_per_word - 1)
166 | FIELD_PREP(CTRLR0_FRF_MASK, priv->type)
167 | FIELD_PREP(CTRLR0_MODE_MASK, priv->mode)
168 | FIELD_PREP(CTRLR0_TMOD_MASK, priv->tmode);
169}
170
171static u32 dw_spi_dwc_update_cr0(struct dw_spi_priv *priv)
172{
173 return FIELD_PREP(DWC_SSI_CTRLR0_DFS_MASK, priv->bits_per_word - 1)
174 | FIELD_PREP(DWC_SSI_CTRLR0_FRF_MASK, priv->type)
175 | FIELD_PREP(DWC_SSI_CTRLR0_MODE_MASK, priv->mode)
176 | FIELD_PREP(DWC_SSI_CTRLR0_TMOD_MASK, priv->tmode);
177}
178
179static int dw_spi_apb_init(struct udevice *bus, struct dw_spi_priv *priv)
180{
181 /* If we read zeros from DFS, then we need to use DFS_32 instead */
182 dw_write(priv, DW_SPI_SSIENR, 0);
183 dw_write(priv, DW_SPI_CTRLR0, 0xffffffff);
184 if (FIELD_GET(CTRLR0_DFS_MASK, dw_read(priv, DW_SPI_CTRLR0))) {
185 priv->max_xfer = 16;
186 priv->update_cr0 = dw_spi_dw16_update_cr0;
187 } else {
188 priv->max_xfer = 32;
189 priv->update_cr0 = dw_spi_dw32_update_cr0;
190 }
191
192 return 0;
193}
194
195static int dw_spi_dwc_init(struct udevice *bus, struct dw_spi_priv *priv)
196{
197 priv->max_xfer = 32;
198 priv->update_cr0 = dw_spi_dwc_update_cr0;
199 return 0;
200}
201
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300202static int request_gpio_cs(struct udevice *bus)
203{
Simon Glassfa4689a2019-12-06 21:41:35 -0700204#if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD)
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300205 struct dw_spi_priv *priv = dev_get_priv(bus);
206 int ret;
207
208 /* External chip select gpio line is optional */
Sean Anderson17f69fb2020-10-16 18:57:45 -0400209 ret = gpio_request_by_name(bus, "cs-gpios", 0, &priv->cs_gpio,
210 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300211 if (ret == -ENOENT)
212 return 0;
213
214 if (ret < 0) {
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400215 dev_err(bus, "Couldn't request gpio! (error %d)\n", ret);
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300216 return ret;
217 }
218
219 if (dm_gpio_is_valid(&priv->cs_gpio)) {
220 dm_gpio_set_dir_flags(&priv->cs_gpio,
221 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
222 }
223
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400224 dev_dbg(bus, "Using external gpio for CS management\n");
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300225#endif
226 return 0;
227}
228
Stefan Roesed987ea62014-11-07 13:50:31 +0100229static int dw_spi_ofdata_to_platdata(struct udevice *bus)
230{
231 struct dw_spi_platdata *plat = bus->platdata;
Stefan Roesed987ea62014-11-07 13:50:31 +0100232
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900233 plat->regs = dev_read_addr_ptr(bus);
Sean Anderson72097a72020-10-16 18:57:46 -0400234 if (!plat->regs)
235 return -EINVAL;
Stefan Roesed987ea62014-11-07 13:50:31 +0100236
237 /* Use 500KHz as a suitable default */
Simon Goldschmidt70cd31b2019-05-09 22:11:57 +0200238 plat->frequency = dev_read_u32_default(bus, "spi-max-frequency",
239 500000);
Sean Andersondebbff82020-10-16 18:57:51 -0400240
241 if (dev_read_bool(bus, "spi-slave"))
242 return -EINVAL;
243
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400244 dev_info(bus, "max-frequency=%d\n", plat->frequency);
Stefan Roesed987ea62014-11-07 13:50:31 +0100245
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300246 return request_gpio_cs(bus);
Stefan Roesed987ea62014-11-07 13:50:31 +0100247}
248
Stefan Roesed987ea62014-11-07 13:50:31 +0100249/* Restart the controller, disable all interrupts, clean rx fifo */
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400250static void spi_hw_init(struct udevice *bus, struct dw_spi_priv *priv)
Stefan Roesed987ea62014-11-07 13:50:31 +0100251{
Sean Andersonf2520822020-10-16 18:57:48 -0400252 dw_write(priv, DW_SPI_SSIENR, 0);
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300253 dw_write(priv, DW_SPI_IMR, 0xff);
Sean Andersonf2520822020-10-16 18:57:48 -0400254 dw_write(priv, DW_SPI_SSIENR, 1);
Stefan Roesed987ea62014-11-07 13:50:31 +0100255
256 /*
257 * Try to detect the FIFO depth if not set by interface driver,
258 * the depth could be from 2 to 256 from HW spec
259 */
260 if (!priv->fifo_len) {
261 u32 fifo;
262
Axel Lin83cfd372015-02-26 10:45:22 +0800263 for (fifo = 1; fifo < 256; fifo++) {
Sean Anderson75ea2f62020-10-16 18:57:47 -0400264 dw_write(priv, DW_SPI_TXFTLR, fifo);
265 if (fifo != dw_read(priv, DW_SPI_TXFTLR))
Stefan Roesed987ea62014-11-07 13:50:31 +0100266 break;
267 }
268
Axel Lin83cfd372015-02-26 10:45:22 +0800269 priv->fifo_len = (fifo == 1) ? 0 : fifo;
Sean Anderson75ea2f62020-10-16 18:57:47 -0400270 dw_write(priv, DW_SPI_TXFTLR, 0);
Stefan Roesed987ea62014-11-07 13:50:31 +0100271 }
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400272 dev_dbg(bus, "fifo_len=%d\n", priv->fifo_len);
Stefan Roesed987ea62014-11-07 13:50:31 +0100273}
274
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300275/*
276 * We define dw_spi_get_clk function as 'weak' as some targets
277 * (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) don't use standard clock API
278 * and implement dw_spi_get_clk their own way in their clock manager.
279 */
280__weak int dw_spi_get_clk(struct udevice *bus, ulong *rate)
281{
282 struct dw_spi_priv *priv = dev_get_priv(bus);
283 int ret;
284
285 ret = clk_get_by_index(bus, 0, &priv->clk);
286 if (ret)
287 return ret;
288
289 ret = clk_enable(&priv->clk);
290 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
291 return ret;
292
293 *rate = clk_get_rate(&priv->clk);
294 if (!*rate)
295 goto err_rate;
296
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400297 dev_dbg(bus, "Got clock via device tree: %lu Hz\n", *rate);
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300298
299 return 0;
300
301err_rate:
302 clk_disable(&priv->clk);
303 clk_free(&priv->clk);
304
305 return -EINVAL;
306}
307
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800308static int dw_spi_reset(struct udevice *bus)
309{
310 int ret;
311 struct dw_spi_priv *priv = dev_get_priv(bus);
312
313 ret = reset_get_bulk(bus, &priv->resets);
314 if (ret) {
315 /*
316 * Return 0 if error due to !CONFIG_DM_RESET and reset
317 * DT property is not present.
318 */
319 if (ret == -ENOENT || ret == -ENOTSUPP)
320 return 0;
321
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400322 dev_warn(bus, "Couldn't find/assert reset device (error %d)\n",
323 ret);
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800324 return ret;
325 }
326
327 ret = reset_deassert_bulk(&priv->resets);
328 if (ret) {
329 reset_release_bulk(&priv->resets);
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400330 dev_err(bus, "Failed to de-assert reset for SPI (error %d)\n",
331 ret);
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800332 return ret;
333 }
334
335 return 0;
336}
337
Sean Andersondebbff82020-10-16 18:57:51 -0400338typedef int (*dw_spi_init_t)(struct udevice *bus, struct dw_spi_priv *priv);
339
Stefan Roesed987ea62014-11-07 13:50:31 +0100340static int dw_spi_probe(struct udevice *bus)
341{
Sean Andersondebbff82020-10-16 18:57:51 -0400342 dw_spi_init_t init = (dw_spi_init_t)dev_get_driver_data(bus);
Stefan Roesed987ea62014-11-07 13:50:31 +0100343 struct dw_spi_platdata *plat = dev_get_platdata(bus);
344 struct dw_spi_priv *priv = dev_get_priv(bus);
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300345 int ret;
Sean Andersondebbff82020-10-16 18:57:51 -0400346 u32 version;
Stefan Roesed987ea62014-11-07 13:50:31 +0100347
348 priv->regs = plat->regs;
349 priv->freq = plat->frequency;
350
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300351 ret = dw_spi_get_clk(bus, &priv->bus_clk_rate);
352 if (ret)
353 return ret;
354
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800355 ret = dw_spi_reset(bus);
356 if (ret)
357 return ret;
358
Sean Andersondebbff82020-10-16 18:57:51 -0400359 if (!init)
360 return -EINVAL;
361 ret = init(bus, priv);
362 if (ret)
363 return ret;
364
365 version = dw_read(priv, DW_SPI_VERSION);
366 dev_dbg(bus, "ssi_version_id=%c.%c%c%c ssi_max_xfer_size=%u\n",
367 version >> 24, version >> 16, version >> 8, version,
368 priv->max_xfer);
369
Stefan Roesed987ea62014-11-07 13:50:31 +0100370 /* Currently only bits_per_word == 8 supported */
371 priv->bits_per_word = 8;
Stefan Roesed987ea62014-11-07 13:50:31 +0100372
373 priv->tmode = 0; /* Tx & Rx */
374
375 /* Basic HW init */
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400376 spi_hw_init(bus, priv);
Stefan Roesed987ea62014-11-07 13:50:31 +0100377
378 return 0;
379}
380
381/* Return the max entries we can fill into tx fifo */
382static inline u32 tx_max(struct dw_spi_priv *priv)
383{
384 u32 tx_left, tx_room, rxtx_gap;
385
Stefan Roese571e2a42014-11-16 12:47:01 +0100386 tx_left = (priv->tx_end - priv->tx) / (priv->bits_per_word >> 3);
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300387 tx_room = priv->fifo_len - dw_read(priv, DW_SPI_TXFLR);
Stefan Roesed987ea62014-11-07 13:50:31 +0100388
389 /*
390 * Another concern is about the tx/rx mismatch, we
Stefan Roese571e2a42014-11-16 12:47:01 +0100391 * thought about using (priv->fifo_len - rxflr - txflr) as
Stefan Roesed987ea62014-11-07 13:50:31 +0100392 * one maximum value for tx, but it doesn't cover the
393 * data which is out of tx/rx fifo and inside the
394 * shift registers. So a control from sw point of
395 * view is taken.
396 */
397 rxtx_gap = ((priv->rx_end - priv->rx) - (priv->tx_end - priv->tx)) /
Stefan Roese571e2a42014-11-16 12:47:01 +0100398 (priv->bits_per_word >> 3);
Stefan Roesed987ea62014-11-07 13:50:31 +0100399
400 return min3(tx_left, tx_room, (u32)(priv->fifo_len - rxtx_gap));
401}
402
403/* Return the max entries we should read out of rx fifo */
404static inline u32 rx_max(struct dw_spi_priv *priv)
405{
Stefan Roese571e2a42014-11-16 12:47:01 +0100406 u32 rx_left = (priv->rx_end - priv->rx) / (priv->bits_per_word >> 3);
Stefan Roesed987ea62014-11-07 13:50:31 +0100407
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300408 return min_t(u32, rx_left, dw_read(priv, DW_SPI_RXFLR));
Stefan Roesed987ea62014-11-07 13:50:31 +0100409}
410
411static void dw_writer(struct dw_spi_priv *priv)
412{
413 u32 max = tx_max(priv);
Sean Andersondebbff82020-10-16 18:57:51 -0400414 u32 txw = 0xFFFFFFFF;
Stefan Roesed987ea62014-11-07 13:50:31 +0100415
416 while (max--) {
417 /* Set the tx word if the transfer's original "tx" is not null */
418 if (priv->tx_end - priv->len) {
Stefan Roese571e2a42014-11-16 12:47:01 +0100419 if (priv->bits_per_word == 8)
Stefan Roesed987ea62014-11-07 13:50:31 +0100420 txw = *(u8 *)(priv->tx);
421 else
422 txw = *(u16 *)(priv->tx);
423 }
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300424 dw_write(priv, DW_SPI_DR, txw);
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400425 log_content("tx=0x%02x\n", txw);
Stefan Roese571e2a42014-11-16 12:47:01 +0100426 priv->tx += priv->bits_per_word >> 3;
Stefan Roesed987ea62014-11-07 13:50:31 +0100427 }
428}
429
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300430static void dw_reader(struct dw_spi_priv *priv)
Stefan Roesed987ea62014-11-07 13:50:31 +0100431{
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300432 u32 max = rx_max(priv);
Stefan Roesed987ea62014-11-07 13:50:31 +0100433 u16 rxw;
434
Stefan Roesed987ea62014-11-07 13:50:31 +0100435 while (max--) {
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300436 rxw = dw_read(priv, DW_SPI_DR);
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400437 log_content("rx=0x%02x\n", rxw);
Stefan Roese571e2a42014-11-16 12:47:01 +0100438
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300439 /* Care about rx if the transfer's original "rx" is not null */
Stefan Roesed987ea62014-11-07 13:50:31 +0100440 if (priv->rx_end - priv->len) {
Stefan Roese571e2a42014-11-16 12:47:01 +0100441 if (priv->bits_per_word == 8)
Stefan Roesed987ea62014-11-07 13:50:31 +0100442 *(u8 *)(priv->rx) = rxw;
443 else
444 *(u16 *)(priv->rx) = rxw;
445 }
Stefan Roese571e2a42014-11-16 12:47:01 +0100446 priv->rx += priv->bits_per_word >> 3;
Stefan Roesed987ea62014-11-07 13:50:31 +0100447 }
Stefan Roesed987ea62014-11-07 13:50:31 +0100448}
449
450static int poll_transfer(struct dw_spi_priv *priv)
451{
Stefan Roesed987ea62014-11-07 13:50:31 +0100452 do {
453 dw_writer(priv);
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300454 dw_reader(priv);
Stefan Roesed987ea62014-11-07 13:50:31 +0100455 } while (priv->rx_end > priv->rx);
456
457 return 0;
458}
459
Gregory CLEMENTf2893372018-10-09 14:14:07 +0200460/*
461 * We define external_cs_manage function as 'weak' as some targets
462 * (like MSCC Ocelot) don't control the external CS pin using a GPIO
463 * controller. These SoCs use specific registers to control by
464 * software the SPI pins (and especially the CS).
465 */
466__weak void external_cs_manage(struct udevice *dev, bool on)
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300467{
Simon Glassfa4689a2019-12-06 21:41:35 -0700468#if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD)
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300469 struct dw_spi_priv *priv = dev_get_priv(dev->parent);
470
471 if (!dm_gpio_is_valid(&priv->cs_gpio))
472 return;
473
474 dm_gpio_set_value(&priv->cs_gpio, on ? 1 : 0);
475#endif
476}
477
Stefan Roesed987ea62014-11-07 13:50:31 +0100478static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
479 const void *dout, void *din, unsigned long flags)
480{
481 struct udevice *bus = dev->parent;
482 struct dw_spi_priv *priv = dev_get_priv(bus);
483 const u8 *tx = dout;
484 u8 *rx = din;
485 int ret = 0;
486 u32 cr0 = 0;
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +0300487 u32 val;
Stefan Roesed987ea62014-11-07 13:50:31 +0100488 u32 cs;
489
490 /* spi core configured to do 8 bit transfers */
491 if (bitlen % 8) {
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400492 dev_err(dev, "Non byte aligned SPI transfer.\n");
Stefan Roesed987ea62014-11-07 13:50:31 +0100493 return -1;
494 }
495
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300496 /* Start the transaction if necessary. */
497 if (flags & SPI_XFER_BEGIN)
498 external_cs_manage(dev, false);
499
Stefan Roesed987ea62014-11-07 13:50:31 +0100500 if (rx && tx)
Sean Andersondebbff82020-10-16 18:57:51 -0400501 priv->tmode = CTRLR0_TMOD_TR;
Stefan Roesed987ea62014-11-07 13:50:31 +0100502 else if (rx)
Sean Andersondebbff82020-10-16 18:57:51 -0400503 priv->tmode = CTRLR0_TMOD_RO;
Stefan Roesed987ea62014-11-07 13:50:31 +0100504 else
Eugeniy Paltsev31f50132018-03-22 13:50:44 +0300505 /*
Sean Andersondebbff82020-10-16 18:57:51 -0400506 * In transmit only mode (CTRL0_TMOD_TO) input FIFO never gets
Eugeniy Paltsev31f50132018-03-22 13:50:44 +0300507 * any data which breaks our logic in poll_transfer() above.
508 */
Sean Andersondebbff82020-10-16 18:57:51 -0400509 priv->tmode = CTRLR0_TMOD_TR;
Stefan Roesed987ea62014-11-07 13:50:31 +0100510
Sean Andersondebbff82020-10-16 18:57:51 -0400511 cr0 = priv->update_cr0(priv);
Stefan Roesed987ea62014-11-07 13:50:31 +0100512
Stefan Roese571e2a42014-11-16 12:47:01 +0100513 priv->len = bitlen >> 3;
Stefan Roesed987ea62014-11-07 13:50:31 +0100514
515 priv->tx = (void *)tx;
516 priv->tx_end = priv->tx + priv->len;
517 priv->rx = rx;
518 priv->rx_end = priv->rx + priv->len;
519
520 /* Disable controller before writing control registers */
Sean Andersonf2520822020-10-16 18:57:48 -0400521 dw_write(priv, DW_SPI_SSIENR, 0);
Stefan Roesed987ea62014-11-07 13:50:31 +0100522
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400523 dev_dbg(dev, "cr0=%08x rx=%p tx=%p len=%d [bytes]\n", cr0, rx, tx,
524 priv->len);
Stefan Roesed987ea62014-11-07 13:50:31 +0100525 /* Reprogram cr0 only if changed */
Sean Anderson75ea2f62020-10-16 18:57:47 -0400526 if (dw_read(priv, DW_SPI_CTRLR0) != cr0)
527 dw_write(priv, DW_SPI_CTRLR0, cr0);
Stefan Roesed987ea62014-11-07 13:50:31 +0100528
529 /*
530 * Configure the desired SS (slave select 0...3) in the controller
531 * The DW SPI controller will activate and deactivate this CS
532 * automatically. So no cs_activate() etc is needed in this driver.
533 */
534 cs = spi_chip_select(dev);
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300535 dw_write(priv, DW_SPI_SER, 1 << cs);
Stefan Roesed987ea62014-11-07 13:50:31 +0100536
537 /* Enable controller after writing control registers */
Sean Andersonf2520822020-10-16 18:57:48 -0400538 dw_write(priv, DW_SPI_SSIENR, 1);
Stefan Roesed987ea62014-11-07 13:50:31 +0100539
540 /* Start transfer in a polling loop */
541 ret = poll_transfer(priv);
542
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +0300543 /*
544 * Wait for current transmit operation to complete.
545 * Otherwise if some data still exists in Tx FIFO it can be
546 * silently flushed, i.e. dropped on disabling of the controller,
547 * which happens when writing 0 to DW_SPI_SSIENR which happens
548 * in the beginning of new transfer.
549 */
550 if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
Eugeniy Paltsev208be8f2018-04-19 17:47:41 +0300551 (val & SR_TF_EMPT) && !(val & SR_BUSY),
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +0300552 RX_TIMEOUT * 1000)) {
553 ret = -ETIMEDOUT;
554 }
555
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300556 /* Stop the transaction if necessary */
557 if (flags & SPI_XFER_END)
558 external_cs_manage(dev, true);
559
Stefan Roesed987ea62014-11-07 13:50:31 +0100560 return ret;
561}
562
563static int dw_spi_set_speed(struct udevice *bus, uint speed)
564{
Sean Andersondebbff82020-10-16 18:57:51 -0400565 struct dw_spi_platdata *plat = dev_get_platdata(bus);
Stefan Roesed987ea62014-11-07 13:50:31 +0100566 struct dw_spi_priv *priv = dev_get_priv(bus);
567 u16 clk_div;
568
569 if (speed > plat->frequency)
570 speed = plat->frequency;
571
572 /* Disable controller before writing control registers */
Sean Andersonf2520822020-10-16 18:57:48 -0400573 dw_write(priv, DW_SPI_SSIENR, 0);
Stefan Roesed987ea62014-11-07 13:50:31 +0100574
575 /* clk_div doesn't support odd number */
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300576 clk_div = priv->bus_clk_rate / speed;
Stefan Roesed987ea62014-11-07 13:50:31 +0100577 clk_div = (clk_div + 1) & 0xfffe;
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300578 dw_write(priv, DW_SPI_BAUDR, clk_div);
Stefan Roesed987ea62014-11-07 13:50:31 +0100579
580 /* Enable controller after writing control registers */
Sean Andersonf2520822020-10-16 18:57:48 -0400581 dw_write(priv, DW_SPI_SSIENR, 1);
Stefan Roesed987ea62014-11-07 13:50:31 +0100582
583 priv->freq = speed;
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400584 dev_dbg(bus, "speed=%d clk_div=%d\n", priv->freq, clk_div);
Stefan Roesed987ea62014-11-07 13:50:31 +0100585
586 return 0;
587}
588
589static int dw_spi_set_mode(struct udevice *bus, uint mode)
590{
591 struct dw_spi_priv *priv = dev_get_priv(bus);
592
593 /*
594 * Can't set mode yet. Since this depends on if rx, tx, or
595 * rx & tx is requested. So we have to defer this to the
596 * real transfer function.
597 */
598 priv->mode = mode;
Sean Anderson0dfb3ac2020-10-16 18:57:44 -0400599 dev_dbg(bus, "mode=%d\n", priv->mode);
Stefan Roesed987ea62014-11-07 13:50:31 +0100600
601 return 0;
602}
603
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800604static int dw_spi_remove(struct udevice *bus)
605{
606 struct dw_spi_priv *priv = dev_get_priv(bus);
Ley Foon Tand95ab402018-09-19 16:27:19 +0800607 int ret;
608
609 ret = reset_release_bulk(&priv->resets);
610 if (ret)
611 return ret;
612
613#if CONFIG_IS_ENABLED(CLK)
614 ret = clk_disable(&priv->clk);
615 if (ret)
616 return ret;
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800617
Ley Foon Tand95ab402018-09-19 16:27:19 +0800618 ret = clk_free(&priv->clk);
619 if (ret)
620 return ret;
621#endif
622 return 0;
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800623}
624
Stefan Roesed987ea62014-11-07 13:50:31 +0100625static const struct dm_spi_ops dw_spi_ops = {
626 .xfer = dw_spi_xfer,
627 .set_speed = dw_spi_set_speed,
628 .set_mode = dw_spi_set_mode,
629 /*
630 * cs_info is not needed, since we require all chip selects to be
631 * in the device tree explicitly
632 */
633};
634
635static const struct udevice_id dw_spi_ids[] = {
Sean Andersondebbff82020-10-16 18:57:51 -0400636 /* Generic compatible strings */
637
638 { .compatible = "snps,dw-apb-ssi", .data = (ulong)dw_spi_apb_init },
639 { .compatible = "snps,dw-apb-ssi-3.20a", .data = (ulong)dw_spi_apb_init },
640 { .compatible = "snps,dw-apb-ssi-3.22a", .data = (ulong)dw_spi_apb_init },
641 /* First version with SSI_MAX_XFER_SIZE */
642 { .compatible = "snps,dw-apb-ssi-3.23a", .data = (ulong)dw_spi_apb_init },
643 /* First version with Dual/Quad SPI; unused by this driver */
644 { .compatible = "snps,dw-apb-ssi-4.00a", .data = (ulong)dw_spi_apb_init },
645 { .compatible = "snps,dw-apb-ssi-4.01", .data = (ulong)dw_spi_apb_init },
646 { .compatible = "snps,dwc-ssi-1.01a", .data = (ulong)dw_spi_dwc_init },
647
648 /* Compatible strings for specific SoCs */
649
650 /*
651 * Both the Cyclone V and Arria V share a device tree and have the same
652 * version of this device. This compatible string is used for those
653 * devices, and is not used for sofpgas in general.
654 */
655 { .compatible = "altr,socfpga-spi", .data = (ulong)dw_spi_apb_init },
656 { .compatible = "altr,socfpga-arria10-spi", .data = (ulong)dw_spi_apb_init },
657 { .compatible = "canaan,kendryte-k210-spi", .data = (ulong)dw_spi_apb_init },
658 { .compatible = "canaan,kendryte-k210-ssi", .data = (ulong)dw_spi_dwc_init },
659 { .compatible = "intel,stratix10-spi", .data = (ulong)dw_spi_apb_init },
660 { .compatible = "intel,agilex-spi", .data = (ulong)dw_spi_apb_init },
661 { .compatible = "mscc,ocelot-spi", .data = (ulong)dw_spi_apb_init },
662 { .compatible = "mscc,jaguar2-spi", .data = (ulong)dw_spi_apb_init },
663 { .compatible = "snps,axs10x-spi", .data = (ulong)dw_spi_apb_init },
664 { .compatible = "snps,hsdk-spi", .data = (ulong)dw_spi_apb_init },
Stefan Roesed987ea62014-11-07 13:50:31 +0100665 { }
666};
667
668U_BOOT_DRIVER(dw_spi) = {
669 .name = "dw_spi",
670 .id = UCLASS_SPI,
671 .of_match = dw_spi_ids,
672 .ops = &dw_spi_ops,
673 .ofdata_to_platdata = dw_spi_ofdata_to_platdata,
674 .platdata_auto_alloc_size = sizeof(struct dw_spi_platdata),
675 .priv_auto_alloc_size = sizeof(struct dw_spi_priv),
Stefan Roesed987ea62014-11-07 13:50:31 +0100676 .probe = dw_spi_probe,
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800677 .remove = dw_spi_remove,
Stefan Roesed987ea62014-11-07 13:50:31 +0100678};