Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.26
      1 /*	$NetBSD: zs.c,v 1.26 1998/09/10 21:40:42 scottr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996-1998 Bill Studenmund
      5  * Copyright (c) 1995 Gordon W. Ross
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  * 4. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by Gordon Ross
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Zilog Z8530 Dual UART driver (machine-dependent part)
     36  *
     37  * Runs two serial lines per chip using slave drivers.
     38  * Plain tty/async lines use the zs_async slave.
     39  * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
     40  * Other ports use their own mice & keyboard slaves.
     41  *
     42  * Credits & history:
     43  *
     44  * With NetBSD 1.1, port-mac68k started using a port of the port-sparc
     45  * (port-sun3?) zs.c driver (which was in turn based on code in the
     46  * Berkeley 4.4 Lite release). Bill Studenmund did the port, with
     47  * help from Allen Briggs and Gordon Ross <gwr (at) netbsd.org>. Noud de
     48  * Brouwer field-tested the driver at a local ISP.
     49  *
     50  * Bill Studenmund and Gordon Ross then ported the machine-independant
     51  * z8530 driver to work with port-mac68k. NetBSD 1.2 contained an
     52  * intermediate version (mac68k using a local, patched version of
     53  * the m.i. drivers), with NetBSD 1.3 containing a full version.
     54  */
     55 
     56 #include "opt_ddb.h"
     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/psc.h>
     73 #include <machine/viareg.h>
     74 
     75 #include <dev/cons.h>
     76 #include <dev/ic/z8530reg.h>
     77 #include <machine/z8530var.h>
     78 #include <mac68k/dev/zs_cons.h>
     79 
     80 /* Are these in a header file anywhere? */
     81 /* Booter flags interface */
     82 #define ZSMAC_RAW	0x01
     83 #define ZSMAC_LOCALTALK	0x02
     84 #define	ZS_STD_BRG	(57600*4)
     85 
     86 #include "zsc.h"	/* get the # of zs chips defined */
     87 
     88 /*
     89  * Some warts needed by z8530tty.c -
     90  */
     91 int zs_def_cflag = (CREAD | CS8 | HUPCL);
     92 int zs_major = 12;
     93 
     94 /*
     95  * abort detection on console will now timeout after iterating on a loop
     96  * the following # of times. Cheep hack. Also, abort detection is turned
     97  * off after a timeout (i.e. maybe there's not a terminal hooked up).
     98  */
     99 #define ZSABORT_DELAY 3000000
    100 
    101 /*
    102  * Define interrupt levels.
    103  */
    104 #define ZSHARD_PRI	4	/* Wired on the CPU board... */
    105 /*
    106  * Serial port cards with zs chips on them are actually at the
    107  * NuBus interrupt level, which is lower than 4. But blocking
    108  * level 4 interrupts will block those interrupts too, so level
    109  * 4 is fine.
    110  */
    111 
    112 /* The layout of this is hardware-dependent (padding, order). */
    113 struct zschan {
    114 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    115 	u_char		zc_xxx0;
    116 	u_char		zc_xxx1;	/* part of the other channel lives here! */
    117 	u_char		zc_xxx2;	/* Yea Apple! */
    118 	volatile u_char	zc_data;	/* data */
    119 	u_char		zc_xxx3;
    120 	u_char		zc_xxx4;
    121 	u_char		zc_xxx5;
    122 };
    123 
    124 /* Saved PROM mappings */
    125 static char *zsaddr[NZSC];	/* See zs_init() */
    126 /* Flags from cninit() */
    127 static int zs_hwflags[NZSC][2];
    128 /* Default speed for each channel */
    129 static int zs_defspeed[NZSC][2] = {
    130 	{ 9600, 	/* tty00 */
    131 	  9600 },	/* tty01 */
    132 };
    133 /* console stuff */
    134 void	*zs_conschan = 0;
    135 int	zs_consunit;
    136 #ifdef	ZS_CONSOLE_ABORT
    137 int	zs_cons_canabort = 1;
    138 #else
    139 int	zs_cons_canabort = 0;
    140 #endif /* ZS_CONSOLE_ABORT*/
    141 /* device to which the console is attached--if serial. */
    142 dev_t	mac68k_zsdev;
    143 /* Mac stuff */
    144 volatile unsigned char *sccA = 0;
    145 
    146 int	zs_cn_check_speed __P((int bps));
    147 
    148 /*
    149  * Even though zsparam will set up the clock multiples, etc., we
    150  * still set them here as: 1) mice & keyboards don't use zsparam,
    151  * and 2) the console stuff uses these defaults before device
    152  * attach.
    153  */
    154 
    155 static u_char zs_init_reg[16] = {
    156 	0,	/* 0: CMD (reset, etc.) */
    157 	0,	/* 1: No interrupts yet. */
    158 	0x18 + ZSHARD_PRI,	/* IVECT */
    159 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    160 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    161 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    162 	0,	/* 6: TXSYNC/SYNCLO */
    163 	0,	/* 7: RXSYNC/SYNCHI */
    164 	0,	/* 8: alias for data port */
    165 	ZSWR9_MASTER_IE,
    166 	0,	/*10: Misc. TX/RX control bits */
    167 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    168 	14,	/*12: BAUDLO (default=9600) */
    169 	0,	/*13: BAUDHI (default=9600) */
    170 	ZSWR14_BAUD_ENA,
    171 	ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
    172 };
    173 
    174 struct zschan *
    175 zs_get_chan_addr(zsc_unit, channel)
    176 	int zsc_unit, channel;
    177 {
    178 	char *addr;
    179 	struct zschan *zc;
    180 
    181 	if (zsc_unit >= NZSC)
    182 		return NULL;
    183 	addr = zsaddr[zsc_unit];
    184 	if (addr == NULL)
    185 		return NULL;
    186 	if (channel == 0) {
    187 		zc = (struct zschan *)(addr + 2);
    188 		/* handle the fact the ports are intertwined. */
    189 	} else {
    190 		zc = (struct zschan *)(addr);
    191 	}
    192 	return (zc);
    193 }
    194 
    195 
    196 /* Find PROM mappings (for console support). */
    197 int zsinited = 0; /* 0 = not, 1 = inited, not attached, 2= attached */
    198 
    199 void
    200 zs_init()
    201 {
    202 	if ((zsinited == 2)&&(zsaddr[0] != (char *) sccA))
    203 		panic("Moved zs0 address after attached!");
    204 	zsaddr[0] = (char *) sccA;
    205 	zsinited = 1;
    206 	if (zs_conschan != 0){ /* we might have moved io under the console */
    207 		zs_conschan = zs_get_chan_addr(0, zs_consunit);
    208 		/* so recalc the console port */
    209 	}
    210 }
    211 
    212 
    213 /****************************************************************
    214  * Autoconfig
    215  ****************************************************************/
    216 
    217 /* Definition of the driver for autoconfig. */
    218 static int	zsc_match __P((struct device *, struct cfdata *, void *));
    219 static void	zsc_attach __P((struct device *, struct device *, void *));
    220 static int  zsc_print __P((void *, const char *name));
    221 
    222 struct cfattach zsc_ca = {
    223 	sizeof(struct zsc_softc), zsc_match, zsc_attach
    224 };
    225 
    226 extern struct cfdriver zsc_cd;
    227 
    228 int zshard __P((void *));
    229 int zssoft __P((void *));
    230 
    231 
    232 /*
    233  * Is the zs chip present?
    234  */
    235 static int
    236 zsc_match(parent, cf, aux)
    237 	struct device *parent;
    238 	struct cfdata *cf;
    239 	void *aux;
    240 {
    241 	return 1;
    242 }
    243 
    244 /*
    245  * Attach a found zs.
    246  *
    247  * Match slave number to zs unit number, so that misconfiguration will
    248  * not set up the keyboard as ttya, etc.
    249  */
    250 static void
    251 zsc_attach(parent, self, aux)
    252 	struct device *parent;
    253 	struct device *self;
    254 	void *aux;
    255 {
    256 	struct zsc_softc *zsc = (void *) self;
    257 	struct zsc_attach_args zsc_args;
    258 	volatile struct zschan *zc;
    259 	struct xzs_chanstate *xcs;
    260 	struct zs_chanstate *cs;
    261 	int zsc_unit, channel;
    262 	int s, chip, theflags;
    263 
    264 	if (!zsinited)
    265 		zs_init();
    266 	zsinited = 2;
    267 
    268 	zsc_unit = zsc->zsc_dev.dv_unit;
    269 
    270 	/* Make sure everything's inited ok. */
    271 	if (zsaddr[zsc_unit] == NULL)
    272 		panic("zs_attach: zs%d not mapped\n", zsc_unit);
    273 
    274 	chip = 0; /* We'll deal with chip types post 1.2 */
    275 	printf(" chip type %d \n",chip);
    276 
    277 	/*
    278 	 * Initialize software state for each channel.
    279 	 */
    280 	for (channel = 0; channel < 2; channel++) {
    281 		zsc_args.channel = channel;
    282 		zsc_args.hwflags = zs_hwflags[zsc_unit][channel];
    283 		xcs = &zsc->xzsc_xcs_store[channel];
    284 		cs  = &xcs->xzs_cs;
    285 		zsc->zsc_cs[channel] = cs;
    286 
    287 		cs->cs_channel = channel;
    288 		cs->cs_private = NULL;
    289 		cs->cs_ops = &zsops_null;
    290 
    291 		zc = zs_get_chan_addr(zsc_unit, channel);
    292 		cs->cs_reg_csr  = &zc->zc_csr;
    293 		cs->cs_reg_data = &zc->zc_data;
    294 
    295 		bcopy(zs_init_reg, cs->cs_creg, 16);
    296 		bcopy(zs_init_reg, cs->cs_preg, 16);
    297 
    298 		/* Current BAUD rate generator clock. */
    299 		cs->cs_brg_clk = ZS_STD_BRG;	/* RTxC is 230400*16, so use 230400 */
    300 		cs->cs_defspeed = zs_defspeed[zsc_unit][channel];
    301 		cs->cs_defcflag = zs_def_cflag;
    302 
    303 		/* Make these correspond to cs_defcflag (-crtscts) */
    304 		cs->cs_rr0_dcd = ZSRR0_DCD;
    305 		cs->cs_rr0_cts = 0;
    306 		cs->cs_wr5_dtr = ZSWR5_DTR;
    307 		cs->cs_wr5_rts = 0;
    308 
    309 #ifdef __notyet__
    310 		cs->cs_slave_type = ZS_SLAVE_NONE;
    311 #endif
    312 
    313 		/* Define BAUD rate stuff. */
    314 		xcs->cs_clocks[0].clk = ZS_STD_BRG * 16;
    315 		xcs->cs_clocks[0].flags = ZSC_RTXBRG;
    316 		xcs->cs_clocks[1].flags =
    317 			ZSC_RTXBRG | ZSC_RTXDIV | ZSC_VARIABLE | ZSC_EXTERN;
    318 		xcs->cs_clocks[2].flags = ZSC_TRXDIV | ZSC_VARIABLE;
    319 		xcs->cs_clock_count = 3;
    320 		if (channel == 0) {
    321 			theflags = mac68k_machine.modem_flags;
    322 			xcs->cs_clocks[1].clk = mac68k_machine.modem_dcd_clk;
    323 			xcs->cs_clocks[2].clk = mac68k_machine.modem_cts_clk;
    324 		} else {
    325 			theflags = mac68k_machine.print_flags;
    326 			xcs->cs_clocks[1].flags = ZSC_VARIABLE;
    327 			/*
    328 			 * Yes, we aren't defining ANY clock source enables for the
    329 			 * printer's DCD clock in. The hardware won't let us
    330 			 * use it. But a clock will freak out the chip, so we
    331 			 * let you set it, telling us to bar interrupts on the line.
    332 			 */
    333 			xcs->cs_clocks[1].clk = mac68k_machine.print_dcd_clk;
    334 			xcs->cs_clocks[2].clk = mac68k_machine.print_cts_clk;
    335 		}
    336 		if (xcs->cs_clocks[1].clk)
    337 			zsc_args.hwflags |= ZS_HWFLAG_NO_DCD;
    338 		if (xcs->cs_clocks[2].clk)
    339 			zsc_args.hwflags |= ZS_HWFLAG_NO_CTS;
    340 
    341 		printf("zsc%d channel %d: d_speed %6d DCD clk %ld CTS clk %ld",
    342 				zsc_unit, channel, cs->cs_defspeed,
    343 				xcs->cs_clocks[1].clk, xcs->cs_clocks[2].clk);
    344 
    345 		/* Set defaults in our "extended" chanstate. */
    346 		xcs->cs_csource = 0;
    347 		xcs->cs_psource = 0;
    348 		xcs->cs_cclk_flag = 0;  /* Nothing fancy by default */
    349 		xcs->cs_pclk_flag = 0;
    350 
    351 		if (theflags & ZSMAC_RAW) {
    352 			zsc_args.hwflags |= ZS_HWFLAG_RAW;
    353 			printf(" (raw defaults)");
    354 		}
    355 
    356 		/*
    357 		 * XXX - This might be better done with a "stub" driver
    358 		 * (to replace zstty) that ignores LocalTalk for now.
    359 		 */
    360 		if (theflags & ZSMAC_LOCALTALK) {
    361 			printf(" shielding from LocalTalk");
    362 			cs->cs_defspeed = 1;
    363 			cs->cs_creg[ZSRR_BAUDLO] = cs->cs_preg[ZSRR_BAUDLO] = 0xff;
    364 			cs->cs_creg[ZSRR_BAUDHI] = cs->cs_preg[ZSRR_BAUDHI] = 0xff;
    365 			zs_write_reg(cs, ZSRR_BAUDLO, 0xff);
    366 			zs_write_reg(cs, ZSRR_BAUDHI, 0xff);
    367 			/*
    368 			 * If we might have LocalTalk, then make sure we have the
    369 			 * Baud rate low-enough to not do any damage.
    370 			 */
    371 		}
    372 
    373 		/*
    374 		 * We used to disable chip interrupts here, but we now
    375 		 * do that in zscnprobe, just in case MacOS left the chip on.
    376 		 */
    377 
    378 		xcs->cs_chip = chip;
    379 
    380 		/* Stash away a copy of the final H/W flags. */
    381 		xcs->cs_hwflags = zsc_args.hwflags;
    382 
    383 		printf("\n");
    384 
    385 		/*
    386 		 * Look for a child driver for this channel.
    387 		 * The child attach will setup the hardware.
    388 		 */
    389 		if (!config_found(self, (void *)&zsc_args, zsc_print)) {
    390 			/* No sub-driver.  Just reset it. */
    391 			u_char reset = (channel == 0) ?
    392 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    393 			s = splzs();
    394 			zs_write_reg(cs,  9, reset);
    395 			splx(s);
    396 		}
    397 	}
    398 
    399 	if (current_mac_model->class == MACH_CLASSAV) {
    400 		add_psc_lev4_intr(PSCINTR_SCCA, zshard, zsc);
    401 		add_psc_lev4_intr(PSCINTR_SCCB, zshard, zsc);
    402 	} else {
    403 		intr_establish(zshard, zsc, ZSHARD_PRI);
    404 	}
    405 
    406 	/* Now safe to enable interrupts. */
    407 
    408 	/*
    409 	 * Set the master interrupt enable and interrupt vector.
    410 	 * (common to both channels, do it on A)
    411 	 */
    412 	cs = zsc->zsc_cs[0];
    413 	s = splzs();
    414 	/* interrupt vector */
    415 	zs_write_reg(cs, 2, zs_init_reg[2]);
    416 	/* master interrupt control (enable) */
    417 	zs_write_reg(cs, 9, zs_init_reg[9]);
    418 	splx(s);
    419 }
    420 
    421 static int
    422 zsc_print(aux, name)
    423 	void *aux;
    424 	const char *name;
    425 {
    426 	struct zsc_attach_args *args = aux;
    427 
    428 	if (name != NULL)
    429 		printf("%s: ", name);
    430 
    431 	if (args->channel != -1)
    432 		printf(" channel %d", args->channel);
    433 
    434 	return UNCONF;
    435 }
    436 
    437 int
    438 zsmdioctl(cs, cmd, data)
    439 	struct zs_chanstate *cs;
    440 	u_long cmd;
    441 	caddr_t data;
    442 {
    443 	switch (cmd) {
    444 	default:
    445 		return (-1);
    446 	}
    447 	return (0);
    448 }
    449 
    450 void
    451 zsmd_setclock(cs)
    452 	struct zs_chanstate *cs;
    453 {
    454 	struct xzs_chanstate *xcs = (void *)cs;
    455 
    456 	if (cs->cs_channel != 0)
    457 		return;
    458 
    459 	/*
    460 	 * If the new clock has the external bit set, then select the
    461 	 * external source.
    462 	 */
    463 	via_set_modem((xcs->cs_pclk_flag & ZSC_EXTERN) ? 1 : 0);
    464 }
    465 
    466 static int zssoftpending;
    467 
    468 /*
    469  * Do the minimum work to pull data off of the chip and queue it up
    470  * for later processing.
    471  */
    472 int
    473 zshard(arg)
    474 	void *arg;
    475 {
    476 	struct zsc_softc *zsc = (struct zsc_softc *)arg;
    477 	int rval;
    478 
    479 	if (zsc == NULL)
    480 		return 0;
    481 
    482 	rval = zsc_intr_hard(zsc);
    483 	if ((zsc->zsc_cs[0]->cs_softreq) || (zsc->zsc_cs[1]->cs_softreq)) {
    484 		/* zsc_req_softint(zsc); */
    485 		/* We are at splzs here, so no need to lock. */
    486 		if (zssoftpending == 0) {
    487 			zssoftpending = 1;
    488 			setsoftserial();
    489 		}
    490 	}
    491 	return (rval);
    492 }
    493 
    494 /*
    495  * Look at all of the zsc softint queues.
    496  */
    497 int
    498 zssoft(arg)
    499 	void *arg;
    500 {
    501 	struct zsc_softc *zsc;
    502 	int unit;
    503 
    504 	/* This is not the only ISR on this IPL. */
    505 	if (zssoftpending == 0)
    506 		return (0);
    507 
    508 	/*
    509 	 * The soft intr. bit will be set by zshard only if
    510 	 * the variable zssoftpending is zero.
    511 	 */
    512 	zssoftpending = 0;
    513 
    514 	for (unit = 0; unit < zsc_cd.cd_ndevs; ++unit) {
    515 		zsc = zsc_cd.cd_devs[unit];
    516 		if (zsc == NULL)
    517 			continue;
    518 		(void) zsc_intr_soft(zsc);
    519 	}
    520 	return (1);
    521 }
    522 
    523 
    524 #ifndef ZS_TOLERANCE
    525 #define ZS_TOLERANCE 51
    526 /* 5% in tenths of a %, plus 1 so that exactly 5% will be ok. */
    527 #endif
    528 
    529 /*
    530  * check out a rate for acceptability from the internal clock
    531  * source. Used in console config to validate a requested
    532  * default speed. Placed here so that all the speed checking code is
    533  * in one place.
    534  *
    535  * != 0 means ok.
    536  */
    537 int
    538 zs_cn_check_speed(bps)
    539 	int bps;	/* target rate */
    540 {
    541 	int tc, rate;
    542 
    543 	tc = BPS_TO_TCONST(ZS_STD_BRG, bps);
    544 	if (tc < 0)
    545 		return 0;
    546 	rate = TCONST_TO_BPS(ZS_STD_BRG, tc);
    547 	if (ZS_TOLERANCE > abs(((rate - bps)*1000)/bps))
    548 		return 1;
    549 	else
    550 		return 0;
    551 }
    552 
    553 /*
    554  * Search through the signal sources in the channel, and
    555  * pick the best one for the baud rate requested. Return
    556  * a -1 if not achievable in tolerance. Otherwise return 0
    557  * and fill in the values.
    558  *
    559  * This routine draws inspiration from the Atari port's zs.c
    560  * driver in NetBSD 1.1 which did the same type of source switching.
    561  * Tolerance code inspired by comspeed routine in isa/com.c.
    562  *
    563  * By Bill Studenmund, 1996-05-12
    564  */
    565 int
    566 zs_set_speed(cs, bps)
    567 	struct zs_chanstate *cs;
    568 	int bps;	/* bits per second */
    569 {
    570 	struct xzs_chanstate *xcs = (void *) cs;
    571 	int i, tc, tc0 = 0, tc1, s, sf = 0;
    572 	int src, rate0, rate1, err, tol;
    573 
    574 	if (bps == 0)
    575 		return (0);
    576 
    577 	src = -1;		/* no valid source yet */
    578 	tol = ZS_TOLERANCE;
    579 
    580 	/*
    581 	 * Step through all the sources and see which one matches
    582 	 * the best. A source has to match BETTER than tol to be chosen.
    583 	 * Thus if two sources give the same error, the first one will be
    584 	 * chosen. Also, allow for the possability that one source might run
    585 	 * both the BRG and the direct divider (i.e. RTxC).
    586 	 */
    587 	for (i=0; i < xcs->cs_clock_count; i++) {
    588 		if (xcs->cs_clocks[i].clk <= 0)
    589 			continue;	/* skip non-existant or bad clocks */
    590 		if (xcs->cs_clocks[i].flags & ZSC_BRG) {
    591 			/* check out BRG at /16 */
    592 			tc1 = BPS_TO_TCONST(xcs->cs_clocks[i].clk >> 4, bps);
    593 			if (tc1 >= 0) {
    594 				rate1 = TCONST_TO_BPS(xcs->cs_clocks[i].clk >> 4, tc1);
    595 				err = abs(((rate1 - bps)*1000)/bps);
    596 				if (err < tol) {
    597 					tol = err;
    598 					src = i;
    599 					sf = xcs->cs_clocks[i].flags & ~ZSC_DIV;
    600 					tc0 = tc1;
    601 					rate0 = rate1;
    602 				}
    603 			}
    604 		}
    605 		if (xcs->cs_clocks[i].flags & ZSC_DIV) {
    606 			/*
    607 			 * Check out either /1, /16, /32, or /64
    608 			 * Note: for /1, you'd better be using a synchronized
    609 			 * clock!
    610 			 */
    611 			int b0 = xcs->cs_clocks[i].clk, e0 = abs(b0-bps);
    612 			int b1 = b0 >> 4, e1 = abs(b1-bps);
    613 			int b2 = b1 >> 1, e2 = abs(b2-bps);
    614 			int b3 = b2 >> 1, e3 = abs(b3-bps);
    615 
    616 			if (e0 < e1 && e0 < e2 && e0 < e3) {
    617 				err = e0;
    618 				rate1 = b0;
    619 				tc1 = ZSWR4_CLK_X1;
    620 			} else if (e0 > e1 && e1 < e2  && e1 < e3) {
    621 				err = e1;
    622 				rate1 = b1;
    623 				tc1 = ZSWR4_CLK_X16;
    624 			} else if (e0 > e2 && e1 > e2 && e2 < e3) {
    625 				err = e2;
    626 				rate1 = b2;
    627 				tc1 = ZSWR4_CLK_X32;
    628 			} else {
    629 				err = e3;
    630 				rate1 = b3;
    631 				tc1 = ZSWR4_CLK_X64;
    632 			}
    633 
    634 			err = (err * 1000)/bps;
    635 			if (err < tol) {
    636 				tol = err;
    637 				src = i;
    638 				sf = xcs->cs_clocks[i].flags & ~ZSC_BRG;
    639 				tc0 = tc1;
    640 				rate0 = rate1;
    641 			}
    642 		}
    643 	}
    644 #ifdef ZSMACDEBUG
    645 	zsprintf("Checking for rate %d. Found source #%d.\n",bps, src);
    646 #endif
    647 	if (src == -1)
    648 		return (EINVAL); /* no can do */
    649 
    650 	/*
    651 	 * The M.I. layer likes to keep cs_brg_clk current, even though
    652 	 * we are the only ones who should be touching the BRG's rate.
    653 	 *
    654 	 * Note: we are assuming that any ZSC_EXTERN signal source comes in
    655 	 * on the RTxC pin. Correct for the mac68k obio zsc.
    656 	 */
    657 	if (sf & ZSC_EXTERN)
    658 		cs->cs_brg_clk = xcs->cs_clocks[i].clk >> 4;
    659 	else
    660 		cs->cs_brg_clk = ZS_STD_BRG;
    661 
    662 	/*
    663 	 * Now we have a source, so set it up.
    664 	 */
    665 	s = splzs();
    666 	xcs->cs_psource = src;
    667 	xcs->cs_pclk_flag = sf;
    668 	bps = rate0;
    669 	if (sf & ZSC_BRG) {
    670 		cs->cs_preg[4] = ZSWR4_CLK_X16;
    671 		cs->cs_preg[11]= ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD;
    672 		if (sf & ZSC_PCLK) {
    673 			cs->cs_preg[14] = ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK;
    674 		} else {
    675 			cs->cs_preg[14] = ZSWR14_BAUD_ENA;
    676 		}
    677 		tc = tc0;
    678 	} else {
    679 		cs->cs_preg[4] = tc0;
    680 		if (sf & ZSC_RTXDIV) {
    681 			cs->cs_preg[11] = ZSWR11_RXCLK_RTXC | ZSWR11_TXCLK_RTXC;
    682 		} else {
    683 			cs->cs_preg[11] = ZSWR11_RXCLK_TRXC | ZSWR11_TXCLK_TRXC;
    684 		}
    685 		cs->cs_preg[14]= 0;
    686 		tc = 0xffff;
    687 	}
    688 	/* Set the BAUD rate divisor. */
    689 	cs->cs_preg[12] = tc;
    690 	cs->cs_preg[13] = tc >> 8;
    691 	splx(s);
    692 
    693 #ifdef ZSMACDEBUG
    694 	zsprintf("Rate is %7d, tc is %7d, source no. %2d, flags %4x\n", \
    695 	    bps, tc, src, sf);
    696 	zsprintf("Registers are: 4 %x, 11 %x, 14 %x\n\n",
    697 		cs->cs_preg[4], cs->cs_preg[11], cs->cs_preg[14]);
    698 #endif
    699 
    700 	cs->cs_preg[5] |= ZSWR5_RTS;	/* Make sure the drivers are on! */
    701 
    702 	/* Caller will stuff the pending registers. */
    703 	return (0);
    704 }
    705 
    706 int
    707 zs_set_modes(cs, cflag)
    708 	struct zs_chanstate *cs;
    709 	int cflag;	/* bits per second */
    710 {
    711 	struct xzs_chanstate *xcs = (void*)cs;
    712 	int s;
    713 
    714 	/*
    715 	 * Make sure we don't enable hfc on a signal line we're ignoring.
    716 	 * As we enable CTS interrupts only if we have CRTSCTS or CDTRCTS,
    717 	 * this code also effectivly turns off ZSWR15_CTS_IE.
    718 	 *
    719 	 * Also, disable DCD interrupts if we've been told to ignore
    720 	 * the DCD pin. Happens on mac68k because the input line for
    721 	 * DCD can also be used as a clock input.  (Just set CLOCAL.)
    722 	 *
    723 	 * If someone tries to turn an invalid flow mode on, Just Say No
    724 	 * (Suggested by gwr)
    725 	 */
    726 	if ((cflag & CDTRCTS) && (cflag & (CRTSCTS | MDMBUF)))
    727 		return (EINVAL);
    728 	if (xcs->cs_hwflags & ZS_HWFLAG_NO_DCD) {
    729 		if (cflag & MDMBUF)
    730 			return (EINVAL);
    731 		cflag |= CLOCAL;
    732 	}
    733 	if ((xcs->cs_hwflags & ZS_HWFLAG_NO_CTS) && (cflag & (CRTSCTS | CDTRCTS)))
    734 		return (EINVAL);
    735 
    736 	/*
    737 	 * Output hardware flow control on the chip is horrendous:
    738 	 * if carrier detect drops, the receiver is disabled, and if
    739 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    740 	 * Therefore, NEVER set the HFC bit, and instead use the
    741 	 * status interrupt to detect CTS changes.
    742 	 */
    743 	s = splzs();
    744 	if ((cflag & (CLOCAL | MDMBUF)) != 0)
    745 		cs->cs_rr0_dcd = 0;
    746 	else
    747 		cs->cs_rr0_dcd = ZSRR0_DCD;
    748 	/*
    749 	 * The mac hardware only has one output, DTR (HSKo in Mac
    750 	 * parlance). In HFC mode, we use it for the functions
    751 	 * typically served by RTS and DTR on other ports, so we
    752 	 * have to fake the upper layer out some.
    753 	 *
    754 	 * CRTSCTS we use CTS as an input which tells us when to shut up.
    755 	 * We make no effort to shut up the other side of the connection.
    756 	 * DTR is used to hang up the modem.
    757 	 *
    758 	 * In CDTRCTS, we use CTS to tell us to stop, but we use DTR to
    759 	 * shut up the other side.
    760 	 */
    761 	if ((cflag & CRTSCTS) != 0) {
    762 		cs->cs_wr5_dtr = ZSWR5_DTR;
    763 		cs->cs_wr5_rts = 0;
    764 		cs->cs_rr0_cts = ZSRR0_CTS;
    765 	} else if ((cflag & CDTRCTS) != 0) {
    766 		cs->cs_wr5_dtr = 0;
    767 		cs->cs_wr5_rts = ZSWR5_DTR;
    768 		cs->cs_rr0_cts = ZSRR0_CTS;
    769 	} else if ((cflag & MDMBUF) != 0) {
    770 		cs->cs_wr5_dtr = 0;
    771 		cs->cs_wr5_rts = ZSWR5_DTR;
    772 		cs->cs_rr0_cts = ZSRR0_DCD;
    773 	} else {
    774 		cs->cs_wr5_dtr = ZSWR5_DTR;
    775 		cs->cs_wr5_rts = 0;
    776 		cs->cs_rr0_cts = 0;
    777 	}
    778 	splx(s);
    779 
    780 	/* Caller will stuff the pending registers. */
    781 	return (0);
    782 }
    783 
    784 
    785 /*
    786  * Read or write the chip with suitable delays.
    787  * MacII hardware has the delay built in.
    788  * No need for extra delay. :-) However, some clock-chirped
    789  * macs, or zsc's on serial add-on boards might need it.
    790  */
    791 #define	ZS_DELAY()
    792 
    793 u_char
    794 zs_read_reg(cs, reg)
    795 	struct zs_chanstate *cs;
    796 	u_char reg;
    797 {
    798 	u_char val;
    799 
    800 	*cs->cs_reg_csr = reg;
    801 	ZS_DELAY();
    802 	val = *cs->cs_reg_csr;
    803 	ZS_DELAY();
    804 	return val;
    805 }
    806 
    807 void
    808 zs_write_reg(cs, reg, val)
    809 	struct zs_chanstate *cs;
    810 	u_char reg, val;
    811 {
    812 	*cs->cs_reg_csr = reg;
    813 	ZS_DELAY();
    814 	*cs->cs_reg_csr = val;
    815 	ZS_DELAY();
    816 }
    817 
    818 u_char zs_read_csr(cs)
    819 	struct zs_chanstate *cs;
    820 {
    821 	u_char val;
    822 
    823 	val = *cs->cs_reg_csr;
    824 	ZS_DELAY();
    825 	/* make up for the fact CTS is wired backwards */
    826 	val ^= ZSRR0_CTS;
    827 	return val;
    828 }
    829 
    830 void  zs_write_csr(cs, val)
    831 	struct zs_chanstate *cs;
    832 	u_char val;
    833 {
    834 	/* Note, the csr does not write CTS... */
    835 	*cs->cs_reg_csr = val;
    836 	ZS_DELAY();
    837 }
    838 
    839 u_char zs_read_data(cs)
    840 	struct zs_chanstate *cs;
    841 {
    842 	u_char val;
    843 
    844 	val = *cs->cs_reg_data;
    845 	ZS_DELAY();
    846 	return val;
    847 }
    848 
    849 void  zs_write_data(cs, val)
    850 	struct zs_chanstate *cs;
    851 	u_char val;
    852 {
    853 	*cs->cs_reg_data = val;
    854 	ZS_DELAY();
    855 }
    856 
    857 /****************************************************************
    858  * Console support functions (mac68k specific!)
    859  * Note: this code is allowed to know about the layout of
    860  * the chip registers, and uses that to keep things simple.
    861  * XXX - I think I like the mvme167 code better. -gwr
    862  * XXX - Well :-P  :-)  -wrs
    863  ****************************************************************/
    864 
    865 #define zscnpollc	nullcnpollc
    866 cons_decl(zs);
    867 
    868 static void	zscnsetup __P((void));
    869 extern int	zsopen __P(( dev_t dev, int flags, int mode, struct proc *p));
    870 
    871 /*
    872  * Console functions.
    873  */
    874 
    875 /*
    876  * This code modled after the zs_setparam routine in zskgdb
    877  * It sets the console unit to a known state so we can output
    878  * correctly.
    879  */
    880 static void
    881 zscnsetup()
    882 {
    883 	struct xzs_chanstate xcs;
    884 	struct zs_chanstate *cs;
    885 	struct zschan *zc;
    886 	int    tconst, s;
    887 
    888 	/* Setup temporary chanstate. */
    889 	bzero((caddr_t)&xcs, sizeof(xcs));
    890 	cs = &xcs.xzs_cs;
    891 	zc = zs_conschan;
    892 	cs->cs_reg_csr  = &zc->zc_csr;
    893 	cs->cs_reg_data = &zc->zc_data;
    894 	cs->cs_channel = zs_consunit;
    895 	cs->cs_brg_clk = ZS_STD_BRG;
    896 
    897 	bcopy(zs_init_reg, cs->cs_preg, 16);
    898 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    899 	cs->cs_preg[15] = ZSWR15_BREAK_IE;
    900 	tconst = BPS_TO_TCONST(cs->cs_brg_clk,
    901 		zs_defspeed[0][zs_consunit]);
    902 	cs->cs_preg[12] = tconst;
    903 	cs->cs_preg[13] = tconst >> 8;
    904 	/* can't use zs_set_speed as we haven't set up the
    905 	 * signal sources, and it's not worth it for now
    906 	 */
    907 
    908 	/*
    909 	 * As zs_loadchannelregs doesn't touch reg 9 (interupt control),
    910 	 * we won't accidentally turn on interupts below
    911 	 */
    912 	s = splhigh();
    913 	zs_loadchannelregs(cs);
    914 	splx(s);
    915 }
    916 
    917 /*
    918  * zscnprobe is the routine which gets called as the kernel is trying to
    919  * figure out where the console should be. Each io driver which might
    920  * be the console (as defined in mac68k/conf.c) gets probed. The probe
    921  * fills in the consdev structure. Important parts are the device #,
    922  * and the console priority. Values are CN_DEAD (don't touch me),
    923  * CN_NORMAL (I'm here, but elsewhere might be better), CN_INTERNAL
    924  * (the video, better than CN_NORMAL), and CN_REMOTE (pick me!)
    925  *
    926  * As the mac's a bit different, we do extra work here. We mainly check
    927  * to see if we have serial echo going on. Also chould check for default
    928  * speeds.
    929  */
    930 void
    931 zscnprobe(struct consdev * cp)
    932 {
    933 	extern u_long   IOBase;
    934 	int     maj, unit, i;
    935 
    936 	for (maj = 0; maj < nchrdev; maj++) {
    937 		if (cdevsw[maj].d_open == zsopen) {
    938 			break;
    939 		}
    940 	}
    941 	if (maj != nchrdev) {
    942 		cp->cn_pri = CN_NORMAL;		 /* Lower than CN_INTERNAL */
    943 		if (mac68k_machine.serial_console != 0) {
    944 			cp->cn_pri = CN_REMOTE;	 /* Higher than CN_INTERNAL */
    945 			mac68k_machine.serial_boot_echo =0;
    946 		}
    947 
    948 		unit = (mac68k_machine.serial_console == 1) ? 0 : 1;
    949 		zs_consunit = unit;
    950 		zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
    951 
    952 		mac68k_zsdev = cp->cn_dev = makedev(maj, unit);
    953 	}
    954 	if (mac68k_machine.serial_boot_echo) {
    955 		/*
    956 		 * at this point, we know that we don't have a serial
    957 		 * console, but are doing echo
    958 		 */
    959 		zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
    960 		zs_consunit = 1;
    961 		zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
    962 	}
    963 
    964 	if ((i = mac68k_machine.modem_d_speed) > 0) {
    965 		if (zs_cn_check_speed(i))
    966 			zs_defspeed[0][0] = i;
    967 	}
    968 	if ((i = mac68k_machine.print_d_speed) > 0) {
    969 		if (zs_cn_check_speed(i))
    970 			zs_defspeed[0][1] = i;
    971 	}
    972 	mac68k_set_io_offsets(IOBase);
    973 	zs_init();
    974 	/*
    975 	 * zsinit will set up the addresses of the scc. It will also, if
    976 	 * zs_conschan != 0, calculate the new address of the conschan for
    977 	 * unit zs_consunit. So if we are (or think we are) going to use the
    978 	 * chip for console I/O, we just set up the internal addresses for it.
    979 	 *
    980 	 * Now turn off interrupts for the chip. Note: using sccA to get at
    981 	 * the chip is the only vestage of the NetBSD 1.0 ser driver. :-)
    982 	 */
    983 	unit = sccA[2];			/* reset reg. access */
    984 	unit = sccA[0];
    985 	sccA[2] = 9; sccA[2] = 0;	/* write 0 to reg. 9, clearing MIE */
    986 	sccA[2] = ZSWR0_CLR_INTR; unit = sccA[2]; /* reset any pending ints. */
    987 	sccA[0] = ZSWR0_CLR_INTR; unit = sccA[0];
    988 
    989 	if (mac68k_machine.serial_boot_echo)
    990 		zscnsetup();
    991 	return;
    992 }
    993 
    994 void
    995 zscninit(struct consdev * cp)
    996 {
    997 
    998 	zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
    999 	/*
   1000 	 * zsinit will set up the addresses of the scc. It will also, if
   1001 	 * zs_conschan != 0, calculate the new address of the conschan for
   1002 	 * unit zs_consunit. So zs_init implicitly sets zs_conschan to the right
   1003 	 * number. :-)
   1004 	 */
   1005 	zscnsetup();
   1006 	printf("\nNetBSD/mac68k console\n");
   1007 }
   1008 
   1009 
   1010 /*
   1011  * Polled input char.
   1012  */
   1013 int
   1014 zs_getc(arg)
   1015 	void *arg;
   1016 {
   1017 	volatile struct zschan *zc = arg;
   1018 	int s, c, rr0;
   1019 
   1020 	s = splhigh();
   1021 	/* Wait for a character to arrive. */
   1022 	do {
   1023 		rr0 = zc->zc_csr;
   1024 		ZS_DELAY();
   1025 	} while ((rr0 & ZSRR0_RX_READY) == 0);
   1026 
   1027 	c = zc->zc_data;
   1028 	ZS_DELAY();
   1029 	splx(s);
   1030 
   1031 	/*
   1032 	 * This is used by the kd driver to read scan codes,
   1033 	 * so don't translate '\r' ==> '\n' here...
   1034 	 */
   1035 	return (c);
   1036 }
   1037 
   1038 /*
   1039  * Polled output char.
   1040  */
   1041 void
   1042 zs_putc(arg, c)
   1043 	void *arg;
   1044 	int c;
   1045 {
   1046 	volatile struct zschan *zc = arg;
   1047 	int s, rr0;
   1048 	long wait = 0;
   1049 
   1050 	s = splhigh();
   1051 	/* Wait for transmitter to become ready. */
   1052 	do {
   1053 		rr0 = zc->zc_csr;
   1054 		ZS_DELAY();
   1055 	} while (((rr0 & ZSRR0_TX_READY) == 0) && (wait++ < 1000000));
   1056 
   1057 	if ((rr0 & ZSRR0_TX_READY) != 0) {
   1058 		zc->zc_data = c;
   1059 		ZS_DELAY();
   1060 	}
   1061 	splx(s);
   1062 }
   1063 
   1064 
   1065 /*
   1066  * Polled console input putchar.
   1067  */
   1068 int
   1069 zscngetc(dev)
   1070 	dev_t dev;
   1071 {
   1072 	struct zschan *zc = zs_conschan;
   1073 	int c;
   1074 
   1075 	c = zs_getc(zc);
   1076 	return (c);
   1077 }
   1078 
   1079 /*
   1080  * Polled console output putchar.
   1081  */
   1082 void
   1083 zscnputc(dev, c)
   1084 	dev_t dev;
   1085 	int c;
   1086 {
   1087 	struct zschan *zc = zs_conschan;
   1088 
   1089 	zs_putc(zc, c);
   1090 }
   1091 
   1092 
   1093 
   1094 /*
   1095  * Handle user request to enter kernel debugger.
   1096  */
   1097 void
   1098 zs_abort(cs)
   1099 	struct zs_chanstate *cs;
   1100 {
   1101 	volatile struct zschan *zc = zs_conschan;
   1102 	int rr0;
   1103 	long wait = 0;
   1104 
   1105 	if (zs_cons_canabort == 0)
   1106 		return;
   1107 
   1108 	/* Wait for end of break to avoid PROM abort. */
   1109 	do {
   1110 		rr0 = zc->zc_csr;
   1111 		ZS_DELAY();
   1112 	} while ((rr0 & ZSRR0_BREAK) && (wait++ < ZSABORT_DELAY));
   1113 
   1114 	if (wait > ZSABORT_DELAY) {
   1115 		zs_cons_canabort = 0;
   1116 	/* If we time out, turn off the abort ability! */
   1117 	}
   1118 
   1119 #ifdef DDB
   1120 	Debugger();
   1121 #endif
   1122 }
   1123 
   1124