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