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