kbd_zs.c revision 1.15 1 /* $NetBSD: kbd_zs.c,v 1.15 2002/10/21 15:36:35 uwe Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * @(#)kbd.c 8.2 (Berkeley) 10/30/93
45 */
46
47 /*
48 * /dev/kbd lower layer for sun keyboard off a zs channel.
49 * This driver uses kbdsun middle layer to hook up to /dev/kbd.
50 */
51
52 /*
53 * Zilog Z8530 Dual UART driver (keyboard interface)
54 *
55 * This is the 8530 portion of the driver that will be attached to
56 * the "zsc" driver for a Sun keyboard.
57 */
58
59 #include <sys/cdefs.h>
60 __KERNEL_RCSID(0, "$NetBSD: kbd_zs.c,v 1.15 2002/10/21 15:36:35 uwe Exp $");
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/conf.h>
65 #include <sys/device.h>
66 #include <sys/kernel.h>
67 #include <sys/malloc.h>
68 #include <sys/proc.h>
69 #include <sys/signal.h>
70 #include <sys/signalvar.h>
71 #include <sys/time.h>
72 #include <sys/select.h>
73 #include <sys/syslog.h>
74
75 #include <dev/ic/z8530reg.h>
76 #include <machine/z8530var.h>
77 #include <dev/sun/vuid_event.h>
78 #include <dev/sun/event_var.h>
79 #include <dev/sun/kbd_reg.h>
80 #include <dev/sun/kbd_xlate.h>
81 #include <dev/sun/kbdvar.h>
82 #include <dev/sun/kbdsunvar.h>
83
84
85 /****************************************************************
86 * Interface to the lower layer (zscc)
87 ****************************************************************/
88
89 static void kbd_zs_rxint __P((struct zs_chanstate *));
90 static void kbd_zs_stint __P((struct zs_chanstate *, int));
91 static void kbd_zs_txint __P((struct zs_chanstate *));
92 static void kbd_zs_softint __P((struct zs_chanstate *));
93
94 struct zsops zsops_kbd = {
95 kbd_zs_rxint, /* receive char available */
96 kbd_zs_stint, /* external/status */
97 kbd_zs_txint, /* xmit buffer empty */
98 kbd_zs_softint, /* process software interrupt */
99 };
100
101 static int kbd_zs_match(struct device *, struct cfdata *, void *);
102 static void kbd_zs_attach(struct device *, struct device *, void *);
103 static void kbd_zs_write_data __P((struct kbd_sun_softc *, int));
104
105 CFATTACH_DECL(kbd_zs, sizeof(struct kbd_sun_softc),
106 kbd_zs_match, kbd_zs_attach, NULL, NULL);
107
108 /* Fall-back baud rate */
109 int kbd_zs_bps = KBD_DEFAULT_BPS;
110
111 /*
112 * kbd_zs_match: how is this zs channel configured?
113 */
114 int
115 kbd_zs_match(parent, cf, aux)
116 struct device *parent;
117 struct cfdata *cf;
118 void *aux;
119 {
120 struct zsc_attach_args *args = aux;
121
122 /* Exact match required for keyboard. */
123 if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
124 return 2;
125
126 return 0;
127 }
128
129 void
130 kbd_zs_attach(parent, self, aux)
131 struct device *parent, *self;
132 void *aux;
133
134 {
135 struct zsc_softc *zsc = (void *) parent;
136 struct kbd_sun_softc *k = (void *) self;
137 struct zsc_attach_args *args = aux;
138 struct zs_chanstate *cs;
139 int channel;
140 int reset, s;
141 int bps;
142
143 /* provide upper layer with a link to the middle layer */
144 k->k_kbd.k_ops = &kbd_ops_sun;
145
146 /* provide middle layer with a link to the lower layer (i.e. us) */
147 channel = args->channel;
148 cs = zsc->zsc_cs[channel];
149 cs->cs_private = k;
150 cs->cs_ops = &zsops_kbd;
151 k->k_cs = cs;
152 k->k_write_data = kbd_zs_write_data;
153
154 if ((bps = cs->cs_defspeed) == 0)
155 bps = kbd_zs_bps;
156
157 printf(": baud rate %d", bps);
158
159 if ((args->hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
160 /*
161 * Hookup ourselves as the console input channel
162 */
163 struct cons_channel *cc = kbd_cc_alloc(&k->k_kbd);
164
165 if (cc == NULL)
166 return;
167
168 cons_attach_input(cc, args->consdev);
169 k->k_kbd.k_isconsole = 1;
170 printf(" (console input)");
171 }
172 printf("\n");
173
174 /* Initialize the speed, etc. */
175 s = splzs();
176 if (k->k_kbd.k_isconsole == 0) {
177 /* Not the console; may need reset. */
178 reset = (channel == 0) ?
179 ZSWR9_A_RESET : ZSWR9_B_RESET;
180 zs_write_reg(cs, 9, reset);
181 }
182 /* These are OK as set by zscc: WR3, WR4, WR5 */
183 /* We don't care about status interrupts. */
184 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE;
185 (void) zs_set_speed(cs, bps);
186 zs_loadchannelregs(cs);
187 splx(s);
188
189 /* Do this before any calls to kbd_rint(). */
190 kbd_xlate_init(&k->k_kbd.k_state);
191
192 /* Magic sequence. */
193 k->k_magic1 = KBD_L1;
194 k->k_magic2 = KBD_A;
195 }
196
197 /*
198 * used by kbd_sun_start_tx();
199 */
200 void
201 kbd_zs_write_data(k, c)
202 struct kbd_sun_softc *k;
203 int c;
204 {
205 int s;
206
207 /* Need splzs to avoid interruption of the delay. */
208 s = splzs();
209 zs_write_data(k->k_cs, c);
210 splx(s);
211 }
212
213 static void
214 kbd_zs_rxint(cs)
215 struct zs_chanstate *cs;
216 {
217 struct kbd_sun_softc *k;
218 int put, put_next;
219 u_char c, rr1;
220
221 k = cs->cs_private;
222 put = k->k_rbput;
223
224 /*
225 * First read the status, because reading the received char
226 * destroys the status of this char.
227 */
228 rr1 = zs_read_reg(cs, 1);
229 c = zs_read_data(cs);
230
231 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
232 /* Clear the receive error. */
233 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
234 }
235
236 /*
237 * Check NOW for a console abort sequence, so that we can
238 * abort even when interrupts are locking up the machine.
239 */
240 if (k->k_magic1_down) {
241 /* The last keycode was "MAGIC1" down. */
242 k->k_magic1_down = 0;
243 if (c == k->k_magic2) {
244 /* Magic "L1-A" sequence; enter debugger. */
245 if (k->k_kbd.k_isconsole) {
246 zs_abort(cs);
247 /* Debugger done. Fake L1-up to finish it. */
248 c = k->k_magic1 | KBD_UP;
249 } else {
250 printf("kbd: magic sequence, but not console\n");
251 }
252 }
253 }
254 if (c == k->k_magic1) {
255 k->k_magic1_down = 1;
256 }
257
258 k->k_rbuf[put] = (c << 8) | rr1;
259 put_next = (put + 1) & KBD_RX_RING_MASK;
260
261 /* Would overrun if increment makes (put==get). */
262 if (put_next == k->k_rbget) {
263 k->k_intr_flags |= INTR_RX_OVERRUN;
264 } else {
265 /* OK, really increment. */
266 put = put_next;
267 }
268
269 /* Done reading. */
270 k->k_rbput = put;
271
272 /* Ask for softint() call. */
273 cs->cs_softreq = 1;
274 }
275
276
277 static void
278 kbd_zs_txint(cs)
279 struct zs_chanstate *cs;
280 {
281 struct kbd_sun_softc *k;
282
283 k = cs->cs_private;
284 zs_write_csr(cs, ZSWR0_RESET_TXINT);
285 k->k_intr_flags |= INTR_TX_EMPTY;
286 /* Ask for softint() call. */
287 cs->cs_softreq = 1;
288 }
289
290
291 static void
292 kbd_zs_stint(cs, force)
293 struct zs_chanstate *cs;
294 int force;
295 {
296 struct kbd_sun_softc *k;
297 int rr0;
298
299 k = cs->cs_private;
300
301 rr0 = zs_read_csr(cs);
302 zs_write_csr(cs, ZSWR0_RESET_STATUS);
303
304 #if 0
305 if (rr0 & ZSRR0_BREAK) {
306 /* Keyboard unplugged? */
307 zs_abort(cs);
308 return (0);
309 }
310 #endif
311
312 /*
313 * We have to accumulate status line changes here.
314 * Otherwise, if we get multiple status interrupts
315 * before the softint runs, we could fail to notice
316 * some status line changes in the softint routine.
317 * Fix from Bill Studenmund, October 1996.
318 */
319 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
320 cs->cs_rr0 = rr0;
321 k->k_intr_flags |= INTR_ST_CHECK;
322
323 /* Ask for softint() call. */
324 cs->cs_softreq = 1;
325 }
326
327 /*
328 * Get input from the receive ring and pass it on.
329 * Note: this is called at splsoftclock()
330 */
331 static void
332 kbd_zs_softint(cs)
333 struct zs_chanstate *cs;
334 {
335 struct kbd_sun_softc *k;
336 int get, c, s;
337 int intr_flags;
338 u_short ring_data;
339
340 k = cs->cs_private;
341
342 /* Atomically get and clear flags. */
343 s = splzs();
344 intr_flags = k->k_intr_flags;
345 k->k_intr_flags = 0;
346
347 /* Now lower to spltty for the rest. */
348 (void) spltty();
349
350 /*
351 * Copy data from the receive ring to the event layer.
352 */
353 get = k->k_rbget;
354 while (get != k->k_rbput) {
355 ring_data = k->k_rbuf[get];
356 get = (get + 1) & KBD_RX_RING_MASK;
357
358 /* low byte of ring_data is rr1 */
359 c = (ring_data >> 8) & 0xff;
360
361 if (ring_data & ZSRR1_DO)
362 intr_flags |= INTR_RX_OVERRUN;
363 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
364 /*
365 * After garbage, flush pending input, and
366 * send a reset to resync key translation.
367 */
368 log(LOG_ERR, "%s: input error (0x%x)\n",
369 k->k_kbd.k_dev.dv_xname, ring_data);
370 get = k->k_rbput; /* flush */
371 goto send_reset;
372 }
373
374 /* Pass this up to the "middle" layer. */
375 kbd_sun_input(k, c);
376 }
377 if (intr_flags & INTR_RX_OVERRUN) {
378 log(LOG_ERR, "%s: input overrun\n",
379 k->k_kbd.k_dev.dv_xname);
380 send_reset:
381 /* Send a reset to resync translation. */
382 kbd_sun_output(k, KBD_CMD_RESET);
383 kbd_sun_start_tx(k);
384 }
385 k->k_rbget = get;
386
387 if (intr_flags & INTR_TX_EMPTY) {
388 /*
389 * Transmit done. Try to send more, or
390 * clear busy and wakeup drain waiters.
391 */
392 k->k_txflags &= ~K_TXBUSY;
393 kbd_sun_start_tx(k);
394 }
395
396 if (intr_flags & INTR_ST_CHECK) {
397 /*
398 * Status line change. (Not expected.)
399 */
400 log(LOG_ERR, "%s: status interrupt?\n",
401 k->k_kbd.k_dev.dv_xname);
402 cs->cs_rr0_delta = 0;
403 }
404
405 splx(s);
406 }
407