Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.4
      1 /*	$NetBSD: zs.c,v 1.4 1996/06/07 10:41:35 briggs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Gordon W. Ross
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  * 4. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed by Gordon Ross
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Zilog Z8530 Dual UART driver (machine-dependent part)
     35  *
     36  * Runs two serial lines per chip using slave drivers.
     37  * Plain tty/async lines use the zs_async slave.
     38  * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
     39  */
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/proc.h>
     44 #include <sys/device.h>
     45 #include <sys/conf.h>
     46 #include <sys/file.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/tty.h>
     49 #include <sys/time.h>
     50 #include <sys/kernel.h>
     51 #include <sys/syslog.h>
     52 
     53 #include <dev/cons.h>
     54 #include "z8530reg.h"
     55 #include <machine/z8530var.h>
     56 
     57 #include <machine/autoconf.h>
     58 #include <machine/cpu.h>
     59 
     60 /*
     61  * XXX: Hard code this to make console init easier...
     62  */
     63 #define	NZSC	1		/* XXX */
     64 
     65 /*
     66  * Define interrupt levels.
     67  */
     68 #define ZSHARD_PRI	6	/* Wired on the CPU board... */
     69 #define ZSSOFT_PRI	3	/* Want tty pri (4) but this is OK. */
     70 
     71 /* The layout of this is hardware-dependent (padding, order). */
     72 struct zschan {
     73 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
     74 	u_char		zc_xxx0;
     75 	u_char		zc_xxx1;
     76 	u_char		zc_xxx2;
     77 	volatile u_char	zc_data;	/* data */
     78 	u_char		zc_xxx3;
     79 	u_char		zc_xxx4;
     80 	u_char		zc_xxx5;
     81 };
     82 /*
     83  * The zsdevice structure is not used on the mac68k port as the
     84  * chip is wired up weird. Channel B & A are interspursed with
     85  * the data & control bytes
     86 struct zsdevice {
     87 	/! Yes, they are backwards. !/
     88 	struct	zschan zs_chan_b;
     89 	struct	zschan zs_chan_a;
     90 };
     91 */
     92 
     93 /* Saved PROM mappings */
     94 static char *zsaddr[NZSC];	/* See zs_init() */
     95 /* Flags from cninit() */
     96 static int zs_hwflags[NZSC][2];
     97 /* Default speed for each channel */
     98 static int zs_defspeed[NZSC][2] = {
     99 	{ 9600, 	/* tty00 */
    100 	  9600 },	/* tty01 */
    101 };
    102 /* console stuff */
    103 void *zs_conschan = 0;
    104 int   zs_consunit;
    105 /* device that the console is attached to--if serial. */
    106 dev_t	mac68k_zsdev;
    107 /* Mac stuff, some vestages of old mac serial driver here */
    108 volatile unsigned char *sccA = 0;
    109 
    110 static struct zschan	*zs_get_chan_addr __P((int zsc_unit, int channel));
    111 void			zs_init __P((void));
    112 
    113 static struct zschan *
    114 zs_get_chan_addr(zsc_unit, channel)
    115 	int zsc_unit, channel;
    116 {
    117 	char *addr;
    118 	struct zschan *zc;
    119 
    120 	if (zsc_unit >= NZSC)
    121 		return NULL;
    122 	addr = zsaddr[zsc_unit];
    123 	if (addr == NULL)
    124 		return NULL;
    125 	if (channel == 0) {
    126 		zc = (struct zschan *)(addr +2);
    127 		/* handle the fact the ports are intertwined. */
    128 	} else {
    129 		zc = (struct zschan *)(addr);
    130 	}
    131 	return (zc);
    132 }
    133 
    134 
    135 /* Find PROM mappings (for console support). */
    136 static int zsinited = 0; /* 0 = not, 1 = inited, not attached, 2= attached */
    137 
    138 void
    139 zs_init()
    140 {
    141 	if ((zsinited == 2)&&(zsaddr[0] != (char *) sccA))
    142 		panic("Moved zs0 address after attached!");
    143 	zsaddr[0] = (char *) sccA;
    144 	zsinited = 1;
    145 	if (zs_conschan != 0){ /* we might have moved io under the console */
    146 		zs_conschan = zs_get_chan_addr(0, zs_consunit);
    147 		/* so recalc the console port */
    148 	}
    149 }
    150 
    151 
    152 /*
    153  * Even though zsparam will set up the clock multiples, etc., we
    154  * still set them here as: 1) mice & keyboards don't use zsparam,
    155  * and 2) the console stuff uses these defaults before device
    156  * attach.
    157  */
    158 
    159 static u_char zs_init_reg[16] = {
    160 	0,	/* 0: CMD (reset, etc.) */
    161 	ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE,
    162 	0x18 + ZSHARD_PRI,	/* IVECT */
    163 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    164 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    165 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    166 	0,	/* 6: TXSYNC/SYNCLO */
    167 	0,	/* 7: RXSYNC/SYNCHI */
    168 	0,	/* 8: alias for data port */
    169 	ZSWR9_MASTER_IE,
    170 	0,	/*10: Misc. TX/RX control bits */
    171 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    172 	14,	/*12: BAUDLO (default=9600) */
    173 	0,	/*13: BAUDHI (default=9600) */
    174 	ZSWR14_BAUD_ENA,
    175 	ZSWR15_BREAK_IE | ZSWR15_DCD_IE | ZSWR15_CTS_IE,
    176 };
    177 
    178 
    179 /****************************************************************
    180  * Autoconfig
    181  ****************************************************************/
    182 
    183 /* Definition of the driver for autoconfig. */
    184 static int	zsc_match __P((struct device *, void *, void *));
    185 static void	zsc_attach __P((struct device *, struct device *, void *));
    186 static int	zsc_print __P((void *aux, char *name));
    187 
    188 struct cfattach zsc_ca = {
    189 	sizeof(struct zsc_softc), zsc_match, zsc_attach
    190 };
    191 
    192 struct cfdriver zsc_cd = {
    193 	NULL, "zsc", DV_DULL
    194 };
    195 
    196 int	zshard __P((void *));
    197 int	zssoft __P((void *));
    198 
    199 
    200 /*
    201  * Is the zs chip present?
    202  */
    203 static int
    204 zsc_match(parent, vcf, aux)
    205 	struct device *parent;
    206 	void *vcf;
    207 	void *aux;
    208 {
    209 	return 1;
    210 }
    211 
    212 static int
    213 zsc_print(aux, name)
    214 	void *aux;
    215 	char *name;
    216 {
    217 	struct zsc_attach_args *args = aux;
    218 
    219 	if (name != NULL)
    220 		printf("%s: ", name);
    221 
    222 	if (args->channel != -1)
    223 		printf(" channel %d", args->channel);
    224 
    225 	return UNCONF;
    226 }
    227 
    228 /*
    229  * Attach a found zs.
    230  *
    231  * Match slave number to zs unit number, so that misconfiguration will
    232  * not set up the keyboard as ttya, etc.
    233  */
    234 static void
    235 zsc_attach(parent, self, aux)
    236 	struct device *parent;
    237 	struct device *self;
    238 	void *aux;
    239 {
    240 	struct zsc_softc *zsc = (void *) self;
    241 	struct zsc_attach_args zsc_args;
    242 	volatile struct zschan *zc;
    243 	struct zs_chanstate *cs;
    244 	int zsc_unit, channel;
    245 	int reset, s, chip;
    246 
    247 	if (!zsinited) zs_init();
    248 	zsinited = 2;
    249 
    250 	zsc_unit = zsc->zsc_dev.dv_unit;
    251 
    252 	/* Make sure everything's inited ok. */
    253 	if (zsaddr[zsc_unit] == NULL)
    254 		panic("zs_attach: zs%d not mapped\n", zsc_unit);
    255 
    256 	/*
    257 	 * Initialize software state for each channel.
    258 	 */
    259 	for (channel = 0; channel < 2; channel++) {
    260 		cs = &zsc->zsc_cs[channel];
    261 
    262 		zc = zs_get_chan_addr(zsc_unit, channel);
    263 		cs->cs_reg_csr  = &zc->zc_csr;
    264 		cs->cs_reg_data = &zc->zc_data;
    265 
    266 		cs->cs_channel = channel;
    267 		cs->cs_private = NULL;
    268 		cs->cs_ops = &zsops_null;
    269 
    270 		/* Define BAUD rate clock for the MI code. */
    271 		cs->cs_pclk_div16 = mac68k_machine.sccClkConst*2;
    272 		cs->cs_csource = 0;
    273 		cs->cs_psource = 0;
    274 
    275 		cs->cs_defspeed = zs_defspeed[zsc_unit][channel];
    276 
    277 		bcopy(zs_init_reg, cs->cs_creg, 16);
    278 		bcopy(zs_init_reg, cs->cs_preg, 16);
    279 
    280 		/*
    281 		 * Clear the master interrupt enable.
    282 		 * The INTENA is common to both channels,
    283 		 * so just do it on the A channel.
    284 		 */
    285 		if (channel == 0) {
    286 			zs_write_reg(cs, 9, 0);
    287 
    288 			chip = 0; /* We'll turn chip checking on post 1.2 */
    289 			printf(" chip type %d \n",chip);
    290 		}
    291 		cs->cs_chip = chip;
    292 
    293 		/*
    294 		 * Look for a child driver for this channel.
    295 		 * The child attach will setup the hardware.
    296 		 */
    297 		zsc_args.channel = channel;
    298 		zsc_args.hwflags = zs_hwflags[zsc_unit][channel];
    299 		if (!config_found(self, (void *) &zsc_args, zsc_print)) {
    300 			/* No sub-driver.  Just reset it. */
    301 			reset = (channel == 0) ?
    302 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    303 			s = splzs();
    304 			zs_write_reg(cs,  9, reset);
    305 			splx(s);
    306 		}
    307 	}
    308 
    309 	/*
    310 	 * Set the master interrupt enable and interrupt vector.
    311 	 * (common to both channels, do it on A)
    312 	 */
    313 	cs = &zsc->zsc_cs[0];
    314 	s = splzs();
    315 	/* interrupt vector */
    316 	zs_write_reg(cs, 2, zs_init_reg[2]);
    317 	/* master interrupt control (enable) */
    318 	zs_write_reg(cs, 9, zs_init_reg[9]);
    319 	splx(s);
    320 }
    321 
    322 void
    323 zstty_mdattach(zsc, zst, cs, tp)
    324 	struct zsc_softc *zsc;
    325 	struct zstty_softc *zst;
    326 	struct zs_chanstate *cs;
    327 	struct tty *tp;
    328 {
    329 	int theflags;
    330 
    331 	zst->zst_resetdef = 0;
    332 	cs->cs_clock_count = 3; /* internal + externals */
    333 	cs->cs_cclk_flag = 0;  /* Not doing anything fancy by default */
    334 	cs->cs_pclk_flag = 0;
    335 	cs->cs_clocks[0].clk = mac68k_machine.sccClkConst*32;
    336 	cs->cs_clocks[0].flags = ZSC_RTXBRG; /* allowing divide by 16 will
    337 					melt the driver! */
    338 
    339 	cs->cs_clocks[1].flags = ZSC_RTXBRG | ZSC_RTXDIV | ZSC_VARIABLE | ZSC_EXTERN;
    340 	cs->cs_clocks[2].flags = ZSC_TRXDIV | ZSC_VARIABLE;
    341 	if (zst->zst_dev.dv_unit == 0) {
    342 		theflags = mac68k_machine.modem_flags;
    343 		cs->cs_clocks[1].clk = mac68k_machine.modem_dcd_clk;
    344 		cs->cs_clocks[2].clk = mac68k_machine.modem_cts_clk;
    345 	} else if (zst->zst_dev.dv_unit == 1) {
    346 		theflags = mac68k_machine.print_flags;
    347 		cs->cs_clocks[1].flags = ZSC_VARIABLE;
    348 		/*
    349 		 * Yes, we aren't defining ANY clock source enables for the
    350 		 * printer's DCD clock in. The hardware won't let us
    351 		 * use it. But a clock will freak out the chip, so we
    352 		 * let you set it, telling us to bar interrupts on the line.
    353 		 */
    354 		cs->cs_clocks[1].clk = mac68k_machine.print_dcd_clk;
    355 		cs->cs_clocks[2].clk = mac68k_machine.print_cts_clk;
    356 	}
    357 
    358 	if (cs->cs_clocks[1].clk)
    359 		zst->zst_hwflags |= ZS_HWFLAG_IGDCD;
    360 	if (cs->cs_clocks[2].clk)
    361 		zst->zst_hwflags |= ZS_HWFLAG_IGCTS;
    362 
    363 	if (theflags & ZSMAC_RAW) {
    364 		zst->zst_cflag = ZSTTY_RAW_CFLAG;
    365 		zst->zst_iflag = ZSTTY_RAW_IFLAG;
    366 		zst->zst_lflag = ZSTTY_RAW_LFLAG;
    367 		zst->zst_oflag = ZSTTY_RAW_OFLAG;
    368 		printf(" (raw defaults)");
    369 	}
    370 	if (theflags & ZSMAC_LOCALTALK) {
    371 		printf(" shielding from LocalTalk");
    372 		zst->zst_ospeed = tp->t_ospeed = 1;
    373 		zst->zst_ispeed = tp->t_ispeed = 1;
    374 		cs->cs_defspeed = 1;
    375 		cs->cs_creg[ZSRR_BAUDLO] = cs->cs_preg[ZSRR_BAUDLO] = 0xff;
    376 		cs->cs_creg[ZSRR_BAUDHI] = cs->cs_preg[ZSRR_BAUDHI] = 0xff;
    377 		zs_write_reg(cs, ZSRR_BAUDLO, 0xff);
    378 		zs_write_reg(cs, ZSRR_BAUDHI, 0xff);
    379 		/*
    380 		 * If we might have LocalTalk, then make sure we have the
    381 		 * Baud rate low-enough to not do any damage.
    382 		 */
    383 	}
    384 
    385 	/* For the mac, we have rtscts = check CTS for output control, no
    386 	 * input control. mdmbuf means check DCD for output, and use DTR
    387 	 * for input control. mdmbuf & rtscts means use CTS for output
    388 	 * control, and DTR for input control. */
    389 
    390 	zst->zst_hwimasks[1] = 0;
    391 	zst->zst_hwimasks[2] = ZSWR5_DTR;
    392 	zst->zst_hwimasks[3] = ZSWR5_DTR;
    393 }
    394 
    395 int
    396 zsmdioctl(tp, com, data, flag, p)
    397 	struct tty *tp;
    398 	u_long com;
    399 	caddr_t data;
    400 	int flag;
    401 	struct proc *p;
    402 {
    403 	return (-1);
    404 }
    405 
    406 void
    407 zsmd_setclock(cs)
    408 	struct zs_chanstate *cs;
    409 {
    410 	if (cs->cs_channel != 0)
    411 		return;
    412 	/*
    413 	 * If the new clock has the external bit set, then select the
    414 	 * external source.
    415 	 */
    416 	via_set_modem((cs->cs_pclk_flag & ZSC_EXTERN) ? 1 : 0);
    417 }
    418 
    419 int
    420 zshard(arg)
    421 	void *arg;
    422 {
    423 	struct zsc_softc *zsc;
    424 	int unit, rval;
    425 #ifdef ZSMACDEBUG
    426 	itecnputc(mac68k_zsdev, 'Z');
    427 #endif
    428 
    429 	rval = 0;
    430 	unit = zsc_cd.cd_ndevs;
    431 	while (--unit >= 0) {
    432 		zsc = zsc_cd.cd_devs[unit];
    433 		if (zsc != NULL) {
    434 			rval |= zsc_intr_hard(zsc);
    435 		}
    436 	}
    437 #ifdef ZSMACDEBUG
    438 	itecnputc(mac68k_zsdev, '\n');
    439 #endif
    440 	return (rval);
    441 }
    442 
    443 int zssoftpending;
    444 
    445 void
    446 zsc_req_softint(zsc)
    447 	struct zsc_softc *zsc;
    448 {
    449 	if (zssoftpending == 0) {
    450 		/* We are at splzs here, so no need to lock. */
    451 		zssoftpending = ZSSOFT_PRI;
    452 	/*	isr_soft_request(ZSSOFT_PRI); */
    453 		setsoftserial();
    454 	}
    455 }
    456 
    457 int
    458 zssoft(arg)
    459 	void *arg;
    460 {
    461 	struct zsc_softc *zsc;
    462 	int unit;
    463 
    464 	/* This is not the only ISR on this IPL. */
    465 	if (zssoftpending == 0)
    466 		return (0);
    467 
    468 	/*
    469 	 * The soft intr. bit will be set by zshard only if
    470 	 * the variable zssoftpending is zero.  The order of
    471 	 * these next two statements prevents our clearing
    472 	 * the soft intr bit just after zshard has set it.
    473 	 */
    474 /*	isr_soft_clear(ZSSOFT_PRI); */
    475 	zssoftpending = 0;
    476 
    477 	/* Do ttya/ttyb first, because they go faster. */
    478 	unit = zsc_cd.cd_ndevs;
    479 	while (--unit >= 0) {
    480 		zsc = zsc_cd.cd_devs[unit];
    481 		if (zsc != NULL) {
    482 			(void) zsc_intr_soft(zsc);
    483 		}
    484 	}
    485 	return (1);
    486 }
    487 
    488 
    489 /*
    490  * Read or write the chip with suitable delays.
    491  */
    492 #define	ZS_DELAY()
    493 /*
    494  * MacII hardware has the delay built in. No need for extra delay. :-)
    495  */
    496 
    497 u_char
    498 zs_read_reg(cs, reg)
    499 	struct zs_chanstate *cs;
    500 	u_char reg;
    501 {
    502 	u_char val;
    503 
    504 	*cs->cs_reg_csr = reg;
    505 	ZS_DELAY();
    506 	val = *cs->cs_reg_csr;
    507 	ZS_DELAY();
    508 	return val;
    509 }
    510 
    511 void
    512 zs_write_reg(cs, reg, val)
    513 	struct zs_chanstate *cs;
    514 	u_char reg, val;
    515 {
    516 	*cs->cs_reg_csr = reg;
    517 	ZS_DELAY();
    518 	*cs->cs_reg_csr = val;
    519 	ZS_DELAY();
    520 }
    521 
    522 u_char zs_read_csr(cs)
    523 	struct zs_chanstate *cs;
    524 {
    525 	register u_char v;
    526 
    527 	v = (*cs->cs_reg_csr) ^ ZSRR0_CTS;
    528 	/* make up for the fact CTS is wired backwards */
    529 	ZS_DELAY();
    530 	return v;
    531 }
    532 
    533 u_char zs_read_data(cs)
    534 	struct zs_chanstate *cs;
    535 {
    536 	register u_char v;
    537 
    538 	v = *cs->cs_reg_data;
    539 	ZS_DELAY();
    540 	return v;
    541 }
    542 
    543 void  zs_write_csr(cs, val)
    544 	struct zs_chanstate *cs;
    545 	u_char val;
    546 {
    547 	*cs->cs_reg_csr = val;
    548 	ZS_DELAY();
    549 }
    550 
    551 void  zs_write_data(cs, val)
    552 	struct zs_chanstate *cs;
    553 	u_char val;
    554 {
    555 	*cs->cs_reg_data = val;
    556 	ZS_DELAY();
    557 }
    558 
    559 /****************************************************************
    560  * Console support functions (Originally Sun3 specific!)
    561  * Now works w/ just mac68k port!
    562  ****************************************************************/
    563 
    564 #define zscnpollc	nullcnpollc
    565 cons_decl(zs);
    566 
    567 static void	zs_putc __P((register volatile struct zschan *, int));
    568 static int	zs_getc __P((register volatile struct zschan *));
    569 static void	zscnsetup __P((void));
    570 extern int	zsopen __P(( dev_t dev, int flags, int mode, struct proc *p));
    571 
    572 /*
    573  * Console functions.
    574  */
    575 
    576 /*
    577  * This code modled after the zs_setparam routine in zskgdb
    578  * It sets the console unit to a known state so we can output
    579  * correctly.
    580  */
    581 static void
    582 zscnsetup()
    583 {
    584 	struct zs_chanstate cs;
    585 	struct zschan *zc;
    586 	int    tconst, s;
    587 
    588 	/* Setup temporary chanstate. */
    589 	bzero((caddr_t)&cs, sizeof(cs));
    590 	zc = zs_conschan;
    591 	cs.cs_reg_csr  = &zc->zc_csr;
    592 	cs.cs_reg_data = &zc->zc_data;
    593 	cs.cs_channel = zs_consunit;
    594 
    595 	bcopy(zs_init_reg, cs.cs_preg, 16);
    596 	tconst = BPS_TO_TCONST(mac68k_machine.sccClkConst*2, zs_defspeed[0][zs_consunit]);
    597         cs.cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    598 	cs.cs_preg[1] = 0; /* don't enable interrupts */
    599         cs.cs_preg[12] = tconst;
    600         cs.cs_preg[13] = tconst >> 8;
    601 
    602         s = splhigh();
    603         zs_loadchannelregs(&cs);
    604         splx(s);
    605 }
    606 
    607 /*
    608  * zscnprobe is the routine which gets called as the kernel is trying to
    609  * figure out where the console should be. Each io driver which might
    610  * be the console (as defined in mac68k/conf.c) gets probed. The probe
    611  * fills in the consdev structure. Important parts are the device #,
    612  * and the console priority. Values are CN_DEAD (don't touch me),
    613  * CN_NORMAL (I'm here, but elsewhere might be better), CN_INTERNAL
    614  * (the video, better than CN_NORMAL), and CN_REMOTE (pick me!)
    615  *
    616  * As the mac's a bit different, we do extra work here. We mainly check
    617  * to see if we have serial echo going on, and if the tty's are supposed
    618  * to default to raw or not.
    619  */
    620 void
    621 zscnprobe(struct consdev * cp)
    622 {
    623         extern u_long   IOBase;
    624         int     maj, unit;
    625 
    626         for (maj = 0; maj < nchrdev; maj++) {
    627                 if (cdevsw[maj].d_open == zsopen) {
    628                         break;
    629                 }
    630         }
    631         if (maj == nchrdev) {
    632                 /* no console entry for us */
    633                 if (mac68k_machine.serial_boot_echo) {
    634                         mac68k_set_io_offsets(IOBase);
    635                 	zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
    636 			zs_consunit = 1;
    637 			zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
    638 			zs_init();
    639                         zscnsetup();
    640                 }
    641                 return;
    642         }
    643 
    644         cp->cn_pri = CN_NORMAL;                 /* Lower than CN_INTERNAL */
    645         if (mac68k_machine.serial_console != 0) {
    646                 cp->cn_pri = CN_REMOTE;         /* Higher than CN_INTERNAL */
    647                 mac68k_machine.serial_boot_echo =0;
    648         }
    649 
    650         unit = (mac68k_machine.serial_console == 1) ? 0 : 1;
    651 	zs_consunit = unit;
    652 
    653         mac68k_zsdev = cp->cn_dev = makedev(maj, unit);
    654 
    655         if (mac68k_machine.serial_boot_echo) {
    656                 /*
    657                  * at this point, we know that we don't have a serial
    658                  * console, but are doing echo
    659                  */
    660                 mac68k_set_io_offsets(IOBase);
    661                 zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
    662 		zs_consunit = 1;
    663 		zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
    664 		zs_init();
    665                 zscnsetup();
    666         }
    667         return;
    668 }
    669 
    670 void
    671 zscninit(struct consdev * cp)
    672 {
    673         extern u_long   IOBase;
    674 	int chan = minor(cp->cn_dev & 1);
    675 
    676         mac68k_set_io_offsets(IOBase);
    677 	zs_conschan = (struct zschan *) -1;
    678 	zs_consunit = chan;
    679 	zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE | ZS_HWFLAG_CONABRT;
    680 	zs_init();
    681         /*
    682 	 * zsinit will set up the addresses of the scc. It will also, if
    683 	 * zs_conschan != 0, calculate the new address of the conschan for
    684 	 * unit zs_consunit. So zs_init implicitly sets zs_conschan to the right
    685 	 * number. :-)
    686          */
    687         zscnsetup();
    688         printf("\nNetBSD/mac68k console\n");
    689 }
    690 
    691 
    692 /*
    693  * Polled input char.
    694  */
    695 static int
    696 zs_getc(zc)
    697 	register volatile struct zschan *zc;
    698 {
    699 	register int s, c, rr0;
    700 
    701 	s = splhigh();
    702 	/* Wait for a character to arrive. */
    703 	do {
    704 		rr0 = zc->zc_csr;
    705 		ZS_DELAY();
    706 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    707 
    708 	c = zc->zc_data;
    709 	ZS_DELAY();
    710 	splx(s);
    711 
    712 	/*
    713 	 * This is used by the kd driver to read scan codes,
    714 	 * so don't translate '\r' ==> '\n' here...
    715 	 */
    716 	return (c);
    717 }
    718 
    719 /*
    720  * Polled output char.
    721  */
    722 static void
    723 zs_putc(zc, c)
    724 	register volatile struct zschan *zc;
    725 	int c;
    726 {
    727 	register int s, rr0;
    728 	register long wait = 0;
    729 
    730 	s = splhigh();
    731 	/* Wait for transmitter to become ready. */
    732 	do {
    733 		rr0 = zc->zc_csr;
    734 		ZS_DELAY();
    735 	} while (((rr0 & ZSRR0_TX_READY) == 0) && (wait++ < 1000000));
    736 
    737 	if ((rr0 & ZSRR0_TX_READY) != 0) {
    738 		zc->zc_data = c;
    739 		ZS_DELAY();
    740 	}
    741 	splx(s);
    742 }
    743 
    744 
    745 /*
    746  * Polled console input putchar.
    747  */
    748 int
    749 zscngetc(dev)
    750 	dev_t dev;
    751 {
    752 	register volatile struct zschan *zc = zs_conschan;
    753 	register int c;
    754 
    755 	c = zs_getc(zc);
    756 	return (c);
    757 }
    758 
    759 /*
    760  * Polled console output putchar.
    761  */
    762 void
    763 zscnputc(dev, c)
    764 	dev_t dev;
    765 	int c;
    766 {
    767 	register volatile struct zschan *zc = zs_conschan;
    768 
    769 	zs_putc(zc, c);
    770 }
    771 
    772 
    773 
    774 /*
    775  * Handle user request to enter kernel debugger.
    776  */
    777 void
    778 zs_abort(zst)
    779 	register struct zstty_softc *zst;
    780 {
    781 	register volatile struct zschan *zc = zs_conschan;
    782 	int rr0;
    783 	register long wait = 0;
    784 
    785 	/* Wait for end of break to avoid PROM abort. */
    786 	/* XXX - Limit the wait? */
    787 	do {
    788 		rr0 = zc->zc_csr;
    789 		ZS_DELAY();
    790 	} while ((rr0 & ZSRR0_BREAK) && (wait++ < ZSABORT_DELAY));
    791 
    792 	if (wait > ZSABORT_DELAY) {
    793 		if (zst != NULL) zst->zst_hwflags &= ~ZS_HWFLAG_CONABRT;
    794 	/* If we time out, turn off the abort ability! */
    795 	}
    796 
    797 	/* XXX - Always available, but may be the PROM monitor. */
    798 	Debugger();
    799 }
    800