Home | History | Annotate | Line # | Download | only in dev
zs_hb.c revision 1.22
      1 /*	$NetBSD: zs_hb.c,v 1.22 2007/11/09 00:05:05 ad 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 <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: zs_hb.c,v 1.22 2007/11/09 00:05:05 ad Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/device.h>
     53 #include <sys/tty.h>
     54 #include <sys/conf.h>
     55 
     56 #include <machine/adrsmap.h>
     57 #include <machine/cpu.h>
     58 #include <machine/z8530var.h>
     59 
     60 #include <dev/cons.h>
     61 #include <dev/ic/z8530reg.h>
     62 
     63 #include <newsmips/dev/hbvar.h>
     64 
     65 #include "zsc.h"	/* NZSC */
     66 #define NZS NZSC
     67 
     68 /* Make life easier for the initialized arrays here. */
     69 #if NZS < 2
     70 #undef  NZS
     71 #define NZS 2
     72 #endif
     73 
     74 #define ZSCFLAG_EX	0x01	/* expansion board */
     75 
     76 /*
     77  * The news3400 provides a 4.9152 MHz clock to the ZS chips.
     78  */
     79 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     80 #define PCLK_EX	(9600 * 384)
     81 
     82 /*
     83  * Define interrupt levels.
     84  */
     85 #define ZSHARD_PRI 64
     86 
     87 #define ZS_DELAY() {(void)*(volatile char *)INTEN1; delay(2);}
     88 
     89 /* The layout of this is hardware-dependent (padding, order). */
     90 struct zschan {
     91 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
     92 	volatile u_char	zc_data;	/* data */
     93 };
     94 struct zsdevice {
     95 	/* Yes, they are backwards. */
     96 	struct	zschan zs_chan_b;
     97 	struct	zschan zs_chan_a;
     98 };
     99 
    100 extern int zs_def_cflag;
    101 
    102 static struct zsdevice *zsaddr[NZS];
    103 
    104 /* Flags from cninit() */
    105 static int zs_hwflags[NZS][2];
    106 
    107 /* Default speed for all channels */
    108 static int zs_defspeed = 9600;
    109 
    110 static u_char zs_init_reg[16] = {
    111 	0,	/* 0: CMD (reset, etc.) */
    112 	0,	/* 1: No interrupts yet. */
    113 	ZSHARD_PRI,	/* IVECT */
    114 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    115 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    116 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    117 	0,	/* 6: TXSYNC/SYNCLO */
    118 	0,	/* 7: RXSYNC/SYNCHI */
    119 	0,	/* 8: alias for data port */
    120 	ZSWR9_MASTER_IE,
    121 	0,	/*10: Misc. TX/RX control bits */
    122 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    123 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
    124 	0,			/*13: BAUDHI (default=9600) */
    125 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    126 	ZSWR15_BREAK_IE,
    127 };
    128 
    129 static struct zschan * zs_get_chan_addr(int, int);
    130 static void zs_hb_delay(void);
    131 static int zshard_hb(void *);
    132 static int zs_getc(void *);
    133 static void zs_putc(void *, int);
    134 
    135 struct zschan *
    136 zs_get_chan_addr(int zs_unit, int channel)
    137 {
    138 	struct zsdevice *addr;
    139 	struct zschan *zc;
    140 
    141 	if (zs_unit >= NZS)
    142 		return NULL;
    143 	addr = zsaddr[zs_unit];
    144 	if (addr == NULL)
    145 		return NULL;
    146 	if (channel == 0) {
    147 		zc = &addr->zs_chan_a;
    148 	} else {
    149 		zc = &addr->zs_chan_b;
    150 	}
    151 	return zc;
    152 }
    153 
    154 static void
    155 zs_hb_delay(void)
    156 {
    157 
    158 	ZS_DELAY();
    159 }
    160 
    161 /****************************************************************
    162  * Autoconfig
    163  ****************************************************************/
    164 
    165 /* Definition of the driver for autoconfig. */
    166 int zs_hb_match(struct device *, struct cfdata *, void *);
    167 void zs_hb_attach(struct device *, struct device *, void *);
    168 
    169 CFATTACH_DECL(zsc_hb, sizeof(struct zsc_softc),
    170     zs_hb_match, zs_hb_attach, NULL, NULL);
    171 
    172 /*
    173  * Is the zs chip present?
    174  */
    175 int
    176 zs_hb_match(struct device *parent, struct cfdata *cf, void *aux)
    177 {
    178 	struct hb_attach_args *ha = aux;
    179 
    180 	if (strcmp(ha->ha_name, "zsc"))
    181 		return 0;
    182 
    183 	/* This returns -1 on a fault (bus error). */
    184 	if (hb_badaddr((char *)ha->ha_addr, 1))
    185 		return 0;
    186 
    187 	return 1;
    188 }
    189 
    190 /*
    191  * Attach a found zs.
    192  *
    193  * Match slave number to zs unit number, so that misconfiguration will
    194  * not set up the keyboard as ttya, etc.
    195  */
    196 void
    197 zs_hb_attach(struct device *parent, struct device *self, void *aux)
    198 {
    199 	struct zsc_softc *zsc = (void *)self;
    200 	struct hb_attach_args *ha = aux;
    201 	struct zsc_attach_args zsc_args;
    202 	volatile struct zschan *zc;
    203 	struct zs_chanstate *cs;
    204 	int s, zs_unit, channel, intlevel;
    205 	static int didintr;
    206 
    207 	zs_unit = device_unit(&zsc->zsc_dev);
    208 	intlevel = ha->ha_level;
    209 	zsaddr[zs_unit] = (void *)ha->ha_addr;
    210 
    211 	if (intlevel == -1) {
    212 #if 0
    213 		printf(": interrupt level not configured\n");
    214 		return;
    215 #else
    216 		printf(": interrupt level not configured; using");
    217 		intlevel = 1;
    218 #endif
    219 	}
    220 
    221 	printf(" level %d\n", intlevel);
    222 
    223 	zs_delay = zs_hb_delay;
    224 
    225 	/*
    226 	 * Initialize software state for each channel.
    227 	 */
    228 	for (channel = 0; channel < 2; channel++) {
    229 		zsc_args.channel = channel;
    230 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
    231 		cs = &zsc->zsc_cs_store[channel];
    232 		zsc->zsc_cs[channel] = cs;
    233 
    234 		zs_lock_init(cs);
    235 		cs->cs_channel = channel;
    236 		cs->cs_private = NULL;
    237 		cs->cs_ops = &zsops_null;
    238 		if ((device_cfdata(&zsc->zsc_dev)->cf_flags & ZSCFLAG_EX) == 0)
    239 			cs->cs_brg_clk = PCLK / 16;
    240 		else
    241 			cs->cs_brg_clk = PCLK_EX / 16;
    242 
    243 		zc = zs_get_chan_addr(zs_unit, channel);
    244 		cs->cs_reg_csr  = &zc->zc_csr;
    245 		cs->cs_reg_data = &zc->zc_data;
    246 
    247 		memcpy(cs->cs_creg, zs_init_reg, 16);
    248 		memcpy(cs->cs_preg, zs_init_reg, 16);
    249 
    250 		/* XXX: Get these from the EEPROM instead? */
    251 		/* XXX: See the mvme167 code.  Better. */
    252 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    253 			cs->cs_defspeed = zs_get_speed(cs);
    254 		else
    255 			cs->cs_defspeed = zs_defspeed;
    256 		cs->cs_defcflag = zs_def_cflag;
    257 
    258 		/* Make these correspond to cs_defcflag (-crtscts) */
    259 		cs->cs_rr0_dcd = ZSRR0_DCD;
    260 		cs->cs_rr0_cts = 0;
    261 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    262 		cs->cs_wr5_rts = 0;
    263 
    264 		/*
    265 		 * Clear the master interrupt enable.
    266 		 * The INTENA is common to both channels,
    267 		 * so just do it on the A channel.
    268 		 */
    269 		if (channel == 0) {
    270 			zs_write_reg(cs, 9, 0);
    271 		}
    272 
    273 		/*
    274 		 * Look for a child driver for this channel.
    275 		 * The child attach will setup the hardware.
    276 		 */
    277 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
    278 			/* No sub-driver.  Just reset it. */
    279 			u_char reset = (channel == 0) ?
    280 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    281 			s = splhigh();
    282 			zs_write_reg(cs, 9, reset);
    283 			splx(s);
    284 		}
    285 	}
    286 
    287 	/*
    288 	 * Now safe to install interrupt handlers.  Note the arguments
    289 	 * to the interrupt handlers aren't used.  Note, we only do this
    290 	 * once since both SCCs interrupt at the same level and vector.
    291 	 */
    292 	if (!didintr) {
    293 		didintr = 1;
    294 
    295 		zsc->zsc_si = softintr_establish(IPL_SOFTSERIAL, zssoft, zsc);
    296 		hb_intr_establish(intlevel, INTST1_SCC, IPL_SERIAL,
    297 		    zshard_hb, NULL);
    298 	}
    299 	/* XXX; evcnt_attach() ? */
    300 
    301 	/*
    302 	 * Set the master interrupt enable and interrupt vector.
    303 	 * (common to both channels, do it on A)
    304 	 */
    305 	cs = zsc->zsc_cs[0];
    306 	s = splhigh();
    307 	/* interrupt vector */
    308 	zs_write_reg(cs, 2, zs_init_reg[2]);
    309 	/* master interrupt control (enable) */
    310 	zs_write_reg(cs, 9, zs_init_reg[9]);
    311 	splx(s);
    312 }
    313 
    314 static int
    315 zshard_hb(void *arg)
    316 {
    317 	int rv;
    318 
    319 	(void) *(volatile u_char *)SCCVECT;
    320 	rv = zshard(arg);
    321 
    322 	/* XXX news3400 sometimes losts zs interrupt */
    323 	if (rv)
    324 		zshard(arg);
    325 
    326 	return rv;
    327 }
    328 
    329 /*
    330  * Polled input char.
    331  */
    332 int
    333 zs_getc(void *arg)
    334 {
    335 	volatile struct zschan *zc = arg;
    336 	int s, c, rr0;
    337 
    338 	s = splhigh();
    339 	/* Wait for a character to arrive. */
    340 	do {
    341 		rr0 = zc->zc_csr;
    342 		ZS_DELAY();
    343 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    344 
    345 	c = zc->zc_data;
    346 	ZS_DELAY();
    347 	splx(s);
    348 
    349 	/*
    350 	 * This is used by the kd driver to read scan codes,
    351 	 * so don't translate '\r' ==> '\n' here...
    352 	 */
    353 	return c;
    354 }
    355 
    356 /*
    357  * Polled output char.
    358  */
    359 void
    360 zs_putc(void *arg, int c)
    361 {
    362 	volatile struct zschan *zc = arg;
    363 	int s, rr0;
    364 
    365 	s = splhigh();
    366 	/* Wait for transmitter to become ready. */
    367 	do {
    368 		rr0 = zc->zc_csr;
    369 		ZS_DELAY();
    370 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    371 
    372 	zc->zc_data = c;
    373 	ZS_DELAY();
    374 	splx(s);
    375 }
    376 
    377 /*****************************************************************/
    378 
    379 static void zscnprobe(struct consdev *);
    380 static void zscninit(struct consdev *);
    381 static int  zscngetc(dev_t);
    382 static void zscnputc(dev_t, int);
    383 
    384 struct consdev consdev_zs = {
    385 	zscnprobe,
    386 	zscninit,
    387 	zscngetc,
    388 	zscnputc,
    389 	nullcnpollc,
    390 	NULL,
    391 	NULL,
    392 	NULL,
    393 	NODEV,
    394 	CN_DEAD
    395 };
    396 
    397 static void
    398 zscnprobe(struct consdev *cn)
    399 {
    400 }
    401 
    402 static void
    403 zscninit(struct consdev *cn)
    404 {
    405 	extern const struct cdevsw zstty_cdevsw;
    406 
    407 	cn->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), 0);
    408 	cn->cn_pri = CN_REMOTE;
    409 	zs_hwflags[0][0] = ZS_HWFLAG_CONSOLE;
    410 }
    411 
    412 static int
    413 zscngetc(dev_t dev)
    414 {
    415 
    416 	return zs_getc((void *)SCCPORT0A);
    417 }
    418 
    419 static void
    420 zscnputc(dev_t dev, int c)
    421 {
    422 
    423 	zs_putc((void *)SCCPORT0A, c);
    424 }
    425