apbus.c revision 1.4.2.3 1 1.4.2.3 bouyer /* $NetBSD: apbus.c,v 1.4.2.3 2000/12/08 09:28:49 bouyer Exp $ */
2 1.4.2.2 bouyer
3 1.4.2.2 bouyer /*-
4 1.4.2.2 bouyer * Copyright (C) 1999 SHIMIZU Ryo. All rights reserved.
5 1.4.2.2 bouyer *
6 1.4.2.2 bouyer * Redistribution and use in source and binary forms, with or without
7 1.4.2.2 bouyer * modification, are permitted provided that the following conditions
8 1.4.2.2 bouyer * are met:
9 1.4.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.4.2.2 bouyer * notice, this list of conditions and the following disclaimer.
11 1.4.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
12 1.4.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
13 1.4.2.2 bouyer * documentation and/or other materials provided with the distribution.
14 1.4.2.2 bouyer * 3. The name of the author may not be used to endorse or promote products
15 1.4.2.2 bouyer * derived from this software without specific prior written permission.
16 1.4.2.2 bouyer *
17 1.4.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.4.2.2 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.4.2.2 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.4.2.2 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.4.2.2 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.4.2.2 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.4.2.2 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.4.2.2 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.4.2.2 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 1.4.2.2 bouyer * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.4.2.2 bouyer */
28 1.4.2.2 bouyer
29 1.4.2.2 bouyer #include <sys/param.h>
30 1.4.2.2 bouyer #include <sys/systm.h>
31 1.4.2.2 bouyer #include <sys/malloc.h>
32 1.4.2.2 bouyer #include <sys/device.h>
33 1.4.2.2 bouyer
34 1.4.2.2 bouyer #include <uvm/uvm_extern.h>
35 1.4.2.2 bouyer
36 1.4.2.2 bouyer #include <machine/adrsmap.h>
37 1.4.2.2 bouyer #include <machine/autoconf.h>
38 1.4.2.2 bouyer #define _NEWSMIPS_BUS_DMA_PRIVATE
39 1.4.2.2 bouyer #include <machine/bus.h>
40 1.4.2.2 bouyer #include <newsmips/apbus/apbusvar.h>
41 1.4.2.2 bouyer
42 1.4.2.3 bouyer static int apbusmatch (struct device *, struct cfdata *, void *);
43 1.4.2.3 bouyer static void apbusattach (struct device *, struct device *, void *);
44 1.4.2.3 bouyer static int apbusprint (void *, const char *);
45 1.4.2.3 bouyer /* static void *aptokseg0 (void *); */
46 1.4.2.3 bouyer static void apbus_dma_unmapped (bus_dma_tag_t, bus_dmamap_t);
47 1.4.2.3 bouyer static int apbus_dma_mapalloc (bus_dma_tag_t, bus_dmamap_t, int);
48 1.4.2.3 bouyer static void apbus_dma_mapfree (bus_dma_tag_t, bus_dmamap_t);
49 1.4.2.3 bouyer static void apbus_dma_mapset (bus_dma_tag_t, bus_dmamap_t);
50 1.4.2.3 bouyer static int apbus_dmamap_create (bus_dma_tag_t, bus_size_t, int, bus_size_t,
51 1.4.2.3 bouyer bus_size_t, int, bus_dmamap_t *);
52 1.4.2.3 bouyer static void apbus_dmamap_destroy (bus_dma_tag_t, bus_dmamap_t);
53 1.4.2.3 bouyer static int apbus_dmamap_load (bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t,
54 1.4.2.3 bouyer struct proc *, int);
55 1.4.2.3 bouyer static int apbus_dmamap_load_mbuf (bus_dma_tag_t, bus_dmamap_t, struct mbuf *,
56 1.4.2.3 bouyer int);
57 1.4.2.3 bouyer static int apbus_dmamap_load_uio (bus_dma_tag_t, bus_dmamap_t, struct uio *,
58 1.4.2.3 bouyer int);
59 1.4.2.3 bouyer static int apbus_dmamap_load_raw (bus_dma_tag_t, bus_dmamap_t,
60 1.4.2.3 bouyer bus_dma_segment_t *, int, bus_size_t, int);
61 1.4.2.3 bouyer static void apbus_dmamap_sync (bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
62 1.4.2.3 bouyer bus_size_t, int);
63 1.4.2.2 bouyer
64 1.4.2.2 bouyer #define MAXAPDEVNUM 32
65 1.4.2.2 bouyer
66 1.4.2.2 bouyer struct apbus_softc {
67 1.4.2.2 bouyer struct device apbs_dev;
68 1.4.2.2 bouyer };
69 1.4.2.2 bouyer
70 1.4.2.2 bouyer struct cfattach ap_ca = {
71 1.4.2.2 bouyer sizeof(struct apbus_softc), apbusmatch, apbusattach
72 1.4.2.2 bouyer };
73 1.4.2.2 bouyer
74 1.4.2.2 bouyer #define APBUS_DEVNAMELEN 16
75 1.4.2.2 bouyer
76 1.4.2.2 bouyer struct ap_intrhand {
77 1.4.2.2 bouyer struct ap_intrhand *ai_next;
78 1.4.2.2 bouyer int ai_mask;
79 1.4.2.2 bouyer int ai_priority;
80 1.4.2.3 bouyer int (*ai_func) (void*); /* function */
81 1.4.2.2 bouyer void *ai_aux; /* softc */
82 1.4.2.2 bouyer char ai_name[APBUS_DEVNAMELEN];
83 1.4.2.2 bouyer int ai_ctlno;
84 1.4.2.2 bouyer };
85 1.4.2.2 bouyer
86 1.4.2.2 bouyer #define NLEVEL 2
87 1.4.2.2 bouyer
88 1.4.2.2 bouyer static struct ap_intrhand *apintr[NLEVEL];
89 1.4.2.2 bouyer
90 1.4.2.2 bouyer static int
91 1.4.2.2 bouyer apbusmatch(parent, cfdata, aux)
92 1.4.2.2 bouyer struct device *parent;
93 1.4.2.2 bouyer struct cfdata *cfdata;
94 1.4.2.2 bouyer void *aux;
95 1.4.2.2 bouyer {
96 1.4.2.2 bouyer struct confargs *ca = aux;
97 1.4.2.2 bouyer
98 1.4.2.2 bouyer if (strcmp(ca->ca_name, "ap") != 0)
99 1.4.2.2 bouyer return 0;
100 1.4.2.2 bouyer
101 1.4.2.2 bouyer return 1;
102 1.4.2.2 bouyer }
103 1.4.2.2 bouyer
104 1.4.2.2 bouyer
105 1.4.2.2 bouyer static void
106 1.4.2.2 bouyer apbusattach(parent, self, aux)
107 1.4.2.2 bouyer struct device *parent;
108 1.4.2.2 bouyer struct device *self;
109 1.4.2.2 bouyer void *aux;
110 1.4.2.2 bouyer {
111 1.4.2.2 bouyer struct apbus_attach_args child;
112 1.4.2.2 bouyer struct apbus_dev *apdev;
113 1.4.2.2 bouyer struct apbus_ctl *apctl;
114 1.4.2.2 bouyer
115 1.4.2.2 bouyer *(volatile u_int *)(NEWS5000_APBUS_INTST) = 0xffffffff;
116 1.4.2.2 bouyer *(volatile u_int *)(NEWS5000_APBUS_INTMSK) = 0xffffffff;
117 1.4.2.2 bouyer *(volatile u_int *)(NEWS5000_APBUS_CTRL) = 0x00000004;
118 1.4.2.2 bouyer *(volatile u_int *)(NEWS5000_APBUS_DMA) = 0xffffffff;
119 1.4.2.2 bouyer
120 1.4.2.2 bouyer printf("\n");
121 1.4.2.2 bouyer
122 1.4.2.2 bouyer /*
123 1.4.2.2 bouyer * get first ap-device
124 1.4.2.2 bouyer */
125 1.4.2.2 bouyer apdev = apbus_lookupdev(NULL);
126 1.4.2.2 bouyer
127 1.4.2.2 bouyer /*
128 1.4.2.2 bouyer * trace device chain
129 1.4.2.2 bouyer */
130 1.4.2.2 bouyer while (apdev) {
131 1.4.2.2 bouyer apctl = apdev->apbd_ctl;
132 1.4.2.2 bouyer
133 1.4.2.2 bouyer /*
134 1.4.2.2 bouyer * probe physical device only
135 1.4.2.2 bouyer * (no pseudo device)
136 1.4.2.2 bouyer */
137 1.4.2.2 bouyer if (apctl && apctl->apbc_hwbase) {
138 1.4.2.2 bouyer /*
139 1.4.2.2 bouyer * ... and, all units
140 1.4.2.2 bouyer */
141 1.4.2.2 bouyer while (apctl) {
142 1.4.2.2 bouyer /* make apbus_attach_args for devices */
143 1.4.2.2 bouyer child.apa_name = apdev->apbd_name;
144 1.4.2.2 bouyer child.apa_ctlnum = apctl->apbc_ctlno;
145 1.4.2.2 bouyer child.apa_slotno = apctl->apbc_sl;
146 1.4.2.2 bouyer child.apa_hwbase = apctl->apbc_hwbase;
147 1.4.2.2 bouyer
148 1.4.2.2 bouyer config_found(self, &child, apbusprint);
149 1.4.2.2 bouyer
150 1.4.2.2 bouyer apctl = apctl->apbc_link;
151 1.4.2.2 bouyer }
152 1.4.2.2 bouyer }
153 1.4.2.2 bouyer
154 1.4.2.2 bouyer apdev = apdev->apbd_link;
155 1.4.2.2 bouyer }
156 1.4.2.2 bouyer }
157 1.4.2.2 bouyer
158 1.4.2.2 bouyer int
159 1.4.2.2 bouyer apbusprint(aux, pnp)
160 1.4.2.2 bouyer void *aux;
161 1.4.2.2 bouyer const char *pnp;
162 1.4.2.2 bouyer {
163 1.4.2.2 bouyer struct apbus_attach_args *a = aux;
164 1.4.2.2 bouyer
165 1.4.2.2 bouyer if (pnp)
166 1.4.2.2 bouyer printf("%s at %s slot%d addr 0x%lx",
167 1.4.2.2 bouyer a->apa_name, pnp, a->apa_slotno, a->apa_hwbase);
168 1.4.2.2 bouyer
169 1.4.2.2 bouyer return UNCONF;
170 1.4.2.2 bouyer }
171 1.4.2.2 bouyer
172 1.4.2.2 bouyer #if 0
173 1.4.2.2 bouyer void *
174 1.4.2.2 bouyer aptokseg0(va)
175 1.4.2.2 bouyer void *va;
176 1.4.2.2 bouyer {
177 1.4.2.2 bouyer vaddr_t addr = (vaddr_t)va;
178 1.4.2.2 bouyer
179 1.4.2.2 bouyer if (addr >= 0xfff00000) {
180 1.4.2.2 bouyer addr -= 0xfff00000;
181 1.4.2.2 bouyer addr += physmem << PGSHIFT;
182 1.4.2.2 bouyer addr += 0x80000000;
183 1.4.2.2 bouyer va = (void *)addr;
184 1.4.2.2 bouyer }
185 1.4.2.2 bouyer return va;
186 1.4.2.2 bouyer }
187 1.4.2.2 bouyer #endif
188 1.4.2.2 bouyer
189 1.4.2.2 bouyer void
190 1.4.2.2 bouyer apbus_wbflush()
191 1.4.2.2 bouyer {
192 1.4.2.2 bouyer volatile int *wbflush = (int *)NEWS5000_WBFLUSH;
193 1.4.2.2 bouyer
194 1.4.2.2 bouyer (void)*wbflush;
195 1.4.2.2 bouyer }
196 1.4.2.2 bouyer
197 1.4.2.2 bouyer /*
198 1.4.2.2 bouyer * called by hardware interrupt routine
199 1.4.2.2 bouyer */
200 1.4.2.2 bouyer int
201 1.4.2.2 bouyer apbus_intr_call(level, stat)
202 1.4.2.2 bouyer int level;
203 1.4.2.2 bouyer int stat;
204 1.4.2.2 bouyer {
205 1.4.2.2 bouyer int nintr = 0;
206 1.4.2.2 bouyer struct ap_intrhand *ai;
207 1.4.2.2 bouyer
208 1.4.2.2 bouyer for (ai = apintr[level]; ai != NULL; ai = ai->ai_next) {
209 1.4.2.2 bouyer if (ai->ai_mask & stat) {
210 1.4.2.2 bouyer nintr += (*ai->ai_func)(ai->ai_aux);
211 1.4.2.2 bouyer }
212 1.4.2.2 bouyer }
213 1.4.2.2 bouyer return nintr;
214 1.4.2.2 bouyer }
215 1.4.2.2 bouyer
216 1.4.2.2 bouyer /*
217 1.4.2.2 bouyer * register device interrupt routine
218 1.4.2.2 bouyer */
219 1.4.2.2 bouyer void *
220 1.4.2.2 bouyer apbus_intr_establish(level, mask, priority, func, aux, name, ctlno)
221 1.4.2.2 bouyer int level;
222 1.4.2.2 bouyer int mask;
223 1.4.2.2 bouyer int priority;
224 1.4.2.3 bouyer int (*func) (void *);
225 1.4.2.2 bouyer void *aux;
226 1.4.2.2 bouyer char *name;
227 1.4.2.2 bouyer int ctlno;
228 1.4.2.2 bouyer {
229 1.4.2.2 bouyer struct ap_intrhand *ai, **aip;
230 1.4.2.2 bouyer volatile unsigned int *inten0 = (volatile unsigned int *)NEWS5000_INTEN0;
231 1.4.2.2 bouyer volatile unsigned int *inten1 = (volatile unsigned int *)NEWS5000_INTEN1;
232 1.4.2.2 bouyer
233 1.4.2.2 bouyer ai = malloc(sizeof(*ai), M_DEVBUF, M_NOWAIT);
234 1.4.2.2 bouyer if (ai == NULL)
235 1.4.2.2 bouyer panic("apbus_intr_establish: can't malloc handler info");
236 1.4.2.2 bouyer ai->ai_mask = mask;
237 1.4.2.2 bouyer ai->ai_priority = priority;
238 1.4.2.2 bouyer ai->ai_func = func;
239 1.4.2.2 bouyer ai->ai_aux = aux;
240 1.4.2.2 bouyer strncpy(ai->ai_name, name, APBUS_DEVNAMELEN-1);
241 1.4.2.2 bouyer ai->ai_ctlno = ctlno;
242 1.4.2.2 bouyer
243 1.4.2.2 bouyer for (aip = &apintr[level]; *aip != NULL; aip = &(*aip)->ai_next) {
244 1.4.2.2 bouyer if ((*aip)->ai_priority < priority)
245 1.4.2.2 bouyer break;
246 1.4.2.2 bouyer }
247 1.4.2.2 bouyer ai->ai_next = *aip;
248 1.4.2.2 bouyer *aip = ai;
249 1.4.2.2 bouyer switch (level) {
250 1.4.2.2 bouyer case 0:
251 1.4.2.2 bouyer *inten0 |= mask;
252 1.4.2.2 bouyer break;
253 1.4.2.2 bouyer case 1:
254 1.4.2.2 bouyer *inten1 |= mask;
255 1.4.2.2 bouyer break;
256 1.4.2.2 bouyer }
257 1.4.2.2 bouyer
258 1.4.2.2 bouyer return (void *)ai;
259 1.4.2.2 bouyer }
260 1.4.2.2 bouyer
261 1.4.2.2 bouyer static void
262 1.4.2.2 bouyer apbus_dma_unmapped(t, map)
263 1.4.2.2 bouyer bus_dma_tag_t t;
264 1.4.2.2 bouyer bus_dmamap_t map;
265 1.4.2.2 bouyer {
266 1.4.2.2 bouyer int seg;
267 1.4.2.2 bouyer
268 1.4.2.2 bouyer for (seg = 0; seg < map->dm_nsegs; seg++) {
269 1.4.2.2 bouyer /*
270 1.4.2.2 bouyer * set MSB to indicate unmapped DMA.
271 1.4.2.2 bouyer * also need bit 30 for memory over 256MB.
272 1.4.2.2 bouyer */
273 1.4.2.2 bouyer if ((map->dm_segs[seg].ds_addr & 0x30000000) == 0)
274 1.4.2.2 bouyer map->dm_segs[seg].ds_addr |= 0x80000000;
275 1.4.2.2 bouyer else
276 1.4.2.2 bouyer map->dm_segs[seg].ds_addr |= 0xc0000000;
277 1.4.2.2 bouyer }
278 1.4.2.2 bouyer }
279 1.4.2.2 bouyer
280 1.4.2.2 bouyer #define APBUS_NDMAMAP (NEWS5000_APBUS_MAPSIZE / NEWS5000_APBUS_MAPENT)
281 1.4.2.2 bouyer #define APBUS_MAPTBL(n, v) (*(volatile u_int *)(NEWS5000_APBUS_DMAMAP + \
282 1.4.2.2 bouyer NEWS5000_APBUS_MAPENT * (n) + 1) = (v))
283 1.4.2.2 bouyer static u_char apbus_dma_maptbl[APBUS_NDMAMAP];
284 1.4.2.2 bouyer
285 1.4.2.2 bouyer static int
286 1.4.2.2 bouyer apbus_dma_mapalloc(t, map, flags)
287 1.4.2.2 bouyer bus_dma_tag_t t;
288 1.4.2.2 bouyer bus_dmamap_t map;
289 1.4.2.2 bouyer int flags;
290 1.4.2.2 bouyer {
291 1.4.2.2 bouyer int i, j, cnt;
292 1.4.2.2 bouyer
293 1.4.2.2 bouyer cnt = round_page(map->_dm_size) / NBPG;
294 1.4.2.2 bouyer
295 1.4.2.2 bouyer again:
296 1.4.2.2 bouyer for (i = 0; i < APBUS_NDMAMAP; i += j + 1) {
297 1.4.2.2 bouyer for (j = 0; j < cnt; j++) {
298 1.4.2.2 bouyer if (apbus_dma_maptbl[i + j])
299 1.4.2.2 bouyer break;
300 1.4.2.2 bouyer }
301 1.4.2.2 bouyer if (j == cnt) {
302 1.4.2.2 bouyer for (j = 0; j < cnt; j++)
303 1.4.2.2 bouyer apbus_dma_maptbl[i + j] = 1;
304 1.4.2.2 bouyer map->_dm_maptbl = i;
305 1.4.2.2 bouyer map->_dm_maptblcnt = cnt;
306 1.4.2.2 bouyer return 0;
307 1.4.2.2 bouyer }
308 1.4.2.2 bouyer }
309 1.4.2.2 bouyer if ((flags & BUS_DMA_NOWAIT) == 0) {
310 1.4.2.2 bouyer tsleep(&apbus_dma_maptbl, PRIBIO, "apdmat", 0);
311 1.4.2.2 bouyer goto again;
312 1.4.2.2 bouyer }
313 1.4.2.2 bouyer return ENOMEM;
314 1.4.2.2 bouyer }
315 1.4.2.2 bouyer
316 1.4.2.2 bouyer static void
317 1.4.2.2 bouyer apbus_dma_mapfree(t, map)
318 1.4.2.2 bouyer bus_dma_tag_t t;
319 1.4.2.2 bouyer bus_dmamap_t map;
320 1.4.2.2 bouyer {
321 1.4.2.2 bouyer int i, n;
322 1.4.2.2 bouyer
323 1.4.2.2 bouyer if (map->_dm_maptblcnt > 0) {
324 1.4.2.2 bouyer n = map->_dm_maptbl;
325 1.4.2.2 bouyer for (i = 0; i < map->_dm_maptblcnt; i++, n++) {
326 1.4.2.2 bouyer #ifdef DIAGNOSTIC
327 1.4.2.2 bouyer if (apbus_dma_maptbl[n] == 0)
328 1.4.2.2 bouyer panic("freeing free dma map");
329 1.4.2.2 bouyer APBUS_MAPTBL(n, 0xffffffff); /* causes DMA error */
330 1.4.2.2 bouyer #endif
331 1.4.2.2 bouyer apbus_dma_maptbl[n] = 0;
332 1.4.2.2 bouyer }
333 1.4.2.2 bouyer wakeup(&apbus_dma_maptbl);
334 1.4.2.2 bouyer map->_dm_maptblcnt = 0;
335 1.4.2.2 bouyer }
336 1.4.2.2 bouyer }
337 1.4.2.2 bouyer
338 1.4.2.2 bouyer static void
339 1.4.2.2 bouyer apbus_dma_mapset(t, map)
340 1.4.2.2 bouyer bus_dma_tag_t t;
341 1.4.2.2 bouyer bus_dmamap_t map;
342 1.4.2.2 bouyer {
343 1.4.2.2 bouyer int i;
344 1.4.2.2 bouyer bus_addr_t addr, eaddr;
345 1.4.2.2 bouyer int seg;
346 1.4.2.2 bouyer bus_dma_segment_t *segs;
347 1.4.2.2 bouyer
348 1.4.2.2 bouyer i = 0;
349 1.4.2.2 bouyer for (seg = 0; seg < map->dm_nsegs; seg++) {
350 1.4.2.2 bouyer segs = &map->dm_segs[seg];
351 1.4.2.2 bouyer for (addr = segs->ds_addr, eaddr = addr + segs->ds_len;
352 1.4.2.2 bouyer addr < eaddr; addr += NBPG, i++) {
353 1.4.2.2 bouyer #ifdef DIAGNOSTIC
354 1.4.2.2 bouyer if (i >= map->_dm_maptblcnt)
355 1.4.2.2 bouyer panic("dma map table overflow");
356 1.4.2.2 bouyer #endif
357 1.4.2.2 bouyer APBUS_MAPTBL(map->_dm_maptbl + i,
358 1.4.2.2 bouyer NEWS5000_APBUS_MAP_VALID |
359 1.4.2.2 bouyer NEWS5000_APBUS_MAP_COHERENT |
360 1.4.2.2 bouyer (addr >> PGSHIFT));
361 1.4.2.2 bouyer }
362 1.4.2.2 bouyer }
363 1.4.2.2 bouyer map->dm_segs[0].ds_addr = map->_dm_maptbl << PGSHIFT;
364 1.4.2.2 bouyer map->dm_segs[0].ds_len = map->dm_mapsize;
365 1.4.2.2 bouyer map->dm_nsegs = 1;
366 1.4.2.2 bouyer }
367 1.4.2.2 bouyer
368 1.4.2.3 bouyer static int
369 1.4.2.2 bouyer apbus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
370 1.4.2.2 bouyer bus_dma_tag_t t;
371 1.4.2.2 bouyer bus_size_t size;
372 1.4.2.2 bouyer int nsegments;
373 1.4.2.2 bouyer bus_size_t maxsegsz;
374 1.4.2.2 bouyer bus_size_t boundary;
375 1.4.2.2 bouyer int flags;
376 1.4.2.2 bouyer bus_dmamap_t *dmamp;
377 1.4.2.2 bouyer {
378 1.4.2.2 bouyer int error;
379 1.4.2.2 bouyer
380 1.4.2.2 bouyer if (flags & NEWSMIPS_DMAMAP_MAPTBL)
381 1.4.2.2 bouyer nsegments = round_page(size) / NBPG;
382 1.4.2.2 bouyer error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
383 1.4.2.2 bouyer flags, dmamp);
384 1.4.2.2 bouyer if (error == 0 && (flags & NEWSMIPS_DMAMAP_MAPTBL)) {
385 1.4.2.2 bouyer error = apbus_dma_mapalloc(t, *dmamp, flags);
386 1.4.2.2 bouyer if (error) {
387 1.4.2.2 bouyer _bus_dmamap_destroy(t, *dmamp);
388 1.4.2.2 bouyer *dmamp = NULL;
389 1.4.2.2 bouyer }
390 1.4.2.2 bouyer }
391 1.4.2.2 bouyer return error;
392 1.4.2.2 bouyer }
393 1.4.2.2 bouyer
394 1.4.2.3 bouyer static void
395 1.4.2.2 bouyer apbus_dmamap_destroy(t, map)
396 1.4.2.2 bouyer bus_dma_tag_t t;
397 1.4.2.2 bouyer bus_dmamap_t map;
398 1.4.2.2 bouyer {
399 1.4.2.2 bouyer if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
400 1.4.2.2 bouyer apbus_dma_mapfree(t, map);
401 1.4.2.2 bouyer _bus_dmamap_destroy(t, map);
402 1.4.2.2 bouyer }
403 1.4.2.2 bouyer
404 1.4.2.3 bouyer static int
405 1.4.2.2 bouyer apbus_dmamap_load(t, map, buf, buflen, p, flags)
406 1.4.2.2 bouyer bus_dma_tag_t t;
407 1.4.2.2 bouyer bus_dmamap_t map;
408 1.4.2.2 bouyer void *buf;
409 1.4.2.2 bouyer bus_size_t buflen;
410 1.4.2.2 bouyer struct proc *p;
411 1.4.2.2 bouyer int flags;
412 1.4.2.2 bouyer {
413 1.4.2.2 bouyer int error;
414 1.4.2.2 bouyer
415 1.4.2.2 bouyer error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
416 1.4.2.2 bouyer if (error == 0) {
417 1.4.2.2 bouyer if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
418 1.4.2.2 bouyer apbus_dma_mapset(t, map);
419 1.4.2.2 bouyer else
420 1.4.2.2 bouyer apbus_dma_unmapped(t, map);
421 1.4.2.2 bouyer }
422 1.4.2.2 bouyer return error;
423 1.4.2.2 bouyer }
424 1.4.2.2 bouyer
425 1.4.2.3 bouyer static int
426 1.4.2.2 bouyer apbus_dmamap_load_mbuf(t, map, m0, flags)
427 1.4.2.2 bouyer bus_dma_tag_t t;
428 1.4.2.2 bouyer bus_dmamap_t map;
429 1.4.2.2 bouyer struct mbuf *m0;
430 1.4.2.2 bouyer int flags;
431 1.4.2.2 bouyer {
432 1.4.2.2 bouyer int error;
433 1.4.2.2 bouyer
434 1.4.2.2 bouyer error = _bus_dmamap_load_mbuf(t, map, m0, flags);
435 1.4.2.2 bouyer if (error == 0) {
436 1.4.2.2 bouyer if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
437 1.4.2.2 bouyer apbus_dma_mapset(t, map);
438 1.4.2.2 bouyer else
439 1.4.2.2 bouyer apbus_dma_unmapped(t, map);
440 1.4.2.2 bouyer }
441 1.4.2.2 bouyer return error;
442 1.4.2.2 bouyer }
443 1.4.2.2 bouyer
444 1.4.2.3 bouyer static int
445 1.4.2.2 bouyer apbus_dmamap_load_uio(t, map, uio, flags)
446 1.4.2.2 bouyer bus_dma_tag_t t;
447 1.4.2.2 bouyer bus_dmamap_t map;
448 1.4.2.2 bouyer struct uio *uio;
449 1.4.2.2 bouyer int flags;
450 1.4.2.2 bouyer {
451 1.4.2.2 bouyer int error;
452 1.4.2.2 bouyer
453 1.4.2.2 bouyer error = _bus_dmamap_load_uio(t, map, uio, flags);
454 1.4.2.2 bouyer if (error == 0) {
455 1.4.2.2 bouyer if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
456 1.4.2.2 bouyer apbus_dma_mapset(t, map);
457 1.4.2.2 bouyer else
458 1.4.2.2 bouyer apbus_dma_unmapped(t, map);
459 1.4.2.2 bouyer }
460 1.4.2.2 bouyer return error;
461 1.4.2.2 bouyer }
462 1.4.2.2 bouyer
463 1.4.2.3 bouyer static int
464 1.4.2.2 bouyer apbus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
465 1.4.2.2 bouyer bus_dma_tag_t t;
466 1.4.2.2 bouyer bus_dmamap_t map;
467 1.4.2.2 bouyer bus_dma_segment_t *segs;
468 1.4.2.2 bouyer int nsegs;
469 1.4.2.2 bouyer bus_size_t size;
470 1.4.2.2 bouyer int flags;
471 1.4.2.2 bouyer {
472 1.4.2.2 bouyer int error;
473 1.4.2.2 bouyer
474 1.4.2.2 bouyer error = _bus_dmamap_load_raw(t, map, segs, nsegs, size, flags);
475 1.4.2.2 bouyer if (error == 0) {
476 1.4.2.2 bouyer if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
477 1.4.2.2 bouyer apbus_dma_mapset(t, map);
478 1.4.2.2 bouyer else
479 1.4.2.2 bouyer apbus_dma_unmapped(t, map);
480 1.4.2.2 bouyer }
481 1.4.2.2 bouyer return error;
482 1.4.2.2 bouyer }
483 1.4.2.2 bouyer
484 1.4.2.3 bouyer static void
485 1.4.2.2 bouyer apbus_dmamap_sync(t, map, offset, len, ops)
486 1.4.2.2 bouyer bus_dma_tag_t t;
487 1.4.2.2 bouyer bus_dmamap_t map;
488 1.4.2.2 bouyer bus_addr_t offset;
489 1.4.2.2 bouyer bus_size_t len;
490 1.4.2.2 bouyer int ops;
491 1.4.2.2 bouyer {
492 1.4.2.2 bouyer
493 1.4.2.2 bouyer /*
494 1.4.2.2 bouyer * Flush DMA cache by issueing IO read for the AProm of specified slot.
495 1.4.2.2 bouyer */
496 1.4.2.2 bouyer bus_space_read_4(t->_slotbaset, t->_slotbaseh, 0);
497 1.4.2.2 bouyer
498 1.4.2.2 bouyer _bus_dmamap_sync(t, map, offset, len, ops);
499 1.4.2.2 bouyer }
500 1.4.2.2 bouyer
501 1.4.2.2 bouyer struct newsmips_bus_dma_tag apbus_dma_tag = {
502 1.4.2.2 bouyer apbus_dmamap_create,
503 1.4.2.2 bouyer apbus_dmamap_destroy,
504 1.4.2.2 bouyer apbus_dmamap_load,
505 1.4.2.2 bouyer apbus_dmamap_load_mbuf,
506 1.4.2.2 bouyer apbus_dmamap_load_uio,
507 1.4.2.2 bouyer apbus_dmamap_load_raw,
508 1.4.2.2 bouyer _bus_dmamap_unload,
509 1.4.2.2 bouyer apbus_dmamap_sync,
510 1.4.2.2 bouyer _bus_dmamem_alloc,
511 1.4.2.2 bouyer _bus_dmamem_free,
512 1.4.2.2 bouyer _bus_dmamem_map,
513 1.4.2.2 bouyer _bus_dmamem_unmap,
514 1.4.2.2 bouyer _bus_dmamem_mmap,
515 1.4.2.2 bouyer };
516 1.4.2.2 bouyer
517 1.4.2.2 bouyer struct newsmips_bus_dma_tag *
518 1.4.2.2 bouyer apbus_dmatag_init(apa)
519 1.4.2.2 bouyer struct apbus_attach_args *apa;
520 1.4.2.2 bouyer {
521 1.4.2.2 bouyer struct newsmips_bus_dma_tag *dmat;
522 1.4.2.2 bouyer
523 1.4.2.2 bouyer dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
524 1.4.2.2 bouyer if (dmat != NULL) {
525 1.4.2.2 bouyer memcpy(dmat, &apbus_dma_tag, sizeof(*dmat));
526 1.4.2.2 bouyer dmat->_slotno = apa->apa_slotno;
527 1.4.2.2 bouyer dmat->_slotbaset = 0;
528 1.4.2.2 bouyer dmat->_slotbaseh = apa->apa_hwbase;
529 1.4.2.2 bouyer }
530 1.4.2.2 bouyer return dmat;
531 1.4.2.2 bouyer }
532