Home | History | Annotate | Line # | Download | only in ebus
dz_ebus.c revision 1.6
      1  1.6  christos /*	$NetBSD: dz_ebus.c,v 1.6 2013/11/10 18:27:15 christos 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.6  christos __KERNEL_RCSID(0, "$NetBSD: dz_ebus.c,v 1.6 2013/11/10 18:27:15 christos 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.1     pooka 	dzopen, dzclose, dzread, dzwrite, dzioctl,
    115  1.1     pooka 	dzstop, dztty, dzpoll, nommap, ttykqfilter, D_TTY
    116  1.1     pooka };
    117  1.1     pooka 
    118  1.1     pooka int
    119  1.1     pooka dzopen(dev_t dev, int flag, int mode, struct lwp *l)
    120  1.1     pooka {
    121  1.1     pooka 	struct tty *tp;
    122  1.1     pooka 	int unit, line;
    123  1.3   tsutsui 	struct dz_softc *sc;
    124  1.1     pooka 	int s, error = 0;
    125  1.1     pooka 
    126  1.1     pooka 	unit = DZ_I2C(minor(dev));
    127  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, unit);
    128  1.4   tsutsui 	if (sc == NULL)
    129  1.4   tsutsui 		return ENXIO;
    130  1.4   tsutsui 
    131  1.1     pooka 	line = DZ_PORT(minor(dev));
    132  1.5   tsutsui 	if (line > 0) /* FIXME for more than one line */
    133  1.1     pooka 		return ENXIO;
    134  1.1     pooka 
    135  1.1     pooka 	tp = sc->sc_dz.dz_tty;
    136  1.1     pooka 	if (tp == NULL)
    137  1.3   tsutsui 		return ENODEV;
    138  1.3   tsutsui 	tp->t_oproc = dzstart;
    139  1.3   tsutsui 	tp->t_param = dzparam;
    140  1.3   tsutsui 	tp->t_dev   = dev;
    141  1.1     pooka 
    142  1.1     pooka 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    143  1.1     pooka 		return (EBUSY);
    144  1.1     pooka 
    145  1.1     pooka 	if ((tp->t_state & TS_ISOPEN) == 0) {
    146  1.1     pooka 		ttychars(tp);
    147  1.1     pooka 		if (tp->t_ispeed == 0) {
    148  1.1     pooka 			tp->t_iflag = TTYDEF_IFLAG;
    149  1.1     pooka 			tp->t_oflag = TTYDEF_OFLAG;
    150  1.1     pooka 			tp->t_cflag = TTYDEF_CFLAG;
    151  1.1     pooka 			tp->t_lflag = TTYDEF_LFLAG;
    152  1.1     pooka 			tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    153  1.1     pooka 		}
    154  1.3   tsutsui 		(void)dzparam(tp, &tp->t_termios);
    155  1.1     pooka 		ttsetwater(tp);
    156  1.1     pooka 	}
    157  1.3   tsutsui 	/* we have no modem control but..*/
    158  1.1     pooka 	if (dzmctl(sc, line, TIOCM_DTR, DMBIS) & TIOCM_CD)
    159  1.3   tsutsui 		tp->t_state |= TS_CARR_ON;
    160  1.3   tsutsui 		s = spltty();
    161  1.3   tsutsui 		while (!(flag & O_NONBLOCK) && !(tp->t_cflag & CLOCAL) &&
    162  1.3   tsutsui 		    !(tp->t_state & TS_CARR_ON)) {
    163  1.3   tsutsui 			tp->t_wopen++;
    164  1.3   tsutsui 			error = ttysleep(tp, &tp->t_rawcv, true, 0);
    165  1.3   tsutsui 			tp->t_wopen--;
    166  1.3   tsutsui 			if (error)
    167  1.3   tsutsui 				break;
    168  1.3   tsutsui 		}
    169  1.3   tsutsui 	(void)splx(s);
    170  1.1     pooka 	if (error)
    171  1.3   tsutsui 		return error;
    172  1.3   tsutsui 	return (*tp->t_linesw->l_open)(dev, tp);
    173  1.1     pooka }
    174  1.3   tsutsui 
    175  1.1     pooka int
    176  1.1     pooka dzclose(dev_t dev, int flag, int mode, struct lwp *l)
    177  1.1     pooka {
    178  1.3   tsutsui 	struct dz_softc *sc;
    179  1.1     pooka 	struct tty *tp;
    180  1.1     pooka 	int unit, line;
    181  1.1     pooka 
    182  1.1     pooka 	unit = DZ_I2C(minor(dev));
    183  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, unit);
    184  1.1     pooka 	line = DZ_PORT(minor(dev));
    185  1.1     pooka 
    186  1.1     pooka 	tp = sc->sc_dz.dz_tty;
    187  1.1     pooka 
    188  1.1     pooka 	(*tp->t_linesw->l_close)(tp, flag);
    189  1.1     pooka 
    190  1.1     pooka 	/* Make sure a BREAK state is not left enabled. */
    191  1.3   tsutsui 	(void)dzmctl(sc, line, TIOCM_BRK, DMBIC);
    192  1.1     pooka 
    193  1.1     pooka 	/* Do a hangup if so required. */
    194  1.1     pooka 	if ((tp->t_cflag & HUPCL) || tp->t_wopen || !(tp->t_state & TS_ISOPEN))
    195  1.5   tsutsui 		(void)dzmctl(sc, line, 0, DMSET);
    196  1.1     pooka 
    197  1.3   tsutsui 	return ttyclose(tp);
    198  1.1     pooka }
    199  1.3   tsutsui 
    200  1.1     pooka int
    201  1.1     pooka dzread(dev_t dev, struct uio *uio, int flag)
    202  1.1     pooka {
    203  1.1     pooka 	struct tty *tp;
    204  1.3   tsutsui 	struct dz_softc *sc;
    205  1.1     pooka 
    206  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, DZ_I2C(minor(dev)));
    207  1.1     pooka 
    208  1.1     pooka 	tp = sc->sc_dz.dz_tty;
    209  1.3   tsutsui 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    210  1.1     pooka }
    211  1.1     pooka 
    212  1.1     pooka int
    213  1.1     pooka dzwrite(dev_t dev, struct uio *uio, int flag)
    214  1.1     pooka {
    215  1.1     pooka 	struct tty *tp;
    216  1.3   tsutsui 	struct dz_softc *sc;
    217  1.1     pooka 
    218  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, DZ_I2C(minor(dev)));
    219  1.1     pooka 
    220  1.1     pooka 	tp = sc->sc_dz.dz_tty;
    221  1.3   tsutsui 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    222  1.1     pooka }
    223  1.1     pooka 
    224  1.1     pooka /*ARGSUSED*/
    225  1.1     pooka int
    226  1.1     pooka dzioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    227  1.1     pooka {
    228  1.1     pooka 	struct	dz_softc *sc;
    229  1.1     pooka 	struct tty *tp;
    230  1.1     pooka 	int unit, line;
    231  1.1     pooka 	int error;
    232  1.1     pooka 
    233  1.1     pooka 	unit = DZ_I2C(minor(dev));
    234  1.1     pooka 	line = 0;
    235  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, unit);
    236  1.1     pooka 	tp = sc->sc_dz.dz_tty;
    237  1.1     pooka 
    238  1.1     pooka 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    239  1.1     pooka 	if (error >= 0)
    240  1.3   tsutsui 		return error;
    241  1.1     pooka 
    242  1.1     pooka 	error = ttioctl(tp, cmd, data, flag, l);
    243  1.1     pooka 	if (error >= 0)
    244  1.3   tsutsui 		return error;
    245  1.1     pooka 
    246  1.1     pooka 	switch (cmd) {
    247  1.1     pooka 
    248  1.1     pooka 	case TIOCSBRK:
    249  1.3   tsutsui 		(void)dzmctl(sc, line, TIOCM_BRK, DMBIS);
    250  1.1     pooka 		break;
    251  1.1     pooka 
    252  1.1     pooka 	case TIOCCBRK:
    253  1.3   tsutsui 		(void)dzmctl(sc, line, TIOCM_BRK, DMBIC);
    254  1.1     pooka 		break;
    255  1.1     pooka 
    256  1.1     pooka 	case TIOCSDTR:
    257  1.3   tsutsui 		(void)dzmctl(sc, line, TIOCM_DTR, DMBIS);
    258  1.1     pooka 		break;
    259  1.1     pooka 
    260  1.1     pooka 	case TIOCCDTR:
    261  1.3   tsutsui 		(void)dzmctl(sc, line, TIOCM_DTR, DMBIC);
    262  1.1     pooka 		break;
    263  1.1     pooka 
    264  1.1     pooka 	case TIOCMSET:
    265  1.3   tsutsui 		(void)dzmctl(sc, line, *(int *)data, DMSET);
    266  1.1     pooka 		break;
    267  1.1     pooka 
    268  1.1     pooka 	case TIOCMBIS:
    269  1.3   tsutsui 		(void)dzmctl(sc, line, *(int *)data, DMBIS);
    270  1.1     pooka 		break;
    271  1.1     pooka 
    272  1.1     pooka 	case TIOCMBIC:
    273  1.3   tsutsui 		(void)dzmctl(sc, line, *(int *)data, DMBIC);
    274  1.1     pooka 		break;
    275  1.1     pooka 
    276  1.1     pooka 	case TIOCMGET:
    277  1.3   tsutsui 		*(int *)data = dzmctl(sc, line, 0, DMGET) & ~TIOCM_BRK;
    278  1.1     pooka 		break;
    279  1.1     pooka 
    280  1.1     pooka 	default:
    281  1.3   tsutsui 		return EPASSTHROUGH;
    282  1.1     pooka 	}
    283  1.3   tsutsui 	return 0;
    284  1.1     pooka }
    285  1.1     pooka 
    286  1.1     pooka /*ARGSUSED*/
    287  1.1     pooka void
    288  1.1     pooka dzstop(struct tty *tp, int flag)
    289  1.1     pooka {
    290  1.3   tsutsui 
    291  1.1     pooka 	if (tp->t_state & TS_BUSY)
    292  1.1     pooka 		if (!(tp->t_state & TS_TTSTOP))
    293  1.1     pooka 			tp->t_state |= TS_FLUSH;
    294  1.1     pooka }
    295  1.1     pooka 
    296  1.1     pooka struct tty *
    297  1.1     pooka dztty(dev_t dev)
    298  1.1     pooka {
    299  1.4   tsutsui 	struct dz_softc *sc;
    300  1.4   tsutsui 	struct tty *tp;
    301  1.4   tsutsui 
    302  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, DZ_I2C(minor(dev)));
    303  1.4   tsutsui 	tp = sc->sc_dz.dz_tty;
    304  1.1     pooka 
    305  1.3   tsutsui 	return tp;
    306  1.1     pooka }
    307  1.1     pooka 
    308  1.1     pooka int
    309  1.3   tsutsui dzpoll(dev_t dev, int events, struct lwp *l)
    310  1.1     pooka {
    311  1.3   tsutsui 	struct dz_softc *sc;
    312  1.1     pooka 	struct tty *tp;
    313  1.1     pooka 
    314  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, DZ_I2C(minor(dev)));
    315  1.1     pooka 
    316  1.1     pooka 	tp = sc->sc_dz.dz_tty;
    317  1.3   tsutsui 	return (*tp->t_linesw->l_poll)(tp, events, l);
    318  1.1     pooka }
    319  1.1     pooka 
    320  1.1     pooka void
    321  1.1     pooka dzstart(struct tty *tp)
    322  1.1     pooka {
    323  1.1     pooka 	struct dz_softc *sc;
    324  1.1     pooka 	struct clist *cl;
    325  1.1     pooka 	int unit, s;
    326  1.1     pooka 
    327  1.1     pooka 	unit = DZ_I2C(minor(tp->t_dev));
    328  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, unit);
    329  1.1     pooka 
    330  1.1     pooka 	s = spltty();
    331  1.1     pooka 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP)) {
    332  1.1     pooka 		splx(s);
    333  1.1     pooka 		return;
    334  1.1     pooka 	}
    335  1.1     pooka 	cl = &tp->t_outq;
    336  1.1     pooka 	ttypull(tp);
    337  1.1     pooka 	if (cl->c_cc == 0) {
    338  1.1     pooka 		splx(s);
    339  1.1     pooka 		return;
    340  1.1     pooka 	}
    341  1.1     pooka 
    342  1.1     pooka 	tp->t_state |= TS_BUSY;
    343  1.1     pooka 
    344  1.3   tsutsui 	/* was idle, get it started */
    345  1.1     pooka 	dzxint(sc,USI_TXRDY);
    346  1.1     pooka 	splx(s);
    347  1.1     pooka }
    348  1.1     pooka 
    349  1.1     pooka static int rclk = 25000000; /* BUGBUGBUGBUG */
    350  1.1     pooka 
    351  1.1     pooka static int
    352  1.1     pooka dzdivisor(int baudrate)
    353  1.1     pooka {
    354  1.1     pooka 	int act_baud, divisor, error;
    355  1.1     pooka 
    356  1.1     pooka 	if (baudrate <= 0)
    357  1.3   tsutsui 		return 0;
    358  1.1     pooka 
    359  1.3   tsutsui 	divisor = (rclk / 8) / (baudrate);
    360  1.3   tsutsui 	divisor = (divisor / 2) + (divisor & 1);
    361  1.1     pooka 
    362  1.1     pooka 	if (divisor <= 0)
    363  1.3   tsutsui 		return -1;
    364  1.1     pooka 	act_baud = rclk / (divisor * 16);
    365  1.1     pooka 
    366  1.1     pooka 	/* 10 times error in percent: */
    367  1.1     pooka 	error = ((act_baud - baudrate) * 2000 / baudrate + 1) >> 1;
    368  1.1     pooka 
    369  1.1     pooka 	/* 3.0% maximum error tolerance: */
    370  1.1     pooka 	if (error < -30 || error > 30)
    371  1.3   tsutsui 		return -1;
    372  1.1     pooka 
    373  1.3   tsutsui 	return divisor;
    374  1.1     pooka }
    375  1.1     pooka 
    376  1.1     pooka static int
    377  1.1     pooka dzparam(struct tty *tp, struct termios *t)
    378  1.1     pooka {
    379  1.1     pooka 	struct	dz_softc *sc;
    380  1.1     pooka 	int cflag = t->c_cflag;
    381  1.1     pooka 	int unit, line;
    382  1.1     pooka 	int speed;
    383  1.1     pooka 	unsigned lpr;
    384  1.1     pooka 	int s;
    385  1.1     pooka 	struct _Usart *dzr;
    386  1.1     pooka 
    387  1.1     pooka 	unit = DZ_I2C(minor(tp->t_dev));
    388  1.1     pooka 	line = DZ_PORT(minor(tp->t_dev));
    389  1.4   tsutsui 	sc = device_lookup_private(&dz_cd, unit);
    390  1.1     pooka 
    391  1.1     pooka 	/* check requested parameters */
    392  1.3   tsutsui 	if (t->c_ispeed != t->c_ospeed)
    393  1.3   tsutsui 		return EINVAL;
    394  1.3   tsutsui 	speed = dzdivisor(t->c_ispeed);
    395  1.3   tsutsui 	if (speed < 0)
    396  1.3   tsutsui 		return EINVAL;
    397  1.3   tsutsui 
    398  1.3   tsutsui 	tp->t_ispeed = t->c_ispeed;
    399  1.3   tsutsui 	tp->t_ospeed = t->c_ospeed;
    400  1.3   tsutsui 	tp->t_cflag = cflag;
    401  1.3   tsutsui 
    402  1.3   tsutsui 	{
    403  1.3   tsutsui 		/* XXX */
    404  1.3   tsutsui 		static int didit = 0;
    405  1.3   tsutsui 		if (!didit && t->c_ispeed != 38400)
    406  1.3   tsutsui 			printf("dzparam: c_ispeed %d ignored, keeping 38400\n",
    407  1.3   tsutsui 			    t->c_ispeed);
    408  1.3   tsutsui 		didit = 1;
    409  1.3   tsutsui 	}
    410  1.3   tsutsui 	speed = dzdivisor(38400);
    411  1.1     pooka 
    412  1.1     pooka 	if (speed == 0) {
    413  1.3   tsutsui 		(void)dzmctl(sc, line, 0, DMSET);	/* hang up line */
    414  1.3   tsutsui 		return 0;
    415  1.1     pooka 	}
    416  1.1     pooka 
    417  1.3   tsutsui 	switch (cflag & CSIZE) {
    418  1.3   tsutsui 	case CS5:
    419  1.1     pooka 		lpr = USC_BPC_5;
    420  1.1     pooka 		break;
    421  1.3   tsutsui 	case CS6:
    422  1.1     pooka 		lpr = USC_BPC_6;
    423  1.1     pooka 		break;
    424  1.3   tsutsui 	case CS7:
    425  1.1     pooka 		lpr = USC_BPC_7;
    426  1.1     pooka 		break;
    427  1.3   tsutsui 	default:
    428  1.1     pooka 		lpr = USC_BPC_8;
    429  1.1     pooka 		break;
    430  1.1     pooka 	}
    431  1.1     pooka 	if (cflag & CSTOPB)
    432  1.1     pooka 		lpr |= USC_2STOP;
    433  1.1     pooka 
    434  1.1     pooka 	if (cflag & PARENB) {
    435  1.3   tsutsui 		if (cflag & PARODD)
    436  1.3   tsutsui 			lpr |= USC_ODD;
    437  1.3   tsutsui 		else
    438  1.3   tsutsui 			lpr |= USC_EVEN;
    439  1.3   tsutsui 	} else
    440  1.3   tsutsui 		lpr |= USC_NONE;
    441  1.1     pooka 
    442  1.1     pooka 	s = spltty();
    443  1.1     pooka 
    444  1.1     pooka 	dzr = sc->sc_dr;
    445  1.1     pooka 
    446  1.3   tsutsui 	dzr->Baud = speed;
    447  1.3   tsutsui 	dzr->Control = USC_CLKDIV_4 | USC_TXEN | USC_RXEN | lpr;
    448  1.1     pooka #define USI_INTRS (USI_RXRDY|USI_RXBRK|USI_OVRE|USI_FRAME|USI_PARE)
    449  1.3   tsutsui 	dzr->IntrEnable = USI_INTRS;
    450  1.1     pooka 
    451  1.3   tsutsui 	(void)splx(s);
    452  1.3   tsutsui 	return 0;
    453  1.1     pooka }
    454  1.1     pooka 
    455  1.1     pooka static unsigned
    456  1.1     pooka dzmctl(struct dz_softc *sc, int line, int bits, int how)
    457  1.1     pooka {
    458  1.3   tsutsui 	unsigned int mbits;
    459  1.1     pooka 	int s;
    460  1.1     pooka 	struct _Usart *dzr;
    461  1.1     pooka 
    462  1.1     pooka 	mbits = 0;
    463  1.1     pooka 
    464  1.1     pooka 	s = spltty();
    465  1.1     pooka 
    466  1.1     pooka 	dzr = sc->sc_dr;
    467  1.1     pooka 
    468  1.3   tsutsui 	/* we have no modem control bits (CD,RI,DTR,DSR,..) */
    469  1.3   tsutsui 	mbits |= TIOCM_CD;
    470  1.3   tsutsui 	mbits |= TIOCM_DTR;
    471  1.1     pooka 
    472  1.1     pooka 	if (dzr->ChannelStatus & USI_RXBRK)
    473  1.1     pooka 		mbits |= TIOCM_BRK;
    474  1.1     pooka 
    475  1.3   tsutsui 	switch (how) {
    476  1.3   tsutsui 	case DMSET:
    477  1.1     pooka 		mbits = bits;
    478  1.1     pooka 		break;
    479  1.1     pooka 
    480  1.3   tsutsui 	case DMBIS:
    481  1.1     pooka 		mbits |= bits;
    482  1.1     pooka 		break;
    483  1.1     pooka 
    484  1.3   tsutsui 	case DMBIC:
    485  1.1     pooka 		mbits &= ~bits;
    486  1.1     pooka 		break;
    487  1.1     pooka 
    488  1.3   tsutsui 	case DMGET:
    489  1.3   tsutsui 		(void)splx(s);
    490  1.3   tsutsui 		return mbits;
    491  1.1     pooka 	}
    492  1.1     pooka 
    493  1.3   tsutsui 	/* BUGBUG work in progress */
    494  1.1     pooka 	if (mbits & TIOCM_BRK) {
    495  1.1     pooka 		sc->sc_brk |= (1 << line);
    496  1.3   tsutsui 		dzr->Control |= USC_STTBRK;
    497  1.1     pooka 	} else {
    498  1.1     pooka 		sc->sc_brk &= ~(1 << line);
    499  1.3   tsutsui 		dzr->Control |= USC_STPBRK;
    500  1.1     pooka 	}
    501  1.1     pooka 
    502  1.3   tsutsui 	(void)splx(s);
    503  1.3   tsutsui 	return mbits;
    504  1.1     pooka }
    505  1.1     pooka 
    506  1.1     pooka 
    507  1.1     pooka #if defined(DDB)
    508  1.1     pooka int dz_ddb = 0;
    509  1.1     pooka #endif
    510  1.1     pooka 
    511  1.1     pooka /* Receiver Interrupt */
    512  1.1     pooka 
    513  1.1     pooka void
    514  1.1     pooka dzrint(struct dz_softc *sc, uint32_t csr)
    515  1.1     pooka {
    516  1.1     pooka 	struct tty *tp;
    517  1.6  christos 	int cc;
    518  1.1     pooka 	struct _Usart *dzr;
    519  1.1     pooka 
    520  1.1     pooka 	sc->sc_rxint++;
    521  1.1     pooka 	dzr = sc->sc_dr;
    522  1.1     pooka 
    523  1.3   tsutsui 	cc = dzr->RxData;
    524  1.3   tsutsui 	tp = sc->sc_dz.dz_tty;
    525  1.3   tsutsui 
    526  1.3   tsutsui 	/* clear errors before we print or bail out */
    527  1.3   tsutsui 	if (csr & (USI_OVRE|USI_FRAME|USI_PARE))
    528  1.3   tsutsui 		dzr->Control = USC_RSTSTA;
    529  1.3   tsutsui 
    530  1.3   tsutsui 	if (!(tp->t_state & TS_ISOPEN)) {
    531  1.3   tsutsui 		wakeup(&tp->t_rawq);
    532  1.3   tsutsui 		return;
    533  1.3   tsutsui 	}
    534  1.3   tsutsui 
    535  1.3   tsutsui 	if (csr & USI_OVRE) {
    536  1.3   tsutsui 		log(LOG_WARNING, "%s: silo overflow, line %d\n",
    537  1.4   tsutsui 		    device_xname(sc->sc_dev), 0);
    538  1.3   tsutsui 	}
    539  1.1     pooka 
    540  1.3   tsutsui 	if (csr & USI_FRAME)
    541  1.3   tsutsui 		cc |= TTY_FE;
    542  1.3   tsutsui 	if (csr & USI_PARE)
    543  1.3   tsutsui 		cc |= TTY_PE;
    544  1.1     pooka 
    545  1.1     pooka #if defined(DDB)
    546  1.3   tsutsui 	/* ^P drops into DDB */
    547  1.3   tsutsui 	if (dz_ddb && (cc == 0x10))
    548  1.3   tsutsui 		Debugger();
    549  1.1     pooka #endif
    550  1.3   tsutsui 	(*tp->t_linesw->l_rint)(cc, tp);
    551  1.1     pooka }
    552  1.1     pooka 
    553  1.1     pooka /* Transmitter Interrupt */
    554  1.1     pooka 
    555  1.1     pooka void
    556  1.1     pooka dzxint(struct dz_softc *sc, uint32_t csr)
    557  1.1     pooka {
    558  1.1     pooka 	struct tty *tp;
    559  1.1     pooka 	struct clist *cl;
    560  1.1     pooka 	int ch;
    561  1.1     pooka 	struct _Usart *dzr;
    562  1.1     pooka 
    563  1.1     pooka 	dzr = sc->sc_dr;
    564  1.1     pooka 
    565  1.3   tsutsui 	tp = sc->sc_dz.dz_tty;
    566  1.3   tsutsui 	cl = &tp->t_outq;
    567  1.3   tsutsui 	tp->t_state &= ~TS_BUSY;
    568  1.3   tsutsui 
    569  1.3   tsutsui 	/* Just send out a char if we have one */
    570  1.3   tsutsui 	if (cl->c_cc) {
    571  1.3   tsutsui 		tp->t_state |= TS_BUSY;
    572  1.3   tsutsui 		ch = getc(cl);
    573  1.3   tsutsui 		dzr->TxData = ch;
    574  1.3   tsutsui 		dzr->IntrEnable = USI_TXRDY;
    575  1.3   tsutsui 		return;
    576  1.3   tsutsui 	}
    577  1.3   tsutsui 
    578  1.3   tsutsui 	/* Nothing to send; turn off intr */
    579  1.3   tsutsui 	dzr->IntrDisable = USI_TXRDY;
    580  1.3   tsutsui 
    581  1.3   tsutsui 	if (tp->t_state & TS_FLUSH)
    582  1.3   tsutsui 		tp->t_state &= ~TS_FLUSH;
    583  1.3   tsutsui 	else
    584  1.3   tsutsui 		ndflush(&tp->t_outq, cl->c_cc);
    585  1.3   tsutsui 
    586  1.3   tsutsui 	(*tp->t_linesw->l_start)(tp);
    587  1.3   tsutsui }
    588  1.3   tsutsui 
    589  1.3   tsutsui /*
    590  1.3   tsutsui  * Machdep part of the driver
    591  1.3   tsutsui  */
    592  1.3   tsutsui int	dz_ebus_match(device_t, cfdata_t, void *);
    593  1.3   tsutsui void	dz_ebus_attach(device_t, device_t, void *);
    594  1.1     pooka int	dz_ebus_intr(void *, void *);
    595  1.1     pooka 
    596  1.1     pooka void	dz_ebus_cnsetup(paddr_t);
    597  1.3   tsutsui void	dz_ebus_cninit(struct consdev *);
    598  1.1     pooka int	dz_ebus_cngetc(dev_t);
    599  1.1     pooka void	dz_ebus_cnputc(dev_t, int);
    600  1.1     pooka void	dz_ebus_cnpollc(dev_t, int);
    601  1.1     pooka 
    602  1.1     pooka static int	dz_ebus_getmajor(void);
    603  1.1     pooka 
    604  1.4   tsutsui CFATTACH_DECL_NEW(dz_ebus, sizeof(struct dz_softc),
    605  1.1     pooka     dz_ebus_match, dz_ebus_attach, NULL, NULL);
    606  1.1     pooka 
    607  1.1     pooka struct consdev dz_ebus_consdev = {
    608  1.1     pooka 	NULL, dz_ebus_cninit, dz_ebus_cngetc, dz_ebus_cnputc,
    609  1.1     pooka 	dz_ebus_cnpollc, NULL, NULL, NULL, NODEV, CN_NORMAL,
    610  1.1     pooka };
    611  1.1     pooka 
    612  1.3   tsutsui /*
    613  1.3   tsutsui  * Points to the console regs. Special mapping until VM is turned on.
    614  1.1     pooka  */
    615  1.1     pooka struct _Usart *dzcn;
    616  1.1     pooka 
    617  1.1     pooka int
    618  1.3   tsutsui dz_ebus_match(device_t parent, cfdata_t cf, void *aux)
    619  1.1     pooka {
    620  1.1     pooka 	struct ebus_attach_args *iba;
    621  1.3   tsutsui 	struct _Usart *us;
    622  1.1     pooka 
    623  1.1     pooka 	iba = aux;
    624  1.1     pooka 
    625  1.1     pooka 	if (strcmp(iba->ia_name, "dz") != 0)
    626  1.3   tsutsui 		return 0;
    627  1.1     pooka 
    628  1.3   tsutsui 	us = (struct _Usart *)iba->ia_vaddr;
    629  1.3   tsutsui 	if ((us == NULL) ||
    630  1.3   tsutsui 	    (us->Tag != PMTTAG_USART))
    631  1.3   tsutsui 		return 0;
    632  1.1     pooka 
    633  1.3   tsutsui 	return 1;
    634  1.1     pooka }
    635  1.1     pooka 
    636  1.1     pooka void
    637  1.3   tsutsui dz_ebus_attach(device_t parent, device_t self, void *aux)
    638  1.1     pooka {
    639  1.1     pooka 	struct ebus_attach_args *iba;
    640  1.1     pooka 	struct dz_softc *sc;
    641  1.1     pooka 
    642  1.4   tsutsui 	sc = device_private(self);
    643  1.1     pooka 	iba = aux;
    644  1.1     pooka 
    645  1.4   tsutsui 	sc->sc_dev = self;
    646  1.1     pooka 	sc->sc_dr = (struct _Usart *)iba->ia_vaddr;
    647  1.1     pooka #if DEBUG
    648  1.3   tsutsui 	printf(" virt=%p ", (void *)sc->sc_dr);
    649  1.1     pooka #endif
    650  1.1     pooka 
    651  1.1     pooka 	printf(": neilsart 1 line");
    652  1.1     pooka 	ebus_intr_establish(parent, (void *)iba->ia_cookie, IPL_TTY,
    653  1.1     pooka 	    dz_ebus_intr, sc);
    654  1.1     pooka 
    655  1.1     pooka 	sc->sc_rxint = sc->sc_brk = 0;
    656  1.1     pooka 	sc->sc_consline = 0;
    657  1.1     pooka 
    658  1.1     pooka 	/* Initialize our softc structure. Should be done in open? */
    659  1.1     pooka 
    660  1.2     rmind 	sc->sc_dz.dz_sc = sc;
    661  1.2     rmind 	sc->sc_dz.dz_line = 0;
    662  1.2     rmind 	sc->sc_dz.dz_tty = tty_alloc();
    663  1.1     pooka 
    664  1.1     pooka 	evcnt_attach_dynamic(&sc->sc_rintrcnt, EVCNT_TYPE_INTR, NULL,
    665  1.4   tsutsui 	    device_xname(self), "rintr");
    666  1.1     pooka 	evcnt_attach_dynamic(&sc->sc_tintrcnt, EVCNT_TYPE_INTR, NULL,
    667  1.4   tsutsui 	    device_xname(self), "tintr");
    668  1.1     pooka 
    669  1.3   tsutsui 	/* Initialize hw regs */
    670  1.1     pooka #if 0
    671  1.1     pooka 	DZ_WRITE_WORD(dr_csr, DZ_CSR_MSE | DZ_CSR_RXIE | DZ_CSR_TXIE);
    672  1.1     pooka 	DZ_WRITE_BYTE(dr_dtr, 0);
    673  1.1     pooka 	DZ_WRITE_BYTE(dr_break, 0);
    674  1.1     pooka #endif
    675  1.1     pooka 
    676  1.3   tsutsui 	/* Switch the console to virtual mode */
    677  1.3   tsutsui 	dzcn = sc->sc_dr;
    678  1.3   tsutsui 	/* And test it */
    679  1.1     pooka 	printf("\n");
    680  1.1     pooka }
    681  1.1     pooka 
    682  1.1     pooka static int
    683  1.1     pooka dz_ebus_getmajor(void)
    684  1.1     pooka {
    685  1.1     pooka 	extern const struct cdevsw dz_cdevsw;
    686  1.1     pooka 	static int cache = -1;
    687  1.1     pooka 
    688  1.1     pooka 	if (cache != -1)
    689  1.3   tsutsui 		return cache;
    690  1.1     pooka 
    691  1.3   tsutsui 	return cache = cdevsw_lookup_major(&dz_cdevsw);
    692  1.1     pooka }
    693  1.1     pooka 
    694  1.1     pooka int
    695  1.1     pooka dz_ebus_intr(void *cookie, void *f)
    696  1.1     pooka {
    697  1.1     pooka 	struct dz_softc *sc;
    698  1.1     pooka 	struct _Usart *dzr;
    699  1.1     pooka 	uint32_t csr;
    700  1.1     pooka 
    701  1.1     pooka 	sc = cookie;
    702  1.1     pooka 	dzr = sc->sc_dr;
    703  1.1     pooka 
    704  1.1     pooka #define USI_INTERRUPTS (USI_INTRS|USI_TXRDY)
    705  1.1     pooka 
    706  1.3   tsutsui 	for (; ((csr = (dzr->ChannelStatus & dzr->IntrMask)) &
    707  1.3   tsutsui 	    USI_INTERRUPTS) != 0;) {
    708  1.1     pooka 		if ((csr & USI_INTRS) != 0)
    709  1.1     pooka 			dzrint(sc, csr);
    710  1.1     pooka 		if ((csr & USI_TXRDY) != 0)
    711  1.1     pooka 			dzxint(sc, csr);
    712  1.1     pooka 	}
    713  1.1     pooka 
    714  1.3   tsutsui 	return 0;
    715  1.1     pooka }
    716  1.1     pooka 
    717  1.1     pooka void
    718  1.1     pooka dz_ebus_cnsetup(paddr_t addr)
    719  1.1     pooka {
    720  1.1     pooka 
    721  1.1     pooka 	dzcn = (struct _Usart *)addr;
    722  1.1     pooka 
    723  1.1     pooka #if 0
    724  1.3   tsutsui 	/*
    725  1.3   tsutsui 	 * Initialize enough to xmit/recv via polling.
    726  1.3   tsutsui 	 * Bootloader might or might not have done it.
    727  1.3   tsutsui 	 */
    728  1.3   tsutsui 	dzcn->Control =
    729  1.3   tsutsui 	    USC_RXEN |
    730  1.3   tsutsui 	    USC_TXEN |
    731  1.3   tsutsui 	    USC_BPC_8 |
    732  1.3   tsutsui 	    USC_NONE |
    733  1.3   tsutsui 	    USC_1STOP |
    734  1.3   tsutsui 	    USC_CLKDIV_4;
    735  1.3   tsutsui 	dzcn->Baud = 0x29; /* 38400 */
    736  1.1     pooka #endif
    737  1.1     pooka 
    738  1.1     pooka 	/*
    739  1.1     pooka 	 * Point the console at us
    740  1.1     pooka 	 */
    741  1.1     pooka 	cn_tab = &dz_ebus_consdev;
    742  1.1     pooka 	cn_tab->cn_pri = CN_NORMAL;/*CN_REMOTE?*/
    743  1.1     pooka 	cn_tab->cn_dev = makedev(dz_ebus_getmajor(), 0);
    744  1.1     pooka }
    745  1.1     pooka 
    746  1.3   tsutsui void
    747  1.3   tsutsui dz_ebus_cninit(struct consdev *cn)
    748  1.1     pooka {
    749  1.1     pooka }
    750  1.1     pooka 
    751  1.1     pooka int
    752  1.1     pooka dz_ebus_cngetc(dev_t dev)
    753  1.1     pooka {
    754  1.1     pooka 	int c, s;
    755  1.1     pooka 
    756  1.1     pooka 	c = 0;
    757  1.1     pooka 	s = spltty();
    758  1.1     pooka 
    759  1.3   tsutsui 	while ((dzcn->ChannelStatus & USI_RXRDY) == 0)
    760  1.3   tsutsui 		DELAY(10);
    761  1.3   tsutsui 	c = dzcn->RxData;
    762  1.1     pooka 
    763  1.1     pooka 	splx(s);
    764  1.1     pooka 	if (c == 13) /* map cr->ln */
    765  1.1     pooka 		c = 10;
    766  1.3   tsutsui 	return c;
    767  1.1     pooka }
    768  1.1     pooka 
    769  1.1     pooka int dzflipped = 0;
    770  1.1     pooka void
    771  1.1     pooka dz_ebus_cnputc(dev_t dev, int ch)
    772  1.1     pooka {
    773  1.1     pooka 	int timeout, s;
    774  1.1     pooka 
    775  1.1     pooka 	/* Don't hang the machine! */
    776  1.1     pooka 	timeout = 1 << 15;
    777  1.1     pooka 
    778  1.1     pooka 	s = spltty();
    779  1.1     pooka 
    780  1.1     pooka #if 1
    781  1.3   tsutsui 	/* Keep wired to hunt for a bug */
    782  1.3   tsutsui 	if (dzcn && (dzcn != (struct _Usart *)0xfff90000)) {
    783  1.3   tsutsui 		dzcn = (struct _Usart *)0xfff90000;
    784  1.3   tsutsui 		dzflipped++;
    785  1.3   tsutsui 	}
    786  1.1     pooka #endif
    787  1.1     pooka 
    788  1.1     pooka 	/* Wait until ready */
    789  1.1     pooka 	while ((dzcn->ChannelStatus & USI_TXRDY) == 0)
    790  1.1     pooka 		if (--timeout < 0)
    791  1.1     pooka 			break;
    792  1.1     pooka 
    793  1.1     pooka 	/* Put the character */
    794  1.1     pooka 	dzcn->TxData = ch;
    795  1.1     pooka 
    796  1.1     pooka 	splx(s);
    797  1.1     pooka }
    798  1.1     pooka 
    799  1.3   tsutsui /*
    800  1.3   tsutsui  * Called before/after going into poll mode
    801  1.1     pooka  */
    802  1.3   tsutsui void
    803  1.1     pooka dz_ebus_cnpollc(dev_t dev, int on)
    804  1.1     pooka {
    805  1.1     pooka }
    806