pckbc_acpi.c revision 1.7
1143880Spjd/*	$NetBSD: pckbc_acpi.c,v 1.7 2003/11/03 06:03:47 kochi Exp $	*/
2143880Spjd
3143880Spjd/*-
4143880Spjd * Copyright (c) 2000 The NetBSD Foundation, Inc.
5143880Spjd * All rights reserved.
6143880Spjd *
7143880Spjd * This code is derived from software contributed to The NetBSD Foundation
8143880Spjd * by Jason R. Thorpe.
9143880Spjd *
10263351Sjmmv * Redistribution and use in source and binary forms, with or without
11143880Spjd * modification, are permitted provided that the following conditions
12143880Spjd * are met:
13143880Spjd * 1. Redistributions of source code must retain the above copyright
14143880Spjd *    notice, this list of conditions and the following disclaimer.
15143880Spjd * 2. Redistributions in binary form must reproduce the above copyright
16143880Spjd *    notice, this list of conditions and the following disclaimer in the
17143880Spjd *    documentation and/or other materials provided with the distribution.
18143880Spjd * 3. All advertising materials mentioning features or use of this software
19143880Spjd *    must display the following acknowledgement:
20143880Spjd *	This product includes software developed by the NetBSD
21143880Spjd *	Foundation, Inc. and its contributors.
22143880Spjd * 4. Neither the name of The NetBSD Foundation nor the names of its
23143880Spjd *    contributors may be used to endorse or promote products derived
24143880Spjd *    from this software without specific prior written permission.
25143880Spjd *
26263351Sjmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27143880Spjd * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28143880Spjd * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29143880Spjd * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30143880Spjd * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31143880Spjd * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32143880Spjd * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33143880Spjd * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34143880Spjd * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35143880Spjd * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36143880Spjd * POSSIBILITY OF SUCH DAMAGE.
37143880Spjd */
38143880Spjd
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.7 2003/11/03 06:03:47 kochi 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
74int	pckbc_acpi_match(struct device *, struct cfdata *, void *);
75void	pckbc_acpi_attach(struct device *, struct device *, void *);
76
77struct 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: */
87static struct pckbc_acpi_softc *first;
88
89extern struct cfdriver pckbc_cd;
90
91CFATTACH_DECL(pckbc_acpi, sizeof(struct pckbc_acpi_softc),
92    pckbc_acpi_match, pckbc_acpi_attach, NULL, NULL);
93
94void	pckbc_acpi_intr_establish(struct pckbc_softc *, pckbc_slot_t);
95
96/*
97 * Supported Device IDs
98 */
99
100static const char * const pckbc_acpi_ids[] = {
101	"PNP0303",	/* Standard PC KBD/MS port */
102	"PNP0320",	/* Japanese 106 */
103	"PNP0F13",
104	"PNP0F03",
105	"IBM3780",	/* IBM pointing device */
106	NULL
107};
108
109/*
110 * pckbc_acpi_match: autoconf(9) match routine
111 */
112int
113pckbc_acpi_match(struct device *parent, struct cfdata *match, void *aux)
114{
115	struct acpi_attach_args *aa = aux;
116
117	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
118		return (0);
119
120	return (acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids));
121}
122
123void
124pckbc_acpi_attach(struct device *parent,
125    struct device *self,
126    void *aux)
127{
128	struct pckbc_acpi_softc *psc = (void *) self;
129	struct pckbc_softc *sc = &psc->sc_pckbc;
130	struct pckbc_internal *t;
131	struct acpi_attach_args *aa = aux;
132	bus_space_handle_t ioh_d, ioh_c;
133	const char *idstr = aa->aa_node->ad_devinfo->HardwareId.Value;
134	pckbc_slot_t peer;
135	struct acpi_resources res;
136	struct acpi_io *io0, *io1;
137	struct acpi_irq *irq;
138	ACPI_STATUS rv;
139
140	psc->sc_ic = aa->aa_ic;
141
142	if (strncmp(idstr, "PNP03", 5) == 0) {
143		psc->sc_slot = PCKBC_KBD_SLOT;
144		peer = PCKBC_AUX_SLOT;
145	} else if (strncmp(idstr, "PNP0F", 5) == 0 ||
146			strcmp(idstr, "IBM3780") == 0) {
147		psc->sc_slot = PCKBC_AUX_SLOT;
148		peer = PCKBC_KBD_SLOT;
149	} else {
150		printf(": unknown port!\n");
151		panic("pckbc_acpi_attach: impossible");
152	}
153
154	printf(": %s port\n", pckbc_slot_names[psc->sc_slot]);
155
156	/* parse resources */
157	rv = acpi_resource_parse(&sc->sc_dv, aa->aa_node, &res,
158	    &acpi_resource_parse_ops_default);
159	if (rv != AE_OK) {
160		printf("%s: unable to parse resources\n", sc->sc_dv.dv_xname);
161		return;
162	}
163
164	/* find our IRQ */
165	irq = acpi_res_irq(&res, 0);
166	if (irq == NULL) {
167		printf("%s: unable to find irq resource\n", sc->sc_dv.dv_xname);
168		return;
169	}
170	psc->sc_irq = irq->ar_irq;
171	psc->sc_ist = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
172
173	if (psc->sc_slot == PCKBC_KBD_SLOT)
174		first = psc;
175
176	if ((!first || !first->sc_pckbc.id) &&
177	    (psc->sc_slot == PCKBC_KBD_SLOT)) {
178
179		io0 = acpi_res_io(&res, 0);
180		if (io0 == NULL) {
181			printf("%s: unable to find i/o resources\n",
182			    sc->sc_dv.dv_xname);
183			return;
184		}
185
186		if (pckbc_is_console(aa->aa_iot, io0->ar_base)) {
187			t = &pckbc_consdata;
188			ioh_d = t->t_ioh_d;
189			ioh_c = t->t_ioh_c;
190			pckbc_console_attached = 1;
191			/* t->t_cmdbyte was initialized by cnattach */
192		} else {
193			io1 = acpi_res_io(&res, 1);
194			if (io1 == NULL) {
195				printf("%s: unable to find i/o resources\n",
196				    sc->sc_dv.dv_xname);
197				return;
198			}
199			if (bus_space_map(aa->aa_iot, io0->ar_base,
200					  io0->ar_length, 0, &ioh_d) ||
201			    bus_space_map(aa->aa_iot, io1->ar_base,
202					  io1->ar_length, 0, &ioh_c))
203				panic("pckbc_acpi_attach: couldn't map");
204
205			t = malloc(sizeof(struct pckbc_internal),
206			    M_DEVBUF, M_WAITOK);
207			memset(t, 0, sizeof(*t));
208			t->t_iot = aa->aa_iot;
209			t->t_ioh_d = ioh_d;
210			t->t_ioh_c = ioh_c;
211			t->t_addr = io0->ar_base;
212			t->t_cmdbyte = KC8_CPU;	/* Enable ports */
213			callout_init(&t->t_cleanup);
214		}
215
216		t->t_sc = &first->sc_pckbc;
217		first->sc_pckbc.id = t;
218
219		first->sc_pckbc.intr_establish = pckbc_acpi_intr_establish;
220		config_defer(&first->sc_pckbc.sc_dv,
221			     (void(*)(struct device *))pckbc_attach);
222	}
223}
224
225void
226pckbc_acpi_intr_establish(struct pckbc_softc *sc,
227    pckbc_slot_t slot)
228{
229	struct pckbc_acpi_softc *psc;
230	isa_chipset_tag_t ic = NULL;
231	void *rv = NULL;
232	int irq = 0, ist = 0; /* XXX: gcc */
233	int i;
234
235	/*
236	 * Note we're always called with sc == first.
237	 */
238	for (i = 0; i < pckbc_cd.cd_ndevs; i++) {
239		psc = pckbc_cd.cd_devs[i];
240		if (psc && psc->sc_slot == slot) {
241			irq = psc->sc_irq;
242			ist = psc->sc_ist;
243			ic = psc->sc_ic;
244			break;
245		}
246	}
247	if (i < pckbc_cd.cd_ndevs)
248		rv = isa_intr_establish(ic, irq, ist, IPL_TTY, pckbcintr, sc);
249	if (rv == NULL) {
250		printf("%s: unable to establish interrupt for %s slot\n",
251		    sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
252	} else {
253		printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname,
254		    irq, pckbc_slot_names[slot]);
255	}
256}
257