1 /*        $NetBSD: pckbc_pnpbios.c,v 1.18 2014/03/23 02:57:58 christos 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.18 2014/03/23 02:57:58 christos 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
pckbc_pnpbios_match(device_t parent,cfdata_t match,void * aux)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
pckbc_pnpbios_attach(device_t parent,device_t self,void * aux)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           int iobase;
112 
113           sc->sc_dv = self;
114           if (strncmp(aa->idstr, "PNP03", 5) == 0) {
115                     psc->sc_slot = PCKBC_KBD_SLOT;
116           } else if (strcmp(aa->idstr, "PNP0F13") == 0) {
117                     psc->sc_slot = PCKBC_AUX_SLOT;
118           } else {
119                     aprint_error(": unknown port!\n");
120                     panic("pckbc_pnpbios_attach: impossible");
121           }
122 
123           aprint_normal(": %s port\n", pckbc_slot_names[psc->sc_slot]);
124 
125           if (pnpbios_getirqnum(aa->pbt, aa->resc, 0, &psc->sc_irq,
126               &psc->sc_ist) != 0) {
127                     aprint_error_dev(self, "unable to get IRQ number or type\n");
128                     return;
129           }
130 
131           psc->sc_ic = aa->ic;
132 
133           if (!first)
134                     first = psc;
135 
136           if (!first->sc_pckbc.id) {
137 
138                     if (pnpbios_getiobase(aa->pbt, aa->resc, 0, &iot, &iobase))
139                               return;
140 
141                     if (pckbc_is_console(iot, iobase)) {
142                               t = &pckbc_consdata;
143                               ioh_d = t->t_ioh_d;
144                               ioh_c = t->t_ioh_c;
145                               pckbc_console_attached = 1;
146                               /* t->t_cmdbyte was initialized by cnattach */
147                     } else {
148                               if (pnpbios_io_map(aa->pbt, aa->resc, 0,
149                                                      &iot, &ioh_d) ||
150                                   pnpbios_io_map(aa->pbt, aa->resc, 1,
151                                                      &iot, &ioh_c))
152                                         panic("pckbc_pnpbios_attach: couldn't map");
153 
154                               t = malloc(sizeof(struct pckbc_internal),
155                                   M_DEVBUF, M_WAITOK);
156                               memset(t, 0, sizeof(*t));
157                               t->t_iot = iot;
158                               t->t_ioh_d = ioh_d;
159                               t->t_ioh_c = ioh_c;
160                               t->t_addr = iobase;
161                               t->t_cmdbyte = KC8_CPU;       /* Enable ports */
162                               callout_init(&t->t_cleanup, 0);
163                     }
164 
165                     t->t_sc = &first->sc_pckbc;
166                     first->sc_pckbc.id = t;
167 
168                     first->sc_pckbc.intr_establish = pckbc_pnpbios_intr_establish;
169                     config_defer(first->sc_pckbc.sc_dv,
170                                    pckbc_pnpbios_finish_attach);
171           }
172 }
173 
174 static void
pckbc_pnpbios_finish_attach(device_t dv)175 pckbc_pnpbios_finish_attach(device_t dv)
176 {
177 
178           pckbc_attach(device_private(dv));
179 }
180 
181 void
pckbc_pnpbios_intr_establish(struct pckbc_softc * sc,pckbc_slot_t slot)182 pckbc_pnpbios_intr_establish(struct pckbc_softc *sc,
183     pckbc_slot_t slot)
184 {
185           struct pckbc_pnpbios_softc *psc;
186           isa_chipset_tag_t ic = NULL;
187           void *rv = NULL;
188           int irq, ist;
189           int i;
190 
191           /*
192            * Note we're always called with sc == first.
193            */
194           for (i = 0; i < pckbc_cd.cd_ndevs; i++) {
195                     psc = device_lookup_private(&pckbc_cd, i);
196                     if (psc && psc->sc_slot == slot) {
197                               irq = psc->sc_irq;
198                               ist = psc->sc_ist;
199                               ic = psc->sc_ic;
200                               break;
201                     }
202           }
203           if (i < pckbc_cd.cd_ndevs)
204                     rv = isa_intr_establish(ic, irq, ist, IPL_TTY, pckbcintr, sc);
205           if (rv == NULL) {
206                     aprint_error_dev(sc->sc_dv,
207                         "unable to establish interrupt for %s slot\n",
208                         pckbc_slot_names[slot]);
209           } else {
210                     aprint_normal_dev(sc->sc_dv, "using irq %d for %s slot\n",
211                         irq, pckbc_slot_names[slot]);
212           }
213 }
214