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