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