blob: 8f0f32f68f6bcb4fe15cb3c2ed5c050b2ba6b95d [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>
Stephan Linzfc77d512012-07-29 00:25:35 +020022
Jagan Teki23e281d2015-06-27 00:51:26 +053023/*
Jagan Teki48a0dbd2015-06-27 00:51:27 +053024 * [0]: http://www.xilinx.com/support/documentation
Jagan Teki23e281d2015-06-27 00:51:26 +053025 *
Jagan Teki48a0dbd2015-06-27 00:51:27 +053026 * Xilinx SPI Register Definitions
Jagan Teki23e281d2015-06-27 00:51:26 +053027 * [1]: [0]/ip_documentation/xps_spi.pdf
28 * page 8, Register Descriptions
29 * [2]: [0]/ip_documentation/axi_spi_ds742.pdf
30 * page 7, Register Overview Table
31 */
Jagan Teki23e281d2015-06-27 00:51:26 +053032
33/* SPI Control Register (spicr), [1] p9, [2] p8 */
Jagan Tekif0a01412015-10-23 01:39:31 +053034#define SPICR_LSB_FIRST BIT(9)
35#define SPICR_MASTER_INHIBIT BIT(8)
36#define SPICR_MANUAL_SS BIT(7)
37#define SPICR_RXFIFO_RESEST BIT(6)
38#define SPICR_TXFIFO_RESEST BIT(5)
39#define SPICR_CPHA BIT(4)
40#define SPICR_CPOL BIT(3)
41#define SPICR_MASTER_MODE BIT(2)
42#define SPICR_SPE BIT(1)
43#define SPICR_LOOP BIT(0)
Jagan Teki23e281d2015-06-27 00:51:26 +053044
45/* SPI Status Register (spisr), [1] p11, [2] p10 */
Jagan Tekif0a01412015-10-23 01:39:31 +053046#define SPISR_SLAVE_MODE_SELECT BIT(5)
47#define SPISR_MODF BIT(4)
48#define SPISR_TX_FULL BIT(3)
49#define SPISR_TX_EMPTY BIT(2)
50#define SPISR_RX_FULL BIT(1)
51#define SPISR_RX_EMPTY BIT(0)
Jagan Teki23e281d2015-06-27 00:51:26 +053052
53/* SPI Data Transmit Register (spidtr), [1] p12, [2] p12 */
Jagan Tekia2888d02015-10-23 01:03:44 +053054#define SPIDTR_8BIT_MASK GENMASK(7, 0)
55#define SPIDTR_16BIT_MASK GENMASK(15, 0)
56#define SPIDTR_32BIT_MASK GENMASK(31, 0)
Jagan Teki23e281d2015-06-27 00:51:26 +053057
58/* SPI Data Receive Register (spidrr), [1] p12, [2] p12 */
Jagan Tekia2888d02015-10-23 01:03:44 +053059#define SPIDRR_8BIT_MASK GENMASK(7, 0)
60#define SPIDRR_16BIT_MASK GENMASK(15, 0)
61#define SPIDRR_32BIT_MASK GENMASK(31, 0)
Jagan Teki23e281d2015-06-27 00:51:26 +053062
63/* SPI Slave Select Register (spissr), [1] p13, [2] p13 */
64#define SPISSR_MASK(cs) (1 << (cs))
65#define SPISSR_ACT(cs) ~SPISSR_MASK(cs)
66#define SPISSR_OFF ~0UL
67
Jagan Teki23e281d2015-06-27 00:51:26 +053068/* SPI Software Reset Register (ssr) */
69#define SPISSR_RESET_VALUE 0x0a
70
Jagan Teki48a0dbd2015-06-27 00:51:27 +053071#define XILSPI_MAX_XFER_BITS 8
72#define XILSPI_SPICR_DFLT_ON (SPICR_MANUAL_SS | SPICR_MASTER_MODE | \
73 SPICR_SPE)
74#define XILSPI_SPICR_DFLT_OFF (SPICR_MASTER_INHIBIT | SPICR_MANUAL_SS)
75
76#ifndef CONFIG_XILINX_SPI_IDLE_VAL
Jagan Tekia2888d02015-10-23 01:03:44 +053077#define CONFIG_XILINX_SPI_IDLE_VAL GENMASK(7, 0)
Jagan Teki48a0dbd2015-06-27 00:51:27 +053078#endif
79
80#ifndef CONFIG_SYS_XILINX_SPI_LIST
81#define CONFIG_SYS_XILINX_SPI_LIST { CONFIG_SYS_SPI_BASE }
82#endif
83
84/* xilinx spi register set */
Jagan Tekifdc2b3d2015-06-29 13:15:18 +053085struct xilinx_spi_regs {
Jagan Teki48a0dbd2015-06-27 00:51:27 +053086 u32 __space0__[7];
87 u32 dgier; /* Device Global Interrupt Enable Register (DGIER) */
88 u32 ipisr; /* IP Interrupt Status Register (IPISR) */
89 u32 __space1__;
90 u32 ipier; /* IP Interrupt Enable Register (IPIER) */
91 u32 __space2__[5];
92 u32 srr; /* Softare Reset Register (SRR) */
93 u32 __space3__[7];
94 u32 spicr; /* SPI Control Register (SPICR) */
95 u32 spisr; /* SPI Status Register (SPISR) */
96 u32 spidtr; /* SPI Data Transmit Register (SPIDTR) */
97 u32 spidrr; /* SPI Data Receive Register (SPIDRR) */
98 u32 spissr; /* SPI Slave Select Register (SPISSR) */
99 u32 spitfor; /* SPI Transmit FIFO Occupancy Register (SPITFOR) */
100 u32 spirfor; /* SPI Receive FIFO Occupancy Register (SPIRFOR) */
101};
102
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530103/* xilinx spi priv */
104struct xilinx_spi_priv {
105 struct xilinx_spi_regs *regs;
Jagan Teki23e281d2015-06-27 00:51:26 +0530106 unsigned int freq;
107 unsigned int mode;
108};
109
Stephan Linzfc77d512012-07-29 00:25:35 +0200110static unsigned long xilinx_spi_base_list[] = CONFIG_SYS_XILINX_SPI_LIST;
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
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530116 priv->regs = (struct xilinx_spi_regs *)xilinx_spi_base_list[bus->seq];
Stephan Linzfc77d512012-07-29 00:25:35 +0200117
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530118 writel(SPISSR_RESET_VALUE, &regs->srr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200119
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530120 return 0;
Stephan Linzfc77d512012-07-29 00:25:35 +0200121}
122
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530123static void spi_cs_activate(struct udevice *dev, uint cs)
Stephan Linzfc77d512012-07-29 00:25:35 +0200124{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530125 struct udevice *bus = dev_get_parent(dev);
126 struct xilinx_spi_priv *priv = dev_get_priv(bus);
127 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200128
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530129 writel(SPISSR_ACT(cs), &regs->spissr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200130}
131
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530132static void spi_cs_deactivate(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200133{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530134 struct udevice *bus = dev_get_parent(dev);
135 struct xilinx_spi_priv *priv = dev_get_priv(bus);
136 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200137
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530138 writel(SPISSR_OFF, &regs->spissr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200139}
140
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530141static int xilinx_spi_claim_bus(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200142{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530143 struct udevice *bus = dev_get_parent(dev);
144 struct xilinx_spi_priv *priv = dev_get_priv(bus);
145 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200146
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530147 writel(SPISSR_OFF, &regs->spissr);
148 writel(XILSPI_SPICR_DFLT_ON, &regs->spicr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200149
Stephan Linzfc77d512012-07-29 00:25:35 +0200150 return 0;
151}
152
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530153static int xilinx_spi_release_bus(struct udevice *dev)
Stephan Linzfc77d512012-07-29 00:25:35 +0200154{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530155 struct udevice *bus = dev_get_parent(dev);
156 struct xilinx_spi_priv *priv = dev_get_priv(bus);
157 struct xilinx_spi_regs *regs = priv->regs;
Stephan Linzfc77d512012-07-29 00:25:35 +0200158
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530159 writel(SPISSR_OFF, &regs->spissr);
160 writel(XILSPI_SPICR_DFLT_OFF, &regs->spicr);
161
162 return 0;
Stephan Linzfc77d512012-07-29 00:25:35 +0200163}
164
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530165static int xilinx_spi_xfer(struct udevice *dev, unsigned int bitlen,
166 const void *dout, void *din, unsigned long flags)
Stephan Linzfc77d512012-07-29 00:25:35 +0200167{
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530168 struct udevice *bus = dev_get_parent(dev);
169 struct xilinx_spi_priv *priv = dev_get_priv(bus);
170 struct xilinx_spi_regs *regs = priv->regs;
171 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
Stephan Linzfc77d512012-07-29 00:25:35 +0200172 /* assume spi core configured to do 8 bit transfers */
173 unsigned int bytes = bitlen / XILSPI_MAX_XFER_BITS;
174 const unsigned char *txp = dout;
175 unsigned char *rxp = din;
176 unsigned rxecount = 17; /* max. 16 elements in FIFO, leftover 1 */
Michal Simekf030d662014-01-22 09:48:55 +0100177 unsigned global_timeout;
Stephan Linzfc77d512012-07-29 00:25:35 +0200178
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530179 debug("spi_xfer: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n",
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530180 bus->seq, slave_plat->cs, bitlen, bytes, flags);
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530181
Stephan Linzfc77d512012-07-29 00:25:35 +0200182 if (bitlen == 0)
183 goto done;
184
185 if (bitlen % XILSPI_MAX_XFER_BITS) {
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530186 printf("XILSPI warning: Not a multiple of %d bits\n",
187 XILSPI_MAX_XFER_BITS);
Stephan Linzfc77d512012-07-29 00:25:35 +0200188 flags |= SPI_XFER_END;
189 goto done;
190 }
191
192 /* empty read buffer */
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530193 while (rxecount && !(readl(&regs->spisr) & SPISR_RX_EMPTY)) {
194 readl(&regs->spidrr);
Stephan Linzfc77d512012-07-29 00:25:35 +0200195 rxecount--;
196 }
197
198 if (!rxecount) {
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530199 printf("XILSPI error: Rx buffer not empty\n");
Stephan Linzfc77d512012-07-29 00:25:35 +0200200 return -1;
201 }
202
203 if (flags & SPI_XFER_BEGIN)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530204 spi_cs_activate(dev, slave_plat->cs);
Stephan Linzfc77d512012-07-29 00:25:35 +0200205
Michal Simekf030d662014-01-22 09:48:55 +0100206 /* at least 1usec or greater, leftover 1 */
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530207 global_timeout = priv->freq > XILSPI_MAX_XFER_BITS * 1000000 ? 2 :
208 (XILSPI_MAX_XFER_BITS * 1000000 / priv->freq) + 1;
Stephan Linzfc77d512012-07-29 00:25:35 +0200209
Michal Simekf030d662014-01-22 09:48:55 +0100210 while (bytes--) {
211 unsigned timeout = global_timeout;
Stephan Linzfc77d512012-07-29 00:25:35 +0200212 /* get Tx element from data out buffer and count up */
213 unsigned char d = txp ? *txp++ : CONFIG_XILINX_SPI_IDLE_VAL;
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530214 debug("spi_xfer: tx:%x ", d);
Stephan Linzfc77d512012-07-29 00:25:35 +0200215
216 /* write out and wait for processing (receive data) */
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530217 writel(d & SPIDTR_8BIT_MASK, &regs->spidtr);
218 while (timeout && readl(&regs->spisr)
Stephan Linzfc77d512012-07-29 00:25:35 +0200219 & SPISR_RX_EMPTY) {
220 timeout--;
221 udelay(1);
222 }
223
224 if (!timeout) {
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530225 printf("XILSPI error: Xfer timeout\n");
Stephan Linzfc77d512012-07-29 00:25:35 +0200226 return -1;
227 }
228
229 /* read Rx element and push into data in buffer */
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530230 d = readl(&regs->spidrr) & SPIDRR_8BIT_MASK;
Stephan Linzfc77d512012-07-29 00:25:35 +0200231 if (rxp)
232 *rxp++ = d;
Jagan Teki48a0dbd2015-06-27 00:51:27 +0530233 debug("spi_xfer: rx:%x\n", d);
Stephan Linzfc77d512012-07-29 00:25:35 +0200234 }
235
236 done:
237 if (flags & SPI_XFER_END)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530238 spi_cs_deactivate(dev);
Stephan Linzfc77d512012-07-29 00:25:35 +0200239
240 return 0;
241}
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530242
243static int xilinx_spi_set_speed(struct udevice *bus, uint speed)
244{
245 struct xilinx_spi_priv *priv = dev_get_priv(bus);
246
247 priv->freq = speed;
248
Jagan Teki0dc543f2015-09-08 01:26:29 +0530249 debug("xilinx_spi_set_speed: regs=%p, speed=%d\n", priv->regs,
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530250 priv->freq);
251
252 return 0;
253}
254
255static int xilinx_spi_set_mode(struct udevice *bus, uint mode)
256{
257 struct xilinx_spi_priv *priv = dev_get_priv(bus);
258 struct xilinx_spi_regs *regs = priv->regs;
259 uint32_t spicr;
260
261 spicr = readl(&regs->spicr);
Jagan Teki0dc543f2015-09-08 01:26:29 +0530262 if (mode & SPI_LSB_FIRST)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530263 spicr |= SPICR_LSB_FIRST;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530264 if (mode & SPI_CPHA)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530265 spicr |= SPICR_CPHA;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530266 if (mode & SPI_CPOL)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530267 spicr |= SPICR_CPOL;
Jagan Teki0dc543f2015-09-08 01:26:29 +0530268 if (mode & SPI_LOOP)
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530269 spicr |= SPICR_LOOP;
270
271 writel(spicr, &regs->spicr);
272 priv->mode = mode;
273
274 debug("xilinx_spi_set_mode: regs=%p, mode=%d\n", priv->regs,
275 priv->mode);
276
277 return 0;
278}
279
280static const struct dm_spi_ops xilinx_spi_ops = {
281 .claim_bus = xilinx_spi_claim_bus,
282 .release_bus = xilinx_spi_release_bus,
283 .xfer = xilinx_spi_xfer,
284 .set_speed = xilinx_spi_set_speed,
285 .set_mode = xilinx_spi_set_mode,
286};
287
288static const struct udevice_id xilinx_spi_ids[] = {
Michal Simek7465f312015-12-11 12:41:14 +0100289 { .compatible = "xlnx,xps-spi-2.00.a" },
290 { .compatible = "xlnx,xps-spi-2.00.b" },
Jagan Tekifdc2b3d2015-06-29 13:15:18 +0530291 { }
292};
293
294U_BOOT_DRIVER(xilinx_spi) = {
295 .name = "xilinx_spi",
296 .id = UCLASS_SPI,
297 .of_match = xilinx_spi_ids,
298 .ops = &xilinx_spi_ops,
299 .priv_auto_alloc_size = sizeof(struct xilinx_spi_priv),
300 .probe = xilinx_spi_probe,
301};