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