1 /* $NetBSD: obio.c,v 1.65 2004/06/27 18:24:47 pk 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 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.65 2004/06/27 18:24:47 pk Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/device.h> 45 #include <sys/malloc.h> 46 47 #ifdef DEBUG 48 #include <sys/proc.h> 49 #include <sys/syslog.h> 50 #endif 51 52 #include <uvm/uvm_extern.h> 53 54 #include <machine/bus.h> 55 #include <sparc/dev/sbusvar.h> 56 #include <machine/autoconf.h> 57 #include <machine/oldmon.h> 58 #include <machine/cpu.h> 59 #include <machine/ctlreg.h> 60 #include <sparc/sparc/asm.h> 61 #include <sparc/sparc/vaddrs.h> 62 #include <sparc/sparc/cpuvar.h> 63 64 struct obio4_softc { 65 struct device sc_dev; /* base device */ 66 bus_space_tag_t sc_bustag; /* parent bus tag */ 67 bus_dma_tag_t sc_dmatag; /* parent bus DMA tag */ 68 }; 69 70 union obio_softc { 71 struct device sc_dev; /* base device */ 72 struct obio4_softc sc_obio; /* sun4 obio */ 73 struct sbus_softc sc_sbus; /* sun4m obio is another sbus slot */ 74 }; 75 76 77 /* autoconfiguration driver */ 78 static int obiomatch __P((struct device *, struct cfdata *, void *)); 79 static void obioattach __P((struct device *, struct device *, void *)); 80 81 CFATTACH_DECL(obio, sizeof(union obio_softc), 82 obiomatch, obioattach, NULL, NULL); 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 /* There's at most one obio bus, so we can allocate the bus tag statically */ 103 static struct sparc_bus_space_tag obio_space_tag; 104 #endif 105 106 /* 107 * Translate obio `interrupts' property value to processor IPL (see sbus.c) 108 * Apparently, the `interrupts' property on obio devices is just 109 * the processor IPL. 110 */ 111 static int intr_obio2ipl[] = { 112 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 113 }; 114 115 int 116 obiomatch(parent, cf, aux) 117 struct device *parent; 118 struct cfdata *cf; 119 void *aux; 120 { 121 struct mainbus_attach_args *ma = aux; 122 123 return (strcmp(cf->cf_name, ma->ma_name) == 0); 124 } 125 126 void 127 obioattach(parent, self, aux) 128 struct device *parent, *self; 129 void *aux; 130 { 131 struct mainbus_attach_args *ma = aux; 132 133 /* 134 * There is only one obio bus 135 */ 136 if (self->dv_unit > 0) { 137 printf(" unsupported\n"); 138 return; 139 } 140 printf("\n"); 141 142 if (CPU_ISSUN4) { 143 #if defined(SUN4) 144 struct obio4_softc *sc = &((union obio_softc *)self)->sc_obio; 145 struct obio4_busattachargs oa; 146 const char *const *cpp; 147 static const char *const special4[] = { 148 /* find these first */ 149 "timer", 150 "dma", /* need this before `esp', if any */ 151 NULL 152 }; 153 154 sc->sc_bustag = ma->ma_bustag; 155 sc->sc_dmatag = ma->ma_dmatag; 156 157 memcpy(&obio_space_tag, sc->sc_bustag, sizeof(obio_space_tag)); 158 obio_space_tag.cookie = sc; 159 obio_space_tag.parent = sc->sc_bustag; 160 obio_space_tag.sparc_bus_map = _obio_bus_map; 161 obio_space_tag.sparc_bus_mmap = obio_bus_mmap; 162 163 oa.ma = ma; 164 165 /* Find all `early' obio devices */ 166 for (cpp = special4; *cpp != NULL; cpp++) { 167 oa.name = *cpp; 168 (void)config_search(obiosearch, self, &oa); 169 } 170 171 /* Find all other obio devices */ 172 oa.name = NULL; 173 (void)config_search(obiosearch, self, &oa); 174 #endif 175 return; 176 } else if (CPU_ISSUN4M) { 177 /* 178 * Attach the on-board I/O bus at on a sun4m. 179 * In this case we treat the obio bus as another sbus slot. 180 */ 181 struct sbus_softc *sc = &((union obio_softc *)self)->sc_sbus; 182 183 static const char *const special4m[] = { 184 /* find these first */ 185 "eeprom", 186 "counter", 187 #if 0 /* Not all sun4m's have an `auxio' */ 188 "auxio", 189 #endif 190 "", 191 /* place device to ignore here */ 192 "interrupt", 193 NULL 194 }; 195 196 sc->sc_bustag = ma->ma_bustag; 197 sc->sc_dmatag = ma->ma_dmatag; 198 sc->sc_intr2ipl = intr_obio2ipl; 199 200 sbus_attach_common(sc, "obio", ma->ma_node, special4m); 201 } else { 202 printf("obio on this machine?\n"); 203 } 204 } 205 206 #if defined(SUN4) 207 int 208 obioprint(args, busname) 209 void *args; 210 const char *busname; 211 { 212 union obio_attach_args *uoba = args; 213 struct obio4_attach_args *oba = &uoba->uoba_oba4; 214 215 aprint_normal(" addr 0x%lx", (u_long)BUS_ADDR_PADDR(oba->oba_paddr)); 216 if (oba->oba_pri != -1) 217 aprint_normal(" level %d", oba->oba_pri); 218 219 return (UNCONF); 220 } 221 222 int 223 _obio_bus_map(t, ba, size, flags, va, hp) 224 bus_space_tag_t t; 225 bus_addr_t ba; 226 bus_size_t size; 227 int flags; 228 vaddr_t va; 229 bus_space_handle_t *hp; 230 { 231 232 if ((flags & OBIO_BUS_MAP_USE_ROM) != 0 && 233 obio_find_rom_map(ba, size, hp) == 0) 234 return (0); 235 236 return (bus_space_map2(t->parent, ba, size, flags, va, hp)); 237 } 238 239 paddr_t 240 obio_bus_mmap(t, ba, off, prot, flags) 241 bus_space_tag_t t; 242 bus_addr_t ba; 243 off_t off; 244 int prot; 245 int flags; 246 { 247 248 return (bus_space_mmap(t->parent, ba, off, prot, flags)); 249 } 250 251 int 252 obiosearch(parent, cf, aux) 253 struct device *parent; 254 struct cfdata *cf; 255 void *aux; 256 { 257 struct obio4_busattachargs *oap = aux; 258 union obio_attach_args uoba; 259 struct obio4_attach_args *oba = &uoba.uoba_oba4; 260 261 /* Check whether we're looking for a specifically named device */ 262 if (oap->name != NULL && strcmp(oap->name, cf->cf_name) != 0) 263 return (0); 264 265 /* 266 * Avoid sun4m entries which don't have valid PAs. 267 * no point in even probing them. 268 */ 269 if (cf->cf_loc[0] == -1) 270 return (0); 271 272 /* 273 * On the 4/100 obio addresses must be mapped at 274 * 0x0YYYYYYY, but alias higher up (we avoid the 275 * alias condition because it causes pmap difficulties) 276 * XXX: We also assume that 4/[23]00 obio addresses 277 * must be 0xZYYYYYYY, where (Z != 0) 278 */ 279 if (cpuinfo.cpu_type == CPUTYP_4_100 && (cf->cf_loc[0] & 0xf0000000)) 280 return (0); 281 if (cpuinfo.cpu_type != CPUTYP_4_100 && !(cf->cf_loc[0] & 0xf0000000)) 282 return (0); 283 284 uoba.uoba_isobio4 = 1; 285 oba->oba_bustag = &obio_space_tag; 286 oba->oba_dmatag = oap->ma->ma_dmatag; 287 oba->oba_paddr = BUS_ADDR(PMAP_OBIO, cf->cf_loc[0]); 288 oba->oba_pri = cf->cf_loc[1]; 289 290 if (config_match(parent, cf, &uoba) == 0) 291 return (0); 292 293 config_attach(parent, cf, &uoba, obioprint); 294 return (1); 295 } 296 297 298 /* 299 * If we can find a mapping that was established by the rom, use it. 300 * Else, create a new mapping. 301 */ 302 int 303 obio_find_rom_map(ba, len, hp) 304 bus_addr_t ba; 305 int len; 306 bus_space_handle_t *hp; 307 { 308 #define getpte(va) lda(va, ASI_PTE) 309 310 u_long pa, pf; 311 int pgtype; 312 u_long va, pte; 313 314 if (len > PAGE_SIZE) 315 return (EINVAL); 316 317 pa = BUS_ADDR_PADDR(ba); 318 pf = pa >> PGSHIFT; 319 pgtype = PMAP_T2PTE_4(PMAP_OBIO); 320 321 for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += PAGE_SIZE) { 322 pte = getpte(va); 323 if ((pte & PG_V) == 0 || (pte & PG_TYPE) != pgtype || 324 (pte & PG_PFNUM) != pf) 325 continue; 326 327 /* 328 * Found entry in PROM's pagetable 329 * note: preserve page offset 330 */ 331 *hp = (bus_space_handle_t)(va | (pa & PGOFSET)); 332 return (0); 333 } 334 335 return (ENOENT); 336 } 337 #endif /* SUN4 */ 338