Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.1.18.2
      1  1.1.18.2  yamt /*	$NetBSD: zs.c,v 1.1.18.2 2006/06/21 14:51:09 yamt Exp $	*/
      2  1.1.18.2  yamt 
      3  1.1.18.2  yamt /*-
      4  1.1.18.2  yamt  * Copyright (c) 1996, 2005 The NetBSD Foundation, Inc.
      5  1.1.18.2  yamt  * All rights reserved.
      6  1.1.18.2  yamt  *
      7  1.1.18.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.18.2  yamt  * by Gordon W. Ross.
      9  1.1.18.2  yamt  *
     10  1.1.18.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.1.18.2  yamt  * modification, are permitted provided that the following conditions
     12  1.1.18.2  yamt  * are met:
     13  1.1.18.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.1.18.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.1.18.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.18.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.18.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.1.18.2  yamt  * 3. All advertising materials mentioning features or use of this software
     19  1.1.18.2  yamt  *    must display the following acknowledgement:
     20  1.1.18.2  yamt  *        This product includes software developed by the NetBSD
     21  1.1.18.2  yamt  *        Foundation, Inc. and its contributors.
     22  1.1.18.2  yamt  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1.18.2  yamt  *    contributors may be used to endorse or promote products derived
     24  1.1.18.2  yamt  *    from this software without specific prior written permission.
     25  1.1.18.2  yamt  *
     26  1.1.18.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1.18.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1.18.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1.18.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1.18.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1.18.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1.18.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1.18.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1.18.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1.18.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1.18.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1.18.2  yamt  */
     38  1.1.18.2  yamt 
     39  1.1.18.2  yamt /*
     40  1.1.18.2  yamt  * Zilog Z8530 Dual UART driver (machine-dependent part)
     41  1.1.18.2  yamt  *
     42  1.1.18.2  yamt  * Runs two serial lines per chip using slave drivers.
     43  1.1.18.2  yamt  * Plain tty/async lines use the zs_async slave.
     44  1.1.18.2  yamt  */
     45  1.1.18.2  yamt 
     46  1.1.18.2  yamt #include <sys/cdefs.h>
     47  1.1.18.2  yamt __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.1.18.2 2006/06/21 14:51:09 yamt Exp $");
     48  1.1.18.2  yamt 
     49  1.1.18.2  yamt #include "opt_ddb.h"
     50  1.1.18.2  yamt 
     51  1.1.18.2  yamt #include <sys/param.h>
     52  1.1.18.2  yamt #include <sys/device.h>
     53  1.1.18.2  yamt #include <sys/tty.h>
     54  1.1.18.2  yamt #include <sys/systm.h>
     55  1.1.18.2  yamt 
     56  1.1.18.2  yamt #include <machine/z8530var.h>
     57  1.1.18.2  yamt #include <dev/ic/z8530reg.h>
     58  1.1.18.2  yamt 
     59  1.1.18.2  yamt #include "ioconf.h"
     60  1.1.18.2  yamt 
     61  1.1.18.2  yamt /* console status for consinit() */
     62  1.1.18.2  yamt static struct zs_chanstate zs_conscs_store;
     63  1.1.18.2  yamt struct zs_chanstate *zs_conscs = &zs_conscs_store;
     64  1.1.18.2  yamt void *zs_consaddr;
     65  1.1.18.2  yamt 
     66  1.1.18.2  yamt /*
     67  1.1.18.2  yamt  * Some warts needed by z8530tty.c -
     68  1.1.18.2  yamt  * The default parity REALLY needs to be the same as the PROM uses,
     69  1.1.18.2  yamt  * or you can not see messages done with printf during boot-up...
     70  1.1.18.2  yamt  */
     71  1.1.18.2  yamt int zs_def_cflag = (CREAD | CS8 | HUPCL);
     72  1.1.18.2  yamt 
     73  1.1.18.2  yamt int
     74  1.1.18.2  yamt zs_print(void *aux, const char *name)
     75  1.1.18.2  yamt {
     76  1.1.18.2  yamt 	struct zsc_attach_args *args = aux;
     77  1.1.18.2  yamt 
     78  1.1.18.2  yamt 	if (name != NULL)
     79  1.1.18.2  yamt 		aprint_normal("%s: ", name);
     80  1.1.18.2  yamt 
     81  1.1.18.2  yamt 	if (args->channel != -1)
     82  1.1.18.2  yamt 		aprint_normal(" channel %d", args->channel);
     83  1.1.18.2  yamt 
     84  1.1.18.2  yamt 	return UNCONF;
     85  1.1.18.2  yamt }
     86  1.1.18.2  yamt 
     87  1.1.18.2  yamt /*
     88  1.1.18.2  yamt  * Our ZS chips all share a common, autovectored interrupt,
     89  1.1.18.2  yamt  * so we have to look at all of them on each interrupt.
     90  1.1.18.2  yamt  */
     91  1.1.18.2  yamt int
     92  1.1.18.2  yamt zshard(void *arg)
     93  1.1.18.2  yamt {
     94  1.1.18.2  yamt 	struct zsc_softc *zsc;
     95  1.1.18.2  yamt 	int unit, rval, softreq;
     96  1.1.18.2  yamt 
     97  1.1.18.2  yamt 	rval = 0;
     98  1.1.18.2  yamt 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
     99  1.1.18.2  yamt 		zsc = zsc_cd.cd_devs[unit];
    100  1.1.18.2  yamt 		if (zsc == NULL)
    101  1.1.18.2  yamt 			continue;
    102  1.1.18.2  yamt 		rval |= zsc_intr_hard(zsc);
    103  1.1.18.2  yamt 		softreq =  zsc->zsc_cs[0]->cs_softreq;
    104  1.1.18.2  yamt 		softreq |= zsc->zsc_cs[1]->cs_softreq;
    105  1.1.18.2  yamt 		if (softreq)
    106  1.1.18.2  yamt 			softintr_schedule(zsc->zsc_si);
    107  1.1.18.2  yamt 	}
    108  1.1.18.2  yamt 
    109  1.1.18.2  yamt 	return rval;
    110  1.1.18.2  yamt }
    111  1.1.18.2  yamt 
    112  1.1.18.2  yamt /*
    113  1.1.18.2  yamt  * Similar scheme as for zshard (look at all of them)
    114  1.1.18.2  yamt  */
    115  1.1.18.2  yamt void
    116  1.1.18.2  yamt zssoft(void *arg)
    117  1.1.18.2  yamt {
    118  1.1.18.2  yamt 	struct zsc_softc *zsc;
    119  1.1.18.2  yamt 	int s, unit;
    120  1.1.18.2  yamt 
    121  1.1.18.2  yamt 	/* Make sure we call the tty layer at spltty. */
    122  1.1.18.2  yamt 	s = spltty();
    123  1.1.18.2  yamt 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    124  1.1.18.2  yamt 		zsc = zsc_cd.cd_devs[unit];
    125  1.1.18.2  yamt 		if (zsc == NULL)
    126  1.1.18.2  yamt 			continue;
    127  1.1.18.2  yamt 		(void)zsc_intr_soft(zsc);
    128  1.1.18.2  yamt 	}
    129  1.1.18.2  yamt 	splx(s);
    130  1.1.18.2  yamt }
    131  1.1.18.2  yamt 
    132  1.1.18.2  yamt /*
    133  1.1.18.2  yamt  * Compute the current baud rate given a ZS channel.
    134  1.1.18.2  yamt  */
    135  1.1.18.2  yamt int
    136  1.1.18.2  yamt zs_get_speed(struct zs_chanstate *cs)
    137  1.1.18.2  yamt {
    138  1.1.18.2  yamt 	int tconst;
    139  1.1.18.2  yamt 
    140  1.1.18.2  yamt 	tconst = zs_read_reg(cs, 12);
    141  1.1.18.2  yamt 	tconst |= zs_read_reg(cs, 13) << 8;
    142  1.1.18.2  yamt 	return TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    143  1.1.18.2  yamt }
    144  1.1.18.2  yamt 
    145  1.1.18.2  yamt /*
    146  1.1.18.2  yamt  * MD functions for setting the baud rate and control modes.
    147  1.1.18.2  yamt  */
    148  1.1.18.2  yamt int
    149  1.1.18.2  yamt zs_set_speed(struct zs_chanstate *cs, int bps)
    150  1.1.18.2  yamt {
    151  1.1.18.2  yamt 	int tconst, real_bps;
    152  1.1.18.2  yamt 
    153  1.1.18.2  yamt 	if (bps == 0)
    154  1.1.18.2  yamt 		return 0;
    155  1.1.18.2  yamt 
    156  1.1.18.2  yamt #ifdef	DIAGNOSTIC
    157  1.1.18.2  yamt 	if (cs->cs_brg_clk == 0)
    158  1.1.18.2  yamt 		panic("zs_set_speed");
    159  1.1.18.2  yamt #endif
    160  1.1.18.2  yamt 
    161  1.1.18.2  yamt 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    162  1.1.18.2  yamt 	if (tconst < 0)
    163  1.1.18.2  yamt 		return EINVAL;
    164  1.1.18.2  yamt 
    165  1.1.18.2  yamt 	/* Convert back to make sure we can do it. */
    166  1.1.18.2  yamt 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    167  1.1.18.2  yamt 
    168  1.1.18.2  yamt 	/* XXX - Allow some tolerance here? */
    169  1.1.18.2  yamt 	if (real_bps != bps)
    170  1.1.18.2  yamt 		return EINVAL;
    171  1.1.18.2  yamt 
    172  1.1.18.2  yamt 	cs->cs_preg[12] = tconst;
    173  1.1.18.2  yamt 	cs->cs_preg[13] = tconst >> 8;
    174  1.1.18.2  yamt 
    175  1.1.18.2  yamt 	/* Caller will stuff the pending registers. */
    176  1.1.18.2  yamt 	return 0;
    177  1.1.18.2  yamt }
    178  1.1.18.2  yamt 
    179  1.1.18.2  yamt int
    180  1.1.18.2  yamt zs_set_modes(struct zs_chanstate *cs, int cflag)
    181  1.1.18.2  yamt {
    182  1.1.18.2  yamt 	int s;
    183  1.1.18.2  yamt 
    184  1.1.18.2  yamt 	/*
    185  1.1.18.2  yamt 	 * Output hardware flow control on the chip is horrendous:
    186  1.1.18.2  yamt 	 * if carrier detect drops, the receiver is disabled, and if
    187  1.1.18.2  yamt 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    188  1.1.18.2  yamt 	 * Therefore, NEVER set the HFC bit, and instead use the
    189  1.1.18.2  yamt 	 * status interrupt to detect CTS changes.
    190  1.1.18.2  yamt 	 */
    191  1.1.18.2  yamt 	s = splserial();
    192  1.1.18.2  yamt 	cs->cs_rr0_pps = 0;
    193  1.1.18.2  yamt 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    194  1.1.18.2  yamt 		cs->cs_rr0_dcd = 0;
    195  1.1.18.2  yamt 		if ((cflag & MDMBUF) == 0)
    196  1.1.18.2  yamt 			cs->cs_rr0_pps = ZSRR0_DCD;
    197  1.1.18.2  yamt 	} else
    198  1.1.18.2  yamt 		cs->cs_rr0_dcd = ZSRR0_DCD;
    199  1.1.18.2  yamt 	if ((cflag & CRTSCTS) != 0) {
    200  1.1.18.2  yamt 		cs->cs_wr5_dtr = ZSWR5_DTR;
    201  1.1.18.2  yamt 		cs->cs_wr5_rts = ZSWR5_RTS;
    202  1.1.18.2  yamt 		cs->cs_rr0_cts = ZSRR0_CTS;
    203  1.1.18.2  yamt 	} else if ((cflag & MDMBUF) != 0) {
    204  1.1.18.2  yamt 		cs->cs_wr5_dtr = 0;
    205  1.1.18.2  yamt 		cs->cs_wr5_rts = ZSWR5_DTR;
    206  1.1.18.2  yamt 		cs->cs_rr0_cts = ZSRR0_DCD;
    207  1.1.18.2  yamt 	} else {
    208  1.1.18.2  yamt 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    209  1.1.18.2  yamt 		cs->cs_wr5_rts = 0;
    210  1.1.18.2  yamt 		cs->cs_rr0_cts = 0;
    211  1.1.18.2  yamt 	}
    212  1.1.18.2  yamt 	splx(s);
    213  1.1.18.2  yamt 
    214  1.1.18.2  yamt 	/* Caller will stuff the pending registers. */
    215  1.1.18.2  yamt 	return 0;
    216  1.1.18.2  yamt }
    217  1.1.18.2  yamt 
    218  1.1.18.2  yamt /*
    219  1.1.18.2  yamt  * Read or write the chip with suitable delays.
    220  1.1.18.2  yamt  */
    221  1.1.18.2  yamt uint8_t
    222  1.1.18.2  yamt zs_read_reg(struct zs_chanstate *cs, uint8_t reg)
    223  1.1.18.2  yamt {
    224  1.1.18.2  yamt 	uint8_t val;
    225  1.1.18.2  yamt 
    226  1.1.18.2  yamt 	*cs->cs_reg_csr = reg;
    227  1.1.18.2  yamt 
    228  1.1.18.2  yamt 	val = *cs->cs_reg_csr;
    229  1.1.18.2  yamt 
    230  1.1.18.2  yamt 	return val;
    231  1.1.18.2  yamt }
    232  1.1.18.2  yamt 
    233  1.1.18.2  yamt void
    234  1.1.18.2  yamt zs_write_reg(struct zs_chanstate *cs, uint8_t reg, uint8_t val)
    235  1.1.18.2  yamt {
    236  1.1.18.2  yamt 
    237  1.1.18.2  yamt 	*cs->cs_reg_csr = reg;
    238  1.1.18.2  yamt 
    239  1.1.18.2  yamt 	*cs->cs_reg_csr = val;
    240  1.1.18.2  yamt 
    241  1.1.18.2  yamt }
    242  1.1.18.2  yamt 
    243  1.1.18.2  yamt uint8_t
    244  1.1.18.2  yamt zs_read_csr(struct zs_chanstate *cs)
    245  1.1.18.2  yamt {
    246  1.1.18.2  yamt 	uint8_t val;
    247  1.1.18.2  yamt 
    248  1.1.18.2  yamt 	val = *cs->cs_reg_csr;
    249  1.1.18.2  yamt 
    250  1.1.18.2  yamt 	return val;
    251  1.1.18.2  yamt }
    252  1.1.18.2  yamt 
    253  1.1.18.2  yamt void
    254  1.1.18.2  yamt zs_write_csr(struct zs_chanstate *cs, uint8_t val)
    255  1.1.18.2  yamt {
    256  1.1.18.2  yamt 
    257  1.1.18.2  yamt 	*cs->cs_reg_csr = val;
    258  1.1.18.2  yamt 
    259  1.1.18.2  yamt }
    260  1.1.18.2  yamt 
    261  1.1.18.2  yamt uint8_t
    262  1.1.18.2  yamt zs_read_data(struct zs_chanstate *cs)
    263  1.1.18.2  yamt {
    264  1.1.18.2  yamt 	uint8_t val;
    265  1.1.18.2  yamt 
    266  1.1.18.2  yamt 	val = *cs->cs_reg_data;
    267  1.1.18.2  yamt 
    268  1.1.18.2  yamt 	return val;
    269  1.1.18.2  yamt }
    270  1.1.18.2  yamt 
    271  1.1.18.2  yamt void
    272  1.1.18.2  yamt zs_write_data(struct zs_chanstate *cs, uint8_t val)
    273  1.1.18.2  yamt {
    274  1.1.18.2  yamt 
    275  1.1.18.2  yamt 	*cs->cs_reg_data = val;
    276  1.1.18.2  yamt }
    277  1.1.18.2  yamt 
    278  1.1.18.2  yamt void
    279  1.1.18.2  yamt zs_abort(struct zs_chanstate *cs)
    280  1.1.18.2  yamt {
    281  1.1.18.2  yamt 
    282  1.1.18.2  yamt #ifdef DDB
    283  1.1.18.2  yamt 	Debugger();
    284  1.1.18.2  yamt #endif
    285  1.1.18.2  yamt }
    286  1.1.18.2  yamt 
    287  1.1.18.2  yamt /*
    288  1.1.18.2  yamt  * Polled input char.
    289  1.1.18.2  yamt  */
    290  1.1.18.2  yamt int
    291  1.1.18.2  yamt zs_getc(void *arg)
    292  1.1.18.2  yamt {
    293  1.1.18.2  yamt 	struct zs_chanstate *cs = arg;
    294  1.1.18.2  yamt 	int s, c, rr0;
    295  1.1.18.2  yamt 
    296  1.1.18.2  yamt 	s = splhigh();
    297  1.1.18.2  yamt 	/* Wait for a character to arrive. */
    298  1.1.18.2  yamt 	do {
    299  1.1.18.2  yamt 		rr0 = *cs->cs_reg_csr;
    300  1.1.18.2  yamt 		ZS_DELAY();
    301  1.1.18.2  yamt 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    302  1.1.18.2  yamt 
    303  1.1.18.2  yamt 	c = *cs->cs_reg_data;
    304  1.1.18.2  yamt 	ZS_DELAY();
    305  1.1.18.2  yamt 	splx(s);
    306  1.1.18.2  yamt 
    307  1.1.18.2  yamt 	/*
    308  1.1.18.2  yamt 	 * This could be used by the kd driver to read scan codes,
    309  1.1.18.2  yamt 	 * so don't translate '\r' ==> '\n' here...
    310  1.1.18.2  yamt 	 */
    311  1.1.18.2  yamt 	return c;
    312  1.1.18.2  yamt }
    313  1.1.18.2  yamt 
    314  1.1.18.2  yamt /*
    315  1.1.18.2  yamt  * Polled output char.
    316  1.1.18.2  yamt  */
    317  1.1.18.2  yamt void
    318  1.1.18.2  yamt zs_putc(void *arg, int c)
    319  1.1.18.2  yamt {
    320  1.1.18.2  yamt 	struct zs_chanstate *cs = arg;
    321  1.1.18.2  yamt 	int s, rr0;
    322  1.1.18.2  yamt 
    323  1.1.18.2  yamt 	s = splhigh();
    324  1.1.18.2  yamt 	/* Wait for transmitter to become ready. */
    325  1.1.18.2  yamt 	do {
    326  1.1.18.2  yamt 		rr0 = *cs->cs_reg_csr;
    327  1.1.18.2  yamt 		ZS_DELAY();
    328  1.1.18.2  yamt 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    329  1.1.18.2  yamt 
    330  1.1.18.2  yamt 	*cs->cs_reg_data = c;
    331  1.1.18.2  yamt 	ZS_DELAY();
    332  1.1.18.2  yamt 	splx(s);
    333  1.1.18.2  yamt }
    334  1.1.18.2  yamt 
    335  1.1.18.2  yamt int
    336  1.1.18.2  yamt zscngetc(dev_t dev)
    337  1.1.18.2  yamt {
    338  1.1.18.2  yamt 
    339  1.1.18.2  yamt 	return zs_getc((void *)zs_conscs);
    340  1.1.18.2  yamt }
    341  1.1.18.2  yamt 
    342  1.1.18.2  yamt void
    343  1.1.18.2  yamt zscnputc(dev_t dev, int c)
    344  1.1.18.2  yamt {
    345  1.1.18.2  yamt 
    346  1.1.18.2  yamt 	zs_putc((void *)zs_conscs, c);
    347  1.1.18.2  yamt }
    348