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