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