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