Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.11.2.2
      1  1.11.2.2    bouyer /*	$NetBSD: zs.c,v 1.11.2.2 2001/04/21 17:54:24 bouyer Exp $	*/
      2       1.1       dbj 
      3       1.1       dbj /*-
      4       1.1       dbj  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5       1.1       dbj  * All rights reserved.
      6       1.1       dbj  *
      7       1.1       dbj  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1       dbj  * by Gordon W. Ross.
      9       1.1       dbj  *
     10       1.1       dbj  * Redistribution and use in source and binary forms, with or without
     11       1.1       dbj  * modification, are permitted provided that the following conditions
     12       1.1       dbj  * are met:
     13       1.1       dbj  * 1. Redistributions of source code must retain the above copyright
     14       1.1       dbj  *    notice, this list of conditions and the following disclaimer.
     15       1.1       dbj  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       dbj  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       dbj  *    documentation and/or other materials provided with the distribution.
     18       1.1       dbj  * 3. All advertising materials mentioning features or use of this software
     19       1.1       dbj  *    must display the following acknowledgement:
     20       1.1       dbj  *        This product includes software developed by the NetBSD
     21       1.1       dbj  *        Foundation, Inc. and its contributors.
     22       1.1       dbj  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1       dbj  *    contributors may be used to endorse or promote products derived
     24       1.1       dbj  *    from this software without specific prior written permission.
     25       1.1       dbj  *
     26       1.1       dbj  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1       dbj  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1       dbj  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1       dbj  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1       dbj  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1       dbj  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1       dbj  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1       dbj  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1       dbj  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1       dbj  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1       dbj  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1       dbj  */
     38       1.1       dbj 
     39       1.1       dbj /*
     40       1.1       dbj  * Zilog Z8530 Dual UART driver (machine-dependent part)
     41       1.1       dbj  *
     42       1.1       dbj  * Runs two serial lines per chip using slave drivers.
     43       1.1       dbj  * Plain tty/async lines use the zs_async slave.
     44       1.1       dbj  * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
     45       1.1       dbj  */
     46       1.1       dbj 
     47       1.1       dbj /* This was snarfed from the netbsd sparc/dev/zs.c at version 1.56
     48       1.1       dbj  * and then updated to reflect changes in 1.59
     49       1.1       dbj  * by Darrin B Jewell <jewell (at) mit.edu>  Mon Mar 30 20:24:46 1998
     50       1.1       dbj  */
     51       1.2  jonathan 
     52       1.2  jonathan #include "opt_ddb.h"
     53      1.11       dbj #include "opt_serial.h"
     54       1.1       dbj 
     55       1.1       dbj #include <sys/param.h>
     56       1.1       dbj #include <sys/systm.h>
     57       1.1       dbj #include <sys/conf.h>
     58       1.1       dbj #include <sys/device.h>
     59       1.1       dbj #include <sys/file.h>
     60       1.1       dbj #include <sys/ioctl.h>
     61       1.1       dbj #include <sys/kernel.h>
     62       1.1       dbj #include <sys/proc.h>
     63       1.1       dbj #include <sys/tty.h>
     64       1.1       dbj #include <sys/time.h>
     65       1.1       dbj #include <sys/syslog.h>
     66       1.1       dbj 
     67       1.1       dbj #include <machine/autoconf.h>
     68       1.1       dbj #include <machine/cpu.h>
     69       1.1       dbj #include <machine/psl.h>
     70       1.1       dbj 
     71       1.1       dbj #include <dev/cons.h>
     72       1.1       dbj 
     73       1.1       dbj #include <dev/ic/z8530reg.h>
     74       1.1       dbj #include <machine/z8530var.h>
     75       1.1       dbj 
     76       1.1       dbj #include <next68k/next68k/isr.h>
     77      1.10       dbj #include <next68k/dev/zs_cons.h>
     78       1.1       dbj 
     79       1.1       dbj #include "zsc.h" 	/* NZSC */
     80       1.1       dbj 
     81       1.1       dbj #if (NZSC < 0)
     82       1.1       dbj #error "No serial controllers?"
     83       1.1       dbj #endif
     84       1.1       dbj 
     85       1.1       dbj /*
     86       1.1       dbj  * Some warts needed by z8530tty.c -
     87       1.1       dbj  * The default parity REALLY needs to be the same as the PROM uses,
     88       1.1       dbj  * or you can not see messages done with printf during boot-up...
     89       1.1       dbj  */
     90       1.1       dbj int zs_def_cflag = (CREAD | CS8 | HUPCL);
     91       1.1       dbj int zs_major = 12;
     92       1.1       dbj 
     93       1.1       dbj /*
     94       1.1       dbj  * The NeXT provides a 3.686400 MHz clock to the ZS chips.
     95       1.1       dbj  */
     96       1.3       dbj #if 1
     97       1.7   mycroft #define PCLK	(9600 * 384)		/* PCLK pin input clock rate */
     98       1.3       dbj #else
     99       1.7   mycroft #define PCLK	10000000
    100       1.3       dbj #endif
    101       1.1       dbj 
    102       1.1       dbj #define	ZS_DELAY()		delay(2)
    103       1.1       dbj 
    104       1.1       dbj /* The layout of this is hardware-dependent (padding, order). */
    105       1.1       dbj struct zschan {
    106       1.1       dbj 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    107       1.1       dbj 	u_char		zc_xxx0;
    108       1.1       dbj 	volatile u_char	zc_data;	/* data */
    109       1.1       dbj 	u_char		zc_xxx1;
    110       1.1       dbj };
    111       1.1       dbj 
    112       1.1       dbj static char *zsaddr[NZSC];
    113       1.1       dbj 
    114       1.1       dbj /* Flags from cninit() */
    115       1.1       dbj static int zs_hwflags[NZSC][2];
    116       1.1       dbj 
    117       1.1       dbj /* Default speed for each channel */
    118       1.1       dbj static int zs_defspeed[NZSC][2] = {
    119       1.1       dbj 	{ 9600, 	/* ttya */
    120       1.1       dbj 	  9600 },	/* ttyb */
    121       1.1       dbj };
    122       1.1       dbj 
    123       1.1       dbj static u_char zs_init_reg[16] = {
    124       1.1       dbj 	0,	/* 0: CMD (reset, etc.) */
    125       1.1       dbj 	0,	/* 1: No interrupts yet. */
    126       1.1       dbj 	0x18 + NEXT_I_IPL(NEXT_I_SCC),	/* 2: IVECT */
    127       1.1       dbj 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    128       1.1       dbj 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    129       1.1       dbj 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    130       1.1       dbj 	0,	/* 6: TXSYNC/SYNCLO */
    131       1.1       dbj 	0,	/* 7: RXSYNC/SYNCHI */
    132       1.1       dbj 	0,	/* 8: alias for data port */
    133       1.1       dbj 	ZSWR9_MASTER_IE,
    134       1.1       dbj 	0,	/*10: Misc. TX/RX control bits */
    135       1.1       dbj 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    136       1.7   mycroft 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
    137       1.7   mycroft 	0,			/*13: BAUDHI (default=9600) */
    138       1.1       dbj 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    139       1.6   mycroft 	ZSWR15_BREAK_IE,
    140       1.1       dbj };
    141       1.1       dbj 
    142      1.10       dbj struct zschan *
    143       1.1       dbj zs_get_chan_addr(zs_unit, channel)
    144       1.1       dbj 	int zs_unit, channel;
    145       1.1       dbj {
    146       1.1       dbj 	char *addr;
    147       1.1       dbj 	struct zschan *zc;
    148       1.1       dbj 
    149       1.1       dbj 	if (zs_unit >= NZSC)
    150       1.1       dbj 		return (NULL);
    151       1.1       dbj 	addr = zsaddr[zs_unit];
    152       1.1       dbj 	if (addr == NULL)
    153       1.1       dbj 		return (NULL);
    154       1.1       dbj 	if (channel == 0) {
    155       1.1       dbj 		/* handle the fact the ports are intertwined. */
    156       1.1       dbj 		zc = (struct zschan *)(addr+1);
    157       1.1       dbj 	} else {
    158       1.1       dbj 		zc = (struct zschan *)(addr);
    159       1.1       dbj 	}
    160       1.1       dbj 	return (zc);
    161       1.1       dbj }
    162       1.1       dbj 
    163       1.1       dbj 
    164       1.1       dbj /****************************************************************
    165       1.1       dbj  * Autoconfig
    166       1.1       dbj  ****************************************************************/
    167       1.1       dbj 
    168       1.1       dbj /* Definition of the driver for autoconfig. */
    169       1.1       dbj static int	zs_match __P((struct device *, struct cfdata *, void *));
    170       1.1       dbj static void	zs_attach __P((struct device *, struct device *, void *));
    171       1.1       dbj static int  zs_print __P((void *, const char *name));
    172       1.1       dbj 
    173       1.1       dbj extern int  zs_getc __P((void *arg));
    174       1.1       dbj extern void zs_putc __P((void *arg, int c));
    175       1.1       dbj 
    176       1.1       dbj struct cfattach zsc_ca = {
    177       1.1       dbj 	sizeof(struct zsc_softc), zs_match, zs_attach
    178       1.1       dbj };
    179       1.1       dbj 
    180       1.1       dbj extern struct cfdriver zsc_cd;
    181       1.1       dbj 
    182       1.1       dbj /* Interrupt handlers. */
    183       1.1       dbj static int zshard __P((void *));
    184       1.1       dbj static int zssoft __P((void *));
    185       1.1       dbj 
    186       1.1       dbj static int zs_get_speed __P((struct zs_chanstate *));
    187       1.1       dbj 
    188       1.1       dbj 
    189       1.1       dbj /*
    190       1.1       dbj  * Is the zs chip present?
    191       1.1       dbj  */
    192       1.1       dbj static int
    193       1.1       dbj zs_match(parent, cf, aux)
    194       1.1       dbj 	struct device *parent;
    195       1.1       dbj 	struct cfdata *cf;
    196       1.1       dbj 	void *aux;
    197       1.1       dbj {
    198       1.1       dbj 	return(1);
    199       1.1       dbj }
    200       1.1       dbj 
    201       1.1       dbj /*
    202       1.1       dbj  * Attach a found zs.
    203       1.1       dbj  *
    204       1.1       dbj  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
    205       1.1       dbj  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
    206       1.1       dbj  */
    207       1.1       dbj static void
    208       1.1       dbj zs_attach(parent, self, aux)
    209       1.1       dbj 	struct device *parent;
    210       1.1       dbj 	struct device *self;
    211       1.1       dbj 	void *aux;
    212       1.1       dbj {
    213       1.1       dbj 	struct zsc_softc *zsc = (void *) self;
    214       1.1       dbj 	struct zsc_attach_args zsc_args;
    215       1.1       dbj 	volatile struct zschan *zc;
    216       1.1       dbj 	struct zs_chanstate *cs;
    217       1.1       dbj 	int s, zs_unit, channel;
    218       1.1       dbj 
    219       1.5       dbj 	printf("\n");
    220       1.5       dbj 
    221       1.1       dbj 	zs_unit = zsc->zsc_dev.dv_unit;
    222  1.11.2.2    bouyer 
    223  1.11.2.2    bouyer 	if (zs_unit == 0) {
    224  1.11.2.2    bouyer 		zsaddr[0] = (void *)IIOV(NEXT_P_SCC);
    225  1.11.2.2    bouyer 	}
    226       1.1       dbj 
    227       1.1       dbj 	if (zsaddr[zs_unit] == NULL)
    228       1.1       dbj 		panic("zs_attach: zs%d not mapped\n", zs_unit);
    229       1.1       dbj 
    230       1.1       dbj 	/*
    231       1.1       dbj 	 * Initialize software state for each channel.
    232       1.1       dbj 	 */
    233       1.1       dbj 	for (channel = 0; channel < 2; channel++) {
    234       1.1       dbj 		zsc_args.channel = channel;
    235       1.1       dbj 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
    236       1.1       dbj 		cs = &zsc->zsc_cs_store[channel];
    237       1.1       dbj 		zsc->zsc_cs[channel] = cs;
    238       1.1       dbj 
    239       1.1       dbj 		cs->cs_channel = channel;
    240       1.1       dbj 		cs->cs_private = NULL;
    241       1.1       dbj 		cs->cs_ops = &zsops_null;
    242       1.1       dbj 		cs->cs_brg_clk = PCLK / 16;
    243       1.1       dbj 
    244       1.1       dbj 		zc = zs_get_chan_addr(zs_unit, channel);
    245       1.1       dbj 		cs->cs_reg_csr  = &zc->zc_csr;
    246       1.1       dbj 		cs->cs_reg_data = &zc->zc_data;
    247       1.1       dbj 
    248       1.1       dbj 		bcopy(zs_init_reg, cs->cs_creg, 16);
    249       1.1       dbj 		bcopy(zs_init_reg, cs->cs_preg, 16);
    250       1.1       dbj 
    251       1.1       dbj 		/* XXX: Get these from the PROM properties! */
    252       1.1       dbj 		/* XXX: See the mvme167 code.  Better. */
    253       1.1       dbj 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    254       1.1       dbj 			cs->cs_defspeed = zs_get_speed(cs);
    255       1.1       dbj 		else
    256       1.1       dbj 			cs->cs_defspeed = zs_defspeed[zs_unit][channel];
    257       1.1       dbj 		cs->cs_defcflag = zs_def_cflag;
    258       1.1       dbj 
    259       1.1       dbj 		/* Make these correspond to cs_defcflag (-crtscts) */
    260       1.1       dbj 		cs->cs_rr0_dcd = ZSRR0_DCD;
    261       1.1       dbj 		cs->cs_rr0_cts = 0;
    262       1.1       dbj 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    263       1.1       dbj 		cs->cs_wr5_rts = 0;
    264       1.1       dbj 
    265       1.1       dbj 		/*
    266       1.1       dbj 		 * Clear the master interrupt enable.
    267       1.1       dbj 		 * The INTENA is common to both channels,
    268       1.1       dbj 		 * so just do it on the A channel.
    269       1.1       dbj 		 */
    270       1.1       dbj 		if (channel == 0) {
    271       1.1       dbj 			zs_write_reg(cs, 9, 0);
    272       1.1       dbj 		}
    273       1.1       dbj 
    274       1.1       dbj 		/*
    275       1.1       dbj 		 * Look for a child driver for this channel.
    276       1.1       dbj 		 * The child attach will setup the hardware.
    277       1.1       dbj 		 */
    278       1.1       dbj 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
    279       1.1       dbj 			/* No sub-driver.  Just reset it. */
    280       1.1       dbj 			u_char reset = (channel == 0) ?
    281       1.1       dbj 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    282       1.1       dbj 			s = splzs();
    283       1.1       dbj 			zs_write_reg(cs,  9, reset);
    284       1.1       dbj 			splx(s);
    285       1.1       dbj 		}
    286       1.1       dbj 	}
    287       1.1       dbj 
    288       1.1       dbj 	isrlink_autovec(zshard, NULL, NEXT_I_IPL(NEXT_I_SCC), 0);
    289       1.1       dbj 	INTR_ENABLE(NEXT_I_SCC);
    290       1.1       dbj 
    291       1.1       dbj 	{
    292       1.1       dbj 		int sir;
    293       1.1       dbj 		sir = allocate_sir(zssoft, zsc);
    294       1.1       dbj 		if (sir != SIR_SERIAL) {
    295       1.1       dbj 			panic("Unexpected zssoft sir");
    296       1.1       dbj 		}
    297       1.1       dbj 	}
    298       1.1       dbj 
    299       1.1       dbj 	/*
    300       1.1       dbj 	 * Set the master interrupt enable and interrupt vector.
    301       1.1       dbj 	 * (common to both channels, do it on A)
    302       1.1       dbj 	 */
    303       1.1       dbj 	cs = zsc->zsc_cs[0];
    304       1.1       dbj 	s = splhigh();
    305       1.1       dbj 	/* interrupt vector */
    306       1.1       dbj 	zs_write_reg(cs, 2, zs_init_reg[2]);
    307       1.1       dbj 	/* master interrupt control (enable) */
    308       1.1       dbj 	zs_write_reg(cs, 9, zs_init_reg[9]);
    309       1.1       dbj 	splx(s);
    310       1.1       dbj }
    311       1.1       dbj 
    312       1.1       dbj static int
    313       1.1       dbj zs_print(aux, name)
    314       1.1       dbj 	void *aux;
    315       1.1       dbj 	const char *name;
    316       1.1       dbj {
    317       1.1       dbj 	struct zsc_attach_args *args = aux;
    318       1.1       dbj 
    319       1.1       dbj 	if (name != NULL)
    320       1.1       dbj 		printf("%s: ", name);
    321       1.1       dbj 
    322       1.1       dbj 	if (args->channel != -1)
    323       1.1       dbj 		printf(" channel %d", args->channel);
    324       1.1       dbj 
    325       1.1       dbj 	return (UNCONF);
    326       1.1       dbj }
    327       1.1       dbj 
    328       1.1       dbj static volatile int zssoftpending;
    329       1.1       dbj 
    330       1.1       dbj /*
    331       1.1       dbj  * Our ZS chips all share a common, autovectored interrupt,
    332       1.1       dbj  * so we have to look at all of them on each interrupt.
    333       1.1       dbj  */
    334       1.1       dbj static int
    335       1.1       dbj zshard(arg)
    336       1.1       dbj 	void *arg;
    337       1.1       dbj {
    338       1.1       dbj 	register struct zsc_softc *zsc;
    339       1.1       dbj 	register int unit, rr3, rval, softreq;
    340       1.1       dbj   if (!INTR_OCCURRED(NEXT_I_SCC)) return 0;
    341       1.1       dbj 
    342       1.1       dbj 	rval = softreq = 0;
    343       1.1       dbj 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    344       1.1       dbj 		zsc = zsc_cd.cd_devs[unit];
    345       1.1       dbj 		if (zsc == NULL)
    346       1.1       dbj 			continue;
    347       1.1       dbj 		rr3 = zsc_intr_hard(zsc);
    348       1.1       dbj 		/* Count up the interrupts. */
    349       1.1       dbj 		if (rr3) {
    350       1.1       dbj 			rval |= rr3;
    351       1.1       dbj 			zsc->zsc_intrcnt.ev_count++;
    352       1.1       dbj 		}
    353       1.1       dbj 		softreq |= zsc->zsc_cs[0]->cs_softreq;
    354       1.1       dbj 		softreq |= zsc->zsc_cs[1]->cs_softreq;
    355       1.1       dbj 	}
    356       1.1       dbj 
    357       1.1       dbj 	/* We are at splzs here, so no need to lock. */
    358       1.1       dbj 	if (softreq && (zssoftpending == 0)) {
    359       1.1       dbj 		zssoftpending = 1;
    360       1.1       dbj 		setsoftserial();
    361       1.1       dbj 	}
    362       1.1       dbj 	return(1);
    363       1.1       dbj }
    364       1.1       dbj 
    365       1.1       dbj /*
    366       1.1       dbj  * Similar scheme as for zshard (look at all of them)
    367       1.1       dbj  */
    368       1.1       dbj static int
    369       1.1       dbj zssoft(arg)
    370       1.1       dbj 	void *arg;
    371       1.1       dbj {
    372       1.1       dbj 	register struct zsc_softc *zsc;
    373       1.1       dbj 	register int s, unit;
    374       1.1       dbj 
    375       1.1       dbj 	/* This is not the only ISR on this IPL. */
    376       1.1       dbj 	if (zssoftpending == 0)
    377       1.1       dbj 		return (0);
    378       1.1       dbj 
    379       1.1       dbj 	/*
    380       1.1       dbj 	 * The soft intr. bit will be set by zshard only if
    381       1.1       dbj 	 * the variable zssoftpending is zero.  The order of
    382       1.1       dbj 	 * these next two statements prevents our clearing
    383       1.1       dbj 	 * the soft intr bit just after zshard has set it.
    384       1.1       dbj 	 */
    385       1.1       dbj 	/* ienab_bic(IE_ZSSOFT); */
    386       1.1       dbj 	zssoftpending = 0;
    387       1.1       dbj 
    388       1.1       dbj 	/* Make sure we call the tty layer at spltty. */
    389       1.1       dbj 	s = spltty();
    390       1.1       dbj 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    391       1.1       dbj 		zsc = zsc_cd.cd_devs[unit];
    392       1.1       dbj 		if (zsc == NULL)
    393       1.1       dbj 			continue;
    394       1.1       dbj 		(void)zsc_intr_soft(zsc);
    395       1.1       dbj 	}
    396       1.1       dbj 	splx(s);
    397       1.1       dbj 	return (1);
    398       1.1       dbj }
    399       1.1       dbj 
    400       1.1       dbj 
    401       1.1       dbj /*
    402       1.1       dbj  * Compute the current baud rate given a ZS channel.
    403       1.1       dbj  */
    404       1.1       dbj static int
    405       1.1       dbj zs_get_speed(cs)
    406       1.1       dbj 	struct zs_chanstate *cs;
    407       1.1       dbj {
    408       1.1       dbj 	int tconst;
    409       1.1       dbj 
    410       1.1       dbj 	tconst = zs_read_reg(cs, 12);
    411       1.1       dbj 	tconst |= zs_read_reg(cs, 13) << 8;
    412       1.1       dbj 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    413       1.1       dbj }
    414       1.1       dbj 
    415       1.1       dbj /*
    416       1.1       dbj  * MD functions for setting the baud rate and control modes.
    417       1.1       dbj  */
    418       1.1       dbj int
    419       1.1       dbj zs_set_speed(cs, bps)
    420       1.1       dbj 	struct zs_chanstate *cs;
    421       1.1       dbj 	int bps;	/* bits per second */
    422       1.1       dbj {
    423       1.1       dbj 	int tconst, real_bps;
    424       1.1       dbj 
    425       1.1       dbj 	if (bps == 0)
    426       1.1       dbj 		return (0);
    427       1.1       dbj 
    428       1.1       dbj #ifdef	DIAGNOSTIC
    429       1.1       dbj 	if (cs->cs_brg_clk == 0)
    430       1.1       dbj 		panic("zs_set_speed");
    431       1.1       dbj #endif
    432       1.1       dbj 
    433       1.1       dbj 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    434       1.1       dbj 	if (tconst < 0)
    435       1.1       dbj 		return (EINVAL);
    436       1.1       dbj 
    437       1.1       dbj 	/* Convert back to make sure we can do it. */
    438       1.1       dbj 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    439       1.1       dbj 
    440       1.1       dbj 	/* XXX - Allow some tolerance here? */
    441       1.1       dbj 	if (real_bps != bps)
    442       1.1       dbj 		return (EINVAL);
    443       1.1       dbj 
    444       1.1       dbj 	cs->cs_preg[12] = tconst;
    445       1.1       dbj 	cs->cs_preg[13] = tconst >> 8;
    446       1.1       dbj 
    447       1.1       dbj 	/* Caller will stuff the pending registers. */
    448       1.1       dbj 	return (0);
    449       1.1       dbj }
    450       1.1       dbj 
    451       1.1       dbj int
    452       1.1       dbj zs_set_modes(cs, cflag)
    453       1.1       dbj 	struct zs_chanstate *cs;
    454       1.1       dbj 	int cflag;	/* bits per second */
    455       1.1       dbj {
    456       1.1       dbj 	int s;
    457       1.1       dbj 
    458       1.1       dbj 	/*
    459       1.1       dbj 	 * Output hardware flow control on the chip is horrendous:
    460       1.1       dbj 	 * if carrier detect drops, the receiver is disabled, and if
    461       1.1       dbj 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    462       1.1       dbj 	 * Therefore, NEVER set the HFC bit, and instead use the
    463       1.1       dbj 	 * status interrupt to detect CTS changes.
    464       1.1       dbj 	 */
    465       1.1       dbj 	s = splzs();
    466       1.9  wrstuden 	cs->cs_rr0_pps = 0;
    467       1.9  wrstuden 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    468       1.1       dbj 		cs->cs_rr0_dcd = 0;
    469       1.9  wrstuden 		if ((cflag & MDMBUF) == 0)
    470       1.9  wrstuden 			cs->cs_rr0_pps = ZSRR0_DCD;
    471       1.9  wrstuden 	} else
    472       1.1       dbj 		cs->cs_rr0_dcd = ZSRR0_DCD;
    473       1.1       dbj 	if ((cflag & CRTSCTS) != 0) {
    474       1.1       dbj 		cs->cs_wr5_dtr = ZSWR5_DTR;
    475       1.1       dbj 		cs->cs_wr5_rts = ZSWR5_RTS;
    476       1.1       dbj 		cs->cs_rr0_cts = ZSRR0_CTS;
    477       1.1       dbj 	} else if ((cflag & CDTRCTS) != 0) {
    478       1.1       dbj 		cs->cs_wr5_dtr = 0;
    479       1.1       dbj 		cs->cs_wr5_rts = ZSWR5_DTR;
    480       1.1       dbj 		cs->cs_rr0_cts = ZSRR0_CTS;
    481       1.1       dbj 	} else if ((cflag & MDMBUF) != 0) {
    482       1.1       dbj 		cs->cs_wr5_dtr = 0;
    483       1.1       dbj 		cs->cs_wr5_rts = ZSWR5_DTR;
    484       1.1       dbj 		cs->cs_rr0_cts = ZSRR0_DCD;
    485       1.1       dbj 	} else {
    486       1.1       dbj 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    487       1.1       dbj 		cs->cs_wr5_rts = 0;
    488       1.1       dbj 		cs->cs_rr0_cts = 0;
    489       1.1       dbj 	}
    490       1.1       dbj 	splx(s);
    491       1.1       dbj 
    492       1.1       dbj 	/* Caller will stuff the pending registers. */
    493       1.1       dbj 	return (0);
    494       1.1       dbj }
    495       1.1       dbj 
    496       1.1       dbj /*
    497       1.1       dbj  * Read or write the chip with suitable delays.
    498       1.1       dbj  */
    499       1.1       dbj 
    500       1.1       dbj u_char
    501       1.1       dbj zs_read_reg(cs, reg)
    502       1.1       dbj 	struct zs_chanstate *cs;
    503       1.1       dbj 	u_char reg;
    504       1.1       dbj {
    505       1.1       dbj 	u_char val;
    506       1.1       dbj 
    507       1.1       dbj 	*cs->cs_reg_csr = reg;
    508       1.1       dbj 	ZS_DELAY();
    509       1.1       dbj 	val = *cs->cs_reg_csr;
    510       1.1       dbj 	ZS_DELAY();
    511       1.1       dbj 	return (val);
    512       1.1       dbj }
    513       1.1       dbj 
    514       1.1       dbj void
    515       1.1       dbj zs_write_reg(cs, reg, val)
    516       1.1       dbj 	struct zs_chanstate *cs;
    517       1.1       dbj 	u_char reg, val;
    518       1.1       dbj {
    519       1.1       dbj 	*cs->cs_reg_csr = reg;
    520       1.1       dbj 	ZS_DELAY();
    521       1.1       dbj 	*cs->cs_reg_csr = val;
    522       1.1       dbj 	ZS_DELAY();
    523       1.1       dbj }
    524       1.1       dbj 
    525       1.1       dbj u_char
    526       1.1       dbj zs_read_csr(cs)
    527       1.1       dbj 	struct zs_chanstate *cs;
    528       1.1       dbj {
    529       1.1       dbj 	register u_char val;
    530       1.1       dbj 
    531       1.1       dbj 	val = *cs->cs_reg_csr;
    532       1.1       dbj 	ZS_DELAY();
    533       1.1       dbj 	return (val);
    534       1.1       dbj }
    535       1.1       dbj 
    536       1.1       dbj void  zs_write_csr(cs, val)
    537       1.1       dbj 	struct zs_chanstate *cs;
    538       1.1       dbj 	u_char val;
    539       1.1       dbj {
    540       1.1       dbj 	*cs->cs_reg_csr = val;
    541       1.1       dbj 	ZS_DELAY();
    542       1.1       dbj }
    543       1.1       dbj 
    544       1.1       dbj u_char zs_read_data(cs)
    545       1.1       dbj 	struct zs_chanstate *cs;
    546       1.1       dbj {
    547       1.1       dbj 	register u_char val;
    548       1.1       dbj 
    549       1.1       dbj 	val = *cs->cs_reg_data;
    550       1.1       dbj 	ZS_DELAY();
    551       1.1       dbj 	return (val);
    552       1.1       dbj }
    553       1.1       dbj 
    554       1.1       dbj void  zs_write_data(cs, val)
    555       1.1       dbj 	struct zs_chanstate *cs;
    556       1.1       dbj 	u_char val;
    557       1.1       dbj {
    558       1.1       dbj 	*cs->cs_reg_data = val;
    559       1.1       dbj 	ZS_DELAY();
    560       1.1       dbj }
    561       1.1       dbj 
    562       1.1       dbj /****************************************************************
    563       1.1       dbj  * Console support functions (Sun specific!)
    564       1.1       dbj  * Note: this code is allowed to know about the layout of
    565       1.1       dbj  * the chip registers, and uses that to keep things simple.
    566       1.1       dbj  * XXX - I think I like the mvme167 code better. -gwr
    567       1.1       dbj  ****************************************************************/
    568       1.1       dbj 
    569       1.1       dbj extern void Debugger __P((void));
    570       1.1       dbj void *zs_conschan;
    571       1.1       dbj int	zs_consunit = 0;
    572       1.1       dbj 
    573       1.1       dbj /*
    574       1.1       dbj  * Handle user request to enter kernel debugger.
    575       1.1       dbj  */
    576       1.1       dbj void
    577       1.1       dbj zs_abort(cs)
    578       1.1       dbj 	struct zs_chanstate *cs;
    579       1.1       dbj {
    580      1.10       dbj #if defined(ZS_CONSOLE_ABORT)
    581       1.1       dbj 	register volatile struct zschan *zc = zs_conschan;
    582       1.1       dbj 	int rr0;
    583       1.1       dbj 
    584       1.1       dbj 	/* Wait for end of break to avoid PROM abort. */
    585       1.1       dbj 	/* XXX - Limit the wait? */
    586       1.1       dbj 	do {
    587       1.1       dbj 		rr0 = zc->zc_csr;
    588       1.1       dbj 		ZS_DELAY();
    589       1.1       dbj 	} while (rr0 & ZSRR0_BREAK);
    590       1.1       dbj 
    591       1.1       dbj #if defined(KGDB)
    592       1.1       dbj 	zskgdb(cs);
    593       1.1       dbj #elif defined(DDB)
    594       1.1       dbj 	Debugger();
    595       1.1       dbj #else
    596  1.11.2.1    bouyer 	/* XXX eventually, drop into next rom monitor here */
    597  1.11.2.1    bouyer 	printf("stopping on keyboard abort not supported without DDB or KGDB\n");
    598      1.10       dbj #endif
    599      1.10       dbj #else /* !ZS_CONSOLE_ABORT */
    600      1.10       dbj 	return;
    601       1.1       dbj #endif
    602       1.1       dbj }
    603       1.1       dbj 
    604       1.1       dbj /*
    605       1.1       dbj  * Polled input char.
    606       1.1       dbj  */
    607       1.1       dbj int
    608       1.1       dbj zs_getc(arg)
    609       1.1       dbj 	void *arg;
    610       1.1       dbj {
    611       1.1       dbj 	register volatile struct zschan *zc = arg;
    612       1.1       dbj 	register int s, c, rr0;
    613       1.1       dbj 
    614       1.1       dbj 	s = splhigh();
    615       1.1       dbj 	/* Wait for a character to arrive. */
    616       1.1       dbj 	do {
    617       1.1       dbj 		rr0 = zc->zc_csr;
    618       1.1       dbj 		ZS_DELAY();
    619       1.1       dbj 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    620       1.1       dbj 
    621       1.1       dbj 	c = zc->zc_data;
    622       1.1       dbj 	ZS_DELAY();
    623       1.1       dbj 	splx(s);
    624       1.1       dbj 
    625       1.1       dbj 	/*
    626       1.1       dbj 	 * This is used by the kd driver to read scan codes,
    627       1.1       dbj 	 * so don't translate '\r' ==> '\n' here...
    628       1.1       dbj 	 */
    629       1.1       dbj 	return (c);
    630       1.1       dbj }
    631       1.1       dbj 
    632       1.1       dbj /*
    633       1.1       dbj  * Polled output char.
    634       1.1       dbj  */
    635       1.1       dbj void
    636       1.1       dbj zs_putc(arg, c)
    637       1.1       dbj 	void *arg;
    638       1.1       dbj 	int c;
    639       1.1       dbj {
    640       1.1       dbj 	register volatile struct zschan *zc = arg;
    641       1.1       dbj 	register int s, rr0;
    642       1.1       dbj 
    643       1.1       dbj 	s = splhigh();
    644       1.1       dbj 	/* Wait for transmitter to become ready. */
    645       1.1       dbj 	do {
    646       1.1       dbj 		rr0 = zc->zc_csr;
    647       1.1       dbj 		ZS_DELAY();
    648       1.1       dbj 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    649       1.1       dbj 
    650       1.1       dbj 
    651       1.1       dbj 	zc->zc_data = c;
    652       1.1       dbj 	ZS_DELAY();
    653       1.1       dbj 
    654       1.1       dbj 	splx(s);
    655       1.1       dbj }
    656       1.1       dbj 
    657       1.1       dbj /*****************************************************************/
    658       1.1       dbj 
    659       1.1       dbj void zscninit __P((struct consdev *));
    660       1.1       dbj int  zscngetc __P((dev_t));
    661       1.1       dbj void zscnputc __P((dev_t, int));
    662       1.1       dbj void zscnprobe __P((struct consdev *));
    663       1.1       dbj extern int	zsopen __P(( dev_t dev, int flags, int mode, struct proc *p));
    664       1.1       dbj 
    665       1.1       dbj void
    666       1.1       dbj zscnprobe(cp)
    667       1.1       dbj 	struct consdev * cp;
    668       1.1       dbj {
    669       1.1       dbj   int     maj;
    670       1.1       dbj   for (maj = 0; maj < nchrdev; maj++) {
    671       1.1       dbj     if (cdevsw[maj].d_open == zsopen) {
    672       1.1       dbj       break;
    673       1.1       dbj     }
    674       1.1       dbj   }
    675       1.1       dbj   if (maj != nchrdev) {
    676       1.8       dbj #ifdef SERCONSOLE
    677       1.8       dbj     cp->cn_pri = CN_REMOTE;
    678       1.8       dbj #else
    679       1.1       dbj     cp->cn_pri = CN_NORMAL;		 /* Lower than CN_INTERNAL */
    680       1.8       dbj #endif
    681       1.1       dbj     zs_major = maj;
    682       1.1       dbj     zs_consunit = 0;
    683       1.1       dbj     zsaddr[0] = (void *)IIOV(NEXT_P_SCC);
    684       1.5       dbj     cp->cn_dev = makedev(maj, zs_consunit);
    685       1.1       dbj     zs_conschan = zs_get_chan_addr(0, zs_consunit);
    686       1.1       dbj   } else {
    687       1.1       dbj     cp->cn_pri = CN_DEAD;
    688       1.1       dbj   }
    689       1.1       dbj }
    690       1.1       dbj 
    691       1.1       dbj 
    692       1.1       dbj void
    693       1.1       dbj zscninit(cn)
    694       1.1       dbj 	struct consdev *cn;
    695       1.1       dbj {
    696       1.1       dbj 	zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
    697       1.1       dbj 
    698       1.1       dbj 	{
    699       1.1       dbj 		struct zs_chanstate xcs;
    700       1.1       dbj 		struct zs_chanstate *cs;
    701       1.1       dbj 		volatile struct zschan *zc;
    702       1.1       dbj 		int    tconst, s;
    703       1.1       dbj 
    704       1.1       dbj 		/* Setup temporary chanstate. */
    705       1.1       dbj 		bzero((caddr_t)&xcs, sizeof(xcs));
    706       1.1       dbj 		cs = &xcs;
    707       1.1       dbj 		zc = zs_conschan;
    708       1.1       dbj 		cs->cs_reg_csr  = &zc->zc_csr;
    709       1.1       dbj 		cs->cs_reg_data = &zc->zc_data;
    710       1.1       dbj 		cs->cs_channel = zs_consunit;
    711       1.7   mycroft 		cs->cs_brg_clk = PCLK / 16;
    712       1.1       dbj 
    713       1.1       dbj 		bcopy(zs_init_reg, cs->cs_preg, 16);
    714       1.1       dbj 		cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    715       1.1       dbj 		cs->cs_preg[15] = ZSWR15_BREAK_IE;
    716       1.1       dbj 
    717       1.1       dbj 		tconst = BPS_TO_TCONST(cs->cs_brg_clk,
    718       1.1       dbj 				zs_defspeed[0][zs_consunit]);
    719       1.1       dbj 		cs->cs_preg[12] = tconst;
    720       1.1       dbj 		cs->cs_preg[13] = tconst >> 8;
    721       1.1       dbj 		/* can't use zs_set_speed as we haven't set up the
    722       1.1       dbj 		 * signal sources, and it's not worth it for now
    723       1.1       dbj 		 */
    724       1.1       dbj 
    725       1.1       dbj 		cs->cs_preg[9] &= ~ZSWR9_MASTER_IE;
    726       1.1       dbj 		/* no interrupts until later, after attach. */
    727       1.1       dbj 
    728       1.1       dbj 		s = splhigh();
    729       1.1       dbj 		zs_loadchannelregs(cs);
    730       1.1       dbj 		splx(s);
    731       1.1       dbj 	}
    732       1.1       dbj 
    733       1.1       dbj 	printf("\nNetBSD/next68k console\n");
    734       1.1       dbj }
    735       1.1       dbj 
    736       1.1       dbj /*
    737       1.1       dbj  * Polled console input putchar.
    738       1.1       dbj  */
    739       1.1       dbj int
    740       1.1       dbj zscngetc(dev)
    741       1.1       dbj 	dev_t dev;
    742       1.1       dbj {
    743       1.1       dbj 	return (zs_getc(zs_conschan));
    744       1.1       dbj }
    745       1.1       dbj 
    746       1.1       dbj /*
    747       1.1       dbj  * Polled console output putchar.
    748       1.1       dbj  */
    749       1.1       dbj void
    750       1.1       dbj zscnputc(dev, c)
    751       1.1       dbj 	dev_t dev;
    752       1.1       dbj 	int c;
    753       1.1       dbj {
    754       1.1       dbj 	zs_putc(zs_conschan, c);
    755       1.1       dbj }
    756       1.1       dbj 
    757       1.1       dbj /*****************************************************************/
    758