blob: 2dc16736a3ec577e4a6bf4adecb3ab7a56e8a8bc [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>
6 *
Stefan Roese571e2a42014-11-16 12:47:01 +01007 * Very loosely based on the Linux driver:
8 * drivers/spi/spi-dw.c, which is:
Stefan Roesed987ea62014-11-07 13:50:31 +01009 * Copyright (c) 2009, Intel Corporation.
Stefan Roesed987ea62014-11-07 13:50:31 +010010 */
11
12#include <common.h>
Horatiu.Vultur@microchip.com340e5b32019-02-25 10:59:54 +000013#include <asm-generic/gpio.h>
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +030014#include <clk.h>
Stefan Roesed987ea62014-11-07 13:50:31 +010015#include <dm.h>
16#include <errno.h>
17#include <malloc.h>
18#include <spi.h>
19#include <fdtdec.h>
Ley Foon Tanfc3382d2018-09-07 14:25:29 +080020#include <reset.h>
Simon Glass9bc15642020-02-03 07:36:16 -070021#include <dm/device_compat.h>
Stefan Roesed987ea62014-11-07 13:50:31 +010022#include <linux/compat.h>
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +030023#include <linux/iopoll.h>
Stefan Roesed987ea62014-11-07 13:50:31 +010024#include <asm/io.h>
25
Stefan Roesed987ea62014-11-07 13:50:31 +010026/* Register offsets */
27#define DW_SPI_CTRL0 0x00
28#define DW_SPI_CTRL1 0x04
29#define DW_SPI_SSIENR 0x08
30#define DW_SPI_MWCR 0x0c
31#define DW_SPI_SER 0x10
32#define DW_SPI_BAUDR 0x14
33#define DW_SPI_TXFLTR 0x18
34#define DW_SPI_RXFLTR 0x1c
35#define DW_SPI_TXFLR 0x20
36#define DW_SPI_RXFLR 0x24
37#define DW_SPI_SR 0x28
38#define DW_SPI_IMR 0x2c
39#define DW_SPI_ISR 0x30
40#define DW_SPI_RISR 0x34
41#define DW_SPI_TXOICR 0x38
42#define DW_SPI_RXOICR 0x3c
43#define DW_SPI_RXUICR 0x40
44#define DW_SPI_MSTICR 0x44
45#define DW_SPI_ICR 0x48
46#define DW_SPI_DMACR 0x4c
47#define DW_SPI_DMATDLR 0x50
48#define DW_SPI_DMARDLR 0x54
49#define DW_SPI_IDR 0x58
50#define DW_SPI_VERSION 0x5c
51#define DW_SPI_DR 0x60
52
53/* Bit fields in CTRLR0 */
54#define SPI_DFS_OFFSET 0
55
56#define SPI_FRF_OFFSET 4
57#define SPI_FRF_SPI 0x0
58#define SPI_FRF_SSP 0x1
59#define SPI_FRF_MICROWIRE 0x2
60#define SPI_FRF_RESV 0x3
61
62#define SPI_MODE_OFFSET 6
63#define SPI_SCPH_OFFSET 6
64#define SPI_SCOL_OFFSET 7
65
66#define SPI_TMOD_OFFSET 8
67#define SPI_TMOD_MASK (0x3 << SPI_TMOD_OFFSET)
68#define SPI_TMOD_TR 0x0 /* xmit & recv */
69#define SPI_TMOD_TO 0x1 /* xmit only */
70#define SPI_TMOD_RO 0x2 /* recv only */
71#define SPI_TMOD_EPROMREAD 0x3 /* eeprom read mode */
72
73#define SPI_SLVOE_OFFSET 10
74#define SPI_SRL_OFFSET 11
75#define SPI_CFS_OFFSET 12
76
77/* Bit fields in SR, 7 bits */
Jagan Tekifac44912015-10-23 01:01:36 +053078#define SR_MASK GENMASK(6, 0) /* cover 7 bits */
Jagan Tekib17746d2015-10-23 01:36:23 +053079#define SR_BUSY BIT(0)
80#define SR_TF_NOT_FULL BIT(1)
81#define SR_TF_EMPT BIT(2)
82#define SR_RF_NOT_EMPT BIT(3)
83#define SR_RF_FULL BIT(4)
84#define SR_TX_ERR BIT(5)
85#define SR_DCOL BIT(6)
Stefan Roesed987ea62014-11-07 13:50:31 +010086
Stefan Roese571e2a42014-11-16 12:47:01 +010087#define RX_TIMEOUT 1000 /* timeout in ms */
Stefan Roesed987ea62014-11-07 13:50:31 +010088
89struct dw_spi_platdata {
90 s32 frequency; /* Default clock frequency, -1 for none */
91 void __iomem *regs;
92};
93
94struct dw_spi_priv {
95 void __iomem *regs;
96 unsigned int freq; /* Default frequency */
97 unsigned int mode;
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +030098 struct clk clk;
99 unsigned long bus_clk_rate;
Stefan Roesed987ea62014-11-07 13:50:31 +0100100
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300101 struct gpio_desc cs_gpio; /* External chip-select gpio */
102
Stefan Roesed987ea62014-11-07 13:50:31 +0100103 int bits_per_word;
104 u8 cs; /* chip select pin */
Stefan Roesed987ea62014-11-07 13:50:31 +0100105 u8 tmode; /* TR/TO/RO/EEPROM */
106 u8 type; /* SPI/SSP/MicroWire */
107 int len;
108
109 u32 fifo_len; /* depth of the FIFO buffer */
110 void *tx;
111 void *tx_end;
112 void *rx;
113 void *rx_end;
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800114
115 struct reset_ctl_bulk resets;
Stefan Roesed987ea62014-11-07 13:50:31 +0100116};
117
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300118static inline u32 dw_read(struct dw_spi_priv *priv, u32 offset)
Stefan Roesed987ea62014-11-07 13:50:31 +0100119{
120 return __raw_readl(priv->regs + offset);
121}
122
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300123static inline void dw_write(struct dw_spi_priv *priv, u32 offset, u32 val)
Stefan Roesed987ea62014-11-07 13:50:31 +0100124{
125 __raw_writel(val, priv->regs + offset);
126}
127
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300128static int request_gpio_cs(struct udevice *bus)
129{
Simon Glassfa4689a2019-12-06 21:41:35 -0700130#if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD)
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300131 struct dw_spi_priv *priv = dev_get_priv(bus);
132 int ret;
133
134 /* External chip select gpio line is optional */
135 ret = gpio_request_by_name(bus, "cs-gpio", 0, &priv->cs_gpio, 0);
136 if (ret == -ENOENT)
137 return 0;
138
139 if (ret < 0) {
140 printf("Error: %d: Can't get %s gpio!\n", ret, bus->name);
141 return ret;
142 }
143
144 if (dm_gpio_is_valid(&priv->cs_gpio)) {
145 dm_gpio_set_dir_flags(&priv->cs_gpio,
146 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
147 }
148
149 debug("%s: used external gpio for CS management\n", __func__);
150#endif
151 return 0;
152}
153
Stefan Roesed987ea62014-11-07 13:50:31 +0100154static int dw_spi_ofdata_to_platdata(struct udevice *bus)
155{
156 struct dw_spi_platdata *plat = bus->platdata;
Stefan Roesed987ea62014-11-07 13:50:31 +0100157
Simon Glassba1dea42017-05-17 17:18:05 -0600158 plat->regs = (struct dw_spi *)devfdt_get_addr(bus);
Stefan Roesed987ea62014-11-07 13:50:31 +0100159
160 /* Use 500KHz as a suitable default */
Simon Goldschmidt70cd31b2019-05-09 22:11:57 +0200161 plat->frequency = dev_read_u32_default(bus, "spi-max-frequency",
162 500000);
Stefan Roesed987ea62014-11-07 13:50:31 +0100163 debug("%s: regs=%p max-frequency=%d\n", __func__, plat->regs,
164 plat->frequency);
165
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300166 return request_gpio_cs(bus);
Stefan Roesed987ea62014-11-07 13:50:31 +0100167}
168
169static inline void spi_enable_chip(struct dw_spi_priv *priv, int enable)
170{
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300171 dw_write(priv, DW_SPI_SSIENR, (enable ? 1 : 0));
Stefan Roesed987ea62014-11-07 13:50:31 +0100172}
173
174/* Restart the controller, disable all interrupts, clean rx fifo */
175static void spi_hw_init(struct dw_spi_priv *priv)
176{
177 spi_enable_chip(priv, 0);
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300178 dw_write(priv, DW_SPI_IMR, 0xff);
Stefan Roesed987ea62014-11-07 13:50:31 +0100179 spi_enable_chip(priv, 1);
180
181 /*
182 * Try to detect the FIFO depth if not set by interface driver,
183 * the depth could be from 2 to 256 from HW spec
184 */
185 if (!priv->fifo_len) {
186 u32 fifo;
187
Axel Lin83cfd372015-02-26 10:45:22 +0800188 for (fifo = 1; fifo < 256; fifo++) {
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300189 dw_write(priv, DW_SPI_TXFLTR, fifo);
190 if (fifo != dw_read(priv, DW_SPI_TXFLTR))
Stefan Roesed987ea62014-11-07 13:50:31 +0100191 break;
192 }
193
Axel Lin83cfd372015-02-26 10:45:22 +0800194 priv->fifo_len = (fifo == 1) ? 0 : fifo;
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300195 dw_write(priv, DW_SPI_TXFLTR, 0);
Stefan Roesed987ea62014-11-07 13:50:31 +0100196 }
197 debug("%s: fifo_len=%d\n", __func__, priv->fifo_len);
198}
199
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300200/*
201 * We define dw_spi_get_clk function as 'weak' as some targets
202 * (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) don't use standard clock API
203 * and implement dw_spi_get_clk their own way in their clock manager.
204 */
205__weak int dw_spi_get_clk(struct udevice *bus, ulong *rate)
206{
207 struct dw_spi_priv *priv = dev_get_priv(bus);
208 int ret;
209
210 ret = clk_get_by_index(bus, 0, &priv->clk);
211 if (ret)
212 return ret;
213
214 ret = clk_enable(&priv->clk);
215 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
216 return ret;
217
218 *rate = clk_get_rate(&priv->clk);
219 if (!*rate)
220 goto err_rate;
221
222 debug("%s: get spi controller clk via device tree: %lu Hz\n",
223 __func__, *rate);
224
225 return 0;
226
227err_rate:
228 clk_disable(&priv->clk);
229 clk_free(&priv->clk);
230
231 return -EINVAL;
232}
233
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800234static int dw_spi_reset(struct udevice *bus)
235{
236 int ret;
237 struct dw_spi_priv *priv = dev_get_priv(bus);
238
239 ret = reset_get_bulk(bus, &priv->resets);
240 if (ret) {
241 /*
242 * Return 0 if error due to !CONFIG_DM_RESET and reset
243 * DT property is not present.
244 */
245 if (ret == -ENOENT || ret == -ENOTSUPP)
246 return 0;
247
248 dev_warn(bus, "Can't get reset: %d\n", ret);
249 return ret;
250 }
251
252 ret = reset_deassert_bulk(&priv->resets);
253 if (ret) {
254 reset_release_bulk(&priv->resets);
255 dev_err(bus, "Failed to reset: %d\n", ret);
256 return ret;
257 }
258
259 return 0;
260}
261
Stefan Roesed987ea62014-11-07 13:50:31 +0100262static int dw_spi_probe(struct udevice *bus)
263{
264 struct dw_spi_platdata *plat = dev_get_platdata(bus);
265 struct dw_spi_priv *priv = dev_get_priv(bus);
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300266 int ret;
Stefan Roesed987ea62014-11-07 13:50:31 +0100267
268 priv->regs = plat->regs;
269 priv->freq = plat->frequency;
270
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300271 ret = dw_spi_get_clk(bus, &priv->bus_clk_rate);
272 if (ret)
273 return ret;
274
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800275 ret = dw_spi_reset(bus);
276 if (ret)
277 return ret;
278
Stefan Roesed987ea62014-11-07 13:50:31 +0100279 /* Currently only bits_per_word == 8 supported */
280 priv->bits_per_word = 8;
Stefan Roesed987ea62014-11-07 13:50:31 +0100281
282 priv->tmode = 0; /* Tx & Rx */
283
284 /* Basic HW init */
285 spi_hw_init(priv);
286
287 return 0;
288}
289
290/* Return the max entries we can fill into tx fifo */
291static inline u32 tx_max(struct dw_spi_priv *priv)
292{
293 u32 tx_left, tx_room, rxtx_gap;
294
Stefan Roese571e2a42014-11-16 12:47:01 +0100295 tx_left = (priv->tx_end - priv->tx) / (priv->bits_per_word >> 3);
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300296 tx_room = priv->fifo_len - dw_read(priv, DW_SPI_TXFLR);
Stefan Roesed987ea62014-11-07 13:50:31 +0100297
298 /*
299 * Another concern is about the tx/rx mismatch, we
Stefan Roese571e2a42014-11-16 12:47:01 +0100300 * thought about using (priv->fifo_len - rxflr - txflr) as
Stefan Roesed987ea62014-11-07 13:50:31 +0100301 * one maximum value for tx, but it doesn't cover the
302 * data which is out of tx/rx fifo and inside the
303 * shift registers. So a control from sw point of
304 * view is taken.
305 */
306 rxtx_gap = ((priv->rx_end - priv->rx) - (priv->tx_end - priv->tx)) /
Stefan Roese571e2a42014-11-16 12:47:01 +0100307 (priv->bits_per_word >> 3);
Stefan Roesed987ea62014-11-07 13:50:31 +0100308
309 return min3(tx_left, tx_room, (u32)(priv->fifo_len - rxtx_gap));
310}
311
312/* Return the max entries we should read out of rx fifo */
313static inline u32 rx_max(struct dw_spi_priv *priv)
314{
Stefan Roese571e2a42014-11-16 12:47:01 +0100315 u32 rx_left = (priv->rx_end - priv->rx) / (priv->bits_per_word >> 3);
Stefan Roesed987ea62014-11-07 13:50:31 +0100316
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300317 return min_t(u32, rx_left, dw_read(priv, DW_SPI_RXFLR));
Stefan Roesed987ea62014-11-07 13:50:31 +0100318}
319
320static void dw_writer(struct dw_spi_priv *priv)
321{
322 u32 max = tx_max(priv);
323 u16 txw = 0;
324
325 while (max--) {
326 /* Set the tx word if the transfer's original "tx" is not null */
327 if (priv->tx_end - priv->len) {
Stefan Roese571e2a42014-11-16 12:47:01 +0100328 if (priv->bits_per_word == 8)
Stefan Roesed987ea62014-11-07 13:50:31 +0100329 txw = *(u8 *)(priv->tx);
330 else
331 txw = *(u16 *)(priv->tx);
332 }
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300333 dw_write(priv, DW_SPI_DR, txw);
Stefan Roesed987ea62014-11-07 13:50:31 +0100334 debug("%s: tx=0x%02x\n", __func__, txw);
Stefan Roese571e2a42014-11-16 12:47:01 +0100335 priv->tx += priv->bits_per_word >> 3;
Stefan Roesed987ea62014-11-07 13:50:31 +0100336 }
337}
338
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300339static void dw_reader(struct dw_spi_priv *priv)
Stefan Roesed987ea62014-11-07 13:50:31 +0100340{
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300341 u32 max = rx_max(priv);
Stefan Roesed987ea62014-11-07 13:50:31 +0100342 u16 rxw;
343
Stefan Roesed987ea62014-11-07 13:50:31 +0100344 while (max--) {
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300345 rxw = dw_read(priv, DW_SPI_DR);
Stefan Roesed987ea62014-11-07 13:50:31 +0100346 debug("%s: rx=0x%02x\n", __func__, rxw);
Stefan Roese571e2a42014-11-16 12:47:01 +0100347
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300348 /* Care about rx if the transfer's original "rx" is not null */
Stefan Roesed987ea62014-11-07 13:50:31 +0100349 if (priv->rx_end - priv->len) {
Stefan Roese571e2a42014-11-16 12:47:01 +0100350 if (priv->bits_per_word == 8)
Stefan Roesed987ea62014-11-07 13:50:31 +0100351 *(u8 *)(priv->rx) = rxw;
352 else
353 *(u16 *)(priv->rx) = rxw;
354 }
Stefan Roese571e2a42014-11-16 12:47:01 +0100355 priv->rx += priv->bits_per_word >> 3;
Stefan Roesed987ea62014-11-07 13:50:31 +0100356 }
Stefan Roesed987ea62014-11-07 13:50:31 +0100357}
358
359static int poll_transfer(struct dw_spi_priv *priv)
360{
Stefan Roesed987ea62014-11-07 13:50:31 +0100361 do {
362 dw_writer(priv);
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300363 dw_reader(priv);
Stefan Roesed987ea62014-11-07 13:50:31 +0100364 } while (priv->rx_end > priv->rx);
365
366 return 0;
367}
368
Gregory CLEMENTf2893372018-10-09 14:14:07 +0200369/*
370 * We define external_cs_manage function as 'weak' as some targets
371 * (like MSCC Ocelot) don't control the external CS pin using a GPIO
372 * controller. These SoCs use specific registers to control by
373 * software the SPI pins (and especially the CS).
374 */
375__weak void external_cs_manage(struct udevice *dev, bool on)
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300376{
Simon Glassfa4689a2019-12-06 21:41:35 -0700377#if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD)
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300378 struct dw_spi_priv *priv = dev_get_priv(dev->parent);
379
380 if (!dm_gpio_is_valid(&priv->cs_gpio))
381 return;
382
383 dm_gpio_set_value(&priv->cs_gpio, on ? 1 : 0);
384#endif
385}
386
Stefan Roesed987ea62014-11-07 13:50:31 +0100387static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
388 const void *dout, void *din, unsigned long flags)
389{
390 struct udevice *bus = dev->parent;
391 struct dw_spi_priv *priv = dev_get_priv(bus);
392 const u8 *tx = dout;
393 u8 *rx = din;
394 int ret = 0;
395 u32 cr0 = 0;
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +0300396 u32 val;
Stefan Roesed987ea62014-11-07 13:50:31 +0100397 u32 cs;
398
399 /* spi core configured to do 8 bit transfers */
400 if (bitlen % 8) {
401 debug("Non byte aligned SPI transfer.\n");
402 return -1;
403 }
404
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300405 /* Start the transaction if necessary. */
406 if (flags & SPI_XFER_BEGIN)
407 external_cs_manage(dev, false);
408
Stefan Roese571e2a42014-11-16 12:47:01 +0100409 cr0 = (priv->bits_per_word - 1) | (priv->type << SPI_FRF_OFFSET) |
Stefan Roesed987ea62014-11-07 13:50:31 +0100410 (priv->mode << SPI_MODE_OFFSET) |
411 (priv->tmode << SPI_TMOD_OFFSET);
412
413 if (rx && tx)
414 priv->tmode = SPI_TMOD_TR;
415 else if (rx)
416 priv->tmode = SPI_TMOD_RO;
417 else
Eugeniy Paltsev31f50132018-03-22 13:50:44 +0300418 /*
419 * In transmit only mode (SPI_TMOD_TO) input FIFO never gets
420 * any data which breaks our logic in poll_transfer() above.
421 */
422 priv->tmode = SPI_TMOD_TR;
Stefan Roesed987ea62014-11-07 13:50:31 +0100423
424 cr0 &= ~SPI_TMOD_MASK;
425 cr0 |= (priv->tmode << SPI_TMOD_OFFSET);
426
Stefan Roese571e2a42014-11-16 12:47:01 +0100427 priv->len = bitlen >> 3;
Stefan Roesed987ea62014-11-07 13:50:31 +0100428 debug("%s: rx=%p tx=%p len=%d [bytes]\n", __func__, rx, tx, priv->len);
429
430 priv->tx = (void *)tx;
431 priv->tx_end = priv->tx + priv->len;
432 priv->rx = rx;
433 priv->rx_end = priv->rx + priv->len;
434
435 /* Disable controller before writing control registers */
436 spi_enable_chip(priv, 0);
437
438 debug("%s: cr0=%08x\n", __func__, cr0);
439 /* Reprogram cr0 only if changed */
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300440 if (dw_read(priv, DW_SPI_CTRL0) != cr0)
441 dw_write(priv, DW_SPI_CTRL0, cr0);
Stefan Roesed987ea62014-11-07 13:50:31 +0100442
443 /*
444 * Configure the desired SS (slave select 0...3) in the controller
445 * The DW SPI controller will activate and deactivate this CS
446 * automatically. So no cs_activate() etc is needed in this driver.
447 */
448 cs = spi_chip_select(dev);
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300449 dw_write(priv, DW_SPI_SER, 1 << cs);
Stefan Roesed987ea62014-11-07 13:50:31 +0100450
451 /* Enable controller after writing control registers */
452 spi_enable_chip(priv, 1);
453
454 /* Start transfer in a polling loop */
455 ret = poll_transfer(priv);
456
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +0300457 /*
458 * Wait for current transmit operation to complete.
459 * Otherwise if some data still exists in Tx FIFO it can be
460 * silently flushed, i.e. dropped on disabling of the controller,
461 * which happens when writing 0 to DW_SPI_SSIENR which happens
462 * in the beginning of new transfer.
463 */
464 if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
Eugeniy Paltsev208be8f2018-04-19 17:47:41 +0300465 (val & SR_TF_EMPT) && !(val & SR_BUSY),
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +0300466 RX_TIMEOUT * 1000)) {
467 ret = -ETIMEDOUT;
468 }
469
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300470 /* Stop the transaction if necessary */
471 if (flags & SPI_XFER_END)
472 external_cs_manage(dev, true);
473
Stefan Roesed987ea62014-11-07 13:50:31 +0100474 return ret;
475}
476
477static int dw_spi_set_speed(struct udevice *bus, uint speed)
478{
479 struct dw_spi_platdata *plat = bus->platdata;
480 struct dw_spi_priv *priv = dev_get_priv(bus);
481 u16 clk_div;
482
483 if (speed > plat->frequency)
484 speed = plat->frequency;
485
486 /* Disable controller before writing control registers */
487 spi_enable_chip(priv, 0);
488
489 /* clk_div doesn't support odd number */
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300490 clk_div = priv->bus_clk_rate / speed;
Stefan Roesed987ea62014-11-07 13:50:31 +0100491 clk_div = (clk_div + 1) & 0xfffe;
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300492 dw_write(priv, DW_SPI_BAUDR, clk_div);
Stefan Roesed987ea62014-11-07 13:50:31 +0100493
494 /* Enable controller after writing control registers */
495 spi_enable_chip(priv, 1);
496
497 priv->freq = speed;
498 debug("%s: regs=%p speed=%d clk_div=%d\n", __func__, priv->regs,
499 priv->freq, clk_div);
500
501 return 0;
502}
503
504static int dw_spi_set_mode(struct udevice *bus, uint mode)
505{
506 struct dw_spi_priv *priv = dev_get_priv(bus);
507
508 /*
509 * Can't set mode yet. Since this depends on if rx, tx, or
510 * rx & tx is requested. So we have to defer this to the
511 * real transfer function.
512 */
513 priv->mode = mode;
514 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
515
516 return 0;
517}
518
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800519static int dw_spi_remove(struct udevice *bus)
520{
521 struct dw_spi_priv *priv = dev_get_priv(bus);
Ley Foon Tand95ab402018-09-19 16:27:19 +0800522 int ret;
523
524 ret = reset_release_bulk(&priv->resets);
525 if (ret)
526 return ret;
527
528#if CONFIG_IS_ENABLED(CLK)
529 ret = clk_disable(&priv->clk);
530 if (ret)
531 return ret;
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800532
Ley Foon Tand95ab402018-09-19 16:27:19 +0800533 ret = clk_free(&priv->clk);
534 if (ret)
535 return ret;
536#endif
537 return 0;
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800538}
539
Stefan Roesed987ea62014-11-07 13:50:31 +0100540static const struct dm_spi_ops dw_spi_ops = {
541 .xfer = dw_spi_xfer,
542 .set_speed = dw_spi_set_speed,
543 .set_mode = dw_spi_set_mode,
544 /*
545 * cs_info is not needed, since we require all chip selects to be
546 * in the device tree explicitly
547 */
548};
549
550static const struct udevice_id dw_spi_ids[] = {
Marek Vasut67e767d2014-12-31 20:14:55 +0100551 { .compatible = "snps,dw-apb-ssi" },
Stefan Roesed987ea62014-11-07 13:50:31 +0100552 { }
553};
554
555U_BOOT_DRIVER(dw_spi) = {
556 .name = "dw_spi",
557 .id = UCLASS_SPI,
558 .of_match = dw_spi_ids,
559 .ops = &dw_spi_ops,
560 .ofdata_to_platdata = dw_spi_ofdata_to_platdata,
561 .platdata_auto_alloc_size = sizeof(struct dw_spi_platdata),
562 .priv_auto_alloc_size = sizeof(struct dw_spi_priv),
Stefan Roesed987ea62014-11-07 13:50:31 +0100563 .probe = dw_spi_probe,
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800564 .remove = dw_spi_remove,
Stefan Roesed987ea62014-11-07 13:50:31 +0100565};