Home | History | Annotate | Line # | Download | only in qbus
dl.c revision 1.18.4.1
      1 /*	$NetBSD: dl.c,v 1.18.4.1 2001/10/10 11:56:58 fvdl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1997  Ben Harris.  All rights reserved.
     41  * Copyright (c) 1996  Ken C. Wellsch.  All rights reserved.
     42  * Copyright (c) 1982, 1986, 1990, 1992, 1993
     43  *	The Regents of the University of California.  All rights reserved.
     44  *
     45  * This code is derived from software contributed to Berkeley by
     46  * Ralph Campbell and Rick Macklem.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. All advertising materials mentioning features or use of this software
     57  *    must display the following acknowledgement:
     58  *	This product includes software developed by the University of
     59  *	California, Berkeley and its contributors.
     60  * 4. Neither the name of the University nor the names of its contributors
     61  *    may be used to endorse or promote products derived from this software
     62  *    without specific prior written permission.
     63  *
     64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     74  * SUCH DAMAGE.
     75  */
     76 
     77 /*
     78  * dl.c -- Device driver for the DL11 and DLV11 serial cards.
     79  *
     80  * OS-interface code derived from the dz and dca (hp300) drivers.
     81  */
     82 
     83 #include <sys/param.h>
     84 #include <sys/systm.h>
     85 #include <sys/ioctl.h>
     86 #include <sys/tty.h>
     87 #include <sys/proc.h>
     88 #include <sys/map.h>
     89 #include <sys/buf.h>
     90 #include <sys/conf.h>
     91 #include <sys/file.h>
     92 #include <sys/uio.h>
     93 #include <sys/kernel.h>
     94 #include <sys/syslog.h>
     95 #include <sys/device.h>
     96 #include <sys/vnode.h>
     97 
     98 #include <machine/bus.h>
     99 
    100 #include <dev/qbus/ubavar.h>
    101 
    102 #include <dev/qbus/dlreg.h>
    103 
    104 #include "ioconf.h"
    105 
    106 struct dl_softc {
    107 	struct device	sc_dev;
    108 	struct evcnt	sc_rintrcnt;
    109 	struct evcnt	sc_tintrcnt;
    110 	bus_space_tag_t	sc_iot;
    111 	bus_space_handle_t sc_ioh;
    112 	struct tty	*sc_tty;
    113 };
    114 
    115 static	int	dl_match (struct device *, struct cfdata *, void *);
    116 static	void	dl_attach (struct device *, struct device *, void *);
    117 static	void	dlrint (void *);
    118 static	void	dlxint (void *);
    119 static	void	dlstart (struct tty *);
    120 static	int	dlparam (struct tty *, struct termios *);
    121 static	void	dlbrk (struct dl_softc *, int);
    122 struct	tty *	dltty (struct vnode *);
    123 
    124 cdev_decl(dl);
    125 
    126 struct cfattach dl_ca = {
    127 	sizeof(struct dl_softc), dl_match, dl_attach
    128 };
    129 
    130 #define	DL_READ_WORD(reg) \
    131 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, reg)
    132 #define	DL_WRITE_WORD(reg, val) \
    133 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, reg, val)
    134 #define	DL_WRITE_BYTE(reg, val) \
    135 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, val)
    136 
    137 /* Autoconfig handles: setup the controller to interrupt, */
    138 /* then complete the housecleaning for full operation */
    139 
    140 static int
    141 dl_match (struct device *parent, struct cfdata *cf, void *aux)
    142 {
    143 	struct uba_attach_args *ua = aux;
    144 
    145 #ifdef DL_DEBUG
    146 	printf("Probing for dl at %lo ... ", (long)ua->ua_iaddr);
    147 #endif
    148 
    149 	bus_space_write_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR, DL_XCSR_TXIE);
    150 	if (bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR) !=
    151 	    (DL_XCSR_TXIE | DL_XCSR_TX_READY)) {
    152 #ifdef DL_DEBUG
    153 	        printf("failed (step 1; XCSR = %.4b)\n",
    154 		    bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR),
    155 		    DL_XCSR_BITS);
    156 #endif
    157 		return 0;
    158 	}
    159 
    160 	/*
    161 	 * We have to force an interrupt so the uba driver can work
    162 	 * out where we are.  Unfortunately, the only way to make a
    163 	 * DL11 interrupt is to get it to send or receive a
    164 	 * character.  We'll send a NUL and hope it doesn't hurt
    165 	 * anything.
    166 	 */
    167 
    168 	bus_space_write_1(ua->ua_iot, ua->ua_ioh, DL_UBA_XBUFL, '\0');
    169 #if 0 /* This test seems to fail 2/3 of the time :-( */
    170 	if (dladdr->dl_xcsr != (DL_XCSR_TXIE)) {
    171 #ifdef DL_DEBUG
    172 	        printf("failed (step 2; XCSR = %.4b)\n", dladdr->dl_xcsr,
    173 		       DL_XCSR_BITS);
    174 #endif
    175 		return 0;
    176 	}
    177 #endif
    178 	DELAY(100000); /* delay 1/10 s for character to transmit */
    179 	if (bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR) !=
    180 	    (DL_XCSR_TXIE | DL_XCSR_TX_READY)) {
    181 #ifdef DL_DEBUG
    182 	        printf("failed (step 3; XCSR = %.4b)\n",
    183 		    bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR),
    184 		    DL_XCSR_BITS);
    185 #endif
    186 		return 0;
    187 	}
    188 
    189 
    190         /* What else do I need to do? */
    191 
    192 	return 1;
    193 
    194 }
    195 
    196 static void
    197 dl_attach (struct device *parent, struct device *self, void *aux)
    198 {
    199 	struct dl_softc *sc = (void *)self;
    200 	struct uba_attach_args *ua = aux;
    201 
    202 	sc->sc_iot = ua->ua_iot;
    203 	sc->sc_ioh = ua->ua_ioh;
    204 
    205 	/* Tidy up the device */
    206 
    207 	DL_WRITE_WORD(DL_UBA_RCSR, DL_RCSR_RXIE);
    208 	DL_WRITE_WORD(DL_UBA_XCSR, DL_XCSR_TXIE);
    209 
    210 	/* Initialize our softc structure. Should be done in open? */
    211 
    212 	sc->sc_tty = ttymalloc();
    213 	tty_attach(sc->sc_tty);
    214 
    215 	/* Now register the TX & RX interrupt handlers */
    216 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec,
    217 		dlxint, sc, &sc->sc_tintrcnt);
    218 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec - 4,
    219 		dlrint, sc, &sc->sc_rintrcnt);
    220 	evcnt_attach_dynamic(&sc->sc_rintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
    221 		sc->sc_dev.dv_xname, "rintr");
    222 	evcnt_attach_dynamic(&sc->sc_tintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
    223 		sc->sc_dev.dv_xname, "tintr");
    224 
    225 	printf("\n");
    226 }
    227 
    228 /* Receiver Interrupt Handler */
    229 
    230 static void
    231 dlrint(void *arg)
    232 {
    233 	struct dl_softc *sc = arg;
    234 
    235 	if (DL_READ_WORD(DL_UBA_RCSR) & DL_RCSR_RX_DONE) {
    236 		struct tty *tp = sc->sc_tty;
    237 		unsigned c;
    238 		int cc;
    239 
    240 	        c = DL_READ_WORD(DL_UBA_RBUF);
    241 		cc = c & 0xFF;
    242 
    243 		if (!(tp->t_state & TS_ISOPEN)) {
    244 			wakeup((caddr_t)&tp->t_rawq);
    245 			return;
    246 		}
    247 
    248 		if (c & DL_RBUF_OVERRUN_ERR) {
    249 			/*
    250 			 * XXX: This should really be logged somwhere
    251 			 * else where we can afford the time.
    252 			 */
    253 			log(LOG_WARNING, "%s: rx overrun\n",
    254 			    sc->sc_dev.dv_xname);
    255 		}
    256 		if (c & DL_RBUF_FRAMING_ERR)
    257 			cc |= TTY_FE;
    258 		if (c & DL_RBUF_PARITY_ERR)
    259 			cc |= TTY_PE;
    260 
    261 		(*tp->t_linesw->l_rint)(cc, tp);
    262 #if defined(DIAGNOSTIC)
    263 	} else {
    264 		log(LOG_WARNING, "%s: stray rx interrupt\n",
    265 		    sc->sc_dev.dv_xname);
    266 #endif
    267 	}
    268 }
    269 
    270 /* Transmitter Interrupt Handler */
    271 
    272 static void
    273 dlxint(void *arg)
    274 {
    275 	struct dl_softc *sc = arg;
    276 	struct tty *tp = sc->sc_tty;
    277 
    278 	tp->t_state &= ~(TS_BUSY | TS_FLUSH);
    279 	(*tp->t_linesw->l_start)(tp);
    280 
    281 	return;
    282 }
    283 
    284 int
    285 dlopen(struct vnode *devvp, int flag, int mode, struct proc *p)
    286 {
    287 	struct tty *tp;
    288 	struct dl_softc *sc;
    289 	int unit;
    290 	dev_t dev;
    291 
    292 	dev = vdev_rdev(devvp);
    293 	unit = minor(dev);
    294 
    295 	if (unit >= dl_cd.cd_ndevs || dl_cd.cd_devs[unit] == NULL)
    296 		return ENXIO;
    297 	sc = dl_cd.cd_devs[unit];
    298 
    299 	tp = sc->sc_tty;
    300 	if (tp == NULL)
    301 		return ENODEV;
    302 	tp->t_oproc = dlstart;
    303 	tp->t_param = dlparam;
    304 	tp->t_devvp = devvp;
    305 
    306 	vdev_setprivdata(devvp, sc);
    307 
    308 	if (!(tp->t_state & TS_ISOPEN)) {
    309 		ttychars(tp);
    310 		tp->t_iflag = TTYDEF_IFLAG;
    311 		tp->t_oflag = TTYDEF_OFLAG;
    312 		/* No modem control, so set CLOCAL. */
    313 		tp->t_cflag = TTYDEF_CFLAG | CLOCAL;
    314 		tp->t_lflag = TTYDEF_LFLAG;
    315 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    316 
    317 		dlparam(tp, &tp->t_termios);
    318 		ttsetwater(tp);
    319 
    320 	} else if ((tp->t_state & TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    321 		return EBUSY;
    322 
    323 	return ((*tp->t_linesw->l_open)(devvp, tp));
    324 }
    325 
    326 /*ARGSUSED*/
    327 int
    328 dlclose(struct vnode *devvp, int flag, int mode, struct proc *p)
    329 {
    330 	struct dl_softc *sc = vdev_privdata(devvp);
    331 	struct tty *tp = sc->sc_tty;
    332 
    333 	(*tp->t_linesw->l_close)(tp, flag);
    334 
    335 	/* Make sure a BREAK state is not left enabled. */
    336 	dlbrk(sc, 0);
    337 
    338 	return (ttyclose(tp));
    339 }
    340 
    341 int
    342 dlread(struct vnode *devvp, struct uio *uio, int flag)
    343 {
    344 	struct dl_softc *sc = vdev_privdata(devvp);
    345 	struct tty *tp = sc->sc_tty;
    346 
    347 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    348 }
    349 
    350 int
    351 dlwrite(struct vnode *devvp, struct uio *uio, int flag)
    352 {
    353 	struct dl_softc *sc = vdev_privdata(devvp);
    354 	struct tty *tp = sc->sc_tty;
    355 
    356 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    357 }
    358 
    359 int
    360 dlpoll(struct vnode *devvp, int events, struct proc *p)
    361 {
    362 	struct dl_softc *sc = vdev_privdata(devvp);
    363 	struct tty *tp = sc->sc_tty;
    364 
    365 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    366 }
    367 
    368 int
    369 dlioctl(struct vnode *devvp, unsigned long cmd, caddr_t data, int flag,
    370 	struct proc *p)
    371 {
    372 	struct dl_softc *sc = vdev_privdata(devvp);
    373         struct tty *tp = sc->sc_tty;
    374         int error;
    375 
    376 
    377         error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    378         if (error >= 0)
    379                 return (error);
    380         error = ttioctl(tp, cmd, data, flag, p);
    381         if (error >= 0)
    382                 return (error);
    383 
    384 	switch (cmd) {
    385 
    386         case TIOCSBRK:
    387                 dlbrk(sc, 1);
    388                 break;
    389 
    390         case TIOCCBRK:
    391                 dlbrk(sc, 0);
    392                 break;
    393 
    394         case TIOCMGET:
    395 		/* No modem control, assume they're all low. */
    396                 *(int *)data = 0;
    397                 break;
    398 
    399         default:
    400                 return (ENOTTY);
    401         }
    402         return (0);
    403 }
    404 
    405 struct tty *
    406 dltty(struct vnode *devvp)
    407 {
    408 	struct dl_softc *sc = vdev_privdata(devvp);
    409 
    410 	return sc->sc_tty;
    411 }
    412 
    413 void
    414 dlstop(struct tty *tp, int flag)
    415 {
    416 	int s = spltty();
    417 
    418 	if ((tp->t_state & (TS_BUSY|TS_TTSTOP)) == TS_BUSY)
    419 		tp->t_state |= TS_FLUSH;
    420         splx(s);
    421 }
    422 
    423 static void
    424 dlstart(struct tty *tp)
    425 {
    426 	struct dl_softc *sc = vdev_privdata(tp->t_devvp);
    427 	int s = spltty();
    428 
    429         if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
    430                 goto out;
    431         if (tp->t_outq.c_cc <= tp->t_lowat) {
    432                 if (tp->t_state & TS_ASLEEP) {
    433                         tp->t_state &= ~TS_ASLEEP;
    434                         wakeup((caddr_t)&tp->t_outq);
    435                 }
    436                 selwakeup(&tp->t_wsel);
    437         }
    438         if (tp->t_outq.c_cc == 0)
    439                 goto out;
    440 
    441 
    442 	if (DL_READ_WORD(DL_UBA_XCSR) & DL_XCSR_TX_READY) {
    443 		tp->t_state |= TS_BUSY;
    444 		DL_WRITE_BYTE(DL_UBA_XBUFL, getc(&tp->t_outq));
    445 	}
    446 out:
    447 	splx(s);
    448 	return;
    449 }
    450 
    451 /*ARGSUSED*/
    452 static int
    453 dlparam(struct tty *tp, struct termios *t)
    454 {
    455 	/*
    456 	 * All this kind of stuff (speed, character format, whatever)
    457 	 * is set by jumpers on the card.  Changing it is thus rather
    458 	 * tricky for a mere device driver.
    459 	 */
    460 	return 0;
    461 }
    462 
    463 static void
    464 dlbrk(struct dl_softc *sc, int state)
    465 {
    466 	int s = spltty();
    467 
    468 	if (state) {
    469 		DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) |
    470 		    DL_XCSR_TX_BREAK);
    471 	} else {
    472 		DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) &
    473 		    ~DL_XCSR_TX_BREAK);
    474 	}
    475 	splx(s);
    476 }
    477