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