4 * supports 8 bit SPI transfers only, with or w/o FIFO
6 * based on bfin_spi.c, by way of altera_spi.c
7 * Copyright (c) 2005-2008 Analog Devices Inc.
8 * Copyright (c) 2010 Thomas Chou <thomas@wytron.com.tw>
9 * Copyright (c) 2010 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
10 * Copyright (c) 2012 Stephan Linz <linz@li-pro.net>
12 * Licensed under the GPL-2 or later.
14 * [0]: http://www.xilinx.com/support/documentation
16 * [S]: [0]/ip_documentation/xps_spi.pdf
17 * [0]/ip_documentation/axi_spi_ds742.pdf
24 #include "xilinx_spi.h"
26 #ifndef CONFIG_SYS_XILINX_SPI_LIST
27 #define CONFIG_SYS_XILINX_SPI_LIST { CONFIG_SYS_SPI_BASE }
30 #ifndef CONFIG_XILINX_SPI_IDLE_VAL
31 #define CONFIG_XILINX_SPI_IDLE_VAL 0xff
34 #define XILSPI_SPICR_DFLT_ON (SPICR_MANUAL_SS | \
38 #define XILSPI_SPICR_DFLT_OFF (SPICR_MASTER_INHIBIT | \
41 #define XILSPI_MAX_XFER_BITS 8
43 static unsigned long xilinx_spi_base_list[] = CONFIG_SYS_XILINX_SPI_LIST;
46 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
48 return bus < ARRAY_SIZE(xilinx_spi_base_list) && cs < 32;
52 void spi_cs_activate(struct spi_slave *slave)
54 struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
56 writel(SPISSR_ACT(slave->cs), &xilspi->regs->spissr);
60 void spi_cs_deactivate(struct spi_slave *slave)
62 struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
64 writel(SPISSR_OFF, &xilspi->regs->spissr);
72 void spi_set_speed(struct spi_slave *slave, uint hz)
74 /* xilinx spi core does not support programmable speed */
77 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
78 unsigned int max_hz, unsigned int mode)
80 struct xilinx_spi_slave *xilspi;
82 if (!spi_cs_is_valid(bus, cs)) {
83 printf("XILSPI error: %s: unsupported bus %d / cs %d\n",
88 xilspi = malloc(sizeof(*xilspi));
90 printf("XILSPI error: %s: malloc of SPI structure failed\n",
94 xilspi->slave.bus = bus;
95 xilspi->slave.cs = cs;
96 xilspi->regs = (struct xilinx_spi_reg *)xilinx_spi_base_list[bus];
97 xilspi->freq = max_hz;
99 debug("%s: bus:%i cs:%i base:%p mode:%x max_hz:%d\n", __func__,
100 bus, cs, xilspi->regs, xilspi->mode, xilspi->freq);
102 writel(SPISSR_RESET_VALUE, &xilspi->regs->srr);
104 return &xilspi->slave;
107 void spi_free_slave(struct spi_slave *slave)
109 struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
114 int spi_claim_bus(struct spi_slave *slave)
116 struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
119 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
120 writel(SPISSR_OFF, &xilspi->regs->spissr);
122 spicr = XILSPI_SPICR_DFLT_ON;
123 if (xilspi->mode & SPI_LSB_FIRST)
124 spicr |= SPICR_LSB_FIRST;
125 if (xilspi->mode & SPI_CPHA)
127 if (xilspi->mode & SPI_CPOL)
129 if (xilspi->mode & SPI_LOOP)
132 writel(spicr, &xilspi->regs->spicr);
136 void spi_release_bus(struct spi_slave *slave)
138 struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
140 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
141 writel(SPISSR_OFF, &xilspi->regs->spissr);
142 writel(XILSPI_SPICR_DFLT_OFF, &xilspi->regs->spicr);
145 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
146 void *din, unsigned long flags)
148 struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
149 /* assume spi core configured to do 8 bit transfers */
150 unsigned int bytes = bitlen / XILSPI_MAX_XFER_BITS;
151 const unsigned char *txp = dout;
152 unsigned char *rxp = din;
153 unsigned rxecount = 17; /* max. 16 elements in FIFO, leftover 1 */
155 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
156 slave->bus, slave->cs, bitlen, bytes, flags);
160 if (bitlen % XILSPI_MAX_XFER_BITS) {
161 printf("XILSPI warning: %s: Not a multiple of %d bits\n",
162 __func__, XILSPI_MAX_XFER_BITS);
163 flags |= SPI_XFER_END;
167 /* empty read buffer */
168 while (rxecount && !(readl(&xilspi->regs->spisr) & SPISR_RX_EMPTY)) {
169 readl(&xilspi->regs->spidrr);
174 printf("XILSPI error: %s: Rx buffer not empty\n", __func__);
178 if (flags & SPI_XFER_BEGIN)
179 spi_cs_activate(slave);
182 unsigned timeout = /* at least 1usec or greater, leftover 1 */
183 xilspi->freq > XILSPI_MAX_XFER_BITS * 1000000 ? 2 :
184 (XILSPI_MAX_XFER_BITS * 1000000 / xilspi->freq) + 1;
186 /* get Tx element from data out buffer and count up */
187 unsigned char d = txp ? *txp++ : CONFIG_XILINX_SPI_IDLE_VAL;
188 debug("%s: tx:%x ", __func__, d);
190 /* write out and wait for processing (receive data) */
191 writel(d & SPIDTR_8BIT_MASK, &xilspi->regs->spidtr);
192 while (timeout && readl(&xilspi->regs->spisr)
199 printf("XILSPI error: %s: Xfer timeout\n", __func__);
203 /* read Rx element and push into data in buffer */
204 d = readl(&xilspi->regs->spidrr) & SPIDRR_8BIT_MASK;
211 if (flags & SPI_XFER_END)
212 spi_cs_deactivate(slave);