Home | History | Annotate | Line # | Download | only in sbd
zs_sbdio.c revision 1.9
      1 /*	$NetBSD: zs_sbdio.c,v 1.9 2008/03/29 19:15:34 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 2005 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_sbdio.c,v 1.9 2008/03/29 19:15:34 tsutsui Exp $");
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/device.h>
     52 #include <sys/tty.h>
     53 #include <sys/conf.h>
     54 #include <sys/intr.h>
     55 
     56 #include <dev/cons.h>
     57 #include <dev/ic/z8530reg.h>
     58 
     59 #include <machine/sbdiovar.h>
     60 #include <machine/z8530var.h>
     61 
     62 #define ZS_DEFSPEED	9600
     63 #define PCLK		(9600 * 512)		/* 4.915200MHz */
     64 
     65 /* The layout of this is hardware-dependent (padding, order). */
     66 struct zschan {
     67 	volatile uint8_t zc_csr;	/* ctrl, status, and indirect access */
     68 	uint8_t padding1[3];
     69 	volatile uint8_t zc_data;	/* data */
     70 	uint8_t padding2[3];
     71 } __attribute__((__packed__));
     72 
     73 struct zsdevice {
     74 	/* Yes, they are backwards. */
     75 	struct	zschan zs_chan_b;
     76 	struct	zschan zs_chan_a;
     77 } __attribute__((__packed__));
     78 
     79 static uint8_t zs_init_reg[16] = {
     80 	0,				/*  0: CMD (reset, etc.) */
     81 	0,				/*  1: No interrupts yet. */
     82 	0,				/*  2: IVECT EWS-UX don't set this. */
     83 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
     84 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
     85 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
     86 	0,				/*  6: TXSYNC/SYNCLO */
     87 	0,				/*  7: RXSYNC/SYNCHI */
     88 	0,				/*  8: alias for data port */
     89 	ZSWR9_MASTER_IE,
     90 	0,				/* 10: Misc. TX/RX control bits */
     91 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
     92 	BPS_TO_TCONST((PCLK/16), ZS_DEFSPEED), /* 12: BAUDLO (default=9600) */
     93 	0,				/*13: BAUDHI (default=9600) */
     94 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
     95 	ZSWR15_BREAK_IE,
     96 };
     97 
     98 static int zs_sbdio_match(device_t, cfdata_t, void *);
     99 static void zs_sbdio_attach(device_t, device_t, void *);
    100 
    101 CFATTACH_DECL_NEW(zsc_sbdio, sizeof(struct zsc_softc),
    102     zs_sbdio_match, zs_sbdio_attach, NULL, NULL);
    103 
    104 int
    105 zs_sbdio_match(device_t parent, cfdata_t cf, void *aux)
    106 {
    107 	struct sbdio_attach_args *sa = aux;
    108 
    109 	return strcmp(sa->sa_name, "zsc") ? 0 : 1;
    110 }
    111 
    112 void
    113 zs_sbdio_attach(device_t parent, device_t self, void *aux)
    114 {
    115 	struct zsc_softc *zsc = device_private(self);
    116 	struct sbdio_attach_args *sa = aux;
    117 	struct zsc_attach_args zsc_args;
    118 	struct zschan *zc;
    119 	struct zs_chanstate *cs;
    120 	struct zsdevice *zs_addr;
    121 	int s, zs_unit, channel;
    122 
    123 	zsc->zsc_dev = self;
    124 	aprint_normal("\n");
    125 
    126 	zs_unit = device_unit(self);
    127 	zs_addr = (void *)MIPS_PHYS_TO_KSEG1(sa->sa_addr1);
    128 	zsc->zsc_flags = sa->sa_flags;
    129 
    130 	/*
    131 	 * Initialize software state for each channel.
    132 	 */
    133 	for (channel = 0; channel < 2; channel++) {
    134 		zsc_args.channel = channel;
    135 		zsc_args.hwflags = 0;
    136 		cs = &zsc->zsc_cs_store[channel];
    137 		zsc->zsc_cs[channel] = cs;
    138 
    139 		cs->cs_channel = channel;
    140 		cs->cs_private = NULL;
    141 		cs->cs_ops = &zsops_null;
    142 
    143 		if (channel == 0)
    144 			zc = &zs_addr->zs_chan_a;
    145 		else
    146 			zc = &zs_addr->zs_chan_b;
    147 
    148 		if (zc == zs_consaddr) {
    149 			memcpy(cs, zs_conscs, sizeof(struct zs_chanstate));
    150 			zs_conscs = cs;
    151 			zsc_args.hwflags = ZS_HWFLAG_CONSOLE;
    152 		} else {
    153 			cs->cs_reg_csr  = &zc->zc_csr;
    154 			cs->cs_reg_data = &zc->zc_data;
    155 			memcpy(cs->cs_creg, zs_init_reg, 16);
    156 			memcpy(cs->cs_preg, zs_init_reg, 16);
    157 			cs->cs_defspeed = ZS_DEFSPEED;
    158 			zsc_args.hwflags = 0;
    159 		}
    160 
    161 		zs_lock_init(cs);
    162 		cs->cs_brg_clk = PCLK / 16;
    163 		cs->cs_defcflag = zs_def_cflag;
    164 
    165 		/* Make these correspond to cs_defcflag (-crtscts) */
    166 		cs->cs_rr0_dcd = ZSRR0_DCD;
    167 		cs->cs_rr0_cts = 0;
    168 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    169 		cs->cs_wr5_rts = 0;
    170 
    171 		/*
    172 		 * Clear the master interrupt enable.
    173 		 * The INTENA is common to both channels,
    174 		 * so just do it on the A channel.
    175 		 */
    176 		if (channel == 0) {
    177 			zs_write_reg(cs, 9, 0);
    178 		}
    179 
    180 		/*
    181 		 * Look for a child driver for this channel.
    182 		 * The child attach will setup the hardware.
    183 		 */
    184 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
    185 			/* No sub-driver.  Just reset it. */
    186 			uint8_t reset = (channel == 0) ?
    187 			    ZSWR9_A_RESET : ZSWR9_B_RESET;
    188 			s = splhigh();
    189 			zs_write_reg(cs, 9, reset);
    190 			splx(s);
    191 		}
    192 	}
    193 
    194 	zsc->zsc_si = softint_establish(SOFTINT_SERIAL,
    195 	    (void (*)(void *))zsc_intr_soft, zsc);
    196 	intr_establish(sa->sa_irq, zshard, zsc);
    197 
    198 	/*
    199 	 * Set the master interrupt enable and interrupt vector.
    200 	 * (common to both channels, do it on A)
    201 	 */
    202 	cs = zsc->zsc_cs[0];
    203 	s = splhigh();
    204 	/* interrupt vector */
    205 	zs_write_reg(cs, 2, zs_init_reg[2]);
    206 	/* master interrupt control (enable) */
    207 	zs_write_reg(cs, 9, zs_init_reg[9]);
    208 	splx(s);
    209 }
    210 
    211 /*
    212  * console stuff
    213  */
    214 
    215 static void zs_sbdio_cnprobe(struct consdev *);
    216 static void zs_sbdio_cninit(struct consdev *);
    217 
    218 struct consdev consdev_zs_sbdio = {
    219 	zs_sbdio_cnprobe,
    220 	zs_sbdio_cninit,
    221 	zscngetc,
    222 	zscnputc,
    223 	nullcnpollc,
    224 	NULL,
    225 	NULL,
    226 	NULL,
    227 	NODEV,
    228 	CN_DEAD
    229 };
    230 
    231 static void
    232 zs_sbdio_cnprobe(struct consdev *cn)
    233 {
    234 
    235 	/* not used */
    236 }
    237 
    238 static void
    239 zs_sbdio_cninit(struct consdev *cn)
    240 {
    241 	struct zs_chanstate *cs;
    242 	struct zschan *zc;
    243 
    244 	zc = zs_consaddr;
    245 	cs = zs_conscs;
    246 
    247 	/* Setup temporary chanstate. */
    248 	cs->cs_reg_csr  = &zc->zc_csr;
    249 	cs->cs_reg_data = &zc->zc_data;
    250 
    251 	/* Initialize the pending registers. */
    252 	memcpy(cs->cs_preg, zs_init_reg, 16);
    253 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    254 
    255 	cs->cs_brg_clk = PCLK / 16;
    256 	cs->cs_defspeed = ZS_DEFSPEED;
    257 	zs_set_speed(cs, ZS_DEFSPEED);
    258 
    259 	/* Clear the master interrupt enable. */
    260 	zs_write_reg(cs, 9, 0);
    261 
    262 	/* Reset the whole SCC chip. */
    263 	zs_write_reg(cs, 9, ZSWR9_HARD_RESET);
    264 
    265 	/* Copy "pending" to "current" and H/W */
    266 	zs_loadchannelregs(cs);
    267 }
    268