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