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