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