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