blob: 8621738b76534dfa8baf4ebe1636432bcde89bec [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stephan Linzfc77d512012-07-29 00:25:35 +02002/*
3 * Xilinx SPI driver
4 *
Jagan Teki48a0dbd2015-06-27 00:51:27 +05305 * Supports 8 bit SPI transfers only, with or w/o FIFO
Stephan Linzfc77d512012-07-29 00:25:35 +02006 *
Jagan Teki48a0dbd2015-06-27 00:51:27 +05307 * Based on bfin_spi.c, by way of altera_spi.c
Jagan Tekifdc2b3d2015-06-29 13:15:18 +05308 * Copyright (c) 2015 Jagan Teki <jteki@openedev.com>
Stephan Linzfc77d512012-07-29 00:25:35 +02009 * Copyright (c) 2012 Stephan Linz <linz@li-pro.net>
Jagan Teki48a0dbd2015-06-27 00:51:27 +053010 * Copyright (c) 2010 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
11 * Copyright (c) 2010 Thomas Chou <thomas@wytron.com.tw>
12 * Copyright (c) 2005-2008 Analog Devices Inc.
Stephan Linzfc77d512012-07-29 00:25:35 +020013 */
Jagan Teki48a0dbd2015-06-27 00:51:27 +053014
Stephan Linzfc77d512012-07-29 00:25:35 +020015#include <config.h>
16#include <common.h>
Jagan Tekifdc2b3d2015-06-29 13:15:18 +053017#include <dm.h>
18#include <errno.h>
Stephan Linzfc77d512012-07-29 00:25:35 +020019#include <malloc.h>
20#include <spi.h>
Jagan Teki41fcbba2015-06-27 00:51:37 +053021#include <asm/io.h>
Vipul Kumar90098ba2018-06-30 08:15:18 +053022#include <wait_bit.h>
Stephan Linzfc77d512012-07-29 00:25:35 +020023
Jagan Teki23e281d2015-06-27 00:51:26 +053024/*
Jagan Teki48a0dbd2015-06-27 00:51:27 +053025 * [0]: http://www.xilinx.com/support/documentation
Jagan Teki23e281d2015-06-27 00:51:26 +053026 *
Jagan Teki48a0dbd2015-06-27 00:51:27 +053027 * Xilinx SPI Register Definitions
Jagan Teki23e281d2015-06-27 00:51:26 +053028 * [1]: [0]/ip_documentation/xps_spi.pdf
29 * page 8, Register Descriptions
30 * [2]: [0]/ip_documentation/axi_spi_ds742.pdf
31 * page 7, Register Overview Table
32 */
Jagan Teki23e281d2015-06-27 00:51:26 +053033
34/* SPI Control Register (spicr), [1] p9, [2] p8 */
Jagan Tekif0a01412015-10-23 01:39:31 +053035#define SPICR_LSB_FIRST BIT(9)
36#define SPICR_MASTER_INHIBIT BIT(8)
37#define SPICR_MANUAL_SS BIT(7)
38#define SPICR_RXFIFO_RESEST BIT(6)
39#define SPICR_TXFIFO_RESEST BIT(5)
40#define SPICR_CPHA BIT(4)
41#define SPICR_CPOL BIT(3)
42#define SPICR_MASTER_MODE BIT(2)
43#define SPICR_SPE BIT(1)
44#define SPICR_LOOP BIT(0)
Jagan Teki23e281d2015-06-27 00:51:26 +053045
46/* SPI Status Register (spisr), [1] p11, [2] p10 */
Jagan Tekif0a01412015-10-23 01:39:31 +053047#define SPISR_SLAVE_MODE_SELECT BIT(5)
48#define SPISR_MODF BIT(4)
49#define SPISR_TX_FULL BIT(3)
50#define SPISR_TX_EMPTY BIT(2)
51#define SPISR_RX_FULL BIT(1)
52#define SPISR_RX_EMPTY BIT(0)
Jagan Teki23e281d2015-06-27 00:51:26 +053053
54/* SPI Data Transmit Register (spidtr), [1] p12, [2] p12 */
Jagan Tekia2888d02015-10-23 01:03:44 +053055#define SPIDTR_8BIT_MASK GENMASK(7, 0)
56#define SPIDTR_16BIT_MASK GENMASK(15, 0)
57#define SPIDTR_32BIT_MASK GENMASK(31, 0)
Jagan Teki23e281d2015-06-27 00:51:26 +053058
59/* SPI Data Receive Register (spidrr), [1] p12, [2] p12 */
Jagan Tekia2888d02015-10-23 01:03:44 +053060#define SPIDRR_8BIT_MASK GENMASK(7, 0)
61#define SPIDRR_16BIT_MASK GENMASK(15, 0)
62#define SPIDRR_32BIT_MASK GENMASK(31, 0)
Jagan Teki23e281d2015-06-27 00:51:26 +053063
64/* SPI Slave Select Register (spissr), [1] p13, [2] p13 */
65#define SPISSR_MASK(cs) (1 << (cs))
66#define SPISSR_ACT(cs) ~SPISSR_MASK(cs)
67#define SPISSR_OFF ~0UL
68
Jagan Teki23e281d2015-06-27 00:51:26 +053069/* SPI Software Reset Register (ssr) */
70#define SPISSR_RESET_VALUE 0x0a
71
Jagan Teki48a0dbd2015-06-27 00:51:27 +053072#define XILSPI_MAX_XFER_BITS 8
73#define XILSPI_SPICR_DFLT_ON (SPICR_MANUAL_SS | SPICR_MASTER_MODE | \
74 SPICR_SPE)
75#define XILSPI_SPICR_DFLT_OFF (SPICR_MASTER_INHIBIT | SPICR_MANUAL_SS)
76
77#ifndef CONFIG_XILINX_SPI_IDLE_VAL
Jagan Tekia2888d02015-10-23 01:03:44 +053078#define CONFIG_XILINX_SPI_IDLE_VAL GENMASK(7, 0)
Jagan Teki48a0dbd2015-06-27 00:51:27 +053079#endif
80
Vipul Kumar90098ba2018-06-30 08:15:18 +053081#define XILINX_SPISR_TIMEOUT 10000 /* in milliseconds */
82
Jagan Teki48a0dbd2015-06-27 00:51:27 +053083/* xilinx spi register set */
Jagan Tekifdc2b3d2015-06-29 13:15:18 +053084struct xilinx_spi_regs {
Jagan Teki48a0dbd2015-06-27 00:51:27 +053085 u32 __space0__[7];
86 u32 dgier; /* Device Global Interrupt Enable Register (DGIER) */
87 u32 ipisr; /* IP Interrupt Status Register (IPISR) */
88 u32 __space1__;
89 u32 ipier; /* IP Interrupt Enable Register (IPIER) */
90 u32 __space2__[5];
91 u32 srr; /* Softare Reset Register (SRR) */
92 u32 __space3__[7];
93 u32 spicr; /* SPI Control Register (SPICR) */
94 u32 spisr; /* SPI Status Register (SPISR) */
95 u32 spidtr; /* SPI Data Transmit Register (SPIDTR) */
96 u32 spidrr; /* SPI Data Receive Register (SPIDRR) */
97 u32 spissr; /* SPI Slave Select Register (SPISSR) */
98 u32 spitfor; /* SPI Transmit FIFO Occupancy Register (SPITFOR) */
99 u32 spirfor; /* SPI Receive FIFO Occupancy Register (SPIRFOR) */
100};
101
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530102/* xilinx spi priv */
103struct xilinx_spi_priv {
104 struct xilinx_spi_regs *regs;
Jagan Teki23e281d2015-06-27 00:51:26 +0530105 unsigned int freq;
106 unsigned int mode;
Vipul Kumar90098ba2018-06-30 08:15:18 +0530107 unsigned int fifo_depth;
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530108 u8 startup;
Jagan Teki23e281d2015-06-27 00:51:26 +0530109};
110
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530111static int xilinx_spi_probe(struct udevice *bus)
Stephan Linzfc77d512012-07-29 00:25:35 +0200112{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530113 struct xilinx_spi_priv *priv = dev_get_priv(bus);
114 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200115
Michal Simek1ba13862018-06-30 08:15:17 +0530116 priv->regs = (struct xilinx_spi_regs *)devfdt_get_addr(bus);
Stephan Linzfc77d512012-07-29 00:25:35 +0200117
Vipul Kumar90098ba2018-06-30 08:15:18 +0530118 priv->fifo_depth = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
119 "fifo-size", 0);
120
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530121 writel(SPISSR_RESET_VALUE, &regs->srr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200122
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530123 return 0;
Stephan Linzfc77d512012-07-29 00:25:35 +0200124}
125
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530126static void spi_cs_activate(struct udevice *dev, uint cs)
Stephan Linzfc77d512012-07-29 00:25:35 +0200127{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530128 struct udevice *bus = dev_get_parent(dev);
129 struct xilinx_spi_priv *priv = dev_get_priv(bus);
130 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200131
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530132 writel(SPISSR_ACT(cs), &regs->spissr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200133}
134
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530135static void spi_cs_deactivate(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200136{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530137 struct udevice *bus = dev_get_parent(dev);
138 struct xilinx_spi_priv *priv = dev_get_priv(bus);
139 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200140
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530141 writel(SPISSR_OFF, &regs->spissr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200142}
143
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530144static int xilinx_spi_claim_bus(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200145{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530146 struct udevice *bus = dev_get_parent(dev);
147 struct xilinx_spi_priv *priv = dev_get_priv(bus);
148 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200149
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530150 writel(SPISSR_OFF, &regs->spissr);
151 writel(XILSPI_SPICR_DFLT_ON, &regs->spicr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200152
Stephan Linzfc77d512012-07-29 00:25:35 +0200153 return 0;
154}
155
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530156static int xilinx_spi_release_bus(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200157{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530158 struct udevice *bus = dev_get_parent(dev);
159 struct xilinx_spi_priv *priv = dev_get_priv(bus);
160 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200161
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530162 writel(SPISSR_OFF, &regs->spissr);
163 writel(XILSPI_SPICR_DFLT_OFF, &regs->spicr);
164
165 return 0;
Stephan Linzfc77d512012-07-29 00:25:35 +0200166}
167
Vipul Kumar90098ba2018-06-30 08:15:18 +0530168static u32 xilinx_spi_fill_txfifo(struct udevice *bus, const u8 *txp,
169 u32 txbytes)
170{
171 struct xilinx_spi_priv *priv = dev_get_priv(bus);
172 struct xilinx_spi_regs *regs = priv->regs;
173 unsigned char d;
174 u32 i = 0;
175
176 while (txbytes && !(readl(&regs->spisr) & SPISR_TX_FULL) &&
177 i < priv->fifo_depth) {
178 d = txp ? *txp++ : CONFIG_XILINX_SPI_IDLE_VAL;
179 debug("spi_xfer: tx:%x ", d);
180 /* write out and wait for processing (receive data) */
181 writel(d & SPIDTR_8BIT_MASK, &regs->spidtr);
182 txbytes--;
183 i++;
184 }
185
186 return i;
187}
188
189static u32 xilinx_spi_read_rxfifo(struct udevice *bus, u8 *rxp, u32 rxbytes)
190{
191 struct xilinx_spi_priv *priv = dev_get_priv(bus);
192 struct xilinx_spi_regs *regs = priv->regs;
193 unsigned char d;
194 unsigned int i = 0;
195
196 while (rxbytes && !(readl(&regs->spisr) & SPISR_RX_EMPTY)) {
197 d = readl(&regs->spidrr) & SPIDRR_8BIT_MASK;
198 if (rxp)
199 *rxp++ = d;
200 debug("spi_xfer: rx:%x\n", d);
201 rxbytes--;
202 i++;
203 }
204 debug("Rx_done\n");
205
206 return i;
207}
208
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530209static void xilinx_spi_startup_block(struct udevice *dev, unsigned int bytes,
210 const void *dout, void *din)
211{
212 struct udevice *bus = dev_get_parent(dev);
213 struct xilinx_spi_priv *priv = dev_get_priv(bus);
214 struct xilinx_spi_regs *regs = priv->regs;
215 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
216 const unsigned char *txp = dout;
217 unsigned char *rxp = din;
218 u32 reg, count;
219 u32 txbytes = bytes;
220 u32 rxbytes = bytes;
221
222 /*
223 * This loop runs two times. First time to send the command.
224 * Second time to transfer data. After transferring data,
225 * it sets txp to the initial value for the normal operation.
226 */
227 for ( ; priv->startup < 2; priv->startup++) {
228 count = xilinx_spi_fill_txfifo(bus, txp, txbytes);
229 reg = readl(&regs->spicr) & ~SPICR_MASTER_INHIBIT;
230 writel(reg, &regs->spicr);
231 count = xilinx_spi_read_rxfifo(bus, rxp, rxbytes);
232 txp = din;
233
234 if (priv->startup) {
235 spi_cs_deactivate(dev);
236 spi_cs_activate(dev, slave_plat->cs);
237 txp = dout;
238 }
239 }
240}
241
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530242static int xilinx_spi_xfer(struct udevice *dev, unsigned int bitlen,
243 const void *dout, void *din, unsigned long flags)
Stephan Linzfc77d512012-07-29 00:25:35 +0200244{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530245 struct udevice *bus = dev_get_parent(dev);
246 struct xilinx_spi_priv *priv = dev_get_priv(bus);
247 struct xilinx_spi_regs *regs = priv->regs;
248 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
Stephan Linzfc77d512012-07-29 00:25:35 +0200249 /* assume spi core configured to do 8 bit transfers */
250 unsigned int bytes = bitlen / XILSPI_MAX_XFER_BITS;
251 const unsigned char *txp = dout;
252 unsigned char *rxp = din;
Vipul Kumar90098ba2018-06-30 08:15:18 +0530253 u32 txbytes = bytes;
254 u32 rxbytes = bytes;
255 u32 reg, count, timeout;
256 int ret;
Stephan Linzfc77d512012-07-29 00:25:35 +0200257
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530258 debug("spi_xfer: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n",
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530259 bus->seq, slave_plat->cs, bitlen, bytes, flags);
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530260
Stephan Linzfc77d512012-07-29 00:25:35 +0200261 if (bitlen == 0)
262 goto done;
263
264 if (bitlen % XILSPI_MAX_XFER_BITS) {
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530265 printf("XILSPI warning: Not a multiple of %d bits\n",
266 XILSPI_MAX_XFER_BITS);
Stephan Linzfc77d512012-07-29 00:25:35 +0200267 flags |= SPI_XFER_END;
268 goto done;
269 }
270
Stephan Linzfc77d512012-07-29 00:25:35 +0200271 if (flags & SPI_XFER_BEGIN)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530272 spi_cs_activate(dev, slave_plat->cs);
Stephan Linzfc77d512012-07-29 00:25:35 +0200273
Vipul Kumarc7e2aae2018-06-30 08:15:19 +0530274 /*
275 * This is the work around for the startup block issue in
276 * the spi controller. SPI clock is passing through STARTUP
277 * block to FLASH. STARTUP block don't provide clock as soon
278 * as QSPI provides command. So first command fails.
279 */
280 xilinx_spi_startup_block(dev, bytes, dout, din);
Stephan Linzfc77d512012-07-29 00:25:35 +0200281
Vipul Kumar90098ba2018-06-30 08:15:18 +0530282 while (txbytes && rxbytes) {
283 count = xilinx_spi_fill_txfifo(bus, txp, txbytes);
284 reg = readl(&regs->spicr) & ~SPICR_MASTER_INHIBIT;
285 writel(reg, &regs->spicr);
286 txbytes -= count;
287 if (txp)
288 txp += count;
Stephan Linzfc77d512012-07-29 00:25:35 +0200289
Vipul Kumar90098ba2018-06-30 08:15:18 +0530290 ret = wait_for_bit_le32(&regs->spisr, SPISR_TX_EMPTY, true,
291 XILINX_SPISR_TIMEOUT, false);
292 if (ret < 0) {
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530293 printf("XILSPI error: Xfer timeout\n");
Vipul Kumar90098ba2018-06-30 08:15:18 +0530294 return ret;
Stephan Linzfc77d512012-07-29 00:25:35 +0200295 }
296
Vipul Kumar90098ba2018-06-30 08:15:18 +0530297 debug("txbytes:0x%x,txp:0x%p\n", txbytes, txp);
298 count = xilinx_spi_read_rxfifo(bus, rxp, rxbytes);
299 rxbytes -= count;
Stephan Linzfc77d512012-07-29 00:25:35 +0200300 if (rxp)
Vipul Kumar90098ba2018-06-30 08:15:18 +0530301 rxp += count;
302 debug("rxbytes:0x%x rxp:0x%p\n", rxbytes, rxp);
Stephan Linzfc77d512012-07-29 00:25:35 +0200303 }
304
305 done:
306 if (flags & SPI_XFER_END)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530307 spi_cs_deactivate(dev);
Stephan Linzfc77d512012-07-29 00:25:35 +0200308
309 return 0;
310}
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530311
312static int xilinx_spi_set_speed(struct udevice *bus, uint speed)
313{
314 struct xilinx_spi_priv *priv = dev_get_priv(bus);
315
316 priv->freq = speed;
317
Jagan Teki0dc543f2015-09-08 01:26:29 +0530318 debug("xilinx_spi_set_speed: regs=%p, speed=%d\n", priv->regs,
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530319 priv->freq);
320
321 return 0;
322}
323
324static int xilinx_spi_set_mode(struct udevice *bus, uint mode)
325{
326 struct xilinx_spi_priv *priv = dev_get_priv(bus);
327 struct xilinx_spi_regs *regs = priv->regs;
328 uint32_t spicr;
329
330 spicr = readl(&regs->spicr);
Jagan Teki0dc543f2015-09-08 01:26:29 +0530331 if (mode & SPI_LSB_FIRST)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530332 spicr |= SPICR_LSB_FIRST;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530333 if (mode & SPI_CPHA)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530334 spicr |= SPICR_CPHA;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530335 if (mode & SPI_CPOL)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530336 spicr |= SPICR_CPOL;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530337 if (mode & SPI_LOOP)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530338 spicr |= SPICR_LOOP;
339
340 writel(spicr, &regs->spicr);
341 priv->mode = mode;
342
343 debug("xilinx_spi_set_mode: regs=%p, mode=%d\n", priv->regs,
344 priv->mode);
345
346 return 0;
347}
348
349static const struct dm_spi_ops xilinx_spi_ops = {
350 .claim_bus = xilinx_spi_claim_bus,
351 .release_bus = xilinx_spi_release_bus,
352 .xfer = xilinx_spi_xfer,
353 .set_speed = xilinx_spi_set_speed,
354 .set_mode = xilinx_spi_set_mode,
355};
356
357static const struct udevice_id xilinx_spi_ids[] = {
Michal Simek7465f312015-12-11 12:41:14 +0100358 { .compatible = "xlnx,xps-spi-2.00.a" },
359 { .compatible = "xlnx,xps-spi-2.00.b" },
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530360 { }
361};
362
363U_BOOT_DRIVER(xilinx_spi) = {
364 .name = "xilinx_spi",
365 .id = UCLASS_SPI,
366 .of_match = xilinx_spi_ids,
367 .ops = &xilinx_spi_ops,
368 .priv_auto_alloc_size = sizeof(struct xilinx_spi_priv),
369 .probe = xilinx_spi_probe,
370};