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