blob: 0472c1a72fa29b6a02f18f6b07f329b0ee7b88e4 [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{
39 return (cs >= 1 && cs <= 7);
40}
41
42__attribute__((weak))
43void spi_cs_activate(struct spi_slave *slave)
44{
45 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
46 write_SPI_FLG(bss,
47 (read_SPI_FLG(bss) &
48 ~((!bss->flg << 8) << slave->cs)) |
49 (1 << slave->cs));
50 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
51}
52
53__attribute__((weak))
54void spi_cs_deactivate(struct spi_slave *slave)
55{
56 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
57 write_SPI_FLG(bss, read_SPI_FLG(bss) & ~(1 << slave->cs));
58 debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
59}
60
61void spi_init()
62{
63}
64
65struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
66 unsigned int max_hz, unsigned int mode)
67{
68 struct bfin_spi_slave *bss;
69 u32 mmr_base;
70 u32 baud;
71
72 if (!spi_cs_is_valid(bus, cs))
73 return NULL;
74
75 switch (bus) {
76#ifdef SPI_CTL
77# define SPI0_CTL SPI_CTL
78#endif
79 case 0: mmr_base = SPI0_CTL; break;
80#ifdef SPI1_CTL
81 case 1: mmr_base = SPI1_CTL; break;
82#endif
83#ifdef SPI2_CTL
84 case 2: mmr_base = SPI2_CTL; break;
85#endif
86 default: return NULL;
87 }
88
89 baud = get_sclk() / (2 * max_hz);
90 if (baud < 2)
91 baud = 2;
92 else if (baud > (u16)-1)
93 baud = -1;
94
95 bss = malloc(sizeof(*bss));
96 if (!bss)
97 return NULL;
98
99 bss->slave.bus = bus;
100 bss->slave.cs = cs;
101 bss->mmr_base = (void *)mmr_base;
102 bss->ctl = SPE | MSTR | TDBR_CORE;
103 if (mode & SPI_CPHA) bss->ctl |= CPHA;
104 if (mode & SPI_CPOL) bss->ctl |= CPOL;
105 if (mode & SPI_LSB_FIRST) bss->ctl |= LSBF;
106 bss->baud = baud;
107 bss->flg = mode & SPI_CS_HIGH ? 1 : 0;
108
109 debug("%s: bus:%i cs:%i mmr:%x ctl:%x baud:%i flg:%i\n", __func__,
110 bus, cs, mmr_base, bss->ctl, baud, bss->flg);
111
112 return &bss->slave;
113}
114
115void spi_free_slave(struct spi_slave *slave)
116{
117 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
118 free(bss);
119}
120
121static void spi_portmux(struct spi_slave *slave)
122{
123#if defined(__ADSPBF51x__)
124#define SET_MUX(port, mux, func) port##_mux = ((port##_mux & ~PORT_x_MUX_##mux##_MASK) | PORT_x_MUX_##mux##_FUNC_##func)
125 u16 f_mux = bfin_read_PORTF_MUX();
126 u16 f_fer = bfin_read_PORTF_FER();
127 u16 g_mux = bfin_read_PORTG_MUX();
128 u16 g_fer = bfin_read_PORTG_FER();
129 u16 h_mux = bfin_read_PORTH_MUX();
130 u16 h_fer = bfin_read_PORTH_FER();
131 switch (slave->bus) {
132 case 0:
133 /* set SCK/MISO/MOSI */
134 SET_MUX(g, 7, 1);
135 g_fer |= PG12 | PG13 | PG14;
136 switch (slave->cs) {
137 case 1: SET_MUX(f, 2, 1); f_fer |= PF7; break;
138 case 2: /* see G above */ g_fer |= PG15; break;
139 case 3: SET_MUX(h, 1, 3); f_fer |= PH4; break;
140 case 4: /* no muxing */ break;
141 case 5: SET_MUX(g, 1, 3); h_fer |= PG3; break;
142 case 6: /* no muxing */ break;
143 case 7: /* no muxing */ break;
144 }
145 case 1:
146 /* set SCK/MISO/MOSI */
147 SET_MUX(h, 0, 2);
148 h_fer |= PH1 | PH2 | PH3;
149 switch (slave->cs) {
150 case 1: SET_MUX(h, 2, 3); h_fer |= PH6; break;
151 case 2: SET_MUX(f, 0, 3); f_fer |= PF0; break;
152 case 3: SET_MUX(g, 0, 3); g_fer |= PG0; break;
153 case 4: SET_MUX(f, 3, 3); f_fer |= PF8; break;
154 case 5: SET_MUX(g, 6, 3); h_fer |= PG11; break;
155 case 6: /* no muxing */ break;
156 case 7: /* no muxing */ break;
157 }
158 }
159 bfin_write_PORTF_MUX(f_mux);
160 bfin_write_PORTF_FER(f_fer);
161 bfin_write_PORTG_MUX(g_mux);
162 bfin_write_PORTG_FER(g_fer);
163 bfin_write_PORTH_MUX(h_mux);
164 bfin_write_PORTH_FER(h_fer);
165#elif defined(__ADSPBF52x__)
166#define SET_MUX(port, mux, func) port##_mux = ((port##_mux & ~PORT_x_MUX_##mux##_MASK) | PORT_x_MUX_##mux##_FUNC_##func)
167 u16 f_mux = bfin_read_PORTF_MUX();
168 u16 f_fer = bfin_read_PORTF_FER();
169 u16 g_mux = bfin_read_PORTG_MUX();
170 u16 g_fer = bfin_read_PORTG_FER();
171 u16 h_mux = bfin_read_PORTH_MUX();
172 u16 h_fer = bfin_read_PORTH_FER();
173 /* set SCK/MISO/MOSI */
174 SET_MUX(g, 0, 3);
175 g_fer |= PG2 | PG3 | PG4;
176 switch (slave->cs) {
177 case 1: /* see G above */ g_fer |= PG1; break;
178 case 2: SET_MUX(f, 4, 3); f_fer |= PF12; break;
179 case 3: SET_MUX(f, 4, 3); f_fer |= PF13; break;
180 case 4: SET_MUX(h, 1, 1); h_fer |= PH8; break;
181 case 5: SET_MUX(h, 2, 1); h_fer |= PH9; break;
182 case 6: SET_MUX(f, 1, 3); f_fer |= PF9; break;
183 case 7: SET_MUX(f, 2, 3); f_fer |= PF10; break;
184 }
185 bfin_write_PORTF_MUX(f_mux);
186 bfin_write_PORTF_FER(f_fer);
187 bfin_write_PORTG_MUX(g_mux);
188 bfin_write_PORTG_FER(g_fer);
189 bfin_write_PORTH_MUX(h_mux);
190 bfin_write_PORTH_FER(h_fer);
191#elif defined(__ADSPBF534__) || defined(__ADSPBF536__) || defined(__ADSPBF537__)
192 u16 mux = bfin_read_PORT_MUX();
193 u16 f_fer = bfin_read_PORTF_FER();
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400194 /* set SCK/MISO/MOSI */
195 f_fer |= PF11 | PF12 | PF13;
196 switch (slave->cs) {
197 case 1: f_fer |= PF10; break;
Sonic Zhang830cb662009-03-20 19:28:20 -0400198 case 2: mux |= PJSE; break;
199 case 3: mux |= PJSE; break;
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400200 case 4: mux |= PFS4E; f_fer |= PF6; break;
201 case 5: mux |= PFS5E; f_fer |= PF5; break;
202 case 6: mux |= PFS6E; f_fer |= PF4; break;
Sonic Zhang830cb662009-03-20 19:28:20 -0400203 case 7: mux |= PJCE_SPI; break;
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400204 }
205 bfin_write_PORT_MUX(mux);
206 bfin_write_PORTF_FER(f_fer);
Mike Frysinger431e4bc2008-10-14 07:54:09 -0400207#elif defined(__ADSPBF54x__)
208#define DO_MUX(port, pin) \
209 mux = ((mux & ~PORT_x_MUX_##pin##_MASK) | PORT_x_MUX_##pin##_FUNC_1); \
210 fer |= P##port##pin;
211 u32 mux;
212 u16 fer;
213 switch (slave->bus) {
214 case 0:
215 mux = bfin_read_PORTE_MUX();
216 fer = bfin_read_PORTE_FER();
217 /* set SCK/MISO/MOSI */
218 DO_MUX(E, 0);
219 DO_MUX(E, 1);
220 DO_MUX(E, 2);
221 switch (slave->cs) {
222 case 1: DO_MUX(E, 4); break;
223 case 2: DO_MUX(E, 5); break;
224 case 3: DO_MUX(E, 6); break;
225 }
226 bfin_write_PORTE_MUX(mux);
227 bfin_write_PORTE_FER(fer);
228 break;
229 case 1:
230 mux = bfin_read_PORTG_MUX();
231 fer = bfin_read_PORTG_FER();
232 /* set SCK/MISO/MOSI */
233 DO_MUX(G, 8);
234 DO_MUX(G, 9);
235 DO_MUX(G, 10);
236 switch (slave->cs) {
237 case 1: DO_MUX(G, 5); break;
238 case 2: DO_MUX(G, 6); break;
239 case 3: DO_MUX(G, 7); break;
240 }
241 bfin_write_PORTG_MUX(mux);
242 bfin_write_PORTG_FER(fer);
243 break;
244 case 2:
245 mux = bfin_read_PORTB_MUX();
246 fer = bfin_read_PORTB_FER();
247 /* set SCK/MISO/MOSI */
248 DO_MUX(B, 12);
249 DO_MUX(B, 13);
250 DO_MUX(B, 14);
251 switch (slave->cs) {
252 case 1: DO_MUX(B, 9); break;
253 case 2: DO_MUX(B, 10); break;
254 case 3: DO_MUX(B, 11); break;
255 }
256 bfin_write_PORTB_MUX(mux);
257 bfin_write_PORTB_FER(fer);
258 break;
259 }
260#endif
261}
262
263int spi_claim_bus(struct spi_slave *slave)
264{
265 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
266
267 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
268
269 spi_portmux(slave);
270 write_SPI_CTL(bss, bss->ctl);
271 write_SPI_BAUD(bss, bss->baud);
272 SSYNC();
273
274 return 0;
275}
276
277void spi_release_bus(struct spi_slave *slave)
278{
279 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
280 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
281 write_SPI_CTL(bss, 0);
282 SSYNC();
283}
284
285int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
286 void *din, unsigned long flags)
287{
288 struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
289 const u8 *tx = dout;
290 u8 *rx = din;
291 uint bytes = bitlen / 8;
292 int ret = 0;
293
294 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
295 slave->bus, slave->cs, bitlen, bytes, flags);
296
297 if (bitlen == 0)
298 goto done;
299
300 /* we can only do 8 bit transfers */
301 if (bitlen % 8) {
302 flags |= SPI_XFER_END;
303 goto done;
304 }
305
306 if (flags & SPI_XFER_BEGIN)
307 spi_cs_activate(slave);
308
309 /* todo: take advantage of hardware fifos and setup RX dma */
310 while (bytes--) {
311 u8 value = (tx ? *tx++ : 0);
312 debug("%s: tx:%x ", __func__, value);
313 write_SPI_TDBR(bss, value);
314 SSYNC();
315 while ((read_SPI_STAT(bss) & TXS))
316 if (ctrlc()) {
317 ret = -1;
318 goto done;
319 }
320 while (!(read_SPI_STAT(bss) & SPIF))
321 if (ctrlc()) {
322 ret = -1;
323 goto done;
324 }
325 while (!(read_SPI_STAT(bss) & RXS))
326 if (ctrlc()) {
327 ret = -1;
328 goto done;
329 }
330 value = read_SPI_RDBR(bss);
331 if (rx)
332 *rx++ = value;
333 debug("rx:%x\n", value);
334 }
335
336 done:
337 if (flags & SPI_XFER_END)
338 spi_cs_deactivate(slave);
339
340 return ret;
341}