pckbc_acpi.c revision 1.3.2.2 1 1.3.2.2 thorpej /* $NetBSD: pckbc_acpi.c,v 1.3.2.2 2002/12/29 20:45:31 thorpej Exp $ */
2 1.3.2.2 thorpej
3 1.3.2.2 thorpej /*-
4 1.3.2.2 thorpej * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.3.2.2 thorpej * All rights reserved.
6 1.3.2.2 thorpej *
7 1.3.2.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 thorpej * by Jason R. Thorpe.
9 1.3.2.2 thorpej *
10 1.3.2.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 thorpej * modification, are permitted provided that the following conditions
12 1.3.2.2 thorpej * are met:
13 1.3.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.3.2.2 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.3.2.2 thorpej * must display the following acknowledgement:
20 1.3.2.2 thorpej * This product includes software developed by the NetBSD
21 1.3.2.2 thorpej * Foundation, Inc. and its contributors.
22 1.3.2.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3.2.2 thorpej * contributors may be used to endorse or promote products derived
24 1.3.2.2 thorpej * from this software without specific prior written permission.
25 1.3.2.2 thorpej *
26 1.3.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3.2.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3.2.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3.2.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3.2.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3.2.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3.2.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3.2.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3.2.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3.2.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3.2.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.3.2.2 thorpej */
38 1.3.2.2 thorpej
39 1.3.2.2 thorpej /*
40 1.3.2.2 thorpej * ACPI attachment for the PC Keyboard Controller driver.
41 1.3.2.2 thorpej *
42 1.3.2.2 thorpej * This is a little wonky. The keyboard controller actually
43 1.3.2.2 thorpej * has 2 ACPI nodes: one for the controller and the keyboard
44 1.3.2.2 thorpej * interrupt, and one for the aux port (mouse) interrupt.
45 1.3.2.2 thorpej *
46 1.3.2.2 thorpej * For this reason, we actually attach *two* instances of this
47 1.3.2.2 thorpej * driver. After both of them have been found, then we attach
48 1.3.2.2 thorpej * sub-devices.
49 1.3.2.2 thorpej */
50 1.3.2.2 thorpej
51 1.3.2.2 thorpej #include <sys/cdefs.h>
52 1.3.2.2 thorpej __KERNEL_RCSID(0, "$NetBSD: pckbc_acpi.c,v 1.3.2.2 2002/12/29 20:45:31 thorpej Exp $");
53 1.3.2.2 thorpej
54 1.3.2.2 thorpej #include <sys/param.h>
55 1.3.2.2 thorpej #include <sys/systm.h>
56 1.3.2.2 thorpej #include <sys/kernel.h>
57 1.3.2.2 thorpej #include <sys/proc.h>
58 1.3.2.2 thorpej #include <sys/device.h>
59 1.3.2.2 thorpej #include <sys/malloc.h>
60 1.3.2.2 thorpej #include <sys/errno.h>
61 1.3.2.2 thorpej #include <sys/queue.h>
62 1.3.2.2 thorpej #include <sys/lock.h>
63 1.3.2.2 thorpej
64 1.3.2.2 thorpej #include <machine/bus.h>
65 1.3.2.2 thorpej
66 1.3.2.2 thorpej #include <dev/isa/isareg.h>
67 1.3.2.2 thorpej #include <dev/isa/isavar.h>
68 1.3.2.2 thorpej
69 1.3.2.2 thorpej #include <dev/ic/i8042reg.h>
70 1.3.2.2 thorpej #include <dev/ic/pckbcvar.h>
71 1.3.2.2 thorpej
72 1.3.2.2 thorpej #include <dev/acpi/acpivar.h>
73 1.3.2.2 thorpej
74 1.3.2.2 thorpej int pckbc_acpi_match(struct device *, struct cfdata *, void *);
75 1.3.2.2 thorpej void pckbc_acpi_attach(struct device *, struct device *, void *);
76 1.3.2.2 thorpej
77 1.3.2.2 thorpej struct pckbc_acpi_softc {
78 1.3.2.2 thorpej struct pckbc_softc sc_pckbc;
79 1.3.2.2 thorpej
80 1.3.2.2 thorpej isa_chipset_tag_t sc_ic;
81 1.3.2.2 thorpej int sc_irq;
82 1.3.2.2 thorpej int sc_ist;
83 1.3.2.2 thorpej pckbc_slot_t sc_slot;
84 1.3.2.2 thorpej };
85 1.3.2.2 thorpej
86 1.3.2.2 thorpej /* Save first port: */
87 1.3.2.2 thorpej static struct pckbc_acpi_softc *first;
88 1.3.2.2 thorpej
89 1.3.2.2 thorpej extern struct cfdriver pckbc_cd;
90 1.3.2.2 thorpej
91 1.3.2.2 thorpej CFATTACH_DECL(pckbc_acpi, sizeof(struct pckbc_acpi_softc),
92 1.3.2.2 thorpej pckbc_acpi_match, pckbc_acpi_attach, NULL, NULL);
93 1.3.2.2 thorpej
94 1.3.2.2 thorpej void pckbc_acpi_intr_establish(struct pckbc_softc *, pckbc_slot_t);
95 1.3.2.2 thorpej
96 1.3.2.2 thorpej /*
97 1.3.2.2 thorpej * Supported Device IDs
98 1.3.2.2 thorpej */
99 1.3.2.2 thorpej
100 1.3.2.2 thorpej static const char * const pckbc_acpi_ids[] = {
101 1.3.2.2 thorpej "PNP0303", /* Standard PC KBD/MS port */
102 1.3.2.2 thorpej "PNP0320", /* Japanese 106 */
103 1.3.2.2 thorpej "PNP0F13",
104 1.3.2.2 thorpej "IBM3780", /* IBM pointing device */
105 1.3.2.2 thorpej NULL
106 1.3.2.2 thorpej };
107 1.3.2.2 thorpej
108 1.3.2.2 thorpej /*
109 1.3.2.2 thorpej * pckbc_acpi_match: autoconf(9) match routine
110 1.3.2.2 thorpej */
111 1.3.2.2 thorpej int
112 1.3.2.2 thorpej pckbc_acpi_match(struct device *parent, struct cfdata *match, void *aux)
113 1.3.2.2 thorpej {
114 1.3.2.2 thorpej struct acpi_attach_args *aa = aux;
115 1.3.2.2 thorpej const char *id;
116 1.3.2.2 thorpej int i;
117 1.3.2.2 thorpej
118 1.3.2.2 thorpej if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
119 1.3.2.2 thorpej return (0);
120 1.3.2.2 thorpej
121 1.3.2.2 thorpej for (i = 0; (id = pckbc_acpi_ids[i]) != NULL; ++i) {
122 1.3.2.2 thorpej if (strcmp(aa->aa_node->ad_devinfo.HardwareId, id) == 0)
123 1.3.2.2 thorpej return (1);
124 1.3.2.2 thorpej }
125 1.3.2.2 thorpej
126 1.3.2.2 thorpej /* No matches found */
127 1.3.2.2 thorpej return (0);
128 1.3.2.2 thorpej }
129 1.3.2.2 thorpej
130 1.3.2.2 thorpej void
131 1.3.2.2 thorpej pckbc_acpi_attach(struct device *parent,
132 1.3.2.2 thorpej struct device *self,
133 1.3.2.2 thorpej void *aux)
134 1.3.2.2 thorpej {
135 1.3.2.2 thorpej struct pckbc_acpi_softc *psc = (void *) self;
136 1.3.2.2 thorpej struct pckbc_softc *sc = &psc->sc_pckbc;
137 1.3.2.2 thorpej struct pckbc_internal *t;
138 1.3.2.2 thorpej struct acpi_attach_args *aa = aux;
139 1.3.2.2 thorpej bus_space_handle_t ioh_d, ioh_c;
140 1.3.2.2 thorpej const char *idstr = aa->aa_node->ad_devinfo.HardwareId;
141 1.3.2.2 thorpej pckbc_slot_t peer;
142 1.3.2.2 thorpej struct acpi_resources res;
143 1.3.2.2 thorpej struct acpi_io *io0, *io1;
144 1.3.2.2 thorpej struct acpi_irq *irq;
145 1.3.2.2 thorpej ACPI_STATUS rv;
146 1.3.2.2 thorpej
147 1.3.2.2 thorpej psc->sc_ic = aa->aa_ic;
148 1.3.2.2 thorpej
149 1.3.2.2 thorpej if (strncmp(idstr, "PNP03", 5) == 0) {
150 1.3.2.2 thorpej psc->sc_slot = PCKBC_KBD_SLOT;
151 1.3.2.2 thorpej peer = PCKBC_AUX_SLOT;
152 1.3.2.2 thorpej } else if (strncmp(idstr, "PNP0F", 5) == 0 ||
153 1.3.2.2 thorpej strcmp(idstr, "IBM3780") == 0) {
154 1.3.2.2 thorpej psc->sc_slot = PCKBC_AUX_SLOT;
155 1.3.2.2 thorpej peer = PCKBC_KBD_SLOT;
156 1.3.2.2 thorpej } else {
157 1.3.2.2 thorpej printf(": unknown port!\n");
158 1.3.2.2 thorpej panic("pckbc_acpi_attach: impossible");
159 1.3.2.2 thorpej }
160 1.3.2.2 thorpej
161 1.3.2.2 thorpej printf(": %s port\n", pckbc_slot_names[psc->sc_slot]);
162 1.3.2.2 thorpej
163 1.3.2.2 thorpej /* parse resources */
164 1.3.2.2 thorpej rv = acpi_resource_parse(&sc->sc_dv, aa->aa_node, &res,
165 1.3.2.2 thorpej &acpi_resource_parse_ops_default);
166 1.3.2.2 thorpej if (rv != AE_OK) {
167 1.3.2.2 thorpej printf("%s: unable to parse resources\n", sc->sc_dv.dv_xname);
168 1.3.2.2 thorpej return;
169 1.3.2.2 thorpej }
170 1.3.2.2 thorpej
171 1.3.2.2 thorpej /* find our IRQ */
172 1.3.2.2 thorpej irq = acpi_res_irq(&res, 0);
173 1.3.2.2 thorpej if (irq == NULL) {
174 1.3.2.2 thorpej printf("%s: unable to find irq resource\n", sc->sc_dv.dv_xname);
175 1.3.2.2 thorpej return;
176 1.3.2.2 thorpej }
177 1.3.2.2 thorpej psc->sc_irq = irq->ar_irq;
178 1.3.2.2 thorpej psc->sc_ist = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
179 1.3.2.2 thorpej
180 1.3.2.2 thorpej if (psc->sc_slot == PCKBC_KBD_SLOT)
181 1.3.2.2 thorpej first = psc;
182 1.3.2.2 thorpej
183 1.3.2.2 thorpej if ((!first || !first->sc_pckbc.id) &&
184 1.3.2.2 thorpej (psc->sc_slot == PCKBC_KBD_SLOT)) {
185 1.3.2.2 thorpej
186 1.3.2.2 thorpej io0 = acpi_res_io(&res, 0);
187 1.3.2.2 thorpej if (io0 == NULL) {
188 1.3.2.2 thorpej printf("%s: unable to find i/o resources\n",
189 1.3.2.2 thorpej sc->sc_dv.dv_xname);
190 1.3.2.2 thorpej return;
191 1.3.2.2 thorpej }
192 1.3.2.2 thorpej
193 1.3.2.2 thorpej if (pckbc_is_console(aa->aa_iot, io0->ar_base)) {
194 1.3.2.2 thorpej t = &pckbc_consdata;
195 1.3.2.2 thorpej ioh_d = t->t_ioh_d;
196 1.3.2.2 thorpej ioh_c = t->t_ioh_c;
197 1.3.2.2 thorpej pckbc_console_attached = 1;
198 1.3.2.2 thorpej /* t->t_cmdbyte was initialized by cnattach */
199 1.3.2.2 thorpej } else {
200 1.3.2.2 thorpej io1 = acpi_res_io(&res, 1);
201 1.3.2.2 thorpej if (io1 == NULL) {
202 1.3.2.2 thorpej printf("%s: unable to find i/o resources\n",
203 1.3.2.2 thorpej sc->sc_dv.dv_xname);
204 1.3.2.2 thorpej return;
205 1.3.2.2 thorpej }
206 1.3.2.2 thorpej if (bus_space_map(aa->aa_iot, io0->ar_base,
207 1.3.2.2 thorpej io0->ar_length, 0, &ioh_d) ||
208 1.3.2.2 thorpej bus_space_map(aa->aa_iot, io1->ar_base,
209 1.3.2.2 thorpej io1->ar_length, 0, &ioh_c))
210 1.3.2.2 thorpej panic("pckbc_acpi_attach: couldn't map");
211 1.3.2.2 thorpej
212 1.3.2.2 thorpej t = malloc(sizeof(struct pckbc_internal),
213 1.3.2.2 thorpej M_DEVBUF, M_WAITOK);
214 1.3.2.2 thorpej memset(t, 0, sizeof(*t));
215 1.3.2.2 thorpej t->t_iot = aa->aa_iot;
216 1.3.2.2 thorpej t->t_ioh_d = ioh_d;
217 1.3.2.2 thorpej t->t_ioh_c = ioh_c;
218 1.3.2.2 thorpej t->t_addr = io0->ar_base;
219 1.3.2.2 thorpej t->t_cmdbyte = KC8_CPU; /* Enable ports */
220 1.3.2.2 thorpej callout_init(&t->t_cleanup);
221 1.3.2.2 thorpej }
222 1.3.2.2 thorpej
223 1.3.2.2 thorpej t->t_sc = &first->sc_pckbc;
224 1.3.2.2 thorpej first->sc_pckbc.id = t;
225 1.3.2.2 thorpej
226 1.3.2.2 thorpej first->sc_pckbc.intr_establish = pckbc_acpi_intr_establish;
227 1.3.2.2 thorpej config_defer(&first->sc_pckbc.sc_dv,
228 1.3.2.2 thorpej (void(*)(struct device *))pckbc_attach);
229 1.3.2.2 thorpej }
230 1.3.2.2 thorpej }
231 1.3.2.2 thorpej
232 1.3.2.2 thorpej void
233 1.3.2.2 thorpej pckbc_acpi_intr_establish(struct pckbc_softc *sc,
234 1.3.2.2 thorpej pckbc_slot_t slot)
235 1.3.2.2 thorpej {
236 1.3.2.2 thorpej struct pckbc_acpi_softc *psc;
237 1.3.2.2 thorpej isa_chipset_tag_t ic = NULL;
238 1.3.2.2 thorpej void *rv = NULL;
239 1.3.2.2 thorpej int irq, ist;
240 1.3.2.2 thorpej int i;
241 1.3.2.2 thorpej
242 1.3.2.2 thorpej /*
243 1.3.2.2 thorpej * Note we're always called with sc == first.
244 1.3.2.2 thorpej */
245 1.3.2.2 thorpej for (i = 0; i < pckbc_cd.cd_ndevs; i++) {
246 1.3.2.2 thorpej psc = pckbc_cd.cd_devs[i];
247 1.3.2.2 thorpej if (psc && psc->sc_slot == slot) {
248 1.3.2.2 thorpej irq = psc->sc_irq;
249 1.3.2.2 thorpej ist = psc->sc_ist;
250 1.3.2.2 thorpej ic = psc->sc_ic;
251 1.3.2.2 thorpej break;
252 1.3.2.2 thorpej }
253 1.3.2.2 thorpej }
254 1.3.2.2 thorpej if (i < pckbc_cd.cd_ndevs)
255 1.3.2.2 thorpej rv = isa_intr_establish(ic, irq, ist, IPL_TTY, pckbcintr, sc);
256 1.3.2.2 thorpej if (rv == NULL) {
257 1.3.2.2 thorpej printf("%s: unable to establish interrupt for %s slot\n",
258 1.3.2.2 thorpej sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
259 1.3.2.2 thorpej } else {
260 1.3.2.2 thorpej printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname,
261 1.3.2.2 thorpej irq, pckbc_slot_names[slot]);
262 1.3.2.2 thorpej }
263 1.3.2.2 thorpej }
264