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 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #include <asm/arch/hardware.h>
32 #include "davinci_spi.h"
39 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
40 unsigned int max_hz, unsigned int mode)
42 struct davinci_spi_slave *ds;
44 if (!spi_cs_is_valid(bus, cs))
47 ds = spi_alloc_slave(struct davinci_spi_slave, bus, cs);
51 ds->regs = (struct davinci_spi_regs *)CONFIG_SYS_SPI_BASE;
57 void spi_free_slave(struct spi_slave *slave)
59 struct davinci_spi_slave *ds = to_davinci_spi(slave);
64 int spi_claim_bus(struct spi_slave *slave)
66 struct davinci_spi_slave *ds = to_davinci_spi(slave);
69 /* Enable the SPI hardware */
70 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
72 writel(SPIGCR0_SPIENA_MASK, &ds->regs->gcr0);
74 /* Set master mode, powered up and not activated */
75 writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1);
77 /* CS, CLK, SIMO and SOMI are functional pins */
78 writel((SPIPC0_EN0FUN_MASK | SPIPC0_CLKFUN_MASK |
79 SPIPC0_DOFUN_MASK | SPIPC0_DIFUN_MASK), &ds->regs->pc0);
82 scalar = ((CONFIG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF;
85 * Use following format:
86 * character length = 8,
87 * clock signal delayed by half clk cycle,
88 * clock low in idle state - Mode 0,
89 * MSB shifted out first
91 writel(8 | (scalar << SPIFMT_PRESCALE_SHIFT) |
92 (1 << SPIFMT_PHASE_SHIFT), &ds->regs->fmt0);
95 * Including a minor delay. No science here. Should be good even with
98 writel((50 << SPI_C2TDELAY_SHIFT) |
99 (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay);
101 /* default chip select register */
102 writel(SPIDEF_CSDEF0_MASK, &ds->regs->def);
105 writel(0, &ds->regs->int0);
106 writel(0, &ds->regs->lvl);
109 writel((readl(&ds->regs->gcr1) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
114 void spi_release_bus(struct spi_slave *slave)
116 struct davinci_spi_slave *ds = to_davinci_spi(slave);
118 /* Disable the SPI hardware */
119 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
123 * This functions needs to act like a macro to avoid pipeline reloads in the
124 * loops below. Use always_inline. This gains us about 160KiB/s and the bloat
125 * appears to be zero bytes (da830).
127 __attribute__((always_inline))
128 static inline u32 davinci_spi_xfer_data(struct davinci_spi_slave *ds, u32 data)
133 writel(data, &ds->regs->dat1);
135 /* wait for the data to clock in/out */
136 while ((buf_reg_val = readl(&ds->regs->buf)) & SPIBUF_RXEMPTY_MASK)
142 static int davinci_spi_read(struct spi_slave *slave, unsigned int len,
143 u8 *rxp, unsigned long flags)
145 struct davinci_spi_slave *ds = to_davinci_spi(slave);
146 unsigned int data1_reg_val;
148 /* enable CS hold, CS[n] and clear the data bits */
149 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
150 (slave->cs << SPIDAT1_CSNR_SHIFT));
152 /* wait till TXFULL is deasserted */
153 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
156 /* preload the TX buffer to avoid clock starvation */
157 writel(data1_reg_val, &ds->regs->dat1);
159 /* keep reading 1 byte until only 1 byte left */
161 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val);
163 /* clear CS hold when we reach the end */
164 if (flags & SPI_XFER_END)
165 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
167 /* read the last byte */
168 *rxp = davinci_spi_xfer_data(ds, data1_reg_val);
173 static int davinci_spi_write(struct spi_slave *slave, unsigned int len,
174 const u8 *txp, unsigned long flags)
176 struct davinci_spi_slave *ds = to_davinci_spi(slave);
177 unsigned int data1_reg_val;
179 /* enable CS hold and clear the data bits */
180 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
181 (slave->cs << SPIDAT1_CSNR_SHIFT));
183 /* wait till TXFULL is deasserted */
184 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
187 /* preload the TX buffer to avoid clock starvation */
189 writel(data1_reg_val | *txp++, &ds->regs->dat1);
193 /* keep writing 1 byte until only 1 byte left */
195 davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
197 /* clear CS hold when we reach the end */
198 if (flags & SPI_XFER_END)
199 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
201 /* write the last byte */
202 davinci_spi_xfer_data(ds, data1_reg_val | *txp);
207 #ifndef CONFIG_SPI_HALF_DUPLEX
208 static int davinci_spi_read_write(struct spi_slave *slave, unsigned int len,
209 u8 *rxp, const u8 *txp, unsigned long flags)
211 struct davinci_spi_slave *ds = to_davinci_spi(slave);
212 unsigned int data1_reg_val;
214 /* enable CS hold and clear the data bits */
215 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
216 (slave->cs << SPIDAT1_CSNR_SHIFT));
218 /* wait till TXFULL is deasserted */
219 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
222 /* keep reading and writing 1 byte until only 1 byte left */
224 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
226 /* clear CS hold when we reach the end */
227 if (flags & SPI_XFER_END)
228 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
230 /* read and write the last byte */
231 *rxp = davinci_spi_xfer_data(ds, data1_reg_val | *txp);
237 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
238 const void *dout, void *din, unsigned long flags)
243 /* Finish any previously submitted transfers */
247 * It's not clear how non-8-bit-aligned transfers are supposed to be
248 * represented as a stream of bytes...this is a limitation of
249 * the current SPI interface - here we terminate on receiving such a
253 /* Errors always terminate an ongoing transfer */
254 flags |= SPI_XFER_END;
261 return davinci_spi_read(slave, len, din, flags);
263 return davinci_spi_write(slave, len, dout, flags);
264 #ifndef CONFIG_SPI_HALF_DUPLEX
266 return davinci_spi_read_write(slave, len, din, dout, flags);
268 printf("SPI full duplex transaction requested with "
269 "CONFIG_SPI_HALF_DUPLEX defined.\n");
270 flags |= SPI_XFER_END;
274 if (flags & SPI_XFER_END) {
276 davinci_spi_write(slave, 1, &dummy, flags);
281 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
283 return bus == 0 && cs == 0;
286 void spi_cs_activate(struct spi_slave *slave)
291 void spi_cs_deactivate(struct spi_slave *slave)