siotty.c revision 1.31 1 /* $NetBSD: siotty.c,v 1.31 2011/07/27 11:54:40 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.31 2011/07/27 11:54:40 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 u_int8_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 u_int8_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(struct device *parent, struct device *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 }
136 else {
137 setsioreg(sc->sc_ctl, WR0, WR0_CHANRST);
138 setsioreg(sc->sc_ctl, WR2A, WR2_VEC86 | WR2_INTR_1);
139 setsioreg(sc->sc_ctl, WR2B, 0);
140 setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
141 setsioreg(sc->sc_ctl, WR4, sc->sc_wr[WR4]);
142 setsioreg(sc->sc_ctl, WR3, sc->sc_wr[WR3]);
143 setsioreg(sc->sc_ctl, WR5, sc->sc_wr[WR5]);
144 setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
145 }
146 setsioreg(sc->sc_ctl, WR1, sc->sc_wr[WR1]); /* now interrupt driven */
147
148 aprint_normal("\n");
149 }
150
151 /*-------------------- low level routine --------------------*/
152
153 static void
154 siottyintr(int chan)
155 {
156 struct siotty_softc *sc;
157 struct sioreg *sio;
158 struct tty *tp;
159 unsigned int code;
160 int rr;
161
162 sc = device_lookup_private(&siotty_cd, chan);
163 if (sc == NULL)
164 return;
165
166 tp = sc->sc_tty;
167 sio = sc->sc_ctl;
168 rr = getsiocsr(sio);
169 if (rr & RR_RXRDY) {
170 do {
171 code = sio->sio_data;
172 if (rr & (RR_FRAMING | RR_OVERRUN | RR_PARITY)) {
173 sio->sio_cmd = WR0_ERRRST;
174 if (sio->sio_stat & RR_FRAMING)
175 code |= TTY_FE;
176 else if (sio->sio_stat & RR_PARITY)
177 code |= TTY_PE;
178 }
179 if (tp == NULL || (tp->t_state & TS_ISOPEN) == 0)
180 continue;
181 #if 0 && defined(DDB) /* ?!?! fails to resume ?!?! */
182 if ((rr & RR_BREAK) && tp->t_dev == cn_tab->cn_dev) {
183 cpu_Debugger();
184 return;
185 }
186 #endif
187 (*tp->t_linesw->l_rint)(code, tp);
188 } while ((rr = getsiocsr(sio)) & RR_RXRDY);
189 }
190 if (rr & RR_TXRDY) {
191 sio->sio_cmd = WR0_RSTPEND;
192 if (tp != NULL) {
193 tp->t_state &= ~(TS_BUSY|TS_FLUSH);
194 (*tp->t_linesw->l_start)(tp);
195 }
196 }
197 }
198
199 static void
200 siostart(struct tty *tp)
201 {
202 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(tp->t_dev));
203 int s, c;
204
205 s = spltty();
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 = spltty();
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 = device_lookup_private(&siotty_cd,minor(tp->t_dev));
239 int wr4, s;
240
241 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
242 return EINVAL;
243 wr4 = ttspeedtab(t->c_ospeed, siospeedtab);
244 if (wr4 < 0)
245 return EINVAL;
246
247 if (sc->sc_flags & TIOCFLAG_SOFTCAR) {
248 t->c_cflag |= CLOCAL;
249 t->c_cflag &= ~HUPCL;
250 }
251 if (sc->sc_flags & TIOCFLAG_CLOCAL)
252 t->c_cflag |= CLOCAL;
253
254 /*
255 * If there were no changes, don't do anything. This avoids dropping
256 * input and improves performance when all we did was frob things like
257 * VMIN and VTIME.
258 */
259 if (tp->t_ospeed == t->c_ospeed && tp->t_cflag == t->c_cflag)
260 return 0;
261
262 tp->t_ispeed = t->c_ispeed;
263 tp->t_ospeed = t->c_ospeed;
264 tp->t_cflag = t->c_cflag;
265
266 sc->sc_wr[WR3] &= 0x3f;
267 sc->sc_wr[WR5] &= 0x9f;
268 switch (tp->t_cflag & CSIZE) {
269 case CS7:
270 sc->sc_wr[WR3] |= WR3_RX7BIT; sc->sc_wr[WR5] |= WR5_TX7BIT;
271 break;
272 case CS8:
273 sc->sc_wr[WR3] |= WR3_RX8BIT; sc->sc_wr[WR5] |= WR5_TX8BIT;
274 break;
275 }
276 if (tp->t_cflag & PARENB) {
277 wr4 |= WR4_PARENAB;
278 if ((tp->t_cflag & PARODD) == 0)
279 wr4 |= WR4_EPARITY;
280 }
281 wr4 |= (tp->t_cflag & CSTOPB) ? WR4_STOP2 : WR4_STOP1;
282 sc->sc_wr[WR4] = wr4;
283
284 s = spltty();
285 setsioreg(sc->sc_ctl, WR4, sc->sc_wr[WR4]);
286 setsioreg(sc->sc_ctl, WR3, sc->sc_wr[WR3]);
287 setsioreg(sc->sc_ctl, WR5, sc->sc_wr[WR5]);
288 splx(s);
289
290 return 0;
291 }
292
293 static int
294 siomctl(struct siotty_softc *sc, int control, int op)
295 {
296 int val, s, wr5, rr;
297
298 val = 0;
299 if (control & TIOCM_BREAK)
300 val |= WR5_BREAK;
301 if (control & TIOCM_DTR)
302 val |= WR5_DTR;
303 if (control & TIOCM_RTS)
304 val |= WR5_RTS;
305 s = spltty();
306 wr5 = sc->sc_wr[WR5];
307 switch (op) {
308 case DMSET:
309 wr5 &= ~(WR5_BREAK|WR5_DTR|WR5_RTS);
310 /* FALLTHRU */
311 case DMBIS:
312 wr5 |= val;
313 break;
314 case DMBIC:
315 wr5 &= ~val;
316 break;
317 case DMGET:
318 val = 0;
319 rr = getsiocsr(sc->sc_ctl);
320 if (wr5 & WR5_DTR)
321 val |= TIOCM_DTR;
322 if (wr5 & WR5_RTS)
323 val |= TIOCM_RTS;
324 if (rr & RR_CTS)
325 val |= TIOCM_CTS;
326 if (rr & RR_DCD)
327 val |= TIOCM_CD;
328 goto done;
329 }
330 sc->sc_wr[WR5] = wr5;
331 setsioreg(sc->sc_ctl, WR5, wr5);
332 val = 0;
333 done:
334 splx(s);
335 return val;
336 }
337
338 /*-------------------- cdevsw[] interface --------------------*/
339
340 int
341 sioopen(dev_t dev, int flag, int mode, struct lwp *l)
342 {
343 struct siotty_softc *sc;
344 struct tty *tp;
345 int error;
346
347 sc = device_lookup_private(&siotty_cd, minor(dev));
348 if (sc == NULL)
349 return ENXIO;
350 if ((tp = sc->sc_tty) == NULL) {
351 tp = sc->sc_tty = tty_alloc();
352 tty_attach(tp);
353 }
354
355 tp->t_oproc = siostart;
356 tp->t_param = sioparam;
357 tp->t_hwiflow = NULL /* XXX siohwiflow XXX */;
358 tp->t_dev = dev;
359
360 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
361 return (EBUSY);
362
363 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
364 struct termios t;
365
366 t.c_ispeed = t.c_ospeed = TTYDEF_SPEED;
367 t.c_cflag = TTYDEF_CFLAG;
368 tp->t_ospeed = 0; /* force register update */
369 (void)sioparam(tp, &t);
370 tp->t_iflag = TTYDEF_IFLAG;
371 tp->t_oflag = TTYDEF_OFLAG;
372 tp->t_lflag = TTYDEF_LFLAG;
373 ttychars(tp);
374 ttsetwater(tp);
375 /* raise RTS and DTR here; but, DTR lead is not wired */
376 /* then check DCD condition; but, DCD lead is not wired */
377 tp->t_state |= TS_CARR_ON; /* assume detected all the time */
378 #if 0
379 if ((sc->sc_flags & TIOCFLAG_SOFTCAR)
380 || (tp->t_cflag & MDMBUF)
381 || (getsiocsr(sc->sc_ctl) & RR_DCD))
382 tp->t_state |= TS_CARR_ON;
383 else
384 tp->t_state &= ~TS_CARR_ON;
385 #endif
386 }
387
388 error = ttyopen(tp, 0, (flag & O_NONBLOCK));
389 if (error > 0)
390 return error;
391 return (*tp->t_linesw->l_open)(dev, tp);
392 }
393
394 int
395 sioclose(dev_t dev, int flag, int mode, struct lwp *l)
396 {
397 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
398 struct tty *tp = sc->sc_tty;
399 int s;
400
401 (*tp->t_linesw->l_close)(tp, flag);
402
403 s = spltty();
404 siomctl(sc, TIOCM_BREAK, DMBIC);
405 #if 0 /* because unable to feed DTR signal */
406 if ((tp->t_cflag & HUPCL)
407 || tp->t_wopen || (tp->t_state & TS_ISOPEN) == 0) {
408 siomctl(sc, TIOCM_DTR, DMBIC);
409 /* Yield CPU time to others for 1 second, then ... */
410 siomctl(sc, TIOCM_DTR, DMBIS);
411 }
412 #endif
413 splx(s);
414 return ttyclose(tp);
415 }
416
417 int
418 sioread(dev_t dev, struct uio *uio, int flag)
419 {
420 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
421 struct tty *tp = sc->sc_tty;
422
423 return (*tp->t_linesw->l_read)(tp, uio, flag);
424 }
425
426 int
427 siowrite(dev_t dev, struct uio *uio, int flag)
428 {
429 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
430 struct tty *tp = sc->sc_tty;
431
432 return (*tp->t_linesw->l_write)(tp, uio, flag);
433 }
434
435 int
436 siopoll(dev_t dev, int events, struct lwp *l)
437 {
438 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
439 struct tty *tp = sc->sc_tty;
440
441 return ((*tp->t_linesw->l_poll)(tp, events, l));
442 }
443
444 int
445 sioioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
446 {
447 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
448 struct tty *tp = sc->sc_tty;
449 int error;
450
451 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
452 if (error != EPASSTHROUGH)
453 return error;
454
455 error = ttioctl(tp, cmd, data, flag, l);
456 if (error != EPASSTHROUGH)
457 return error;
458
459 /* the last resort for TIOC ioctl tranversing */
460 switch (cmd) {
461 case TIOCSBRK: /* Set the hardware into BREAK condition */
462 siomctl(sc, TIOCM_BREAK, DMBIS);
463 break;
464 case TIOCCBRK: /* Clear the hardware BREAK condition */
465 siomctl(sc, TIOCM_BREAK, DMBIC);
466 break;
467 case TIOCSDTR: /* Assert DTR signal */
468 siomctl(sc, TIOCM_DTR|TIOCM_RTS, DMBIS);
469 break;
470 case TIOCCDTR: /* Clear DTR signal */
471 siomctl(sc, TIOCM_DTR|TIOCM_RTS, DMBIC);
472 break;
473 case TIOCMSET: /* Set modem state replacing current one */
474 siomctl(sc, *(int *)data, DMSET);
475 break;
476 case TIOCMGET: /* Return current modem state */
477 *(int *)data = siomctl(sc, 0, DMGET);
478 break;
479 case TIOCMBIS: /* Set individual bits of modem state */
480 siomctl(sc, *(int *)data, DMBIS);
481 break;
482 case TIOCMBIC: /* Clear individual bits of modem state */
483 siomctl(sc, *(int *)data, DMBIC);
484 break;
485 case TIOCSFLAGS: /* Instruct how serial port behaves */
486 sc->sc_flags = *(int *)data;
487 break;
488 case TIOCGFLAGS: /* Return current serial port state */
489 *(int *)data = sc->sc_flags;
490 break;
491 default:
492 return EPASSTHROUGH;
493 }
494 return 0;
495 }
496
497 /* ARSGUSED */
498 struct tty *
499 siotty(dev_t dev)
500 {
501 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
502
503 return sc->sc_tty;
504 }
505
506 /*-------------------- miscelleneous routine --------------------*/
507
508 /* EXPORT */ void
509 setsioreg(struct sioreg *sio, int regno, int val)
510 {
511 if (regno != 0)
512 sio->sio_cmd = regno; /* DELAY(); */
513 sio->sio_cmd = val; /* DELAY(); */
514 }
515
516 /* EXPORT */ int
517 getsiocsr(struct sioreg *sio)
518 {
519 int val;
520
521 val = sio->sio_stat << 8; /* DELAY(); */
522 sio->sio_cmd = 1; /* DELAY(); */
523 val |= sio->sio_stat; /* DELAY(); */
524 return val;
525 }
526
527 /*--------------------- console interface ----------------------*/
528
529 void syscnattach(int);
530 int syscngetc(dev_t);
531 void syscnputc(dev_t, int);
532
533 struct consdev syscons = {
534 NULL,
535 NULL,
536 syscngetc,
537 syscnputc,
538 nullcnpollc,
539 NULL,
540 NULL,
541 NULL,
542 NODEV,
543 CN_REMOTE,
544 };
545
546 /* EXPORT */ void
547 syscnattach(int channel)
548 {
549 /*
550 * Channel A is immediately initialized with 9600N1 right after cold
551 * boot/reset/poweron. ROM monitor emits one line message on CH.A.
552 */
553 struct sioreg *sio;
554 sio = (struct sioreg *)0x51000000 + channel;
555
556 syscons.cn_dev = makedev(cdevsw_lookup_major(&siotty_cdevsw),
557 channel);
558 cn_tab = &syscons;
559
560 setsioreg(sio, WR0, WR0_CHANRST);
561 setsioreg(sio, WR2A, WR2_VEC86 | WR2_INTR_1);
562 setsioreg(sio, WR2B, 0);
563 setsioreg(sio, WR0, ch0_regs[WR0]);
564 setsioreg(sio, WR4, ch0_regs[WR4]);
565 setsioreg(sio, WR3, ch0_regs[WR3]);
566 setsioreg(sio, WR5, ch0_regs[WR5]);
567 setsioreg(sio, WR0, ch0_regs[WR0]);
568 }
569
570 /* EXPORT */ int
571 syscngetc(dev_t dev)
572 {
573 struct sioreg *sio;
574 int s, c;
575
576 sio = (struct sioreg *)0x51000000 + ((int)dev & 0x1);
577 s = splhigh();
578 while ((getsiocsr(sio) & RR_RXRDY) == 0)
579 ;
580 c = sio->sio_data;
581 splx(s);
582
583 return c;
584 }
585
586 /* EXPORT */ void
587 syscnputc(dev_t dev, int c)
588 {
589 struct sioreg *sio;
590 int s;
591
592 sio = (struct sioreg *)0x51000000 + ((int)dev & 0x1);
593 s = splhigh();
594 while ((getsiocsr(sio) & RR_TXRDY) == 0)
595 ;
596 sio->sio_cmd = WR0_RSTPEND;
597 sio->sio_data = c;
598 splx(s);
599 }
600