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