Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.13
      1 /*	$NetBSD: zs.c,v 1.13 1999/02/03 20:25:07 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 Minoura Makoto
      5  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Gordon W. Ross.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Zilog Z8530 Dual UART driver (machine-dependent part)
     42  *
     43  * X68k uses one Z8530 built-in. Channel A is for RS-232C serial port;
     44  * while channel B is dedicated to the mouse.
     45  * Extra Z8530's can be installed.  This driver supports up to 5 chips
     46  * including the built-in one.
     47  */
     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/cpu.h>
     62 #include <machine/z8530var.h>
     63 /*#include <arch/x68k/x68k/iodevice.h>*/
     64 
     65 #include <dev/ic/z8530reg.h>
     66 
     67 #include "zsc.h"	/* NZSC */
     68 #include "zstty.h"
     69 
     70 /* Make life easier for the initialized arrays here. */
     71 
     72 extern void Debugger __P((void));
     73 
     74 /*
     75  * Some warts needed by z8530tty.c -
     76  * The default parity REALLY needs to be the same as the PROM uses,
     77  * or you can not see messages done with printf during boot-up...
     78  */
     79 int zs_def_cflag = (CREAD | CS8 | HUPCL);
     80 int zs_major = 12;
     81 
     82 /*
     83  * X68k provides a 5.0 MHz clock to the ZS chips.
     84  * XXX: use 4.9152MHz constant for now!!!
     85  */
     86 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     87 
     88 static u_char zs_init_reg[16] = {
     89 	0,	/* 0: CMD (reset, etc.) */
     90 	0,	/* 1: No interrupts yet. */
     91 	0x70,	/* 2: XXX: IVECT */
     92 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
     93 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
     94 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
     95 	0,	/* 6: TXSYNC/SYNCLO */
     96 	0,	/* 7: RXSYNC/SYNCHI */
     97 	0,	/* 8: alias for data port */
     98 	ZSWR9_MASTER_IE,
     99 	ZSWR10_NRZ,	/*10: Misc. TX/RX control bits */
    100 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    101 	14,	/*12: BAUDLO (default=9600) */
    102 	0,	/*13: BAUDHI (default=9600) */
    103 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    104 	ZSWR15_BREAK_IE,
    105 };
    106 
    107 static volatile struct zschan *conschan = 0;
    108 
    109 
    110 /****************************************************************
    111  * Autoconfig
    112  ****************************************************************/
    113 
    114 /* Definition of the driver for autoconfig. */
    115 static int	zs_match __P((struct device *, struct cfdata *, void *));
    116 static void	zs_attach __P((struct device *, struct device *, void *));
    117 static int  zs_print __P((void *, const char *name));
    118 
    119 struct cfattach zsc_ca = {
    120 	sizeof(struct zsc_softc), zs_match, zs_attach
    121 };
    122 
    123 extern struct cfdriver zsc_cd;
    124 
    125 static volatile struct zsdevice *findzs(int);
    126 int zshard __P((void));
    127 int zssoft __P((void *));
    128 static int zs_get_speed __P((struct zs_chanstate *));
    129 
    130 
    131 /*
    132  * find zs address for x68k architecture
    133  */
    134 static volatile struct zsdevice *
    135 findzs(zs)
    136 	int zs;
    137 {
    138 	if (zs == 0)
    139 		return &IODEVbase->io_inscc;
    140 	if (1 <= zs && zs <= 4)
    141 		return &(IODEVbase->io_exscc)[zs - 1];
    142 	/* none */
    143 	return 0;
    144 }
    145 
    146 /*
    147  * Is the zs chip present?
    148  */
    149 static int
    150 zs_match(parent, cfp, aux)
    151 	struct device *parent;
    152 	struct cfdata *cfp;
    153 	void *aux;
    154 {
    155 	volatile void *addr;
    156 
    157 	if(strcmp("zs", aux) || (addr = findzs(cfp->cf_unit)) == 0)
    158 		return(0);
    159 	if (badaddr(addr))
    160 		return 0;
    161 	return(1);
    162 }
    163 
    164 /*
    165  * Attach a found zs.
    166  */
    167 static void
    168 zs_attach(parent, self, aux)
    169 	struct device *parent;
    170 	struct device *self;
    171 	void *aux;
    172 {
    173 	struct zsc_softc *zsc = (void *) self;
    174 	struct zsc_attach_args zsc_args;
    175 	volatile struct zschan *zc;
    176 	struct zs_chanstate *cs;
    177 	int s, zs_unit, channel;
    178 
    179 	zs_unit = zsc->zsc_dev.dv_unit;
    180 	zsc->zsc_addr = (void*) findzs (zs_unit);
    181 
    182 	printf("\n");
    183 
    184 	/*
    185 	 * Initialize software state for each channel.
    186 	 */
    187 	for (channel = 0; channel < 2; channel++) {
    188 		struct device *child;
    189 
    190 		zsc_args.channel = channel;
    191 		zsc_args.hwflags = 0;
    192 		cs = &zsc->zsc_cs_store[channel];
    193 		zsc->zsc_cs[channel] = cs;
    194 
    195 		cs->cs_channel = channel;
    196 		cs->cs_private = NULL;
    197 		cs->cs_ops = &zsops_null;
    198 		cs->cs_brg_clk = PCLK / 16;
    199 
    200 		if (channel == 0)
    201 			zc = (void*) &zsc->zsc_addr->zs_chan_a;
    202 		else
    203 			zc = (void*) &zsc->zsc_addr->zs_chan_b;
    204 		cs->cs_reg_csr  = &zc->zc_csr;
    205 		cs->cs_reg_data = &zc->zc_data;
    206 
    207 		zs_init_reg[2] = 0x70 + zs_unit;
    208 		bcopy(zs_init_reg, cs->cs_creg, 16);
    209 		bcopy(zs_init_reg, cs->cs_preg, 16);
    210 
    211 		cs->cs_defspeed = 9600;
    212 		cs->cs_defcflag = zs_def_cflag;
    213 
    214 		/* Make these correspond to cs_defcflag (-crtscts) */
    215 		cs->cs_rr0_dcd = ZSRR0_DCD;
    216 		cs->cs_rr0_cts = 0;
    217 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    218 		cs->cs_wr5_rts = 0;
    219 
    220 		/*
    221 		 * Clear the master interrupt enable.
    222 		 * The INTENA is common to both channels,
    223 		 * so just do it on the A channel.
    224 		 */
    225 		if (channel == 0) {
    226 			s = splzs();
    227 			zs_write_reg(cs, 9, 0);
    228 			splx(s);
    229 		}
    230 
    231 		/*
    232 		 * Look for a child driver for this channel.
    233 		 * The child attach will setup the hardware.
    234 		 */
    235 		child = config_found(self, (void *)&zsc_args, zs_print);
    236 		if (child == NULL) {
    237 			/* No sub-driver.  Just reset it. */
    238 			u_char reset = (channel == 0) ?
    239 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    240 			s = splzs();
    241 			zs_write_reg(cs,  9, reset);
    242 			splx(s);
    243 		}
    244 	}
    245 
    246 	/*
    247 	 * Set the master interrupt enable and interrupt vector.
    248 	 * (common to both channels, do it on A)
    249 	 */
    250 	cs = zsc->zsc_cs[0];
    251 	s = splzs();
    252 	/* interrupt vector */
    253 	zs_write_reg(cs, 2, 0x70 + zs_unit);
    254 	/* master interrupt control (enable) */
    255 	zs_write_reg(cs, 9, zs_init_reg[9]);
    256 	splx(s);
    257 }
    258 
    259 static int
    260 zs_print(aux, name)
    261 	void *aux;
    262 	const char *name;
    263 {
    264 	struct zsc_attach_args *args = aux;
    265 
    266 	if (name != NULL)
    267 		printf("%s: ", name);
    268 
    269 	if (args->channel != -1)
    270 		printf(" channel %d", args->channel);
    271 
    272 	return UNCONF;
    273 }
    274 
    275 static volatile int zssoftpending;
    276 
    277 /*
    278  * Our ZS chips all share a common, autovectored interrupt,
    279  * so we have to look at all of them on each interrupt.
    280  */
    281 int
    282 zshard(void)
    283 {
    284 	register struct zsc_softc *zsc;
    285 	register int unit, rval, softreq;
    286 
    287 	rval = softreq = 0;
    288 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    289 		zsc = zsc_cd.cd_devs[unit];
    290 		if (zsc == NULL)
    291 			continue;
    292 		rval |= zsc_intr_hard(zsc);
    293 		softreq |= zsc->zsc_cs[0]->cs_softreq;
    294 		softreq |= zsc->zsc_cs[1]->cs_softreq;
    295 	}
    296 
    297 	/* We are at splzs here, so no need to lock. */
    298 	if (softreq && (zssoftpending == 0)) {
    299 		zssoftpending = 1;
    300 		setsoftserial();
    301 	}
    302 	return (rval);
    303 }
    304 
    305 /*
    306  * Similar scheme as for zshard (look at all of them)
    307  */
    308 int
    309 zssoft(arg)
    310 	void *arg;
    311 {
    312 	register struct zsc_softc *zsc;
    313 	register int s, unit;
    314 
    315 	/* This is not the only ISR on this IPL. */
    316 	if (zssoftpending == 0)
    317 		return (0);
    318 
    319 	zssoftpending = 0;
    320 
    321 	/* Make sure we call the tty layer at spltty. */
    322 	s = spltty();
    323 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    324 		zsc = zsc_cd.cd_devs[unit];
    325 		if (zsc == NULL)
    326 			continue;
    327 		(void) zsc_intr_soft(zsc);
    328 	}
    329 	splx(s);
    330 	return (1);
    331 }
    332 
    333 
    334 /*
    335  * Compute the current baud rate given a ZS channel.
    336  */
    337 static int
    338 zs_get_speed(cs)
    339 	struct zs_chanstate *cs;
    340 {
    341 	int tconst;
    342 
    343 	tconst = zs_read_reg(cs, 12);
    344 	tconst |= zs_read_reg(cs, 13) << 8;
    345 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    346 }
    347 
    348 /*
    349  * MD functions for setting the baud rate and control modes.
    350  */
    351 int
    352 zs_set_speed(cs, bps)
    353 	struct zs_chanstate *cs;
    354 	int bps;	/* bits per second */
    355 {
    356 	int tconst, real_bps;
    357 
    358 	if (bps == 0)
    359 		return (0);
    360 
    361 #ifdef	DIAGNOSTIC
    362 	if (cs->cs_brg_clk == 0)
    363 		panic("zs_set_speed");
    364 #endif
    365 
    366 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    367 	if (tconst < 0)
    368 		return (EINVAL);
    369 
    370 	/* Convert back to make sure we can do it. */
    371 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    372 
    373 	/* XXX - Allow some tolerance here? */
    374 	if (real_bps != bps)
    375 		return (EINVAL);
    376 
    377 	cs->cs_preg[12] = tconst;
    378 	cs->cs_preg[13] = tconst >> 8;
    379 
    380 	/* Caller will stuff the pending registers. */
    381 	return (0);
    382 }
    383 
    384 int
    385 zs_set_modes(cs, cflag)
    386 	struct zs_chanstate *cs;
    387 	int cflag;	/* bits per second */
    388 {
    389 	int s;
    390 
    391 	/*
    392 	 * Output hardware flow control on the chip is horrendous:
    393 	 * if carrier detect drops, the receiver is disabled, and if
    394 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    395 	 * Therefore, NEVER set the HFC bit, and instead use the
    396 	 * status interrupt to detect CTS changes.
    397 	 */
    398 	s = splzs();
    399 	if ((cflag & (CLOCAL | MDMBUF)) != 0)
    400 		cs->cs_rr0_dcd = 0;
    401 	else
    402 		cs->cs_rr0_dcd = ZSRR0_DCD;
    403 	if ((cflag & CRTSCTS) != 0) {
    404 		cs->cs_wr5_dtr = ZSWR5_DTR;
    405 		cs->cs_wr5_rts = ZSWR5_RTS;
    406 		cs->cs_rr0_cts = ZSRR0_CTS;
    407 	} else if ((cflag & MDMBUF) != 0) {
    408 		cs->cs_wr5_dtr = 0;
    409 		cs->cs_wr5_rts = ZSWR5_DTR;
    410 		cs->cs_rr0_cts = ZSRR0_DCD;
    411 	} else {
    412 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    413 		cs->cs_wr5_rts = 0;
    414 		cs->cs_rr0_cts = 0;
    415 	}
    416 	splx(s);
    417 
    418 	/* Caller will stuff the pending registers. */
    419 	return (0);
    420 }
    421 
    422 
    423 /*
    424  * Read or write the chip with suitable delays.
    425  */
    426 
    427 u_char
    428 zs_read_reg(cs, reg)
    429 	struct zs_chanstate *cs;
    430 	u_char reg;
    431 {
    432 	u_char val;
    433 
    434 	*cs->cs_reg_csr = reg;
    435 	ZS_DELAY();
    436 	val = *cs->cs_reg_csr;
    437 	ZS_DELAY();
    438 	return val;
    439 }
    440 
    441 void
    442 zs_write_reg(cs, reg, val)
    443 	struct zs_chanstate *cs;
    444 	u_char reg, val;
    445 {
    446 	*cs->cs_reg_csr = reg;
    447 	ZS_DELAY();
    448 	*cs->cs_reg_csr = val;
    449 	ZS_DELAY();
    450 }
    451 
    452 u_char zs_read_csr(cs)
    453 	struct zs_chanstate *cs;
    454 {
    455 	register u_char val;
    456 
    457 	val = *cs->cs_reg_csr;
    458 	ZS_DELAY();
    459 	return val;
    460 }
    461 
    462 void  zs_write_csr(cs, val)
    463 	struct zs_chanstate *cs;
    464 	u_char val;
    465 {
    466 	*cs->cs_reg_csr = val;
    467 	ZS_DELAY();
    468 }
    469 
    470 u_char zs_read_data(cs)
    471 	struct zs_chanstate *cs;
    472 {
    473 	register u_char val;
    474 
    475 	val = *cs->cs_reg_data;
    476 	ZS_DELAY();
    477 	return val;
    478 }
    479 
    480 void  zs_write_data(cs, val)
    481 	struct zs_chanstate *cs;
    482 	u_char val;
    483 {
    484 	*cs->cs_reg_data = val;
    485 	ZS_DELAY();
    486 }
    487 
    488 /*
    489  * Handle user request to enter kernel debugger.
    490  */
    491 void
    492 zs_abort(cs)
    493 	struct zs_chanstate *cs;
    494 {
    495 	int rr0;
    496 
    497 	/* Wait for end of break to avoid PROM abort. */
    498 	/* XXX - Limit the wait? */
    499 	do {
    500 		rr0 = *cs->cs_reg_csr;
    501 		ZS_DELAY();
    502 	} while (rr0 & ZSRR0_BREAK);
    503 
    504 #ifdef DDB
    505 	Debugger();
    506 #else
    507 	printf ("BREAK!!\n");
    508 #endif
    509 }
    510