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