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