]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/spc1920/hpi.c
Merge branch 'master' of git://www.denx.de/git/u-boot-cfi-flash
[karo-tx-uboot.git] / board / spc1920 / hpi.c
1 /*
2  * (C) Copyright 2006
3  * Markus Klotzbuecher, DENX Software Engineering, mk@denx.de.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 /*
9  * Host Port Interface (HPI)
10  */
11
12 /* debug levels:
13  *  0 : errors
14  *  1 : usefull info
15  *  2 : lots of info
16  *  3 : noisy
17  */
18
19 #define DEBUG 0
20
21 #include <config.h>
22 #include <common.h>
23 #include <mpc8xx.h>
24
25 #include "pld.h"
26 #include "hpi.h"
27
28 #define _NOT_USED_      0xFFFFFFFF
29
30 /* original table:
31  * - inserted loops to achieve long CS low and high Periods (~217ns)
32  * - move cs high 2/4 to the right
33  */
34 const uint dsp_table_slow[] =
35 {
36         /* single read   (offset  0x00 in upm ram) */
37         0x8fffdc04, 0x0fffdc84, 0x0fffdc84, 0x0fffdc00,
38         0x3fffdc04, 0xffffdc84, 0xffffdc84, 0xffffdc05,
39
40         /* burst read    (offset 0x08 in upm ram) */
41         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
42         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
43         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
44         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
45
46         /* single write  (offset 0x18 in upm ram) */
47         0x8fffd004, 0x0fffd084, 0x0fffd084, 0x3fffd000,
48         0xffffd084, 0xffffd084, 0xffffd005, _NOT_USED_,
49
50         /* burst write   (offset 0x20 in upm ram) */
51         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
52         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
53         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
54         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
55         /* refresh       (offset 0x30 in upm ram) */
56         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
57         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
58         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
59         /* exception     (offset 0x3C in upm ram) */
60         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
61 };
62
63 /* dsp hpi upm ram table
64  * works fine for noninc access, failes on incremental.
65  * - removed first word
66  */
67 const uint dsp_table_fast[] =
68 {
69         /* single read   (offset  0x00 in upm ram) */
70         0x8fffdc04, 0x0fffdc04, 0x0fffdc00, 0x3fffdc04,
71         0xffffdc05, _NOT_USED_, _NOT_USED_, _NOT_USED_,
72
73         /* burst read    (offset 0x08 in upm ram) */
74         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
75         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
76         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
77         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
78
79         /* single write  (offset 0x18 in upm ram) */
80         0x8fffd004, 0x0fffd004, 0x3fffd000, 0xffffd005,
81         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
82
83         /* burst write   (offset 0x20 in upm ram) */
84         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
85         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
86         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
87         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
88         /* refresh       (offset 0x30 in upm ram) */
89         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
90         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
91         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
92         /* exception     (offset 0x3C in upm ram) */
93         _NOT_USED_, _NOT_USED_, _NOT_USED_, _NOT_USED_,
94 };
95
96
97 #ifdef CONFIG_SPC1920_HPI_TEST
98 #undef HPI_TEST_OSZI
99
100 #define HPI_TEST_CHUNKSIZE      0x1000
101 #define HPI_TEST_PATTERN        0x00000000
102 #define HPI_TEST_START          0x0
103 #define HPI_TEST_END            0x30000
104
105 #define TINY_AUTOINC_DATA_SIZE 16 /* 32bit words */
106 #define TINY_AUTOINC_BASE_ADDR 0x0
107
108 static int hpi_activate(void);
109 #if 0
110 static void hpi_inactivate(void);
111 #endif
112 static void dsp_reset(void);
113
114 static int hpi_write_inc(u32 addr, u32 *data, u32 count);
115 static int hpi_read_inc(u32 addr, u32 *buf, u32 count);
116 static int hpi_write_noinc(u32 addr, u32 data);
117 static u32 hpi_read_noinc(u32 addr);
118
119 int hpi_test(void);
120 static int hpi_write_addr_test(u32 addr);
121 static int hpi_read_write_test(u32 addr, u32 data);
122 #ifdef DO_TINY_TEST
123 static int hpi_tiny_autoinc_test(void);
124 #endif /* DO_TINY_TEST */
125 #endif /* CONFIG_SPC1920_HPI_TEST */
126
127
128 /* init the host port interface on UPMA */
129 int hpi_init(void)
130 {
131         volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
132         volatile memctl8xx_t *memctl = &immr->im_memctl;
133         volatile spc1920_pld_t *pld = (spc1920_pld_t *) CONFIG_SYS_SPC1920_PLD_BASE;
134
135         upmconfig(UPMA, (uint *)dsp_table_slow, sizeof(dsp_table_slow)/sizeof(uint));
136         udelay(100);
137
138         memctl->memc_mamr = CONFIG_SYS_MAMR;
139         memctl->memc_or3 = CONFIG_SYS_OR3;
140         memctl->memc_br3 = CONFIG_SYS_BR3;
141
142         /* reset dsp */
143         dsp_reset();
144
145         /* activate hpi switch*/
146         pld->dsp_hpi_on = 0x1;
147
148         udelay(100);
149
150         return 0;
151 }
152
153 #ifdef CONFIG_SPC1920_HPI_TEST
154 /* activate the Host Port interface */
155 static int hpi_activate(void)
156 {
157         volatile spc1920_pld_t *pld = (spc1920_pld_t *) CONFIG_SYS_SPC1920_PLD_BASE;
158
159         /* turn on hpi */
160         pld->dsp_hpi_on = 0x1;
161
162         udelay(5);
163
164         /* turn on the power EN_DSP_POWER high*/
165         /* currently always on TBD */
166
167         /* setup hpi control register */
168         HPI_HPIC_1 = (u16) 0x0008;
169         HPI_HPIC_2 = (u16) 0x0008;
170
171         udelay(100);
172
173         return 0;
174 }
175
176 #if 0
177 /* turn off the host port interface */
178 static void hpi_inactivate(void)
179 {
180         volatile spc1920_pld_t *pld = (spc1920_pld_t *) CONFIG_SYS_SPC1920_PLD_BASE;
181
182         /* deactivate hpi */
183         pld->dsp_hpi_on = 0x0;
184
185         /* reset the dsp */
186         /* pld->dsp_reset = 0x0; */
187
188         /* turn off the power EN_DSP_POWER# high*/
189         /* currently always on TBD */
190
191 }
192 #endif
193
194 /* reset the DSP */
195 static void dsp_reset(void)
196 {
197         volatile spc1920_pld_t *pld = (spc1920_pld_t *) CONFIG_SYS_SPC1920_PLD_BASE;
198         pld->dsp_reset = 0x1;
199         pld->dsp_hpi_on = 0x0;
200
201         udelay(300000);
202
203         pld->dsp_reset = 0x0;
204         pld->dsp_hpi_on = 0x1;
205 }
206
207
208 /* write using autoinc (count is number of 32bit words) */
209 static int hpi_write_inc(u32 addr, u32 *data, u32 count)
210 {
211         int i;
212         u16 addr1, addr2;
213
214         addr1 = (u16) ((addr >> 16) & 0xffff); /* First HW is most significant */
215         addr2 = (u16) (addr & 0xffff);
216
217         /* write address */
218         HPI_HPIA_1 = addr1;
219         HPI_HPIA_2 = addr2;
220
221         debug("writing from data=0x%lx to 0x%lx\n",
222                 (ulong)data, (ulong)(data+count));
223
224         for(i=0; i<count; i++) {
225                 HPI_HPID_INC_1 = (u16) ((data[i] >> 16) & 0xffff);
226                 HPI_HPID_INC_2 = (u16) (data[i] & 0xffff);
227                 debug("hpi_write_inc: data1=0x%x, data2=0x%x\n",
228                        (u16) ((data[i] >> 16) & 0xffff),
229                        (u16) (data[i] & 0xffff));
230         }
231 #if 0
232         while(data_ptr < (u16*) (data + count)) {
233                 HPI_HPID_INC_1 = *(data_ptr++);
234                 HPI_HPID_INC_2 = *(data_ptr++);
235         }
236 #endif
237
238         /* return number of bytes written */
239         return count;
240 }
241
242 /*
243  * read using autoinc (count is number of 32bit words)
244  */
245 static int hpi_read_inc(u32 addr, u32 *buf, u32 count)
246 {
247         int i;
248         u16 addr1, addr2, data1, data2;
249
250         addr1 = (u16) ((addr >> 16) & 0xffff); /* First HW is most significant */
251         addr2 = (u16) (addr & 0xffff);
252
253         /* write address */
254         HPI_HPIA_1 = addr1;
255         HPI_HPIA_2 = addr2;
256
257         for(i=0; i<count; i++) {
258                 data1 = HPI_HPID_INC_1;
259                 data2 = HPI_HPID_INC_2;
260                 debug("hpi_read_inc: data1=0x%x, data2=0x%x\n", data1, data2);
261                 buf[i] = (((u32) data1) << 16) | (data2 & 0xffff);
262         }
263
264 #if 0
265         while(buf_ptr < (u16*) (buf + count)) {
266                 *(buf_ptr++) = HPI_HPID_INC_1;
267                 *(buf_ptr++) = HPI_HPID_INC_2;
268         }
269 #endif
270
271         /* return number of bytes read */
272         return count;
273 }
274
275
276 /* write to non- auto inc regs */
277 static int hpi_write_noinc(u32 addr, u32 data)
278 {
279
280         u16 addr1, addr2, data1, data2;
281
282         addr1 = (u16) ((addr >> 16) & 0xffff); /* First HW is most significant */
283         addr2 = (u16) (addr & 0xffff);
284
285         /* printf("hpi_write_noinc: addr1=0x%x, addr2=0x%x\n", addr1, addr2); */
286
287         HPI_HPIA_1 = addr1;
288         HPI_HPIA_2 = addr2;
289
290         data1 = (u16) ((data >> 16) & 0xffff);
291         data2 = (u16) (data & 0xffff);
292
293         /* printf("hpi_write_noinc: data1=0x%x, data2=0x%x\n", data1, data2); */
294
295         HPI_HPID_NOINC_1 = data1;
296         HPI_HPID_NOINC_2 = data2;
297
298         return 0;
299 }
300
301 /* read from non- auto inc regs */
302 static u32 hpi_read_noinc(u32 addr)
303 {
304         u16 addr1, addr2, data1, data2;
305         u32 ret;
306
307         addr1 = (u16) ((addr >> 16) & 0xffff); /* First HW is most significant */
308         addr2 = (u16) (addr & 0xffff);
309
310         HPI_HPIA_1 = addr1;
311         HPI_HPIA_2 = addr2;
312
313         /* printf("hpi_read_noinc: addr1=0x%x, addr2=0x%x\n", addr1, addr2); */
314
315         data1 = HPI_HPID_NOINC_1;
316         data2 = HPI_HPID_NOINC_2;
317
318         /* printf("hpi_read_noinc: data1=0x%x, data2=0x%x\n", data1, data2); */
319
320         ret = (((u32) data1) << 16) | (data2 & 0xffff);
321         return ret;
322
323 }
324
325 /*
326  * Host Port Interface Tests
327  */
328
329 #ifndef HPI_TEST_OSZI
330 /* main test function */
331 int hpi_test(void)
332 {
333         int err = 0;
334         u32 i, ii, pattern, tmp;
335
336         pattern = HPI_TEST_PATTERN;
337
338         u32 test_data[HPI_TEST_CHUNKSIZE];
339         u32 read_data[HPI_TEST_CHUNKSIZE];
340
341         debug("hpi_test: activating hpi...");
342         hpi_activate();
343         debug("OK.\n");
344
345 #if 0
346         /* Dump the first 1024 bytes
347          *
348          */
349         for(i=0; i<1024; i+=4) {
350                 if(i%16==0)
351                         printf("\n0x%08x: ", i);
352                 printf("0x%08x ", hpi_read_noinc(i));
353         }
354 #endif
355
356         /* HPIA read-write test
357          *
358          */
359         debug("hpi_test: starting HPIA read-write tests...\n");
360         err |= hpi_write_addr_test(0xdeadc0de);
361         err |= hpi_write_addr_test(0xbeefd00d);
362         err |= hpi_write_addr_test(0xabcd1234);
363         err |= hpi_write_addr_test(0xaaaaaaaa);
364         if(err) {
365                 debug("hpi_test: HPIA read-write tests: *** FAILED ***\n");
366                 return -1;
367         }
368         debug("hpi_test: HPIA read-write tests: OK\n");
369
370
371         /* read write test using nonincremental data regs
372          *
373          */
374         debug("hpi_test: starting nonincremental tests...\n");
375         for(i=HPI_TEST_START; i<HPI_TEST_END; i+=4) {
376                 err |= hpi_read_write_test(i, pattern);
377
378                 /* stolen from cmd_mem.c */
379                 if(pattern & 0x80000000) {
380                         pattern = -pattern;     /* complement & increment */
381                 } else {
382                         pattern = ~pattern;
383                 }
384                 err |= hpi_read_write_test(i, pattern);
385
386                 if(err) {
387                         debug("hpi_test: nonincremental tests *** FAILED ***\n");
388                         return -1;
389                 }
390         }
391         debug("hpi_test: nonincremental test OK\n");
392
393         /* read write a chunk of data using nonincremental data regs
394          *
395          */
396         debug("hpi_test: starting nonincremental chunk tests...\n");
397         pattern = HPI_TEST_PATTERN;
398         for(i=HPI_TEST_START; i<HPI_TEST_END; i+=4) {
399                 hpi_write_noinc(i, pattern);
400
401                 /* stolen from cmd_mem.c */
402                 if(pattern & 0x80000000) {
403                         pattern = -pattern;     /* complement & increment */
404                 } else {
405                         pattern = ~pattern;
406                 }
407         }
408         pattern = HPI_TEST_PATTERN;
409         for(i=HPI_TEST_START; i<HPI_TEST_END; i+=4) {
410                 tmp = hpi_read_noinc(i);
411
412                 if(tmp != pattern) {
413                         debug("hpi_test: noninc chunk test *** FAILED *** @ 0x%x, written=0x%x, read=0x%x\n", i, pattern, tmp);
414                         err = -1;
415                 }
416                 /* stolen from cmd_mem.c */
417                 if(pattern & 0x80000000) {
418                         pattern = -pattern;     /* complement & increment */
419                 } else {
420                         pattern = ~pattern;
421                 }
422         }
423         if(err)
424                 return -1;
425         debug("hpi_test: nonincremental chunk test OK\n");
426
427
428 #ifdef DO_TINY_TEST
429         /* small verbose test using autoinc and nonautoinc to compare
430          *
431          */
432         debug("hpi_test: tiny_autoinc_test...\n");
433         hpi_tiny_autoinc_test();
434         debug("hpi_test: tiny_autoinc_test done\n");
435 #endif /* DO_TINY_TEST */
436
437
438         /* $%& write a chunk of data using the autoincremental regs
439          *
440          */
441         debug("hpi_test: starting autoinc test %d chunks with 0x%x bytes...\n",
442                ((HPI_TEST_END - HPI_TEST_START) / HPI_TEST_CHUNKSIZE),
443                HPI_TEST_CHUNKSIZE);
444
445         for(i=HPI_TEST_START;
446             i < ((HPI_TEST_END - HPI_TEST_START) / HPI_TEST_CHUNKSIZE);
447             i++) {
448                 /* generate the pattern data */
449                 debug("generating pattern data: ");
450                 for(ii = 0; ii < HPI_TEST_CHUNKSIZE; ii++) {
451                         debug("0x%x ", pattern);
452
453                         test_data[ii] = pattern;
454                         read_data[ii] = 0x0; /* zero to be sure */
455
456                         /* stolen from cmd_mem.c */
457                         if(pattern & 0x80000000) {
458                                 pattern = -pattern;     /* complement & increment */
459                         } else {
460                                 pattern = ~pattern;
461                         }
462                 }
463                 debug("done\n");
464
465                 debug("Writing autoinc data @ 0x%x\n", i);
466                 hpi_write_inc(i, test_data, HPI_TEST_CHUNKSIZE);
467
468                 debug("Reading autoinc data @ 0x%x\n", i);
469                 hpi_read_inc(i, read_data, HPI_TEST_CHUNKSIZE);
470
471                 /* compare */
472                 for(ii = 0; ii < HPI_TEST_CHUNKSIZE; ii++) {
473                         debug("hpi_test_autoinc: @ 0x%x, written=0x%x, read=0x%x", i+ii, test_data[ii], read_data[ii]);
474                         if(read_data[ii] != test_data[ii]) {
475                                 debug("hpi_test: autoinc test @ 0x%x, written=0x%x, read=0x%x *** FAILED ***\n", i+ii, test_data[ii], read_data[ii]);
476                                 return -1;
477                         }
478                 }
479         }
480         debug("hpi_test: autoinc test OK\n");
481
482         return 0;
483 }
484 #else /* HPI_TEST_OSZI */
485 int hpi_test(void)
486 {
487         int i;
488         u32 read_data[TINY_AUTOINC_DATA_SIZE];
489
490         unsigned int dummy_data[TINY_AUTOINC_DATA_SIZE] = {
491                 0x11112222, 0x33334444, 0x55556666, 0x77778888,
492                 0x9999aaaa, 0xbbbbcccc, 0xddddeeee, 0xffff1111,
493                 0x00010002, 0x00030004, 0x00050006, 0x00070008,
494                 0x0009000a, 0x000b000c, 0x000d000e, 0x000f0001
495         };
496
497         debug("hpi_test: activating hpi...");
498         hpi_activate();
499         debug("OK.\n");
500
501         while(1) {
502                 led9(1);
503                 debug(" writing to autoinc...\n");
504                 hpi_write_inc(TINY_AUTOINC_BASE_ADDR,
505                               dummy_data, TINY_AUTOINC_DATA_SIZE);
506
507                 debug(" reading from autoinc...\n");
508                 hpi_read_inc(TINY_AUTOINC_BASE_ADDR,
509                              read_data, TINY_AUTOINC_DATA_SIZE);
510
511                 for(i=0; i < (TINY_AUTOINC_DATA_SIZE); i++) {
512                         debug(" written=0x%x, read(inc)=0x%x\n",
513                                dummy_data[i], read_data[i]);
514                 }
515                 led9(0);
516                 udelay(2000000);
517         }
518         return 0;
519 }
520 #endif
521
522 /* test if Host Port Address Register can be written correctly */
523 static int hpi_write_addr_test(u32 addr)
524 {
525         u32 read_back;
526         /* write address */
527         HPI_HPIA_1 = ((u16) (addr >> 16)); /* First HW is most significant */
528         HPI_HPIA_2 = ((u16) addr);
529
530         read_back = (((u32) HPI_HPIA_1)<<16) | ((u32) HPI_HPIA_2);
531
532         if(read_back == addr) {
533                 debug(" hpi_write_addr_test OK: written=0x%x, read=0x%x\n",
534                        addr, read_back);
535                 return 0;
536         } else {
537                 debug(" hpi_write_addr_test *** FAILED ***: written=0x%x, read=0x%x\n",
538                       addr, read_back);
539                 return -1;
540         }
541
542         return 0;
543 }
544
545 /* test if a simple read/write sequence succeeds */
546 static int hpi_read_write_test(u32 addr, u32 data)
547 {
548         u32 read_back;
549
550         hpi_write_noinc(addr, data);
551         read_back = hpi_read_noinc(addr);
552
553         if(read_back == data) {
554                 debug(" hpi_read_write_test: OK, addr=0x%x written=0x%x, read=0x%x\n", addr, data, read_back);
555                 return 0;
556         } else {
557                 debug(" hpi_read_write_test: *** FAILED ***, addr=0x%x written=0x%x, read=0x%x\n", addr, data, read_back);
558                 return -1;
559         }
560
561         return 0;
562 }
563
564 #ifdef DO_TINY_TEST
565 static int hpi_tiny_autoinc_test(void)
566 {
567         int i;
568         u32 read_data[TINY_AUTOINC_DATA_SIZE];
569         u32 read_data_noinc[TINY_AUTOINC_DATA_SIZE];
570
571         unsigned int dummy_data[TINY_AUTOINC_DATA_SIZE] = {
572                 0x11112222, 0x33334444, 0x55556666, 0x77778888,
573                 0x9999aaaa, 0xbbbbcccc, 0xddddeeee, 0xffff1111,
574                 0x00010002, 0x00030004, 0x00050006, 0x00070008,
575                 0x0009000a, 0x000b000c, 0x000d000e, 0x000f0001
576         };
577
578         printf(" writing to autoinc...\n");
579         hpi_write_inc(TINY_AUTOINC_BASE_ADDR, dummy_data, TINY_AUTOINC_DATA_SIZE);
580
581         printf(" reading from autoinc...\n");
582         hpi_read_inc(TINY_AUTOINC_BASE_ADDR, read_data, TINY_AUTOINC_DATA_SIZE);
583
584         printf(" reading from noinc for comparison...\n");
585         for(i=0; i < (TINY_AUTOINC_DATA_SIZE); i++)
586                 read_data_noinc[i] = hpi_read_noinc(TINY_AUTOINC_BASE_ADDR+i*4);
587
588         for(i=0; i < (TINY_AUTOINC_DATA_SIZE); i++) {
589                 printf(" written=0x%x, read(inc)=0x%x, read(noinc)=0x%x\n",
590                        dummy_data[i], read_data[i], read_data_noinc[i]);
591         }
592         return 0;
593 }
594 #endif /* DO_TINY_TEST */
595
596 #endif /* CONFIG_SPC1920_HPI_TEST */