2 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
4 * Driver for SPI controller on DaVinci. Based on atmel_spi.c
7 * Copyright (C) 2007 Atmel Corporation
9 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm/arch/hardware.h>
16 #include "davinci_spi.h"
23 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
24 unsigned int max_hz, unsigned int mode)
26 struct davinci_spi_slave *ds;
28 if (!spi_cs_is_valid(bus, cs))
31 ds = spi_alloc_slave(struct davinci_spi_slave, bus, cs);
37 ds->regs = (struct davinci_spi_regs *)SPI0_BASE;
39 #ifdef CONFIG_SYS_SPI1
41 ds->regs = (struct davinci_spi_regs *)SPI1_BASE;
44 #ifdef CONFIG_SYS_SPI2
46 ds->regs = (struct davinci_spi_regs *)SPI2_BASE;
49 default: /* Invalid bus number */
58 void spi_free_slave(struct spi_slave *slave)
60 struct davinci_spi_slave *ds = to_davinci_spi(slave);
65 int spi_claim_bus(struct spi_slave *slave)
67 struct davinci_spi_slave *ds = to_davinci_spi(slave);
70 /* Enable the SPI hardware */
71 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
73 writel(SPIGCR0_SPIENA_MASK, &ds->regs->gcr0);
75 /* Set master mode, powered up and not activated */
76 writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1);
78 /* CS, CLK, SIMO and SOMI are functional pins */
79 writel(((1 << slave->cs) | SPIPC0_CLKFUN_MASK |
80 SPIPC0_DOFUN_MASK | SPIPC0_DIFUN_MASK), &ds->regs->pc0);
83 scalar = ((CONFIG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF;
86 * Use following format:
87 * character length = 8,
88 * clock signal delayed by half clk cycle,
89 * clock low in idle state - Mode 0,
90 * MSB shifted out first
92 writel(8 | (scalar << SPIFMT_PRESCALE_SHIFT) |
93 (1 << SPIFMT_PHASE_SHIFT), &ds->regs->fmt0);
96 * Including a minor delay. No science here. Should be good even with
99 writel((50 << SPI_C2TDELAY_SHIFT) |
100 (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay);
102 /* default chip select register */
103 writel(SPIDEF_CSDEF0_MASK, &ds->regs->def);
106 writel(0, &ds->regs->int0);
107 writel(0, &ds->regs->lvl);
110 writel((readl(&ds->regs->gcr1) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
115 void spi_release_bus(struct spi_slave *slave)
117 struct davinci_spi_slave *ds = to_davinci_spi(slave);
119 /* Disable the SPI hardware */
120 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
124 * This functions needs to act like a macro to avoid pipeline reloads in the
125 * loops below. Use always_inline. This gains us about 160KiB/s and the bloat
126 * appears to be zero bytes (da830).
128 __attribute__((always_inline))
129 static inline u32 davinci_spi_xfer_data(struct davinci_spi_slave *ds, u32 data)
134 writel(data, &ds->regs->dat1);
136 /* wait for the data to clock in/out */
137 while ((buf_reg_val = readl(&ds->regs->buf)) & SPIBUF_RXEMPTY_MASK)
143 static int davinci_spi_read(struct spi_slave *slave, unsigned int len,
144 u8 *rxp, unsigned long flags)
146 struct davinci_spi_slave *ds = to_davinci_spi(slave);
147 unsigned int data1_reg_val;
149 /* enable CS hold, CS[n] and clear the data bits */
150 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
151 (slave->cs << SPIDAT1_CSNR_SHIFT));
153 /* wait till TXFULL is deasserted */
154 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
157 /* preload the TX buffer to avoid clock starvation */
158 writel(data1_reg_val, &ds->regs->dat1);
160 /* keep reading 1 byte until only 1 byte left */
162 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val);
164 /* clear CS hold when we reach the end */
165 if (flags & SPI_XFER_END)
166 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
168 /* read the last byte */
169 *rxp = davinci_spi_xfer_data(ds, data1_reg_val);
174 static int davinci_spi_write(struct spi_slave *slave, unsigned int len,
175 const u8 *txp, unsigned long flags)
177 struct davinci_spi_slave *ds = to_davinci_spi(slave);
178 unsigned int data1_reg_val;
180 /* enable CS hold and clear the data bits */
181 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
182 (slave->cs << SPIDAT1_CSNR_SHIFT));
184 /* wait till TXFULL is deasserted */
185 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
188 /* preload the TX buffer to avoid clock starvation */
190 writel(data1_reg_val | *txp++, &ds->regs->dat1);
194 /* keep writing 1 byte until only 1 byte left */
196 davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
198 /* clear CS hold when we reach the end */
199 if (flags & SPI_XFER_END)
200 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
202 /* write the last byte */
203 davinci_spi_xfer_data(ds, data1_reg_val | *txp);
208 #ifndef CONFIG_SPI_HALF_DUPLEX
209 static int davinci_spi_read_write(struct spi_slave *slave, unsigned int len,
210 u8 *rxp, const u8 *txp, unsigned long flags)
212 struct davinci_spi_slave *ds = to_davinci_spi(slave);
213 unsigned int data1_reg_val;
215 /* enable CS hold and clear the data bits */
216 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
217 (slave->cs << SPIDAT1_CSNR_SHIFT));
219 /* wait till TXFULL is deasserted */
220 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
223 /* keep reading and writing 1 byte until only 1 byte left */
225 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
227 /* clear CS hold when we reach the end */
228 if (flags & SPI_XFER_END)
229 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
231 /* read and write the last byte */
232 *rxp = davinci_spi_xfer_data(ds, data1_reg_val | *txp);
238 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
239 const void *dout, void *din, unsigned long flags)
244 /* Finish any previously submitted transfers */
248 * It's not clear how non-8-bit-aligned transfers are supposed to be
249 * represented as a stream of bytes...this is a limitation of
250 * the current SPI interface - here we terminate on receiving such a
254 /* Errors always terminate an ongoing transfer */
255 flags |= SPI_XFER_END;
262 return davinci_spi_read(slave, len, din, flags);
264 return davinci_spi_write(slave, len, dout, flags);
265 #ifndef CONFIG_SPI_HALF_DUPLEX
267 return davinci_spi_read_write(slave, len, din, dout, flags);
269 printf("SPI full duplex transaction requested with "
270 "CONFIG_SPI_HALF_DUPLEX defined.\n");
271 flags |= SPI_XFER_END;
275 if (flags & SPI_XFER_END) {
277 davinci_spi_write(slave, 1, &dummy, flags);
282 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
288 if (cs < SPI0_NUM_CS)
291 #ifdef CONFIG_SYS_SPI1
293 if (cs < SPI1_NUM_CS)
297 #ifdef CONFIG_SYS_SPI2
299 if (cs < SPI2_NUM_CS)
304 /* Invalid bus number. Do nothing */
310 void spi_cs_activate(struct spi_slave *slave)
315 void spi_cs_deactivate(struct spi_slave *slave)