siotty.c revision 1.17 1 /* $NetBSD: siotty.c,v 1.17 2006/05/14 21:55:38 elad Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tohru Nishimura.
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 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
40
41 __KERNEL_RCSID(0, "$NetBSD: siotty.c,v 1.17 2006/05/14 21:55:38 elad Exp $");
42
43 #include "opt_ddb.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 #include <sys/conf.h>
49 #include <sys/ioctl.h>
50 #include <sys/proc.h>
51 #include <sys/user.h>
52 #include <sys/tty.h>
53 #include <sys/uio.h>
54 #include <sys/callout.h>
55 #include <sys/fcntl.h>
56 #include <dev/cons.h>
57 #include <sys/kauth.h>
58
59 #include <machine/cpu.h>
60
61 #include <luna68k/dev/sioreg.h>
62 #include <luna68k/dev/siovar.h>
63
64 #define TIOCM_BREAK 01000 /* non standard use */
65
66 static const u_int8_t ch0_regs[6] = {
67 WR0_RSTINT, /* reset E/S interrupt */
68 WR1_RXALLS | WR1_TXENBL, /* Rx per char, Tx */
69 0, /* */
70 WR3_RX8BIT | WR3_RXENBL, /* Rx */
71 WR4_BAUD96 | WR4_STOP1, /* Tx/Rx */
72 WR5_TX8BIT | WR5_TXENBL | WR5_DTR | WR5_RTS, /* Tx */
73 };
74
75 static const struct speedtab siospeedtab[] = {
76 { 2400, WR4_BAUD24, },
77 { 4800, WR4_BAUD48, },
78 { 9600, WR4_BAUD96, },
79 { -1, 0, },
80 };
81
82 struct siotty_softc {
83 struct device sc_dev;
84 struct tty *sc_tty;
85 struct sioreg *sc_ctl;
86 u_int sc_flags;
87 u_int8_t sc_wr[6];
88 };
89
90 #include "siotty.h"
91 static void siostart __P((struct tty *));
92 static int sioparam __P((struct tty *, struct termios *));
93 static void siottyintr __P((int));
94 static int siomctl __P((struct siotty_softc *, int, int));
95
96 static int siotty_match __P((struct device *, struct cfdata *, void *));
97 static void siotty_attach __P((struct device *, struct device *, void *));
98
99 CFATTACH_DECL(siotty, sizeof(struct siotty_softc),
100 siotty_match, siotty_attach, NULL, NULL);
101 extern struct cfdriver siotty_cd;
102
103 dev_type_open(sioopen);
104 dev_type_close(sioclose);
105 dev_type_read(sioread);
106 dev_type_write(siowrite);
107 dev_type_ioctl(sioioctl);
108 dev_type_stop(siostop);
109 dev_type_tty(siotty);
110 dev_type_poll(siopoll);
111
112 const struct cdevsw siotty_cdevsw = {
113 sioopen, sioclose, sioread, siowrite, sioioctl,
114 siostop, siotty, siopoll, nommap, ttykqfilter, D_TTY
115 };
116
117 static int
118 siotty_match(parent, cf, aux)
119 struct device *parent;
120 struct cfdata *cf;
121 void *aux;
122 {
123 struct sio_attach_args *args = aux;
124
125 if (args->channel != 0) /* XXX allow tty on Ch.B XXX */
126 return 0;
127 return 1;
128 }
129
130 static void
131 siotty_attach(parent, self, aux)
132 struct device *parent, *self;
133 void *aux;
134 {
135 struct sio_softc *scp = (void *)parent;
136 struct siotty_softc *sc = (void *)self;
137 struct sio_attach_args *args = aux;
138
139 sc->sc_ctl = (struct sioreg *)scp->scp_ctl + args->channel;
140 bcopy(ch0_regs, sc->sc_wr, sizeof(ch0_regs));
141 scp->scp_intr[args->channel] = siottyintr;
142
143 if (args->hwflags == 1) {
144 printf(" (console)");
145 sc->sc_flags = TIOCFLAG_SOFTCAR;
146 }
147 else {
148 setsioreg(sc->sc_ctl, WR0, WR0_CHANRST);
149 setsioreg(sc->sc_ctl, WR2A, WR2_VEC86 | WR2_INTR_1);
150 setsioreg(sc->sc_ctl, WR2B, 0);
151 setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
152 setsioreg(sc->sc_ctl, WR4, sc->sc_wr[WR4]);
153 setsioreg(sc->sc_ctl, WR3, sc->sc_wr[WR3]);
154 setsioreg(sc->sc_ctl, WR5, sc->sc_wr[WR5]);
155 setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
156 }
157 setsioreg(sc->sc_ctl, WR1, sc->sc_wr[WR1]); /* now interrupt driven */
158
159 printf("\n");
160 }
161
162 /*-------------------- low level routine --------------------*/
163
164 static void
165 siottyintr(chan)
166 int chan;
167 {
168 struct siotty_softc *sc;
169 struct sioreg *sio;
170 struct tty *tp;
171 unsigned int code;
172 int rr;
173
174 if (chan >= siotty_cd.cd_ndevs)
175 return;
176 sc = siotty_cd.cd_devs[chan];
177 tp = sc->sc_tty;
178 sio = sc->sc_ctl;
179 rr = getsiocsr(sio);
180 if (rr & RR_RXRDY) {
181 do {
182 code = sio->sio_data;
183 if (rr & (RR_FRAMING | RR_OVERRUN | RR_PARITY)) {
184 sio->sio_cmd = WR0_ERRRST;
185 if (sio->sio_stat & RR_FRAMING)
186 code |= TTY_FE;
187 else if (sio->sio_stat & RR_PARITY)
188 code |= TTY_PE;
189 }
190 if (tp == NULL || (tp->t_state & TS_ISOPEN) == 0)
191 continue;
192 #if 0 && defined(DDB) /* ?!?! fails to resume ?!?! */
193 if ((rr & RR_BREAK) && tp->t_dev == cn_tab->cn_dev) {
194 cpu_Debugger();
195 return;
196 }
197 #endif
198 (*tp->t_linesw->l_rint)(code, tp);
199 } while ((rr = getsiocsr(sio)) & RR_RXRDY);
200 }
201 if (rr & RR_TXRDY) {
202 sio->sio_cmd = WR0_RSTPEND;
203 if (tp != NULL) {
204 tp->t_state &= ~(TS_BUSY|TS_FLUSH);
205 (*tp->t_linesw->l_start)(tp);
206 }
207 }
208 }
209
210 static void
211 siostart(tp)
212 struct tty *tp;
213 {
214 struct siotty_softc *sc = siotty_cd.cd_devs[minor(tp->t_dev)];
215 int s, c;
216
217 s = spltty();
218 if (tp->t_state & (TS_BUSY|TS_TIMEOUT|TS_TTSTOP))
219 goto out;
220 if (tp->t_outq.c_cc <= tp->t_lowat) {
221 if (tp->t_state & TS_ASLEEP) {
222 tp->t_state &= ~TS_ASLEEP;
223 wakeup((caddr_t)&tp->t_outq);
224 }
225 selwakeup(&tp->t_wsel);
226 }
227 if (tp->t_outq.c_cc == 0)
228 goto out;
229
230 tp->t_state |= TS_BUSY;
231 while (getsiocsr(sc->sc_ctl) & RR_TXRDY) {
232 if ((c = getc(&tp->t_outq)) == -1)
233 break;
234 sc->sc_ctl->sio_data = c;
235 }
236 out:
237 splx(s);
238 }
239
240 void
241 siostop(tp, flag)
242 struct tty *tp;
243 int flag;
244 {
245 int s;
246
247 s = spltty();
248 if (TS_BUSY == (tp->t_state & (TS_BUSY|TS_TTSTOP))) {
249 /*
250 * Device is transmitting; must stop it.
251 */
252 tp->t_state |= TS_FLUSH;
253 }
254 splx(s);
255 }
256
257 static int
258 sioparam(tp, t)
259 struct tty *tp;
260 struct termios *t;
261 {
262 struct siotty_softc *sc = siotty_cd.cd_devs[minor(tp->t_dev)];
263 int wr4, s;
264
265 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
266 return EINVAL;
267 wr4 = ttspeedtab(t->c_ospeed, siospeedtab);
268 if (wr4 < 0)
269 return EINVAL;
270
271 if (sc->sc_flags & TIOCFLAG_SOFTCAR) {
272 t->c_cflag |= CLOCAL;
273 t->c_cflag &= ~HUPCL;
274 }
275 if (sc->sc_flags & TIOCFLAG_CLOCAL)
276 t->c_cflag |= CLOCAL;
277
278 /*
279 * If there were no changes, don't do anything. This avoids dropping
280 * input and improves performance when all we did was frob things like
281 * VMIN and VTIME.
282 */
283 if (tp->t_ospeed == t->c_ospeed && tp->t_cflag == t->c_cflag)
284 return 0;
285
286 tp->t_ispeed = t->c_ispeed;
287 tp->t_ospeed = t->c_ospeed;
288 tp->t_cflag = t->c_cflag;
289
290 sc->sc_wr[WR3] &= 0x3f;
291 sc->sc_wr[WR5] &= 0x9f;
292 switch (tp->t_cflag & CSIZE) {
293 case CS7:
294 sc->sc_wr[WR3] |= WR3_RX7BIT; sc->sc_wr[WR5] |= WR5_TX7BIT;
295 break;
296 case CS8:
297 sc->sc_wr[WR3] |= WR3_RX8BIT; sc->sc_wr[WR5] |= WR5_TX8BIT;
298 break;
299 }
300 if (tp->t_cflag & PARENB) {
301 wr4 |= WR4_PARENAB;
302 if ((tp->t_cflag & PARODD) == 0)
303 wr4 |= WR4_EPARITY;
304 }
305 wr4 |= (tp->t_cflag & CSTOPB) ? WR4_STOP2 : WR4_STOP1;
306 sc->sc_wr[WR4] = wr4;
307
308 s = spltty();
309 setsioreg(sc->sc_ctl, WR4, sc->sc_wr[WR4]);
310 setsioreg(sc->sc_ctl, WR3, sc->sc_wr[WR3]);
311 setsioreg(sc->sc_ctl, WR5, sc->sc_wr[WR5]);
312 splx(s);
313
314 return 0;
315 }
316
317 static int
318 siomctl(sc, control, op)
319 struct siotty_softc *sc;
320 int control, op;
321 {
322 int val, s, wr5, rr;
323
324 val = 0;
325 if (control & TIOCM_BREAK)
326 val |= WR5_BREAK;
327 if (control & TIOCM_DTR)
328 val |= WR5_DTR;
329 if (control & TIOCM_RTS)
330 val |= WR5_RTS;
331 s = spltty();
332 wr5 = sc->sc_wr[WR5];
333 switch (op) {
334 case DMSET:
335 wr5 &= ~(WR5_BREAK|WR5_DTR|WR5_RTS);
336 /* FALLTHRU */
337 case DMBIS:
338 wr5 |= val;
339 break;
340 case DMBIC:
341 wr5 &= ~val;
342 break;
343 case DMGET:
344 val = 0;
345 rr = getsiocsr(sc->sc_ctl);
346 if (wr5 & WR5_DTR)
347 val |= TIOCM_DTR;
348 if (wr5 & WR5_RTS)
349 val |= TIOCM_RTS;
350 if (rr & RR_CTS)
351 val |= TIOCM_CTS;
352 if (rr & RR_DCD)
353 val |= TIOCM_CD;
354 goto done;
355 }
356 sc->sc_wr[WR5] = wr5;
357 setsioreg(sc->sc_ctl, WR5, wr5);
358 val = 0;
359 done:
360 splx(s);
361 return val;
362 }
363
364 /*-------------------- cdevsw[] interface --------------------*/
365
366 int
367 sioopen(dev, flag, mode, l)
368 dev_t dev;
369 int flag, mode;
370 struct lwp *l;
371 {
372 struct siotty_softc *sc;
373 struct tty *tp;
374 int error;
375
376 if ((sc = siotty_cd.cd_devs[minor(dev)]) == NULL)
377 return ENXIO;
378 if ((tp = sc->sc_tty) == NULL) {
379 tp = sc->sc_tty = ttymalloc();
380 tty_attach(tp);
381 }
382 else if ((tp->t_state & TS_ISOPEN) && (tp->t_state & TS_XCLUDE)
383 && kauth_authorize_generic(l->l_proc->p_cred, KAUTH_GENERIC_ISSUSER, &l->l_proc->p_acflag) != 0)
384 return EBUSY;
385
386 tp->t_oproc = siostart;
387 tp->t_param = sioparam;
388 tp->t_hwiflow = NULL /* XXX siohwiflow XXX */;
389 tp->t_dev = dev;
390 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
391 struct termios t;
392
393 t.c_ispeed = t.c_ospeed = TTYDEF_SPEED;
394 t.c_cflag = TTYDEF_CFLAG;
395 tp->t_ospeed = 0; /* force register update */
396 (void)sioparam(tp, &t);
397 tp->t_iflag = TTYDEF_IFLAG;
398 tp->t_oflag = TTYDEF_OFLAG;
399 tp->t_lflag = TTYDEF_LFLAG;
400 ttychars(tp);
401 ttsetwater(tp);
402 /* raise RTS and DTR here; but, DTR lead is not wired */
403 /* then check DCD condition; but, DCD lead is not wired */
404 tp->t_state |= TS_CARR_ON; /* assume detected all the time */
405 #if 0
406 if ((sc->sc_flags & TIOCFLAG_SOFTCAR)
407 || (tp->t_cflag & MDMBUF)
408 || (getsiocsr(sc->sc_ctl) & RR_DCD))
409 tp->t_state |= TS_CARR_ON;
410 else
411 tp->t_state &= ~TS_CARR_ON;
412 #endif
413 }
414
415 error = ttyopen(tp, 0, (flag & O_NONBLOCK));
416 if (error > 0)
417 return error;
418 return (*tp->t_linesw->l_open)(dev, tp);
419 }
420
421 int
422 sioclose(dev, flag, mode, l)
423 dev_t dev;
424 int flag, mode;
425 struct lwp *l;
426 {
427 struct siotty_softc *sc = siotty_cd.cd_devs[minor(dev)];
428 struct tty *tp = sc->sc_tty;
429 int s;
430
431 (*tp->t_linesw->l_close)(tp, flag);
432
433 s = spltty();
434 siomctl(sc, TIOCM_BREAK, DMBIC);
435 #if 0 /* because unable to feed DTR signal */
436 if ((tp->t_cflag & HUPCL)
437 || tp->t_wopen || (tp->t_state & TS_ISOPEN) == 0) {
438 siomctl(sc, TIOCM_DTR, DMBIC);
439 /* Yield CPU time to others for 1 second, then ... */
440 siomctl(sc, TIOCM_DTR, DMBIS);
441 }
442 #endif
443 splx(s);
444 return ttyclose(tp);
445 }
446
447 int
448 sioread(dev, uio, flag)
449 dev_t dev;
450 struct uio *uio;
451 int flag;
452 {
453 struct siotty_softc *sc = siotty_cd.cd_devs[minor(dev)];
454 struct tty *tp = sc->sc_tty;
455
456 return (*tp->t_linesw->l_read)(tp, uio, flag);
457 }
458
459 int
460 siowrite(dev, uio, flag)
461 dev_t dev;
462 struct uio *uio;
463 int flag;
464 {
465 struct siotty_softc *sc = siotty_cd.cd_devs[minor(dev)];
466 struct tty *tp = sc->sc_tty;
467
468 return (*tp->t_linesw->l_write)(tp, uio, flag);
469 }
470
471 int
472 siopoll(dev, events, l)
473 dev_t dev;
474 int events;
475 struct lwp *l;
476 {
477 struct siotty_softc *sc = siotty_cd.cd_devs[minor(dev)];
478 struct tty *tp = sc->sc_tty;
479
480 return ((*tp->t_linesw->l_poll)(tp, events, l));
481 }
482
483 int
484 sioioctl(dev, cmd, data, flag, l)
485 dev_t dev;
486 u_long cmd;
487 caddr_t data;
488 int flag;
489 struct lwp *l;
490 {
491 struct siotty_softc *sc = siotty_cd.cd_devs[minor(dev)];
492 struct tty *tp = sc->sc_tty;
493 int error;
494
495 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
496 if (error != EPASSTHROUGH)
497 return error;
498
499 error = ttioctl(tp, cmd, data, flag, l);
500 if (error != EPASSTHROUGH)
501 return error;
502
503 /* the last resort for TIOC ioctl tranversing */
504 switch (cmd) {
505 case TIOCSBRK: /* Set the hardware into BREAK condition */
506 siomctl(sc, TIOCM_BREAK, DMBIS);
507 break;
508 case TIOCCBRK: /* Clear the hardware BREAK condition */
509 siomctl(sc, TIOCM_BREAK, DMBIC);
510 break;
511 case TIOCSDTR: /* Assert DTR signal */
512 siomctl(sc, TIOCM_DTR|TIOCM_RTS, DMBIS);
513 break;
514 case TIOCCDTR: /* Clear DTR signal */
515 siomctl(sc, TIOCM_DTR|TIOCM_RTS, DMBIC);
516 break;
517 case TIOCMSET: /* Set modem state replacing current one */
518 siomctl(sc, *(int *)data, DMSET);
519 break;
520 case TIOCMGET: /* Return current modem state */
521 *(int *)data = siomctl(sc, 0, DMGET);
522 break;
523 case TIOCMBIS: /* Set individual bits of modem state */
524 siomctl(sc, *(int *)data, DMBIS);
525 break;
526 case TIOCMBIC: /* Clear individual bits of modem state */
527 siomctl(sc, *(int *)data, DMBIC);
528 break;
529 case TIOCSFLAGS: /* Instruct how serial port behaves */
530 sc->sc_flags = *(int *)data;
531 break;
532 case TIOCGFLAGS: /* Return current serial port state */
533 *(int *)data = sc->sc_flags;
534 break;
535 default:
536 return EPASSTHROUGH;
537 }
538 return 0;
539 }
540
541 /* ARSGUSED */
542 struct tty *
543 siotty(dev)
544 dev_t dev;
545 {
546 struct siotty_softc *sc = siotty_cd.cd_devs[minor(dev)];
547
548 return sc->sc_tty;
549 }
550
551 /*-------------------- miscelleneous routine --------------------*/
552
553 /* EXPORT */ void
554 setsioreg(sio, regno, val)
555 struct sioreg *sio;
556 int regno, val;
557 {
558 if (regno != 0)
559 sio->sio_cmd = regno; /* DELAY(); */
560 sio->sio_cmd = val; /* DELAY(); */
561 }
562
563 /* EXPORT */ int
564 getsiocsr(sio)
565 struct sioreg *sio;
566 {
567 int val;
568
569 val = sio->sio_stat << 8; /* DELAY(); */
570 sio->sio_cmd = 1; /* DELAY(); */
571 val |= sio->sio_stat; /* DELAY(); */
572 return val;
573 }
574
575 /*--------------------- console interface ----------------------*/
576
577 void syscnattach __P((int));
578 int syscngetc __P((dev_t));
579 void syscnputc __P((dev_t, int));
580
581 struct consdev syscons = {
582 NULL,
583 NULL,
584 syscngetc,
585 syscnputc,
586 nullcnpollc,
587 NULL,
588 NULL,
589 NULL,
590 NODEV,
591 CN_REMOTE,
592 };
593
594 /* EXPORT */ void
595 syscnattach(channel)
596 int channel;
597 {
598 /*
599 * Channel A is immediately initialized with 9600N1 right after cold
600 * boot/reset/poweron. ROM monitor emits one line message on CH.A.
601 */
602 struct sioreg *sio;
603 sio = (struct sioreg *)0x51000000 + channel;
604
605 syscons.cn_dev = makedev(cdevsw_lookup_major(&siotty_cdevsw),
606 channel);
607 cn_tab = &syscons;
608
609 setsioreg(sio, WR0, WR0_CHANRST);
610 setsioreg(sio, WR2A, WR2_VEC86 | WR2_INTR_1);
611 setsioreg(sio, WR2B, 0);
612 setsioreg(sio, WR0, ch0_regs[WR0]);
613 setsioreg(sio, WR4, ch0_regs[WR4]);
614 setsioreg(sio, WR3, ch0_regs[WR3]);
615 setsioreg(sio, WR5, ch0_regs[WR5]);
616 setsioreg(sio, WR0, ch0_regs[WR0]);
617 }
618
619 /* EXPORT */ int
620 syscngetc(dev)
621 dev_t dev;
622 {
623 struct sioreg *sio;
624 int s, c;
625
626 sio = (struct sioreg *)0x51000000 + ((int)dev & 0x1);
627 s = splhigh();
628 while ((getsiocsr(sio) & RR_RXRDY) == 0)
629 ;
630 c = sio->sio_data;
631 splx(s);
632
633 return c;
634 }
635
636 /* EXPORT */ void
637 syscnputc(dev, c)
638 dev_t dev;
639 int c;
640 {
641 struct sioreg *sio;
642 int s;
643
644 sio = (struct sioreg *)0x51000000 + ((int)dev & 0x1);
645 s = splhigh();
646 while ((getsiocsr(sio) & RR_TXRDY) == 0)
647 ;
648 sio->sio_cmd = WR0_RSTPEND;
649 sio->sio_data = c;
650 splx(s);
651 }
652