zs.c revision 1.6 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.6 1994/05/05 09:53:43 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 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, fnstop)
349 register struct tty *tp;
350 register int unit;
351 int out;
352 void (**fnstop) __P((struct tty *, int));
353 {
354 extern int (*v_putc)();
355 int zs;
356 volatile struct zsdevice *addr;
357
358 if (unit >= ZS_KBD)
359 panic("zsconsole");
360 if (out) {
361 zs_consout = unit;
362 zs = unit >> 1;
363 if ((addr = zsaddr[zs]) == NULL)
364 addr = zsaddr[zs] = findzs(zs);
365 zs_conschan = (unit & 1) == 0 ? &addr->zs_chan[CHAN_A] :
366 &addr->zs_chan[CHAN_B];
367 v_putc = zscnputc;
368 } else
369 zs_consin = unit;
370 if(fnstop)
371 *fnstop = &zsstop;
372 zs_ctty = tp;
373 }
374
375 /*
376 * Polled console output putchar.
377 */
378 static int
379 zscnputc(c)
380 int c;
381 {
382 register volatile struct zschan *zc = zs_conschan;
383 register int s;
384
385 if (c == '\n')
386 zscnputc('\r');
387 /*
388 * Must block output interrupts (i.e., raise to >= splzs) without
389 * lowering current ipl. Need a better way.
390 */
391 s = splhigh();
392 #ifdef sun4c /* XXX */
393 if (s <= (12 << 8))
394 (void) splzs();
395 #endif
396 while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
397 continue;
398 zc->zc_data = c;
399 splx(s);
400 }
401
402 /*
403 * Set up the given unit as console input, output, both, or neither, as
404 * needed. Return console tty if it is to receive console input.
405 */
406 static struct tty *
407 zs_checkcons(struct zsinfo *zi, int unit, struct zs_chanstate *cs)
408 {
409 register struct tty *tp;
410 char *i, *o;
411
412 if ((tp = zs_ctty) == NULL)
413 return (0);
414 i = zs_consin == unit ? "input" : NULL;
415 o = zs_consout == unit ? "output" : NULL;
416 if (i == NULL && o == NULL)
417 return (0);
418
419 /* rewire the minor device (gack) */
420 tp->t_dev = makedev(major(tp->t_dev), unit);
421
422 /*
423 * Rewire input and/or output. Note that baud rate reflects
424 * input settings, not output settings, but we can do no better
425 * if the console is split across two ports.
426 *
427 * XXX split consoles don't work anyway -- this needs to be
428 * thrown away and redone
429 */
430 if (i) {
431 tp->t_param = zsparam;
432 tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
433 tp->t_cflag = CS8;
434 ttsetwater(tp);
435 }
436 if (o) {
437 tp->t_oproc = zsstart;
438 /*tp->t_stop = zsstop;*/
439 }
440 printf("%s%c: console %s\n",
441 zi->zi_dev.dv_xname, (unit & 1) + 'a', i ? (o ? "i/o" : i) : o);
442 cs->cs_consio = 1;
443 cs->cs_brkabort = 1;
444 return (tp);
445 }
446
447 #ifdef KGDB
448 /*
449 * The kgdb zs port, if any, was altered at boot time (see zs_kgdb_init).
450 * Pick up the current speed and character size and restore the original
451 * speed.
452 */
453 static void
454 zs_checkkgdb(int unit, struct zs_chanstate *cs, struct tty *tp)
455 {
456
457 if (kgdb_dev == makedev(ZSMAJOR, unit)) {
458 tp->t_ispeed = tp->t_ospeed = kgdb_rate;
459 tp->t_cflag = CS8;
460 cs->cs_kgdb = 1;
461 cs->cs_speed = zs_kgdb_savedspeed;
462 (void) zsparam(tp, &tp->t_termios);
463 }
464 }
465 #endif
466
467 /*
468 * Compute the current baud rate given a ZSCC channel.
469 */
470 static int
471 zs_getspeed(zc)
472 register volatile struct zschan *zc;
473 {
474 register int tconst;
475
476 tconst = ZS_READ(zc, 12);
477 tconst |= ZS_READ(zc, 13) << 8;
478 return (TCONST_TO_BPS(PCLK / 16, tconst));
479 }
480
481
482 /*
483 * Do an internal open.
484 */
485 static void
486 zsiopen(struct tty *tp)
487 {
488
489 (void) zsparam(tp, &tp->t_termios);
490 ttsetwater(tp);
491 tp->t_state = TS_ISOPEN | TS_CARR_ON;
492 }
493
494 /*
495 * Do an internal close. Eventually we should shut off the chip when both
496 * ports on it are closed.
497 */
498 static void
499 zsiclose(struct tty *tp)
500 {
501
502 ttylclose(tp, 0); /* ??? */
503 ttyclose(tp); /* ??? */
504 tp->t_state = 0;
505 }
506
507
508 /*
509 * Open a zs serial port. This interface may not be used to open
510 * the keyboard and mouse ports. (XXX)
511 */
512 int
513 zsopen(dev_t dev, int flags, int mode, struct proc *p)
514 {
515 register struct tty *tp;
516 register struct zs_chanstate *cs;
517 struct zsinfo *zi;
518 int unit = minor(dev), zs = unit >> 1, error, s;
519
520 if (zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL ||
521 unit == ZS_KBD || unit == ZS_MOUSE)
522 return (ENXIO);
523 cs = &zi->zi_cs[unit & 1];
524 if (cs->cs_consio)
525 return (ENXIO); /* ??? */
526 tp = cs->cs_ttyp;
527 s = spltty();
528 if ((tp->t_state & TS_ISOPEN) == 0) {
529 ttychars(tp);
530 if (tp->t_ispeed == 0) {
531 tp->t_iflag = TTYDEF_IFLAG;
532 tp->t_oflag = TTYDEF_OFLAG;
533 tp->t_cflag = TTYDEF_CFLAG;
534 tp->t_lflag = TTYDEF_LFLAG;
535 tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
536 }
537 (void) zsparam(tp, &tp->t_termios);
538 ttsetwater(tp);
539 } else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
540 splx(s);
541 return (EBUSY);
542 }
543 error = 0;
544 for (;;) {
545 /* loop, turning on the device, until carrier present */
546 zs_modem(cs, 1);
547 if (cs->cs_softcar)
548 tp->t_state |= TS_CARR_ON;
549 if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL ||
550 tp->t_state & TS_CARR_ON)
551 break;
552 tp->t_state |= TS_WOPEN;
553 if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
554 ttopen, 0))
555 break;
556 }
557 splx(s);
558 if (error == 0)
559 error = linesw[tp->t_line].l_open(dev, tp);
560 if (error)
561 zs_modem(cs, 0);
562 return (error);
563 }
564
565 /*
566 * Close a zs serial port.
567 */
568 int
569 zsclose(dev_t dev, int flags, int mode, struct proc *p)
570 {
571 register struct zs_chanstate *cs;
572 register struct tty *tp;
573 struct zsinfo *zi;
574 int unit = minor(dev), s;
575
576 zi = zscd.cd_devs[unit >> 1];
577 cs = &zi->zi_cs[unit & 1];
578 tp = cs->cs_ttyp;
579 linesw[tp->t_line].l_close(tp, flags);
580 if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
581 (tp->t_state & TS_ISOPEN) == 0) {
582 zs_modem(cs, 0);
583 /* hold low for 1 second */
584 (void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
585 }
586 ttyclose(tp);
587 #ifdef KGDB
588 /* Reset the speed if we're doing kgdb on this port */
589 if (cs->cs_kgdb) {
590 tp->t_ispeed = tp->t_ospeed = kgdb_rate;
591 (void) zsparam(tp, &tp->t_termios);
592 }
593 #endif
594 return (0);
595 }
596
597 /*
598 * Read/write zs serial port.
599 */
600 int
601 zsread(dev_t dev, struct uio *uio, int flags)
602 {
603 register struct tty *tp = zs_tty[minor(dev)];
604
605 return (linesw[tp->t_line].l_read(tp, uio, flags));
606 }
607
608 int
609 zswrite(dev_t dev, struct uio *uio, int flags)
610 {
611 register struct tty *tp = zs_tty[minor(dev)];
612
613 return (linesw[tp->t_line].l_write(tp, uio, flags));
614 }
615
616 /*
617 * ZS hardware interrupt. Scan all ZS channels. NB: we know here that
618 * channels are kept in (A,B) pairs.
619 *
620 * Do just a little, then get out; set a software interrupt if more
621 * work is needed.
622 *
623 * We deliberately ignore the vectoring Zilog gives us, and match up
624 * only the number of `reset interrupt under service' operations, not
625 * the order.
626 */
627 /* ARGSUSED */
628 int
629 zshard(void *intrarg)
630 {
631 register struct zs_chanstate *a;
632 #define b (a + 1)
633 register volatile struct zschan *zc;
634 register int rr3, intflags = 0, v, i;
635 static int zsrint(struct zs_chanstate *, volatile struct zschan *);
636 static int zsxint(struct zs_chanstate *, volatile struct zschan *);
637 static int zssint(struct zs_chanstate *, volatile struct zschan *);
638
639 for (a = zslist; a != NULL; a = b->cs_next) {
640 rr3 = ZS_READ(a->cs_zc, 3);
641 if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
642 intflags |= 2;
643 zc = a->cs_zc;
644 i = a->cs_rbput;
645 if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
646 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
647 intflags |= 1;
648 }
649 if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
650 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
651 intflags |= 1;
652 }
653 if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
654 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
655 intflags |= 1;
656 }
657 a->cs_rbput = i;
658 }
659 if (rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
660 intflags |= 2;
661 zc = b->cs_zc;
662 i = b->cs_rbput;
663 if (rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
664 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
665 intflags |= 1;
666 }
667 if (rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
668 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
669 intflags |= 1;
670 }
671 if (rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
672 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
673 intflags |= 1;
674 }
675 b->cs_rbput = i;
676 }
677 }
678 #undef b
679 if (intflags & 1) {
680 #if sun4c /* XXX -- but this will go away when zshard moves to locore.s */
681 struct clockframe *p = intrarg;
682
683 if ((p->psr & PSR_PIL) < (PIL_TTY << 8)) {
684 zsshortcuts++;
685 (void) spltty();
686 if (zshardscope) {
687 LED_ON;
688 LED_OFF;
689 }
690 return (zssoft(intrarg));
691 }
692 #endif
693 ienab_bis(IE_ZSSOFT);
694 }
695 return (intflags & 2);
696 }
697
698 static int
699 zsrint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
700 {
701 register int c = zc->zc_data;
702
703 if (cs->cs_conk) {
704 register struct conk_state *conk = &zsconk_state;
705
706 /*
707 * Check here for console abort function, so that we
708 * can abort even when interrupts are locking up the
709 * machine.
710 */
711 if (c == KBD_RESET) {
712 conk->conk_id = 1; /* ignore next byte */
713 conk->conk_l1 = 0;
714 } else if (conk->conk_id)
715 conk->conk_id = 0; /* stop ignoring bytes */
716 else if (c == KBD_L1)
717 conk->conk_l1 = 1; /* L1 went down */
718 else if (c == (KBD_L1|KBD_UP))
719 conk->conk_l1 = 0; /* L1 went up */
720 else if (c == KBD_A && conk->conk_l1) {
721 zsabort();
722 conk->conk_l1 = 0; /* we never see the up */
723 goto clearit; /* eat the A after L1-A */
724 }
725 }
726 #ifdef KGDB
727 if (c == FRAME_START && cs->cs_kgdb &&
728 (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) {
729 zskgdb(cs->cs_unit);
730 goto clearit;
731 }
732 #endif
733 /* compose receive character and status */
734 c <<= 8;
735 c |= ZS_READ(zc, 1);
736
737 /* clear receive error & interrupt condition */
738 zc->zc_csr = ZSWR0_RESET_ERRORS;
739 zc->zc_csr = ZSWR0_CLR_INTR;
740
741 return (ZRING_MAKE(ZRING_RINT, c));
742
743 clearit:
744 zc->zc_csr = ZSWR0_RESET_ERRORS;
745 zc->zc_csr = ZSWR0_CLR_INTR;
746 return (0);
747 }
748
749 static int
750 zsxint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
751 {
752 register int i = cs->cs_tbc;
753
754 if (i == 0) {
755 zc->zc_csr = ZSWR0_RESET_TXINT;
756 zc->zc_csr = ZSWR0_CLR_INTR;
757 return (ZRING_MAKE(ZRING_XINT, 0));
758 }
759 cs->cs_tbc = i - 1;
760 zc->zc_data = *cs->cs_tba++;
761 zc->zc_csr = ZSWR0_CLR_INTR;
762 return (0);
763 }
764
765 static int
766 zssint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
767 {
768 register int rr0;
769
770 rr0 = zc->zc_csr;
771 zc->zc_csr = ZSWR0_RESET_STATUS;
772 zc->zc_csr = ZSWR0_CLR_INTR;
773 /*
774 * The chip's hardware flow control is, as noted in zsreg.h,
775 * busted---if the DCD line goes low the chip shuts off the
776 * receiver (!). If we want hardware CTS flow control but do
777 * not have it, and carrier is now on, turn HFC on; if we have
778 * HFC now but carrier has gone low, turn it off.
779 */
780 if (rr0 & ZSRR0_DCD) {
781 if (cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
782 (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
783 cs->cs_creg[3] |= ZSWR3_HFC;
784 ZS_WRITE(zc, 3, cs->cs_creg[3]);
785 }
786 } else {
787 if (cs->cs_creg[3] & ZSWR3_HFC) {
788 cs->cs_creg[3] &= ~ZSWR3_HFC;
789 ZS_WRITE(zc, 3, cs->cs_creg[3]);
790 }
791 }
792 if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) {
793 zsabort();
794 return (0);
795 }
796 return (ZRING_MAKE(ZRING_SINT, rr0));
797 }
798
799 zsabort()
800 {
801
802 #ifdef DDB
803 Debugger();
804 #else
805 printf("stopping on keyboard abort\n");
806 callrom();
807 #endif
808 }
809
810 #ifdef KGDB
811 /*
812 * KGDB framing character received: enter kernel debugger. This probably
813 * should time out after a few seconds to avoid hanging on spurious input.
814 */
815 zskgdb(int unit)
816 {
817
818 printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a');
819 kgdb_connect(1);
820 }
821 #endif
822
823 /*
824 * Print out a ring or fifo overrun error message.
825 */
826 static void
827 zsoverrun(int unit, long *ptime, char *what)
828 {
829
830 if (*ptime != time.tv_sec) {
831 *ptime = time.tv_sec;
832 log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
833 (unit & 1) + 'a', what);
834 }
835 }
836
837 /*
838 * ZS software interrupt. Scan all channels for deferred interrupts.
839 */
840 int
841 zssoft(void *arg)
842 {
843 register struct zs_chanstate *cs;
844 register volatile struct zschan *zc;
845 register struct linesw *line;
846 register struct tty *tp;
847 register int get, n, c, cc, unit, s;
848
849 for (cs = zslist; cs != NULL; cs = cs->cs_next) {
850 get = cs->cs_rbget;
851 again:
852 n = cs->cs_rbput; /* atomic */
853 if (get == n) /* nothing more on this line */
854 continue;
855 unit = cs->cs_unit; /* set up to handle interrupts */
856 zc = cs->cs_zc;
857 tp = cs->cs_ttyp;
858 line = &linesw[tp->t_line];
859 /*
860 * Compute the number of interrupts in the receive ring.
861 * If the count is overlarge, we lost some events, and
862 * must advance to the first valid one. It may get
863 * overwritten if more data are arriving, but this is
864 * too expensive to check and gains nothing (we already
865 * lost out; all we can do at this point is trade one
866 * kind of loss for another).
867 */
868 n -= get;
869 if (n > ZLRB_RING_SIZE) {
870 zsoverrun(unit, &cs->cs_rotime, "ring");
871 get += n - ZLRB_RING_SIZE;
872 n = ZLRB_RING_SIZE;
873 }
874 while (--n >= 0) {
875 /* race to keep ahead of incoming interrupts */
876 c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
877 switch (ZRING_TYPE(c)) {
878
879 case ZRING_RINT:
880 c = ZRING_VALUE(c);
881 if (c & ZSRR1_DO)
882 zsoverrun(unit, &cs->cs_fotime, "fifo");
883 cc = c >> 8;
884 if (c & ZSRR1_FE)
885 cc |= TTY_FE;
886 if (c & ZSRR1_PE)
887 cc |= TTY_PE;
888 /*
889 * this should be done through
890 * bstreams XXX gag choke
891 */
892 if (unit == ZS_KBD)
893 kbd_rint(cc);
894 else if (unit == ZS_MOUSE)
895 ms_rint(cc);
896 else
897 line->l_rint(cc, tp);
898 break;
899
900 case ZRING_XINT:
901 /*
902 * Transmit done: change registers and resume,
903 * or clear BUSY.
904 */
905 if (cs->cs_heldchange) {
906 s = splzs();
907 c = zc->zc_csr;
908 if ((c & ZSRR0_DCD) == 0)
909 cs->cs_preg[3] &= ~ZSWR3_HFC;
910 bcopy((caddr_t)cs->cs_preg,
911 (caddr_t)cs->cs_creg, 16);
912 zs_loadchannelregs(zc, cs->cs_creg);
913 splx(s);
914 cs->cs_heldchange = 0;
915 if (cs->cs_heldtbc &&
916 (tp->t_state & TS_TTSTOP) == 0) {
917 cs->cs_tbc = cs->cs_heldtbc - 1;
918 zc->zc_data = *cs->cs_tba++;
919 goto again;
920 }
921 }
922 tp->t_state &= ~TS_BUSY;
923 if (tp->t_state & TS_FLUSH)
924 tp->t_state &= ~TS_FLUSH;
925 else
926 ndflush(&tp->t_outq,
927 cs->cs_tba - tp->t_outq.c_cf);
928 line->l_start(tp);
929 break;
930
931 case ZRING_SINT:
932 /*
933 * Status line change. HFC bit is run in
934 * hardware interrupt, to avoid locking
935 * at splzs here.
936 */
937 c = ZRING_VALUE(c);
938 if ((c ^ cs->cs_rr0) & ZSRR0_DCD) {
939 cc = (c & ZSRR0_DCD) != 0;
940 if (line->l_modem(tp, cc) == 0)
941 zs_modem(cs, cc);
942 }
943 cs->cs_rr0 = c;
944 break;
945
946 default:
947 log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
948 unit >> 1, (unit & 1) + 'a', c);
949 break;
950 }
951 }
952 cs->cs_rbget = get;
953 goto again;
954 }
955 return (1);
956 }
957
958 int
959 zsioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
960 {
961 int unit = minor(dev);
962 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
963 register struct tty *tp = zi->zi_cs[unit & 1].cs_ttyp;
964 register int error;
965
966 error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p);
967 if (error >= 0)
968 return (error);
969 error = ttioctl(tp, cmd, data, flag, p);
970 if (error >= 0)
971 return (error);
972
973 switch (cmd) {
974
975 case TIOCSBRK:
976 /* FINISH ME ... need implicit TIOCCBRK in zsclose as well */
977
978 case TIOCCBRK:
979
980 case TIOCSDTR:
981
982 case TIOCCDTR:
983
984 case TIOCMSET:
985
986 case TIOCMBIS:
987
988 case TIOCMBIC:
989
990 case TIOCMGET:
991
992 default:
993 return (ENOTTY);
994 }
995 return (0);
996 }
997
998 /*
999 * Start or restart transmission.
1000 */
1001 static void
1002 zsstart(register struct tty *tp)
1003 {
1004 register struct zs_chanstate *cs;
1005 register int s, nch;
1006 int unit = minor(tp->t_dev);
1007 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1008
1009 cs = &zi->zi_cs[unit & 1];
1010 s = spltty();
1011
1012 /*
1013 * If currently active or delaying, no need to do anything.
1014 */
1015 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
1016 goto out;
1017
1018 /*
1019 * If there are sleepers, and output has drained below low
1020 * water mark, awaken.
1021 */
1022 if (tp->t_outq.c_cc <= tp->t_lowat) {
1023 if (tp->t_state & TS_ASLEEP) {
1024 tp->t_state &= ~TS_ASLEEP;
1025 wakeup((caddr_t)&tp->t_outq);
1026 }
1027 selwakeup(&tp->t_wsel);
1028 }
1029
1030 nch = ndqb(&tp->t_outq, 0); /* XXX */
1031 if (nch) {
1032 register char *p = tp->t_outq.c_cf;
1033
1034 /* mark busy, enable tx done interrupts, & send first byte */
1035 tp->t_state |= TS_BUSY;
1036 (void) splzs();
1037 cs->cs_preg[1] |= ZSWR1_TIE;
1038 cs->cs_creg[1] |= ZSWR1_TIE;
1039 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1040 cs->cs_zc->zc_data = *p;
1041 cs->cs_tba = p + 1;
1042 cs->cs_tbc = nch - 1;
1043 } else {
1044 /*
1045 * Nothing to send, turn off transmit done interrupts.
1046 * This is useful if something is doing polled output.
1047 */
1048 (void) splzs();
1049 cs->cs_preg[1] &= ~ZSWR1_TIE;
1050 cs->cs_creg[1] &= ~ZSWR1_TIE;
1051 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1052 }
1053 out:
1054 splx(s);
1055 }
1056
1057 /*
1058 * Stop output, e.g., for ^S or output flush.
1059 */
1060 void
1061 zsstop(register struct tty *tp, int flag)
1062 {
1063 register struct zs_chanstate *cs;
1064 register int s, unit = minor(tp->t_dev);
1065 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1066
1067 cs = &zi->zi_cs[unit & 1];
1068 s = splzs();
1069 if (tp->t_state & TS_BUSY) {
1070 /*
1071 * Device is transmitting; must stop it.
1072 */
1073 cs->cs_tbc = 0;
1074 if ((tp->t_state & TS_TTSTOP) == 0)
1075 tp->t_state |= TS_FLUSH;
1076 }
1077 splx(s);
1078 }
1079
1080 /*
1081 * Set ZS tty parameters from termios.
1082 *
1083 * This routine makes use of the fact that only registers
1084 * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
1085 */
1086 static int
1087 zsparam(register struct tty *tp, register struct termios *t)
1088 {
1089 int unit = minor(tp->t_dev);
1090 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1091 register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
1092 register int tmp, tmp5, cflag, s;
1093
1094 /*
1095 * Because PCLK is only run at 4.9 MHz, the fastest we
1096 * can go is 51200 baud (this corresponds to TC=1).
1097 * This is somewhat unfortunate as there is no real
1098 * reason we should not be able to handle higher rates.
1099 */
1100 tmp = t->c_ospeed;
1101 if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp))
1102 return (EINVAL);
1103 if (tmp == 0) {
1104 /* stty 0 => drop DTR and RTS */
1105 zs_modem(cs, 0);
1106 return (0);
1107 }
1108 tmp = BPS_TO_TCONST(PCLK / 16, tmp);
1109 if (tmp < 2)
1110 return (EINVAL);
1111
1112 cflag = t->c_cflag;
1113 tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp);
1114 tp->t_cflag = cflag;
1115
1116 /*
1117 * Block interrupts so that state will not
1118 * be altered until we are done setting it up.
1119 */
1120 s = splzs();
1121 cs->cs_preg[12] = tmp;
1122 cs->cs_preg[13] = tmp >> 8;
1123 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
1124 switch (cflag & CSIZE) {
1125 case CS5:
1126 tmp = ZSWR3_RX_5;
1127 tmp5 = ZSWR5_TX_5;
1128 break;
1129 case CS6:
1130 tmp = ZSWR3_RX_6;
1131 tmp5 = ZSWR5_TX_6;
1132 break;
1133 case CS7:
1134 tmp = ZSWR3_RX_7;
1135 tmp5 = ZSWR5_TX_7;
1136 break;
1137 case CS8:
1138 default:
1139 tmp = ZSWR3_RX_8;
1140 tmp5 = ZSWR5_TX_8;
1141 break;
1142 }
1143
1144 /*
1145 * Output hardware flow control on the chip is horrendous: if
1146 * carrier detect drops, the receiver is disabled. Hence we
1147 * can only do this when the carrier is on.
1148 */
1149 if (cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD)
1150 tmp |= ZSWR3_HFC | ZSWR3_RX_ENABLE;
1151 else
1152 tmp |= ZSWR3_RX_ENABLE;
1153 cs->cs_preg[3] = tmp;
1154 cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
1155
1156 tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
1157 if ((cflag & PARODD) == 0)
1158 tmp |= ZSWR4_EVENP;
1159 if (cflag & PARENB)
1160 tmp |= ZSWR4_PARENB;
1161 cs->cs_preg[4] = tmp;
1162 cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR;
1163 cs->cs_preg[10] = ZSWR10_NRZ;
1164 cs->cs_preg[11] = ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD;
1165 cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA;
1166 cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
1167
1168 /*
1169 * If nothing is being transmitted, set up new current values,
1170 * else mark them as pending.
1171 */
1172 if (cs->cs_heldchange == 0) {
1173 if (cs->cs_ttyp->t_state & TS_BUSY) {
1174 cs->cs_heldtbc = cs->cs_tbc;
1175 cs->cs_tbc = 0;
1176 cs->cs_heldchange = 1;
1177 } else {
1178 bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
1179 zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
1180 }
1181 }
1182 splx(s);
1183 return (0);
1184 }
1185
1186 /*
1187 * Raise or lower modem control (DTR/RTS) signals. If a character is
1188 * in transmission, the change is deferred.
1189 */
1190 static void
1191 zs_modem(struct zs_chanstate *cs, int onoff)
1192 {
1193 int s, bis, and;
1194
1195 if (onoff) {
1196 bis = ZSWR5_DTR | ZSWR5_RTS;
1197 and = ~0;
1198 } else {
1199 bis = 0;
1200 and = ~(ZSWR5_DTR | ZSWR5_RTS);
1201 }
1202 s = splzs();
1203 cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
1204 if (cs->cs_heldchange == 0) {
1205 if (cs->cs_ttyp->t_state & TS_BUSY) {
1206 cs->cs_heldtbc = cs->cs_tbc;
1207 cs->cs_tbc = 0;
1208 cs->cs_heldchange = 1;
1209 } else {
1210 cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
1211 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1212 }
1213 }
1214 splx(s);
1215 }
1216
1217 /*
1218 * Write the given register set to the given zs channel in the proper order.
1219 * The channel must not be transmitting at the time. The receiver will
1220 * be disabled for the time it takes to write all the registers.
1221 */
1222 static void
1223 zs_loadchannelregs(volatile struct zschan *zc, u_char *reg)
1224 {
1225 int i;
1226
1227 zc->zc_csr = ZSM_RESET_ERR; /* reset error condition */
1228 i = zc->zc_data; /* drain fifo */
1229 i = zc->zc_data;
1230 i = zc->zc_data;
1231 ZS_WRITE(zc, 4, reg[4]);
1232 ZS_WRITE(zc, 10, reg[10]);
1233 ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
1234 ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
1235 ZS_WRITE(zc, 1, reg[1]);
1236 ZS_WRITE(zc, 9, reg[9]);
1237 ZS_WRITE(zc, 11, reg[11]);
1238 ZS_WRITE(zc, 12, reg[12]);
1239 ZS_WRITE(zc, 13, reg[13]);
1240 ZS_WRITE(zc, 14, reg[14]);
1241 ZS_WRITE(zc, 15, reg[15]);
1242 ZS_WRITE(zc, 3, reg[3]);
1243 ZS_WRITE(zc, 5, reg[5]);
1244 }
1245
1246 #ifdef KGDB
1247 /*
1248 * Get a character from the given kgdb channel. Called at splhigh().
1249 */
1250 static int
1251 zs_kgdb_getc(void *arg)
1252 {
1253 register volatile struct zschan *zc = (volatile struct zschan *)arg;
1254
1255 while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
1256 continue;
1257 return (zc->zc_data);
1258 }
1259
1260 /*
1261 * Put a character to the given kgdb channel. Called at splhigh().
1262 */
1263 static void
1264 zs_kgdb_putc(void *arg, int c)
1265 {
1266 register volatile struct zschan *zc = (volatile struct zschan *)arg;
1267
1268 while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
1269 continue;
1270 zc->zc_data = c;
1271 }
1272
1273 /*
1274 * Set up for kgdb; called at boot time before configuration.
1275 * KGDB interrupts will be enabled later when zs0 is configured.
1276 */
1277 void
1278 zs_kgdb_init()
1279 {
1280 volatile struct zsdevice *addr;
1281 volatile struct zschan *zc;
1282 int unit, zs;
1283
1284 if (major(kgdb_dev) != ZSMAJOR)
1285 return;
1286 unit = minor(kgdb_dev);
1287 /*
1288 * Unit must be 0 or 1 (zs0).
1289 */
1290 if ((unsigned)unit >= ZS_KBD) {
1291 printf("zs_kgdb_init: bad minor dev %d\n", unit);
1292 return;
1293 }
1294 zs = unit >> 1;
1295 if ((addr = zsaddr[zs]) == NULL)
1296 addr = zsaddr[zs] = findzs(zs);
1297 unit &= 1;
1298 zc = unit == 0 ? &addr->zs_chan[CHAN_A] : &addr->zs_chan[CHAN_B];
1299 zs_kgdb_savedspeed = zs_getspeed(zc);
1300 printf("zs_kgdb_init: attaching zs%d%c at %d baud\n",
1301 zs, unit + 'a', kgdb_rate);
1302 zs_reset(zc, 1, kgdb_rate);
1303 kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc);
1304 }
1305 #endif /* KGDB */
1306