Home | History | Annotate | Line # | Download | only in dev
zs_hb.c revision 1.24
      1 /*	$NetBSD: zs_hb.c,v 1.24 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  * 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.24 2008/03/29 19:15:35 tsutsui 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 uint8_t zc_csr;	/* ctrl,status, and indirect access */
     93 	volatile uint8_t 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 uint8_t 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(device_t, cfdata_t, void *);
    168 void zs_hb_attach(device_t, device_t, void *);
    169 
    170 CFATTACH_DECL_NEW(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(device_t parent, cfdata_t 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(device_t parent, device_t self, void *aux)
    199 {
    200 	struct zsc_softc *zsc = device_private(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 	zsc->zsc_dev = self;
    209 	zs_unit = device_unit(self);
    210 	intlevel = ha->ha_level;
    211 	zsaddr[zs_unit] = (void *)ha->ha_addr;
    212 
    213 	if (intlevel == -1) {
    214 #if 0
    215 		aprint_error(": interrupt level not configured\n");
    216 		return;
    217 #else
    218 		aprint_error(": interrupt level not configured; using");
    219 		intlevel = 1;
    220 #endif
    221 	}
    222 
    223 	aprint_error(" level %d\n", intlevel);
    224 
    225 	zs_delay = zs_hb_delay;
    226 
    227 	/*
    228 	 * Initialize software state for each channel.
    229 	 */
    230 	for (channel = 0; channel < 2; channel++) {
    231 		zsc_args.channel = channel;
    232 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
    233 		cs = &zsc->zsc_cs_store[channel];
    234 		zsc->zsc_cs[channel] = cs;
    235 
    236 		zs_lock_init(cs);
    237 		cs->cs_channel = channel;
    238 		cs->cs_private = NULL;
    239 		cs->cs_ops = &zsops_null;
    240 		if ((device_cfdata(self)->cf_flags & ZSCFLAG_EX) == 0)
    241 			cs->cs_brg_clk = PCLK / 16;
    242 		else
    243 			cs->cs_brg_clk = PCLK_EX / 16;
    244 
    245 		zc = zs_get_chan_addr(zs_unit, channel);
    246 		cs->cs_reg_csr  = &zc->zc_csr;
    247 		cs->cs_reg_data = &zc->zc_data;
    248 
    249 		memcpy(cs->cs_creg, zs_init_reg, 16);
    250 		memcpy(cs->cs_preg, zs_init_reg, 16);
    251 
    252 		/* XXX: Get these from the EEPROM instead? */
    253 		/* XXX: See the mvme167 code.  Better. */
    254 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    255 			cs->cs_defspeed = zs_get_speed(cs);
    256 		else
    257 			cs->cs_defspeed = zs_defspeed;
    258 		cs->cs_defcflag = zs_def_cflag;
    259 
    260 		/* Make these correspond to cs_defcflag (-crtscts) */
    261 		cs->cs_rr0_dcd = ZSRR0_DCD;
    262 		cs->cs_rr0_cts = 0;
    263 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    264 		cs->cs_wr5_rts = 0;
    265 
    266 		/*
    267 		 * Clear the master interrupt enable.
    268 		 * The INTENA is common to both channels,
    269 		 * so just do it on the A channel.
    270 		 */
    271 		if (channel == 0) {
    272 			zs_write_reg(cs, 9, 0);
    273 		}
    274 
    275 		/*
    276 		 * Look for a child driver for this channel.
    277 		 * The child attach will setup the hardware.
    278 		 */
    279 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
    280 			/* No sub-driver.  Just reset it. */
    281 			uint8_t reset = (channel == 0) ?
    282 			    ZSWR9_A_RESET : ZSWR9_B_RESET;
    283 			s = splhigh();
    284 			zs_write_reg(cs, 9, reset);
    285 			splx(s);
    286 		}
    287 	}
    288 
    289 	/*
    290 	 * Now safe to install interrupt handlers.  Note the arguments
    291 	 * to the interrupt handlers aren't used.  Note, we only do this
    292 	 * once since both SCCs interrupt at the same level and vector.
    293 	 */
    294 	if (!didintr) {
    295 		didintr = 1;
    296 
    297 		zsc->zsc_si = softint_establish(SOFTINT_SERIAL, zssoft, zsc);
    298 		hb_intr_establish(intlevel, INTST1_SCC, IPL_SERIAL,
    299 		    zshard_hb, NULL);
    300 	}
    301 	/* XXX; evcnt_attach() ? */
    302 
    303 	/*
    304 	 * Set the master interrupt enable and interrupt vector.
    305 	 * (common to both channels, do it on A)
    306 	 */
    307 	cs = zsc->zsc_cs[0];
    308 	s = splhigh();
    309 	/* interrupt vector */
    310 	zs_write_reg(cs, 2, zs_init_reg[2]);
    311 	/* master interrupt control (enable) */
    312 	zs_write_reg(cs, 9, zs_init_reg[9]);
    313 	splx(s);
    314 }
    315 
    316 static int
    317 zshard_hb(void *arg)
    318 {
    319 	int rv;
    320 
    321 	(void) *(volatile u_char *)SCCVECT;
    322 	rv = zshard(arg);
    323 
    324 	/* XXX news3400 sometimes losts zs interrupt */
    325 	if (rv)
    326 		zshard(arg);
    327 
    328 	return rv;
    329 }
    330 
    331 /*
    332  * Polled input char.
    333  */
    334 int
    335 zs_getc(void *arg)
    336 {
    337 	volatile struct zschan *zc = arg;
    338 	int s, c, rr0;
    339 
    340 	s = splhigh();
    341 	/* Wait for a character to arrive. */
    342 	do {
    343 		rr0 = zc->zc_csr;
    344 		ZS_DELAY();
    345 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    346 
    347 	c = zc->zc_data;
    348 	ZS_DELAY();
    349 	splx(s);
    350 
    351 	/*
    352 	 * This is used by the kd driver to read scan codes,
    353 	 * so don't translate '\r' ==> '\n' here...
    354 	 */
    355 	return c;
    356 }
    357 
    358 /*
    359  * Polled output char.
    360  */
    361 void
    362 zs_putc(void *arg, int c)
    363 {
    364 	volatile struct zschan *zc = arg;
    365 	int s, rr0;
    366 
    367 	s = splhigh();
    368 	/* Wait for transmitter to become ready. */
    369 	do {
    370 		rr0 = zc->zc_csr;
    371 		ZS_DELAY();
    372 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    373 
    374 	zc->zc_data = c;
    375 	ZS_DELAY();
    376 	splx(s);
    377 }
    378 
    379 /*****************************************************************/
    380 
    381 static void zscnprobe(struct consdev *);
    382 static void zscninit(struct consdev *);
    383 static int  zscngetc(dev_t);
    384 static void zscnputc(dev_t, int);
    385 
    386 struct consdev consdev_zs = {
    387 	zscnprobe,
    388 	zscninit,
    389 	zscngetc,
    390 	zscnputc,
    391 	nullcnpollc,
    392 	NULL,
    393 	NULL,
    394 	NULL,
    395 	NODEV,
    396 	CN_DEAD
    397 };
    398 
    399 static void
    400 zscnprobe(struct consdev *cn)
    401 {
    402 }
    403 
    404 static void
    405 zscninit(struct consdev *cn)
    406 {
    407 	extern const struct cdevsw zstty_cdevsw;
    408 
    409 	cn->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), 0);
    410 	cn->cn_pri = CN_REMOTE;
    411 	zs_hwflags[0][0] = ZS_HWFLAG_CONSOLE;
    412 }
    413 
    414 static int
    415 zscngetc(dev_t dev)
    416 {
    417 
    418 	return zs_getc((void *)SCCPORT0A);
    419 }
    420 
    421 static void
    422 zscnputc(dev_t dev, int c)
    423 {
    424 
    425 	zs_putc((void *)SCCPORT0A, c);
    426 }
    427