1 /* $NetBSD: pckbc_js.c,v 1.17 2008/03/15 13:23:24 cube Exp $ */
4 * Copyright (c) 2002 Valeriy E. Ushakov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: pckbc_js.c,v 1.17 2008/03/15 13:23:24 cube Exp $");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/device.h>
37 #include <sys/malloc.h>
41 #include <machine/autoconf.h>
43 #include <dev/ic/i8042reg.h>
44 #include <dev/ic/pckbcvar.h>
45 #include <dev/pckbport/pckbportvar.h>
47 #include <dev/ebus/ebusreg.h>
48 #include <dev/ebus/ebusvar.h>
50 struct pckbc_js_softc
{
51 struct pckbc_softc jsc_pckbc
; /* real "pckbc" softc */
53 /* kbd and mouse share interrupt in both mr.coffee and krups */
60 static int pckbc_obio_match(device_t
, cfdata_t
, void *);
61 static void pckbc_obio_attach(device_t
, device_t
, void *);
63 static int pckbc_ebus_match(device_t
, cfdata_t
, void *);
64 static void pckbc_ebus_attach(device_t
, device_t
, void *);
66 static void pckbc_js_attach_common(struct pckbc_js_softc
*,
67 bus_space_tag_t
, bus_addr_t
, int, int);
68 static void pckbc_js_intr_establish(struct pckbc_softc
*, pckbport_slot_t
);
69 static int jsc_pckbdintr(void *);
72 CFATTACH_DECL_NEW(pckbc_obio
, sizeof(struct pckbc_js_softc
),
73 pckbc_obio_match
, pckbc_obio_attach
, NULL
, NULL
);
76 CFATTACH_DECL_NEW(pckbc_ebus
, sizeof(struct pckbc_js_softc
),
77 pckbc_ebus_match
, pckbc_ebus_attach
, NULL
, NULL
);
79 #define PCKBC_PROM_DEVICE_NAME "8042"
82 pckbc_obio_match(device_t parent
, cfdata_t cf
, void *aux
)
84 union obio_attach_args
*uoba
= aux
;
85 struct sbus_attach_args
*sa
= &uoba
->uoba_sbus
;
87 return (strcmp(sa
->sa_name
, PCKBC_PROM_DEVICE_NAME
) == 0);
91 pckbc_ebus_match(device_t parent
, cfdata_t cf
, void *aux
)
93 struct ebus_attach_args
*ea
= aux
;
95 return (strcmp(ea
->ea_name
, PCKBC_PROM_DEVICE_NAME
) == 0);
100 pckbc_obio_attach(device_t parent
, device_t self
, void *aux
)
102 struct pckbc_js_softc
*jsc
= device_private(self
);
103 union obio_attach_args
*uoba
= aux
;
104 struct sbus_attach_args
*sa
= &uoba
->uoba_sbus
;
109 jsc
->jsc_pckbc
.sc_dv
= self
;
111 ioaddr
= BUS_ADDR(sa
->sa_slot
, sa
->sa_offset
);
112 intr
= sa
->sa_nintr
? sa
->sa_pri
: /* level */ 13;
116 * . on OFW machines stdin is keyboard node, not 8042 node
117 * . on OBP machines 8042 node is faked by boot's prompatch
118 * and PROM's stdin points to zs keyboard (add prom patch?)
119 * . we probably want the stdout node to control whether
120 * console goes to serial
124 pckbc_js_attach_common(jsc
, iot
, ioaddr
, intr
, isconsole
);
128 pckbc_ebus_attach(device_t parent
, device_t self
, void *aux
)
130 struct pckbc_js_softc
*jsc
= device_private(self
);
131 struct ebus_attach_args
*ea
= aux
;
135 int stdin_node
, node
;
138 jsc
->jsc_pckbc
.sc_dv
= self
;
140 ioaddr
= EBUS_ADDR_FROM_REG(&ea
->ea_reg
[0]);
141 intr
= ea
->ea_nintr
? ea
->ea_intr
[0] : /* line */ 0;
143 /* search children of "8042" node for stdin (keyboard) */
144 stdin_node
= prom_instance_to_package(prom_stdin());
147 for (node
= prom_firstchild(ea
->ea_node
);
148 node
!= 0; node
= prom_nextsibling(node
))
149 if (node
== stdin_node
) {
154 pckbc_js_attach_common(jsc
, iot
, ioaddr
, intr
, isconsole
);
159 pckbc_js_attach_common(struct pckbc_js_softc
*jsc
,
160 bus_space_tag_t iot
, bus_addr_t ioaddr
, int intr
,
163 struct pckbc_softc
*sc
= (struct pckbc_softc
*)jsc
;
164 struct pckbc_internal
*t
;
166 jsc
->jsc_pckbc
.intr_establish
= pckbc_js_intr_establish
;
167 jsc
->jsc_intr
= intr
;
168 jsc
->jsc_establised
= 0;
173 status
= pckbc_cnattach(iot
, ioaddr
, KBCMDP
, PCKBC_KBD_SLOT
);
175 aprint_normal(": cnattach ok");
177 aprint_error(": cnattach %d", status
);
180 if (pckbc_is_console(iot
, ioaddr
)) {
182 pckbc_console_attached
= 1;
184 bus_space_handle_t ioh_d
, ioh_c
;
186 if (bus_space_map(iot
, ioaddr
+ KBDATAP
, 1, 0, &ioh_d
) != 0) {
187 aprint_error(": unable to map data register\n");
191 if (bus_space_map(iot
, ioaddr
+ KBCMDP
, 1, 0, &ioh_c
) != 0) {
192 bus_space_unmap(iot
, ioh_d
, 1);
193 aprint_error(": unable to map cmd register\n");
197 t
= malloc(sizeof(struct pckbc_internal
), M_DEVBUF
, M_WAITOK
);
198 memset(t
, 0, sizeof(struct pckbc_internal
));
203 t
->t_cmdbyte
= KC8_CPU
; /* initial command: enable ports */
204 callout_init(&t
->t_cleanup
, 0);
206 (void) pckbc_poll_data1(t
, PCKBC_KBD_SLOT
); /* flush */
208 if (pckbc_send_cmd(iot
, ioh_c
, KBC_SELFTEST
) == 0)
209 aprint_error(": unable to request self test");
213 response
= pckbc_poll_data1(t
, PCKBC_KBD_SLOT
);
214 if (response
== 0x55)
215 aprint_normal(": selftest ok");
217 aprint_error(": selftest failed (0x%02x)",
226 /* finish off the attach */
233 * Keyboard and mouse share the interrupt
234 * so don't install interrupt handler twice.
237 pckbc_js_intr_establish(struct pckbc_softc
*sc
, pckbport_slot_t slot
)
239 struct pckbc_js_softc
*jsc
= (struct pckbc_js_softc
*)sc
;
242 if (jsc
->jsc_establised
) {
244 aprint_verbose_dev(sc
->sc_dv
,
245 "%s slot shares interrupt (already established)\n",
246 pckbc_slot_names
[slot
]);
252 * We can not choose the devic class interruptlevel freely,
253 * so we debounce via a softinterrupt.
255 jsc
->jsc_int_cookie
= softint_establish(SOFTINT_SERIAL
,
256 pckbcintr_soft
, &jsc
->jsc_pckbc
);
257 if (jsc
->jsc_int_cookie
== NULL
) {
258 aprint_error_dev(sc
->sc_dv
,
259 "unable to establish %s soft interrupt\n",
260 pckbc_slot_names
[slot
]);
263 res
= bus_intr_establish(sc
->id
->t_iot
, jsc
->jsc_intr
,
264 IPL_SERIAL
, jsc_pckbdintr
, jsc
);
266 aprint_error_dev(sc
->sc_dv
,
267 "unable to establish %s slot interrupt\n",
268 pckbc_slot_names
[slot
]);
270 jsc
->jsc_establised
= 1;
274 jsc_pckbdintr(void *vsc
)
276 struct pckbc_js_softc
*jsc
= vsc
;
278 softint_schedule(jsc
->jsc_int_cookie
);
279 pckbcintr_hard(&jsc
->jsc_pckbc
);
282 * This interrupt is not shared on javastations, avoid "stray"
283 * warnings. XXX - why do "stray interrupt" warnings happen if
284 * we don't claim the interrupt always?
290 * MD hook for use without MI wscons.
291 * Called by pckbc_cnattach().
294 pckbport_machdep_cnattach(pckbport_tag_t constag
, pckbport_slot_t slot
)