summaryrefslogtreecommitdiff
path: root/examples/standalone/mem_to_mem_idma2intr.c
blob: 15779d0a162fc0de4a6203f0527c6a1e615b8363 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* The dpalloc function used and implemented in this file was derieved
 * from PPCBoot/U-Boot file "cpu/mpc8260/commproc.c".
 */

/* Author: Arun Dharankar <ADharankar@ATTBI.Com>
 * This example is meant to only demonstrate how the IDMA could be used.
 */

/*
 * This file is based on "arch/ppc/8260_io/commproc.c" - here is it's
 * copyright notice:
 *
 * General Purpose functions for the global management of the
 * 8260 Communication Processor Module.
 * Copyright (c) 1999 Dan Malek (dmalek@jlc.net)
 * Copyright (c) 2000 MontaVista Software, Inc (source@mvista.com)
 *  2.3.99 Updates
 *
 * In addition to the individual control of the communication
 * channels, there are a few functions that globally affect the
 * communication processor.
 *
 * Buffer descriptors must be allocated from the dual ported memory
 * space.  The allocator for that is here.  When the communication
 * process is reset, we reclaim the memory available.  There is
 * currently no deallocator for this memory.
 */


#include <common.h>
#include <exports.h>

DECLARE_GLOBAL_DATA_PTR;

#define STANDALONE

#ifndef STANDALONE			/* Linked into/Part of  PPCBoot */
#include <command.h>
#include <watchdog.h>
#else					/* Standalone app of PPCBoot */
#define WATCHDOG_RESET() {						\
			*(ushort *)(CONFIG_SYS_IMMR + 0x1000E) = 0x556c;	\
			*(ushort *)(CONFIG_SYS_IMMR + 0x1000E) = 0xaa39;	\
		}
#endif	/* STANDALONE */

static int debug = 1;

#define DEBUG(fmt, args...)	 {					\
	if(debug != 0) {						\
		printf("[%s %d %s]: ",__FILE__,__LINE__,__FUNCTION__);	\
		printf(fmt, ##args);					\
	}								\
}

#define CPM_CR_IDMA1_SBLOCK  (0x14)
#define CPM_CR_IDMA2_SBLOCK  (0x15)
#define CPM_CR_IDMA3_SBLOCK  (0x16)
#define CPM_CR_IDMA4_SBLOCK  (0x17)
#define CPM_CR_IDMA1_PAGE    (0x07)
#define CPM_CR_IDMA2_PAGE    (0x08)
#define CPM_CR_IDMA3_PAGE    (0x09)
#define CPM_CR_IDMA4_PAGE    (0x0a)
#define PROFF_IDMA1_BASE     ((uint)0x87fe)
#define PROFF_IDMA2_BASE     ((uint)0x88fe)
#define PROFF_IDMA3_BASE     ((uint)0x89fe)
#define PROFF_IDMA4_BASE     ((uint)0x8afe)

#define CPM_CR_INIT_TRX     ((ushort)0x0000)
#define CPM_CR_FLG  ((ushort)0x0001)

#define mk_cr_cmd(PG, SBC, MCN, OP) \
    ((PG << 26) | (SBC << 21) | (MCN << 6) | OP)


#pragma pack(1)
typedef struct ibdbits {
	unsigned b_valid:1;
	unsigned b_resv1:1;
	unsigned b_wrap:1;
	unsigned b_interrupt:1;
	unsigned b_last:1;
	unsigned b_resv2:1;
	unsigned b_cm:1;
	unsigned b_resv3:2;
	unsigned b_sdn:1;
	unsigned b_ddn:1;
	unsigned b_dgbl:1;
	unsigned b_dbo:2;
	unsigned b_resv4:1;
	unsigned b_ddtb:1;
	unsigned b_resv5:2;
	unsigned b_sgbl:1;
	unsigned b_sbo:2;
	unsigned b_resv6:1;
	unsigned b_sdtb:1;
	unsigned b_resv7:9;
} ibdbits_t;

#pragma pack(1)
typedef union ibdbitsu {
	ibdbits_t b;
	uint i;
} ibdbitsu_t;

#pragma pack(1)
typedef struct idma_buf_desc {
	ibdbitsu_t ibd_bits;		/* Status and Control */
	uint ibd_datlen;		/* Data length in buffer */
	uint ibd_sbuf;			/* Source buffer addr in host mem */
	uint ibd_dbuf;			/* Destination buffer addr in host mem */
} ibd_t;


#pragma pack(1)
typedef struct dcmbits {
	unsigned b_fb:1;
	unsigned b_lp:1;
	unsigned b_resv1:3;
	unsigned b_tc2:1;
	unsigned b_resv2:1;
	unsigned b_wrap:3;
	unsigned b_sinc:1;
	unsigned b_dinc:1;
	unsigned b_erm:1;
	unsigned b_dt:1;
	unsigned b_sd:2;
} dcmbits_t;

#pragma pack(1)
typedef union dcmbitsu {
	dcmbits_t b;
	ushort i;
} dcmbitsu_t;

#pragma pack(1)
typedef struct pram_idma {
	ushort pi_ibase;
	dcmbitsu_t pi_dcmbits;
	ushort pi_ibdptr;
	ushort pi_dprbuf;
	ushort pi_bufinv;		/* internal to CPM */
	ushort pi_ssmax;
	ushort pi_dprinptr;		/* internal to CPM */
	ushort pi_sts;
	ushort pi_dproutptr;		/* internal to CPM */
	ushort pi_seob;
	ushort pi_deob;
	ushort pi_dts;
	ushort pi_retadd;
	ushort pi_resv1;		/* internal to CPM */
	uint pi_bdcnt;
	uint pi_sptr;
	uint pi_dptr;
	uint pi_istate;
} pram_idma_t;


volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
volatile ibd_t *bdf;
volatile pram_idma_t *piptr;

volatile int dmadone;
volatile int *dmadonep = &dmadone;
void dmadone_handler (void *);

int idma_init (void);
void idma_start (int, int, int, uint, uint, int);
uint dpalloc (uint, uint);


uint dpinit_done = 0;


#ifdef STANDALONE
int ctrlc (void)
{
	if (tstc()) {
		switch (getc ()) {
		case 0x03:		/* ^C - Control C */
			return 1;
		default:
			break;
		}
	}
	return 0;
}
void * memset(void * s,int c,size_t count)
{
	char *xs = (char *) s;
	while (count--)
		*xs++ = c;
	return s;
}
int memcmp(const void * cs,const void * ct,size_t count)
{
	const unsigned char *su1, *su2;
	int res = 0;
	for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
		if ((res = *su1 - *su2) != 0)
			break;
	return res;
}
#endif	/* STANDALONE */

#ifdef STANDALONE
int mem_to_mem_idma2intr (int argc, char *argv[])
#else
int do_idma (bd_t * bd, int argc, char *argv[])
#endif	/* STANDALONE */
{
	int i;

	app_startup(argv);
	dpinit_done = 0;

	idma_init ();

	DEBUG ("Installing dma handler\n");
	install_hdlr (7, dmadone_handler, (void *) bdf);

	memset ((void *) 0x100000, 'a', 512);
	memset ((void *) 0x200000, 'b', 512);

	for (i = 0; i < 32; i++) {
		printf ("Startin IDMA, iteration=%d\n", i);
		idma_start (1, 1, 512, 0x100000, 0x200000, 3);
	}

	DEBUG ("Uninstalling dma handler\n");
	free_hdlr (7);

	return 0;
}

void
idma_start (int sinc, int dinc, int sz, uint sbuf, uint dbuf, int ttype)
{
	/* ttype is for M-M, M-P, P-M or P-P: not used for now */

	piptr->pi_istate = 0;	/* manual says: clear it before every START_IDMA */
	piptr->pi_dcmbits.b.b_resv1 = 0;

	if (sinc == 1)
		piptr->pi_dcmbits.b.b_sinc = 1;
	else
		piptr->pi_dcmbits.b.b_sinc = 0;

	if (dinc == 1)
		piptr->pi_dcmbits.b.b_dinc = 1;
	else
		piptr->pi_dcmbits.b.b_dinc = 0;

	piptr->pi_dcmbits.b.b_erm = 0;
	piptr->pi_dcmbits.b.b_sd = 0x00;	/* M-M */

	bdf->ibd_sbuf = sbuf;
	bdf->ibd_dbuf = dbuf;
	bdf->ibd_bits.b.b_cm = 0;
	bdf->ibd_bits.b.b_interrupt = 1;
	bdf->ibd_bits.b.b_wrap = 1;
	bdf->ibd_bits.b.b_last = 1;
	bdf->ibd_bits.b.b_sdn = 0;
	bdf->ibd_bits.b.b_ddn = 0;
	bdf->ibd_bits.b.b_dgbl = 0;
	bdf->ibd_bits.b.b_ddtb = 0;
	bdf->ibd_bits.b.b_sgbl = 0;
	bdf->ibd_bits.b.b_sdtb = 0;
	bdf->ibd_bits.b.b_dbo = 1;
	bdf->ibd_bits.b.b_sbo = 1;
	bdf->ibd_bits.b.b_valid = 1;
	bdf->ibd_datlen = 512;

	*dmadonep = 0;

	immap->im_sdma.sdma_idmr2 = (uchar) 0xf;

	immap->im_cpm.cp_cpcr = mk_cr_cmd (CPM_CR_IDMA2_PAGE,
					   CPM_CR_IDMA2_SBLOCK, 0x0,
					   0x9) | 0x00010000;

	while (*dmadonep != 1) {
		if (ctrlc ()) {
			DEBUG ("\nInterrupted waiting for DMA interrupt.\n");
			goto done;
		}
		printf ("Waiting for DMA interrupt (dmadone=%d b_valid = %d)...\n",
			dmadone, bdf->ibd_bits.b.b_valid);
		udelay (1000000);
	}
	printf ("DMA complete notification received!\n");

  done:
	DEBUG ("memcmp(0x%08x, 0x%08x, 512) = %d\n",
		sbuf, dbuf, memcmp ((void *) sbuf, (void *) dbuf, 512));

	return;
}

#define MAX_INT_BUFSZ	64
#define DCM_WRAP	 0	/* MUST be consistant with MAX_INT_BUFSZ */

int idma_init (void)
{
	uint memaddr;

	immap->im_cpm.cp_rccr &= ~0x00F3FFFF;
	immap->im_cpm.cp_rccr |= 0x00A00A00;

	memaddr = dpalloc (sizeof (pram_idma_t), 64);

	*(volatile ushort *) &immap->im_dprambase[PROFF_IDMA2_BASE] = memaddr;
	piptr = (volatile pram_idma_t *) ((uint) (immap) + memaddr);

	piptr->pi_resv1 = 0;		/* manual says: clear it */
	piptr->pi_dcmbits.b.b_fb = 0;
	piptr->pi_dcmbits.b.b_lp = 1;
	piptr->pi_dcmbits.b.b_erm = 0;
	piptr->pi_dcmbits.b.b_dt = 0;

	memaddr = (uint) dpalloc (sizeof (ibd_t), 64);
	piptr->pi_ibase = piptr->pi_ibdptr = (volatile short) memaddr;
	bdf = (volatile ibd_t *) ((uint) (immap) + memaddr);
	bdf->ibd_bits.b.b_valid = 0;

	memaddr = (uint) dpalloc (64, 64);
	piptr->pi_dprbuf = (volatile ushort) memaddr;
	piptr->pi_dcmbits.b.b_wrap = 4;
	piptr->pi_ssmax = 32;

	piptr->pi_sts = piptr->pi_ssmax;
	piptr->pi_dts = piptr->pi_ssmax;

	return 1;
}

void dmadone_handler (void *arg)
{
	immap->im_sdma.sdma_idmr2 = (uchar) 0x0;

	*dmadonep = 1;

	return;
}


static uint dpbase = 0;

uint dpalloc (uint size, uint align)
{
	volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
	uint retloc;
	uint align_mask, off;
	uint savebase;

	/* Pointer to initial global data area */

	if (dpinit_done == 0) {
		dpbase = gd->dp_alloc_base;
		dpinit_done = 1;
	}

	align_mask = align - 1;
	savebase = dpbase;

	if ((off = (dpbase & align_mask)) != 0)
		dpbase += (align - off);

	if ((off = size & align_mask) != 0)
		size += align - off;

	if ((dpbase + size) >= gd->dp_alloc_top) {
		dpbase = savebase;
		printf ("dpalloc: ran out of dual port ram!");
		return 0;
	}

	retloc = dpbase;
	dpbase += size;

	memset ((void *) &immr->im_dprambase[retloc], 0, size);

	return (retloc);
}