siotty.c revision 1.25 1 /* $NetBSD: siotty.c,v 1.25 2009/03/14 15:36:08 dsl 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.25 2009/03/14 15:36:08 dsl 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(struct tty *);
85 static int sioparam(struct tty *, struct termios *);
86 static void siottyintr(int);
87 static int siomctl(struct siotty_softc *, int, int);
88
89 static int siotty_match(struct device *, struct cfdata *, void *);
90 static void siotty_attach(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(struct device *parent, struct cfdata *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(parent, self, aux)
122 struct device *parent, *self;
123 void *aux;
124 {
125 struct sio_softc *scp = (void *)parent;
126 struct siotty_softc *sc = (void *)self;
127 struct sio_attach_args *args = aux;
128
129 sc->sc_ctl = (struct sioreg *)scp->scp_ctl + args->channel;
130 bcopy(ch0_regs, sc->sc_wr, sizeof(ch0_regs));
131 scp->scp_intr[args->channel] = siottyintr;
132
133 if (args->hwflags == 1) {
134 printf(" (console)");
135 sc->sc_flags = TIOCFLAG_SOFTCAR;
136 }
137 else {
138 setsioreg(sc->sc_ctl, WR0, WR0_CHANRST);
139 setsioreg(sc->sc_ctl, WR2A, WR2_VEC86 | WR2_INTR_1);
140 setsioreg(sc->sc_ctl, WR2B, 0);
141 setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
142 setsioreg(sc->sc_ctl, WR4, sc->sc_wr[WR4]);
143 setsioreg(sc->sc_ctl, WR3, sc->sc_wr[WR3]);
144 setsioreg(sc->sc_ctl, WR5, sc->sc_wr[WR5]);
145 setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]);
146 }
147 setsioreg(sc->sc_ctl, WR1, sc->sc_wr[WR1]); /* now interrupt driven */
148
149 printf("\n");
150 }
151
152 /*-------------------- low level routine --------------------*/
153
154 static void
155 siottyintr(int chan)
156 {
157 struct siotty_softc *sc;
158 struct sioreg *sio;
159 struct tty *tp;
160 unsigned int code;
161 int rr;
162
163 sc = device_lookup_private(&siotty_cd, chan);
164 if (sc == NULL)
165 return;
166
167 tp = sc->sc_tty;
168 sio = sc->sc_ctl;
169 rr = getsiocsr(sio);
170 if (rr & RR_RXRDY) {
171 do {
172 code = sio->sio_data;
173 if (rr & (RR_FRAMING | RR_OVERRUN | RR_PARITY)) {
174 sio->sio_cmd = WR0_ERRRST;
175 if (sio->sio_stat & RR_FRAMING)
176 code |= TTY_FE;
177 else if (sio->sio_stat & RR_PARITY)
178 code |= TTY_PE;
179 }
180 if (tp == NULL || (tp->t_state & TS_ISOPEN) == 0)
181 continue;
182 #if 0 && defined(DDB) /* ?!?! fails to resume ?!?! */
183 if ((rr & RR_BREAK) && tp->t_dev == cn_tab->cn_dev) {
184 cpu_Debugger();
185 return;
186 }
187 #endif
188 (*tp->t_linesw->l_rint)(code, tp);
189 } while ((rr = getsiocsr(sio)) & RR_RXRDY);
190 }
191 if (rr & RR_TXRDY) {
192 sio->sio_cmd = WR0_RSTPEND;
193 if (tp != NULL) {
194 tp->t_state &= ~(TS_BUSY|TS_FLUSH);
195 (*tp->t_linesw->l_start)(tp);
196 }
197 }
198 }
199
200 static void
201 siostart(struct tty *tp)
202 {
203 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(tp->t_dev));
204 int s, c;
205
206 s = spltty();
207 if (tp->t_state & (TS_BUSY|TS_TIMEOUT|TS_TTSTOP))
208 goto out;
209 if (!ttypull(tp))
210 goto out;
211 tp->t_state |= TS_BUSY;
212 while (getsiocsr(sc->sc_ctl) & RR_TXRDY) {
213 if ((c = getc(&tp->t_outq)) == -1)
214 break;
215 sc->sc_ctl->sio_data = c;
216 }
217 out:
218 splx(s);
219 }
220
221 void
222 siostop(struct tty *tp, int flag)
223 {
224 int s;
225
226 s = spltty();
227 if (TS_BUSY == (tp->t_state & (TS_BUSY|TS_TTSTOP))) {
228 /*
229 * Device is transmitting; must stop it.
230 */
231 tp->t_state |= TS_FLUSH;
232 }
233 splx(s);
234 }
235
236 static int
237 sioparam(struct tty *tp, struct termios *t)
238 {
239 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(tp->t_dev));
240 int wr4, s;
241
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 = spltty();
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(sc, control, op)
296 struct siotty_softc *sc;
297 int control, op;
298 {
299 int val, s, wr5, rr;
300
301 val = 0;
302 if (control & TIOCM_BREAK)
303 val |= WR5_BREAK;
304 if (control & TIOCM_DTR)
305 val |= WR5_DTR;
306 if (control & TIOCM_RTS)
307 val |= WR5_RTS;
308 s = spltty();
309 wr5 = sc->sc_wr[WR5];
310 switch (op) {
311 case DMSET:
312 wr5 &= ~(WR5_BREAK|WR5_DTR|WR5_RTS);
313 /* FALLTHRU */
314 case DMBIS:
315 wr5 |= val;
316 break;
317 case DMBIC:
318 wr5 &= ~val;
319 break;
320 case DMGET:
321 val = 0;
322 rr = getsiocsr(sc->sc_ctl);
323 if (wr5 & WR5_DTR)
324 val |= TIOCM_DTR;
325 if (wr5 & WR5_RTS)
326 val |= TIOCM_RTS;
327 if (rr & RR_CTS)
328 val |= TIOCM_CTS;
329 if (rr & RR_DCD)
330 val |= TIOCM_CD;
331 goto done;
332 }
333 sc->sc_wr[WR5] = wr5;
334 setsioreg(sc->sc_ctl, WR5, wr5);
335 val = 0;
336 done:
337 splx(s);
338 return val;
339 }
340
341 /*-------------------- cdevsw[] interface --------------------*/
342
343 int
344 sioopen(dev_t dev, int flag, int mode, struct lwp *l)
345 {
346 struct siotty_softc *sc;
347 struct tty *tp;
348 int error;
349
350 sc = device_lookup_private(&siotty_cd, minor(dev));
351 if (sc == NULL)
352 return ENXIO;
353 if ((tp = sc->sc_tty) == NULL) {
354 tp = sc->sc_tty = ttymalloc();
355 tty_attach(tp);
356 }
357
358 tp->t_oproc = siostart;
359 tp->t_param = sioparam;
360 tp->t_hwiflow = NULL /* XXX siohwiflow XXX */;
361 tp->t_dev = dev;
362
363 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
364 return (EBUSY);
365
366 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
367 struct termios t;
368
369 t.c_ispeed = t.c_ospeed = TTYDEF_SPEED;
370 t.c_cflag = TTYDEF_CFLAG;
371 tp->t_ospeed = 0; /* force register update */
372 (void)sioparam(tp, &t);
373 tp->t_iflag = TTYDEF_IFLAG;
374 tp->t_oflag = TTYDEF_OFLAG;
375 tp->t_lflag = TTYDEF_LFLAG;
376 ttychars(tp);
377 ttsetwater(tp);
378 /* raise RTS and DTR here; but, DTR lead is not wired */
379 /* then check DCD condition; but, DCD lead is not wired */
380 tp->t_state |= TS_CARR_ON; /* assume detected all the time */
381 #if 0
382 if ((sc->sc_flags & TIOCFLAG_SOFTCAR)
383 || (tp->t_cflag & MDMBUF)
384 || (getsiocsr(sc->sc_ctl) & RR_DCD))
385 tp->t_state |= TS_CARR_ON;
386 else
387 tp->t_state &= ~TS_CARR_ON;
388 #endif
389 }
390
391 error = ttyopen(tp, 0, (flag & O_NONBLOCK));
392 if (error > 0)
393 return error;
394 return (*tp->t_linesw->l_open)(dev, tp);
395 }
396
397 int
398 sioclose(dev_t dev, int flag, int mode, struct lwp *l)
399 {
400 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
401 struct tty *tp = sc->sc_tty;
402 int s;
403
404 (*tp->t_linesw->l_close)(tp, flag);
405
406 s = spltty();
407 siomctl(sc, TIOCM_BREAK, DMBIC);
408 #if 0 /* because unable to feed DTR signal */
409 if ((tp->t_cflag & HUPCL)
410 || tp->t_wopen || (tp->t_state & TS_ISOPEN) == 0) {
411 siomctl(sc, TIOCM_DTR, DMBIC);
412 /* Yield CPU time to others for 1 second, then ... */
413 siomctl(sc, TIOCM_DTR, DMBIS);
414 }
415 #endif
416 splx(s);
417 return ttyclose(tp);
418 }
419
420 int
421 sioread(dev_t dev, struct uio *uio, int flag)
422 {
423 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
424 struct tty *tp = sc->sc_tty;
425
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 = device_lookup_private(&siotty_cd,minor(dev));
433 struct tty *tp = sc->sc_tty;
434
435 return (*tp->t_linesw->l_write)(tp, uio, flag);
436 }
437
438 int
439 siopoll(dev_t dev, int events, struct lwp *l)
440 {
441 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
442 struct tty *tp = sc->sc_tty;
443
444 return ((*tp->t_linesw->l_poll)(tp, events, l));
445 }
446
447 int
448 sioioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
449 {
450 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
451 struct tty *tp = sc->sc_tty;
452 int error;
453
454 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
455 if (error != EPASSTHROUGH)
456 return error;
457
458 error = ttioctl(tp, cmd, data, flag, l);
459 if (error != EPASSTHROUGH)
460 return error;
461
462 /* the last resort for TIOC ioctl tranversing */
463 switch (cmd) {
464 case TIOCSBRK: /* Set the hardware into BREAK condition */
465 siomctl(sc, TIOCM_BREAK, DMBIS);
466 break;
467 case TIOCCBRK: /* Clear the hardware BREAK condition */
468 siomctl(sc, TIOCM_BREAK, DMBIC);
469 break;
470 case TIOCSDTR: /* Assert DTR signal */
471 siomctl(sc, TIOCM_DTR|TIOCM_RTS, DMBIS);
472 break;
473 case TIOCCDTR: /* Clear DTR signal */
474 siomctl(sc, TIOCM_DTR|TIOCM_RTS, DMBIC);
475 break;
476 case TIOCMSET: /* Set modem state replacing current one */
477 siomctl(sc, *(int *)data, DMSET);
478 break;
479 case TIOCMGET: /* Return current modem state */
480 *(int *)data = siomctl(sc, 0, DMGET);
481 break;
482 case TIOCMBIS: /* Set individual bits of modem state */
483 siomctl(sc, *(int *)data, DMBIS);
484 break;
485 case TIOCMBIC: /* Clear individual bits of modem state */
486 siomctl(sc, *(int *)data, DMBIC);
487 break;
488 case TIOCSFLAGS: /* Instruct how serial port behaves */
489 sc->sc_flags = *(int *)data;
490 break;
491 case TIOCGFLAGS: /* Return current serial port state */
492 *(int *)data = sc->sc_flags;
493 break;
494 default:
495 return EPASSTHROUGH;
496 }
497 return 0;
498 }
499
500 /* ARSGUSED */
501 struct tty *
502 siotty(dev_t dev)
503 {
504 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
505
506 return sc->sc_tty;
507 }
508
509 /*-------------------- miscelleneous routine --------------------*/
510
511 /* EXPORT */ void
512 setsioreg(struct sioreg *sio, int regno, int val)
513 {
514 if (regno != 0)
515 sio->sio_cmd = regno; /* DELAY(); */
516 sio->sio_cmd = val; /* DELAY(); */
517 }
518
519 /* EXPORT */ int
520 getsiocsr(struct sioreg *sio)
521 {
522 int val;
523
524 val = sio->sio_stat << 8; /* DELAY(); */
525 sio->sio_cmd = 1; /* DELAY(); */
526 val |= sio->sio_stat; /* DELAY(); */
527 return val;
528 }
529
530 /*--------------------- console interface ----------------------*/
531
532 void syscnattach(int);
533 int syscngetc(dev_t);
534 void syscnputc(dev_t, int);
535
536 struct consdev syscons = {
537 NULL,
538 NULL,
539 syscngetc,
540 syscnputc,
541 nullcnpollc,
542 NULL,
543 NULL,
544 NULL,
545 NODEV,
546 CN_REMOTE,
547 };
548
549 /* EXPORT */ void
550 syscnattach(int channel)
551 {
552 /*
553 * Channel A is immediately initialized with 9600N1 right after cold
554 * boot/reset/poweron. ROM monitor emits one line message on CH.A.
555 */
556 struct sioreg *sio;
557 sio = (struct sioreg *)0x51000000 + channel;
558
559 syscons.cn_dev = makedev(cdevsw_lookup_major(&siotty_cdevsw),
560 channel);
561 cn_tab = &syscons;
562
563 setsioreg(sio, WR0, WR0_CHANRST);
564 setsioreg(sio, WR2A, WR2_VEC86 | WR2_INTR_1);
565 setsioreg(sio, WR2B, 0);
566 setsioreg(sio, WR0, ch0_regs[WR0]);
567 setsioreg(sio, WR4, ch0_regs[WR4]);
568 setsioreg(sio, WR3, ch0_regs[WR3]);
569 setsioreg(sio, WR5, ch0_regs[WR5]);
570 setsioreg(sio, WR0, ch0_regs[WR0]);
571 }
572
573 /* EXPORT */ int
574 syscngetc(dev_t dev)
575 {
576 struct sioreg *sio;
577 int s, c;
578
579 sio = (struct sioreg *)0x51000000 + ((int)dev & 0x1);
580 s = splhigh();
581 while ((getsiocsr(sio) & RR_RXRDY) == 0)
582 ;
583 c = sio->sio_data;
584 splx(s);
585
586 return c;
587 }
588
589 /* EXPORT */ void
590 syscnputc(dev_t dev, int c)
591 {
592 struct sioreg *sio;
593 int s;
594
595 sio = (struct sioreg *)0x51000000 + ((int)dev & 0x1);
596 s = splhigh();
597 while ((getsiocsr(sio) & RR_TXRDY) == 0)
598 ;
599 sio->sio_cmd = WR0_RSTPEND;
600 sio->sio_data = c;
601 splx(s);
602 }
603