]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/spi/davinci_spi.c
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
);
40 ds
->regs
= (struct davinci_spi_regs
*)SPI0_BASE
;
42 #ifdef CONFIG_SYS_SPI1
44 ds
->regs
= (struct davinci_spi_regs
*)SPI0_BASE
;
47 #ifdef CONFIG_SYS_SPI2
49 ds
->regs
= (struct davinci_spi_regs
*)SPI2_BASE
;
52 default: /* Invalid bus number */
61 void spi_free_slave(struct spi_slave
*slave
)
63 struct davinci_spi_slave
*ds
= to_davinci_spi(slave
);
68 int spi_claim_bus(struct spi_slave
*slave
)
70 struct davinci_spi_slave
*ds
= to_davinci_spi(slave
);
73 /* Enable the SPI hardware */
74 writel(SPIGCR0_SPIRST_MASK
, &ds
->regs
->gcr0
);
76 writel(SPIGCR0_SPIENA_MASK
, &ds
->regs
->gcr0
);
78 /* Set master mode, powered up and not activated */
79 writel(SPIGCR1_MASTER_MASK
| SPIGCR1_CLKMOD_MASK
, &ds
->regs
->gcr1
);
81 /* CS, CLK, SIMO and SOMI are functional pins */
82 writel(((1 << slave
->cs
) | SPIPC0_CLKFUN_MASK
|
83 SPIPC0_DOFUN_MASK
| SPIPC0_DIFUN_MASK
), &ds
->regs
->pc0
);
86 scalar
= ((CONFIG_SYS_SPI_CLK
/ ds
->freq
) - 1) & 0xFF;
89 * Use following format:
90 * character length = 8,
91 * clock signal delayed by half clk cycle,
92 * clock low in idle state - Mode 0,
93 * MSB shifted out first
95 writel(8 | (scalar
<< SPIFMT_PRESCALE_SHIFT
) |
96 (1 << SPIFMT_PHASE_SHIFT
), &ds
->regs
->fmt0
);
99 * Including a minor delay. No science here. Should be good even with
102 writel((50 << SPI_C2TDELAY_SHIFT
) |
103 (50 << SPI_T2CDELAY_SHIFT
), &ds
->regs
->delay
);
105 /* default chip select register */
106 writel(SPIDEF_CSDEF0_MASK
, &ds
->regs
->def
);
109 writel(0, &ds
->regs
->int0
);
110 writel(0, &ds
->regs
->lvl
);
113 writel((readl(&ds
->regs
->gcr1
) | SPIGCR1_SPIENA_MASK
), &ds
->regs
->gcr1
);
118 void spi_release_bus(struct spi_slave
*slave
)
120 struct davinci_spi_slave
*ds
= to_davinci_spi(slave
);
122 /* Disable the SPI hardware */
123 writel(SPIGCR0_SPIRST_MASK
, &ds
->regs
->gcr0
);
127 * This functions needs to act like a macro to avoid pipeline reloads in the
128 * loops below. Use always_inline. This gains us about 160KiB/s and the bloat
129 * appears to be zero bytes (da830).
131 __attribute__((always_inline
))
132 static inline u32
davinci_spi_xfer_data(struct davinci_spi_slave
*ds
, u32 data
)
137 writel(data
, &ds
->regs
->dat1
);
139 /* wait for the data to clock in/out */
140 while ((buf_reg_val
= readl(&ds
->regs
->buf
)) & SPIBUF_RXEMPTY_MASK
)
146 static int davinci_spi_read(struct spi_slave
*slave
, unsigned int len
,
147 u8
*rxp
, unsigned long flags
)
149 struct davinci_spi_slave
*ds
= to_davinci_spi(slave
);
150 unsigned int data1_reg_val
;
152 /* enable CS hold, CS[n] and clear the data bits */
153 data1_reg_val
= ((1 << SPIDAT1_CSHOLD_SHIFT
) |
154 (slave
->cs
<< SPIDAT1_CSNR_SHIFT
));
156 /* wait till TXFULL is deasserted */
157 while (readl(&ds
->regs
->buf
) & SPIBUF_TXFULL_MASK
)
160 /* preload the TX buffer to avoid clock starvation */
161 writel(data1_reg_val
, &ds
->regs
->dat1
);
163 /* keep reading 1 byte until only 1 byte left */
165 *rxp
++ = davinci_spi_xfer_data(ds
, data1_reg_val
);
167 /* clear CS hold when we reach the end */
168 if (flags
& SPI_XFER_END
)
169 data1_reg_val
&= ~(1 << SPIDAT1_CSHOLD_SHIFT
);
171 /* read the last byte */
172 *rxp
= davinci_spi_xfer_data(ds
, data1_reg_val
);
177 static int davinci_spi_write(struct spi_slave
*slave
, unsigned int len
,
178 const u8
*txp
, unsigned long flags
)
180 struct davinci_spi_slave
*ds
= to_davinci_spi(slave
);
181 unsigned int data1_reg_val
;
183 /* enable CS hold and clear the data bits */
184 data1_reg_val
= ((1 << SPIDAT1_CSHOLD_SHIFT
) |
185 (slave
->cs
<< SPIDAT1_CSNR_SHIFT
));
187 /* wait till TXFULL is deasserted */
188 while (readl(&ds
->regs
->buf
) & SPIBUF_TXFULL_MASK
)
191 /* preload the TX buffer to avoid clock starvation */
193 writel(data1_reg_val
| *txp
++, &ds
->regs
->dat1
);
197 /* keep writing 1 byte until only 1 byte left */
199 davinci_spi_xfer_data(ds
, data1_reg_val
| *txp
++);
201 /* clear CS hold when we reach the end */
202 if (flags
& SPI_XFER_END
)
203 data1_reg_val
&= ~(1 << SPIDAT1_CSHOLD_SHIFT
);
205 /* write the last byte */
206 davinci_spi_xfer_data(ds
, data1_reg_val
| *txp
);
211 #ifndef CONFIG_SPI_HALF_DUPLEX
212 static int davinci_spi_read_write(struct spi_slave
*slave
, unsigned int len
,
213 u8
*rxp
, const u8
*txp
, unsigned long flags
)
215 struct davinci_spi_slave
*ds
= to_davinci_spi(slave
);
216 unsigned int data1_reg_val
;
218 /* enable CS hold and clear the data bits */
219 data1_reg_val
= ((1 << SPIDAT1_CSHOLD_SHIFT
) |
220 (slave
->cs
<< SPIDAT1_CSNR_SHIFT
));
222 /* wait till TXFULL is deasserted */
223 while (readl(&ds
->regs
->buf
) & SPIBUF_TXFULL_MASK
)
226 /* keep reading and writing 1 byte until only 1 byte left */
228 *rxp
++ = davinci_spi_xfer_data(ds
, data1_reg_val
| *txp
++);
230 /* clear CS hold when we reach the end */
231 if (flags
& SPI_XFER_END
)
232 data1_reg_val
&= ~(1 << SPIDAT1_CSHOLD_SHIFT
);
234 /* read and write the last byte */
235 *rxp
= davinci_spi_xfer_data(ds
, data1_reg_val
| *txp
);
241 int spi_xfer(struct spi_slave
*slave
, unsigned int bitlen
,
242 const void *dout
, void *din
, unsigned long flags
)
247 /* Finish any previously submitted transfers */
251 * It's not clear how non-8-bit-aligned transfers are supposed to be
252 * represented as a stream of bytes...this is a limitation of
253 * the current SPI interface - here we terminate on receiving such a
257 /* Errors always terminate an ongoing transfer */
258 flags
|= SPI_XFER_END
;
265 return davinci_spi_read(slave
, len
, din
, flags
);
267 return davinci_spi_write(slave
, len
, dout
, flags
);
268 #ifndef CONFIG_SPI_HALF_DUPLEX
270 return davinci_spi_read_write(slave
, len
, din
, dout
, flags
);
272 printf("SPI full duplex transaction requested with "
273 "CONFIG_SPI_HALF_DUPLEX defined.\n");
274 flags
|= SPI_XFER_END
;
278 if (flags
& SPI_XFER_END
) {
280 davinci_spi_write(slave
, 1, &dummy
, flags
);
285 int spi_cs_is_valid(unsigned int bus
, unsigned int cs
)
291 if (cs
< SPI0_NUM_CS
)
294 #ifdef CONFIG_SYS_SPI1
296 if (cs
< SPI1_NUM_CS
)
300 #ifdef CONFIG_SYS_SPI2
302 if (cs
< SPI2_NUM_CS
)
307 /* Invalid bus number. Do nothing */
313 void spi_cs_activate(struct spi_slave
*slave
)
318 void spi_cs_deactivate(struct spi_slave
*slave
)