blob: 093166efed8f6155329b703f1a1bed2d533973eb [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>
16#include <asm/mach-common/bits/spi.h>
17
18struct bfin_spi_slave {
19 struct spi_slave slave;
20 void *mmr_base;
21 u16 ctl, baud, flg;
22};
23
24#define MAKE_SPI_FUNC(mmr, off) \
25static inline void write_##mmr(struct bfin_spi_slave *bss, u16 val) { bfin_write16(bss->mmr_base + off, val); } \
26static inline u16 read_##mmr(struct bfin_spi_slave *bss) { return bfin_read16(bss->mmr_base + off); }
27MAKE_SPI_FUNC(SPI_CTL, 0x00)
28MAKE_SPI_FUNC(SPI_FLG, 0x04)
29MAKE_SPI_FUNC(SPI_STAT, 0x08)
30MAKE_SPI_FUNC(SPI_TDBR, 0x0c)
31MAKE_SPI_FUNC(SPI_RDBR, 0x10)
32MAKE_SPI_FUNC(SPI_BAUD, 0x14)
33
34#define to_bfin_spi_slave(s) container_of(s, struct bfin_spi_slave, slave)
35
36__attribute__((weak))
37int spi_cs_is_valid(unsigned int bus, unsigned int cs)
38{
Mike Frysinger2153ace2009-03-26 15:42:12 -040039#if defined(__ADSPBF538__) || defined(__ADSPBF539__)
40 /* The SPI1/SPI2 buses are weird ... only 1 CS */
41 if (bus > 0 && cs != 1)
42 return 0;
43#endif
Mike Frysinger431e4bc2008-10-14 07:54:09 -040044 return (cs >= 1 && cs <= 7);
45}
46
47__attribute__((weak))
48void spi_cs_activate(struct spi_slave *slave)
49{
50 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
51 write_SPI_FLG(bss,
52 (read_SPI_FLG(bss) &
53 ~((!bss->flg << 8) << slave->cs)) |
54 (1 << slave->cs));
Todor I Mollovee9e3c42009-04-04 06:53:06 -040055 SSYNC();
Mike Frysinger431e4bc2008-10-14 07:54:09 -040056 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
57}
58
59__attribute__((weak))
60void spi_cs_deactivate(struct spi_slave *slave)
61{
62 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
Todor I Mollovee9e3c42009-04-04 06:53:06 -040063 u16 flg;
64
65 /* make sure we force the cs to deassert rather than let the
66 * pin float back up. otherwise, exact timings may not be
67 * met some of the time leading to random behavior (ugh).
68 */
69 flg = read_SPI_FLG(bss) | ((!bss->flg << 8) << slave->cs);
70 write_SPI_FLG(bss, flg);
71 SSYNC();
72 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
73
74 flg &= ~(1 << slave->cs);
75 write_SPI_FLG(bss, flg);
76 SSYNC();
Mike Frysinger431e4bc2008-10-14 07:54:09 -040077 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
78}
79
80void spi_init()
81{
82}
83
84struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
85 unsigned int max_hz, unsigned int mode)
86{
87 struct bfin_spi_slave *bss;
88 u32 mmr_base;
89 u32 baud;
90
91 if (!spi_cs_is_valid(bus, cs))
92 return NULL;
93
94 switch (bus) {
95#ifdef SPI_CTL
96# define SPI0_CTL SPI_CTL
97#endif
98 case 0: mmr_base = SPI0_CTL; break;
99#ifdef SPI1_CTL
100 case 1: mmr_base = SPI1_CTL; break;
101#endif
102#ifdef SPI2_CTL
103 case 2: mmr_base = SPI2_CTL; break;
104#endif
105 default: return NULL;
106 }
107
108 baud = get_sclk() / (2 * max_hz);
109 if (baud < 2)
110 baud = 2;
111 else if (baud > (u16)-1)
112 baud = -1;
113
114 bss = malloc(sizeof(*bss));
115 if (!bss)
116 return NULL;
117
118 bss->slave.bus = bus;
119 bss->slave.cs = cs;
120 bss->mmr_base = (void *)mmr_base;
121 bss->ctl = SPE | MSTR | TDBR_CORE;
122 if (mode & SPI_CPHA) bss->ctl |= CPHA;
123 if (mode & SPI_CPOL) bss->ctl |= CPOL;
124 if (mode & SPI_LSB_FIRST) bss->ctl |= LSBF;
125 bss->baud = baud;
126 bss->flg = mode & SPI_CS_HIGH ? 1 : 0;
127
128 debug("%s: bus:%i cs:%i mmr:%x ctl:%x baud:%i flg:%i\n", __func__,
129 bus, cs, mmr_base, bss->ctl, baud, bss->flg);
130
131 return &bss->slave;
132}
133
134void spi_free_slave(struct spi_slave *slave)
135{
136 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
137 free(bss);
138}
139
140static void spi_portmux(struct spi_slave *slave)
141{
142#if defined(__ADSPBF51x__)
143#define SET_MUX(port, mux, func) port##_mux = ((port##_mux & ~PORT_x_MUX_##mux##_MASK) | PORT_x_MUX_##mux##_FUNC_##func)
144 u16 f_mux = bfin_read_PORTF_MUX();
145 u16 f_fer = bfin_read_PORTF_FER();
146 u16 g_mux = bfin_read_PORTG_MUX();
147 u16 g_fer = bfin_read_PORTG_FER();
148 u16 h_mux = bfin_read_PORTH_MUX();
149 u16 h_fer = bfin_read_PORTH_FER();
150 switch (slave->bus) {
151 case 0:
152 /* set SCK/MISO/MOSI */
153 SET_MUX(g, 7, 1);
154 g_fer |= PG12 | PG13 | PG14;
155 switch (slave->cs) {
156 case 1: SET_MUX(f, 2, 1); f_fer |= PF7; break;
157 case 2: /* see G above */ g_fer |= PG15; break;
158 case 3: SET_MUX(h, 1, 3); f_fer |= PH4; break;
Mike Frysingera6ec9572009-05-29 17:01:48 -0400159 case 4: /* no muxing */ h_fer |= PH8; break;
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400160 case 5: SET_MUX(g, 1, 3); h_fer |= PG3; break;
161 case 6: /* no muxing */ break;
162 case 7: /* no muxing */ break;
163 }
164 case 1:
165 /* set SCK/MISO/MOSI */
166 SET_MUX(h, 0, 2);
167 h_fer |= PH1 | PH2 | PH3;
168 switch (slave->cs) {
169 case 1: SET_MUX(h, 2, 3); h_fer |= PH6; break;
170 case 2: SET_MUX(f, 0, 3); f_fer |= PF0; break;
171 case 3: SET_MUX(g, 0, 3); g_fer |= PG0; break;
172 case 4: SET_MUX(f, 3, 3); f_fer |= PF8; break;
173 case 5: SET_MUX(g, 6, 3); h_fer |= PG11; break;
174 case 6: /* no muxing */ break;
175 case 7: /* no muxing */ break;
176 }
177 }
178 bfin_write_PORTF_MUX(f_mux);
179 bfin_write_PORTF_FER(f_fer);
180 bfin_write_PORTG_MUX(g_mux);
181 bfin_write_PORTG_FER(g_fer);
182 bfin_write_PORTH_MUX(h_mux);
183 bfin_write_PORTH_FER(h_fer);
184#elif defined(__ADSPBF52x__)
185#define SET_MUX(port, mux, func) port##_mux = ((port##_mux & ~PORT_x_MUX_##mux##_MASK) | PORT_x_MUX_##mux##_FUNC_##func)
186 u16 f_mux = bfin_read_PORTF_MUX();
187 u16 f_fer = bfin_read_PORTF_FER();
188 u16 g_mux = bfin_read_PORTG_MUX();
189 u16 g_fer = bfin_read_PORTG_FER();
190 u16 h_mux = bfin_read_PORTH_MUX();
191 u16 h_fer = bfin_read_PORTH_FER();
192 /* set SCK/MISO/MOSI */
193 SET_MUX(g, 0, 3);
194 g_fer |= PG2 | PG3 | PG4;
195 switch (slave->cs) {
196 case 1: /* see G above */ g_fer |= PG1; break;
197 case 2: SET_MUX(f, 4, 3); f_fer |= PF12; break;
198 case 3: SET_MUX(f, 4, 3); f_fer |= PF13; break;
199 case 4: SET_MUX(h, 1, 1); h_fer |= PH8; break;
200 case 5: SET_MUX(h, 2, 1); h_fer |= PH9; break;
201 case 6: SET_MUX(f, 1, 3); f_fer |= PF9; break;
202 case 7: SET_MUX(f, 2, 3); f_fer |= PF10; break;
203 }
204 bfin_write_PORTF_MUX(f_mux);
205 bfin_write_PORTF_FER(f_fer);
206 bfin_write_PORTG_MUX(g_mux);
207 bfin_write_PORTG_FER(g_fer);
208 bfin_write_PORTH_MUX(h_mux);
209 bfin_write_PORTH_FER(h_fer);
210#elif defined(__ADSPBF534__) || defined(__ADSPBF536__) || defined(__ADSPBF537__)
211 u16 mux = bfin_read_PORT_MUX();
212 u16 f_fer = bfin_read_PORTF_FER();
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400213 /* set SCK/MISO/MOSI */
214 f_fer |= PF11 | PF12 | PF13;
215 switch (slave->cs) {
216 case 1: f_fer |= PF10; break;
Sonic Zhang830cb662009-03-20 19:28:20 -0400217 case 2: mux |= PJSE; break;
218 case 3: mux |= PJSE; break;
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400219 case 4: mux |= PFS4E; f_fer |= PF6; break;
220 case 5: mux |= PFS5E; f_fer |= PF5; break;
221 case 6: mux |= PFS6E; f_fer |= PF4; break;
Sonic Zhang830cb662009-03-20 19:28:20 -0400222 case 7: mux |= PJCE_SPI; break;
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400223 }
224 bfin_write_PORT_MUX(mux);
225 bfin_write_PORTF_FER(f_fer);
Mike Frysinger2153ace2009-03-26 15:42:12 -0400226#elif defined(__ADSPBF538__) || defined(__ADSPBF539__)
227 u16 fer, pins;
228 if (slave->bus == 1)
229 pins = PD0 | PD1 | PD2 | (slave->cs == 1 ? PD4 : 0);
230 else if (slave->bus == 2)
231 pins = PD5 | PD6 | PD7 | (slave->cs == 1 ? PD9 : 0);
232 else
233 pins = 0;
234 if (pins) {
235 fer = bfin_read_PORTDIO_FER();
236 fer &= ~pins;
237 bfin_write_PORTDIO_FER(fer);
238 }
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400239#elif defined(__ADSPBF54x__)
240#define DO_MUX(port, pin) \
241 mux = ((mux & ~PORT_x_MUX_##pin##_MASK) | PORT_x_MUX_##pin##_FUNC_1); \
242 fer |= P##port##pin;
243 u32 mux;
244 u16 fer;
245 switch (slave->bus) {
246 case 0:
247 mux = bfin_read_PORTE_MUX();
248 fer = bfin_read_PORTE_FER();
249 /* set SCK/MISO/MOSI */
250 DO_MUX(E, 0);
251 DO_MUX(E, 1);
252 DO_MUX(E, 2);
253 switch (slave->cs) {
254 case 1: DO_MUX(E, 4); break;
255 case 2: DO_MUX(E, 5); break;
256 case 3: DO_MUX(E, 6); break;
257 }
258 bfin_write_PORTE_MUX(mux);
259 bfin_write_PORTE_FER(fer);
260 break;
261 case 1:
262 mux = bfin_read_PORTG_MUX();
263 fer = bfin_read_PORTG_FER();
264 /* set SCK/MISO/MOSI */
265 DO_MUX(G, 8);
266 DO_MUX(G, 9);
267 DO_MUX(G, 10);
268 switch (slave->cs) {
269 case 1: DO_MUX(G, 5); break;
270 case 2: DO_MUX(G, 6); break;
271 case 3: DO_MUX(G, 7); break;
272 }
273 bfin_write_PORTG_MUX(mux);
274 bfin_write_PORTG_FER(fer);
275 break;
276 case 2:
277 mux = bfin_read_PORTB_MUX();
278 fer = bfin_read_PORTB_FER();
279 /* set SCK/MISO/MOSI */
280 DO_MUX(B, 12);
281 DO_MUX(B, 13);
282 DO_MUX(B, 14);
283 switch (slave->cs) {
284 case 1: DO_MUX(B, 9); break;
285 case 2: DO_MUX(B, 10); break;
286 case 3: DO_MUX(B, 11); break;
287 }
288 bfin_write_PORTB_MUX(mux);
289 bfin_write_PORTB_FER(fer);
290 break;
291 }
292#endif
293}
294
295int spi_claim_bus(struct spi_slave *slave)
296{
297 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
298
299 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
300
301 spi_portmux(slave);
302 write_SPI_CTL(bss, bss->ctl);
303 write_SPI_BAUD(bss, bss->baud);
304 SSYNC();
305
306 return 0;
307}
308
309void spi_release_bus(struct spi_slave *slave)
310{
311 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
312 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
313 write_SPI_CTL(bss, 0);
314 SSYNC();
315}
316
Cliff Caie1e54fc2009-11-17 09:36:21 +0000317#ifndef CONFIG_BFIN_SPI_IDLE_VAL
318# define CONFIG_BFIN_SPI_IDLE_VAL 0xff
319#endif
320
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400321int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
322 void *din, unsigned long flags)
323{
324 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
325 const u8 *tx = dout;
326 u8 *rx = din;
327 uint bytes = bitlen / 8;
328 int ret = 0;
329
330 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
331 slave->bus, slave->cs, bitlen, bytes, flags);
332
333 if (bitlen == 0)
334 goto done;
335
336 /* we can only do 8 bit transfers */
337 if (bitlen % 8) {
338 flags |= SPI_XFER_END;
339 goto done;
340 }
341
342 if (flags & SPI_XFER_BEGIN)
343 spi_cs_activate(slave);
344
345 /* todo: take advantage of hardware fifos and setup RX dma */
346 while (bytes--) {
Cliff Caie1e54fc2009-11-17 09:36:21 +0000347 u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400348 debug("%s: tx:%x ", __func__, value);
349 write_SPI_TDBR(bss, value);
350 SSYNC();
351 while ((read_SPI_STAT(bss) & TXS))
352 if (ctrlc()) {
353 ret = -1;
354 goto done;
355 }
356 while (!(read_SPI_STAT(bss) & SPIF))
357 if (ctrlc()) {
358 ret = -1;
359 goto done;
360 }
361 while (!(read_SPI_STAT(bss) & RXS))
362 if (ctrlc()) {
363 ret = -1;
364 goto done;
365 }
366 value = read_SPI_RDBR(bss);
367 if (rx)
368 *rx++ = value;
369 debug("rx:%x\n", value);
370 }
371
372 done:
373 if (flags & SPI_XFER_END)
374 spi_cs_deactivate(slave);
375
376 return ret;
377}