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.
25 #include <asm/blackfin.h>
26 #include <asm/clock.h>
28 #include <asm/portmux.h>
29 #include <asm/mach-common/bits/spi6xx.h>
31 struct bfin_spi_slave {
32 struct spi_slave slave;
34 struct bfin_spi_regs *regs;
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->cs_pol);
64 ssel = bfin_read32(&bss->regs->ssel);
65 ssel |= 1 << slave->cs;
67 ssel |= BIT(8) << slave->cs;
69 ssel &= ~(BIT(8) << slave->cs);
70 bfin_write32(&bss->regs->ssel, ssel);
76 void spi_cs_deactivate(struct spi_slave *slave)
78 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
80 if (is_gpio_cs(slave->cs)) {
81 unsigned int cs = gpio_cs(slave->cs);
82 gpio_set_value(cs, !bss->cs_pol);
85 ssel = bfin_read32(&bss->regs->ssel);
87 ssel &= ~(BIT(8) << slave->cs);
89 ssel |= BIT(8) << slave->cs;
91 bfin_write32(&bss->regs->ssel, ssel);
94 ssel &= ~(1 << slave->cs);
95 bfin_write32(&bss->regs->ssel, ssel);
105 #define SPI_PINS(n) \
106 { 0, P_SPI##n##_SCK, P_SPI##n##_MISO, P_SPI##n##_MOSI, 0 }
107 static unsigned short pins[][5] = {
119 #define SPI_CS_PINS(n) \
121 P_SPI##n##_SSEL1, P_SPI##n##_SSEL2, P_SPI##n##_SSEL3, \
122 P_SPI##n##_SSEL4, P_SPI##n##_SSEL5, P_SPI##n##_SSEL6, \
125 static const unsigned short cs_pins[][7] = {
127 [0] = SPI_CS_PINS(0),
130 [1] = SPI_CS_PINS(1),
133 [2] = SPI_CS_PINS(2),
137 void spi_set_speed(struct spi_slave *slave, uint hz)
139 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
150 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
151 unsigned int max_hz, unsigned int mode)
153 struct bfin_spi_slave *bss;
156 if (!spi_cs_is_valid(bus, cs))
162 reg_base = SPI0_REGBASE;
167 reg_base = SPI1_REGBASE;
172 reg_base = SPI2_REGBASE;
176 debug("%s: invalid bus %u\n", __func__, bus);
180 bss = spi_alloc_slave(struct bfin_spi_slave, bus, cs);
184 bss->regs = (struct bfin_spi_regs *)reg_base;
185 bss->control = SPI_CTL_EN | SPI_CTL_MSTR;
187 bss->control |= SPI_CTL_CPHA;
189 bss->control |= SPI_CTL_CPOL;
190 if (mode & SPI_LSB_FIRST)
191 bss->control |= SPI_CTL_LSBF;
192 bss->control &= ~SPI_CTL_ASSEL;
193 bss->cs_pol = mode & SPI_CS_HIGH ? 1 : 0;
194 spi_set_speed(&bss->slave, max_hz);
199 void spi_free_slave(struct spi_slave *slave)
201 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
205 int spi_claim_bus(struct spi_slave *slave)
207 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
209 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
211 if (is_gpio_cs(slave->cs)) {
212 unsigned int cs = gpio_cs(slave->cs);
213 gpio_request(cs, "bfin-spi");
214 gpio_direction_output(cs, !bss->cs_pol);
215 pins[slave->bus][0] = P_DONTCARE;
217 pins[slave->bus][0] = cs_pins[slave->bus][slave->cs - 1];
218 peripheral_request_list(pins[slave->bus], "bfin-spi");
220 bfin_write32(&bss->regs->control, bss->control);
221 bfin_write32(&bss->regs->clock, bss->clock);
222 bfin_write32(&bss->regs->delay, 0x0);
223 bfin_write32(&bss->regs->rx_control, SPI_RXCTL_REN);
224 bfin_write32(&bss->regs->tx_control, SPI_TXCTL_TEN | SPI_TXCTL_TTI);
230 void spi_release_bus(struct spi_slave *slave)
232 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
234 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
236 peripheral_free_list(pins[slave->bus]);
237 if (is_gpio_cs(slave->cs))
238 gpio_free(gpio_cs(slave->cs));
240 bfin_write32(&bss->regs->rx_control, 0x0);
241 bfin_write32(&bss->regs->tx_control, 0x0);
242 bfin_write32(&bss->regs->control, 0x0);
246 #ifndef CONFIG_BFIN_SPI_IDLE_VAL
247 # define CONFIG_BFIN_SPI_IDLE_VAL 0xff
250 static int spi_pio_xfer(struct bfin_spi_slave *bss, const u8 *tx, u8 *rx,
253 /* discard invalid rx data and empty rfifo */
254 while (!(bfin_read32(&bss->regs->status) & SPI_STAT_RFE))
255 bfin_read32(&bss->regs->rfifo);
258 u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
259 debug("%s: tx:%x ", __func__, value);
260 bfin_write32(&bss->regs->tfifo, value);
262 while (bfin_read32(&bss->regs->status) & SPI_STAT_RFE)
265 value = bfin_read32(&bss->regs->rfifo);
268 debug("rx:%x\n", value);
274 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
275 void *din, unsigned long flags)
277 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
280 uint bytes = bitlen / 8;
283 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
284 slave->bus, slave->cs, bitlen, bytes, flags);
289 /* we can only do 8 bit transfers */
291 flags |= SPI_XFER_END;
295 if (flags & SPI_XFER_BEGIN)
296 spi_cs_activate(slave);
298 ret = spi_pio_xfer(bss, tx, rx, bytes);
301 if (flags & SPI_XFER_END)
302 spi_cs_deactivate(slave);