Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / newsmips / apbus / apbus.c
blobaed370d656a594e03ef045725c8142abc0ead8d9
1 /* $NetBSD: apbus.c,v 1.20 2005/12/11 12:18:24 christos Exp $ */
3 /*-
4 * Copyright (C) 1999 SHIMIZU Ryo. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. 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 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
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
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: apbus.c,v 1.20 2005/12/11 12:18:24 christos Exp $");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/device.h>
36 #include <sys/proc.h>
38 #include <uvm/uvm_extern.h>
40 #include <machine/adrsmap.h>
41 #include <machine/autoconf.h>
42 #define _NEWSMIPS_BUS_DMA_PRIVATE
43 #include <machine/bus.h>
44 #include <machine/intr.h>
45 #include <newsmips/apbus/apbusvar.h>
47 static int apbusmatch(device_t, cfdata_t, void *);
48 static void apbusattach(device_t, device_t, void *);
49 static int apbusprint(void *, const char *);
50 #if 0
51 static void *aptokseg0 (void *);
52 #endif
53 static void apbus_dma_unmapped(bus_dma_tag_t, bus_dmamap_t);
54 static int apbus_dma_mapalloc(bus_dma_tag_t, bus_dmamap_t, int);
55 static void apbus_dma_mapfree(bus_dma_tag_t, bus_dmamap_t);
56 static void apbus_dma_mapset(bus_dma_tag_t, bus_dmamap_t);
57 static int apbus_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
58 bus_size_t, int, bus_dmamap_t *);
59 static void apbus_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t);
60 static int apbus_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t,
61 struct proc *, int);
62 static int apbus_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t, struct mbuf *,
63 int);
64 static int apbus_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t, struct uio *,
65 int);
66 static int apbus_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t,
67 bus_dma_segment_t *, int, bus_size_t, int);
68 static void apbus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
69 bus_size_t, int);
71 #define MAXAPDEVNUM 32
73 CFATTACH_DECL_NEW(ap, 0,
74 apbusmatch, apbusattach, NULL, NULL);
76 #define NLEVEL 2
77 static struct newsmips_intr apintr_tab[NLEVEL];
79 static int
80 apbusmatch(device_t parent, cfdata_t cf, void *aux)
82 struct confargs *ca = aux;
84 if (strcmp(ca->ca_name, "ap") != 0)
85 return 0;
87 return 1;
91 static void
92 apbusattach(device_t parent, device_t self, void *aux)
94 struct apbus_attach_args child;
95 struct apbus_dev *apdev;
96 struct apbus_ctl *apctl;
97 struct newsmips_intr *ip;
98 int i;
100 *(volatile uint32_t *)(NEWS5000_APBUS_INTST) = 0xffffffff;
101 *(volatile uint32_t *)(NEWS5000_APBUS_INTMSK) = 0xffffffff;
102 *(volatile uint32_t *)(NEWS5000_APBUS_CTRL) = 0x00000004;
103 *(volatile uint32_t *)(NEWS5000_APBUS_DMA) = 0xffffffff;
105 aprint_normal("\n");
107 for (i = 0; i < NLEVEL; i++) {
108 ip = &apintr_tab[i];
109 LIST_INIT(&ip->intr_q);
113 * get first ap-device
115 apdev = apbus_lookupdev(NULL);
118 * trace device chain
120 while (apdev) {
121 apctl = apdev->apbd_ctl;
124 * probe physical device only
125 * (no pseudo device)
127 if (apctl && apctl->apbc_hwbase) {
129 * ... and, all units
131 while (apctl) {
132 /* make apbus_attach_args for devices */
133 child.apa_name = apdev->apbd_name;
134 child.apa_ctlnum = apctl->apbc_ctlno;
135 child.apa_slotno = apctl->apbc_sl;
136 child.apa_hwbase = apctl->apbc_hwbase;
138 config_found(self, &child, apbusprint);
140 apctl = apctl->apbc_link;
144 apdev = apdev->apbd_link;
149 apbusprint(void *aux, const char *pnp)
151 struct apbus_attach_args *a = aux;
153 if (pnp)
154 aprint_normal("%s at %s slot%d addr 0x%lx",
155 a->apa_name, pnp, a->apa_slotno, a->apa_hwbase);
157 return UNCONF;
160 #if 0
161 void *
162 aptokseg0(void *va)
164 vaddr_t addr = (vaddr_t)va;
166 if (addr >= 0xfff00000) {
167 addr -= 0xfff00000;
168 addr += physmem << PGSHIFT;
169 addr += 0x80000000;
170 va = (void *)addr;
172 return va;
174 #endif
176 void
177 apbus_wbflush(void)
179 volatile int32_t *wbflush = (uint32_t *)NEWS5000_WBFLUSH;
181 (void)*wbflush;
185 * called by hardware interrupt routine
188 apbus_intr_dispatch(int level, int stat)
190 struct newsmips_intr *ip;
191 struct newsmips_intrhand *ih;
192 int nintr;
194 ip = &apintr_tab[level];
196 nintr = 0;
197 LIST_FOREACH(ih, &ip->intr_q, ih_q) {
198 if (ih->ih_mask & stat)
199 nintr += (*ih->ih_func)(ih->ih_arg);
201 return nintr;
205 * register device interrupt routine
207 void *
208 apbus_intr_establish(int level, int mask, int priority, int (*func)(void *),
209 void *arg, const char *name, int ctlno)
211 struct newsmips_intr *ip;
212 struct newsmips_intrhand *ih, *curih;
213 volatile uint32_t *inten0, *inten1;
215 ip = &apintr_tab[level];
217 ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
218 if (ih == NULL)
219 panic("%s: can't malloc handler info", __func__);
220 ih->ih_mask = mask;
221 ih->ih_priority = priority;
222 ih->ih_func = func;
223 ih->ih_arg = arg;
225 if (LIST_EMPTY(&ip->intr_q)) {
226 LIST_INSERT_HEAD(&ip->intr_q, ih, ih_q);
227 goto done;
230 for (curih = LIST_FIRST(&ip->intr_q);
231 LIST_NEXT(curih, ih_q) != NULL;
232 curih = LIST_NEXT(curih, ih_q)) {
233 if (ih->ih_priority > curih->ih_priority) {
234 LIST_INSERT_BEFORE(curih, ih, ih_q);
235 goto done;
239 LIST_INSERT_AFTER(curih, ih, ih_q);
241 done:
242 switch (level) {
243 case 0:
244 inten0 = (volatile uint32_t *)NEWS5000_INTEN0;
245 *inten0 |= mask;
246 break;
247 case 1:
248 inten1 = (volatile uint32_t *)NEWS5000_INTEN1;
249 *inten1 |= mask;
250 break;
253 return (void *)ih;
256 static void
257 apbus_dma_unmapped(bus_dma_tag_t t, bus_dmamap_t map)
259 int seg;
261 for (seg = 0; seg < map->dm_nsegs; seg++) {
263 * set MSB to indicate unmapped DMA.
264 * also need bit 30 for memory over 256MB.
266 if ((map->dm_segs[seg].ds_addr & 0x30000000) == 0)
267 map->dm_segs[seg].ds_addr |= 0x80000000;
268 else
269 map->dm_segs[seg].ds_addr |= 0xc0000000;
273 #define APBUS_NDMAMAP (NEWS5000_APBUS_MAPSIZE / NEWS5000_APBUS_MAPENT)
274 #define APBUS_MAPTBL(n, v) \
275 (*(volatile uint32_t *)(NEWS5000_APBUS_DMAMAP + \
276 NEWS5000_APBUS_MAPENT * (n) + 1) = (v))
277 static uint8_t apbus_dma_maptbl[APBUS_NDMAMAP];
279 static int
280 apbus_dma_mapalloc(bus_dma_tag_t t, bus_dmamap_t map, int flags)
282 int i, j, cnt;
284 cnt = round_page(map->_dm_size) / PAGE_SIZE;
286 again:
287 for (i = 0; i < APBUS_NDMAMAP; i += j + 1) {
288 for (j = 0; j < cnt; j++) {
289 if (apbus_dma_maptbl[i + j])
290 break;
292 if (j == cnt) {
293 for (j = 0; j < cnt; j++)
294 apbus_dma_maptbl[i + j] = 1;
295 map->_dm_maptbl = i;
296 map->_dm_maptblcnt = cnt;
297 return 0;
300 if ((flags & BUS_DMA_NOWAIT) == 0) {
301 tsleep(&apbus_dma_maptbl, PRIBIO, "apdmat", 0);
302 goto again;
304 return ENOMEM;
307 static void
308 apbus_dma_mapfree(bus_dma_tag_t t, bus_dmamap_t map)
310 int i, n;
312 if (map->_dm_maptblcnt > 0) {
313 n = map->_dm_maptbl;
314 for (i = 0; i < map->_dm_maptblcnt; i++, n++) {
315 #ifdef DIAGNOSTIC
316 if (apbus_dma_maptbl[n] == 0)
317 panic("freeing free DMA map");
318 APBUS_MAPTBL(n, 0xffffffff); /* causes DMA error */
319 #endif
320 apbus_dma_maptbl[n] = 0;
322 wakeup(&apbus_dma_maptbl);
323 map->_dm_maptblcnt = 0;
327 static void
328 apbus_dma_mapset(bus_dma_tag_t t, bus_dmamap_t map)
330 int i;
331 bus_addr_t addr, eaddr;
332 int seg;
333 bus_dma_segment_t *segs;
335 i = 0;
336 for (seg = 0; seg < map->dm_nsegs; seg++) {
337 segs = &map->dm_segs[seg];
338 for (addr = segs->ds_addr, eaddr = addr + segs->ds_len;
339 addr < eaddr; addr += PAGE_SIZE, i++) {
340 #ifdef DIAGNOSTIC
341 if (i >= map->_dm_maptblcnt)
342 panic("DMA map table overflow");
343 #endif
344 APBUS_MAPTBL(map->_dm_maptbl + i,
345 NEWS5000_APBUS_MAP_VALID |
346 NEWS5000_APBUS_MAP_COHERENT |
347 (addr >> PGSHIFT));
350 map->dm_segs[0].ds_addr = map->_dm_maptbl << PGSHIFT;
351 map->dm_segs[0].ds_len = map->dm_mapsize;
352 map->dm_nsegs = 1;
355 static int
356 apbus_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
357 bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
359 int error;
361 if (flags & NEWSMIPS_DMAMAP_MAPTBL)
362 nsegments = round_page(size) / PAGE_SIZE;
363 error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
364 flags, dmamp);
365 if (error == 0 && (flags & NEWSMIPS_DMAMAP_MAPTBL)) {
366 error = apbus_dma_mapalloc(t, *dmamp, flags);
367 if (error) {
368 _bus_dmamap_destroy(t, *dmamp);
369 *dmamp = NULL;
372 return error;
375 static void
376 apbus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
379 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
380 apbus_dma_mapfree(t, map);
381 _bus_dmamap_destroy(t, map);
384 static int
385 apbus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
386 bus_size_t buflen, struct proc *p, int flags)
388 int error;
390 error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
391 if (error == 0) {
392 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
393 apbus_dma_mapset(t, map);
394 else
395 apbus_dma_unmapped(t, map);
397 return error;
400 static int
401 apbus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
402 int flags)
404 int error;
406 error = _bus_dmamap_load_mbuf(t, map, m0, flags);
407 if (error == 0) {
408 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
409 apbus_dma_mapset(t, map);
410 else
411 apbus_dma_unmapped(t, map);
413 return error;
416 static int
417 apbus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
418 int flags)
420 int error;
422 error = _bus_dmamap_load_uio(t, map, uio, flags);
423 if (error == 0) {
424 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
425 apbus_dma_mapset(t, map);
426 else
427 apbus_dma_unmapped(t, map);
429 return error;
432 static int
433 apbus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
434 bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
436 int error;
438 error = _bus_dmamap_load_raw(t, map, segs, nsegs, size, flags);
439 if (error == 0) {
440 if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
441 apbus_dma_mapset(t, map);
442 else
443 apbus_dma_unmapped(t, map);
445 return error;
448 static void
449 apbus_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
450 bus_size_t len, int ops)
454 * Flush DMA cache by issuing IO read for the AProm of specified slot.
456 bus_space_read_4(t->_slotbaset, t->_slotbaseh, 0);
458 bus_dmamap_sync(&newsmips_default_bus_dma_tag, map, offset, len, ops);
461 struct newsmips_bus_dma_tag apbus_dma_tag = {
462 apbus_dmamap_create,
463 apbus_dmamap_destroy,
464 apbus_dmamap_load,
465 apbus_dmamap_load_mbuf,
466 apbus_dmamap_load_uio,
467 apbus_dmamap_load_raw,
468 _bus_dmamap_unload,
469 apbus_dmamap_sync,
470 _bus_dmamem_alloc,
471 _bus_dmamem_free,
472 _bus_dmamem_map,
473 _bus_dmamem_unmap,
474 _bus_dmamem_mmap,
477 struct newsmips_bus_dma_tag *
478 apbus_dmatag_init(struct apbus_attach_args *apa)
480 struct newsmips_bus_dma_tag *dmat;
482 dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
483 if (dmat != NULL) {
484 memcpy(dmat, &apbus_dma_tag, sizeof(*dmat));
485 dmat->_slotno = apa->apa_slotno;
486 dmat->_slotbaset = 0;
487 dmat->_slotbaseh = apa->apa_hwbase;
489 return dmat;