1/* $NetBSD: pckbc_acpi.c,v 1.35 2016/10/18 22:08:30 jdolecek 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.35 2016/10/18 22:08:30 jdolecek Exp $");
46
47#include <sys/param.h>
48#include <sys/callout.h>
49#include <sys/device.h>
50#include <sys/malloc.h>
51#include <sys/systm.h>
52
53#include <dev/acpi/acpivar.h>
54
55#include <dev/isa/isareg.h>
56
57#include <dev/ic/i8042reg.h>
58#include <dev/ic/pckbcvar.h>
59
60static int pckbc_acpi_match(device_t, cfdata_t, void *);
61static void pckbc_acpi_attach(device_t, device_t, void *);
62
63struct pckbc_acpi_softc {
64 struct pckbc_softc sc_pckbc;
65
66 isa_chipset_tag_t sc_ic;
67 int sc_irq;
68 int sc_ist;
69 pckbc_slot_t sc_slot;
70};
71
72/* Save first port: */
73static struct pckbc_acpi_softc *first;
74
75extern struct cfdriver pckbc_cd;
76
77CFATTACH_DECL_NEW(pckbc_acpi, sizeof(struct pckbc_acpi_softc),
78 pckbc_acpi_match, pckbc_acpi_attach, NULL, NULL);
79
80static void pckbc_acpi_intr_establish(struct pckbc_softc *, pckbc_slot_t);
81static void pckbc_acpi_finish_attach(device_t);
82
83/*
84 * Supported Device IDs
85 */
86
87static const char * const pckbc_acpi_ids_kbd[] = {
88 "PNP03??", /* Standard PC KBD port */
89 NULL
90};
91
92static const char * const pckbc_acpi_ids_ms[] = {
93 "PNP0F03",
94 "PNP0F0E",
95 "PNP0F12",
96 "PNP0F13",
97 "PNP0F19",
98 "PNP0F1B",
99 "PNP0F1C",
100 "SYN0302",
101 NULL
102};
103
104/*
105 * pckbc_acpi_match: autoconf(9) match routine
106 */
107static int
108pckbc_acpi_match(device_t parent, cfdata_t match, void *aux)
109{
110 struct acpi_attach_args *aa = aux;
111 int rv;
112
113 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
114 return 0;
115
116 rv = acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_kbd);
117 if (rv)
118 return rv;
119 rv = acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_ms);
120 if (rv)
121 return rv;
122 return 0;
123}
124
125static void
126pckbc_acpi_attach(device_t parent, device_t self, void *aux)
127{
128 struct pckbc_acpi_softc *psc = device_private(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 struct acpi_resources res;
134 struct acpi_io *io0, *io1, *ioswap;
135 struct acpi_irq *irq;
136 ACPI_STATUS rv;
137
138 sc->sc_dv = self;
139 psc->sc_ic = aa->aa_ic;
140
141 if (acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_kbd)) {
142 psc->sc_slot = PCKBC_KBD_SLOT;
143 } else if (acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_ms)) {
144 psc->sc_slot = PCKBC_AUX_SLOT;
145 } else {
146 aprint_error(": unknown port!\n");
147 panic("pckbc_acpi_attach: impossible");
148 }
149
150 aprint_normal(" (%s port)", pckbc_slot_names[psc->sc_slot]);
151
152 /* parse resources */
153 rv = acpi_resource_parse(sc->sc_dv, aa->aa_node->ad_handle, "_CRS",
154 &res, &acpi_resource_parse_ops_default);
155 if (ACPI_FAILURE(rv))
156 return;
157
158 /* find our IRQ */
159 irq = acpi_res_irq(&res, 0);
160 if (irq == NULL) {
161 aprint_error_dev(self, "unable to find irq resource\n");
162 goto out;
163 }
164 psc->sc_irq = irq->ar_irq;
165 psc->sc_ist = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
166
167 if (psc->sc_slot == PCKBC_KBD_SLOT)
168 first = psc;
169
170 if ((!first || !first->sc_pckbc.id) &&
171 (psc->sc_slot == PCKBC_KBD_SLOT)) {
172
173 io0 = acpi_res_io(&res, 0);
174 io1 = acpi_res_io(&res, 1);
175 if (io0 == NULL || io1 == NULL) {
176 aprint_error_dev(self,
177 "unable to find i/o resources\n");
178 goto out;
179 }
180
181 /*
182 * JDM: Some firmware doesn't report resources in the order we
183 * expect; sort IO resources here (lowest first)
184 */
185 if (io0->ar_base > io1->ar_base) {
186 ioswap = io0;
187 io0 = io1;
188 io1 = ioswap;
189 }
190
191 if (pckbc_is_console(aa->aa_iot, io0->ar_base)) {
192 t = &pckbc_consdata;
193 ioh_d = t->t_ioh_d;
194 ioh_c = t->t_ioh_c;
195 pckbc_console_attached = 1;
196 /* t->t_cmdbyte was initialized by cnattach */
197 } else {
198 if (bus_space_map(aa->aa_iot, io0->ar_base,
199 io0->ar_length, 0, &ioh_d) ||
200 bus_space_map(aa->aa_iot, io1->ar_base,
201 io1->ar_length, 0, &ioh_c)) {
202 aprint_error_dev(self,
203 "unable to map registers\n");
204 goto out;
205 }
206
207 t = malloc(sizeof(struct pckbc_internal),
208 M_DEVBUF, M_WAITOK|M_ZERO);
209 t->t_iot = aa->aa_iot;
210 t->t_ioh_d = ioh_d;
211 t->t_ioh_c = ioh_c;
212 t->t_addr = io0->ar_base;
213 t->t_cmdbyte = KC8_CPU; /* Enable ports */
214 callout_init(&t->t_cleanup, 0);
215 }
216
217 t->t_sc = &first->sc_pckbc;
218 first->sc_pckbc.id = t;
219
220 if (!pmf_device_register(self, NULL, pckbc_resume))
221 aprint_error_dev(self,
222 "couldn't establish power handler\n");
223
224 first->sc_pckbc.intr_establish = pckbc_acpi_intr_establish;
225 config_defer(first->sc_pckbc.sc_dv, pckbc_acpi_finish_attach);
226 }
227
228out:
229 if (!pmf_device_register(self, NULL, NULL))
230 aprint_error_dev(self, "couldn't establish power handler\n");
231 acpi_resource_cleanup(&res);
232}
233
234static void
235pckbc_acpi_intr_establish(struct pckbc_softc *sc, pckbc_slot_t slot)
236{
237 struct pckbc_acpi_softc *psc;
238 isa_chipset_tag_t ic = NULL;
239 void *rv = NULL;
240 int irq = 0, ist = 0; /* XXX: gcc */
241 int i;
242
243 /*
244 * Note we're always called with sc == first.
245 */
246 for (i = 0; i < pckbc_cd.cd_ndevs; i++) {
247 psc = device_lookup_private(&pckbc_cd, i);
248 if (psc && psc->sc_slot == slot) {
249 irq = psc->sc_irq;
250 ist = psc->sc_ist;
251 ic = psc->sc_ic;
252 break;
253 }
254 }
255 if (i < pckbc_cd.cd_ndevs) {
256 char intr_xname[64];
257 snprintf(intr_xname, sizeof(intr_xname), "%s %s",
258 device_xname(psc->sc_pckbc.sc_dv), pckbc_slot_names[slot]);
259
260 rv = isa_intr_establish_xname(ic, irq, ist, IPL_TTY, pckbcintr,
261 sc, intr_xname);
262 }
263 if (rv == NULL) {
264 aprint_error_dev(sc->sc_dv,
265 "unable to establish interrupt for %s slot\n",
266 pckbc_slot_names[slot]);
267 } else {
268 aprint_normal_dev(sc->sc_dv, "using irq %d for %s slot\n",
269 irq, pckbc_slot_names[slot]);
270 }
271}
272
273static void
274pckbc_acpi_finish_attach(device_t dv)
275{
276
277 pckbc_attach(device_private(dv));
278}
279