Home | History | Annotate | Line # | Download | only in obio
zs.c revision 1.21
      1  1.21       ad /*	$NetBSD: zs.c,v 1.21 2007/11/26 23:29:36 ad Exp $	*/
      2   1.1      wdk 
      3   1.1      wdk /*-
      4   1.1      wdk  * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
      5   1.1      wdk  * All rights reserved.
      6   1.1      wdk  *
      7   1.1      wdk  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      wdk  * by Gordon W. Ross and Wayne Knowles
      9   1.1      wdk  *
     10   1.1      wdk  * Redistribution and use in source and binary forms, with or without
     11   1.1      wdk  * modification, are permitted provided that the following conditions
     12   1.1      wdk  * are met:
     13   1.1      wdk  * 1. Redistributions of source code must retain the above copyright
     14   1.1      wdk  *    notice, this list of conditions and the following disclaimer.
     15   1.1      wdk  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      wdk  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      wdk  *    documentation and/or other materials provided with the distribution.
     18   1.1      wdk  * 3. All advertising materials mentioning features or use of this software
     19   1.1      wdk  *    must display the following acknowledgement:
     20   1.1      wdk  *        This product includes software developed by the NetBSD
     21   1.1      wdk  *        Foundation, Inc. and its contributors.
     22   1.1      wdk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1      wdk  *    contributors may be used to endorse or promote products derived
     24   1.1      wdk  *    from this software without specific prior written permission.
     25   1.1      wdk  *
     26   1.1      wdk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1      wdk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1      wdk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1      wdk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1      wdk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1      wdk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1      wdk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1      wdk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1      wdk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1      wdk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1      wdk  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1      wdk  */
     38   1.1      wdk 
     39   1.1      wdk /*
     40   1.1      wdk  * Zilog Z8530 Dual UART driver (machine-dependent part)
     41   1.1      wdk  *
     42   1.1      wdk  * Runs two serial lines per chip using slave drivers.
     43   1.1      wdk  * Plain tty/async lines use the zs_async slave.
     44   1.1      wdk  */
     45  1.17    lukem 
     46  1.17    lukem #include <sys/cdefs.h>
     47  1.21       ad __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.21 2007/11/26 23:29:36 ad Exp $");
     48   1.1      wdk 
     49   1.1      wdk #include "opt_ddb.h"
     50  1.11    lukem #include "opt_kgdb.h"
     51   1.1      wdk 
     52   1.1      wdk #include <sys/param.h>
     53   1.1      wdk #include <sys/systm.h>
     54   1.1      wdk #include <sys/conf.h>
     55   1.1      wdk #include <sys/device.h>
     56   1.1      wdk #include <sys/file.h>
     57   1.1      wdk #include <sys/ioctl.h>
     58   1.1      wdk #include <sys/kernel.h>
     59   1.1      wdk #include <sys/proc.h>
     60   1.1      wdk #include <sys/tty.h>
     61   1.1      wdk #include <sys/time.h>
     62   1.1      wdk #include <sys/syslog.h>
     63  1.21       ad #include <sys/cpu.h>
     64  1.21       ad #include <sys/intr.h>
     65   1.1      wdk 
     66   1.1      wdk #include <machine/mainboard.h>
     67   1.1      wdk #include <machine/autoconf.h>
     68   1.5     matt #include <machine/prom.h>
     69   1.1      wdk #include <machine/z8530var.h>
     70   1.1      wdk 
     71   1.1      wdk #include <dev/cons.h>
     72   1.1      wdk #include <dev/ic/z8530reg.h>
     73   1.1      wdk 
     74   1.1      wdk #include "zsc.h"	/* NZSC */
     75   1.1      wdk #define NZS NZSC
     76   1.1      wdk 
     77   1.1      wdk /* Make life easier for the initialized arrays here. */
     78   1.1      wdk #if NZS < 2
     79   1.1      wdk #undef  NZS
     80   1.1      wdk #define NZS 2
     81   1.1      wdk #endif
     82   1.1      wdk 
     83   1.1      wdk /*
     84   1.1      wdk  * Some warts needed by z8530tty.c -
     85   1.1      wdk  * The default parity REALLY needs to be the same as the PROM uses,
     86   1.1      wdk  * or you can not see messages done with printf during boot-up...
     87   1.1      wdk  */
     88   1.1      wdk int zs_def_cflag = (CREAD | CS8 | HUPCL);
     89   1.1      wdk 
     90   1.6      wdk 
     91   1.6      wdk #define PCLK		10000000	/* PCLK pin input clock rate */
     92   1.6      wdk 
     93   1.7      wdk #ifndef ZS_DEFSPEED
     94   1.6      wdk #define ZS_DEFSPEED	9600
     95   1.7      wdk #endif
     96   1.1      wdk 
     97   1.1      wdk /*
     98   1.1      wdk  * Define interrupt levels.
     99   1.1      wdk  */
    100   1.1      wdk #define ZSHARD_PRI 64
    101   1.1      wdk 
    102   1.6      wdk /* Register recovery time is 3.5 to 4 PCLK Cycles */
    103   1.6      wdk #define ZS_RECOVERY	1		/* 1us = 10 PCLK Cycles */
    104   1.6      wdk #define ZS_DELAY()	delay(ZS_RECOVERY)
    105   1.5     matt 
    106   1.1      wdk /* The layout of this is hardware-dependent (padding, order). */
    107   1.1      wdk struct zschan {
    108   1.1      wdk 	u_char   pad1[3];
    109   1.1      wdk 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
    110   1.1      wdk 	u_char   pad2[3];
    111   1.1      wdk 	volatile u_char	zc_data;	/* data */
    112   1.1      wdk };
    113   1.1      wdk struct zsdevice {
    114   1.1      wdk 	/* Yes, they are backwards. */
    115   1.1      wdk 	struct	zschan zs_chan_b;
    116   1.1      wdk 	struct	zschan zs_chan_a;
    117   1.1      wdk };
    118   1.1      wdk 
    119   1.6      wdk /* Return the byte offset of element within a structure */
    120   1.6      wdk #define OFFSET(struct_def, el)		((size_t)&((struct_def *)0)->el)
    121   1.6      wdk 
    122   1.6      wdk #define ZS_CHAN_A	OFFSET(struct zsdevice, zs_chan_a)
    123   1.6      wdk #define ZS_CHAN_B	OFFSET(struct zsdevice, zs_chan_b)
    124   1.6      wdk #define ZS_REG_CSR	OFFSET(struct zschan, zc_csr)
    125   1.6      wdk #define ZS_REG_DATA	OFFSET(struct zschan, zc_data)
    126   1.6      wdk static int zs_chan_offset[] = {ZS_CHAN_A, ZS_CHAN_B};
    127   1.6      wdk 
    128   1.1      wdk /* Flags from cninit() */
    129   1.1      wdk static int zs_hwflags[NZS][2];
    130   1.1      wdk 
    131   1.1      wdk /* Default speed for all channels */
    132   1.6      wdk static int zs_defspeed = ZS_DEFSPEED;
    133   1.6      wdk static volatile int zssoftpending;
    134   1.1      wdk 
    135   1.1      wdk static u_char zs_init_reg[16] = {
    136   1.6      wdk 	0,				/* 0: CMD (reset, etc.) */
    137   1.6      wdk 	0,				/* 1: No interrupts yet. */
    138   1.6      wdk 	ZSHARD_PRI,			/* 2: IVECT */
    139   1.1      wdk 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
    140   1.6      wdk 	ZSWR4_CLK_X16 | ZSWR4_ONESB,
    141   1.1      wdk 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
    142   1.6      wdk 	0,				/* 6: TXSYNC/SYNCLO */
    143   1.6      wdk 	0,				/* 7: RXSYNC/SYNCHI */
    144   1.6      wdk 	0,				/* 8: alias for data port */
    145   1.1      wdk 	ZSWR9_MASTER_IE,
    146   1.6      wdk 	0,				/*10: Misc. TX/RX control bits */
    147   1.6      wdk 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD | ZSWR11_TRXC_OUT_ENA,
    148   1.6      wdk 	BPS_TO_TCONST(PCLK/16, ZS_DEFSPEED), /*12: BAUDLO (default=9600) */
    149   1.6      wdk 	0,				/*13: BAUDHI (default=9600) */
    150   1.1      wdk 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    151   1.1      wdk 	ZSWR15_BREAK_IE,
    152   1.1      wdk };
    153   1.1      wdk 
    154   1.1      wdk 
    155   1.1      wdk /****************************************************************
    156   1.1      wdk  * Autoconfig
    157   1.1      wdk  ****************************************************************/
    158   1.1      wdk 
    159   1.1      wdk /* Definition of the driver for autoconfig. */
    160   1.1      wdk static int	zs_match __P((struct device *, struct cfdata *, void *));
    161   1.1      wdk static void	zs_attach __P((struct device *, struct device *, void *));
    162   1.6      wdk static int	zs_print __P((void *, const char *name));
    163   1.1      wdk 
    164  1.14  thorpej CFATTACH_DECL(zsc, sizeof(struct zsc_softc),
    165  1.14  thorpej     zs_match, zs_attach, NULL, NULL);
    166   1.1      wdk 
    167   1.6      wdk extern struct	cfdriver zsc_cd;
    168   1.1      wdk 
    169   1.6      wdk static int	zshard __P((void *));
    170   1.9      wdk void		zssoft __P((void *));
    171   1.6      wdk static int	zs_get_speed __P((struct zs_chanstate *));
    172   1.7      wdk struct		zschan *zs_get_chan_addr (int zs_unit, int channel);
    173   1.7      wdk int		zs_getc __P((void *));
    174   1.7      wdk void		zs_putc __P((void *, int));
    175   1.1      wdk 
    176   1.1      wdk /*
    177   1.1      wdk  * Is the zs chip present?
    178   1.1      wdk  */
    179   1.1      wdk static int
    180   1.1      wdk zs_match(parent, cf, aux)
    181   1.1      wdk 	struct device *parent;
    182   1.1      wdk 	struct cfdata *cf;
    183   1.1      wdk 	void *aux;
    184   1.1      wdk {
    185   1.1      wdk 	struct confargs *ca = aux;
    186   1.1      wdk 	void *va;
    187   1.1      wdk 
    188   1.1      wdk 	if (strcmp(ca->ca_name, "zsc"))
    189   1.1      wdk 		return 0;
    190   1.1      wdk 
    191   1.1      wdk 	va = (void *)cf->cf_addr;
    192   1.1      wdk 
    193   1.1      wdk 	/* This returns -1 on a fault (bus error). */
    194   1.1      wdk 	if (badaddr(va, 1))
    195   1.1      wdk 		return 0;
    196   1.1      wdk 	return 1;
    197   1.1      wdk }
    198   1.1      wdk 
    199   1.1      wdk /*
    200   1.1      wdk  * Attach a found zs.
    201   1.1      wdk  *
    202   1.1      wdk  * Match slave number to zs unit number, so that misconfiguration will
    203   1.1      wdk  * not set up the keyboard as ttya, etc.
    204   1.1      wdk  */
    205   1.1      wdk static void
    206   1.1      wdk zs_attach(parent, self, aux)
    207   1.1      wdk 	struct device *parent;
    208   1.1      wdk 	struct device *self;
    209   1.1      wdk 	void *aux;
    210   1.1      wdk {
    211   1.1      wdk 	struct zsc_softc *zsc = (void *) self;
    212   1.1      wdk 	struct confargs *ca = aux;
    213   1.1      wdk 	struct zsc_attach_args zsc_args;
    214   1.1      wdk 	struct zs_chanstate *cs;
    215   1.6      wdk 	struct zs_channel *ch;
    216   1.6      wdk 	int    zs_unit, channel, s;
    217   1.1      wdk 
    218   1.1      wdk 	zsc->zsc_bustag = ca->ca_bustag;
    219   1.1      wdk 	if (bus_space_map(ca->ca_bustag, ca->ca_addr,
    220   1.1      wdk 			  sizeof(struct zsdevice),
    221   1.1      wdk 			  BUS_SPACE_MAP_LINEAR,
    222   1.1      wdk 			  &zsc->zsc_base) != 0) {
    223   1.1      wdk 		printf(": cannot map registers\n");
    224   1.1      wdk 		return;
    225   1.1      wdk 	}
    226   1.6      wdk 
    227  1.19  thorpej 	zs_unit = device_unit(&zsc->zsc_dev);
    228   1.1      wdk 	printf("\n");
    229   1.1      wdk 
    230   1.1      wdk 	/*
    231   1.1      wdk 	 * Initialize software state for each channel.
    232   1.1      wdk 	 */
    233   1.1      wdk 	for (channel = 0; channel < 2; channel++) {
    234   1.1      wdk 		zsc_args.channel = channel;
    235   1.1      wdk 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
    236   1.6      wdk 		ch = &zsc->zsc_cs_store[channel];
    237   1.6      wdk 		cs = zsc->zsc_cs[channel] = (struct zs_chanstate *)ch;
    238   1.1      wdk 
    239  1.20       ad 		zs_lock_init(cs);
    240   1.6      wdk 		cs->cs_reg_csr = NULL;
    241   1.6      wdk 		cs->cs_reg_data = NULL;
    242   1.1      wdk 		cs->cs_channel = channel;
    243   1.1      wdk 		cs->cs_private = NULL;
    244   1.1      wdk 		cs->cs_ops = &zsops_null;
    245   1.1      wdk 		cs->cs_brg_clk = PCLK / 16;
    246   1.1      wdk 
    247   1.6      wdk 		if (bus_space_subregion(ca->ca_bustag, zsc->zsc_base,
    248   1.6      wdk 					zs_chan_offset[channel],
    249   1.6      wdk 					sizeof(struct zschan),
    250   1.6      wdk 					&ch->cs_regs) != 0) {
    251   1.6      wdk 			printf(": cannot map regs\n");
    252   1.6      wdk 			return;
    253   1.6      wdk 		}
    254   1.6      wdk 		ch->cs_bustag = ca->ca_bustag;
    255   1.1      wdk 
    256  1.10      wdk 		memcpy(cs->cs_creg, zs_init_reg, 16);
    257  1.10      wdk 		memcpy(cs->cs_preg, zs_init_reg, 16);
    258   1.1      wdk 
    259   1.1      wdk 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
    260   1.1      wdk 			cs->cs_defspeed = zs_get_speed(cs);
    261   1.1      wdk 		else
    262   1.1      wdk 			cs->cs_defspeed = zs_defspeed;
    263   1.1      wdk 		cs->cs_defcflag = zs_def_cflag;
    264   1.1      wdk 
    265   1.1      wdk 		/* Make these correspond to cs_defcflag (-crtscts) */
    266   1.1      wdk 		cs->cs_rr0_dcd = ZSRR0_DCD;
    267   1.1      wdk 		cs->cs_rr0_cts = 0;
    268   1.1      wdk 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    269   1.1      wdk 		cs->cs_wr5_rts = 0;
    270   1.1      wdk 
    271   1.1      wdk 		/*
    272   1.1      wdk 		 * Clear the master interrupt enable.
    273   1.1      wdk 		 * The INTENA is common to both channels,
    274   1.1      wdk 		 * so just do it on the A channel.
    275   1.1      wdk 		 */
    276   1.1      wdk 		if (channel == 0) {
    277   1.1      wdk 			zs_write_reg(cs, 9, 0);
    278   1.1      wdk 		}
    279   1.1      wdk 		/*
    280   1.1      wdk 		 * Look for a child driver for this channel.
    281   1.1      wdk 		 * The child attach will setup the hardware.
    282   1.1      wdk 		 */
    283   1.1      wdk 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
    284   1.1      wdk 			/* No sub-driver.  Just reset it. */
    285   1.1      wdk 			u_char reset = (channel == 0) ?
    286   1.1      wdk 				ZSWR9_A_RESET : ZSWR9_B_RESET;
    287   1.1      wdk 
    288   1.1      wdk 			s = splhigh();
    289   1.1      wdk  			zs_write_reg(cs,  9, reset);
    290   1.1      wdk 			splx(s);
    291   1.1      wdk 		}
    292   1.1      wdk 	}
    293   1.1      wdk 
    294   1.9      wdk 
    295  1.21       ad 	zsc->sc_si = softint_establish(SOFTINT_SERIAL, zssoft, zsc);
    296   1.2      wdk 	bus_intr_establish(zsc->zsc_bustag, SYS_INTR_SCC0, 0, 0, zshard, NULL);
    297   1.1      wdk 
    298   1.1      wdk 	evcnt_attach_dynamic(&zsc->zs_intrcnt, EVCNT_TYPE_INTR, NULL,
    299   1.1      wdk 			     self->dv_xname, "intr");
    300   1.1      wdk 
    301   1.1      wdk 	/*
    302   1.1      wdk 	 * Set the master interrupt enable and interrupt vector.
    303   1.1      wdk 	 * (common to both channels, do it on A)
    304   1.1      wdk 	 */
    305   1.1      wdk 	cs = zsc->zsc_cs[0];
    306   1.1      wdk 	s = splhigh();
    307   1.1      wdk 	/* interrupt vector */
    308   1.1      wdk 	zs_write_reg(cs, 2, zs_init_reg[2]);
    309   1.1      wdk 	/* master interrupt control (enable) */
    310   1.1      wdk 	zs_write_reg(cs, 9, zs_init_reg[9]);
    311   1.1      wdk 	splx(s);
    312   1.1      wdk }
    313   1.1      wdk 
    314   1.1      wdk static int
    315   1.1      wdk zs_print(aux, name)
    316   1.1      wdk 	void *aux;
    317   1.1      wdk 	const char *name;
    318   1.1      wdk {
    319   1.1      wdk 	struct zsc_attach_args *args = aux;
    320   1.1      wdk 
    321   1.1      wdk 	if (name != NULL)
    322  1.15  thorpej 		aprint_normal("%s: ", name);
    323   1.1      wdk 
    324   1.1      wdk 	if (args->channel != -1)
    325  1.15  thorpej 		aprint_normal(" channel %d", args->channel);
    326   1.1      wdk 
    327   1.1      wdk 	return UNCONF;
    328   1.1      wdk }
    329   1.1      wdk 
    330   1.1      wdk /*
    331   1.1      wdk  * Our ZS chips all share a common, autovectored interrupt,
    332   1.1      wdk  * so we have to look at all of them on each interrupt.
    333   1.1      wdk  */
    334   1.2      wdk static int
    335   1.1      wdk zshard(arg)
    336   1.1      wdk 	void *arg;
    337   1.1      wdk {
    338   1.1      wdk 	register struct zsc_softc *zsc;
    339   1.1      wdk 	register int unit, rval, softreq;
    340   1.1      wdk 
    341   1.9      wdk 	rval = 0;
    342   1.1      wdk 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    343   1.1      wdk 		zsc = zsc_cd.cd_devs[unit];
    344   1.1      wdk 		if (zsc == NULL)
    345   1.1      wdk 			continue;
    346   1.1      wdk 		rval |= zsc_intr_hard(zsc);
    347   1.9      wdk 		softreq = zsc->zsc_cs[0]->cs_softreq;
    348   1.1      wdk 		softreq |= zsc->zsc_cs[1]->cs_softreq;
    349   1.9      wdk 		if (softreq && (zssoftpending == 0)) {
    350   1.9      wdk 		    zssoftpending = 1;
    351  1.21       ad 		    softint_schedule(zsc->sc_si);
    352   1.9      wdk 		}
    353   1.1      wdk 		zsc->zs_intrcnt.ev_count++;
    354   1.1      wdk 	}
    355  1.10      wdk 	return rval;
    356   1.1      wdk }
    357   1.1      wdk 
    358   1.1      wdk /*
    359   1.1      wdk  * Similar scheme as for zshard (look at all of them)
    360   1.1      wdk  */
    361   1.9      wdk void
    362   1.1      wdk zssoft(arg)
    363   1.1      wdk 	void *arg;
    364   1.1      wdk {
    365   1.1      wdk 	register struct zsc_softc *zsc;
    366   1.1      wdk 	register int s, unit;
    367   1.1      wdk 
    368   1.1      wdk 	/* This is not the only ISR on this IPL. */
    369   1.1      wdk 	if (zssoftpending == 0)
    370   1.1      wdk 		return;
    371   1.1      wdk 
    372   1.1      wdk 	/*
    373   1.1      wdk 	 * The soft intr. bit will be set by zshard only if
    374   1.1      wdk 	 * the variable zssoftpending is zero.  The order of
    375   1.1      wdk 	 * these next two statements prevents our clearing
    376   1.1      wdk 	 * the soft intr bit just after zshard has set it.
    377   1.1      wdk 	 */
    378   1.1      wdk 	/*isr_soft_clear(ZSSOFT_PRI);*/
    379   1.9      wdk 	zssoftpending = 0;
    380   1.1      wdk 
    381   1.1      wdk 	/* Make sure we call the tty layer at spltty. */
    382   1.1      wdk 	s = spltty();
    383   1.1      wdk 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
    384   1.1      wdk 		zsc = zsc_cd.cd_devs[unit];
    385   1.1      wdk 		if (zsc == NULL)
    386   1.1      wdk 			continue;
    387   1.1      wdk 		(void) zsc_intr_soft(zsc);
    388   1.1      wdk 	}
    389   1.1      wdk 	splx(s);
    390   1.1      wdk 	return;
    391   1.1      wdk }
    392   1.1      wdk 
    393   1.1      wdk 
    394   1.1      wdk /*
    395   1.1      wdk  * Compute the current baud rate given a ZS channel.
    396   1.1      wdk  */
    397   1.1      wdk static int
    398   1.1      wdk zs_get_speed(cs)
    399   1.1      wdk 	struct zs_chanstate *cs;
    400   1.1      wdk {
    401   1.1      wdk 	int tconst;
    402   1.1      wdk 
    403   1.1      wdk 	tconst = zs_read_reg(cs, 12);
    404   1.1      wdk 	tconst |= zs_read_reg(cs, 13) << 8;
    405   1.1      wdk 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
    406   1.1      wdk }
    407   1.1      wdk 
    408   1.1      wdk /*
    409   1.1      wdk  * MD functions for setting the baud rate and control modes.
    410   1.1      wdk  */
    411   1.1      wdk int
    412   1.1      wdk zs_set_speed(cs, bps)
    413   1.1      wdk 	struct zs_chanstate *cs;
    414   1.1      wdk 	int bps;	/* bits per second */
    415   1.1      wdk {
    416   1.1      wdk 	int tconst, real_bps;
    417   1.6      wdk 
    418  1.10      wdk #if 0
    419   1.6      wdk 	while (!(zs_read_csr(cs) & ZSRR0_TX_READY))
    420   1.6      wdk 	        {/*nop*/}
    421   1.6      wdk #endif
    422   1.4      wdk 	/* Wait for transmit buffer to empty */
    423   1.6      wdk 	if (bps == 0) {
    424   1.1      wdk 		return (0);
    425   1.6      wdk 	}
    426   1.1      wdk 
    427   1.1      wdk #ifdef	DIAGNOSTIC
    428   1.1      wdk 	if (cs->cs_brg_clk == 0)
    429   1.1      wdk 		panic("zs_set_speed");
    430   1.1      wdk #endif
    431   1.1      wdk 
    432   1.1      wdk 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
    433   1.1      wdk 	if (tconst < 0)
    434   1.1      wdk 		return (EINVAL);
    435   1.1      wdk 
    436   1.1      wdk 	/* Convert back to make sure we can do it. */
    437   1.1      wdk 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
    438   1.1      wdk 
    439   1.1      wdk 	/* XXX - Allow some tolerance here? */
    440   1.1      wdk #if 0
    441   1.1      wdk 	if (real_bps != bps)
    442   1.1      wdk 		return (EINVAL);
    443   1.1      wdk #endif
    444   1.1      wdk 
    445   1.1      wdk 	cs->cs_preg[12] = tconst;
    446   1.1      wdk 	cs->cs_preg[13] = tconst >> 8;
    447   1.1      wdk 
    448   1.1      wdk 	/* Caller will stuff the pending registers. */
    449   1.1      wdk 	return (0);
    450   1.1      wdk }
    451   1.1      wdk 
    452   1.1      wdk int
    453   1.1      wdk zs_set_modes(cs, cflag)
    454   1.1      wdk 	struct zs_chanstate *cs;
    455   1.1      wdk 	int cflag;	/* bits per second */
    456   1.1      wdk {
    457   1.1      wdk 	int s;
    458   1.1      wdk 
    459   1.1      wdk 	/*
    460   1.1      wdk 	 * Output hardware flow control on the chip is horrendous:
    461   1.1      wdk 	 * if carrier detect drops, the receiver is disabled, and if
    462   1.1      wdk 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
    463   1.1      wdk 	 * Therefore, NEVER set the HFC bit, and instead use the
    464   1.1      wdk 	 * status interrupt to detect CTS changes.
    465   1.1      wdk 	 */
    466   1.1      wdk 	s = splzs();
    467   1.1      wdk 	cs->cs_rr0_pps = 0;
    468   1.1      wdk 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
    469   1.1      wdk 		cs->cs_rr0_dcd = 0;
    470   1.1      wdk 		if ((cflag & MDMBUF) == 0)
    471   1.1      wdk 			cs->cs_rr0_pps = ZSRR0_DCD;
    472   1.1      wdk 	} else
    473   1.1      wdk 		cs->cs_rr0_dcd = ZSRR0_DCD;
    474   1.1      wdk 	if ((cflag & CRTSCTS) != 0) {
    475   1.1      wdk 		cs->cs_wr5_dtr = ZSWR5_DTR;
    476   1.1      wdk 		cs->cs_wr5_rts = ZSWR5_RTS;
    477   1.1      wdk 		cs->cs_rr0_cts = ZSRR0_CTS;
    478   1.1      wdk 	} else if ((cflag & MDMBUF) != 0) {
    479   1.1      wdk 		cs->cs_wr5_dtr = 0;
    480   1.1      wdk 		cs->cs_wr5_rts = ZSWR5_DTR;
    481   1.1      wdk 		cs->cs_rr0_cts = ZSRR0_DCD;
    482   1.1      wdk 	} else {
    483   1.1      wdk 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
    484   1.1      wdk 		cs->cs_wr5_rts = 0;
    485   1.1      wdk 		cs->cs_rr0_cts = 0;
    486   1.1      wdk 	}
    487   1.1      wdk 	splx(s);
    488   1.1      wdk 
    489   1.1      wdk 	/* Caller will stuff the pending registers. */
    490   1.1      wdk 	return (0);
    491   1.1      wdk }
    492   1.1      wdk 
    493   1.1      wdk 
    494   1.1      wdk /*
    495   1.1      wdk  * Read or write the chip with suitable delays.
    496   1.1      wdk  */
    497   1.1      wdk 
    498   1.1      wdk u_char
    499   1.1      wdk zs_read_reg(cs, reg)
    500   1.1      wdk 	struct zs_chanstate *cs;
    501   1.1      wdk 	u_char reg;
    502   1.1      wdk {
    503   1.1      wdk 	u_char val;
    504   1.6      wdk 	struct zs_channel *zsc = (struct zs_channel *)cs;
    505   1.1      wdk 
    506   1.6      wdk 	bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, reg);
    507   1.1      wdk 	ZS_DELAY();
    508   1.6      wdk 	val = bus_space_read_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR);
    509   1.1      wdk 	ZS_DELAY();
    510   1.1      wdk 	return val;
    511   1.1      wdk }
    512   1.1      wdk 
    513   1.1      wdk void
    514   1.1      wdk zs_write_reg(cs, reg, val)
    515   1.1      wdk 	struct zs_chanstate *cs;
    516   1.1      wdk 	u_char reg, val;
    517   1.1      wdk {
    518   1.6      wdk 	struct zs_channel *zsc = (struct zs_channel *)cs;
    519   1.6      wdk 
    520   1.6      wdk 	bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, reg);
    521   1.1      wdk 	ZS_DELAY();
    522   1.6      wdk 	bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, val);
    523   1.1      wdk 	ZS_DELAY();
    524   1.1      wdk }
    525   1.1      wdk 
    526   1.1      wdk u_char zs_read_csr(cs)
    527   1.1      wdk 	struct zs_chanstate *cs;
    528   1.1      wdk {
    529   1.6      wdk 	struct zs_channel *zsc = (struct zs_channel *)cs;
    530   1.1      wdk 	register u_char val;
    531   1.1      wdk 
    532   1.6      wdk 	val = bus_space_read_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR);
    533   1.1      wdk 	ZS_DELAY();
    534   1.1      wdk 	return val;
    535   1.1      wdk }
    536   1.1      wdk 
    537   1.1      wdk void  zs_write_csr(cs, val)
    538   1.1      wdk 	struct zs_chanstate *cs;
    539   1.1      wdk 	u_char val;
    540   1.1      wdk {
    541   1.6      wdk 	struct zs_channel *zsc = (struct zs_channel *)cs;
    542   1.6      wdk 
    543   1.6      wdk 	bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, val);
    544   1.1      wdk 	ZS_DELAY();
    545   1.1      wdk }
    546   1.1      wdk 
    547   1.1      wdk u_char zs_read_data(cs)
    548   1.1      wdk 	struct zs_chanstate *cs;
    549   1.1      wdk {
    550   1.6      wdk 	struct zs_channel *zsc = (struct zs_channel *)cs;
    551   1.1      wdk 	register u_char val;
    552   1.1      wdk 
    553   1.6      wdk 	val = bus_space_read_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_DATA);
    554   1.1      wdk 	ZS_DELAY();
    555   1.1      wdk 	return val;
    556   1.1      wdk }
    557   1.1      wdk 
    558   1.1      wdk void  zs_write_data(cs, val)
    559   1.1      wdk 	struct zs_chanstate *cs;
    560   1.1      wdk 	u_char val;
    561   1.1      wdk {
    562   1.6      wdk 	struct zs_channel *zsc = (struct zs_channel *)cs;
    563   1.6      wdk 
    564   1.6      wdk 	bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_DATA, val);
    565   1.1      wdk 	ZS_DELAY();
    566   1.1      wdk }
    567   1.1      wdk 
    568   1.1      wdk void
    569   1.1      wdk zs_abort(cs)
    570   1.1      wdk 	struct zs_chanstate *cs;
    571   1.1      wdk {
    572  1.10      wdk #if defined(KGDB)
    573  1.10      wdk 	zskgdb(cs);
    574  1.10      wdk #elif defined(DDB)
    575   1.1      wdk 	Debugger();
    576   1.1      wdk #endif
    577   1.1      wdk }
    578   1.1      wdk 
    579   1.7      wdk 
    580   1.7      wdk /*********************************************************/
    581   1.7      wdk /*  Polled character I/O functions for console and KGDB  */
    582   1.7      wdk /*********************************************************/
    583   1.7      wdk 
    584   1.7      wdk struct zschan *
    585   1.7      wdk zs_get_chan_addr(zs_unit, channel)
    586   1.7      wdk         int zs_unit, channel;
    587   1.7      wdk {
    588   1.7      wdk         struct zsdevice *addr;
    589   1.7      wdk         struct zschan *zc;
    590   1.7      wdk 
    591   1.7      wdk         if (zs_unit >= NZS)
    592   1.7      wdk                 return NULL;
    593   1.7      wdk 
    594   1.7      wdk         addr = (struct zsdevice *) ZS0_ADDR;
    595   1.7      wdk 
    596   1.7      wdk         if (channel == 0) {
    597   1.7      wdk                 zc = &addr->zs_chan_a;
    598   1.7      wdk         } else {
    599   1.7      wdk                 zc = &addr->zs_chan_b;
    600   1.7      wdk         }
    601   1.7      wdk         return (zc);
    602   1.7      wdk }
    603   1.7      wdk 
    604   1.1      wdk int
    605   1.1      wdk zs_getc(arg)
    606   1.1      wdk 	void *arg;
    607   1.1      wdk {
    608   1.1      wdk 	register volatile struct zschan *zc = arg;
    609   1.1      wdk 	register int s, c, rr0;
    610   1.1      wdk 
    611   1.1      wdk 	s = splhigh();
    612   1.1      wdk 	/* Wait for a character to arrive. */
    613   1.1      wdk 	do {
    614   1.1      wdk 		rr0 = zc->zc_csr;
    615   1.1      wdk 		ZS_DELAY();
    616   1.1      wdk 	} while ((rr0 & ZSRR0_RX_READY) == 0);
    617   1.1      wdk 
    618   1.1      wdk 	c = zc->zc_data;
    619   1.1      wdk 	ZS_DELAY();
    620   1.1      wdk 	splx(s);
    621   1.1      wdk 
    622   1.1      wdk 	return (c);
    623   1.1      wdk }
    624   1.1      wdk 
    625   1.1      wdk /*
    626   1.1      wdk  * Polled output char.
    627   1.1      wdk  */
    628   1.7      wdk void
    629   1.1      wdk zs_putc(arg, c)
    630   1.1      wdk 	void *arg;
    631   1.1      wdk 	int c;
    632   1.1      wdk {
    633   1.1      wdk 	register volatile struct zschan *zc = arg;
    634   1.1      wdk 	register int s, rr0;
    635   1.1      wdk 
    636   1.1      wdk 	s = splhigh();
    637   1.1      wdk 	/* Wait for transmitter to become ready. */
    638   1.1      wdk 	do {
    639   1.1      wdk 		rr0 = zc->zc_csr;
    640   1.1      wdk 		ZS_DELAY();
    641   1.1      wdk 	} while ((rr0 & ZSRR0_TX_READY) == 0);
    642   1.1      wdk 
    643   1.1      wdk 	zc->zc_data = c;
    644   1.6      wdk 	wbflush();
    645   1.1      wdk 	ZS_DELAY();
    646   1.1      wdk 	splx(s);
    647   1.1      wdk }
    648   1.1      wdk 
    649   1.7      wdk /***************************************************************/
    650   1.1      wdk 
    651   1.1      wdk static void zscnprobe __P((struct consdev *));
    652   1.1      wdk static void zscninit __P((struct consdev *));
    653   1.1      wdk static int  zscngetc __P((dev_t));
    654   1.1      wdk static void zscnputc __P((dev_t, int));
    655   1.1      wdk static void zscnpollc __P((dev_t, int));
    656   1.1      wdk 
    657   1.3      wdk static int  cons_port;
    658   1.1      wdk 
    659   1.1      wdk struct consdev consdev_zs = {
    660   1.1      wdk 	zscnprobe,
    661   1.1      wdk 	zscninit,
    662   1.1      wdk 	zscngetc,
    663   1.1      wdk 	zscnputc,
    664   1.1      wdk 	zscnpollc
    665   1.1      wdk };
    666   1.1      wdk 
    667   1.1      wdk void
    668   1.1      wdk zscnprobe(cn)
    669   1.1      wdk 	struct consdev *cn;
    670   1.1      wdk {
    671   1.1      wdk }
    672   1.1      wdk 
    673   1.1      wdk void
    674   1.1      wdk zscninit(cn)
    675   1.1      wdk 	struct consdev *cn;
    676   1.1      wdk {
    677  1.12  gehenna 	extern const struct cdevsw zstty_cdevsw;
    678  1.12  gehenna 
    679   1.3      wdk 	cons_port = prom_getconsole();
    680  1.12  gehenna 	cn->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), cons_port);
    681   1.1      wdk 	cn->cn_pri = CN_REMOTE;
    682   1.1      wdk 	zs_hwflags[0][cons_port] = ZS_HWFLAG_CONSOLE;
    683   1.1      wdk }
    684   1.1      wdk 
    685   1.1      wdk int
    686   1.1      wdk zscngetc(dev)
    687   1.1      wdk 	dev_t dev;
    688   1.1      wdk {
    689   1.1      wdk 	struct zschan *zs;
    690   1.1      wdk 
    691   1.1      wdk 	zs = zs_get_chan_addr(0, cons_port);
    692   1.1      wdk 	return zs_getc(zs);
    693   1.1      wdk }
    694   1.1      wdk 
    695   1.1      wdk void
    696   1.1      wdk zscnputc(dev, c)
    697   1.1      wdk 	dev_t dev;
    698   1.1      wdk 	int c;
    699   1.1      wdk {
    700   1.1      wdk 	struct zschan *zs;
    701   1.1      wdk 
    702   1.1      wdk 	zs = zs_get_chan_addr(0, cons_port);
    703   1.1      wdk 	zs_putc(zs, c);
    704   1.1      wdk }
    705   1.1      wdk 
    706   1.1      wdk void
    707   1.1      wdk zscnpollc(dev, on)
    708   1.1      wdk 	dev_t dev;
    709   1.1      wdk 	int on;
    710   1.1      wdk {
    711   1.1      wdk }
    712