xref: /netbsd/sys/dev/sun/kbd_zs.c (revision bf9ec67e)
1 /*	$NetBSD: kbd_zs.c,v 1.10 2001/12/09 12:03:32 pk 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  * Keyboard driver (/dev/kbd -- note that we do not have minor numbers
49  * [yet?]).  Translates incoming bytes to ASCII or to `firm_events' and
50  * passes them up to the appropriate reader.
51  */
52 
53 /*
54  * Zilog Z8530 Dual UART driver (keyboard interface)
55  *
56  * This is the 8530 portion of the driver that will be attached to
57  * the "zsc" driver for a Sun keyboard.
58  */
59 
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: kbd_zs.c,v 1.10 2001/12/09 12:03:32 pk Exp $");
62 
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/conf.h>
66 #include <sys/device.h>
67 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/proc.h>
70 #include <sys/signal.h>
71 #include <sys/signalvar.h>
72 #include <sys/time.h>
73 #include <sys/select.h>
74 #include <sys/syslog.h>
75 
76 #include <dev/ic/z8530reg.h>
77 #include <machine/z8530var.h>
78 #include <machine/vuid_event.h>
79 #include <machine/kbd.h>
80 #include <dev/sun/event_var.h>
81 #include <dev/sun/kbd_xlate.h>
82 #include <dev/sun/kbdvar.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_softc *, int));
103 
104 struct cfattach kbd_zs_ca = {
105 	sizeof(struct kbd_softc), kbd_zs_match, kbd_zs_attach
106 };
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_softc *k = (void *) self;
137 	struct zsc_attach_args *args = aux;
138 	struct zs_chanstate *cs;
139 	struct cfdata *cf;
140 	int channel, kbd_unit;
141 	int reset, s;
142 	int bps;
143 
144 	cf = k->k_dev.dv_cfdata;
145 	kbd_unit = k->k_dev.dv_unit;
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 	if ((bps = cs->cs_defspeed) == 0)
153 		bps = kbd_zs_bps;
154 
155 	printf(": baud rate %d", bps);
156 
157 	if ((args->hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
158 		/*
159 		 * Hookup ourselves as the console input channel
160 		 */
161 		struct cons_channel *cc;
162 
163 		if ((cc = malloc(sizeof *cc, M_DEVBUF, M_NOWAIT)) == NULL)
164 			return;
165 
166 		cc->cc_dev = self;
167 		cc->cc_iopen = kbd_cc_open;
168 		cc->cc_iclose = kbd_cc_close;
169 		cc->cc_upstream = NULL;
170 		cons_attach_input(cc, args->consdev);
171 		k->k_cc = cc;
172 		k->k_isconsole = 1;
173 		printf(" (console input)");
174 	}
175 	printf("\n");
176 
177 	callout_init(&k->k_repeat_ch);
178 
179 	/* Initialize the speed, etc. */
180 	s = splzs();
181 	if (k->k_isconsole == 0) {
182 		/* Not the console; may need reset. */
183 		reset = (channel == 0) ?
184 			ZSWR9_A_RESET : ZSWR9_B_RESET;
185 		zs_write_reg(cs, 9, reset);
186 	}
187 	/* These are OK as set by zscc: WR3, WR4, WR5 */
188 	/* We don't care about status interrupts. */
189 	cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE;
190 	(void) zs_set_speed(cs, bps);
191 	zs_loadchannelregs(cs);
192 	splx(s);
193 
194 	/* Do this before any calls to kbd_rint(). */
195 	kbd_xlate_init(&k->k_state);
196 
197 	/* XXX - Do this in open? */
198 	k->k_repeat_start = hz/2;
199 	k->k_repeat_step = hz/20;
200 
201 	/* Magic sequence. */
202 	k->k_magic1 = KBD_L1;
203 	k->k_magic2 = KBD_A;
204 }
205 
206 /*
207  * used by kbd_start_tx();
208  */
209 void
210 kbd_zs_write_data(k, c)
211 	struct kbd_softc *k;
212 	int c;
213 {
214 	int	s;
215 
216 	/* Need splzs to avoid interruption of the delay. */
217 	s = splzs();
218 	zs_write_data(k->k_cs, c);
219 	splx(s);
220 }
221 
222 static void
223 kbd_zs_rxint(cs)
224 	struct zs_chanstate *cs;
225 {
226 	struct kbd_softc *k;
227 	int put, put_next;
228 	u_char c, rr1;
229 
230 	k = cs->cs_private;
231 	put = k->k_rbput;
232 
233 	/*
234 	 * First read the status, because reading the received char
235 	 * destroys the status of this char.
236 	 */
237 	rr1 = zs_read_reg(cs, 1);
238 	c = zs_read_data(cs);
239 
240 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
241 		/* Clear the receive error. */
242 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
243 	}
244 
245 	/*
246 	 * Check NOW for a console abort sequence, so that we can
247 	 * abort even when interrupts are locking up the machine.
248 	 */
249 	if (k->k_magic1_down) {
250 		/* The last keycode was "MAGIC1" down. */
251 		k->k_magic1_down = 0;
252 		if (c == k->k_magic2) {
253 			/* Magic "L1-A" sequence; enter debugger. */
254 			if (k->k_isconsole) {
255 				zs_abort(cs);
256 				/* Debugger done.  Fake L1-up to finish it. */
257 				c = k->k_magic1 | KBD_UP;
258 			} else {
259 				printf("kbd: magic sequence, but not console\n");
260 			}
261 		}
262 	}
263 	if (c == k->k_magic1) {
264 		k->k_magic1_down = 1;
265 	}
266 
267 	k->k_rbuf[put] = (c << 8) | rr1;
268 	put_next = (put + 1) & KBD_RX_RING_MASK;
269 
270 	/* Would overrun if increment makes (put==get). */
271 	if (put_next == k->k_rbget) {
272 		k->k_intr_flags |= INTR_RX_OVERRUN;
273 	} else {
274 		/* OK, really increment. */
275 		put = put_next;
276 	}
277 
278 	/* Done reading. */
279 	k->k_rbput = put;
280 
281 	/* Ask for softint() call. */
282 	cs->cs_softreq = 1;
283 }
284 
285 
286 static void
287 kbd_zs_txint(cs)
288 	struct zs_chanstate *cs;
289 {
290 	struct kbd_softc *k;
291 
292 	k = cs->cs_private;
293 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
294 	k->k_intr_flags |= INTR_TX_EMPTY;
295 	/* Ask for softint() call. */
296 	cs->cs_softreq = 1;
297 }
298 
299 
300 static void
301 kbd_zs_stint(cs, force)
302 	struct zs_chanstate *cs;
303 	int force;
304 {
305 	struct kbd_softc *k;
306 	int rr0;
307 
308 	k = cs->cs_private;
309 
310 	rr0 = zs_read_csr(cs);
311 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
312 
313 #if 0
314 	if (rr0 & ZSRR0_BREAK) {
315 		/* Keyboard unplugged? */
316 		zs_abort(cs);
317 		return (0);
318 	}
319 #endif
320 
321 	/*
322 	 * We have to accumulate status line changes here.
323 	 * Otherwise, if we get multiple status interrupts
324 	 * before the softint runs, we could fail to notice
325 	 * some status line changes in the softint routine.
326 	 * Fix from Bill Studenmund, October 1996.
327 	 */
328 	cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
329 	cs->cs_rr0 = rr0;
330 	k->k_intr_flags |= INTR_ST_CHECK;
331 
332 	/* Ask for softint() call. */
333 	cs->cs_softreq = 1;
334 }
335 
336 /*
337  * Get input from the receive ring and pass it on.
338  * Note: this is called at splsoftclock()
339  */
340 static void
341 kbd_zs_softint(cs)
342 	struct zs_chanstate *cs;
343 {
344 	struct kbd_softc *k;
345 	int get, c, s;
346 	int intr_flags;
347 	u_short ring_data;
348 
349 	k = cs->cs_private;
350 
351 	/* Atomically get and clear flags. */
352 	s = splzs();
353 	intr_flags = k->k_intr_flags;
354 	k->k_intr_flags = 0;
355 
356 	/* Now lower to spltty for the rest. */
357 	(void) spltty();
358 
359 	/*
360 	 * Copy data from the receive ring to the event layer.
361 	 */
362 	get = k->k_rbget;
363 	while (get != k->k_rbput) {
364 		ring_data = k->k_rbuf[get];
365 		get = (get + 1) & KBD_RX_RING_MASK;
366 
367 		/* low byte of ring_data is rr1 */
368 		c = (ring_data >> 8) & 0xff;
369 
370 		if (ring_data & ZSRR1_DO)
371 			intr_flags |= INTR_RX_OVERRUN;
372 		if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
373 			/*
374 			 * After garbage, flush pending input, and
375 			 * send a reset to resync key translation.
376 			 */
377 			log(LOG_ERR, "%s: input error (0x%x)\n",
378 				k->k_dev.dv_xname, ring_data);
379 			get = k->k_rbput; /* flush */
380 			goto send_reset;
381 		}
382 
383 		/* Pass this up to the "middle" layer. */
384 		kbd_input_raw(k, c);
385 	}
386 	if (intr_flags & INTR_RX_OVERRUN) {
387 		log(LOG_ERR, "%s: input overrun\n",
388 		    k->k_dev.dv_xname);
389 	send_reset:
390 		/* Send a reset to resync translation. */
391 		kbd_output(k, KBD_CMD_RESET);
392 		kbd_start_tx(k);
393 	}
394 	k->k_rbget = get;
395 
396 	if (intr_flags & INTR_TX_EMPTY) {
397 		/*
398 		 * Transmit done.  Try to send more, or
399 		 * clear busy and wakeup drain waiters.
400 		 */
401 		k->k_txflags &= ~K_TXBUSY;
402 		kbd_start_tx(k);
403 	}
404 
405 	if (intr_flags & INTR_ST_CHECK) {
406 		/*
407 		 * Status line change.  (Not expected.)
408 		 */
409 		log(LOG_ERR, "%s: status interrupt?\n",
410 		    k->k_dev.dv_xname);
411 		cs->cs_rr0_delta = 0;
412 	}
413 
414 	splx(s);
415 }
416