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
);
35 ds
->regs
= (struct davinci_spi_regs
*)CONFIG_SYS_SPI_BASE
;
41 void spi_free_slave(struct spi_slave
*slave
)
43 struct davinci_spi_slave
*ds
= to_davinci_spi(slave
);
48 int spi_claim_bus(struct spi_slave
*slave
)
50 struct davinci_spi_slave
*ds
= to_davinci_spi(slave
);
53 /* Enable the SPI hardware */
54 writel(SPIGCR0_SPIRST_MASK
, &ds
->regs
->gcr0
);
56 writel(SPIGCR0_SPIENA_MASK
, &ds
->regs
->gcr0
);
58 /* Set master mode, powered up and not activated */
59 writel(SPIGCR1_MASTER_MASK
| SPIGCR1_CLKMOD_MASK
, &ds
->regs
->gcr1
);
61 /* CS, CLK, SIMO and SOMI are functional pins */
62 writel((SPIPC0_EN0FUN_MASK
| SPIPC0_CLKFUN_MASK
|
63 SPIPC0_DOFUN_MASK
| SPIPC0_DIFUN_MASK
), &ds
->regs
->pc0
);
66 scalar
= ((CONFIG_SYS_SPI_CLK
/ ds
->freq
) - 1) & 0xFF;
69 * Use following format:
70 * character length = 8,
71 * clock signal delayed by half clk cycle,
72 * clock low in idle state - Mode 0,
73 * MSB shifted out first
75 writel(8 | (scalar
<< SPIFMT_PRESCALE_SHIFT
) |
76 (1 << SPIFMT_PHASE_SHIFT
), &ds
->regs
->fmt0
);
79 * Including a minor delay. No science here. Should be good even with
82 writel((50 << SPI_C2TDELAY_SHIFT
) |
83 (50 << SPI_T2CDELAY_SHIFT
), &ds
->regs
->delay
);
85 /* default chip select register */
86 writel(SPIDEF_CSDEF0_MASK
, &ds
->regs
->def
);
89 writel(0, &ds
->regs
->int0
);
90 writel(0, &ds
->regs
->lvl
);
93 writel((readl(&ds
->regs
->gcr1
) | SPIGCR1_SPIENA_MASK
), &ds
->regs
->gcr1
);
98 void spi_release_bus(struct spi_slave
*slave
)
100 struct davinci_spi_slave
*ds
= to_davinci_spi(slave
);
102 /* Disable the SPI hardware */
103 writel(SPIGCR0_SPIRST_MASK
, &ds
->regs
->gcr0
);
107 * This functions needs to act like a macro to avoid pipeline reloads in the
108 * loops below. Use always_inline. This gains us about 160KiB/s and the bloat
109 * appears to be zero bytes (da830).
111 __attribute__((always_inline
))
112 static inline u32
davinci_spi_xfer_data(struct davinci_spi_slave
*ds
, u32 data
)
117 writel(data
, &ds
->regs
->dat1
);
119 /* wait for the data to clock in/out */
120 while ((buf_reg_val
= readl(&ds
->regs
->buf
)) & SPIBUF_RXEMPTY_MASK
)
126 static int davinci_spi_read(struct spi_slave
*slave
, unsigned int len
,
127 u8
*rxp
, unsigned long flags
)
129 struct davinci_spi_slave
*ds
= to_davinci_spi(slave
);
130 unsigned int data1_reg_val
;
132 /* enable CS hold, CS[n] and clear the data bits */
133 data1_reg_val
= ((1 << SPIDAT1_CSHOLD_SHIFT
) |
134 (slave
->cs
<< SPIDAT1_CSNR_SHIFT
));
136 /* wait till TXFULL is deasserted */
137 while (readl(&ds
->regs
->buf
) & SPIBUF_TXFULL_MASK
)
140 /* preload the TX buffer to avoid clock starvation */
141 writel(data1_reg_val
, &ds
->regs
->dat1
);
143 /* keep reading 1 byte until only 1 byte left */
145 *rxp
++ = davinci_spi_xfer_data(ds
, data1_reg_val
);
147 /* clear CS hold when we reach the end */
148 if (flags
& SPI_XFER_END
)
149 data1_reg_val
&= ~(1 << SPIDAT1_CSHOLD_SHIFT
);
151 /* read the last byte */
152 *rxp
= davinci_spi_xfer_data(ds
, data1_reg_val
);
157 static int davinci_spi_write(struct spi_slave
*slave
, unsigned int len
,
158 const u8
*txp
, unsigned long flags
)
160 struct davinci_spi_slave
*ds
= to_davinci_spi(slave
);
161 unsigned int data1_reg_val
;
163 /* enable CS hold and clear the data bits */
164 data1_reg_val
= ((1 << SPIDAT1_CSHOLD_SHIFT
) |
165 (slave
->cs
<< SPIDAT1_CSNR_SHIFT
));
167 /* wait till TXFULL is deasserted */
168 while (readl(&ds
->regs
->buf
) & SPIBUF_TXFULL_MASK
)
171 /* preload the TX buffer to avoid clock starvation */
173 writel(data1_reg_val
| *txp
++, &ds
->regs
->dat1
);
177 /* keep writing 1 byte until only 1 byte left */
179 davinci_spi_xfer_data(ds
, data1_reg_val
| *txp
++);
181 /* clear CS hold when we reach the end */
182 if (flags
& SPI_XFER_END
)
183 data1_reg_val
&= ~(1 << SPIDAT1_CSHOLD_SHIFT
);
185 /* write the last byte */
186 davinci_spi_xfer_data(ds
, data1_reg_val
| *txp
);
191 #ifndef CONFIG_SPI_HALF_DUPLEX
192 static int davinci_spi_read_write(struct spi_slave
*slave
, unsigned int len
,
193 u8
*rxp
, const u8
*txp
, unsigned long flags
)
195 struct davinci_spi_slave
*ds
= to_davinci_spi(slave
);
196 unsigned int data1_reg_val
;
198 /* enable CS hold and clear the data bits */
199 data1_reg_val
= ((1 << SPIDAT1_CSHOLD_SHIFT
) |
200 (slave
->cs
<< SPIDAT1_CSNR_SHIFT
));
202 /* wait till TXFULL is deasserted */
203 while (readl(&ds
->regs
->buf
) & SPIBUF_TXFULL_MASK
)
206 /* keep reading and writing 1 byte until only 1 byte left */
208 *rxp
++ = davinci_spi_xfer_data(ds
, data1_reg_val
| *txp
++);
210 /* clear CS hold when we reach the end */
211 if (flags
& SPI_XFER_END
)
212 data1_reg_val
&= ~(1 << SPIDAT1_CSHOLD_SHIFT
);
214 /* read and write the last byte */
215 *rxp
= davinci_spi_xfer_data(ds
, data1_reg_val
| *txp
);
221 int spi_xfer(struct spi_slave
*slave
, unsigned int bitlen
,
222 const void *dout
, void *din
, unsigned long flags
)
227 /* Finish any previously submitted transfers */
231 * It's not clear how non-8-bit-aligned transfers are supposed to be
232 * represented as a stream of bytes...this is a limitation of
233 * the current SPI interface - here we terminate on receiving such a
237 /* Errors always terminate an ongoing transfer */
238 flags
|= SPI_XFER_END
;
245 return davinci_spi_read(slave
, len
, din
, flags
);
247 return davinci_spi_write(slave
, len
, dout
, flags
);
248 #ifndef CONFIG_SPI_HALF_DUPLEX
250 return davinci_spi_read_write(slave
, len
, din
, dout
, flags
);
252 printf("SPI full duplex transaction requested with "
253 "CONFIG_SPI_HALF_DUPLEX defined.\n");
254 flags
|= SPI_XFER_END
;
258 if (flags
& SPI_XFER_END
) {
260 davinci_spi_write(slave
, 1, &dummy
, flags
);
265 int spi_cs_is_valid(unsigned int bus
, unsigned int cs
)
267 return bus
== 0 && cs
== 0;
270 void spi_cs_activate(struct spi_slave
*slave
)
275 void spi_cs_deactivate(struct spi_slave
*slave
)