zs.c revision 1.34 1 /* $NetBSD: zs.c,v 1.34 1996/03/14 19:45:28 christos 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 * @(#)zs.c 8.1 (Berkeley) 7/19/93
45 */
46
47 /*
48 * Zilog Z8530 (ZSCC) driver.
49 *
50 * Runs two tty ports (ttya and ttyb) on zs0,
51 * and runs a keyboard and mouse on zs1.
52 *
53 * This driver knows far too much about chip to usage mappings.
54 */
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/proc.h>
58 #include <sys/device.h>
59 #include <sys/file.h>
60 #include <sys/ioctl.h>
61 #include <sys/malloc.h>
62 #include <sys/tty.h>
63 #include <sys/time.h>
64 #include <sys/kernel.h>
65 #include <sys/syslog.h>
66
67 #include <machine/autoconf.h>
68 #include <machine/cpu.h>
69
70 #include <sparc/sparc/vaddrs.h>
71 #include <sparc/sparc/auxreg.h>
72
73
74 #include <machine/kbd.h>
75 #include <dev/ic/z8530reg.h>
76
77 #include <sparc/dev/zsvar.h>
78 #include <sparc/dev/dev_conf.h>
79
80 #ifdef KGDB
81 #include <machine/remote-sl.h>
82 #endif
83
84 #define ZSMAJOR 12 /* XXX */
85
86 #define ZS_KBD 2 /* XXX */
87 #define ZS_MOUSE 3 /* XXX */
88
89 /* the magic number below was stolen from the Sprite source. */
90 #define PCLK (19660800/4) /* PCLK pin input clock rate */
91
92 /*
93 * Select software interrupt bit based on TTY ipl.
94 */
95 #if PIL_TTY == 1
96 # define IE_ZSSOFT IE_L1
97 #elif PIL_TTY == 4
98 # define IE_ZSSOFT IE_L4
99 #elif PIL_TTY == 6
100 # define IE_ZSSOFT IE_L6
101 #else
102 # error "no suitable software interrupt bit"
103 #endif
104
105 /*
106 * Software state per found chip. This would be called `zs_softc',
107 * but the previous driver had a rather different zs_softc....
108 */
109 struct zsinfo {
110 struct device zi_dev; /* base device */
111 volatile struct zsdevice *zi_zs;/* chip registers */
112 struct zs_chanstate zi_cs[2]; /* channel A and B software state */
113 };
114
115 /* Definition of the driver for autoconfig. */
116 static int zsmatch __P((struct device *, void *, void *));
117 static void zsattach __P((struct device *, struct device *, void *));
118 struct cfdriver zscd =
119 { NULL, "zs", zsmatch, zsattach, DV_TTY, sizeof(struct zsinfo) };
120
121 /* Interrupt handlers. */
122 static int zshard __P((void *));
123 static struct intrhand levelhard = { zshard };
124 static int zssoft __P((void *));
125 static struct intrhand levelsoft = { zssoft };
126
127 struct zs_chanstate *zslist;
128
129 /* Routines called from other code. */
130 static void zsiopen __P((struct tty *));
131 static void zsiclose __P((struct tty *));
132 static void zsstart __P((struct tty *));
133 static int zsparam __P((struct tty *, struct termios *));
134
135 /* Routines purely local to this driver. */
136 static int zs_getspeed __P((volatile struct zschan *));
137 #ifdef KGDB
138 static void zs_reset __P((volatile struct zschan *, int, int));
139 #endif
140 static void zs_modem __P((struct zs_chanstate *, int));
141 static void zs_loadchannelregs __P((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 void zscnputc __P((int)); /* console putc function */
147 static volatile struct zschan *zs_conschan;
148 static struct tty *zs_checkcons __P((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 __P((int, struct zs_chanstate *, struct tty *));
155 void zskgdb __P((int));
156 static int zs_kgdb_getc __P((void *));
157 static void zs_kgdb_putc __P((void *, int));
158 #endif
159
160 static int zsrint __P((struct zs_chanstate *, volatile struct zschan *));
161 static int zsxint __P((struct zs_chanstate *, volatile struct zschan *));
162 static int zssint __P((struct zs_chanstate *, volatile struct zschan *));
163
164 void zsabort __P((void));
165 static void zsoverrun __P((int, long *, char *));
166
167 static volatile struct zsdevice *zsaddr[NZS]; /* XXX, but saves work */
168
169 /*
170 * Console keyboard L1-A processing is done in the hardware interrupt code,
171 * so we need to duplicate some of the console keyboard decode state. (We
172 * must not use the regular state as the hardware code keeps ahead of the
173 * software state: the software state tracks the most recent ring input but
174 * the hardware state tracks the most recent ZSCC input.) See also kbd.h.
175 */
176 static struct conk_state { /* console keyboard state */
177 char conk_id; /* true => ID coming up (console only) */
178 char conk_l1; /* true => L1 pressed (console only) */
179 } zsconk_state;
180
181 int zshardscope;
182 int zsshortcuts; /* number of "shortcut" software interrupts */
183
184 #ifdef SUN4
185 static u_int zs_read __P((volatile struct zschan *, u_int reg));
186 static u_int zs_write __P((volatile struct zschan *, u_int, u_int));
187
188 static u_int
189 zs_read(zc, reg)
190 volatile struct zschan *zc;
191 u_int reg;
192 {
193 u_char val;
194
195 zc->zc_csr = reg;
196 ZS_DELAY();
197 val = zc->zc_csr;
198 ZS_DELAY();
199 return val;
200 }
201
202 static u_int
203 zs_write(zc, reg, val)
204 volatile struct zschan *zc;
205 u_int reg, val;
206 {
207 zc->zc_csr = reg;
208 ZS_DELAY();
209 zc->zc_csr = val;
210 ZS_DELAY();
211 return val;
212 }
213 #endif /* SUN4 */
214
215 /*
216 * Match slave number to zs unit number, so that misconfiguration will
217 * not set up the keyboard as ttya, etc.
218 */
219 static int
220 zsmatch(parent, vcf, aux)
221 struct device *parent;
222 void *vcf, *aux;
223 {
224 struct cfdata *cf = vcf;
225 struct confargs *ca = aux;
226 struct romaux *ra = &ca->ca_ra;
227
228 if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
229 return (0);
230 if ((ca->ca_bustype == BUS_MAIN && !CPU_ISSUN4) ||
231 (ca->ca_bustype == BUS_OBIO && CPU_ISSUN4M))
232 return (getpropint(ra->ra_node, "slave", -2) == cf->cf_unit);
233 ra->ra_len = NBPG;
234 return (probeget(ra->ra_vaddr, 1) != -1);
235 }
236
237 /*
238 * Attach a found zs.
239 *
240 * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
241 * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
242 */
243 static void
244 zsattach(parent, dev, aux)
245 struct device *parent;
246 struct device *dev;
247 void *aux;
248 {
249 register int zs = dev->dv_unit, unit;
250 register struct zsinfo *zi;
251 register struct zs_chanstate *cs;
252 register volatile struct zsdevice *addr;
253 register struct tty *tp, *ctp;
254 register struct confargs *ca = aux;
255 register struct romaux *ra = &ca->ca_ra;
256 int pri;
257 static int didintr, prevpri;
258 int ringsize;
259
260 if ((addr = zsaddr[zs]) == NULL)
261 addr = zsaddr[zs] = findzs(zs);
262 if (ca->ca_bustype==BUS_MAIN)
263 if ((void *)addr != ra->ra_vaddr)
264 panic("zsattach");
265 if (ra->ra_nintr != 1) {
266 printf(": expected 1 interrupt, got %d\n", ra->ra_nintr);
267 return;
268 }
269 pri = ra->ra_intr[0].int_pri;
270 printf(" pri %d, softpri %d\n", pri, PIL_TTY);
271 if (!didintr) {
272 didintr = 1;
273 prevpri = pri;
274 intr_establish(pri, &levelhard);
275 intr_establish(PIL_TTY, &levelsoft);
276 } else if (pri != prevpri)
277 panic("broken zs interrupt scheme");
278 zi = (struct zsinfo *)dev;
279 zi->zi_zs = addr;
280 unit = zs * 2;
281 cs = zi->zi_cs;
282 cs->cs_ttyp = tp = ttymalloc();
283
284 /* link into interrupt list with order (A,B) (B=A+1) */
285 cs[0].cs_next = &cs[1];
286 cs[1].cs_next = zslist;
287 zslist = cs;
288
289 cs->cs_unit = unit;
290 cs->cs_speed = zs_getspeed(&addr->zs_chan[ZS_CHAN_A]);
291 cs->cs_zc = &addr->zs_chan[ZS_CHAN_A];
292 tp->t_dev = makedev(ZSMAJOR, unit);
293 tp->t_oproc = zsstart;
294 tp->t_param = zsparam;
295 if ((ctp = zs_checkcons(zi, unit, cs)) != NULL)
296 cs->cs_ttyp = tp = ctp;
297 #ifdef KGDB
298 if (ctp == NULL)
299 zs_checkkgdb(unit, cs, tp);
300 #endif
301 if (unit == ZS_KBD) {
302 /*
303 * Keyboard: tell /dev/kbd driver how to talk to us.
304 */
305 tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
306 tp->t_cflag = CS8;
307 kbd_serial(tp, zsiopen, zsiclose);
308 cs->cs_conk = 1; /* do L1-A processing */
309 ringsize = 128;
310 } else
311 ringsize = 4096;
312
313 cs->cs_ringmask = ringsize - 1;
314 cs->cs_rbuf = malloc((u_long)ringsize * sizeof(*cs->cs_rbuf),
315 M_DEVBUF, M_NOWAIT);
316 unit++;
317 cs++;
318 cs->cs_ttyp = tp = ttymalloc();
319 cs->cs_unit = unit;
320 cs->cs_speed = zs_getspeed(&addr->zs_chan[ZS_CHAN_B]);
321 cs->cs_zc = &addr->zs_chan[ZS_CHAN_B];
322 tp->t_dev = makedev(ZSMAJOR, unit);
323 tp->t_oproc = zsstart;
324 tp->t_param = zsparam;
325 if ((ctp = zs_checkcons(zi, unit, cs)) != NULL)
326 cs->cs_ttyp = tp = ctp;
327 #ifdef KGDB
328 if (ctp == NULL)
329 zs_checkkgdb(unit, cs, tp);
330 #endif
331 if (unit == ZS_MOUSE) {
332 /*
333 * Mouse: tell /dev/mouse driver how to talk to us.
334 */
335 tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
336 tp->t_cflag = CS8;
337 ms_serial(tp, zsiopen, zsiclose);
338 ringsize = 128;
339 } else
340 ringsize = 4096;
341 cs->cs_ringmask = ringsize - 1;
342 cs->cs_rbuf = malloc((u_long)ringsize * sizeof(*cs->cs_rbuf),
343 M_DEVBUF, M_NOWAIT);
344 }
345
346 #ifdef KGDB
347 /*
348 * Put a channel in a known state. Interrupts may be left disabled
349 * or enabled, as desired.
350 */
351 static void
352 zs_reset(zc, inten, speed)
353 volatile struct zschan *zc;
354 int inten, speed;
355 {
356 int tconst;
357 static u_char reg[16] = {
358 0,
359 0,
360 0,
361 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
362 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
363 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
364 0,
365 0,
366 0,
367 0,
368 ZSWR10_NRZ,
369 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
370 0,
371 0,
372 ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
373 ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
374 };
375
376 reg[9] = inten ? ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR : ZSWR9_NO_VECTOR;
377 tconst = BPS_TO_TCONST(PCLK / 16, speed);
378 reg[12] = tconst;
379 reg[13] = tconst >> 8;
380 zs_loadchannelregs(zc, reg);
381 }
382 #endif
383
384 /*
385 * Declare the given tty (which is in fact &cons) as a console input
386 * or output. This happens before the zs chip is attached; the hookup
387 * is finished later, in zs_setcons() below.
388 *
389 * This is used only for ports a and b. The console keyboard is decoded
390 * independently (we always send unit-2 input to /dev/kbd, which will
391 * direct it to /dev/console if appropriate).
392 */
393 void
394 zsconsole(tp, unit, out, fnstop)
395 register struct tty *tp;
396 register int unit;
397 int out;
398 int (**fnstop) __P((struct tty *, int));
399 {
400 int zs;
401 volatile struct zsdevice *addr;
402
403 if (unit >= ZS_KBD)
404 panic("zsconsole");
405 if (out) {
406 zs_consout = unit;
407 zs = unit >> 1;
408 if ((addr = zsaddr[zs]) == NULL)
409 addr = zsaddr[zs] = findzs(zs);
410 zs_conschan = (unit & 1) == 0 ? &addr->zs_chan[ZS_CHAN_A] :
411 &addr->zs_chan[ZS_CHAN_B];
412 v_putc = zscnputc;
413 } else
414 zs_consin = unit;
415 if(fnstop)
416 *fnstop = &zsstop;
417 zs_ctty = tp;
418 }
419
420 /*
421 * Polled console output putchar.
422 */
423 static void
424 zscnputc(c)
425 int c;
426 {
427 register volatile struct zschan *zc = zs_conschan;
428 register int s;
429
430 if (c == '\n')
431 zscnputc('\r');
432 /*
433 * Must block output interrupts (i.e., raise to >= splzs) without
434 * lowering current ipl. Need a better way.
435 */
436 s = splhigh();
437 if (CPU_ISSUN4C && s <= (12 << 8)) /* XXX */
438 (void) splzs();
439 while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
440 ZS_DELAY();
441 zc->zc_data = c;
442 ZS_DELAY();
443 splx(s);
444 }
445
446 /*
447 * Set up the given unit as console input, output, both, or neither, as
448 * needed. Return console tty if it is to receive console input.
449 */
450 static struct tty *
451 zs_checkcons(zi, unit, cs)
452 struct zsinfo *zi;
453 int unit;
454 struct zs_chanstate *cs;
455 {
456 register struct tty *tp;
457 char *i, *o;
458
459 if ((tp = zs_ctty) == NULL) /* XXX */
460 return (0);
461 i = zs_consin == unit ? "input" : NULL;
462 o = zs_consout == unit ? "output" : NULL;
463 if (i == NULL && o == NULL)
464 return (0);
465
466 /* rewire the minor device (gack) */
467 tp->t_dev = makedev(major(tp->t_dev), unit);
468
469 /*
470 * Rewire input and/or output. Note that baud rate reflects
471 * input settings, not output settings, but we can do no better
472 * if the console is split across two ports.
473 *
474 * XXX split consoles don't work anyway -- this needs to be
475 * thrown away and redone
476 */
477 if (i) {
478 tp->t_param = zsparam;
479 tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
480 tp->t_cflag = CS8;
481 ttsetwater(tp);
482 }
483 if (o) {
484 tp->t_oproc = zsstart;
485 }
486 printf("%s%c: console %s\n",
487 zi->zi_dev.dv_xname, (unit & 1) + 'a', i ? (o ? "i/o" : i) : o);
488 cs->cs_consio = 1;
489 cs->cs_brkabort = 1;
490 return (tp);
491 }
492
493 #ifdef KGDB
494 /*
495 * The kgdb zs port, if any, was altered at boot time (see zs_kgdb_init).
496 * Pick up the current speed and character size and restore the original
497 * speed.
498 */
499 static void
500 zs_checkkgdb(unit, cs, tp)
501 int unit;
502 struct zs_chanstate *cs;
503 struct tty *tp;
504 {
505
506 if (kgdb_dev == makedev(ZSMAJOR, unit)) {
507 tp->t_ispeed = tp->t_ospeed = kgdb_rate;
508 tp->t_cflag = CS8;
509 cs->cs_kgdb = 1;
510 cs->cs_speed = zs_kgdb_savedspeed;
511 (void) zsparam(tp, &tp->t_termios);
512 }
513 }
514 #endif
515
516 /*
517 * Compute the current baud rate given a ZSCC channel.
518 */
519 static int
520 zs_getspeed(zc)
521 register volatile struct zschan *zc;
522 {
523 register int tconst;
524
525 tconst = ZS_READ(zc, 12);
526 tconst |= ZS_READ(zc, 13) << 8;
527 return (TCONST_TO_BPS(PCLK / 16, tconst));
528 }
529
530
531 /*
532 * Do an internal open.
533 */
534 static void
535 zsiopen(tp)
536 struct tty *tp;
537 {
538
539 (void) zsparam(tp, &tp->t_termios);
540 ttsetwater(tp);
541 tp->t_state = TS_ISOPEN | TS_CARR_ON;
542 }
543
544 /*
545 * Do an internal close. Eventually we should shut off the chip when both
546 * ports on it are closed.
547 */
548 static void
549 zsiclose(tp)
550 struct tty *tp;
551 {
552
553 ttylclose(tp, 0); /* ??? */
554 ttyclose(tp); /* ??? */
555 tp->t_state = 0;
556 }
557
558
559 /*
560 * Open a zs serial port. This interface may not be used to open
561 * the keyboard and mouse ports. (XXX)
562 */
563 int
564 zsopen(dev, flags, mode, p)
565 dev_t dev;
566 int flags;
567 int mode;
568 struct proc *p;
569 {
570 register struct tty *tp;
571 register struct zs_chanstate *cs;
572 struct zsinfo *zi;
573 int unit = minor(dev), zs = unit >> 1, error, s;
574
575 if (zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL ||
576 unit == ZS_KBD || unit == ZS_MOUSE)
577 return (ENXIO);
578 cs = &zi->zi_cs[unit & 1];
579 if (cs->cs_consio)
580 return (ENXIO); /* ??? */
581 tp = cs->cs_ttyp;
582 s = spltty();
583 if ((tp->t_state & TS_ISOPEN) == 0) {
584 ttychars(tp);
585 if (tp->t_ispeed == 0) {
586 tp->t_iflag = TTYDEF_IFLAG;
587 tp->t_oflag = TTYDEF_OFLAG;
588 tp->t_cflag = TTYDEF_CFLAG;
589 tp->t_lflag = TTYDEF_LFLAG;
590 tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
591 }
592 (void) zsparam(tp, &tp->t_termios);
593 ttsetwater(tp);
594 } else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
595 splx(s);
596 return (EBUSY);
597 }
598 error = 0;
599 for (;;) {
600 register int rr0;
601
602 /* loop, turning on the device, until carrier present */
603 zs_modem(cs, 1);
604 /* May never get status intr if carrier already on. -gwr */
605 rr0 = cs->cs_zc->zc_csr;
606 ZS_DELAY();
607 if ((rr0 & ZSRR0_DCD) || cs->cs_softcar)
608 tp->t_state |= TS_CARR_ON;
609 if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL ||
610 tp->t_state & TS_CARR_ON)
611 break;
612 tp->t_state |= TS_WOPEN;
613 error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
614 ttopen, 0);
615 if (error) {
616 if (!(tp->t_state & TS_ISOPEN)) {
617 zs_modem(cs, 0);
618 tp->t_state &= ~TS_WOPEN;
619 ttwakeup(tp);
620 }
621 splx(s);
622 return error;
623 }
624 }
625 splx(s);
626 if (error == 0)
627 error = linesw[tp->t_line].l_open(dev, tp);
628 if (error)
629 zs_modem(cs, 0);
630 return (error);
631 }
632
633 /*
634 * Close a zs serial port.
635 */
636 int
637 zsclose(dev, flags, mode, p)
638 dev_t dev;
639 int flags;
640 int mode;
641 struct proc *p;
642 {
643 register struct zs_chanstate *cs;
644 register struct tty *tp;
645 struct zsinfo *zi;
646 int unit = minor(dev), s;
647
648 zi = zscd.cd_devs[unit >> 1];
649 cs = &zi->zi_cs[unit & 1];
650 tp = cs->cs_ttyp;
651 linesw[tp->t_line].l_close(tp, flags);
652 if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
653 (tp->t_state & TS_ISOPEN) == 0) {
654 zs_modem(cs, 0);
655 /* hold low for 1 second */
656 (void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
657 }
658 if (cs->cs_creg[5] & ZSWR5_BREAK)
659 {
660 s = splzs();
661 cs->cs_preg[5] &= ~ZSWR5_BREAK;
662 cs->cs_creg[5] &= ~ZSWR5_BREAK;
663 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
664 splx(s);
665 }
666 ttyclose(tp);
667 #ifdef KGDB
668 /* Reset the speed if we're doing kgdb on this port */
669 if (cs->cs_kgdb) {
670 tp->t_ispeed = tp->t_ospeed = kgdb_rate;
671 (void) zsparam(tp, &tp->t_termios);
672 }
673 #endif
674 return (0);
675 }
676
677 /*
678 * Read/write zs serial port.
679 */
680 int
681 zsread(dev, uio, flags)
682 dev_t dev;
683 struct uio *uio;
684 int flags;
685 {
686 register struct zs_chanstate *cs;
687 register struct zsinfo *zi;
688 register struct tty *tp;
689 int unit = minor(dev);
690
691 zi = zscd.cd_devs[unit >> 1];
692 cs = &zi->zi_cs[unit & 1];
693 tp = cs->cs_ttyp;
694
695 return (linesw[tp->t_line].l_read(tp, uio, flags));
696
697 }
698
699 int
700 zswrite(dev, uio, flags)
701 dev_t dev;
702 struct uio *uio;
703 int flags;
704 {
705 register struct zs_chanstate *cs;
706 register struct zsinfo *zi;
707 register struct tty *tp;
708 int unit = minor(dev);
709
710 zi = zscd.cd_devs[unit >> 1];
711 cs = &zi->zi_cs[unit & 1];
712 tp = cs->cs_ttyp;
713
714 return (linesw[tp->t_line].l_write(tp, uio, flags));
715 }
716
717 struct tty *
718 zstty(dev)
719 dev_t dev;
720 {
721 register struct zs_chanstate *cs;
722 register struct zsinfo *zi;
723 int unit = minor(dev);
724
725 zi = zscd.cd_devs[unit >> 1];
726 cs = &zi->zi_cs[unit & 1];
727
728 return (cs->cs_ttyp);
729
730 }
731
732 /*
733 * ZS hardware interrupt. Scan all ZS channels. NB: we know here that
734 * channels are kept in (A,B) pairs.
735 *
736 * Do just a little, then get out; set a software interrupt if more
737 * work is needed.
738 *
739 * We deliberately ignore the vectoring Zilog gives us, and match up
740 * only the number of `reset interrupt under service' operations, not
741 * the order.
742 */
743 /* ARGSUSED */
744 int
745 zshard(intrarg)
746 void *intrarg;
747 {
748 register struct zs_chanstate *a;
749 #define b (a + 1)
750 register volatile struct zschan *zc;
751 register int rr3, intflags = 0, v, i, ringmask;
752 static int zsrint(struct zs_chanstate *, volatile struct zschan *);
753 static int zsxint(struct zs_chanstate *, volatile struct zschan *);
754 static int zssint(struct zs_chanstate *, volatile struct zschan *);
755
756 for (a = zslist; a != NULL; a = b->cs_next) {
757 ringmask = a->cs_ringmask;
758 rr3 = ZS_READ(a->cs_zc, 3);
759 if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
760 intflags |= 2;
761 zc = a->cs_zc;
762 i = a->cs_rbput;
763 if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
764 a->cs_rbuf[i++ & ringmask] = v;
765 intflags |= 1;
766 }
767 if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
768 a->cs_rbuf[i++ & ringmask] = v;
769 intflags |= 1;
770 }
771 if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
772 a->cs_rbuf[i++ & ringmask] = v;
773 intflags |= 1;
774 }
775 a->cs_rbput = i;
776 }
777 if (rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
778 intflags |= 2;
779 zc = b->cs_zc;
780 i = b->cs_rbput;
781 if (rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
782 b->cs_rbuf[i++ & ringmask] = v;
783 intflags |= 1;
784 }
785 if (rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
786 b->cs_rbuf[i++ & ringmask] = v;
787 intflags |= 1;
788 }
789 if (rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
790 b->cs_rbuf[i++ & ringmask] = v;
791 intflags |= 1;
792 }
793 b->cs_rbput = i;
794 }
795 }
796 #undef b
797
798 if (intflags & 1) {
799 if (CPU_ISSUN4COR4M) {
800 /* XXX -- but this will go away when zshard moves to locore.s */
801 struct clockframe *p = intrarg;
802
803 if ((p->psr & PSR_PIL) < (PIL_TTY << 8)) {
804 zsshortcuts++;
805 (void) spltty();
806 if (zshardscope) {
807 LED_ON;
808 LED_OFF;
809 }
810 return (zssoft(intrarg));
811 }
812 }
813
814 #if defined(SUN4M)
815 if (CPU_ISSUN4M)
816 raise(0, PIL_TTY);
817 else
818 #endif
819 ienab_bis(IE_ZSSOFT);
820 }
821 return (intflags & 2);
822 }
823
824 static int
825 zsrint(cs, zc)
826 register struct zs_chanstate *cs;
827 register volatile struct zschan *zc;
828 {
829 register int c = zc->zc_data;
830
831 ZS_DELAY();
832 if (cs->cs_conk) {
833 register struct conk_state *conk = &zsconk_state;
834
835 /*
836 * Check here for console abort function, so that we
837 * can abort even when interrupts are locking up the
838 * machine.
839 */
840 if (c == KBD_RESET) {
841 conk->conk_id = 1; /* ignore next byte */
842 conk->conk_l1 = 0;
843 } else if (conk->conk_id)
844 conk->conk_id = 0; /* stop ignoring bytes */
845 else if (c == KBD_L1)
846 conk->conk_l1 = 1; /* L1 went down */
847 else if (c == (KBD_L1|KBD_UP))
848 conk->conk_l1 = 0; /* L1 went up */
849 else if (c == KBD_A && conk->conk_l1) {
850 zsabort();
851 conk->conk_l1 = 0; /* we never see the up */
852 goto clearit; /* eat the A after L1-A */
853 }
854 }
855 #ifdef KGDB
856 if (c == FRAME_START && cs->cs_kgdb &&
857 (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) {
858 zskgdb(cs->cs_unit);
859 goto clearit;
860 }
861 #endif
862 /* compose receive character and status */
863 c <<= 8;
864 c |= ZS_READ(zc, 1);
865
866 /* clear receive error & interrupt condition */
867 zc->zc_csr = ZSWR0_RESET_ERRORS;
868 ZS_DELAY();
869 zc->zc_csr = ZSWR0_CLR_INTR;
870 ZS_DELAY();
871
872 return (ZRING_MAKE(ZRING_RINT, c));
873
874 clearit:
875 zc->zc_csr = ZSWR0_RESET_ERRORS;
876 ZS_DELAY();
877 zc->zc_csr = ZSWR0_CLR_INTR;
878 ZS_DELAY();
879 return (0);
880 }
881
882 static int
883 zsxint(cs, zc)
884 register struct zs_chanstate *cs;
885 register volatile struct zschan *zc;
886 {
887 register int i = cs->cs_tbc;
888
889 if (i == 0) {
890 zc->zc_csr = ZSWR0_RESET_TXINT;
891 ZS_DELAY();
892 zc->zc_csr = ZSWR0_CLR_INTR;
893 ZS_DELAY();
894 return (ZRING_MAKE(ZRING_XINT, 0));
895 }
896 cs->cs_tbc = i - 1;
897 zc->zc_data = *cs->cs_tba++;
898 ZS_DELAY();
899 zc->zc_csr = ZSWR0_CLR_INTR;
900 ZS_DELAY();
901 return (0);
902 }
903
904 static int
905 zssint(cs, zc)
906 register struct zs_chanstate *cs;
907 register volatile struct zschan *zc;
908 {
909 register int rr0;
910
911 rr0 = zc->zc_csr;
912 ZS_DELAY();
913 zc->zc_csr = ZSWR0_RESET_STATUS;
914 ZS_DELAY();
915 zc->zc_csr = ZSWR0_CLR_INTR;
916 ZS_DELAY();
917 /*
918 * The chip's hardware flow control is, as noted in zsreg.h,
919 * busted---if the DCD line goes low the chip shuts off the
920 * receiver (!). If we want hardware CTS flow control but do
921 * not have it, and carrier is now on, turn HFC on; if we have
922 * HFC now but carrier has gone low, turn it off.
923 */
924 if (rr0 & ZSRR0_DCD) {
925 if (cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
926 (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
927 cs->cs_creg[3] |= ZSWR3_HFC;
928 ZS_WRITE(zc, 3, cs->cs_creg[3]);
929 }
930 } else {
931 if (cs->cs_creg[3] & ZSWR3_HFC) {
932 cs->cs_creg[3] &= ~ZSWR3_HFC;
933 ZS_WRITE(zc, 3, cs->cs_creg[3]);
934 }
935 }
936 if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) {
937 /*
938 * XXX This might not be necessary. Test and
939 * delete if it isn't.
940 */
941 if (CPU_ISSUN4) {
942 while (zc->zc_csr & ZSRR0_BREAK)
943 ZS_DELAY();
944 }
945 zsabort();
946 return (0);
947 }
948 return (ZRING_MAKE(ZRING_SINT, rr0));
949 }
950
951 void
952 zsabort()
953 {
954
955 #ifdef DDB
956 Debugger();
957 #else
958 printf("stopping on keyboard abort\n");
959 callrom();
960 #endif
961 }
962
963 #ifdef KGDB
964 /*
965 * KGDB framing character received: enter kernel debugger. This probably
966 * should time out after a few seconds to avoid hanging on spurious input.
967 */
968 void
969 zskgdb(unit)
970 int unit;
971 {
972
973 printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a');
974 kgdb_connect(1);
975 }
976 #endif
977
978 /*
979 * Print out a ring or fifo overrun error message.
980 */
981 static void
982 zsoverrun(unit, ptime, what)
983 int unit;
984 long *ptime;
985 char *what;
986 {
987
988 if (*ptime != time.tv_sec) {
989 *ptime = time.tv_sec;
990 log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
991 (unit & 1) + 'a', what);
992 }
993 }
994
995 /*
996 * ZS software interrupt. Scan all channels for deferred interrupts.
997 */
998 int
999 zssoft(arg)
1000 void *arg;
1001 {
1002 register struct zs_chanstate *cs;
1003 register volatile struct zschan *zc;
1004 register struct linesw *line;
1005 register struct tty *tp;
1006 register int get, n, c, cc, unit, s, ringmask, ringsize;
1007 int retval = 0;
1008
1009 for (cs = zslist; cs != NULL; cs = cs->cs_next) {
1010 ringmask = cs->cs_ringmask;
1011 get = cs->cs_rbget;
1012 again:
1013 n = cs->cs_rbput; /* atomic */
1014 if (get == n) /* nothing more on this line */
1015 continue;
1016 retval = 1;
1017 unit = cs->cs_unit; /* set up to handle interrupts */
1018 zc = cs->cs_zc;
1019 tp = cs->cs_ttyp;
1020 line = &linesw[tp->t_line];
1021 /*
1022 * Compute the number of interrupts in the receive ring.
1023 * If the count is overlarge, we lost some events, and
1024 * must advance to the first valid one. It may get
1025 * overwritten if more data are arriving, but this is
1026 * too expensive to check and gains nothing (we already
1027 * lost out; all we can do at this point is trade one
1028 * kind of loss for another).
1029 */
1030 ringsize = ringmask + 1;
1031 n -= get;
1032 if (n > ringsize) {
1033 zsoverrun(unit, &cs->cs_rotime, "ring");
1034 get += n - ringsize;
1035 n = ringsize;
1036 }
1037 while (--n >= 0) {
1038 /* race to keep ahead of incoming interrupts */
1039 c = cs->cs_rbuf[get++ & ringmask];
1040 switch (ZRING_TYPE(c)) {
1041
1042 case ZRING_RINT:
1043 c = ZRING_VALUE(c);
1044 if (c & ZSRR1_DO)
1045 zsoverrun(unit, &cs->cs_fotime, "fifo");
1046 cc = c >> 8;
1047 if (c & ZSRR1_FE)
1048 cc |= TTY_FE;
1049 if (c & ZSRR1_PE)
1050 cc |= TTY_PE;
1051 /*
1052 * this should be done through
1053 * bstreams XXX gag choke
1054 */
1055 if (unit == ZS_KBD)
1056 kbd_rint(cc);
1057 else if (unit == ZS_MOUSE)
1058 ms_rint(cc);
1059 else
1060 line->l_rint(cc, tp);
1061 break;
1062
1063 case ZRING_XINT:
1064 /*
1065 * Transmit done: change registers and resume,
1066 * or clear BUSY.
1067 */
1068 if (cs->cs_heldchange) {
1069 s = splzs();
1070 c = zc->zc_csr;
1071 ZS_DELAY();
1072 if ((c & ZSRR0_DCD) == 0)
1073 cs->cs_preg[3] &= ~ZSWR3_HFC;
1074 bcopy((caddr_t)cs->cs_preg,
1075 (caddr_t)cs->cs_creg, 16);
1076 zs_loadchannelregs(zc, cs->cs_creg);
1077 splx(s);
1078 cs->cs_heldchange = 0;
1079 if (cs->cs_heldtbc &&
1080 (tp->t_state & TS_TTSTOP) == 0) {
1081 cs->cs_tbc = cs->cs_heldtbc - 1;
1082 zc->zc_data = *cs->cs_tba++;
1083 ZS_DELAY();
1084 goto again;
1085 }
1086 }
1087 tp->t_state &= ~TS_BUSY;
1088 if (tp->t_state & TS_FLUSH)
1089 tp->t_state &= ~TS_FLUSH;
1090 else
1091 ndflush(&tp->t_outq,
1092 cs->cs_tba - (caddr_t)tp->t_outq.c_cf);
1093 line->l_start(tp);
1094 break;
1095
1096 case ZRING_SINT:
1097 /*
1098 * Status line change. HFC bit is run in
1099 * hardware interrupt, to avoid locking
1100 * at splzs here.
1101 */
1102 c = ZRING_VALUE(c);
1103 if ((c ^ cs->cs_rr0) & ZSRR0_DCD) {
1104 cc = (c & ZSRR0_DCD) != 0;
1105 if (line->l_modem(tp, cc) == 0)
1106 zs_modem(cs, cc);
1107 }
1108 cs->cs_rr0 = c;
1109 break;
1110
1111 default:
1112 log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
1113 unit >> 1, (unit & 1) + 'a', c);
1114 break;
1115 }
1116 }
1117 cs->cs_rbget = get;
1118 goto again;
1119 }
1120 return (retval);
1121 }
1122
1123 int
1124 zsioctl(dev, cmd, data, flag, p)
1125 dev_t dev;
1126 u_long cmd;
1127 caddr_t data;
1128 int flag;
1129 struct proc *p;
1130 {
1131 int unit = minor(dev);
1132 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1133 register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
1134 register struct tty *tp = cs->cs_ttyp;
1135 register int error, s;
1136
1137 error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p);
1138 if (error >= 0)
1139 return (error);
1140 error = ttioctl(tp, cmd, data, flag, p);
1141 if (error >= 0)
1142 return (error);
1143
1144 switch (cmd) {
1145 case TIOCSBRK:
1146 s = splzs();
1147 cs->cs_preg[5] |= ZSWR5_BREAK;
1148 cs->cs_creg[5] |= ZSWR5_BREAK;
1149 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1150 splx(s);
1151 break;
1152 case TIOCCBRK:
1153 s = splzs();
1154 cs->cs_preg[5] &= ~ZSWR5_BREAK;
1155 cs->cs_creg[5] &= ~ZSWR5_BREAK;
1156 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1157 splx(s);
1158 break;
1159 case TIOCGFLAGS: {
1160 int bits = 0;
1161
1162 if (cs->cs_softcar)
1163 bits |= TIOCFLAG_SOFTCAR;
1164 if (cs->cs_creg[15] & ZSWR15_DCD_IE)
1165 bits |= TIOCFLAG_CLOCAL;
1166 if (cs->cs_creg[3] & ZSWR3_HFC)
1167 bits |= TIOCFLAG_CRTSCTS;
1168 *(int *)data = bits;
1169 break;
1170 }
1171 case TIOCSFLAGS: {
1172 int userbits;
1173
1174 error = suser(p->p_ucred, &p->p_acflag);
1175 if (error != 0)
1176 return (EPERM);
1177
1178 userbits = *(int *)data;
1179
1180 /*
1181 * can have `local' or `softcar', and `rtscts' or `mdmbuf'
1182 # defaulting to software flow control.
1183 */
1184 if (userbits & TIOCFLAG_SOFTCAR && userbits & TIOCFLAG_CLOCAL)
1185 return(EINVAL);
1186 if (userbits & TIOCFLAG_MDMBUF) /* don't support this (yet?) */
1187 return(ENXIO);
1188
1189 s = splzs();
1190 if ((userbits & TIOCFLAG_SOFTCAR) || cs->cs_consio) {
1191 cs->cs_softcar = 1; /* turn on softcar */
1192 cs->cs_preg[15] &= ~ZSWR15_DCD_IE; /* turn off dcd */
1193 cs->cs_creg[15] &= ~ZSWR15_DCD_IE;
1194 ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
1195 } else if (userbits & TIOCFLAG_CLOCAL) {
1196 cs->cs_softcar = 0; /* turn off softcar */
1197 cs->cs_preg[15] |= ZSWR15_DCD_IE; /* turn on dcd */
1198 cs->cs_creg[15] |= ZSWR15_DCD_IE;
1199 ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
1200 tp->t_termios.c_cflag |= CLOCAL;
1201 }
1202 if (userbits & TIOCFLAG_CRTSCTS) {
1203 cs->cs_preg[15] |= ZSWR15_CTS_IE;
1204 cs->cs_creg[15] |= ZSWR15_CTS_IE;
1205 ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
1206 cs->cs_preg[3] |= ZSWR3_HFC;
1207 cs->cs_creg[3] |= ZSWR3_HFC;
1208 ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
1209 tp->t_termios.c_cflag |= CRTSCTS;
1210 } else {
1211 /* no mdmbuf, so we must want software flow control */
1212 cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
1213 cs->cs_creg[15] &= ~ZSWR15_CTS_IE;
1214 ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
1215 cs->cs_preg[3] &= ~ZSWR3_HFC;
1216 cs->cs_creg[3] &= ~ZSWR3_HFC;
1217 ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
1218 tp->t_termios.c_cflag &= ~CRTSCTS;
1219 }
1220 splx(s);
1221 break;
1222 }
1223 case TIOCSDTR:
1224 zs_modem(cs, 1);
1225 break;
1226 case TIOCCDTR:
1227 zs_modem(cs, 0);
1228 break;
1229 case TIOCMSET:
1230 case TIOCMBIS:
1231 case TIOCMBIC:
1232 case TIOCMGET:
1233 default:
1234 return (ENOTTY);
1235 }
1236 return (0);
1237 }
1238
1239 /*
1240 * Start or restart transmission.
1241 */
1242 static void
1243 zsstart(tp)
1244 register struct tty *tp;
1245 {
1246 register struct zs_chanstate *cs;
1247 register int s, nch;
1248 int unit = minor(tp->t_dev);
1249 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1250
1251 cs = &zi->zi_cs[unit & 1];
1252 s = spltty();
1253
1254 /*
1255 * If currently active or delaying, no need to do anything.
1256 */
1257 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
1258 goto out;
1259
1260 /*
1261 * If there are sleepers, and output has drained below low
1262 * water mark, awaken.
1263 */
1264 if (tp->t_outq.c_cc <= tp->t_lowat) {
1265 if (tp->t_state & TS_ASLEEP) {
1266 tp->t_state &= ~TS_ASLEEP;
1267 wakeup((caddr_t)&tp->t_outq);
1268 }
1269 selwakeup(&tp->t_wsel);
1270 }
1271
1272 nch = ndqb(&tp->t_outq, 0); /* XXX */
1273 if (nch) {
1274 register char *p = tp->t_outq.c_cf;
1275
1276 /* mark busy, enable tx done interrupts, & send first byte */
1277 tp->t_state |= TS_BUSY;
1278 (void) splzs();
1279 cs->cs_preg[1] |= ZSWR1_TIE;
1280 cs->cs_creg[1] |= ZSWR1_TIE;
1281 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1282 cs->cs_zc->zc_data = *p;
1283 ZS_DELAY();
1284 cs->cs_tba = p + 1;
1285 cs->cs_tbc = nch - 1;
1286 } else {
1287 /*
1288 * Nothing to send, turn off transmit done interrupts.
1289 * This is useful if something is doing polled output.
1290 */
1291 (void) splzs();
1292 cs->cs_preg[1] &= ~ZSWR1_TIE;
1293 cs->cs_creg[1] &= ~ZSWR1_TIE;
1294 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1295 }
1296 out:
1297 splx(s);
1298 }
1299
1300 /*
1301 * Stop output, e.g., for ^S or output flush.
1302 */
1303 int
1304 zsstop(tp, flag)
1305 register struct tty *tp;
1306 int flag;
1307 {
1308 register struct zs_chanstate *cs;
1309 register int s, unit = minor(tp->t_dev);
1310 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1311
1312 cs = &zi->zi_cs[unit & 1];
1313 s = splzs();
1314 if (tp->t_state & TS_BUSY) {
1315 /*
1316 * Device is transmitting; must stop it.
1317 */
1318 cs->cs_tbc = 0;
1319 if ((tp->t_state & TS_TTSTOP) == 0)
1320 tp->t_state |= TS_FLUSH;
1321 }
1322 splx(s);
1323 return 0;
1324 }
1325
1326 /*
1327 * Set ZS tty parameters from termios.
1328 *
1329 * This routine makes use of the fact that only registers
1330 * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
1331 */
1332 static int
1333 zsparam(tp, t)
1334 register struct tty *tp;
1335 register struct termios *t;
1336 {
1337 int unit = minor(tp->t_dev);
1338 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1339 register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
1340 register int tmp, tmp5, cflag, s;
1341
1342 /*
1343 * Because PCLK is only run at 4.9 MHz, the fastest we
1344 * can go is 51200 baud (this corresponds to TC=1).
1345 * This is somewhat unfortunate as there is no real
1346 * reason we should not be able to handle higher rates.
1347 */
1348 tmp = t->c_ospeed;
1349 if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp))
1350 return (EINVAL);
1351 if (tmp == 0) {
1352 /* stty 0 => drop DTR and RTS */
1353 zs_modem(cs, 0);
1354 return (0);
1355 }
1356 tmp = BPS_TO_TCONST(PCLK / 16, tmp);
1357 if (tmp < 2)
1358 return (EINVAL);
1359
1360 cflag = t->c_cflag;
1361 tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp);
1362 tp->t_cflag = cflag;
1363
1364 /*
1365 * Block interrupts so that state will not
1366 * be altered until we are done setting it up.
1367 */
1368 s = splzs();
1369 cs->cs_preg[12] = tmp;
1370 cs->cs_preg[13] = tmp >> 8;
1371 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
1372 switch (cflag & CSIZE) {
1373 case CS5:
1374 tmp = ZSWR3_RX_5;
1375 tmp5 = ZSWR5_TX_5;
1376 break;
1377 case CS6:
1378 tmp = ZSWR3_RX_6;
1379 tmp5 = ZSWR5_TX_6;
1380 break;
1381 case CS7:
1382 tmp = ZSWR3_RX_7;
1383 tmp5 = ZSWR5_TX_7;
1384 break;
1385 case CS8:
1386 default:
1387 tmp = ZSWR3_RX_8;
1388 tmp5 = ZSWR5_TX_8;
1389 break;
1390 }
1391
1392 /*
1393 * Output hardware flow control on the chip is horrendous: if
1394 * carrier detect drops, the receiver is disabled. Hence we
1395 * can only do this when the carrier is on.
1396 */
1397 tmp |= ZSWR3_RX_ENABLE;
1398 if (cflag & CCTS_OFLOW) {
1399 if (cs->cs_zc->zc_csr & ZSRR0_DCD)
1400 tmp |= ZSWR3_HFC;
1401 ZS_DELAY();
1402 }
1403 cs->cs_preg[3] = tmp;
1404 cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
1405
1406 tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
1407 if ((cflag & PARODD) == 0)
1408 tmp |= ZSWR4_EVENP;
1409 if (cflag & PARENB)
1410 tmp |= ZSWR4_PARENB;
1411 cs->cs_preg[4] = tmp;
1412 cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR;
1413 cs->cs_preg[10] = ZSWR10_NRZ;
1414 cs->cs_preg[11] = ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD;
1415 cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA;
1416 cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
1417
1418 /*
1419 * If nothing is being transmitted, set up new current values,
1420 * else mark them as pending.
1421 */
1422 if (cs->cs_heldchange == 0) {
1423 if (cs->cs_ttyp->t_state & TS_BUSY) {
1424 cs->cs_heldtbc = cs->cs_tbc;
1425 cs->cs_tbc = 0;
1426 cs->cs_heldchange = 1;
1427 } else {
1428 bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
1429 zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
1430 }
1431 }
1432 splx(s);
1433 return (0);
1434 }
1435
1436 /*
1437 * Raise or lower modem control (DTR/RTS) signals. If a character is
1438 * in transmission, the change is deferred.
1439 */
1440 static void
1441 zs_modem(cs, onoff)
1442 struct zs_chanstate *cs;
1443 int onoff;
1444 {
1445 int s, bis, and;
1446
1447 if (onoff) {
1448 bis = ZSWR5_DTR | ZSWR5_RTS;
1449 and = ~0;
1450 } else {
1451 bis = 0;
1452 and = ~(ZSWR5_DTR | ZSWR5_RTS);
1453 }
1454 s = splzs();
1455 cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
1456 if (cs->cs_heldchange == 0) {
1457 if (cs->cs_ttyp->t_state & TS_BUSY) {
1458 cs->cs_heldtbc = cs->cs_tbc;
1459 cs->cs_tbc = 0;
1460 cs->cs_heldchange = 1;
1461 } else {
1462 cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
1463 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1464 }
1465 }
1466 splx(s);
1467 }
1468
1469 /*
1470 * Write the given register set to the given zs channel in the proper order.
1471 * The channel must not be transmitting at the time. The receiver will
1472 * be disabled for the time it takes to write all the registers.
1473 */
1474 static void
1475 zs_loadchannelregs(zc, reg)
1476 volatile struct zschan *zc;
1477 u_char *reg;
1478 {
1479 int i;
1480
1481 zc->zc_csr = ZSM_RESET_ERR; /* reset error condition */
1482 ZS_DELAY();
1483 i = zc->zc_data; /* drain fifo */
1484 ZS_DELAY();
1485 i = zc->zc_data;
1486 ZS_DELAY();
1487 i = zc->zc_data;
1488 ZS_DELAY();
1489 ZS_WRITE(zc, 4, reg[4]);
1490 ZS_WRITE(zc, 10, reg[10]);
1491 ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
1492 ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
1493 ZS_WRITE(zc, 1, reg[1]);
1494 ZS_WRITE(zc, 9, reg[9]);
1495 ZS_WRITE(zc, 11, reg[11]);
1496 ZS_WRITE(zc, 12, reg[12]);
1497 ZS_WRITE(zc, 13, reg[13]);
1498 ZS_WRITE(zc, 14, reg[14]);
1499 ZS_WRITE(zc, 15, reg[15]);
1500 ZS_WRITE(zc, 3, reg[3]);
1501 ZS_WRITE(zc, 5, reg[5]);
1502 }
1503
1504 #ifdef KGDB
1505 /*
1506 * Get a character from the given kgdb channel. Called at splhigh().
1507 */
1508 static int
1509 zs_kgdb_getc(arg)
1510 void *arg;
1511 {
1512 register volatile struct zschan *zc = (volatile struct zschan *)arg;
1513
1514 while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
1515 ZS_DELAY();
1516 return (zc->zc_data);
1517 }
1518
1519 /*
1520 * Put a character to the given kgdb channel. Called at splhigh().
1521 */
1522 static void
1523 zs_kgdb_putc(arg, c)
1524 void *arg;
1525 int c;
1526 {
1527 register volatile struct zschan *zc = (volatile struct zschan *)arg;
1528
1529 while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
1530 ZS_DELAY();
1531 zc->zc_data = c;
1532 ZS_DELAY();
1533 }
1534
1535 /*
1536 * Set up for kgdb; called at boot time before configuration.
1537 * KGDB interrupts will be enabled later when zs0 is configured.
1538 */
1539 void
1540 zs_kgdb_init()
1541 {
1542 volatile struct zsdevice *addr;
1543 volatile struct zschan *zc;
1544 int unit, zs;
1545
1546 if (major(kgdb_dev) != ZSMAJOR)
1547 return;
1548 unit = minor(kgdb_dev);
1549 /*
1550 * Unit must be 0 or 1 (zs0).
1551 */
1552 if ((unsigned)unit >= ZS_KBD) {
1553 printf("zs_kgdb_init: bad minor dev %d\n", unit);
1554 return;
1555 }
1556 zs = unit >> 1;
1557 if ((addr = zsaddr[zs]) == NULL)
1558 addr = zsaddr[zs] = findzs(zs);
1559 unit &= 1;
1560 zc = unit == 0 ? &addr->zs_chan[ZS_CHAN_A] : &addr->zs_chan[ZS_CHAN_B];
1561 zs_kgdb_savedspeed = zs_getspeed(zc);
1562 printf("zs_kgdb_init: attaching zs%d%c at %d baud\n",
1563 zs, unit + 'a', kgdb_rate);
1564 zs_reset(zc, 1, kgdb_rate);
1565 kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc);
1566 }
1567 #endif /* KGDB */
1568