No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / luna68k / dev / siotty.c
blobb9648ec912a9729e38b1c95e1ce1cb922d11914b
1 /* $NetBSD: siotty.c,v 1.28 2009/10/26 19:16:56 cegger Exp $ */
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tohru Nishimura.
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.
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.
32 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34 __KERNEL_RCSID(0, "$NetBSD: siotty.c,v 1.28 2009/10/26 19:16:56 cegger Exp $");
36 #include "opt_ddb.h"
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>
51 #include <machine/cpu.h>
53 #include <luna68k/dev/sioreg.h>
54 #include <luna68k/dev/siovar.h>
56 #define TIOCM_BREAK 01000 /* non standard use */
58 static const u_int8_t ch0_regs[6] = {
59 WR0_RSTINT, /* reset E/S interrupt */
60 WR1_RXALLS | WR1_TXENBL, /* Rx per char, Tx */
61 0, /* */
62 WR3_RX8BIT | WR3_RXENBL, /* Rx */
63 WR4_BAUD96 | WR4_STOP1, /* Tx/Rx */
64 WR5_TX8BIT | WR5_TXENBL | WR5_DTR | WR5_RTS, /* Tx */
67 static const struct speedtab siospeedtab[] = {
68 { 2400, WR4_BAUD24, },
69 { 4800, WR4_BAUD48, },
70 { 9600, WR4_BAUD96, },
71 { -1, 0, },
74 struct siotty_softc {
75 struct device sc_dev;
76 struct tty *sc_tty;
77 struct sioreg *sc_ctl;
78 u_int sc_flags;
79 u_int8_t sc_wr[6];
82 #include "siotty.h"
83 static void siostart(struct tty *);
84 static int sioparam(struct tty *, struct termios *);
85 static void siottyintr(int);
86 static int siomctl(struct siotty_softc *, int, int);
88 static int siotty_match(struct device *, struct cfdata *, void *);
89 static void siotty_attach(struct device *, struct device *, void *);
91 CFATTACH_DECL(siotty, sizeof(struct siotty_softc),
92 siotty_match, siotty_attach, NULL, NULL);
93 extern struct cfdriver siotty_cd;
95 dev_type_open(sioopen);
96 dev_type_close(sioclose);
97 dev_type_read(sioread);
98 dev_type_write(siowrite);
99 dev_type_ioctl(sioioctl);
100 dev_type_stop(siostop);
101 dev_type_tty(siotty);
102 dev_type_poll(siopoll);
104 const struct cdevsw siotty_cdevsw = {
105 sioopen, sioclose, sioread, siowrite, sioioctl,
106 siostop, siotty, siopoll, nommap, ttykqfilter, D_TTY
109 static int
110 siotty_match(struct device *parent, struct cfdata *cf, void *aux)
112 struct sio_attach_args *args = aux;
114 if (args->channel != 0) /* XXX allow tty on Ch.B XXX */
115 return 0;
116 return 1;
119 static void
120 siotty_attach(struct device *parent, struct device *self, void *aux)
122 struct sio_softc *scp = (void *)parent;
123 struct siotty_softc *sc = (void *)self;
124 struct sio_attach_args *args = aux;
126 sc->sc_ctl = (struct sioreg *)scp->scp_ctl + args->channel;
127 memcpy(sc->sc_wr, ch0_regs, sizeof(ch0_regs));
128 scp->scp_intr[args->channel] = siottyintr;
130 if (args->hwflags == 1) {
131 printf(" (console)");
132 sc->sc_flags = TIOCFLAG_SOFTCAR;
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]);
144 setsioreg(sc->sc_ctl, WR1, sc->sc_wr[WR1]); /* now interrupt driven */
146 printf("\n");
149 /*-------------------- low level routine --------------------*/
151 static void
152 siottyintr(int chan)
154 struct siotty_softc *sc;
155 struct sioreg *sio;
156 struct tty *tp;
157 unsigned int code;
158 int rr;
160 sc = device_lookup_private(&siotty_cd, chan);
161 if (sc == NULL)
162 return;
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;
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;
184 #endif
185 (*tp->t_linesw->l_rint)(code, tp);
186 } while ((rr = getsiocsr(sio)) & RR_RXRDY);
188 if (rr & RR_TXRDY) {
189 sio->sio_cmd = WR0_RSTPEND;
190 if (tp != NULL) {
191 tp->t_state &= ~(TS_BUSY|TS_FLUSH);
192 (*tp->t_linesw->l_start)(tp);
197 static void
198 siostart(struct tty *tp)
200 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(tp->t_dev));
201 int s, c;
203 s = spltty();
204 if (tp->t_state & (TS_BUSY|TS_TIMEOUT|TS_TTSTOP))
205 goto out;
206 if (!ttypull(tp))
207 goto out;
208 tp->t_state |= TS_BUSY;
209 while (getsiocsr(sc->sc_ctl) & RR_TXRDY) {
210 if ((c = getc(&tp->t_outq)) == -1)
211 break;
212 sc->sc_ctl->sio_data = c;
214 out:
215 splx(s);
218 void
219 siostop(struct tty *tp, int flag)
221 int s;
223 s = spltty();
224 if (TS_BUSY == (tp->t_state & (TS_BUSY|TS_TTSTOP))) {
226 * Device is transmitting; must stop it.
228 tp->t_state |= TS_FLUSH;
230 splx(s);
233 static int
234 sioparam(struct tty *tp, struct termios *t)
236 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(tp->t_dev));
237 int wr4, s;
239 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
240 return EINVAL;
241 wr4 = ttspeedtab(t->c_ospeed, siospeedtab);
242 if (wr4 < 0)
243 return EINVAL;
245 if (sc->sc_flags & TIOCFLAG_SOFTCAR) {
246 t->c_cflag |= CLOCAL;
247 t->c_cflag &= ~HUPCL;
249 if (sc->sc_flags & TIOCFLAG_CLOCAL)
250 t->c_cflag |= CLOCAL;
253 * If there were no changes, don't do anything. This avoids dropping
254 * input and improves performance when all we did was frob things like
255 * VMIN and VTIME.
257 if (tp->t_ospeed == t->c_ospeed && tp->t_cflag == t->c_cflag)
258 return 0;
260 tp->t_ispeed = t->c_ispeed;
261 tp->t_ospeed = t->c_ospeed;
262 tp->t_cflag = t->c_cflag;
264 sc->sc_wr[WR3] &= 0x3f;
265 sc->sc_wr[WR5] &= 0x9f;
266 switch (tp->t_cflag & CSIZE) {
267 case CS7:
268 sc->sc_wr[WR3] |= WR3_RX7BIT; sc->sc_wr[WR5] |= WR5_TX7BIT;
269 break;
270 case CS8:
271 sc->sc_wr[WR3] |= WR3_RX8BIT; sc->sc_wr[WR5] |= WR5_TX8BIT;
272 break;
274 if (tp->t_cflag & PARENB) {
275 wr4 |= WR4_PARENAB;
276 if ((tp->t_cflag & PARODD) == 0)
277 wr4 |= WR4_EPARITY;
279 wr4 |= (tp->t_cflag & CSTOPB) ? WR4_STOP2 : WR4_STOP1;
280 sc->sc_wr[WR4] = wr4;
282 s = spltty();
283 setsioreg(sc->sc_ctl, WR4, sc->sc_wr[WR4]);
284 setsioreg(sc->sc_ctl, WR3, sc->sc_wr[WR3]);
285 setsioreg(sc->sc_ctl, WR5, sc->sc_wr[WR5]);
286 splx(s);
288 return 0;
291 static int
292 siomctl(struct siotty_softc *sc, int control, int op)
294 int val, s, wr5, rr;
296 val = 0;
297 if (control & TIOCM_BREAK)
298 val |= WR5_BREAK;
299 if (control & TIOCM_DTR)
300 val |= WR5_DTR;
301 if (control & TIOCM_RTS)
302 val |= WR5_RTS;
303 s = spltty();
304 wr5 = sc->sc_wr[WR5];
305 switch (op) {
306 case DMSET:
307 wr5 &= ~(WR5_BREAK|WR5_DTR|WR5_RTS);
308 /* FALLTHRU */
309 case DMBIS:
310 wr5 |= val;
311 break;
312 case DMBIC:
313 wr5 &= ~val;
314 break;
315 case DMGET:
316 val = 0;
317 rr = getsiocsr(sc->sc_ctl);
318 if (wr5 & WR5_DTR)
319 val |= TIOCM_DTR;
320 if (wr5 & WR5_RTS)
321 val |= TIOCM_RTS;
322 if (rr & RR_CTS)
323 val |= TIOCM_CTS;
324 if (rr & RR_DCD)
325 val |= TIOCM_CD;
326 goto done;
328 sc->sc_wr[WR5] = wr5;
329 setsioreg(sc->sc_ctl, WR5, wr5);
330 val = 0;
331 done:
332 splx(s);
333 return val;
336 /*-------------------- cdevsw[] interface --------------------*/
339 sioopen(dev_t dev, int flag, int mode, struct lwp *l)
341 struct siotty_softc *sc;
342 struct tty *tp;
343 int error;
345 sc = device_lookup_private(&siotty_cd, minor(dev));
346 if (sc == NULL)
347 return ENXIO;
348 if ((tp = sc->sc_tty) == NULL) {
349 tp = sc->sc_tty = ttymalloc();
350 tty_attach(tp);
353 tp->t_oproc = siostart;
354 tp->t_param = sioparam;
355 tp->t_hwiflow = NULL /* XXX siohwiflow XXX */;
356 tp->t_dev = dev;
358 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
359 return (EBUSY);
361 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
362 struct termios t;
364 t.c_ispeed = t.c_ospeed = TTYDEF_SPEED;
365 t.c_cflag = TTYDEF_CFLAG;
366 tp->t_ospeed = 0; /* force register update */
367 (void)sioparam(tp, &t);
368 tp->t_iflag = TTYDEF_IFLAG;
369 tp->t_oflag = TTYDEF_OFLAG;
370 tp->t_lflag = TTYDEF_LFLAG;
371 ttychars(tp);
372 ttsetwater(tp);
373 /* raise RTS and DTR here; but, DTR lead is not wired */
374 /* then check DCD condition; but, DCD lead is not wired */
375 tp->t_state |= TS_CARR_ON; /* assume detected all the time */
376 #if 0
377 if ((sc->sc_flags & TIOCFLAG_SOFTCAR)
378 || (tp->t_cflag & MDMBUF)
379 || (getsiocsr(sc->sc_ctl) & RR_DCD))
380 tp->t_state |= TS_CARR_ON;
381 else
382 tp->t_state &= ~TS_CARR_ON;
383 #endif
386 error = ttyopen(tp, 0, (flag & O_NONBLOCK));
387 if (error > 0)
388 return error;
389 return (*tp->t_linesw->l_open)(dev, tp);
393 sioclose(dev_t dev, int flag, int mode, struct lwp *l)
395 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
396 struct tty *tp = sc->sc_tty;
397 int s;
399 (*tp->t_linesw->l_close)(tp, flag);
401 s = spltty();
402 siomctl(sc, TIOCM_BREAK, DMBIC);
403 #if 0 /* because unable to feed DTR signal */
404 if ((tp->t_cflag & HUPCL)
405 || tp->t_wopen || (tp->t_state & TS_ISOPEN) == 0) {
406 siomctl(sc, TIOCM_DTR, DMBIC);
407 /* Yield CPU time to others for 1 second, then ... */
408 siomctl(sc, TIOCM_DTR, DMBIS);
410 #endif
411 splx(s);
412 return ttyclose(tp);
416 sioread(dev_t dev, struct uio *uio, int flag)
418 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
419 struct tty *tp = sc->sc_tty;
421 return (*tp->t_linesw->l_read)(tp, uio, flag);
425 siowrite(dev_t dev, struct uio *uio, int flag)
427 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
428 struct tty *tp = sc->sc_tty;
430 return (*tp->t_linesw->l_write)(tp, uio, flag);
434 siopoll(dev_t dev, int events, struct lwp *l)
436 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
437 struct tty *tp = sc->sc_tty;
439 return ((*tp->t_linesw->l_poll)(tp, events, l));
443 sioioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
445 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
446 struct tty *tp = sc->sc_tty;
447 int error;
449 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
450 if (error != EPASSTHROUGH)
451 return error;
453 error = ttioctl(tp, cmd, data, flag, l);
454 if (error != EPASSTHROUGH)
455 return error;
457 /* the last resort for TIOC ioctl tranversing */
458 switch (cmd) {
459 case TIOCSBRK: /* Set the hardware into BREAK condition */
460 siomctl(sc, TIOCM_BREAK, DMBIS);
461 break;
462 case TIOCCBRK: /* Clear the hardware BREAK condition */
463 siomctl(sc, TIOCM_BREAK, DMBIC);
464 break;
465 case TIOCSDTR: /* Assert DTR signal */
466 siomctl(sc, TIOCM_DTR|TIOCM_RTS, DMBIS);
467 break;
468 case TIOCCDTR: /* Clear DTR signal */
469 siomctl(sc, TIOCM_DTR|TIOCM_RTS, DMBIC);
470 break;
471 case TIOCMSET: /* Set modem state replacing current one */
472 siomctl(sc, *(int *)data, DMSET);
473 break;
474 case TIOCMGET: /* Return current modem state */
475 *(int *)data = siomctl(sc, 0, DMGET);
476 break;
477 case TIOCMBIS: /* Set individual bits of modem state */
478 siomctl(sc, *(int *)data, DMBIS);
479 break;
480 case TIOCMBIC: /* Clear individual bits of modem state */
481 siomctl(sc, *(int *)data, DMBIC);
482 break;
483 case TIOCSFLAGS: /* Instruct how serial port behaves */
484 sc->sc_flags = *(int *)data;
485 break;
486 case TIOCGFLAGS: /* Return current serial port state */
487 *(int *)data = sc->sc_flags;
488 break;
489 default:
490 return EPASSTHROUGH;
492 return 0;
495 /* ARSGUSED */
496 struct tty *
497 siotty(dev_t dev)
499 struct siotty_softc *sc = device_lookup_private(&siotty_cd,minor(dev));
501 return sc->sc_tty;
504 /*-------------------- miscelleneous routine --------------------*/
506 /* EXPORT */ void
507 setsioreg(struct sioreg *sio, int regno, int val)
509 if (regno != 0)
510 sio->sio_cmd = regno; /* DELAY(); */
511 sio->sio_cmd = val; /* DELAY(); */
514 /* EXPORT */ int
515 getsiocsr(struct sioreg *sio)
517 int val;
519 val = sio->sio_stat << 8; /* DELAY(); */
520 sio->sio_cmd = 1; /* DELAY(); */
521 val |= sio->sio_stat; /* DELAY(); */
522 return val;
525 /*--------------------- console interface ----------------------*/
527 void syscnattach(int);
528 int syscngetc(dev_t);
529 void syscnputc(dev_t, int);
531 struct consdev syscons = {
532 NULL,
533 NULL,
534 syscngetc,
535 syscnputc,
536 nullcnpollc,
537 NULL,
538 NULL,
539 NULL,
540 NODEV,
541 CN_REMOTE,
544 /* EXPORT */ void
545 syscnattach(int channel)
548 * Channel A is immediately initialized with 9600N1 right after cold
549 * boot/reset/poweron. ROM monitor emits one line message on CH.A.
551 struct sioreg *sio;
552 sio = (struct sioreg *)0x51000000 + channel;
554 syscons.cn_dev = makedev(cdevsw_lookup_major(&siotty_cdevsw),
555 channel);
556 cn_tab = &syscons;
558 setsioreg(sio, WR0, WR0_CHANRST);
559 setsioreg(sio, WR2A, WR2_VEC86 | WR2_INTR_1);
560 setsioreg(sio, WR2B, 0);
561 setsioreg(sio, WR0, ch0_regs[WR0]);
562 setsioreg(sio, WR4, ch0_regs[WR4]);
563 setsioreg(sio, WR3, ch0_regs[WR3]);
564 setsioreg(sio, WR5, ch0_regs[WR5]);
565 setsioreg(sio, WR0, ch0_regs[WR0]);
568 /* EXPORT */ int
569 syscngetc(dev_t dev)
571 struct sioreg *sio;
572 int s, c;
574 sio = (struct sioreg *)0x51000000 + ((int)dev & 0x1);
575 s = splhigh();
576 while ((getsiocsr(sio) & RR_RXRDY) == 0)
578 c = sio->sio_data;
579 splx(s);
581 return c;
584 /* EXPORT */ void
585 syscnputc(dev_t dev, int c)
587 struct sioreg *sio;
588 int s;
590 sio = (struct sioreg *)0x51000000 + ((int)dev & 0x1);
591 s = splhigh();
592 while ((getsiocsr(sio) & RR_TXRDY) == 0)
594 sio->sio_cmd = WR0_RSTPEND;
595 sio->sio_data = c;
596 splx(s);