Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.7
      1 /*	$NetBSD: zs.c,v 1.7 2002/09/06 13:18:43 gehenna 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  */
     45 
     46 /*
     47  * news68k/dev/zs.c - based on {newsmips,x68k,mvme68k}/dev/zs.c
     48  */
     49 
     50 #include "opt_ddb.h"
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/conf.h>
     55 #include <sys/device.h>
     56 #include <sys/tty.h>
     57 
     58 #include <machine/cpu.h>
     59 #include <machine/z8530var.h>
     60 
     61 #include <dev/cons.h>
     62 #include <dev/ic/z8530reg.h>
     63 
     64 #include <news68k/dev/hbvar.h>
     65 
     66 int zs_getc __P((void *));
     67 void zs_putc __P((void *, int));
     68 
     69 extern void Debugger __P((void));
     70 
     71 /*
     72  * Some warts needed by z8530tty.c -
     73  * The default parity REALLY needs to be the same as the PROM uses,
     74  * or you can not see messages done with printf during boot-up...
     75  */
     76 int zs_def_cflag = (CREAD | CS8 | HUPCL);
     77 
     78 /*
     79  * The news68k machines use three different clocks for the ZS chips.
     80  */
     81 #define NPCLK	3
     82 #define PCLK0	(9600 * 416)	/*  news1700: 3.9936MHz */
     83 #define PCLK1	(9600 * 512)	/*  news1200: 4.9152MHz */
     84 #define PCLK2	(9600 * 384)	/*  external: 3.6864MHz */
     85 
     86 static const u_int pclk[NPCLK] = {
     87 	PCLK0,
     88 	PCLK1,
     89 	PCLK2,
     90 };
     91 
     92 /*
     93  * Define interrupt levels.
     94  */
     95 #define ZSHARD_PRI 5
     96 #define ZS_IVECT 64
     97 
     98 #define ZS_DELAY() /* delay(2) */
     99 
    100 /* The layout of this is hardware-dependent (padding, order). */
    101 struct zschan {
    102 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    103 	volatile u_char	zc_data;	/* data */
    104 };
    105 struct zsdevice {
    106 	/* Yes, they are backwards. */
    107 	struct	zschan zs_chan_b;
    108 	struct	zschan zs_chan_a;
    109 };
    110 
    111 static u_char zs_sir;
    112 
    113 /* Default speed for all channels */
    114 static int zs_defspeed = 9600;
    115 
    116 /* console status from cninit */
    117 static struct zs_chanstate zs_conschan_store;
    118 static struct zs_chanstate *zs_conschan;
    119 static struct zschan *zc_cons;
    120 
    121 static u_char zs_init_reg[16] = {
    122 	0,	/* 0: CMD (reset, etc.) */
    123 	0,	/* 1: No interrupts yet. */
    124 	ZS_IVECT,	/* IVECT */
    125 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    126 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    127 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    128 	0,	/* 6: TXSYNC/SYNCLO */
    129 	0,	/* 7: RXSYNC/SYNCHI */
    130 	0,	/* 8: alias for data port */
    131 	ZSWR9_MASTER_IE,
    132 	0,	/*10: Misc. TX/RX control bits */
    133 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    134 	BPS_TO_TCONST((PCLK0/16), 9600), /*12: BAUDLO (default=9600) */
    135 	0,			/*13: BAUDHI (default=9600) */
    136 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    137 	ZSWR15_BREAK_IE,
    138 };
    139 
    140 
    141 /****************************************************************
    142  * Autoconfig
    143  ****************************************************************/
    144 
    145 /* Definition of the driver for autoconfig. */
    146 static int zs_match __P((struct device *, struct cfdata *, void *));
    147 static void zs_attach __P((struct device *, struct device *, void *));
    148 static int zs_print __P((void *, const char *name));
    149 
    150 struct cfattach zsc_ca = {
    151 	sizeof(struct zsc_softc), zs_match, zs_attach
    152 };
    153 
    154 extern struct cfdriver zsc_cd;
    155 
    156 static int zshard __P((void *));
    157 void zssoft __P((void *));
    158 #if 0
    159 static int zs_get_speed __P((struct zs_chanstate *));
    160 #endif
    161 
    162 /*
    163  * Is the zs chip present?
    164  */
    165 static int
    166 zs_match(parent, cf, aux)
    167 	struct device *parent;
    168 	struct cfdata *cf;
    169 	void *aux;
    170 {
    171 	struct hb_attach_args *ha = aux;
    172 	u_int addr;
    173 
    174 	if (strcmp(ha->ha_name, "zsc"))
    175 		return 0;
    176 
    177 	/* XXX no default address */
    178 	if (ha->ha_address == -1)
    179 		return 0;
    180 
    181 	addr = IIOV(ha->ha_address);
    182 	/* This returns -1 on a fault (bus error). */
    183 	if (badaddr((void *)addr, 1))
    184 		return 0;
    185 
    186 	return 1;
    187 }
    188 
    189 /*
    190  * Attach a found zs.
    191  */
    192 static void
    193 zs_attach(parent, self, aux)
    194 	struct device *parent;
    195 	struct device *self;
    196 	void *aux;
    197 {
    198 	struct zsc_softc *zsc = (void *) self;
    199 	struct cfdata *cf = self->dv_cfdata;
    200 	struct hb_attach_args *ha = aux;
    201 	struct zsc_attach_args zsc_args;
    202 	struct zsdevice *zs;
    203 	struct zschan *zc;
    204 	struct zs_chanstate *cs;
    205 	int s, channel, clk;
    206 
    207 	zs = (void *)IIOV(ha->ha_address);
    208 
    209 	clk = cf->cf_flags;
    210 	if (clk < 0 || clk >= NPCLK)
    211 		clk = 0;
    212 
    213 	printf("\n");
    214 
    215 	/*
    216 	 * Initialize software state for each channel.
    217 	 */
    218 	for (channel = 0; channel < 2; channel++) {
    219 		zsc_args.channel = channel;
    220 		cs = &zsc->zsc_cs_store[channel];
    221 		zsc->zsc_cs[channel] = cs;
    222 		zc = (channel == 0) ? &zs->zs_chan_a : &zs->zs_chan_b;
    223 
    224 		if (ha->ha_vect != -1)
    225 			zs_init_reg[2] = ha->ha_vect;
    226 
    227 		if (zc == zc_cons) {
    228 			memcpy(cs, zs_conschan, sizeof(struct zs_chanstate));
    229 			zs_conschan = cs;
    230 			zsc_args.hwflags = ZS_HWFLAG_CONSOLE;
    231 		} else {
    232 			cs->cs_reg_csr  = &zc->zc_csr;
    233 			cs->cs_reg_data = &zc->zc_data;
    234 			memcpy(cs->cs_creg, zs_init_reg, 16);
    235 			memcpy(cs->cs_preg, zs_init_reg, 16);
    236 			cs->cs_defspeed = zs_defspeed;
    237 			zsc_args.hwflags = 0;
    238 		}
    239 
    240 		cs->cs_defcflag = zs_def_cflag;
    241 
    242 		cs->cs_channel = channel;
    243 		cs->cs_private = NULL;
    244 		cs->cs_ops = &zsops_null;
    245 		cs->cs_brg_clk = pclk[clk] / 16;
    246 
    247 		/* Make these correspond to cs_defcflag (-crtscts) */
    248 		cs->cs_rr0_dcd = ZSRR0_DCD;
    249 		cs->cs_rr0_cts = 0;
    250 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    251 		cs->cs_wr5_rts = 0;
    252 
    253 		/*
    254 		 * Clear the master interrupt enable.
    255 		 * The INTENA is common to both channels,
    256 		 * so just do it on the A channel.
    257 		 */
    258 		if (channel == 0) {
    259 			s = splhigh();
    260 			zs_write_reg(cs, 9, 0);
    261 			splx(s);
    262 		}
    263 
    264 		/*
    265 		 * Look for a child driver for this channel.
    266 		 * The child attach will setup the hardware.
    267 		 */
    268 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
    269 			/* No sub-driver.  Just reset it. */
    270 			u_char reset = (channel == 0) ?
    271 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    272 			s = splhigh();
    273 			zs_write_reg(cs,  9, reset);
    274 			splx(s);
    275 		}
    276 	}
    277 
    278 	/*
    279 	 * Now safe to install interrupt handlers.
    280 	 */
    281 	hb_intr_establish(zs_init_reg[2], zshard, ZSHARD_PRI, zsc);
    282 
    283 	/*
    284 	 * Set the master interrupt enable and interrupt vector.
    285 	 * (common to both channels, do it on A)
    286 	 */
    287 	cs = zsc->zsc_cs[0];
    288 	s = splhigh();
    289 	/* interrupt vector */
    290 	zs_write_reg(cs, 2, zs_init_reg[2]);
    291 	/* master interrupt control (enable) */
    292 	zs_write_reg(cs, 9, zs_init_reg[9]);
    293 	splx(s);
    294 
    295 	if (zs_sir == 0)
    296 		zs_sir = allocate_sir(zssoft, zsc);
    297 }
    298 
    299 static int
    300 zs_print(aux, name)
    301 	void *aux;
    302 	const char *name;
    303 {
    304 	struct zsc_attach_args *args = aux;
    305 
    306 	if (name != NULL)
    307 		printf("%s: ", name);
    308 
    309 	if (args->channel != -1)
    310 		printf(" channel %d", args->channel);
    311 
    312 	return UNCONF;
    313 }
    314 
    315 /*
    316  * For news68k-port, we don't use autovectored interrupt.
    317  * We do not need to look at all of the zs chips.
    318  */
    319 static int
    320 zshard(arg)
    321 	void *arg;
    322 {
    323 	struct zsc_softc *zsc = arg;
    324 	int rval;
    325 
    326 	rval = zsc_intr_hard(zsc);
    327 
    328 	/* We are at splzs here, so no need to lock. */
    329 	if (zsc->zsc_cs[0]->cs_softreq || zsc->zsc_cs[1]->cs_softreq) {
    330 		setsoftint(zs_sir);
    331 	}
    332 
    333 	return (rval);
    334 }
    335 
    336 /*
    337  * Shared among the all chips. We have to look at all of them.
    338  */
    339 void
    340 zssoft(arg)
    341 	void *arg;
    342 {
    343 	struct zsc_softc *zsc;
    344 	int s, unit;
    345 
    346 	/* Make sure we call the tty layer at spltty. */
    347 	s = spltty();
    348 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    349 		zsc = zsc_cd.cd_devs[unit];
    350 		if (zsc == NULL)
    351 			continue;
    352 		(void) zsc_intr_soft(zsc);
    353 	}
    354 	splx(s);
    355 }
    356 
    357 /*
    358  * Compute the current baud rate given a ZS channel.
    359  */
    360 #if 0
    361 static int
    362 zs_get_speed(cs)
    363 	struct zs_chanstate *cs;
    364 {
    365 	int tconst;
    366 
    367 	tconst = zs_read_reg(cs, 12);
    368 	tconst |= zs_read_reg(cs, 13) << 8;
    369 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    370 }
    371 #endif
    372 
    373 /*
    374  * MD functions for setting the baud rate and control modes.
    375  */
    376 int
    377 zs_set_speed(cs, bps)
    378 	struct zs_chanstate *cs;
    379 	int bps;	/* bits per second */
    380 {
    381 	int tconst, real_bps;
    382 
    383 	if (bps == 0)
    384 		return (0);
    385 
    386 #ifdef	DIAGNOSTIC
    387 	if (cs->cs_brg_clk == 0)
    388 		panic("zs_set_speed");
    389 #endif
    390 
    391 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    392 	if (tconst < 0)
    393 		return (EINVAL);
    394 
    395 	/* Convert back to make sure we can do it. */
    396 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    397 
    398 	/* XXX - Allow some tolerance here? */
    399 	if (real_bps != bps)
    400 		return (EINVAL);
    401 
    402 	cs->cs_preg[12] = tconst;
    403 	cs->cs_preg[13] = tconst >> 8;
    404 
    405 	/* Caller will stuff the pending registers. */
    406 	return (0);
    407 }
    408 
    409 int
    410 zs_set_modes(cs, cflag)
    411 	struct zs_chanstate *cs;
    412 	int cflag;	/* bits per second */
    413 {
    414 	int s;
    415 
    416 	/*
    417 	 * Output hardware flow control on the chip is horrendous:
    418 	 * if carrier detect drops, the receiver is disabled, and if
    419 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    420 	 * Therefore, NEVER set the HFC bit, and instead use the
    421 	 * status interrupt to detect CTS changes.
    422 	 */
    423 	s = splzs();
    424 	cs->cs_rr0_pps = 0;
    425 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    426 		cs->cs_rr0_dcd = 0;
    427 		if ((cflag & MDMBUF) == 0)
    428 			cs->cs_rr0_pps = ZSRR0_DCD;
    429 	} else
    430 		cs->cs_rr0_dcd = ZSRR0_DCD;
    431 	if ((cflag & CRTSCTS) != 0) {
    432 		cs->cs_wr5_dtr = ZSWR5_DTR;
    433 		cs->cs_wr5_rts = ZSWR5_RTS;
    434 		cs->cs_rr0_cts = ZSRR0_CTS;
    435 	} else if ((cflag & MDMBUF) != 0) {
    436 		cs->cs_wr5_dtr = 0;
    437 		cs->cs_wr5_rts = ZSWR5_DTR;
    438 		cs->cs_rr0_cts = ZSRR0_DCD;
    439 	} else {
    440 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    441 		cs->cs_wr5_rts = 0;
    442 		cs->cs_rr0_cts = 0;
    443 	}
    444 	splx(s);
    445 
    446 	/* Caller will stuff the pending registers. */
    447 	return (0);
    448 }
    449 
    450 
    451 /*
    452  * Read or write the chip with suitable delays.
    453  */
    454 
    455 u_char
    456 zs_read_reg(cs, reg)
    457 	struct zs_chanstate *cs;
    458 	u_char reg;
    459 {
    460 	u_char val;
    461 
    462 	*cs->cs_reg_csr = reg;
    463 	ZS_DELAY();
    464 	val = *cs->cs_reg_csr;
    465 	ZS_DELAY();
    466 	return val;
    467 }
    468 
    469 void
    470 zs_write_reg(cs, reg, val)
    471 	struct zs_chanstate *cs;
    472 	u_char reg, val;
    473 {
    474 	*cs->cs_reg_csr = reg;
    475 	ZS_DELAY();
    476 	*cs->cs_reg_csr = val;
    477 	ZS_DELAY();
    478 }
    479 
    480 u_char
    481 zs_read_csr(cs)
    482 	struct zs_chanstate *cs;
    483 {
    484 	u_char val;
    485 
    486 	val = *cs->cs_reg_csr;
    487 	ZS_DELAY();
    488 	return val;
    489 }
    490 
    491 void
    492 zs_write_csr(cs, val)
    493 	struct zs_chanstate *cs;
    494 	u_char val;
    495 {
    496 	*cs->cs_reg_csr = val;
    497 	ZS_DELAY();
    498 }
    499 
    500 u_char
    501 zs_read_data(cs)
    502 	struct zs_chanstate *cs;
    503 {
    504 	u_char val;
    505 
    506 	val = *cs->cs_reg_data;
    507 	ZS_DELAY();
    508 	return val;
    509 }
    510 
    511 void
    512 zs_write_data(cs, val)
    513 	struct zs_chanstate *cs;
    514 	u_char val;
    515 {
    516 	*cs->cs_reg_data = val;
    517 	ZS_DELAY();
    518 }
    519 
    520 void
    521 zs_abort(cs)
    522 	struct zs_chanstate *cs;
    523 {
    524 #ifdef DDB
    525 	Debugger();
    526 #endif
    527 }
    528 
    529 /*
    530  * Polled input char.
    531  */
    532 int
    533 zs_getc(arg)
    534 	void *arg;
    535 {
    536 	struct zs_chanstate *cs = arg;
    537 	int s, c, rr0;
    538 
    539 	s = splhigh();
    540 	/* Wait for a character to arrive. */
    541 	do {
    542 		rr0 = *cs->cs_reg_csr;
    543 		ZS_DELAY();
    544 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    545 
    546 	c = *cs->cs_reg_data;
    547 	ZS_DELAY();
    548 	splx(s);
    549 
    550 	return c;
    551 }
    552 
    553 /*
    554  * Polled output char.
    555  */
    556 void
    557 zs_putc(arg, c)
    558 	void *arg;
    559 	int c;
    560 {
    561 	struct zs_chanstate *cs = arg;
    562 	int s, rr0;
    563 
    564 	s = splhigh();
    565 	/* Wait for transmitter to become ready. */
    566 	do {
    567 		rr0 = *cs->cs_reg_csr;
    568 		ZS_DELAY();
    569 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    570 
    571 	*cs->cs_reg_data = c;
    572 	ZS_DELAY();
    573 	splx(s);
    574 }
    575 
    576 /*****************************************************************/
    577 
    578 static void zscnprobe __P((struct consdev *));
    579 static void zscninit __P((struct consdev *));
    580 static int  zscngetc __P((dev_t));
    581 static void zscnputc __P((dev_t, int));
    582 
    583 struct consdev consdev_zs = {
    584 	zscnprobe,
    585 	zscninit,
    586 	zscngetc,
    587 	zscnputc,
    588 	nullcnpollc,
    589 	NULL,
    590 };
    591 
    592 static void
    593 zscnprobe(cn)
    594 	struct consdev *cn;
    595 {
    596 	extern const struct cdevsw zstty_cdevsw;
    597 	extern int tty00_is_console;
    598 
    599 	cn->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), 0);
    600 	if (tty00_is_console)
    601 		cn->cn_pri = CN_REMOTE;
    602 	else
    603 		cn->cn_pri = CN_NORMAL;
    604 }
    605 
    606 static void
    607 zscninit(cn)
    608 	struct consdev *cn;
    609 {
    610 	struct zs_chanstate *cs;
    611 
    612 	extern volatile u_char *sccport0a;
    613 
    614 	zc_cons = (struct zschan *)sccport0a; /* XXX */
    615 
    616 	zs_conschan = cs = &zs_conschan_store;
    617 
    618 	/* Setup temporary chanstate. */
    619 	cs->cs_reg_csr  = &zc_cons->zc_csr;
    620 	cs->cs_reg_data = &zc_cons->zc_data;
    621 
    622 	/* Initialize the pending registers. */
    623 	memcpy(cs->cs_preg, zs_init_reg, 16);
    624 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    625 
    626 	cs->cs_preg[12] = BPS_TO_TCONST(pclk[systype] / 16, 9600); /* XXX */
    627 	cs->cs_preg[13] = 0;
    628 	cs->cs_defspeed = 9600;
    629 
    630 	/* Clear the master interrupt enable. */
    631 	zs_write_reg(cs, 9, 0);
    632 
    633 	/* Reset the whole SCC chip. */
    634 	zs_write_reg(cs, 9, ZSWR9_HARD_RESET);
    635 
    636 	/* Copy "pending" to "current" and H/W */
    637 	zs_loadchannelregs(cs);
    638 }
    639 
    640 static int
    641 zscngetc(dev)
    642 	dev_t dev;
    643 {
    644 	return zs_getc((void *)zs_conschan);
    645 }
    646 
    647 static void
    648 zscnputc(dev, c)
    649 	dev_t dev;
    650 	int c;
    651 {
    652 	zs_putc((void *)zs_conschan, c);
    653 }
    654