dl.c revision 1.11 1 /* $NetBSD: dl.c,v 1.11 2000/01/24 02:40:29 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 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((void *));
116 static void dlxint __P((void *));
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
195 /* What else do I need to do? */
196
197 return 1;
198
199 }
200
201 static void
202 dl_attach (parent, self, aux)
203 struct device *parent, *self;
204 void *aux;
205 {
206 struct dl_softc *sc = (void *)self;
207 register struct uba_attach_args *ua = aux;
208
209 sc->sc_iot = ua->ua_iot;
210 sc->sc_ioh = ua->ua_ioh;
211
212 /* Tidy up the device */
213
214 DL_WRITE_WORD(DL_UBA_RCSR, DL_RCSR_RXIE);
215 DL_WRITE_WORD(DL_UBA_XCSR, DL_XCSR_TXIE);
216
217 /* Initialize our softc structure. Should be done in open? */
218
219 sc->sc_tty = ttymalloc();
220 tty_attach(sc->sc_tty);
221
222 /* Now register the TX & RX interrupt handlers */
223 uba_intr_establish(ua->ua_icookie, ua->ua_cvec , dlxint, sc);
224 uba_intr_establish(ua->ua_icookie, ua->ua_cvec - 4, dlrint, sc);
225
226 printf("\n");
227 }
228
229 /* Receiver Interrupt Handler */
230
231 static void
232 dlrint(arg)
233 void *arg;
234 {
235 struct dl_softc *sc = arg;
236 register struct tty *tp;
237 register int cc;
238 register unsigned c;
239
240 if (DL_READ_WORD(DL_UBA_RCSR) & DL_RCSR_RX_DONE) {
241 c = DL_READ_WORD(DL_UBA_RBUF);
242 cc = c & 0xFF;
243 tp = sc->sc_tty;
244
245 if (!(tp->t_state & TS_ISOPEN)) {
246 wakeup((caddr_t)&tp->t_rawq);
247 return;
248 }
249
250 if (c & DL_RBUF_OVERRUN_ERR)
251 /*
252 * XXX: This should really be logged somwhere
253 * else where we can afford the time.
254 */
255 log(LOG_WARNING, "%s: rx overrun\n",
256 sc->sc_dev.dv_xname);
257 if (c & DL_RBUF_FRAMING_ERR)
258 cc |= TTY_FE;
259 if (c & DL_RBUF_PARITY_ERR)
260 cc |= TTY_PE;
261
262 (*linesw[tp->t_line].l_rint)(cc, tp);
263 } else
264 log(LOG_WARNING, "%s: stray rx interrupt\n",
265 sc->sc_dev.dv_xname);
266 return;
267 }
268
269 /* Transmitter Interrupt Handler */
270
271 static void
272 dlxint(arg)
273 void *arg;
274 {
275 struct dl_softc *sc = arg;
276 register struct tty *tp;
277
278 tp = sc->sc_tty;
279 tp->t_state &= ~(TS_BUSY | TS_FLUSH);
280 if (tp->t_line)
281 (*linesw[tp->t_line].l_start)(tp);
282 else
283 dlstart(tp);
284
285 return;
286 }
287
288 int
289 dlopen(dev, flag, mode, p)
290 dev_t dev;
291 int flag, mode;
292 struct proc *p;
293 {
294 register struct tty *tp;
295 register int unit;
296 struct dl_softc *sc;
297
298 unit = minor(dev);
299
300 if (unit >= dl_cd.cd_ndevs || dl_cd.cd_devs[unit] == NULL)
301 return ENXIO;
302 sc = dl_cd.cd_devs[unit];
303
304 tp = sc->sc_tty;
305 if (tp == NULL)
306 return ENODEV;
307 tp->t_oproc = dlstart;
308 tp->t_param = dlparam;
309 tp->t_dev = dev;
310
311 if (!(tp->t_state & TS_ISOPEN)) {
312 ttychars(tp);
313 tp->t_iflag = TTYDEF_IFLAG;
314 tp->t_oflag = TTYDEF_OFLAG;
315 /* No modem control, so set CLOCAL. */
316 tp->t_cflag = TTYDEF_CFLAG | CLOCAL;
317 tp->t_lflag = TTYDEF_LFLAG;
318 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
319
320 dlparam(tp, &tp->t_termios);
321 ttsetwater(tp);
322
323 } else if ((tp->t_state & TS_XCLUDE) && p->p_ucred->cr_uid != 0)
324 return EBUSY;
325
326 return ((*linesw[tp->t_line].l_open)(dev, tp));
327 }
328
329 /*ARGSUSED*/
330 int
331 dlclose(dev, flag, mode, p)
332 dev_t dev;
333 int flag, mode;
334 struct proc *p;
335 {
336 struct dl_softc *sc;
337 register struct tty *tp;
338 register int unit;
339
340 unit = minor(dev);
341 sc = dl_cd.cd_devs[unit];
342 tp = sc->sc_tty;
343
344 (*linesw[tp->t_line].l_close)(tp, flag);
345
346 /* Make sure a BREAK state is not left enabled. */
347 dlbrk(sc, 0);
348
349 return (ttyclose(tp));
350 }
351
352 int
353 dlread(dev, uio, flag)
354 dev_t dev;
355 struct uio *uio;
356 int flag;
357 {
358 register struct tty *tp;
359 struct dl_softc *sc;
360 register int unit;
361
362 unit = minor(dev);
363 sc = dl_cd.cd_devs[unit];
364 tp = sc->sc_tty;
365 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
366 }
367
368 int
369 dlwrite(dev, uio, flag)
370 dev_t dev;
371 struct uio *uio;
372 int flag;
373 {
374 register struct tty *tp;
375 struct dl_softc *sc;
376 register int unit;
377
378 unit = minor(dev);
379 sc = dl_cd.cd_devs[unit];
380 tp = sc->sc_tty;
381 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
382 }
383
384 int
385 dlioctl(dev, cmd, data, flag, p)
386 dev_t dev;
387 int cmd;
388 caddr_t data;
389 int flag;
390 struct proc *p;
391 {
392 struct dl_softc *sc;
393 register struct tty *tp;
394 register int unit;
395 int error;
396
397 unit = minor(dev);
398 sc = dl_cd.cd_devs[unit];
399 tp = sc->sc_tty;
400
401 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
402 if (error >= 0)
403 return (error);
404 error = ttioctl(tp, cmd, data, flag, p);
405 if (error >= 0)
406 return (error);
407
408 switch (cmd) {
409
410 case TIOCSBRK:
411 dlbrk(sc, 1);
412 break;
413
414 case TIOCCBRK:
415 dlbrk(sc, 0);
416 break;
417
418 case TIOCMGET:
419 /* No modem control, assume they're all low. */
420 *(int *)data = 0;
421 break;
422
423 default:
424 return (ENOTTY);
425 }
426 return (0);
427 }
428
429 struct tty *
430 dltty(dev)
431 dev_t dev;
432 {
433 register struct dl_softc* sc;
434
435 sc = dl_cd.cd_devs[minor(dev)];
436 return sc->sc_tty;
437 }
438
439 void
440 dlstop(tp, flag)
441 register struct tty *tp;
442 int flag;
443 {
444 register struct dl_softc *sc;
445 int unit, s;
446
447 unit = minor(tp->t_dev);
448 sc = dl_cd.cd_devs[unit];
449
450 s = spltty();
451
452 if (tp->t_state & TS_BUSY)
453 if (!(tp->t_state & TS_TTSTOP))
454 tp->t_state |= TS_FLUSH;
455 splx(s);
456 }
457
458 static void
459 dlstart(tp)
460 register struct tty *tp;
461 {
462 register struct dl_softc *sc;
463 register int unit;
464 int s;
465
466 unit = minor(tp->t_dev);
467 sc = dl_cd.cd_devs[unit];
468
469 s = spltty();
470 if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
471 goto out;
472 if (tp->t_outq.c_cc <= tp->t_lowat) {
473 if (tp->t_state & TS_ASLEEP) {
474 tp->t_state &= ~TS_ASLEEP;
475 wakeup((caddr_t)&tp->t_outq);
476 }
477 selwakeup(&tp->t_wsel);
478 }
479 if (tp->t_outq.c_cc == 0)
480 goto out;
481
482
483 if (DL_READ_WORD(DL_UBA_XCSR) & DL_XCSR_TX_READY) {
484 tp->t_state |= TS_BUSY;
485 DL_WRITE_BYTE(DL_UBA_XBUFL, getc(&tp->t_outq));
486 }
487 out:
488 splx(s);
489 return;
490 }
491
492 /*ARGSUSED*/
493 static int
494 dlparam(tp, t)
495 register struct tty *tp;
496 register struct termios *t;
497 {
498 /*
499 * All this kind of stuff (speed, character format, whatever)
500 * is set by jumpers on the card. Changing it is thus rather
501 * tricky for a mere device driver.
502 */
503 return 0;
504 }
505
506 static void
507 dlbrk(sc, state)
508 register struct dl_softc *sc;
509 int state;
510 {
511 int s = spltty();
512
513 if (state) {
514 DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) |
515 DL_XCSR_TX_BREAK);
516 } else {
517 DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) &
518 ~DL_XCSR_TX_BREAK);
519 }
520 splx(s);
521 }
522