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