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