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