No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / isa / pckbc_isa.c
blob3fdc3c01b63e54ef4d5f9c9caf8fd2ae68c4906d
1 /* $NetBSD: pckbc_isa.c,v 1.24 2008/03/15 13:23:25 cube Exp $ */
3 /*
4 * Copyright (c) 1998
5 * Matthias Drochner. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: pckbc_isa.c,v 1.24 2008/03/15 13:23:25 cube Exp $");
31 #include "opt_pckbc.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/proc.h>
37 #include <sys/device.h>
38 #include <sys/malloc.h>
39 #include <sys/errno.h>
40 #include <sys/queue.h>
42 #include <sys/bus.h>
44 #include <dev/isa/isareg.h>
45 #include <dev/isa/isavar.h>
47 #include <dev/ic/i8042reg.h>
48 #include <dev/ic/pckbcvar.h>
50 int pckbc_isa_match(device_t, cfdata_t, void *);
51 void pckbc_isa_attach(device_t, device_t, void *);
53 struct pckbc_isa_softc {
54 struct pckbc_softc sc_pckbc;
56 isa_chipset_tag_t sc_ic;
57 int sc_irq[PCKBC_NSLOTS];
60 CFATTACH_DECL_NEW(pckbc_isa, sizeof(struct pckbc_isa_softc),
61 pckbc_isa_match, pckbc_isa_attach, NULL, NULL);
63 void pckbc_isa_intr_establish(struct pckbc_softc *, pckbc_slot_t);
65 int
66 pckbc_isa_match(device_t parent, cfdata_t match, void *aux)
68 struct isa_attach_args *ia = aux;
69 bus_space_tag_t iot = ia->ia_iot;
70 bus_space_handle_t ioh_d, ioh_c;
71 int res, ok = 1;
73 if (ISA_DIRECT_CONFIG(ia))
74 return (0);
76 /* If values are hardwired to something that they can't be, punt. */
77 if (ia->ia_nio < 1 ||
78 (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
79 ia->ia_io[0].ir_addr != IO_KBD))
80 return (0);
82 if (ia->ia_niomem > 0 &&
83 (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM))
84 return (0);
86 if (ia->ia_nirq < 1 ||
87 (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
88 ia->ia_irq[0].ir_irq != 1 /*XXX*/))
89 return (0);
91 if (ia->ia_ndrq > 0 &&
92 (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ))
93 return (0);
95 if (pckbc_is_console(iot, IO_KBD) == 0) {
96 struct pckbc_internal t;
98 if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d))
99 return (0);
100 if (bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c)) {
101 bus_space_unmap(iot, ioh_d, 1);
102 return (0);
105 memset(&t, 0, sizeof(t));
106 t.t_iot = iot;
107 t.t_ioh_d = ioh_d;
108 t.t_ioh_c = ioh_c;
110 /* flush KBC */
111 (void) pckbc_poll_data1(&t, PCKBC_KBD_SLOT);
113 /* KBC selftest */
114 if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) {
115 ok = 0;
116 goto out;
118 res = pckbc_poll_data1(&t, PCKBC_KBD_SLOT);
119 #ifndef PCKBCNOTEST
120 if (res != 0x55) {
121 #ifdef PCKBCDEBUG
122 aprint_verbose("kbc selftest: %x\n", res);
123 #endif
124 ok = 0;
126 #endif /* PCKBCNOTEST */
127 out:
128 bus_space_unmap(iot, ioh_d, 1);
129 bus_space_unmap(iot, ioh_c, 1);
132 if (ok) {
133 ia->ia_io[0].ir_addr = IO_KBD;
134 ia->ia_io[0].ir_size = 5;
135 ia->ia_nio = 1;
137 ia->ia_niomem = 0;
138 ia->ia_nirq = 0;
139 ia->ia_ndrq = 0;
141 return (ok);
144 void
145 pckbc_isa_attach(device_t parent, device_t self, void *aux)
147 struct pckbc_isa_softc *isc = device_private(self);
148 struct pckbc_softc *sc = &isc->sc_pckbc;
149 struct isa_attach_args *ia = aux;
150 struct pckbc_internal *t;
151 bus_space_tag_t iot;
152 bus_space_handle_t ioh_d, ioh_c;
154 sc->sc_dv = self;
155 isc->sc_ic = ia->ia_ic;
156 iot = ia->ia_iot;
158 switch (ia->ia_nirq) {
159 case 1:
160 /* Both channels use the same IRQ. */
161 isc->sc_irq[PCKBC_KBD_SLOT] =
162 isc->sc_irq[PCKBC_AUX_SLOT] = ia->ia_irq[0].ir_irq;
163 break;
165 case 2:
166 /* First IRQ is kbd, second IRQ is aux port. */
167 isc->sc_irq[PCKBC_KBD_SLOT] = ia->ia_irq[0].ir_irq;
168 isc->sc_irq[PCKBC_AUX_SLOT] = ia->ia_irq[1].ir_irq;
169 break;
171 default:
172 /* Set up IRQs for "normal" ISA. */
173 isc->sc_irq[PCKBC_KBD_SLOT] = 1;
174 isc->sc_irq[PCKBC_AUX_SLOT] = 12;
175 break;
178 sc->intr_establish = pckbc_isa_intr_establish;
180 if (pckbc_is_console(iot, IO_KBD)) {
181 t = &pckbc_consdata;
182 ioh_d = t->t_ioh_d;
183 ioh_c = t->t_ioh_c;
184 pckbc_console_attached = 1;
185 /* t->t_cmdbyte was initialized by cnattach */
186 } else {
187 if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d) ||
188 bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c))
189 panic("pckbc_attach: couldn't map");
191 t = malloc(sizeof(struct pckbc_internal), M_DEVBUF,
192 M_WAITOK|M_ZERO);
193 t->t_iot = iot;
194 t->t_ioh_d = ioh_d;
195 t->t_ioh_c = ioh_c;
196 t->t_addr = IO_KBD;
197 t->t_cmdbyte = KC8_CPU; /* Enable ports */
198 callout_init(&t->t_cleanup, 0);
201 t->t_sc = sc;
202 sc->id = t;
204 aprint_normal("\n");
206 if (!pmf_device_register(self, NULL, pckbc_resume))
207 aprint_error_dev(self, "couldn't establish power handler\n");
209 /* Finish off the attach. */
210 pckbc_attach(sc);
213 void
214 pckbc_isa_intr_establish(struct pckbc_softc *sc, pckbc_slot_t slot)
216 struct pckbc_isa_softc *isc = (void *) sc;
217 void *rv;
219 rv = isa_intr_establish(isc->sc_ic, isc->sc_irq[slot], IST_EDGE,
220 IPL_TTY, pckbcintr, sc);
221 if (rv == NULL) {
222 aprint_error_dev(sc->sc_dv,
223 "unable to establish interrupt for %s slot\n",
224 pckbc_slot_names[slot]);
225 } else {
226 aprint_normal_dev(sc->sc_dv, "using irq %d for %s slot\n",
227 isc->sc_irq[slot], pckbc_slot_names[slot]);