blob: 09044bd137f078d5079df3eeda09f3406f1e4934 [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>
6 *
Stefan Roesea72f8022014-11-16 12:47:01 +01007 * Very loosely based on the Linux driver:
8 * drivers/spi/spi-dw.c, which is:
Stefan Roese5bef6fd2014-11-07 13:50:31 +01009 * Copyright (c) 2009, Intel Corporation.
Stefan Roese5bef6fd2014-11-07 13:50:31 +010010 */
11
12#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Horatiu.Vultur@microchip.com1b77de42019-02-25 10:59:54 +000014#include <asm-generic/gpio.h>
Eugeniy Paltsev58c125b2017-12-28 15:09:03 +030015#include <clk.h>
Stefan Roese5bef6fd2014-11-07 13:50:31 +010016#include <dm.h>
17#include <errno.h>
18#include <malloc.h>
19#include <spi.h>
20#include <fdtdec.h>
Ley Foon Tan6ac59092018-09-07 14:25:29 +080021#include <reset.h>
Simon Glass336d4612020-02-03 07:36:16 -070022#include <dm/device_compat.h>
Stefan Roese5bef6fd2014-11-07 13:50:31 +010023#include <linux/compat.h>
Eugeniy Paltsevc6b4f032018-03-22 13:50:43 +030024#include <linux/iopoll.h>
Stefan Roese5bef6fd2014-11-07 13:50:31 +010025#include <asm/io.h>
26
Stefan Roese5bef6fd2014-11-07 13:50:31 +010027/* Register offsets */
28#define DW_SPI_CTRL0 0x00
29#define DW_SPI_CTRL1 0x04
30#define DW_SPI_SSIENR 0x08
31#define DW_SPI_MWCR 0x0c
32#define DW_SPI_SER 0x10
33#define DW_SPI_BAUDR 0x14
34#define DW_SPI_TXFLTR 0x18
35#define DW_SPI_RXFLTR 0x1c
36#define DW_SPI_TXFLR 0x20
37#define DW_SPI_RXFLR 0x24
38#define DW_SPI_SR 0x28
39#define DW_SPI_IMR 0x2c
40#define DW_SPI_ISR 0x30
41#define DW_SPI_RISR 0x34
42#define DW_SPI_TXOICR 0x38
43#define DW_SPI_RXOICR 0x3c
44#define DW_SPI_RXUICR 0x40
45#define DW_SPI_MSTICR 0x44
46#define DW_SPI_ICR 0x48
47#define DW_SPI_DMACR 0x4c
48#define DW_SPI_DMATDLR 0x50
49#define DW_SPI_DMARDLR 0x54
50#define DW_SPI_IDR 0x58
51#define DW_SPI_VERSION 0x5c
52#define DW_SPI_DR 0x60
53
54/* Bit fields in CTRLR0 */
55#define SPI_DFS_OFFSET 0
56
57#define SPI_FRF_OFFSET 4
58#define SPI_FRF_SPI 0x0
59#define SPI_FRF_SSP 0x1
60#define SPI_FRF_MICROWIRE 0x2
61#define SPI_FRF_RESV 0x3
62
63#define SPI_MODE_OFFSET 6
64#define SPI_SCPH_OFFSET 6
65#define SPI_SCOL_OFFSET 7
66
67#define SPI_TMOD_OFFSET 8
68#define SPI_TMOD_MASK (0x3 << SPI_TMOD_OFFSET)
69#define SPI_TMOD_TR 0x0 /* xmit & recv */
70#define SPI_TMOD_TO 0x1 /* xmit only */
71#define SPI_TMOD_RO 0x2 /* recv only */
72#define SPI_TMOD_EPROMREAD 0x3 /* eeprom read mode */
73
74#define SPI_SLVOE_OFFSET 10
75#define SPI_SRL_OFFSET 11
76#define SPI_CFS_OFFSET 12
77
78/* Bit fields in SR, 7 bits */
Jagan Teki95e77d92015-10-23 01:01:36 +053079#define SR_MASK GENMASK(6, 0) /* cover 7 bits */
Jagan Teki431a9f02015-10-23 01:36:23 +053080#define SR_BUSY BIT(0)
81#define SR_TF_NOT_FULL BIT(1)
82#define SR_TF_EMPT BIT(2)
83#define SR_RF_NOT_EMPT BIT(3)
84#define SR_RF_FULL BIT(4)
85#define SR_TX_ERR BIT(5)
86#define SR_DCOL BIT(6)
Stefan Roese5bef6fd2014-11-07 13:50:31 +010087
Stefan Roesea72f8022014-11-16 12:47:01 +010088#define RX_TIMEOUT 1000 /* timeout in ms */
Stefan Roese5bef6fd2014-11-07 13:50:31 +010089
90struct dw_spi_platdata {
91 s32 frequency; /* Default clock frequency, -1 for none */
92 void __iomem *regs;
93};
94
95struct dw_spi_priv {
96 void __iomem *regs;
97 unsigned int freq; /* Default frequency */
98 unsigned int mode;
Eugeniy Paltsev58c125b2017-12-28 15:09:03 +030099 struct clk clk;
100 unsigned long bus_clk_rate;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100101
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300102 struct gpio_desc cs_gpio; /* External chip-select gpio */
103
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100104 int bits_per_word;
105 u8 cs; /* chip select pin */
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100106 u8 tmode; /* TR/TO/RO/EEPROM */
107 u8 type; /* SPI/SSP/MicroWire */
108 int len;
109
110 u32 fifo_len; /* depth of the FIFO buffer */
111 void *tx;
112 void *tx_end;
113 void *rx;
114 void *rx_end;
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800115
116 struct reset_ctl_bulk resets;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100117};
118
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300119static inline u32 dw_read(struct dw_spi_priv *priv, u32 offset)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100120{
121 return __raw_readl(priv->regs + offset);
122}
123
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300124static inline void dw_write(struct dw_spi_priv *priv, u32 offset, u32 val)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100125{
126 __raw_writel(val, priv->regs + offset);
127}
128
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300129static int request_gpio_cs(struct udevice *bus)
130{
Simon Glassbcee8d62019-12-06 21:41:35 -0700131#if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD)
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300132 struct dw_spi_priv *priv = dev_get_priv(bus);
133 int ret;
134
135 /* External chip select gpio line is optional */
136 ret = gpio_request_by_name(bus, "cs-gpio", 0, &priv->cs_gpio, 0);
137 if (ret == -ENOENT)
138 return 0;
139
140 if (ret < 0) {
141 printf("Error: %d: Can't get %s gpio!\n", ret, bus->name);
142 return ret;
143 }
144
145 if (dm_gpio_is_valid(&priv->cs_gpio)) {
146 dm_gpio_set_dir_flags(&priv->cs_gpio,
147 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
148 }
149
150 debug("%s: used external gpio for CS management\n", __func__);
151#endif
152 return 0;
153}
154
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100155static int dw_spi_ofdata_to_platdata(struct udevice *bus)
156{
157 struct dw_spi_platdata *plat = bus->platdata;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100158
Simon Glassa821c4a2017-05-17 17:18:05 -0600159 plat->regs = (struct dw_spi *)devfdt_get_addr(bus);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100160
161 /* Use 500KHz as a suitable default */
Simon Goldschmidt27c3e072019-05-09 22:11:57 +0200162 plat->frequency = dev_read_u32_default(bus, "spi-max-frequency",
163 500000);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100164 debug("%s: regs=%p max-frequency=%d\n", __func__, plat->regs,
165 plat->frequency);
166
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300167 return request_gpio_cs(bus);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100168}
169
170static inline void spi_enable_chip(struct dw_spi_priv *priv, int enable)
171{
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300172 dw_write(priv, DW_SPI_SSIENR, (enable ? 1 : 0));
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100173}
174
175/* Restart the controller, disable all interrupts, clean rx fifo */
176static void spi_hw_init(struct dw_spi_priv *priv)
177{
178 spi_enable_chip(priv, 0);
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300179 dw_write(priv, DW_SPI_IMR, 0xff);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100180 spi_enable_chip(priv, 1);
181
182 /*
183 * Try to detect the FIFO depth if not set by interface driver,
184 * the depth could be from 2 to 256 from HW spec
185 */
186 if (!priv->fifo_len) {
187 u32 fifo;
188
Axel Lin52091ad2015-02-26 10:45:22 +0800189 for (fifo = 1; fifo < 256; fifo++) {
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300190 dw_write(priv, DW_SPI_TXFLTR, fifo);
191 if (fifo != dw_read(priv, DW_SPI_TXFLTR))
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100192 break;
193 }
194
Axel Lin52091ad2015-02-26 10:45:22 +0800195 priv->fifo_len = (fifo == 1) ? 0 : fifo;
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300196 dw_write(priv, DW_SPI_TXFLTR, 0);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100197 }
198 debug("%s: fifo_len=%d\n", __func__, priv->fifo_len);
199}
200
Eugeniy Paltsev58c125b2017-12-28 15:09:03 +0300201/*
202 * We define dw_spi_get_clk function as 'weak' as some targets
203 * (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) don't use standard clock API
204 * and implement dw_spi_get_clk their own way in their clock manager.
205 */
206__weak int dw_spi_get_clk(struct udevice *bus, ulong *rate)
207{
208 struct dw_spi_priv *priv = dev_get_priv(bus);
209 int ret;
210
211 ret = clk_get_by_index(bus, 0, &priv->clk);
212 if (ret)
213 return ret;
214
215 ret = clk_enable(&priv->clk);
216 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
217 return ret;
218
219 *rate = clk_get_rate(&priv->clk);
220 if (!*rate)
221 goto err_rate;
222
223 debug("%s: get spi controller clk via device tree: %lu Hz\n",
224 __func__, *rate);
225
226 return 0;
227
228err_rate:
229 clk_disable(&priv->clk);
230 clk_free(&priv->clk);
231
232 return -EINVAL;
233}
234
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800235static int dw_spi_reset(struct udevice *bus)
236{
237 int ret;
238 struct dw_spi_priv *priv = dev_get_priv(bus);
239
240 ret = reset_get_bulk(bus, &priv->resets);
241 if (ret) {
242 /*
243 * Return 0 if error due to !CONFIG_DM_RESET and reset
244 * DT property is not present.
245 */
246 if (ret == -ENOENT || ret == -ENOTSUPP)
247 return 0;
248
249 dev_warn(bus, "Can't get reset: %d\n", ret);
250 return ret;
251 }
252
253 ret = reset_deassert_bulk(&priv->resets);
254 if (ret) {
255 reset_release_bulk(&priv->resets);
256 dev_err(bus, "Failed to reset: %d\n", ret);
257 return ret;
258 }
259
260 return 0;
261}
262
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100263static int dw_spi_probe(struct udevice *bus)
264{
265 struct dw_spi_platdata *plat = dev_get_platdata(bus);
266 struct dw_spi_priv *priv = dev_get_priv(bus);
Eugeniy Paltsev58c125b2017-12-28 15:09:03 +0300267 int ret;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100268
269 priv->regs = plat->regs;
270 priv->freq = plat->frequency;
271
Eugeniy Paltsev58c125b2017-12-28 15:09:03 +0300272 ret = dw_spi_get_clk(bus, &priv->bus_clk_rate);
273 if (ret)
274 return ret;
275
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800276 ret = dw_spi_reset(bus);
277 if (ret)
278 return ret;
279
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100280 /* Currently only bits_per_word == 8 supported */
281 priv->bits_per_word = 8;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100282
283 priv->tmode = 0; /* Tx & Rx */
284
285 /* Basic HW init */
286 spi_hw_init(priv);
287
288 return 0;
289}
290
291/* Return the max entries we can fill into tx fifo */
292static inline u32 tx_max(struct dw_spi_priv *priv)
293{
294 u32 tx_left, tx_room, rxtx_gap;
295
Stefan Roesea72f8022014-11-16 12:47:01 +0100296 tx_left = (priv->tx_end - priv->tx) / (priv->bits_per_word >> 3);
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300297 tx_room = priv->fifo_len - dw_read(priv, DW_SPI_TXFLR);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100298
299 /*
300 * Another concern is about the tx/rx mismatch, we
Stefan Roesea72f8022014-11-16 12:47:01 +0100301 * thought about using (priv->fifo_len - rxflr - txflr) as
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100302 * one maximum value for tx, but it doesn't cover the
303 * data which is out of tx/rx fifo and inside the
304 * shift registers. So a control from sw point of
305 * view is taken.
306 */
307 rxtx_gap = ((priv->rx_end - priv->rx) - (priv->tx_end - priv->tx)) /
Stefan Roesea72f8022014-11-16 12:47:01 +0100308 (priv->bits_per_word >> 3);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100309
310 return min3(tx_left, tx_room, (u32)(priv->fifo_len - rxtx_gap));
311}
312
313/* Return the max entries we should read out of rx fifo */
314static inline u32 rx_max(struct dw_spi_priv *priv)
315{
Stefan Roesea72f8022014-11-16 12:47:01 +0100316 u32 rx_left = (priv->rx_end - priv->rx) / (priv->bits_per_word >> 3);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100317
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300318 return min_t(u32, rx_left, dw_read(priv, DW_SPI_RXFLR));
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100319}
320
321static void dw_writer(struct dw_spi_priv *priv)
322{
323 u32 max = tx_max(priv);
324 u16 txw = 0;
325
326 while (max--) {
327 /* Set the tx word if the transfer's original "tx" is not null */
328 if (priv->tx_end - priv->len) {
Stefan Roesea72f8022014-11-16 12:47:01 +0100329 if (priv->bits_per_word == 8)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100330 txw = *(u8 *)(priv->tx);
331 else
332 txw = *(u16 *)(priv->tx);
333 }
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300334 dw_write(priv, DW_SPI_DR, txw);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100335 debug("%s: tx=0x%02x\n", __func__, txw);
Stefan Roesea72f8022014-11-16 12:47:01 +0100336 priv->tx += priv->bits_per_word >> 3;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100337 }
338}
339
Eugeniy Paltsevd3d8aae2018-03-22 13:50:45 +0300340static void dw_reader(struct dw_spi_priv *priv)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100341{
Eugeniy Paltsevd3d8aae2018-03-22 13:50:45 +0300342 u32 max = rx_max(priv);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100343 u16 rxw;
344
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100345 while (max--) {
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300346 rxw = dw_read(priv, DW_SPI_DR);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100347 debug("%s: rx=0x%02x\n", __func__, rxw);
Stefan Roesea72f8022014-11-16 12:47:01 +0100348
Eugeniy Paltsevd3d8aae2018-03-22 13:50:45 +0300349 /* Care about rx if the transfer's original "rx" is not null */
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100350 if (priv->rx_end - priv->len) {
Stefan Roesea72f8022014-11-16 12:47:01 +0100351 if (priv->bits_per_word == 8)
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100352 *(u8 *)(priv->rx) = rxw;
353 else
354 *(u16 *)(priv->rx) = rxw;
355 }
Stefan Roesea72f8022014-11-16 12:47:01 +0100356 priv->rx += priv->bits_per_word >> 3;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100357 }
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100358}
359
360static int poll_transfer(struct dw_spi_priv *priv)
361{
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100362 do {
363 dw_writer(priv);
Eugeniy Paltsevd3d8aae2018-03-22 13:50:45 +0300364 dw_reader(priv);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100365 } while (priv->rx_end > priv->rx);
366
367 return 0;
368}
369
Gregory CLEMENTbea91b02018-10-09 14:14:07 +0200370/*
371 * We define external_cs_manage function as 'weak' as some targets
372 * (like MSCC Ocelot) don't control the external CS pin using a GPIO
373 * controller. These SoCs use specific registers to control by
374 * software the SPI pins (and especially the CS).
375 */
376__weak void external_cs_manage(struct udevice *dev, bool on)
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300377{
Simon Glassbcee8d62019-12-06 21:41:35 -0700378#if CONFIG_IS_ENABLED(DM_GPIO) && !defined(CONFIG_SPL_BUILD)
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300379 struct dw_spi_priv *priv = dev_get_priv(dev->parent);
380
381 if (!dm_gpio_is_valid(&priv->cs_gpio))
382 return;
383
384 dm_gpio_set_value(&priv->cs_gpio, on ? 1 : 0);
385#endif
386}
387
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100388static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
389 const void *dout, void *din, unsigned long flags)
390{
391 struct udevice *bus = dev->parent;
392 struct dw_spi_priv *priv = dev_get_priv(bus);
393 const u8 *tx = dout;
394 u8 *rx = din;
395 int ret = 0;
396 u32 cr0 = 0;
Eugeniy Paltsevc6b4f032018-03-22 13:50:43 +0300397 u32 val;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100398 u32 cs;
399
400 /* spi core configured to do 8 bit transfers */
401 if (bitlen % 8) {
402 debug("Non byte aligned SPI transfer.\n");
403 return -1;
404 }
405
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300406 /* Start the transaction if necessary. */
407 if (flags & SPI_XFER_BEGIN)
408 external_cs_manage(dev, false);
409
Stefan Roesea72f8022014-11-16 12:47:01 +0100410 cr0 = (priv->bits_per_word - 1) | (priv->type << SPI_FRF_OFFSET) |
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100411 (priv->mode << SPI_MODE_OFFSET) |
412 (priv->tmode << SPI_TMOD_OFFSET);
413
414 if (rx && tx)
415 priv->tmode = SPI_TMOD_TR;
416 else if (rx)
417 priv->tmode = SPI_TMOD_RO;
418 else
Eugeniy Paltsevfc282c72018-03-22 13:50:44 +0300419 /*
420 * In transmit only mode (SPI_TMOD_TO) input FIFO never gets
421 * any data which breaks our logic in poll_transfer() above.
422 */
423 priv->tmode = SPI_TMOD_TR;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100424
425 cr0 &= ~SPI_TMOD_MASK;
426 cr0 |= (priv->tmode << SPI_TMOD_OFFSET);
427
Stefan Roesea72f8022014-11-16 12:47:01 +0100428 priv->len = bitlen >> 3;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100429 debug("%s: rx=%p tx=%p len=%d [bytes]\n", __func__, rx, tx, priv->len);
430
431 priv->tx = (void *)tx;
432 priv->tx_end = priv->tx + priv->len;
433 priv->rx = rx;
434 priv->rx_end = priv->rx + priv->len;
435
436 /* Disable controller before writing control registers */
437 spi_enable_chip(priv, 0);
438
439 debug("%s: cr0=%08x\n", __func__, cr0);
440 /* Reprogram cr0 only if changed */
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300441 if (dw_read(priv, DW_SPI_CTRL0) != cr0)
442 dw_write(priv, DW_SPI_CTRL0, cr0);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100443
444 /*
445 * Configure the desired SS (slave select 0...3) in the controller
446 * The DW SPI controller will activate and deactivate this CS
447 * automatically. So no cs_activate() etc is needed in this driver.
448 */
449 cs = spi_chip_select(dev);
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300450 dw_write(priv, DW_SPI_SER, 1 << cs);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100451
452 /* Enable controller after writing control registers */
453 spi_enable_chip(priv, 1);
454
455 /* Start transfer in a polling loop */
456 ret = poll_transfer(priv);
457
Eugeniy Paltsevc6b4f032018-03-22 13:50:43 +0300458 /*
459 * Wait for current transmit operation to complete.
460 * Otherwise if some data still exists in Tx FIFO it can be
461 * silently flushed, i.e. dropped on disabling of the controller,
462 * which happens when writing 0 to DW_SPI_SSIENR which happens
463 * in the beginning of new transfer.
464 */
465 if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
Eugeniy Paltsev9b14ac52018-04-19 17:47:41 +0300466 (val & SR_TF_EMPT) && !(val & SR_BUSY),
Eugeniy Paltsevc6b4f032018-03-22 13:50:43 +0300467 RX_TIMEOUT * 1000)) {
468 ret = -ETIMEDOUT;
469 }
470
Eugeniy Paltsevbcdcb3e2018-03-22 13:50:46 +0300471 /* Stop the transaction if necessary */
472 if (flags & SPI_XFER_END)
473 external_cs_manage(dev, true);
474
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100475 return ret;
476}
477
478static int dw_spi_set_speed(struct udevice *bus, uint speed)
479{
480 struct dw_spi_platdata *plat = bus->platdata;
481 struct dw_spi_priv *priv = dev_get_priv(bus);
482 u16 clk_div;
483
484 if (speed > plat->frequency)
485 speed = plat->frequency;
486
487 /* Disable controller before writing control registers */
488 spi_enable_chip(priv, 0);
489
490 /* clk_div doesn't support odd number */
Eugeniy Paltsev58c125b2017-12-28 15:09:03 +0300491 clk_div = priv->bus_clk_rate / speed;
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100492 clk_div = (clk_div + 1) & 0xfffe;
Eugeniy Paltsev4b5f6c52018-03-22 13:50:47 +0300493 dw_write(priv, DW_SPI_BAUDR, clk_div);
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100494
495 /* Enable controller after writing control registers */
496 spi_enable_chip(priv, 1);
497
498 priv->freq = speed;
499 debug("%s: regs=%p speed=%d clk_div=%d\n", __func__, priv->regs,
500 priv->freq, clk_div);
501
502 return 0;
503}
504
505static int dw_spi_set_mode(struct udevice *bus, uint mode)
506{
507 struct dw_spi_priv *priv = dev_get_priv(bus);
508
509 /*
510 * Can't set mode yet. Since this depends on if rx, tx, or
511 * rx & tx is requested. So we have to defer this to the
512 * real transfer function.
513 */
514 priv->mode = mode;
515 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
516
517 return 0;
518}
519
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800520static int dw_spi_remove(struct udevice *bus)
521{
522 struct dw_spi_priv *priv = dev_get_priv(bus);
Ley Foon Tane7e05fc2018-09-19 16:27:19 +0800523 int ret;
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800524
Ley Foon Tane7e05fc2018-09-19 16:27:19 +0800525 ret = reset_release_bulk(&priv->resets);
526 if (ret)
527 return ret;
528
529#if CONFIG_IS_ENABLED(CLK)
530 ret = clk_disable(&priv->clk);
531 if (ret)
532 return ret;
533
534 ret = clk_free(&priv->clk);
535 if (ret)
536 return ret;
537#endif
538 return 0;
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800539}
540
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100541static const struct dm_spi_ops dw_spi_ops = {
542 .xfer = dw_spi_xfer,
543 .set_speed = dw_spi_set_speed,
544 .set_mode = dw_spi_set_mode,
545 /*
546 * cs_info is not needed, since we require all chip selects to be
547 * in the device tree explicitly
548 */
549};
550
551static const struct udevice_id dw_spi_ids[] = {
Marek Vasut74114862014-12-31 20:14:55 +0100552 { .compatible = "snps,dw-apb-ssi" },
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100553 { }
554};
555
556U_BOOT_DRIVER(dw_spi) = {
557 .name = "dw_spi",
558 .id = UCLASS_SPI,
559 .of_match = dw_spi_ids,
560 .ops = &dw_spi_ops,
561 .ofdata_to_platdata = dw_spi_ofdata_to_platdata,
562 .platdata_auto_alloc_size = sizeof(struct dw_spi_platdata),
563 .priv_auto_alloc_size = sizeof(struct dw_spi_priv),
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100564 .probe = dw_spi_probe,
Ley Foon Tan6ac59092018-09-07 14:25:29 +0800565 .remove = dw_spi_remove,
Stefan Roese5bef6fd2014-11-07 13:50:31 +0100566};