1 /* $NetBSD: pckbc_isa.c,v 1.22 2007/10/19 12:00:22 ad Exp $ */ 2 3 /* 4 * Copyright (c) 1998 5 * Matthias Drochner. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: pckbc_isa.c,v 1.22 2007/10/19 12:00:22 ad Exp $"); 30 31 #include "opt_pckbc.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/proc.h> 37 #include <sys/device.h> 38 #include <sys/malloc.h> 39 #include <sys/errno.h> 40 #include <sys/queue.h> 41 #include <sys/lock.h> 42 43 #include <sys/bus.h> 44 45 #include <dev/isa/isareg.h> 46 #include <dev/isa/isavar.h> 47 48 #include <dev/ic/i8042reg.h> 49 #include <dev/ic/pckbcvar.h> 50 51 int pckbc_isa_match(struct device *, struct cfdata *, void *); 52 void pckbc_isa_attach(struct device *, struct device *, void *); 53 54 struct pckbc_isa_softc { 55 struct pckbc_softc sc_pckbc; 56 57 isa_chipset_tag_t sc_ic; 58 int sc_irq[PCKBC_NSLOTS]; 59 }; 60 61 CFATTACH_DECL(pckbc_isa, sizeof(struct pckbc_isa_softc), 62 pckbc_isa_match, pckbc_isa_attach, NULL, NULL); 63 64 void pckbc_isa_intr_establish(struct pckbc_softc *, pckbc_slot_t); 65 66 int 67 pckbc_isa_match(struct device *parent, struct cfdata *match, 68 void *aux) 69 { 70 struct isa_attach_args *ia = aux; 71 bus_space_tag_t iot = ia->ia_iot; 72 bus_space_handle_t ioh_d, ioh_c; 73 int res, ok = 1; 74 75 if (ISA_DIRECT_CONFIG(ia)) 76 return (0); 77 78 /* If values are hardwired to something that they can't be, punt. */ 79 if (ia->ia_nio < 1 || 80 (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT && 81 ia->ia_io[0].ir_addr != IO_KBD)) 82 return (0); 83 84 if (ia->ia_niomem > 0 && 85 (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM)) 86 return (0); 87 88 if (ia->ia_nirq < 1 || 89 (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ && 90 ia->ia_irq[0].ir_irq != 1 /*XXX*/)) 91 return (0); 92 93 if (ia->ia_ndrq > 0 && 94 (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ)) 95 return (0); 96 97 if (pckbc_is_console(iot, IO_KBD) == 0) { 98 struct pckbc_internal t; 99 100 if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d)) 101 return (0); 102 if (bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c)) { 103 bus_space_unmap(iot, ioh_d, 1); 104 return (0); 105 } 106 107 memset(&t, 0, sizeof(t)); 108 t.t_iot = iot; 109 t.t_ioh_d = ioh_d; 110 t.t_ioh_c = ioh_c; 111 112 /* flush KBC */ 113 (void) pckbc_poll_data1(&t, PCKBC_KBD_SLOT); 114 115 /* KBC selftest */ 116 if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) { 117 ok = 0; 118 goto out; 119 } 120 res = pckbc_poll_data1(&t, PCKBC_KBD_SLOT); 121 #ifndef PCKBCNOTEST 122 if (res != 0x55) { 123 #ifdef PCKBCDEBUG 124 printf("kbc selftest: %x\n", res); 125 #endif 126 ok = 0; 127 } 128 #endif /* PCKBCNOTEST */ 129 out: 130 bus_space_unmap(iot, ioh_d, 1); 131 bus_space_unmap(iot, ioh_c, 1); 132 } 133 134 if (ok) { 135 ia->ia_io[0].ir_addr = IO_KBD; 136 ia->ia_io[0].ir_size = 5; 137 ia->ia_nio = 1; 138 139 ia->ia_niomem = 0; 140 ia->ia_nirq = 0; 141 ia->ia_ndrq = 0; 142 } 143 return (ok); 144 } 145 146 void 147 pckbc_isa_attach(struct device *parent, struct device *self, void *aux) 148 { 149 struct pckbc_isa_softc *isc = (void *)self; 150 struct pckbc_softc *sc = &isc->sc_pckbc; 151 struct isa_attach_args *ia = aux; 152 struct pckbc_internal *t; 153 bus_space_tag_t iot; 154 bus_space_handle_t ioh_d, ioh_c; 155 156 isc->sc_ic = ia->ia_ic; 157 iot = ia->ia_iot; 158 159 switch (ia->ia_nirq) { 160 case 1: 161 /* Both channels use the same IRQ. */ 162 isc->sc_irq[PCKBC_KBD_SLOT] = 163 isc->sc_irq[PCKBC_AUX_SLOT] = ia->ia_irq[0].ir_irq; 164 break; 165 166 case 2: 167 /* First IRQ is kbd, second IRQ is aux port. */ 168 isc->sc_irq[PCKBC_KBD_SLOT] = ia->ia_irq[0].ir_irq; 169 isc->sc_irq[PCKBC_AUX_SLOT] = ia->ia_irq[1].ir_irq; 170 break; 171 172 default: 173 /* Set up IRQs for "normal" ISA. */ 174 isc->sc_irq[PCKBC_KBD_SLOT] = 1; 175 isc->sc_irq[PCKBC_AUX_SLOT] = 12; 176 break; 177 } 178 179 sc->intr_establish = pckbc_isa_intr_establish; 180 181 if (pckbc_is_console(iot, IO_KBD)) { 182 t = &pckbc_consdata; 183 ioh_d = t->t_ioh_d; 184 ioh_c = t->t_ioh_c; 185 pckbc_console_attached = 1; 186 /* t->t_cmdbyte was initialized by cnattach */ 187 } else { 188 if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d) || 189 bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c)) 190 panic("pckbc_attach: couldn't map"); 191 192 t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, 193 M_WAITOK|M_ZERO); 194 t->t_iot = iot; 195 t->t_ioh_d = ioh_d; 196 t->t_ioh_c = ioh_c; 197 t->t_addr = IO_KBD; 198 t->t_cmdbyte = KC8_CPU; /* Enable ports */ 199 callout_init(&t->t_cleanup, 0); 200 } 201 202 t->t_sc = sc; 203 sc->id = t; 204 205 printf("\n"); 206 207 /* Finish off the attach. */ 208 pckbc_attach(sc); 209 } 210 211 void 212 pckbc_isa_intr_establish(sc, slot) 213 struct pckbc_softc *sc; 214 pckbc_slot_t slot; 215 { 216 struct pckbc_isa_softc *isc = (void *) sc; 217 void *rv; 218 219 rv = isa_intr_establish(isc->sc_ic, isc->sc_irq[slot], IST_EDGE, 220 IPL_TTY, pckbcintr, sc); 221 if (rv == NULL) { 222 printf("%s: unable to establish interrupt for %s slot\n", 223 sc->sc_dv.dv_xname, pckbc_slot_names[slot]); 224 } else { 225 printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname, 226 isc->sc_irq[slot], pckbc_slot_names[slot]); 227 } 228 } 229