siotty.c revision 1.21 1 /* $NetBSD: siotty.c,v 1.21 2007/11/19 18:51:40 ad 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.21 2007/11/19 18:51:40 ad 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 (!ttypull(tp))
221 goto out;
222 tp->t_state |= TS_BUSY;
223 while (getsiocsr(sc->sc_ctl) & RR_TXRDY) {
224 if ((c = getc(&tp->t_outq)) == -1)
225 break;
226 sc->sc_ctl->sio_data = c;
227 }
228 out:
229 splx(s);
230 }
231
232 void
233 siostop(tp, flag)
234 struct tty *tp;
235 int flag;
236 {
237 int s;
238
239 s = spltty();
240 if (TS_BUSY == (tp->t_state & (TS_BUSY|TS_TTSTOP))) {
241 /*
242 * Device is transmitting; must stop it.
243 */
244 tp->t_state |= TS_FLUSH;
245 }
246 splx(s);
247 }
248
249 static int
250 sioparam(tp, t)
251 struct tty *tp;
252 struct termios *t;
253 {
254 struct siotty_softc *sc = siotty_cd.cd_devs[minor(tp->t_dev)];
255 int wr4, s;
256
257 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
258 return EINVAL;
259 wr4 = ttspeedtab(t->c_ospeed, siospeedtab);
260 if (wr4 < 0)
261 return EINVAL;
262
263 if (sc->sc_flags & TIOCFLAG_SOFTCAR) {
264 t->c_cflag |= CLOCAL;
265 t->c_cflag &= ~HUPCL;
266 }
267 if (sc->sc_flags & TIOCFLAG_CLOCAL)
268 t->c_cflag |= CLOCAL;
269
270 /*
271 * If there were no changes, don't do anything. This avoids dropping
272 * input and improves performance when all we did was frob things like
273 * VMIN and VTIME.
274 */
275 if (tp->t_ospeed == t->c_ospeed && tp->t_cflag == t->c_cflag)
276 return 0;
277
278 tp->t_ispeed = t->c_ispeed;
279 tp->t_ospeed = t->c_ospeed;
280 tp->t_cflag = t->c_cflag;
281
282 sc->sc_wr[WR3] &= 0x3f;
283 sc->sc_wr[WR5] &= 0x9f;
284 switch (tp->t_cflag & CSIZE) {
285 case CS7:
286 sc->sc_wr[WR3] |= WR3_RX7BIT; sc->sc_wr[WR5] |= WR5_TX7BIT;
287 break;
288 case CS8:
289 sc->sc_wr[WR3] |= WR3_RX8BIT; sc->sc_wr[WR5] |= WR5_TX8BIT;
290 break;
291 }
292 if (tp->t_cflag & PARENB) {
293 wr4 |= WR4_PARENAB;
294 if ((tp->t_cflag & PARODD) == 0)
295 wr4 |= WR4_EPARITY;
296 }
297 wr4 |= (tp->t_cflag & CSTOPB) ? WR4_STOP2 : WR4_STOP1;
298 sc->sc_wr[WR4] = wr4;
299
300 s = spltty();
301 setsioreg(sc->sc_ctl, WR4, sc->sc_wr[WR4]);
302 setsioreg(sc->sc_ctl, WR3, sc->sc_wr[WR3]);
303 setsioreg(sc->sc_ctl, WR5, sc->sc_wr[WR5]);
304 splx(s);
305
306 return 0;
307 }
308
309 static int
310 siomctl(sc, control, op)
311 struct siotty_softc *sc;
312 int control, op;
313 {
314 int val, s, wr5, rr;
315
316 val = 0;
317 if (control & TIOCM_BREAK)
318 val |= WR5_BREAK;
319 if (control & TIOCM_DTR)
320 val |= WR5_DTR;
321 if (control & TIOCM_RTS)
322 val |= WR5_RTS;
323 s = spltty();
324 wr5 = sc->sc_wr[WR5];
325 switch (op) {
326 case DMSET:
327 wr5 &= ~(WR5_BREAK|WR5_DTR|WR5_RTS);
328 /* FALLTHRU */
329 case DMBIS:
330 wr5 |= val;
331 break;
332 case DMBIC:
333 wr5 &= ~val;
334 break;
335 case DMGET:
336 val = 0;
337 rr = getsiocsr(sc->sc_ctl);
338 if (wr5 & WR5_DTR)
339 val |= TIOCM_DTR;
340 if (wr5 & WR5_RTS)
341 val |= TIOCM_RTS;
342 if (rr & RR_CTS)
343 val |= TIOCM_CTS;
344 if (rr & RR_DCD)
345 val |= TIOCM_CD;
346 goto done;
347 }
348 sc->sc_wr[WR5] = wr5;
349 setsioreg(sc->sc_ctl, WR5, wr5);
350 val = 0;
351 done:
352 splx(s);
353 return val;
354 }
355
356 /*-------------------- cdevsw[] interface --------------------*/
357
358 int
359 sioopen(dev, flag, mode, l)
360 dev_t dev;
361 int flag, mode;
362 struct lwp *l;
363 {
364 struct siotty_softc *sc;
365 struct tty *tp;
366 int error;
367
368 if ((sc = siotty_cd.cd_devs[minor(dev)]) == NULL)
369 return ENXIO;
370 if ((tp = sc->sc_tty) == NULL) {
371 tp = sc->sc_tty = ttymalloc();
372 tty_attach(tp);
373 }
374
375 tp->t_oproc = siostart;
376 tp->t_param = sioparam;
377 tp->t_hwiflow = NULL /* XXX siohwiflow XXX */;
378 tp->t_dev = dev;
379
380 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
381 return (EBUSY);
382
383 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
384 struct termios t;
385
386 t.c_ispeed = t.c_ospeed = TTYDEF_SPEED;
387 t.c_cflag = TTYDEF_CFLAG;
388 tp->t_ospeed = 0; /* force register update */
389 (void)sioparam(tp, &t);
390 tp->t_iflag = TTYDEF_IFLAG;
391 tp->t_oflag = TTYDEF_OFLAG;
392 tp->t_lflag = TTYDEF_LFLAG;
393 ttychars(tp);
394 ttsetwater(tp);
395 /* raise RTS and DTR here; but, DTR lead is not wired */
396 /* then check DCD condition; but, DCD lead is not wired */
397 tp->t_state |= TS_CARR_ON; /* assume detected all the time */
398 #if 0
399 if ((sc->sc_flags & TIOCFLAG_SOFTCAR)
400 || (tp->t_cflag & MDMBUF)
401 || (getsiocsr(sc->sc_ctl) & RR_DCD))
402 tp->t_state |= TS_CARR_ON;
403 else
404 tp->t_state &= ~TS_CARR_ON;
405 #endif
406 }
407
408 error = ttyopen(tp, 0, (flag & O_NONBLOCK));
409 if (error > 0)
410 return error;
411 return (*tp->t_linesw->l_open)(dev, tp);
412 }
413
414 int
415 sioclose(dev, flag, mode, l)
416 dev_t dev;
417 int flag, mode;
418 struct lwp *l;
419 {
420 struct siotty_softc *sc = siotty_cd.cd_devs[minor(dev)];
421 struct tty *tp = sc->sc_tty;
422 int s;
423
424 (*tp->t_linesw->l_close)(tp, flag);
425
426 s = spltty();
427 siomctl(sc, TIOCM_BREAK, DMBIC);
428 #if 0 /* because unable to feed DTR signal */
429 if ((tp->t_cflag & HUPCL)
430 || tp->t_wopen || (tp->t_state & TS_ISOPEN) == 0) {
431 siomctl(sc, TIOCM_DTR, DMBIC);
432 /* Yield CPU time to others for 1 second, then ... */
433 siomctl(sc, TIOCM_DTR, DMBIS);
434 }
435 #endif
436 splx(s);
437 return ttyclose(tp);
438 }
439
440 int
441 sioread(dev, uio, flag)
442 dev_t dev;
443 struct uio *uio;
444 int flag;
445 {
446 struct siotty_softc *sc = siotty_cd.cd_devs[minor(dev)];
447 struct tty *tp = sc->sc_tty;
448
449 return (*tp->t_linesw->l_read)(tp, uio, flag);
450 }
451
452 int
453 siowrite(dev, uio, flag)
454 dev_t dev;
455 struct uio *uio;
456 int flag;
457 {
458 struct siotty_softc *sc = siotty_cd.cd_devs[minor(dev)];
459 struct tty *tp = sc->sc_tty;
460
461 return (*tp->t_linesw->l_write)(tp, uio, flag);
462 }
463
464 int
465 siopoll(dev, events, l)
466 dev_t dev;
467 int events;
468 struct lwp *l;
469 {
470 struct siotty_softc *sc = siotty_cd.cd_devs[minor(dev)];
471 struct tty *tp = sc->sc_tty;
472
473 return ((*tp->t_linesw->l_poll)(tp, events, l));
474 }
475
476 int
477 sioioctl(dev, cmd, data, flag, l)
478 dev_t dev;
479 u_long cmd;
480 void *data;
481 int flag;
482 struct lwp *l;
483 {
484 struct siotty_softc *sc = siotty_cd.cd_devs[minor(dev)];
485 struct tty *tp = sc->sc_tty;
486 int error;
487
488 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
489 if (error != EPASSTHROUGH)
490 return error;
491
492 error = ttioctl(tp, cmd, data, flag, l);
493 if (error != EPASSTHROUGH)
494 return error;
495
496 /* the last resort for TIOC ioctl tranversing */
497 switch (cmd) {
498 case TIOCSBRK: /* Set the hardware into BREAK condition */
499 siomctl(sc, TIOCM_BREAK, DMBIS);
500 break;
501 case TIOCCBRK: /* Clear the hardware BREAK condition */
502 siomctl(sc, TIOCM_BREAK, DMBIC);
503 break;
504 case TIOCSDTR: /* Assert DTR signal */
505 siomctl(sc, TIOCM_DTR|TIOCM_RTS, DMBIS);
506 break;
507 case TIOCCDTR: /* Clear DTR signal */
508 siomctl(sc, TIOCM_DTR|TIOCM_RTS, DMBIC);
509 break;
510 case TIOCMSET: /* Set modem state replacing current one */
511 siomctl(sc, *(int *)data, DMSET);
512 break;
513 case TIOCMGET: /* Return current modem state */
514 *(int *)data = siomctl(sc, 0, DMGET);
515 break;
516 case TIOCMBIS: /* Set individual bits of modem state */
517 siomctl(sc, *(int *)data, DMBIS);
518 break;
519 case TIOCMBIC: /* Clear individual bits of modem state */
520 siomctl(sc, *(int *)data, DMBIC);
521 break;
522 case TIOCSFLAGS: /* Instruct how serial port behaves */
523 sc->sc_flags = *(int *)data;
524 break;
525 case TIOCGFLAGS: /* Return current serial port state */
526 *(int *)data = sc->sc_flags;
527 break;
528 default:
529 return EPASSTHROUGH;
530 }
531 return 0;
532 }
533
534 /* ARSGUSED */
535 struct tty *
536 siotty(dev)
537 dev_t dev;
538 {
539 struct siotty_softc *sc = siotty_cd.cd_devs[minor(dev)];
540
541 return sc->sc_tty;
542 }
543
544 /*-------------------- miscelleneous routine --------------------*/
545
546 /* EXPORT */ void
547 setsioreg(sio, regno, val)
548 struct sioreg *sio;
549 int regno, val;
550 {
551 if (regno != 0)
552 sio->sio_cmd = regno; /* DELAY(); */
553 sio->sio_cmd = val; /* DELAY(); */
554 }
555
556 /* EXPORT */ int
557 getsiocsr(sio)
558 struct sioreg *sio;
559 {
560 int val;
561
562 val = sio->sio_stat << 8; /* DELAY(); */
563 sio->sio_cmd = 1; /* DELAY(); */
564 val |= sio->sio_stat; /* DELAY(); */
565 return val;
566 }
567
568 /*--------------------- console interface ----------------------*/
569
570 void syscnattach __P((int));
571 int syscngetc __P((dev_t));
572 void syscnputc __P((dev_t, int));
573
574 struct consdev syscons = {
575 NULL,
576 NULL,
577 syscngetc,
578 syscnputc,
579 nullcnpollc,
580 NULL,
581 NULL,
582 NULL,
583 NODEV,
584 CN_REMOTE,
585 };
586
587 /* EXPORT */ void
588 syscnattach(channel)
589 int channel;
590 {
591 /*
592 * Channel A is immediately initialized with 9600N1 right after cold
593 * boot/reset/poweron. ROM monitor emits one line message on CH.A.
594 */
595 struct sioreg *sio;
596 sio = (struct sioreg *)0x51000000 + channel;
597
598 syscons.cn_dev = makedev(cdevsw_lookup_major(&siotty_cdevsw),
599 channel);
600 cn_tab = &syscons;
601
602 setsioreg(sio, WR0, WR0_CHANRST);
603 setsioreg(sio, WR2A, WR2_VEC86 | WR2_INTR_1);
604 setsioreg(sio, WR2B, 0);
605 setsioreg(sio, WR0, ch0_regs[WR0]);
606 setsioreg(sio, WR4, ch0_regs[WR4]);
607 setsioreg(sio, WR3, ch0_regs[WR3]);
608 setsioreg(sio, WR5, ch0_regs[WR5]);
609 setsioreg(sio, WR0, ch0_regs[WR0]);
610 }
611
612 /* EXPORT */ int
613 syscngetc(dev)
614 dev_t dev;
615 {
616 struct sioreg *sio;
617 int s, c;
618
619 sio = (struct sioreg *)0x51000000 + ((int)dev & 0x1);
620 s = splhigh();
621 while ((getsiocsr(sio) & RR_RXRDY) == 0)
622 ;
623 c = sio->sio_data;
624 splx(s);
625
626 return c;
627 }
628
629 /* EXPORT */ void
630 syscnputc(dev, c)
631 dev_t dev;
632 int c;
633 {
634 struct sioreg *sio;
635 int s;
636
637 sio = (struct sioreg *)0x51000000 + ((int)dev & 0x1);
638 s = splhigh();
639 while ((getsiocsr(sio) & RR_TXRDY) == 0)
640 ;
641 sio->sio_cmd = WR0_RSTPEND;
642 sio->sio_data = c;
643 splx(s);
644 }
645