2 * Driver for Blackfin On-Chip SPI device
4 * Copyright (c) 2005-2010 Analog Devices Inc.
6 * SPDX-License-Identifier: GPL-2.0+
15 #include <asm/blackfin.h>
17 #include <asm/portmux.h>
18 #include <asm/mach-common/bits/spi.h>
20 struct bfin_spi_slave {
21 struct spi_slave slave;
26 #define MAKE_SPI_FUNC(mmr, off) \
27 static inline void write_##mmr(struct bfin_spi_slave *bss, u16 val) { bfin_write16(bss->mmr_base + off, val); } \
28 static inline u16 read_##mmr(struct bfin_spi_slave *bss) { return bfin_read16(bss->mmr_base + off); }
29 MAKE_SPI_FUNC(SPI_CTL, 0x00)
30 MAKE_SPI_FUNC(SPI_FLG, 0x04)
31 MAKE_SPI_FUNC(SPI_STAT, 0x08)
32 MAKE_SPI_FUNC(SPI_TDBR, 0x0c)
33 MAKE_SPI_FUNC(SPI_RDBR, 0x10)
34 MAKE_SPI_FUNC(SPI_BAUD, 0x14)
36 #define to_bfin_spi_slave(s) container_of(s, struct bfin_spi_slave, slave)
38 #define gpio_cs(cs) ((cs) - MAX_CTRL_CS)
39 #ifdef CONFIG_BFIN_SPI_GPIO_CS
40 # define is_gpio_cs(cs) ((cs) > MAX_CTRL_CS)
42 # define is_gpio_cs(cs) 0
45 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
48 return gpio_is_valid(gpio_cs(cs));
50 return (cs >= 1 && cs <= MAX_CTRL_CS);
53 void spi_cs_activate(struct spi_slave *slave)
55 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
57 if (is_gpio_cs(slave->cs)) {
58 unsigned int cs = gpio_cs(slave->cs);
59 gpio_set_value(cs, bss->flg);
60 debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
64 ~((!bss->flg << 8) << slave->cs)) |
66 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
72 void spi_cs_deactivate(struct spi_slave *slave)
74 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
76 if (is_gpio_cs(slave->cs)) {
77 unsigned int cs = gpio_cs(slave->cs);
78 gpio_set_value(cs, !bss->flg);
79 debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
83 /* make sure we force the cs to deassert rather than let the
84 * pin float back up. otherwise, exact timings may not be
85 * met some of the time leading to random behavior (ugh).
87 flg = read_SPI_FLG(bss) | ((!bss->flg << 8) << slave->cs);
88 write_SPI_FLG(bss, flg);
90 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
92 flg &= ~(1 << slave->cs);
93 write_SPI_FLG(bss, flg);
94 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
105 # define SPI0_CTL SPI_CTL
108 #define SPI_PINS(n) \
109 [n] = { 0, P_SPI##n##_SCK, P_SPI##n##_MISO, P_SPI##n##_MOSI, 0 }
110 static unsigned short pins[][5] = {
122 #define SPI_CS_PINS(n) \
124 P_SPI##n##_SSEL1, P_SPI##n##_SSEL2, P_SPI##n##_SSEL3, \
125 P_SPI##n##_SSEL4, P_SPI##n##_SSEL5, P_SPI##n##_SSEL6, \
128 static const unsigned short cs_pins[][7] = {
140 void spi_set_speed(struct spi_slave *slave, uint hz)
142 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
147 /* baud should be rounded up */
148 baud = DIV_ROUND_UP(sclk, 2 * hz);
151 else if (baud > (u16)-1)
156 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
157 unsigned int max_hz, unsigned int mode)
159 struct bfin_spi_slave *bss;
162 if (!spi_cs_is_valid(bus, cs))
168 mmr_base = SPI0_CTL; break;
172 mmr_base = SPI1_CTL; break;
176 mmr_base = SPI2_CTL; break;
179 debug("%s: invalid bus %u\n", __func__, bus);
183 bss = spi_alloc_slave(struct bfin_spi_slave, bus, cs);
187 bss->mmr_base = (void *)mmr_base;
188 bss->ctl = SPE | MSTR | TDBR_CORE;
189 if (mode & SPI_CPHA) bss->ctl |= CPHA;
190 if (mode & SPI_CPOL) bss->ctl |= CPOL;
191 if (mode & SPI_LSB_FIRST) bss->ctl |= LSBF;
192 bss->flg = mode & SPI_CS_HIGH ? 1 : 0;
193 spi_set_speed(&bss->slave, max_hz);
195 debug("%s: bus:%i cs:%i mmr:%x ctl:%x baud:%i flg:%i\n", __func__,
196 bus, cs, mmr_base, bss->ctl, bss->baud, bss->flg);
201 void spi_free_slave(struct spi_slave *slave)
203 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
207 int spi_claim_bus(struct spi_slave *slave)
209 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
211 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
213 if (is_gpio_cs(slave->cs)) {
214 unsigned int cs = gpio_cs(slave->cs);
215 gpio_request(cs, "bfin-spi");
216 gpio_direction_output(cs, !bss->flg);
217 pins[slave->bus][0] = P_DONTCARE;
219 pins[slave->bus][0] = cs_pins[slave->bus][slave->cs - 1];
220 peripheral_request_list(pins[slave->bus], "bfin-spi");
222 write_SPI_CTL(bss, bss->ctl);
223 write_SPI_BAUD(bss, bss->baud);
229 void spi_release_bus(struct spi_slave *slave)
231 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
233 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
235 peripheral_free_list(pins[slave->bus]);
236 if (is_gpio_cs(slave->cs))
237 gpio_free(gpio_cs(slave->cs));
239 write_SPI_CTL(bss, 0);
243 #ifndef CONFIG_BFIN_SPI_IDLE_VAL
244 # define CONFIG_BFIN_SPI_IDLE_VAL 0xff
247 static int spi_pio_xfer(struct bfin_spi_slave *bss, const u8 *tx, u8 *rx,
250 /* discard invalid data and clear RXS */
252 /* todo: take advantage of hardware fifos */
254 u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
255 debug("%s: tx:%x ", __func__, value);
256 write_SPI_TDBR(bss, value);
258 while ((read_SPI_STAT(bss) & TXS))
261 while (!(read_SPI_STAT(bss) & SPIF))
264 while (!(read_SPI_STAT(bss) & RXS))
267 value = read_SPI_RDBR(bss);
270 debug("rx:%x\n", value);
276 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
277 void *din, unsigned long flags)
279 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
282 uint bytes = bitlen / 8;
285 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
286 slave->bus, slave->cs, bitlen, bytes, flags);
291 /* we can only do 8 bit transfers */
293 flags |= SPI_XFER_END;
297 if (flags & SPI_XFER_BEGIN)
298 spi_cs_activate(slave);
300 ret = spi_pio_xfer(bss, tx, rx, bytes);
303 if (flags & SPI_XFER_END)
304 spi_cs_deactivate(slave);