Home | History | Annotate | Line # | Download | only in qbus
uba.c revision 1.66.2.1
      1 /*	$NetBSD: uba.c,v 1.66.2.1 2004/08/03 10:50:28 skrll Exp $	   */
      2 /*
      3  * Copyright (c) 1982, 1986 The Regents of the University of California.
      4  * 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. Neither the name of the University nor the names of its contributors
     15  *    may be used to endorse or promote products derived from this software
     16  *    without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  *
     30  *	@(#)uba.c	7.10 (Berkeley) 12/16/90
     31  *	@(#)autoconf.c	7.20 (Berkeley) 5/9/91
     32  */
     33 
     34 /*
     35  * Copyright (c) 1996 Jonathan Stone.
     36  * Copyright (c) 1994, 1996 Ludd, University of Lule}, Sweden.
     37  * All rights reserved.
     38  *
     39  * Redistribution and use in source and binary forms, with or without
     40  * modification, are permitted provided that the following conditions
     41  * are met:
     42  * 1. Redistributions of source code must retain the above copyright
     43  *    notice, this list of conditions and the following disclaimer.
     44  * 2. Redistributions in binary form must reproduce the above copyright
     45  *    notice, this list of conditions and the following disclaimer in the
     46  *    documentation and/or other materials provided with the distribution.
     47  * 3. All advertising materials mentioning features or use of this software
     48  *    must display the following acknowledgement:
     49  *	This product includes software developed by the University of
     50  *	California, Berkeley and its contributors.
     51  * 4. Neither the name of the University nor the names of its contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  *	@(#)uba.c	7.10 (Berkeley) 12/16/90
     68  *	@(#)autoconf.c	7.20 (Berkeley) 5/9/91
     69  */
     70 
     71 #include <sys/cdefs.h>
     72 __KERNEL_RCSID(0, "$NetBSD: uba.c,v 1.66.2.1 2004/08/03 10:50:28 skrll Exp $");
     73 
     74 #include <sys/param.h>
     75 #include <sys/time.h>
     76 #include <sys/systm.h>
     77 #include <sys/buf.h>
     78 #include <sys/proc.h>
     79 #include <sys/user.h>
     80 #include <sys/conf.h>
     81 #include <sys/kernel.h>
     82 #include <sys/malloc.h>
     83 #include <sys/device.h>
     84 
     85 #include <uvm/uvm_extern.h>
     86 
     87 #include <machine/bus.h>
     88 #include <machine/scb.h>
     89 #include <machine/cpu.h>
     90 
     91 #include <dev/qbus/ubareg.h>
     92 #include <dev/qbus/ubavar.h>
     93 
     94 #include "ioconf.h"
     95 
     96 static int ubasearch (struct device *, struct cfdata *, void *);
     97 static int ubaprint (void *, const char *);
     98 
     99 /*
    100  * If we failed to allocate uba resources, put us on a queue to wait
    101  * until there is available resources. Resources to compete about
    102  * are map registers and BDPs. This is normally only a problem on
    103  * Unibus systems, Qbus systems have more map registers than usable.
    104  */
    105 void
    106 uba_enqueue(struct uba_unit *uu)
    107 {
    108 	struct uba_softc *uh;
    109 	int s;
    110 
    111 	uh = (void *)((struct device *)(uu->uu_softc))->dv_parent;
    112 
    113 	s = spluba();
    114 	SIMPLEQ_INSERT_TAIL(&uh->uh_resq, uu, uu_resq);
    115 	splx(s);
    116 }
    117 
    118 /*
    119  * When a routine that uses resources is finished, the next device
    120  * in queue for map registers etc is called. If it succeeds to get
    121  * resources, call next, and next, and next...
    122  * This routine must be called at spluba.
    123  */
    124 void
    125 uba_done(struct uba_softc *uh)
    126 {
    127 	struct uba_unit *uu;
    128 
    129 	while ((uu = SIMPLEQ_FIRST(&uh->uh_resq))) {
    130 		SIMPLEQ_REMOVE_HEAD(&uh->uh_resq, uu_resq);
    131 		if ((*uu->uu_ready)(uu) == 0) {
    132 			SIMPLEQ_INSERT_HEAD(&uh->uh_resq, uu, uu_resq);
    133 			break;
    134 		}
    135 	}
    136 }
    137 
    138 /*
    139  * Each device that needs some handling if an ubareset occurs must
    140  * register for reset first through this routine.
    141  */
    142 void
    143 uba_reset_establish(void (*reset)(struct device *), struct device *dev)
    144 {
    145 	struct uba_softc *uh = (void *)dev->dv_parent;
    146 	struct uba_reset *ur;
    147 
    148 	ur = malloc(sizeof(struct uba_reset), M_DEVBUF, M_NOWAIT);
    149 	if (ur == NULL)
    150 		panic("uba_reset_establish");
    151 	ur->ur_dev = dev;
    152 	ur->ur_reset = reset;
    153 
    154 	SIMPLEQ_INSERT_TAIL(&uh->uh_resetq, ur, ur_resetq);
    155 }
    156 
    157 /*
    158  * Allocate a bunch of map registers and map them to the given address.
    159  */
    160 int
    161 uballoc(struct uba_softc *uh, struct ubinfo *ui, int flags)
    162 {
    163 	int waitok = (flags & UBA_CANTWAIT) == 0;
    164 	int error;
    165 
    166 	if ((error = bus_dmamap_create(uh->uh_dmat, ui->ui_size, 1,
    167 	    ui->ui_size, 0, (waitok ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT),
    168 	    &ui->ui_dmam)))
    169 		return error;
    170 
    171 	if ((error = bus_dmamap_load(uh->uh_dmat, ui->ui_dmam, ui->ui_vaddr,
    172 	    ui->ui_size, NULL, (waitok ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT)))) {
    173 		bus_dmamap_destroy(uh->uh_dmat, ui->ui_dmam);
    174 		return error;
    175 	}
    176 	ui->ui_baddr = ui->ui_dmam->dm_segs[0].ds_addr;
    177 	return 0;
    178 }
    179 
    180 /*
    181  * Allocate DMA-able memory and map it on the unibus.
    182  */
    183 int
    184 ubmemalloc(struct uba_softc *uh, struct ubinfo *ui, int flags)
    185 {
    186 	int waitok = (flags & UBA_CANTWAIT ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
    187 	int error;
    188 
    189 	if ((error = bus_dmamem_alloc(uh->uh_dmat, ui->ui_size, PAGE_SIZE, 0,
    190 	    &ui->ui_seg, 1, &ui->ui_rseg, waitok)))
    191 		return error;
    192 	if ((error = bus_dmamem_map(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg,
    193 	    ui->ui_size, &ui->ui_vaddr, waitok|BUS_DMA_COHERENT))) {
    194 		bus_dmamem_free(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg);
    195 		return error;
    196 	}
    197 	if ((error = uballoc(uh, ui, flags))) {
    198 		bus_dmamem_unmap(uh->uh_dmat, ui->ui_vaddr, ui->ui_size);
    199 		bus_dmamem_free(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg);
    200 	}
    201 	return error;
    202 }
    203 
    204 void
    205 ubfree(struct uba_softc *uh, struct ubinfo *ui)
    206 {
    207 	bus_dmamap_unload(uh->uh_dmat, ui->ui_dmam);
    208 	bus_dmamap_destroy(uh->uh_dmat, ui->ui_dmam);
    209 }
    210 
    211 void
    212 ubmemfree(struct uba_softc *uh, struct ubinfo *ui)
    213 {
    214 	bus_dmamem_unmap(uh->uh_dmat, ui->ui_vaddr, ui->ui_size);
    215 	bus_dmamem_free(uh->uh_dmat, &ui->ui_seg, ui->ui_rseg);
    216 	ubfree(uh, ui);
    217 }
    218 
    219 /*
    220  * Generate a reset on uba number uban.	 Then
    221  * call each device that asked to be called during attach,
    222  * giving it a chance to clean up so as to be able to continue.
    223  */
    224 void
    225 ubareset(struct uba_softc *uh)
    226 {
    227 	struct uba_reset *ur;
    228 	int s;
    229 
    230 	s = spluba();
    231 	SIMPLEQ_INIT(&uh->uh_resq);
    232 	printf("%s: reset", uh->uh_dev.dv_xname);
    233 	(*uh->uh_ubainit)(uh);
    234 
    235 	ur = SIMPLEQ_FIRST(&uh->uh_resetq);
    236 	if (ur) do {
    237 		printf(" %s", ur->ur_dev->dv_xname);
    238 		(*ur->ur_reset)(ur->ur_dev);
    239 	} while ((ur = SIMPLEQ_NEXT(ur, ur_resetq)));
    240 
    241 	printf("\n");
    242 	splx(s);
    243 }
    244 
    245 /*
    246  * The common attach routine:
    247  *   Calls the scan routine to search for uba devices.
    248  */
    249 void
    250 uba_attach(struct uba_softc *sc, paddr_t iopagephys)
    251 {
    252 
    253 	/*
    254 	 * Set last free interrupt vector for devices with
    255 	 * programmable interrupt vectors.  Use is to decrement
    256 	 * this number and use result as interrupt vector.
    257 	 */
    258 	sc->uh_lastiv = 0x200;
    259 	SIMPLEQ_INIT(&sc->uh_resq);
    260 	SIMPLEQ_INIT(&sc->uh_resetq);
    261 
    262 	/*
    263 	 * Allocate place for unibus I/O space in virtual space.
    264 	 */
    265 	if (bus_space_map(sc->uh_iot, iopagephys, UBAIOSIZE, 0, &sc->uh_ioh))
    266 		return;
    267 
    268 	/*
    269 	 * Keep track of which addressed devices are found.
    270 	 */
    271 	sc->uh_used = malloc(UBAIOSIZE, M_TEMP, M_WAITOK);
    272 	memset(sc->uh_used, 0, UBAIOSIZE);
    273 
    274 	if (sc->uh_beforescan)
    275 		(*sc->uh_beforescan)(sc);
    276 	/*
    277 	 * Now start searching for devices.
    278 	 */
    279 	config_search(ubasearch,(struct device *)sc, NULL);
    280 
    281 	if (sc->uh_afterscan)
    282 		(*sc->uh_afterscan)(sc);
    283 
    284 	free(sc->uh_used, M_TEMP);
    285 }
    286 
    287 int
    288 ubasearch(struct device *parent, struct cfdata *cf, void *aux)
    289 {
    290 	struct	uba_softc *sc = (struct uba_softc *)parent;
    291 	struct	uba_attach_args ua;
    292 	int	i, vec, br;
    293 
    294 	if (sc->uh_used[ubdevreg(cf->cf_loc[0])])
    295 		return 0; /* something are already at this address */
    296 
    297 	ua.ua_ioh = ubdevreg(cf->cf_loc[0]) + sc->uh_ioh;
    298 	ua.ua_iot = sc->uh_iot;
    299 	ua.ua_dmat = sc->uh_dmat;
    300 
    301 	if (badaddr((caddr_t)ua.ua_ioh, 2) ||
    302 	    (sc->uh_errchk ? (*sc->uh_errchk)(sc):0))
    303 		goto forgetit;
    304 
    305 	scb_vecref(0, 0); /* Clear vector ref */
    306 	i = config_match(parent, cf, &ua);
    307 
    308 	if (sc->uh_errchk)
    309 		if ((*sc->uh_errchk)(sc))
    310 			goto forgetit;
    311 	if (i == 0)
    312 		goto forgetit;
    313 
    314 	i = scb_vecref(&vec, &br);
    315 	if (i == 0)
    316 		goto fail;
    317 	if (vec == 0)
    318 		goto fail;
    319 
    320 	ua.ua_br = br;
    321 	ua.ua_cvec = vec;
    322 	ua.ua_iaddr = cf->cf_loc[0];
    323 	ua.ua_evcnt = NULL;
    324 
    325 	sc->uh_used[ubdevreg(cf->cf_loc[0])] = 1;
    326 
    327 	config_attach(parent, cf, &ua, ubaprint);
    328 	return 0;
    329 
    330 fail:
    331 	printf("%s%d at %s csr %o %s\n",
    332 	    cf->cf_name, cf->cf_unit, parent->dv_xname,
    333 	    cf->cf_loc[0], (i ? "zero vector" : "didn't interrupt"));
    334 
    335 forgetit:
    336 	return 0;
    337 }
    338 
    339 /*
    340  * Print out some interesting info common to all unibus devices.
    341  */
    342 int
    343 ubaprint(void *aux, const char *uba)
    344 {
    345 	struct uba_attach_args *ua = aux;
    346 
    347 	aprint_normal(" csr %o vec %o ipl %x", ua->ua_iaddr,
    348 	    ua->ua_cvec & 511, ua->ua_br);
    349 	return UNCONF;
    350 }
    351 
    352 /*
    353  * Move to machdep eventually
    354  */
    355 void
    356 uba_intr_establish(void *icookie, int vec, void (*ifunc)(void *iarg),
    357 	void *iarg, struct evcnt *ev)
    358 {
    359 	scb_vecalloc(vec, ifunc, iarg, SCB_ISTACK, ev);
    360 }
    361