blob: 556b97a64dada50370298118809097e6ce764afa [file] [log] [blame]
Mike Frysinger400f5772008-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 Frysinger37a4b752010-06-02 06:13:50 -040016#include <asm/gpio.h>
Mike Frysingerf3732ed2010-06-02 06:12:47 -040017#include <asm/portmux.h>
Mike Frysinger400f5772008-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 Frysinger37a4b752010-06-02 06:13:50 -040038#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)
41#else
42# define is_gpio_cs(cs) 0
43#endif
44
Mike Frysinger400f5772008-10-14 07:54:09 -040045int spi_cs_is_valid(unsigned int bus, unsigned int cs)
46{
Mike Frysinger37a4b752010-06-02 06:13:50 -040047 if (is_gpio_cs(cs))
48 return gpio_is_valid(gpio_cs(cs));
49 else
50 return (cs >= 1 && cs <= MAX_CTRL_CS);
Mike Frysinger400f5772008-10-14 07:54:09 -040051}
52
Mike Frysinger400f5772008-10-14 07:54:09 -040053void spi_cs_activate(struct spi_slave *slave)
54{
55 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
Mike Frysinger37a4b752010-06-02 06:13:50 -040056
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));
61 } else {
62 write_SPI_FLG(bss,
63 (read_SPI_FLG(bss) &
64 ~((!bss->flg << 8) << slave->cs)) |
65 (1 << slave->cs));
66 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
67 }
68
Todor I Mollovd04371a2009-04-04 06:53:06 -040069 SSYNC();
Mike Frysinger400f5772008-10-14 07:54:09 -040070}
71
Mike Frysinger400f5772008-10-14 07:54:09 -040072void spi_cs_deactivate(struct spi_slave *slave)
73{
74 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
Todor I Mollovd04371a2009-04-04 06:53:06 -040075
Mike Frysinger37a4b752010-06-02 06:13:50 -040076 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));
80 } else {
81 u16 flg;
Todor I Mollovd04371a2009-04-04 06:53:06 -040082
Mike Frysinger37a4b752010-06-02 06:13:50 -040083 /* 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).
86 */
87 flg = read_SPI_FLG(bss) | ((!bss->flg << 8) << slave->cs);
88 write_SPI_FLG(bss, flg);
89 SSYNC();
90 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
91
92 flg &= ~(1 << slave->cs);
93 write_SPI_FLG(bss, flg);
94 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
95 }
96
Todor I Mollovd04371a2009-04-04 06:53:06 -040097 SSYNC();
Mike Frysinger400f5772008-10-14 07:54:09 -040098}
99
100void spi_init()
101{
102}
103
Mike Frysingerf3732ed2010-06-02 06:12:47 -0400104#ifdef SPI_CTL
105# define SPI0_CTL SPI_CTL
106#endif
107
108#define SPI_PINS(n) \
109 [n] = { 0, P_SPI##n##_SCK, P_SPI##n##_MISO, P_SPI##n##_MOSI, 0 }
110static unsigned short pins[][5] = {
111#ifdef SPI0_CTL
112 SPI_PINS(0),
113#endif
114#ifdef SPI1_CTL
115 SPI_PINS(1),
116#endif
117#ifdef SPI2_CTL
118 SPI_PINS(2),
119#endif
120};
121
122#define SPI_CS_PINS(n) \
123 [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, \
126 P_SPI##n##_SSEL7, \
127 }
128static const unsigned short cs_pins[][7] = {
129#ifdef SPI0_CTL
130 SPI_CS_PINS(0),
131#endif
132#ifdef SPI1_CTL
133 SPI_CS_PINS(1),
134#endif
135#ifdef SPI2_CTL
136 SPI_CS_PINS(2),
137#endif
138};
139
Mike Frysinger400f5772008-10-14 07:54:09 -0400140struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
141 unsigned int max_hz, unsigned int mode)
142{
143 struct bfin_spi_slave *bss;
Cliff Cai581d92e2009-12-07 08:03:06 +0000144 ulong sclk;
Mike Frysinger400f5772008-10-14 07:54:09 -0400145 u32 mmr_base;
146 u32 baud;
147
148 if (!spi_cs_is_valid(bus, cs))
149 return NULL;
150
Mike Frysingerf3732ed2010-06-02 06:12:47 -0400151 if (bus >= ARRAY_SIZE(pins) || pins[bus] == NULL) {
152 debug("%s: invalid bus %u\n", __func__, bus);
153 return NULL;
154 }
Mike Frysinger400f5772008-10-14 07:54:09 -0400155 switch (bus) {
Mike Frysingerf3732ed2010-06-02 06:12:47 -0400156#ifdef SPI0_CTL
Mike Frysinger400f5772008-10-14 07:54:09 -0400157 case 0: mmr_base = SPI0_CTL; break;
Mike Frysingerf3732ed2010-06-02 06:12:47 -0400158#endif
Mike Frysinger400f5772008-10-14 07:54:09 -0400159#ifdef SPI1_CTL
160 case 1: mmr_base = SPI1_CTL; break;
161#endif
162#ifdef SPI2_CTL
163 case 2: mmr_base = SPI2_CTL; break;
164#endif
165 default: return NULL;
166 }
167
Cliff Cai581d92e2009-12-07 08:03:06 +0000168 sclk = get_sclk();
169 baud = sclk / (2 * max_hz);
170 /* baud should be rounded up */
171 if (sclk % (2 * max_hz))
172 baud += 1;
Mike Frysinger400f5772008-10-14 07:54:09 -0400173 if (baud < 2)
174 baud = 2;
175 else if (baud > (u16)-1)
176 baud = -1;
177
178 bss = malloc(sizeof(*bss));
179 if (!bss)
180 return NULL;
181
182 bss->slave.bus = bus;
183 bss->slave.cs = cs;
184 bss->mmr_base = (void *)mmr_base;
185 bss->ctl = SPE | MSTR | TDBR_CORE;
186 if (mode & SPI_CPHA) bss->ctl |= CPHA;
187 if (mode & SPI_CPOL) bss->ctl |= CPOL;
188 if (mode & SPI_LSB_FIRST) bss->ctl |= LSBF;
189 bss->baud = baud;
190 bss->flg = mode & SPI_CS_HIGH ? 1 : 0;
191
192 debug("%s: bus:%i cs:%i mmr:%x ctl:%x baud:%i flg:%i\n", __func__,
193 bus, cs, mmr_base, bss->ctl, baud, bss->flg);
194
195 return &bss->slave;
196}
197
198void spi_free_slave(struct spi_slave *slave)
199{
200 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
201 free(bss);
202}
203
Mike Frysinger400f5772008-10-14 07:54:09 -0400204int spi_claim_bus(struct spi_slave *slave)
205{
206 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
207
208 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
209
Mike Frysinger37a4b752010-06-02 06:13:50 -0400210 if (is_gpio_cs(slave->cs)) {
211 unsigned int cs = gpio_cs(slave->cs);
212 gpio_request(cs, "bfin-spi");
213 gpio_direction_output(cs, !bss->flg);
214 pins[slave->bus][0] = P_DONTCARE;
215 } else
216 pins[slave->bus][0] = cs_pins[slave->bus][slave->cs - 1];
Mike Frysingerf3732ed2010-06-02 06:12:47 -0400217 peripheral_request_list(pins[slave->bus], "bfin-spi");
218
Mike Frysinger400f5772008-10-14 07:54:09 -0400219 write_SPI_CTL(bss, bss->ctl);
220 write_SPI_BAUD(bss, bss->baud);
221 SSYNC();
222
223 return 0;
224}
225
226void spi_release_bus(struct spi_slave *slave)
227{
228 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
Mike Frysingerf3732ed2010-06-02 06:12:47 -0400229
Mike Frysinger400f5772008-10-14 07:54:09 -0400230 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
Mike Frysingerf3732ed2010-06-02 06:12:47 -0400231
232 peripheral_free_list(pins[slave->bus]);
Mike Frysinger37a4b752010-06-02 06:13:50 -0400233 if (is_gpio_cs(slave->cs))
234 gpio_free(gpio_cs(slave->cs));
Mike Frysingerf3732ed2010-06-02 06:12:47 -0400235
Mike Frysinger400f5772008-10-14 07:54:09 -0400236 write_SPI_CTL(bss, 0);
237 SSYNC();
238}
239
Cliff Cai17ebd5b2009-11-17 09:36:21 +0000240#ifndef CONFIG_BFIN_SPI_IDLE_VAL
241# define CONFIG_BFIN_SPI_IDLE_VAL 0xff
242#endif
243
Mike Frysinger400f5772008-10-14 07:54:09 -0400244int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
245 void *din, unsigned long flags)
246{
247 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
248 const u8 *tx = dout;
249 u8 *rx = din;
250 uint bytes = bitlen / 8;
251 int ret = 0;
252
253 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
254 slave->bus, slave->cs, bitlen, bytes, flags);
255
256 if (bitlen == 0)
257 goto done;
258
259 /* we can only do 8 bit transfers */
260 if (bitlen % 8) {
261 flags |= SPI_XFER_END;
262 goto done;
263 }
264
265 if (flags & SPI_XFER_BEGIN)
266 spi_cs_activate(slave);
267
268 /* todo: take advantage of hardware fifos and setup RX dma */
269 while (bytes--) {
Cliff Cai17ebd5b2009-11-17 09:36:21 +0000270 u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
Mike Frysinger400f5772008-10-14 07:54:09 -0400271 debug("%s: tx:%x ", __func__, value);
272 write_SPI_TDBR(bss, value);
273 SSYNC();
274 while ((read_SPI_STAT(bss) & TXS))
275 if (ctrlc()) {
276 ret = -1;
277 goto done;
278 }
279 while (!(read_SPI_STAT(bss) & SPIF))
280 if (ctrlc()) {
281 ret = -1;
282 goto done;
283 }
284 while (!(read_SPI_STAT(bss) & RXS))
285 if (ctrlc()) {
286 ret = -1;
287 goto done;
288 }
289 value = read_SPI_RDBR(bss);
290 if (rx)
291 *rx++ = value;
292 debug("rx:%x\n", value);
293 }
294
295 done:
296 if (flags & SPI_XFER_END)
297 spi_cs_deactivate(slave);
298
299 return ret;
300}