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