blob: e0ad0298d8abd1b6574e9b5f59ed18bff895ea22 [file] [log] [blame]
Mike Frysinger431e4bc2008-10-14 07:54:09 -04001/*
2 * Driver for Blackfin On-Chip SPI device
3 *
4 * Copyright (c) 2005-2008 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9/*#define DEBUG*/
10
11#include <common.h>
12#include <malloc.h>
13#include <spi.h>
14
15#include <asm/blackfin.h>
Mike Frysinger9c7c6152010-06-02 06:13:50 -040016#include <asm/gpio.h>
Mike Frysinger9fd36072010-06-02 06:12:47 -040017#include <asm/portmux.h>
Mike Frysinger431e4bc2008-10-14 07:54:09 -040018#include <asm/mach-common/bits/spi.h>
19
20struct bfin_spi_slave {
21 struct spi_slave slave;
22 void *mmr_base;
23 u16 ctl, baud, flg;
24};
25
26#define MAKE_SPI_FUNC(mmr, off) \
27static inline void write_##mmr(struct bfin_spi_slave *bss, u16 val) { bfin_write16(bss->mmr_base + off, val); } \
28static inline u16 read_##mmr(struct bfin_spi_slave *bss) { return bfin_read16(bss->mmr_base + off); }
29MAKE_SPI_FUNC(SPI_CTL, 0x00)
30MAKE_SPI_FUNC(SPI_FLG, 0x04)
31MAKE_SPI_FUNC(SPI_STAT, 0x08)
32MAKE_SPI_FUNC(SPI_TDBR, 0x0c)
33MAKE_SPI_FUNC(SPI_RDBR, 0x10)
34MAKE_SPI_FUNC(SPI_BAUD, 0x14)
35
36#define to_bfin_spi_slave(s) container_of(s, struct bfin_spi_slave, slave)
37
Mike Frysinger9c7c6152010-06-02 06:13:50 -040038#define MAX_CTRL_CS 7
39
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)
43#else
44# define is_gpio_cs(cs) 0
45#endif
46
Mike Frysinger431e4bc2008-10-14 07:54:09 -040047int spi_cs_is_valid(unsigned int bus, unsigned int cs)
48{
Mike Frysinger9c7c6152010-06-02 06:13:50 -040049 if (is_gpio_cs(cs))
50 return gpio_is_valid(gpio_cs(cs));
51 else
52 return (cs >= 1 && cs <= MAX_CTRL_CS);
Mike Frysinger431e4bc2008-10-14 07:54:09 -040053}
54
Mike Frysinger431e4bc2008-10-14 07:54:09 -040055void spi_cs_activate(struct spi_slave *slave)
56{
57 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
Mike Frysinger9c7c6152010-06-02 06:13:50 -040058
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));
63 } else {
64 write_SPI_FLG(bss,
65 (read_SPI_FLG(bss) &
66 ~((!bss->flg << 8) << slave->cs)) |
67 (1 << slave->cs));
68 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
69 }
70
Todor I Mollovee9e3c42009-04-04 06:53:06 -040071 SSYNC();
Mike Frysinger431e4bc2008-10-14 07:54:09 -040072}
73
Mike Frysinger431e4bc2008-10-14 07:54:09 -040074void spi_cs_deactivate(struct spi_slave *slave)
75{
76 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
Todor I Mollovee9e3c42009-04-04 06:53:06 -040077
Mike Frysinger9c7c6152010-06-02 06:13:50 -040078 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));
82 } else {
83 u16 flg;
84
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).
88 */
89 flg = read_SPI_FLG(bss) | ((!bss->flg << 8) << slave->cs);
90 write_SPI_FLG(bss, flg);
91 SSYNC();
92 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
Todor I Mollovee9e3c42009-04-04 06:53:06 -040093
Mike Frysinger9c7c6152010-06-02 06:13:50 -040094 flg &= ~(1 << slave->cs);
95 write_SPI_FLG(bss, flg);
96 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
97 }
98
Todor I Mollovee9e3c42009-04-04 06:53:06 -040099 SSYNC();
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400100}
101
102void spi_init()
103{
104}
105
Mike Frysinger9fd36072010-06-02 06:12:47 -0400106#ifdef SPI_CTL
107# define SPI0_CTL SPI_CTL
108#endif
109
110#define SPI_PINS(n) \
111 [n] = { 0, P_SPI##n##_SCK, P_SPI##n##_MISO, P_SPI##n##_MOSI, 0 }
112static unsigned short pins[][5] = {
113#ifdef SPI0_CTL
114 SPI_PINS(0),
115#endif
116#ifdef SPI1_CTL
117 SPI_PINS(1),
118#endif
119#ifdef SPI2_CTL
120 SPI_PINS(2),
121#endif
122};
123
124#define SPI_CS_PINS(n) \
125 [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, \
128 P_SPI##n##_SSEL7, \
129 }
130static const unsigned short cs_pins[][7] = {
131#ifdef SPI0_CTL
132 SPI_CS_PINS(0),
133#endif
134#ifdef SPI1_CTL
135 SPI_CS_PINS(1),
136#endif
137#ifdef SPI2_CTL
138 SPI_CS_PINS(2),
139#endif
140};
141
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400142struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
143 unsigned int max_hz, unsigned int mode)
144{
145 struct bfin_spi_slave *bss;
Cliff Caid2e653b2009-12-07 08:03:06 +0000146 ulong sclk;
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400147 u32 mmr_base;
148 u32 baud;
149
150 if (!spi_cs_is_valid(bus, cs))
151 return NULL;
152
Mike Frysinger9fd36072010-06-02 06:12:47 -0400153 if (bus >= ARRAY_SIZE(pins) || pins[bus] == NULL) {
154 debug("%s: invalid bus %u\n", __func__, bus);
155 return NULL;
156 }
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400157 switch (bus) {
Mike Frysinger9fd36072010-06-02 06:12:47 -0400158#ifdef SPI0_CTL
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400159 case 0: mmr_base = SPI0_CTL; break;
Mike Frysinger9fd36072010-06-02 06:12:47 -0400160#endif
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400161#ifdef SPI1_CTL
162 case 1: mmr_base = SPI1_CTL; break;
163#endif
164#ifdef SPI2_CTL
165 case 2: mmr_base = SPI2_CTL; break;
166#endif
167 default: return NULL;
168 }
169
Cliff Caid2e653b2009-12-07 08:03:06 +0000170 sclk = get_sclk();
171 baud = sclk / (2 * max_hz);
172 /* baud should be rounded up */
173 if (sclk % (2 * max_hz))
174 baud += 1;
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400175 if (baud < 2)
176 baud = 2;
177 else if (baud > (u16)-1)
178 baud = -1;
179
180 bss = malloc(sizeof(*bss));
181 if (!bss)
182 return NULL;
183
184 bss->slave.bus = bus;
185 bss->slave.cs = cs;
186 bss->mmr_base = (void *)mmr_base;
187 bss->ctl = SPE | MSTR | TDBR_CORE;
188 if (mode & SPI_CPHA) bss->ctl |= CPHA;
189 if (mode & SPI_CPOL) bss->ctl |= CPOL;
190 if (mode & SPI_LSB_FIRST) bss->ctl |= LSBF;
191 bss->baud = baud;
192 bss->flg = mode & SPI_CS_HIGH ? 1 : 0;
193
194 debug("%s: bus:%i cs:%i mmr:%x ctl:%x baud:%i flg:%i\n", __func__,
195 bus, cs, mmr_base, bss->ctl, baud, bss->flg);
196
197 return &bss->slave;
198}
199
200void spi_free_slave(struct spi_slave *slave)
201{
202 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
203 free(bss);
204}
205
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400206int spi_claim_bus(struct spi_slave *slave)
207{
208 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
209
210 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
211
Mike Frysinger9c7c6152010-06-02 06:13:50 -0400212 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->flg);
216 pins[slave->bus][0] = P_DONTCARE;
217 } else
218 pins[slave->bus][0] = cs_pins[slave->bus][slave->cs - 1];
Mike Frysinger9fd36072010-06-02 06:12:47 -0400219 peripheral_request_list(pins[slave->bus], "bfin-spi");
220
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400221 write_SPI_CTL(bss, bss->ctl);
222 write_SPI_BAUD(bss, bss->baud);
223 SSYNC();
224
225 return 0;
226}
227
228void spi_release_bus(struct spi_slave *slave)
229{
230 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
Mike Frysinger9fd36072010-06-02 06:12:47 -0400231
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400232 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
Mike Frysinger9fd36072010-06-02 06:12:47 -0400233
234 peripheral_free_list(pins[slave->bus]);
Mike Frysinger9c7c6152010-06-02 06:13:50 -0400235 if (is_gpio_cs(slave->cs))
236 gpio_free(gpio_cs(slave->cs));
Mike Frysinger9fd36072010-06-02 06:12:47 -0400237
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400238 write_SPI_CTL(bss, 0);
239 SSYNC();
240}
241
Cliff Caie1e54fc2009-11-17 09:36:21 +0000242#ifndef CONFIG_BFIN_SPI_IDLE_VAL
243# define CONFIG_BFIN_SPI_IDLE_VAL 0xff
244#endif
245
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400246int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
247 void *din, unsigned long flags)
248{
249 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
250 const u8 *tx = dout;
251 u8 *rx = din;
252 uint bytes = bitlen / 8;
253 int ret = 0;
254
255 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
256 slave->bus, slave->cs, bitlen, bytes, flags);
257
258 if (bitlen == 0)
259 goto done;
260
261 /* we can only do 8 bit transfers */
262 if (bitlen % 8) {
263 flags |= SPI_XFER_END;
264 goto done;
265 }
266
267 if (flags & SPI_XFER_BEGIN)
268 spi_cs_activate(slave);
269
270 /* todo: take advantage of hardware fifos and setup RX dma */
271 while (bytes--) {
Cliff Caie1e54fc2009-11-17 09:36:21 +0000272 u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400273 debug("%s: tx:%x ", __func__, value);
274 write_SPI_TDBR(bss, value);
275 SSYNC();
276 while ((read_SPI_STAT(bss) & TXS))
277 if (ctrlc()) {
278 ret = -1;
279 goto done;
280 }
281 while (!(read_SPI_STAT(bss) & SPIF))
282 if (ctrlc()) {
283 ret = -1;
284 goto done;
285 }
286 while (!(read_SPI_STAT(bss) & RXS))
287 if (ctrlc()) {
288 ret = -1;
289 goto done;
290 }
291 value = read_SPI_RDBR(bss);
292 if (rx)
293 *rx++ = value;
294 debug("rx:%x\n", value);
295 }
296
297 done:
298 if (flags & SPI_XFER_END)
299 spi_cs_deactivate(slave);
300
301 return ret;
302}