Home | History | Annotate | Line # | Download | only in apbus
apbus.c revision 1.17
      1 /*	$NetBSD: apbus.c,v 1.17 2003/07/15 02:59:28 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1999 SHIMIZU Ryo.  All rights reserved.
      5  *
      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.
     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
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: apbus.c,v 1.17 2003/07/15 02:59:28 lukem Exp $");
     31 
     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>
     37 
     38 #include <uvm/uvm_extern.h>
     39 
     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>
     46 
     47 static int  apbusmatch (struct device *, struct cfdata *, void *);
     48 static void apbusattach (struct device *, struct device *, void *);
     49 static int apbusprint (void *, const char *);
     50 /* static void *aptokseg0 (void *); */
     51 static void apbus_dma_unmapped (bus_dma_tag_t, bus_dmamap_t);
     52 static int apbus_dma_mapalloc (bus_dma_tag_t, bus_dmamap_t, int);
     53 static void apbus_dma_mapfree (bus_dma_tag_t, bus_dmamap_t);
     54 static void apbus_dma_mapset (bus_dma_tag_t, bus_dmamap_t);
     55 static int apbus_dmamap_create (bus_dma_tag_t, bus_size_t, int, bus_size_t,
     56 			bus_size_t, int, bus_dmamap_t *);
     57 static void apbus_dmamap_destroy (bus_dma_tag_t, bus_dmamap_t);
     58 static int apbus_dmamap_load (bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t,
     59 			struct proc *, int);
     60 static int apbus_dmamap_load_mbuf (bus_dma_tag_t, bus_dmamap_t, struct mbuf *,
     61 			int);
     62 static int apbus_dmamap_load_uio (bus_dma_tag_t, bus_dmamap_t, struct uio *,
     63 			int);
     64 static int apbus_dmamap_load_raw (bus_dma_tag_t, bus_dmamap_t,
     65 			bus_dma_segment_t *, int, bus_size_t, int);
     66 static void apbus_dmamap_sync (bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
     67 			bus_size_t, int);
     68 
     69 #define	MAXAPDEVNUM	32
     70 
     71 struct apbus_softc {
     72 	struct device apbs_dev;
     73 };
     74 
     75 CFATTACH_DECL(ap, sizeof(struct apbus_softc),
     76     apbusmatch, apbusattach, NULL, NULL);
     77 
     78 #define	NLEVEL	2
     79 static struct newsmips_intr apintr_tab[NLEVEL];
     80 
     81 static int
     82 apbusmatch(parent, cfdata, aux)
     83 	struct device *parent;
     84 	struct cfdata *cfdata;
     85 	void *aux;
     86 {
     87 	struct confargs *ca = aux;
     88 
     89 	if (strcmp(ca->ca_name, "ap") != 0)
     90 		return 0;
     91 
     92 	return 1;
     93 }
     94 
     95 
     96 static void
     97 apbusattach(parent, self, aux)
     98 	struct device *parent;
     99 	struct device *self;
    100 	void *aux;
    101 {
    102 	struct apbus_attach_args child;
    103 	struct apbus_dev *apdev;
    104 	struct apbus_ctl *apctl;
    105 	struct newsmips_intr *ip;
    106 	int i;
    107 
    108 	*(volatile u_int *)(NEWS5000_APBUS_INTST) = 0xffffffff;
    109 	*(volatile u_int *)(NEWS5000_APBUS_INTMSK) = 0xffffffff;
    110 	*(volatile u_int *)(NEWS5000_APBUS_CTRL) = 0x00000004;
    111 	*(volatile u_int *)(NEWS5000_APBUS_DMA) = 0xffffffff;
    112 
    113 	printf("\n");
    114 
    115 	for (i = 0; i < NLEVEL; i++) {
    116 		ip = &apintr_tab[i];
    117 		LIST_INIT(&ip->intr_q);
    118 	}
    119 
    120 	/*
    121 	 * get first ap-device
    122 	 */
    123 	apdev = apbus_lookupdev(NULL);
    124 
    125 	/*
    126 	 * trace device chain
    127 	 */
    128 	while (apdev) {
    129 		apctl = apdev->apbd_ctl;
    130 
    131 		/*
    132 		 * probe physical device only
    133 		 * (no pseudo device)
    134 		 */
    135 		if (apctl && apctl->apbc_hwbase) {
    136 			/*
    137 			 * ... and, all units
    138 			 */
    139 			while (apctl) {
    140 				/* make apbus_attach_args for devices */
    141 				child.apa_name = apdev->apbd_name;
    142 				child.apa_ctlnum = apctl->apbc_ctlno;
    143 				child.apa_slotno = apctl->apbc_sl;
    144 				child.apa_hwbase = apctl->apbc_hwbase;
    145 
    146 				config_found(self, &child, apbusprint);
    147 
    148 				apctl = apctl->apbc_link;
    149 			}
    150 		}
    151 
    152 		apdev = apdev->apbd_link;
    153 	}
    154 }
    155 
    156 int
    157 apbusprint(aux, pnp)
    158 	void *aux;
    159 	const char *pnp;
    160 {
    161 	struct apbus_attach_args *a = aux;
    162 
    163 	if (pnp)
    164 		aprint_normal("%s at %s slot%d addr 0x%lx",
    165 			a->apa_name, pnp, a->apa_slotno, a->apa_hwbase);
    166 
    167 	return UNCONF;
    168 }
    169 
    170 #if 0
    171 void *
    172 aptokseg0(va)
    173 	void *va;
    174 {
    175 	vaddr_t addr = (vaddr_t)va;
    176 
    177 	if (addr >= 0xfff00000) {
    178 		addr -= 0xfff00000;
    179 		addr += physmem << PGSHIFT;
    180 		addr += 0x80000000;
    181 		va = (void *)addr;
    182 	}
    183 	return va;
    184 }
    185 #endif
    186 
    187 void
    188 apbus_wbflush()
    189 {
    190 	volatile int *wbflush = (int *)NEWS5000_WBFLUSH;
    191 
    192 	(void)*wbflush;
    193 }
    194 
    195 /*
    196  * called by hardware interrupt routine
    197  */
    198 int
    199 apbus_intr_dispatch(level, stat)
    200 	int level;
    201 	int stat;
    202 {
    203 	struct newsmips_intr *ip;
    204 	struct newsmips_intrhand *ih;
    205 	int nintr;
    206 
    207 	ip = &apintr_tab[level];
    208 
    209 	nintr = 0;
    210 	LIST_FOREACH(ih, &ip->intr_q, ih_q) {
    211 		if (ih->ih_mask & stat)
    212 			nintr += (*ih->ih_func)(ih->ih_arg);
    213 	}
    214 	return nintr;
    215 }
    216 
    217 /*
    218  * register device interrupt routine
    219  */
    220 void *
    221 apbus_intr_establish(level, mask, priority, func, arg, name, ctlno)
    222 	int level;
    223 	int mask;
    224 	int priority;
    225 	int (*func) (void *);
    226 	void *arg;
    227 	char *name;
    228 	int ctlno;
    229 {
    230 	struct newsmips_intr *ip;
    231 	struct newsmips_intrhand *ih, *curih;
    232 	volatile u_int32_t *inten0, *inten1;
    233 
    234 	ip = &apintr_tab[level];
    235 
    236 	ih = malloc(sizeof(*ih), M_DEVBUF, M_NOWAIT);
    237 	if (ih == NULL)
    238 		panic("apbus_intr_establish: can't malloc handler info");
    239 	ih->ih_mask = mask;
    240 	ih->ih_priority = priority;
    241 	ih->ih_func = func;
    242 	ih->ih_arg = arg;
    243 
    244 	if (LIST_EMPTY(&ip->intr_q)) {
    245 		LIST_INSERT_HEAD(&ip->intr_q, ih, ih_q);
    246 		goto done;
    247 	}
    248 
    249 	for (curih = LIST_FIRST(&ip->intr_q);
    250 	    LIST_NEXT(curih, ih_q) != NULL;
    251 	    curih = LIST_NEXT(curih, ih_q)) {
    252 		if (ih->ih_priority > curih->ih_priority) {
    253 			LIST_INSERT_BEFORE(curih, ih, ih_q);
    254 			goto done;
    255 		}
    256 	}
    257 
    258 	LIST_INSERT_AFTER(curih, ih, ih_q);
    259 
    260  done:
    261 	switch (level) {
    262 	case 0:
    263 		inten0 = (volatile u_int32_t *)NEWS5000_INTEN0;
    264 		*inten0 |= mask;
    265 		break;
    266 	case 1:
    267 		inten1 = (volatile u_int32_t *)NEWS5000_INTEN1;
    268 		*inten1 |= mask;
    269 		break;
    270 	}
    271 
    272 	return (void *)ih;
    273 }
    274 
    275 static void
    276 apbus_dma_unmapped(t, map)
    277 	bus_dma_tag_t t;
    278 	bus_dmamap_t map;
    279 {
    280 	int seg;
    281 
    282 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    283 		/*
    284 		 * set MSB to indicate unmapped DMA.
    285 		 * also need bit 30 for memory over 256MB.
    286 		 */
    287 		if ((map->dm_segs[seg].ds_addr & 0x30000000) == 0)
    288 			map->dm_segs[seg].ds_addr |= 0x80000000;
    289 		else
    290 			map->dm_segs[seg].ds_addr |= 0xc0000000;
    291 	}
    292 }
    293 
    294 #define	APBUS_NDMAMAP	(NEWS5000_APBUS_MAPSIZE / NEWS5000_APBUS_MAPENT)
    295 #define	APBUS_MAPTBL(n, v)	(*(volatile u_int *)(NEWS5000_APBUS_DMAMAP + \
    296 			 NEWS5000_APBUS_MAPENT * (n) + 1) = (v))
    297 static u_char apbus_dma_maptbl[APBUS_NDMAMAP];
    298 
    299 static int
    300 apbus_dma_mapalloc(t, map, flags)
    301 	bus_dma_tag_t t;
    302 	bus_dmamap_t map;
    303 	int flags;
    304 {
    305 	int i, j, cnt;
    306 
    307 	cnt = round_page(map->_dm_size) / PAGE_SIZE;
    308 
    309  again:
    310 	for (i = 0; i < APBUS_NDMAMAP; i += j + 1) {
    311 		for (j = 0; j < cnt; j++) {
    312 			if (apbus_dma_maptbl[i + j])
    313 				break;
    314 		}
    315 		if (j == cnt) {
    316 			for (j = 0; j < cnt; j++)
    317 				apbus_dma_maptbl[i + j] = 1;
    318 			map->_dm_maptbl = i;
    319 			map->_dm_maptblcnt = cnt;
    320 			return 0;
    321 		}
    322 	}
    323 	if ((flags & BUS_DMA_NOWAIT) == 0) {
    324 		tsleep(&apbus_dma_maptbl, PRIBIO, "apdmat", 0);
    325 		goto again;
    326 	}
    327 	return ENOMEM;
    328 }
    329 
    330 static void
    331 apbus_dma_mapfree(t, map)
    332 	bus_dma_tag_t t;
    333 	bus_dmamap_t map;
    334 {
    335 	int i, n;
    336 
    337 	if (map->_dm_maptblcnt > 0) {
    338 		n = map->_dm_maptbl;
    339 		for (i = 0; i < map->_dm_maptblcnt; i++, n++) {
    340 #ifdef DIAGNOSTIC
    341 			if (apbus_dma_maptbl[n] == 0)
    342 				panic("freeing free DMA map");
    343 			APBUS_MAPTBL(n, 0xffffffff);	/* causes DMA error */
    344 #endif
    345 			apbus_dma_maptbl[n] = 0;
    346 		}
    347 		wakeup(&apbus_dma_maptbl);
    348 		map->_dm_maptblcnt = 0;
    349 	}
    350 }
    351 
    352 static void
    353 apbus_dma_mapset(t, map)
    354 	bus_dma_tag_t t;
    355 	bus_dmamap_t map;
    356 {
    357 	int i;
    358 	bus_addr_t addr, eaddr;
    359 	int seg;
    360 	bus_dma_segment_t *segs;
    361 
    362 	i = 0;
    363 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    364 		segs = &map->dm_segs[seg];
    365 		for (addr = segs->ds_addr, eaddr = addr + segs->ds_len;
    366 		    addr < eaddr; addr += PAGE_SIZE, i++) {
    367 #ifdef DIAGNOSTIC
    368 			if (i >= map->_dm_maptblcnt)
    369 				panic("DMA map table overflow");
    370 #endif
    371 			APBUS_MAPTBL(map->_dm_maptbl + i,
    372 				NEWS5000_APBUS_MAP_VALID |
    373 				NEWS5000_APBUS_MAP_COHERENT |
    374 				(addr >> PGSHIFT));
    375 		}
    376 	}
    377 	map->dm_segs[0].ds_addr = map->_dm_maptbl << PGSHIFT;
    378 	map->dm_segs[0].ds_len = map->dm_mapsize;
    379 	map->dm_nsegs = 1;
    380 }
    381 
    382 static int
    383 apbus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
    384 	bus_dma_tag_t t;
    385 	bus_size_t size;
    386 	int nsegments;
    387 	bus_size_t maxsegsz;
    388 	bus_size_t boundary;
    389 	int flags;
    390 	bus_dmamap_t *dmamp;
    391 {
    392 	int error;
    393 
    394 	if (flags & NEWSMIPS_DMAMAP_MAPTBL)
    395 		nsegments = round_page(size) / PAGE_SIZE;
    396 	error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
    397 	    flags, dmamp);
    398 	if (error == 0 && (flags & NEWSMIPS_DMAMAP_MAPTBL)) {
    399 		error = apbus_dma_mapalloc(t, *dmamp, flags);
    400 		if (error) {
    401 			_bus_dmamap_destroy(t, *dmamp);
    402 			*dmamp = NULL;
    403 		}
    404 	}
    405 	return error;
    406 }
    407 
    408 static void
    409 apbus_dmamap_destroy(t, map)
    410 	bus_dma_tag_t t;
    411 	bus_dmamap_t map;
    412 {
    413 	if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
    414 		apbus_dma_mapfree(t, map);
    415 	_bus_dmamap_destroy(t, map);
    416 }
    417 
    418 static int
    419 apbus_dmamap_load(t, map, buf, buflen, p, flags)
    420 	bus_dma_tag_t t;
    421 	bus_dmamap_t map;
    422 	void *buf;
    423 	bus_size_t buflen;
    424 	struct proc *p;
    425 	int flags;
    426 {
    427 	int error;
    428 
    429 	error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
    430 	if (error == 0) {
    431 		if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
    432 			apbus_dma_mapset(t, map);
    433 		else
    434 			apbus_dma_unmapped(t, map);
    435 	}
    436 	return error;
    437 }
    438 
    439 static int
    440 apbus_dmamap_load_mbuf(t, map, m0, flags)
    441 	bus_dma_tag_t t;
    442 	bus_dmamap_t map;
    443 	struct mbuf *m0;
    444 	int flags;
    445 {
    446 	int error;
    447 
    448 	error = _bus_dmamap_load_mbuf(t, map, m0, flags);
    449 	if (error == 0) {
    450 		if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
    451 			apbus_dma_mapset(t, map);
    452 		else
    453 			apbus_dma_unmapped(t, map);
    454 	}
    455 	return error;
    456 }
    457 
    458 static int
    459 apbus_dmamap_load_uio(t, map, uio, flags)
    460 	bus_dma_tag_t t;
    461 	bus_dmamap_t map;
    462 	struct uio *uio;
    463 	int flags;
    464 {
    465 	int error;
    466 
    467 	error = _bus_dmamap_load_uio(t, map, uio, flags);
    468 	if (error == 0) {
    469 		if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
    470 			apbus_dma_mapset(t, map);
    471 		else
    472 			apbus_dma_unmapped(t, map);
    473 	}
    474 	return error;
    475 }
    476 
    477 static int
    478 apbus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
    479 	bus_dma_tag_t t;
    480 	bus_dmamap_t map;
    481 	bus_dma_segment_t *segs;
    482 	int nsegs;
    483 	bus_size_t size;
    484 	int flags;
    485 {
    486 	int error;
    487 
    488 	error = _bus_dmamap_load_raw(t, map, segs, nsegs, size, flags);
    489 	if (error == 0) {
    490 		if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
    491 			apbus_dma_mapset(t, map);
    492 		else
    493 			apbus_dma_unmapped(t, map);
    494 	}
    495 	return error;
    496 }
    497 
    498 static void
    499 apbus_dmamap_sync(t, map, offset, len, ops)
    500 	bus_dma_tag_t t;
    501 	bus_dmamap_t map;
    502 	bus_addr_t offset;
    503 	bus_size_t len;
    504 	int ops;
    505 {
    506 
    507 	/*
    508 	 * Flush DMA cache by issuing IO read for the AProm of specified slot.
    509 	 */
    510 	bus_space_read_4(t->_slotbaset, t->_slotbaseh, 0);
    511 
    512 	bus_dmamap_sync(&newsmips_default_bus_dma_tag, map, offset, len, ops);
    513 }
    514 
    515 struct newsmips_bus_dma_tag apbus_dma_tag = {
    516 	apbus_dmamap_create,
    517 	apbus_dmamap_destroy,
    518 	apbus_dmamap_load,
    519 	apbus_dmamap_load_mbuf,
    520 	apbus_dmamap_load_uio,
    521 	apbus_dmamap_load_raw,
    522 	_bus_dmamap_unload,
    523 	apbus_dmamap_sync,
    524 	_bus_dmamem_alloc,
    525 	_bus_dmamem_free,
    526 	_bus_dmamem_map,
    527 	_bus_dmamem_unmap,
    528 	_bus_dmamem_mmap,
    529 };
    530 
    531 struct newsmips_bus_dma_tag *
    532 apbus_dmatag_init(apa)
    533 	struct apbus_attach_args *apa;
    534 {
    535 	struct newsmips_bus_dma_tag *dmat;
    536 
    537 	dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT);
    538 	if (dmat != NULL) {
    539 		memcpy(dmat, &apbus_dma_tag, sizeof(*dmat));
    540 		dmat->_slotno = apa->apa_slotno;
    541 		dmat->_slotbaset = 0;
    542 		dmat->_slotbaseh = apa->apa_hwbase;
    543 	}
    544 	return dmat;
    545 }
    546