2 * Analog Devices SPI3 controller driver
4 * Copyright (c) 2011 Analog Devices Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <asm/blackfin.h>
26 #include <asm/portmux.h>
27 #include <asm/mach-common/bits/spi6xx.h>
29 struct bfin_spi_slave {
30 struct spi_slave slave;
32 struct bfin_spi_regs *regs;
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->cs_pol);
62 ssel = bfin_read32(&bss->regs->ssel);
63 ssel |= 1 << slave->cs;
65 ssel |= (1 << 8) << slave->cs;
67 ssel &= ~((1 << 8) << slave->cs);
68 bfin_write32(&bss->regs->ssel, ssel);
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->cs_pol);
83 ssel = bfin_read32(&bss->regs->ssel);
85 ssel &= ~((1 << 8) << slave->cs);
87 ssel |= (1 << 8) << slave->cs;
89 bfin_write32(&bss->regs->ssel, ssel);
92 ssel &= ~(1 << slave->cs);
93 bfin_write32(&bss->regs->ssel, ssel);
103 #define SPI_PINS(n) \
104 { 0, P_SPI##n##_SCK, P_SPI##n##_MISO, P_SPI##n##_MOSI, 0 }
105 static unsigned short pins[][5] = {
117 #define SPI_CS_PINS(n) \
119 P_SPI##n##_SSEL1, P_SPI##n##_SSEL2, P_SPI##n##_SSEL3, \
120 P_SPI##n##_SSEL4, P_SPI##n##_SSEL5, P_SPI##n##_SSEL6, \
123 static const unsigned short cs_pins[][7] = {
125 [0] = SPI_CS_PINS(0),
128 [1] = SPI_CS_PINS(1),
131 [2] = SPI_CS_PINS(2),
135 void spi_set_speed(struct spi_slave *slave, uint hz)
137 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
148 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
149 unsigned int max_hz, unsigned int mode)
151 struct bfin_spi_slave *bss;
154 if (!spi_cs_is_valid(bus, cs))
157 if (bus >= ARRAY_SIZE(pins) || pins[bus] == NULL) {
158 debug("%s: invalid bus %u\n", __func__, bus);
164 reg_base = SPI0_REGBASE;
169 reg_base = SPI1_REGBASE;
174 reg_base = SPI2_REGBASE;
181 bss = spi_alloc_slave(struct bfin_spi_slave, bus, cs);
185 bss->regs = (struct bfin_spi_regs *)reg_base;
186 bss->control = SPI_CTL_EN | SPI_CTL_MSTR;
188 bss->control |= SPI_CTL_CPHA;
190 bss->control |= SPI_CTL_CPOL;
191 if (mode & SPI_LSB_FIRST)
192 bss->control |= SPI_CTL_LSBF;
193 bss->control &= ~SPI_CTL_ASSEL;
194 bss->cs_pol = mode & SPI_CS_HIGH ? 1 : 0;
195 spi_set_speed(&bss->slave, max_hz);
200 void spi_free_slave(struct spi_slave *slave)
202 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
206 int spi_claim_bus(struct spi_slave *slave)
208 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
210 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
212 if (is_gpio_cs(slave->cs)) {
213 unsigned int cs = gpio_cs(slave->cs);
214 gpio_request(cs, "bfin-spi");
215 gpio_direction_output(cs, !bss->cs_pol);
216 pins[slave->bus][0] = P_DONTCARE;
218 pins[slave->bus][0] = cs_pins[slave->bus][slave->cs - 1];
219 peripheral_request_list(pins[slave->bus], "bfin-spi");
221 bfin_write32(&bss->regs->control, bss->control);
222 bfin_write32(&bss->regs->clock, bss->clock);
223 bfin_write32(&bss->regs->delay, 0x0);
224 bfin_write32(&bss->regs->rx_control, SPI_RXCTL_REN);
225 bfin_write32(&bss->regs->tx_control, SPI_TXCTL_TEN | SPI_TXCTL_TTI);
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 bfin_write32(&bss->regs->rx_control, 0x0);
242 bfin_write32(&bss->regs->tx_control, 0x0);
243 bfin_write32(&bss->regs->control, 0x0);
247 #ifndef CONFIG_BFIN_SPI_IDLE_VAL
248 # define CONFIG_BFIN_SPI_IDLE_VAL 0xff
251 static int spi_pio_xfer(struct bfin_spi_slave *bss, const u8 *tx, u8 *rx,
254 /* discard invalid rx data and empty rfifo */
255 while (!(bfin_read32(&bss->regs->status) & SPI_STAT_RFE))
256 bfin_read32(&bss->regs->rfifo);
259 u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
260 debug("%s: tx:%x ", __func__, value);
261 bfin_write32(&bss->regs->tfifo, value);
263 while (bfin_read32(&bss->regs->status) & SPI_STAT_RFE)
266 value = bfin_read32(&bss->regs->rfifo);
269 debug("rx:%x\n", value);
275 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
276 void *din, unsigned long flags)
278 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
281 uint bytes = bitlen / 8;
284 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
285 slave->bus, slave->cs, bitlen, bytes, flags);
290 /* we can only do 8 bit transfers */
292 flags |= SPI_XFER_END;
296 if (flags & SPI_XFER_BEGIN)
297 spi_cs_activate(slave);
299 ret = spi_pio_xfer(bss, tx, rx, bytes);
302 if (flags & SPI_XFER_END)
303 spi_cs_deactivate(slave);