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