1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <common.h>
29#include <spi.h>
30#include <malloc.h>
31#include <asm/immap.h>
32
33struct cf_spi_slave {
34 struct spi_slave slave;
35 uint baudrate;
36 int charbit;
37};
38
39int cfspi_xfer(struct spi_slave *slave, uint bitlen, const void *dout,
40 void *din, ulong flags);
41struct spi_slave *cfspi_setup_slave(struct cf_spi_slave *cfslave, uint mode);
42void cfspi_init(void);
43void cfspi_tx(u32 ctrl, u16 data);
44u16 cfspi_rx(void);
45
46extern void cfspi_port_conf(void);
47extern int cfspi_claim_bus(uint bus, uint cs);
48extern void cfspi_release_bus(uint bus, uint cs);
49
50DECLARE_GLOBAL_DATA_PTR;
51
52#if defined(CONFIG_CF_DSPI)
53
54#define SPI_MODE_MOD 0x00200000
55#define SPI_DBLRATE 0x00100000
56
57void cfspi_init(void)
58{
59 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
60
61 cfspi_port_conf();
62
63 dspi->mcr = DSPI_MCR_MSTR | DSPI_MCR_CSIS7 | DSPI_MCR_CSIS6 |
64 DSPI_MCR_CSIS5 | DSPI_MCR_CSIS4 | DSPI_MCR_CSIS3 |
65 DSPI_MCR_CSIS2 | DSPI_MCR_CSIS1 | DSPI_MCR_CSIS0 |
66 DSPI_MCR_CRXF | DSPI_MCR_CTXF;
67
68
69#ifdef CONFIG_SYS_DSPI_CTAR0
70 dspi->ctar[0] = CONFIG_SYS_DSPI_CTAR0;
71#endif
72#ifdef CONFIG_SYS_DSPI_CTAR1
73 dspi->ctar[1] = CONFIG_SYS_DSPI_CTAR1;
74#endif
75#ifdef CONFIG_SYS_DSPI_CTAR2
76 dspi->ctar[2] = CONFIG_SYS_DSPI_CTAR2;
77#endif
78#ifdef CONFIG_SYS_DSPI_CTAR3
79 dspi->ctar[3] = CONFIG_SYS_DSPI_CTAR3;
80#endif
81#ifdef CONFIG_SYS_DSPI_CTAR4
82 dspi->ctar[4] = CONFIG_SYS_DSPI_CTAR4;
83#endif
84#ifdef CONFIG_SYS_DSPI_CTAR5
85 dspi->ctar[5] = CONFIG_SYS_DSPI_CTAR5;
86#endif
87#ifdef CONFIG_SYS_DSPI_CTAR6
88 dspi->ctar[6] = CONFIG_SYS_DSPI_CTAR6;
89#endif
90#ifdef CONFIG_SYS_DSPI_CTAR7
91 dspi->ctar[7] = CONFIG_SYS_DSPI_CTAR7;
92#endif
93}
94
95void cfspi_tx(u32 ctrl, u16 data)
96{
97 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
98
99 while ((dspi->sr & 0x0000F000) >= 4) ;
100
101 dspi->tfr = (ctrl | data);
102}
103
104u16 cfspi_rx(void)
105{
106 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
107
108 while ((dspi->sr & 0x000000F0) == 0) ;
109
110 return (dspi->rfr & 0xFFFF);
111}
112
113int cfspi_xfer(struct spi_slave *slave, uint bitlen, const void *dout,
114 void *din, ulong flags)
115{
116 struct cf_spi_slave *cfslave = (struct cf_spi_slave *)slave;
117 u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
118 u8 *spi_rd = NULL, *spi_wr = NULL;
119 static u32 ctrl = 0;
120 uint len = bitlen >> 3;
121
122 if (cfslave->charbit == 16) {
123 bitlen >>= 1;
124 spi_wr16 = (u16 *) dout;
125 spi_rd16 = (u16 *) din;
126 } else {
127 spi_wr = (u8 *) dout;
128 spi_rd = (u8 *) din;
129 }
130
131 if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
132 ctrl |= DSPI_TFR_CONT;
133
134 ctrl = (ctrl & 0xFF000000) | ((1 << slave->cs) << 16);
135
136 if (len > 1) {
137 int tmp_len = len - 1;
138 while (tmp_len--) {
139 if (dout != NULL) {
140 if (cfslave->charbit == 16)
141 cfspi_tx(ctrl, *spi_wr16++);
142 else
143 cfspi_tx(ctrl, *spi_wr++);
144 cfspi_rx();
145 }
146
147 if (din != NULL) {
148 cfspi_tx(ctrl, 0);
149 if (cfslave->charbit == 16)
150 *spi_rd16++ = cfspi_rx();
151 else
152 *spi_rd++ = cfspi_rx();
153 }
154 }
155
156 len = 1;
157 }
158
159 if ((flags & SPI_XFER_END) == SPI_XFER_END)
160 ctrl &= ~DSPI_TFR_CONT;
161
162 if (len) {
163 if (dout != NULL) {
164 if (cfslave->charbit == 16)
165 cfspi_tx(ctrl, *spi_wr16);
166 else
167 cfspi_tx(ctrl, *spi_wr);
168 cfspi_rx();
169 }
170
171 if (din != NULL) {
172 cfspi_tx(ctrl, 0);
173 if (cfslave->charbit == 16)
174 *spi_rd16 = cfspi_rx();
175 else
176 *spi_rd = cfspi_rx();
177 }
178 } else {
179
180 cfspi_tx(ctrl, 0);
181 cfspi_rx();
182 }
183
184 return 0;
185}
186
187struct spi_slave *cfspi_setup_slave(struct cf_spi_slave *cfslave, uint mode)
188{
189
190
191
192
193
194
195
196
197
198
199
200
201
202 volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
203 int prescaler[] = { 2, 3, 5, 7 };
204 int scaler[] = {
205 2, 4, 6, 8,
206 16, 32, 64, 128,
207 256, 512, 1024, 2048,
208 4096, 8192, 16384, 32768
209 };
210 int i, j, pbrcnt, brcnt, diff, tmp, dbr = 0;
211 int best_i, best_j, bestmatch = 0x7FFFFFFF, baud_speed;
212 u32 bus_setup = 0;
213
214 tmp = (prescaler[3] * scaler[15]);
215
216 if ((cfslave->baudrate > (gd->bus_clk >> 1)) ||
217 (cfslave->baudrate < (gd->bus_clk / tmp))) {
218 printf("Exceed baudrate limitation: Max %d - Min %d\n",
219 (int)(gd->bus_clk >> 1), (int)(gd->bus_clk / tmp));
220 return NULL;
221 }
222
223
224 if ((CONFIG_SYS_DSPI_CTAR0 & DSPI_CTAR_DBR) ||
225 (cfslave->baudrate > (gd->bus_clk / (prescaler[0] * scaler[0])))) {
226 bus_setup |= DSPI_CTAR_DBR;
227 dbr = 1;
228 }
229
230 if (mode & SPI_CPOL)
231 bus_setup |= DSPI_CTAR_CPOL;
232 if (mode & SPI_CPHA)
233 bus_setup |= DSPI_CTAR_CPHA;
234 if (mode & SPI_LSB_FIRST)
235 bus_setup |= DSPI_CTAR_LSBFE;
236
237
238 if (mode & SPI_MODE_MOD) {
239
240 if ((mode & 0xF0000000) == 0)
241 bus_setup |=
242 dspi->ctar[cfslave->slave.bus] & 0x78000000;
243 else
244 bus_setup |= ((mode & 0xF0000000) >> 1);
245
246
247
248
249
250 if (mode & SPI_DBLRATE) {
251 bus_setup |= DSPI_CTAR_DBR;
252 dbr = 1;
253 }
254 bus_setup |= (mode & 0x0FC00000) >> 4;
255 bus_setup |= (mode & 0x000FFF00) >> 4;
256 } else
257 bus_setup |= (dspi->ctar[cfslave->slave.bus] & 0x78FCFFF0);
258
259 cfslave->charbit =
260 ((dspi->ctar[cfslave->slave.bus] & 0x78000000) ==
261 0x78000000) ? 16 : 8;
262
263 pbrcnt = sizeof(prescaler) / sizeof(int);
264 brcnt = sizeof(scaler) / sizeof(int);
265
266
267 for (best_i = 0, best_j = 0, i = 0; i < pbrcnt; i++) {
268 baud_speed = gd->bus_clk / prescaler[i];
269 for (j = 0; j < brcnt; j++) {
270 tmp = (baud_speed / scaler[j]) * (1 + dbr);
271
272 if (tmp > cfslave->baudrate)
273 diff = tmp - cfslave->baudrate;
274 else
275 diff = cfslave->baudrate - tmp;
276
277 if (diff < bestmatch) {
278 bestmatch = diff;
279 best_i = i;
280 best_j = j;
281 }
282 }
283 }
284 bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
285 dspi->ctar[cfslave->slave.bus] = bus_setup;
286
287 return &cfslave->slave;
288}
289#endif
290
291#ifdef CONFIG_CF_QSPI
292
293#endif
294
295#ifdef CONFIG_CMD_SPI
296int spi_cs_is_valid(unsigned int bus, unsigned int cs)
297{
298 if (((cs >= 0) && (cs < 8)) && ((bus >= 0) && (bus < 8)))
299 return 1;
300 else
301 return 0;
302}
303
304void spi_init_f(void)
305{
306}
307
308void spi_init_r(void)
309{
310}
311
312void spi_init(void)
313{
314 cfspi_init();
315}
316
317struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
318 unsigned int max_hz, unsigned int mode)
319{
320 struct cf_spi_slave *cfslave;
321
322 if (!spi_cs_is_valid(bus, cs))
323 return NULL;
324
325 cfslave = malloc(sizeof(struct cf_spi_slave));
326 if (!cfslave)
327 return NULL;
328
329 cfslave->slave.bus = bus;
330 cfslave->slave.cs = cs;
331 cfslave->baudrate = max_hz;
332
333
334 return cfspi_setup_slave(cfslave, mode);
335}
336
337void spi_free_slave(struct spi_slave *slave)
338{
339 free(slave);
340}
341
342int spi_claim_bus(struct spi_slave *slave)
343{
344 return cfspi_claim_bus(slave->bus, slave->cs);
345}
346
347void spi_release_bus(struct spi_slave *slave)
348{
349 cfspi_release_bus(slave->bus, slave->cs);
350}
351
352int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
353 void *din, unsigned long flags)
354{
355 return cfspi_xfer(slave, bitlen, dout, din, flags);
356}
357#endif
358