Home | History | Annotate | Line # | Download | only in dev
zs.c revision 1.20
      1  1.20    scottr /*	$NetBSD: zs.c,v 1.20 1998/05/05 06:48:51 scottr Exp $	*/
      2   1.1    briggs 
      3   1.1    briggs /*
      4  1.15    scottr  * Copyright (c) 1996 Bill Studenmund
      5   1.1    briggs  * Copyright (c) 1995 Gordon W. Ross
      6   1.1    briggs  * All rights reserved.
      7   1.1    briggs  *
      8   1.1    briggs  * Redistribution and use in source and binary forms, with or without
      9   1.1    briggs  * modification, are permitted provided that the following conditions
     10   1.1    briggs  * are met:
     11   1.1    briggs  * 1. Redistributions of source code must retain the above copyright
     12   1.1    briggs  *    notice, this list of conditions and the following disclaimer.
     13   1.1    briggs  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1    briggs  *    notice, this list of conditions and the following disclaimer in the
     15   1.1    briggs  *    documentation and/or other materials provided with the distribution.
     16   1.1    briggs  * 3. The name of the author may not be used to endorse or promote products
     17   1.1    briggs  *    derived from this software without specific prior written permission.
     18   1.1    briggs  * 4. All advertising materials mentioning features or use of this software
     19   1.1    briggs  *    must display the following acknowledgement:
     20   1.1    briggs  *      This product includes software developed by Gordon Ross
     21   1.1    briggs  *
     22   1.1    briggs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1    briggs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1    briggs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1    briggs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1    briggs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1    briggs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1    briggs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1    briggs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1    briggs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1    briggs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1    briggs  */
     33   1.1    briggs 
     34   1.1    briggs /*
     35   1.1    briggs  * Zilog Z8530 Dual UART driver (machine-dependent part)
     36   1.1    briggs  *
     37   1.1    briggs  * Runs two serial lines per chip using slave drivers.
     38   1.1    briggs  * Plain tty/async lines use the zs_async slave.
     39   1.1    briggs  * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
     40  1.15    scottr  * Other ports use their own mice & keyboard slaves.
     41  1.15    scottr  *
     42  1.15    scottr  * Credits & history:
     43  1.15    scottr  *
     44  1.15    scottr  * With NetBSD 1.1, port-mac68k started using a port of the port-sparc
     45  1.15    scottr  * (port-sun3?) zs.c driver (which was in turn based on code in the
     46  1.15    scottr  * Berkeley 4.4 Lite release). Bill Studenmund did the port, with
     47  1.15    scottr  * help from Allen Briggs and Gordon Ross <gwr (at) netbsd.org>. Noud de
     48  1.15    scottr  * Brouwer field-tested the driver at a local ISP.
     49  1.15    scottr  *
     50  1.15    scottr  * Bill Studenmund and Gordon Ross then ported the machine-independant
     51  1.15    scottr  * z8530 driver to work with port-mac68k. NetBSD 1.2 contained an
     52  1.15    scottr  * intermediate version (mac68k using a local, patched version of
     53  1.15    scottr  * the m.i. drivers), with NetBSD 1.3 containing a full version.
     54   1.1    briggs  */
     55   1.1    briggs 
     56   1.1    briggs #include <sys/param.h>
     57   1.1    briggs #include <sys/systm.h>
     58   1.1    briggs #include <sys/proc.h>
     59   1.1    briggs #include <sys/device.h>
     60   1.1    briggs #include <sys/conf.h>
     61   1.1    briggs #include <sys/file.h>
     62   1.1    briggs #include <sys/ioctl.h>
     63   1.1    briggs #include <sys/tty.h>
     64   1.1    briggs #include <sys/time.h>
     65   1.1    briggs #include <sys/kernel.h>
     66   1.1    briggs #include <sys/syslog.h>
     67   1.1    briggs 
     68  1.20    scottr #include <machine/autoconf.h>
     69  1.20    scottr #include <machine/cpu.h>
     70  1.20    scottr #include <machine/viareg.h>
     71  1.20    scottr 
     72   1.1    briggs #include <dev/cons.h>
     73  1.15    scottr #include <dev/ic/z8530reg.h>
     74   1.1    briggs #include <machine/z8530var.h>
     75  1.20    scottr #include <mac68k/dev/zs_cons.h>
     76   1.1    briggs 
     77  1.15    scottr /* Are these in a header file anywhere? */
     78  1.15    scottr /* Booter flags interface */
     79  1.15    scottr #define ZSMAC_RAW	0x01
     80  1.15    scottr #define ZSMAC_LOCALTALK	0x02
     81  1.15    scottr #define	ZS_STD_BRG	(57600*4)
     82  1.15    scottr 
     83  1.15    scottr #include "zsc.h"	/* get the # of zs chips defined */
     84  1.15    scottr 
     85  1.15    scottr /*
     86  1.15    scottr  * Some warts needed by z8530tty.c -
     87  1.15    scottr  */
     88  1.15    scottr int zs_def_cflag = (CREAD | CS8 | HUPCL);
     89  1.15    scottr int zs_major = 12;
     90  1.15    scottr 
     91   1.1    briggs /*
     92  1.15    scottr  * abort detection on console will now timeout after iterating on a loop
     93  1.15    scottr  * the following # of times. Cheep hack. Also, abort detection is turned
     94  1.15    scottr  * off after a timeout (i.e. maybe there's not a terminal hooked up).
     95   1.1    briggs  */
     96  1.15    scottr #define ZSABORT_DELAY 3000000
     97   1.1    briggs 
     98   1.1    briggs /*
     99   1.1    briggs  * Define interrupt levels.
    100   1.1    briggs  */
    101  1.15    scottr #define ZSHARD_PRI	4	/* Wired on the CPU board... */
    102  1.15    scottr /*
    103  1.15    scottr  * Serial port cards with zs chips on them are actually at the
    104  1.15    scottr  * NuBus interrupt level, which is lower than 4. But blocking
    105  1.15    scottr  * level 4 interrupts will block those interrupts too, so level
    106  1.15    scottr  * 4 is fine.
    107  1.15    scottr  */
    108   1.1    briggs 
    109   1.1    briggs /* The layout of this is hardware-dependent (padding, order). */
    110   1.1    briggs struct zschan {
    111   1.1    briggs 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    112   1.1    briggs 	u_char		zc_xxx0;
    113  1.15    scottr 	u_char		zc_xxx1;	/* part of the other channel lives here! */
    114  1.15    scottr 	u_char		zc_xxx2;	/* Yea Apple! */
    115   1.1    briggs 	volatile u_char	zc_data;	/* data */
    116   1.1    briggs 	u_char		zc_xxx3;
    117   1.1    briggs 	u_char		zc_xxx4;
    118   1.1    briggs 	u_char		zc_xxx5;
    119   1.1    briggs };
    120   1.1    briggs 
    121   1.1    briggs /* Saved PROM mappings */
    122   1.1    briggs static char *zsaddr[NZSC];	/* See zs_init() */
    123   1.1    briggs /* Flags from cninit() */
    124   1.1    briggs static int zs_hwflags[NZSC][2];
    125   1.1    briggs /* Default speed for each channel */
    126   1.1    briggs static int zs_defspeed[NZSC][2] = {
    127   1.1    briggs 	{ 9600, 	/* tty00 */
    128   1.1    briggs 	  9600 },	/* tty01 */
    129   1.1    briggs };
    130   1.1    briggs /* console stuff */
    131  1.15    scottr void	*zs_conschan = 0;
    132  1.15    scottr int	zs_consunit;
    133  1.15    scottr #ifdef	ZS_CONSOLE_ABORT
    134  1.15    scottr int	zs_cons_canabort = 1;
    135  1.15    scottr #else
    136  1.15    scottr int	zs_cons_canabort = 0;
    137  1.15    scottr #endif /* ZS_CONSOLE_ABORT*/
    138  1.15    scottr /* device to which the console is attached--if serial. */
    139   1.1    briggs dev_t	mac68k_zsdev;
    140  1.15    scottr /* Mac stuff */
    141   1.1    briggs volatile unsigned char *sccA = 0;
    142   1.1    briggs 
    143  1.20    scottr int	zs_cn_check_speed __P((int bps));
    144  1.15    scottr 
    145  1.15    scottr /*
    146  1.15    scottr  * Even though zsparam will set up the clock multiples, etc., we
    147  1.15    scottr  * still set them here as: 1) mice & keyboards don't use zsparam,
    148  1.15    scottr  * and 2) the console stuff uses these defaults before device
    149  1.15    scottr  * attach.
    150  1.15    scottr  */
    151  1.15    scottr 
    152  1.15    scottr static u_char zs_init_reg[16] = {
    153  1.15    scottr 	0,	/* 0: CMD (reset, etc.) */
    154  1.15    scottr 	0,	/* 1: No interrupts yet. */
    155  1.15    scottr 	0x18 + ZSHARD_PRI,	/* IVECT */
    156  1.15    scottr 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    157  1.15    scottr 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
    158  1.15    scottr 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    159  1.15    scottr 	0,	/* 6: TXSYNC/SYNCLO */
    160  1.15    scottr 	0,	/* 7: RXSYNC/SYNCHI */
    161  1.15    scottr 	0,	/* 8: alias for data port */
    162  1.15    scottr 	ZSWR9_MASTER_IE,
    163  1.15    scottr 	0,	/*10: Misc. TX/RX control bits */
    164  1.15    scottr 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    165  1.15    scottr 	14,	/*12: BAUDLO (default=9600) */
    166  1.15    scottr 	0,	/*13: BAUDHI (default=9600) */
    167  1.15    scottr 	ZSWR14_BAUD_ENA,
    168  1.15    scottr 	ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
    169  1.15    scottr };
    170   1.1    briggs 
    171  1.20    scottr struct zschan *
    172   1.1    briggs zs_get_chan_addr(zsc_unit, channel)
    173   1.1    briggs 	int zsc_unit, channel;
    174   1.1    briggs {
    175   1.1    briggs 	char *addr;
    176   1.1    briggs 	struct zschan *zc;
    177   1.1    briggs 
    178   1.1    briggs 	if (zsc_unit >= NZSC)
    179   1.1    briggs 		return NULL;
    180   1.1    briggs 	addr = zsaddr[zsc_unit];
    181   1.1    briggs 	if (addr == NULL)
    182   1.1    briggs 		return NULL;
    183   1.1    briggs 	if (channel == 0) {
    184  1.20    scottr 		zc = (struct zschan *)(addr + 2);
    185   1.1    briggs 		/* handle the fact the ports are intertwined. */
    186   1.1    briggs 	} else {
    187   1.1    briggs 		zc = (struct zschan *)(addr);
    188   1.1    briggs 	}
    189   1.1    briggs 	return (zc);
    190   1.1    briggs }
    191   1.1    briggs 
    192   1.1    briggs 
    193   1.1    briggs /* Find PROM mappings (for console support). */
    194  1.15    scottr int zsinited = 0; /* 0 = not, 1 = inited, not attached, 2= attached */
    195   1.1    briggs 
    196   1.1    briggs void
    197   1.1    briggs zs_init()
    198   1.1    briggs {
    199   1.1    briggs 	if ((zsinited == 2)&&(zsaddr[0] != (char *) sccA))
    200   1.1    briggs 		panic("Moved zs0 address after attached!");
    201   1.1    briggs 	zsaddr[0] = (char *) sccA;
    202   1.1    briggs 	zsinited = 1;
    203   1.1    briggs 	if (zs_conschan != 0){ /* we might have moved io under the console */
    204   1.1    briggs 		zs_conschan = zs_get_chan_addr(0, zs_consunit);
    205   1.1    briggs 		/* so recalc the console port */
    206   1.1    briggs 	}
    207   1.1    briggs }
    208   1.1    briggs 
    209   1.1    briggs 
    210   1.1    briggs /****************************************************************
    211   1.1    briggs  * Autoconfig
    212   1.1    briggs  ****************************************************************/
    213   1.1    briggs 
    214   1.1    briggs /* Definition of the driver for autoconfig. */
    215  1.11    scottr static int	zsc_match __P((struct device *, struct cfdata *, void *));
    216   1.1    briggs static void	zsc_attach __P((struct device *, struct device *, void *));
    217  1.15    scottr static int  zsc_print __P((void *, const char *name));
    218   1.1    briggs 
    219   1.1    briggs struct cfattach zsc_ca = {
    220   1.1    briggs 	sizeof(struct zsc_softc), zsc_match, zsc_attach
    221   1.1    briggs };
    222   1.1    briggs 
    223  1.19   thorpej extern struct cfdriver zsc_cd;
    224   1.1    briggs 
    225  1.15    scottr int zshard __P((void *));
    226  1.15    scottr int zssoft __P((void *));
    227   1.1    briggs 
    228   1.1    briggs 
    229   1.1    briggs /*
    230   1.1    briggs  * Is the zs chip present?
    231   1.1    briggs  */
    232   1.1    briggs static int
    233  1.11    scottr zsc_match(parent, cf, aux)
    234   1.1    briggs 	struct device *parent;
    235  1.11    scottr 	struct cfdata *cf;
    236   1.1    briggs 	void *aux;
    237   1.1    briggs {
    238   1.1    briggs 	return 1;
    239   1.1    briggs }
    240   1.1    briggs 
    241   1.1    briggs /*
    242   1.1    briggs  * Attach a found zs.
    243   1.1    briggs  *
    244   1.1    briggs  * Match slave number to zs unit number, so that misconfiguration will
    245   1.1    briggs  * not set up the keyboard as ttya, etc.
    246   1.1    briggs  */
    247   1.1    briggs static void
    248   1.1    briggs zsc_attach(parent, self, aux)
    249   1.1    briggs 	struct device *parent;
    250   1.1    briggs 	struct device *self;
    251   1.1    briggs 	void *aux;
    252   1.1    briggs {
    253   1.1    briggs 	struct zsc_softc *zsc = (void *) self;
    254   1.1    briggs 	struct zsc_attach_args zsc_args;
    255   1.1    briggs 	volatile struct zschan *zc;
    256  1.15    scottr 	struct xzs_chanstate *xcs;
    257   1.1    briggs 	struct zs_chanstate *cs;
    258   1.1    briggs 	int zsc_unit, channel;
    259  1.15    scottr 	int s, chip, theflags;
    260   1.1    briggs 
    261  1.15    scottr 	if (!zsinited)
    262  1.15    scottr 		zs_init();
    263   1.1    briggs 	zsinited = 2;
    264   1.1    briggs 
    265   1.1    briggs 	zsc_unit = zsc->zsc_dev.dv_unit;
    266   1.1    briggs 
    267   1.1    briggs 	/* Make sure everything's inited ok. */
    268   1.1    briggs 	if (zsaddr[zsc_unit] == NULL)
    269   1.1    briggs 		panic("zs_attach: zs%d not mapped\n", zsc_unit);
    270   1.1    briggs 
    271  1.15    scottr 	chip = 0; /* We'll deal with chip types post 1.2 */
    272  1.15    scottr 	printf(" chip type %d \n",chip);
    273  1.15    scottr 
    274   1.1    briggs 	/*
    275   1.1    briggs 	 * Initialize software state for each channel.
    276   1.1    briggs 	 */
    277   1.1    briggs 	for (channel = 0; channel < 2; channel++) {
    278  1.15    scottr 		zsc_args.channel = channel;
    279  1.15    scottr 		zsc_args.hwflags = zs_hwflags[zsc_unit][channel];
    280  1.15    scottr 		xcs = &zsc->xzsc_xcs_store[channel];
    281  1.15    scottr 		cs  = &xcs->xzs_cs;
    282  1.15    scottr 		zsc->zsc_cs[channel] = cs;
    283  1.15    scottr 
    284  1.15    scottr 		cs->cs_channel = channel;
    285  1.15    scottr 		cs->cs_private = NULL;
    286  1.15    scottr 		cs->cs_ops = &zsops_null;
    287   1.1    briggs 
    288   1.1    briggs 		zc = zs_get_chan_addr(zsc_unit, channel);
    289   1.1    briggs 		cs->cs_reg_csr  = &zc->zc_csr;
    290   1.1    briggs 		cs->cs_reg_data = &zc->zc_data;
    291   1.1    briggs 
    292  1.15    scottr 		bcopy(zs_init_reg, cs->cs_creg, 16);
    293  1.15    scottr 		bcopy(zs_init_reg, cs->cs_preg, 16);
    294   1.1    briggs 
    295  1.15    scottr 		/* Current BAUD rate generator clock. */
    296  1.15    scottr 		cs->cs_brg_clk = ZS_STD_BRG;	/* RTxC is 230400*16, so use 230400 */
    297  1.15    scottr 		cs->cs_defspeed = zs_defspeed[zsc_unit][channel];
    298  1.15    scottr 		cs->cs_defcflag = zs_def_cflag;
    299  1.18  wrstuden 
    300  1.18  wrstuden 		/* Make these correspond to cs_defcflag (-crtscts) */
    301  1.18  wrstuden 		cs->cs_rr0_dcd = ZSRR0_DCD;
    302  1.18  wrstuden 		cs->cs_rr0_cts = 0;
    303  1.18  wrstuden 		cs->cs_wr5_dtr = ZSWR5_DTR;
    304  1.18  wrstuden 		cs->cs_wr5_rts = 0;
    305  1.18  wrstuden 
    306  1.15    scottr #ifdef __notyet__
    307  1.15    scottr 		cs->cs_slave_type = ZS_SLAVE_NONE;
    308  1.15    scottr #endif
    309   1.1    briggs 
    310  1.15    scottr 		/* Define BAUD rate stuff. */
    311  1.15    scottr 		xcs->cs_clocks[0].clk = ZS_STD_BRG * 16;
    312  1.15    scottr 		xcs->cs_clocks[0].flags = ZSC_RTXBRG;
    313  1.15    scottr 		xcs->cs_clocks[1].flags =
    314  1.15    scottr 			ZSC_RTXBRG | ZSC_RTXDIV | ZSC_VARIABLE | ZSC_EXTERN;
    315  1.15    scottr 		xcs->cs_clocks[2].flags = ZSC_TRXDIV | ZSC_VARIABLE;
    316  1.15    scottr 		xcs->cs_clock_count = 3;
    317  1.15    scottr 		if (channel == 0) {
    318  1.15    scottr 			theflags = mac68k_machine.modem_flags;
    319  1.15    scottr 			xcs->cs_clocks[1].clk = mac68k_machine.modem_dcd_clk;
    320  1.15    scottr 			xcs->cs_clocks[2].clk = mac68k_machine.modem_cts_clk;
    321  1.15    scottr 		} else {
    322  1.15    scottr 			theflags = mac68k_machine.print_flags;
    323  1.15    scottr 			xcs->cs_clocks[1].flags = ZSC_VARIABLE;
    324  1.15    scottr 			/*
    325  1.15    scottr 			 * Yes, we aren't defining ANY clock source enables for the
    326  1.15    scottr 			 * printer's DCD clock in. The hardware won't let us
    327  1.15    scottr 			 * use it. But a clock will freak out the chip, so we
    328  1.15    scottr 			 * let you set it, telling us to bar interrupts on the line.
    329  1.15    scottr 			 */
    330  1.15    scottr 			xcs->cs_clocks[1].clk = mac68k_machine.print_dcd_clk;
    331  1.15    scottr 			xcs->cs_clocks[2].clk = mac68k_machine.print_cts_clk;
    332  1.15    scottr 		}
    333  1.15    scottr 		if (xcs->cs_clocks[1].clk)
    334  1.15    scottr 			zsc_args.hwflags |= ZS_HWFLAG_NO_DCD;
    335  1.15    scottr 		if (xcs->cs_clocks[2].clk)
    336  1.15    scottr 			zsc_args.hwflags |= ZS_HWFLAG_NO_CTS;
    337  1.15    scottr 
    338  1.15    scottr 		printf("zsc%d channel %d: d_speed %6d DCD clk %ld CTS clk %ld",
    339  1.15    scottr 				zsc_unit, channel, cs->cs_defspeed,
    340  1.15    scottr 				xcs->cs_clocks[1].clk, xcs->cs_clocks[2].clk);
    341  1.15    scottr 
    342  1.15    scottr 		/* Set defaults in our "extended" chanstate. */
    343  1.15    scottr 		xcs->cs_csource = 0;
    344  1.15    scottr 		xcs->cs_psource = 0;
    345  1.15    scottr 		xcs->cs_cclk_flag = 0;  /* Nothing fancy by default */
    346  1.15    scottr 		xcs->cs_pclk_flag = 0;
    347  1.15    scottr 
    348  1.15    scottr 		if (theflags & ZSMAC_RAW) {
    349  1.15    scottr 			zsc_args.hwflags |= ZS_HWFLAG_RAW;
    350  1.15    scottr 			printf(" (raw defaults)");
    351  1.15    scottr 		}
    352   1.1    briggs 
    353  1.15    scottr 		/*
    354  1.15    scottr 		 * XXX - This might be better done with a "stub" driver
    355  1.15    scottr 		 * (to replace zstty) that ignores LocalTalk for now.
    356  1.15    scottr 		 */
    357  1.15    scottr 		if (theflags & ZSMAC_LOCALTALK) {
    358  1.15    scottr 			printf(" shielding from LocalTalk");
    359  1.15    scottr 			cs->cs_defspeed = 1;
    360  1.15    scottr 			cs->cs_creg[ZSRR_BAUDLO] = cs->cs_preg[ZSRR_BAUDLO] = 0xff;
    361  1.15    scottr 			cs->cs_creg[ZSRR_BAUDHI] = cs->cs_preg[ZSRR_BAUDHI] = 0xff;
    362  1.15    scottr 			zs_write_reg(cs, ZSRR_BAUDLO, 0xff);
    363  1.15    scottr 			zs_write_reg(cs, ZSRR_BAUDHI, 0xff);
    364  1.15    scottr 			/*
    365  1.15    scottr 			 * If we might have LocalTalk, then make sure we have the
    366  1.15    scottr 			 * Baud rate low-enough to not do any damage.
    367  1.15    scottr 			 */
    368  1.15    scottr 		}
    369   1.1    briggs 
    370   1.1    briggs 		/*
    371  1.15    scottr 		 * We used to disable chip interrupts here, but we now
    372  1.15    scottr 		 * do that in zscnprobe, just in case MacOS left the chip on.
    373   1.1    briggs 		 */
    374   1.1    briggs 
    375  1.15    scottr 		xcs->cs_chip = chip;
    376  1.15    scottr 
    377  1.15    scottr 		/* Stash away a copy of the final H/W flags. */
    378  1.15    scottr 		xcs->cs_hwflags = zsc_args.hwflags;
    379  1.15    scottr 
    380  1.15    scottr 		printf("\n");
    381   1.1    briggs 
    382   1.1    briggs 		/*
    383   1.1    briggs 		 * Look for a child driver for this channel.
    384   1.1    briggs 		 * The child attach will setup the hardware.
    385   1.1    briggs 		 */
    386  1.15    scottr 		if (!config_found(self, (void *)&zsc_args, zsc_print)) {
    387   1.1    briggs 			/* No sub-driver.  Just reset it. */
    388  1.15    scottr 			u_char reset = (channel == 0) ?
    389   1.1    briggs 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    390   1.1    briggs 			s = splzs();
    391   1.1    briggs 			zs_write_reg(cs,  9, reset);
    392   1.1    briggs 			splx(s);
    393   1.1    briggs 		}
    394   1.1    briggs 	}
    395   1.1    briggs 
    396  1.15    scottr 	/* XXX - Now safe to install interrupt handlers. */
    397  1.15    scottr 
    398   1.1    briggs 	/*
    399   1.1    briggs 	 * Set the master interrupt enable and interrupt vector.
    400   1.1    briggs 	 * (common to both channels, do it on A)
    401   1.1    briggs 	 */
    402  1.15    scottr 	cs = zsc->zsc_cs[0];
    403   1.1    briggs 	s = splzs();
    404   1.1    briggs 	/* interrupt vector */
    405   1.1    briggs 	zs_write_reg(cs, 2, zs_init_reg[2]);
    406   1.1    briggs 	/* master interrupt control (enable) */
    407   1.1    briggs 	zs_write_reg(cs, 9, zs_init_reg[9]);
    408   1.1    briggs 	splx(s);
    409   1.1    briggs }
    410   1.1    briggs 
    411  1.15    scottr static int
    412  1.15    scottr zsc_print(aux, name)
    413  1.15    scottr 	void *aux;
    414  1.15    scottr 	const char *name;
    415  1.15    scottr {
    416  1.15    scottr 	struct zsc_attach_args *args = aux;
    417  1.15    scottr 
    418  1.15    scottr 	if (name != NULL)
    419  1.15    scottr 		printf("%s: ", name);
    420   1.3    briggs 
    421  1.15    scottr 	if (args->channel != -1)
    422  1.15    scottr 		printf(" channel %d", args->channel);
    423   1.1    briggs 
    424  1.15    scottr 	return UNCONF;
    425   1.1    briggs }
    426   1.1    briggs 
    427   1.1    briggs int
    428  1.15    scottr zsmdioctl(cs, cmd, data)
    429  1.15    scottr 	struct zs_chanstate *cs;
    430  1.15    scottr 	u_long cmd;
    431   1.1    briggs 	caddr_t data;
    432   1.1    briggs {
    433  1.15    scottr 	switch (cmd) {
    434  1.15    scottr 	default:
    435  1.15    scottr 		return (-1);
    436  1.15    scottr 	}
    437  1.15    scottr 	return (0);
    438   1.1    briggs }
    439   1.1    briggs 
    440   1.1    briggs void
    441   1.1    briggs zsmd_setclock(cs)
    442   1.1    briggs 	struct zs_chanstate *cs;
    443   1.1    briggs {
    444  1.15    scottr 	struct xzs_chanstate *xcs = (void *)cs;
    445  1.15    scottr 
    446   1.4    briggs 	if (cs->cs_channel != 0)
    447   1.4    briggs 		return;
    448  1.15    scottr 
    449   1.4    briggs 	/*
    450   1.4    briggs 	 * If the new clock has the external bit set, then select the
    451   1.4    briggs 	 * external source.
    452   1.4    briggs 	 */
    453  1.15    scottr 	via_set_modem((xcs->cs_pclk_flag & ZSC_EXTERN) ? 1 : 0);
    454   1.1    briggs }
    455   1.1    briggs 
    456  1.15    scottr static int zssoftpending;
    457  1.15    scottr 
    458  1.15    scottr /*
    459  1.15    scottr  * Our ZS chips all share a common, autovectored interrupt,
    460  1.15    scottr  * so we have to look at all of them on each interrupt.
    461  1.15    scottr  */
    462   1.1    briggs int
    463   1.1    briggs zshard(arg)
    464   1.1    briggs 	void *arg;
    465   1.1    briggs {
    466  1.20    scottr 	struct zsc_softc *zsc;
    467  1.20    scottr 	int unit, rval;
    468  1.15    scottr 
    469   1.1    briggs 	rval = 0;
    470  1.15    scottr 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    471   1.1    briggs 		zsc = zsc_cd.cd_devs[unit];
    472  1.15    scottr 		if (zsc == NULL)
    473  1.15    scottr 			continue;
    474  1.15    scottr 		rval |= zsc_intr_hard(zsc);
    475  1.15    scottr 		if ((zsc->zsc_cs[0]->cs_softreq) ||
    476  1.15    scottr 			(zsc->zsc_cs[1]->cs_softreq))
    477  1.15    scottr 		{
    478  1.15    scottr 			/* zsc_req_softint(zsc); */
    479  1.15    scottr 			/* We are at splzs here, so no need to lock. */
    480  1.15    scottr 			if (zssoftpending == 0) {
    481  1.15    scottr 				zssoftpending = 1;
    482  1.15    scottr 				setsoftserial();
    483  1.15    scottr 			}
    484   1.1    briggs 		}
    485   1.1    briggs 	}
    486   1.1    briggs 	return (rval);
    487   1.1    briggs }
    488   1.1    briggs 
    489  1.15    scottr /*
    490  1.15    scottr  * Similar scheme as for zshard (look at all of them)
    491  1.15    scottr  */
    492   1.1    briggs int
    493   1.1    briggs zssoft(arg)
    494   1.1    briggs 	void *arg;
    495   1.1    briggs {
    496  1.20    scottr 	struct zsc_softc *zsc;
    497  1.20    scottr 	int unit;
    498   1.1    briggs 
    499   1.1    briggs 	/* This is not the only ISR on this IPL. */
    500   1.1    briggs 	if (zssoftpending == 0)
    501   1.1    briggs 		return (0);
    502   1.1    briggs 
    503   1.1    briggs 	/*
    504   1.1    briggs 	 * The soft intr. bit will be set by zshard only if
    505  1.15    scottr 	 * the variable zssoftpending is zero.
    506   1.1    briggs 	 */
    507   1.1    briggs 	zssoftpending = 0;
    508   1.1    briggs 
    509  1.15    scottr 	for (unit = 0; unit < zsc_cd.cd_ndevs; ++unit) {
    510   1.1    briggs 		zsc = zsc_cd.cd_devs[unit];
    511  1.15    scottr 		if (zsc == NULL)
    512  1.15    scottr 			continue;
    513  1.15    scottr 		(void) zsc_intr_soft(zsc);
    514  1.15    scottr 	}
    515  1.15    scottr 	return (1);
    516  1.15    scottr }
    517  1.15    scottr 
    518  1.15    scottr 
    519  1.15    scottr #ifndef ZS_TOLERANCE
    520  1.15    scottr #define ZS_TOLERANCE 51
    521  1.15    scottr /* 5% in tenths of a %, plus 1 so that exactly 5% will be ok. */
    522  1.15    scottr #endif
    523  1.15    scottr 
    524  1.15    scottr /*
    525  1.15    scottr  * check out a rate for acceptability from the internal clock
    526  1.15    scottr  * source. Used in console config to validate a requested
    527  1.15    scottr  * default speed. Placed here so that all the speed checking code is
    528  1.15    scottr  * in one place.
    529  1.15    scottr  *
    530  1.15    scottr  * != 0 means ok.
    531  1.15    scottr  */
    532  1.15    scottr int
    533  1.15    scottr zs_cn_check_speed(bps)
    534  1.15    scottr 	int bps;	/* target rate */
    535  1.15    scottr {
    536  1.15    scottr 	int tc, rate;
    537  1.15    scottr 
    538  1.15    scottr 	tc = BPS_TO_TCONST(ZS_STD_BRG, bps);
    539  1.15    scottr 	if (tc < 0)
    540  1.15    scottr 		return 0;
    541  1.15    scottr 	rate = TCONST_TO_BPS(ZS_STD_BRG, tc);
    542  1.15    scottr 	if (ZS_TOLERANCE > abs(((rate - bps)*1000)/bps))
    543  1.15    scottr 		return 1;
    544  1.15    scottr 	else
    545  1.15    scottr 		return 0;
    546  1.15    scottr }
    547  1.15    scottr 
    548  1.15    scottr /*
    549  1.15    scottr  * Search through the signal sources in the channel, and
    550  1.15    scottr  * pick the best one for the baud rate requested. Return
    551  1.15    scottr  * a -1 if not achievable in tolerance. Otherwise return 0
    552  1.15    scottr  * and fill in the values.
    553  1.15    scottr  *
    554  1.15    scottr  * This routine draws inspiration from the Atari port's zs.c
    555  1.15    scottr  * driver in NetBSD 1.1 which did the same type of source switching.
    556  1.15    scottr  * Tolerance code inspired by comspeed routine in isa/com.c.
    557  1.15    scottr  *
    558  1.15    scottr  * By Bill Studenmund, 1996-05-12
    559  1.15    scottr  */
    560  1.15    scottr int
    561  1.15    scottr zs_set_speed(cs, bps)
    562  1.15    scottr 	struct zs_chanstate *cs;
    563  1.15    scottr 	int bps;	/* bits per second */
    564  1.15    scottr {
    565  1.15    scottr 	struct xzs_chanstate *xcs = (void *) cs;
    566  1.15    scottr 	int i, tc, tc0 = 0, tc1, s, sf = 0;
    567  1.15    scottr 	int src, rate0, rate1, err, tol;
    568  1.15    scottr 
    569  1.15    scottr 	if (bps == 0)
    570  1.15    scottr 		return (0);
    571  1.15    scottr 
    572  1.15    scottr 	src = -1;		/* no valid source yet */
    573  1.15    scottr 	tol = ZS_TOLERANCE;
    574  1.15    scottr 
    575  1.15    scottr 	/*
    576  1.15    scottr 	 * Step through all the sources and see which one matches
    577  1.15    scottr 	 * the best. A source has to match BETTER than tol to be chosen.
    578  1.15    scottr 	 * Thus if two sources give the same error, the first one will be
    579  1.15    scottr 	 * chosen. Also, allow for the possability that one source might run
    580  1.15    scottr 	 * both the BRG and the direct divider (i.e. RTxC).
    581  1.15    scottr 	 */
    582  1.15    scottr 	for (i=0; i < xcs->cs_clock_count; i++) {
    583  1.15    scottr 		if (xcs->cs_clocks[i].clk <= 0)
    584  1.15    scottr 			continue;	/* skip non-existant or bad clocks */
    585  1.15    scottr 		if (xcs->cs_clocks[i].flags & ZSC_BRG) {
    586  1.15    scottr 			/* check out BRG at /16 */
    587  1.15    scottr 			tc1 = BPS_TO_TCONST(xcs->cs_clocks[i].clk >> 4, bps);
    588  1.15    scottr 			if (tc1 >= 0) {
    589  1.15    scottr 				rate1 = TCONST_TO_BPS(xcs->cs_clocks[i].clk >> 4, tc1);
    590  1.15    scottr 				err = abs(((rate1 - bps)*1000)/bps);
    591  1.15    scottr 				if (err < tol) {
    592  1.15    scottr 					tol = err;
    593  1.15    scottr 					src = i;
    594  1.15    scottr 					sf = xcs->cs_clocks[i].flags & ~ZSC_DIV;
    595  1.15    scottr 					tc0 = tc1;
    596  1.15    scottr 					rate0 = rate1;
    597  1.15    scottr 				}
    598  1.15    scottr 			}
    599  1.15    scottr 		}
    600  1.15    scottr 		if (xcs->cs_clocks[i].flags & ZSC_DIV) {
    601  1.15    scottr 			/*
    602  1.15    scottr 			 * Check out either /1, /16, /32, or /64
    603  1.15    scottr 			 * Note: for /1, you'd better be using a synchronized
    604  1.15    scottr 			 * clock!
    605  1.15    scottr 			 */
    606  1.15    scottr 			int b0 = xcs->cs_clocks[i].clk, e0 = abs(b0-bps);
    607  1.15    scottr 			int b1 = b0 >> 4, e1 = abs(b1-bps);
    608  1.15    scottr 			int b2 = b1 >> 1, e2 = abs(b2-bps);
    609  1.15    scottr 			int b3 = b2 >> 1, e3 = abs(b3-bps);
    610  1.15    scottr 
    611  1.15    scottr 			if (e0 < e1 && e0 < e2 && e0 < e3) {
    612  1.15    scottr 				err = e0;
    613  1.15    scottr 				rate1 = b0;
    614  1.15    scottr 				tc1 = ZSWR4_CLK_X1;
    615  1.15    scottr 			} else if (e0 > e1 && e1 < e2  && e1 < e3) {
    616  1.15    scottr 				err = e1;
    617  1.15    scottr 				rate1 = b1;
    618  1.15    scottr 				tc1 = ZSWR4_CLK_X16;
    619  1.15    scottr 			} else if (e0 > e2 && e1 > e2 && e2 < e3) {
    620  1.15    scottr 				err = e2;
    621  1.15    scottr 				rate1 = b2;
    622  1.15    scottr 				tc1 = ZSWR4_CLK_X32;
    623  1.15    scottr 			} else {
    624  1.15    scottr 				err = e3;
    625  1.15    scottr 				rate1 = b3;
    626  1.15    scottr 				tc1 = ZSWR4_CLK_X64;
    627  1.15    scottr 			}
    628  1.15    scottr 
    629  1.15    scottr 			err = (err * 1000)/bps;
    630  1.15    scottr 			if (err < tol) {
    631  1.15    scottr 				tol = err;
    632  1.15    scottr 				src = i;
    633  1.15    scottr 				sf = xcs->cs_clocks[i].flags & ~ZSC_BRG;
    634  1.15    scottr 				tc0 = tc1;
    635  1.15    scottr 				rate0 = rate1;
    636  1.15    scottr 			}
    637  1.15    scottr 		}
    638  1.15    scottr 	}
    639  1.15    scottr #ifdef ZSMACDEBUG
    640  1.15    scottr 	zsprintf("Checking for rate %d. Found source #%d.\n",bps, src);
    641  1.15    scottr #endif
    642  1.15    scottr 	if (src == -1)
    643  1.15    scottr 		return (EINVAL); /* no can do */
    644  1.15    scottr 
    645  1.15    scottr 	/*
    646  1.15    scottr 	 * The M.I. layer likes to keep cs_brg_clk current, even though
    647  1.15    scottr 	 * we are the only ones who should be touching the BRG's rate.
    648  1.15    scottr 	 *
    649  1.15    scottr 	 * Note: we are assuming that any ZSC_EXTERN signal source comes in
    650  1.15    scottr 	 * on the RTxC pin. Correct for the mac68k obio zsc.
    651  1.15    scottr 	 */
    652  1.15    scottr 	if (sf & ZSC_EXTERN)
    653  1.15    scottr 		cs->cs_brg_clk = xcs->cs_clocks[i].clk >> 4;
    654  1.15    scottr 	else
    655  1.15    scottr 		cs->cs_brg_clk = ZS_STD_BRG;
    656  1.15    scottr 
    657  1.15    scottr 	/*
    658  1.15    scottr 	 * Now we have a source, so set it up.
    659  1.15    scottr 	 */
    660  1.15    scottr 	s = splzs();
    661  1.15    scottr 	xcs->cs_psource = src;
    662  1.15    scottr 	xcs->cs_pclk_flag = sf;
    663  1.15    scottr 	bps = rate0;
    664  1.15    scottr 	if (sf & ZSC_BRG) {
    665  1.15    scottr 		cs->cs_preg[4] = ZSWR4_CLK_X16;
    666  1.15    scottr 		cs->cs_preg[11]= ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD;
    667  1.15    scottr 		if (sf & ZSC_PCLK) {
    668  1.15    scottr 			cs->cs_preg[14] = ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK;
    669  1.15    scottr 		} else {
    670  1.15    scottr 			cs->cs_preg[14] = ZSWR14_BAUD_ENA;
    671   1.1    briggs 		}
    672  1.15    scottr 		tc = tc0;
    673  1.15    scottr 	} else {
    674  1.15    scottr 		cs->cs_preg[4] = tc0;
    675  1.15    scottr 		if (sf & ZSC_RTXDIV) {
    676  1.15    scottr 			cs->cs_preg[11] = ZSWR11_RXCLK_RTXC | ZSWR11_TXCLK_RTXC;
    677  1.15    scottr 		} else {
    678  1.15    scottr 			cs->cs_preg[11] = ZSWR11_RXCLK_TRXC | ZSWR11_TXCLK_TRXC;
    679  1.15    scottr 		}
    680  1.15    scottr 		cs->cs_preg[14]= 0;
    681  1.15    scottr 		tc = 0xffff;
    682   1.1    briggs 	}
    683  1.15    scottr 	/* Set the BAUD rate divisor. */
    684  1.15    scottr 	cs->cs_preg[12] = tc;
    685  1.15    scottr 	cs->cs_preg[13] = tc >> 8;
    686  1.15    scottr 	splx(s);
    687  1.15    scottr 
    688  1.15    scottr #ifdef ZSMACDEBUG
    689  1.15    scottr 	zsprintf("Rate is %7d, tc is %7d, source no. %2d, flags %4x\n", \
    690  1.15    scottr 	    bps, tc, src, sf);
    691  1.15    scottr 	zsprintf("Registers are: 4 %x, 11 %x, 14 %x\n\n",
    692  1.15    scottr 		cs->cs_preg[4], cs->cs_preg[11], cs->cs_preg[14]);
    693  1.15    scottr #endif
    694  1.15    scottr 
    695  1.15    scottr 	cs->cs_preg[5] |= ZSWR5_RTS;	/* Make sure the drivers are on! */
    696  1.15    scottr 
    697  1.15    scottr 	/* Caller will stuff the pending registers. */
    698  1.15    scottr 	return (0);
    699  1.15    scottr }
    700  1.15    scottr 
    701  1.15    scottr int
    702  1.15    scottr zs_set_modes(cs, cflag)
    703  1.15    scottr 	struct zs_chanstate *cs;
    704  1.15    scottr 	int cflag;	/* bits per second */
    705  1.15    scottr {
    706  1.15    scottr 	struct xzs_chanstate *xcs = (void*)cs;
    707  1.15    scottr 	int s;
    708  1.15    scottr 
    709  1.15    scottr 	/*
    710  1.15    scottr 	 * Make sure we don't enable hfc on a signal line we're ignoring.
    711  1.15    scottr 	 * As we enable CTS interrupts only if we have CRTSCTS or CDTRCTS,
    712  1.15    scottr 	 * this code also effectivly turns off ZSWR15_CTS_IE.
    713  1.15    scottr 	 *
    714  1.15    scottr 	 * Also, disable DCD interrupts if we've been told to ignore
    715  1.15    scottr 	 * the DCD pin. Happens on mac68k because the input line for
    716  1.15    scottr 	 * DCD can also be used as a clock input.  (Just set CLOCAL.)
    717  1.15    scottr 	 *
    718  1.15    scottr 	 * If someone tries to turn an invalid flow mode on, Just Say No
    719  1.15    scottr 	 * (Suggested by gwr)
    720  1.15    scottr 	 */
    721  1.15    scottr 	if ((cflag & CDTRCTS) && (cflag & (CRTSCTS | MDMBUF)))
    722  1.15    scottr 		return (EINVAL);
    723  1.15    scottr 	if (xcs->cs_hwflags & ZS_HWFLAG_NO_DCD) {
    724  1.15    scottr 		if (cflag & MDMBUF)
    725  1.15    scottr 			return (EINVAL);
    726  1.15    scottr 		cflag |= CLOCAL;
    727  1.16   mycroft 	}
    728  1.15    scottr 	if ((xcs->cs_hwflags & ZS_HWFLAG_NO_CTS) && (cflag & (CRTSCTS | CDTRCTS)))
    729  1.15    scottr 		return (EINVAL);
    730  1.15    scottr 
    731  1.15    scottr 	/*
    732  1.15    scottr 	 * Output hardware flow control on the chip is horrendous:
    733  1.15    scottr 	 * if carrier detect drops, the receiver is disabled, and if
    734  1.15    scottr 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    735  1.15    scottr 	 * Therefore, NEVER set the HFC bit, and instead use the
    736  1.15    scottr 	 * status interrupt to detect CTS changes.
    737  1.15    scottr 	 */
    738  1.15    scottr 	s = splzs();
    739  1.16   mycroft 	if ((cflag & (CLOCAL | MDMBUF)) != 0)
    740  1.16   mycroft 		cs->cs_rr0_dcd = 0;
    741  1.16   mycroft 	else
    742  1.16   mycroft 		cs->cs_rr0_dcd = ZSRR0_DCD;
    743  1.15    scottr 	/*
    744  1.15    scottr 	 * The mac hardware only has one output, DTR (HSKo in Mac
    745  1.15    scottr 	 * parlance). In HFC mode, we use it for the functions
    746  1.15    scottr 	 * typically served by RTS and DTR on other ports, so we
    747  1.15    scottr 	 * have to fake the upper layer out some.
    748  1.15    scottr 	 *
    749  1.15    scottr 	 * CRTSCTS we use CTS as an input which tells us when to shut up.
    750  1.15    scottr 	 * We make no effort to shut up the other side of the connection.
    751  1.15    scottr 	 * DTR is used to hang up the modem.
    752  1.15    scottr 	 *
    753  1.15    scottr 	 * In CDTRCTS, we use CTS to tell us to stop, but we use DTR to
    754  1.15    scottr 	 * shut up the other side.
    755  1.15    scottr 	 */
    756  1.16   mycroft 	if ((cflag & CRTSCTS) != 0) {
    757  1.15    scottr 		cs->cs_wr5_dtr = ZSWR5_DTR;
    758  1.15    scottr 		cs->cs_wr5_rts = 0;
    759  1.15    scottr 		cs->cs_rr0_cts = ZSRR0_CTS;
    760  1.16   mycroft 	} else if ((cflag & CDTRCTS) != 0) {
    761  1.15    scottr 		cs->cs_wr5_dtr = 0;
    762  1.15    scottr 		cs->cs_wr5_rts = ZSWR5_DTR;
    763  1.15    scottr 		cs->cs_rr0_cts = ZSRR0_CTS;
    764  1.16   mycroft 	} else if ((cflag & MDMBUF) != 0) {
    765  1.16   mycroft 		cs->cs_wr5_dtr = 0;
    766  1.16   mycroft 		cs->cs_wr5_rts = ZSWR5_DTR;
    767  1.16   mycroft 		cs->cs_rr0_cts = ZSRR0_DCD;
    768  1.15    scottr 	} else {
    769  1.15    scottr 		cs->cs_wr5_dtr = ZSWR5_DTR;
    770  1.15    scottr 		cs->cs_wr5_rts = 0;
    771  1.15    scottr 		cs->cs_rr0_cts = 0;
    772  1.15    scottr 	}
    773  1.15    scottr 	splx(s);
    774  1.15    scottr 
    775  1.15    scottr 	/* Caller will stuff the pending registers. */
    776  1.15    scottr 	return (0);
    777   1.1    briggs }
    778   1.1    briggs 
    779   1.1    briggs 
    780   1.1    briggs /*
    781   1.1    briggs  * Read or write the chip with suitable delays.
    782  1.15    scottr  * MacII hardware has the delay built in.
    783  1.15    scottr  * No need for extra delay. :-) However, some clock-chirped
    784  1.15    scottr  * macs, or zsc's on serial add-on boards might need it.
    785   1.1    briggs  */
    786   1.1    briggs #define	ZS_DELAY()
    787   1.1    briggs 
    788   1.1    briggs u_char
    789   1.1    briggs zs_read_reg(cs, reg)
    790   1.1    briggs 	struct zs_chanstate *cs;
    791   1.1    briggs 	u_char reg;
    792   1.1    briggs {
    793   1.1    briggs 	u_char val;
    794   1.1    briggs 
    795   1.1    briggs 	*cs->cs_reg_csr = reg;
    796   1.1    briggs 	ZS_DELAY();
    797   1.1    briggs 	val = *cs->cs_reg_csr;
    798   1.1    briggs 	ZS_DELAY();
    799   1.1    briggs 	return val;
    800   1.1    briggs }
    801   1.1    briggs 
    802   1.1    briggs void
    803   1.1    briggs zs_write_reg(cs, reg, val)
    804   1.1    briggs 	struct zs_chanstate *cs;
    805   1.1    briggs 	u_char reg, val;
    806   1.1    briggs {
    807   1.1    briggs 	*cs->cs_reg_csr = reg;
    808   1.1    briggs 	ZS_DELAY();
    809   1.1    briggs 	*cs->cs_reg_csr = val;
    810   1.1    briggs 	ZS_DELAY();
    811   1.1    briggs }
    812   1.1    briggs 
    813   1.1    briggs u_char zs_read_csr(cs)
    814   1.1    briggs 	struct zs_chanstate *cs;
    815   1.1    briggs {
    816  1.20    scottr 	u_char val;
    817   1.1    briggs 
    818  1.15    scottr 	val = *cs->cs_reg_csr;
    819  1.15    scottr 	ZS_DELAY();
    820   1.1    briggs 	/* make up for the fact CTS is wired backwards */
    821  1.15    scottr 	val ^= ZSRR0_CTS;
    822  1.15    scottr 	return val;
    823   1.1    briggs }
    824   1.1    briggs 
    825  1.15    scottr void  zs_write_csr(cs, val)
    826   1.1    briggs 	struct zs_chanstate *cs;
    827  1.15    scottr 	u_char val;
    828   1.1    briggs {
    829  1.15    scottr 	/* Note, the csr does not write CTS... */
    830  1.15    scottr 	*cs->cs_reg_csr = val;
    831   1.1    briggs 	ZS_DELAY();
    832   1.1    briggs }
    833   1.1    briggs 
    834  1.15    scottr u_char zs_read_data(cs)
    835   1.1    briggs 	struct zs_chanstate *cs;
    836   1.1    briggs {
    837  1.20    scottr 	u_char val;
    838  1.15    scottr 
    839  1.15    scottr 	val = *cs->cs_reg_data;
    840   1.1    briggs 	ZS_DELAY();
    841  1.15    scottr 	return val;
    842   1.1    briggs }
    843   1.1    briggs 
    844   1.1    briggs void  zs_write_data(cs, val)
    845   1.1    briggs 	struct zs_chanstate *cs;
    846   1.1    briggs 	u_char val;
    847   1.1    briggs {
    848   1.1    briggs 	*cs->cs_reg_data = val;
    849   1.1    briggs 	ZS_DELAY();
    850   1.1    briggs }
    851   1.1    briggs 
    852   1.1    briggs /****************************************************************
    853  1.15    scottr  * Console support functions (mac68k specific!)
    854  1.15    scottr  * Note: this code is allowed to know about the layout of
    855  1.15    scottr  * the chip registers, and uses that to keep things simple.
    856  1.15    scottr  * XXX - I think I like the mvme167 code better. -gwr
    857  1.15    scottr  * XXX - Well :-P  :-)  -wrs
    858   1.1    briggs  ****************************************************************/
    859   1.1    briggs 
    860   1.1    briggs #define zscnpollc	nullcnpollc
    861   1.1    briggs cons_decl(zs);
    862   1.1    briggs 
    863   1.1    briggs static void	zscnsetup __P((void));
    864   1.1    briggs extern int	zsopen __P(( dev_t dev, int flags, int mode, struct proc *p));
    865   1.1    briggs 
    866   1.1    briggs /*
    867   1.1    briggs  * Console functions.
    868   1.1    briggs  */
    869   1.1    briggs 
    870   1.1    briggs /*
    871   1.1    briggs  * This code modled after the zs_setparam routine in zskgdb
    872   1.1    briggs  * It sets the console unit to a known state so we can output
    873   1.1    briggs  * correctly.
    874   1.1    briggs  */
    875   1.1    briggs static void
    876   1.1    briggs zscnsetup()
    877   1.1    briggs {
    878  1.15    scottr 	struct xzs_chanstate xcs;
    879  1.15    scottr 	struct zs_chanstate *cs;
    880   1.1    briggs 	struct zschan *zc;
    881   1.1    briggs 	int    tconst, s;
    882  1.15    scottr 
    883   1.1    briggs 	/* Setup temporary chanstate. */
    884  1.15    scottr 	bzero((caddr_t)&xcs, sizeof(xcs));
    885  1.15    scottr 	cs = &xcs.xzs_cs;
    886   1.1    briggs 	zc = zs_conschan;
    887  1.15    scottr 	cs->cs_reg_csr  = &zc->zc_csr;
    888  1.15    scottr 	cs->cs_reg_data = &zc->zc_data;
    889  1.15    scottr 	cs->cs_channel = zs_consunit;
    890  1.15    scottr 	cs->cs_brg_clk = ZS_STD_BRG;
    891  1.15    scottr 
    892  1.15    scottr 	bcopy(zs_init_reg, cs->cs_preg, 16);
    893  1.15    scottr 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    894  1.15    scottr 	cs->cs_preg[15] = ZSWR15_BREAK_IE;
    895  1.15    scottr 	tconst = BPS_TO_TCONST(cs->cs_brg_clk,
    896  1.15    scottr 		zs_defspeed[0][zs_consunit]);
    897  1.15    scottr 	cs->cs_preg[12] = tconst;
    898  1.15    scottr 	cs->cs_preg[13] = tconst >> 8;
    899  1.15    scottr 	/* can't use zs_set_speed as we haven't set up the
    900  1.15    scottr 	 * signal sources, and it's not worth it for now
    901  1.15    scottr 	 */
    902  1.15    scottr 
    903  1.15    scottr 	cs->cs_preg[9] &= ~ZSWR9_MASTER_IE;
    904  1.15    scottr 		/* no interrupts until later, after attach. */
    905  1.15    scottr 	s = splhigh();
    906  1.15    scottr 	zs_loadchannelregs(cs);
    907  1.15    scottr 	splx(s);
    908   1.1    briggs }
    909   1.1    briggs 
    910   1.1    briggs /*
    911   1.1    briggs  * zscnprobe is the routine which gets called as the kernel is trying to
    912   1.1    briggs  * figure out where the console should be. Each io driver which might
    913   1.1    briggs  * be the console (as defined in mac68k/conf.c) gets probed. The probe
    914   1.1    briggs  * fills in the consdev structure. Important parts are the device #,
    915   1.1    briggs  * and the console priority. Values are CN_DEAD (don't touch me),
    916   1.1    briggs  * CN_NORMAL (I'm here, but elsewhere might be better), CN_INTERNAL
    917   1.1    briggs  * (the video, better than CN_NORMAL), and CN_REMOTE (pick me!)
    918   1.1    briggs  *
    919   1.1    briggs  * As the mac's a bit different, we do extra work here. We mainly check
    920  1.15    scottr  * to see if we have serial echo going on. Also chould check for default
    921  1.15    scottr  * speeds.
    922   1.1    briggs  */
    923   1.1    briggs void
    924   1.1    briggs zscnprobe(struct consdev * cp)
    925   1.1    briggs {
    926  1.15    scottr 	extern u_long   IOBase;
    927  1.15    scottr 	int     maj, unit, i;
    928   1.1    briggs 
    929  1.15    scottr 	for (maj = 0; maj < nchrdev; maj++) {
    930  1.15    scottr 		if (cdevsw[maj].d_open == zsopen) {
    931  1.15    scottr 			break;
    932  1.15    scottr 		}
    933  1.15    scottr 	}
    934  1.15    scottr 	if (maj != nchrdev) {
    935  1.15    scottr 		cp->cn_pri = CN_NORMAL;		 /* Lower than CN_INTERNAL */
    936  1.15    scottr 		if (mac68k_machine.serial_console != 0) {
    937  1.15    scottr 			cp->cn_pri = CN_REMOTE;	 /* Higher than CN_INTERNAL */
    938  1.15    scottr 			mac68k_machine.serial_boot_echo =0;
    939  1.15    scottr 		}
    940  1.15    scottr 
    941  1.15    scottr 		unit = (mac68k_machine.serial_console == 1) ? 0 : 1;
    942  1.15    scottr 		zs_consunit = unit;
    943  1.15    scottr 		zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
    944  1.15    scottr 
    945  1.15    scottr 		mac68k_zsdev = cp->cn_dev = makedev(maj, unit);
    946  1.15    scottr 	}
    947  1.15    scottr 	if (mac68k_machine.serial_boot_echo) {
    948  1.15    scottr 		/*
    949  1.15    scottr 		 * at this point, we know that we don't have a serial
    950  1.15    scottr 		 * console, but are doing echo
    951  1.15    scottr 		 */
    952  1.15    scottr 		zs_conschan = (struct zschan *) -1; /* dummy flag for zs_init() */
    953   1.1    briggs 		zs_consunit = 1;
    954   1.1    briggs 		zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
    955  1.15    scottr 	}
    956  1.15    scottr 
    957  1.15    scottr 	if ((i = mac68k_machine.modem_d_speed) > 0) {
    958  1.15    scottr 		if (zs_cn_check_speed(i))
    959  1.15    scottr 			zs_defspeed[0][0] = i;
    960  1.15    scottr 	}
    961  1.15    scottr 	if ((i = mac68k_machine.print_d_speed) > 0) {
    962  1.15    scottr 		if (zs_cn_check_speed(i))
    963  1.15    scottr 			zs_defspeed[0][1] = i;
    964  1.15    scottr 	}
    965  1.15    scottr 	mac68k_set_io_offsets(IOBase);
    966  1.15    scottr 	zs_init();
    967  1.15    scottr 	/*
    968  1.15    scottr 	 * zsinit will set up the addresses of the scc. It will also, if
    969  1.15    scottr 	 * zs_conschan != 0, calculate the new address of the conschan for
    970  1.15    scottr 	 * unit zs_consunit. So if we are (or think we are) going to use the
    971  1.15    scottr 	 * chip for console I/O, we just set up the internal addresses for it.
    972  1.15    scottr 	 *
    973  1.15    scottr 	 * Now turn off interrupts for the chip. Note: this code piece is the
    974  1.15    scottr 	 * only vestage of the NetBSD 1.0 ser driver. :-)
    975  1.15    scottr 	 */
    976  1.15    scottr 	sccA[2] = 9; sccA[2] = 0;	/* write 0 to register 9, clearing MIE */
    977  1.15    scottr 
    978  1.15    scottr 	if (mac68k_machine.serial_boot_echo)
    979  1.15    scottr 		zscnsetup();
    980  1.15    scottr 	return;
    981   1.1    briggs }
    982   1.1    briggs 
    983   1.1    briggs void
    984   1.1    briggs zscninit(struct consdev * cp)
    985   1.1    briggs {
    986   1.1    briggs 
    987   1.7    scottr 	zs_hwflags[0][zs_consunit] = ZS_HWFLAG_CONSOLE;
    988  1.15    scottr 	/*
    989   1.1    briggs 	 * zsinit will set up the addresses of the scc. It will also, if
    990   1.1    briggs 	 * zs_conschan != 0, calculate the new address of the conschan for
    991   1.1    briggs 	 * unit zs_consunit. So zs_init implicitly sets zs_conschan to the right
    992   1.1    briggs 	 * number. :-)
    993  1.15    scottr 	 */
    994  1.15    scottr 	zscnsetup();
    995  1.15    scottr 	printf("\nNetBSD/mac68k console\n");
    996   1.1    briggs }
    997   1.1    briggs 
    998   1.1    briggs 
    999   1.1    briggs /*
   1000   1.1    briggs  * Polled input char.
   1001   1.1    briggs  */
   1002  1.20    scottr int
   1003  1.20    scottr zs_getc(arg)
   1004  1.20    scottr 	void *arg;
   1005   1.1    briggs {
   1006  1.20    scottr 	volatile struct zschan *zc = arg;
   1007  1.20    scottr 	int s, c, rr0;
   1008   1.1    briggs 
   1009   1.1    briggs 	s = splhigh();
   1010   1.1    briggs 	/* Wait for a character to arrive. */
   1011   1.1    briggs 	do {
   1012   1.1    briggs 		rr0 = zc->zc_csr;
   1013   1.1    briggs 		ZS_DELAY();
   1014   1.1    briggs 	} while ((rr0 & ZSRR0_RX_READY) == 0);
   1015   1.1    briggs 
   1016   1.1    briggs 	c = zc->zc_data;
   1017   1.1    briggs 	ZS_DELAY();
   1018   1.1    briggs 	splx(s);
   1019   1.1    briggs 
   1020   1.1    briggs 	/*
   1021   1.1    briggs 	 * This is used by the kd driver to read scan codes,
   1022   1.1    briggs 	 * so don't translate '\r' ==> '\n' here...
   1023   1.1    briggs 	 */
   1024   1.1    briggs 	return (c);
   1025   1.1    briggs }
   1026   1.1    briggs 
   1027   1.1    briggs /*
   1028   1.1    briggs  * Polled output char.
   1029   1.1    briggs  */
   1030  1.20    scottr void
   1031  1.20    scottr zs_putc(arg, c)
   1032  1.20    scottr 	void *arg;
   1033   1.1    briggs 	int c;
   1034   1.1    briggs {
   1035  1.20    scottr 	volatile struct zschan *zc = arg;
   1036  1.20    scottr 	int s, rr0;
   1037  1.20    scottr 	long wait = 0;
   1038   1.1    briggs 
   1039   1.1    briggs 	s = splhigh();
   1040   1.1    briggs 	/* Wait for transmitter to become ready. */
   1041   1.1    briggs 	do {
   1042   1.1    briggs 		rr0 = zc->zc_csr;
   1043   1.1    briggs 		ZS_DELAY();
   1044   1.1    briggs 	} while (((rr0 & ZSRR0_TX_READY) == 0) && (wait++ < 1000000));
   1045   1.1    briggs 
   1046   1.1    briggs 	if ((rr0 & ZSRR0_TX_READY) != 0) {
   1047   1.1    briggs 		zc->zc_data = c;
   1048   1.1    briggs 		ZS_DELAY();
   1049   1.1    briggs 	}
   1050   1.1    briggs 	splx(s);
   1051   1.1    briggs }
   1052   1.1    briggs 
   1053   1.1    briggs 
   1054   1.1    briggs /*
   1055   1.1    briggs  * Polled console input putchar.
   1056   1.1    briggs  */
   1057   1.1    briggs int
   1058   1.1    briggs zscngetc(dev)
   1059   1.1    briggs 	dev_t dev;
   1060   1.1    briggs {
   1061  1.20    scottr 	struct zschan *zc = zs_conschan;
   1062  1.20    scottr 	int c;
   1063   1.1    briggs 
   1064   1.1    briggs 	c = zs_getc(zc);
   1065   1.1    briggs 	return (c);
   1066   1.1    briggs }
   1067   1.1    briggs 
   1068   1.1    briggs /*
   1069   1.1    briggs  * Polled console output putchar.
   1070   1.1    briggs  */
   1071   1.1    briggs void
   1072   1.1    briggs zscnputc(dev, c)
   1073   1.1    briggs 	dev_t dev;
   1074   1.1    briggs 	int c;
   1075   1.1    briggs {
   1076  1.20    scottr 	struct zschan *zc = zs_conschan;
   1077   1.1    briggs 
   1078   1.1    briggs 	zs_putc(zc, c);
   1079   1.1    briggs }
   1080   1.1    briggs 
   1081   1.1    briggs 
   1082   1.1    briggs 
   1083   1.1    briggs /*
   1084   1.1    briggs  * Handle user request to enter kernel debugger.
   1085   1.1    briggs  */
   1086   1.1    briggs void
   1087  1.15    scottr zs_abort(cs)
   1088  1.15    scottr 	struct zs_chanstate *cs;
   1089   1.1    briggs {
   1090  1.15    scottr 	volatile struct zschan *zc = zs_conschan;
   1091   1.1    briggs 	int rr0;
   1092  1.20    scottr 	long wait = 0;
   1093   1.8    scottr 
   1094  1.15    scottr 	if (zs_cons_canabort == 0)
   1095   1.8    scottr 		return;
   1096   1.1    briggs 
   1097   1.1    briggs 	/* Wait for end of break to avoid PROM abort. */
   1098   1.1    briggs 	do {
   1099   1.1    briggs 		rr0 = zc->zc_csr;
   1100   1.1    briggs 		ZS_DELAY();
   1101   1.1    briggs 	} while ((rr0 & ZSRR0_BREAK) && (wait++ < ZSABORT_DELAY));
   1102   1.1    briggs 
   1103   1.1    briggs 	if (wait > ZSABORT_DELAY) {
   1104  1.15    scottr 		zs_cons_canabort = 0;
   1105   1.1    briggs 	/* If we time out, turn off the abort ability! */
   1106   1.1    briggs 	}
   1107   1.1    briggs 
   1108  1.17    scottr #ifdef DDB
   1109   1.1    briggs 	Debugger();
   1110  1.17    scottr #endif
   1111   1.1    briggs }
   1112  1.15    scottr 
   1113