Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.25.16.1
      1  1.25.16.1  uebayasi /*	$NetBSD: zs.c,v 1.25.16.1 2010/08/17 06:44:58 uebayasi Exp $	*/
      2        1.1    tsubai 
      3        1.1    tsubai /*-
      4        1.1    tsubai  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5        1.1    tsubai  * All rights reserved.
      6        1.1    tsubai  *
      7        1.1    tsubai  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1    tsubai  * by Gordon W. Ross.
      9        1.1    tsubai  *
     10        1.1    tsubai  * Redistribution and use in source and binary forms, with or without
     11        1.1    tsubai  * modification, are permitted provided that the following conditions
     12        1.1    tsubai  * are met:
     13        1.1    tsubai  * 1. Redistributions of source code must retain the above copyright
     14        1.1    tsubai  *    notice, this list of conditions and the following disclaimer.
     15        1.1    tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1    tsubai  *    notice, this list of conditions and the following disclaimer in the
     17        1.1    tsubai  *    documentation and/or other materials provided with the distribution.
     18        1.1    tsubai  *
     19        1.1    tsubai  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1    tsubai  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1    tsubai  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1    tsubai  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1    tsubai  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1    tsubai  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1    tsubai  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1    tsubai  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1    tsubai  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1    tsubai  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1    tsubai  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1    tsubai  */
     31        1.1    tsubai 
     32        1.1    tsubai /*
     33        1.1    tsubai  * Zilog Z8530 Dual UART driver (machine-dependent part)
     34        1.1    tsubai  *
     35        1.1    tsubai  * Runs two serial lines per chip using slave drivers.
     36        1.1    tsubai  * Plain tty/async lines use the zs_async slave.
     37        1.1    tsubai  * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
     38        1.1    tsubai  */
     39       1.19     lukem 
     40       1.19     lukem #include <sys/cdefs.h>
     41  1.25.16.1  uebayasi __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.25.16.1 2010/08/17 06:44:58 uebayasi Exp $");
     42        1.1    tsubai 
     43        1.3    tsubai #include "opt_ddb.h"
     44        1.3    tsubai 
     45        1.1    tsubai #include <sys/param.h>
     46        1.1    tsubai #include <sys/device.h>
     47        1.1    tsubai #include <sys/tty.h>
     48       1.10    tsubai #include <sys/systm.h>
     49       1.22        ad #include <sys/cpu.h>
     50       1.22        ad #include <sys/intr.h>
     51        1.1    tsubai 
     52       1.10    tsubai #include <machine/adrsmap.h>
     53        1.1    tsubai #include <machine/z8530var.h>
     54        1.1    tsubai 
     55        1.1    tsubai #include <dev/ic/z8530reg.h>
     56        1.1    tsubai 
     57       1.23   tsutsui #include "ioconf.h"
     58       1.23   tsutsui 
     59       1.10    tsubai #define ZS_DELAY() (*zs_delay)()
     60        1.1    tsubai 
     61        1.1    tsubai /*
     62        1.1    tsubai  * Some warts needed by z8530tty.c -
     63        1.1    tsubai  * The default parity REALLY needs to be the same as the PROM uses,
     64        1.1    tsubai  * or you can not see messages done with printf during boot-up...
     65        1.1    tsubai  */
     66        1.1    tsubai int zs_def_cflag = (CREAD | CS8 | HUPCL);
     67        1.1    tsubai 
     68       1.10    tsubai int
     69       1.20   tsutsui zs_print(void *aux, const char *name)
     70        1.1    tsubai {
     71        1.1    tsubai 	struct zsc_attach_args *args = aux;
     72        1.1    tsubai 
     73        1.1    tsubai 	if (name != NULL)
     74       1.14   thorpej 		aprint_normal("%s: ", name);
     75        1.1    tsubai 
     76        1.1    tsubai 	if (args->channel != -1)
     77       1.14   thorpej 		aprint_normal(" channel %d", args->channel);
     78        1.1    tsubai 
     79        1.1    tsubai 	return UNCONF;
     80       1.11    tsubai }
     81       1.11    tsubai 
     82       1.11    tsubai /*
     83  1.25.16.1  uebayasi  * Our ZS chips all share a common interrupt level,
     84  1.25.16.1  uebayasi  * but we establish zshard handler per each ZS chips
     85  1.25.16.1  uebayasi  * to avoid holding unnecessary locks in interrupt context.
     86       1.11    tsubai  */
     87       1.11    tsubai int
     88       1.20   tsutsui zshard(void *arg)
     89       1.11    tsubai {
     90  1.25.16.1  uebayasi 	struct zsc_softc *zsc = arg;
     91  1.25.16.1  uebayasi 	int rval;
     92       1.11    tsubai 
     93  1.25.16.1  uebayasi 	rval = zsc_intr_hard(zsc);
     94  1.25.16.1  uebayasi 	if (zsc->zsc_cs[0]->cs_softreq || zsc->zsc_cs[1]->cs_softreq)
     95  1.25.16.1  uebayasi 		softint_schedule(zsc->zsc_si);
     96       1.11    tsubai 
     97       1.11    tsubai 	return rval;
     98       1.11    tsubai }
     99       1.11    tsubai 
    100       1.11    tsubai /*
    101        1.1    tsubai  * Compute the current baud rate given a ZS channel.
    102        1.1    tsubai  */
    103       1.10    tsubai int
    104       1.20   tsutsui zs_get_speed(struct zs_chanstate *cs)
    105        1.1    tsubai {
    106        1.1    tsubai 	int tconst;
    107        1.1    tsubai 
    108        1.1    tsubai 	tconst = zs_read_reg(cs, 12);
    109        1.1    tsubai 	tconst |= zs_read_reg(cs, 13) << 8;
    110       1.20   tsutsui 	return TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    111        1.1    tsubai }
    112        1.1    tsubai 
    113        1.1    tsubai /*
    114        1.1    tsubai  * MD functions for setting the baud rate and control modes.
    115        1.1    tsubai  */
    116        1.1    tsubai int
    117       1.20   tsutsui zs_set_speed(struct zs_chanstate *cs, int bps)
    118        1.1    tsubai {
    119        1.1    tsubai 	int tconst, real_bps;
    120        1.1    tsubai 
    121        1.1    tsubai 	if (bps == 0)
    122       1.20   tsutsui 		return 0;
    123        1.1    tsubai 
    124        1.1    tsubai #ifdef	DIAGNOSTIC
    125        1.1    tsubai 	if (cs->cs_brg_clk == 0)
    126        1.1    tsubai 		panic("zs_set_speed");
    127        1.1    tsubai #endif
    128        1.1    tsubai 
    129        1.1    tsubai 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    130        1.1    tsubai 	if (tconst < 0)
    131       1.20   tsutsui 		return EINVAL;
    132        1.1    tsubai 
    133        1.1    tsubai 	/* Convert back to make sure we can do it. */
    134        1.1    tsubai 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    135        1.1    tsubai 
    136        1.1    tsubai 	/* XXX - Allow some tolerance here? */
    137        1.1    tsubai 	if (real_bps != bps)
    138       1.20   tsutsui 		return EINVAL;
    139        1.1    tsubai 
    140        1.1    tsubai 	cs->cs_preg[12] = tconst;
    141        1.1    tsubai 	cs->cs_preg[13] = tconst >> 8;
    142        1.1    tsubai 
    143        1.1    tsubai 	/* Caller will stuff the pending registers. */
    144       1.20   tsutsui 	return 0;
    145        1.1    tsubai }
    146        1.1    tsubai 
    147        1.1    tsubai int
    148       1.20   tsutsui zs_set_modes(struct zs_chanstate *cs, int cflag)
    149        1.1    tsubai {
    150        1.1    tsubai 	int s;
    151        1.1    tsubai 
    152        1.1    tsubai 	/*
    153        1.1    tsubai 	 * Output hardware flow control on the chip is horrendous:
    154        1.1    tsubai 	 * if carrier detect drops, the receiver is disabled, and if
    155        1.1    tsubai 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    156        1.1    tsubai 	 * Therefore, NEVER set the HFC bit, and instead use the
    157        1.1    tsubai 	 * status interrupt to detect CTS changes.
    158        1.1    tsubai 	 */
    159       1.18   tsutsui 	s = splserial();
    160        1.7  wrstuden 	cs->cs_rr0_pps = 0;
    161        1.7  wrstuden 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    162        1.1    tsubai 		cs->cs_rr0_dcd = 0;
    163        1.7  wrstuden 		if ((cflag & MDMBUF) == 0)
    164        1.7  wrstuden 			cs->cs_rr0_pps = ZSRR0_DCD;
    165        1.7  wrstuden 	} else
    166        1.1    tsubai 		cs->cs_rr0_dcd = ZSRR0_DCD;
    167        1.1    tsubai 	if ((cflag & CRTSCTS) != 0) {
    168        1.1    tsubai 		cs->cs_wr5_dtr = ZSWR5_DTR;
    169        1.1    tsubai 		cs->cs_wr5_rts = ZSWR5_RTS;
    170        1.1    tsubai 		cs->cs_rr0_cts = ZSRR0_CTS;
    171        1.1    tsubai 	} else if ((cflag & MDMBUF) != 0) {
    172        1.1    tsubai 		cs->cs_wr5_dtr = 0;
    173        1.1    tsubai 		cs->cs_wr5_rts = ZSWR5_DTR;
    174        1.1    tsubai 		cs->cs_rr0_cts = ZSRR0_DCD;
    175        1.1    tsubai 	} else {
    176        1.1    tsubai 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    177        1.1    tsubai 		cs->cs_wr5_rts = 0;
    178        1.1    tsubai 		cs->cs_rr0_cts = 0;
    179        1.1    tsubai 	}
    180        1.1    tsubai 	splx(s);
    181        1.1    tsubai 
    182        1.1    tsubai 	/* Caller will stuff the pending registers. */
    183       1.20   tsutsui 	return 0;
    184        1.1    tsubai }
    185        1.1    tsubai 
    186        1.1    tsubai /*
    187        1.1    tsubai  * Read or write the chip with suitable delays.
    188        1.1    tsubai  */
    189        1.1    tsubai 
    190       1.23   tsutsui uint8_t
    191       1.23   tsutsui zs_read_reg(struct zs_chanstate *cs, uint8_t reg)
    192        1.1    tsubai {
    193       1.23   tsutsui 	uint8_t val;
    194        1.1    tsubai 
    195        1.1    tsubai 	*cs->cs_reg_csr = reg;
    196        1.1    tsubai 	ZS_DELAY();
    197        1.1    tsubai 	val = *cs->cs_reg_csr;
    198        1.1    tsubai 	ZS_DELAY();
    199        1.1    tsubai 	return val;
    200        1.1    tsubai }
    201        1.1    tsubai 
    202        1.1    tsubai void
    203       1.23   tsutsui zs_write_reg(struct zs_chanstate *cs, uint8_t reg, uint8_t val)
    204        1.1    tsubai {
    205       1.20   tsutsui 
    206        1.1    tsubai 	*cs->cs_reg_csr = reg;
    207        1.1    tsubai 	ZS_DELAY();
    208        1.1    tsubai 	*cs->cs_reg_csr = val;
    209        1.1    tsubai 	ZS_DELAY();
    210        1.1    tsubai }
    211        1.1    tsubai 
    212       1.23   tsutsui uint8_t
    213       1.23   tsutsui zs_read_csr(struct zs_chanstate *cs)
    214        1.1    tsubai {
    215       1.23   tsutsui 	uint8_t val;
    216        1.1    tsubai 
    217        1.1    tsubai 	val = *cs->cs_reg_csr;
    218        1.1    tsubai 	ZS_DELAY();
    219        1.1    tsubai 	return val;
    220        1.1    tsubai }
    221        1.1    tsubai 
    222       1.23   tsutsui void
    223       1.23   tsutsui zs_write_csr(struct zs_chanstate *cs, uint8_t val)
    224        1.1    tsubai {
    225       1.20   tsutsui 
    226        1.1    tsubai 	*cs->cs_reg_csr = val;
    227        1.1    tsubai 	ZS_DELAY();
    228        1.1    tsubai }
    229        1.1    tsubai 
    230       1.23   tsutsui uint8_t
    231       1.23   tsutsui  zs_read_data(struct zs_chanstate *cs)
    232        1.1    tsubai {
    233       1.23   tsutsui 	uint8_t val;
    234        1.1    tsubai 
    235        1.1    tsubai 	val = *cs->cs_reg_data;
    236        1.1    tsubai 	ZS_DELAY();
    237        1.1    tsubai 	return val;
    238        1.1    tsubai }
    239        1.1    tsubai 
    240       1.23   tsutsui void
    241       1.23   tsutsui zs_write_data(struct zs_chanstate *cs, uint8_t val)
    242        1.1    tsubai {
    243       1.20   tsutsui 
    244        1.1    tsubai 	*cs->cs_reg_data = val;
    245        1.1    tsubai 	ZS_DELAY();
    246        1.1    tsubai }
    247        1.1    tsubai 
    248        1.1    tsubai void
    249       1.20   tsutsui zs_abort(struct zs_chanstate *cs)
    250        1.1    tsubai {
    251       1.20   tsutsui 
    252        1.3    tsubai #ifdef DDB
    253        1.2    tsubai 	Debugger();
    254        1.3    tsubai #endif
    255        1.1    tsubai }
    256