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