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