1 /* $NetBSD: obio.c,v 1.29 2007/02/03 05:14:38 tsutsui Exp $ */ 2 3 /*- 4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Adam Glass and Gordon W. Ross. 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.29 2007/02/03 05:14:38 tsutsui Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/device.h> 45 46 #include <uvm/uvm_extern.h> 47 48 #define _SUN68K_BUS_DMA_PRIVATE 49 #include <machine/autoconf.h> 50 #include <machine/bus.h> 51 #include <machine/dvma.h> 52 #include <machine/mon.h> 53 #include <machine/pte.h> 54 55 #include <sun3/sun3/machdep.h> 56 #include <sun3/sun3x/obio.h> 57 58 static int obio_match(struct device *, struct cfdata *, void *); 59 static void obio_attach(struct device *, struct device *, void *); 60 static int obio_print(void *, const char *); 61 static int obio_submatch(struct device *, struct cfdata *, 62 const int *, void *); 63 64 struct obio_softc { 65 struct device sc_dev; 66 bus_space_tag_t sc_bustag; 67 bus_dma_tag_t sc_dmatag; 68 }; 69 CFATTACH_DECL(obio, sizeof(struct obio_softc), 70 obio_match, obio_attach, NULL, NULL); 71 72 static int obio_attached; 73 74 static int obio_bus_map(bus_space_tag_t, bus_type_t, bus_addr_t, bus_size_t, 75 int, vaddr_t, bus_space_handle_t *); 76 static paddr_t obio_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t, 77 off_t, int, int); 78 static int obio_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t, 79 struct proc *, int); 80 81 static struct sun68k_bus_space_tag obio_space_tag = { 82 NULL, /* cookie */ 83 NULL, /* parent bus space tag */ 84 obio_bus_map, /* bus_space_map */ 85 NULL, /* bus_space_unmap */ 86 NULL, /* bus_space_subregion */ 87 NULL, /* bus_space_barrier */ 88 obio_bus_mmap, /* bus_space_mmap */ 89 NULL, /* bus_intr_establish */ 90 NULL, /* bus_space_peek_N */ 91 NULL /* bus_space_poke_N */ 92 }; 93 94 static struct sun68k_bus_dma_tag obio_dma_tag; 95 96 static int 97 obio_match(struct device *parent, struct cfdata *cf, void *aux) 98 { 99 struct confargs *ca = aux; 100 101 if (obio_attached) 102 return 0; 103 104 if (ca->ca_bustype != BUS_OBIO) 105 return 0; 106 107 if (ca->ca_name != NULL && strcmp(cf->cf_name, ca->ca_name) != 0) 108 return 0; 109 110 return 1; 111 } 112 113 /* 114 * We need control over the order of attachment on OBIO, 115 * so do "direct" style autoconfiguration with addresses 116 * from the list below. OBIO addresses are fixed forever. 117 * 118 * Warning: This whole list is very carefully ordered! 119 * In general, anything not already shown here should 120 * be added at or near the end. 121 */ 122 static paddr_t obio_alist[] = { 123 124 /* This is used by the Ethernet and SCSI drivers. */ 125 OBIO_IOMMU, 126 127 /* Misc. registers - needed by many things */ 128 OBIO_ENABLEREG, 129 OBIO_BUSERRREG, 130 OBIO_DIAGREG, /* leds.c */ 131 OBIO_IDPROM1, /* idprom.c (3/470) */ 132 OBIO_MEMREG, /* memerr.c */ 133 OBIO_INTERREG, /* intreg.c */ 134 135 /* Zilog UARTs */ 136 OBIO_ZS_KBD_MS, 137 OBIO_ZS_TTY_AB, 138 139 /* eeprom.c */ 140 OBIO_EEPROM, 141 142 /* Note: This must come after OBIO_IDPROM1. */ 143 OBIO_IDPROM2, /* idprom.c (3/80) */ 144 145 /* Note: Must probe for the Intersil first! */ 146 OBIO_CLOCK1, /* clock.c (3/470) */ 147 OBIO_CLOCK2, /* clock.c (3/80) */ 148 149 OBIO_INTEL_ETHER, 150 OBIO_LANCE_ETHER, 151 152 /* Need esp DMA before SCSI. */ 153 OBIO_EMULEX_DMA, /* 3/80 only */ 154 OBIO_EMULEX_SCSI, /* 3/80 only */ 155 156 /* Memory subsystem */ 157 OBIO_PCACHE_TAGS, 158 OBIO_ECCPARREG, 159 OBIO_IOC_TAGS, 160 OBIO_IOC_FLUSH, 161 162 OBIO_FDC, /* floppy disk (3/80) */ 163 OBIO_PRINTER_PORT, /* printer port (3/80) */ 164 }; 165 #define OBIO_ALIST_LEN (sizeof(obio_alist) / \ 166 sizeof(obio_alist[0])) 167 168 static void 169 obio_attach(struct device *parent, struct device *self, void *aux) 170 { 171 struct confargs *ca = aux; 172 struct obio_softc *sc = (void *)self; 173 struct confargs oba; 174 int i; 175 176 obio_attached = 1; 177 178 printf("\n"); 179 180 sc->sc_bustag = ca->ca_bustag; 181 sc->sc_dmatag = ca->ca_dmatag; 182 183 obio_space_tag.cookie = sc; 184 obio_space_tag.parent = sc->sc_bustag; 185 186 obio_dma_tag = *sc->sc_dmatag; 187 obio_dma_tag._cookie = sc; 188 obio_dma_tag._dmamap_load = obio_dmamap_load; 189 190 oba = *ca; 191 oba.ca_bustag = &obio_space_tag; 192 oba.ca_dmatag = &obio_dma_tag; 193 194 /* Configure these in the order listed above. */ 195 for (i = 0; i < OBIO_ALIST_LEN; i++) { 196 /* Our parent set ca->ca_bustype already. */ 197 oba.ca_paddr = obio_alist[i]; 198 /* These are filled-in by obio_submatch. */ 199 oba.ca_intpri = -1; 200 oba.ca_intvec = -1; 201 (void)config_found_sm_loc(self, "obio", NULL, &oba, obio_print, 202 obio_submatch); 203 } 204 } 205 206 /* 207 * Print out the confargs. The (parent) name is non-NULL 208 * when there was no match found by config_found(). 209 */ 210 static int 211 obio_print(void *args, const char *name) 212 { 213 214 /* Be quiet about empty OBIO locations. */ 215 if (name) 216 return(QUIET); 217 218 /* Otherwise do the usual. */ 219 return(bus_print(args, name)); 220 } 221 222 int 223 obio_submatch(struct device *parent, struct cfdata *cf, 224 const int *ldesc, void *aux) 225 { 226 struct confargs *ca = aux; 227 228 /* 229 * Note that a defaulted address locator can never match 230 * the value of ca->ca_paddr set by the obio_attach loop. 231 * Without this diagnostic, any device with a defaulted 232 * address locator would always be silently unmatched. 233 * Therefore, just disallow default addresses on OBIO. 234 */ 235 #ifdef DIAGNOSTIC 236 if (cf->cf_paddr == -1) 237 panic("obio_submatch: invalid address for: %s%d", 238 cf->cf_name, cf->cf_unit); 239 #endif 240 241 /* 242 * Note that obio_attach calls config_found_sm() with 243 * this function as the "submatch" and ca->ca_paddr 244 * set to each of the possible OBIO locations, so we 245 * want to reject any unmatched address here. 246 */ 247 if (cf->cf_paddr != ca->ca_paddr) 248 return 0; 249 250 /* 251 * Copy the locators into our confargs for the child. 252 * Note: ca->ca_bustype was set by our parent driver 253 * (mainbus) and ca->ca_paddr was set by obio_attach. 254 */ 255 ca->ca_intpri = cf->cf_intpri; 256 ca->ca_intvec = cf->cf_intvec; 257 258 /* Now call the match function of the potential child. */ 259 return (config_match(parent, cf, aux)); 260 } 261 262 263 /*****************************************************************/ 264 265 /* 266 * This is our record of "interesting" OBIO mappings that 267 * the PROM has left in the virtual space reserved for it. 268 * Each row of the array holds a virtual address and the 269 * physical address it maps to (if found). 270 */ 271 static struct prom_map { 272 paddr_t pa; 273 vaddr_t va; 274 } prom_mappings[] = { 275 { OBIO_ENABLEREG, 0 }, /* regs: Sys ENA, Bus ERR, etc. */ 276 { OBIO_ZS_KBD_MS, 0 }, /* Keyboard and Mouse */ 277 { OBIO_ZS_TTY_AB, 0 }, /* Serial Ports */ 278 { OBIO_EEPROM, 0 }, /* EEPROM/IDPROM/clock */ 279 }; 280 #define PROM_MAP_CNT (sizeof(prom_mappings) / \ 281 sizeof(prom_mappings[0])) 282 283 /* 284 * Find a virtual address for a device at physical address 'pa'. 285 * If one is found among the mappings already made by the PROM 286 * at power-up time, use it and return 0. Otherwise return errno 287 * as a sign that a mapping will have to be created. 288 */ 289 int 290 find_prom_map(paddr_t pa, bus_type_t iospace, int sz, vaddr_t *vap) 291 { 292 int i; 293 vsize_t off; 294 295 off = pa & PGOFSET; 296 pa -= off; 297 sz += off; 298 299 /* The saved mappings are all one page long. */ 300 if (sz > PAGE_SIZE) 301 return EINVAL; 302 303 /* Linear search for it. The list is short. */ 304 for (i = 0; i < PROM_MAP_CNT; i++) { 305 if (pa == prom_mappings[i].pa) { 306 *vap = prom_mappings[i].va + off; 307 return 0; 308 } 309 } 310 return ENOENT; 311 } 312 313 /* 314 * Search the PROM page tables for OBIO mappings that 315 * we would like to borrow. 316 */ 317 static void 318 save_prom_mappings(void) 319 { 320 int *mon_pte; 321 vaddr_t va; 322 paddr_t pa; 323 int i; 324 325 /* Note: mon_ctbl[0] maps SUN3X_MON_KDB_BASE */ 326 mon_pte = *romVectorPtr->monptaddr; 327 328 for (va = SUN3X_MON_KDB_BASE; va < SUN3X_MONEND; 329 va += PAGE_SIZE, mon_pte++) 330 { 331 /* Is this a valid mapping to OBIO? */ 332 /* XXX - Some macros would be nice... */ 333 if ((*mon_pte & 0xF0000003) != 0x60000001) 334 continue; 335 336 /* Yes it is. Is this a mapping we want? */ 337 pa = *mon_pte & MMU_SHORT_PTE_BASEADDR; 338 for (i = 0; i < PROM_MAP_CNT; i++) { 339 if (pa != prom_mappings[i].pa) 340 continue; 341 /* Yes, we want it. Save the va? */ 342 if (prom_mappings[i].va == 0) { 343 prom_mappings[i].va = va; 344 } 345 } 346 } 347 348 } 349 350 /* 351 * These are all the OBIO address that are required early in 352 * the life of the kernel. All are less than one page long. 353 * This function should make any required mappings that we 354 * were not able to find among the PROM monitor's mappings. 355 */ 356 static void 357 make_required_mappings(void) 358 { 359 int i; 360 361 for (i = 0; i < PROM_MAP_CNT; i++) { 362 if (prom_mappings[i].va == 0) { 363 /* 364 * Actually, the PROM always has all the 365 * "required" mappings we need, (smile) 366 * but this makes sure that is true. 367 */ 368 mon_printf("obio: no mapping for pa=0x%x\n", 369 prom_mappings[i].pa); 370 sunmon_abort(); /* Ancient PROM? */ 371 } 372 } 373 } 374 375 376 /* 377 * Find mappings for devices that are needed before autoconfiguration. 378 * We first look for and record any useful PROM mappings, then call 379 * the "init" functions for drivers that we need to use before the 380 * normal autoconfiguration calls configure(). Warning: this is 381 * called before pmap_bootstrap, so no allocation allowed! 382 */ 383 void 384 obio_init(void) 385 { 386 save_prom_mappings(); 387 make_required_mappings(); 388 389 enable_init(); 390 391 /* 392 * Find the interrupt reg mapping and turn off the 393 * interrupts, otherwise the PROM clock interrupt 394 * would poll the zs and toggle some LEDs... 395 */ 396 intreg_init(); 397 } 398 399 int 400 obio_bus_map(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, 401 bus_size_t size, int flags, vaddr_t vaddr, bus_space_handle_t *hp) 402 { 403 struct obio_softc *sc = t->cookie; 404 405 return bus_space_map2(sc->sc_bustag, PMAP_OBIO, paddr, size, 406 flags | _SUN68K_BUS_MAP_USE_PROM, vaddr, hp); 407 } 408 409 paddr_t 410 obio_bus_mmap(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, off_t off, 411 int prot, int flags) 412 { 413 struct obio_softc *sc = t->cookie; 414 415 return bus_space_mmap2(sc->sc_bustag, PMAP_OBIO, paddr, off, prot, 416 flags); 417 } 418 419 static int 420 obio_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf, 421 bus_size_t buflen, struct proc *p, int flags) 422 { 423 int error; 424 425 error = _bus_dmamap_load(t, map, buf, buflen, p, flags); 426 if (error == 0) 427 map->dm_segs[0].ds_addr &= DVMA_OBIO_SLAVE_MASK; 428 return error; 429 } 430 431