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