1 /* $OpenBSD: pckbc_isa.c,v 1.2 2001/07/08 06:41:38 fgsch Exp $ */ 2 /* $NetBSD: pckbc_isa.c,v 1.2 2000/03/23 07:01:35 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1998 6 * Matthias Drochner. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed for the NetBSD Project 19 * by Matthias Drochner. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/proc.h> 39 #include <sys/device.h> 40 #include <sys/malloc.h> 41 #include <sys/errno.h> 42 #include <sys/queue.h> 43 #include <sys/lock.h> 44 45 #include <machine/bus.h> 46 47 #include <dev/isa/isareg.h> 48 #include <dev/isa/isavar.h> 49 50 #include <dev/ic/i8042reg.h> 51 #include <dev/ic/pckbcvar.h> 52 53 int pckbc_isa_match __P((struct device *, void *, void *)); 54 void pckbc_isa_attach __P((struct device *, struct device *, void *)); 55 56 struct pckbc_isa_softc { 57 struct pckbc_softc sc_pckbc; 58 59 isa_chipset_tag_t sc_ic; 60 int sc_irq[PCKBC_NSLOTS]; 61 }; 62 63 struct cfattach pckbc_isa_ca = { 64 sizeof(struct pckbc_isa_softc), pckbc_isa_match, pckbc_isa_attach, 65 }; 66 67 void pckbc_isa_intr_establish __P((struct pckbc_softc *, pckbc_slot_t)); 68 69 int 70 pckbc_isa_match(parent, match, aux) 71 struct device *parent; 72 void *match; 73 void *aux; 74 { 75 struct isa_attach_args *ia = aux; 76 bus_space_tag_t iot = ia->ia_iot; 77 bus_space_handle_t ioh_d, ioh_c; 78 int res, ok = 1; 79 80 /* If values are hardwired to something that they can't be, punt. */ 81 if ((ia->ia_iobase != IOBASEUNK && ia->ia_iobase != IO_KBD) || 82 ia->ia_maddr != MADDRUNK || 83 (ia->ia_irq != IRQUNK && ia->ia_irq != 1 /* XXX */) || 84 ia->ia_drq != DRQUNK) 85 return (0); 86 87 if (pckbc_is_console(iot, IO_KBD) == 0) { 88 if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d)) 89 return (0); 90 if (bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c)) { 91 bus_space_unmap(iot, ioh_d, 1); 92 return (0); 93 } 94 95 /* flush KBC */ 96 (void) pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0); 97 98 /* KBC selftest */ 99 if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) { 100 ok = 0; 101 goto out; 102 } 103 res = pckbc_poll_data1(iot, ioh_d, ioh_c, PCKBC_KBD_SLOT, 0); 104 if (res != 0x55) { 105 printf("kbc selftest: %x\n", res); 106 ok = 0; 107 } 108 out: 109 bus_space_unmap(iot, ioh_d, 1); 110 bus_space_unmap(iot, ioh_c, 1); 111 } 112 113 if (ok) { 114 ia->ia_iobase = IO_KBD; 115 ia->ia_iosize = 5; 116 ia->ia_msize = 0x0; 117 } 118 return (ok); 119 } 120 121 void 122 pckbc_isa_attach(parent, self, aux) 123 struct device *parent, *self; 124 void *aux; 125 { 126 struct pckbc_isa_softc *isc = (void *)self; 127 struct pckbc_softc *sc = &isc->sc_pckbc; 128 struct isa_attach_args *ia = aux; 129 struct pckbc_internal *t; 130 bus_space_tag_t iot; 131 bus_space_handle_t ioh_d, ioh_c; 132 133 isc->sc_ic = ia->ia_ic; 134 iot = ia->ia_iot; 135 136 /* 137 * Set up IRQs for "normal" ISA. 138 * 139 * XXX The "aux" slot is different (9) on the Alpha AXP150 Jensen. 140 */ 141 isc->sc_irq[PCKBC_KBD_SLOT] = 1; 142 isc->sc_irq[PCKBC_AUX_SLOT] = 12; 143 144 sc->intr_establish = pckbc_isa_intr_establish; 145 146 if (pckbc_is_console(iot, IO_KBD)) { 147 t = &pckbc_consdata; 148 ioh_d = t->t_ioh_d; 149 ioh_c = t->t_ioh_c; 150 pckbc_console_attached = 1; 151 /* t->t_cmdbyte was initialized by cnattach */ 152 } else { 153 if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d) || 154 bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c)) 155 panic("pckbc_attach: couldn't map"); 156 157 t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK); 158 bzero(t, sizeof(struct pckbc_internal)); 159 t->t_iot = iot; 160 t->t_ioh_d = ioh_d; 161 t->t_ioh_c = ioh_c; 162 t->t_addr = IO_KBD; 163 t->t_cmdbyte = KC8_CPU; /* Enable ports */ 164 } 165 166 t->t_sc = sc; 167 sc->id = t; 168 169 printf("\n"); 170 171 /* Finish off the attach. */ 172 pckbc_attach(sc); 173 } 174 175 void 176 pckbc_isa_intr_establish(sc, slot) 177 struct pckbc_softc *sc; 178 pckbc_slot_t slot; 179 { 180 struct pckbc_isa_softc *isc = (void *) sc; 181 void *rv; 182 183 rv = isa_intr_establish(isc->sc_ic, isc->sc_irq[slot], IST_EDGE, 184 IPL_TTY, pckbcintr, sc, sc->sc_dv.dv_xname); 185 if (rv == NULL) { 186 printf("%s: unable to establish interrupt for %s slot\n", 187 sc->sc_dv.dv_xname, pckbc_slot_names[slot]); 188 } else { 189 printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname, 190 isc->sc_irq[slot], pckbc_slot_names[slot]); 191 } 192 } 193