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