155107Storek /* 255107Storek * Copyright (c) 1992 The Regents of the University of California. 355107Storek * All rights reserved. 455107Storek * 555107Storek * This software was developed by the Computer Systems Engineering group 655107Storek * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 755107Storek * contributed to Berkeley. 855107Storek * 955499Sbostic * All advertising materials mentioning features or use of this software 1055499Sbostic * must display the following acknowledgement: 1155499Sbostic * This product includes software developed by the University of 1259191Storek * California, Lawrence Berkeley Laboratory. 1355499Sbostic * 1455107Storek * %sccs.include.redist.c% 1555107Storek * 16*63808Storek * @(#)zs.c 7.5 (Berkeley) 07/15/93 1755107Storek * 18*63808Storek * from: $Header: zs.c,v 1.29 93/07/15 02:57:07 torek Exp $ 1955107Storek */ 2055107Storek 2155107Storek /* 2255107Storek * Zilog Z8530 (ZSCC) driver. 2355107Storek * 2455107Storek * Runs two tty ports (ttya and ttyb) on zs0, 2555107Storek * and runs a keyboard and mouse on zs1. 2655107Storek * 2755107Storek * This driver knows far too much about chip to usage mappings. 2855107Storek */ 2955107Storek #define NZS 2 /* XXX */ 3055107Storek 3156536Sbostic #include <sys/param.h> 3256536Sbostic #include <sys/proc.h> 3356536Sbostic #include <sys/device.h> 3456536Sbostic #include <sys/conf.h> 3556536Sbostic #include <sys/file.h> 3656536Sbostic #include <sys/ioctl.h> 3756536Sbostic #include <sys/tty.h> 3856536Sbostic #include <sys/time.h> 3956536Sbostic #include <sys/kernel.h> 4056536Sbostic #include <sys/syslog.h> 4155107Storek 4256536Sbostic #include <machine/autoconf.h> 4356536Sbostic #include <machine/cpu.h> 4455107Storek 4559191Storek #include <sparc/sparc/vaddrs.h> 4659191Storek #include <sparc/sparc/auxreg.h> 4759191Storek 4856536Sbostic #include <sparc/dev/kbd.h> 4956536Sbostic #include <sparc/dev/zsreg.h> 5056536Sbostic #include <sparc/dev/zsvar.h> 5155107Storek 5255107Storek #ifdef KGDB 5356536Sbostic #include <machine/remote-sl.h> 5455107Storek #endif 5555107Storek 5655107Storek #define ZSMAJOR 12 /* XXX */ 5755107Storek 5855107Storek #define ZS_KBD 2 /* XXX */ 5955107Storek #define ZS_MOUSE 3 /* XXX */ 6055107Storek 6155107Storek /* the magic number below was stolen from the Sprite source. */ 6255107Storek #define PCLK (19660800/4) /* PCLK pin input clock rate */ 6355107Storek 6455107Storek /* 6555107Storek * Select software interrupt bit based on TTY ipl. 6655107Storek */ 6755107Storek #if PIL_TTY == 1 6855107Storek # define IE_ZSSOFT IE_L1 6955107Storek #elif PIL_TTY == 4 7055107Storek # define IE_ZSSOFT IE_L4 7155107Storek #elif PIL_TTY == 6 7255107Storek # define IE_ZSSOFT IE_L6 7355107Storek #else 7455107Storek # error "no suitable software interrupt bit" 7555107Storek #endif 7655107Storek 7755107Storek /* 7855107Storek * Software state per found chip. This would be called `zs_softc', 7955107Storek * but the previous driver had a rather different zs_softc.... 8055107Storek */ 8155107Storek struct zsinfo { 8255107Storek struct device zi_dev; /* base device */ 8355107Storek volatile struct zsdevice *zi_zs;/* chip registers */ 8455107Storek struct zs_chanstate zi_cs[2]; /* channel A and B software state */ 8555107Storek }; 8655107Storek 8755107Storek struct tty zs_tty[NZS * 2]; /* XXX should be dynamic */ 8855107Storek 8955107Storek /* Definition of the driver for autoconfig. */ 9055107Storek static int zsmatch(struct device *, struct cfdata *, void *); 9155107Storek static void zsattach(struct device *, struct device *, void *); 9255107Storek struct cfdriver zscd = 9355107Storek { NULL, "zs", zsmatch, zsattach, DV_TTY, sizeof(struct zsinfo) }; 9455107Storek 9555107Storek /* Interrupt handlers. */ 9655107Storek static int zshard(void *); 9755107Storek static struct intrhand levelhard = { zshard }; 9855107Storek static int zssoft(void *); 9955107Storek static struct intrhand levelsoft = { zssoft }; 10055107Storek 10155107Storek struct zs_chanstate *zslist; 10255107Storek 10355107Storek /* Routines called from other code. */ 10455107Storek static void zsiopen(struct tty *); 10555107Storek static void zsiclose(struct tty *); 10655107Storek static void zsstart(struct tty *); 10755107Storek static void zsstop(struct tty *, int); 10855107Storek static int zsparam(struct tty *, struct termios *); 10955107Storek 11055107Storek /* Routines purely local to this driver. */ 11155107Storek static int zs_getspeed(volatile struct zschan *); 11255107Storek static void zs_reset(volatile struct zschan *, int, int); 11355107Storek static void zs_modem(struct zs_chanstate *, int); 11455107Storek static void zs_loadchannelregs(volatile struct zschan *, u_char *); 11555107Storek 11655107Storek /* Console stuff. */ 11755107Storek static struct tty *zs_ctty; /* console `struct tty *' */ 11855107Storek static int zs_consin = -1, zs_consout = -1; 11955107Storek static int zscnputc(int); /* console putc function */ 12055107Storek static volatile struct zschan *zs_conschan; 12155107Storek static struct tty *zs_checkcons(struct zsinfo *, int, struct zs_chanstate *); 12255107Storek 12355107Storek #ifdef KGDB 12455107Storek /* KGDB stuff. Must reboot to change zs_kgdbunit. */ 12555107Storek extern int kgdb_dev, kgdb_rate; 12655107Storek static int zs_kgdb_savedspeed; 12755107Storek static void zs_checkkgdb(int, struct zs_chanstate *, struct tty *); 12855107Storek #endif 12955107Storek 13055107Storek extern volatile struct zsdevice *findzs(int); 13155107Storek static volatile struct zsdevice *zsaddr[NZS]; /* XXX, but saves work */ 13255107Storek 13355107Storek /* 13455107Storek * Console keyboard L1-A processing is done in the hardware interrupt code, 13555107Storek * so we need to duplicate some of the console keyboard decode state. (We 13655107Storek * must not use the regular state as the hardware code keeps ahead of the 13755107Storek * software state: the software state tracks the most recent ring input but 13855107Storek * the hardware state tracks the most recent ZSCC input.) See also kbd.h. 13955107Storek */ 14055107Storek static struct conk_state { /* console keyboard state */ 14155107Storek char conk_id; /* true => ID coming up (console only) */ 14255107Storek char conk_l1; /* true => L1 pressed (console only) */ 14355107Storek } zsconk_state; 14455107Storek 14559191Storek int zshardscope; 14659191Storek int zsshortcuts; /* number of "shortcut" software interrupts */ 14759191Storek 14855107Storek /* 14955107Storek * Match slave number to zs unit number, so that misconfiguration will 15055107Storek * not set up the keyboard as ttya, etc. 15155107Storek */ 15255107Storek static int 15355107Storek zsmatch(struct device *parent, struct cfdata *cf, void *aux) 15455107Storek { 15555107Storek struct romaux *ra = aux; 15655107Storek 15755107Storek return (getpropint(ra->ra_node, "slave", -2) == cf->cf_unit); 15855107Storek } 15955107Storek 16055107Storek /* 16155107Storek * Attach a found zs. 16255107Storek * 16355107Storek * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR 16455107Storek * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE? 16555107Storek */ 16655107Storek static void 16755107Storek zsattach(struct device *parent, struct device *dev, void *aux) 16855107Storek { 16955107Storek register int zs = dev->dv_unit, unit; 17055107Storek register struct zsinfo *zi; 17155107Storek register struct zs_chanstate *cs; 17255107Storek register volatile struct zsdevice *addr; 17355107Storek register struct tty *tp, *ctp; 17455107Storek register struct romaux *ra = aux; 17555107Storek int pri, softcar; 17655107Storek static int didintr, prevpri; 17755107Storek 17855107Storek if ((addr = zsaddr[zs]) == NULL) 17955107Storek addr = zsaddr[zs] = findzs(zs); 18055107Storek if ((void *)addr != ra->ra_vaddr) 18155107Storek panic("zsattach"); 18255107Storek if (ra->ra_nintr != 1) { 18355107Storek printf(": expected 1 interrupt, got %d\n", ra->ra_nintr); 18455107Storek return; 18555107Storek } 18655107Storek pri = ra->ra_intr[0].int_pri; 18755107Storek printf(" pri %d, softpri %d\n", pri, PIL_TTY); 18855107Storek if (!didintr) { 18955107Storek didintr = 1; 19055107Storek prevpri = pri; 19155107Storek intr_establish(pri, &levelhard); 19255107Storek intr_establish(PIL_TTY, &levelsoft); 19355107Storek } else if (pri != prevpri) 19455107Storek panic("broken zs interrupt scheme"); 19555107Storek zi = (struct zsinfo *)dev; 19655107Storek zi->zi_zs = addr; 19755107Storek unit = zs * 2; 19855107Storek cs = zi->zi_cs; 19955107Storek tp = &zs_tty[unit]; 20055107Storek 20155107Storek if (unit == 0) { 20255107Storek /* Get software carrier flags from options node in OPENPROM. */ 20355107Storek extern int optionsnode; 20455107Storek 20555107Storek softcar = 0; 20655107Storek if (*getpropstring(optionsnode, "ttya-ignore-cd") == 't') 20755107Storek softcar |= 1; 20855107Storek if (*getpropstring(optionsnode, "ttyb-ignore-cd") == 't') 20955107Storek softcar |= 2; 21055107Storek } else 21155107Storek softcar = dev->dv_cfdata->cf_flags; 21255107Storek 21355107Storek /* link into interrupt list with order (A,B) (B=A+1) */ 21455107Storek cs[0].cs_next = &cs[1]; 21555107Storek cs[1].cs_next = zslist; 21655107Storek zslist = cs; 21755107Storek 21855107Storek cs->cs_unit = unit; 21955107Storek cs->cs_speed = zs_getspeed(&addr->zs_chan[CHAN_A]); 22055107Storek cs->cs_softcar = softcar & 1; 22155107Storek cs->cs_zc = &addr->zs_chan[CHAN_A]; 22255107Storek tp->t_dev = makedev(ZSMAJOR, unit); 22355107Storek tp->t_oproc = zsstart; 22455107Storek tp->t_param = zsparam; 22555107Storek tp->t_stop = zsstop; 22655107Storek if ((ctp = zs_checkcons(zi, unit, cs)) != NULL) 22755107Storek tp = ctp; 22855107Storek cs->cs_ttyp = tp; 22955107Storek #ifdef KGDB 23055107Storek if (ctp == NULL) 23155107Storek zs_checkkgdb(unit, cs, tp); 23255107Storek #endif 23355107Storek if (unit == ZS_KBD) { 23455107Storek /* 23555107Storek * Keyboard: tell /dev/kbd driver how to talk to us. 23655107Storek */ 23755107Storek tp->t_ispeed = tp->t_ospeed = cs->cs_speed; 23855107Storek tp->t_cflag = CS8; 23955107Storek kbd_serial(tp, zsiopen, zsiclose); 24055107Storek cs->cs_conk = 1; /* do L1-A processing */ 24155107Storek } 24255107Storek unit++; 24355107Storek cs++; 24455107Storek tp++; 24555107Storek cs->cs_unit = unit; 24655107Storek cs->cs_speed = zs_getspeed(&addr->zs_chan[CHAN_B]); 24755107Storek cs->cs_softcar = softcar & 2; 24855107Storek cs->cs_zc = &addr->zs_chan[CHAN_B]; 24955107Storek tp->t_dev = makedev(ZSMAJOR, unit); 25055107Storek tp->t_oproc = zsstart; 25155107Storek tp->t_param = zsparam; 25255107Storek tp->t_stop = zsstop; 25355107Storek if ((ctp = zs_checkcons(zi, unit, cs)) != NULL) 25455107Storek tp = ctp; 25555107Storek cs->cs_ttyp = tp; 25655107Storek #ifdef KGDB 25755107Storek if (ctp == NULL) 25855107Storek zs_checkkgdb(unit, cs, tp); 25955107Storek #endif 26055107Storek if (unit == ZS_MOUSE) { 26155107Storek /* 26255107Storek * Mouse: tell /dev/mouse driver how to talk to us. 26355107Storek */ 26455107Storek tp->t_ispeed = tp->t_ospeed = cs->cs_speed; 26555107Storek tp->t_cflag = CS8; 26655107Storek ms_serial(tp, zsiopen, zsiclose); 26755107Storek } 26855107Storek } 26955107Storek 27055107Storek /* 27155107Storek * Put a channel in a known state. Interrupts may be left disabled 27255107Storek * or enabled, as desired. 27355107Storek */ 27455107Storek static void 27555107Storek zs_reset(zc, inten, speed) 27655107Storek volatile struct zschan *zc; 27755107Storek int inten, speed; 27855107Storek { 27955107Storek int tconst; 28055107Storek static u_char reg[16] = { 28155107Storek 0, 28255107Storek 0, 28355107Storek 0, 28455107Storek ZSWR3_RX_8 | ZSWR3_RX_ENABLE, 28555107Storek ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP, 28655107Storek ZSWR5_TX_8 | ZSWR5_TX_ENABLE, 28755107Storek 0, 28855107Storek 0, 28955107Storek 0, 29055107Storek 0, 29155107Storek ZSWR10_NRZ, 29255107Storek ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD, 29355107Storek 0, 29455107Storek 0, 29555107Storek ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA, 29655107Storek ZSWR15_BREAK_IE | ZSWR15_DCD_IE, 29755107Storek }; 29855107Storek 29955107Storek reg[9] = inten ? ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR : ZSWR9_NO_VECTOR; 30055107Storek tconst = BPS_TO_TCONST(PCLK / 16, speed); 30155107Storek reg[12] = tconst; 30255107Storek reg[13] = tconst >> 8; 30355107Storek zs_loadchannelregs(zc, reg); 30455107Storek } 30555107Storek 30655107Storek /* 30755107Storek * Declare the given tty (which is in fact &cons) as a console input 30855107Storek * or output. This happens before the zs chip is attached; the hookup 30955107Storek * is finished later, in zs_setcons() below. 31055107Storek * 31155107Storek * This is used only for ports a and b. The console keyboard is decoded 31255107Storek * independently (we always send unit-2 input to /dev/kbd, which will 31355107Storek * direct it to /dev/console if appropriate). 31455107Storek */ 31555107Storek void 31655107Storek zsconsole(tp, unit, out) 31755107Storek register struct tty *tp; 31855107Storek register int unit; 31955107Storek int out; 32055107Storek { 32155107Storek extern int (*v_putc)(); 32255107Storek int zs; 32355107Storek volatile struct zsdevice *addr; 32455107Storek 32555107Storek if (unit >= ZS_KBD) 32655107Storek panic("zsconsole"); 32755107Storek if (out) { 32855107Storek zs_consout = unit; 32955107Storek zs = unit >> 1; 33055107Storek if ((addr = zsaddr[zs]) == NULL) 33155107Storek addr = zsaddr[zs] = findzs(zs); 33255107Storek zs_conschan = (unit & 1) == 0 ? &addr->zs_chan[CHAN_A] : 33355107Storek &addr->zs_chan[CHAN_B]; 33455107Storek v_putc = zscnputc; 33555107Storek } else 33655107Storek zs_consin = unit; 33755107Storek zs_ctty = tp; 33855107Storek } 33955107Storek 34055107Storek /* 34155107Storek * Polled console output putchar. 34255107Storek */ 34355107Storek static int 34455107Storek zscnputc(c) 34555107Storek int c; 34655107Storek { 34755107Storek register volatile struct zschan *zc = zs_conschan; 34855107Storek register int s; 34955107Storek 350*63808Storek if (c == '\n') 351*63808Storek zscnputc('\r'); 35255107Storek /* 35355107Storek * Must block output interrupts (i.e., raise to >= splzs) without 35455107Storek * lowering current ipl. Need a better way. 35555107Storek */ 35655107Storek s = splhigh(); 35755107Storek #ifdef sun4c /* XXX */ 35855107Storek if (s <= (12 << 8)) 35955107Storek (void) splzs(); 36055107Storek #endif 36155107Storek while ((zc->zc_csr & ZSRR0_TX_READY) == 0) 36255107Storek continue; 36355107Storek zc->zc_data = c; 36455107Storek splx(s); 36555107Storek } 36655107Storek 36755107Storek /* 36855107Storek * Set up the given unit as console input, output, both, or neither, as 36955107Storek * needed. Return console tty if it is to receive console input. 37055107Storek */ 37155107Storek static struct tty * 37255107Storek zs_checkcons(struct zsinfo *zi, int unit, struct zs_chanstate *cs) 37355107Storek { 37455107Storek register struct tty *tp; 37555107Storek char *i, *o; 37655107Storek 37755107Storek if ((tp = zs_ctty) == NULL) 37855107Storek return (0); 37955107Storek i = zs_consin == unit ? "input" : NULL; 38055107Storek o = zs_consout == unit ? "output" : NULL; 38155107Storek if (i == NULL && o == NULL) 38255107Storek return (0); 38355107Storek 38455107Storek /* rewire the minor device (gack) */ 38555107Storek tp->t_dev = makedev(major(tp->t_dev), unit); 38655107Storek 38755107Storek /* 38855107Storek * Rewire input and/or output. Note that baud rate reflects 38955107Storek * input settings, not output settings, but we can do no better 39055107Storek * if the console is split across two ports. 391*63808Storek * 392*63808Storek * XXX split consoles don't work anyway -- this needs to be 393*63808Storek * thrown away and redone 39455107Storek */ 39555107Storek if (i) { 39655107Storek tp->t_param = zsparam; 39755107Storek tp->t_ispeed = tp->t_ospeed = cs->cs_speed; 39855107Storek tp->t_cflag = CS8; 39955107Storek ttsetwater(tp); 40055107Storek } 40155107Storek if (o) { 40255107Storek tp->t_oproc = zsstart; 40355107Storek tp->t_stop = zsstop; 40455107Storek } 40555107Storek printf("%s%c: console %s\n", 40655107Storek zi->zi_dev.dv_xname, (unit & 1) + 'a', i ? (o ? "i/o" : i) : o); 40755107Storek cs->cs_consio = 1; 40855107Storek cs->cs_brkabort = 1; 409*63808Storek return (tp); 41055107Storek } 41155107Storek 41255107Storek #ifdef KGDB 41355107Storek /* 41455107Storek * The kgdb zs port, if any, was altered at boot time (see zs_kgdb_init). 41555107Storek * Pick up the current speed and character size and restore the original 41655107Storek * speed. 41755107Storek */ 41855107Storek static void 41955107Storek zs_checkkgdb(int unit, struct zs_chanstate *cs, struct tty *tp) 42055107Storek { 42155107Storek 42255107Storek if (kgdb_dev == makedev(ZSMAJOR, unit)) { 42355107Storek tp->t_ispeed = tp->t_ospeed = kgdb_rate; 42455107Storek tp->t_cflag = CS8; 42555107Storek cs->cs_kgdb = 1; 42655107Storek cs->cs_speed = zs_kgdb_savedspeed; 42755107Storek (void) zsparam(tp, &tp->t_termios); 42855107Storek } 42955107Storek } 43055107Storek #endif 43155107Storek 43255107Storek /* 43355107Storek * Compute the current baud rate given a ZSCC channel. 43455107Storek */ 43555107Storek static int 43655107Storek zs_getspeed(zc) 43755107Storek register volatile struct zschan *zc; 43855107Storek { 43955107Storek register int tconst; 44055107Storek 44155107Storek tconst = ZS_READ(zc, 12); 44255107Storek tconst |= ZS_READ(zc, 13) << 8; 44355107Storek return (TCONST_TO_BPS(PCLK / 16, tconst)); 44455107Storek } 44555107Storek 44655107Storek 44755107Storek /* 44855107Storek * Do an internal open. 44955107Storek */ 45055107Storek static void 45155107Storek zsiopen(struct tty *tp) 45255107Storek { 45355107Storek 45455107Storek (void) zsparam(tp, &tp->t_termios); 45555107Storek ttsetwater(tp); 45655107Storek tp->t_state = TS_ISOPEN | TS_CARR_ON; 45755107Storek } 45855107Storek 45955107Storek /* 46055107Storek * Do an internal close. Eventually we should shut off the chip when both 46155107Storek * ports on it are closed. 46255107Storek */ 46355107Storek static void 46455107Storek zsiclose(struct tty *tp) 46555107Storek { 46655107Storek 46755107Storek ttylclose(tp, 0); /* ??? */ 46855107Storek ttyclose(tp); /* ??? */ 46955107Storek tp->t_state = 0; 47055107Storek } 47155107Storek 47255107Storek 47355107Storek /* 47455107Storek * Open a zs serial port. This interface may not be used to open 47555107Storek * the keyboard and mouse ports. (XXX) 47655107Storek */ 47755107Storek int 47855107Storek zsopen(dev_t dev, int flags, int mode, struct proc *p) 47955107Storek { 48055107Storek register struct tty *tp; 48155107Storek register struct zs_chanstate *cs; 48255107Storek struct zsinfo *zi; 48355107Storek int unit = minor(dev), zs = unit >> 1, error, s; 48455107Storek 48555107Storek if (zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL || 48655107Storek unit == ZS_KBD || unit == ZS_MOUSE) 48755107Storek return (ENXIO); 48855107Storek cs = &zi->zi_cs[unit & 1]; 48955107Storek if (cs->cs_consio) 49055107Storek return (ENXIO); /* ??? */ 49155107Storek tp = cs->cs_ttyp; 49255107Storek s = spltty(); 49355107Storek if ((tp->t_state & TS_ISOPEN) == 0) { 49455107Storek ttychars(tp); 49555107Storek if (tp->t_ispeed == 0) { 49655107Storek tp->t_iflag = TTYDEF_IFLAG; 49755107Storek tp->t_oflag = TTYDEF_OFLAG; 49855107Storek tp->t_cflag = TTYDEF_CFLAG; 49955107Storek tp->t_lflag = TTYDEF_LFLAG; 50055107Storek tp->t_ispeed = tp->t_ospeed = cs->cs_speed; 50155107Storek } 50255107Storek (void) zsparam(tp, &tp->t_termios); 50355107Storek ttsetwater(tp); 50455107Storek } else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) { 50555107Storek splx(s); 50655107Storek return (EBUSY); 50755107Storek } 50855107Storek error = 0; 50955107Storek for (;;) { 51055107Storek /* loop, turning on the device, until carrier present */ 51155107Storek zs_modem(cs, 1); 51255107Storek if (cs->cs_softcar) 51355107Storek tp->t_state |= TS_CARR_ON; 51455107Storek if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL || 51555107Storek tp->t_state & TS_CARR_ON) 51655107Storek break; 51755107Storek tp->t_state |= TS_WOPEN; 51855107Storek if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH, 51955107Storek ttopen, 0)) 52055107Storek break; 52155107Storek } 52255107Storek splx(s); 52355107Storek if (error == 0) 52459191Storek error = linesw[tp->t_line].l_open(dev, tp); 52555107Storek if (error) 52655107Storek zs_modem(cs, 0); 52755107Storek return (error); 52855107Storek } 52955107Storek 53055107Storek /* 53155107Storek * Close a zs serial port. 53255107Storek */ 53355107Storek int 53455107Storek zsclose(dev_t dev, int flags, int mode, struct proc *p) 53555107Storek { 53655107Storek register struct zs_chanstate *cs; 53755107Storek register struct tty *tp; 53855107Storek struct zsinfo *zi; 53955107Storek int unit = minor(dev), s; 54055107Storek 54155107Storek zi = zscd.cd_devs[unit >> 1]; 54255107Storek cs = &zi->zi_cs[unit & 1]; 54355107Storek tp = cs->cs_ttyp; 54459191Storek linesw[tp->t_line].l_close(tp, flags); 54555107Storek if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN || 54655107Storek (tp->t_state & TS_ISOPEN) == 0) { 54755107Storek zs_modem(cs, 0); 54855107Storek /* hold low for 1 second */ 54955107Storek (void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz); 55055107Storek } 55155107Storek ttyclose(tp); 55255107Storek #ifdef KGDB 55355107Storek /* Reset the speed if we're doing kgdb on this port */ 55455107Storek if (cs->cs_kgdb) { 55555107Storek tp->t_ispeed = tp->t_ospeed = kgdb_rate; 55655107Storek (void) zsparam(tp, &tp->t_termios); 55755107Storek } 55855107Storek #endif 55955107Storek return (0); 56055107Storek } 56155107Storek 56255107Storek /* 56355107Storek * Read/write zs serial port. 56455107Storek */ 56555107Storek int 56655107Storek zsread(dev_t dev, struct uio *uio, int flags) 56755107Storek { 56855107Storek register struct tty *tp = &zs_tty[minor(dev)]; 56955107Storek 57055107Storek return (linesw[tp->t_line].l_read(tp, uio, flags)); 57155107Storek } 57255107Storek 57355107Storek int 57455107Storek zswrite(dev_t dev, struct uio *uio, int flags) 57555107Storek { 57655107Storek register struct tty *tp = &zs_tty[minor(dev)]; 57755107Storek 57855107Storek return (linesw[tp->t_line].l_write(tp, uio, flags)); 57955107Storek } 58055107Storek 58155107Storek /* 58255107Storek * ZS hardware interrupt. Scan all ZS channels. NB: we know here that 58355107Storek * channels are kept in (A,B) pairs. 58455107Storek * 58555107Storek * Do just a little, then get out; set a software interrupt if more 58655107Storek * work is needed. 58755107Storek * 58855107Storek * We deliberately ignore the vectoring Zilog gives us, and match up 58955107Storek * only the number of `reset interrupt under service' operations, not 59055107Storek * the order. 59155107Storek */ 59255107Storek /* ARGSUSED */ 59355107Storek int 59455107Storek zshard(void *intrarg) 59555107Storek { 59655107Storek register struct zs_chanstate *a; 59755107Storek #define b (a + 1) 59859191Storek register volatile struct zschan *zc; 59959191Storek register int rr3, intflags = 0, v, i; 60059191Storek static int zsrint(struct zs_chanstate *, volatile struct zschan *); 60159191Storek static int zsxint(struct zs_chanstate *, volatile struct zschan *); 60259191Storek static int zssint(struct zs_chanstate *, volatile struct zschan *); 60355107Storek 60455107Storek for (a = zslist; a != NULL; a = b->cs_next) { 60555107Storek rr3 = ZS_READ(a->cs_zc, 3); 60659191Storek if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) { 60759191Storek intflags |= 2; 60859191Storek zc = a->cs_zc; 60959191Storek i = a->cs_rbput; 61059191Storek if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) { 61159191Storek a->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 61259191Storek intflags |= 1; 61359191Storek } 61459191Storek if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) { 61559191Storek a->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 61659191Storek intflags |= 1; 61759191Storek } 61859191Storek if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) { 61959191Storek a->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 62059191Storek intflags |= 1; 62159191Storek } 62259191Storek a->cs_rbput = i; 62359191Storek } 62459191Storek if (rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) { 62559191Storek intflags |= 2; 62659191Storek zc = b->cs_zc; 62759191Storek i = b->cs_rbput; 62859191Storek if (rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) { 62959191Storek b->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 63059191Storek intflags |= 1; 63159191Storek } 63259191Storek if (rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) { 63359191Storek b->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 63459191Storek intflags |= 1; 63559191Storek } 63659191Storek if (rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) { 63759191Storek b->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 63859191Storek intflags |= 1; 63959191Storek } 64059191Storek b->cs_rbput = i; 64159191Storek } 64255107Storek } 64355107Storek #undef b 64455107Storek if (intflags & 1) { 64555107Storek #if sun4c /* XXX -- but this will go away when zshard moves to locore.s */ 64655107Storek struct clockframe *p = intrarg; 64755107Storek 64855107Storek if ((p->psr & PSR_PIL) < (PIL_TTY << 8)) { 64959191Storek zsshortcuts++; 65055107Storek (void) spltty(); 65159191Storek if (zshardscope) { 65259191Storek LED_ON; 65359191Storek LED_OFF; 65459191Storek } 65555107Storek return (zssoft(intrarg)); 65655107Storek } 65755107Storek #endif 65855107Storek ienab_bis(IE_ZSSOFT); 65955107Storek } 66055107Storek return (intflags & 2); 66155107Storek } 66255107Storek 66355107Storek static int 66459191Storek zsrint(register struct zs_chanstate *cs, register volatile struct zschan *zc) 66555107Storek { 66659191Storek register int c = zc->zc_data; 66755107Storek 66855107Storek if (cs->cs_conk) { 66955107Storek register struct conk_state *conk = &zsconk_state; 67055107Storek 67155107Storek /* 67255107Storek * Check here for console abort function, so that we 67355107Storek * can abort even when interrupts are locking up the 67455107Storek * machine. 67555107Storek */ 67655107Storek if (c == KBD_RESET) { 67755107Storek conk->conk_id = 1; /* ignore next byte */ 67855107Storek conk->conk_l1 = 0; 67955107Storek } else if (conk->conk_id) 68055107Storek conk->conk_id = 0; /* stop ignoring bytes */ 68155107Storek else if (c == KBD_L1) 68255107Storek conk->conk_l1 = 1; /* L1 went down */ 68355107Storek else if (c == (KBD_L1|KBD_UP)) 68455107Storek conk->conk_l1 = 0; /* L1 went up */ 68555107Storek else if (c == KBD_A && conk->conk_l1) { 68655107Storek zsabort(); 68755107Storek conk->conk_l1 = 0; /* we never see the up */ 68855107Storek goto clearit; /* eat the A after L1-A */ 68955107Storek } 69055107Storek } 69155107Storek #ifdef KGDB 69255107Storek if (c == FRAME_START && cs->cs_kgdb && 69355107Storek (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) { 69455107Storek zskgdb(cs->cs_unit); 69555107Storek goto clearit; 69655107Storek } 69755107Storek #endif 69859191Storek /* compose receive character and status */ 69955107Storek c <<= 8; 70055107Storek c |= ZS_READ(zc, 1); 70155107Storek 70255107Storek /* clear receive error & interrupt condition */ 70355107Storek zc->zc_csr = ZSWR0_RESET_ERRORS; 70455107Storek zc->zc_csr = ZSWR0_CLR_INTR; 70555107Storek 70659191Storek return (ZRING_MAKE(ZRING_RINT, c)); 70759191Storek 70855107Storek clearit: 70955107Storek zc->zc_csr = ZSWR0_RESET_ERRORS; 71055107Storek zc->zc_csr = ZSWR0_CLR_INTR; 71155107Storek return (0); 71255107Storek } 71355107Storek 71455107Storek static int 71559191Storek zsxint(register struct zs_chanstate *cs, register volatile struct zschan *zc) 71655107Storek { 71759191Storek register int i = cs->cs_tbc; 71855107Storek 71955107Storek if (i == 0) { 72055107Storek zc->zc_csr = ZSWR0_RESET_TXINT; 72155107Storek zc->zc_csr = ZSWR0_CLR_INTR; 72259191Storek return (ZRING_MAKE(ZRING_XINT, 0)); 72355107Storek } 72455107Storek cs->cs_tbc = i - 1; 72555107Storek zc->zc_data = *cs->cs_tba++; 72655107Storek zc->zc_csr = ZSWR0_CLR_INTR; 72755107Storek return (0); 72855107Storek } 72955107Storek 73055107Storek static int 73159191Storek zssint(register struct zs_chanstate *cs, register volatile struct zschan *zc) 73255107Storek { 73359191Storek register int rr0; 73455107Storek 73559191Storek rr0 = zc->zc_csr; 73655107Storek zc->zc_csr = ZSWR0_RESET_STATUS; 73755107Storek zc->zc_csr = ZSWR0_CLR_INTR; 73859191Storek /* 73959191Storek * The chip's hardware flow control is, as noted in zsreg.h, 74059191Storek * busted---if the DCD line goes low the chip shuts off the 74159191Storek * receiver (!). If we want hardware CTS flow control but do 74259191Storek * not have it, and carrier is now on, turn HFC on; if we have 74359191Storek * HFC now but carrier has gone low, turn it off. 74459191Storek */ 74559191Storek if (rr0 & ZSRR0_DCD) { 74659191Storek if (cs->cs_ttyp->t_cflag & CCTS_OFLOW && 74759191Storek (cs->cs_creg[3] & ZSWR3_HFC) == 0) { 74859191Storek cs->cs_creg[3] |= ZSWR3_HFC; 74959191Storek ZS_WRITE(zc, 3, cs->cs_creg[3]); 75059191Storek } 75159191Storek } else { 75259191Storek if (cs->cs_creg[3] & ZSWR3_HFC) { 75359191Storek cs->cs_creg[3] &= ~ZSWR3_HFC; 75459191Storek ZS_WRITE(zc, 3, cs->cs_creg[3]); 75559191Storek } 75659191Storek } 75759191Storek if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) { 75855107Storek zsabort(); 75959191Storek return (0); 76055107Storek } 76159191Storek return (ZRING_MAKE(ZRING_SINT, rr0)); 76255107Storek } 76355107Storek 76455107Storek zsabort() 76555107Storek { 76655107Storek 76755107Storek printf("stopping on keyboard abort\n"); 76855107Storek callrom(); 76955107Storek } 77055107Storek 77155107Storek #ifdef KGDB 77255107Storek /* 77355107Storek * KGDB framing character received: enter kernel debugger. This probably 77455107Storek * should time out after a few seconds to avoid hanging on spurious input. 77555107Storek */ 77655107Storek zskgdb(int unit) 77755107Storek { 77855107Storek 77955107Storek printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a'); 78055107Storek kgdb_connect(1); 78155107Storek } 78255107Storek #endif 78355107Storek 78455107Storek /* 78555107Storek * Print out a ring or fifo overrun error message. 78655107Storek */ 78755107Storek static void 78855107Storek zsoverrun(int unit, long *ptime, char *what) 78955107Storek { 79055107Storek 79155107Storek if (*ptime != time.tv_sec) { 79255107Storek *ptime = time.tv_sec; 79359191Storek log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1, 79455107Storek (unit & 1) + 'a', what); 79555107Storek } 79655107Storek } 79755107Storek 79855107Storek /* 79959191Storek * ZS software interrupt. Scan all channels for deferred interrupts. 80055107Storek */ 80155107Storek int 80255107Storek zssoft(void *arg) 80355107Storek { 80455107Storek register struct zs_chanstate *cs; 80555107Storek register volatile struct zschan *zc; 80659191Storek register struct linesw *line; 80755107Storek register struct tty *tp; 80859191Storek register int get, n, c, cc, unit, s; 80955107Storek 81059191Storek for (cs = zslist; cs != NULL; cs = cs->cs_next) { 81159191Storek get = cs->cs_rbget; 81259191Storek again: 81359191Storek n = cs->cs_rbput; /* atomic */ 81459191Storek if (get == n) /* nothing more on this line */ 81559191Storek continue; 81659191Storek unit = cs->cs_unit; /* set up to handle interrupts */ 81755107Storek zc = cs->cs_zc; 81855107Storek tp = cs->cs_ttyp; 81959191Storek line = &linesw[tp->t_line]; 82055107Storek /* 82159191Storek * Compute the number of interrupts in the receive ring. 82259191Storek * If the count is overlarge, we lost some events, and 82359191Storek * must advance to the first valid one. It may get 82459191Storek * overwritten if more data are arriving, but this is 82559191Storek * too expensive to check and gains nothing (we already 82659191Storek * lost out; all we can do at this point is trade one 82759191Storek * kind of loss for another). 82855107Storek */ 82959191Storek n -= get; 83059191Storek if (n > ZLRB_RING_SIZE) { 83159191Storek zsoverrun(unit, &cs->cs_rotime, "ring"); 83259191Storek get += n - ZLRB_RING_SIZE; 83359191Storek n = ZLRB_RING_SIZE; 83459191Storek } 83559191Storek while (--n >= 0) { 83659191Storek /* race to keep ahead of incoming interrupts */ 83759191Storek c = cs->cs_rbuf[get++ & ZLRB_RING_MASK]; 83859191Storek switch (ZRING_TYPE(c)) { 83959191Storek 84059191Storek case ZRING_RINT: 84159191Storek c = ZRING_VALUE(c); 84255107Storek if (c & ZSRR1_DO) 84355107Storek zsoverrun(unit, &cs->cs_fotime, "fifo"); 84455107Storek cc = c >> 8; 84555107Storek if (c & ZSRR1_FE) 84655107Storek cc |= TTY_FE; 84755107Storek if (c & ZSRR1_PE) 84855107Storek cc |= TTY_PE; 84955107Storek /* 85055107Storek * this should be done through 85155107Storek * bstreams XXX gag choke 85255107Storek */ 85355107Storek if (unit == ZS_KBD) 85455107Storek kbd_rint(cc); 85555107Storek else if (unit == ZS_MOUSE) 85655107Storek ms_rint(cc); 85755107Storek else 85859191Storek line->l_rint(cc, tp); 85959191Storek break; 86055107Storek 86159191Storek case ZRING_XINT: 86259191Storek /* 86359191Storek * Transmit done: change registers and resume, 86459191Storek * or clear BUSY. 86559191Storek */ 86659191Storek if (cs->cs_heldchange) { 86759191Storek s = splzs(); 86859191Storek c = zc->zc_csr; 86959191Storek if ((c & ZSRR0_DCD) == 0) 87059191Storek cs->cs_preg[3] &= ~ZSWR3_HFC; 87159191Storek bcopy((caddr_t)cs->cs_preg, 87259191Storek (caddr_t)cs->cs_creg, 16); 87359191Storek zs_loadchannelregs(zc, cs->cs_creg); 87459191Storek splx(s); 87559191Storek cs->cs_heldchange = 0; 87659191Storek if (cs->cs_heldtbc && 87759191Storek (tp->t_state & TS_TTSTOP) == 0) { 87859191Storek cs->cs_tbc = cs->cs_heldtbc - 1; 87959191Storek zc->zc_data = *cs->cs_tba++; 88059191Storek goto again; 88159191Storek } 88255107Storek } 88359191Storek tp->t_state &= ~TS_BUSY; 88459191Storek if (tp->t_state & TS_FLUSH) 88559191Storek tp->t_state &= ~TS_FLUSH; 88659191Storek else 88759191Storek ndflush(&tp->t_outq, 88859191Storek cs->cs_tba - tp->t_outq.c_cf); 88959191Storek line->l_start(tp); 89059191Storek break; 89159191Storek 89259191Storek case ZRING_SINT: 89359191Storek /* 89459191Storek * Status line change. HFC bit is run in 89559191Storek * hardware interrupt, to avoid locking 89659191Storek * at splzs here. 89759191Storek */ 89859191Storek c = ZRING_VALUE(c); 89959191Storek if ((c ^ cs->cs_rr0) & ZSRR0_DCD) { 90059191Storek cc = (c & ZSRR0_DCD) != 0; 90159191Storek if (line->l_modem(tp, cc) == 0) 90259191Storek zs_modem(cs, cc); 90355107Storek } 90459191Storek cs->cs_rr0 = c; 90559191Storek break; 90655107Storek 90759191Storek default: 90859191Storek log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n", 90959191Storek unit >> 1, (unit & 1) + 'a', c); 91059191Storek break; 91155107Storek } 91255107Storek } 91359191Storek cs->cs_rbget = get; 91459191Storek goto again; 91555107Storek } 91655107Storek return (1); 91755107Storek } 91855107Storek 91955107Storek int 92055107Storek zsioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p) 92155107Storek { 92255107Storek int unit = minor(dev); 92355107Storek struct zsinfo *zi = zscd.cd_devs[unit >> 1]; 92455107Storek register struct tty *tp = zi->zi_cs[unit & 1].cs_ttyp; 92555107Storek register int error; 92655107Storek 92759191Storek error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p); 92855107Storek if (error >= 0) 92955107Storek return (error); 93055107Storek error = ttioctl(tp, cmd, data, flag); 93155107Storek if (error >= 0) 93255107Storek return (error); 93355107Storek 93455107Storek switch (cmd) { 93555107Storek 93655107Storek case TIOCSBRK: 93755107Storek /* FINISH ME ... need implicit TIOCCBRK in zsclose as well */ 93855107Storek 93955107Storek case TIOCCBRK: 94055107Storek 94155107Storek case TIOCSDTR: 94255107Storek 94355107Storek case TIOCCDTR: 94455107Storek 94555107Storek case TIOCMSET: 94655107Storek 94755107Storek case TIOCMBIS: 94855107Storek 94955107Storek case TIOCMBIC: 95055107Storek 95155107Storek case TIOCMGET: 95255107Storek 95355107Storek default: 95455107Storek return (ENOTTY); 95555107Storek } 95655107Storek return (0); 95755107Storek } 95855107Storek 95955107Storek /* 96055107Storek * Start or restart transmission. 96155107Storek */ 96255107Storek static void 96355107Storek zsstart(register struct tty *tp) 96455107Storek { 96555107Storek register struct zs_chanstate *cs; 96655107Storek register int s, nch; 96755107Storek int unit = minor(tp->t_dev); 96855107Storek struct zsinfo *zi = zscd.cd_devs[unit >> 1]; 96955107Storek 97055107Storek cs = &zi->zi_cs[unit & 1]; 97155107Storek s = spltty(); 97255107Storek 97355107Storek /* 97455107Storek * If currently active or delaying, no need to do anything. 97555107Storek */ 97655107Storek if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) 97755107Storek goto out; 97855107Storek 97955107Storek /* 98055107Storek * If there are sleepers, and output has drained below low 98155107Storek * water mark, awaken. 98255107Storek */ 98355107Storek if (tp->t_outq.c_cc <= tp->t_lowat) { 98455107Storek if (tp->t_state & TS_ASLEEP) { 98555107Storek tp->t_state &= ~TS_ASLEEP; 98655107Storek wakeup((caddr_t)&tp->t_outq); 98755107Storek } 98855107Storek selwakeup(&tp->t_wsel); 98955107Storek } 99055107Storek 99155107Storek nch = ndqb(&tp->t_outq, 0); /* XXX */ 99255107Storek if (nch) { 99355107Storek register char *p = tp->t_outq.c_cf; 99455107Storek 99555107Storek /* mark busy, enable tx done interrupts, & send first byte */ 99655107Storek tp->t_state |= TS_BUSY; 99755107Storek (void) splzs(); 99855107Storek cs->cs_preg[1] |= ZSWR1_TIE; 99955107Storek cs->cs_creg[1] |= ZSWR1_TIE; 100055107Storek ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]); 100155107Storek cs->cs_zc->zc_data = *p; 100255107Storek cs->cs_tba = p + 1; 100355107Storek cs->cs_tbc = nch - 1; 100455107Storek } else { 100555107Storek /* 100655107Storek * Nothing to send, turn off transmit done interrupts. 100755107Storek * This is useful if something is doing polled output. 100855107Storek */ 100955107Storek (void) splzs(); 101055107Storek cs->cs_preg[1] &= ~ZSWR1_TIE; 101155107Storek cs->cs_creg[1] &= ~ZSWR1_TIE; 101255107Storek ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]); 101355107Storek } 101455107Storek out: 101555107Storek splx(s); 101655107Storek } 101755107Storek 101855107Storek /* 101955107Storek * Stop output, e.g., for ^S or output flush. 102055107Storek */ 102155107Storek static void 102255107Storek zsstop(register struct tty *tp, int flag) 102355107Storek { 102455107Storek register struct zs_chanstate *cs; 102555107Storek register int s, unit = minor(tp->t_dev); 102655107Storek struct zsinfo *zi = zscd.cd_devs[unit >> 1]; 102755107Storek 102855107Storek cs = &zi->zi_cs[unit & 1]; 102955107Storek s = splzs(); 103055107Storek if (tp->t_state & TS_BUSY) { 103155107Storek /* 103255107Storek * Device is transmitting; must stop it. 103355107Storek */ 103455107Storek cs->cs_tbc = 0; 103555107Storek if ((tp->t_state & TS_TTSTOP) == 0) 103655107Storek tp->t_state |= TS_FLUSH; 103755107Storek } 103855107Storek splx(s); 103955107Storek } 104055107Storek 104155107Storek /* 104255107Storek * Set ZS tty parameters from termios. 104355107Storek * 104455107Storek * This routine makes use of the fact that only registers 104555107Storek * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written. 104655107Storek */ 104755107Storek static int 104855107Storek zsparam(register struct tty *tp, register struct termios *t) 104955107Storek { 105055107Storek int unit = minor(tp->t_dev); 105155107Storek struct zsinfo *zi = zscd.cd_devs[unit >> 1]; 105255107Storek register struct zs_chanstate *cs = &zi->zi_cs[unit & 1]; 105355107Storek register int tmp, tmp5, cflag, s; 105455107Storek 105555107Storek /* 105655107Storek * Because PCLK is only run at 4.9 MHz, the fastest we 105755107Storek * can go is 51200 baud (this corresponds to TC=1). 105855107Storek * This is somewhat unfortunate as there is no real 105955107Storek * reason we should not be able to handle higher rates. 106055107Storek */ 106155107Storek tmp = t->c_ospeed; 106255107Storek if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp)) 106355107Storek return (EINVAL); 106455107Storek if (tmp == 0) { 106555107Storek /* stty 0 => drop DTR and RTS */ 106655107Storek zs_modem(cs, 0); 106755107Storek return (0); 106855107Storek } 106955107Storek tmp = BPS_TO_TCONST(PCLK / 16, tmp); 107055107Storek if (tmp < 2) 107155107Storek return (EINVAL); 107255107Storek 107355107Storek cflag = t->c_cflag; 107455107Storek tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp); 107555107Storek tp->t_cflag = cflag; 107655107Storek 107755107Storek /* 107855107Storek * Block interrupts so that state will not 107955107Storek * be altered until we are done setting it up. 108055107Storek */ 108155107Storek s = splzs(); 108255107Storek cs->cs_preg[12] = tmp; 108355107Storek cs->cs_preg[13] = tmp >> 8; 108455107Storek cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE; 108555107Storek switch (cflag & CSIZE) { 108655107Storek case CS5: 108755107Storek tmp = ZSWR3_RX_5; 108855107Storek tmp5 = ZSWR5_TX_5; 108955107Storek break; 109055107Storek case CS6: 109155107Storek tmp = ZSWR3_RX_6; 109255107Storek tmp5 = ZSWR5_TX_6; 109355107Storek break; 109455107Storek case CS7: 109555107Storek tmp = ZSWR3_RX_7; 109655107Storek tmp5 = ZSWR5_TX_7; 109755107Storek break; 109855107Storek case CS8: 109955107Storek default: 110055107Storek tmp = ZSWR3_RX_8; 110155107Storek tmp5 = ZSWR5_TX_8; 110255107Storek break; 110355107Storek } 110455107Storek 110555107Storek /* 110655107Storek * Output hardware flow control on the chip is horrendous: if 110755107Storek * carrier detect drops, the receiver is disabled. Hence we 110855107Storek * can only do this when the carrier is on. 110955107Storek */ 111055107Storek if (cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD) 111155107Storek tmp |= ZSWR3_HFC | ZSWR3_RX_ENABLE; 111255107Storek else 111355107Storek tmp |= ZSWR3_RX_ENABLE; 111455107Storek cs->cs_preg[3] = tmp; 111555107Storek cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS; 111655107Storek 111755107Storek tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB); 111855107Storek if ((cflag & PARODD) == 0) 111955107Storek tmp |= ZSWR4_EVENP; 112055107Storek if (cflag & PARENB) 112155107Storek tmp |= ZSWR4_PARENB; 112255107Storek cs->cs_preg[4] = tmp; 112355107Storek cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR; 112455107Storek cs->cs_preg[10] = ZSWR10_NRZ; 112555107Storek cs->cs_preg[11] = ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD; 112655107Storek cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA; 112755107Storek cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE; 112855107Storek 112955107Storek /* 113055107Storek * If nothing is being transmitted, set up new current values, 113155107Storek * else mark them as pending. 113255107Storek */ 113355107Storek if (cs->cs_heldchange == 0) { 113455107Storek if (cs->cs_ttyp->t_state & TS_BUSY) { 113555107Storek cs->cs_heldtbc = cs->cs_tbc; 113655107Storek cs->cs_tbc = 0; 113755107Storek cs->cs_heldchange = 1; 113855107Storek } else { 113955107Storek bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16); 114055107Storek zs_loadchannelregs(cs->cs_zc, cs->cs_creg); 114155107Storek } 114255107Storek } 114355107Storek splx(s); 114455107Storek return (0); 114555107Storek } 114655107Storek 114755107Storek /* 114855107Storek * Raise or lower modem control (DTR/RTS) signals. If a character is 114955107Storek * in transmission, the change is deferred. 115055107Storek */ 115155107Storek static void 115255107Storek zs_modem(struct zs_chanstate *cs, int onoff) 115355107Storek { 115455107Storek int s, bis, and; 115555107Storek 115655107Storek if (onoff) { 115755107Storek bis = ZSWR5_DTR | ZSWR5_RTS; 115855107Storek and = ~0; 115955107Storek } else { 116055107Storek bis = 0; 116155107Storek and = ~(ZSWR5_DTR | ZSWR5_RTS); 116255107Storek } 116355107Storek s = splzs(); 116455107Storek cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and; 116555107Storek if (cs->cs_heldchange == 0) { 116655107Storek if (cs->cs_ttyp->t_state & TS_BUSY) { 116755107Storek cs->cs_heldtbc = cs->cs_tbc; 116855107Storek cs->cs_tbc = 0; 116955107Storek cs->cs_heldchange = 1; 117055107Storek } else { 117155107Storek cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and; 117255107Storek ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]); 117355107Storek } 117455107Storek } 117555107Storek splx(s); 117655107Storek } 117755107Storek 117855107Storek /* 117955107Storek * Write the given register set to the given zs channel in the proper order. 118055107Storek * The channel must not be transmitting at the time. The receiver will 118155107Storek * be disabled for the time it takes to write all the registers. 118255107Storek */ 118355107Storek static void 118455107Storek zs_loadchannelregs(volatile struct zschan *zc, u_char *reg) 118555107Storek { 118655107Storek int i; 118755107Storek 118855107Storek zc->zc_csr = ZSM_RESET_ERR; /* reset error condition */ 118955107Storek i = zc->zc_data; /* drain fifo */ 119055107Storek i = zc->zc_data; 119155107Storek i = zc->zc_data; 119255107Storek ZS_WRITE(zc, 4, reg[4]); 119355107Storek ZS_WRITE(zc, 10, reg[10]); 119455107Storek ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE); 119555107Storek ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE); 119655107Storek ZS_WRITE(zc, 1, reg[1]); 119755107Storek ZS_WRITE(zc, 9, reg[9]); 119855107Storek ZS_WRITE(zc, 11, reg[11]); 119955107Storek ZS_WRITE(zc, 12, reg[12]); 120055107Storek ZS_WRITE(zc, 13, reg[13]); 120155107Storek ZS_WRITE(zc, 14, reg[14]); 120255107Storek ZS_WRITE(zc, 15, reg[15]); 120355107Storek ZS_WRITE(zc, 3, reg[3]); 120455107Storek ZS_WRITE(zc, 5, reg[5]); 120555107Storek } 120655107Storek 120755107Storek #ifdef KGDB 120855107Storek /* 120955107Storek * Get a character from the given kgdb channel. Called at splhigh(). 121055107Storek */ 121155107Storek static int 121255107Storek zs_kgdb_getc(void *arg) 121355107Storek { 121455107Storek register volatile struct zschan *zc = (volatile struct zschan *)arg; 121555107Storek 121655107Storek while ((zc->zc_csr & ZSRR0_RX_READY) == 0) 121755107Storek continue; 121855107Storek return (zc->zc_data); 121955107Storek } 122055107Storek 122155107Storek /* 122255107Storek * Put a character to the given kgdb channel. Called at splhigh(). 122355107Storek */ 122455107Storek static void 122555107Storek zs_kgdb_putc(void *arg, int c) 122655107Storek { 122755107Storek register volatile struct zschan *zc = (volatile struct zschan *)arg; 122855107Storek 122955107Storek while ((zc->zc_csr & ZSRR0_TX_READY) == 0) 123055107Storek continue; 123155107Storek zc->zc_data = c; 123255107Storek } 123355107Storek 123455107Storek /* 123555107Storek * Set up for kgdb; called at boot time before configuration. 123655107Storek * KGDB interrupts will be enabled later when zs0 is configured. 123755107Storek */ 123855107Storek void 123955107Storek zs_kgdb_init() 124055107Storek { 124155107Storek volatile struct zsdevice *addr; 124255107Storek volatile struct zschan *zc; 124355107Storek int unit, zs; 124455107Storek 124555107Storek if (major(kgdb_dev) != ZSMAJOR) 124655107Storek return; 124755107Storek unit = minor(kgdb_dev); 124855107Storek /* 124955107Storek * Unit must be 0 or 1 (zs0). 125055107Storek */ 125155107Storek if ((unsigned)unit >= ZS_KBD) { 125255107Storek printf("zs_kgdb_init: bad minor dev %d\n", unit); 125355107Storek return; 125455107Storek } 125555107Storek zs = unit >> 1; 125655107Storek if ((addr = zsaddr[zs]) == NULL) 125755107Storek addr = zsaddr[zs] = findzs(zs); 125855107Storek unit &= 1; 125955107Storek zc = unit == 0 ? &addr->zs_chan[CHAN_A] : &addr->zs_chan[CHAN_B]; 126055107Storek zs_kgdb_savedspeed = zs_getspeed(zc); 126155107Storek printf("zs_kgdb_init: attaching zs%d%c at %d baud\n", 126255107Storek zs, unit + 'a', kgdb_rate); 126355107Storek zs_reset(zc, 1, kgdb_rate); 126455107Storek kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc); 126555107Storek } 126655107Storek #endif /* KGDB */ 1267