wmcom.c revision 1.4 1 /* $NetBSD: wmcom.c,v 1.4 2014/08/10 16:44:33 tls Exp $ */
2 /*
3 * Copyright (c) 2012 KIYOHARA Takashi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: wmcom.c,v 1.4 2014/08/10 16:44:33 tls Exp $");
29
30 #include "rnd.h"
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/device.h>
36 #include <sys/errno.h>
37 #include <sys/fcntl.h>
38 #include <sys/intr.h>
39 #include <sys/kauth.h>
40 #include <sys/lwp.h>
41 #include <sys/malloc.h>
42 #include <sys/systm.h>
43 #include <sys/termios.h>
44 #include <sys/tty.h>
45 #include <sys/types.h>
46
47 #include <epoc32/windermere/windermerereg.h>
48 #include <epoc32/windermere/windermerevar.h>
49
50 #include <dev/cons.h>
51
52 #ifdef RND_COM
53 #include <sys/rnd.h>
54 #endif
55
56 #include "ioconf.h"
57 #include "locators.h"
58
59 #define COMUNIT_MASK 0x7ffff
60 #define COMDIALOUT_MASK 0x80000
61
62 #define COMUNIT(x) (minor(x) & COMUNIT_MASK)
63 #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK)
64
65 #define WMCOM_RING_SIZE 2048
66
67 struct wmcom_softc {
68 device_t sc_dev;
69 bus_space_tag_t sc_iot;
70 bus_space_handle_t sc_ioh;
71
72 void *sc_si;
73
74 struct tty *sc_tty;
75
76 u_char *sc_tba;
77 u_int sc_tbc;
78 u_char *sc_rbuf;
79 char *volatile sc_rbget;
80 char *volatile sc_rbput;
81 volatile int sc_rbavail;
82
83 int sc_tx_done;
84 int sc_rx_ready;
85
86 int sc_hwflags;
87 #define COM_HW_CONSOLE (1 << 0)
88 #define COM_HW_DEV_OK (1 << 1)
89 #define COM_HW_KGDB (1 << 2)
90 int sc_swflags;
91
92 int sc_flags;
93 #define WMCOM_IRDA (1 << 0)
94
95 #ifdef RND_COM
96 krandsource_t rnd_source;
97 #endif
98 };
99
100 static int wmcom_match(device_t, cfdata_t, void *);
101 static void wmcom_attach(device_t, device_t, void *);
102
103 static int wmcom_intr(void *);
104 static void wmcom_soft(void *);
105
106 static void wmcom_start(struct tty *);
107 static int wmcom_param(struct tty *, struct termios *);
108 static int wmcom_hwiflow(struct tty *, int);
109
110 dev_type_open(wmcomopen);
111 dev_type_close(wmcomclose);
112 dev_type_read(wmcomread);
113 dev_type_write(wmcomwrite);
114 dev_type_ioctl(wmcomioctl);
115 dev_type_stop(wmcomstop);
116 dev_type_tty(wmcomtty);
117 dev_type_poll(wmcompoll);
118
119 static void wmcom_iflush(struct wmcom_softc *);
120 static void wmcom_shutdown(struct wmcom_softc *);
121 static void wmcom_break(struct wmcom_softc *, int);
122
123 static void wmcom_rxsoft(struct wmcom_softc *, struct tty *);
124
125 static inline uint32_t wmcom_rate2lcr(int);
126 static uint8_t wmcom_cflag2fcr(tcflag_t);
127
128 static int wmcom_cngetc(dev_t);
129 static void wmcom_cnputc(dev_t, int);
130 static void wmcom_cnpollc(dev_t, int);
131
132 CFATTACH_DECL_NEW(wmcom, sizeof(struct wmcom_softc),
133 wmcom_match, wmcom_attach, NULL, NULL);
134
135 const struct cdevsw wmcom_cdevsw = {
136 .d_open = wmcomopen,
137 .d_close = wmcomclose,
138 .d_read = wmcomread,
139 .d_write = wmcomwrite,
140 .d_ioctl = wmcomioctl,
141 .d_stop = wmcomstop,
142 .d_tty = wmcomtty,
143 .d_poll = wmcompoll,
144 .d_mmap = nommap,
145 .d_kqfilter = ttykqfilter,
146 .d_discard = nodiscard,
147 .d_flag = D_TTY
148 };
149
150 static struct cnm_state wmcom_cnm_state;
151 static vaddr_t wmcom_cnaddr;
152 static int wmcom_cnrate;
153 static tcflag_t wmcom_cncflag;
154
155
156 /* ARGSUSED */
157 static int
158 wmcom_match(device_t parent, cfdata_t match, void *aux)
159 {
160 struct windermere_attach_args *aa = aux;
161
162 /* Wildcard not accept */
163 if (aa->aa_offset == WINDERMERECF_OFFSET_DEFAULT ||
164 aa->aa_irq == WINDERMERECF_IRQ_DEFAULT)
165 return 0;
166
167 aa->aa_size = UART_SIZE;
168 return 1;
169 }
170
171 /* ARGSUSED */
172 static void
173 wmcom_attach(device_t parent, device_t self, void *aux)
174 {
175 struct wmcom_softc *sc = device_private(self);
176 struct windermere_attach_args *aa = aux;
177
178 aprint_naive("\n");
179 aprint_normal("\n");
180
181 sc->sc_dev = self;
182 if (windermere_bus_space_subregion(aa->aa_iot, *aa->aa_ioh,
183 aa->aa_offset, aa->aa_size, &sc->sc_ioh) != 0) {
184 aprint_error_dev(self, "can't map registers\n");
185 return;
186 }
187 sc->sc_iot = aa->aa_iot;
188 if (intr_establish(aa->aa_irq, IPL_SERIAL, 0, wmcom_intr, sc) == NULL) {
189 aprint_error_dev(self, "can't establish interrupt\n");
190 return;
191 }
192
193 if (aa->aa_offset == (wmcom_cnaddr & 0xfff))
194 SET(sc->sc_hwflags, COM_HW_CONSOLE);
195
196 if (aa->aa_offset == WINDERMERE_COM0_OFFSET)
197 SET(sc->sc_flags, WMCOM_IRDA);
198
199 sc->sc_tty = tty_alloc();
200 sc->sc_tty->t_oproc = wmcom_start;
201 sc->sc_tty->t_param = wmcom_param;
202 sc->sc_tty->t_hwiflow = wmcom_hwiflow;
203
204 sc->sc_tbc = 0;
205 sc->sc_rbuf = malloc(WMCOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
206 if (sc->sc_rbuf == NULL) {
207 aprint_error_dev(self, "unable to allocate ring buffer\n");
208 return;
209 }
210 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
211 sc->sc_rbavail = WMCOM_RING_SIZE;
212
213 tty_attach(sc->sc_tty);
214
215 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
216 int maj = cdevsw_lookup_major(&wmcom_cdevsw);
217
218 sc->sc_tty->t_dev = makedev(maj, device_unit(sc->sc_dev));
219 cn_tab->cn_dev = sc->sc_tty->t_dev;
220
221 aprint_normal_dev(self, "console\n");
222 }
223
224 sc->sc_si = softint_establish(SOFTINT_SERIAL, wmcom_soft, sc);
225
226 #ifdef RND_COM
227 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
228 RND_TYPE_TTY, RND_FLAG_DEFAULT);
229 #endif
230
231 SET(sc->sc_hwflags, COM_HW_DEV_OK);
232 }
233
234 static int
235 wmcom_intr(void *arg)
236 {
237 struct wmcom_softc *sc = arg;
238 bus_space_tag_t iot = sc->sc_iot;
239 bus_space_handle_t ioh = sc->sc_ioh;
240 int cc;
241 uint32_t data;
242 uint8_t fr, intm;
243 u_char *put;
244
245 if (!device_is_active(sc->sc_dev))
246 return 0;
247
248 fr = bus_space_read_1(iot, ioh, UARTFR);
249 intm = bus_space_read_1(iot, ioh, UARTINTM);
250 if (bus_space_read_1(iot, ioh, UARTINT) & INT_RXINT) {
251 put = sc->sc_rbput;
252 cc = sc->sc_rbavail;
253 while (cc > 0) {
254 if (ISSET(fr, FR_RXFE))
255 break;
256 data = bus_space_read_4(iot, ioh, UARTDR);
257 cn_check_magic(sc->sc_tty->t_dev, data & 0xff,
258 wmcom_cnm_state);
259
260 put[0] = data & 0xff;
261 put[1] = (data >> 8) & 0xff;
262 put += 2;
263 if (put >= sc->sc_rbuf + (WMCOM_RING_SIZE << 1))
264 put = sc->sc_rbuf;
265 cc--;
266 sc->sc_rx_ready = 1;
267
268 fr = bus_space_read_1(iot, ioh, UARTFR);
269 }
270
271 /*
272 * Current string of incoming characters ended because
273 * no more data was available or we ran out of space.
274 * Schedule a receive event if any data was received.
275 * If we're out of space, turn off receive interrupts.
276 */
277 sc->sc_rbput = put;
278 sc->sc_rbavail = cc;
279
280 /*
281 * See if we are in danger of overflowing a buffer. If
282 * so, use hardware flow control to ease the pressure.
283 */
284
285 /* but wmcom cannot. X-( */
286
287 /*
288 * If we're out of space, disable receive interrupts
289 * until the queue has drained a bit.
290 */
291 if (cc <= 0)
292 CLR(intm, INT_RXINT);
293 }
294
295 /*
296 * Done handling any receive interrupts. See if data can be
297 * transmitted as well. Schedule tx done event if no data left
298 * and tty was marked busy.
299 */
300
301 if (!ISSET(fr, FR_TXFF)) {
302 /* Output the next chunk of the contiguous buffer, if any. */
303 if (sc->sc_tbc > 0) {
304 while (sc->sc_tbc > 0 && !ISSET(fr, FR_TXFF)) {
305 bus_space_write_1(iot, ioh, UARTDR,
306 *sc->sc_tba);
307 sc->sc_tba++;
308 sc->sc_tbc--;
309 fr = bus_space_read_1(iot, ioh, UARTFR);
310 }
311 } else if (!ISSET(fr, FR_BUSY) && ISSET(intm, INT_TXINT)) {
312 CLR(intm, INT_TXINT);
313 sc->sc_tx_done = 1;
314 }
315 }
316
317 bus_space_write_1(iot, ioh, UARTINTM, intm);
318
319 /* Wake up the poller. */
320 softint_schedule(sc->sc_si);
321
322 return 1;
323 }
324
325 static void
326 wmcom_soft(void *arg)
327 {
328 struct wmcom_softc *sc = arg;
329 struct tty *tp = sc->sc_tty;
330
331 if (!device_is_active(sc->sc_dev))
332 return;
333
334 if (sc->sc_rx_ready) {
335 sc->sc_rx_ready = 0;
336 wmcom_rxsoft(sc, tp);
337 }
338 if (sc->sc_tx_done) {
339 sc->sc_tx_done = 0;
340 CLR(tp->t_state, TS_BUSY);
341 if (ISSET(tp->t_state, TS_FLUSH))
342 CLR(tp->t_state, TS_FLUSH);
343 else
344 ndflush(&tp->t_outq,
345 (int)(sc->sc_tba - tp->t_outq.c_cf));
346 (*tp->t_linesw->l_start)(tp);
347 }
348 }
349
350 static void
351 wmcom_start(struct tty *tp)
352 {
353 struct wmcom_softc *sc
354 = device_lookup_private(&wmcom_cd, COMUNIT(tp->t_dev));
355 bus_space_tag_t iot = sc->sc_iot;
356 bus_space_handle_t ioh = sc->sc_ioh;
357 int s, n;
358 uint8_t intm;
359
360 if (!device_is_active(sc->sc_dev))
361 return;
362
363 s = spltty();
364 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
365 goto out;
366 if (!ttypull(tp))
367 goto out;
368
369 /* Grab the first contiguous region of buffer space. */
370 {
371 u_char *tba;
372 int tbc;
373
374 tba = tp->t_outq.c_cf;
375 tbc = ndqb(&tp->t_outq, 0);
376
377 (void)splserial();
378
379 sc->sc_tba = tba;
380 sc->sc_tbc = tbc;
381 }
382
383 SET(tp->t_state, TS_BUSY);
384
385 intm = bus_space_read_1(iot, ioh, UARTINTM);
386 if (!ISSET(intm, INT_TXINT)) {
387 bus_space_write_1(iot, ioh, UARTINTM, intm | INT_TXINT);
388
389 /* Output the first chunk of the contiguous buffer. */
390 n = min(sc->sc_tbc, UART_FIFO_SIZE);
391 bus_space_write_multi_1(iot, ioh, UARTDR, sc->sc_tba, n);
392 sc->sc_tba += n;
393 sc->sc_tbc -= n;
394 }
395 out:
396 splx(s);
397 return;
398 }
399
400 static int
401 wmcom_param(struct tty *tp, struct termios *t)
402 {
403 struct wmcom_softc *sc =
404 device_lookup_private(&wmcom_cd, COMUNIT(tp->t_dev));
405 bus_space_tag_t iot = sc->sc_iot;
406 bus_space_handle_t ioh = sc->sc_ioh;
407 int s;
408
409 if (!device_is_active(sc->sc_dev))
410 return ENXIO;
411 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
412 return EINVAL;
413
414 /*
415 * For the console, always force CLOCAL and !HUPCL, so that the port
416 * is always active.
417 */
418 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
419 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
420 SET(t->c_cflag, CLOCAL);
421 CLR(t->c_cflag, HUPCL);
422 }
423
424 /*
425 * If there were no changes, don't do anything. This avoids dropping
426 * input and improves performance when all we did was frob things like
427 * VMIN and VTIME.
428 */
429 if (tp->t_ospeed == t->c_ospeed &&
430 tp->t_cflag == t->c_cflag)
431 return 0;
432
433 s = splserial();
434 bus_space_write_4(iot, ioh, UARTLCR, wmcom_rate2lcr(t->c_ospeed));
435 bus_space_write_1(iot, ioh, UARTFCR, wmcom_cflag2fcr(t->c_cflag));
436
437 /* And copy to tty. */
438 tp->t_ispeed = 0;
439 tp->t_ospeed = t->c_ospeed;
440 tp->t_cflag = t->c_cflag;
441 splx(s);
442
443 /*
444 * Update the tty layer's idea of the carrier bit.
445 * We tell tty the carrier is always on.
446 */
447 (*tp->t_linesw->l_modem)(tp, 1);
448
449 return 0;
450 }
451
452 static int
453 wmcom_hwiflow(struct tty *tp, int block)
454 {
455 /* Nothing */
456 return 0;
457 }
458
459 /* ARGSUSED */
460 int
461 wmcomopen(dev_t dev, int flag, int mode, struct lwp *l)
462 {
463 struct wmcom_softc *sc;
464 struct tty *tp;
465 int error, s, s2;
466 uint8_t con;
467
468 sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
469 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
470 return ENXIO;
471 if (!device_is_active(sc->sc_dev))
472 return ENXIO;
473
474 #ifdef KGDB
475 /*
476 * If this is the kgdb port, no other use is permitted.
477 */
478 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
479 return EBUSY;
480 #endif
481
482 tp = sc->sc_tty;
483
484 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
485 return EBUSY;
486
487 s = spltty();
488
489 /*
490 * Do the following iff this is a first open.
491 */
492 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
493 struct termios t;
494
495 tp->t_dev = dev;
496
497 /* Enable and turn on interrupt */
498 con = CON_UARTEN;
499 if (ISSET(sc->sc_flags, WMCOM_IRDA))
500 con |= CON_IRTXM;
501 bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTCON, con);
502 bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTINTM, INT_RXINT);
503
504 /*
505 * Initialize the termios status to the defaults. Add in the
506 * sticky bits from TIOCSFLAGS.
507 */
508 t.c_ispeed = 0;
509 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
510 t.c_ospeed = wmcom_cnrate;
511 t.c_cflag = wmcom_cncflag;
512 } else {
513 t.c_ospeed = TTYDEF_SPEED;
514 t.c_cflag = TTYDEF_CFLAG;
515 }
516 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
517 SET(t.c_cflag, CLOCAL);
518 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
519 SET(t.c_cflag, CRTSCTS);
520 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
521 SET(t.c_cflag, MDMBUF);
522 /* Make sure wmcom_param() we do something */
523 tp->t_ospeed = 0;
524 wmcom_param(tp, &t);
525 tp->t_iflag = TTYDEF_IFLAG;
526 tp->t_oflag = TTYDEF_OFLAG;
527 tp->t_lflag = TTYDEF_LFLAG;
528 ttychars(tp);
529 ttsetwater(tp);
530
531 s2 = splserial();
532
533 /* Clear the input ring. */
534 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
535 sc->sc_rbavail = WMCOM_RING_SIZE;
536 wmcom_iflush(sc);
537
538 splx(s2);
539 }
540
541 splx(s);
542
543 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
544 if (error)
545 goto bad;
546
547 error = (*tp->t_linesw->l_open)(dev, tp);
548 if (error)
549 goto bad;
550 return 0;
551
552 bad:
553 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
554 /*
555 * We failed to open the device, and nobody else had it opened.
556 * Clean up the state as appropriate.
557 */
558 wmcom_shutdown(sc);
559
560 /* Disable UART */
561 if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
562 bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTCON, 0);
563 }
564
565 return error;
566 }
567
568 /* ARGSUSED */
569 int
570 wmcomclose(dev_t dev, int flag, int mode, struct lwp *l)
571 {
572 struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
573 struct tty *tp = sc->sc_tty;
574
575 /* XXXX This is for cons.c. */
576 if (!ISSET(tp->t_state, TS_ISOPEN))
577 return 0;
578
579 (*tp->t_linesw->l_close)(tp, flag);
580 ttyclose(tp);
581
582 if (!device_is_active(sc->sc_dev))
583 return 0;
584
585 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
586 /*
587 * Although we got a last close, the device may still be in
588 * use; e.g. if this was the dialout node, and there are still
589 * processes waiting for carrier on the non-dialout node.
590 */
591 wmcom_shutdown(sc);
592
593 /* Disable UART */
594 if (!ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
595 bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTCON, 0);
596 }
597
598 return 0;
599 }
600
601 int
602 wmcomread(dev_t dev, struct uio *uio, int flag)
603 {
604 struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
605 struct tty *tp = sc->sc_tty;
606
607 if (!device_is_active(sc->sc_dev))
608 return EIO;
609
610 return (*tp->t_linesw->l_read)(tp, uio, flag);
611 }
612
613 int
614 wmcomwrite(dev_t dev, struct uio *uio, int flag)
615 {
616 struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
617 struct tty *tp = sc->sc_tty;
618
619 if (!device_is_active(sc->sc_dev))
620 return EIO;
621
622 return (*tp->t_linesw->l_write)(tp, uio, flag);
623 }
624
625 int
626 wmcomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
627 {
628 struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
629 struct tty *tp = sc->sc_tty;
630 int error, s;
631
632 if (!device_is_active(sc->sc_dev))
633 return EIO;
634
635 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
636 if (error != EPASSTHROUGH)
637 return error;
638
639 error = ttioctl(tp, cmd, data, flag, l);
640 if (error != EPASSTHROUGH)
641 return error;
642
643 switch (cmd) {
644 case TIOCSFLAGS:
645 error = kauth_authorize_device_tty(l->l_cred,
646 KAUTH_DEVICE_TTY_PRIVSET, tp);
647 break;
648 default:
649 break;
650 }
651 if (error)
652 return error;
653
654 s = splserial();
655 error = 0;
656 switch (cmd) {
657 case TIOCSBRK:
658 wmcom_break(sc, 1);
659 break;
660
661 case TIOCCBRK:
662 wmcom_break(sc, 0);
663 break;
664
665 case TIOCGFLAGS:
666 *(int *)data = sc->sc_swflags;
667 break;
668
669 case TIOCSFLAGS:
670 sc->sc_swflags = *(int *)data;
671 break;
672
673 default:
674 error = EPASSTHROUGH;
675 break;
676 }
677 splx(s);
678 return error;
679 }
680
681 int
682 wmcompoll(dev_t dev, int events, struct lwp *l)
683 {
684 struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
685 struct tty *tp = sc->sc_tty;
686
687 if (!device_is_active(sc->sc_dev))
688 return EIO;
689
690 return (*tp->t_linesw->l_poll)(tp, events, l);
691 }
692
693 struct tty *
694 wmcomtty(dev_t dev)
695 {
696 struct wmcom_softc *sc = device_lookup_private(&wmcom_cd, COMUNIT(dev));
697
698 return sc->sc_tty;
699 }
700
701 void
702 wmcomstop(struct tty *tp, int flag)
703 {
704 int s;
705
706 s = splserial();
707 if (ISSET(tp->t_state, TS_BUSY)) {
708 /* Stop transmitting at the next chunk. */
709 if (!ISSET(tp->t_state, TS_TTSTOP))
710 SET(tp->t_state, TS_FLUSH);
711 }
712 splx(s);
713 }
714
715
716 static void
717 wmcom_iflush(struct wmcom_softc *sc)
718 {
719 bus_space_tag_t iot = sc->sc_iot;
720 bus_space_handle_t ioh = sc->sc_ioh;
721 int timo;
722
723 timo = 50000;
724 while ((bus_space_read_1(iot, ioh, UARTFR) & FR_RXFE) == 0 &&
725 timo--)
726 bus_space_read_1(iot, ioh, UARTDR);
727 if (timo == 0)
728 printf("%s: iflush timeout\n", device_xname(sc->sc_dev));
729 }
730
731 static void
732 wmcom_shutdown(struct wmcom_softc *sc)
733 {
734 int s;
735
736 s = splserial();
737
738 /* Turn off interrupt */
739 bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTINTM, 0);
740
741 /* Clear any break condition set with TIOCSBRK. */
742 wmcom_break(sc, 0);
743
744 splx(s);
745 }
746
747 static void
748 wmcom_break(struct wmcom_softc *sc, int onoff)
749 {
750 int s;
751 uint8_t fcr;
752
753 s = splserial();
754 fcr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, UARTFCR);
755 if (onoff)
756 SET(fcr, FCR_BREAK);
757 else
758 CLR(fcr, FCR_BREAK);
759 bus_space_write_1(sc->sc_iot, sc->sc_ioh, UARTFCR, fcr);
760 splx(s);
761 }
762
763 static void
764 wmcom_rxsoft(struct wmcom_softc *sc, struct tty *tp)
765 {
766 bus_space_tag_t iot = sc->sc_iot;
767 bus_space_handle_t ioh = sc->sc_ioh;
768 int code, s;
769 u_int cc, scc;
770 uint8_t intm;
771 u_char sts, *get;
772
773 get = sc->sc_rbget;
774 scc = cc = WMCOM_RING_SIZE - sc->sc_rbavail;
775 while (cc) {
776 code = get[0];
777 sts = get[1];
778 if (ISSET(sts, RSR_FE | RSR_PE | RSR_OE)) {
779 if (ISSET(sts, (RSR_FE)))
780 SET(code, TTY_FE);
781 if (ISSET(sts, RSR_PE))
782 SET(code, TTY_PE);
783 if (ISSET(sts, RSR_OE))
784 ; /* XXXXX: Overrun */
785 }
786 if ((*tp->t_linesw->l_rint)(code, tp) == -1) {
787 /*
788 * The line discipline's buffer is out of space.
789 */
790 /*
791 * We're either not using flow control, or the
792 * line discipline didn't tell us to block for
793 * some reason. Either way, we have no way to
794 * know when there's more space available, so
795 * just drop the rest of the data.
796 */
797 get += cc << 1;
798 if (get >= sc->sc_rbuf + (WMCOM_RING_SIZE << 1))
799 get -= (WMCOM_RING_SIZE << 1);
800 cc = 0;
801 break;
802 }
803 get += 2;
804 if (get >= sc->sc_rbuf + (WMCOM_RING_SIZE << 1))
805 get = sc->sc_rbuf;
806 cc--;
807 }
808
809 if (cc != scc) {
810 sc->sc_rbget = get;
811 s = splserial();
812
813 cc = sc->sc_rbavail += scc - cc;
814 /* Buffers should be ok again, release possible block. */
815 if (cc >= 1) {
816 intm = bus_space_read_1(iot, ioh, UARTINTM);
817 SET(intm, INT_RXINT);
818 bus_space_write_1(iot, ioh, UARTINTM, intm);
819 }
820 splx(s);
821 }
822 }
823
824 static inline uint32_t
825 wmcom_rate2lcr(int rate)
826 {
827
828 return 7372800 / (16 * rate) - 1;
829 }
830
831 static uint8_t
832 wmcom_cflag2fcr(tcflag_t cflag)
833 {
834 int8_t fcr = FCR_UFIFOEN;
835
836 switch (cflag & CSIZE) {
837 case CS5: SET(fcr, FCR_WLEN_5); break;
838 case CS6: SET(fcr, FCR_WLEN_6); break;
839 case CS7: SET(fcr, FCR_WLEN_7); break;
840 case CS8: SET(fcr, FCR_WLEN_8); break;
841 default: SET(fcr, FCR_WLEN_8); break;
842 }
843 if (cflag & CSTOPB)
844 SET(fcr, FCR_XSTOP);
845 if (cflag & PARENB) {
846 SET(fcr, (FCR_PRTEN | FCR_EVENPRT));
847 if (cflag & PARODD)
848 CLR(fcr, FCR_EVENPRT);
849 }
850 return fcr;
851 }
852
853 #define WMCOM_CNREAD_1(offset) \
854 (*(volatile uint8_t *)(wmcom_cnaddr + ((offset) << 2)))
855 #define WMCOM_CNREAD_4(offset) \
856 (*(volatile uint32_t *)(wmcom_cnaddr + ((offset) << 2)))
857 #define WMCOM_CNWRITE_1(offset, val) \
858 (*(volatile uint8_t *)(wmcom_cnaddr + ((offset) << 2)) = val)
859 #define WMCOM_CNWRITE_4(offset, val) \
860 (*(volatile uint32_t *)(wmcom_cnaddr + ((offset) << 2)) = val)
861
862 static struct consdev wmcomcons = {
863 NULL, NULL, wmcom_cngetc, wmcom_cnputc, wmcom_cnpollc, NULL, NULL, NULL,
864 NODEV, CN_NORMAL
865 };
866
867 int
868 wmcom_cnattach(vaddr_t addr, int rate, tcflag_t cflag, int irda)
869 {
870
871 wmcom_cnaddr = addr;
872 wmcom_cnrate = rate;
873 wmcom_cncflag = cflag;
874 WMCOM_CNWRITE_4(UARTLCR, wmcom_rate2lcr(rate));
875 WMCOM_CNWRITE_1(UARTFCR, wmcom_cflag2fcr(cflag));
876 if (irda)
877 WMCOM_CNWRITE_1(UARTCON, CON_UARTEN | CON_IRTXM);
878 else
879 WMCOM_CNWRITE_1(UARTCON, CON_UARTEN);
880
881 cn_tab = &wmcomcons;
882 cn_init_magic(&wmcom_cnm_state);
883 cn_set_magic("\047\001"); /* default magic is BREAK */
884
885 return 0;
886 }
887
888 /* ARGSUSED */
889 static int
890 wmcom_cngetc(dev_t dev)
891 {
892 int s = splserial();
893 char ch;
894
895 while (WMCOM_CNREAD_1(UARTFR) & FR_RXFE);
896
897 ch = WMCOM_CNREAD_4(UARTDR);
898
899 {
900 #ifdef DDB
901 extern int db_active;
902 if (!db_active)
903 #endif
904 cn_check_magic(dev, ch, wmcom_cnm_state);
905 }
906
907 splx(s);
908 return ch;
909 }
910
911 /* ARGSUSED */
912 static void
913 wmcom_cnputc(dev_t dev, int c)
914 {
915 int s = splserial();
916
917 while (WMCOM_CNREAD_1(UARTFR) & FR_TXFF);
918
919 WMCOM_CNWRITE_1(UARTDR, c);
920
921 /* Make sure output. */
922 while (WMCOM_CNREAD_1(UARTFR) & FR_BUSY);
923
924 splx(s);
925 }
926
927 /* ARGSUSED */
928 static void
929 wmcom_cnpollc(dev_t dev, int on)
930 {
931 /* Nothing */
932 }
933