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