pckbc_acpi.c revision 1.3
1/* $NetBSD: pckbc_acpi.c,v 1.3 2002/12/28 10:14:43 matt 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 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.3 2002/12/28 10:14:43 matt 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 "IBM3780", /* IBM pointing device */ 105 NULL 106}; 107 108/* 109 * pckbc_acpi_match: autoconf(9) match routine 110 */ 111int 112pckbc_acpi_match(struct device *parent, struct cfdata *match, void *aux) 113{ 114 struct acpi_attach_args *aa = aux; 115 const char *id; 116 int i; 117 118 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 119 return (0); 120 121 for (i = 0; (id = pckbc_acpi_ids[i]) != NULL; ++i) { 122 if (strcmp(aa->aa_node->ad_devinfo.HardwareId, id) == 0) 123 return (1); 124 } 125 126 /* No matches found */ 127 return (0); 128} 129 130void 131pckbc_acpi_attach(struct device *parent, 132 struct device *self, 133 void *aux) 134{ 135 struct pckbc_acpi_softc *psc = (void *) self; 136 struct pckbc_softc *sc = &psc->sc_pckbc; 137 struct pckbc_internal *t; 138 struct acpi_attach_args *aa = aux; 139 bus_space_handle_t ioh_d, ioh_c; 140 const char *idstr = aa->aa_node->ad_devinfo.HardwareId; 141 pckbc_slot_t peer; 142 struct acpi_resources res; 143 struct acpi_io *io0, *io1; 144 struct acpi_irq *irq; 145 ACPI_STATUS rv; 146 147 psc->sc_ic = aa->aa_ic; 148 149 if (strncmp(idstr, "PNP03", 5) == 0) { 150 psc->sc_slot = PCKBC_KBD_SLOT; 151 peer = PCKBC_AUX_SLOT; 152 } else if (strncmp(idstr, "PNP0F", 5) == 0 || 153 strcmp(idstr, "IBM3780") == 0) { 154 psc->sc_slot = PCKBC_AUX_SLOT; 155 peer = PCKBC_KBD_SLOT; 156 } else { 157 printf(": unknown port!\n"); 158 panic("pckbc_acpi_attach: impossible"); 159 } 160 161 printf(": %s port\n", pckbc_slot_names[psc->sc_slot]); 162 163 /* parse resources */ 164 rv = acpi_resource_parse(&sc->sc_dv, aa->aa_node, &res, 165 &acpi_resource_parse_ops_default); 166 if (rv != AE_OK) { 167 printf("%s: unable to parse resources\n", sc->sc_dv.dv_xname); 168 return; 169 } 170 171 /* find our IRQ */ 172 irq = acpi_res_irq(&res, 0); 173 if (irq == NULL) { 174 printf("%s: unable to find irq resource\n", sc->sc_dv.dv_xname); 175 return; 176 } 177 psc->sc_irq = irq->ar_irq; 178 psc->sc_ist = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL; 179 180 if (psc->sc_slot == PCKBC_KBD_SLOT) 181 first = psc; 182 183 if ((!first || !first->sc_pckbc.id) && 184 (psc->sc_slot == PCKBC_KBD_SLOT)) { 185 186 io0 = acpi_res_io(&res, 0); 187 if (io0 == NULL) { 188 printf("%s: unable to find i/o resources\n", 189 sc->sc_dv.dv_xname); 190 return; 191 } 192 193 if (pckbc_is_console(aa->aa_iot, io0->ar_base)) { 194 t = &pckbc_consdata; 195 ioh_d = t->t_ioh_d; 196 ioh_c = t->t_ioh_c; 197 pckbc_console_attached = 1; 198 /* t->t_cmdbyte was initialized by cnattach */ 199 } else { 200 io1 = acpi_res_io(&res, 1); 201 if (io1 == NULL) { 202 printf("%s: unable to find i/o resources\n", 203 sc->sc_dv.dv_xname); 204 return; 205 } 206 if (bus_space_map(aa->aa_iot, io0->ar_base, 207 io0->ar_length, 0, &ioh_d) || 208 bus_space_map(aa->aa_iot, io1->ar_base, 209 io1->ar_length, 0, &ioh_c)) 210 panic("pckbc_acpi_attach: couldn't map"); 211 212 t = malloc(sizeof(struct pckbc_internal), 213 M_DEVBUF, M_WAITOK); 214 memset(t, 0, sizeof(*t)); 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); 221 } 222 223 t->t_sc = &first->sc_pckbc; 224 first->sc_pckbc.id = t; 225 226 first->sc_pckbc.intr_establish = pckbc_acpi_intr_establish; 227 config_defer(&first->sc_pckbc.sc_dv, 228 (void(*)(struct device *))pckbc_attach); 229 } 230} 231 232void 233pckbc_acpi_intr_establish(struct pckbc_softc *sc, 234 pckbc_slot_t slot) 235{ 236 struct pckbc_acpi_softc *psc; 237 isa_chipset_tag_t ic = NULL; 238 void *rv = NULL; 239 int irq, ist; 240 int i; 241 242 /* 243 * Note we're always called with sc == first. 244 */ 245 for (i = 0; i < pckbc_cd.cd_ndevs; i++) { 246 psc = pckbc_cd.cd_devs[i]; 247 if (psc && psc->sc_slot == slot) { 248 irq = psc->sc_irq; 249 ist = psc->sc_ist; 250 ic = psc->sc_ic; 251 break; 252 } 253 } 254 if (i < pckbc_cd.cd_ndevs) 255 rv = isa_intr_establish(ic, irq, ist, IPL_TTY, pckbcintr, sc); 256 if (rv == NULL) { 257 printf("%s: unable to establish interrupt for %s slot\n", 258 sc->sc_dv.dv_xname, pckbc_slot_names[slot]); 259 } else { 260 printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname, 261 irq, pckbc_slot_names[slot]); 262 } 263} 264