File: arch_dma.c

package info (click to toggle)
faumachine 20100527-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 53,836 kB
  • ctags: 20,552
  • sloc: ansic: 179,550; asm: 3,645; makefile: 3,611; perl: 2,103; sh: 1,529; python: 600; xml: 563; lex: 210; vhdl: 204
file content (300 lines) | stat: -rw-r--r-- 7,912 bytes parent folder | download
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
/* $Id: arch_dma.c,v 1.23 2009-01-28 12:59:15 potyra Exp $ 
 *
 * Copyright (C) 2005-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#ifdef STATE

struct {
	unsigned short base_address[4];
	unsigned short base_counter[4];
	unsigned short address[4];
	unsigned short count[4];
	unsigned char status;	/* Not implemented yet. FIXME VOSSI */
	unsigned char command;	/* Not implemented yet. FIXME VOSSI */
	unsigned char temporary;/* Not implemented yet. FIXME VOSSI */
	unsigned char mode[4];
	unsigned char mask;
	unsigned char request;	/* Not implemented yet. FIXME VOSSI */

	unsigned char inout_index;
} NAME;

#endif /* STATE */
#ifdef BEHAVIOR

#define DEBUG_CONTROL_FLOW	0

/*
 * NOTES about DMA transfers:
 *
 *  controller 1: channels 0-3, byte operations, ports 00-1F
 *  controller 2: channels 4-7, word operations, ports C0-DF
 *
 *  - ALL registers are 8 bits only, regardless of transfer size
 *  - channel 4 is not used - cascades 1 into 2.
 *  - channels 0-3 are byte - addresses/counts are for physical bytes
 *  - channels 5-7 are word - addresses/counts are for physical words
 *  - transfers must not cross physical 64K (0-3) or 128K (5-7) boundaries
 *  - transfer count loaded to registers is 1 less than actual count
 *  - controller 2 offsets are all even (2x offsets for controller 1)
 *  - page registers for 5-7 don't use data bit 0, represent 128K pages
 *  - page registers for 0-3 use bit 0, represent 64K pages
 *
 * DMA transfers are limited to the lower 16MB of _physical_ memory.
 * Note that addresses loaded into registers must be _physical_ addresses,
 * not logical addresses (which may differ if paging is active).
 *
 *  Address mapping for channels 0-3:
 *
 *   A23 ... A16 A15 ... A8  A7 ... A0    (Physical addresses)
 *    |  ...  |   |  ... |   |  ... |
 *    |  ...  |   |  ... |   |  ... |
 *    |  ...  |   |  ... |   |  ... |
 *   P7  ...  P0  A7 ... A0  A7 ... A0
 * |    Page    | Addr MSB | Addr LSB |   (DMA registers)
 *
 *  Address mapping for channels 5-7:
 *
 *   A23 ... A17 A16 A15 ... A9 A8 A7 ... A1 A0    (Physical addresses)
 *    |  ...  |   \   \   ... \  \  \  ... \  \
 *    |  ...  |    \   \   ... \  \  \  ... \  (not used)
 *    |  ...  |     \   \   ... \  \  \  ... \
 *   P7  ...  P1 (0) A7 A6  ... A0 A7 A6 ... A0
 * |      Page      |  Addr MSB   |  Addr LSB  |   (DMA registers)
 *
 * Again, channels 5-7 transfer _physical_ words (16 bits), so addresses
 * and counts _must_ be word-aligned (the lowest address bit is _ignored_ at
 * the hardware level, so odd-byte transfers aren't possible).
 *
 * Transfer count (_not # bytes_) is limited to 64K, represented as actual
 * count - 1 : 64K => 0xFFFF, 1 => 0x0000.  Thus, count is always 1 or more,
 * and up to 128K bytes may be transferred on channels 5-7 in one operation.
 *
 */

static void
NAME_(req)(struct cpssp *css, unsigned char chan)
{
	assert(/* 0 <= chan && */ chan < 4);

	switch ((css->NAME.mode[chan] >> 2) & 3) {
	case 0:
		/* Verify transfer. */
		NAME_(ack_verify)(css, chan, css->NAME.count[chan] == 0);
		break;
	case 1:
		/* Write transfer. */
		NAME_(ack_in)(css, css->NAME.address[chan],
				chan, css->NAME.count[chan] == 0);
		break;
	case 2:
		/* Read transfer. */
		NAME_(ack_out)(css, css->NAME.address[chan],
				chan, css->NAME.count[chan] == 0);
		break;
	case 3:
		/* Illegal. */
		assert(0);	/* FIXME VOSSI */
		break;
	default:
		assert(0);	/* Cannot happen. */
	}

	if ((css->NAME.mode[chan] >> 5) & 1) {
		/* Decrement address. */
		css->NAME.address[chan]--;
	} else {
		/* Increment address. */
		css->NAME.address[chan]++;
	}
	css->NAME.count[chan]--;

	if ((css->NAME.mode[chan] >> 4) & 1) {
		/* Autoinit mode. */
		if (css->NAME.count[chan] == 0xffff) {
			css->NAME.count[chan] = css->NAME.base_counter[chan];
			css->NAME.address[chan] = css->NAME.base_address[chan];
		}
	}
}

static void
NAME_(inb)(struct cpssp *css, unsigned char *valuep, unsigned short port)
{
	unsigned char chan;

#if DEBUG_CONTROL_FLOW
	fprintf(stderr, "%s called(port=0x%04x)\n", __FUNCTION__, port);
#endif

	switch (port) {
	case 0x00: /* current address, channel 0 */
	case 0x02: /* current address, channel 1 */
	case 0x04: /* current address, channel 2 */
	case 0x06: /* current address, channel 3 */
		chan = port / 2;
		if (css->NAME.inout_index == 0){
			css->NAME.inout_index++;
			*valuep = css->NAME.address[chan] & 0xff;
		} else {
			css->NAME.inout_index = 0;
			*valuep = css->NAME.address[chan] >> 8 ;
		}
		break;

	case 0x01: /* current count, channel 0 */
	case 0x03: /* current count, channel 1 */
	case 0x05: /* current count, channel 2 */
	case 0x07: /* current count, channel 3 */
		chan = port / 2;
		if (css->NAME.inout_index == 0) {
			css->NAME.inout_index++;
			*valuep = css->NAME.count[chan] & 0xff;
		} else {
			css->NAME.inout_index = 0;
			*valuep = css->NAME.count[chan] >> 8 ;
		}
		break;

	case 0x08:
		/* Read status register. */
		*valuep = css->NAME.status;
		break;

	case 0x09:
	case 0x0a:
	case 0x0b:
	case 0x0c:
		goto write_only;

	case 0x0d:
		/* Read temporary register. */
		*valuep = css->NAME.temporary;
		break;

	case 0x0e:
	case 0x0f:
	write_only:;
		faum_log(FAUM_LOG_WARNING, "DMA", "",
				"Reading write-only register 0x%02x.\n", port);
		*valuep = 0x00;
		break;

	default:
		assert(0);
	}

#if DEBUG_CONTROL_FLOW
	fprintf(stderr, "%s return(value=0x%02x)\n", __FUNCTION__, *valuep);
#endif
}

static void
NAME_(outb)(struct cpssp *css, unsigned char value, unsigned short port)
{
	unsigned char chan;

#if DEBUG_CONTROL_FLOW
	fprintf(stderr, "%s called(value=0x%02x, port=0x%04x)\n", __FUNCTION__,
			value, port);
#endif

	switch (port) {
	case 0x00: /* current address, channel 0 */
	case 0x02: /* current address, channel 1 */
	case 0x04: /* current address, channel 2 */
	case 0x06: /* current address, channel 3 */
		chan = port / 2;
		if (css->NAME.inout_index == 0) {
			css->NAME.inout_index++;
			css->NAME.address[chan] = value;
		} else {
			css->NAME.inout_index = 0;
			css->NAME.address[chan] |= ((unsigned short) value) << 8;
			css->NAME.base_address[chan] = css->NAME.address[chan];
		}
		break;
	case 0x01: /* current count, channel 0 */
	case 0x03: /* current count, channel 1 */
	case 0x05: /* current count, channel 2 */
	case 0x07: /* current count, channel 3 */
		chan = port / 2;
		if (css->NAME.inout_index == 0) {
			css->NAME.inout_index++;
			css->NAME.count[chan] = value;
		} else {
			css->NAME.inout_index = 0;
			css->NAME.count[chan] |= ((unsigned short) value) << 8;
			css->NAME.base_counter[chan] = css->NAME.count[chan];
		}
		break;

	case 0x08:
		/* Write command register. */
		css->NAME.command = value;
		break;

	case 0x09:
		/* Write single request register bit. */
		chan = value & 3;
		css->NAME.request &= ~(1 << chan);
		css->NAME.request |= ((value >> 2) & 1) << chan;
		break;

	case 0x0a:
		/* Write single mask register bit. */
		chan = value & 3;
		css->NAME.mask &= ~(1 << chan);
		css->NAME.mask |= ((value >> 2) & 1) << chan;
		break;

	case 0x0b:
		/* Write mode register. */
		chan = value & 3;
		css->NAME.mode[chan] = value & ~3;
		break;

	case 0x0c:
		/* Clear byte pointer flip/flop. */
		css->NAME.inout_index = 0;
		break;
	
	case 0x0d:
		/* Master clear (reset). */
		faum_log(FAUM_LOG_WARNING, "DMA", "",
				"Reset called (not implemented yet).\n");
		break;
	
	case 0x0e:
		/* Clear mask register. */
		css->NAME.mask = 0;
		break;
	
	case 0x0f:
		/* Write all mask register bits. */
		css->NAME.mask = value & 0xf;
		break;

	default:
		assert(0);
	}
}

static void
NAME_(reset)(struct cpssp *css)
{
}

void
NAME_(init)(struct cpssp *css)
{
	/* Nothing to do... */
}

#undef DEBUG_CONTROL_FLOW

#endif /* BEHAVIOR */