Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.4.4.1
      1  1.4.4.1       he /*	$NetBSD: zs.c,v 1.4.4.1 2000/12/15 04:20:27 he Exp $	*/
      2      1.1  tsutsui 
      3      1.1  tsutsui /*-
      4      1.1  tsutsui  * Copyright (c) 1996 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 /*
     47      1.1  tsutsui  * news68k/dev/zs.c - based on {newsmips,x68k,mvme68k}/dev/zs.c
     48      1.1  tsutsui  */
     49      1.1  tsutsui 
     50      1.1  tsutsui #include "opt_ddb.h"
     51      1.1  tsutsui 
     52      1.1  tsutsui #include <sys/param.h>
     53      1.1  tsutsui #include <sys/systm.h>
     54      1.1  tsutsui #include <sys/conf.h>
     55      1.1  tsutsui #include <sys/device.h>
     56      1.1  tsutsui #include <sys/tty.h>
     57      1.1  tsutsui 
     58      1.1  tsutsui #include <machine/cpu.h>
     59      1.1  tsutsui #include <machine/z8530var.h>
     60      1.1  tsutsui 
     61      1.1  tsutsui #include <dev/cons.h>
     62      1.1  tsutsui #include <dev/ic/z8530reg.h>
     63      1.1  tsutsui 
     64      1.1  tsutsui #include <news68k/dev/hbvar.h>
     65      1.1  tsutsui 
     66      1.1  tsutsui int zs_getc __P((void *));
     67      1.1  tsutsui void zs_putc __P((void *, int));
     68      1.1  tsutsui 
     69      1.1  tsutsui extern void Debugger __P((void));
     70      1.1  tsutsui 
     71      1.1  tsutsui /*
     72      1.1  tsutsui  * Some warts needed by z8530tty.c -
     73      1.1  tsutsui  * The default parity REALLY needs to be the same as the PROM uses,
     74      1.1  tsutsui  * or you can not see messages done with printf during boot-up...
     75      1.1  tsutsui  */
     76      1.1  tsutsui int zs_def_cflag = (CREAD | CS8 | HUPCL);
     77      1.1  tsutsui int zs_major = 1;
     78      1.1  tsutsui 
     79      1.1  tsutsui /*
     80      1.3  tsutsui  * The news68k machines use three different clocks for the ZS chips.
     81      1.1  tsutsui  */
     82      1.3  tsutsui #define NPCLK	3
     83      1.3  tsutsui #define PCLK0	(9600 * 416)	/*  news1700: 3.9936MHz */
     84      1.3  tsutsui #define PCLK1	(9600 * 512)	/*  news1200: 4.9152MHz */
     85      1.3  tsutsui #define PCLK2	(9600 * 384)	/*  external: 3.6864MHz */
     86      1.3  tsutsui 
     87      1.3  tsutsui static const u_int pclk[NPCLK] = {
     88      1.3  tsutsui 	PCLK0,
     89      1.3  tsutsui 	PCLK1,
     90      1.3  tsutsui 	PCLK2,
     91      1.3  tsutsui };
     92      1.1  tsutsui 
     93      1.1  tsutsui /*
     94      1.1  tsutsui  * Define interrupt levels.
     95      1.1  tsutsui  */
     96      1.1  tsutsui #define ZSHARD_PRI 5
     97      1.1  tsutsui #define ZS_IVECT 64
     98      1.1  tsutsui 
     99      1.1  tsutsui #define ZS_DELAY() /* delay(2) */
    100      1.1  tsutsui 
    101      1.1  tsutsui /* The layout of this is hardware-dependent (padding, order). */
    102      1.1  tsutsui struct zschan {
    103      1.1  tsutsui 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    104      1.1  tsutsui 	volatile u_char	zc_data;	/* data */
    105      1.1  tsutsui };
    106      1.1  tsutsui struct zsdevice {
    107      1.1  tsutsui 	/* Yes, they are backwards. */
    108      1.1  tsutsui 	struct	zschan zs_chan_b;
    109      1.1  tsutsui 	struct	zschan zs_chan_a;
    110      1.1  tsutsui };
    111      1.1  tsutsui 
    112      1.2  tsutsui static u_char zs_sir;
    113      1.1  tsutsui 
    114      1.1  tsutsui /* Default speed for all channels */
    115      1.1  tsutsui static int zs_defspeed = 9600;
    116      1.1  tsutsui 
    117  1.4.4.1       he /* console status from cninit */
    118  1.4.4.1       he static struct zs_chanstate zs_conschan_store;
    119  1.4.4.1       he static struct zs_chanstate *zs_conschan;
    120  1.4.4.1       he static struct zschan *zc_cons;
    121  1.4.4.1       he 
    122      1.1  tsutsui static u_char zs_init_reg[16] = {
    123      1.1  tsutsui 	0,	/* 0: CMD (reset, etc.) */
    124      1.1  tsutsui 	0,	/* 1: No interrupts yet. */
    125      1.1  tsutsui 	ZS_IVECT,	/* IVECT */
    126      1.1  tsutsui 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    127      1.1  tsutsui 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    128      1.1  tsutsui 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    129      1.1  tsutsui 	0,	/* 6: TXSYNC/SYNCLO */
    130      1.1  tsutsui 	0,	/* 7: RXSYNC/SYNCHI */
    131      1.1  tsutsui 	0,	/* 8: alias for data port */
    132      1.1  tsutsui 	ZSWR9_MASTER_IE,
    133      1.1  tsutsui 	0,	/*10: Misc. TX/RX control bits */
    134      1.1  tsutsui 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    135  1.4.4.1       he 	BPS_TO_TCONST((PCLK0/16), 9600), /*12: BAUDLO (default=9600) */
    136      1.1  tsutsui 	0,			/*13: BAUDHI (default=9600) */
    137      1.1  tsutsui 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    138      1.1  tsutsui 	ZSWR15_BREAK_IE,
    139      1.1  tsutsui };
    140      1.1  tsutsui 
    141      1.1  tsutsui 
    142      1.1  tsutsui /****************************************************************
    143      1.1  tsutsui  * Autoconfig
    144      1.1  tsutsui  ****************************************************************/
    145      1.1  tsutsui 
    146      1.1  tsutsui /* Definition of the driver for autoconfig. */
    147      1.1  tsutsui static int zs_match __P((struct device *, struct cfdata *, void *));
    148      1.1  tsutsui static void zs_attach __P((struct device *, struct device *, void *));
    149      1.1  tsutsui static int zs_print __P((void *, const char *name));
    150      1.1  tsutsui 
    151      1.1  tsutsui struct cfattach zsc_ca = {
    152      1.1  tsutsui 	sizeof(struct zsc_softc), zs_match, zs_attach
    153      1.1  tsutsui };
    154      1.1  tsutsui 
    155      1.1  tsutsui extern struct cfdriver zsc_cd;
    156      1.1  tsutsui 
    157      1.1  tsutsui static int zshard __P((void *));
    158  1.4.4.1       he void zssoft __P((void *));
    159  1.4.4.1       he #if 0
    160      1.1  tsutsui static int zs_get_speed __P((struct zs_chanstate *));
    161  1.4.4.1       he #endif
    162      1.1  tsutsui 
    163      1.1  tsutsui /*
    164      1.1  tsutsui  * Is the zs chip present?
    165      1.1  tsutsui  */
    166      1.1  tsutsui static int
    167      1.1  tsutsui zs_match(parent, cf, aux)
    168      1.1  tsutsui 	struct device *parent;
    169      1.1  tsutsui 	struct cfdata *cf;
    170      1.1  tsutsui 	void *aux;
    171      1.1  tsutsui {
    172      1.1  tsutsui 	struct hb_attach_args *ha = aux;
    173      1.3  tsutsui 	u_int addr;
    174      1.1  tsutsui 
    175      1.1  tsutsui 	if (strcmp(ha->ha_name, "zsc"))
    176      1.1  tsutsui 		return 0;
    177      1.1  tsutsui 
    178      1.1  tsutsui 	/* XXX no default address */
    179      1.1  tsutsui 	if (ha->ha_address == -1)
    180      1.1  tsutsui 		return 0;
    181      1.1  tsutsui 
    182      1.1  tsutsui 	addr = IIOV(ha->ha_address);
    183      1.1  tsutsui 	/* This returns -1 on a fault (bus error). */
    184      1.1  tsutsui 	if (badaddr((void *)addr, 1))
    185      1.1  tsutsui 		return 0;
    186      1.1  tsutsui 
    187      1.1  tsutsui 	return 1;
    188      1.1  tsutsui }
    189      1.1  tsutsui 
    190      1.1  tsutsui /*
    191      1.1  tsutsui  * Attach a found zs.
    192      1.1  tsutsui  */
    193      1.1  tsutsui static void
    194      1.1  tsutsui zs_attach(parent, self, aux)
    195      1.1  tsutsui 	struct device *parent;
    196      1.1  tsutsui 	struct device *self;
    197      1.1  tsutsui 	void *aux;
    198      1.1  tsutsui {
    199      1.1  tsutsui 	struct zsc_softc *zsc = (void *) self;
    200      1.3  tsutsui 	struct cfdata *cf = self->dv_cfdata;
    201      1.1  tsutsui 	struct hb_attach_args *ha = aux;
    202      1.1  tsutsui 	struct zsc_attach_args zsc_args;
    203  1.4.4.1       he 	struct zsdevice *zs;
    204  1.4.4.1       he 	struct zschan *zc;
    205      1.1  tsutsui 	struct zs_chanstate *cs;
    206  1.4.4.1       he 	int s, channel, clk;
    207      1.1  tsutsui 
    208  1.4.4.1       he 	zs = (void *)IIOV(ha->ha_address);
    209      1.1  tsutsui 
    210      1.3  tsutsui 	clk = cf->cf_flags;
    211      1.3  tsutsui 	if (clk < 0 || clk >= NPCLK)
    212      1.3  tsutsui 		clk = 0;
    213      1.3  tsutsui 
    214      1.1  tsutsui 	printf("\n");
    215      1.1  tsutsui 
    216      1.1  tsutsui 	/*
    217      1.1  tsutsui 	 * Initialize software state for each channel.
    218      1.1  tsutsui 	 */
    219      1.1  tsutsui 	for (channel = 0; channel < 2; channel++) {
    220      1.1  tsutsui 		zsc_args.channel = channel;
    221      1.1  tsutsui 		cs = &zsc->zsc_cs_store[channel];
    222      1.1  tsutsui 		zsc->zsc_cs[channel] = cs;
    223  1.4.4.1       he 		zc = (channel == 0) ? &zs->zs_chan_a : &zs->zs_chan_b;
    224      1.1  tsutsui 
    225      1.1  tsutsui 		if (ha->ha_vect != -1)
    226      1.1  tsutsui 			zs_init_reg[2] = ha->ha_vect;
    227      1.1  tsutsui 
    228  1.4.4.1       he 		if (zc == zc_cons) {
    229  1.4.4.1       he 			bcopy(zs_conschan, cs, sizeof(struct zs_chanstate));
    230  1.4.4.1       he 			zs_conschan = cs;
    231  1.4.4.1       he 			zsc_args.hwflags = ZS_HWFLAG_CONSOLE;
    232  1.4.4.1       he 		} else {
    233  1.4.4.1       he 			cs->cs_reg_csr  = &zc->zc_csr;
    234  1.4.4.1       he 			cs->cs_reg_data = &zc->zc_data;
    235  1.4.4.1       he 			bcopy(zs_init_reg, cs->cs_creg, 16);
    236  1.4.4.1       he 			bcopy(zs_init_reg, cs->cs_preg, 16);
    237      1.1  tsutsui 			cs->cs_defspeed = zs_defspeed;
    238  1.4.4.1       he 			zsc_args.hwflags = 0;
    239  1.4.4.1       he 		}
    240  1.4.4.1       he 
    241      1.1  tsutsui 		cs->cs_defcflag = zs_def_cflag;
    242      1.1  tsutsui 
    243  1.4.4.1       he 		cs->cs_channel = channel;
    244  1.4.4.1       he 		cs->cs_private = NULL;
    245  1.4.4.1       he 		cs->cs_ops = &zsops_null;
    246  1.4.4.1       he 		cs->cs_brg_clk = pclk[clk] / 16;
    247  1.4.4.1       he 
    248      1.1  tsutsui 		/* Make these correspond to cs_defcflag (-crtscts) */
    249      1.1  tsutsui 		cs->cs_rr0_dcd = ZSRR0_DCD;
    250      1.1  tsutsui 		cs->cs_rr0_cts = 0;
    251      1.1  tsutsui 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    252      1.1  tsutsui 		cs->cs_wr5_rts = 0;
    253      1.1  tsutsui 
    254      1.1  tsutsui 		/*
    255      1.1  tsutsui 		 * Clear the master interrupt enable.
    256      1.1  tsutsui 		 * The INTENA is common to both channels,
    257      1.1  tsutsui 		 * so just do it on the A channel.
    258      1.1  tsutsui 		 */
    259      1.1  tsutsui 		if (channel == 0) {
    260      1.1  tsutsui 			s = splhigh();
    261      1.1  tsutsui 			zs_write_reg(cs, 9, 0);
    262      1.1  tsutsui 			splx(s);
    263      1.1  tsutsui 		}
    264      1.1  tsutsui 
    265      1.1  tsutsui 		/*
    266      1.1  tsutsui 		 * Look for a child driver for this channel.
    267      1.1  tsutsui 		 * The child attach will setup the hardware.
    268      1.1  tsutsui 		 */
    269      1.1  tsutsui 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
    270      1.1  tsutsui 			/* No sub-driver.  Just reset it. */
    271      1.1  tsutsui 			u_char reset = (channel == 0) ?
    272      1.1  tsutsui 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    273      1.1  tsutsui 			s = splhigh();
    274      1.1  tsutsui 			zs_write_reg(cs,  9, reset);
    275      1.1  tsutsui 			splx(s);
    276      1.1  tsutsui 		}
    277      1.1  tsutsui 	}
    278      1.1  tsutsui 
    279      1.1  tsutsui 	/*
    280      1.1  tsutsui 	 * Now safe to install interrupt handlers.
    281      1.1  tsutsui 	 */
    282      1.1  tsutsui 	hb_intr_establish(zs_init_reg[2], zshard, ZSHARD_PRI, zsc);
    283      1.1  tsutsui 
    284      1.1  tsutsui 	/*
    285      1.1  tsutsui 	 * Set the master interrupt enable and interrupt vector.
    286      1.1  tsutsui 	 * (common to both channels, do it on A)
    287      1.1  tsutsui 	 */
    288      1.1  tsutsui 	cs = zsc->zsc_cs[0];
    289      1.1  tsutsui 	s = splhigh();
    290      1.1  tsutsui 	/* interrupt vector */
    291      1.1  tsutsui 	zs_write_reg(cs, 2, zs_init_reg[2]);
    292      1.1  tsutsui 	/* master interrupt control (enable) */
    293      1.1  tsutsui 	zs_write_reg(cs, 9, zs_init_reg[9]);
    294      1.1  tsutsui 	splx(s);
    295      1.1  tsutsui 
    296      1.1  tsutsui 	if (zs_sir == 0)
    297  1.4.4.1       he 		zs_sir = allocate_sir(zssoft, zsc);
    298      1.1  tsutsui }
    299      1.1  tsutsui 
    300      1.1  tsutsui static int
    301      1.1  tsutsui zs_print(aux, name)
    302      1.1  tsutsui 	void *aux;
    303      1.1  tsutsui 	const char *name;
    304      1.1  tsutsui {
    305      1.1  tsutsui 	struct zsc_attach_args *args = aux;
    306      1.1  tsutsui 
    307      1.1  tsutsui 	if (name != NULL)
    308      1.1  tsutsui 		printf("%s: ", name);
    309      1.1  tsutsui 
    310      1.1  tsutsui 	if (args->channel != -1)
    311      1.1  tsutsui 		printf(" channel %d", args->channel);
    312      1.1  tsutsui 
    313      1.1  tsutsui 	return UNCONF;
    314      1.1  tsutsui }
    315      1.1  tsutsui 
    316      1.1  tsutsui /*
    317      1.1  tsutsui  * For news68k-port, we don't use autovectored interrupt.
    318      1.1  tsutsui  * We do not need to look at all of the zs chips.
    319      1.1  tsutsui  */
    320      1.1  tsutsui static int
    321      1.1  tsutsui zshard(arg)
    322      1.1  tsutsui 	void *arg;
    323      1.1  tsutsui {
    324      1.1  tsutsui 	struct zsc_softc *zsc = arg;
    325      1.1  tsutsui 	int rval;
    326      1.1  tsutsui 
    327      1.1  tsutsui 	rval = zsc_intr_hard(zsc);
    328      1.1  tsutsui 
    329      1.1  tsutsui 	/* We are at splzs here, so no need to lock. */
    330      1.1  tsutsui 	if (zsc->zsc_cs[0]->cs_softreq || zsc->zsc_cs[1]->cs_softreq) {
    331      1.1  tsutsui 		setsoftint(zs_sir);
    332      1.1  tsutsui 	}
    333      1.1  tsutsui 
    334      1.1  tsutsui 	return (rval);
    335      1.1  tsutsui }
    336      1.1  tsutsui 
    337      1.1  tsutsui /*
    338      1.1  tsutsui  * Shared among the all chips. We have to look at all of them.
    339      1.1  tsutsui  */
    340  1.4.4.1       he void
    341      1.1  tsutsui zssoft(arg)
    342      1.1  tsutsui 	void *arg;
    343      1.1  tsutsui {
    344      1.1  tsutsui 	struct zsc_softc *zsc;
    345      1.1  tsutsui 	int s, unit;
    346      1.1  tsutsui 
    347      1.1  tsutsui 	/* Make sure we call the tty layer at spltty. */
    348      1.1  tsutsui 	s = spltty();
    349      1.1  tsutsui 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    350      1.1  tsutsui 		zsc = zsc_cd.cd_devs[unit];
    351      1.1  tsutsui 		if (zsc == NULL)
    352      1.1  tsutsui 			continue;
    353      1.1  tsutsui 		(void) zsc_intr_soft(zsc);
    354      1.1  tsutsui 	}
    355      1.1  tsutsui 	splx(s);
    356      1.1  tsutsui }
    357      1.1  tsutsui 
    358      1.1  tsutsui /*
    359      1.1  tsutsui  * Compute the current baud rate given a ZS channel.
    360      1.1  tsutsui  */
    361  1.4.4.1       he #if 0
    362      1.1  tsutsui static int
    363      1.1  tsutsui zs_get_speed(cs)
    364      1.1  tsutsui 	struct zs_chanstate *cs;
    365      1.1  tsutsui {
    366      1.1  tsutsui 	int tconst;
    367      1.1  tsutsui 
    368      1.1  tsutsui 	tconst = zs_read_reg(cs, 12);
    369      1.1  tsutsui 	tconst |= zs_read_reg(cs, 13) << 8;
    370      1.1  tsutsui 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    371      1.1  tsutsui }
    372  1.4.4.1       he #endif
    373      1.1  tsutsui 
    374      1.1  tsutsui /*
    375      1.1  tsutsui  * MD functions for setting the baud rate and control modes.
    376      1.1  tsutsui  */
    377      1.1  tsutsui int
    378      1.1  tsutsui zs_set_speed(cs, bps)
    379      1.1  tsutsui 	struct zs_chanstate *cs;
    380      1.1  tsutsui 	int bps;	/* bits per second */
    381      1.1  tsutsui {
    382      1.1  tsutsui 	int tconst, real_bps;
    383      1.1  tsutsui 
    384      1.1  tsutsui 	if (bps == 0)
    385      1.1  tsutsui 		return (0);
    386      1.1  tsutsui 
    387      1.1  tsutsui #ifdef	DIAGNOSTIC
    388      1.1  tsutsui 	if (cs->cs_brg_clk == 0)
    389      1.1  tsutsui 		panic("zs_set_speed");
    390      1.1  tsutsui #endif
    391      1.1  tsutsui 
    392      1.1  tsutsui 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    393      1.1  tsutsui 	if (tconst < 0)
    394      1.1  tsutsui 		return (EINVAL);
    395      1.1  tsutsui 
    396      1.1  tsutsui 	/* Convert back to make sure we can do it. */
    397      1.1  tsutsui 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    398      1.1  tsutsui 
    399      1.1  tsutsui 	/* XXX - Allow some tolerance here? */
    400      1.1  tsutsui 	if (real_bps != bps)
    401      1.1  tsutsui 		return (EINVAL);
    402      1.1  tsutsui 
    403      1.1  tsutsui 	cs->cs_preg[12] = tconst;
    404      1.1  tsutsui 	cs->cs_preg[13] = tconst >> 8;
    405      1.1  tsutsui 
    406      1.1  tsutsui 	/* Caller will stuff the pending registers. */
    407      1.1  tsutsui 	return (0);
    408      1.1  tsutsui }
    409      1.1  tsutsui 
    410      1.1  tsutsui int
    411      1.1  tsutsui zs_set_modes(cs, cflag)
    412      1.1  tsutsui 	struct zs_chanstate *cs;
    413      1.1  tsutsui 	int cflag;	/* bits per second */
    414      1.1  tsutsui {
    415      1.1  tsutsui 	int s;
    416      1.1  tsutsui 
    417      1.1  tsutsui 	/*
    418      1.1  tsutsui 	 * Output hardware flow control on the chip is horrendous:
    419      1.1  tsutsui 	 * if carrier detect drops, the receiver is disabled, and if
    420      1.1  tsutsui 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    421      1.1  tsutsui 	 * Therefore, NEVER set the HFC bit, and instead use the
    422      1.1  tsutsui 	 * status interrupt to detect CTS changes.
    423      1.1  tsutsui 	 */
    424      1.1  tsutsui 	s = splzs();
    425      1.1  tsutsui 	cs->cs_rr0_pps = 0;
    426      1.1  tsutsui 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    427      1.1  tsutsui 		cs->cs_rr0_dcd = 0;
    428      1.1  tsutsui 		if ((cflag & MDMBUF) == 0)
    429      1.1  tsutsui 			cs->cs_rr0_pps = ZSRR0_DCD;
    430      1.1  tsutsui 	} else
    431      1.1  tsutsui 		cs->cs_rr0_dcd = ZSRR0_DCD;
    432      1.1  tsutsui 	if ((cflag & CRTSCTS) != 0) {
    433      1.1  tsutsui 		cs->cs_wr5_dtr = ZSWR5_DTR;
    434      1.1  tsutsui 		cs->cs_wr5_rts = ZSWR5_RTS;
    435      1.1  tsutsui 		cs->cs_rr0_cts = ZSRR0_CTS;
    436      1.1  tsutsui 	} else if ((cflag & MDMBUF) != 0) {
    437      1.1  tsutsui 		cs->cs_wr5_dtr = 0;
    438      1.1  tsutsui 		cs->cs_wr5_rts = ZSWR5_DTR;
    439      1.1  tsutsui 		cs->cs_rr0_cts = ZSRR0_DCD;
    440      1.1  tsutsui 	} else {
    441      1.1  tsutsui 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    442      1.1  tsutsui 		cs->cs_wr5_rts = 0;
    443      1.1  tsutsui 		cs->cs_rr0_cts = 0;
    444      1.1  tsutsui 	}
    445      1.1  tsutsui 	splx(s);
    446      1.1  tsutsui 
    447      1.1  tsutsui 	/* Caller will stuff the pending registers. */
    448      1.1  tsutsui 	return (0);
    449      1.1  tsutsui }
    450      1.1  tsutsui 
    451      1.1  tsutsui 
    452      1.1  tsutsui /*
    453      1.1  tsutsui  * Read or write the chip with suitable delays.
    454      1.1  tsutsui  */
    455      1.1  tsutsui 
    456      1.1  tsutsui u_char
    457      1.1  tsutsui zs_read_reg(cs, reg)
    458      1.1  tsutsui 	struct zs_chanstate *cs;
    459      1.1  tsutsui 	u_char reg;
    460      1.1  tsutsui {
    461      1.1  tsutsui 	u_char val;
    462      1.1  tsutsui 
    463      1.1  tsutsui 	*cs->cs_reg_csr = reg;
    464      1.1  tsutsui 	ZS_DELAY();
    465      1.1  tsutsui 	val = *cs->cs_reg_csr;
    466      1.1  tsutsui 	ZS_DELAY();
    467      1.1  tsutsui 	return val;
    468      1.1  tsutsui }
    469      1.1  tsutsui 
    470      1.1  tsutsui void
    471      1.1  tsutsui zs_write_reg(cs, reg, val)
    472      1.1  tsutsui 	struct zs_chanstate *cs;
    473      1.1  tsutsui 	u_char reg, val;
    474      1.1  tsutsui {
    475      1.1  tsutsui 	*cs->cs_reg_csr = reg;
    476      1.1  tsutsui 	ZS_DELAY();
    477      1.1  tsutsui 	*cs->cs_reg_csr = val;
    478      1.1  tsutsui 	ZS_DELAY();
    479      1.1  tsutsui }
    480      1.1  tsutsui 
    481      1.3  tsutsui u_char
    482      1.3  tsutsui zs_read_csr(cs)
    483      1.1  tsutsui 	struct zs_chanstate *cs;
    484      1.1  tsutsui {
    485      1.1  tsutsui 	u_char val;
    486      1.1  tsutsui 
    487      1.1  tsutsui 	val = *cs->cs_reg_csr;
    488      1.1  tsutsui 	ZS_DELAY();
    489      1.1  tsutsui 	return val;
    490      1.1  tsutsui }
    491      1.1  tsutsui 
    492      1.3  tsutsui void
    493      1.3  tsutsui zs_write_csr(cs, val)
    494      1.1  tsutsui 	struct zs_chanstate *cs;
    495      1.1  tsutsui 	u_char val;
    496      1.1  tsutsui {
    497      1.1  tsutsui 	*cs->cs_reg_csr = val;
    498      1.1  tsutsui 	ZS_DELAY();
    499      1.1  tsutsui }
    500      1.1  tsutsui 
    501      1.3  tsutsui u_char
    502      1.3  tsutsui zs_read_data(cs)
    503      1.1  tsutsui 	struct zs_chanstate *cs;
    504      1.1  tsutsui {
    505      1.1  tsutsui 	u_char val;
    506      1.1  tsutsui 
    507      1.1  tsutsui 	val = *cs->cs_reg_data;
    508      1.1  tsutsui 	ZS_DELAY();
    509      1.1  tsutsui 	return val;
    510      1.1  tsutsui }
    511      1.1  tsutsui 
    512      1.3  tsutsui void
    513      1.3  tsutsui zs_write_data(cs, val)
    514      1.1  tsutsui 	struct zs_chanstate *cs;
    515      1.1  tsutsui 	u_char val;
    516      1.1  tsutsui {
    517      1.1  tsutsui 	*cs->cs_reg_data = val;
    518      1.1  tsutsui 	ZS_DELAY();
    519      1.1  tsutsui }
    520      1.1  tsutsui 
    521      1.1  tsutsui void
    522      1.1  tsutsui zs_abort(cs)
    523      1.1  tsutsui 	struct zs_chanstate *cs;
    524      1.1  tsutsui {
    525      1.1  tsutsui #ifdef DDB
    526      1.1  tsutsui 	Debugger();
    527      1.1  tsutsui #endif
    528      1.1  tsutsui }
    529      1.1  tsutsui 
    530      1.1  tsutsui /*
    531      1.1  tsutsui  * Polled input char.
    532      1.1  tsutsui  */
    533      1.1  tsutsui int
    534      1.1  tsutsui zs_getc(arg)
    535      1.1  tsutsui 	void *arg;
    536      1.1  tsutsui {
    537  1.4.4.1       he 	struct zs_chanstate *cs = arg;
    538      1.1  tsutsui 	int s, c, rr0;
    539      1.1  tsutsui 
    540      1.1  tsutsui 	s = splhigh();
    541      1.1  tsutsui 	/* Wait for a character to arrive. */
    542      1.1  tsutsui 	do {
    543  1.4.4.1       he 		rr0 = *cs->cs_reg_csr;
    544      1.1  tsutsui 		ZS_DELAY();
    545      1.1  tsutsui 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    546      1.1  tsutsui 
    547  1.4.4.1       he 	c = *cs->cs_reg_data;
    548      1.1  tsutsui 	ZS_DELAY();
    549      1.1  tsutsui 	splx(s);
    550      1.1  tsutsui 
    551      1.1  tsutsui 	return c;
    552      1.1  tsutsui }
    553      1.1  tsutsui 
    554      1.1  tsutsui /*
    555      1.1  tsutsui  * Polled output char.
    556      1.1  tsutsui  */
    557      1.1  tsutsui void
    558      1.1  tsutsui zs_putc(arg, c)
    559      1.1  tsutsui 	void *arg;
    560      1.1  tsutsui 	int c;
    561      1.1  tsutsui {
    562  1.4.4.1       he 	struct zs_chanstate *cs = arg;
    563      1.1  tsutsui 	int s, rr0;
    564      1.1  tsutsui 
    565      1.1  tsutsui 	s = splhigh();
    566      1.1  tsutsui 	/* Wait for transmitter to become ready. */
    567      1.1  tsutsui 	do {
    568  1.4.4.1       he 		rr0 = *cs->cs_reg_csr;
    569      1.1  tsutsui 		ZS_DELAY();
    570      1.1  tsutsui 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    571      1.1  tsutsui 
    572  1.4.4.1       he 	*cs->cs_reg_data = c;
    573      1.1  tsutsui 	ZS_DELAY();
    574      1.1  tsutsui 	splx(s);
    575      1.1  tsutsui }
    576      1.1  tsutsui 
    577      1.1  tsutsui /*****************************************************************/
    578      1.1  tsutsui 
    579      1.1  tsutsui static void zscnprobe __P((struct consdev *));
    580      1.1  tsutsui static void zscninit __P((struct consdev *));
    581      1.1  tsutsui static int  zscngetc __P((dev_t));
    582      1.1  tsutsui static void zscnputc __P((dev_t, int));
    583      1.1  tsutsui 
    584      1.1  tsutsui struct consdev consdev_zs = {
    585      1.1  tsutsui 	zscnprobe,
    586      1.1  tsutsui 	zscninit,
    587      1.1  tsutsui 	zscngetc,
    588      1.1  tsutsui 	zscnputc,
    589      1.4  thorpej 	nullcnpollc,
    590      1.4  thorpej 	NULL,
    591      1.1  tsutsui };
    592      1.1  tsutsui 
    593      1.3  tsutsui static void
    594      1.1  tsutsui zscnprobe(cn)
    595      1.1  tsutsui 	struct consdev *cn;
    596      1.1  tsutsui {
    597  1.4.4.1       he 	extern int tty00_is_console;
    598  1.4.4.1       he 
    599  1.4.4.1       he 	cn->cn_dev = makedev(zs_major, 0);
    600  1.4.4.1       he 	if (tty00_is_console)
    601  1.4.4.1       he 		cn->cn_pri = CN_REMOTE;
    602  1.4.4.1       he 	else
    603  1.4.4.1       he 		cn->cn_pri = CN_NORMAL;
    604      1.1  tsutsui }
    605      1.1  tsutsui 
    606      1.3  tsutsui static void
    607      1.1  tsutsui zscninit(cn)
    608      1.1  tsutsui 	struct consdev *cn;
    609      1.1  tsutsui {
    610  1.4.4.1       he 	struct zs_chanstate *cs;
    611  1.4.4.1       he 
    612      1.3  tsutsui 	extern volatile u_char *sccport0a;
    613      1.3  tsutsui 
    614  1.4.4.1       he 	zc_cons = (struct zschan *)sccport0a; /* XXX */
    615  1.4.4.1       he 
    616  1.4.4.1       he 	zs_conschan = cs = &zs_conschan_store;
    617  1.4.4.1       he 
    618  1.4.4.1       he 	/* Setup temporary chanstate. */
    619  1.4.4.1       he 	cs->cs_reg_csr  = &zc_cons->zc_csr;
    620  1.4.4.1       he 	cs->cs_reg_data = &zc_cons->zc_data;
    621  1.4.4.1       he 
    622  1.4.4.1       he 	/* Initialize the pending registers. */
    623  1.4.4.1       he 	bcopy(zs_init_reg, cs->cs_preg, 16);
    624  1.4.4.1       he 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    625  1.4.4.1       he 
    626  1.4.4.1       he 	cs->cs_preg[12] = BPS_TO_TCONST(pclk[systype] / 16, 9600); /* XXX */
    627  1.4.4.1       he 	cs->cs_preg[13] = 0;
    628  1.4.4.1       he 	cs->cs_defspeed = 9600;
    629  1.4.4.1       he 
    630  1.4.4.1       he 	/* Clear the master interrupt enable. */
    631  1.4.4.1       he 	zs_write_reg(cs, 9, 0);
    632  1.4.4.1       he 
    633  1.4.4.1       he 	/* Reset the whole SCC chip. */
    634  1.4.4.1       he 	zs_write_reg(cs, 9, ZSWR9_HARD_RESET);
    635  1.4.4.1       he 
    636  1.4.4.1       he 	/* Copy "pending" to "current" and H/W */
    637  1.4.4.1       he 	zs_loadchannelregs(cs);
    638      1.1  tsutsui }
    639      1.1  tsutsui 
    640      1.3  tsutsui static int
    641      1.1  tsutsui zscngetc(dev)
    642      1.1  tsutsui 	dev_t dev;
    643      1.1  tsutsui {
    644  1.4.4.1       he 	return zs_getc((void *)zs_conschan);
    645      1.1  tsutsui }
    646      1.1  tsutsui 
    647      1.3  tsutsui static void
    648      1.1  tsutsui zscnputc(dev, c)
    649      1.1  tsutsui 	dev_t dev;
    650      1.1  tsutsui 	int c;
    651      1.1  tsutsui {
    652  1.4.4.1       he 	zs_putc((void *)zs_conschan, c);
    653      1.1  tsutsui }
    654