pckbc_pnpbios.c revision 1.15 1 /* $NetBSD: pckbc_pnpbios.c,v 1.15 2008/03/17 01:16:22 cube Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * PNPBIOS attachment for the PC Keyboard Controller driver.
41 *
42 * This is a little wonky. The keyboard controller actually
43 * has 2 PNPBIOS nodes: one for the controller and the keyboard
44 * interrupt, and one for the aux port (mouse) interrupt.
45 *
46 * For this reason, we actually attach *two* instances of this
47 * driver. After both of them have been found, then we attach
48 * sub-devices.
49 */
50
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: pckbc_pnpbios.c,v 1.15 2008/03/17 01:16:22 cube Exp $");
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/proc.h>
58 #include <sys/device.h>
59 #include <sys/malloc.h>
60 #include <sys/errno.h>
61 #include <sys/queue.h>
62 #include <sys/bus.h>
63
64 #include <dev/isa/isareg.h>
65 #include <dev/isa/isavar.h>
66
67 #include <dev/ic/i8042reg.h>
68 #include <dev/ic/pckbcvar.h>
69
70 #include <i386/pnpbios/pnpbiosvar.h>
71
72 int pckbc_pnpbios_match(device_t, cfdata_t, void *);
73 void pckbc_pnpbios_attach(device_t, device_t, void *);
74
75 struct pckbc_pnpbios_softc {
76 struct pckbc_softc sc_pckbc;
77
78 isa_chipset_tag_t sc_ic;
79 int sc_irq;
80 int sc_ist;
81 pckbc_slot_t sc_slot;
82 };
83
84 /* Save first port: */
85 static struct pckbc_pnpbios_softc *first;
86
87 extern struct cfdriver pckbc_cd;
88
89 CFATTACH_DECL_NEW(pckbc_pnpbios, sizeof(struct pckbc_pnpbios_softc),
90 pckbc_pnpbios_match, pckbc_pnpbios_attach, NULL, NULL);
91
92 void pckbc_pnpbios_intr_establish(struct pckbc_softc *, pckbc_slot_t);
93 static void pckbc_pnpbios_finish_attach(device_t);
94
95 int
96 pckbc_pnpbios_match(device_t parent, cfdata_t match, void *aux)
97 {
98 struct pnpbiosdev_attach_args *aa = aux;
99
100 if (strcmp(aa->idstr, "PNP0303") == 0 ||
101 strcmp(aa->idstr, "PNP0320") == 0) /* Japanese 106 */
102 return (1); /* plus more? */
103 if (strcmp(aa->idstr, "PNP0F13") == 0)
104 return (1);
105
106 return (0);
107 }
108
109 void
110 pckbc_pnpbios_attach(device_t parent, device_t self, void *aux)
111 {
112 struct pckbc_pnpbios_softc *psc = device_private(self);
113 struct pckbc_softc *sc = &psc->sc_pckbc;
114 struct pckbc_internal *t;
115 struct pnpbiosdev_attach_args *aa = aux;
116 bus_space_tag_t iot;
117 bus_space_handle_t ioh_d, ioh_c;
118 pckbc_slot_t peer;
119 int iobase;
120
121 sc->sc_dv = self;
122 if (strncmp(aa->idstr, "PNP03", 5) == 0) {
123 psc->sc_slot = PCKBC_KBD_SLOT;
124 peer = PCKBC_AUX_SLOT;
125 } else if (strcmp(aa->idstr, "PNP0F13") == 0) {
126 psc->sc_slot = PCKBC_AUX_SLOT;
127 peer = PCKBC_KBD_SLOT;
128 } else {
129 aprint_error(": unknown port!\n");
130 panic("pckbc_pnpbios_attach: impossible");
131 }
132
133 aprint_normal(": %s port\n", pckbc_slot_names[psc->sc_slot]);
134
135 if (pnpbios_getirqnum(aa->pbt, aa->resc, 0, &psc->sc_irq,
136 &psc->sc_ist) != 0) {
137 aprint_error_dev(self, "unable to get IRQ number or type\n");
138 return;
139 }
140
141 psc->sc_ic = aa->ic;
142
143 if (!first)
144 first = psc;
145
146 if (!first->sc_pckbc.id) {
147
148 if (pnpbios_getiobase(aa->pbt, aa->resc, 0, &iot, &iobase))
149 return;
150
151 if (pckbc_is_console(iot, iobase)) {
152 t = &pckbc_consdata;
153 ioh_d = t->t_ioh_d;
154 ioh_c = t->t_ioh_c;
155 pckbc_console_attached = 1;
156 /* t->t_cmdbyte was initialized by cnattach */
157 } else {
158 if (pnpbios_io_map(aa->pbt, aa->resc, 0,
159 &iot, &ioh_d) ||
160 pnpbios_io_map(aa->pbt, aa->resc, 1,
161 &iot, &ioh_c))
162 panic("pckbc_pnpbios_attach: couldn't map");
163
164 t = malloc(sizeof(struct pckbc_internal),
165 M_DEVBUF, M_WAITOK);
166 memset(t, 0, sizeof(*t));
167 t->t_iot = iot;
168 t->t_ioh_d = ioh_d;
169 t->t_ioh_c = ioh_c;
170 t->t_addr = iobase;
171 t->t_cmdbyte = KC8_CPU; /* Enable ports */
172 callout_init(&t->t_cleanup, 0);
173 }
174
175 t->t_sc = &first->sc_pckbc;
176 first->sc_pckbc.id = t;
177
178 first->sc_pckbc.intr_establish = pckbc_pnpbios_intr_establish;
179 config_defer(first->sc_pckbc.sc_dv,
180 pckbc_pnpbios_finish_attach);
181 }
182 }
183
184 static void
185 pckbc_pnpbios_finish_attach(device_t dv)
186 {
187
188 pckbc_attach(device_private(dv));
189 }
190
191 void
192 pckbc_pnpbios_intr_establish(struct pckbc_softc *sc,
193 pckbc_slot_t slot)
194 {
195 struct pckbc_pnpbios_softc *psc;
196 isa_chipset_tag_t ic = NULL;
197 void *rv = NULL;
198 int irq, ist;
199 int i;
200
201 /*
202 * Note we're always called with sc == first.
203 */
204 for (i = 0; i < pckbc_cd.cd_ndevs; i++) {
205 psc = pckbc_cd.cd_devs[i];
206 if (psc && psc->sc_slot == slot) {
207 irq = psc->sc_irq;
208 ist = psc->sc_ist;
209 ic = psc->sc_ic;
210 break;
211 }
212 }
213 if (i < pckbc_cd.cd_ndevs)
214 rv = isa_intr_establish(ic, irq, ist, IPL_TTY, pckbcintr, sc);
215 if (rv == NULL) {
216 aprint_error_dev(sc->sc_dv,
217 "unable to establish interrupt for %s slot\n",
218 pckbc_slot_names[slot]);
219 } else {
220 aprint_normal_dev(sc->sc_dv, "using irq %d for %s slot\n",
221 irq, pckbc_slot_names[slot]);
222 }
223 }
224