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