1 /* $NetBSD: obio.c,v 1.53 2002/08/25 17:54:58 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1997,1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Paul Kranenburg. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/device.h> 43 #include <sys/malloc.h> 44 45 #ifdef DEBUG 46 #include <sys/proc.h> 47 #include <sys/syslog.h> 48 #endif 49 50 #include <uvm/uvm_extern.h> 51 52 #include <machine/bus.h> 53 #include <sparc/dev/sbusvar.h> 54 #include <machine/autoconf.h> 55 #include <machine/oldmon.h> 56 #include <machine/cpu.h> 57 #include <machine/ctlreg.h> 58 #include <sparc/sparc/asm.h> 59 #include <sparc/sparc/vaddrs.h> 60 #include <sparc/sparc/cpuvar.h> 61 62 struct obio4_softc { 63 struct device sc_dev; /* base device */ 64 bus_space_tag_t sc_bustag; /* parent bus tag */ 65 bus_dma_tag_t sc_dmatag; /* parent bus dma tag */ 66 }; 67 68 union obio_softc { 69 struct device sc_dev; /* base device */ 70 struct obio4_softc sc_obio; /* sun4 obio */ 71 struct sbus_softc sc_sbus; /* sun4m obio is another sbus slot */ 72 }; 73 74 75 /* autoconfiguration driver */ 76 static int obiomatch __P((struct device *, struct cfdata *, void *)); 77 static void obioattach __P((struct device *, struct device *, void *)); 78 79 struct cfattach obio_ca = { 80 sizeof(union obio_softc), obiomatch, obioattach 81 }; 82 83 84 /* 85 * This `obio4_busattachargs' data structure only exists to pass down 86 * to obiosearch() the name of a device that must be configured early. 87 */ 88 struct obio4_busattachargs { 89 struct mainbus_attach_args *ma; 90 const char *name; 91 }; 92 93 #if defined(SUN4) 94 static int obioprint __P((void *, const char *)); 95 static int obiosearch __P((struct device *, struct cfdata *, void *)); 96 static paddr_t obio_bus_mmap __P((bus_space_tag_t, bus_addr_t, off_t, 97 int, int)); 98 static int _obio_bus_map __P((bus_space_tag_t, bus_addr_t, 99 bus_size_t, int, 100 vaddr_t, bus_space_handle_t *)); 101 102 static struct sparc_bus_space_tag obio_space_tag = { 103 NULL, /* cookie */ 104 NULL, /* parent bus tag */ 105 NULL, /* ranges */ 106 0, /* nranges */ 107 _obio_bus_map, /* bus_space_map */ 108 NULL, /* bus_space_unmap */ 109 NULL, /* bus_space_subregion */ 110 NULL, /* bus_space_barrier */ 111 obio_bus_mmap, /* bus_space_mmap */ 112 NULL /* bus_intr_establish */ 113 }; 114 #endif 115 116 /* 117 * Translate obio `interrupts' property value to processor IPL (see sbus.c) 118 * Apparently, the `interrupts' property on obio devices is just 119 * the processor IPL. 120 */ 121 static int intr_obio2ipl[] = { 122 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 123 }; 124 125 int 126 obiomatch(parent, cf, aux) 127 struct device *parent; 128 struct cfdata *cf; 129 void *aux; 130 { 131 struct mainbus_attach_args *ma = aux; 132 133 return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0); 134 } 135 136 void 137 obioattach(parent, self, aux) 138 struct device *parent, *self; 139 void *aux; 140 { 141 struct mainbus_attach_args *ma = aux; 142 143 /* 144 * There is only one obio bus 145 */ 146 if (self->dv_unit > 0) { 147 printf(" unsupported\n"); 148 return; 149 } 150 printf("\n"); 151 152 if (CPU_ISSUN4) { 153 #if defined(SUN4) 154 struct obio4_softc *sc = &((union obio_softc *)self)->sc_obio; 155 struct obio4_busattachargs oa; 156 const char *const *cpp; 157 static const char *const special4[] = { 158 /* find these first */ 159 "timer", 160 "dma", /* need this before `esp', if any */ 161 NULL 162 }; 163 164 sc->sc_bustag = ma->ma_bustag; 165 sc->sc_dmatag = ma->ma_dmatag; 166 167 obio_space_tag.cookie = sc; 168 obio_space_tag.parent = sc->sc_bustag; 169 170 oa.ma = ma; 171 172 /* Find all `early' obio devices */ 173 for (cpp = special4; *cpp != NULL; cpp++) { 174 oa.name = *cpp; 175 (void)config_search(obiosearch, self, &oa); 176 } 177 178 /* Find all other obio devices */ 179 oa.name = NULL; 180 (void)config_search(obiosearch, self, &oa); 181 #endif 182 return; 183 } else if (CPU_ISSUN4M) { 184 /* 185 * Attach the on-board I/O bus at on a sun4m. 186 * In this case we treat the obio bus as another sbus slot. 187 */ 188 struct sbus_softc *sc = &((union obio_softc *)self)->sc_sbus; 189 190 static const char *const special4m[] = { 191 /* find these first */ 192 "eeprom", 193 "counter", 194 #if 0 /* Not all sun4m's have an `auxio' */ 195 "auxio", 196 #endif 197 "", 198 /* place device to ignore here */ 199 "interrupt", 200 NULL 201 }; 202 203 sc->sc_bustag = ma->ma_bustag; 204 sc->sc_dmatag = ma->ma_dmatag; 205 sc->sc_intr2ipl = intr_obio2ipl; 206 207 sbus_attach_common(sc, "obio", ma->ma_node, special4m); 208 } else { 209 printf("obio on this machine?\n"); 210 } 211 } 212 213 #if defined(SUN4) 214 int 215 obioprint(args, busname) 216 void *args; 217 const char *busname; 218 { 219 union obio_attach_args *uoba = args; 220 struct obio4_attach_args *oba = &uoba->uoba_oba4; 221 222 printf(" addr 0x%lx", (u_long)BUS_ADDR_PADDR(oba->oba_paddr)); 223 if (oba->oba_pri != -1) 224 printf(" level %d", oba->oba_pri); 225 226 return (UNCONF); 227 } 228 229 int 230 _obio_bus_map(t, ba, size, flags, va, hp) 231 bus_space_tag_t t; 232 bus_addr_t ba; 233 bus_size_t size; 234 int flags; 235 vaddr_t va; 236 bus_space_handle_t *hp; 237 { 238 struct obio4_softc *sc = t->cookie; 239 240 if ((flags & OBIO_BUS_MAP_USE_ROM) != 0 && 241 obio_find_rom_map(ba, size, hp) == 0) 242 return (0); 243 244 return (bus_space_map2(sc->sc_bustag, ba, size, flags, va, hp)); 245 } 246 247 paddr_t 248 obio_bus_mmap(t, ba, off, prot, flags) 249 bus_space_tag_t t; 250 bus_addr_t ba; 251 off_t off; 252 int prot; 253 int flags; 254 { 255 struct obio4_softc *sc = t->cookie; 256 257 return (bus_space_mmap(sc->sc_bustag, ba, off, prot, flags)); 258 } 259 260 int 261 obiosearch(parent, cf, aux) 262 struct device *parent; 263 struct cfdata *cf; 264 void *aux; 265 { 266 struct obio4_busattachargs *oap = aux; 267 union obio_attach_args uoba; 268 struct obio4_attach_args *oba = &uoba.uoba_oba4; 269 270 /* Check whether we're looking for a specifically named device */ 271 if (oap->name != NULL && strcmp(oap->name, cf->cf_driver->cd_name) != 0) 272 return (0); 273 274 /* 275 * Avoid sun4m entries which don't have valid PAs. 276 * no point in even probing them. 277 */ 278 if (cf->cf_loc[0] == -1) 279 return (0); 280 281 /* 282 * On the 4/100 obio addresses must be mapped at 283 * 0x0YYYYYYY, but alias higher up (we avoid the 284 * alias condition because it causes pmap difficulties) 285 * XXX: We also assume that 4/[23]00 obio addresses 286 * must be 0xZYYYYYYY, where (Z != 0) 287 */ 288 if (cpuinfo.cpu_type == CPUTYP_4_100 && (cf->cf_loc[0] & 0xf0000000)) 289 return (0); 290 if (cpuinfo.cpu_type != CPUTYP_4_100 && !(cf->cf_loc[0] & 0xf0000000)) 291 return (0); 292 293 uoba.uoba_isobio4 = 1; 294 oba->oba_bustag = &obio_space_tag; 295 oba->oba_dmatag = oap->ma->ma_dmatag; 296 oba->oba_paddr = BUS_ADDR(PMAP_OBIO, cf->cf_loc[0]); 297 oba->oba_pri = cf->cf_loc[1]; 298 299 if ((*cf->cf_attach->ca_match)(parent, cf, &uoba) == 0) 300 return (0); 301 302 config_attach(parent, cf, &uoba, obioprint); 303 return (1); 304 } 305 306 307 /* 308 * If we can find a mapping that was established by the rom, use it. 309 * Else, create a new mapping. 310 */ 311 int 312 obio_find_rom_map(ba, len, hp) 313 bus_addr_t ba; 314 int len; 315 bus_space_handle_t *hp; 316 { 317 #define getpte(va) lda(va, ASI_PTE) 318 319 u_long pa, pf; 320 int pgtype; 321 u_long va, pte; 322 323 if (len > NBPG) 324 return (EINVAL); 325 326 pa = BUS_ADDR_PADDR(ba); 327 pf = pa >> PGSHIFT; 328 pgtype = PMAP_T2PTE_4(PMAP_OBIO); 329 330 for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) { 331 pte = getpte(va); 332 if ((pte & PG_V) == 0 || (pte & PG_TYPE) != pgtype || 333 (pte & PG_PFNUM) != pf) 334 continue; 335 336 /* 337 * Found entry in PROM's pagetable 338 * note: preserve page offset 339 */ 340 *hp = (bus_space_handle_t)(va | (pa & PGOFSET)); 341 return (0); 342 } 343 344 return (ENOENT); 345 } 346 #endif /* SUN4 */ 347