xref: /netbsd/sys/arch/i386/pnpbios/pckbc_pnpbios.c (revision 6550d01e)
1 /*	$NetBSD: pckbc_pnpbios.c,v 1.17 2008/06/08 18:35:25 tsutsui 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  * PNPBIOS attachment for the PC Keyboard Controller driver.
34  *
35  * This is a little wonky.  The keyboard controller actually
36  * has 2 PNPBIOS 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_pnpbios.c,v 1.17 2008/06/08 18:35:25 tsutsui Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/device.h>
52 #include <sys/malloc.h>
53 #include <sys/errno.h>
54 #include <sys/queue.h>
55 #include <sys/bus.h>
56 
57 #include <dev/isa/isareg.h>
58 #include <dev/isa/isavar.h>
59 
60 #include <dev/ic/i8042reg.h>
61 #include <dev/ic/pckbcvar.h>
62 
63 #include <i386/pnpbios/pnpbiosvar.h>
64 
65 int	pckbc_pnpbios_match(device_t, cfdata_t, void *);
66 void	pckbc_pnpbios_attach(device_t, device_t, void *);
67 
68 struct pckbc_pnpbios_softc {
69 	struct pckbc_softc sc_pckbc;
70 
71 	isa_chipset_tag_t sc_ic;
72 	int sc_irq;
73 	int sc_ist;
74 	pckbc_slot_t sc_slot;
75 };
76 
77 /* Save first port: */
78 static struct pckbc_pnpbios_softc *first;
79 
80 extern struct cfdriver pckbc_cd;
81 
82 CFATTACH_DECL_NEW(pckbc_pnpbios, sizeof(struct pckbc_pnpbios_softc),
83     pckbc_pnpbios_match, pckbc_pnpbios_attach, NULL, NULL);
84 
85 void	pckbc_pnpbios_intr_establish(struct pckbc_softc *, pckbc_slot_t);
86 static void pckbc_pnpbios_finish_attach(device_t);
87 
88 int
89 pckbc_pnpbios_match(device_t parent, cfdata_t match, void *aux)
90 {
91 	struct pnpbiosdev_attach_args *aa = aux;
92 
93 	if (strcmp(aa->idstr, "PNP0303") == 0 ||
94 	    strcmp(aa->idstr, "PNP0320") == 0)	/* Japanese 106 */
95 		return (1);			/* plus more? */
96 	if (strcmp(aa->idstr, "PNP0F13") == 0)
97 		return (1);
98 
99 	return (0);
100 }
101 
102 void
103 pckbc_pnpbios_attach(device_t parent, device_t self, void *aux)
104 {
105 	struct pckbc_pnpbios_softc *psc = device_private(self);
106 	struct pckbc_softc *sc = &psc->sc_pckbc;
107 	struct pckbc_internal *t;
108 	struct pnpbiosdev_attach_args *aa = aux;
109 	bus_space_tag_t iot;
110 	bus_space_handle_t ioh_d, ioh_c;
111 	pckbc_slot_t peer;
112 	int iobase;
113 
114 	sc->sc_dv = self;
115 	if (strncmp(aa->idstr, "PNP03", 5) == 0) {
116 		psc->sc_slot = PCKBC_KBD_SLOT;
117 		peer = PCKBC_AUX_SLOT;
118 	} else if (strcmp(aa->idstr, "PNP0F13") == 0) {
119 		psc->sc_slot = PCKBC_AUX_SLOT;
120 		peer = PCKBC_KBD_SLOT;
121 	} else {
122 		aprint_error(": unknown port!\n");
123 		panic("pckbc_pnpbios_attach: impossible");
124 	}
125 
126 	aprint_normal(": %s port\n", pckbc_slot_names[psc->sc_slot]);
127 
128 	if (pnpbios_getirqnum(aa->pbt, aa->resc, 0, &psc->sc_irq,
129 	    &psc->sc_ist) != 0) {
130 		aprint_error_dev(self, "unable to get IRQ number or type\n");
131 		return;
132 	}
133 
134 	psc->sc_ic = aa->ic;
135 
136 	if (!first)
137 		first = psc;
138 
139 	if (!first->sc_pckbc.id) {
140 
141 		if (pnpbios_getiobase(aa->pbt, aa->resc, 0, &iot, &iobase))
142 			return;
143 
144 		if (pckbc_is_console(iot, iobase)) {
145 			t = &pckbc_consdata;
146 			ioh_d = t->t_ioh_d;
147 			ioh_c = t->t_ioh_c;
148 			pckbc_console_attached = 1;
149 			/* t->t_cmdbyte was initialized by cnattach */
150 		} else {
151 			if (pnpbios_io_map(aa->pbt, aa->resc, 0,
152 					   &iot, &ioh_d) ||
153 			    pnpbios_io_map(aa->pbt, aa->resc, 1,
154 			    		   &iot, &ioh_c))
155 				panic("pckbc_pnpbios_attach: couldn't map");
156 
157 			t = malloc(sizeof(struct pckbc_internal),
158 			    M_DEVBUF, M_WAITOK);
159 			memset(t, 0, sizeof(*t));
160 			t->t_iot = iot;
161 			t->t_ioh_d = ioh_d;
162 			t->t_ioh_c = ioh_c;
163 			t->t_addr = iobase;
164 			t->t_cmdbyte = KC8_CPU;	/* Enable ports */
165 			callout_init(&t->t_cleanup, 0);
166 		}
167 
168 		t->t_sc = &first->sc_pckbc;
169 		first->sc_pckbc.id = t;
170 
171 		first->sc_pckbc.intr_establish = pckbc_pnpbios_intr_establish;
172 		config_defer(first->sc_pckbc.sc_dv,
173 			     pckbc_pnpbios_finish_attach);
174 	}
175 }
176 
177 static void
178 pckbc_pnpbios_finish_attach(device_t dv)
179 {
180 
181 	pckbc_attach(device_private(dv));
182 }
183 
184 void
185 pckbc_pnpbios_intr_establish(struct pckbc_softc *sc,
186     pckbc_slot_t slot)
187 {
188 	struct pckbc_pnpbios_softc *psc;
189 	isa_chipset_tag_t ic = NULL;
190 	void *rv = NULL;
191 	int irq, ist;
192 	int i;
193 
194 	/*
195 	 * Note we're always called with sc == first.
196 	 */
197 	for (i = 0; i < pckbc_cd.cd_ndevs; i++) {
198 		psc = device_lookup_private(&pckbc_cd, i);
199 		if (psc && psc->sc_slot == slot) {
200 			irq = psc->sc_irq;
201 			ist = psc->sc_ist;
202 			ic = psc->sc_ic;
203 			break;
204 		}
205 	}
206 	if (i < pckbc_cd.cd_ndevs)
207 		rv = isa_intr_establish(ic, irq, ist, IPL_TTY, pckbcintr, sc);
208 	if (rv == NULL) {
209 		aprint_error_dev(sc->sc_dv,
210 		    "unable to establish interrupt for %s slot\n",
211 		    pckbc_slot_names[slot]);
212 	} else {
213 		aprint_normal_dev(sc->sc_dv, "using irq %d for %s slot\n",
214 		    irq, pckbc_slot_names[slot]);
215 	}
216 }
217