source: mainline/uspace/drv/bus/isa/i8237.c@ 3de67b4c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3de67b4c was 3e6a98c5, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Standards-compliant boolean type.

  • Property mode set to 100644
File size: 11.0 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup isa
30 * @{
31 */
32
33/** @file
34 * @brief DMA controller management
35 */
36
37#include <assert.h>
38#include <stdbool.h>
39#include <errno.h>
40#include <fibril_synch.h>
41#include <ddi.h>
42#include <ddf/log.h>
43#include "i8237.h"
44
45/** DMA Slave controller I/O Address. */
46#define DMA_CONTROLLER_FIRST_BASE ((void *) 0x00)
47
48/** DMA Master controller I/O Address. */
49#define DMA_CONTROLLER_SECOND_BASE ((void *) 0xc0)
50
51/** Shared DMA page address register I/O address. */
52#define DMA_CONTROLLER_PAGE_BASE ((void *) 0x81)
53
54#define DMA_STATUS_REQ(x) (1 << (((x) % 4) + 4))
55#define DMA_STATUS_COMPLETE(x) (1 << ((x) % 4))
56
57/** http://wiki.osdev.org/DMA: The only bit that works is COND (bit 2) */
58#define DMA_COMMAND_COND (1 << 2) /**< Disables DMA controller */
59
60#define DMA_SINGLE_MASK_CHAN_SEL_MASK 0x03
61#define DMA_SINGLE_MASK_CHAN_SEL_SHIFT 0
62
63#define DMA_SINGLE_MASK_CHAN_TO_REG(x) \
64 (((x) & DMA_SINGLE_MASK_CHAN_SEL_MASK) << DMA_SINGLE_MASK_CHAN_SEL_SHIFT)
65
66#define DMA_SINGLE_MASK_MASKED_FLAG (1 << 2)
67
68#define DMA_MODE_CHAN_SELECT_MASK 0x03
69#define DMA_MODE_CHAN_SELECT_SHIFT 0
70
71#define DMA_MODE_CHAN_TO_REG(x) \
72 (((x) & DMA_MODE_CHAN_SELECT_MASK) << DMA_MODE_CHAN_SELECT_SHIFT)
73
74#define DMA_MODE_CHAN_TRA_MASK 0x03
75#define DMA_MODE_CHAN_TRA_SHIFT 2
76#define DMA_MODE_CHAN_TRA_SELF_TEST 0
77#define DMA_MODE_CHAN_TRA_WRITE 0x01
78#define DMA_MODE_CHAN_TRA_READ 0x02
79
80#define DMA_MODE_CHAN_AUTO_FLAG (1 << 4)
81#define DMA_MODE_CHAN_DOWN_FLAG (1 << 5)
82
83#define DMA_MODE_CHAN_MODE_MASK 0x03
84#define DMA_MODE_CHAN_MODE_SHIFT 6
85#define DMA_MODE_CHAN_MODE_DEMAND 0
86#define DMA_MODE_CHAN_MODE_SINGLE 1
87#define DMA_MODE_CHAN_MODE_BLOCK 2
88#define DMA_MODE_CHAN_MODE_CASCADE 3
89
90#define DMA_MULTI_MASK_CHAN(x) (1 << ((x) % 4))
91
92typedef struct {
93 uint8_t channel_start0;
94 uint8_t channel_count0;
95 uint8_t channel_start1;
96 uint8_t channel_count1;
97 uint8_t channel_start2;
98 uint8_t channel_count2;
99 uint8_t channel_start3;
100 uint8_t channel_count3;
101
102 uint8_t command_status;
103
104 /** Memory to memory transfers, NOT implemented on PCs */
105 uint8_t request;
106 uint8_t single_mask;
107 uint8_t mode;
108 uint8_t flip_flop;
109
110 /*
111 * Master reset sets Flip-Flop low, clears status,
112 * sets all mask bits on.
113 *
114 * Intermediate is not implemented on PCs.
115 *
116 */
117 uint8_t master_reset;
118 uint8_t mask_reset;
119 uint8_t multi_mask;
120} dma_controller_regs_first_t;
121
122typedef struct {
123 uint8_t channel_start4;
124 uint8_t reserved0;
125 uint8_t channel_count4;
126 uint8_t reserved1;
127 uint8_t channel_start5;
128 uint8_t reserved2;
129 uint8_t channel_count5;
130 uint8_t reserved3;
131 uint8_t channel_start6;
132 uint8_t reserved4;
133 uint8_t channel_count6;
134 uint8_t reserved5;
135 uint8_t channel_start7;
136 uint8_t reserved6;
137 uint8_t channel_count7;
138
139 uint8_t command_status;
140 uint8_t reserved8;
141 uint8_t request;
142 uint8_t reserved9;
143 uint8_t single_mask;
144 uint8_t reserveda;
145 uint8_t mode;
146 uint8_t reservedb;
147 uint8_t flip_flop;
148 uint8_t reservedc;
149 uint8_t master_reset;
150 uint8_t reservedd;
151 uint8_t multi_mask;
152} dma_controller_regs_second_t;
153
154typedef struct {
155 uint8_t channel2;
156 uint8_t channel3;
157 uint8_t channel1;
158 uint8_t reserved0;
159 uint8_t reserved1;
160 uint8_t reserved2;
161 uint8_t channel0;
162 uint8_t reserved3;
163 uint8_t channel6;
164 uint8_t channel7;
165 uint8_t channel5;
166 uint8_t reserved4;
167 uint8_t reserved5;
168 uint8_t reserved6;
169 uint8_t channel4;
170} dma_page_regs_t;
171
172/** Addresses needed to setup a DMA channel. */
173typedef struct {
174 ioport8_t *offset_reg_address;
175 ioport8_t *size_reg_address;
176 ioport8_t *page_reg_address;
177 ioport8_t *single_mask_address;
178 ioport8_t *mode_address;
179 ioport8_t *flip_flop_address;
180} dma_channel_t;
181
182typedef struct {
183 dma_channel_t channels[8];
184 dma_page_regs_t *page_table;
185 dma_controller_regs_first_t *first;
186 dma_controller_regs_second_t *second;
187 bool initialized;
188} dma_controller_t;
189
190static fibril_mutex_t guard = FIBRIL_MUTEX_INITIALIZER(guard);
191
192/** Standard i8237 DMA controller.
193 *
194 * http://zet.aluzina.org/index.php/8237_DMA_controller#DMA_Channel_Registers
195 *
196 */
197static dma_controller_t controller_8237 = {
198 .channels = {
199 /* The first chip 8-bit */
200 {
201 (uint8_t *) 0x00,
202 (uint8_t *) 0x01,
203 (uint8_t *) 0x87,
204 (uint8_t *) 0x0a,
205 (uint8_t *) 0x0b,
206 (uint8_t *) 0x0c,
207 },
208 {
209 (uint8_t *) 0x02,
210 (uint8_t *) 0x03,
211 (uint8_t *) 0x83,
212 (uint8_t *) 0x0a,
213 (uint8_t *) 0x0b,
214 (uint8_t *) 0x0c,
215 },
216 {
217 (uint8_t *) 0x04,
218 (uint8_t *) 0x05,
219 (uint8_t *) 0x81,
220 (uint8_t *) 0x0a,
221 (uint8_t *) 0x0b,
222 (uint8_t *) 0x0c,
223 },
224 {
225 (uint8_t *) 0x06,
226 (uint8_t *) 0x07,
227 (uint8_t *) 0x82,
228 (uint8_t *) 0x0a,
229 (uint8_t *) 0x0b,
230 (uint8_t *) 0x0c,
231 },
232
233 /* The second chip 16-bit */
234 {
235 (uint8_t *) 0xc0,
236 (uint8_t *) 0xc2,
237 (uint8_t *) 0x8f,
238 (uint8_t *) 0xd4,
239 (uint8_t *) 0xd6,
240 (uint8_t *) 0xd8,
241 },
242 {
243 (uint8_t *) 0xc4,
244 (uint8_t *) 0xc6,
245 (uint8_t *) 0x8b,
246 (uint8_t *) 0xd4,
247 (uint8_t *) 0xd6,
248 (uint8_t *) 0xd8,
249 },
250 {
251 (uint8_t *) 0xc8,
252 (uint8_t *) 0xca,
253 (uint8_t *) 0x89,
254 (uint8_t *) 0xd4,
255 (uint8_t *) 0xd6,
256 (uint8_t *) 0xd8,
257 },
258 {
259 (uint8_t *) 0xcc,
260 (uint8_t *) 0xce,
261 (uint8_t *) 0x8a,
262 (uint8_t *) 0xd4,
263 (uint8_t *) 0xd6,
264 (uint8_t *) 0xd8,
265 },
266 },
267
268 .page_table = NULL,
269 .first = NULL,
270 .second = NULL,
271 .initialized = false,
272};
273
274/* Initialize I/O access to DMA controller I/O ports.
275 *
276 * @param controller DMA Controller structure to initialize.
277 *
278 * @return Error code.
279 *
280 */
281static inline int dma_controller_init(dma_controller_t *controller)
282{
283 assert(controller);
284 int ret = pio_enable(DMA_CONTROLLER_PAGE_BASE, sizeof(dma_page_regs_t),
285 (void **) &controller->page_table);
286 if (ret != EOK)
287 return EIO;
288
289 ret = pio_enable(DMA_CONTROLLER_FIRST_BASE,
290 sizeof(dma_controller_regs_first_t), (void **) &controller->first);
291 if (ret != EOK)
292 return EIO;
293
294 ret = pio_enable(DMA_CONTROLLER_SECOND_BASE,
295 sizeof(dma_controller_regs_second_t), (void **) &controller->second);
296 if (ret != EOK)
297 return EIO;
298
299 controller->initialized = true;
300
301 /* Reset the controller */
302 pio_write_8(&controller->second->master_reset, 0xff);
303 pio_write_8(&controller->first->master_reset, 0xff);
304
305 return EOK;
306}
307
308/** Setup DMA channel to specified place and mode.
309 *
310 * @param channel DMA Channel 1, 2, 3 for 8 bit transfers,
311 * 5, 6, 7 for 16 bit.
312 * @param pa Physical address of the buffer. Must be < 16 MB
313 * for 16 bit and < 1 MB for 8 bit transfers.
314 * @param size DMA buffer size, limited to 64 KB.
315 * @param mode Mode of the DMA channel:
316 * - Read or Write
317 * - Allow automatic reset
318 * - Use address decrement instead of increment
319 * - Use SINGLE/BLOCK/ON DEMAND transfer mode
320 *
321 * @return Error code.
322 *
323 */
324int dma_setup_channel(unsigned int channel, uint32_t pa, uint16_t size,
325 uint8_t mode)
326{
327 if ((channel == 0) || (channel == 4))
328 return ENOTSUP;
329
330 if (channel > 7)
331 return ENOENT;
332
333 /* DMA is limited to 24bit addresses. */
334 if (pa >= (1 << 24))
335 return EINVAL;
336
337 /* 8 bit channels use only 4 bits from the page register. */
338 if ((channel > 0) && (channel < 4) && (pa >= (1 << 20)))
339 return EINVAL;
340
341 fibril_mutex_lock(&guard);
342
343 if (!controller_8237.initialized)
344 dma_controller_init(&controller_8237);
345
346 if (!controller_8237.initialized) {
347 fibril_mutex_unlock(&guard);
348 return EIO;
349 }
350
351 /* 16 bit transfers are a bit special */
352 ddf_msg(LVL_DEBUG, "Unspoiled address %#" PRIx32 " (size %" PRIu16 ")",
353 pa, size);
354 if (channel > 4) {
355 /* Size must be aligned to 16 bits */
356 if ((size & 1) != 0) {
357 fibril_mutex_unlock(&guard);
358 return EINVAL;
359 }
360
361 size >>= 1;
362
363 /* Address is fun: lower 16 bits need to be shifted by 1 */
364 pa = ((pa & 0xffff) >> 1) | (pa & 0xff0000);
365 }
366
367 const dma_channel_t dma_channel = controller_8237.channels[channel];
368
369 ddf_msg(LVL_DEBUG, "Setting channel %u to address %#" PRIx32 " "
370 "(size %" PRIu16 "), mode %hhx.", channel, pa, size, mode);
371
372 /* Mask DMA request */
373 uint8_t value = DMA_SINGLE_MASK_CHAN_TO_REG(channel) |
374 DMA_SINGLE_MASK_MASKED_FLAG;
375 pio_write_8(dma_channel.single_mask_address, value);
376
377 /* Set mode */
378 value = DMA_MODE_CHAN_TO_REG(channel) | mode;
379 ddf_msg(LVL_DEBUG2, "Writing mode byte: %p:%hhx.",
380 dma_channel.mode_address, value);
381 pio_write_8(dma_channel.mode_address, value);
382
383 /* Set address - reset flip-flop */
384 pio_write_8(dma_channel.flip_flop_address, 0);
385
386 /* Low byte */
387 value = pa & 0xff;
388 ddf_msg(LVL_DEBUG2, "Writing address low byte: %p:%hhx.",
389 dma_channel.offset_reg_address, value);
390 pio_write_8(dma_channel.offset_reg_address, value);
391
392 /* High byte */
393 value = (pa >> 8) & 0xff;
394 ddf_msg(LVL_DEBUG2, "Writing address high byte: %p:%hhx.",
395 dma_channel.offset_reg_address, value);
396 pio_write_8(dma_channel.offset_reg_address, value);
397
398 /* Page address - third byte */
399 value = (pa >> 16) & 0xff;
400 ddf_msg(LVL_DEBUG2, "Writing address page byte: %p:%hhx.",
401 dma_channel.page_reg_address, value);
402 pio_write_8(dma_channel.page_reg_address, value);
403
404 /* Set size - reset flip-flop */
405 pio_write_8(dma_channel.flip_flop_address, 0);
406
407 /* Low byte */
408 value = (size - 1) & 0xff;
409 ddf_msg(LVL_DEBUG2, "Writing size low byte: %p:%hhx.",
410 dma_channel.size_reg_address, value);
411 pio_write_8(dma_channel.size_reg_address, value);
412
413 /* High byte */
414 value = ((size - 1) >> 8) & 0xff;
415 ddf_msg(LVL_DEBUG2, "Writing size high byte: %p:%hhx.",
416 dma_channel.size_reg_address, value);
417 pio_write_8(dma_channel.size_reg_address, value);
418
419 /* Unmask DMA request */
420 value = DMA_SINGLE_MASK_CHAN_TO_REG(channel);
421 pio_write_8(dma_channel.single_mask_address, value);
422
423 fibril_mutex_unlock(&guard);
424
425 return EOK;
426}
427
428/**
429 * @}
430 */
Note: See TracBrowser for help on using the repository browser.