pckbc_acpi.c revision 1.1 1 /* $NetBSD: pckbc_acpi.c,v 1.1 2002/12/28 08:45:31 matt 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_acpi.c,v 1.1 2002/12/28 08:45:31 matt 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/lock.h>
63
64 #include <machine/bus.h>
65
66 #include <dev/isa/isareg.h>
67 #include <dev/isa/isavar.h>
68
69 #include <dev/ic/i8042reg.h>
70 #include <dev/ic/pckbcvar.h>
71
72 #include <dev/acpi/acpivar.h>
73
74 int pckbc_acpi_match(struct device *, struct cfdata *, void *);
75 void pckbc_acpi_attach(struct device *, struct device *, void *);
76
77 struct pckbc_acpi_softc {
78 struct pckbc_softc sc_pckbc;
79
80 isa_chipset_tag_t sc_ic;
81 int sc_irq;
82 int sc_ist;
83 pckbc_slot_t sc_slot;
84 };
85
86 /* Save first port: */
87 static struct pckbc_acpi_softc *first;
88
89 extern struct cfdriver pckbc_cd;
90
91 CFATTACH_DECL(pckbc_acpi, sizeof(struct pckbc_acpi_softc),
92 pckbc_acpi_match, pckbc_acpi_attach, NULL, NULL);
93
94 void pckbc_acpi_intr_establish(struct pckbc_softc *, pckbc_slot_t);
95
96 /*
97 * Supported Device IDs
98 */
99
100 static const char * const pckbc_acpi_ids[] = {
101 "PNP0303", /* Standard PC KBD/MS port */
102 "PNP0320", /* Japanese 106 */
103 "PNP0F13",
104 NULL
105 };
106
107 /*
108 * com_acpi_match: autoconf(9) match routine
109 */
110 int
111 pckbc_acpi_match(struct device *parent, struct cfdata *match, void *aux)
112 {
113 struct acpi_attach_args *aa = aux;
114 const char *id;
115 int i;
116
117 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
118 return (0);
119
120 for (i = 0; (id = pckbc_acpi_ids[i]) != NULL; ++i) {
121 if (strcmp(aa->aa_node->ad_devinfo.HardwareId, id) == 0)
122 return (1);
123 }
124
125 /* No matches found */
126 return (0);
127 }
128
129 void
130 pckbc_acpi_attach(struct device *parent,
131 struct device *self,
132 void *aux)
133 {
134 struct pckbc_acpi_softc *psc = (void *) self;
135 struct pckbc_softc *sc = &psc->sc_pckbc;
136 struct pckbc_internal *t;
137 struct acpi_attach_args *aa = aux;
138 bus_space_handle_t ioh_d, ioh_c;
139 const char *idstr = aa->aa_node->ad_devinfo.HardwareId;
140 pckbc_slot_t peer;
141 struct acpi_resources res;
142 struct acpi_io *io0, *io1;
143 struct acpi_irq *irq;
144 ACPI_STATUS rv;
145
146 psc->sc_ic = aa->aa_ic;
147
148 if (strncmp(idstr, "PNP03", 5) == 0) {
149 psc->sc_slot = PCKBC_KBD_SLOT;
150 peer = PCKBC_AUX_SLOT;
151 } else if (strcmp(idstr, "PNP0F13") == 0) {
152 psc->sc_slot = PCKBC_AUX_SLOT;
153 peer = PCKBC_KBD_SLOT;
154 } else {
155 printf(": unknown port!\n");
156 panic("pckbc_acpi_attach: impossible");
157 }
158
159 printf(": %s port\n", pckbc_slot_names[psc->sc_slot]);
160
161 /* parse resources */
162 rv = acpi_resource_parse(&sc->sc_dv, aa->aa_node, &res,
163 &acpi_resource_parse_ops_default);
164 if (rv != AE_OK) {
165 printf("%s: unable to parse resources\n", sc->sc_dv.dv_xname);
166 return;
167 }
168
169 /* find our IRQ */
170 irq = acpi_res_irq(&res, 0);
171 if (irq == NULL) {
172 printf("%s: unable to find irq resource\n", sc->sc_dv.dv_xname);
173 return;
174 }
175 psc->sc_irq = irq->ar_irq;
176 psc->sc_ist = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
177
178 if (!first)
179 first = psc;
180
181 if (!first->sc_pckbc.id) {
182
183 io0 = acpi_res_io(&res, 0);
184 if (io0 == NULL) {
185 printf("%s: unable to find i/o resources\n",
186 sc->sc_dv.dv_xname);
187 return;
188 }
189
190 if (pckbc_is_console(aa->aa_iot, io0->ar_base)) {
191 t = &pckbc_consdata;
192 ioh_d = t->t_ioh_d;
193 ioh_c = t->t_ioh_c;
194 pckbc_console_attached = 1;
195 /* t->t_cmdbyte was initialized by cnattach */
196 } else {
197 io1 = acpi_res_io(&res, 1);
198 if (io1 == NULL) {
199 printf("%s: unable to find i/o resources\n",
200 sc->sc_dv.dv_xname);
201 return;
202 }
203 if (bus_space_map(aa->aa_iot, io0->ar_base,
204 io0->ar_length, 0, &ioh_d) ||
205 bus_space_map(aa->aa_iot, io1->ar_base,
206 io1->ar_length, 0, &ioh_c))
207 panic("pckbc_acpi_attach: couldn't map");
208
209 t = malloc(sizeof(struct pckbc_internal),
210 M_DEVBUF, M_WAITOK);
211 memset(t, 0, sizeof(*t));
212 t->t_iot = aa->aa_iot;
213 t->t_ioh_d = ioh_d;
214 t->t_ioh_c = ioh_c;
215 t->t_addr = io0->ar_base;
216 t->t_cmdbyte = KC8_CPU; /* Enable ports */
217 callout_init(&t->t_cleanup);
218 }
219
220 t->t_sc = &first->sc_pckbc;
221 first->sc_pckbc.id = t;
222
223 first->sc_pckbc.intr_establish = pckbc_acpi_intr_establish;
224 config_defer(&first->sc_pckbc.sc_dv,
225 (void(*)(struct device *))pckbc_attach);
226 }
227 }
228
229 void
230 pckbc_acpi_intr_establish(struct pckbc_softc *sc,
231 pckbc_slot_t slot)
232 {
233 struct pckbc_acpi_softc *psc;
234 isa_chipset_tag_t ic = NULL;
235 void *rv = NULL;
236 int irq, ist;
237 int i;
238
239 /*
240 * Note we're always called with sc == first.
241 */
242 for (i = 0; i < pckbc_cd.cd_ndevs; i++) {
243 psc = pckbc_cd.cd_devs[i];
244 if (psc && psc->sc_slot == slot) {
245 irq = psc->sc_irq;
246 ist = psc->sc_ist;
247 ic = psc->sc_ic;
248 break;
249 }
250 }
251 if (i < pckbc_cd.cd_ndevs)
252 rv = isa_intr_establish(ic, irq, ist, IPL_TTY, pckbcintr, sc);
253 if (rv == NULL) {
254 printf("%s: unable to establish interrupt for %s slot\n",
255 sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
256 } else {
257 printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname,
258 irq, pckbc_slot_names[slot]);
259 }
260 }
261