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