xref: /netbsd-src/sys/dev/qbus/dl.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: dl.c,v 1.31 2005/12/11 12:23:29 christos 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) 1982, 1986, 1990, 1992, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  *
44  * This code is derived from software contributed to Berkeley by
45  * Ralph Campbell and Rick Macklem.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  */
71 
72 /*
73  * Copyright (c) 1996  Ken C. Wellsch.  All rights reserved.
74  *
75  * This code is derived from software contributed to Berkeley by
76  * Ralph Campbell and Rick Macklem.
77  *
78  * Redistribution and use in source and binary forms, with or without
79  * modification, are permitted provided that the following conditions
80  * are met:
81  * 1. Redistributions of source code must retain the above copyright
82  *    notice, this list of conditions and the following disclaimer.
83  * 2. Redistributions in binary form must reproduce the above copyright
84  *    notice, this list of conditions and the following disclaimer in the
85  *    documentation and/or other materials provided with the distribution.
86  * 3. All advertising materials mentioning features or use of this software
87  *    must display the following acknowledgement:
88  *	This product includes software developed by the University of
89  *	California, Berkeley and its contributors.
90  * 4. Neither the name of the University nor the names of its contributors
91  *    may be used to endorse or promote products derived from this software
92  *    without specific prior written permission.
93  *
94  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
95  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
98  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104  * SUCH DAMAGE.
105  */
106 
107 /*
108  * dl.c -- Device driver for the DL11 and DLV11 serial cards.
109  *
110  * OS-interface code derived from the dz and dca (hp300) drivers.
111  */
112 
113 #include <sys/cdefs.h>
114 __KERNEL_RCSID(0, "$NetBSD: dl.c,v 1.31 2005/12/11 12:23:29 christos Exp $");
115 
116 #include <sys/param.h>
117 #include <sys/systm.h>
118 #include <sys/ioctl.h>
119 #include <sys/tty.h>
120 #include <sys/proc.h>
121 #include <sys/buf.h>
122 #include <sys/conf.h>
123 #include <sys/file.h>
124 #include <sys/uio.h>
125 #include <sys/kernel.h>
126 #include <sys/syslog.h>
127 #include <sys/device.h>
128 
129 #include <machine/bus.h>
130 
131 #include <dev/qbus/ubavar.h>
132 
133 #include <dev/qbus/dlreg.h>
134 
135 #include "ioconf.h"
136 
137 struct dl_softc {
138 	struct device	sc_dev;
139 	struct evcnt	sc_rintrcnt;
140 	struct evcnt	sc_tintrcnt;
141 	bus_space_tag_t	sc_iot;
142 	bus_space_handle_t sc_ioh;
143 	struct tty	*sc_tty;
144 };
145 
146 static	int	dl_match (struct device *, struct cfdata *, void *);
147 static	void	dl_attach (struct device *, struct device *, void *);
148 static	void	dlrint (void *);
149 static	void	dlxint (void *);
150 static	void	dlstart (struct tty *);
151 static	int	dlparam (struct tty *, struct termios *);
152 static	void	dlbrk (struct dl_softc *, int);
153 
154 CFATTACH_DECL(dl, sizeof(struct dl_softc),
155     dl_match, dl_attach, NULL, NULL);
156 
157 dev_type_open(dlopen);
158 dev_type_close(dlclose);
159 dev_type_read(dlread);
160 dev_type_write(dlwrite);
161 dev_type_ioctl(dlioctl);
162 dev_type_stop(dlstop);
163 dev_type_tty(dltty);
164 dev_type_poll(dlpoll);
165 
166 const struct cdevsw dl_cdevsw = {
167 	dlopen, dlclose, dlread, dlwrite, dlioctl,
168 	dlstop, dltty, dlpoll, nommap, ttykqfilter, D_TTY
169 };
170 
171 #define	DL_READ_WORD(reg) \
172 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, reg)
173 #define	DL_WRITE_WORD(reg, val) \
174 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, reg, val)
175 #define	DL_WRITE_BYTE(reg, val) \
176 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, reg, val)
177 
178 /* Autoconfig handles: setup the controller to interrupt, */
179 /* then complete the housecleaning for full operation */
180 
181 static int
182 dl_match (struct device *parent, struct cfdata *cf, void *aux)
183 {
184 	struct uba_attach_args *ua = aux;
185 
186 #ifdef DL_DEBUG
187 	printf("Probing for dl at %lo ... ", (long)ua->ua_iaddr);
188 #endif
189 
190 	bus_space_write_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR, DL_XCSR_TXIE);
191 	if (bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR) !=
192 	    (DL_XCSR_TXIE | DL_XCSR_TX_READY)) {
193 #ifdef DL_DEBUG
194 		printf("failed (step 1; XCSR = %.4b)\n",
195 		    bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR),
196 		    DL_XCSR_BITS);
197 #endif
198 		return 0;
199 	}
200 
201 	/*
202 	 * We have to force an interrupt so the uba driver can work
203 	 * out where we are.  Unfortunately, the only way to make a
204 	 * DL11 interrupt is to get it to send or receive a
205 	 * character.  We'll send a NUL and hope it doesn't hurt
206 	 * anything.
207 	 */
208 
209 	bus_space_write_1(ua->ua_iot, ua->ua_ioh, DL_UBA_XBUFL, '\0');
210 #if 0 /* This test seems to fail 2/3 of the time :-( */
211 	if (dladdr->dl_xcsr != (DL_XCSR_TXIE)) {
212 #ifdef DL_DEBUG
213 		printf("failed (step 2; XCSR = %.4b)\n", dladdr->dl_xcsr,
214 		    DL_XCSR_BITS);
215 #endif
216 		return 0;
217 	}
218 #endif
219 	DELAY(100000); /* delay 1/10 s for character to transmit */
220 	if (bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR) !=
221 	    (DL_XCSR_TXIE | DL_XCSR_TX_READY)) {
222 #ifdef DL_DEBUG
223 		printf("failed (step 3; XCSR = %.4b)\n",
224 		    bus_space_read_2(ua->ua_iot, ua->ua_ioh, DL_UBA_XCSR),
225 		    DL_XCSR_BITS);
226 #endif
227 		return 0;
228 	}
229 
230 
231 	/* What else do I need to do? */
232 
233 	return 1;
234 
235 }
236 
237 static void
238 dl_attach (struct device *parent, struct device *self, void *aux)
239 {
240 	struct dl_softc *sc = (void *)self;
241 	struct uba_attach_args *ua = aux;
242 
243 	sc->sc_iot = ua->ua_iot;
244 	sc->sc_ioh = ua->ua_ioh;
245 
246 	/* Tidy up the device */
247 
248 	DL_WRITE_WORD(DL_UBA_RCSR, DL_RCSR_RXIE);
249 	DL_WRITE_WORD(DL_UBA_XCSR, DL_XCSR_TXIE);
250 
251 	/* Initialize our softc structure. Should be done in open? */
252 
253 	sc->sc_tty = ttymalloc();
254 	tty_attach(sc->sc_tty);
255 
256 	/* Now register the TX & RX interrupt handlers */
257 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec,
258 		dlxint, sc, &sc->sc_tintrcnt);
259 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec - 4,
260 		dlrint, sc, &sc->sc_rintrcnt);
261 	evcnt_attach_dynamic(&sc->sc_rintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
262 		sc->sc_dev.dv_xname, "rintr");
263 	evcnt_attach_dynamic(&sc->sc_tintrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
264 		sc->sc_dev.dv_xname, "tintr");
265 
266 	printf("\n");
267 }
268 
269 /* Receiver Interrupt Handler */
270 
271 static void
272 dlrint(void *arg)
273 {
274 	struct dl_softc *sc = arg;
275 
276 	if (DL_READ_WORD(DL_UBA_RCSR) & DL_RCSR_RX_DONE) {
277 		struct tty *tp = sc->sc_tty;
278 		unsigned c;
279 		int cc;
280 
281 		c = DL_READ_WORD(DL_UBA_RBUF);
282 		cc = c & 0xFF;
283 
284 		if (!(tp->t_state & TS_ISOPEN)) {
285 			wakeup((caddr_t)&tp->t_rawq);
286 			return;
287 		}
288 
289 		if (c & DL_RBUF_OVERRUN_ERR) {
290 			/*
291 			 * XXX: This should really be logged somwhere
292 			 * else where we can afford the time.
293 			 */
294 			log(LOG_WARNING, "%s: rx overrun\n",
295 			    sc->sc_dev.dv_xname);
296 		}
297 		if (c & DL_RBUF_FRAMING_ERR)
298 			cc |= TTY_FE;
299 		if (c & DL_RBUF_PARITY_ERR)
300 			cc |= TTY_PE;
301 
302 		(*tp->t_linesw->l_rint)(cc, tp);
303 #if defined(DIAGNOSTIC)
304 	} else {
305 		log(LOG_WARNING, "%s: stray rx interrupt\n",
306 		    sc->sc_dev.dv_xname);
307 #endif
308 	}
309 }
310 
311 /* Transmitter Interrupt Handler */
312 
313 static void
314 dlxint(void *arg)
315 {
316 	struct dl_softc *sc = arg;
317 	struct tty *tp = sc->sc_tty;
318 
319 	tp->t_state &= ~(TS_BUSY | TS_FLUSH);
320 	(*tp->t_linesw->l_start)(tp);
321 
322 	return;
323 }
324 
325 int
326 dlopen(dev_t dev, int flag, int mode, struct lwp *l)
327 {
328 	struct proc *p = l->l_proc;
329 	struct tty *tp;
330 	struct dl_softc *sc;
331 	int unit;
332 
333 	unit = minor(dev);
334 
335 	if (unit >= dl_cd.cd_ndevs || dl_cd.cd_devs[unit] == NULL)
336 		return ENXIO;
337 	sc = dl_cd.cd_devs[unit];
338 
339 	tp = sc->sc_tty;
340 	if (tp == NULL)
341 		return ENODEV;
342 	tp->t_oproc = dlstart;
343 	tp->t_param = dlparam;
344 	tp->t_dev = dev;
345 
346 	if (!(tp->t_state & TS_ISOPEN)) {
347 		ttychars(tp);
348 		tp->t_iflag = TTYDEF_IFLAG;
349 		tp->t_oflag = TTYDEF_OFLAG;
350 		/* No modem control, so set CLOCAL. */
351 		tp->t_cflag = TTYDEF_CFLAG | CLOCAL;
352 		tp->t_lflag = TTYDEF_LFLAG;
353 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
354 
355 		dlparam(tp, &tp->t_termios);
356 		ttsetwater(tp);
357 
358 	} else if ((tp->t_state & TS_XCLUDE) &&
359 		   suser(p->p_ucred, &p->p_acflag) != 0)
360 		return EBUSY;
361 
362 	return ((*tp->t_linesw->l_open)(dev, tp));
363 }
364 
365 /*ARGSUSED*/
366 int
367 dlclose(dev_t dev, int flag, int mode, struct lwp *l)
368 {
369 	struct dl_softc *sc = dl_cd.cd_devs[minor(dev)];
370 	struct tty *tp = sc->sc_tty;
371 
372 	(*tp->t_linesw->l_close)(tp, flag);
373 
374 	/* Make sure a BREAK state is not left enabled. */
375 	dlbrk(sc, 0);
376 
377 	return (ttyclose(tp));
378 }
379 
380 int
381 dlread(dev_t dev, struct uio *uio, int flag)
382 {
383 	struct dl_softc *sc = dl_cd.cd_devs[minor(dev)];
384 	struct tty *tp = sc->sc_tty;
385 
386 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
387 }
388 
389 int
390 dlwrite(dev_t dev, struct uio *uio, int flag)
391 {
392 	struct dl_softc *sc = dl_cd.cd_devs[minor(dev)];
393 	struct tty *tp = sc->sc_tty;
394 
395 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
396 }
397 
398 int
399 dlpoll(dev_t dev, int events, struct lwp *l)
400 {
401 	struct dl_softc *sc = dl_cd.cd_devs[minor(dev)];
402 	struct tty *tp = sc->sc_tty;
403 
404 	return ((*tp->t_linesw->l_poll)(tp, events, l));
405 }
406 
407 int
408 dlioctl(dev_t dev, unsigned long cmd, caddr_t data, int flag, struct lwp *l)
409 {
410 	struct dl_softc *sc = dl_cd.cd_devs[minor(dev)];
411 	struct tty *tp = sc->sc_tty;
412 	int error;
413 
414 
415 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
416 	if (error != EPASSTHROUGH)
417 		return (error);
418 
419 	error = ttioctl(tp, cmd, data, flag, l);
420 	if (error != EPASSTHROUGH)
421 		return (error);
422 
423 	switch (cmd) {
424 
425 	case TIOCSBRK:
426 		dlbrk(sc, 1);
427 		break;
428 
429 	case TIOCCBRK:
430 		dlbrk(sc, 0);
431 		break;
432 
433 	case TIOCMGET:
434 		/* No modem control, assume they're all low. */
435 		*(int *)data = 0;
436 		break;
437 
438 	default:
439 		return (EPASSTHROUGH);
440 	}
441 	return (0);
442 }
443 
444 struct tty *
445 dltty(dev_t dev)
446 {
447 	struct dl_softc *sc = dl_cd.cd_devs[minor(dev)];
448 
449 	return sc->sc_tty;
450 }
451 
452 void
453 dlstop(struct tty *tp, int flag)
454 {
455 	int s = spltty();
456 
457 	if ((tp->t_state & (TS_BUSY|TS_TTSTOP)) == TS_BUSY)
458 		tp->t_state |= TS_FLUSH;
459 	splx(s);
460 }
461 
462 static void
463 dlstart(struct tty *tp)
464 {
465 	struct dl_softc *sc = dl_cd.cd_devs[minor(tp->t_dev)];
466 	int s = spltty();
467 
468 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
469 		goto out;
470 	if (tp->t_outq.c_cc <= tp->t_lowat) {
471 		if (tp->t_state & TS_ASLEEP) {
472 			tp->t_state &= ~TS_ASLEEP;
473 			wakeup((caddr_t)&tp->t_outq);
474 		}
475 		selwakeup(&tp->t_wsel);
476 	}
477 	if (tp->t_outq.c_cc == 0)
478 		goto out;
479 
480 
481 	if (DL_READ_WORD(DL_UBA_XCSR) & DL_XCSR_TX_READY) {
482 		tp->t_state |= TS_BUSY;
483 		DL_WRITE_BYTE(DL_UBA_XBUFL, getc(&tp->t_outq));
484 	}
485 out:
486 	splx(s);
487 	return;
488 }
489 
490 /*ARGSUSED*/
491 static int
492 dlparam(struct tty *tp, struct termios *t)
493 {
494 	/*
495 	 * All this kind of stuff (speed, character format, whatever)
496 	 * is set by jumpers on the card.  Changing it is thus rather
497 	 * tricky for a mere device driver.
498 	 */
499 	return 0;
500 }
501 
502 static void
503 dlbrk(struct dl_softc *sc, int state)
504 {
505 	int s = spltty();
506 
507 	if (state) {
508 		DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) |
509 		    DL_XCSR_TX_BREAK);
510 	} else {
511 		DL_WRITE_WORD(DL_UBA_XCSR, DL_READ_WORD(DL_UBA_XCSR) &
512 		    ~DL_XCSR_TX_BREAK);
513 	}
514 	splx(s);
515 }
516