Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.21
      1 /*	$NetBSD: zs.c,v 1.21 1998/06/30 18:13:21 wrstuden 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 <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/proc.h>
     59 #include <sys/device.h>
     60 #include <sys/conf.h>
     61 #include <sys/file.h>
     62 #include <sys/ioctl.h>
     63 #include <sys/tty.h>
     64 #include <sys/time.h>
     65 #include <sys/kernel.h>
     66 #include <sys/syslog.h>
     67 
     68 #include <machine/autoconf.h>
     69 #include <machine/cpu.h>
     70 #include <machine/viareg.h>
     71 
     72 #include <dev/cons.h>
     73 #include <dev/ic/z8530reg.h>
     74 #include <machine/z8530var.h>
     75 #include <mac68k/dev/zs_cons.h>
     76 
     77 /* Are these in a header file anywhere? */
     78 /* Booter flags interface */
     79 #define ZSMAC_RAW	0x01
     80 #define ZSMAC_LOCALTALK	0x02
     81 #define	ZS_STD_BRG	(57600*4)
     82 
     83 #include "zsc.h"	/* get the # of zs chips defined */
     84 
     85 /*
     86  * Some warts needed by z8530tty.c -
     87  */
     88 int zs_def_cflag = (CREAD | CS8 | HUPCL);
     89 int zs_major = 12;
     90 
     91 /*
     92  * abort detection on console will now timeout after iterating on a loop
     93  * the following # of times. Cheep hack. Also, abort detection is turned
     94  * off after a timeout (i.e. maybe there's not a terminal hooked up).
     95  */
     96 #define ZSABORT_DELAY 3000000
     97 
     98 /*
     99  * Define interrupt levels.
    100  */
    101 #define ZSHARD_PRI	4	/* Wired on the CPU board... */
    102 /*
    103  * Serial port cards with zs chips on them are actually at the
    104  * NuBus interrupt level, which is lower than 4. But blocking
    105  * level 4 interrupts will block those interrupts too, so level
    106  * 4 is fine.
    107  */
    108 
    109 /* The layout of this is hardware-dependent (padding, order). */
    110 struct zschan {
    111 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    112 	u_char		zc_xxx0;
    113 	u_char		zc_xxx1;	/* part of the other channel lives here! */
    114 	u_char		zc_xxx2;	/* Yea Apple! */
    115 	volatile u_char	zc_data;	/* data */
    116 	u_char		zc_xxx3;
    117 	u_char		zc_xxx4;
    118 	u_char		zc_xxx5;
    119 };
    120 
    121 /* Saved PROM mappings */
    122 static char *zsaddr[NZSC];	/* See zs_init() */
    123 /* Flags from cninit() */
    124 static int zs_hwflags[NZSC][2];
    125 /* Default speed for each channel */
    126 static int zs_defspeed[NZSC][2] = {
    127 	{ 9600, 	/* tty00 */
    128 	  9600 },	/* tty01 */
    129 };
    130 /* console stuff */
    131 void	*zs_conschan = 0;
    132 int	zs_consunit;
    133 #ifdef	ZS_CONSOLE_ABORT
    134 int	zs_cons_canabort = 1;
    135 #else
    136 int	zs_cons_canabort = 0;
    137 #endif /* ZS_CONSOLE_ABORT*/
    138 /* device to which the console is attached--if serial. */
    139 dev_t	mac68k_zsdev;
    140 /* Mac stuff */
    141 volatile unsigned char *sccA = 0;
    142 int	nzsc_attached = 0;	/* needed as long as we have spurious
    143 				 * interupt problems.
    144 				 */
    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 		if (channel == 0) /* Double check interupts are off */
    296 			zs_write_reg(cs, 9, 0);
    297 
    298 		bcopy(zs_init_reg, cs->cs_creg, 16);
    299 		bcopy(zs_init_reg, cs->cs_preg, 16);
    300 
    301 		/* Current BAUD rate generator clock. */
    302 		cs->cs_brg_clk = ZS_STD_BRG;	/* RTxC is 230400*16, so use 230400 */
    303 		cs->cs_defspeed = zs_defspeed[zsc_unit][channel];
    304 		cs->cs_defcflag = zs_def_cflag;
    305 
    306 		/* Make these correspond to cs_defcflag (-crtscts) */
    307 		cs->cs_rr0_dcd = ZSRR0_DCD;
    308 		cs->cs_rr0_cts = 0;
    309 		cs->cs_wr5_dtr = ZSWR5_DTR;
    310 		cs->cs_wr5_rts = 0;
    311 
    312 #ifdef __notyet__
    313 		cs->cs_slave_type = ZS_SLAVE_NONE;
    314 #endif
    315 
    316 		/* Define BAUD rate stuff. */
    317 		xcs->cs_clocks[0].clk = ZS_STD_BRG * 16;
    318 		xcs->cs_clocks[0].flags = ZSC_RTXBRG;
    319 		xcs->cs_clocks[1].flags =
    320 			ZSC_RTXBRG | ZSC_RTXDIV | ZSC_VARIABLE | ZSC_EXTERN;
    321 		xcs->cs_clocks[2].flags = ZSC_TRXDIV | ZSC_VARIABLE;
    322 		xcs->cs_clock_count = 3;
    323 		if (channel == 0) {
    324 			theflags = mac68k_machine.modem_flags;
    325 			xcs->cs_clocks[1].clk = mac68k_machine.modem_dcd_clk;
    326 			xcs->cs_clocks[2].clk = mac68k_machine.modem_cts_clk;
    327 		} else {
    328 			theflags = mac68k_machine.print_flags;
    329 			xcs->cs_clocks[1].flags = ZSC_VARIABLE;
    330 			/*
    331 			 * Yes, we aren't defining ANY clock source enables for the
    332 			 * printer's DCD clock in. The hardware won't let us
    333 			 * use it. But a clock will freak out the chip, so we
    334 			 * let you set it, telling us to bar interrupts on the line.
    335 			 */
    336 			xcs->cs_clocks[1].clk = mac68k_machine.print_dcd_clk;
    337 			xcs->cs_clocks[2].clk = mac68k_machine.print_cts_clk;
    338 		}
    339 		if (xcs->cs_clocks[1].clk)
    340 			zsc_args.hwflags |= ZS_HWFLAG_NO_DCD;
    341 		if (xcs->cs_clocks[2].clk)
    342 			zsc_args.hwflags |= ZS_HWFLAG_NO_CTS;
    343 
    344 		printf("zsc%d channel %d: d_speed %6d DCD clk %ld CTS clk %ld",
    345 				zsc_unit, channel, cs->cs_defspeed,
    346 				xcs->cs_clocks[1].clk, xcs->cs_clocks[2].clk);
    347 
    348 		/* Set defaults in our "extended" chanstate. */
    349 		xcs->cs_csource = 0;
    350 		xcs->cs_psource = 0;
    351 		xcs->cs_cclk_flag = 0;  /* Nothing fancy by default */
    352 		xcs->cs_pclk_flag = 0;
    353 
    354 		if (theflags & ZSMAC_RAW) {
    355 			zsc_args.hwflags |= ZS_HWFLAG_RAW;
    356 			printf(" (raw defaults)");
    357 		}
    358 
    359 		/*
    360 		 * XXX - This might be better done with a "stub" driver
    361 		 * (to replace zstty) that ignores LocalTalk for now.
    362 		 */
    363 		if (theflags & ZSMAC_LOCALTALK) {
    364 			printf(" shielding from LocalTalk");
    365 			cs->cs_defspeed = 1;
    366 			cs->cs_creg[ZSRR_BAUDLO] = cs->cs_preg[ZSRR_BAUDLO] = 0xff;
    367 			cs->cs_creg[ZSRR_BAUDHI] = cs->cs_preg[ZSRR_BAUDHI] = 0xff;
    368 			zs_write_reg(cs, ZSRR_BAUDLO, 0xff);
    369 			zs_write_reg(cs, ZSRR_BAUDHI, 0xff);
    370 			/*
    371 			 * If we might have LocalTalk, then make sure we have the
    372 			 * Baud rate low-enough to not do any damage.
    373 			 */
    374 		}
    375 
    376 		/*
    377 		 * We used to disable chip interrupts here, but we now
    378 		 * do that in zscnprobe, just in case MacOS left the chip on.
    379 		 */
    380 
    381 		xcs->cs_chip = chip;
    382 
    383 		/* Stash away a copy of the final H/W flags. */
    384 		xcs->cs_hwflags = zsc_args.hwflags;
    385 
    386 		printf("\n");
    387 
    388 		/*
    389 		 * Look for a child driver for this channel.
    390 		 * The child attach will setup the hardware.
    391 		 */
    392 		if (!config_found(self, (void *)&zsc_args, zsc_print)) {
    393 			/* No sub-driver.  Just reset it. */
    394 			u_char reset = (channel == 0) ?
    395 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    396 			s = splzs();
    397 			zs_write_reg(cs,  9, reset);
    398 			splx(s);
    399 		}
    400 	}
    401 
    402 	/* XXX - Now safe to install interrupt handlers. */
    403 
    404 	/*
    405 	 * Set the master interrupt enable and interrupt vector.
    406 	 * (common to both channels, do it on A)
    407 	 */
    408 	cs = zsc->zsc_cs[0];
    409 	s = splzs();
    410 	/* interrupt vector */
    411 	zs_write_reg(cs, 2, zs_init_reg[2]);
    412 	/* master interrupt control (enable) */
    413 	zs_write_reg(cs, 9, zs_init_reg[9]);
    414 	nzsc_attached++;
    415 	splx(s);
    416 }
    417 
    418 static int
    419 zsc_print(aux, name)
    420 	void *aux;
    421 	const char *name;
    422 {
    423 	struct zsc_attach_args *args = aux;
    424 
    425 	if (name != NULL)
    426 		printf("%s: ", name);
    427 
    428 	if (args->channel != -1)
    429 		printf(" channel %d", args->channel);
    430 
    431 	return UNCONF;
    432 }
    433 
    434 int
    435 zsmdioctl(cs, cmd, data)
    436 	struct zs_chanstate *cs;
    437 	u_long cmd;
    438 	caddr_t data;
    439 {
    440 	switch (cmd) {
    441 	default:
    442 		return (-1);
    443 	}
    444 	return (0);
    445 }
    446 
    447 void
    448 zsmd_setclock(cs)
    449 	struct zs_chanstate *cs;
    450 {
    451 	struct xzs_chanstate *xcs = (void *)cs;
    452 
    453 	if (cs->cs_channel != 0)
    454 		return;
    455 
    456 	/*
    457 	 * If the new clock has the external bit set, then select the
    458 	 * external source.
    459 	 */
    460 	via_set_modem((xcs->cs_pclk_flag & ZSC_EXTERN) ? 1 : 0);
    461 }
    462 
    463 static int zssoftpending;
    464 
    465 /*
    466  * Our ZS chips all share a common, autovectored interrupt,
    467  * so we have to look at all of them on each interrupt.
    468  */
    469 int
    470 zshard(arg)
    471 	void *arg;
    472 {
    473 	struct zsc_softc *zsc;
    474 	int unit, rval;
    475 
    476 	rval = 0;
    477 	for (unit = 0; unit < nzsc_attached; unit++) {
    478 		zsc = zsc_cd.cd_devs[unit];
    479 		if (zsc == NULL)
    480 			continue;
    481 		rval |= zsc_intr_hard(zsc);
    482 		if ((zsc->zsc_cs[0]->cs_softreq) ||
    483 			(zsc->zsc_cs[1]->cs_softreq))
    484 		{
    485 			/* zsc_req_softint(zsc); */
    486 			/* We are at splzs here, so no need to lock. */
    487 			if (zssoftpending == 0) {
    488 				zssoftpending = 1;
    489 				setsoftserial();
    490 			}
    491 		}
    492 	}
    493 	return (rval);
    494 }
    495 
    496 /*
    497  * Similar scheme as for zshard (look at all of them)
    498  */
    499 int
    500 zssoft(arg)
    501 	void *arg;
    502 {
    503 	struct zsc_softc *zsc;
    504 	int unit;
    505 
    506 	/* This is not the only ISR on this IPL. */
    507 	if (zssoftpending == 0)
    508 		return (0);
    509 
    510 	/*
    511 	 * The soft intr. bit will be set by zshard only if
    512 	 * the variable zssoftpending is zero.
    513 	 */
    514 	zssoftpending = 0;
    515 
    516 	for (unit = 0; unit < zsc_cd.cd_ndevs; ++unit) {
    517 		zsc = zsc_cd.cd_devs[unit];
    518 		if (zsc == NULL)
    519 			continue;
    520 		(void) zsc_intr_soft(zsc);
    521 	}
    522 	return (1);
    523 }
    524 
    525 
    526 #ifndef ZS_TOLERANCE
    527 #define ZS_TOLERANCE 51
    528 /* 5% in tenths of a %, plus 1 so that exactly 5% will be ok. */
    529 #endif
    530 
    531 /*
    532  * check out a rate for acceptability from the internal clock
    533  * source. Used in console config to validate a requested
    534  * default speed. Placed here so that all the speed checking code is
    535  * in one place.
    536  *
    537  * != 0 means ok.
    538  */
    539 int
    540 zs_cn_check_speed(bps)
    541 	int bps;	/* target rate */
    542 {
    543 	int tc, rate;
    544 
    545 	tc = BPS_TO_TCONST(ZS_STD_BRG, bps);
    546 	if (tc < 0)
    547 		return 0;
    548 	rate = TCONST_TO_BPS(ZS_STD_BRG, tc);
    549 	if (ZS_TOLERANCE > abs(((rate - bps)*1000)/bps))
    550 		return 1;
    551 	else
    552 		return 0;
    553 }
    554 
    555 /*
    556  * Search through the signal sources in the channel, and
    557  * pick the best one for the baud rate requested. Return
    558  * a -1 if not achievable in tolerance. Otherwise return 0
    559  * and fill in the values.
    560  *
    561  * This routine draws inspiration from the Atari port's zs.c
    562  * driver in NetBSD 1.1 which did the same type of source switching.
    563  * Tolerance code inspired by comspeed routine in isa/com.c.
    564  *
    565  * By Bill Studenmund, 1996-05-12
    566  */
    567 int
    568 zs_set_speed(cs, bps)
    569 	struct zs_chanstate *cs;
    570 	int bps;	/* bits per second */
    571 {
    572 	struct xzs_chanstate *xcs = (void *) cs;
    573 	int i, tc, tc0 = 0, tc1, s, sf = 0;
    574 	int src, rate0, rate1, err, tol;
    575 
    576 	if (bps == 0)
    577 		return (0);
    578 
    579 	src = -1;		/* no valid source yet */
    580 	tol = ZS_TOLERANCE;
    581 
    582 	/*
    583 	 * Step through all the sources and see which one matches
    584 	 * the best. A source has to match BETTER than tol to be chosen.
    585 	 * Thus if two sources give the same error, the first one will be
    586 	 * chosen. Also, allow for the possability that one source might run
    587 	 * both the BRG and the direct divider (i.e. RTxC).
    588 	 */
    589 	for (i=0; i < xcs->cs_clock_count; i++) {
    590 		if (xcs->cs_clocks[i].clk <= 0)
    591 			continue;	/* skip non-existant or bad clocks */
    592 		if (xcs->cs_clocks[i].flags & ZSC_BRG) {
    593 			/* check out BRG at /16 */
    594 			tc1 = BPS_TO_TCONST(xcs->cs_clocks[i].clk >> 4, bps);
    595 			if (tc1 >= 0) {
    596 				rate1 = TCONST_TO_BPS(xcs->cs_clocks[i].clk >> 4, tc1);
    597 				err = abs(((rate1 - bps)*1000)/bps);
    598 				if (err < tol) {
    599 					tol = err;
    600 					src = i;
    601 					sf = xcs->cs_clocks[i].flags & ~ZSC_DIV;
    602 					tc0 = tc1;
    603 					rate0 = rate1;
    604 				}
    605 			}
    606 		}
    607 		if (xcs->cs_clocks[i].flags & ZSC_DIV) {
    608 			/*
    609 			 * Check out either /1, /16, /32, or /64
    610 			 * Note: for /1, you'd better be using a synchronized
    611 			 * clock!
    612 			 */
    613 			int b0 = xcs->cs_clocks[i].clk, e0 = abs(b0-bps);
    614 			int b1 = b0 >> 4, e1 = abs(b1-bps);
    615 			int b2 = b1 >> 1, e2 = abs(b2-bps);
    616 			int b3 = b2 >> 1, e3 = abs(b3-bps);
    617 
    618 			if (e0 < e1 && e0 < e2 && e0 < e3) {
    619 				err = e0;
    620 				rate1 = b0;
    621 				tc1 = ZSWR4_CLK_X1;
    622 			} else if (e0 > e1 && e1 < e2  && e1 < e3) {
    623 				err = e1;
    624 				rate1 = b1;
    625 				tc1 = ZSWR4_CLK_X16;
    626 			} else if (e0 > e2 && e1 > e2 && e2 < e3) {
    627 				err = e2;
    628 				rate1 = b2;
    629 				tc1 = ZSWR4_CLK_X32;
    630 			} else {
    631 				err = e3;
    632 				rate1 = b3;
    633 				tc1 = ZSWR4_CLK_X64;
    634 			}
    635 
    636 			err = (err * 1000)/bps;
    637 			if (err < tol) {
    638 				tol = err;
    639 				src = i;
    640 				sf = xcs->cs_clocks[i].flags & ~ZSC_BRG;
    641 				tc0 = tc1;
    642 				rate0 = rate1;
    643 			}
    644 		}
    645 	}
    646 #ifdef ZSMACDEBUG
    647 	zsprintf("Checking for rate %d. Found source #%d.\n",bps, src);
    648 #endif
    649 	if (src == -1)
    650 		return (EINVAL); /* no can do */
    651 
    652 	/*
    653 	 * The M.I. layer likes to keep cs_brg_clk current, even though
    654 	 * we are the only ones who should be touching the BRG's rate.
    655 	 *
    656 	 * Note: we are assuming that any ZSC_EXTERN signal source comes in
    657 	 * on the RTxC pin. Correct for the mac68k obio zsc.
    658 	 */
    659 	if (sf & ZSC_EXTERN)
    660 		cs->cs_brg_clk = xcs->cs_clocks[i].clk >> 4;
    661 	else
    662 		cs->cs_brg_clk = ZS_STD_BRG;
    663 
    664 	/*
    665 	 * Now we have a source, so set it up.
    666 	 */
    667 	s = splzs();
    668 	xcs->cs_psource = src;
    669 	xcs->cs_pclk_flag = sf;
    670 	bps = rate0;
    671 	if (sf & ZSC_BRG) {
    672 		cs->cs_preg[4] = ZSWR4_CLK_X16;
    673 		cs->cs_preg[11]= ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD;
    674 		if (sf & ZSC_PCLK) {
    675 			cs->cs_preg[14] = ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK;
    676 		} else {
    677 			cs->cs_preg[14] = ZSWR14_BAUD_ENA;
    678 		}
    679 		tc = tc0;
    680 	} else {
    681 		cs->cs_preg[4] = tc0;
    682 		if (sf & ZSC_RTXDIV) {
    683 			cs->cs_preg[11] = ZSWR11_RXCLK_RTXC | ZSWR11_TXCLK_RTXC;
    684 		} else {
    685 			cs->cs_preg[11] = ZSWR11_RXCLK_TRXC | ZSWR11_TXCLK_TRXC;
    686 		}
    687 		cs->cs_preg[14]= 0;
    688 		tc = 0xffff;
    689 	}
    690 	/* Set the BAUD rate divisor. */
    691 	cs->cs_preg[12] = tc;
    692 	cs->cs_preg[13] = tc >> 8;
    693 	splx(s);
    694 
    695 #ifdef ZSMACDEBUG
    696 	zsprintf("Rate is %7d, tc is %7d, source no. %2d, flags %4x\n", \
    697 	    bps, tc, src, sf);
    698 	zsprintf("Registers are: 4 %x, 11 %x, 14 %x\n\n",
    699 		cs->cs_preg[4], cs->cs_preg[11], cs->cs_preg[14]);
    700 #endif
    701 
    702 	cs->cs_preg[5] |= ZSWR5_RTS;	/* Make sure the drivers are on! */
    703 
    704 	/* Caller will stuff the pending registers. */
    705 	return (0);
    706 }
    707 
    708 int
    709 zs_set_modes(cs, cflag)
    710 	struct zs_chanstate *cs;
    711 	int cflag;	/* bits per second */
    712 {
    713 	struct xzs_chanstate *xcs = (void*)cs;
    714 	int s;
    715 
    716 	/*
    717 	 * Make sure we don't enable hfc on a signal line we're ignoring.
    718 	 * As we enable CTS interrupts only if we have CRTSCTS or CDTRCTS,
    719 	 * this code also effectivly turns off ZSWR15_CTS_IE.
    720 	 *
    721 	 * Also, disable DCD interrupts if we've been told to ignore
    722 	 * the DCD pin. Happens on mac68k because the input line for
    723 	 * DCD can also be used as a clock input.  (Just set CLOCAL.)
    724 	 *
    725 	 * If someone tries to turn an invalid flow mode on, Just Say No
    726 	 * (Suggested by gwr)
    727 	 */
    728 	if ((cflag & CDTRCTS) && (cflag & (CRTSCTS | MDMBUF)))
    729 		return (EINVAL);
    730 	if (xcs->cs_hwflags & ZS_HWFLAG_NO_DCD) {
    731 		if (cflag & MDMBUF)
    732 			return (EINVAL);
    733 		cflag |= CLOCAL;
    734 	}
    735 	if ((xcs->cs_hwflags & ZS_HWFLAG_NO_CTS) && (cflag & (CRTSCTS | CDTRCTS)))
    736 		return (EINVAL);
    737 
    738 	/*
    739 	 * Output hardware flow control on the chip is horrendous:
    740 	 * if carrier detect drops, the receiver is disabled, and if
    741 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    742 	 * Therefore, NEVER set the HFC bit, and instead use the
    743 	 * status interrupt to detect CTS changes.
    744 	 */
    745 	s = splzs();
    746 	if ((cflag & (CLOCAL | MDMBUF)) != 0)
    747 		cs->cs_rr0_dcd = 0;
    748 	else
    749 		cs->cs_rr0_dcd = ZSRR0_DCD;
    750 	/*
    751 	 * The mac hardware only has one output, DTR (HSKo in Mac
    752 	 * parlance). In HFC mode, we use it for the functions
    753 	 * typically served by RTS and DTR on other ports, so we
    754 	 * have to fake the upper layer out some.
    755 	 *
    756 	 * CRTSCTS we use CTS as an input which tells us when to shut up.
    757 	 * We make no effort to shut up the other side of the connection.
    758 	 * DTR is used to hang up the modem.
    759 	 *
    760 	 * In CDTRCTS, we use CTS to tell us to stop, but we use DTR to
    761 	 * shut up the other side.
    762 	 */
    763 	if ((cflag & CRTSCTS) != 0) {
    764 		cs->cs_wr5_dtr = ZSWR5_DTR;
    765 		cs->cs_wr5_rts = 0;
    766 		cs->cs_rr0_cts = ZSRR0_CTS;
    767 	} else if ((cflag & CDTRCTS) != 0) {
    768 		cs->cs_wr5_dtr = 0;
    769 		cs->cs_wr5_rts = ZSWR5_DTR;
    770 		cs->cs_rr0_cts = ZSRR0_CTS;
    771 	} else if ((cflag & MDMBUF) != 0) {
    772 		cs->cs_wr5_dtr = 0;
    773 		cs->cs_wr5_rts = ZSWR5_DTR;
    774 		cs->cs_rr0_cts = ZSRR0_DCD;
    775 	} else {
    776 		cs->cs_wr5_dtr = ZSWR5_DTR;
    777 		cs->cs_wr5_rts = 0;
    778 		cs->cs_rr0_cts = 0;
    779 	}
    780 	splx(s);
    781 
    782 	/* Caller will stuff the pending registers. */
    783 	return (0);
    784 }
    785 
    786 
    787 /*
    788  * Read or write the chip with suitable delays.
    789  * MacII hardware has the delay built in.
    790  * No need for extra delay. :-) However, some clock-chirped
    791  * macs, or zsc's on serial add-on boards might need it.
    792  */
    793 #define	ZS_DELAY()
    794 
    795 u_char
    796 zs_read_reg(cs, reg)
    797 	struct zs_chanstate *cs;
    798 	u_char reg;
    799 {
    800 	u_char val;
    801 
    802 	*cs->cs_reg_csr = reg;
    803 	ZS_DELAY();
    804 	val = *cs->cs_reg_csr;
    805 	ZS_DELAY();
    806 	return val;
    807 }
    808 
    809 void
    810 zs_write_reg(cs, reg, val)
    811 	struct zs_chanstate *cs;
    812 	u_char reg, val;
    813 {
    814 	*cs->cs_reg_csr = reg;
    815 	ZS_DELAY();
    816 	*cs->cs_reg_csr = val;
    817 	ZS_DELAY();
    818 }
    819 
    820 u_char zs_read_csr(cs)
    821 	struct zs_chanstate *cs;
    822 {
    823 	u_char val;
    824 
    825 	val = *cs->cs_reg_csr;
    826 	ZS_DELAY();
    827 	/* make up for the fact CTS is wired backwards */
    828 	val ^= ZSRR0_CTS;
    829 	return val;
    830 }
    831 
    832 void  zs_write_csr(cs, val)
    833 	struct zs_chanstate *cs;
    834 	u_char val;
    835 {
    836 	/* Note, the csr does not write CTS... */
    837 	*cs->cs_reg_csr = val;
    838 	ZS_DELAY();
    839 }
    840 
    841 u_char zs_read_data(cs)
    842 	struct zs_chanstate *cs;
    843 {
    844 	u_char val;
    845 
    846 	val = *cs->cs_reg_data;
    847 	ZS_DELAY();
    848 	return val;
    849 }
    850 
    851 void  zs_write_data(cs, val)
    852 	struct zs_chanstate *cs;
    853 	u_char val;
    854 {
    855 	*cs->cs_reg_data = val;
    856 	ZS_DELAY();
    857 }
    858 
    859 /****************************************************************
    860  * Console support functions (mac68k specific!)
    861  * Note: this code is allowed to know about the layout of
    862  * the chip registers, and uses that to keep things simple.
    863  * XXX - I think I like the mvme167 code better. -gwr
    864  * XXX - Well :-P  :-)  -wrs
    865  ****************************************************************/
    866 
    867 #define zscnpollc	nullcnpollc
    868 cons_decl(zs);
    869 
    870 static void	zscnsetup __P((void));
    871 extern int	zsopen __P(( dev_t dev, int flags, int mode, struct proc *p));
    872 
    873 /*
    874  * Console functions.
    875  */
    876 
    877 /*
    878  * This code modled after the zs_setparam routine in zskgdb
    879  * It sets the console unit to a known state so we can output
    880  * correctly.
    881  */
    882 static void
    883 zscnsetup()
    884 {
    885 	struct xzs_chanstate xcs;
    886 	struct zs_chanstate *cs;
    887 	struct zschan *zc;
    888 	int    tconst, s;
    889 
    890 	/* Setup temporary chanstate. */
    891 	bzero((caddr_t)&xcs, sizeof(xcs));
    892 	cs = &xcs.xzs_cs;
    893 	zc = zs_conschan;
    894 	cs->cs_reg_csr  = &zc->zc_csr;
    895 	cs->cs_reg_data = &zc->zc_data;
    896 	cs->cs_channel = zs_consunit;
    897 	cs->cs_brg_clk = ZS_STD_BRG;
    898 
    899 	bcopy(zs_init_reg, cs->cs_preg, 16);
    900 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    901 	cs->cs_preg[15] = ZSWR15_BREAK_IE;
    902 	tconst = BPS_TO_TCONST(cs->cs_brg_clk,
    903 		zs_defspeed[0][zs_consunit]);
    904 	cs->cs_preg[12] = tconst;
    905 	cs->cs_preg[13] = tconst >> 8;
    906 	/* can't use zs_set_speed as we haven't set up the
    907 	 * signal sources, and it's not worth it for now
    908 	 */
    909 
    910 	/*
    911 	 * As zs_loadchannelregs doesn't touch reg 9 (interupt control),
    912 	 * we won't accidentally turn on interupts below
    913 	 */
    914 	s = splhigh();
    915 	zs_loadchannelregs(cs);
    916 	splx(s);
    917 }
    918 
    919 /*
    920  * zscnprobe is the routine which gets called as the kernel is trying to
    921  * figure out where the console should be. Each io driver which might
    922  * be the console (as defined in mac68k/conf.c) gets probed. The probe
    923  * fills in the consdev structure. Important parts are the device #,
    924  * and the console priority. Values are CN_DEAD (don't touch me),
    925  * CN_NORMAL (I'm here, but elsewhere might be better), CN_INTERNAL
    926  * (the video, better than CN_NORMAL), and CN_REMOTE (pick me!)
    927  *
    928  * As the mac's a bit different, we do extra work here. We mainly check
    929  * to see if we have serial echo going on. Also chould check for default
    930  * speeds.
    931  */
    932 void
    933 zscnprobe(struct consdev * cp)
    934 {
    935 	extern u_long   IOBase;
    936 	int     maj, unit, i;
    937 
    938 	for (maj = 0; maj < nchrdev; maj++) {
    939 		if (cdevsw[maj].d_open == zsopen) {
    940 			break;
    941 		}
    942 	}
    943 	if (maj != nchrdev) {
    944 		cp->cn_pri = CN_NORMAL;		 /* Lower than CN_INTERNAL */
    945 		if (mac68k_machine.serial_console != 0) {
    946 			cp->cn_pri = CN_REMOTE;	 /* Higher than CN_INTERNAL */
    947 			mac68k_machine.serial_boot_echo =0;
    948 		}
    949 
    950 		unit = (mac68k_machine.serial_console == 1) ? 0 : 1;
    951 		zs_consunit = unit;
    952 		zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
    953 
    954 		mac68k_zsdev = cp->cn_dev = makedev(maj, unit);
    955 	}
    956 	if (mac68k_machine.serial_boot_echo) {
    957 		/*
    958 		 * at this point, we know that we don't have a serial
    959 		 * console, but are doing echo
    960 		 */
    961 		zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
    962 		zs_consunit = 1;
    963 		zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
    964 	}
    965 
    966 	if ((i = mac68k_machine.modem_d_speed) > 0) {
    967 		if (zs_cn_check_speed(i))
    968 			zs_defspeed[0][0] = i;
    969 	}
    970 	if ((i = mac68k_machine.print_d_speed) > 0) {
    971 		if (zs_cn_check_speed(i))
    972 			zs_defspeed[0][1] = i;
    973 	}
    974 	mac68k_set_io_offsets(IOBase);
    975 	zs_init();
    976 	/*
    977 	 * zsinit will set up the addresses of the scc. It will also, if
    978 	 * zs_conschan != 0, calculate the new address of the conschan for
    979 	 * unit zs_consunit. So if we are (or think we are) going to use the
    980 	 * chip for console I/O, we just set up the internal addresses for it.
    981 	 *
    982 	 * Now turn off interrupts for the chip. Note: this code piece is the
    983 	 * only vestage of the NetBSD 1.0 ser driver. :-)
    984 	 */
    985 	unit = sccA[2];			/* reset reg. access */
    986 	unit = sccA[0];
    987 	sccA[2] = 9; sccA[2] = 0;	/* write 0 to reg. 9, clearing MIE */
    988 	sccA[2] = ZSWR0_CLR_INTR; unit = sccA[2]; /* reset any pending ints. */
    989 	sccA[0] = ZSWR0_CLR_INTR; unit = sccA[0];
    990 
    991 	if (mac68k_machine.serial_boot_echo)
    992 		zscnsetup();
    993 	return;
    994 }
    995 
    996 void
    997 zscninit(struct consdev * cp)
    998 {
    999 
   1000 	zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
   1001 	/*
   1002 	 * zsinit will set up the addresses of the scc. It will also, if
   1003 	 * zs_conschan != 0, calculate the new address of the conschan for
   1004 	 * unit zs_consunit. So zs_init implicitly sets zs_conschan to the right
   1005 	 * number. :-)
   1006 	 */
   1007 	zscnsetup();
   1008 	printf("\nNetBSD/mac68k console\n");
   1009 }
   1010 
   1011 
   1012 /*
   1013  * Polled input char.
   1014  */
   1015 int
   1016 zs_getc(arg)
   1017 	void *arg;
   1018 {
   1019 	volatile struct zschan *zc = arg;
   1020 	int s, c, rr0;
   1021 
   1022 	s = splhigh();
   1023 	/* Wait for a character to arrive. */
   1024 	do {
   1025 		rr0 = zc->zc_csr;
   1026 		ZS_DELAY();
   1027 	} while ((rr0 & ZSRR0_RX_READY) == 0);
   1028 
   1029 	c = zc->zc_data;
   1030 	ZS_DELAY();
   1031 	splx(s);
   1032 
   1033 	/*
   1034 	 * This is used by the kd driver to read scan codes,
   1035 	 * so don't translate '\r' ==> '\n' here...
   1036 	 */
   1037 	return (c);
   1038 }
   1039 
   1040 /*
   1041  * Polled output char.
   1042  */
   1043 void
   1044 zs_putc(arg, c)
   1045 	void *arg;
   1046 	int c;
   1047 {
   1048 	volatile struct zschan *zc = arg;
   1049 	int s, rr0;
   1050 	long wait = 0;
   1051 
   1052 	s = splhigh();
   1053 	/* Wait for transmitter to become ready. */
   1054 	do {
   1055 		rr0 = zc->zc_csr;
   1056 		ZS_DELAY();
   1057 	} while (((rr0 & ZSRR0_TX_READY) == 0) && (wait++ < 1000000));
   1058 
   1059 	if ((rr0 & ZSRR0_TX_READY) != 0) {
   1060 		zc->zc_data = c;
   1061 		ZS_DELAY();
   1062 	}
   1063 	splx(s);
   1064 }
   1065 
   1066 
   1067 /*
   1068  * Polled console input putchar.
   1069  */
   1070 int
   1071 zscngetc(dev)
   1072 	dev_t dev;
   1073 {
   1074 	struct zschan *zc = zs_conschan;
   1075 	int c;
   1076 
   1077 	c = zs_getc(zc);
   1078 	return (c);
   1079 }
   1080 
   1081 /*
   1082  * Polled console output putchar.
   1083  */
   1084 void
   1085 zscnputc(dev, c)
   1086 	dev_t dev;
   1087 	int c;
   1088 {
   1089 	struct zschan *zc = zs_conschan;
   1090 
   1091 	zs_putc(zc, c);
   1092 }
   1093 
   1094 
   1095 
   1096 /*
   1097  * Handle user request to enter kernel debugger.
   1098  */
   1099 void
   1100 zs_abort(cs)
   1101 	struct zs_chanstate *cs;
   1102 {
   1103 	volatile struct zschan *zc = zs_conschan;
   1104 	int rr0;
   1105 	long wait = 0;
   1106 
   1107 	if (zs_cons_canabort == 0)
   1108 		return;
   1109 
   1110 	/* Wait for end of break to avoid PROM abort. */
   1111 	do {
   1112 		rr0 = zc->zc_csr;
   1113 		ZS_DELAY();
   1114 	} while ((rr0 & ZSRR0_BREAK) && (wait++ < ZSABORT_DELAY));
   1115 
   1116 	if (wait > ZSABORT_DELAY) {
   1117 		zs_cons_canabort = 0;
   1118 	/* If we time out, turn off the abort ability! */
   1119 	}
   1120 
   1121 #ifdef DDB
   1122 	Debugger();
   1123 #endif
   1124 }
   1125 
   1126