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