Home | History | Annotate | Line # | Download | only in apbus
apbus.c revision 1.21
      1 /*	$NetBSD: apbus.c,v 1.21 2008/04/09 15:40:30 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.21 2008/04/09 15:40:30 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(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);
     70 
     71 #define	MAXAPDEVNUM	32
     72 
     73 CFATTACH_DECL_NEW(ap, 0,
     74     apbusmatch, apbusattach, NULL, NULL);
     75 
     76 #define	NLEVEL	2
     77 static struct newsmips_intr apintr_tab[NLEVEL];
     78 
     79 static int
     80 apbusmatch(device_t parent, cfdata_t cf, void *aux)
     81 {
     82 	struct confargs *ca = aux;
     83 
     84 	if (strcmp(ca->ca_name, "ap") != 0)
     85 		return 0;
     86 
     87 	return 1;
     88 }
     89 
     90 
     91 static void
     92 apbusattach(device_t parent, device_t self, void *aux)
     93 {
     94 	struct apbus_attach_args child;
     95 	struct apbus_dev *apdev;
     96 	struct apbus_ctl *apctl;
     97 	struct newsmips_intr *ip;
     98 	int i;
     99 
    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;
    104 
    105 	aprint_normal("\n");
    106 
    107 	for (i = 0; i < NLEVEL; i++) {
    108 		ip = &apintr_tab[i];
    109 		LIST_INIT(&ip->intr_q);
    110 	}
    111 
    112 	/*
    113 	 * get first ap-device
    114 	 */
    115 	apdev = apbus_lookupdev(NULL);
    116 
    117 	/*
    118 	 * trace device chain
    119 	 */
    120 	while (apdev) {
    121 		apctl = apdev->apbd_ctl;
    122 
    123 		/*
    124 		 * probe physical device only
    125 		 * (no pseudo device)
    126 		 */
    127 		if (apctl && apctl->apbc_hwbase) {
    128 			/*
    129 			 * ... and, all units
    130 			 */
    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;
    137 
    138 				config_found(self, &child, apbusprint);
    139 
    140 				apctl = apctl->apbc_link;
    141 			}
    142 		}
    143 
    144 		apdev = apdev->apbd_link;
    145 	}
    146 }
    147 
    148 int
    149 apbusprint(void *aux, const char *pnp)
    150 {
    151 	struct apbus_attach_args *a = aux;
    152 
    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);
    156 
    157 	return UNCONF;
    158 }
    159 
    160 #if 0
    161 void *
    162 aptokseg0(void *va)
    163 {
    164 	vaddr_t addr = (vaddr_t)va;
    165 
    166 	if (addr >= 0xfff00000) {
    167 		addr -= 0xfff00000;
    168 		addr += physmem << PGSHIFT;
    169 		addr += 0x80000000;
    170 		va = (void *)addr;
    171 	}
    172 	return va;
    173 }
    174 #endif
    175 
    176 void
    177 apbus_wbflush(void)
    178 {
    179 	volatile int32_t *wbflush = (uint32_t *)NEWS5000_WBFLUSH;
    180 
    181 	(void)*wbflush;
    182 }
    183 
    184 /*
    185  * called by hardware interrupt routine
    186  */
    187 int
    188 apbus_intr_dispatch(int level, int stat)
    189 {
    190 	struct newsmips_intr *ip;
    191 	struct newsmips_intrhand *ih;
    192 	int nintr;
    193 
    194 	ip = &apintr_tab[level];
    195 
    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);
    200 	}
    201 	return nintr;
    202 }
    203 
    204 /*
    205  * register device interrupt routine
    206  */
    207 void *
    208 apbus_intr_establish(int level, int mask, int priority, int (*func)(void *),
    209     void *arg, const char *name, int ctlno)
    210 {
    211 	struct newsmips_intr *ip;
    212 	struct newsmips_intrhand *ih, *curih;
    213 	volatile uint32_t *inten0, *inten1;
    214 
    215 	ip = &apintr_tab[level];
    216 
    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;
    224 
    225 	if (LIST_EMPTY(&ip->intr_q)) {
    226 		LIST_INSERT_HEAD(&ip->intr_q, ih, ih_q);
    227 		goto done;
    228 	}
    229 
    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;
    236 		}
    237 	}
    238 
    239 	LIST_INSERT_AFTER(curih, ih, ih_q);
    240 
    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;
    251 	}
    252 
    253 	return (void *)ih;
    254 }
    255 
    256 static void
    257 apbus_dma_unmapped(bus_dma_tag_t t, bus_dmamap_t map)
    258 {
    259 	int seg;
    260 
    261 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    262 		/*
    263 		 * set MSB to indicate unmapped DMA.
    264 		 * also need bit 30 for memory over 256MB.
    265 		 */
    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;
    270 	}
    271 }
    272 
    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];
    278 
    279 static int
    280 apbus_dma_mapalloc(bus_dma_tag_t t, bus_dmamap_t map, int flags)
    281 {
    282 	int i, j, cnt;
    283 
    284 	cnt = round_page(map->_dm_size) / PAGE_SIZE;
    285 
    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;
    291 		}
    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;
    298 		}
    299 	}
    300 	if ((flags & BUS_DMA_NOWAIT) == 0) {
    301 		tsleep(&apbus_dma_maptbl, PRIBIO, "apdmat", 0);
    302 		goto again;
    303 	}
    304 	return ENOMEM;
    305 }
    306 
    307 static void
    308 apbus_dma_mapfree(bus_dma_tag_t t, bus_dmamap_t map)
    309 {
    310 	int i, n;
    311 
    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;
    321 		}
    322 		wakeup(&apbus_dma_maptbl);
    323 		map->_dm_maptblcnt = 0;
    324 	}
    325 }
    326 
    327 static void
    328 apbus_dma_mapset(bus_dma_tag_t t, bus_dmamap_t map)
    329 {
    330 	int i;
    331 	bus_addr_t addr, eaddr;
    332 	int seg;
    333 	bus_dma_segment_t *segs;
    334 
    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));
    348 		}
    349 	}
    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;
    353 }
    354 
    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)
    358 {
    359 	int error;
    360 
    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;
    370 		}
    371 	}
    372 	return error;
    373 }
    374 
    375 static void
    376 apbus_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    377 {
    378 
    379 	if (map->_dm_flags & NEWSMIPS_DMAMAP_MAPTBL)
    380 		apbus_dma_mapfree(t, map);
    381 	_bus_dmamap_destroy(t, map);
    382 }
    383 
    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)
    387 {
    388 	int error;
    389 
    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);
    396 	}
    397 	return error;
    398 }
    399 
    400 static int
    401 apbus_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
    402     int flags)
    403 {
    404 	int error;
    405 
    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);
    412 	}
    413 	return error;
    414 }
    415 
    416 static int
    417 apbus_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
    418     int flags)
    419 {
    420 	int error;
    421 
    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);
    428 	}
    429 	return error;
    430 }
    431 
    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)
    435 {
    436 	int error;
    437 
    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);
    444 	}
    445 	return error;
    446 }
    447 
    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)
    451 {
    452 
    453 	/*
    454 	 * Flush DMA cache by issuing IO read for the AProm of specified slot.
    455 	 */
    456 	bus_space_read_4(t->_slotbaset, t->_slotbaseh, 0);
    457 
    458 	bus_dmamap_sync(&newsmips_default_bus_dma_tag, map, offset, len, ops);
    459 }
    460 
    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,
    475 };
    476 
    477 struct newsmips_bus_dma_tag *
    478 apbus_dmatag_init(struct apbus_attach_args *apa)
    479 {
    480 	struct newsmips_bus_dma_tag *dmat;
    481 
    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;
    488 	}
    489 	return dmat;
    490 }
    491