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