2 * Driver for Blackfin On-Chip SPI device
4 * Copyright (c) 2005-2010 Analog Devices Inc.
6 * SPDX-License-Identifier: GPL-2.0+
16 #include <asm/blackfin.h>
17 #include <asm/clock.h>
19 #include <asm/portmux.h>
20 #include <asm/mach-common/bits/spi.h>
22 struct bfin_spi_slave {
23 struct spi_slave slave;
28 #define MAKE_SPI_FUNC(mmr, off) \
29 static inline void write_##mmr(struct bfin_spi_slave *bss, u16 val) { bfin_write16(bss->mmr_base + off, val); } \
30 static inline u16 read_##mmr(struct bfin_spi_slave *bss) { return bfin_read16(bss->mmr_base + off); }
31 MAKE_SPI_FUNC(SPI_CTL, 0x00)
32 MAKE_SPI_FUNC(SPI_FLG, 0x04)
33 MAKE_SPI_FUNC(SPI_STAT, 0x08)
34 MAKE_SPI_FUNC(SPI_TDBR, 0x0c)
35 MAKE_SPI_FUNC(SPI_RDBR, 0x10)
36 MAKE_SPI_FUNC(SPI_BAUD, 0x14)
38 #define to_bfin_spi_slave(s) container_of(s, struct bfin_spi_slave, slave)
40 #define gpio_cs(cs) ((cs) - MAX_CTRL_CS)
41 #ifdef CONFIG_BFIN_SPI_GPIO_CS
42 # define is_gpio_cs(cs) ((cs) > MAX_CTRL_CS)
44 # define is_gpio_cs(cs) 0
47 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
50 return gpio_is_valid(gpio_cs(cs));
52 return (cs >= 1 && cs <= MAX_CTRL_CS);
55 void spi_cs_activate(struct spi_slave *slave)
57 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
59 if (is_gpio_cs(slave->cs)) {
60 unsigned int cs = gpio_cs(slave->cs);
61 gpio_set_value(cs, bss->flg);
62 debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
66 ~((!bss->flg << 8) << slave->cs)) |
68 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
74 void spi_cs_deactivate(struct spi_slave *slave)
76 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
78 if (is_gpio_cs(slave->cs)) {
79 unsigned int cs = gpio_cs(slave->cs);
80 gpio_set_value(cs, !bss->flg);
81 debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
85 /* make sure we force the cs to deassert rather than let the
86 * pin float back up. otherwise, exact timings may not be
87 * met some of the time leading to random behavior (ugh).
89 flg = read_SPI_FLG(bss) | ((!bss->flg << 8) << slave->cs);
90 write_SPI_FLG(bss, flg);
92 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
94 flg &= ~(1 << slave->cs);
95 write_SPI_FLG(bss, flg);
96 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
107 # define SPI0_CTL SPI_CTL
110 #define SPI_PINS(n) \
111 [n] = { 0, P_SPI##n##_SCK, P_SPI##n##_MISO, P_SPI##n##_MOSI, 0 }
112 static unsigned short pins[][5] = {
124 #define SPI_CS_PINS(n) \
126 P_SPI##n##_SSEL1, P_SPI##n##_SSEL2, P_SPI##n##_SSEL3, \
127 P_SPI##n##_SSEL4, P_SPI##n##_SSEL5, P_SPI##n##_SSEL6, \
130 static const unsigned short cs_pins[][7] = {
142 void spi_set_speed(struct spi_slave *slave, uint hz)
144 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
149 /* baud should be rounded up */
150 baud = DIV_ROUND_UP(clk, 2 * hz);
153 else if (baud > (u16)-1)
158 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
159 unsigned int max_hz, unsigned int mode)
161 struct bfin_spi_slave *bss;
164 if (!spi_cs_is_valid(bus, cs))
170 mmr_base = SPI0_CTL; break;
174 mmr_base = SPI1_CTL; break;
178 mmr_base = SPI2_CTL; break;
181 debug("%s: invalid bus %u\n", __func__, bus);
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);
245 #ifndef CONFIG_BFIN_SPI_IDLE_VAL
246 # define CONFIG_BFIN_SPI_IDLE_VAL 0xff
249 static int spi_pio_xfer(struct bfin_spi_slave *bss, const u8 *tx, u8 *rx,
252 /* discard invalid data and clear RXS */
254 /* todo: take advantage of hardware fifos */
256 u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
257 debug("%s: tx:%x ", __func__, value);
258 write_SPI_TDBR(bss, value);
260 while ((read_SPI_STAT(bss) & TXS))
263 while (!(read_SPI_STAT(bss) & SPIF))
266 while (!(read_SPI_STAT(bss) & RXS))
269 value = read_SPI_RDBR(bss);
272 debug("rx:%x\n", value);
278 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
279 void *din, unsigned long flags)
281 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
284 uint bytes = bitlen / 8;
287 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
288 slave->bus, slave->cs, bitlen, bytes, flags);
293 /* we can only do 8 bit transfers */
295 flags |= SPI_XFER_END;
299 if (flags & SPI_XFER_BEGIN)
300 spi_cs_activate(slave);
302 ret = spi_pio_xfer(bss, tx, rx, bytes);
305 if (flags & SPI_XFER_END)
306 spi_cs_deactivate(slave);