2 * Driver for Blackfin On-Chip SPI device
4 * Copyright (c) 2005-2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
15 #include <asm/blackfin.h>
18 #include <asm/portmux.h>
19 #include <asm/mach-common/bits/spi.h>
21 struct bfin_spi_slave {
22 struct spi_slave slave;
27 #define MAKE_SPI_FUNC(mmr, off) \
28 static inline void write_##mmr(struct bfin_spi_slave *bss, u16 val) { bfin_write16(bss->mmr_base + off, val); } \
29 static inline u16 read_##mmr(struct bfin_spi_slave *bss) { return bfin_read16(bss->mmr_base + off); }
30 MAKE_SPI_FUNC(SPI_CTL, 0x00)
31 MAKE_SPI_FUNC(SPI_FLG, 0x04)
32 MAKE_SPI_FUNC(SPI_STAT, 0x08)
33 MAKE_SPI_FUNC(SPI_TDBR, 0x0c)
34 MAKE_SPI_FUNC(SPI_RDBR, 0x10)
35 MAKE_SPI_FUNC(SPI_BAUD, 0x14)
37 #define to_bfin_spi_slave(s) container_of(s, struct bfin_spi_slave, slave)
39 #define gpio_cs(cs) ((cs) - MAX_CTRL_CS)
40 #ifdef CONFIG_BFIN_SPI_GPIO_CS
41 # define is_gpio_cs(cs) ((cs) > MAX_CTRL_CS)
43 # define is_gpio_cs(cs) 0
46 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
49 return gpio_is_valid(gpio_cs(cs));
51 return (cs >= 1 && cs <= MAX_CTRL_CS);
54 void spi_cs_activate(struct spi_slave *slave)
56 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
58 if (is_gpio_cs(slave->cs)) {
59 unsigned int cs = gpio_cs(slave->cs);
60 gpio_set_value(cs, bss->flg);
61 debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
65 ~((!bss->flg << 8) << slave->cs)) |
67 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
73 void spi_cs_deactivate(struct spi_slave *slave)
75 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
77 if (is_gpio_cs(slave->cs)) {
78 unsigned int cs = gpio_cs(slave->cs);
79 gpio_set_value(cs, !bss->flg);
80 debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
84 /* make sure we force the cs to deassert rather than let the
85 * pin float back up. otherwise, exact timings may not be
86 * met some of the time leading to random behavior (ugh).
88 flg = read_SPI_FLG(bss) | ((!bss->flg << 8) << slave->cs);
89 write_SPI_FLG(bss, flg);
91 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
93 flg &= ~(1 << slave->cs);
94 write_SPI_FLG(bss, flg);
95 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
106 # define SPI0_CTL SPI_CTL
109 #define SPI_PINS(n) \
110 [n] = { 0, P_SPI##n##_SCK, P_SPI##n##_MISO, P_SPI##n##_MOSI, 0 }
111 static unsigned short pins[][5] = {
123 #define SPI_CS_PINS(n) \
125 P_SPI##n##_SSEL1, P_SPI##n##_SSEL2, P_SPI##n##_SSEL3, \
126 P_SPI##n##_SSEL4, P_SPI##n##_SSEL5, P_SPI##n##_SSEL6, \
129 static const unsigned short cs_pins[][7] = {
141 void spi_set_speed(struct spi_slave *slave, uint hz)
143 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
148 baud = sclk / (2 * hz);
149 /* baud should be rounded up */
154 else if (baud > (u16)-1)
159 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
160 unsigned int max_hz, unsigned int mode)
162 struct bfin_spi_slave *bss;
165 if (!spi_cs_is_valid(bus, cs))
168 if (bus >= ARRAY_SIZE(pins) || pins[bus] == NULL) {
169 debug("%s: invalid bus %u\n", __func__, bus);
174 case 0: mmr_base = SPI0_CTL; break;
177 case 1: mmr_base = SPI1_CTL; break;
180 case 2: mmr_base = SPI2_CTL; break;
182 default: return NULL;
185 bss = spi_alloc_slave(struct bfin_spi_slave, bus, cs);
189 bss->mmr_base = (void *)mmr_base;
190 bss->ctl = SPE | MSTR | TDBR_CORE;
191 if (mode & SPI_CPHA) bss->ctl |= CPHA;
192 if (mode & SPI_CPOL) bss->ctl |= CPOL;
193 if (mode & SPI_LSB_FIRST) bss->ctl |= LSBF;
194 bss->flg = mode & SPI_CS_HIGH ? 1 : 0;
195 spi_set_speed(&bss->slave, max_hz);
197 debug("%s: bus:%i cs:%i mmr:%x ctl:%x baud:%i flg:%i\n", __func__,
198 bus, cs, mmr_base, bss->ctl, bss->baud, bss->flg);
203 void spi_free_slave(struct spi_slave *slave)
205 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
209 int spi_claim_bus(struct spi_slave *slave)
211 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
213 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
215 if (is_gpio_cs(slave->cs)) {
216 unsigned int cs = gpio_cs(slave->cs);
217 gpio_request(cs, "bfin-spi");
218 gpio_direction_output(cs, !bss->flg);
219 pins[slave->bus][0] = P_DONTCARE;
221 pins[slave->bus][0] = cs_pins[slave->bus][slave->cs - 1];
222 peripheral_request_list(pins[slave->bus], "bfin-spi");
224 write_SPI_CTL(bss, bss->ctl);
225 write_SPI_BAUD(bss, bss->baud);
231 void spi_release_bus(struct spi_slave *slave)
233 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
235 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
237 peripheral_free_list(pins[slave->bus]);
238 if (is_gpio_cs(slave->cs))
239 gpio_free(gpio_cs(slave->cs));
241 write_SPI_CTL(bss, 0);
246 # define SPI_DMA_BASE DMA4_NEXT_DESC_PTR
247 #elif defined(__ADSPBF533__) || defined(__ADSPBF532__) || defined(__ADSPBF531__) || \
248 defined(__ADSPBF538__) || defined(__ADSPBF539__)
249 # define SPI_DMA_BASE DMA5_NEXT_DESC_PTR
250 #elif defined(__ADSPBF561__)
251 # define SPI_DMA_BASE DMA2_4_NEXT_DESC_PTR
252 #elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__) || \
253 defined(__ADSPBF52x__) || defined(__ADSPBF51x__)
254 # define SPI_DMA_BASE DMA7_NEXT_DESC_PTR
255 # elif defined(__ADSPBF50x__)
256 # define SPI_DMA_BASE DMA6_NEXT_DESC_PTR
258 # error "Please provide SPI DMA channel defines"
260 static volatile struct dma_register *dma = (void *)SPI_DMA_BASE;
262 #ifndef CONFIG_BFIN_SPI_IDLE_VAL
263 # define CONFIG_BFIN_SPI_IDLE_VAL 0xff
266 #ifdef CONFIG_BFIN_SPI_NO_DMA
272 static int spi_dma_xfer(struct bfin_spi_slave *bss, const u8 *tx, u8 *rx,
276 u16 ndsize, spi_config, dma_config;
277 struct dmasg dmasg[2];
281 debug("%s: doing half duplex TX\n", __func__);
283 spi_config = TDBR_DMA;
286 debug("%s: doing half duplex RX\n", __func__);
288 spi_config = RDBR_DMA;
292 dmasg[0].start_addr = (unsigned long)buf;
293 dmasg[0].x_modify = 1;
294 dma_config |= WDSIZE_8 | DMAEN;
295 if (bytes <= 65536) {
296 blackfin_dcache_flush_invalidate_range(buf, buf + bytes);
298 dmasg[0].cfg = NDSIZE_0 | dma_config | FLOW_STOP | DI_EN;
299 dmasg[0].x_count = bytes;
301 blackfin_dcache_flush_invalidate_range(buf, buf + 65536 - 1);
303 dmasg[0].cfg = NDSIZE_5 | dma_config | FLOW_ARRAY | DMA2D;
304 dmasg[0].x_count = 0; /* 2^16 */
305 dmasg[0].y_count = bytes >> 16; /* count / 2^16 */
306 dmasg[0].y_modify = 1;
307 dmasg[1].start_addr = (unsigned long)(buf + (bytes & ~0xFFFF));
308 dmasg[1].cfg = NDSIZE_0 | dma_config | FLOW_STOP | DI_EN;
309 dmasg[1].x_count = bytes & 0xFFFF; /* count % 2^16 */
310 dmasg[1].x_modify = 1;
314 dma->irq_status = DMA_DONE | DMA_ERR;
315 dma->curr_desc_ptr = dmasg;
316 write_SPI_CTL(bss, (bss->ctl & ~TDBR_CORE));
317 write_SPI_STAT(bss, -1);
320 write_SPI_TDBR(bss, CONFIG_BFIN_SPI_IDLE_VAL);
321 dma->cfg = ndsize | FLOW_ARRAY | DMAEN;
322 write_SPI_CTL(bss, (bss->ctl & ~TDBR_CORE) | spi_config);
326 * We already invalidated the first 64k,
327 * now while we just wait invalidate the remaining part.
328 * Its not likely that the DMA is going to overtake
331 blackfin_dcache_flush_invalidate_range(buf + 65536, buf + bytes);
333 while (!(dma->irq_status & DMA_DONE))
341 write_SPI_CTL(bss, bss->ctl);
345 static int spi_pio_xfer(struct bfin_spi_slave *bss, const u8 *tx, u8 *rx,
348 /* todo: take advantage of hardware fifos */
350 u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
351 debug("%s: tx:%x ", __func__, value);
352 write_SPI_TDBR(bss, value);
354 while ((read_SPI_STAT(bss) & TXS))
357 while (!(read_SPI_STAT(bss) & SPIF))
360 while (!(read_SPI_STAT(bss) & RXS))
363 value = read_SPI_RDBR(bss);
366 debug("rx:%x\n", value);
372 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
373 void *din, unsigned long flags)
375 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
378 uint bytes = bitlen / 8;
381 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
382 slave->bus, slave->cs, bitlen, bytes, flags);
387 /* we can only do 8 bit transfers */
389 flags |= SPI_XFER_END;
393 if (flags & SPI_XFER_BEGIN)
394 spi_cs_activate(slave);
396 /* TX DMA doesn't work quite right */
397 if (SPI_DMA && bytes > 6 && (!tx /*|| !rx*/))
398 ret = spi_dma_xfer(bss, tx, rx, bytes);
400 ret = spi_pio_xfer(bss, tx, rx, bytes);
403 if (flags & SPI_XFER_END)
404 spi_cs_deactivate(slave);