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