dl.c revision 1.13 1 /* $NetBSD: dl.c,v 1.13 2000/06/04 06:17:02 matt 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 struct evcnt sc_rintrcnt;
109 struct evcnt sc_tintrcnt;
110 bus_space_tag_t sc_iot;
111 bus_space_handle_t sc_ioh;
112 struct tty *sc_tty;
113 };
114
115 static int dl_match (struct device *, struct cfdata *, void *);
116 static void dl_attach (struct device *, struct device *, void *);
117 static void dlrint (void *);
118 static void dlxint (void *);
119 static void dlstart (struct tty *);
120 static int dlparam (struct tty *, struct termios *);
121 static void dlbrk (struct dl_softc *, int);
122 struct tty * dltty (dev_t);
123 int dlopen (dev_t, int, int, struct proc *);
124 int dlclose (dev_t, int, int, struct proc *);
125 int dlread (dev_t, struct uio *, int);
126 int dlwrite (dev_t, struct uio *, int);
127 int dlioctl (dev_t, int, caddr_t, int, struct proc *);
128 void dlstop (struct tty *, int);
129
130 struct cfattach dl_ca = {
131 sizeof(struct dl_softc), dl_match, dl_attach
132 };
133
134 #define DL_READ_WORD(reg) \
135 bus_space_read_2(sc->sc_iot, sc->sc_ioh, reg)
136 #define DL_WRITE_WORD(reg, val) \
137 bus_space_write_2(sc->sc_iot, sc->sc_ioh, reg, val)
138 #define DL_WRITE_BYTE(reg, val) \
139 bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, val)
140
141 /* Autoconfig handles: setup the controller to interrupt, */
142 /* then complete the housecleaning for full operation */
143
144 static int
145 dl_match (parent, cf, aux)
146 struct device * parent;
147 struct cfdata *cf;
148 void *aux;
149 {
150 struct uba_attach_args *ua = aux;
151
152 #ifdef DL_DEBUG
153 printf("Probing for dl at %lo ... ", (long)ua->ua_iaddr);
154 #endif
155
156 bus_space_write_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR, DL_XCSR_TXIE);
157 if (bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR) !=
158 (DL_XCSR_TXIE | DL_XCSR_TX_READY)) {
159 #ifdef DL_DEBUG
160 printf("failed (step 1; XCSR = %.4b)\n",
161 bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR),
162 DL_XCSR_BITS);
163 #endif
164 return 0;
165 }
166
167 /*
168 * We have to force an interrupt so the uba driver can work
169 * out where we are. Unfortunately, the only way to make a
170 * DL11 interrupt is to get it to send or receive a
171 * character. We'll send a NUL and hope it doesn't hurt
172 * anything.
173 */
174
175 bus_space_write_1(ua->ua_iot, ua->ua_ioh, DL_UBA_XBUFL, '\0');
176 #if 0 /* This test seems to fail 2/3 of the time :-( */
177 if (dladdr->dl_xcsr != (DL_XCSR_TXIE)) {
178 #ifdef DL_DEBUG
179 printf("failed (step 2; XCSR = %.4b)\n", dladdr->dl_xcsr,
180 DL_XCSR_BITS);
181 #endif
182 return 0;
183 }
184 #endif
185 DELAY(100000); /* delay 1/10 s for character to transmit */
186 if (bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR) !=
187 (DL_XCSR_TXIE | DL_XCSR_TX_READY)) {
188 #ifdef DL_DEBUG
189 printf("failed (step 3; XCSR = %.4b)\n",
190 bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR),
191 DL_XCSR_BITS);
192 #endif
193 return 0;
194 }
195
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 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 TX & RX interrupt handlers */
225 uba_intr_establish(ua->ua_icookie, ua->ua_cvec,
226 dlxint, sc, &sc->sc_tintrcnt);
227 uba_intr_establish(ua->ua_icookie, ua->ua_cvec - 4,
228 dlrint, sc, &sc->sc_rintrcnt);
229 evcnt_attach(&sc->sc_dev, "rintr", &sc->sc_rintrcnt);
230 evcnt_attach(&sc->sc_dev, "tintr", &sc->sc_tintrcnt);
231
232 printf("\n");
233 }
234
235 /* Receiver Interrupt Handler */
236
237 static void
238 dlrint(arg)
239 void *arg;
240 {
241 struct dl_softc *sc = arg;
242 struct tty *tp;
243 int cc;
244 unsigned c;
245
246 if (DL_READ_WORD(DL_UBA_RCSR) & DL_RCSR_RX_DONE) {
247 c = DL_READ_WORD(DL_UBA_RBUF);
248 cc = c & 0xFF;
249 tp = sc->sc_tty;
250
251 if (!(tp->t_state & TS_ISOPEN)) {
252 wakeup((caddr_t)&tp->t_rawq);
253 return;
254 }
255
256 if (c & DL_RBUF_OVERRUN_ERR)
257 /*
258 * XXX: This should really be logged somwhere
259 * else where we can afford the time.
260 */
261 log(LOG_WARNING, "%s: rx overrun\n",
262 sc->sc_dev.dv_xname);
263 if (c & DL_RBUF_FRAMING_ERR)
264 cc |= TTY_FE;
265 if (c & DL_RBUF_PARITY_ERR)
266 cc |= TTY_PE;
267
268 (*linesw[tp->t_line].l_rint)(cc, tp);
269 } else
270 log(LOG_WARNING, "%s: stray rx interrupt\n",
271 sc->sc_dev.dv_xname);
272 return;
273 }
274
275 /* Transmitter Interrupt Handler */
276
277 static void
278 dlxint(arg)
279 void *arg;
280 {
281 struct dl_softc *sc = arg;
282 struct tty *tp;
283
284 tp = sc->sc_tty;
285 tp->t_state &= ~(TS_BUSY | TS_FLUSH);
286 if (tp->t_line)
287 (*linesw[tp->t_line].l_start)(tp);
288 else
289 dlstart(tp);
290
291 return;
292 }
293
294 int
295 dlopen(dev, flag, mode, p)
296 dev_t dev;
297 int flag, mode;
298 struct proc *p;
299 {
300 struct tty *tp;
301 int unit;
302 struct dl_softc *sc;
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 ((*linesw[tp->t_line].l_open)(dev, tp));
333 }
334
335 /*ARGSUSED*/
336 int
337 dlclose(dev, flag, mode, p)
338 dev_t dev;
339 int flag, mode;
340 struct proc *p;
341 {
342 struct dl_softc *sc;
343 struct tty *tp;
344 int unit;
345
346 unit = minor(dev);
347 sc = dl_cd.cd_devs[unit];
348 tp = sc->sc_tty;
349
350 (*linesw[tp->t_line].l_close)(tp, flag);
351
352 /* Make sure a BREAK state is not left enabled. */
353 dlbrk(sc, 0);
354
355 return (ttyclose(tp));
356 }
357
358 int
359 dlread(dev, uio, flag)
360 dev_t dev;
361 struct uio *uio;
362 int flag;
363 {
364 struct tty *tp;
365 struct dl_softc *sc;
366 int unit;
367
368 unit = minor(dev);
369 sc = dl_cd.cd_devs[unit];
370 tp = sc->sc_tty;
371 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
372 }
373
374 int
375 dlwrite(dev, uio, flag)
376 dev_t dev;
377 struct uio *uio;
378 int flag;
379 {
380 struct tty *tp;
381 struct dl_softc *sc;
382 int unit;
383
384 unit = minor(dev);
385 sc = dl_cd.cd_devs[unit];
386 tp = sc->sc_tty;
387 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
388 }
389
390 int
391 dlioctl(dev, cmd, data, flag, p)
392 dev_t dev;
393 int cmd;
394 caddr_t data;
395 int flag;
396 struct proc *p;
397 {
398 struct dl_softc *sc;
399 struct tty *tp;
400 int unit;
401 int error;
402
403 unit = minor(dev);
404 sc = dl_cd.cd_devs[unit];
405 tp = sc->sc_tty;
406
407 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
408 if (error >= 0)
409 return (error);
410 error = ttioctl(tp, cmd, data, flag, p);
411 if (error >= 0)
412 return (error);
413
414 switch (cmd) {
415
416 case TIOCSBRK:
417 dlbrk(sc, 1);
418 break;
419
420 case TIOCCBRK:
421 dlbrk(sc, 0);
422 break;
423
424 case TIOCMGET:
425 /* No modem control, assume they're all low. */
426 *(int *)data = 0;
427 break;
428
429 default:
430 return (ENOTTY);
431 }
432 return (0);
433 }
434
435 struct tty *
436 dltty(dev)
437 dev_t dev;
438 {
439 struct dl_softc* sc;
440
441 sc = dl_cd.cd_devs[minor(dev)];
442 return sc->sc_tty;
443 }
444
445 void
446 dlstop(tp, flag)
447 struct tty *tp;
448 int flag;
449 {
450 struct dl_softc *sc;
451 int unit, s;
452
453 unit = minor(tp->t_dev);
454 sc = dl_cd.cd_devs[unit];
455
456 s = spltty();
457
458 if (tp->t_state & TS_BUSY)
459 if (!(tp->t_state & TS_TTSTOP))
460 tp->t_state |= TS_FLUSH;
461 splx(s);
462 }
463
464 static void
465 dlstart(tp)
466 struct tty *tp;
467 {
468 struct dl_softc *sc;
469 int unit;
470 int s;
471
472 unit = minor(tp->t_dev);
473 sc = dl_cd.cd_devs[unit];
474
475 s = spltty();
476 if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
477 goto out;
478 if (tp->t_outq.c_cc <= tp->t_lowat) {
479 if (tp->t_state & TS_ASLEEP) {
480 tp->t_state &= ~TS_ASLEEP;
481 wakeup((caddr_t)&tp->t_outq);
482 }
483 selwakeup(&tp->t_wsel);
484 }
485 if (tp->t_outq.c_cc == 0)
486 goto out;
487
488
489 if (DL_READ_WORD(DL_UBA_XCSR) & DL_XCSR_TX_READY) {
490 tp->t_state |= TS_BUSY;
491 DL_WRITE_BYTE(DL_UBA_XBUFL, getc(&tp->t_outq));
492 }
493 out:
494 splx(s);
495 return;
496 }
497
498 /*ARGSUSED*/
499 static int
500 dlparam(tp, t)
501 struct tty *tp;
502 struct termios *t;
503 {
504 /*
505 * All this kind of stuff (speed, character format, whatever)
506 * is set by jumpers on the card. Changing it is thus rather
507 * tricky for a mere device driver.
508 */
509 return 0;
510 }
511
512 static void
513 dlbrk(sc, state)
514 struct dl_softc *sc;
515 int state;
516 {
517 int s = spltty();
518
519 if (state) {
520 DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) |
521 DL_XCSR_TX_BREAK);
522 } else {
523 DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) &
524 ~DL_XCSR_TX_BREAK);
525 }
526 splx(s);
527 }
528