pckbc_acpi.c revision 1.24.6.1 1 /* $NetBSD: pckbc_acpi.c,v 1.24.6.1 2008/04/03 12:42:37 mjf 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 * ACPI attachment for the PC Keyboard Controller driver.
41 *
42 * This is a little wonky. The keyboard controller actually
43 * has 2 ACPI 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_acpi.c,v 1.24.6.1 2008/04/03 12:42:37 mjf 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 <dev/acpi/acpivar.h>
71
72 static int pckbc_acpi_match(device_t, cfdata_t, void *);
73 static void pckbc_acpi_attach(device_t, device_t, void *);
74
75 struct pckbc_acpi_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_acpi_softc *first;
86
87 extern struct cfdriver pckbc_cd;
88
89 CFATTACH_DECL_NEW(pckbc_acpi, sizeof(struct pckbc_acpi_softc),
90 pckbc_acpi_match, pckbc_acpi_attach, NULL, NULL);
91
92 static void pckbc_acpi_intr_establish(struct pckbc_softc *, pckbc_slot_t);
93 static void pckbc_acpi_finish_attach(device_t);
94
95 /*
96 * Supported Device IDs
97 */
98
99 static const char * const pckbc_acpi_ids_kbd[] = {
100 "PNP03??", /* Standard PC KBD port */
101 NULL
102 };
103
104 static const char * const pckbc_acpi_ids_ms[] = {
105 "PNP0F03",
106 "PNP0F0E",
107 "PNP0F12",
108 "PNP0F13",
109 "PNP0F19",
110 "PNP0F1B",
111 "PNP0F1C",
112 NULL
113 };
114
115 /*
116 * pckbc_acpi_match: autoconf(9) match routine
117 */
118 static int
119 pckbc_acpi_match(device_t parent, cfdata_t match, void *aux)
120 {
121 struct acpi_attach_args *aa = aux;
122 int rv;
123
124 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
125 return 0;
126
127 rv = acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_kbd);
128 if (rv)
129 return rv;
130 rv = acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_ms);
131 if (rv)
132 return rv;
133 return 0;
134 }
135
136 static void
137 pckbc_acpi_attach(device_t parent, device_t self, void *aux)
138 {
139 struct pckbc_acpi_softc *psc = device_private(self);
140 struct pckbc_softc *sc = &psc->sc_pckbc;
141 struct pckbc_internal *t;
142 struct acpi_attach_args *aa = aux;
143 bus_space_handle_t ioh_d, ioh_c;
144 pckbc_slot_t peer;
145 struct acpi_resources res;
146 struct acpi_io *io0, *io1, *ioswap;
147 struct acpi_irq *irq;
148 ACPI_STATUS rv;
149
150 sc->sc_dv = self;
151 psc->sc_ic = aa->aa_ic;
152
153 if (acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_kbd)) {
154 psc->sc_slot = PCKBC_KBD_SLOT;
155 peer = PCKBC_AUX_SLOT;
156 } else if (acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_ms)) {
157 psc->sc_slot = PCKBC_AUX_SLOT;
158 peer = PCKBC_KBD_SLOT;
159 } else {
160 aprint_error(": unknown port!\n");
161 panic("pckbc_acpi_attach: impossible");
162 }
163
164 aprint_naive("\n");
165 aprint_normal(": %s port\n", pckbc_slot_names[psc->sc_slot]);
166
167 /* parse resources */
168 rv = acpi_resource_parse(sc->sc_dv, aa->aa_node->ad_handle, "_CRS",
169 &res, &acpi_resource_parse_ops_default);
170 if (ACPI_FAILURE(rv))
171 return;
172
173 /* find our IRQ */
174 irq = acpi_res_irq(&res, 0);
175 if (irq == NULL) {
176 aprint_error_dev(self, "unable to find irq resource\n");
177 goto out;
178 }
179 psc->sc_irq = irq->ar_irq;
180 psc->sc_ist = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
181
182 if (psc->sc_slot == PCKBC_KBD_SLOT)
183 first = psc;
184
185 if ((!first || !first->sc_pckbc.id) &&
186 (psc->sc_slot == PCKBC_KBD_SLOT)) {
187
188 io0 = acpi_res_io(&res, 0);
189 io1 = acpi_res_io(&res, 1);
190 if (io0 == NULL || io1 == NULL) {
191 aprint_error_dev(self,
192 "unable to find i/o resources\n");
193 goto out;
194 }
195
196 /*
197 * JDM: Some firmware doesn't report resources in the order we
198 * expect; sort IO resources here (lowest first)
199 */
200 if (io0->ar_base > io1->ar_base) {
201 ioswap = io0;
202 io0 = io1;
203 io1 = ioswap;
204 }
205
206 if (pckbc_is_console(aa->aa_iot, io0->ar_base)) {
207 t = &pckbc_consdata;
208 ioh_d = t->t_ioh_d;
209 ioh_c = t->t_ioh_c;
210 pckbc_console_attached = 1;
211 /* t->t_cmdbyte was initialized by cnattach */
212 } else {
213 if (bus_space_map(aa->aa_iot, io0->ar_base,
214 io0->ar_length, 0, &ioh_d) ||
215 bus_space_map(aa->aa_iot, io1->ar_base,
216 io1->ar_length, 0, &ioh_c))
217 panic("pckbc_acpi_attach: couldn't map");
218
219 t = malloc(sizeof(struct pckbc_internal),
220 M_DEVBUF, M_WAITOK);
221 memset(t, 0, sizeof(*t));
222 t->t_iot = aa->aa_iot;
223 t->t_ioh_d = ioh_d;
224 t->t_ioh_c = ioh_c;
225 t->t_addr = io0->ar_base;
226 t->t_cmdbyte = KC8_CPU; /* Enable ports */
227 callout_init(&t->t_cleanup, 0);
228 }
229
230 t->t_sc = &first->sc_pckbc;
231 first->sc_pckbc.id = t;
232
233 if (!pmf_device_register(self, NULL, pckbc_resume))
234 aprint_error_dev(self,
235 "couldn't establish power handler\n");
236
237 first->sc_pckbc.intr_establish = pckbc_acpi_intr_establish;
238 config_defer(first->sc_pckbc.sc_dv, pckbc_acpi_finish_attach);
239 } else if (!pmf_device_register(self, NULL, NULL))
240 aprint_error_dev(self, "couldn't establish power handler\n");
241 out:
242 acpi_resource_cleanup(&res);
243 }
244
245 static void
246 pckbc_acpi_intr_establish(struct pckbc_softc *sc, pckbc_slot_t slot)
247 {
248 struct pckbc_acpi_softc *psc;
249 isa_chipset_tag_t ic = NULL;
250 void *rv = NULL;
251 int irq = 0, ist = 0; /* XXX: gcc */
252 int i;
253
254 /*
255 * Note we're always called with sc == first.
256 */
257 for (i = 0; i < pckbc_cd.cd_ndevs; i++) {
258 psc = device_private(pckbc_cd.cd_devs[i]);
259 if (psc && psc->sc_slot == slot) {
260 irq = psc->sc_irq;
261 ist = psc->sc_ist;
262 ic = psc->sc_ic;
263 break;
264 }
265 }
266 if (i < pckbc_cd.cd_ndevs)
267 rv = isa_intr_establish(ic, irq, ist, IPL_TTY, pckbcintr, sc);
268 if (rv == NULL) {
269 aprint_error_dev(sc->sc_dv,
270 "unable to establish interrupt for %s slot\n",
271 pckbc_slot_names[slot]);
272 } else {
273 aprint_normal_dev(sc->sc_dv, "using irq %d for %s slot\n",
274 irq, pckbc_slot_names[slot]);
275 }
276 }
277
278 static void
279 pckbc_acpi_finish_attach(device_t dv)
280 {
281
282 pckbc_attach(device_private(dv));
283 }
284