1 /* $NetBSD: obio.c,v 1.63 2003/05/03 18:11:00 wiz 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 CFATTACH_DECL(obio, sizeof(union obio_softc), 80 obiomatch, obioattach, NULL, NULL); 81 82 /* 83 * This `obio4_busattachargs' data structure only exists to pass down 84 * to obiosearch() the name of a device that must be configured early. 85 */ 86 struct obio4_busattachargs { 87 struct mainbus_attach_args *ma; 88 const char *name; 89 }; 90 91 #if defined(SUN4) 92 static int obioprint __P((void *, const char *)); 93 static int obiosearch __P((struct device *, struct cfdata *, void *)); 94 static paddr_t obio_bus_mmap __P((bus_space_tag_t, bus_addr_t, off_t, 95 int, int)); 96 static int _obio_bus_map __P((bus_space_tag_t, bus_addr_t, 97 bus_size_t, int, 98 vaddr_t, bus_space_handle_t *)); 99 100 static struct sparc_bus_space_tag obio_space_tag = { 101 NULL, /* cookie */ 102 NULL, /* parent bus tag */ 103 NULL, /* ranges */ 104 0, /* nranges */ 105 _obio_bus_map, /* bus_space_map */ 106 NULL, /* bus_space_unmap */ 107 NULL, /* bus_space_subregion */ 108 NULL, /* bus_space_barrier */ 109 obio_bus_mmap, /* bus_space_mmap */ 110 NULL, /* bus_intr_establish */ 111 #if __FULL_SPARC_BUS_SPACE 112 NULL, /* read_1 */ 113 NULL, /* read_2 */ 114 NULL, /* read_4 */ 115 NULL, /* read_8 */ 116 NULL, /* write_1 */ 117 NULL, /* write_2 */ 118 NULL, /* write_4 */ 119 NULL, /* write_8 */ 120 #endif 121 }; 122 #endif 123 124 /* 125 * Translate obio `interrupts' property value to processor IPL (see sbus.c) 126 * Apparently, the `interrupts' property on obio devices is just 127 * the processor IPL. 128 */ 129 static int intr_obio2ipl[] = { 130 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 131 }; 132 133 int 134 obiomatch(parent, cf, aux) 135 struct device *parent; 136 struct cfdata *cf; 137 void *aux; 138 { 139 struct mainbus_attach_args *ma = aux; 140 141 return (strcmp(cf->cf_name, ma->ma_name) == 0); 142 } 143 144 void 145 obioattach(parent, self, aux) 146 struct device *parent, *self; 147 void *aux; 148 { 149 struct mainbus_attach_args *ma = aux; 150 151 /* 152 * There is only one obio bus 153 */ 154 if (self->dv_unit > 0) { 155 printf(" unsupported\n"); 156 return; 157 } 158 printf("\n"); 159 160 if (CPU_ISSUN4) { 161 #if defined(SUN4) 162 struct obio4_softc *sc = &((union obio_softc *)self)->sc_obio; 163 struct obio4_busattachargs oa; 164 const char *const *cpp; 165 static const char *const special4[] = { 166 /* find these first */ 167 "timer", 168 "dma", /* need this before `esp', if any */ 169 NULL 170 }; 171 172 sc->sc_bustag = ma->ma_bustag; 173 sc->sc_dmatag = ma->ma_dmatag; 174 175 obio_space_tag.cookie = sc; 176 obio_space_tag.parent = sc->sc_bustag; 177 178 oa.ma = ma; 179 180 /* Find all `early' obio devices */ 181 for (cpp = special4; *cpp != NULL; cpp++) { 182 oa.name = *cpp; 183 (void)config_search(obiosearch, self, &oa); 184 } 185 186 /* Find all other obio devices */ 187 oa.name = NULL; 188 (void)config_search(obiosearch, self, &oa); 189 #endif 190 return; 191 } else if (CPU_ISSUN4M) { 192 /* 193 * Attach the on-board I/O bus at on a sun4m. 194 * In this case we treat the obio bus as another sbus slot. 195 */ 196 struct sbus_softc *sc = &((union obio_softc *)self)->sc_sbus; 197 198 static const char *const special4m[] = { 199 /* find these first */ 200 "eeprom", 201 "counter", 202 #if 0 /* Not all sun4m's have an `auxio' */ 203 "auxio", 204 #endif 205 "", 206 /* place device to ignore here */ 207 "interrupt", 208 NULL 209 }; 210 211 sc->sc_bustag = ma->ma_bustag; 212 sc->sc_dmatag = ma->ma_dmatag; 213 sc->sc_intr2ipl = intr_obio2ipl; 214 215 sbus_attach_common(sc, "obio", ma->ma_node, special4m); 216 } else { 217 printf("obio on this machine?\n"); 218 } 219 } 220 221 #if defined(SUN4) 222 int 223 obioprint(args, busname) 224 void *args; 225 const char *busname; 226 { 227 union obio_attach_args *uoba = args; 228 struct obio4_attach_args *oba = &uoba->uoba_oba4; 229 230 aprint_normal(" addr 0x%lx", (u_long)BUS_ADDR_PADDR(oba->oba_paddr)); 231 if (oba->oba_pri != -1) 232 aprint_normal(" level %d", oba->oba_pri); 233 234 return (UNCONF); 235 } 236 237 int 238 _obio_bus_map(t, ba, size, flags, va, hp) 239 bus_space_tag_t t; 240 bus_addr_t ba; 241 bus_size_t size; 242 int flags; 243 vaddr_t va; 244 bus_space_handle_t *hp; 245 { 246 struct obio4_softc *sc = t->cookie; 247 248 if ((flags & OBIO_BUS_MAP_USE_ROM) != 0 && 249 obio_find_rom_map(ba, size, hp) == 0) 250 return (0); 251 252 return (bus_space_map2(sc->sc_bustag, ba, size, flags, va, hp)); 253 } 254 255 paddr_t 256 obio_bus_mmap(t, ba, off, prot, flags) 257 bus_space_tag_t t; 258 bus_addr_t ba; 259 off_t off; 260 int prot; 261 int flags; 262 { 263 struct obio4_softc *sc = t->cookie; 264 265 return (bus_space_mmap(sc->sc_bustag, ba, off, prot, flags)); 266 } 267 268 int 269 obiosearch(parent, cf, aux) 270 struct device *parent; 271 struct cfdata *cf; 272 void *aux; 273 { 274 struct obio4_busattachargs *oap = aux; 275 union obio_attach_args uoba; 276 struct obio4_attach_args *oba = &uoba.uoba_oba4; 277 278 /* Check whether we're looking for a specifically named device */ 279 if (oap->name != NULL && strcmp(oap->name, cf->cf_name) != 0) 280 return (0); 281 282 /* 283 * Avoid sun4m entries which don't have valid PAs. 284 * no point in even probing them. 285 */ 286 if (cf->cf_loc[0] == -1) 287 return (0); 288 289 /* 290 * On the 4/100 obio addresses must be mapped at 291 * 0x0YYYYYYY, but alias higher up (we avoid the 292 * alias condition because it causes pmap difficulties) 293 * XXX: We also assume that 4/[23]00 obio addresses 294 * must be 0xZYYYYYYY, where (Z != 0) 295 */ 296 if (cpuinfo.cpu_type == CPUTYP_4_100 && (cf->cf_loc[0] & 0xf0000000)) 297 return (0); 298 if (cpuinfo.cpu_type != CPUTYP_4_100 && !(cf->cf_loc[0] & 0xf0000000)) 299 return (0); 300 301 uoba.uoba_isobio4 = 1; 302 oba->oba_bustag = &obio_space_tag; 303 oba->oba_dmatag = oap->ma->ma_dmatag; 304 oba->oba_paddr = BUS_ADDR(PMAP_OBIO, cf->cf_loc[0]); 305 oba->oba_pri = cf->cf_loc[1]; 306 307 if (config_match(parent, cf, &uoba) == 0) 308 return (0); 309 310 config_attach(parent, cf, &uoba, obioprint); 311 return (1); 312 } 313 314 315 /* 316 * If we can find a mapping that was established by the rom, use it. 317 * Else, create a new mapping. 318 */ 319 int 320 obio_find_rom_map(ba, len, hp) 321 bus_addr_t ba; 322 int len; 323 bus_space_handle_t *hp; 324 { 325 #define getpte(va) lda(va, ASI_PTE) 326 327 u_long pa, pf; 328 int pgtype; 329 u_long va, pte; 330 331 if (len > PAGE_SIZE) 332 return (EINVAL); 333 334 pa = BUS_ADDR_PADDR(ba); 335 pf = pa >> PGSHIFT; 336 pgtype = PMAP_T2PTE_4(PMAP_OBIO); 337 338 for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += PAGE_SIZE) { 339 pte = getpte(va); 340 if ((pte & PG_V) == 0 || (pte & PG_TYPE) != pgtype || 341 (pte & PG_PFNUM) != pf) 342 continue; 343 344 /* 345 * Found entry in PROM's pagetable 346 * note: preserve page offset 347 */ 348 *hp = (bus_space_handle_t)(va | (pa & PGOFSET)); 349 return (0); 350 } 351 352 return (ENOENT); 353 } 354 #endif /* SUN4 */ 355