kbd_zs.c revision 1.14 1 /* $NetBSD: kbd_zs.c,v 1.14 2002/10/03 16:13:26 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.14 2002/10/03 16:13:26 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 <machine/vuid_event.h>
78 #include <machine/kbd.h>
79 #include <dev/sun/event_var.h>
80 #include <dev/sun/kbd_xlate.h>
81 #include <dev/sun/kbdvar.h>
82 #include <dev/sun/kbdsunvar.h>
83
84 /****************************************************************
85 * Interface to the lower layer (zscc)
86 ****************************************************************/
87
88 static void kbd_zs_rxint __P((struct zs_chanstate *));
89 static void kbd_zs_stint __P((struct zs_chanstate *, int));
90 static void kbd_zs_txint __P((struct zs_chanstate *));
91 static void kbd_zs_softint __P((struct zs_chanstate *));
92
93 struct zsops zsops_kbd = {
94 kbd_zs_rxint, /* receive char available */
95 kbd_zs_stint, /* external/status */
96 kbd_zs_txint, /* xmit buffer empty */
97 kbd_zs_softint, /* process software interrupt */
98 };
99
100 static int kbd_zs_match(struct device *, struct cfdata *, void *);
101 static void kbd_zs_attach(struct device *, struct device *, void *);
102 static void kbd_zs_write_data __P((struct kbd_sun_softc *, int));
103
104 CFATTACH_DECL(kbd_zs, sizeof(struct kbd_sun_softc),
105 kbd_zs_match, kbd_zs_attach, NULL, NULL);
106
107 /* Fall-back baud rate */
108 int kbd_zs_bps = KBD_DEFAULT_BPS;
109
110 /*
111 * kbd_zs_match: how is this zs channel configured?
112 */
113 int
114 kbd_zs_match(parent, cf, aux)
115 struct device *parent;
116 struct cfdata *cf;
117 void *aux;
118 {
119 struct zsc_attach_args *args = aux;
120
121 /* Exact match required for keyboard. */
122 if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
123 return 2;
124
125 return 0;
126 }
127
128 void
129 kbd_zs_attach(parent, self, aux)
130 struct device *parent, *self;
131 void *aux;
132
133 {
134 struct zsc_softc *zsc = (void *) parent;
135 struct kbd_sun_softc *k = (void *) self;
136 struct zsc_attach_args *args = aux;
137 struct zs_chanstate *cs;
138 int channel;
139 int reset, s;
140 int bps;
141
142 /* provide upper layer with a link to the middle layer */
143 k->k_kbd.k_ops = &kbd_ops_sun;
144
145 /* provide middle layer with a link to the lower layer (i.e. us) */
146 channel = args->channel;
147 cs = zsc->zsc_cs[channel];
148 cs->cs_private = k;
149 cs->cs_ops = &zsops_kbd;
150 k->k_cs = cs;
151 k->k_write_data = kbd_zs_write_data;
152
153 if ((bps = cs->cs_defspeed) == 0)
154 bps = kbd_zs_bps;
155
156 printf(": baud rate %d", bps);
157
158 if ((args->hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
159 /*
160 * Hookup ourselves as the console input channel
161 */
162 struct cons_channel *cc;
163
164 if ((cc = malloc(sizeof *cc, M_DEVBUF, M_NOWAIT)) == NULL)
165 return;
166
167 cc->cc_dev = self;
168 cc->cc_iopen = kbd_cc_open;
169 cc->cc_iclose = kbd_cc_close;
170 cc->cc_upstream = NULL;
171 cons_attach_input(cc, args->consdev);
172 k->k_kbd.k_cc = cc;
173 k->k_kbd.k_isconsole = 1;
174 printf(" (console input)");
175 }
176 printf("\n");
177
178 callout_init(&k->k_repeat_ch);
179
180 /* Initialize the speed, etc. */
181 s = splzs();
182 if (k->k_kbd.k_isconsole == 0) {
183 /* Not the console; may need reset. */
184 reset = (channel == 0) ?
185 ZSWR9_A_RESET : ZSWR9_B_RESET;
186 zs_write_reg(cs, 9, reset);
187 }
188 /* These are OK as set by zscc: WR3, WR4, WR5 */
189 /* We don't care about status interrupts. */
190 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE;
191 (void) zs_set_speed(cs, bps);
192 zs_loadchannelregs(cs);
193 splx(s);
194
195 /* Do this before any calls to kbd_rint(). */
196 kbd_xlate_init(&k->k_kbd.k_state);
197
198 /* XXX - Do this in open? */
199 k->k_repeat_start = hz/2;
200 k->k_repeat_step = hz/20;
201
202 /* Magic sequence. */
203 k->k_magic1 = KBD_L1;
204 k->k_magic2 = KBD_A;
205 }
206
207 /*
208 * used by kbd_sun_start_tx();
209 */
210 void
211 kbd_zs_write_data(k, c)
212 struct kbd_sun_softc *k;
213 int c;
214 {
215 int s;
216
217 /* Need splzs to avoid interruption of the delay. */
218 s = splzs();
219 zs_write_data(k->k_cs, c);
220 splx(s);
221 }
222
223 static void
224 kbd_zs_rxint(cs)
225 struct zs_chanstate *cs;
226 {
227 struct kbd_sun_softc *k;
228 int put, put_next;
229 u_char c, rr1;
230
231 k = cs->cs_private;
232 put = k->k_rbput;
233
234 /*
235 * First read the status, because reading the received char
236 * destroys the status of this char.
237 */
238 rr1 = zs_read_reg(cs, 1);
239 c = zs_read_data(cs);
240
241 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
242 /* Clear the receive error. */
243 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
244 }
245
246 /*
247 * Check NOW for a console abort sequence, so that we can
248 * abort even when interrupts are locking up the machine.
249 */
250 if (k->k_magic1_down) {
251 /* The last keycode was "MAGIC1" down. */
252 k->k_magic1_down = 0;
253 if (c == k->k_magic2) {
254 /* Magic "L1-A" sequence; enter debugger. */
255 if (k->k_kbd.k_isconsole) {
256 zs_abort(cs);
257 /* Debugger done. Fake L1-up to finish it. */
258 c = k->k_magic1 | KBD_UP;
259 } else {
260 printf("kbd: magic sequence, but not console\n");
261 }
262 }
263 }
264 if (c == k->k_magic1) {
265 k->k_magic1_down = 1;
266 }
267
268 k->k_rbuf[put] = (c << 8) | rr1;
269 put_next = (put + 1) & KBD_RX_RING_MASK;
270
271 /* Would overrun if increment makes (put==get). */
272 if (put_next == k->k_rbget) {
273 k->k_intr_flags |= INTR_RX_OVERRUN;
274 } else {
275 /* OK, really increment. */
276 put = put_next;
277 }
278
279 /* Done reading. */
280 k->k_rbput = put;
281
282 /* Ask for softint() call. */
283 cs->cs_softreq = 1;
284 }
285
286
287 static void
288 kbd_zs_txint(cs)
289 struct zs_chanstate *cs;
290 {
291 struct kbd_sun_softc *k;
292
293 k = cs->cs_private;
294 zs_write_csr(cs, ZSWR0_RESET_TXINT);
295 k->k_intr_flags |= INTR_TX_EMPTY;
296 /* Ask for softint() call. */
297 cs->cs_softreq = 1;
298 }
299
300
301 static void
302 kbd_zs_stint(cs, force)
303 struct zs_chanstate *cs;
304 int force;
305 {
306 struct kbd_sun_softc *k;
307 int rr0;
308
309 k = cs->cs_private;
310
311 rr0 = zs_read_csr(cs);
312 zs_write_csr(cs, ZSWR0_RESET_STATUS);
313
314 #if 0
315 if (rr0 & ZSRR0_BREAK) {
316 /* Keyboard unplugged? */
317 zs_abort(cs);
318 return (0);
319 }
320 #endif
321
322 /*
323 * We have to accumulate status line changes here.
324 * Otherwise, if we get multiple status interrupts
325 * before the softint runs, we could fail to notice
326 * some status line changes in the softint routine.
327 * Fix from Bill Studenmund, October 1996.
328 */
329 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
330 cs->cs_rr0 = rr0;
331 k->k_intr_flags |= INTR_ST_CHECK;
332
333 /* Ask for softint() call. */
334 cs->cs_softreq = 1;
335 }
336
337 /*
338 * Get input from the receive ring and pass it on.
339 * Note: this is called at splsoftclock()
340 */
341 static void
342 kbd_zs_softint(cs)
343 struct zs_chanstate *cs;
344 {
345 struct kbd_sun_softc *k;
346 int get, c, s;
347 int intr_flags;
348 u_short ring_data;
349
350 k = cs->cs_private;
351
352 /* Atomically get and clear flags. */
353 s = splzs();
354 intr_flags = k->k_intr_flags;
355 k->k_intr_flags = 0;
356
357 /* Now lower to spltty for the rest. */
358 (void) spltty();
359
360 /*
361 * Copy data from the receive ring to the event layer.
362 */
363 get = k->k_rbget;
364 while (get != k->k_rbput) {
365 ring_data = k->k_rbuf[get];
366 get = (get + 1) & KBD_RX_RING_MASK;
367
368 /* low byte of ring_data is rr1 */
369 c = (ring_data >> 8) & 0xff;
370
371 if (ring_data & ZSRR1_DO)
372 intr_flags |= INTR_RX_OVERRUN;
373 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
374 /*
375 * After garbage, flush pending input, and
376 * send a reset to resync key translation.
377 */
378 log(LOG_ERR, "%s: input error (0x%x)\n",
379 k->k_kbd.k_dev.dv_xname, ring_data);
380 get = k->k_rbput; /* flush */
381 goto send_reset;
382 }
383
384 /* Pass this up to the "middle" layer. */
385 kbd_sun_input(k, c);
386 }
387 if (intr_flags & INTR_RX_OVERRUN) {
388 log(LOG_ERR, "%s: input overrun\n",
389 k->k_kbd.k_dev.dv_xname);
390 send_reset:
391 /* Send a reset to resync translation. */
392 kbd_sun_output(k, KBD_CMD_RESET);
393 kbd_sun_start_tx(k);
394 }
395 k->k_rbget = get;
396
397 if (intr_flags & INTR_TX_EMPTY) {
398 /*
399 * Transmit done. Try to send more, or
400 * clear busy and wakeup drain waiters.
401 */
402 k->k_txflags &= ~K_TXBUSY;
403 kbd_sun_start_tx(k);
404 }
405
406 if (intr_flags & INTR_ST_CHECK) {
407 /*
408 * Status line change. (Not expected.)
409 */
410 log(LOG_ERR, "%s: status interrupt?\n",
411 k->k_kbd.k_dev.dv_xname);
412 cs->cs_rr0_delta = 0;
413 }
414
415 splx(s);
416 }
417