zs.c revision 1.2 1 /*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * All advertising materials mentioning features or use of this software
10 * must display the following acknowledgement:
11 * This product includes software developed by the University of
12 * California, Lawrence Berkeley Laboratory.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * @(#)zs.c 8.1 (Berkeley) 7/19/93
43 *
44 * from: Header: zs.c,v 1.30 93/07/19 23:44:42 torek Exp
45 * $Id: zs.c,v 1.2 1993/10/11 02:36:44 deraadt Exp $
46 */
47
48 /*
49 * Zilog Z8530 (ZSCC) driver.
50 *
51 * Runs two tty ports (ttya and ttyb) on zs0,
52 * and runs a keyboard and mouse on zs1.
53 *
54 * This driver knows far too much about chip to usage mappings.
55 */
56 #define NZS 2 /* XXX */
57
58 #include <sys/param.h>
59 #include <sys/proc.h>
60 #include <sys/device.h>
61 #include <sys/conf.h>
62 #include <sys/file.h>
63 #include <sys/ioctl.h>
64 #include <sys/tty.h>
65 #include <sys/time.h>
66 #include <sys/kernel.h>
67 #include <sys/syslog.h>
68
69 #include <machine/autoconf.h>
70 #include <machine/cpu.h>
71
72 #include <sparc/sparc/vaddrs.h>
73 #include <sparc/sparc/auxreg.h>
74
75 #include <sparc/dev/kbd.h>
76 #include <sparc/dev/zsreg.h>
77 #include <sparc/dev/zsvar.h>
78
79 #ifdef KGDB
80 #include <machine/remote-sl.h>
81 #endif
82
83 #define ZSMAJOR 12 /* XXX */
84
85 #define ZS_KBD 2 /* XXX */
86 #define ZS_MOUSE 3 /* XXX */
87
88 /* the magic number below was stolen from the Sprite source. */
89 #define PCLK (19660800/4) /* PCLK pin input clock rate */
90
91 /*
92 * Select software interrupt bit based on TTY ipl.
93 */
94 #if PIL_TTY == 1
95 # define IE_ZSSOFT IE_L1
96 #elif PIL_TTY == 4
97 # define IE_ZSSOFT IE_L4
98 #elif PIL_TTY == 6
99 # define IE_ZSSOFT IE_L6
100 #else
101 # error "no suitable software interrupt bit"
102 #endif
103
104 /*
105 * Software state per found chip. This would be called `zs_softc',
106 * but the previous driver had a rather different zs_softc....
107 */
108 struct zsinfo {
109 struct device zi_dev; /* base device */
110 volatile struct zsdevice *zi_zs;/* chip registers */
111 struct zs_chanstate zi_cs[2]; /* channel A and B software state */
112 };
113
114 struct tty *zs_tty[NZS * 2]; /* XXX should be dynamic */
115
116 /* Definition of the driver for autoconfig. */
117 static int zsmatch(struct device *, struct cfdata *, void *);
118 static void zsattach(struct device *, struct device *, void *);
119 struct cfdriver zscd =
120 { NULL, "zs", zsmatch, zsattach, DV_TTY, sizeof(struct zsinfo) };
121
122 /* Interrupt handlers. */
123 static int zshard(void *);
124 static struct intrhand levelhard = { zshard };
125 static int zssoft(void *);
126 static struct intrhand levelsoft = { zssoft };
127
128 struct zs_chanstate *zslist;
129
130 /* Routines called from other code. */
131 static void zsiopen(struct tty *);
132 static void zsiclose(struct tty *);
133 static void zsstart(struct tty *);
134 static void zsstop(struct tty *, int);
135 static int zsparam(struct tty *, struct termios *);
136
137 /* Routines purely local to this driver. */
138 static int zs_getspeed(volatile struct zschan *);
139 static void zs_reset(volatile struct zschan *, int, int);
140 static void zs_modem(struct zs_chanstate *, int);
141 static void zs_loadchannelregs(volatile struct zschan *, u_char *);
142
143 /* Console stuff. */
144 static struct tty *zs_ctty; /* console `struct tty *' */
145 static int zs_consin = -1, zs_consout = -1;
146 static int zscnputc(int); /* console putc function */
147 static volatile struct zschan *zs_conschan;
148 static struct tty *zs_checkcons(struct zsinfo *, int, struct zs_chanstate *);
149
150 #ifdef KGDB
151 /* KGDB stuff. Must reboot to change zs_kgdbunit. */
152 extern int kgdb_dev, kgdb_rate;
153 static int zs_kgdb_savedspeed;
154 static void zs_checkkgdb(int, struct zs_chanstate *, struct tty *);
155 #endif
156
157 extern volatile struct zsdevice *findzs(int);
158 static volatile struct zsdevice *zsaddr[NZS]; /* XXX, but saves work */
159
160 /*
161 * Console keyboard L1-A processing is done in the hardware interrupt code,
162 * so we need to duplicate some of the console keyboard decode state. (We
163 * must not use the regular state as the hardware code keeps ahead of the
164 * software state: the software state tracks the most recent ring input but
165 * the hardware state tracks the most recent ZSCC input.) See also kbd.h.
166 */
167 static struct conk_state { /* console keyboard state */
168 char conk_id; /* true => ID coming up (console only) */
169 char conk_l1; /* true => L1 pressed (console only) */
170 } zsconk_state;
171
172 int zshardscope;
173 int zsshortcuts; /* number of "shortcut" software interrupts */
174
175 /*
176 * Match slave number to zs unit number, so that misconfiguration will
177 * not set up the keyboard as ttya, etc.
178 */
179 static int
180 zsmatch(struct device *parent, struct cfdata *cf, void *aux)
181 {
182 struct romaux *ra = aux;
183
184 return (getpropint(ra->ra_node, "slave", -2) == cf->cf_unit);
185 }
186
187 /*
188 * Attach a found zs.
189 *
190 * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
191 * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
192 */
193 static void
194 zsattach(struct device *parent, struct device *dev, void *aux)
195 {
196 register int zs = dev->dv_unit, unit;
197 register struct zsinfo *zi;
198 register struct zs_chanstate *cs;
199 register volatile struct zsdevice *addr;
200 register struct tty *tp, *ctp;
201 register struct romaux *ra = aux;
202 int pri, softcar;
203 static int didintr, prevpri;
204
205 if ((addr = zsaddr[zs]) == NULL)
206 addr = zsaddr[zs] = findzs(zs);
207 if ((void *)addr != ra->ra_vaddr)
208 panic("zsattach");
209 if (ra->ra_nintr != 1) {
210 printf(": expected 1 interrupt, got %d\n", ra->ra_nintr);
211 return;
212 }
213 pri = ra->ra_intr[0].int_pri;
214 printf(" pri %d, softpri %d\n", pri, PIL_TTY);
215 if (!didintr) {
216 didintr = 1;
217 prevpri = pri;
218 intr_establish(pri, &levelhard);
219 intr_establish(PIL_TTY, &levelsoft);
220 } else if (pri != prevpri)
221 panic("broken zs interrupt scheme");
222 zi = (struct zsinfo *)dev;
223 zi->zi_zs = addr;
224 unit = zs * 2;
225 cs = zi->zi_cs;
226
227 if(!zs_tty[unit])
228 zs_tty[unit] = ttymalloc();
229 tp = zs_tty[unit];
230 if(!zs_tty[unit+1])
231 zs_tty[unit+1] = ttymalloc();
232
233 if (unit == 0) {
234 /* Get software carrier flags from options node in OPENPROM. */
235 extern int optionsnode;
236
237 softcar = 0;
238 if (*getpropstring(optionsnode, "ttya-ignore-cd") == 't')
239 softcar |= 1;
240 if (*getpropstring(optionsnode, "ttyb-ignore-cd") == 't')
241 softcar |= 2;
242 } else
243 softcar = dev->dv_cfdata->cf_flags;
244
245 /* link into interrupt list with order (A,B) (B=A+1) */
246 cs[0].cs_next = &cs[1];
247 cs[1].cs_next = zslist;
248 zslist = cs;
249
250 cs->cs_unit = unit;
251 cs->cs_speed = zs_getspeed(&addr->zs_chan[CHAN_A]);
252 cs->cs_softcar = softcar & 1;
253 cs->cs_zc = &addr->zs_chan[CHAN_A];
254 tp->t_dev = makedev(ZSMAJOR, unit);
255 tp->t_oproc = zsstart;
256 tp->t_param = zsparam;
257 /*tp->t_stop = zsstop;*/
258 if ((ctp = zs_checkcons(zi, unit, cs)) != NULL)
259 tp = ctp;
260 cs->cs_ttyp = tp;
261 #ifdef KGDB
262 if (ctp == NULL)
263 zs_checkkgdb(unit, cs, tp);
264 #endif
265 if (unit == ZS_KBD) {
266 /*
267 * Keyboard: tell /dev/kbd driver how to talk to us.
268 */
269 tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
270 tp->t_cflag = CS8;
271 kbd_serial(tp, zsiopen, zsiclose);
272 cs->cs_conk = 1; /* do L1-A processing */
273 }
274 unit++;
275 cs++;
276 tp = zs_tty[unit];
277 cs->cs_unit = unit;
278 cs->cs_speed = zs_getspeed(&addr->zs_chan[CHAN_B]);
279 cs->cs_softcar = softcar & 2;
280 cs->cs_zc = &addr->zs_chan[CHAN_B];
281 tp->t_dev = makedev(ZSMAJOR, unit);
282 tp->t_oproc = zsstart;
283 tp->t_param = zsparam;
284 /*tp->t_stop = zsstop;*/
285 if ((ctp = zs_checkcons(zi, unit, cs)) != NULL)
286 tp = ctp;
287 cs->cs_ttyp = tp;
288 #ifdef KGDB
289 if (ctp == NULL)
290 zs_checkkgdb(unit, cs, tp);
291 #endif
292 if (unit == ZS_MOUSE) {
293 /*
294 * Mouse: tell /dev/mouse driver how to talk to us.
295 */
296 tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
297 tp->t_cflag = CS8;
298 ms_serial(tp, zsiopen, zsiclose);
299 }
300 }
301
302 /*
303 * Put a channel in a known state. Interrupts may be left disabled
304 * or enabled, as desired.
305 */
306 static void
307 zs_reset(zc, inten, speed)
308 volatile struct zschan *zc;
309 int inten, speed;
310 {
311 int tconst;
312 static u_char reg[16] = {
313 0,
314 0,
315 0,
316 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
317 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
318 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
319 0,
320 0,
321 0,
322 0,
323 ZSWR10_NRZ,
324 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
325 0,
326 0,
327 ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
328 ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
329 };
330
331 reg[9] = inten ? ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR : ZSWR9_NO_VECTOR;
332 tconst = BPS_TO_TCONST(PCLK / 16, speed);
333 reg[12] = tconst;
334 reg[13] = tconst >> 8;
335 zs_loadchannelregs(zc, reg);
336 }
337
338 /*
339 * Declare the given tty (which is in fact &cons) as a console input
340 * or output. This happens before the zs chip is attached; the hookup
341 * is finished later, in zs_setcons() below.
342 *
343 * This is used only for ports a and b. The console keyboard is decoded
344 * independently (we always send unit-2 input to /dev/kbd, which will
345 * direct it to /dev/console if appropriate).
346 */
347 void
348 zsconsole(tp, unit, out)
349 register struct tty *tp;
350 register int unit;
351 int out;
352 {
353 extern int (*v_putc)();
354 int zs;
355 volatile struct zsdevice *addr;
356
357 if (unit >= ZS_KBD)
358 panic("zsconsole");
359 if (out) {
360 zs_consout = unit;
361 zs = unit >> 1;
362 if ((addr = zsaddr[zs]) == NULL)
363 addr = zsaddr[zs] = findzs(zs);
364 zs_conschan = (unit & 1) == 0 ? &addr->zs_chan[CHAN_A] :
365 &addr->zs_chan[CHAN_B];
366 v_putc = zscnputc;
367 } else
368 zs_consin = unit;
369 zs_ctty = tp;
370 }
371
372 /*
373 * Polled console output putchar.
374 */
375 static int
376 zscnputc(c)
377 int c;
378 {
379 register volatile struct zschan *zc = zs_conschan;
380 register int s;
381
382 if (c == '\n')
383 zscnputc('\r');
384 /*
385 * Must block output interrupts (i.e., raise to >= splzs) without
386 * lowering current ipl. Need a better way.
387 */
388 s = splhigh();
389 #ifdef sun4c /* XXX */
390 if (s <= (12 << 8))
391 (void) splzs();
392 #endif
393 while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
394 continue;
395 zc->zc_data = c;
396 splx(s);
397 }
398
399 /*
400 * Set up the given unit as console input, output, both, or neither, as
401 * needed. Return console tty if it is to receive console input.
402 */
403 static struct tty *
404 zs_checkcons(struct zsinfo *zi, int unit, struct zs_chanstate *cs)
405 {
406 register struct tty *tp;
407 char *i, *o;
408
409 if ((tp = zs_ctty) == NULL)
410 return (0);
411 i = zs_consin == unit ? "input" : NULL;
412 o = zs_consout == unit ? "output" : NULL;
413 if (i == NULL && o == NULL)
414 return (0);
415
416 /* rewire the minor device (gack) */
417 tp->t_dev = makedev(major(tp->t_dev), unit);
418
419 /*
420 * Rewire input and/or output. Note that baud rate reflects
421 * input settings, not output settings, but we can do no better
422 * if the console is split across two ports.
423 *
424 * XXX split consoles don't work anyway -- this needs to be
425 * thrown away and redone
426 */
427 if (i) {
428 tp->t_param = zsparam;
429 tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
430 tp->t_cflag = CS8;
431 ttsetwater(tp);
432 }
433 if (o) {
434 tp->t_oproc = zsstart;
435 /*tp->t_stop = zsstop;*/
436 }
437 printf("%s%c: console %s\n",
438 zi->zi_dev.dv_xname, (unit & 1) + 'a', i ? (o ? "i/o" : i) : o);
439 cs->cs_consio = 1;
440 cs->cs_brkabort = 1;
441 return (tp);
442 }
443
444 #ifdef KGDB
445 /*
446 * The kgdb zs port, if any, was altered at boot time (see zs_kgdb_init).
447 * Pick up the current speed and character size and restore the original
448 * speed.
449 */
450 static void
451 zs_checkkgdb(int unit, struct zs_chanstate *cs, struct tty *tp)
452 {
453
454 if (kgdb_dev == makedev(ZSMAJOR, unit)) {
455 tp->t_ispeed = tp->t_ospeed = kgdb_rate;
456 tp->t_cflag = CS8;
457 cs->cs_kgdb = 1;
458 cs->cs_speed = zs_kgdb_savedspeed;
459 (void) zsparam(tp, &tp->t_termios);
460 }
461 }
462 #endif
463
464 /*
465 * Compute the current baud rate given a ZSCC channel.
466 */
467 static int
468 zs_getspeed(zc)
469 register volatile struct zschan *zc;
470 {
471 register int tconst;
472
473 tconst = ZS_READ(zc, 12);
474 tconst |= ZS_READ(zc, 13) << 8;
475 return (TCONST_TO_BPS(PCLK / 16, tconst));
476 }
477
478
479 /*
480 * Do an internal open.
481 */
482 static void
483 zsiopen(struct tty *tp)
484 {
485
486 (void) zsparam(tp, &tp->t_termios);
487 ttsetwater(tp);
488 tp->t_state = TS_ISOPEN | TS_CARR_ON;
489 }
490
491 /*
492 * Do an internal close. Eventually we should shut off the chip when both
493 * ports on it are closed.
494 */
495 static void
496 zsiclose(struct tty *tp)
497 {
498
499 ttylclose(tp, 0); /* ??? */
500 ttyclose(tp); /* ??? */
501 tp->t_state = 0;
502 }
503
504
505 /*
506 * Open a zs serial port. This interface may not be used to open
507 * the keyboard and mouse ports. (XXX)
508 */
509 int
510 zsopen(dev_t dev, int flags, int mode, struct proc *p)
511 {
512 register struct tty *tp;
513 register struct zs_chanstate *cs;
514 struct zsinfo *zi;
515 int unit = minor(dev), zs = unit >> 1, error, s;
516
517 if (zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL ||
518 unit == ZS_KBD || unit == ZS_MOUSE)
519 return (ENXIO);
520 cs = &zi->zi_cs[unit & 1];
521 if (cs->cs_consio)
522 return (ENXIO); /* ??? */
523 tp = cs->cs_ttyp;
524 s = spltty();
525 if ((tp->t_state & TS_ISOPEN) == 0) {
526 ttychars(tp);
527 if (tp->t_ispeed == 0) {
528 tp->t_iflag = TTYDEF_IFLAG;
529 tp->t_oflag = TTYDEF_OFLAG;
530 tp->t_cflag = TTYDEF_CFLAG;
531 tp->t_lflag = TTYDEF_LFLAG;
532 tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
533 }
534 (void) zsparam(tp, &tp->t_termios);
535 ttsetwater(tp);
536 } else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
537 splx(s);
538 return (EBUSY);
539 }
540 error = 0;
541 for (;;) {
542 /* loop, turning on the device, until carrier present */
543 zs_modem(cs, 1);
544 if (cs->cs_softcar)
545 tp->t_state |= TS_CARR_ON;
546 if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL ||
547 tp->t_state & TS_CARR_ON)
548 break;
549 tp->t_state |= TS_WOPEN;
550 if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
551 ttopen, 0))
552 break;
553 }
554 splx(s);
555 if (error == 0)
556 error = linesw[tp->t_line].l_open(dev, tp);
557 if (error)
558 zs_modem(cs, 0);
559 return (error);
560 }
561
562 /*
563 * Close a zs serial port.
564 */
565 int
566 zsclose(dev_t dev, int flags, int mode, struct proc *p)
567 {
568 register struct zs_chanstate *cs;
569 register struct tty *tp;
570 struct zsinfo *zi;
571 int unit = minor(dev), s;
572
573 zi = zscd.cd_devs[unit >> 1];
574 cs = &zi->zi_cs[unit & 1];
575 tp = cs->cs_ttyp;
576 linesw[tp->t_line].l_close(tp, flags);
577 if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
578 (tp->t_state & TS_ISOPEN) == 0) {
579 zs_modem(cs, 0);
580 /* hold low for 1 second */
581 (void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
582 }
583 ttyclose(tp);
584 #ifdef KGDB
585 /* Reset the speed if we're doing kgdb on this port */
586 if (cs->cs_kgdb) {
587 tp->t_ispeed = tp->t_ospeed = kgdb_rate;
588 (void) zsparam(tp, &tp->t_termios);
589 }
590 #endif
591 return (0);
592 }
593
594 /*
595 * Read/write zs serial port.
596 */
597 int
598 zsread(dev_t dev, struct uio *uio, int flags)
599 {
600 register struct tty *tp = zs_tty[minor(dev)];
601
602 return (linesw[tp->t_line].l_read(tp, uio, flags));
603 }
604
605 int
606 zswrite(dev_t dev, struct uio *uio, int flags)
607 {
608 register struct tty *tp = zs_tty[minor(dev)];
609
610 return (linesw[tp->t_line].l_write(tp, uio, flags));
611 }
612
613 /*
614 * ZS hardware interrupt. Scan all ZS channels. NB: we know here that
615 * channels are kept in (A,B) pairs.
616 *
617 * Do just a little, then get out; set a software interrupt if more
618 * work is needed.
619 *
620 * We deliberately ignore the vectoring Zilog gives us, and match up
621 * only the number of `reset interrupt under service' operations, not
622 * the order.
623 */
624 /* ARGSUSED */
625 int
626 zshard(void *intrarg)
627 {
628 register struct zs_chanstate *a;
629 #define b (a + 1)
630 register volatile struct zschan *zc;
631 register int rr3, intflags = 0, v, i;
632 static int zsrint(struct zs_chanstate *, volatile struct zschan *);
633 static int zsxint(struct zs_chanstate *, volatile struct zschan *);
634 static int zssint(struct zs_chanstate *, volatile struct zschan *);
635
636 for (a = zslist; a != NULL; a = b->cs_next) {
637 rr3 = ZS_READ(a->cs_zc, 3);
638 if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
639 intflags |= 2;
640 zc = a->cs_zc;
641 i = a->cs_rbput;
642 if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
643 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
644 intflags |= 1;
645 }
646 if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
647 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
648 intflags |= 1;
649 }
650 if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
651 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
652 intflags |= 1;
653 }
654 a->cs_rbput = i;
655 }
656 if (rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
657 intflags |= 2;
658 zc = b->cs_zc;
659 i = b->cs_rbput;
660 if (rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
661 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
662 intflags |= 1;
663 }
664 if (rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
665 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
666 intflags |= 1;
667 }
668 if (rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
669 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
670 intflags |= 1;
671 }
672 b->cs_rbput = i;
673 }
674 }
675 #undef b
676 if (intflags & 1) {
677 #if sun4c /* XXX -- but this will go away when zshard moves to locore.s */
678 struct clockframe *p = intrarg;
679
680 if ((p->psr & PSR_PIL) < (PIL_TTY << 8)) {
681 zsshortcuts++;
682 (void) spltty();
683 if (zshardscope) {
684 LED_ON;
685 LED_OFF;
686 }
687 return (zssoft(intrarg));
688 }
689 #endif
690 ienab_bis(IE_ZSSOFT);
691 }
692 return (intflags & 2);
693 }
694
695 static int
696 zsrint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
697 {
698 register int c = zc->zc_data;
699
700 if (cs->cs_conk) {
701 register struct conk_state *conk = &zsconk_state;
702
703 /*
704 * Check here for console abort function, so that we
705 * can abort even when interrupts are locking up the
706 * machine.
707 */
708 if (c == KBD_RESET) {
709 conk->conk_id = 1; /* ignore next byte */
710 conk->conk_l1 = 0;
711 } else if (conk->conk_id)
712 conk->conk_id = 0; /* stop ignoring bytes */
713 else if (c == KBD_L1)
714 conk->conk_l1 = 1; /* L1 went down */
715 else if (c == (KBD_L1|KBD_UP))
716 conk->conk_l1 = 0; /* L1 went up */
717 else if (c == KBD_A && conk->conk_l1) {
718 zsabort();
719 conk->conk_l1 = 0; /* we never see the up */
720 goto clearit; /* eat the A after L1-A */
721 }
722 }
723 #ifdef KGDB
724 if (c == FRAME_START && cs->cs_kgdb &&
725 (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) {
726 zskgdb(cs->cs_unit);
727 goto clearit;
728 }
729 #endif
730 /* compose receive character and status */
731 c <<= 8;
732 c |= ZS_READ(zc, 1);
733
734 /* clear receive error & interrupt condition */
735 zc->zc_csr = ZSWR0_RESET_ERRORS;
736 zc->zc_csr = ZSWR0_CLR_INTR;
737
738 return (ZRING_MAKE(ZRING_RINT, c));
739
740 clearit:
741 zc->zc_csr = ZSWR0_RESET_ERRORS;
742 zc->zc_csr = ZSWR0_CLR_INTR;
743 return (0);
744 }
745
746 static int
747 zsxint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
748 {
749 register int i = cs->cs_tbc;
750
751 if (i == 0) {
752 zc->zc_csr = ZSWR0_RESET_TXINT;
753 zc->zc_csr = ZSWR0_CLR_INTR;
754 return (ZRING_MAKE(ZRING_XINT, 0));
755 }
756 cs->cs_tbc = i - 1;
757 zc->zc_data = *cs->cs_tba++;
758 zc->zc_csr = ZSWR0_CLR_INTR;
759 return (0);
760 }
761
762 static int
763 zssint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
764 {
765 register int rr0;
766
767 rr0 = zc->zc_csr;
768 zc->zc_csr = ZSWR0_RESET_STATUS;
769 zc->zc_csr = ZSWR0_CLR_INTR;
770 /*
771 * The chip's hardware flow control is, as noted in zsreg.h,
772 * busted---if the DCD line goes low the chip shuts off the
773 * receiver (!). If we want hardware CTS flow control but do
774 * not have it, and carrier is now on, turn HFC on; if we have
775 * HFC now but carrier has gone low, turn it off.
776 */
777 if (rr0 & ZSRR0_DCD) {
778 if (cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
779 (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
780 cs->cs_creg[3] |= ZSWR3_HFC;
781 ZS_WRITE(zc, 3, cs->cs_creg[3]);
782 }
783 } else {
784 if (cs->cs_creg[3] & ZSWR3_HFC) {
785 cs->cs_creg[3] &= ~ZSWR3_HFC;
786 ZS_WRITE(zc, 3, cs->cs_creg[3]);
787 }
788 }
789 if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) {
790 zsabort();
791 return (0);
792 }
793 return (ZRING_MAKE(ZRING_SINT, rr0));
794 }
795
796 zsabort()
797 {
798
799 printf("stopping on keyboard abort\n");
800 callrom();
801 }
802
803 #ifdef KGDB
804 /*
805 * KGDB framing character received: enter kernel debugger. This probably
806 * should time out after a few seconds to avoid hanging on spurious input.
807 */
808 zskgdb(int unit)
809 {
810
811 printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a');
812 kgdb_connect(1);
813 }
814 #endif
815
816 /*
817 * Print out a ring or fifo overrun error message.
818 */
819 static void
820 zsoverrun(int unit, long *ptime, char *what)
821 {
822
823 if (*ptime != time.tv_sec) {
824 *ptime = time.tv_sec;
825 log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
826 (unit & 1) + 'a', what);
827 }
828 }
829
830 /*
831 * ZS software interrupt. Scan all channels for deferred interrupts.
832 */
833 int
834 zssoft(void *arg)
835 {
836 register struct zs_chanstate *cs;
837 register volatile struct zschan *zc;
838 register struct linesw *line;
839 register struct tty *tp;
840 register int get, n, c, cc, unit, s;
841
842 for (cs = zslist; cs != NULL; cs = cs->cs_next) {
843 get = cs->cs_rbget;
844 again:
845 n = cs->cs_rbput; /* atomic */
846 if (get == n) /* nothing more on this line */
847 continue;
848 unit = cs->cs_unit; /* set up to handle interrupts */
849 zc = cs->cs_zc;
850 tp = cs->cs_ttyp;
851 line = &linesw[tp->t_line];
852 /*
853 * Compute the number of interrupts in the receive ring.
854 * If the count is overlarge, we lost some events, and
855 * must advance to the first valid one. It may get
856 * overwritten if more data are arriving, but this is
857 * too expensive to check and gains nothing (we already
858 * lost out; all we can do at this point is trade one
859 * kind of loss for another).
860 */
861 n -= get;
862 if (n > ZLRB_RING_SIZE) {
863 zsoverrun(unit, &cs->cs_rotime, "ring");
864 get += n - ZLRB_RING_SIZE;
865 n = ZLRB_RING_SIZE;
866 }
867 while (--n >= 0) {
868 /* race to keep ahead of incoming interrupts */
869 c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
870 switch (ZRING_TYPE(c)) {
871
872 case ZRING_RINT:
873 c = ZRING_VALUE(c);
874 if (c & ZSRR1_DO)
875 zsoverrun(unit, &cs->cs_fotime, "fifo");
876 cc = c >> 8;
877 if (c & ZSRR1_FE)
878 cc |= TTY_FE;
879 if (c & ZSRR1_PE)
880 cc |= TTY_PE;
881 /*
882 * this should be done through
883 * bstreams XXX gag choke
884 */
885 if (unit == ZS_KBD)
886 kbd_rint(cc);
887 else if (unit == ZS_MOUSE)
888 ms_rint(cc);
889 else
890 line->l_rint(cc, tp);
891 break;
892
893 case ZRING_XINT:
894 /*
895 * Transmit done: change registers and resume,
896 * or clear BUSY.
897 */
898 if (cs->cs_heldchange) {
899 s = splzs();
900 c = zc->zc_csr;
901 if ((c & ZSRR0_DCD) == 0)
902 cs->cs_preg[3] &= ~ZSWR3_HFC;
903 bcopy((caddr_t)cs->cs_preg,
904 (caddr_t)cs->cs_creg, 16);
905 zs_loadchannelregs(zc, cs->cs_creg);
906 splx(s);
907 cs->cs_heldchange = 0;
908 if (cs->cs_heldtbc &&
909 (tp->t_state & TS_TTSTOP) == 0) {
910 cs->cs_tbc = cs->cs_heldtbc - 1;
911 zc->zc_data = *cs->cs_tba++;
912 goto again;
913 }
914 }
915 tp->t_state &= ~TS_BUSY;
916 if (tp->t_state & TS_FLUSH)
917 tp->t_state &= ~TS_FLUSH;
918 else
919 ndflush(&tp->t_outq,
920 (u_char *)cs->cs_tba - tp->t_outq.c_cf);
921 line->l_start(tp);
922 break;
923
924 case ZRING_SINT:
925 /*
926 * Status line change. HFC bit is run in
927 * hardware interrupt, to avoid locking
928 * at splzs here.
929 */
930 c = ZRING_VALUE(c);
931 if ((c ^ cs->cs_rr0) & ZSRR0_DCD) {
932 cc = (c & ZSRR0_DCD) != 0;
933 if (line->l_modem(tp, cc) == 0)
934 zs_modem(cs, cc);
935 }
936 cs->cs_rr0 = c;
937 break;
938
939 default:
940 log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
941 unit >> 1, (unit & 1) + 'a', c);
942 break;
943 }
944 }
945 cs->cs_rbget = get;
946 goto again;
947 }
948 return (1);
949 }
950
951 int
952 zsioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
953 {
954 int unit = minor(dev);
955 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
956 register struct tty *tp = zi->zi_cs[unit & 1].cs_ttyp;
957 register int error;
958
959 error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag);
960 if (error >= 0)
961 return (error);
962 error = ttioctl(tp, cmd, data, flag);
963 if (error >= 0)
964 return (error);
965
966 switch (cmd) {
967
968 case TIOCSBRK:
969 /* FINISH ME ... need implicit TIOCCBRK in zsclose as well */
970
971 case TIOCCBRK:
972
973 case TIOCSDTR:
974
975 case TIOCCDTR:
976
977 case TIOCMSET:
978
979 case TIOCMBIS:
980
981 case TIOCMBIC:
982
983 case TIOCMGET:
984
985 default:
986 return (ENOTTY);
987 }
988 return (0);
989 }
990
991 /*
992 * Start or restart transmission.
993 */
994 static void
995 zsstart(register struct tty *tp)
996 {
997 register struct zs_chanstate *cs;
998 register int s, nch;
999 int unit = minor(tp->t_dev);
1000 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1001
1002 cs = &zi->zi_cs[unit & 1];
1003 s = spltty();
1004
1005 /*
1006 * If currently active or delaying, no need to do anything.
1007 */
1008 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
1009 goto out;
1010
1011 /*
1012 * If there are sleepers, and output has drained below low
1013 * water mark, awaken.
1014 */
1015 if (tp->t_outq.c_cc <= tp->t_lowat) {
1016 if (tp->t_state & TS_ASLEEP) {
1017 tp->t_state &= ~TS_ASLEEP;
1018 wakeup((caddr_t)&tp->t_outq);
1019 }
1020 selwakeup(&tp->t_wsel);
1021 }
1022
1023 nch = ndqb(&tp->t_outq, 0); /* XXX */
1024 if (nch) {
1025 register char *p = tp->t_outq.c_cf;
1026
1027 /* mark busy, enable tx done interrupts, & send first byte */
1028 tp->t_state |= TS_BUSY;
1029 (void) splzs();
1030 cs->cs_preg[1] |= ZSWR1_TIE;
1031 cs->cs_creg[1] |= ZSWR1_TIE;
1032 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1033 cs->cs_zc->zc_data = *p;
1034 cs->cs_tba = p + 1;
1035 cs->cs_tbc = nch - 1;
1036 } else {
1037 /*
1038 * Nothing to send, turn off transmit done interrupts.
1039 * This is useful if something is doing polled output.
1040 */
1041 (void) splzs();
1042 cs->cs_preg[1] &= ~ZSWR1_TIE;
1043 cs->cs_creg[1] &= ~ZSWR1_TIE;
1044 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1045 }
1046 out:
1047 splx(s);
1048 }
1049
1050 /*
1051 * Stop output, e.g., for ^S or output flush.
1052 */
1053 static void
1054 zsstop(register struct tty *tp, int flag)
1055 {
1056 register struct zs_chanstate *cs;
1057 register int s, unit = minor(tp->t_dev);
1058 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1059
1060 cs = &zi->zi_cs[unit & 1];
1061 s = splzs();
1062 if (tp->t_state & TS_BUSY) {
1063 /*
1064 * Device is transmitting; must stop it.
1065 */
1066 cs->cs_tbc = 0;
1067 if ((tp->t_state & TS_TTSTOP) == 0)
1068 tp->t_state |= TS_FLUSH;
1069 }
1070 splx(s);
1071 }
1072
1073 /*
1074 * Set ZS tty parameters from termios.
1075 *
1076 * This routine makes use of the fact that only registers
1077 * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
1078 */
1079 static int
1080 zsparam(register struct tty *tp, register struct termios *t)
1081 {
1082 int unit = minor(tp->t_dev);
1083 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1084 register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
1085 register int tmp, tmp5, cflag, s;
1086
1087 /*
1088 * Because PCLK is only run at 4.9 MHz, the fastest we
1089 * can go is 51200 baud (this corresponds to TC=1).
1090 * This is somewhat unfortunate as there is no real
1091 * reason we should not be able to handle higher rates.
1092 */
1093 tmp = t->c_ospeed;
1094 if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp))
1095 return (EINVAL);
1096 if (tmp == 0) {
1097 /* stty 0 => drop DTR and RTS */
1098 zs_modem(cs, 0);
1099 return (0);
1100 }
1101 tmp = BPS_TO_TCONST(PCLK / 16, tmp);
1102 if (tmp < 2)
1103 return (EINVAL);
1104
1105 cflag = t->c_cflag;
1106 tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp);
1107 tp->t_cflag = cflag;
1108
1109 /*
1110 * Block interrupts so that state will not
1111 * be altered until we are done setting it up.
1112 */
1113 s = splzs();
1114 cs->cs_preg[12] = tmp;
1115 cs->cs_preg[13] = tmp >> 8;
1116 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
1117 switch (cflag & CSIZE) {
1118 case CS5:
1119 tmp = ZSWR3_RX_5;
1120 tmp5 = ZSWR5_TX_5;
1121 break;
1122 case CS6:
1123 tmp = ZSWR3_RX_6;
1124 tmp5 = ZSWR5_TX_6;
1125 break;
1126 case CS7:
1127 tmp = ZSWR3_RX_7;
1128 tmp5 = ZSWR5_TX_7;
1129 break;
1130 case CS8:
1131 default:
1132 tmp = ZSWR3_RX_8;
1133 tmp5 = ZSWR5_TX_8;
1134 break;
1135 }
1136
1137 /*
1138 * Output hardware flow control on the chip is horrendous: if
1139 * carrier detect drops, the receiver is disabled. Hence we
1140 * can only do this when the carrier is on.
1141 */
1142 if (cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD)
1143 tmp |= ZSWR3_HFC | ZSWR3_RX_ENABLE;
1144 else
1145 tmp |= ZSWR3_RX_ENABLE;
1146 cs->cs_preg[3] = tmp;
1147 cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
1148
1149 tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
1150 if ((cflag & PARODD) == 0)
1151 tmp |= ZSWR4_EVENP;
1152 if (cflag & PARENB)
1153 tmp |= ZSWR4_PARENB;
1154 cs->cs_preg[4] = tmp;
1155 cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR;
1156 cs->cs_preg[10] = ZSWR10_NRZ;
1157 cs->cs_preg[11] = ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD;
1158 cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA;
1159 cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
1160
1161 /*
1162 * If nothing is being transmitted, set up new current values,
1163 * else mark them as pending.
1164 */
1165 if (cs->cs_heldchange == 0) {
1166 if (cs->cs_ttyp->t_state & TS_BUSY) {
1167 cs->cs_heldtbc = cs->cs_tbc;
1168 cs->cs_tbc = 0;
1169 cs->cs_heldchange = 1;
1170 } else {
1171 bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
1172 zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
1173 }
1174 }
1175 splx(s);
1176 return (0);
1177 }
1178
1179 /*
1180 * Raise or lower modem control (DTR/RTS) signals. If a character is
1181 * in transmission, the change is deferred.
1182 */
1183 static void
1184 zs_modem(struct zs_chanstate *cs, int onoff)
1185 {
1186 int s, bis, and;
1187
1188 if (onoff) {
1189 bis = ZSWR5_DTR | ZSWR5_RTS;
1190 and = ~0;
1191 } else {
1192 bis = 0;
1193 and = ~(ZSWR5_DTR | ZSWR5_RTS);
1194 }
1195 s = splzs();
1196 cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
1197 if (cs->cs_heldchange == 0) {
1198 if (cs->cs_ttyp->t_state & TS_BUSY) {
1199 cs->cs_heldtbc = cs->cs_tbc;
1200 cs->cs_tbc = 0;
1201 cs->cs_heldchange = 1;
1202 } else {
1203 cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
1204 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1205 }
1206 }
1207 splx(s);
1208 }
1209
1210 /*
1211 * Write the given register set to the given zs channel in the proper order.
1212 * The channel must not be transmitting at the time. The receiver will
1213 * be disabled for the time it takes to write all the registers.
1214 */
1215 static void
1216 zs_loadchannelregs(volatile struct zschan *zc, u_char *reg)
1217 {
1218 int i;
1219
1220 zc->zc_csr = ZSM_RESET_ERR; /* reset error condition */
1221 i = zc->zc_data; /* drain fifo */
1222 i = zc->zc_data;
1223 i = zc->zc_data;
1224 ZS_WRITE(zc, 4, reg[4]);
1225 ZS_WRITE(zc, 10, reg[10]);
1226 ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
1227 ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
1228 ZS_WRITE(zc, 1, reg[1]);
1229 ZS_WRITE(zc, 9, reg[9]);
1230 ZS_WRITE(zc, 11, reg[11]);
1231 ZS_WRITE(zc, 12, reg[12]);
1232 ZS_WRITE(zc, 13, reg[13]);
1233 ZS_WRITE(zc, 14, reg[14]);
1234 ZS_WRITE(zc, 15, reg[15]);
1235 ZS_WRITE(zc, 3, reg[3]);
1236 ZS_WRITE(zc, 5, reg[5]);
1237 }
1238
1239 #ifdef KGDB
1240 /*
1241 * Get a character from the given kgdb channel. Called at splhigh().
1242 */
1243 static int
1244 zs_kgdb_getc(void *arg)
1245 {
1246 register volatile struct zschan *zc = (volatile struct zschan *)arg;
1247
1248 while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
1249 continue;
1250 return (zc->zc_data);
1251 }
1252
1253 /*
1254 * Put a character to the given kgdb channel. Called at splhigh().
1255 */
1256 static void
1257 zs_kgdb_putc(void *arg, int c)
1258 {
1259 register volatile struct zschan *zc = (volatile struct zschan *)arg;
1260
1261 while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
1262 continue;
1263 zc->zc_data = c;
1264 }
1265
1266 /*
1267 * Set up for kgdb; called at boot time before configuration.
1268 * KGDB interrupts will be enabled later when zs0 is configured.
1269 */
1270 void
1271 zs_kgdb_init()
1272 {
1273 volatile struct zsdevice *addr;
1274 volatile struct zschan *zc;
1275 int unit, zs;
1276
1277 if (major(kgdb_dev) != ZSMAJOR)
1278 return;
1279 unit = minor(kgdb_dev);
1280 /*
1281 * Unit must be 0 or 1 (zs0).
1282 */
1283 if ((unsigned)unit >= ZS_KBD) {
1284 printf("zs_kgdb_init: bad minor dev %d\n", unit);
1285 return;
1286 }
1287 zs = unit >> 1;
1288 if ((addr = zsaddr[zs]) == NULL)
1289 addr = zsaddr[zs] = findzs(zs);
1290 unit &= 1;
1291 zc = unit == 0 ? &addr->zs_chan[CHAN_A] : &addr->zs_chan[CHAN_B];
1292 zs_kgdb_savedspeed = zs_getspeed(zc);
1293 printf("zs_kgdb_init: attaching zs%d%c at %d baud\n",
1294 zs, unit + 'a', kgdb_rate);
1295 zs_reset(zc, 1, kgdb_rate);
1296 kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc);
1297 }
1298 #endif /* KGDB */
1299