Blame view

drivers/spi/designware_spi.c 13.3 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0
5bef6fd79   Stefan Roese   spi: Add designwa...
2
3
4
5
6
  /*
   * Designware master SPI core controller driver
   *
   * Copyright (C) 2014 Stefan Roese <sr@denx.de>
   *
a72f80208   Stefan Roese   spi: designware_s...
7
8
   * Very loosely based on the Linux driver:
   * drivers/spi/spi-dw.c, which is:
5bef6fd79   Stefan Roese   spi: Add designwa...
9
   * Copyright (c) 2009, Intel Corporation.
5bef6fd79   Stefan Roese   spi: Add designwa...
10
11
12
   */
  
  #include <common.h>
1b77de447   Horatiu.Vultur@microchip.com   spi: designware: ...
13
  #include <asm-generic/gpio.h>
58c125b9e   Eugeniy Paltsev   DW SPI: Get clock...
14
  #include <clk.h>
5bef6fd79   Stefan Roese   spi: Add designwa...
15
16
17
18
19
  #include <dm.h>
  #include <errno.h>
  #include <malloc.h>
  #include <spi.h>
  #include <fdtdec.h>
6ac5909f5   Ley Foon Tan   spi: designware_s...
20
  #include <reset.h>
5bef6fd79   Stefan Roese   spi: Add designwa...
21
  #include <linux/compat.h>
c6b4f031d   Eugeniy Paltsev   DW SPI: fix tx da...
22
  #include <linux/iopoll.h>
5bef6fd79   Stefan Roese   spi: Add designwa...
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  #include <asm/io.h>
  
  DECLARE_GLOBAL_DATA_PTR;
  
  /* Register offsets */
  #define DW_SPI_CTRL0			0x00
  #define DW_SPI_CTRL1			0x04
  #define DW_SPI_SSIENR			0x08
  #define DW_SPI_MWCR			0x0c
  #define DW_SPI_SER			0x10
  #define DW_SPI_BAUDR			0x14
  #define DW_SPI_TXFLTR			0x18
  #define DW_SPI_RXFLTR			0x1c
  #define DW_SPI_TXFLR			0x20
  #define DW_SPI_RXFLR			0x24
  #define DW_SPI_SR			0x28
  #define DW_SPI_IMR			0x2c
  #define DW_SPI_ISR			0x30
  #define DW_SPI_RISR			0x34
  #define DW_SPI_TXOICR			0x38
  #define DW_SPI_RXOICR			0x3c
  #define DW_SPI_RXUICR			0x40
  #define DW_SPI_MSTICR			0x44
  #define DW_SPI_ICR			0x48
  #define DW_SPI_DMACR			0x4c
  #define DW_SPI_DMATDLR			0x50
  #define DW_SPI_DMARDLR			0x54
  #define DW_SPI_IDR			0x58
  #define DW_SPI_VERSION			0x5c
  #define DW_SPI_DR			0x60
  
  /* Bit fields in CTRLR0 */
  #define SPI_DFS_OFFSET			0
  
  #define SPI_FRF_OFFSET			4
  #define SPI_FRF_SPI			0x0
  #define SPI_FRF_SSP			0x1
  #define SPI_FRF_MICROWIRE		0x2
  #define SPI_FRF_RESV			0x3
  
  #define SPI_MODE_OFFSET			6
  #define SPI_SCPH_OFFSET			6
  #define SPI_SCOL_OFFSET			7
  
  #define SPI_TMOD_OFFSET			8
  #define SPI_TMOD_MASK			(0x3 << SPI_TMOD_OFFSET)
  #define	SPI_TMOD_TR			0x0		/* xmit & recv */
  #define SPI_TMOD_TO			0x1		/* xmit only */
  #define SPI_TMOD_RO			0x2		/* recv only */
  #define SPI_TMOD_EPROMREAD		0x3		/* eeprom read mode */
  
  #define SPI_SLVOE_OFFSET		10
  #define SPI_SRL_OFFSET			11
  #define SPI_CFS_OFFSET			12
  
  /* Bit fields in SR, 7 bits */
95e77d904   Jagan Teki   spi: designware_s...
79
  #define SR_MASK				GENMASK(6, 0)	/* cover 7 bits */
431a9f028   Jagan Teki   spi: designware_s...
80
81
82
83
84
85
86
  #define SR_BUSY				BIT(0)
  #define SR_TF_NOT_FULL			BIT(1)
  #define SR_TF_EMPT			BIT(2)
  #define SR_RF_NOT_EMPT			BIT(3)
  #define SR_RF_FULL			BIT(4)
  #define SR_TX_ERR			BIT(5)
  #define SR_DCOL				BIT(6)
5bef6fd79   Stefan Roese   spi: Add designwa...
87

a72f80208   Stefan Roese   spi: designware_s...
88
  #define RX_TIMEOUT			1000		/* timeout in ms */
5bef6fd79   Stefan Roese   spi: Add designwa...
89
90
91
92
93
94
95
96
97
98
  
  struct dw_spi_platdata {
  	s32 frequency;		/* Default clock frequency, -1 for none */
  	void __iomem *regs;
  };
  
  struct dw_spi_priv {
  	void __iomem *regs;
  	unsigned int freq;		/* Default frequency */
  	unsigned int mode;
58c125b9e   Eugeniy Paltsev   DW SPI: Get clock...
99
100
  	struct clk clk;
  	unsigned long bus_clk_rate;
5bef6fd79   Stefan Roese   spi: Add designwa...
101

bcdcb3e61   Eugeniy Paltsev   DW SPI: add optio...
102
  	struct gpio_desc cs_gpio;	/* External chip-select gpio */
5bef6fd79   Stefan Roese   spi: Add designwa...
103
104
  	int bits_per_word;
  	u8 cs;			/* chip select pin */
5bef6fd79   Stefan Roese   spi: Add designwa...
105
106
107
108
109
110
111
112
113
  	u8 tmode;		/* TR/TO/RO/EEPROM */
  	u8 type;		/* SPI/SSP/MicroWire */
  	int len;
  
  	u32 fifo_len;		/* depth of the FIFO buffer */
  	void *tx;
  	void *tx_end;
  	void *rx;
  	void *rx_end;
6ac5909f5   Ley Foon Tan   spi: designware_s...
114
115
  
  	struct reset_ctl_bulk	resets;
5bef6fd79   Stefan Roese   spi: Add designwa...
116
  };
4b5f6c52e   Eugeniy Paltsev   DW SPI: use 32 bi...
117
  static inline u32 dw_read(struct dw_spi_priv *priv, u32 offset)
5bef6fd79   Stefan Roese   spi: Add designwa...
118
119
120
  {
  	return __raw_readl(priv->regs + offset);
  }
4b5f6c52e   Eugeniy Paltsev   DW SPI: use 32 bi...
121
  static inline void dw_write(struct dw_spi_priv *priv, u32 offset, u32 val)
5bef6fd79   Stefan Roese   spi: Add designwa...
122
123
124
  {
  	__raw_writel(val, priv->regs + offset);
  }
bcdcb3e61   Eugeniy Paltsev   DW SPI: add optio...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  static int request_gpio_cs(struct udevice *bus)
  {
  #if defined(CONFIG_DM_GPIO) && !defined(CONFIG_SPL_BUILD)
  	struct dw_spi_priv *priv = dev_get_priv(bus);
  	int ret;
  
  	/* External chip select gpio line is optional */
  	ret = gpio_request_by_name(bus, "cs-gpio", 0, &priv->cs_gpio, 0);
  	if (ret == -ENOENT)
  		return 0;
  
  	if (ret < 0) {
  		printf("Error: %d: Can't get %s gpio!
  ", ret, bus->name);
  		return ret;
  	}
  
  	if (dm_gpio_is_valid(&priv->cs_gpio)) {
  		dm_gpio_set_dir_flags(&priv->cs_gpio,
  				      GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
  	}
  
  	debug("%s: used external gpio for CS management
  ", __func__);
  #endif
  	return 0;
  }
5bef6fd79   Stefan Roese   spi: Add designwa...
152
153
154
155
  static int dw_spi_ofdata_to_platdata(struct udevice *bus)
  {
  	struct dw_spi_platdata *plat = bus->platdata;
  	const void *blob = gd->fdt_blob;
e160f7d43   Simon Glass   dm: core: Replace...
156
  	int node = dev_of_offset(bus);
5bef6fd79   Stefan Roese   spi: Add designwa...
157

a821c4af7   Simon Glass   dm: Rename dev_ad...
158
  	plat->regs = (struct dw_spi *)devfdt_get_addr(bus);
5bef6fd79   Stefan Roese   spi: Add designwa...
159
160
161
162
163
164
165
  
  	/* Use 500KHz as a suitable default */
  	plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  					500000);
  	debug("%s: regs=%p max-frequency=%d
  ", __func__, plat->regs,
  	      plat->frequency);
bcdcb3e61   Eugeniy Paltsev   DW SPI: add optio...
166
  	return request_gpio_cs(bus);
5bef6fd79   Stefan Roese   spi: Add designwa...
167
168
169
170
  }
  
  static inline void spi_enable_chip(struct dw_spi_priv *priv, int enable)
  {
4b5f6c52e   Eugeniy Paltsev   DW SPI: use 32 bi...
171
  	dw_write(priv, DW_SPI_SSIENR, (enable ? 1 : 0));
5bef6fd79   Stefan Roese   spi: Add designwa...
172
173
174
175
176
177
  }
  
  /* Restart the controller, disable all interrupts, clean rx fifo */
  static void spi_hw_init(struct dw_spi_priv *priv)
  {
  	spi_enable_chip(priv, 0);
4b5f6c52e   Eugeniy Paltsev   DW SPI: use 32 bi...
178
  	dw_write(priv, DW_SPI_IMR, 0xff);
5bef6fd79   Stefan Roese   spi: Add designwa...
179
180
181
182
183
184
185
186
  	spi_enable_chip(priv, 1);
  
  	/*
  	 * Try to detect the FIFO depth if not set by interface driver,
  	 * the depth could be from 2 to 256 from HW spec
  	 */
  	if (!priv->fifo_len) {
  		u32 fifo;
52091ad14   Axel Lin   spi: designware_s...
187
  		for (fifo = 1; fifo < 256; fifo++) {
4b5f6c52e   Eugeniy Paltsev   DW SPI: use 32 bi...
188
189
  			dw_write(priv, DW_SPI_TXFLTR, fifo);
  			if (fifo != dw_read(priv, DW_SPI_TXFLTR))
5bef6fd79   Stefan Roese   spi: Add designwa...
190
191
  				break;
  		}
52091ad14   Axel Lin   spi: designware_s...
192
  		priv->fifo_len = (fifo == 1) ? 0 : fifo;
4b5f6c52e   Eugeniy Paltsev   DW SPI: use 32 bi...
193
  		dw_write(priv, DW_SPI_TXFLTR, 0);
5bef6fd79   Stefan Roese   spi: Add designwa...
194
195
196
197
  	}
  	debug("%s: fifo_len=%d
  ", __func__, priv->fifo_len);
  }
58c125b9e   Eugeniy Paltsev   DW SPI: Get clock...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  /*
   * We define dw_spi_get_clk function as 'weak' as some targets
   * (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) don't use standard clock API
   * and implement dw_spi_get_clk their own way in their clock manager.
   */
  __weak int dw_spi_get_clk(struct udevice *bus, ulong *rate)
  {
  	struct dw_spi_priv *priv = dev_get_priv(bus);
  	int ret;
  
  	ret = clk_get_by_index(bus, 0, &priv->clk);
  	if (ret)
  		return ret;
  
  	ret = clk_enable(&priv->clk);
  	if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
  		return ret;
  
  	*rate = clk_get_rate(&priv->clk);
  	if (!*rate)
  		goto err_rate;
  
  	debug("%s: get spi controller clk via device tree: %lu Hz
  ",
  	      __func__, *rate);
  
  	return 0;
  
  err_rate:
  	clk_disable(&priv->clk);
  	clk_free(&priv->clk);
  
  	return -EINVAL;
  }
6ac5909f5   Ley Foon Tan   spi: designware_s...
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  static int dw_spi_reset(struct udevice *bus)
  {
  	int ret;
  	struct dw_spi_priv *priv = dev_get_priv(bus);
  
  	ret = reset_get_bulk(bus, &priv->resets);
  	if (ret) {
  		/*
  		 * Return 0 if error due to !CONFIG_DM_RESET and reset
  		 * DT property is not present.
  		 */
  		if (ret == -ENOENT || ret == -ENOTSUPP)
  			return 0;
  
  		dev_warn(bus, "Can't get reset: %d
  ", ret);
  		return ret;
  	}
  
  	ret = reset_deassert_bulk(&priv->resets);
  	if (ret) {
  		reset_release_bulk(&priv->resets);
  		dev_err(bus, "Failed to reset: %d
  ", ret);
  		return ret;
  	}
  
  	return 0;
  }
5bef6fd79   Stefan Roese   spi: Add designwa...
261
262
263
264
  static int dw_spi_probe(struct udevice *bus)
  {
  	struct dw_spi_platdata *plat = dev_get_platdata(bus);
  	struct dw_spi_priv *priv = dev_get_priv(bus);
58c125b9e   Eugeniy Paltsev   DW SPI: Get clock...
265
  	int ret;
5bef6fd79   Stefan Roese   spi: Add designwa...
266
267
268
  
  	priv->regs = plat->regs;
  	priv->freq = plat->frequency;
58c125b9e   Eugeniy Paltsev   DW SPI: Get clock...
269
270
271
  	ret = dw_spi_get_clk(bus, &priv->bus_clk_rate);
  	if (ret)
  		return ret;
6ac5909f5   Ley Foon Tan   spi: designware_s...
272
273
274
  	ret = dw_spi_reset(bus);
  	if (ret)
  		return ret;
5bef6fd79   Stefan Roese   spi: Add designwa...
275
276
  	/* Currently only bits_per_word == 8 supported */
  	priv->bits_per_word = 8;
5bef6fd79   Stefan Roese   spi: Add designwa...
277
278
279
280
281
282
283
284
285
286
287
288
289
  
  	priv->tmode = 0; /* Tx & Rx */
  
  	/* Basic HW init */
  	spi_hw_init(priv);
  
  	return 0;
  }
  
  /* Return the max entries we can fill into tx fifo */
  static inline u32 tx_max(struct dw_spi_priv *priv)
  {
  	u32 tx_left, tx_room, rxtx_gap;
a72f80208   Stefan Roese   spi: designware_s...
290
  	tx_left = (priv->tx_end - priv->tx) / (priv->bits_per_word >> 3);
4b5f6c52e   Eugeniy Paltsev   DW SPI: use 32 bi...
291
  	tx_room = priv->fifo_len - dw_read(priv, DW_SPI_TXFLR);
5bef6fd79   Stefan Roese   spi: Add designwa...
292
293
294
  
  	/*
  	 * Another concern is about the tx/rx mismatch, we
a72f80208   Stefan Roese   spi: designware_s...
295
  	 * thought about using (priv->fifo_len - rxflr - txflr) as
5bef6fd79   Stefan Roese   spi: Add designwa...
296
297
298
299
300
301
  	 * one maximum value for tx, but it doesn't cover the
  	 * data which is out of tx/rx fifo and inside the
  	 * shift registers. So a control from sw point of
  	 * view is taken.
  	 */
  	rxtx_gap = ((priv->rx_end - priv->rx) - (priv->tx_end - priv->tx)) /
a72f80208   Stefan Roese   spi: designware_s...
302
  		(priv->bits_per_word >> 3);
5bef6fd79   Stefan Roese   spi: Add designwa...
303
304
305
306
307
308
309
  
  	return min3(tx_left, tx_room, (u32)(priv->fifo_len - rxtx_gap));
  }
  
  /* Return the max entries we should read out of rx fifo */
  static inline u32 rx_max(struct dw_spi_priv *priv)
  {
a72f80208   Stefan Roese   spi: designware_s...
310
  	u32 rx_left = (priv->rx_end - priv->rx) / (priv->bits_per_word >> 3);
5bef6fd79   Stefan Roese   spi: Add designwa...
311

4b5f6c52e   Eugeniy Paltsev   DW SPI: use 32 bi...
312
  	return min_t(u32, rx_left, dw_read(priv, DW_SPI_RXFLR));
5bef6fd79   Stefan Roese   spi: Add designwa...
313
314
315
316
317
318
319
320
321
322
  }
  
  static void dw_writer(struct dw_spi_priv *priv)
  {
  	u32 max = tx_max(priv);
  	u16 txw = 0;
  
  	while (max--) {
  		/* Set the tx word if the transfer's original "tx" is not null */
  		if (priv->tx_end - priv->len) {
a72f80208   Stefan Roese   spi: designware_s...
323
  			if (priv->bits_per_word == 8)
5bef6fd79   Stefan Roese   spi: Add designwa...
324
325
326
327
  				txw = *(u8 *)(priv->tx);
  			else
  				txw = *(u16 *)(priv->tx);
  		}
4b5f6c52e   Eugeniy Paltsev   DW SPI: use 32 bi...
328
  		dw_write(priv, DW_SPI_DR, txw);
5bef6fd79   Stefan Roese   spi: Add designwa...
329
330
  		debug("%s: tx=0x%02x
  ", __func__, txw);
a72f80208   Stefan Roese   spi: designware_s...
331
  		priv->tx += priv->bits_per_word >> 3;
5bef6fd79   Stefan Roese   spi: Add designwa...
332
333
  	}
  }
d3d8aaec7   Eugeniy Paltsev   DW SPI: refactor ...
334
  static void dw_reader(struct dw_spi_priv *priv)
5bef6fd79   Stefan Roese   spi: Add designwa...
335
  {
d3d8aaec7   Eugeniy Paltsev   DW SPI: refactor ...
336
  	u32 max = rx_max(priv);
5bef6fd79   Stefan Roese   spi: Add designwa...
337
  	u16 rxw;
5bef6fd79   Stefan Roese   spi: Add designwa...
338
  	while (max--) {
4b5f6c52e   Eugeniy Paltsev   DW SPI: use 32 bi...
339
  		rxw = dw_read(priv, DW_SPI_DR);
5bef6fd79   Stefan Roese   spi: Add designwa...
340
341
  		debug("%s: rx=0x%02x
  ", __func__, rxw);
a72f80208   Stefan Roese   spi: designware_s...
342

d3d8aaec7   Eugeniy Paltsev   DW SPI: refactor ...
343
  		/* Care about rx if the transfer's original "rx" is not null */
5bef6fd79   Stefan Roese   spi: Add designwa...
344
  		if (priv->rx_end - priv->len) {
a72f80208   Stefan Roese   spi: designware_s...
345
  			if (priv->bits_per_word == 8)
5bef6fd79   Stefan Roese   spi: Add designwa...
346
347
348
349
  				*(u8 *)(priv->rx) = rxw;
  			else
  				*(u16 *)(priv->rx) = rxw;
  		}
a72f80208   Stefan Roese   spi: designware_s...
350
  		priv->rx += priv->bits_per_word >> 3;
5bef6fd79   Stefan Roese   spi: Add designwa...
351
  	}
5bef6fd79   Stefan Roese   spi: Add designwa...
352
353
354
355
  }
  
  static int poll_transfer(struct dw_spi_priv *priv)
  {
5bef6fd79   Stefan Roese   spi: Add designwa...
356
357
  	do {
  		dw_writer(priv);
d3d8aaec7   Eugeniy Paltsev   DW SPI: refactor ...
358
  		dw_reader(priv);
5bef6fd79   Stefan Roese   spi: Add designwa...
359
360
361
362
  	} while (priv->rx_end > priv->rx);
  
  	return 0;
  }
bea91b0c9   Gregory CLEMENT   DW SPI: Allow to ...
363
364
365
366
367
368
369
  /*
   * We define external_cs_manage function as 'weak' as some targets
   * (like MSCC Ocelot) don't control the external CS pin using a GPIO
   * controller. These SoCs use specific registers to control by
   * software the SPI pins (and especially the CS).
   */
  __weak void external_cs_manage(struct udevice *dev, bool on)
bcdcb3e61   Eugeniy Paltsev   DW SPI: add optio...
370
371
372
373
374
375
376
377
378
379
  {
  #if defined(CONFIG_DM_GPIO) && !defined(CONFIG_SPL_BUILD)
  	struct dw_spi_priv *priv = dev_get_priv(dev->parent);
  
  	if (!dm_gpio_is_valid(&priv->cs_gpio))
  		return;
  
  	dm_gpio_set_value(&priv->cs_gpio, on ? 1 : 0);
  #endif
  }
5bef6fd79   Stefan Roese   spi: Add designwa...
380
381
382
383
384
385
386
387
388
  static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
  		       const void *dout, void *din, unsigned long flags)
  {
  	struct udevice *bus = dev->parent;
  	struct dw_spi_priv *priv = dev_get_priv(bus);
  	const u8 *tx = dout;
  	u8 *rx = din;
  	int ret = 0;
  	u32 cr0 = 0;
c6b4f031d   Eugeniy Paltsev   DW SPI: fix tx da...
389
  	u32 val;
5bef6fd79   Stefan Roese   spi: Add designwa...
390
391
392
393
394
395
396
397
  	u32 cs;
  
  	/* spi core configured to do 8 bit transfers */
  	if (bitlen % 8) {
  		debug("Non byte aligned SPI transfer.
  ");
  		return -1;
  	}
bcdcb3e61   Eugeniy Paltsev   DW SPI: add optio...
398
399
400
  	/* Start the transaction if necessary. */
  	if (flags & SPI_XFER_BEGIN)
  		external_cs_manage(dev, false);
a72f80208   Stefan Roese   spi: designware_s...
401
  	cr0 = (priv->bits_per_word - 1) | (priv->type << SPI_FRF_OFFSET) |
5bef6fd79   Stefan Roese   spi: Add designwa...
402
403
404
405
406
407
408
409
  		(priv->mode << SPI_MODE_OFFSET) |
  		(priv->tmode << SPI_TMOD_OFFSET);
  
  	if (rx && tx)
  		priv->tmode = SPI_TMOD_TR;
  	else if (rx)
  		priv->tmode = SPI_TMOD_RO;
  	else
fc282c7bc   Eugeniy Paltsev   DW SPI: fix trans...
410
411
412
413
414
  		/*
  		 * In transmit only mode (SPI_TMOD_TO) input FIFO never gets
  		 * any data which breaks our logic in poll_transfer() above.
  		 */
  		priv->tmode = SPI_TMOD_TR;
5bef6fd79   Stefan Roese   spi: Add designwa...
415
416
417
  
  	cr0 &= ~SPI_TMOD_MASK;
  	cr0 |= (priv->tmode << SPI_TMOD_OFFSET);
a72f80208   Stefan Roese   spi: designware_s...
418
  	priv->len = bitlen >> 3;
5bef6fd79   Stefan Roese   spi: Add designwa...
419
420
421
422
423
424
425
426
427
428
429
430
431
432
  	debug("%s: rx=%p tx=%p len=%d [bytes]
  ", __func__, rx, tx, priv->len);
  
  	priv->tx = (void *)tx;
  	priv->tx_end = priv->tx + priv->len;
  	priv->rx = rx;
  	priv->rx_end = priv->rx + priv->len;
  
  	/* Disable controller before writing control registers */
  	spi_enable_chip(priv, 0);
  
  	debug("%s: cr0=%08x
  ", __func__, cr0);
  	/* Reprogram cr0 only if changed */
4b5f6c52e   Eugeniy Paltsev   DW SPI: use 32 bi...
433
434
  	if (dw_read(priv, DW_SPI_CTRL0) != cr0)
  		dw_write(priv, DW_SPI_CTRL0, cr0);
5bef6fd79   Stefan Roese   spi: Add designwa...
435
436
437
438
439
440
441
  
  	/*
  	 * Configure the desired SS (slave select 0...3) in the controller
  	 * The DW SPI controller will activate and deactivate this CS
  	 * automatically. So no cs_activate() etc is needed in this driver.
  	 */
  	cs = spi_chip_select(dev);
4b5f6c52e   Eugeniy Paltsev   DW SPI: use 32 bi...
442
  	dw_write(priv, DW_SPI_SER, 1 << cs);
5bef6fd79   Stefan Roese   spi: Add designwa...
443
444
445
446
447
448
  
  	/* Enable controller after writing control registers */
  	spi_enable_chip(priv, 1);
  
  	/* Start transfer in a polling loop */
  	ret = poll_transfer(priv);
c6b4f031d   Eugeniy Paltsev   DW SPI: fix tx da...
449
450
451
452
453
454
455
456
  	/*
  	 * Wait for current transmit operation to complete.
  	 * Otherwise if some data still exists in Tx FIFO it can be
  	 * silently flushed, i.e. dropped on disabling of the controller,
  	 * which happens when writing 0 to DW_SPI_SSIENR which happens
  	 * in the beginning of new transfer.
  	 */
  	if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
9b14ac5cc   Eugeniy Paltsev   spi: dw: invert w...
457
  			       (val & SR_TF_EMPT) && !(val & SR_BUSY),
c6b4f031d   Eugeniy Paltsev   DW SPI: fix tx da...
458
459
460
  			       RX_TIMEOUT * 1000)) {
  		ret = -ETIMEDOUT;
  	}
bcdcb3e61   Eugeniy Paltsev   DW SPI: add optio...
461
462
463
  	/* Stop the transaction if necessary */
  	if (flags & SPI_XFER_END)
  		external_cs_manage(dev, true);
5bef6fd79   Stefan Roese   spi: Add designwa...
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
  	return ret;
  }
  
  static int dw_spi_set_speed(struct udevice *bus, uint speed)
  {
  	struct dw_spi_platdata *plat = bus->platdata;
  	struct dw_spi_priv *priv = dev_get_priv(bus);
  	u16 clk_div;
  
  	if (speed > plat->frequency)
  		speed = plat->frequency;
  
  	/* Disable controller before writing control registers */
  	spi_enable_chip(priv, 0);
  
  	/* clk_div doesn't support odd number */
58c125b9e   Eugeniy Paltsev   DW SPI: Get clock...
480
  	clk_div = priv->bus_clk_rate / speed;
5bef6fd79   Stefan Roese   spi: Add designwa...
481
  	clk_div = (clk_div + 1) & 0xfffe;
4b5f6c52e   Eugeniy Paltsev   DW SPI: use 32 bi...
482
  	dw_write(priv, DW_SPI_BAUDR, clk_div);
5bef6fd79   Stefan Roese   spi: Add designwa...
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
  
  	/* Enable controller after writing control registers */
  	spi_enable_chip(priv, 1);
  
  	priv->freq = speed;
  	debug("%s: regs=%p speed=%d clk_div=%d
  ", __func__, priv->regs,
  	      priv->freq, clk_div);
  
  	return 0;
  }
  
  static int dw_spi_set_mode(struct udevice *bus, uint mode)
  {
  	struct dw_spi_priv *priv = dev_get_priv(bus);
  
  	/*
  	 * Can't set mode yet. Since this depends on if rx, tx, or
  	 * rx & tx is requested. So we have to defer this to the
  	 * real transfer function.
  	 */
  	priv->mode = mode;
  	debug("%s: regs=%p, mode=%d
  ", __func__, priv->regs, priv->mode);
  
  	return 0;
  }
6ac5909f5   Ley Foon Tan   spi: designware_s...
510
511
512
513
514
515
  static int dw_spi_remove(struct udevice *bus)
  {
  	struct dw_spi_priv *priv = dev_get_priv(bus);
  
  	return reset_release_bulk(&priv->resets);
  }
5bef6fd79   Stefan Roese   spi: Add designwa...
516
517
518
519
520
521
522
523
524
525
526
  static const struct dm_spi_ops dw_spi_ops = {
  	.xfer		= dw_spi_xfer,
  	.set_speed	= dw_spi_set_speed,
  	.set_mode	= dw_spi_set_mode,
  	/*
  	 * cs_info is not needed, since we require all chip selects to be
  	 * in the device tree explicitly
  	 */
  };
  
  static const struct udevice_id dw_spi_ids[] = {
741148625   Marek Vasut   dt: socfpga: Rena...
527
  	{ .compatible = "snps,dw-apb-ssi" },
5bef6fd79   Stefan Roese   spi: Add designwa...
528
529
530
531
532
533
534
535
536
537
538
  	{ }
  };
  
  U_BOOT_DRIVER(dw_spi) = {
  	.name = "dw_spi",
  	.id = UCLASS_SPI,
  	.of_match = dw_spi_ids,
  	.ops = &dw_spi_ops,
  	.ofdata_to_platdata = dw_spi_ofdata_to_platdata,
  	.platdata_auto_alloc_size = sizeof(struct dw_spi_platdata),
  	.priv_auto_alloc_size = sizeof(struct dw_spi_priv),
5bef6fd79   Stefan Roese   spi: Add designwa...
539
  	.probe = dw_spi_probe,
6ac5909f5   Ley Foon Tan   spi: designware_s...
540
  	.remove = dw_spi_remove,
5bef6fd79   Stefan Roese   spi: Add designwa...
541
  };