summaryrefslogtreecommitdiff
path: root/os/sa1110/dma.c
blob: c52e28bb4a4cc633cdf0a69ca8c230b4b0ad92f0 (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
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"
#include	"io.h"

enum {
	 /* DMA CSR bits */
	CSRrun=		1 << 0,
	CSRie=		1 << 1,
	CSRerror=	1 << 2,
	CSRdonea=	1 << 3,
	CSRstrta=	1 << 4,
	CSRdoneb=	1 << 5,
	CSRstrtb=	1 << 6,
	CSRbiu=		1 << 7,

	Ndma=	6,		/* number of dma channels */
};

/* DDAR configuration: DA 31:8, DS 3:0, data width, burst size */
#define	DMACFG(da, ds, dw, bs) (((da)<<8)|((ds)<<4)|((dw)<<3)|((bs)<<2))

static ulong dmaconfig[16] = {
[DmaUDC] 	DMACFG(0x80000A, 0, 0, 1),
[DmaUART0]	DMACFG(0x804005, 4, 0, 0),
[DmaHSSP]	DMACFG(0x81001B, 6, 0, 1),
[DmaUART1]	DMACFG(0x80C005, 6, 0, 0),
[DmaUART2]	DMACFG(0x814005, 8, 0, 0),
[DmaMCPaudio] DMACFG(0x818002, 10, 1, 1),
[DmaMCPtelecom] DMACFG(0x818003, 12, 1, 1),
[DmaSSP]		DMACFG(0x81C01B, 14, 1, 0),	/* see SSP description not DMA section for correct burst size */
};

struct Dma {
	int	chan;
	DmaReg*	reg;
	void	(*interrupt)(void*, ulong);
	void*	arg;
	Rendez	r;
	int	intrset;
};

static struct {
	Lock;
	int	avail;
	Dma	dma[Ndma];
} dmachans;

static	void	dmaintr(Ureg*, void*);

void
dmareset(void)
{
	int i;
	Dma *d;

	for(i=0; i<nelem(dmachans.dma); i++){
		dmachans.avail |= 1<<i;
		d = &dmachans.dma[i];
		d->chan = i;
		d->reg = DMAREG(i);
		d->reg->dcsr_c = 0xFF;
	}
	/* this is the place to mask off bits in avail corresponding to broken channels in old revisions */
}

/*
 * allocate a DMA channel, reset it, and configure it for the given device
 */
Dma*
dmasetup(int device, int direction, int bigend, void (*interrupt)(void*, ulong), void *arg)
{
	Dma *d;
	DmaReg *dr;
	ulong cfg;
	int i;
	char name[KNAMELEN];

	cfg = dmaconfig[device];
	if(cfg == 0){
		print("dmasetup: no device %d\n", device);
		return nil;
	}

	ilock(&dmachans);
	for(i=0; (dmachans.avail & (1<<i)) == 0; i++)
		if(i >= nelem(dmachans.dma)){
			iunlock(&dmachans);
			return nil;
		}
	dmachans.avail &= ~(1<<i);
	iunlock(&dmachans);

	d = &dmachans.dma[i];
	d->interrupt = interrupt;
	d->arg = arg;
	dr = d->reg;
	dr->dcsr_c = CSRrun | CSRie | CSRerror | CSRdonea | CSRstrta | CSRdoneb | CSRstrtb;
	dr->ddar = cfg | (direction<<4) | (bigend<<1);
	if(d->intrset == 0){
		d->intrset = 1;
		snprint(name, sizeof(name), "dma%d", i);
		intrenable(DMAbit(i), dmaintr, d, BusCPU, name);
	}
	return d;
}

void
dmafree(Dma *dma)
{
	dma->reg->dcsr_c = CSRrun | CSRie;
	ilock(&dmachans);
	dmachans.avail |= 1<<dma->chan;
	dma->interrupt = nil;
	iunlock(&dmachans);
}

/*
 * start dma on the given channel on one or two buffers,
 * each of which must adhere to DMA controller restrictions.
 * (eg, on some versions of the StrongArm it musn't span 256-byte boundaries).
 * virtual buffer addresses are assumed to refer to contiguous physical addresses.
 */
int
dmastart(Dma *dma, void *buf, int nbytes)
{
	ulong v, csr;
	DmaReg *dr;
	int b;

	dr = dma->reg;
	v = dr->dcsr;
	if((v & (CSRstrta|CSRstrtb|CSRrun)) == (CSRstrta|CSRstrtb|CSRrun))
		return 0;	/* fully occupied */

	dcflush(buf, nbytes);

	csr = CSRrun | CSRie;

	/* start first xfer with buffer B or A? */
	b = (v & CSRbiu) != 0 && (v & CSRstrtb) == 0 || (v & CSRstrta) != 0;
	if(b)
		csr |= CSRstrtb;
	else
		csr |= CSRstrta;

	if(v & csr & (CSRstrtb|CSRstrta))
		panic("dmasetup csr=%2.2lux %2.2lux", v, csr);

	 /* set first src/dst and size */
	dr->buf[b].start = (ulong)buf;
	dr->buf[b].count = nbytes;
	dr->dcsr_s = csr;
	return 1;
}

/*
 * stop dma on a channel
 */
void
dmastop(Dma *dma)
{
	// print("dmastop (was %ux)\n", dma->reg->dcsr);

	dma->reg->dcsr_c =	CSRrun |
					CSRie |
					CSRerror |
					CSRdonea |
					CSRstrta |
					CSRdoneb |
					CSRstrtb;
}

/*
 * return nonzero if there was a memory error during DMA,
 * and clear the error state
 */
int
dmaerror(Dma *dma)
{
	DmaReg *dr;
	ulong e;

	dr = dma->reg;
	e = dr->dcsr & CSRerror;
	dr->dcsr_c = e;
	return e;
}

/*
 * return nonzero if the DMA channel is not busy
 */
int
dmaidle(Dma *d)
{
	return (d->reg->dcsr & (CSRstrta|CSRstrtb)) == 0;
}

static int
dmaidlep(void *a)
{
	return dmaidle((Dma*)a);
}

void
dmawait(Dma *d)
{
	while(!dmaidle(d))
		sleep(&d->r, dmaidlep, d);
}

/*
 * this interface really only copes with one buffer at once
 */
static void
dmaintr(Ureg*, void *a)
{
	Dma *d;
	ulong s;

	d = (Dma*)a;
	s = d->reg->dcsr;
	if(s & CSRerror)
		iprint("DMA error, chan %d status #%2.2lux\n", d->chan, s);
	s &= (CSRdonea|CSRdoneb|CSRerror);
	d->reg->dcsr_c = s;
	if(d->interrupt != nil)
		d->interrupt(d->arg, s & (CSRdonea|CSRdoneb));
	wakeup(&d->r);
}