xref: /netbsd/sys/arch/arc/isa/isadma.c (revision c4a72b64)
1 /*	$NetBSD: isadma.c,v 1.6 2002/10/02 04:59:48 thorpej Exp $	*/
2 /*	$OpenBSD: isadma.c,v 1.2 1996/11/23 21:45:34 kstailey Exp $	*/
3 /*	NetBSD: isadma.c,v 1.19 1996/04/29 20:03:26 christos Exp 	*/
4 
5 #include <sys/param.h>
6 #include <sys/systm.h>
7 #include <sys/device.h>
8 #include <sys/file.h>
9 #include <sys/buf.h>
10 #include <sys/syslog.h>
11 #include <sys/malloc.h>
12 #include <sys/uio.h>
13 
14 #include <uvm/uvm_extern.h>
15 
16 #include <machine/pio.h>
17 
18 #include <dev/isa/isareg.h>
19 #include <dev/isa/isavar.h>
20 #include <dev/isa/isadmavar.h>
21 #include <arch/arc/isa/isadmareg.h> /*XXX*/
22 
23 struct dma_info {
24 	int flags;
25 	int active;
26 	caddr_t addr;
27 	bus_size_t nbytes;
28 	struct isadma_seg phys[1];
29 };
30 
31 static struct isadma_softc *isadma_sc;	/*XXX ugly */
32 static struct dma_info dma_info[8];
33 static u_int8_t dma_finished;
34 
35 /* high byte of address is stored in this port for i-th dma channel */
36 static int dmapageport[2][4] = {
37 	{0x87, 0x83, 0x81, 0x82},
38 	{0x8f, 0x8b, 0x89, 0x8a}
39 };
40 
41 static u_int8_t dmamode[4] = {
42 	DMA37MD_READ | DMA37MD_SINGLE,
43 	DMA37MD_WRITE | DMA37MD_SINGLE,
44 	DMA37MD_READ | DMA37MD_LOOP,
45 	DMA37MD_WRITE | DMA37MD_LOOP
46 };
47 
48 int isadmamatch __P((struct device *, struct cfdata *, void *));
49 void isadmaattach __P((struct device *, struct device *, void *));
50 int isadmaprint __P((void *, const char *));
51 
52 struct isadma_softc {
53 	struct device sc_dev;
54 	bus_space_tag_t sc_iot;
55 	bus_space_handle_t sc_ioh1;
56 	bus_space_handle_t sc_ioh2;
57 }
58 
59 CFATTACH_DECL(isadma, sizeof(struct isadma_softc),
60     isadmamatch, isadmaattach, NULL, NULL);
61 
62 struct cfdriver isadma_cd = {
63 	NULL, "isadma", DV_DULL, 1
64 };
65 
66 isadmamatch(parent, match, aux)
67 	struct device *parent;
68 	struct cfdata *match;
69 	void *aux;
70 {
71 	struct isa_attach_args *ia = aux;
72 
73 	/* Sure we exist */
74 	ia->ia_iosize = 0;
75 	return (1);
76 }
77 
78 void
79 isadmaattach(parent, self, aux)
80 	struct device *parent, *self;
81 	void *aux;
82 {
83 	struct isadma_softc *sc = (void *)self;
84 	struct isa_attach_args *ia = aux;
85 	bus_space_tag_t iot;
86 	bus_space_handle_t ioh;
87 
88 	printf("\n");
89 
90 	iot = sc->sc_iot = ia->ia_iot;
91 	if (bus_space_map(iot, IO_DMA1, DMA_NREGS, 0, &ioh))
92 		panic("isadmaattach: couldn't map I/O ports");
93 	sc->sc_ioh1 = ioh;
94 	if (bus_space_map(iot, IO_DMA2, DMA_NREGS*2, 0, &ioh))
95 		panic("isadmaattach: couldn't map I/O ports");
96 	sc->sc_ioh2 = ioh;
97 	isadma_sc = sc;
98 }
99 
100 /*
101  * isadma_cascade(): program 8237 DMA controller channel to accept
102  * external dma control by a board.
103  */
104 void
105 isadma_cascade(chan)
106 	int chan;
107 {
108 	struct isadma_softc *sc = isadma_sc;
109 	bus_space_tag_t iot = sc->sc_iot;
110 
111 #ifdef ISADMA_DEBUG
112 	if (chan < 0 || chan > 7)
113 		panic("isadma_cascade: impossible request");
114 #endif
115 
116 	/* set dma channel mode, and set dma channel mode */
117 	if ((chan & 4) == 0) {
118 		bus_space_write_1(iot, sc->sc_ioh1, DMA1_MODE, chan | DMA37MD_CASCADE);
119 		bus_space_write_1(iot, sc->sc_ioh1, DMA1_SMSK, chan);
120 	} else {
121 		chan &= 3;
122 
123 		bus_space_write_1(iot, sc->sc_ioh2, DMA2_MODE, chan | DMA37MD_CASCADE);
124 		bus_space_write_1(iot, sc->sc_ioh2, DMA2_SMSK, chan);
125 	}
126 }
127 
128 /*
129  * isadma_start(): program 8237 DMA controller channel, avoid page alignment
130  * problems by using a bounce buffer.
131  */
132 void
133 isadma_start(addr, nbytes, chan, flags)
134 	caddr_t addr;
135 	bus_size_t nbytes;
136 	int chan;
137 	int flags;
138 {
139 	struct dma_info *di;
140 	int waport;
141 	int mflags;
142 	struct isadma_softc *sc = isadma_sc;
143 	bus_space_tag_t iot = sc->sc_iot;
144 	bus_space_handle_t ioh;
145 
146 #ifdef ISADMA_DEBUG
147 	if (chan < 0 || chan > 7 ||
148 	    (((flags & DMAMODE_READ) != 0) + ((flags & DMAMODE_WRITE) != 0) +
149 	    ((flags & DMAMODE_LOOP) != 0) != 1) ||
150 	    ((chan & 4) ? (nbytes >= (1<<17) || nbytes & 1 || (u_int)addr & 1) :
151 	    (nbytes >= (1<<16))))
152 		panic("isadma_start: impossible request");
153 #endif
154 
155 	di = dma_info+chan;
156 	if (di->active) {
157 		log(LOG_ERR,"isadma_start: old request active on %d\n",chan);
158 		isadma_abort(chan);
159 	}
160 
161 	di->flags = flags;
162 	di->active = 1;
163 	di->addr = addr;
164 	di->nbytes = nbytes;
165 
166 	mflags = ISADMA_MAP_WAITOK | ISADMA_MAP_BOUNCE | ISADMA_MAP_CONTIG;
167 	mflags |= (chan & 4) ? ISADMA_MAP_16BIT : ISADMA_MAP_8BIT;
168 
169 	if (isadma_map(addr, nbytes, di->phys, mflags) != 1)
170 		panic("isadma_start: cannot map");
171 
172 	/* XXX Will this do what we want with DMAMODE_LOOP?  */
173 	if ((flags & DMAMODE_READ) == 0)
174 		isadma_copytobuf(addr, nbytes, 1, di->phys);
175 
176 	dma_finished &= ~(1 << chan);
177 
178 	if ((chan & 4) == 0) {
179 		ioh = sc->sc_ioh1;
180 		/*
181 		 * Program one of DMA channels 0..3.  These are
182 		 * byte mode channels.
183 		 */
184 		/* set dma channel mode, and reset address ff */
185 		bus_space_write_1(iot, ioh, DMA1_MODE, chan | dmamode[flags]);
186 		bus_space_write_1(iot, ioh, DMA1_FFC, 0);
187 
188 		/* send start address */
189 		waport = DMA1_CHN(chan);
190 		outb(dmapageport[0][chan], di->phys[0].addr>>16);
191 		outb(waport, di->phys[0].addr);
192 		outb(waport, di->phys[0].addr>>8);
193 
194 		/* send count */
195 		outb(waport + 1, --nbytes);
196 		outb(waport + 1, nbytes>>8);
197 
198 		/* unmask channel */
199 		bus_space_write_1(iot, ioh, DMA1_SMSK, chan | DMA37SM_CLEAR);
200 	} else {
201 		ioh = sc->sc_ioh2;
202 		/*
203 		 * Program one of DMA channels 4..7.  These are
204 		 * word mode channels.
205 		 */
206 		/* set dma channel mode, and reset address ff */
207 		bus_space_write_1(iot, ioh, DMA2_MODE, (chan & 3) | dmamode[flags]);
208 		bus_space_write_1(iot, ioh, DMA2_FFC, 0);
209 
210 		/* send start address */
211 		waport = DMA2_CHN(chan & 3);
212 		outb(dmapageport[1][chan], di->phys[0].addr>>16);
213 		outb(waport, di->phys[0].addr>>1);
214 		outb(waport, di->phys[0].addr>>9);
215 
216 		/* send count */
217 		nbytes >>= 1;
218 		outb(waport + 2, --nbytes);
219 		outb(waport + 2, nbytes>>8);
220 
221 		/* unmask channel */
222 		bus_space_write_1(iot, ioh, DMA2_SMSK, (chan & 3) | DMA37SM_CLEAR);
223 	}
224 }
225 
226 void
227 isadma_abort(chan)
228 	int chan;
229 {
230 	struct dma_info *di;
231 	struct isadma_softc *sc = isadma_sc;
232 	bus_space_tag_t iot = sc->sc_iot;
233 
234 #ifdef ISADMA_DEBUG
235 	if (chan < 0 || chan > 7)
236 		panic("isadma_abort: impossible request");
237 #endif
238 
239 	di = dma_info+chan;
240 	if (! di->active) {
241 		log(LOG_ERR,"isadma_abort: no request active on %d\n",chan);
242 		return;
243 	}
244 
245 	/* mask channel */
246 	if ((chan & 4) == 0)
247 		bus_space_write_1(iot, sc->sc_ioh1, DMA1_SMSK, DMA37SM_SET | chan);
248 	else
249 		bus_space_write_1(iot, sc->sc_ioh2, DMA2_SMSK, DMA37SM_SET | (chan & 3));
250 
251 	isadma_unmap(di->addr, di->nbytes, 1, di->phys);
252 	di->active = 0;
253 }
254 
255 int
256 isadma_finished(chan)
257 	int chan;
258 {
259 	struct isadma_softc *sc = isadma_sc;
260 	bus_space_tag_t iot = sc->sc_iot;
261 
262 #ifdef ISADMA_DEBUG
263 	if (chan < 0 || chan > 7)
264 		panic("isadma_finished: impossible request");
265 #endif
266 
267 	/* check that the terminal count was reached */
268 	if ((chan & 4) == 0)
269 		dma_finished |= bus_space_read_1(iot, sc->sc_ioh1, DMA1_SR) & 0x0f;
270 	else
271 		dma_finished |= (bus_space_read_1(iot, sc->sc_ioh2, DMA2_SR) & 0x0f) << 4;
272 
273 	return ((dma_finished & (1 << chan)) != 0);
274 }
275 
276 void
277 isadma_done(chan)
278 	int chan;
279 {
280 	struct dma_info *di;
281 	u_char tc;
282 	struct isadma_softc *sc = isadma_sc;
283 	bus_space_tag_t iot = sc->sc_iot;
284 
285 #ifdef DIAGNOSTIC
286 	if (chan < 0 || chan > 7)
287 		panic("isadma_done: impossible request");
288 #endif
289 
290 	di = dma_info+chan;
291 	if (! di->active) {
292 		log(LOG_ERR,"isadma_done: no request active on %d\n",chan);
293 		return;
294 	}
295 
296 	/* check that the terminal count was reached */
297 	if ((chan & 4) == 0)
298 		tc = bus_space_read_1(iot, sc->sc_ioh1, DMA1_SR) & (1 << chan);
299 	else
300 		tc = bus_space_read_1(iot, sc->sc_ioh2, DMA2_SR) & (1 << (chan & 3));
301 	if (tc == 0)
302 		/* XXX probably should panic or something */
303 		log(LOG_ERR, "dma channel %d not finished\n", chan);
304 
305 	/* mask channel */
306 	if ((chan & 4) == 0)
307 		bus_space_write_1(iot, sc->sc_ioh1, DMA1_SMSK, DMA37SM_SET | chan);
308 	else
309 		bus_space_write_1(iot, sc->sc_ioh2, DMA2_SMSK, DMA37SM_SET | (chan & 3));
310 
311 	/* XXX Will this do what we want with DMAMODE_LOOP?  */
312 	if (di->flags & DMAMODE_READ)
313 		isadma_copyfrombuf(di->addr, di->nbytes, 1, di->phys);
314 
315 	isadma_unmap(di->addr, di->nbytes, 1, di->phys);
316 	di->active = 0;
317 }
318