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