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