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