xref: /netbsd-src/sys/dev/acpi/pckbc_acpi.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: pckbc_acpi.c,v 1.30 2008/09/16 11:24:55 pgoyette 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * ACPI attachment for the PC Keyboard Controller driver.
34  *
35  * This is a little wonky.  The keyboard controller actually
36  * has 2 ACPI nodes: one for the controller and the keyboard
37  * interrupt, and one for the aux port (mouse) interrupt.
38  *
39  * For this reason, we actually attach *two* instances of this
40  * driver.  After both of them have been found, then we attach
41  * sub-devices.
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: pckbc_acpi.c,v 1.30 2008/09/16 11:24:55 pgoyette Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/device.h>
52 #include <sys/malloc.h>
53 #include <sys/errno.h>
54 #include <sys/queue.h>
55 #include <sys/bus.h>
56 
57 #include <dev/isa/isareg.h>
58 #include <dev/isa/isavar.h>
59 
60 #include <dev/ic/i8042reg.h>
61 #include <dev/ic/pckbcvar.h>
62 
63 #include <dev/acpi/acpivar.h>
64 
65 static int	pckbc_acpi_match(device_t, cfdata_t, void *);
66 static void	pckbc_acpi_attach(device_t, device_t, void *);
67 
68 struct pckbc_acpi_softc {
69 	struct pckbc_softc sc_pckbc;
70 
71 	isa_chipset_tag_t sc_ic;
72 	int sc_irq;
73 	int sc_ist;
74 	pckbc_slot_t sc_slot;
75 };
76 
77 /* Save first port: */
78 static struct pckbc_acpi_softc *first;
79 
80 extern struct cfdriver pckbc_cd;
81 
82 CFATTACH_DECL_NEW(pckbc_acpi, sizeof(struct pckbc_acpi_softc),
83     pckbc_acpi_match, pckbc_acpi_attach, NULL, NULL);
84 
85 static void	pckbc_acpi_intr_establish(struct pckbc_softc *, pckbc_slot_t);
86 static void	pckbc_acpi_finish_attach(device_t);
87 
88 /*
89  * Supported Device IDs
90  */
91 
92 static const char * const pckbc_acpi_ids_kbd[] = {
93 	"PNP03??",	/* Standard PC KBD port */
94 	NULL
95 };
96 
97 static const char * const pckbc_acpi_ids_ms[] = {
98 	"PNP0F03",
99 	"PNP0F0E",
100 	"PNP0F12",
101 	"PNP0F13",
102 	"PNP0F19",
103 	"PNP0F1B",
104 	"PNP0F1C",
105 	"SYN0302",
106 	NULL
107 };
108 
109 /*
110  * pckbc_acpi_match: autoconf(9) match routine
111  */
112 static int
113 pckbc_acpi_match(device_t parent, cfdata_t match, void *aux)
114 {
115 	struct acpi_attach_args *aa = aux;
116 	int rv;
117 
118 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
119 		return 0;
120 
121 	rv = acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_kbd);
122 	if (rv)
123 		return rv;
124 	rv = acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_ms);
125 	if (rv)
126 		return rv;
127 	return 0;
128 }
129 
130 static void
131 pckbc_acpi_attach(device_t parent, device_t self, void *aux)
132 {
133 	struct pckbc_acpi_softc *psc = device_private(self);
134 	struct pckbc_softc *sc = &psc->sc_pckbc;
135 	struct pckbc_internal *t;
136 	struct acpi_attach_args *aa = aux;
137 	bus_space_handle_t ioh_d, ioh_c;
138 	pckbc_slot_t peer;
139 	struct acpi_resources res;
140 	struct acpi_io *io0, *io1, *ioswap;
141 	struct acpi_irq *irq;
142 	ACPI_STATUS rv;
143 
144 	sc->sc_dv = self;
145 	psc->sc_ic = aa->aa_ic;
146 
147 	if (acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_kbd)) {
148 		psc->sc_slot = PCKBC_KBD_SLOT;
149 		peer = PCKBC_AUX_SLOT;
150 	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_ms)) {
151 		psc->sc_slot = PCKBC_AUX_SLOT;
152 		peer = PCKBC_KBD_SLOT;
153 	} else {
154 		aprint_error(": unknown port!\n");
155 		panic("pckbc_acpi_attach: impossible");
156 	}
157 
158 	aprint_naive("\n");
159 	aprint_normal(": %s port\n", pckbc_slot_names[psc->sc_slot]);
160 
161 	/* parse resources */
162 	rv = acpi_resource_parse(sc->sc_dv, aa->aa_node->ad_handle, "_CRS",
163 	    &res, &acpi_resource_parse_ops_default);
164 	if (ACPI_FAILURE(rv))
165 		return;
166 
167 	/* find our IRQ */
168 	irq = acpi_res_irq(&res, 0);
169 	if (irq == NULL) {
170 		aprint_error_dev(self, "unable to find irq resource\n");
171 		goto out;
172 	}
173 	psc->sc_irq = irq->ar_irq;
174 	psc->sc_ist = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
175 
176 	if (psc->sc_slot == PCKBC_KBD_SLOT)
177 		first = psc;
178 
179 	if ((!first || !first->sc_pckbc.id) &&
180 	    (psc->sc_slot == PCKBC_KBD_SLOT)) {
181 
182 		io0 = acpi_res_io(&res, 0);
183 		io1 = acpi_res_io(&res, 1);
184 		if (io0 == NULL || io1 == NULL) {
185 			aprint_error_dev(self,
186 			    "unable to find i/o resources\n");
187 			goto out;
188 		}
189 
190 		/*
191 		 * JDM: Some firmware doesn't report resources in the order we
192 		 * expect; sort IO resources here (lowest first)
193 		 */
194 		if (io0->ar_base > io1->ar_base) {
195 			ioswap = io0;
196 			io0 = io1;
197 			io1 = ioswap;
198 		}
199 
200 		if (pckbc_is_console(aa->aa_iot, io0->ar_base)) {
201 			t = &pckbc_consdata;
202 			ioh_d = t->t_ioh_d;
203 			ioh_c = t->t_ioh_c;
204 			pckbc_console_attached = 1;
205 			/* t->t_cmdbyte was initialized by cnattach */
206 		} else {
207 			if (bus_space_map(aa->aa_iot, io0->ar_base,
208 					  io0->ar_length, 0, &ioh_d) ||
209 			    bus_space_map(aa->aa_iot, io1->ar_base,
210 					  io1->ar_length, 0, &ioh_c))
211 				panic("pckbc_acpi_attach: couldn't map");
212 
213 			t = malloc(sizeof(struct pckbc_internal),
214 			    M_DEVBUF, M_WAITOK|M_ZERO);
215 			t->t_iot = aa->aa_iot;
216 			t->t_ioh_d = ioh_d;
217 			t->t_ioh_c = ioh_c;
218 			t->t_addr = io0->ar_base;
219 			t->t_cmdbyte = KC8_CPU;	/* Enable ports */
220 			callout_init(&t->t_cleanup, 0);
221 		}
222 
223 		t->t_sc = &first->sc_pckbc;
224 		first->sc_pckbc.id = t;
225 
226 		if (!pmf_device_register(self, NULL, pckbc_resume))
227 			aprint_error_dev(self,
228 			    "couldn't establish power handler\n");
229 
230 		first->sc_pckbc.intr_establish = pckbc_acpi_intr_establish;
231 		config_defer(first->sc_pckbc.sc_dv, pckbc_acpi_finish_attach);
232 	} else if (!pmf_device_register(self, NULL, NULL))
233 		aprint_error_dev(self, "couldn't establish power handler\n");
234  out:
235 	acpi_resource_cleanup(&res);
236 }
237 
238 static void
239 pckbc_acpi_intr_establish(struct pckbc_softc *sc, pckbc_slot_t slot)
240 {
241 	struct pckbc_acpi_softc *psc;
242 	isa_chipset_tag_t ic = NULL;
243 	void *rv = NULL;
244 	int irq = 0, ist = 0; /* XXX: gcc */
245 	int i;
246 
247 	/*
248 	 * Note we're always called with sc == first.
249 	 */
250 	for (i = 0; i < pckbc_cd.cd_ndevs; i++) {
251 		psc = device_lookup_private(&pckbc_cd, i);
252 		if (psc && psc->sc_slot == slot) {
253 			irq = psc->sc_irq;
254 			ist = psc->sc_ist;
255 			ic = psc->sc_ic;
256 			break;
257 		}
258 	}
259 	if (i < pckbc_cd.cd_ndevs)
260 		rv = isa_intr_establish(ic, irq, ist, IPL_TTY, pckbcintr, sc);
261 	if (rv == NULL) {
262 		aprint_error_dev(sc->sc_dv,
263 		    "unable to establish interrupt for %s slot\n",
264 		    pckbc_slot_names[slot]);
265 	} else {
266 		aprint_normal_dev(sc->sc_dv, "using irq %d for %s slot\n",
267 		    irq, pckbc_slot_names[slot]);
268 	}
269 }
270 
271 static void
272 pckbc_acpi_finish_attach(device_t dv)
273 {
274 
275 	pckbc_attach(device_private(dv));
276 }
277