Home | History | Annotate | Line # | Download | only in ebus
dz_ebus.c revision 1.7
      1  1.7  dholland /*	$NetBSD: dz_ebus.c,v 1.7 2014/03/16 05:20:23 dholland Exp $	*/
      2  1.1     pooka 
      3  1.1     pooka /*-
      4  1.1     pooka  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.1     pooka  * All rights reserved.
      6  1.1     pooka  *
      7  1.1     pooka  * This code was written by Alessandro Forin and Neil Pittman
      8  1.1     pooka  * at Microsoft Research and contributed to The NetBSD Foundation
      9  1.1     pooka  * by Microsoft Corporation.
     10  1.1     pooka  *
     11  1.1     pooka  * Redistribution and use in source and binary forms, with or without
     12  1.1     pooka  * modification, are permitted provided that the following conditions
     13  1.1     pooka  * are met:
     14  1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     15  1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     16  1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     18  1.1     pooka  *    documentation and/or other materials provided with the distribution.
     19  1.1     pooka  *
     20  1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1     pooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1     pooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1     pooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1     pooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1     pooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1     pooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1     pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1     pooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1     pooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1     pooka  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1     pooka  */
     32  1.1     pooka 
     33  1.1     pooka #include <sys/cdefs.h>
     34  1.7  dholland __KERNEL_RCSID(0, "$NetBSD: dz_ebus.c,v 1.7 2014/03/16 05:20:23 dholland Exp $");
     35  1.1     pooka 
     36  1.1     pooka #include "opt_ddb.h"
     37  1.1     pooka 
     38  1.1     pooka #include <sys/param.h>
     39  1.1     pooka #include <sys/systm.h>
     40  1.1     pooka #include <sys/callout.h>
     41  1.1     pooka #include <sys/ioctl.h>
     42  1.1     pooka #include <sys/tty.h>
     43  1.1     pooka #include <sys/proc.h>
     44  1.1     pooka #include <sys/buf.h>
     45  1.1     pooka #include <sys/conf.h>
     46  1.1     pooka #include <sys/file.h>
     47  1.1     pooka #include <sys/uio.h>
     48  1.1     pooka #include <sys/kernel.h>
     49  1.1     pooka #include <sys/syslog.h>
     50  1.1     pooka #include <sys/device.h>
     51  1.1     pooka #include <sys/kauth.h>
     52  1.1     pooka 
     53  1.1     pooka #include <machine/bus.h>
     54  1.1     pooka #include <machine/emipsreg.h>
     55  1.1     pooka 
     56  1.1     pooka #include <dev/cons.h>
     57  1.1     pooka 
     58  1.1     pooka 
     59  1.1     pooka #include <emips/ebus/ebusvar.h>
     60  1.1     pooka #include <emips/emips/cons.h>
     61  1.3   tsutsui #if 0
     62  1.3   tsutsui #include <emips/emips/machdep.h>
     63  1.3   tsutsui #endif
     64  1.1     pooka 
     65  1.1     pooka #include "ioconf.h" /* for dz_cd */
     66  1.1     pooka 
     67  1.3   tsutsui #define DZ_C2I(c)	((c) << 3)	/* convert controller # to index */
     68  1.3   tsutsui #define DZ_I2C(c)	((c) >> 3)	/* convert minor to controller # */
     69  1.3   tsutsui #define DZ_PORT(u)	((u) & 07)	/* extract the port # */
     70  1.1     pooka 
     71  1.1     pooka struct	dz_softc {
     72  1.4   tsutsui 	device_t	sc_dev;		/* Autoconf blaha */
     73  1.1     pooka 	struct	evcnt	sc_rintrcnt;	/* recevive interrupt counts */
     74  1.1     pooka 	struct	evcnt	sc_tintrcnt;	/* transmit interrupt counts */
     75  1.1     pooka 	struct	_Usart	*sc_dr;		/* reg pointers */
     76  1.1     pooka 	bus_space_tag_t	sc_iot;
     77  1.1     pooka 	bus_space_handle_t sc_ioh;
     78  1.1     pooka 	int		sc_consline;	/* console line, or -1 */
     79  1.1     pooka 	int		sc_rxint;	/* Receive interrupt count XXX */
     80  1.1     pooka 	u_char		sc_brk;		/* Break asserted on some lines */
     81  1.1     pooka 	u_char		sc_dsr;		/* DSR set bits if no mdm ctrl */
     82  1.1     pooka 	struct dz_linestate {
     83  1.1     pooka 		struct	dz_softc *dz_sc;	/* backpointer to softc */
     84  1.1     pooka 		int		dz_line;	/* channel number */
     85  1.3   tsutsui 		struct	tty	*dz_tty;	/* what we work on */
     86  1.1     pooka 	} sc_dz;
     87  1.1     pooka };
     88  1.1     pooka 
     89  1.1     pooka void	dzrint(struct dz_softc *, uint32_t);
     90  1.1     pooka void	dzxint(struct dz_softc *, uint32_t);
     91  1.1     pooka 
     92  1.1     pooka #ifndef TIOCM_BRK
     93  1.1     pooka #define TIOCM_BRK		0100000		/* no equivalent */
     94  1.1     pooka 
     95  1.1     pooka static void	dzstart(struct tty *);
     96  1.1     pooka static int	dzparam(struct tty *, struct termios *);
     97  1.1     pooka static unsigned dzmctl(struct dz_softc *sc, int line,
     98  1.1     pooka                        int bits, /* one of the TIOCM_xx */
     99  1.1     pooka                        int how); /* one of the DMSET/BIS.. */
    100  1.1     pooka 
    101  1.1     pooka #include <dev/dec/dzkbdvar.h>
    102  1.1     pooka #endif
    103  1.1     pooka 
    104  1.1     pooka dev_type_open(dzopen);
    105  1.1     pooka dev_type_close(dzclose);
    106  1.1     pooka dev_type_read(dzread);
    107  1.1     pooka dev_type_write(dzwrite);
    108  1.1     pooka dev_type_ioctl(dzioctl);
    109  1.1     pooka dev_type_stop(dzstop);
    110  1.1     pooka dev_type_tty(dztty);
    111  1.1     pooka dev_type_poll(dzpoll);
    112  1.1     pooka 
    113  1.1     pooka const struct cdevsw dz_cdevsw = {
    114  1.7  dholland 	.d_open = dzopen,
    115  1.7  dholland 	.d_close = dzclose,
    116  1.7  dholland 	.d_read = dzread,
    117  1.7  dholland 	.d_write = dzwrite,
    118  1.7  dholland 	.d_ioctl = dzioctl,
    119  1.7  dholland 	.d_stop = dzstop,
    120  1.7  dholland 	.d_tty = dztty,
    121  1.7  dholland 	.d_poll = dzpoll,
    122  1.7  dholland 	.d_mmap = nommap,
    123  1.7  dholland 	.d_kqfilter = ttykqfilter,
    124  1.7  dholland 	.d_flag = D_TTY
    125  1.1     pooka };
    126  1.1     pooka 
    127  1.1     pooka int
    128  1.1     pooka dzopen(dev_t dev, int flag, int mode, struct lwp *l)
    129  1.1     pooka {
    130  1.1     pooka 	struct tty *tp;
    131  1.1     pooka 	int unit, line;
    132  1.3   tsutsui 	struct dz_softc *sc;
    133  1.1     pooka 	int s, error = 0;
    134  1.1     pooka 
    135  1.1     pooka 	unit = DZ_I2C(minor(dev));
    136  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, unit);
    137  1.4   tsutsui 	if (sc == NULL)
    138  1.4   tsutsui 		return ENXIO;
    139  1.4   tsutsui 
    140  1.1     pooka 	line = DZ_PORT(minor(dev));
    141  1.5   tsutsui 	if (line > 0) /* FIXME for more than one line */
    142  1.1     pooka 		return ENXIO;
    143  1.1     pooka 
    144  1.1     pooka 	tp = sc->sc_dz.dz_tty;
    145  1.1     pooka 	if (tp == NULL)
    146  1.3   tsutsui 		return ENODEV;
    147  1.3   tsutsui 	tp->t_oproc = dzstart;
    148  1.3   tsutsui 	tp->t_param = dzparam;
    149  1.3   tsutsui 	tp->t_dev   = dev;
    150  1.1     pooka 
    151  1.1     pooka 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    152  1.1     pooka 		return (EBUSY);
    153  1.1     pooka 
    154  1.1     pooka 	if ((tp->t_state & TS_ISOPEN) == 0) {
    155  1.1     pooka 		ttychars(tp);
    156  1.1     pooka 		if (tp->t_ispeed == 0) {
    157  1.1     pooka 			tp->t_iflag = TTYDEF_IFLAG;
    158  1.1     pooka 			tp->t_oflag = TTYDEF_OFLAG;
    159  1.1     pooka 			tp->t_cflag = TTYDEF_CFLAG;
    160  1.1     pooka 			tp->t_lflag = TTYDEF_LFLAG;
    161  1.1     pooka 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    162  1.1     pooka 		}
    163  1.3   tsutsui 		(void)dzparam(tp, &tp->t_termios);
    164  1.1     pooka 		ttsetwater(tp);
    165  1.1     pooka 	}
    166  1.3   tsutsui 	/* we have no modem control but..*/
    167  1.1     pooka 	if (dzmctl(sc, line, TIOCM_DTR, DMBIS) & TIOCM_CD)
    168  1.3   tsutsui 		tp->t_state |= TS_CARR_ON;
    169  1.3   tsutsui 		s = spltty();
    170  1.3   tsutsui 		while (!(flag & O_NONBLOCK) && !(tp->t_cflag & CLOCAL) &&
    171  1.3   tsutsui 		    !(tp->t_state & TS_CARR_ON)) {
    172  1.3   tsutsui 			tp->t_wopen++;
    173  1.3   tsutsui 			error = ttysleep(tp, &tp->t_rawcv, true, 0);
    174  1.3   tsutsui 			tp->t_wopen--;
    175  1.3   tsutsui 			if (error)
    176  1.3   tsutsui 				break;
    177  1.3   tsutsui 		}
    178  1.3   tsutsui 	(void)splx(s);
    179  1.1     pooka 	if (error)
    180  1.3   tsutsui 		return error;
    181  1.3   tsutsui 	return (*tp->t_linesw->l_open)(dev, tp);
    182  1.1     pooka }
    183  1.3   tsutsui 
    184  1.1     pooka int
    185  1.1     pooka dzclose(dev_t dev, int flag, int mode, struct lwp *l)
    186  1.1     pooka {
    187  1.3   tsutsui 	struct dz_softc *sc;
    188  1.1     pooka 	struct tty *tp;
    189  1.1     pooka 	int unit, line;
    190  1.1     pooka 
    191  1.1     pooka 	unit = DZ_I2C(minor(dev));
    192  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, unit);
    193  1.1     pooka 	line = DZ_PORT(minor(dev));
    194  1.1     pooka 
    195  1.1     pooka 	tp = sc->sc_dz.dz_tty;
    196  1.1     pooka 
    197  1.1     pooka 	(*tp->t_linesw->l_close)(tp, flag);
    198  1.1     pooka 
    199  1.1     pooka 	/* Make sure a BREAK state is not left enabled. */
    200  1.3   tsutsui 	(void)dzmctl(sc, line, TIOCM_BRK, DMBIC);
    201  1.1     pooka 
    202  1.1     pooka 	/* Do a hangup if so required. */
    203  1.1     pooka 	if ((tp->t_cflag & HUPCL) || tp->t_wopen || !(tp->t_state & TS_ISOPEN))
    204  1.5   tsutsui 		(void)dzmctl(sc, line, 0, DMSET);
    205  1.1     pooka 
    206  1.3   tsutsui 	return ttyclose(tp);
    207  1.1     pooka }
    208  1.3   tsutsui 
    209  1.1     pooka int
    210  1.1     pooka dzread(dev_t dev, struct uio *uio, int flag)
    211  1.1     pooka {
    212  1.1     pooka 	struct tty *tp;
    213  1.3   tsutsui 	struct dz_softc *sc;
    214  1.1     pooka 
    215  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, DZ_I2C(minor(dev)));
    216  1.1     pooka 
    217  1.1     pooka 	tp = sc->sc_dz.dz_tty;
    218  1.3   tsutsui 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    219  1.1     pooka }
    220  1.1     pooka 
    221  1.1     pooka int
    222  1.1     pooka dzwrite(dev_t dev, struct uio *uio, int flag)
    223  1.1     pooka {
    224  1.1     pooka 	struct tty *tp;
    225  1.3   tsutsui 	struct dz_softc *sc;
    226  1.1     pooka 
    227  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, DZ_I2C(minor(dev)));
    228  1.1     pooka 
    229  1.1     pooka 	tp = sc->sc_dz.dz_tty;
    230  1.3   tsutsui 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    231  1.1     pooka }
    232  1.1     pooka 
    233  1.1     pooka /*ARGSUSED*/
    234  1.1     pooka int
    235  1.1     pooka dzioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    236  1.1     pooka {
    237  1.1     pooka 	struct	dz_softc *sc;
    238  1.1     pooka 	struct tty *tp;
    239  1.1     pooka 	int unit, line;
    240  1.1     pooka 	int error;
    241  1.1     pooka 
    242  1.1     pooka 	unit = DZ_I2C(minor(dev));
    243  1.1     pooka 	line = 0;
    244  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, unit);
    245  1.1     pooka 	tp = sc->sc_dz.dz_tty;
    246  1.1     pooka 
    247  1.1     pooka 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    248  1.1     pooka 	if (error >= 0)
    249  1.3   tsutsui 		return error;
    250  1.1     pooka 
    251  1.1     pooka 	error = ttioctl(tp, cmd, data, flag, l);
    252  1.1     pooka 	if (error >= 0)
    253  1.3   tsutsui 		return error;
    254  1.1     pooka 
    255  1.1     pooka 	switch (cmd) {
    256  1.1     pooka 
    257  1.1     pooka 	case TIOCSBRK:
    258  1.3   tsutsui 		(void)dzmctl(sc, line, TIOCM_BRK, DMBIS);
    259  1.1     pooka 		break;
    260  1.1     pooka 
    261  1.1     pooka 	case TIOCCBRK:
    262  1.3   tsutsui 		(void)dzmctl(sc, line, TIOCM_BRK, DMBIC);
    263  1.1     pooka 		break;
    264  1.1     pooka 
    265  1.1     pooka 	case TIOCSDTR:
    266  1.3   tsutsui 		(void)dzmctl(sc, line, TIOCM_DTR, DMBIS);
    267  1.1     pooka 		break;
    268  1.1     pooka 
    269  1.1     pooka 	case TIOCCDTR:
    270  1.3   tsutsui 		(void)dzmctl(sc, line, TIOCM_DTR, DMBIC);
    271  1.1     pooka 		break;
    272  1.1     pooka 
    273  1.1     pooka 	case TIOCMSET:
    274  1.3   tsutsui 		(void)dzmctl(sc, line, *(int *)data, DMSET);
    275  1.1     pooka 		break;
    276  1.1     pooka 
    277  1.1     pooka 	case TIOCMBIS:
    278  1.3   tsutsui 		(void)dzmctl(sc, line, *(int *)data, DMBIS);
    279  1.1     pooka 		break;
    280  1.1     pooka 
    281  1.1     pooka 	case TIOCMBIC:
    282  1.3   tsutsui 		(void)dzmctl(sc, line, *(int *)data, DMBIC);
    283  1.1     pooka 		break;
    284  1.1     pooka 
    285  1.1     pooka 	case TIOCMGET:
    286  1.3   tsutsui 		*(int *)data = dzmctl(sc, line, 0, DMGET) & ~TIOCM_BRK;
    287  1.1     pooka 		break;
    288  1.1     pooka 
    289  1.1     pooka 	default:
    290  1.3   tsutsui 		return EPASSTHROUGH;
    291  1.1     pooka 	}
    292  1.3   tsutsui 	return 0;
    293  1.1     pooka }
    294  1.1     pooka 
    295  1.1     pooka /*ARGSUSED*/
    296  1.1     pooka void
    297  1.1     pooka dzstop(struct tty *tp, int flag)
    298  1.1     pooka {
    299  1.3   tsutsui 
    300  1.1     pooka 	if (tp->t_state & TS_BUSY)
    301  1.1     pooka 		if (!(tp->t_state & TS_TTSTOP))
    302  1.1     pooka 			tp->t_state |= TS_FLUSH;
    303  1.1     pooka }
    304  1.1     pooka 
    305  1.1     pooka struct tty *
    306  1.1     pooka dztty(dev_t dev)
    307  1.1     pooka {
    308  1.4   tsutsui 	struct dz_softc *sc;
    309  1.4   tsutsui 	struct tty *tp;
    310  1.4   tsutsui 
    311  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, DZ_I2C(minor(dev)));
    312  1.4   tsutsui 	tp = sc->sc_dz.dz_tty;
    313  1.1     pooka 
    314  1.3   tsutsui 	return tp;
    315  1.1     pooka }
    316  1.1     pooka 
    317  1.1     pooka int
    318  1.3   tsutsui dzpoll(dev_t dev, int events, struct lwp *l)
    319  1.1     pooka {
    320  1.3   tsutsui 	struct dz_softc *sc;
    321  1.1     pooka 	struct tty *tp;
    322  1.1     pooka 
    323  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, DZ_I2C(minor(dev)));
    324  1.1     pooka 
    325  1.1     pooka 	tp = sc->sc_dz.dz_tty;
    326  1.3   tsutsui 	return (*tp->t_linesw->l_poll)(tp, events, l);
    327  1.1     pooka }
    328  1.1     pooka 
    329  1.1     pooka void
    330  1.1     pooka dzstart(struct tty *tp)
    331  1.1     pooka {
    332  1.1     pooka 	struct dz_softc *sc;
    333  1.1     pooka 	struct clist *cl;
    334  1.1     pooka 	int unit, s;
    335  1.1     pooka 
    336  1.1     pooka 	unit = DZ_I2C(minor(tp->t_dev));
    337  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, unit);
    338  1.1     pooka 
    339  1.1     pooka 	s = spltty();
    340  1.1     pooka 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP)) {
    341  1.1     pooka 		splx(s);
    342  1.1     pooka 		return;
    343  1.1     pooka 	}
    344  1.1     pooka 	cl = &tp->t_outq;
    345  1.1     pooka 	ttypull(tp);
    346  1.1     pooka 	if (cl->c_cc == 0) {
    347  1.1     pooka 		splx(s);
    348  1.1     pooka 		return;
    349  1.1     pooka 	}
    350  1.1     pooka 
    351  1.1     pooka 	tp->t_state |= TS_BUSY;
    352  1.1     pooka 
    353  1.3   tsutsui 	/* was idle, get it started */
    354  1.1     pooka 	dzxint(sc,USI_TXRDY);
    355  1.1     pooka 	splx(s);
    356  1.1     pooka }
    357  1.1     pooka 
    358  1.1     pooka static int rclk = 25000000; /* BUGBUGBUGBUG */
    359  1.1     pooka 
    360  1.1     pooka static int
    361  1.1     pooka dzdivisor(int baudrate)
    362  1.1     pooka {
    363  1.1     pooka 	int act_baud, divisor, error;
    364  1.1     pooka 
    365  1.1     pooka 	if (baudrate <= 0)
    366  1.3   tsutsui 		return 0;
    367  1.1     pooka 
    368  1.3   tsutsui 	divisor = (rclk / 8) / (baudrate);
    369  1.3   tsutsui 	divisor = (divisor / 2) + (divisor & 1);
    370  1.1     pooka 
    371  1.1     pooka 	if (divisor <= 0)
    372  1.3   tsutsui 		return -1;
    373  1.1     pooka 	act_baud = rclk / (divisor * 16);
    374  1.1     pooka 
    375  1.1     pooka 	/* 10 times error in percent: */
    376  1.1     pooka 	error = ((act_baud - baudrate) * 2000 / baudrate + 1) >> 1;
    377  1.1     pooka 
    378  1.1     pooka 	/* 3.0% maximum error tolerance: */
    379  1.1     pooka 	if (error < -30 || error > 30)
    380  1.3   tsutsui 		return -1;
    381  1.1     pooka 
    382  1.3   tsutsui 	return divisor;
    383  1.1     pooka }
    384  1.1     pooka 
    385  1.1     pooka static int
    386  1.1     pooka dzparam(struct tty *tp, struct termios *t)
    387  1.1     pooka {
    388  1.1     pooka 	struct	dz_softc *sc;
    389  1.1     pooka 	int cflag = t->c_cflag;
    390  1.1     pooka 	int unit, line;
    391  1.1     pooka 	int speed;
    392  1.1     pooka 	unsigned lpr;
    393  1.1     pooka 	int s;
    394  1.1     pooka 	struct _Usart *dzr;
    395  1.1     pooka 
    396  1.1     pooka 	unit = DZ_I2C(minor(tp->t_dev));
    397  1.1     pooka 	line = DZ_PORT(minor(tp->t_dev));
    398  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, unit);
    399  1.1     pooka 
    400  1.1     pooka 	/* check requested parameters */
    401  1.3   tsutsui 	if (t->c_ispeed != t->c_ospeed)
    402  1.3   tsutsui 		return EINVAL;
    403  1.3   tsutsui 	speed = dzdivisor(t->c_ispeed);
    404  1.3   tsutsui 	if (speed < 0)
    405  1.3   tsutsui 		return EINVAL;
    406  1.3   tsutsui 
    407  1.3   tsutsui 	tp->t_ispeed = t->c_ispeed;
    408  1.3   tsutsui 	tp->t_ospeed = t->c_ospeed;
    409  1.3   tsutsui 	tp->t_cflag = cflag;
    410  1.3   tsutsui 
    411  1.3   tsutsui 	{
    412  1.3   tsutsui 		/* XXX */
    413  1.3   tsutsui 		static int didit = 0;
    414  1.3   tsutsui 		if (!didit && t->c_ispeed != 38400)
    415  1.3   tsutsui 			printf("dzparam: c_ispeed %d ignored, keeping 38400\n",
    416  1.3   tsutsui 			    t->c_ispeed);
    417  1.3   tsutsui 		didit = 1;
    418  1.3   tsutsui 	}
    419  1.3   tsutsui 	speed = dzdivisor(38400);
    420  1.1     pooka 
    421  1.1     pooka 	if (speed == 0) {
    422  1.3   tsutsui 		(void)dzmctl(sc, line, 0, DMSET);	/* hang up line */
    423  1.3   tsutsui 		return 0;
    424  1.1     pooka 	}
    425  1.1     pooka 
    426  1.3   tsutsui 	switch (cflag & CSIZE) {
    427  1.3   tsutsui 	case CS5:
    428  1.1     pooka 		lpr = USC_BPC_5;
    429  1.1     pooka 		break;
    430  1.3   tsutsui 	case CS6:
    431  1.1     pooka 		lpr = USC_BPC_6;
    432  1.1     pooka 		break;
    433  1.3   tsutsui 	case CS7:
    434  1.1     pooka 		lpr = USC_BPC_7;
    435  1.1     pooka 		break;
    436  1.3   tsutsui 	default:
    437  1.1     pooka 		lpr = USC_BPC_8;
    438  1.1     pooka 		break;
    439  1.1     pooka 	}
    440  1.1     pooka 	if (cflag & CSTOPB)
    441  1.1     pooka 		lpr |= USC_2STOP;
    442  1.1     pooka 
    443  1.1     pooka 	if (cflag & PARENB) {
    444  1.3   tsutsui 		if (cflag & PARODD)
    445  1.3   tsutsui 			lpr |= USC_ODD;
    446  1.3   tsutsui 		else
    447  1.3   tsutsui 			lpr |= USC_EVEN;
    448  1.3   tsutsui 	} else
    449  1.3   tsutsui 		lpr |= USC_NONE;
    450  1.1     pooka 
    451  1.1     pooka 	s = spltty();
    452  1.1     pooka 
    453  1.1     pooka 	dzr = sc->sc_dr;
    454  1.1     pooka 
    455  1.3   tsutsui 	dzr->Baud = speed;
    456  1.3   tsutsui 	dzr->Control = USC_CLKDIV_4 | USC_TXEN | USC_RXEN | lpr;
    457  1.1     pooka #define USI_INTRS (USI_RXRDY|USI_RXBRK|USI_OVRE|USI_FRAME|USI_PARE)
    458  1.3   tsutsui 	dzr->IntrEnable = USI_INTRS;
    459  1.1     pooka 
    460  1.3   tsutsui 	(void)splx(s);
    461  1.3   tsutsui 	return 0;
    462  1.1     pooka }
    463  1.1     pooka 
    464  1.1     pooka static unsigned
    465  1.1     pooka dzmctl(struct dz_softc *sc, int line, int bits, int how)
    466  1.1     pooka {
    467  1.3   tsutsui 	unsigned int mbits;
    468  1.1     pooka 	int s;
    469  1.1     pooka 	struct _Usart *dzr;
    470  1.1     pooka 
    471  1.1     pooka 	mbits = 0;
    472  1.1     pooka 
    473  1.1     pooka 	s = spltty();
    474  1.1     pooka 
    475  1.1     pooka 	dzr = sc->sc_dr;
    476  1.1     pooka 
    477  1.3   tsutsui 	/* we have no modem control bits (CD,RI,DTR,DSR,..) */
    478  1.3   tsutsui 	mbits |= TIOCM_CD;
    479  1.3   tsutsui 	mbits |= TIOCM_DTR;
    480  1.1     pooka 
    481  1.1     pooka 	if (dzr->ChannelStatus & USI_RXBRK)
    482  1.1     pooka 		mbits |= TIOCM_BRK;
    483  1.1     pooka 
    484  1.3   tsutsui 	switch (how) {
    485  1.3   tsutsui 	case DMSET:
    486  1.1     pooka 		mbits = bits;
    487  1.1     pooka 		break;
    488  1.1     pooka 
    489  1.3   tsutsui 	case DMBIS:
    490  1.1     pooka 		mbits |= bits;
    491  1.1     pooka 		break;
    492  1.1     pooka 
    493  1.3   tsutsui 	case DMBIC:
    494  1.1     pooka 		mbits &= ~bits;
    495  1.1     pooka 		break;
    496  1.1     pooka 
    497  1.3   tsutsui 	case DMGET:
    498  1.3   tsutsui 		(void)splx(s);
    499  1.3   tsutsui 		return mbits;
    500  1.1     pooka 	}
    501  1.1     pooka 
    502  1.3   tsutsui 	/* BUGBUG work in progress */
    503  1.1     pooka 	if (mbits & TIOCM_BRK) {
    504  1.1     pooka 		sc->sc_brk |= (1 << line);
    505  1.3   tsutsui 		dzr->Control |= USC_STTBRK;
    506  1.1     pooka 	} else {
    507  1.1     pooka 		sc->sc_brk &= ~(1 << line);
    508  1.3   tsutsui 		dzr->Control |= USC_STPBRK;
    509  1.1     pooka 	}
    510  1.1     pooka 
    511  1.3   tsutsui 	(void)splx(s);
    512  1.3   tsutsui 	return mbits;
    513  1.1     pooka }
    514  1.1     pooka 
    515  1.1     pooka 
    516  1.1     pooka #if defined(DDB)
    517  1.1     pooka int dz_ddb = 0;
    518  1.1     pooka #endif
    519  1.1     pooka 
    520  1.1     pooka /* Receiver Interrupt */
    521  1.1     pooka 
    522  1.1     pooka void
    523  1.1     pooka dzrint(struct dz_softc *sc, uint32_t csr)
    524  1.1     pooka {
    525  1.1     pooka 	struct tty *tp;
    526  1.6  christos 	int cc;
    527  1.1     pooka 	struct _Usart *dzr;
    528  1.1     pooka 
    529  1.1     pooka 	sc->sc_rxint++;
    530  1.1     pooka 	dzr = sc->sc_dr;
    531  1.1     pooka 
    532  1.3   tsutsui 	cc = dzr->RxData;
    533  1.3   tsutsui 	tp = sc->sc_dz.dz_tty;
    534  1.3   tsutsui 
    535  1.3   tsutsui 	/* clear errors before we print or bail out */
    536  1.3   tsutsui 	if (csr & (USI_OVRE|USI_FRAME|USI_PARE))
    537  1.3   tsutsui 		dzr->Control = USC_RSTSTA;
    538  1.3   tsutsui 
    539  1.3   tsutsui 	if (!(tp->t_state & TS_ISOPEN)) {
    540  1.3   tsutsui 		wakeup(&tp->t_rawq);
    541  1.3   tsutsui 		return;
    542  1.3   tsutsui 	}
    543  1.3   tsutsui 
    544  1.3   tsutsui 	if (csr & USI_OVRE) {
    545  1.3   tsutsui 		log(LOG_WARNING, "%s: silo overflow, line %d\n",
    546  1.4   tsutsui 		    device_xname(sc->sc_dev), 0);
    547  1.3   tsutsui 	}
    548  1.1     pooka 
    549  1.3   tsutsui 	if (csr & USI_FRAME)
    550  1.3   tsutsui 		cc |= TTY_FE;
    551  1.3   tsutsui 	if (csr & USI_PARE)
    552  1.3   tsutsui 		cc |= TTY_PE;
    553  1.1     pooka 
    554  1.1     pooka #if defined(DDB)
    555  1.3   tsutsui 	/* ^P drops into DDB */
    556  1.3   tsutsui 	if (dz_ddb && (cc == 0x10))
    557  1.3   tsutsui 		Debugger();
    558  1.1     pooka #endif
    559  1.3   tsutsui 	(*tp->t_linesw->l_rint)(cc, tp);
    560  1.1     pooka }
    561  1.1     pooka 
    562  1.1     pooka /* Transmitter Interrupt */
    563  1.1     pooka 
    564  1.1     pooka void
    565  1.1     pooka dzxint(struct dz_softc *sc, uint32_t csr)
    566  1.1     pooka {
    567  1.1     pooka 	struct tty *tp;
    568  1.1     pooka 	struct clist *cl;
    569  1.1     pooka 	int ch;
    570  1.1     pooka 	struct _Usart *dzr;
    571  1.1     pooka 
    572  1.1     pooka 	dzr = sc->sc_dr;
    573  1.1     pooka 
    574  1.3   tsutsui 	tp = sc->sc_dz.dz_tty;
    575  1.3   tsutsui 	cl = &tp->t_outq;
    576  1.3   tsutsui 	tp->t_state &= ~TS_BUSY;
    577  1.3   tsutsui 
    578  1.3   tsutsui 	/* Just send out a char if we have one */
    579  1.3   tsutsui 	if (cl->c_cc) {
    580  1.3   tsutsui 		tp->t_state |= TS_BUSY;
    581  1.3   tsutsui 		ch = getc(cl);
    582  1.3   tsutsui 		dzr->TxData = ch;
    583  1.3   tsutsui 		dzr->IntrEnable = USI_TXRDY;
    584  1.3   tsutsui 		return;
    585  1.3   tsutsui 	}
    586  1.3   tsutsui 
    587  1.3   tsutsui 	/* Nothing to send; turn off intr */
    588  1.3   tsutsui 	dzr->IntrDisable = USI_TXRDY;
    589  1.3   tsutsui 
    590  1.3   tsutsui 	if (tp->t_state & TS_FLUSH)
    591  1.3   tsutsui 		tp->t_state &= ~TS_FLUSH;
    592  1.3   tsutsui 	else
    593  1.3   tsutsui 		ndflush(&tp->t_outq, cl->c_cc);
    594  1.3   tsutsui 
    595  1.3   tsutsui 	(*tp->t_linesw->l_start)(tp);
    596  1.3   tsutsui }
    597  1.3   tsutsui 
    598  1.3   tsutsui /*
    599  1.3   tsutsui  * Machdep part of the driver
    600  1.3   tsutsui  */
    601  1.3   tsutsui int	dz_ebus_match(device_t, cfdata_t, void *);
    602  1.3   tsutsui void	dz_ebus_attach(device_t, device_t, void *);
    603  1.1     pooka int	dz_ebus_intr(void *, void *);
    604  1.1     pooka 
    605  1.1     pooka void	dz_ebus_cnsetup(paddr_t);
    606  1.3   tsutsui void	dz_ebus_cninit(struct consdev *);
    607  1.1     pooka int	dz_ebus_cngetc(dev_t);
    608  1.1     pooka void	dz_ebus_cnputc(dev_t, int);
    609  1.1     pooka void	dz_ebus_cnpollc(dev_t, int);
    610  1.1     pooka 
    611  1.1     pooka static int	dz_ebus_getmajor(void);
    612  1.1     pooka 
    613  1.4   tsutsui CFATTACH_DECL_NEW(dz_ebus, sizeof(struct dz_softc),
    614  1.1     pooka     dz_ebus_match, dz_ebus_attach, NULL, NULL);
    615  1.1     pooka 
    616  1.1     pooka struct consdev dz_ebus_consdev = {
    617  1.1     pooka 	NULL, dz_ebus_cninit, dz_ebus_cngetc, dz_ebus_cnputc,
    618  1.1     pooka 	dz_ebus_cnpollc, NULL, NULL, NULL, NODEV, CN_NORMAL,
    619  1.1     pooka };
    620  1.1     pooka 
    621  1.3   tsutsui /*
    622  1.3   tsutsui  * Points to the console regs. Special mapping until VM is turned on.
    623  1.1     pooka  */
    624  1.1     pooka struct _Usart *dzcn;
    625  1.1     pooka 
    626  1.1     pooka int
    627  1.3   tsutsui dz_ebus_match(device_t parent, cfdata_t cf, void *aux)
    628  1.1     pooka {
    629  1.1     pooka 	struct ebus_attach_args *iba;
    630  1.3   tsutsui 	struct _Usart *us;
    631  1.1     pooka 
    632  1.1     pooka 	iba = aux;
    633  1.1     pooka 
    634  1.1     pooka 	if (strcmp(iba->ia_name, "dz") != 0)
    635  1.3   tsutsui 		return 0;
    636  1.1     pooka 
    637  1.3   tsutsui 	us = (struct _Usart *)iba->ia_vaddr;
    638  1.3   tsutsui 	if ((us == NULL) ||
    639  1.3   tsutsui 	    (us->Tag != PMTTAG_USART))
    640  1.3   tsutsui 		return 0;
    641  1.1     pooka 
    642  1.3   tsutsui 	return 1;
    643  1.1     pooka }
    644  1.1     pooka 
    645  1.1     pooka void
    646  1.3   tsutsui dz_ebus_attach(device_t parent, device_t self, void *aux)
    647  1.1     pooka {
    648  1.1     pooka 	struct ebus_attach_args *iba;
    649  1.1     pooka 	struct dz_softc *sc;
    650  1.1     pooka 
    651  1.4   tsutsui 	sc = device_private(self);
    652  1.1     pooka 	iba = aux;
    653  1.1     pooka 
    654  1.4   tsutsui 	sc->sc_dev = self;
    655  1.1     pooka 	sc->sc_dr = (struct _Usart *)iba->ia_vaddr;
    656  1.1     pooka #if DEBUG
    657  1.3   tsutsui 	printf(" virt=%p ", (void *)sc->sc_dr);
    658  1.1     pooka #endif
    659  1.1     pooka 
    660  1.1     pooka 	printf(": neilsart 1 line");
    661  1.1     pooka 	ebus_intr_establish(parent, (void *)iba->ia_cookie, IPL_TTY,
    662  1.1     pooka 	    dz_ebus_intr, sc);
    663  1.1     pooka 
    664  1.1     pooka 	sc->sc_rxint = sc->sc_brk = 0;
    665  1.1     pooka 	sc->sc_consline = 0;
    666  1.1     pooka 
    667  1.1     pooka 	/* Initialize our softc structure. Should be done in open? */
    668  1.1     pooka 
    669  1.2     rmind 	sc->sc_dz.dz_sc = sc;
    670  1.2     rmind 	sc->sc_dz.dz_line = 0;
    671  1.2     rmind 	sc->sc_dz.dz_tty = tty_alloc();
    672  1.1     pooka 
    673  1.1     pooka 	evcnt_attach_dynamic(&sc->sc_rintrcnt, EVCNT_TYPE_INTR, NULL,
    674  1.4   tsutsui 	    device_xname(self), "rintr");
    675  1.1     pooka 	evcnt_attach_dynamic(&sc->sc_tintrcnt, EVCNT_TYPE_INTR, NULL,
    676  1.4   tsutsui 	    device_xname(self), "tintr");
    677  1.1     pooka 
    678  1.3   tsutsui 	/* Initialize hw regs */
    679  1.1     pooka #if 0
    680  1.1     pooka 	DZ_WRITE_WORD(dr_csr, DZ_CSR_MSE | DZ_CSR_RXIE | DZ_CSR_TXIE);
    681  1.1     pooka 	DZ_WRITE_BYTE(dr_dtr, 0);
    682  1.1     pooka 	DZ_WRITE_BYTE(dr_break, 0);
    683  1.1     pooka #endif
    684  1.1     pooka 
    685  1.3   tsutsui 	/* Switch the console to virtual mode */
    686  1.3   tsutsui 	dzcn = sc->sc_dr;
    687  1.3   tsutsui 	/* And test it */
    688  1.1     pooka 	printf("\n");
    689  1.1     pooka }
    690  1.1     pooka 
    691  1.1     pooka static int
    692  1.1     pooka dz_ebus_getmajor(void)
    693  1.1     pooka {
    694  1.1     pooka 	extern const struct cdevsw dz_cdevsw;
    695  1.1     pooka 	static int cache = -1;
    696  1.1     pooka 
    697  1.1     pooka 	if (cache != -1)
    698  1.3   tsutsui 		return cache;
    699  1.1     pooka 
    700  1.3   tsutsui 	return cache = cdevsw_lookup_major(&dz_cdevsw);
    701  1.1     pooka }
    702  1.1     pooka 
    703  1.1     pooka int
    704  1.1     pooka dz_ebus_intr(void *cookie, void *f)
    705  1.1     pooka {
    706  1.1     pooka 	struct dz_softc *sc;
    707  1.1     pooka 	struct _Usart *dzr;
    708  1.1     pooka 	uint32_t csr;
    709  1.1     pooka 
    710  1.1     pooka 	sc = cookie;
    711  1.1     pooka 	dzr = sc->sc_dr;
    712  1.1     pooka 
    713  1.1     pooka #define USI_INTERRUPTS (USI_INTRS|USI_TXRDY)
    714  1.1     pooka 
    715  1.3   tsutsui 	for (; ((csr = (dzr->ChannelStatus & dzr->IntrMask)) &
    716  1.3   tsutsui 	    USI_INTERRUPTS) != 0;) {
    717  1.1     pooka 		if ((csr & USI_INTRS) != 0)
    718  1.1     pooka 			dzrint(sc, csr);
    719  1.1     pooka 		if ((csr & USI_TXRDY) != 0)
    720  1.1     pooka 			dzxint(sc, csr);
    721  1.1     pooka 	}
    722  1.1     pooka 
    723  1.3   tsutsui 	return 0;
    724  1.1     pooka }
    725  1.1     pooka 
    726  1.1     pooka void
    727  1.1     pooka dz_ebus_cnsetup(paddr_t addr)
    728  1.1     pooka {
    729  1.1     pooka 
    730  1.1     pooka 	dzcn = (struct _Usart *)addr;
    731  1.1     pooka 
    732  1.1     pooka #if 0
    733  1.3   tsutsui 	/*
    734  1.3   tsutsui 	 * Initialize enough to xmit/recv via polling.
    735  1.3   tsutsui 	 * Bootloader might or might not have done it.
    736  1.3   tsutsui 	 */
    737  1.3   tsutsui 	dzcn->Control =
    738  1.3   tsutsui 	    USC_RXEN |
    739  1.3   tsutsui 	    USC_TXEN |
    740  1.3   tsutsui 	    USC_BPC_8 |
    741  1.3   tsutsui 	    USC_NONE |
    742  1.3   tsutsui 	    USC_1STOP |
    743  1.3   tsutsui 	    USC_CLKDIV_4;
    744  1.3   tsutsui 	dzcn->Baud = 0x29; /* 38400 */
    745  1.1     pooka #endif
    746  1.1     pooka 
    747  1.1     pooka 	/*
    748  1.1     pooka 	 * Point the console at us
    749  1.1     pooka 	 */
    750  1.1     pooka 	cn_tab = &dz_ebus_consdev;
    751  1.1     pooka 	cn_tab->cn_pri = CN_NORMAL;/*CN_REMOTE?*/
    752  1.1     pooka 	cn_tab->cn_dev = makedev(dz_ebus_getmajor(), 0);
    753  1.1     pooka }
    754  1.1     pooka 
    755  1.3   tsutsui void
    756  1.3   tsutsui dz_ebus_cninit(struct consdev *cn)
    757  1.1     pooka {
    758  1.1     pooka }
    759  1.1     pooka 
    760  1.1     pooka int
    761  1.1     pooka dz_ebus_cngetc(dev_t dev)
    762  1.1     pooka {
    763  1.1     pooka 	int c, s;
    764  1.1     pooka 
    765  1.1     pooka 	c = 0;
    766  1.1     pooka 	s = spltty();
    767  1.1     pooka 
    768  1.3   tsutsui 	while ((dzcn->ChannelStatus & USI_RXRDY) == 0)
    769  1.3   tsutsui 		DELAY(10);
    770  1.3   tsutsui 	c = dzcn->RxData;
    771  1.1     pooka 
    772  1.1     pooka 	splx(s);
    773  1.1     pooka 	if (c == 13) /* map cr->ln */
    774  1.1     pooka 		c = 10;
    775  1.3   tsutsui 	return c;
    776  1.1     pooka }
    777  1.1     pooka 
    778  1.1     pooka int dzflipped = 0;
    779  1.1     pooka void
    780  1.1     pooka dz_ebus_cnputc(dev_t dev, int ch)
    781  1.1     pooka {
    782  1.1     pooka 	int timeout, s;
    783  1.1     pooka 
    784  1.1     pooka 	/* Don't hang the machine! */
    785  1.1     pooka 	timeout = 1 << 15;
    786  1.1     pooka 
    787  1.1     pooka 	s = spltty();
    788  1.1     pooka 
    789  1.1     pooka #if 1
    790  1.3   tsutsui 	/* Keep wired to hunt for a bug */
    791  1.3   tsutsui 	if (dzcn && (dzcn != (struct _Usart *)0xfff90000)) {
    792  1.3   tsutsui 		dzcn = (struct _Usart *)0xfff90000;
    793  1.3   tsutsui 		dzflipped++;
    794  1.3   tsutsui 	}
    795  1.1     pooka #endif
    796  1.1     pooka 
    797  1.1     pooka 	/* Wait until ready */
    798  1.1     pooka 	while ((dzcn->ChannelStatus & USI_TXRDY) == 0)
    799  1.1     pooka 		if (--timeout < 0)
    800  1.1     pooka 			break;
    801  1.1     pooka 
    802  1.1     pooka 	/* Put the character */
    803  1.1     pooka 	dzcn->TxData = ch;
    804  1.1     pooka 
    805  1.1     pooka 	splx(s);
    806  1.1     pooka }
    807  1.1     pooka 
    808  1.3   tsutsui /*
    809  1.3   tsutsui  * Called before/after going into poll mode
    810  1.1     pooka  */
    811  1.3   tsutsui void
    812  1.1     pooka dz_ebus_cnpollc(dev_t dev, int on)
    813  1.1     pooka {
    814  1.1     pooka }
    815