Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.3
      1 /*	$NetBSD: zs.c,v 1.3 1998/08/21 14:07:03 tsubai Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Gordon W. Ross.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Zilog Z8530 Dual UART driver (machine-dependent part)
     41  *
     42  * Runs two serial lines per chip using slave drivers.
     43  * Plain tty/async lines use the zs_async slave.
     44  * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
     45  */
     46 
     47 #include "opt_ddb.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/conf.h>
     52 #include <sys/device.h>
     53 #include <sys/file.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/kernel.h>
     56 #include <sys/proc.h>
     57 #include <sys/tty.h>
     58 #include <sys/time.h>
     59 #include <sys/syslog.h>
     60 
     61 #include <machine/autoconf.h>
     62 #include <machine/cpu.h>
     63 #include <machine/adrsmap.h>
     64 #include <machine/z8530var.h>
     65 
     66 #include <dev/cons.h>
     67 #include <dev/ic/z8530reg.h>
     68 
     69 #include "zsc.h"	/* NZSC */
     70 #define NZS NZSC
     71 
     72 /* Make life easier for the initialized arrays here. */
     73 #if NZS < 2
     74 #undef  NZS
     75 #define NZS 2
     76 #endif
     77 
     78 extern void Debugger __P((void));
     79 
     80 /*
     81  * Some warts needed by z8530tty.c -
     82  * The default parity REALLY needs to be the same as the PROM uses,
     83  * or you can not see messages done with printf during boot-up...
     84  */
     85 int zs_def_cflag = (CREAD | CS8 | HUPCL);
     86 int zs_major = 1;
     87 
     88 /*
     89  * The news3400 provides a 4.9152 MHz clock to the ZS chips.
     90  */
     91 #define PCLK1	(9600 * 512)	/* PCLK pin input clock rate */
     92 #define PCLK2	(7200 * 512)
     93 
     94 /*
     95  * Define interrupt levels.
     96  */
     97 #define ZSHARD_PRI 64
     98 
     99 #define ZS_DELAY()			delay(2)
    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 	volatile u_char	zc_data;	/* data */
    105 };
    106 struct zsdevice {
    107 	/* Yes, they are backwards. */
    108 	struct	zschan zs_chan_b;
    109 	struct	zschan zs_chan_a;
    110 };
    111 
    112 static struct zsdevice *zsaddr[NZS];
    113 
    114 /* Flags from cninit() */
    115 static int zs_hwflags[NZS][2];
    116 
    117 /* Default speed for all channels */
    118 static int zs_defspeed = 9600;
    119 
    120 static u_char zs_init_reg[16] = {
    121 	0,	/* 0: CMD (reset, etc.) */
    122 	0,	/* 1: No interrupts yet. */
    123 	ZSHARD_PRI,	/* IVECT */
    124 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    125 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    126 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    127 	0,	/* 6: TXSYNC/SYNCLO */
    128 	0,	/* 7: RXSYNC/SYNCHI */
    129 	0,	/* 8: alias for data port */
    130 	ZSWR9_MASTER_IE,
    131 	0,	/*10: Misc. TX/RX control bits */
    132 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    133 	14,	/*12: BAUDLO (default=9600) */
    134 	0,	/*13: BAUDHI (default=9600) */
    135 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    136 	ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
    137 };
    138 
    139 
    140 struct zschan *
    141 zs_get_chan_addr(zs_unit, channel)
    142 	int zs_unit, channel;
    143 {
    144 	struct zsdevice *addr;
    145 	struct zschan *zc;
    146 
    147 	if (zs_unit >= NZS)
    148 		return NULL;
    149 	addr = zsaddr[zs_unit];
    150 	if (addr == NULL)
    151 		return NULL;
    152 	if (channel == 0) {
    153 		zc = &addr->zs_chan_a;
    154 	} else {
    155 		zc = &addr->zs_chan_b;
    156 	}
    157 	return (zc);
    158 }
    159 
    160 
    161 /****************************************************************
    162  * Autoconfig
    163  ****************************************************************/
    164 
    165 /* Definition of the driver for autoconfig. */
    166 static int	zs_match __P((struct device *, struct cfdata *, void *));
    167 static void	zs_attach __P((struct device *, struct device *, void *));
    168 static int  zs_print __P((void *, const char *name));
    169 
    170 struct cfattach zsc_ca = {
    171 	sizeof(struct zsc_softc), zs_match, zs_attach
    172 };
    173 
    174 extern struct cfdriver zsc_cd;
    175 
    176 static void zshard __P((void *));
    177 static void zssoft __P((void *));
    178 static int zs_get_speed __P((struct zs_chanstate *));
    179 
    180 
    181 /*
    182  * Is the zs chip present?
    183  */
    184 static int
    185 zs_match(parent, cf, aux)
    186 	struct device *parent;
    187 	struct cfdata *cf;
    188 	void *aux;
    189 {
    190 	struct confargs *ca = aux;
    191 	int unit = cf->cf_unit;
    192 	void *va;
    193 
    194 	if (strcmp(ca->ca_name, "zsc"))
    195 		return 0;
    196 
    197 	va = zsaddr[unit];
    198 	if (va == NULL)
    199 		va = zsaddr[unit] = (void *)cf->cf_addr;
    200 
    201 	/* This returns -1 on a fault (bus error). */
    202 	if (badaddr(va, 1))
    203 		return 0;
    204 
    205 	return 1;
    206 }
    207 
    208 /*
    209  * Attach a found zs.
    210  *
    211  * Match slave number to zs unit number, so that misconfiguration will
    212  * not set up the keyboard as ttya, etc.
    213  */
    214 static void
    215 zs_attach(parent, self, aux)
    216 	struct device *parent;
    217 	struct device *self;
    218 	void *aux;
    219 {
    220 	struct zsc_softc *zsc = (void *) self;
    221 	/* struct confargs *ca = aux; */
    222 	struct zsc_attach_args zsc_args;
    223 	volatile struct zschan *zc;
    224 	struct zs_chanstate *cs;
    225 	int s, zs_unit, channel;
    226 	static int didintr;
    227 
    228 	zs_unit = zsc->zsc_dev.dv_unit;
    229 
    230 	printf("\n");
    231 
    232 	/*
    233 	 * Initialize software state for each channel.
    234 	 */
    235 	for (channel = 0; channel < 2; channel++) {
    236 		zsc_args.channel = channel;
    237 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
    238 		cs = &zsc->zsc_cs_store[channel];
    239 		zsc->zsc_cs[channel] = cs;
    240 
    241 		cs->cs_channel = channel;
    242 		cs->cs_private = NULL;
    243 		cs->cs_ops = &zsops_null;
    244 		if (zs_unit == 0)
    245 			cs->cs_brg_clk = PCLK1 / 16;
    246 		else
    247 			cs->cs_brg_clk = PCLK2 / 16;
    248 
    249 		zc = zs_get_chan_addr(zs_unit, channel);
    250 		cs->cs_reg_csr  = &zc->zc_csr;
    251 		cs->cs_reg_data = &zc->zc_data;
    252 
    253 		bcopy(zs_init_reg, cs->cs_creg, 16);
    254 		bcopy(zs_init_reg, cs->cs_preg, 16);
    255 
    256 		/* XXX: Get these from the EEPROM instead? */
    257 		/* XXX: See the mvme167 code.  Better. */
    258 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    259 			cs->cs_defspeed = zs_get_speed(cs);
    260 		else
    261 			cs->cs_defspeed = zs_defspeed;
    262 		cs->cs_defcflag = zs_def_cflag;
    263 
    264 		/* Make these correspond to cs_defcflag (-crtscts) */
    265 		cs->cs_rr0_dcd = ZSRR0_DCD;
    266 		cs->cs_rr0_cts = 0;
    267 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    268 		cs->cs_wr5_rts = 0;
    269 
    270 		/*
    271 		 * Clear the master interrupt enable.
    272 		 * The INTENA is common to both channels,
    273 		 * so just do it on the A channel.
    274 		 */
    275 		if (channel == 0) {
    276 			zs_write_reg(cs, 9, 0);
    277 		}
    278 
    279 		/*
    280 		 * Look for a child driver for this channel.
    281 		 * The child attach will setup the hardware.
    282 		 */
    283 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
    284 			/* No sub-driver.  Just reset it. */
    285 			u_char reset = (channel == 0) ?
    286 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    287 			s = splhigh();
    288 			zs_write_reg(cs,  9, reset);
    289 			splx(s);
    290 		}
    291 	}
    292 
    293 	/*
    294 	 * Now safe to install interrupt handlers.  Note the arguments
    295 	 * to the interrupt handlers aren't used.  Note, we only do this
    296 	 * once since both SCCs interrupt at the same level and vector.
    297 	 */
    298 	if (!didintr) {
    299 		didintr = 1;
    300 #if 0
    301 		isr_add_autovect(zssoft, NULL, ZSSOFT_PRI);
    302 		isr_add_autovect(zshard, NULL, ca->ca_intpri);
    303 #endif
    304 	}
    305 	/* XXX; evcnt_attach() ? */
    306 
    307 	/*
    308 	 * Set the master interrupt enable and interrupt vector.
    309 	 * (common to both channels, do it on A)
    310 	 */
    311 	cs = zsc->zsc_cs[0];
    312 	s = splhigh();
    313 	/* interrupt vector */
    314 	zs_write_reg(cs, 2, zs_init_reg[2]);
    315 	/* master interrupt control (enable) */
    316 	zs_write_reg(cs, 9, zs_init_reg[9]);
    317 	splx(s);
    318 }
    319 
    320 static int
    321 zs_print(aux, name)
    322 	void *aux;
    323 	const char *name;
    324 {
    325 	struct zsc_attach_args *args = aux;
    326 
    327 	if (name != NULL)
    328 		printf("%s: ", name);
    329 
    330 	if (args->channel != -1)
    331 		printf(" channel %d", args->channel);
    332 
    333 	return UNCONF;
    334 }
    335 
    336 static volatile int zssoftpending;
    337 
    338 /*
    339  * Our ZS chips all share a common, autovectored interrupt,
    340  * so we have to look at all of them on each interrupt.
    341  */
    342 static void
    343 zshard(arg)
    344 	void *arg;
    345 {
    346 	register struct zsc_softc *zsc;
    347 	register int unit, rval, softreq;
    348 
    349 	rval = softreq = 0;
    350 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    351 		zsc = zsc_cd.cd_devs[unit];
    352 		if (zsc == NULL)
    353 			continue;
    354 		rval |= zsc_intr_hard(zsc);
    355 		softreq |= zsc->zsc_cs[0]->cs_softreq;
    356 		softreq |= zsc->zsc_cs[1]->cs_softreq;
    357 	}
    358 
    359 	/* We are at splzs here, so no need to lock. */
    360 	if (softreq && (zssoftpending == 0)) {
    361 		zssoftpending = 1;
    362 		zssoft(arg);	/*isr_soft_request(ZSSOFT_PRI);*/
    363 	}
    364 	return;
    365 }
    366 
    367 /*
    368  * Similar scheme as for zshard (look at all of them)
    369  */
    370 static void
    371 zssoft(arg)
    372 	void *arg;
    373 {
    374 	register struct zsc_softc *zsc;
    375 	register int s, unit;
    376 
    377 	/* This is not the only ISR on this IPL. */
    378 	if (zssoftpending == 0)
    379 		return;
    380 
    381 	/*
    382 	 * The soft intr. bit will be set by zshard only if
    383 	 * the variable zssoftpending is zero.  The order of
    384 	 * these next two statements prevents our clearing
    385 	 * the soft intr bit just after zshard has set it.
    386 	 */
    387 	/*isr_soft_clear(ZSSOFT_PRI);*/
    388 	/*zssoftpending = 0;*/
    389 
    390 	/* Make sure we call the tty layer at spltty. */
    391 	s = spltty();
    392 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    393 		zsc = zsc_cd.cd_devs[unit];
    394 		if (zsc == NULL)
    395 			continue;
    396 		(void) zsc_intr_soft(zsc);
    397 	}
    398 	splx(s);
    399 	zssoftpending = 0;
    400 	return;
    401 }
    402 
    403 
    404 /*
    405  * Compute the current baud rate given a ZS channel.
    406  */
    407 static int
    408 zs_get_speed(cs)
    409 	struct zs_chanstate *cs;
    410 {
    411 	int tconst;
    412 
    413 	tconst = zs_read_reg(cs, 12);
    414 	tconst |= zs_read_reg(cs, 13) << 8;
    415 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    416 }
    417 
    418 /*
    419  * MD functions for setting the baud rate and control modes.
    420  */
    421 int
    422 zs_set_speed(cs, bps)
    423 	struct zs_chanstate *cs;
    424 	int bps;	/* bits per second */
    425 {
    426 	int tconst, real_bps;
    427 
    428 	if (bps == 0)
    429 		return (0);
    430 
    431 #ifdef	DIAGNOSTIC
    432 	if (cs->cs_brg_clk == 0)
    433 		panic("zs_set_speed");
    434 #endif
    435 
    436 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    437 	if (tconst < 0)
    438 		return (EINVAL);
    439 
    440 	/* Convert back to make sure we can do it. */
    441 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    442 
    443 	/* XXX - Allow some tolerance here? */
    444 	if (real_bps != bps)
    445 		return (EINVAL);
    446 
    447 	cs->cs_preg[12] = tconst;
    448 	cs->cs_preg[13] = tconst >> 8;
    449 
    450 	/* Caller will stuff the pending registers. */
    451 	return (0);
    452 }
    453 
    454 int
    455 zs_set_modes(cs, cflag)
    456 	struct zs_chanstate *cs;
    457 	int cflag;	/* bits per second */
    458 {
    459 	int s;
    460 
    461 	/*
    462 	 * Output hardware flow control on the chip is horrendous:
    463 	 * if carrier detect drops, the receiver is disabled, and if
    464 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    465 	 * Therefore, NEVER set the HFC bit, and instead use the
    466 	 * status interrupt to detect CTS changes.
    467 	 */
    468 	s = splzs();
    469 	if ((cflag & (CLOCAL | MDMBUF)) != 0)
    470 		cs->cs_rr0_dcd = 0;
    471 	else
    472 		cs->cs_rr0_dcd = ZSRR0_DCD;
    473 	if ((cflag & CRTSCTS) != 0) {
    474 		cs->cs_wr5_dtr = ZSWR5_DTR;
    475 		cs->cs_wr5_rts = ZSWR5_RTS;
    476 		cs->cs_rr0_cts = ZSRR0_CTS;
    477 	} else if ((cflag & MDMBUF) != 0) {
    478 		cs->cs_wr5_dtr = 0;
    479 		cs->cs_wr5_rts = ZSWR5_DTR;
    480 		cs->cs_rr0_cts = ZSRR0_DCD;
    481 	} else {
    482 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    483 		cs->cs_wr5_rts = 0;
    484 		cs->cs_rr0_cts = 0;
    485 	}
    486 	splx(s);
    487 
    488 	/* Caller will stuff the pending registers. */
    489 	return (0);
    490 }
    491 
    492 
    493 /*
    494  * Read or write the chip with suitable delays.
    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 val;
    526 
    527 	val = *cs->cs_reg_csr;
    528 	ZS_DELAY();
    529 	return val;
    530 }
    531 
    532 void  zs_write_csr(cs, val)
    533 	struct zs_chanstate *cs;
    534 	u_char val;
    535 {
    536 	*cs->cs_reg_csr = val;
    537 	ZS_DELAY();
    538 }
    539 
    540 u_char zs_read_data(cs)
    541 	struct zs_chanstate *cs;
    542 {
    543 	register u_char val;
    544 
    545 	val = *cs->cs_reg_data;
    546 	ZS_DELAY();
    547 	return val;
    548 }
    549 
    550 void  zs_write_data(cs, val)
    551 	struct zs_chanstate *cs;
    552 	u_char val;
    553 {
    554 	*cs->cs_reg_data = val;
    555 	ZS_DELAY();
    556 }
    557 
    558 void
    559 zs_abort(cs)
    560 	struct zs_chanstate *cs;
    561 {
    562 #ifdef DDB
    563 	Debugger();
    564 #endif
    565 }
    566 
    567 /*
    568  * Polled input char.
    569  */
    570 int
    571 zs_getc(arg)
    572 	void *arg;
    573 {
    574 	register volatile struct zschan *zc = arg;
    575 	register int s, c, rr0;
    576 
    577 	s = splhigh();
    578 	/* Wait for a character to arrive. */
    579 	do {
    580 		rr0 = zc->zc_csr;
    581 		ZS_DELAY();
    582 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    583 
    584 	c = zc->zc_data;
    585 	ZS_DELAY();
    586 	splx(s);
    587 
    588 	/*
    589 	 * This is used by the kd driver to read scan codes,
    590 	 * so don't translate '\r' ==> '\n' here...
    591 	 */
    592 	return (c);
    593 }
    594 
    595 /*
    596  * Polled output char.
    597  */
    598 void
    599 zs_putc(arg, c)
    600 	void *arg;
    601 	int c;
    602 {
    603 	register volatile struct zschan *zc = arg;
    604 	register int s, rr0;
    605 
    606 	s = splhigh();
    607 	/* Wait for transmitter to become ready. */
    608 	do {
    609 		rr0 = zc->zc_csr;
    610 		ZS_DELAY();
    611 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    612 
    613 	zc->zc_data = c;
    614 	ZS_DELAY();
    615 	splx(s);
    616 }
    617 
    618 /*****************************************************************/
    619 
    620 static void zscnprobe __P((struct consdev *));
    621 static void zscninit __P((struct consdev *));
    622 static int  zscngetc __P((dev_t));
    623 static void zscnputc __P((dev_t, int));
    624 static void zscnpollc __P((dev_t, int));
    625 
    626 struct consdev consdev_zs = {
    627 	zscnprobe,
    628 	zscninit,
    629 	zscngetc,
    630 	zscnputc,
    631 	zscnpollc
    632 };
    633 
    634 void
    635 zscnprobe(cn)
    636 	struct consdev *cn;
    637 {
    638 }
    639 
    640 void
    641 zscninit(cn)
    642 	struct consdev *cn;
    643 {
    644 	cn->cn_dev = makedev(zs_major, 0);
    645 	cn->cn_pri = CN_REMOTE;
    646 	zs_hwflags[0][0] = ZS_HWFLAG_CONSOLE;
    647 }
    648 
    649 int
    650 zscngetc(dev)
    651 	dev_t dev;
    652 {
    653 	return zs_getc(SCCPORT0A);
    654 }
    655 
    656 void
    657 zscnputc(dev, c)
    658 	dev_t dev;
    659 	int c;
    660 {
    661 	zs_putc(SCCPORT0A, c);
    662 }
    663 
    664 void
    665 zscnpollc(dev, on)
    666 	dev_t dev;
    667 	int on;
    668 {
    669 }
    670 
    671 /*
    672  * ZS vector interrupt service routine.
    673  */
    674 void
    675 zs_intr()
    676 {
    677 	int vec;
    678 
    679 	vec = *(volatile u_char *)SCCVECT;
    680 	zshard((void *)vec);		/* XXX vec is not used */
    681 }
    682