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