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