1 /* $NetBSD: isa_machdep.c,v 1.33 2008/01/04 22:13:56 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by UCHIYAMA Yasushi. 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: isa_machdep.c,v 1.33 2008/01/04 22:13:56 ad Exp $"); 41 42 #include "opt_vr41xx.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/reboot.h> 47 #include <sys/device.h> 48 49 #include <dev/isa/isavar.h> 50 #include <dev/isa/isareg.h> 51 52 #include <machine/platid.h> 53 #include <machine/platid_mask.h> 54 #include <machine/bus.h> 55 #include <machine/bus_space_hpcmips.h> 56 #include <machine/debug.h> 57 58 #include <dev/hpc/hpciovar.h> 59 60 #include <hpcmips/vr/vripif.h> 61 62 #include "locators.h" 63 64 #define VRISADEBUG 65 66 #ifdef VRISADEBUG 67 #ifndef VRISADEBUG_CONF 68 #define VRISADEBUG_CONF 0 69 #endif /* VRISADEBUG_CONF */ 70 int vrisa_debug = VRISADEBUG_CONF; 71 #define DPRINTF(arg) if (vrisa_debug) printf arg; 72 #define DBITDISP(mask) if (vrisa_debug) dbg_bit_print(mask); 73 #define VPRINTF(arg) if (bootverbose || vrisa_debug) printf arg; 74 #else /* VRISADEBUG */ 75 #define DPRINTF(arg) 76 #define DBITDISP(mask) 77 #define VPRINTF(arg) if (bootverbose) printf arg; 78 #endif /* VRISADEBUG */ 79 80 /* 81 * intrrupt no. encoding: 82 * 83 * 0x0000000f ISA IRQ# 84 * 0x00ff0000 GPIO port# 85 * 0x01000000 interrupt signal hold/through (1:hold/0:though) 86 * 0x02000000 interrupt detection level (1:low /0:high ) 87 * 0x04000000 interrupt detection trigger (1:edge/0:level ) 88 */ 89 #define INTR_IRQ(i) (((i)>> 0) & 0x0f) 90 #define INTR_PORT(i) (((i)>>16) & 0xff) 91 #define INTR_MODE(i) (((i)>>24) & 0x07) 92 #define INTR_NIRQS 16 93 94 int vrisabprint(void *, const char *); 95 int vrisabmatch(struct device *, struct cfdata *, void *); 96 void vrisabattach(struct device *, struct device *, void *); 97 98 struct vrisab_softc { 99 struct device sc_dev; 100 hpcio_chip_t sc_hc; 101 int sc_intr_map[INTR_NIRQS]; /* ISA <-> GIU inerrupt line mapping */ 102 struct hpcmips_isa_chipset sc_isa_ic; 103 }; 104 105 CFATTACH_DECL(vrisab, sizeof(struct vrisab_softc), 106 vrisabmatch, vrisabattach, NULL, NULL); 107 108 #ifdef DEBUG_FIND_PCIC 109 #include <mips/cpuregs.h> 110 #warning DEBUG_FIND_PCIC 111 static void __find_pcic(void); 112 #endif 113 114 #ifdef DEBUG_FIND_COMPORT 115 #include <mips/cpuregs.h> 116 #include <dev/ic/ns16550reg.h> 117 #include <dev/ic/comreg.h> 118 #warning DEBUG_FIND_COMPORT 119 static void __find_comport(void); 120 #endif 121 122 int 123 vrisabmatch(struct device *parent, struct cfdata *match, void *aux) 124 { 125 struct hpcio_attach_args *haa = aux; 126 platid_mask_t mask; 127 int n; 128 129 if (strcmp(haa->haa_busname, match->cf_name)) 130 return (0); 131 132 if (match->cf_loc[HPCIOIFCF_PLATFORM] == HPCIOIFCF_PLATFORM_DEFAULT) 133 return (1); 134 135 mask = PLATID_DEREF(match->cf_loc[HPCIOIFCF_PLATFORM]); 136 if ((n = platid_match(&platid, &mask)) != 0) 137 return (n + 2); 138 139 return (0); 140 } 141 142 void 143 vrisabattach(struct device *parent, struct device *self, void *aux) 144 { 145 struct hpcio_attach_args *haa = aux; 146 struct vrisab_softc *sc = (void*)self; 147 struct isabus_attach_args iba; 148 struct bus_space_tag_hpcmips *iot, *memt; 149 bus_addr_t offset; 150 int i; 151 152 sc->sc_hc = (*haa->haa_getchip)(haa->haa_sc, VRIP_IOCHIP_VRGIU); 153 sc->sc_isa_ic.ic_sc = sc; 154 155 iba.iba_ic = &sc->sc_isa_ic; 156 iba.iba_dmat = 0; /* XXX not yet */ 157 158 /* Allocate ISA memory space */ 159 memt = hpcmips_alloc_bus_space_tag(); 160 offset = device_cfdata(&sc->sc_dev)->cf_loc[VRISABIFCF_ISAMEMOFFSET]; 161 hpcmips_init_bus_space(memt, 162 (struct bus_space_tag_hpcmips *)haa->haa_iot, "ISA mem", 163 VR_ISA_MEM_BASE + offset, VR_ISA_MEM_SIZE - offset); 164 iba.iba_memt = &memt->bst; 165 166 /* Allocate ISA port space */ 167 iot = hpcmips_alloc_bus_space_tag(); 168 offset = device_cfdata(&sc->sc_dev)->cf_loc[VRISABIFCF_ISAPORTOFFSET]; 169 hpcmips_init_bus_space(iot, 170 (struct bus_space_tag_hpcmips *)haa->haa_iot, "ISA port", 171 VR_ISA_PORT_BASE + offset, VR_ISA_PORT_SIZE - offset); 172 iba.iba_iot = &iot->bst; 173 174 #ifdef DEBUG_FIND_PCIC 175 #warning DEBUG_FIND_PCIC 176 __find_pcic(); 177 #else 178 /* Initialize ISA IRQ <-> GPIO mapping */ 179 for (i = 0; i < INTR_NIRQS; i++) 180 sc->sc_intr_map[i] = -1; 181 printf(": ISA port %#x-%#x mem %#x-%#x\n", 182 iot->base, iot->base + iot->size, 183 memt->base, memt->base + memt->size); 184 config_found_ia(self, "isabus", &iba, vrisabprint); 185 #endif 186 187 #ifdef DEBUG_FIND_COMPORT 188 #warning DEBUG_FIND_COMPORT 189 __find_comport(); 190 #endif 191 } 192 193 int 194 vrisabprint(void *aux, const char *pnp) 195 { 196 if (pnp) 197 return (QUIET); 198 199 return (UNCONF); 200 } 201 202 void 203 isa_attach_hook(struct device *parent, struct device *self, 204 struct isabus_attach_args *iba) 205 { 206 207 } 208 209 const struct evcnt * 210 isa_intr_evcnt(isa_chipset_tag_t ic, int irq) 211 { 212 213 /* XXX for now, no evcnt parent reported */ 214 return (NULL); 215 } 216 217 void * 218 isa_intr_establish(isa_chipset_tag_t ic, int intr, int type, int level, 219 int (*ih_fun)(void*), void *ih_arg) 220 { 221 struct vrisab_softc *sc = ic->ic_sc; 222 int port, irq, mode; 223 224 static int intr_modes[8] = { 225 HPCIO_INTR_LEVEL_HIGH_THROUGH, 226 HPCIO_INTR_LEVEL_HIGH_HOLD, 227 HPCIO_INTR_LEVEL_LOW_THROUGH, 228 HPCIO_INTR_LEVEL_LOW_HOLD, 229 HPCIO_INTR_EDGE_THROUGH, 230 HPCIO_INTR_EDGE_HOLD, 231 HPCIO_INTR_EDGE_THROUGH, 232 HPCIO_INTR_EDGE_HOLD, 233 }; 234 #ifdef VRISADEBUG 235 static const char* intr_mode_names[8] = { 236 "level high through", 237 "level high hold", 238 "level low through", 239 "level low hold", 240 "edge through", 241 "edge hold", 242 "edge through", 243 "edge hold", 244 }; 245 #endif /* VRISADEBUG */ 246 /* 247 * ISA IRQ <-> GPIO port mapping 248 */ 249 irq = INTR_IRQ(intr); 250 if (sc->sc_intr_map[irq] != -1) { 251 /* already mapped */ 252 intr = sc->sc_intr_map[irq]; 253 } else { 254 /* not mapped yet */ 255 sc->sc_intr_map[irq] = intr; /* Register it */ 256 } 257 mode = INTR_MODE(intr); 258 port = INTR_PORT(intr); 259 260 VPRINTF(("ISA IRQ %d -> %s port %d, %s\n", 261 irq, sc->sc_hc->hc_name, port, intr_mode_names[mode])); 262 263 /* Call Vr routine */ 264 return (hpcio_intr_establish(sc->sc_hc, port, intr_modes[mode], 265 ih_fun, ih_arg)); 266 } 267 268 void 269 isa_intr_disestablish(ic, arg) 270 isa_chipset_tag_t ic; 271 void *arg; 272 { 273 struct vrisab_softc *sc = ic->ic_sc; 274 /* Call Vr routine */ 275 hpcio_intr_disestablish(sc->sc_hc, arg); 276 } 277 278 int 279 isa_intr_alloc(isa_chipset_tag_t ic, int mask, int type, int *irq) 280 { 281 /* XXX not coded yet. this is temporary XXX */ 282 DPRINTF(("isa_intr_alloc:")); 283 DBITDISP(mask); 284 *irq = (ffs(mask) -1); /* XXX */ 285 286 return (0); 287 } 288 289 #ifdef DEBUG_FIND_PCIC 290 #warning DEBUG_FIND_PCIC 291 static void 292 __find_pcic(void) 293 { 294 int i, j, step, found; 295 u_int32_t addr; 296 u_int8_t reg; 297 int __read_revid (u_int32_t port) 298 { 299 addr = MIPS_PHYS_TO_KSEG1(i + port); 300 printf("%#x\r", i); 301 for (found = 0, j = 0; j < 0x100; j += 0x40) { 302 *((volatile u_int8_t *)addr) = j; 303 reg = *((volatile u_int8_t *)(addr + 1)); 304 #ifdef DEBUG_FIND_PCIC_I82365SL_ONLY 305 if (reg == 0x82 || reg == 0x83) { 306 #else 307 if ((reg & 0xc0) == 0x80) { 308 #endif 309 found++; 310 } 311 if (found) 312 printf("\nfound %d socket at %#x" 313 "(base from %#x)\n", found, addr, 314 i + port - VR_ISA_PORT_BASE); 315 } 316 } 317 step = 0x1000000; 318 printf("\nFinding PCIC. Trying ISA port %#x-%#x step %#x\n", 319 VR_ISA_PORT_BASE, VR_ISA_PORT_BASE + VR_ISA_PORT_SIZE, step); 320 for (i = VR_ISA_PORT_BASE; i < VR_ISA_PORT_BASE+VR_ISA_PORT_SIZE; 321 i+= step) { 322 __read_revid (0x3e0); 323 __read_revid (0x3e2); 324 } 325 } 326 #endif /* DEBUG_FIND_PCIC */ 327 328 329 #ifdef DEBUG_FIND_COMPORT 330 #warning DEBUG_FIND_COMPORT 331 332 static int probe_com(u_int32_t); 333 334 static int 335 probe_com(u_int32_t port_addr) 336 { 337 u_int32_t addr; 338 u_int8_t ubtmp1, ubtmp2; 339 340 addr = MIPS_PHYS_TO_KSEG1(port_addr); 341 342 *((volatile u_int8_t *)(addr + com_cfcr)) = LCR_8BITS; 343 *((volatile u_int8_t *)(addr + com_iir)) = 0; 344 345 ubtmp1 = *((volatile u_int8_t *)(addr + com_cfcr)); 346 ubtmp2 = *((volatile u_int8_t *)(addr + com_iir)); 347 348 if ((ubtmp1 != LCR_8BITS) || ((ubtmp2 & 0x38) != 0)) { 349 return (0); 350 } 351 352 return (1); 353 } 354 355 static void 356 __find_comport() 357 { 358 int found; 359 u_int32_t port, step; 360 361 found = 0; 362 step = 0x08; 363 364 printf("Searching COM port. Trying ISA port %#x-%#x step %#x\n", 365 VR_ISA_PORT_BASE, VR_ISA_PORT_BASE + VR_ISA_PORT_SIZE - 1, step ); 366 367 for (port = VR_ISA_PORT_BASE; 368 port < (VR_ISA_PORT_BASE + VR_ISA_PORT_SIZE); port += step){ 369 if (probe_com(port)) { 370 found++; 371 printf("found %d at %#x\n", found, port); 372 } 373 } 374 } 375 #endif /* DEBUG_FIND_COMPORT */ 376