Home | History | Annotate | Line # | Download | only in acpi
pckbc_acpi.c revision 1.20.6.2
      1 /*	$NetBSD: pckbc_acpi.c,v 1.20.6.2 2007/08/05 19:01:02 jmcneill 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.20.6.2 2007/08/05 19:01:02 jmcneill 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 
     74 static int	pckbc_acpi_match(struct device *, struct cfdata *, void *);
     75 static void	pckbc_acpi_attach(struct device *, struct device *, void *);
     76 static pnp_status_t pckbc_acpi_power(device_t, pnp_request_t, void *);
     77 
     78 struct pckbc_acpi_softc {
     79 	struct pckbc_softc sc_pckbc;
     80 
     81 	isa_chipset_tag_t sc_ic;
     82 	int sc_irq;
     83 	int sc_ist;
     84 	pckbc_slot_t sc_slot;
     85 };
     86 
     87 /* Save first port: */
     88 static struct pckbc_acpi_softc *first;
     89 
     90 extern struct cfdriver pckbc_cd;
     91 
     92 CFATTACH_DECL(pckbc_acpi, sizeof(struct pckbc_acpi_softc),
     93     pckbc_acpi_match, pckbc_acpi_attach, NULL, NULL);
     94 
     95 static void	pckbc_acpi_intr_establish(struct pckbc_softc *, pckbc_slot_t);
     96 
     97 /*
     98  * Supported Device IDs
     99  */
    100 
    101 static const char * const pckbc_acpi_ids_kbd[] = {
    102 	"PNP03??",	/* Standard PC KBD port */
    103 	NULL
    104 };
    105 
    106 static const char * const pckbc_acpi_ids_ms[] = {
    107 	"PNP0F03",
    108 	"PNP0F0E",
    109 	"PNP0F12",
    110 	"PNP0F13",
    111 	"PNP0F19",
    112 	"PNP0F1B",
    113 	"PNP0F1C",
    114 	NULL
    115 };
    116 
    117 /*
    118  * pckbc_acpi_match: autoconf(9) match routine
    119  */
    120 static int
    121 pckbc_acpi_match(struct device *parent, struct cfdata *match,
    122     void *aux)
    123 {
    124 	struct acpi_attach_args *aa = aux;
    125 	int rv;
    126 
    127 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    128 		return 0;
    129 
    130 	rv = acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_kbd);
    131 	if (rv)
    132 		return rv;
    133 	rv = acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_ms);
    134 	if (rv)
    135 		return rv;
    136 	return 0;
    137 }
    138 
    139 static void
    140 pckbc_acpi_attach(struct device *parent, struct device *self,
    141     void *aux)
    142 {
    143 	struct pckbc_acpi_softc *psc = (void *) self;
    144 	struct pckbc_softc *sc = &psc->sc_pckbc;
    145 	struct pckbc_internal *t;
    146 	struct acpi_attach_args *aa = aux;
    147 	bus_space_handle_t ioh_d, ioh_c;
    148 	pckbc_slot_t peer;
    149 	pnp_status_t status;
    150 	struct acpi_resources res;
    151 	struct acpi_io *io0, *io1;
    152 	struct acpi_irq *irq;
    153 	ACPI_STATUS rv;
    154 
    155 	psc->sc_ic = aa->aa_ic;
    156 
    157 	if (acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_kbd)) {
    158 		psc->sc_slot = PCKBC_KBD_SLOT;
    159 		peer = PCKBC_AUX_SLOT;
    160 	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, pckbc_acpi_ids_ms)) {
    161 		psc->sc_slot = PCKBC_AUX_SLOT;
    162 		peer = PCKBC_KBD_SLOT;
    163 	} else {
    164 		printf(": unknown port!\n");
    165 		panic("pckbc_acpi_attach: impossible");
    166 	}
    167 
    168 	aprint_naive("\n");
    169 	aprint_normal(": %s port\n", pckbc_slot_names[psc->sc_slot]);
    170 
    171 	/* parse resources */
    172 	rv = acpi_resource_parse(&sc->sc_dv, aa->aa_node->ad_handle, "_CRS",
    173 	    &res, &acpi_resource_parse_ops_default);
    174 	if (ACPI_FAILURE(rv))
    175 		return;
    176 
    177 	/* find our IRQ */
    178 	irq = acpi_res_irq(&res, 0);
    179 	if (irq == NULL) {
    180 		aprint_error("%s: unable to find irq resource\n", sc->sc_dv.dv_xname);
    181 		goto out;
    182 	}
    183 	psc->sc_irq = irq->ar_irq;
    184 	psc->sc_ist = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
    185 
    186 	if (psc->sc_slot == PCKBC_KBD_SLOT)
    187 		first = psc;
    188 
    189 	if ((!first || !first->sc_pckbc.id) &&
    190 	    (psc->sc_slot == PCKBC_KBD_SLOT)) {
    191 
    192 		io0 = acpi_res_io(&res, 0);
    193 		if (io0 == NULL) {
    194 			aprint_error("%s: unable to find i/o resources\n",
    195 			    sc->sc_dv.dv_xname);
    196 			goto out;
    197 		}
    198 
    199 		if (pckbc_is_console(aa->aa_iot, io0->ar_base)) {
    200 			t = &pckbc_consdata;
    201 			ioh_d = t->t_ioh_d;
    202 			ioh_c = t->t_ioh_c;
    203 			pckbc_console_attached = 1;
    204 			/* t->t_cmdbyte was initialized by cnattach */
    205 		} else {
    206 			io1 = acpi_res_io(&res, 1);
    207 			if (io1 == NULL) {
    208 				aprint_error("%s: unable to find i/o resources\n",
    209 				    sc->sc_dv.dv_xname);
    210 				goto out;
    211 			}
    212 			if (bus_space_map(aa->aa_iot, io0->ar_base,
    213 					  io0->ar_length, 0, &ioh_d) ||
    214 			    bus_space_map(aa->aa_iot, io1->ar_base,
    215 					  io1->ar_length, 0, &ioh_c))
    216 				panic("pckbc_acpi_attach: couldn't map");
    217 
    218 			t = malloc(sizeof(struct pckbc_internal),
    219 			    M_DEVBUF, M_WAITOK);
    220 			memset(t, 0, sizeof(*t));
    221 			t->t_iot = aa->aa_iot;
    222 			t->t_ioh_d = ioh_d;
    223 			t->t_ioh_c = ioh_c;
    224 			t->t_addr = io0->ar_base;
    225 			t->t_cmdbyte = KC8_CPU;	/* Enable ports */
    226 			callout_init(&t->t_cleanup, 0);
    227 		}
    228 
    229 		t->t_sc = &first->sc_pckbc;
    230 		first->sc_pckbc.id = t;
    231 
    232 		status = pnp_register(self, pckbc_acpi_power);
    233 		if (status != PNP_STATUS_SUCCESS)
    234 			aprint_error("%s: couldn't establish power handler\n",
    235 			    device_xname(self));
    236 
    237 		first->sc_pckbc.intr_establish = pckbc_acpi_intr_establish;
    238 		config_defer(&first->sc_pckbc.sc_dv,
    239 			     (void(*)(struct device *))pckbc_attach);
    240 	} else
    241 		(void)pnp_register(self, pnp_generic_power);
    242  out:
    243 	acpi_resource_cleanup(&res);
    244 }
    245 
    246 static void
    247 pckbc_acpi_intr_establish(struct pckbc_softc *sc,
    248     pckbc_slot_t slot)
    249 {
    250 	struct pckbc_acpi_softc *psc;
    251 	isa_chipset_tag_t ic = NULL;
    252 	void *rv = NULL;
    253 	int irq = 0, ist = 0; /* XXX: gcc */
    254 	int i;
    255 
    256 	/*
    257 	 * Note we're always called with sc == first.
    258 	 */
    259 	for (i = 0; i < pckbc_cd.cd_ndevs; i++) {
    260 		psc = pckbc_cd.cd_devs[i];
    261 		if (psc && psc->sc_slot == slot) {
    262 			irq = psc->sc_irq;
    263 			ist = psc->sc_ist;
    264 			ic = psc->sc_ic;
    265 			break;
    266 		}
    267 	}
    268 	if (i < pckbc_cd.cd_ndevs)
    269 		rv = isa_intr_establish(ic, irq, ist, IPL_TTY, pckbcintr, sc);
    270 	if (rv == NULL) {
    271 		aprint_error("%s: unable to establish interrupt for %s slot\n",
    272 		    sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
    273 	} else {
    274 		aprint_normal("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname,
    275 		    irq, pckbc_slot_names[slot]);
    276 	}
    277 }
    278 
    279 static pnp_status_t
    280 pckbc_acpi_power(device_t dv, pnp_request_t req, void *opaque)
    281 {
    282 	return pckbc_power(dv, req, opaque);
    283 }
    284