Home | History | Annotate | Line # | Download | only in qbus
dl.c revision 1.20
      1 /*	$NetBSD: dl.c,v 1.20 2002/03/17 19:41:01 atatat 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/cdefs.h>
     84 __KERNEL_RCSID(0, "$NetBSD: dl.c,v 1.20 2002/03/17 19:41:01 atatat Exp $");
     85 
     86 #include <sys/param.h>
     87 #include <sys/systm.h>
     88 #include <sys/ioctl.h>
     89 #include <sys/tty.h>
     90 #include <sys/proc.h>
     91 #include <sys/map.h>
     92 #include <sys/buf.h>
     93 #include <sys/conf.h>
     94 #include <sys/file.h>
     95 #include <sys/uio.h>
     96 #include <sys/kernel.h>
     97 #include <sys/syslog.h>
     98 #include <sys/device.h>
     99 
    100 #include <machine/bus.h>
    101 
    102 #include <dev/qbus/ubavar.h>
    103 
    104 #include <dev/qbus/dlreg.h>
    105 
    106 #include "ioconf.h"
    107 
    108 struct dl_softc {
    109 	struct device	sc_dev;
    110 	struct evcnt	sc_rintrcnt;
    111 	struct evcnt	sc_tintrcnt;
    112 	bus_space_tag_t	sc_iot;
    113 	bus_space_handle_t sc_ioh;
    114 	struct tty	*sc_tty;
    115 };
    116 
    117 static	int	dl_match (struct device *, struct cfdata *, void *);
    118 static	void	dl_attach (struct device *, struct device *, void *);
    119 static	void	dlrint (void *);
    120 static	void	dlxint (void *);
    121 static	void	dlstart (struct tty *);
    122 static	int	dlparam (struct tty *, struct termios *);
    123 static	void	dlbrk (struct dl_softc *, int);
    124 struct	tty *	dltty (dev_t);
    125 
    126 cdev_decl(dl);
    127 
    128 struct cfattach dl_ca = {
    129 	sizeof(struct dl_softc), dl_match, dl_attach
    130 };
    131 
    132 #define	DL_READ_WORD(reg) \
    133 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, reg)
    134 #define	DL_WRITE_WORD(reg, val) \
    135 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, reg, val)
    136 #define	DL_WRITE_BYTE(reg, val) \
    137 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, val)
    138 
    139 /* Autoconfig handles: setup the controller to interrupt, */
    140 /* then complete the housecleaning for full operation */
    141 
    142 static int
    143 dl_match (struct device *parent, struct cfdata *cf, void *aux)
    144 {
    145 	struct uba_attach_args *ua = aux;
    146 
    147 #ifdef DL_DEBUG
    148 	printf("Probing for dl at %lo ... ", (long)ua->ua_iaddr);
    149 #endif
    150 
    151 	bus_space_write_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR, DL_XCSR_TXIE);
    152 	if (bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR) !=
    153 	    (DL_XCSR_TXIE | DL_XCSR_TX_READY)) {
    154 #ifdef DL_DEBUG
    155 	        printf("failed (step 1; XCSR = %.4b)\n",
    156 		    bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR),
    157 		    DL_XCSR_BITS);
    158 #endif
    159 		return 0;
    160 	}
    161 
    162 	/*
    163 	 * We have to force an interrupt so the uba driver can work
    164 	 * out where we are.  Unfortunately, the only way to make a
    165 	 * DL11 interrupt is to get it to send or receive a
    166 	 * character.  We'll send a NUL and hope it doesn't hurt
    167 	 * anything.
    168 	 */
    169 
    170 	bus_space_write_1(ua->ua_iot, ua->ua_ioh, DL_UBA_XBUFL, '\0');
    171 #if 0 /* This test seems to fail 2/3 of the time :-( */
    172 	if (dladdr->dl_xcsr != (DL_XCSR_TXIE)) {
    173 #ifdef DL_DEBUG
    174 	        printf("failed (step 2; XCSR = %.4b)\n", dladdr->dl_xcsr,
    175 		       DL_XCSR_BITS);
    176 #endif
    177 		return 0;
    178 	}
    179 #endif
    180 	DELAY(100000); /* delay 1/10 s for character to transmit */
    181 	if (bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR) !=
    182 	    (DL_XCSR_TXIE | DL_XCSR_TX_READY)) {
    183 #ifdef DL_DEBUG
    184 	        printf("failed (step 3; XCSR = %.4b)\n",
    185 		    bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR),
    186 		    DL_XCSR_BITS);
    187 #endif
    188 		return 0;
    189 	}
    190 
    191 
    192         /* What else do I need to do? */
    193 
    194 	return 1;
    195 
    196 }
    197 
    198 static void
    199 dl_attach (struct device *parent, struct device *self, void *aux)
    200 {
    201 	struct dl_softc *sc = (void *)self;
    202 	struct uba_attach_args *ua = aux;
    203 
    204 	sc->sc_iot = ua->ua_iot;
    205 	sc->sc_ioh = ua->ua_ioh;
    206 
    207 	/* Tidy up the device */
    208 
    209 	DL_WRITE_WORD(DL_UBA_RCSR, DL_RCSR_RXIE);
    210 	DL_WRITE_WORD(DL_UBA_XCSR, DL_XCSR_TXIE);
    211 
    212 	/* Initialize our softc structure. Should be done in open? */
    213 
    214 	sc->sc_tty = ttymalloc();
    215 	tty_attach(sc->sc_tty);
    216 
    217 	/* Now register the TX & RX interrupt handlers */
    218 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec,
    219 		dlxint, sc, &sc->sc_tintrcnt);
    220 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec - 4,
    221 		dlrint, sc, &sc->sc_rintrcnt);
    222 	evcnt_attach_dynamic(&sc->sc_rintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
    223 		sc->sc_dev.dv_xname, "rintr");
    224 	evcnt_attach_dynamic(&sc->sc_tintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
    225 		sc->sc_dev.dv_xname, "tintr");
    226 
    227 	printf("\n");
    228 }
    229 
    230 /* Receiver Interrupt Handler */
    231 
    232 static void
    233 dlrint(void *arg)
    234 {
    235 	struct dl_softc *sc = arg;
    236 
    237 	if (DL_READ_WORD(DL_UBA_RCSR) & DL_RCSR_RX_DONE) {
    238 		struct tty *tp = sc->sc_tty;
    239 		unsigned c;
    240 		int cc;
    241 
    242 	        c = DL_READ_WORD(DL_UBA_RBUF);
    243 		cc = c & 0xFF;
    244 
    245 		if (!(tp->t_state & TS_ISOPEN)) {
    246 			wakeup((caddr_t)&tp->t_rawq);
    247 			return;
    248 		}
    249 
    250 		if (c & DL_RBUF_OVERRUN_ERR) {
    251 			/*
    252 			 * XXX: This should really be logged somwhere
    253 			 * else where we can afford the time.
    254 			 */
    255 			log(LOG_WARNING, "%s: rx overrun\n",
    256 			    sc->sc_dev.dv_xname);
    257 		}
    258 		if (c & DL_RBUF_FRAMING_ERR)
    259 			cc |= TTY_FE;
    260 		if (c & DL_RBUF_PARITY_ERR)
    261 			cc |= TTY_PE;
    262 
    263 		(*tp->t_linesw->l_rint)(cc, tp);
    264 #if defined(DIAGNOSTIC)
    265 	} else {
    266 		log(LOG_WARNING, "%s: stray rx interrupt\n",
    267 		    sc->sc_dev.dv_xname);
    268 #endif
    269 	}
    270 }
    271 
    272 /* Transmitter Interrupt Handler */
    273 
    274 static void
    275 dlxint(void *arg)
    276 {
    277 	struct dl_softc *sc = arg;
    278 	struct tty *tp = sc->sc_tty;
    279 
    280 	tp->t_state &= ~(TS_BUSY | TS_FLUSH);
    281 	(*tp->t_linesw->l_start)(tp);
    282 
    283 	return;
    284 }
    285 
    286 int
    287 dlopen(dev_t dev, int flag, int mode, struct proc *p)
    288 {
    289 	struct tty *tp;
    290 	struct dl_softc *sc;
    291 	int unit;
    292 
    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_dev = dev;
    305 
    306 	if (!(tp->t_state & TS_ISOPEN)) {
    307 		ttychars(tp);
    308 		tp->t_iflag = TTYDEF_IFLAG;
    309 		tp->t_oflag = TTYDEF_OFLAG;
    310 		/* No modem control, so set CLOCAL. */
    311 		tp->t_cflag = TTYDEF_CFLAG | CLOCAL;
    312 		tp->t_lflag = TTYDEF_LFLAG;
    313 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    314 
    315 		dlparam(tp, &tp->t_termios);
    316 		ttsetwater(tp);
    317 
    318 	} else if ((tp->t_state & TS_XCLUDE) && p->p_ucred->cr_uid != 0)
    319 		return EBUSY;
    320 
    321 	return ((*tp->t_linesw->l_open)(dev, tp));
    322 }
    323 
    324 /*ARGSUSED*/
    325 int
    326 dlclose(dev_t dev, int flag, int mode, struct proc *p)
    327 {
    328 	struct dl_softc *sc = dl_cd.cd_devs[minor(dev)];
    329 	struct tty *tp = sc->sc_tty;
    330 
    331 	(*tp->t_linesw->l_close)(tp, flag);
    332 
    333 	/* Make sure a BREAK state is not left enabled. */
    334 	dlbrk(sc, 0);
    335 
    336 	return (ttyclose(tp));
    337 }
    338 
    339 int
    340 dlread(dev_t dev, struct uio *uio, int flag)
    341 {
    342 	struct dl_softc *sc = dl_cd.cd_devs[minor(dev)];
    343 	struct tty *tp = sc->sc_tty;
    344 
    345 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    346 }
    347 
    348 int
    349 dlwrite(dev_t dev, struct uio *uio, int flag)
    350 {
    351 	struct dl_softc *sc = dl_cd.cd_devs[minor(dev)];
    352 	struct tty *tp = sc->sc_tty;
    353 
    354 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    355 }
    356 
    357 int
    358 dlpoll(dev_t dev, int events, struct proc *p)
    359 {
    360 	struct dl_softc *sc = dl_cd.cd_devs[minor(dev)];
    361 	struct tty *tp = sc->sc_tty;
    362 
    363 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    364 }
    365 
    366 int
    367 dlioctl(dev_t dev, unsigned long cmd, caddr_t data, int flag, struct proc *p)
    368 {
    369 	struct dl_softc *sc = dl_cd.cd_devs[minor(dev)];
    370         struct tty *tp = sc->sc_tty;
    371         int error;
    372 
    373 
    374         error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    375         if (error != EPASSTHROUGH)
    376                 return (error);
    377 
    378         error = ttioctl(tp, cmd, data, flag, p);
    379         if (error != EPASSTHROUGH)
    380                 return (error);
    381 
    382 	switch (cmd) {
    383 
    384         case TIOCSBRK:
    385                 dlbrk(sc, 1);
    386                 break;
    387 
    388         case TIOCCBRK:
    389                 dlbrk(sc, 0);
    390                 break;
    391 
    392         case TIOCMGET:
    393 		/* No modem control, assume they're all low. */
    394                 *(int *)data = 0;
    395                 break;
    396 
    397         default:
    398                 return (EPASSTHROUGH);
    399         }
    400         return (0);
    401 }
    402 
    403 struct tty *
    404 dltty(dev_t dev)
    405 {
    406 	struct dl_softc *sc = dl_cd.cd_devs[minor(dev)];
    407 
    408 	return sc->sc_tty;
    409 }
    410 
    411 void
    412 dlstop(struct tty *tp, int flag)
    413 {
    414 	int s = spltty();
    415 
    416 	if ((tp->t_state & (TS_BUSY|TS_TTSTOP)) == TS_BUSY)
    417 		tp->t_state |= TS_FLUSH;
    418         splx(s);
    419 }
    420 
    421 static void
    422 dlstart(struct tty *tp)
    423 {
    424 	struct dl_softc *sc = dl_cd.cd_devs[minor(tp->t_dev)];
    425 	int s = spltty();
    426 
    427         if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
    428                 goto out;
    429         if (tp->t_outq.c_cc <= tp->t_lowat) {
    430                 if (tp->t_state & TS_ASLEEP) {
    431                         tp->t_state &= ~TS_ASLEEP;
    432                         wakeup((caddr_t)&tp->t_outq);
    433                 }
    434                 selwakeup(&tp->t_wsel);
    435         }
    436         if (tp->t_outq.c_cc == 0)
    437                 goto out;
    438 
    439 
    440 	if (DL_READ_WORD(DL_UBA_XCSR) & DL_XCSR_TX_READY) {
    441 		tp->t_state |= TS_BUSY;
    442 		DL_WRITE_BYTE(DL_UBA_XBUFL, getc(&tp->t_outq));
    443 	}
    444 out:
    445 	splx(s);
    446 	return;
    447 }
    448 
    449 /*ARGSUSED*/
    450 static int
    451 dlparam(struct tty *tp, struct termios *t)
    452 {
    453 	/*
    454 	 * All this kind of stuff (speed, character format, whatever)
    455 	 * is set by jumpers on the card.  Changing it is thus rather
    456 	 * tricky for a mere device driver.
    457 	 */
    458 	return 0;
    459 }
    460 
    461 static void
    462 dlbrk(struct dl_softc *sc, int state)
    463 {
    464 	int s = spltty();
    465 
    466 	if (state) {
    467 		DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) |
    468 		    DL_XCSR_TX_BREAK);
    469 	} else {
    470 		DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) &
    471 		    ~DL_XCSR_TX_BREAK);
    472 	}
    473 	splx(s);
    474 }
    475