pckbc_isa.c revision 1.5 1 /* $NetBSD: pckbc_isa.c,v 1.5 2001/11/13 08:01:28 lukem 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/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: pckbc_isa.c,v 1.5 2001/11/13 08:01:28 lukem Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43 #include <sys/errno.h>
44 #include <sys/queue.h>
45 #include <sys/lock.h>
46
47 #include <machine/bus.h>
48
49 #include <dev/isa/isareg.h>
50 #include <dev/isa/isavar.h>
51
52 #include <dev/ic/i8042reg.h>
53 #include <dev/ic/pckbcvar.h>
54
55 int pckbc_isa_match __P((struct device *, struct cfdata *, void *));
56 void pckbc_isa_attach __P((struct device *, struct device *, void *));
57
58 struct pckbc_isa_softc {
59 struct pckbc_softc sc_pckbc;
60
61 isa_chipset_tag_t sc_ic;
62 int sc_irq[PCKBC_NSLOTS];
63 };
64
65 struct cfattach pckbc_isa_ca = {
66 sizeof(struct pckbc_isa_softc), pckbc_isa_match, pckbc_isa_attach,
67 };
68
69 void pckbc_isa_intr_establish __P((struct pckbc_softc *, pckbc_slot_t));
70
71 int
72 pckbc_isa_match(parent, match, aux)
73 struct device *parent;
74 struct cfdata *match;
75 void *aux;
76 {
77 struct isa_attach_args *ia = aux;
78 bus_space_tag_t iot = ia->ia_iot;
79 bus_space_handle_t ioh_d, ioh_c;
80 int res, ok = 1;
81
82 /* If values are hardwired to something that they can't be, punt. */
83 if ((ia->ia_iobase != IOBASEUNK && ia->ia_iobase != IO_KBD) ||
84 ia->ia_maddr != MADDRUNK ||
85 (ia->ia_irq != IRQUNK && ia->ia_irq != 1 /* XXX */) ||
86 ia->ia_drq != DRQUNK)
87 return (0);
88
89 if (pckbc_is_console(iot, IO_KBD) == 0) {
90 struct pckbc_internal t;
91
92 if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d))
93 return (0);
94 if (bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c)) {
95 bus_space_unmap(iot, ioh_d, 1);
96 return (0);
97 }
98
99 memset(&t, 0, sizeof(t));
100 t.t_iot = iot;
101 t.t_ioh_d = ioh_d;
102 t.t_ioh_c = ioh_c;
103
104 /* flush KBC */
105 (void) pckbc_poll_data1(&t, PCKBC_KBD_SLOT, 0);
106
107 /* KBC selftest */
108 if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) {
109 ok = 0;
110 goto out;
111 }
112 res = pckbc_poll_data1(&t, PCKBC_KBD_SLOT, 0);
113 if (res != 0x55) {
114 printf("kbc selftest: %x\n", res);
115 ok = 0;
116 }
117 out:
118 bus_space_unmap(iot, ioh_d, 1);
119 bus_space_unmap(iot, ioh_c, 1);
120 }
121
122 if (ok) {
123 ia->ia_iobase = IO_KBD;
124 ia->ia_iosize = 5;
125 ia->ia_msize = 0x0;
126 }
127 return (ok);
128 }
129
130 void
131 pckbc_isa_attach(parent, self, aux)
132 struct device *parent, *self;
133 void *aux;
134 {
135 struct pckbc_isa_softc *isc = (void *)self;
136 struct pckbc_softc *sc = &isc->sc_pckbc;
137 struct isa_attach_args *ia = aux;
138 struct pckbc_internal *t;
139 bus_space_tag_t iot;
140 bus_space_handle_t ioh_d, ioh_c;
141
142 isc->sc_ic = ia->ia_ic;
143 iot = ia->ia_iot;
144
145 /*
146 * Set up IRQs for "normal" ISA.
147 *
148 * XXX The "aux" slot is different (9) on the Alpha AXP150 Jensen.
149 */
150 isc->sc_irq[PCKBC_KBD_SLOT] = 1;
151 isc->sc_irq[PCKBC_AUX_SLOT] = 12;
152
153 sc->intr_establish = pckbc_isa_intr_establish;
154
155 if (pckbc_is_console(iot, IO_KBD)) {
156 t = &pckbc_consdata;
157 ioh_d = t->t_ioh_d;
158 ioh_c = t->t_ioh_c;
159 pckbc_console_attached = 1;
160 /* t->t_cmdbyte was initialized by cnattach */
161 } else {
162 if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d) ||
163 bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c))
164 panic("pckbc_attach: couldn't map");
165
166 t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
167 memset(t, 0, sizeof(struct pckbc_internal));
168 t->t_iot = iot;
169 t->t_ioh_d = ioh_d;
170 t->t_ioh_c = ioh_c;
171 t->t_addr = IO_KBD;
172 t->t_cmdbyte = KC8_CPU; /* Enable ports */
173 callout_init(&t->t_cleanup);
174 }
175
176 t->t_sc = sc;
177 sc->id = t;
178
179 printf("\n");
180
181 /* Finish off the attach. */
182 pckbc_attach(sc);
183 }
184
185 void
186 pckbc_isa_intr_establish(sc, slot)
187 struct pckbc_softc *sc;
188 pckbc_slot_t slot;
189 {
190 struct pckbc_isa_softc *isc = (void *) sc;
191 void *rv;
192
193 rv = isa_intr_establish(isc->sc_ic, isc->sc_irq[slot], IST_EDGE,
194 IPL_TTY, pckbcintr, sc);
195 if (rv == NULL) {
196 printf("%s: unable to establish interrupt for %s slot\n",
197 sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
198 } else {
199 printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname,
200 isc->sc_irq[slot], pckbc_slot_names[slot]);
201 }
202 }
203