obio.c revision 1.3
1/* $NetBSD: obio.c,v 1.3 1997/01/27 22:15:06 gwr 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/param.h> 40#include <sys/systm.h> 41#include <sys/device.h> 42 43#include <machine/autoconf.h> 44#include <machine/machdep.h> 45#include <machine/mon.h> 46#include <machine/obio.h> 47#include <machine/pte.h> 48 49short *enable_reg; 50 51static int obio_match __P((struct device *, struct cfdata *, void *)); 52static void obio_attach __P((struct device *, struct device *, void *)); 53static int obio_print __P((void *, const char *parentname)); 54static int obio_submatch __P((struct device *, struct cfdata *, void *)); 55 56struct cfattach obio_ca = { 57 sizeof(struct device), obio_match, obio_attach 58}; 59 60struct cfdriver obio_cd = { 61 NULL, "obio", DV_DULL 62}; 63 64static int 65obio_match(parent, cf, aux) 66 struct device *parent; 67 struct cfdata *cf; 68 void *aux; 69{ 70 struct confargs *ca = aux; 71 72 if (ca->ca_bustype != BUS_OBIO) 73 return (0); 74 return(1); 75} 76 77/* 78 * We need some control over the order of attachment on OBIO, 79 * and all OBIO device addresses are known and fixed foerver. 80 * Therefore, this uses a list of addresses to attach. 81 * XXX - Any other way to control search/attach order? 82 */ 83static int obio_alist[] = { 84 OBIO_ZS_KBD_MS, 85 OBIO_ZS_TTY_AB, 86 87 OBIO_EEPROM, /* the next two are part of the eeprom */ 88 OBIO_IDPROM2, /* 3/80 only */ 89 OBIO_CLOCK2, /* 3/80 only */ 90 91 OBIO_CLOCK1, /* 3/470 only */ 92 93 /* These are all in the same page - could be just one driver... */ 94 OBIO_ENABLEREG, 95 OBIO_BUSERRREG, 96 OBIO_DIAGREG, 97 OBIO_IDPROM1, 98 OBIO_MEMREG, 99 OBIO_INTERREG, 100 101 OBIO_P4_REG, 102 OBIO_IOMMU, 103 104 OBIO_INTEL_ETHER, 105 OBIO_LANCE_ETHER, 106 107 /* ...todo... */ 108 OBIO_FDC, 109 OBIO_PRINTER_PORT, 110}; 111#define OBIO_ALIST_LEN (sizeof(obio_alist) / \ 112 sizeof(obio_alist[0])) 113 114static void 115obio_attach(parent, self, aux) 116 struct device *parent; 117 struct device *self; 118 void *aux; 119{ 120 struct confargs *ca = aux; 121 int i; 122 123 printf("\n"); 124 125 /* Configure these in order of address. */ 126 for (i = 0; i < OBIO_ALIST_LEN; i++) { 127 /* We know ca_bustype == BUS_OBIO */ 128 ca->ca_paddr = obio_alist[i]; 129 ca->ca_intpri = -1; 130 ca->ca_intvec = -1; 131 (void) config_found_sm(self, ca, obio_print, obio_submatch); 132 } 133} 134 135/* 136 * Print out the confargs. The (parent) name is non-NULL 137 * when there was no match found by config_found(). 138 */ 139static int 140obio_print(args, name) 141 void *args; 142 const char *name; 143{ 144 struct confargs *ca = args; 145 146 /* Be quiet about empty OBIO locations. */ 147 if (name) 148 return(QUIET); 149 150 if (ca->ca_paddr != -1) 151 printf(" addr 0x%x", ca->ca_paddr); 152 if (ca->ca_intpri != -1) 153 printf(" level %d", ca->ca_intpri); 154 155 return(UNCONF); 156} 157 158int 159obio_submatch(parent, cf, aux) 160 struct device *parent; 161 struct cfdata *cf; 162 void *aux; 163{ 164 struct confargs *ca = aux; 165 cfmatch_t submatch; 166 167 /* 168 * Default addresses are mostly useless for OBIO. 169 * The address assignments are fixed for all time, 170 * so our config files might as well reflect that. 171 */ 172#ifdef DIAGNOSTIC 173 if (cf->cf_paddr == -1) 174 panic("obio_submatch: invalid address for: %s%d\n", 175 cf->cf_driver->cd_name, cf->cf_unit); 176#endif 177 178 /* This enforces exact address match. */ 179 if (cf->cf_paddr != ca->ca_paddr) 180 return 0; 181 182 /* Now call the match function of the potential child. */ 183 submatch = cf->cf_attach->ca_match; 184 if (submatch == NULL) 185 panic("obio_submatch: no match function for: %s\n", 186 cf->cf_driver->cd_name); 187 188 return ((*submatch)(parent, cf, aux)); 189} 190 191 192/*****************************************************************/ 193 194/* 195 * This is our record of "interesting" OBIO mappings that 196 * the PROM has left in the virtual space reserved for it. 197 * Each non-null array element holds the virtual address 198 * of an OBIO mapping where the OBIO address mapped is: 199 * (array_index * SAVE_INCR) 200 * and the length of the mapping is one page. 201 */ 202static struct prom_map { 203 vm_offset_t pa, va; 204} prom_mappings[] = { 205 { OBIO_ZS_KBD_MS, 0 }, /* Keyboard and Mouse */ 206 { OBIO_ZS_TTY_AB, 0 }, /* Serial Ports */ 207 { OBIO_EEPROM, 0 }, /* EEPROM/IDPROM/clock */ 208 { OBIO_ENABLEREG, 0 }, /* regs: Sys ENA, Bus ERR, etc. */ 209}; 210#define PROM_MAP_CNT (sizeof(prom_mappings) / \ 211 sizeof(prom_mappings[0])) 212 213/* 214 * Find a virtual address for a device at physical address 'pa'. 215 * If one is found among the mappings already made by the PROM 216 * at power-up time, use it. Otherwise return 0 as a sign that 217 * a mapping will have to be created. 218 */ 219caddr_t 220obio_find_mapping(int pa, int size) 221{ 222 int i, off; 223 224 if (size >= NBPG) 225 return (caddr_t)0; 226 227 off = pa & PGOFSET; 228 pa -= off; 229 230 for (i = 0; i < PROM_MAP_CNT; i++) { 231 if (pa == prom_mappings[i].pa) { 232 return ((caddr_t)(prom_mappings[i].va + off)); 233 } 234 } 235 return (caddr_t)0; 236} 237 238/* 239 * Search the PROM page tables for OBIO mappings that 240 * we would like to borrow. 241 */ 242static void 243save_prom_mappings __P((void)) 244{ 245 int *mon_pte; 246 vm_offset_t va, pa; 247 int i; 248 249 /* Note: mon_ctbl[0] maps MON_KDB_START */ 250 mon_pte = *romVectorPtr->monptaddr; 251 252 for (va = MON_KDB_START; va < MONEND; 253 va += NBPG, mon_pte++) 254 { 255 /* Is this a valid mapping to OBIO? */ 256 /* XXX - Some macros would be nice... */ 257 if ((*mon_pte & 0xF0000003) != 0x60000001) 258 continue; 259 260 /* Yes it is. Is this a mapping we want? */ 261 pa = *mon_pte & MMU_SHORT_PTE_BASEADDR; 262 for (i = 0; i < PROM_MAP_CNT; i++) { 263 if (pa != prom_mappings[i].pa) 264 continue; 265 /* Yes, we want it. Save the va? */ 266 if (prom_mappings[i].va == 0) { 267 prom_mappings[i].va = va; 268 } 269 } 270 } 271 272} 273 274/* 275 * These are all the OBIO address that are required early in 276 * the life of the kernel. All are less than one page long. 277 * This function should make any required mappings that we 278 * were not able to find among the PROM monitor's mappings. 279 */ 280static void 281make_required_mappings __P((void)) 282{ 283 int i; 284 285 for (i = 0; i < PROM_MAP_CNT; i++) { 286 if (prom_mappings[i].va == 0) { 287 /* 288 * Actually, the PROM always has all the 289 * "required" mappings we need, (smile) 290 * but this makes sure that is true. 291 */ 292 mon_printf("obio: no mapping for pa=0x%x\n", 293 prom_mappings[i].pa); 294 sunmon_abort(); /* Ancient PROM? */ 295 } 296 } 297} 298 299 300/* 301 * Find mappings for devices that are needed before autoconfiguration. 302 * We first look for and record any useful PROM mappings, then call 303 * the "init" functions for drivers that we need to use before the 304 * normal autoconfiguration calls configure(). Warning: this is 305 * called before pmap_bootstrap, so no allocation allowed! 306 */ 307void 308obio_init() 309{ 310 save_prom_mappings(); 311 make_required_mappings(); 312 313 /* Init drivers that use the required OBIO mappings. */ 314 idprom_init(); 315 eeprom_init(); 316 zs_init(); 317 318 enable_reg = (short*) obio_find_mapping(OBIO_ENABLEREG, 2); 319 intreg_init(); 320 clock_init(); 321} 322 323caddr_t 324obio_alloc(obio_addr, obio_size) 325 int obio_addr, obio_size; 326{ 327 caddr_t cp; 328 329 cp = obio_find_mapping((vm_offset_t)obio_addr, obio_size); 330 if (cp) return (cp); 331 332 cp = bus_mapin(BUS_OBIO, obio_addr, obio_size); 333 return (cp); 334} 335