blob: dadb6fa18b058db38951c80b1019b3514c5aa39f [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>
Stefan Roesed987ea62014-11-07 13:50:31 +010021#include <linux/compat.h>
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +030022#include <linux/iopoll.h>
Stefan Roesed987ea62014-11-07 13:50:31 +010023#include <asm/io.h>
24
25DECLARE_GLOBAL_DATA_PTR;
26
27/* 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 Tekifac44912015-10-23 01:01:36 +053079#define SR_MASK GENMASK(6, 0) /* cover 7 bits */
Jagan Tekib17746d2015-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 Roesed987ea62014-11-07 13:50:31 +010087
Stefan Roese571e2a42014-11-16 12:47:01 +010088#define RX_TIMEOUT 1000 /* timeout in ms */
Stefan Roesed987ea62014-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 Paltsev8b841e32017-12-28 15:09:03 +030099 struct clk clk;
100 unsigned long bus_clk_rate;
Stefan Roesed987ea62014-11-07 13:50:31 +0100101
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300102 struct gpio_desc cs_gpio; /* External chip-select gpio */
103
Stefan Roesed987ea62014-11-07 13:50:31 +0100104 int bits_per_word;
105 u8 cs; /* chip select pin */
Stefan Roesed987ea62014-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 Tanfc3382d2018-09-07 14:25:29 +0800115
116 struct reset_ctl_bulk resets;
Stefan Roesed987ea62014-11-07 13:50:31 +0100117};
118
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300119static inline u32 dw_read(struct dw_spi_priv *priv, u32 offset)
Stefan Roesed987ea62014-11-07 13:50:31 +0100120{
121 return __raw_readl(priv->regs + offset);
122}
123
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300124static inline void dw_write(struct dw_spi_priv *priv, u32 offset, u32 val)
Stefan Roesed987ea62014-11-07 13:50:31 +0100125{
126 __raw_writel(val, priv->regs + offset);
127}
128
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300129static int request_gpio_cs(struct udevice *bus)
130{
131#if defined(CONFIG_DM_GPIO) && !defined(CONFIG_SPL_BUILD)
132 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 Roesed987ea62014-11-07 13:50:31 +0100155static int dw_spi_ofdata_to_platdata(struct udevice *bus)
156{
157 struct dw_spi_platdata *plat = bus->platdata;
158 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700159 int node = dev_of_offset(bus);
Stefan Roesed987ea62014-11-07 13:50:31 +0100160
Simon Glassba1dea42017-05-17 17:18:05 -0600161 plat->regs = (struct dw_spi *)devfdt_get_addr(bus);
Stefan Roesed987ea62014-11-07 13:50:31 +0100162
163 /* Use 500KHz as a suitable default */
164 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
165 500000);
166 debug("%s: regs=%p max-frequency=%d\n", __func__, plat->regs,
167 plat->frequency);
168
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300169 return request_gpio_cs(bus);
Stefan Roesed987ea62014-11-07 13:50:31 +0100170}
171
172static inline void spi_enable_chip(struct dw_spi_priv *priv, int enable)
173{
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300174 dw_write(priv, DW_SPI_SSIENR, (enable ? 1 : 0));
Stefan Roesed987ea62014-11-07 13:50:31 +0100175}
176
177/* Restart the controller, disable all interrupts, clean rx fifo */
178static void spi_hw_init(struct dw_spi_priv *priv)
179{
180 spi_enable_chip(priv, 0);
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300181 dw_write(priv, DW_SPI_IMR, 0xff);
Stefan Roesed987ea62014-11-07 13:50:31 +0100182 spi_enable_chip(priv, 1);
183
184 /*
185 * Try to detect the FIFO depth if not set by interface driver,
186 * the depth could be from 2 to 256 from HW spec
187 */
188 if (!priv->fifo_len) {
189 u32 fifo;
190
Axel Lin83cfd372015-02-26 10:45:22 +0800191 for (fifo = 1; fifo < 256; fifo++) {
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300192 dw_write(priv, DW_SPI_TXFLTR, fifo);
193 if (fifo != dw_read(priv, DW_SPI_TXFLTR))
Stefan Roesed987ea62014-11-07 13:50:31 +0100194 break;
195 }
196
Axel Lin83cfd372015-02-26 10:45:22 +0800197 priv->fifo_len = (fifo == 1) ? 0 : fifo;
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300198 dw_write(priv, DW_SPI_TXFLTR, 0);
Stefan Roesed987ea62014-11-07 13:50:31 +0100199 }
200 debug("%s: fifo_len=%d\n", __func__, priv->fifo_len);
201}
202
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300203/*
204 * We define dw_spi_get_clk function as 'weak' as some targets
205 * (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) don't use standard clock API
206 * and implement dw_spi_get_clk their own way in their clock manager.
207 */
208__weak int dw_spi_get_clk(struct udevice *bus, ulong *rate)
209{
210 struct dw_spi_priv *priv = dev_get_priv(bus);
211 int ret;
212
213 ret = clk_get_by_index(bus, 0, &priv->clk);
214 if (ret)
215 return ret;
216
217 ret = clk_enable(&priv->clk);
218 if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
219 return ret;
220
221 *rate = clk_get_rate(&priv->clk);
222 if (!*rate)
223 goto err_rate;
224
225 debug("%s: get spi controller clk via device tree: %lu Hz\n",
226 __func__, *rate);
227
228 return 0;
229
230err_rate:
231 clk_disable(&priv->clk);
232 clk_free(&priv->clk);
233
234 return -EINVAL;
235}
236
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800237static int dw_spi_reset(struct udevice *bus)
238{
239 int ret;
240 struct dw_spi_priv *priv = dev_get_priv(bus);
241
242 ret = reset_get_bulk(bus, &priv->resets);
243 if (ret) {
244 /*
245 * Return 0 if error due to !CONFIG_DM_RESET and reset
246 * DT property is not present.
247 */
248 if (ret == -ENOENT || ret == -ENOTSUPP)
249 return 0;
250
251 dev_warn(bus, "Can't get reset: %d\n", ret);
252 return ret;
253 }
254
255 ret = reset_deassert_bulk(&priv->resets);
256 if (ret) {
257 reset_release_bulk(&priv->resets);
258 dev_err(bus, "Failed to reset: %d\n", ret);
259 return ret;
260 }
261
262 return 0;
263}
264
Stefan Roesed987ea62014-11-07 13:50:31 +0100265static int dw_spi_probe(struct udevice *bus)
266{
267 struct dw_spi_platdata *plat = dev_get_platdata(bus);
268 struct dw_spi_priv *priv = dev_get_priv(bus);
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300269 int ret;
Stefan Roesed987ea62014-11-07 13:50:31 +0100270
271 priv->regs = plat->regs;
272 priv->freq = plat->frequency;
273
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300274 ret = dw_spi_get_clk(bus, &priv->bus_clk_rate);
275 if (ret)
276 return ret;
277
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800278 ret = dw_spi_reset(bus);
279 if (ret)
280 return ret;
281
Stefan Roesed987ea62014-11-07 13:50:31 +0100282 /* Currently only bits_per_word == 8 supported */
283 priv->bits_per_word = 8;
Stefan Roesed987ea62014-11-07 13:50:31 +0100284
285 priv->tmode = 0; /* Tx & Rx */
286
287 /* Basic HW init */
288 spi_hw_init(priv);
289
290 return 0;
291}
292
293/* Return the max entries we can fill into tx fifo */
294static inline u32 tx_max(struct dw_spi_priv *priv)
295{
296 u32 tx_left, tx_room, rxtx_gap;
297
Stefan Roese571e2a42014-11-16 12:47:01 +0100298 tx_left = (priv->tx_end - priv->tx) / (priv->bits_per_word >> 3);
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300299 tx_room = priv->fifo_len - dw_read(priv, DW_SPI_TXFLR);
Stefan Roesed987ea62014-11-07 13:50:31 +0100300
301 /*
302 * Another concern is about the tx/rx mismatch, we
Stefan Roese571e2a42014-11-16 12:47:01 +0100303 * thought about using (priv->fifo_len - rxflr - txflr) as
Stefan Roesed987ea62014-11-07 13:50:31 +0100304 * one maximum value for tx, but it doesn't cover the
305 * data which is out of tx/rx fifo and inside the
306 * shift registers. So a control from sw point of
307 * view is taken.
308 */
309 rxtx_gap = ((priv->rx_end - priv->rx) - (priv->tx_end - priv->tx)) /
Stefan Roese571e2a42014-11-16 12:47:01 +0100310 (priv->bits_per_word >> 3);
Stefan Roesed987ea62014-11-07 13:50:31 +0100311
312 return min3(tx_left, tx_room, (u32)(priv->fifo_len - rxtx_gap));
313}
314
315/* Return the max entries we should read out of rx fifo */
316static inline u32 rx_max(struct dw_spi_priv *priv)
317{
Stefan Roese571e2a42014-11-16 12:47:01 +0100318 u32 rx_left = (priv->rx_end - priv->rx) / (priv->bits_per_word >> 3);
Stefan Roesed987ea62014-11-07 13:50:31 +0100319
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300320 return min_t(u32, rx_left, dw_read(priv, DW_SPI_RXFLR));
Stefan Roesed987ea62014-11-07 13:50:31 +0100321}
322
323static void dw_writer(struct dw_spi_priv *priv)
324{
325 u32 max = tx_max(priv);
326 u16 txw = 0;
327
328 while (max--) {
329 /* Set the tx word if the transfer's original "tx" is not null */
330 if (priv->tx_end - priv->len) {
Stefan Roese571e2a42014-11-16 12:47:01 +0100331 if (priv->bits_per_word == 8)
Stefan Roesed987ea62014-11-07 13:50:31 +0100332 txw = *(u8 *)(priv->tx);
333 else
334 txw = *(u16 *)(priv->tx);
335 }
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300336 dw_write(priv, DW_SPI_DR, txw);
Stefan Roesed987ea62014-11-07 13:50:31 +0100337 debug("%s: tx=0x%02x\n", __func__, txw);
Stefan Roese571e2a42014-11-16 12:47:01 +0100338 priv->tx += priv->bits_per_word >> 3;
Stefan Roesed987ea62014-11-07 13:50:31 +0100339 }
340}
341
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300342static void dw_reader(struct dw_spi_priv *priv)
Stefan Roesed987ea62014-11-07 13:50:31 +0100343{
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300344 u32 max = rx_max(priv);
Stefan Roesed987ea62014-11-07 13:50:31 +0100345 u16 rxw;
346
Stefan Roesed987ea62014-11-07 13:50:31 +0100347 while (max--) {
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300348 rxw = dw_read(priv, DW_SPI_DR);
Stefan Roesed987ea62014-11-07 13:50:31 +0100349 debug("%s: rx=0x%02x\n", __func__, rxw);
Stefan Roese571e2a42014-11-16 12:47:01 +0100350
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300351 /* Care about rx if the transfer's original "rx" is not null */
Stefan Roesed987ea62014-11-07 13:50:31 +0100352 if (priv->rx_end - priv->len) {
Stefan Roese571e2a42014-11-16 12:47:01 +0100353 if (priv->bits_per_word == 8)
Stefan Roesed987ea62014-11-07 13:50:31 +0100354 *(u8 *)(priv->rx) = rxw;
355 else
356 *(u16 *)(priv->rx) = rxw;
357 }
Stefan Roese571e2a42014-11-16 12:47:01 +0100358 priv->rx += priv->bits_per_word >> 3;
Stefan Roesed987ea62014-11-07 13:50:31 +0100359 }
Stefan Roesed987ea62014-11-07 13:50:31 +0100360}
361
362static int poll_transfer(struct dw_spi_priv *priv)
363{
Stefan Roesed987ea62014-11-07 13:50:31 +0100364 do {
365 dw_writer(priv);
Eugeniy Paltsevc5c6d452018-03-22 13:50:45 +0300366 dw_reader(priv);
Stefan Roesed987ea62014-11-07 13:50:31 +0100367 } while (priv->rx_end > priv->rx);
368
369 return 0;
370}
371
Gregory CLEMENTf2893372018-10-09 14:14:07 +0200372/*
373 * We define external_cs_manage function as 'weak' as some targets
374 * (like MSCC Ocelot) don't control the external CS pin using a GPIO
375 * controller. These SoCs use specific registers to control by
376 * software the SPI pins (and especially the CS).
377 */
378__weak void external_cs_manage(struct udevice *dev, bool on)
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300379{
380#if defined(CONFIG_DM_GPIO) && !defined(CONFIG_SPL_BUILD)
381 struct dw_spi_priv *priv = dev_get_priv(dev->parent);
382
383 if (!dm_gpio_is_valid(&priv->cs_gpio))
384 return;
385
386 dm_gpio_set_value(&priv->cs_gpio, on ? 1 : 0);
387#endif
388}
389
Stefan Roesed987ea62014-11-07 13:50:31 +0100390static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
391 const void *dout, void *din, unsigned long flags)
392{
393 struct udevice *bus = dev->parent;
394 struct dw_spi_priv *priv = dev_get_priv(bus);
395 const u8 *tx = dout;
396 u8 *rx = din;
397 int ret = 0;
398 u32 cr0 = 0;
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +0300399 u32 val;
Stefan Roesed987ea62014-11-07 13:50:31 +0100400 u32 cs;
401
402 /* spi core configured to do 8 bit transfers */
403 if (bitlen % 8) {
404 debug("Non byte aligned SPI transfer.\n");
405 return -1;
406 }
407
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300408 /* Start the transaction if necessary. */
409 if (flags & SPI_XFER_BEGIN)
410 external_cs_manage(dev, false);
411
Stefan Roese571e2a42014-11-16 12:47:01 +0100412 cr0 = (priv->bits_per_word - 1) | (priv->type << SPI_FRF_OFFSET) |
Stefan Roesed987ea62014-11-07 13:50:31 +0100413 (priv->mode << SPI_MODE_OFFSET) |
414 (priv->tmode << SPI_TMOD_OFFSET);
415
416 if (rx && tx)
417 priv->tmode = SPI_TMOD_TR;
418 else if (rx)
419 priv->tmode = SPI_TMOD_RO;
420 else
Eugeniy Paltsev31f50132018-03-22 13:50:44 +0300421 /*
422 * In transmit only mode (SPI_TMOD_TO) input FIFO never gets
423 * any data which breaks our logic in poll_transfer() above.
424 */
425 priv->tmode = SPI_TMOD_TR;
Stefan Roesed987ea62014-11-07 13:50:31 +0100426
427 cr0 &= ~SPI_TMOD_MASK;
428 cr0 |= (priv->tmode << SPI_TMOD_OFFSET);
429
Stefan Roese571e2a42014-11-16 12:47:01 +0100430 priv->len = bitlen >> 3;
Stefan Roesed987ea62014-11-07 13:50:31 +0100431 debug("%s: rx=%p tx=%p len=%d [bytes]\n", __func__, rx, tx, priv->len);
432
433 priv->tx = (void *)tx;
434 priv->tx_end = priv->tx + priv->len;
435 priv->rx = rx;
436 priv->rx_end = priv->rx + priv->len;
437
438 /* Disable controller before writing control registers */
439 spi_enable_chip(priv, 0);
440
441 debug("%s: cr0=%08x\n", __func__, cr0);
442 /* Reprogram cr0 only if changed */
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300443 if (dw_read(priv, DW_SPI_CTRL0) != cr0)
444 dw_write(priv, DW_SPI_CTRL0, cr0);
Stefan Roesed987ea62014-11-07 13:50:31 +0100445
446 /*
447 * Configure the desired SS (slave select 0...3) in the controller
448 * The DW SPI controller will activate and deactivate this CS
449 * automatically. So no cs_activate() etc is needed in this driver.
450 */
451 cs = spi_chip_select(dev);
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300452 dw_write(priv, DW_SPI_SER, 1 << cs);
Stefan Roesed987ea62014-11-07 13:50:31 +0100453
454 /* Enable controller after writing control registers */
455 spi_enable_chip(priv, 1);
456
457 /* Start transfer in a polling loop */
458 ret = poll_transfer(priv);
459
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +0300460 /*
461 * Wait for current transmit operation to complete.
462 * Otherwise if some data still exists in Tx FIFO it can be
463 * silently flushed, i.e. dropped on disabling of the controller,
464 * which happens when writing 0 to DW_SPI_SSIENR which happens
465 * in the beginning of new transfer.
466 */
467 if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
Eugeniy Paltsev208be8f2018-04-19 17:47:41 +0300468 (val & SR_TF_EMPT) && !(val & SR_BUSY),
Eugeniy Paltsev7215ad22018-03-22 13:50:43 +0300469 RX_TIMEOUT * 1000)) {
470 ret = -ETIMEDOUT;
471 }
472
Eugeniy Paltseva7b4de12018-03-22 13:50:46 +0300473 /* Stop the transaction if necessary */
474 if (flags & SPI_XFER_END)
475 external_cs_manage(dev, true);
476
Stefan Roesed987ea62014-11-07 13:50:31 +0100477 return ret;
478}
479
480static int dw_spi_set_speed(struct udevice *bus, uint speed)
481{
482 struct dw_spi_platdata *plat = bus->platdata;
483 struct dw_spi_priv *priv = dev_get_priv(bus);
484 u16 clk_div;
485
486 if (speed > plat->frequency)
487 speed = plat->frequency;
488
489 /* Disable controller before writing control registers */
490 spi_enable_chip(priv, 0);
491
492 /* clk_div doesn't support odd number */
Eugeniy Paltsev8b841e32017-12-28 15:09:03 +0300493 clk_div = priv->bus_clk_rate / speed;
Stefan Roesed987ea62014-11-07 13:50:31 +0100494 clk_div = (clk_div + 1) & 0xfffe;
Eugeniy Paltseve0c89232018-03-22 13:50:47 +0300495 dw_write(priv, DW_SPI_BAUDR, clk_div);
Stefan Roesed987ea62014-11-07 13:50:31 +0100496
497 /* Enable controller after writing control registers */
498 spi_enable_chip(priv, 1);
499
500 priv->freq = speed;
501 debug("%s: regs=%p speed=%d clk_div=%d\n", __func__, priv->regs,
502 priv->freq, clk_div);
503
504 return 0;
505}
506
507static int dw_spi_set_mode(struct udevice *bus, uint mode)
508{
509 struct dw_spi_priv *priv = dev_get_priv(bus);
510
511 /*
512 * Can't set mode yet. Since this depends on if rx, tx, or
513 * rx & tx is requested. So we have to defer this to the
514 * real transfer function.
515 */
516 priv->mode = mode;
517 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
518
519 return 0;
520}
521
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800522static int dw_spi_remove(struct udevice *bus)
523{
524 struct dw_spi_priv *priv = dev_get_priv(bus);
525
526 return reset_release_bulk(&priv->resets);
527}
528
Stefan Roesed987ea62014-11-07 13:50:31 +0100529static const struct dm_spi_ops dw_spi_ops = {
530 .xfer = dw_spi_xfer,
531 .set_speed = dw_spi_set_speed,
532 .set_mode = dw_spi_set_mode,
533 /*
534 * cs_info is not needed, since we require all chip selects to be
535 * in the device tree explicitly
536 */
537};
538
539static const struct udevice_id dw_spi_ids[] = {
Marek Vasut67e767d2014-12-31 20:14:55 +0100540 { .compatible = "snps,dw-apb-ssi" },
Stefan Roesed987ea62014-11-07 13:50:31 +0100541 { }
542};
543
544U_BOOT_DRIVER(dw_spi) = {
545 .name = "dw_spi",
546 .id = UCLASS_SPI,
547 .of_match = dw_spi_ids,
548 .ops = &dw_spi_ops,
549 .ofdata_to_platdata = dw_spi_ofdata_to_platdata,
550 .platdata_auto_alloc_size = sizeof(struct dw_spi_platdata),
551 .priv_auto_alloc_size = sizeof(struct dw_spi_priv),
Stefan Roesed987ea62014-11-07 13:50:31 +0100552 .probe = dw_spi_probe,
Ley Foon Tanfc3382d2018-09-07 14:25:29 +0800553 .remove = dw_spi_remove,
Stefan Roesed987ea62014-11-07 13:50:31 +0100554};