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