]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - examples/mem_to_mem_idma2intr.c
Fix SDRAM timing on Purple board
[karo-tx-uboot.git] / examples / mem_to_mem_idma2intr.c
1 /* The dpalloc function used and implemented in this file was derieved
2  * from PPCBoot/U-Boot file "cpu/mpc8260/commproc.c".
3  */
4
5 /* Author: Arun Dharankar <ADharankar@ATTBI.Com>
6  * This example is meant to only demonstrate how the IDMA could be used.
7  */
8
9 /*
10  * This file is based on "arch/ppc/8260_io/commproc.c" - here is it's
11  * copyright notice:
12  *
13  * General Purpose functions for the global management of the
14  * 8260 Communication Processor Module.
15  * Copyright (c) 1999 Dan Malek (dmalek@jlc.net)
16  * Copyright (c) 2000 MontaVista Software, Inc (source@mvista.com)
17  *  2.3.99 Updates
18  *
19  * In addition to the individual control of the communication
20  * channels, there are a few functions that globally affect the
21  * communication processor.
22  *
23  * Buffer descriptors must be allocated from the dual ported memory
24  * space.  The allocator for that is here.  When the communication
25  * process is reset, we reclaim the memory available.  There is
26  * currently no deallocator for this memory.
27  */
28
29
30
31 #include <common.h>
32 #include <syscall.h>
33
34 #define STANDALONE
35
36 #ifndef STANDALONE                      /* Linked into/Part of  PPCBoot */
37 #include <command.h>
38 #include <watchdog.h>
39 #else                                   /* Standalone app of PPCBoot */
40 #include <syscall.h>
41 #define printf  mon_printf
42 #define tstc    mon_tstc
43 #define getc    mon_getc
44 #define putc    mon_putc
45 #define udelay  mon_udelay
46 #define malloc  mon_malloc
47 #define WATCHDOG_RESET() {                                              \
48                         *(ushort *)(CFG_IMMR + 0x1000E) = 0x556c;       \
49                         *(ushort *)(CFG_IMMR + 0x1000E) = 0xaa39;       \
50                 }
51 #endif  /* STANDALONE */
52
53 static int debug = 1;
54
55 #define DEBUG(fmt, args...)      {                                      \
56         if(debug != 0) {                                                \
57                 printf("[%s %d %s]: ",__FILE__,__LINE__,__FUNCTION__);  \
58                 printf(fmt, ##args);                                    \
59         }                                                               \
60 }
61
62 #define CPM_CR_IDMA1_SBLOCK  (0x14)
63 #define CPM_CR_IDMA2_SBLOCK  (0x15)
64 #define CPM_CR_IDMA3_SBLOCK  (0x16)
65 #define CPM_CR_IDMA4_SBLOCK  (0x17)
66 #define CPM_CR_IDMA1_PAGE    (0x07)
67 #define CPM_CR_IDMA2_PAGE    (0x08)
68 #define CPM_CR_IDMA3_PAGE    (0x09)
69 #define CPM_CR_IDMA4_PAGE    (0x0a)
70 #define PROFF_IDMA1_BASE     ((uint)0x87fe)
71 #define PROFF_IDMA2_BASE     ((uint)0x88fe)
72 #define PROFF_IDMA3_BASE     ((uint)0x89fe)
73 #define PROFF_IDMA4_BASE     ((uint)0x8afe)
74
75 #define CPM_CR_INIT_TRX     ((ushort)0x0000)
76 #define CPM_CR_FLG  ((ushort)0x0001)
77
78 #define mk_cr_cmd(PG, SBC, MCN, OP) \
79     ((PG << 26) | (SBC << 21) | (MCN << 6) | OP)
80
81
82 #pragma pack(1)
83 typedef struct ibdbits {
84         unsigned b_valid:1;
85         unsigned b_resv1:1;
86         unsigned b_wrap:1;
87         unsigned b_interrupt:1;
88         unsigned b_last:1;
89         unsigned b_resv2:1;
90         unsigned b_cm:1;
91         unsigned b_resv3:2;
92         unsigned b_sdn:1;
93         unsigned b_ddn:1;
94         unsigned b_dgbl:1;
95         unsigned b_dbo:2;
96         unsigned b_resv4:1;
97         unsigned b_ddtb:1;
98         unsigned b_resv5:2;
99         unsigned b_sgbl:1;
100         unsigned b_sbo:2;
101         unsigned b_resv6:1;
102         unsigned b_sdtb:1;
103         unsigned b_resv7:9;
104 } ibdbits_t;
105
106 #pragma pack(1)
107 typedef union ibdbitsu {
108         ibdbits_t b;
109         uint i;
110 } ibdbitsu_t;
111
112 #pragma pack(1)
113 typedef struct idma_buf_desc {
114         ibdbitsu_t ibd_bits;            /* Status and Control */
115         uint ibd_datlen;                /* Data length in buffer */
116         uint ibd_sbuf;                  /* Source buffer addr in host mem */
117         uint ibd_dbuf;                  /* Destination buffer addr in host mem */
118 } ibd_t;
119
120
121 #pragma pack(1)
122 typedef struct dcmbits {
123         unsigned b_fb:1;
124         unsigned b_lp:1;
125         unsigned b_resv1:3;
126         unsigned b_tc2:1;
127         unsigned b_resv2:1;
128         unsigned b_wrap:3;
129         unsigned b_sinc:1;
130         unsigned b_dinc:1;
131         unsigned b_erm:1;
132         unsigned b_dt:1;
133         unsigned b_sd:2;
134 } dcmbits_t;
135
136 #pragma pack(1)
137 typedef union dcmbitsu {
138         dcmbits_t b;
139         ushort i;
140 } dcmbitsu_t;
141
142 #pragma pack(1)
143 typedef struct pram_idma {
144         ushort pi_ibase;
145         dcmbitsu_t pi_dcmbits;
146         ushort pi_ibdptr;
147         ushort pi_dprbuf;
148         ushort pi_bufinv;               /* internal to CPM */
149         ushort pi_ssmax;
150         ushort pi_dprinptr;             /* internal to CPM */
151         ushort pi_sts;
152         ushort pi_dproutptr;            /* internal to CPM */
153         ushort pi_seob;
154         ushort pi_deob;
155         ushort pi_dts;
156         ushort pi_retadd;
157         ushort pi_resv1;                /* internal to CPM */
158         uint pi_bdcnt;
159         uint pi_sptr;
160         uint pi_dptr;
161         uint pi_istate;
162 } pram_idma_t;
163
164
165 volatile immap_t *immap = (immap_t *) CFG_IMMR;
166 volatile ibd_t *bdf;
167 volatile pram_idma_t *piptr;
168
169 volatile int dmadone;
170 volatile int *dmadonep = &dmadone;
171 void dmadone_handler (void *);
172
173 int idma_init (void);
174 void idma_start (int, int, int, uint, uint, int);
175 uint dpalloc (uint, uint);
176
177
178 uint dpinit_done = 0;
179
180
181 #ifdef STANDALONE
182 int ctrlc (void)
183 {
184         if (mon_tstc()) {
185                 switch (mon_getc ()) {
186                 case 0x03:              /* ^C - Control C */
187                         return 1;
188                 default:
189                         break;
190                 }
191         }
192         return 0;
193 }
194 void * memset(void * s,int c,size_t count)
195 {
196         char *xs = (char *) s;
197         while (count--)
198                 *xs++ = c;
199         return s;
200 }
201 int memcmp(const void * cs,const void * ct,size_t count)
202 {
203         const unsigned char *su1, *su2;
204         int res = 0;
205         for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
206                 if ((res = *su1 - *su2) != 0)
207                         break;
208         return res;
209 }
210 #endif  /* STANDALONE */
211
212 #ifdef STANDALONE
213 int mem_to_mem_idma2intr (bd_t * bd, int argc, char *argv[])
214 #else
215 int do_idma (bd_t * bd, int argc, char *argv[])
216 #endif  /* STANDALONE */
217 {
218         int i;
219
220         dpinit_done = 0;
221
222         idma_init ();
223
224         DEBUG ("Installing dma handler\n");
225         mon_install_hdlr (7, dmadone_handler, (void *) bdf);
226
227         memset ((void *) 0x100000, 'a', 512);
228         memset ((void *) 0x200000, 'b', 512);
229
230         for (i = 0; i < 32; i++) {
231                 printf ("Startin IDMA, iteration=%d\n", i);
232                 idma_start (1, 1, 512, 0x100000, 0x200000, 3);
233         }
234
235         DEBUG ("Uninstalling dma handler\n");
236         mon_free_hdlr (7);
237
238         return 0;
239 }
240
241 void
242 idma_start (int sinc, int dinc, int sz, uint sbuf, uint dbuf, int ttype)
243 {
244         /* ttype is for M-M, M-P, P-M or P-P: not used for now */
245
246         piptr->pi_istate = 0;   /* manual says: clear it before every START_IDMA */
247         piptr->pi_dcmbits.b.b_resv1 = 0;
248
249         if (sinc == 1)
250                 piptr->pi_dcmbits.b.b_sinc = 1;
251         else
252                 piptr->pi_dcmbits.b.b_sinc = 0;
253
254         if (dinc == 1)
255                 piptr->pi_dcmbits.b.b_dinc = 1;
256         else
257                 piptr->pi_dcmbits.b.b_dinc = 0;
258
259         piptr->pi_dcmbits.b.b_erm = 0;
260         piptr->pi_dcmbits.b.b_sd = 0x00;        /* M-M */
261
262         bdf->ibd_sbuf = sbuf;
263         bdf->ibd_dbuf = dbuf;
264         bdf->ibd_bits.b.b_cm = 0;
265         bdf->ibd_bits.b.b_interrupt = 1;
266         bdf->ibd_bits.b.b_wrap = 1;
267         bdf->ibd_bits.b.b_last = 1;
268         bdf->ibd_bits.b.b_sdn = 0;
269         bdf->ibd_bits.b.b_ddn = 0;
270         bdf->ibd_bits.b.b_dgbl = 0;
271         bdf->ibd_bits.b.b_ddtb = 0;
272         bdf->ibd_bits.b.b_sgbl = 0;
273         bdf->ibd_bits.b.b_sdtb = 0;
274         bdf->ibd_bits.b.b_dbo = 1;
275         bdf->ibd_bits.b.b_sbo = 1;
276         bdf->ibd_bits.b.b_valid = 1;
277         bdf->ibd_datlen = 512;
278
279         *dmadonep = 0;
280
281         immap->im_sdma.sdma_idmr2 = (uchar) 0xf;
282
283         immap->im_cpm.cp_cpcr = mk_cr_cmd (CPM_CR_IDMA2_PAGE,
284                                            CPM_CR_IDMA2_SBLOCK, 0x0,
285                                            0x9) | 0x00010000;
286
287         while (*dmadonep != 1) {
288                 if (ctrlc ()) {
289                         DEBUG ("\nInterrupted waiting for DMA interrupt.\n");
290                         goto done;
291                 }
292                 printf ("Waiting for DMA interrupt (dmadone=%d b_valid = %d)...\n",
293                         dmadone, bdf->ibd_bits.b.b_valid);
294                 udelay (1000000);
295         }
296         printf ("DMA complete notification received!\n");
297
298   done:
299         DEBUG ("memcmp(0x%08x, 0x%08x, 512) = %d\n",
300                 sbuf, dbuf, memcmp ((void *) sbuf, (void *) dbuf, 512));
301
302         return;
303 }
304
305 #define MAX_INT_BUFSZ   64
306 #define DCM_WRAP         0      /* MUST be consistant with MAX_INT_BUFSZ */
307
308 int idma_init (void)
309 {
310         uint memaddr;
311
312         immap->im_cpm.cp_rccr &= ~0x00F3FFFF;
313         immap->im_cpm.cp_rccr |= 0x00A00A00;
314
315         memaddr = dpalloc (sizeof (pram_idma_t), 64);
316
317         *(volatile ushort *) &immap->im_dprambase[PROFF_IDMA2_BASE] = memaddr;
318         piptr = (volatile pram_idma_t *) ((uint) (immap) + memaddr);
319
320         piptr->pi_resv1 = 0;            /* manual says: clear it */
321         piptr->pi_dcmbits.b.b_fb = 0;
322         piptr->pi_dcmbits.b.b_lp = 1;
323         piptr->pi_dcmbits.b.b_erm = 0;
324         piptr->pi_dcmbits.b.b_dt = 0;
325
326         memaddr = (uint) dpalloc (sizeof (ibd_t), 64);
327         piptr->pi_ibase = piptr->pi_ibdptr = (volatile short) memaddr;
328         bdf = (volatile ibd_t *) ((uint) (immap) + memaddr);
329         bdf->ibd_bits.b.b_valid = 0;
330
331         memaddr = (uint) dpalloc (64, 64);
332         piptr->pi_dprbuf = (volatile ushort) memaddr;
333         piptr->pi_dcmbits.b.b_wrap = 4;
334         piptr->pi_ssmax = 32;
335
336         piptr->pi_sts = piptr->pi_ssmax;
337         piptr->pi_dts = piptr->pi_ssmax;
338
339         return 1;
340 }
341
342 void dmadone_handler (void *arg)
343 {
344         immap->im_sdma.sdma_idmr2 = (uchar) 0x0;
345
346         *dmadonep = 1;
347
348         return;
349 }
350
351
352 static uint dpbase = 0;
353
354 uint dpalloc (uint size, uint align)
355 {
356         DECLARE_GLOBAL_DATA_PTR;
357
358         volatile immap_t *immr = (immap_t *) CFG_IMMR;
359         uint retloc;
360         uint align_mask, off;
361         uint savebase;
362
363         /* Pointer to initial global data area */
364
365         if (dpinit_done == 0) {
366                 dpbase = gd->dp_alloc_base;
367                 dpinit_done = 1;
368         }
369
370         align_mask = align - 1;
371         savebase = dpbase;
372
373         if ((off = (dpbase & align_mask)) != 0)
374                 dpbase += (align - off);
375
376         if ((off = size & align_mask) != 0)
377                 size += align - off;
378
379         if ((dpbase + size) >= gd->dp_alloc_top) {
380                 dpbase = savebase;
381                 printf ("dpalloc: ran out of dual port ram!");
382                 return 0;
383         }
384
385         retloc = dpbase;
386         dpbase += size;
387
388         memset ((void *) &immr->im_dprambase[retloc], 0, size);
389
390         return (retloc);
391 }