1 /* $NetBSD: pckbc_acpi.c,v 1.40 2024/11/10 13:29:38 martin 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.40 2024/11/10 13:29:38 martin 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 #include <dev/acpi/acpi_intr.h> 55 56 #include <dev/isa/isareg.h> 57 58 #include <dev/ic/i8042reg.h> 59 #include <dev/ic/pckbcvar.h> 60 61 #include "ioconf.h" 62 63 static int pckbc_acpi_match(device_t, cfdata_t, void *); 64 static void pckbc_acpi_attach(device_t, device_t, void *); 65 66 struct pckbc_acpi_softc { 67 struct pckbc_softc sc_pckbc; 68 69 ACPI_HANDLE sc_handle; 70 pckbc_slot_t sc_slot; 71 }; 72 73 /* Save first port: */ 74 static struct pckbc_acpi_softc *first; 75 76 CFATTACH_DECL_NEW(pckbc_acpi, sizeof(struct pckbc_acpi_softc), 77 pckbc_acpi_match, pckbc_acpi_attach, NULL, NULL); 78 79 static void pckbc_acpi_intr_establish(struct pckbc_softc *, pckbc_slot_t); 80 static void pckbc_acpi_finish_attach(device_t); 81 82 /* 83 * Supported Device IDs 84 */ 85 86 static const struct device_compatible_entry compat_data[] = { 87 /* Standard PC KBD port */ 88 { .compat = "PNP03??", .value = PCKBC_KBD_SLOT }, 89 90 /* (Nobody else here but us mouses...) */ 91 { .compat = "PNP0F03", .value = PCKBC_AUX_SLOT }, 92 { .compat = "PNP0F0E", .value = PCKBC_AUX_SLOT }, 93 { .compat = "PNP0F12", .value = PCKBC_AUX_SLOT }, 94 { .compat = "PNP0F13", .value = PCKBC_AUX_SLOT }, 95 { .compat = "PNP0F19", .value = PCKBC_AUX_SLOT }, 96 { .compat = "PNP0F1B", .value = PCKBC_AUX_SLOT }, 97 { .compat = "PNP0F1C", .value = PCKBC_AUX_SLOT }, 98 { .compat = "SYN0302", .value = PCKBC_AUX_SLOT }, 99 { .compat = "ELAN0501", .value = PCKBC_AUX_SLOT }, 100 101 DEVICE_COMPAT_EOL 102 }; 103 104 /* 105 * pckbc_acpi_match: autoconf(9) match routine 106 */ 107 static int 108 pckbc_acpi_match(device_t parent, cfdata_t match, void *aux) 109 { 110 struct acpi_attach_args *aa = aux; 111 112 return acpi_compatible_match(aa, compat_data); 113 } 114 115 static void 116 pckbc_acpi_attach(device_t parent, device_t self, void *aux) 117 { 118 struct pckbc_acpi_softc *psc = device_private(self); 119 struct pckbc_softc *sc = &psc->sc_pckbc; 120 struct pckbc_internal *t; 121 struct acpi_attach_args *aa = aux; 122 const struct device_compatible_entry *dce; 123 bus_space_handle_t ioh_d, ioh_c; 124 struct acpi_resources res; 125 struct acpi_io *io0, *io1, *ioswap; 126 struct acpi_irq *irq; 127 ACPI_STATUS rv; 128 129 sc->sc_dv = self; 130 131 dce = acpi_compatible_lookup(aa, compat_data); 132 KASSERT(dce != NULL); 133 134 psc->sc_slot = dce->value; 135 136 aprint_normal(" (%s port)", pckbc_slot_names[psc->sc_slot]); 137 138 /* parse resources */ 139 rv = acpi_resource_parse(sc->sc_dv, aa->aa_node->ad_handle, "_CRS", 140 &res, &acpi_resource_parse_ops_default); 141 if (ACPI_FAILURE(rv)) 142 return; 143 144 /* find our IRQ */ 145 irq = acpi_res_irq(&res, 0); 146 if (irq == NULL) { 147 aprint_error_dev(self, "unable to find irq resource\n"); 148 goto out; 149 } 150 psc->sc_handle = aa->aa_node->ad_handle; 151 152 if (psc->sc_slot == PCKBC_KBD_SLOT) 153 first = psc; 154 155 if ((!first || !first->sc_pckbc.id) && 156 (psc->sc_slot == PCKBC_KBD_SLOT)) { 157 158 io0 = acpi_res_io(&res, 0); 159 io1 = acpi_res_io(&res, 1); 160 if (io0 == NULL || io1 == NULL) { 161 aprint_error_dev(self, 162 "unable to find i/o resources\n"); 163 goto out; 164 } 165 166 /* 167 * JDM: Some firmware doesn't report resources in the order we 168 * expect; sort IO resources here (lowest first) 169 */ 170 if (io0->ar_base > io1->ar_base) { 171 ioswap = io0; 172 io0 = io1; 173 io1 = ioswap; 174 } 175 176 if (pckbc_is_console(aa->aa_iot, io0->ar_base)) { 177 t = &pckbc_consdata; 178 ioh_d = t->t_ioh_d; 179 ioh_c = t->t_ioh_c; 180 pckbc_console_attached = 1; 181 /* t->t_cmdbyte was initialized by cnattach */ 182 } else { 183 if (bus_space_map(aa->aa_iot, io0->ar_base, 184 io0->ar_length, 0, &ioh_d) || 185 bus_space_map(aa->aa_iot, io1->ar_base, 186 io1->ar_length, 0, &ioh_c)) { 187 aprint_error_dev(self, 188 "unable to map registers\n"); 189 goto out; 190 } 191 192 t = malloc(sizeof(struct pckbc_internal), 193 M_DEVBUF, M_WAITOK|M_ZERO); 194 t->t_iot = aa->aa_iot; 195 t->t_ioh_d = ioh_d; 196 t->t_ioh_c = ioh_c; 197 t->t_addr = io0->ar_base; 198 t->t_cmdbyte = KC8_CPU; /* Enable ports */ 199 callout_init(&t->t_cleanup, 0); 200 } 201 202 t->t_sc = &first->sc_pckbc; 203 first->sc_pckbc.id = t; 204 205 if (!pmf_device_register(self, NULL, pckbc_resume)) 206 aprint_error_dev(self, 207 "couldn't establish power handler\n"); 208 209 first->sc_pckbc.intr_establish = pckbc_acpi_intr_establish; 210 config_defer(first->sc_pckbc.sc_dv, pckbc_acpi_finish_attach); 211 } 212 213 out: 214 if (!pmf_device_register(self, NULL, NULL)) 215 aprint_error_dev(self, "couldn't establish power handler\n"); 216 acpi_resource_cleanup(&res); 217 } 218 219 static void 220 pckbc_acpi_intr_establish(struct pckbc_softc *sc, pckbc_slot_t slot) 221 { 222 struct pckbc_acpi_softc *psc = NULL; /* XXX: gcc */ 223 void *rv = NULL; 224 ACPI_HANDLE handle; 225 char intr_name[64]; 226 int i; 227 228 /* 229 * Note we're always called with sc == first. 230 */ 231 for (i = 0; i < pckbc_cd.cd_ndevs; i++) { 232 psc = device_lookup_private(&pckbc_cd, i); 233 if (psc && psc->sc_slot == slot) { 234 handle = psc->sc_handle; 235 break; 236 } 237 } 238 if (i < pckbc_cd.cd_ndevs) { 239 snprintf(intr_name, sizeof(intr_name), "%s %s", 240 device_xname(psc->sc_pckbc.sc_dv), pckbc_slot_names[slot]); 241 242 rv = acpi_intr_establish(sc->sc_dv, (uint64_t)(uintptr_t)handle, 243 IPL_TTY, false, pckbcintr, sc, intr_name); 244 } 245 if (rv == NULL) { 246 aprint_error_dev(sc->sc_dv, 247 "unable to establish interrupt for %s slot\n", 248 pckbc_slot_names[slot]); 249 } else { 250 aprint_normal_dev(sc->sc_dv, "using %s for %s slot\n", 251 acpi_intr_string(rv, intr_name, sizeof(intr_name)), 252 pckbc_slot_names[slot]); 253 } 254 } 255 256 static void 257 pckbc_acpi_finish_attach(device_t dv) 258 { 259 260 pckbc_attach(device_private(dv)); 261 } 262