1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <common.h>
21#include <mpc8xx.h>
22#include <commproc.h>
23#include <linux/ctype.h>
24#include <malloc.h>
25#include <post.h>
26#include <serial.h>
27
28#if (defined(CONFIG_SPI)) || (CONFIG_POST & CONFIG_SYS_POST_SPI)
29
30
31
32
33
34
35
36#undef DEBUG
37
38#define SPI_EEPROM_WREN 0x06
39#define SPI_EEPROM_RDSR 0x05
40#define SPI_EEPROM_READ 0x03
41#define SPI_EEPROM_WRITE 0x02
42
43
44
45
46
47
48
49
50
51#ifndef CONFIG_SYS_SPI_INIT_OFFSET
52#define CONFIG_SYS_SPI_INIT_OFFSET 0xB00
53#endif
54
55#ifdef DEBUG
56
57#define DPRINT(a) printf a;
58
59
60
61static const char * const hex_digit = "0123456789ABCDEF";
62
63static char quickhex (int i)
64{
65 return hex_digit[i];
66}
67
68static void memdump (void *pv, int num)
69{
70 int i;
71 unsigned char *pc = (unsigned char *) pv;
72
73 for (i = 0; i < num; i++)
74 printf ("%c%c ", quickhex (pc[i] >> 4), quickhex (pc[i] & 0x0f));
75 printf ("\t");
76 for (i = 0; i < num; i++)
77 printf ("%c", isprint (pc[i]) ? pc[i] : '.');
78 printf ("\n");
79}
80#else
81
82#define DPRINT(a)
83
84#endif
85
86
87
88
89void spi_init (void);
90
91ssize_t spi_read (uchar *, int, uchar *, int);
92ssize_t spi_write (uchar *, int, uchar *, int);
93ssize_t spi_xfer (size_t);
94
95
96
97
98
99#define MAX_BUFFER 0x104
100
101
102
103
104static uchar *rxbuf =
105 (uchar *)&((cpm8xx_t *)&((immap_t *)CONFIG_SYS_IMMR)->im_cpm)->cp_dpmem
106 [CONFIG_SYS_SPI_INIT_OFFSET];
107static uchar *txbuf =
108 (uchar *)&((cpm8xx_t *)&((immap_t *)CONFIG_SYS_IMMR)->im_cpm)->cp_dpmem
109 [CONFIG_SYS_SPI_INIT_OFFSET+MAX_BUFFER];
110
111
112
113
114
115
116
117
118
119
120void spi_init_f (void)
121{
122 unsigned int dpaddr;
123
124 volatile spi_t *spi;
125 volatile immap_t *immr;
126 volatile cpm8xx_t *cp;
127 volatile cbd_t *tbdf, *rbdf;
128
129 immr = (immap_t *) CONFIG_SYS_IMMR;
130 cp = (cpm8xx_t *) &immr->im_cpm;
131
132#ifdef CONFIG_SYS_SPI_UCODE_PATCH
133 spi = (spi_t *)&cp->cp_dpmem[spi->spi_rpbase];
134#else
135 spi = (spi_t *)&cp->cp_dparam[PROFF_SPI];
136
137 spi->spi_rpbase = 0;
138#endif
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 cp->cp_pbpar |= 0x0000000E;
154 cp->cp_pbpar &= ~0x00000001;
155
156
157
158
159
160
161
162
163 cp->cp_pbdir |= 0x0000000F;
164
165
166
167
168
169
170
171
172
173 cp->cp_pbodr |= 0x00000008;
174 cp->cp_pbodr &= ~0x00000007;
175
176
177
178
179 spi->spi_rstate = 0;
180 spi->spi_rdp = 0;
181 spi->spi_rbptr = 0;
182 spi->spi_rbc = 0;
183 spi->spi_rxtmp = 0;
184 spi->spi_tstate = 0;
185 spi->spi_tdp = 0;
186 spi->spi_tbptr = 0;
187 spi->spi_tbc = 0;
188 spi->spi_txtmp = 0;
189
190
191
192
193#ifdef CONFIG_SYS_ALLOC_DPRAM
194 dpaddr = dpram_alloc_align (sizeof(cbd_t)*2, 8);
195#else
196 dpaddr = CPM_SPI_BASE;
197#endif
198
199
200
201 spi->spi_rbase = dpaddr;
202 spi->spi_tbase = dpaddr + sizeof (cbd_t);
203
204
205
206
207
208
209
210
211
212
213 spi->spi_rbptr = spi->spi_rbase;
214 spi->spi_tbptr = spi->spi_tbase;
215
216
217#ifdef CONFIG_SYS_SPI_UCODE_PATCH
218
219
220
221 spi->spi_rstate = 0;
222 spi->spi_tstate = 0;
223#else
224
225 while (cp->cp_cpcr & CPM_CR_FLG)
226 ;
227 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SPI, CPM_CR_INIT_TRX) | CPM_CR_FLG;
228 while (cp->cp_cpcr & CPM_CR_FLG)
229 ;
230#endif
231
232
233
234 immr->im_siu_conf.sc_sdcr = 0x0001;
235
236
237
238 spi->spi_tfcr = SMC_EB;
239 spi->spi_rfcr = SMC_EB;
240
241
242
243 spi->spi_mrblr = MAX_BUFFER;
244
245
246
247 tbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_tbase];
248 rbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_rbase];
249
250 tbdf->cbd_sc &= ~BD_SC_READY;
251 rbdf->cbd_sc &= ~BD_SC_EMPTY;
252
253
254 rbdf->cbd_bufaddr = (ulong) rxbuf;
255 tbdf->cbd_bufaddr = (ulong) txbuf;
256
257
258 cp->cp_spim = 0;
259 cp->cp_spie = SPI_EMASK;
260
261 return;
262}
263
264
265
266
267
268
269
270
271
272
273
274
275void spi_init_r (void)
276{
277 volatile cpm8xx_t *cp;
278 volatile spi_t *spi;
279 volatile immap_t *immr;
280 volatile cbd_t *tbdf, *rbdf;
281
282 immr = (immap_t *) CONFIG_SYS_IMMR;
283 cp = (cpm8xx_t *) &immr->im_cpm;
284
285#ifdef CONFIG_SYS_SPI_UCODE_PATCH
286 spi = (spi_t *)&cp->cp_dpmem[spi->spi_rpbase];
287#else
288 spi = (spi_t *)&cp->cp_dparam[PROFF_SPI];
289
290 spi->spi_rpbase = 0;
291#endif
292
293
294 tbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_tbase];
295 rbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_rbase];
296
297
298 rxbuf = (uchar *) malloc (MAX_BUFFER);
299 txbuf = (uchar *) malloc (MAX_BUFFER);
300
301 rbdf->cbd_bufaddr = (ulong) rxbuf;
302 tbdf->cbd_bufaddr = (ulong) txbuf;
303
304 return;
305}
306
307
308
309
310ssize_t spi_write (uchar *addr, int alen, uchar *buffer, int len)
311{
312 int i;
313
314 memset(rxbuf, 0, MAX_BUFFER);
315 memset(txbuf, 0, MAX_BUFFER);
316 *txbuf = SPI_EEPROM_WREN;
317 spi_xfer(1);
318 memcpy(txbuf, addr, alen);
319 *txbuf = SPI_EEPROM_WRITE;
320 memcpy(alen + txbuf, buffer, len);
321 spi_xfer(alen + len);
322
323 for (i = 0; i < 1000; i++) {
324 *txbuf = SPI_EEPROM_RDSR;
325 txbuf[1] = 0;
326 spi_xfer(2);
327 if (!(rxbuf[1] & 1)) {
328 break;
329 }
330 udelay(1000);
331 }
332 if (i >= 1000) {
333 printf ("*** spi_write: Time out while writing!\n");
334 }
335
336 return len;
337}
338
339
340
341
342ssize_t spi_read (uchar *addr, int alen, uchar *buffer, int len)
343{
344 memset(rxbuf, 0, MAX_BUFFER);
345 memset(txbuf, 0, MAX_BUFFER);
346 memcpy(txbuf, addr, alen);
347 *txbuf = SPI_EEPROM_READ;
348
349
350
351
352
353
354
355 spi_xfer(alen + len + 1);
356 memcpy(buffer, alen + rxbuf, len);
357
358 return len;
359}
360
361
362
363
364ssize_t spi_xfer (size_t count)
365{
366 volatile immap_t *immr;
367 volatile cpm8xx_t *cp;
368 volatile spi_t *spi;
369 cbd_t *tbdf, *rbdf;
370 ushort loop;
371 int tm;
372
373 DPRINT (("*** spi_xfer entered ***\n"));
374
375 immr = (immap_t *) CONFIG_SYS_IMMR;
376 cp = (cpm8xx_t *) &immr->im_cpm;
377
378#ifdef CONFIG_SYS_SPI_UCODE_PATCH
379 spi = (spi_t *)&cp->cp_dpmem[spi->spi_rpbase];
380#else
381 spi = (spi_t *)&cp->cp_dparam[PROFF_SPI];
382
383 spi->spi_rpbase = 0;
384#endif
385
386 tbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_tbase];
387 rbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_rbase];
388
389
390 cp->cp_pbdat &= ~0x0001;
391
392
393 tbdf->cbd_sc = BD_SC_READY | BD_SC_LAST | BD_SC_WRAP;
394 tbdf->cbd_datlen = count;
395
396 DPRINT (("*** spi_xfer: Bytes to be xferred: %d ***\n",
397 tbdf->cbd_datlen));
398
399
400 rbdf->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
401 rbdf->cbd_datlen = 0;
402
403 loop = cp->cp_spmode & SPMODE_LOOP;
404 cp->cp_spmode =
405 loop |
406 SPMODE_REV |
407 SPMODE_MSTR |
408 SPMODE_EN |
409 SPMODE_LEN(8) |
410 SPMODE_PM(0x8) ;
411 cp->cp_spim = 0;
412 cp->cp_spie = SPI_EMASK;
413
414
415 DPRINT (("*** spi_xfer: Performing transfer ...\n"));
416 cp->cp_spcom |= SPI_STR;
417
418
419
420
421
422 for (tm=0; tm<1000; ++tm) {
423 if (cp->cp_spie & SPI_TXB) {
424 DPRINT (("*** spi_xfer: Tx buffer empty\n"));
425 break;
426 }
427 if ((tbdf->cbd_sc & BD_SC_READY) == 0) {
428 DPRINT (("*** spi_xfer: Tx BD done\n"));
429 break;
430 }
431 udelay (1000);
432 }
433 if (tm >= 1000) {
434 printf ("*** spi_xfer: Time out while xferring to/from SPI!\n");
435 }
436 DPRINT (("*** spi_xfer: ... transfer ended\n"));
437
438#ifdef DEBUG
439 printf ("\nspi_xfer: txbuf after xfer\n");
440 memdump ((void *) txbuf, 16);
441 printf ("spi_xfer: rxbuf after xfer\n");
442 memdump ((void *) rxbuf, 16);
443 printf ("\n");
444#endif
445
446
447 cp->cp_pbdat |= 0x0001;
448
449 return count;
450}
451#endif
452
453
454
455
456
457
458
459
460
461
462
463
464#if CONFIG_POST & CONFIG_SYS_POST_SPI
465
466#define TEST_MIN_LENGTH 1
467#define TEST_MAX_LENGTH MAX_BUFFER
468#define TEST_NUM 1
469
470static void packet_fill (char * packet, int length)
471{
472 char c = (char) length;
473 int i;
474
475 for (i = 0; i < length; i++)
476 {
477 packet[i] = c++;
478 }
479}
480
481static int packet_check (char * packet, int length)
482{
483 char c = (char) length;
484 int i;
485
486 for (i = 0; i < length; i++) {
487 if (packet[i] != c++) return -1;
488 }
489
490 return 0;
491}
492
493int spi_post_test (int flags)
494{
495 int res = -1;
496 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
497 volatile cpm8xx_t *cp = (cpm8xx_t *) & immr->im_cpm;
498 int i;
499 int l;
500
501 spi_init_f ();
502 spi_init_r ();
503
504 cp->cp_spmode |= SPMODE_LOOP;
505
506 for (i = 0; i < TEST_NUM; i++) {
507 for (l = TEST_MIN_LENGTH; l <= TEST_MAX_LENGTH; l += 8) {
508 packet_fill ((char *)txbuf, l);
509
510 spi_xfer (l);
511
512 if (packet_check ((char *)rxbuf, l) < 0) {
513 goto Done;
514 }
515 }
516 }
517
518 res = 0;
519
520 Done:
521
522 cp->cp_spmode &= ~SPMODE_LOOP;
523
524
525
526
527
528
529
530#if !defined(CONFIG_8xx_CONS_NONE)
531 serial_reinit_all ();
532#endif
533
534 if (res != 0) {
535 post_log ("SPI test failed\n");
536 }
537
538 return res;
539}
540#endif
541