pckbc_js.c revision 1.7.6.3 1 1.7.6.3 skrll /* $NetBSD: pckbc_js.c,v 1.7.6.3 2004/09/21 13:22:02 skrll Exp $ */
2 1.1 uwe
3 1.1 uwe /*
4 1.1 uwe * Copyright (c) 2002 Valeriy E. Ushakov
5 1.1 uwe * All rights reserved.
6 1.1 uwe *
7 1.1 uwe * Redistribution and use in source and binary forms, with or without
8 1.1 uwe * modification, are permitted provided that the following conditions
9 1.1 uwe * are met:
10 1.1 uwe * 1. Redistributions of source code must retain the above copyright
11 1.1 uwe * notice, this list of conditions and the following disclaimer.
12 1.1 uwe * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 uwe * notice, this list of conditions and the following disclaimer in the
14 1.1 uwe * documentation and/or other materials provided with the distribution.
15 1.1 uwe * 3. The name of the author may not be used to endorse or promote products
16 1.1 uwe * derived from this software without specific prior written permission
17 1.1 uwe *
18 1.1 uwe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 uwe * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 uwe * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 uwe * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 uwe * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 uwe * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 uwe * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 uwe * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 uwe * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 uwe * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 uwe */
29 1.1 uwe
30 1.7.6.1 skrll #include <sys/cdefs.h>
31 1.7.6.3 skrll __KERNEL_RCSID(0, "$NetBSD: pckbc_js.c,v 1.7.6.3 2004/09/21 13:22:02 skrll Exp $");
32 1.7.6.1 skrll
33 1.1 uwe #include <sys/param.h>
34 1.1 uwe #include <sys/systm.h>
35 1.1 uwe #include <sys/kernel.h>
36 1.1 uwe #include <sys/device.h>
37 1.1 uwe #include <sys/malloc.h>
38 1.1 uwe
39 1.1 uwe #include <machine/autoconf.h>
40 1.1 uwe #include <machine/bus.h>
41 1.1 uwe #include <machine/intr.h>
42 1.1 uwe
43 1.1 uwe #include <dev/ic/i8042reg.h>
44 1.1 uwe #include <dev/ic/pckbcvar.h>
45 1.7.6.1 skrll #include <dev/pckbport/pckbportvar.h>
46 1.1 uwe
47 1.2 uwe #include <dev/ebus/ebusreg.h>
48 1.2 uwe #include <dev/ebus/ebusvar.h>
49 1.1 uwe
50 1.7.6.1 skrll #ifdef __GENERIC_SOFT_INTERRUPTS_ALL_LEVELS
51 1.7.6.1 skrll #define IPL_JSCKBD IPL_TTY
52 1.7.6.1 skrll #else
53 1.7.6.1 skrll #define IPL_JSCKBD IPL_SOFTSERIAL
54 1.7.6.1 skrll #endif
55 1.1 uwe
56 1.1 uwe struct pckbc_js_softc {
57 1.1 uwe struct pckbc_softc jsc_pckbc; /* real "pckbc" softc */
58 1.1 uwe
59 1.1 uwe /* kbd and mouse share interrupt in both mr.coffee and krups */
60 1.1 uwe u_int32_t jsc_intr;
61 1.1 uwe int jsc_establised;
62 1.7.6.1 skrll void *jsc_int_cookie;
63 1.1 uwe };
64 1.1 uwe
65 1.1 uwe
66 1.1 uwe static int pckbc_obio_match(struct device *, struct cfdata *, void *);
67 1.1 uwe static void pckbc_obio_attach(struct device *, struct device *, void *);
68 1.1 uwe
69 1.1 uwe static int pckbc_ebus_match(struct device *, struct cfdata *, void *);
70 1.1 uwe static void pckbc_ebus_attach(struct device *, struct device *, void *);
71 1.1 uwe
72 1.1 uwe static void pckbc_js_attach_common( struct pckbc_js_softc *,
73 1.1 uwe bus_space_tag_t, bus_addr_t, int, int);
74 1.7.6.1 skrll static void pckbc_js_intr_establish(struct pckbc_softc *, pckbport_slot_t);
75 1.7.6.1 skrll static int jsc_pckbdintr(void *vsc);
76 1.1 uwe
77 1.1 uwe /* Mr.Coffee */
78 1.4 thorpej CFATTACH_DECL(pckbc_obio, sizeof(struct pckbc_js_softc),
79 1.5 thorpej pckbc_obio_match, pckbc_obio_attach, NULL, NULL);
80 1.1 uwe
81 1.1 uwe /* ms-IIep */
82 1.4 thorpej CFATTACH_DECL(pckbc_ebus, sizeof(struct pckbc_js_softc),
83 1.5 thorpej pckbc_ebus_match, pckbc_ebus_attach, NULL, NULL);
84 1.1 uwe
85 1.1 uwe #define PCKBC_PROM_DEVICE_NAME "8042"
86 1.1 uwe
87 1.1 uwe static int
88 1.1 uwe pckbc_obio_match(parent, cf, aux)
89 1.1 uwe struct device *parent;
90 1.1 uwe struct cfdata *cf;
91 1.1 uwe void *aux;
92 1.1 uwe {
93 1.1 uwe union obio_attach_args *uoba = aux;
94 1.1 uwe struct sbus_attach_args *sa = &uoba->uoba_sbus;
95 1.1 uwe
96 1.1 uwe return (strcmp(sa->sa_name, PCKBC_PROM_DEVICE_NAME) == 0);
97 1.1 uwe }
98 1.1 uwe
99 1.1 uwe static int
100 1.1 uwe pckbc_ebus_match(parent, cf, aux)
101 1.1 uwe struct device *parent;
102 1.1 uwe struct cfdata *cf;
103 1.1 uwe void *aux;
104 1.1 uwe {
105 1.1 uwe struct ebus_attach_args *ea = aux;
106 1.1 uwe
107 1.1 uwe return (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0);
108 1.1 uwe }
109 1.1 uwe
110 1.1 uwe
111 1.1 uwe static void
112 1.1 uwe pckbc_obio_attach(parent, self, aux)
113 1.1 uwe struct device *parent, *self;
114 1.1 uwe void *aux;
115 1.1 uwe {
116 1.1 uwe struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
117 1.1 uwe union obio_attach_args *uoba = aux;
118 1.1 uwe struct sbus_attach_args *sa = &uoba->uoba_sbus;
119 1.1 uwe bus_space_tag_t iot;
120 1.1 uwe bus_addr_t ioaddr;
121 1.1 uwe int intr, isconsole;
122 1.1 uwe
123 1.1 uwe iot = sa->sa_bustag;
124 1.1 uwe ioaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
125 1.1 uwe intr = sa->sa_nintr ? sa->sa_pri : /* level */ 13;
126 1.1 uwe
127 1.1 uwe /*
128 1.1 uwe * TODO:
129 1.1 uwe * . on OFW machines stdin is keyboard node, not 8042 node
130 1.1 uwe * . on OBP machines 8042 node is faked by boot's prompatch
131 1.1 uwe * and PROM's stdin points to zs keyboard (add prom patch?)
132 1.1 uwe * . we probably want the stdout node to control whether
133 1.1 uwe * console goes to serial
134 1.1 uwe */
135 1.1 uwe isconsole = 1;
136 1.1 uwe
137 1.1 uwe pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
138 1.1 uwe }
139 1.1 uwe
140 1.1 uwe static void
141 1.1 uwe pckbc_ebus_attach(parent, self, aux)
142 1.1 uwe struct device *parent, *self;
143 1.1 uwe void *aux;
144 1.1 uwe {
145 1.1 uwe struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
146 1.1 uwe struct ebus_attach_args *ea = aux;
147 1.1 uwe bus_space_tag_t iot;
148 1.1 uwe bus_addr_t ioaddr;
149 1.6 uwe int intr;
150 1.6 uwe int stdin_node, node;
151 1.6 uwe int isconsole;
152 1.1 uwe
153 1.1 uwe iot = ea->ea_bustag;
154 1.2 uwe ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
155 1.1 uwe intr = ea->ea_nintr ? ea->ea_intr[0] : /* line */ 0;
156 1.1 uwe
157 1.6 uwe /* search children of "8042" node for stdin (keyboard) */
158 1.6 uwe stdin_node = prom_instance_to_package(prom_stdin());
159 1.6 uwe isconsole = 0;
160 1.6 uwe
161 1.6 uwe for (node = prom_firstchild(ea->ea_node);
162 1.6 uwe node != 0; node = prom_nextsibling(node))
163 1.6 uwe if (node == stdin_node) {
164 1.6 uwe isconsole = 1;
165 1.6 uwe break;
166 1.6 uwe }
167 1.1 uwe
168 1.1 uwe pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
169 1.1 uwe }
170 1.1 uwe
171 1.1 uwe
172 1.1 uwe static void
173 1.1 uwe pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole)
174 1.1 uwe struct pckbc_js_softc *jsc;
175 1.1 uwe bus_space_tag_t iot;
176 1.1 uwe bus_addr_t ioaddr;
177 1.1 uwe int intr;
178 1.1 uwe int isconsole;
179 1.1 uwe {
180 1.1 uwe struct pckbc_softc *sc = (struct pckbc_softc *)jsc;
181 1.1 uwe struct pckbc_internal *t;
182 1.1 uwe
183 1.1 uwe jsc->jsc_pckbc.intr_establish = pckbc_js_intr_establish;
184 1.1 uwe jsc->jsc_intr = intr;
185 1.1 uwe jsc->jsc_establised = 0;
186 1.1 uwe
187 1.1 uwe if (isconsole) {
188 1.1 uwe int status;
189 1.1 uwe
190 1.1 uwe status = pckbc_cnattach(iot, ioaddr, KBCMDP, PCKBC_KBD_SLOT);
191 1.1 uwe if (status == 0)
192 1.1 uwe printf(": cnattach ok");
193 1.1 uwe else
194 1.1 uwe printf(": cnattach %d", status);
195 1.1 uwe }
196 1.1 uwe
197 1.1 uwe if (pckbc_is_console(iot, ioaddr)) {
198 1.1 uwe t = &pckbc_consdata;
199 1.1 uwe pckbc_console_attached = 1;
200 1.1 uwe } else {
201 1.1 uwe bus_space_handle_t ioh_d, ioh_c;
202 1.1 uwe
203 1.1 uwe if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) {
204 1.1 uwe printf(": unable to map data register\n");
205 1.1 uwe return;
206 1.1 uwe }
207 1.1 uwe
208 1.1 uwe if (bus_space_map(iot, ioaddr + KBCMDP, 1, 0, &ioh_c) != 0) {
209 1.1 uwe bus_space_unmap(iot, ioh_d, 1);
210 1.1 uwe printf(": unable to map cmd register\n");
211 1.1 uwe return;
212 1.1 uwe }
213 1.1 uwe
214 1.1 uwe t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
215 1.1 uwe memset(t, 0, sizeof(struct pckbc_internal));
216 1.1 uwe t->t_iot = iot;
217 1.1 uwe t->t_ioh_d = ioh_d;
218 1.1 uwe t->t_ioh_c = ioh_c;
219 1.1 uwe t->t_addr = ioaddr;
220 1.1 uwe t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */
221 1.1 uwe callout_init(&t->t_cleanup);
222 1.1 uwe
223 1.7.6.1 skrll (void) pckbc_poll_data1(t, PCKBC_KBD_SLOT); /* flush */
224 1.1 uwe
225 1.1 uwe if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
226 1.1 uwe printf(": unable to request self test");
227 1.1 uwe else {
228 1.1 uwe int response;
229 1.1 uwe
230 1.7.6.1 skrll response = pckbc_poll_data1(t, PCKBC_KBD_SLOT);
231 1.1 uwe if (response == 0x55)
232 1.1 uwe printf(": selftest ok");
233 1.1 uwe else
234 1.1 uwe printf(": selftest failed (0x%02x)", response);
235 1.1 uwe }
236 1.1 uwe }
237 1.1 uwe
238 1.1 uwe /* crosslink */
239 1.1 uwe t->t_sc = sc;
240 1.1 uwe sc->id = t;
241 1.1 uwe
242 1.1 uwe /* finish off the attach */
243 1.1 uwe printf("\n");
244 1.1 uwe pckbc_attach(sc);
245 1.1 uwe }
246 1.1 uwe
247 1.1 uwe
248 1.1 uwe /*
249 1.1 uwe * Keyboard and mouse share the interrupt
250 1.1 uwe * so don't install interrupt handler twice.
251 1.1 uwe */
252 1.1 uwe static void
253 1.1 uwe pckbc_js_intr_establish(sc, slot)
254 1.1 uwe struct pckbc_softc *sc;
255 1.7.6.1 skrll pckbport_slot_t slot;
256 1.1 uwe {
257 1.1 uwe struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)sc;
258 1.1 uwe void *res;
259 1.1 uwe
260 1.1 uwe if (jsc->jsc_establised) {
261 1.1 uwe #ifdef DEBUG
262 1.1 uwe printf("%s: %s slot shares interrupt (already established)\n",
263 1.1 uwe sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
264 1.1 uwe #endif
265 1.1 uwe return;
266 1.1 uwe }
267 1.1 uwe
268 1.7.6.1 skrll /*
269 1.7.6.1 skrll * We can not choose the devic class interruptlevel freely,
270 1.7.6.1 skrll * so we debounce via a softinterrupt.
271 1.7.6.1 skrll */
272 1.7.6.1 skrll jsc->jsc_int_cookie = softintr_establish(IPL_JSCKBD,
273 1.7.6.1 skrll pckbcintr_soft, &jsc->jsc_pckbc);
274 1.7.6.1 skrll if (jsc->jsc_int_cookie == NULL) {
275 1.7.6.1 skrll printf("%s: unable to establish %s soft interrupt\n",
276 1.7.6.1 skrll sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
277 1.7.6.1 skrll return;
278 1.7.6.1 skrll }
279 1.1 uwe res = bus_intr_establish(sc->id->t_iot, jsc->jsc_intr,
280 1.7.6.1 skrll IPL_SERIAL, jsc_pckbdintr, jsc);
281 1.1 uwe if (res == NULL)
282 1.1 uwe printf("%s: unable to establish %s slot interrupt\n",
283 1.1 uwe sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
284 1.1 uwe else
285 1.1 uwe jsc->jsc_establised = 1;
286 1.1 uwe }
287 1.1 uwe
288 1.7.6.1 skrll static int
289 1.7.6.1 skrll jsc_pckbdintr(void *vsc)
290 1.7.6.1 skrll {
291 1.7.6.1 skrll struct pckbc_js_softc *jsc = vsc;
292 1.7.6.1 skrll
293 1.7.6.1 skrll softintr_schedule(jsc->jsc_int_cookie);
294 1.7.6.1 skrll pckbcintr_hard(&jsc->jsc_pckbc);
295 1.7.6.1 skrll /*
296 1.7.6.1 skrll * This interrupt is not shared on javastations, avoid "stray"
297 1.7.6.1 skrll * warnings. XXX - why do "stray interrupt" warnings happen if
298 1.7.6.1 skrll * we don't claim the interrupt always?
299 1.7.6.1 skrll */
300 1.7.6.1 skrll return 1;
301 1.7.6.1 skrll }
302 1.1 uwe
303 1.1 uwe /*
304 1.1 uwe * MD hook for use without MI wscons.
305 1.1 uwe * Called by pckbc_cnattach().
306 1.1 uwe */
307 1.1 uwe int
308 1.7.6.1 skrll pckbport_machdep_cnattach(constag, slot)
309 1.7.6.1 skrll pckbport_tag_t constag;
310 1.7.6.1 skrll pckbport_slot_t slot;
311 1.1 uwe {
312 1.1 uwe
313 1.1 uwe return (0);
314 1.1 uwe }
315