txcom.c revision 1.41 1 /* $NetBSD: txcom.c,v 1.41 2009/03/14 15:36:07 dsl Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
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 *
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.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: txcom.c,v 1.41 2009/03/14 15:36:07 dsl Exp $");
34
35 #include "opt_tx39uart_debug.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/malloc.h>
42 #include <sys/kauth.h>
43
44 #include <sys/proc.h> /* tsleep/wakeup */
45
46 #include <sys/ioctl.h>
47 #include <sys/select.h>
48 #include <sys/file.h>
49
50 #include <sys/tty.h>
51 #include <sys/conf.h>
52 #include <dev/cons.h> /* consdev */
53
54 #include <machine/bus.h>
55 #include <machine/config_hook.h>
56
57 #include <hpcmips/tx/tx39var.h>
58 #include <hpcmips/tx/tx39icureg.h>
59 #include <hpcmips/tx/tx39uartvar.h>
60 #include <hpcmips/tx/tx39uartreg.h>
61
62 #include <hpcmips/tx/tx39irvar.h>
63
64 #include <hpcmips/tx/tx39clockreg.h> /* XXX */
65
66 /*
67 * UARTA channel has DTR, DSR, RTS, CTS lines. and they wired to MFIO/IO port.
68 */
69 #define IS_COM0(s) ((s) == 0)
70 #define IS_COM1(s) ((s) == 1)
71 #define ON ((void *)1)
72 #define OFF ((void *)0)
73
74 #ifdef TX39UART_DEBUG
75 #define DPRINTF_ENABLE
76 #define DPRINTF_DEBUG tx39uart_debug
77 #endif
78 #include <machine/debug.h>
79
80 #define TXCOM_HW_CONSOLE 0x40
81 #define TXCOM_RING_SIZE 256 /* must be a power of two! */
82 #define TXCOM_RING_MASK (TXCOM_RING_SIZE - 1)
83
84 struct txcom_chip {
85 tx_chipset_tag_t sc_tc;
86 int sc_slot; /* UARTA or UARTB */
87 int sc_cflag;
88 int sc_speed;
89 int sc_swflags;
90 int sc_hwflags;
91
92 int sc_dcd;
93 int sc_msr_cts;
94 int sc_tx_stopped;
95 };
96
97 struct txcom_softc {
98 struct device sc_dev;
99 struct tty *sc_tty;
100 struct txcom_chip *sc_chip;
101
102 struct callout sc_txsoft_ch;
103 struct callout sc_rxsoft_ch;
104
105 u_int8_t *sc_tba; /* transmit buffer address */
106 int sc_tbc; /* transmit byte count */
107 int sc_heldtbc;
108 u_int8_t *sc_rbuf; /* receive buffer address */
109 int sc_rbput; /* receive byte count */
110 int sc_rbget;
111 };
112
113 extern struct cfdriver txcom_cd;
114
115 int txcom_match(struct device *, struct cfdata *, void *);
116 void txcom_attach(struct device *, struct device *, void *);
117 int txcom_print(void*, const char *);
118
119 int txcom_txintr(void *);
120 int txcom_rxintr(void *);
121 int txcom_frameerr_intr(void *);
122 int txcom_parityerr_intr(void *);
123 int txcom_break_intr(void *);
124
125 void txcom_rxsoft(void *);
126 void txcom_txsoft(void *);
127
128 int txcom_stsoft(void *);
129 int txcom_stsoft2(void *);
130 int txcom_stsoft3(void *);
131 int txcom_stsoft4(void *);
132
133
134 void txcom_shutdown(struct txcom_softc *);
135 void txcom_break(struct txcom_softc *, int);
136 void txcom_modem(struct txcom_softc *, int);
137 void txcomstart(struct tty *);
138 int txcomparam(struct tty *, struct termios *);
139
140 void txcom_reset (struct txcom_chip *);
141 int txcom_enable (struct txcom_chip *, bool);
142 void txcom_disable (struct txcom_chip *);
143 void txcom_setmode (struct txcom_chip *);
144 void txcom_setbaudrate(struct txcom_chip *);
145 int txcom_cngetc (dev_t);
146 void txcom_cnputc (dev_t, int);
147 void txcom_cnpollc (dev_t, int);
148
149 int txcom_dcd_hook(void *, int, long, void *);
150 int txcom_cts_hook(void *, int, long, void *);
151
152
153 inline int __txcom_txbufready(struct txcom_chip *, int);
154 const char *__txcom_slotname(int);
155
156 #ifdef TX39UARTDEBUG
157 void txcom_dump(struct txcom_chip *);
158 #endif
159
160 struct consdev txcomcons = {
161 NULL, NULL, txcom_cngetc, txcom_cnputc, txcom_cnpollc, NULL, NULL,
162 NULL, NODEV, CN_NORMAL
163 };
164
165 /* Serial console */
166 struct txcom_chip txcom_chip;
167
168 CFATTACH_DECL(txcom, sizeof(struct txcom_softc),
169 txcom_match, txcom_attach, NULL, NULL);
170
171 dev_type_open(txcomopen);
172 dev_type_close(txcomclose);
173 dev_type_read(txcomread);
174 dev_type_write(txcomwrite);
175 dev_type_ioctl(txcomioctl);
176 dev_type_stop(txcomstop);
177 dev_type_tty(txcomtty);
178 dev_type_poll(txcompoll);
179
180 const struct cdevsw txcom_cdevsw = {
181 txcomopen, txcomclose, txcomread, txcomwrite, txcomioctl,
182 txcomstop, txcomtty, txcompoll, nommap, ttykqfilter, D_TTY
183 };
184
185 int
186 txcom_match(struct device *parent, struct cfdata *cf, void *aux)
187 {
188 /* if the autoconfiguration got this far, there's a slot here */
189 return 1;
190 }
191
192 void
193 txcom_attach(struct device *parent, struct device *self, void *aux)
194 {
195 struct tx39uart_attach_args *ua = aux;
196 struct txcom_softc *sc = (void*)self;
197 tx_chipset_tag_t tc;
198 struct tty *tp;
199 struct txcom_chip *chip;
200 int slot, console;
201
202 /* Check this slot used as serial console */
203 console = (ua->ua_slot == txcom_chip.sc_slot) &&
204 (txcom_chip.sc_hwflags & TXCOM_HW_CONSOLE);
205
206 if (console) {
207 sc->sc_chip = &txcom_chip;
208 } else {
209 if (!(sc->sc_chip = malloc(sizeof(struct txcom_chip),
210 M_DEVBUF, M_WAITOK))) {
211 printf(": can't allocate chip\n");
212 return;
213 }
214 memset(sc->sc_chip, 0, sizeof(struct txcom_chip));
215 }
216
217 chip = sc->sc_chip;
218 tc = chip->sc_tc = ua->ua_tc;
219 slot = chip->sc_slot = ua->ua_slot;
220
221 #ifdef TX39UARTDEBUG
222 txcom_dump(chip);
223 #endif
224 if (!console)
225 txcom_reset(chip);
226
227 if (!(sc->sc_rbuf = malloc(TXCOM_RING_SIZE, M_DEVBUF, M_WAITOK))) {
228 printf(": can't allocate buffer.\n");
229 return;
230 }
231 memset(sc->sc_rbuf, 0, TXCOM_RING_SIZE);
232
233 tp = ttymalloc();
234 tp->t_oproc = txcomstart;
235 tp->t_param = txcomparam;
236 tp->t_hwiflow = NULL;
237 sc->sc_tty = tp;
238 tty_attach(tp);
239
240 if (ISSET(chip->sc_hwflags, TXCOM_HW_CONSOLE)) {
241 int maj;
242 /* locate the major number */
243 maj = cdevsw_lookup_major(&txcom_cdevsw);
244
245 cn_tab->cn_dev = makedev(maj, device_unit(&sc->sc_dev));
246
247 printf(": console");
248 }
249
250 printf("\n");
251
252 /*
253 * Enable interrupt
254 */
255 #define TXCOMINTR(i, s) MAKEINTR(2, TX39_INTRSTATUS2_UART##i##INT(s))
256
257 tx_intr_establish(tc, TXCOMINTR(RX, slot), IST_EDGE, IPL_TTY,
258 txcom_rxintr, sc);
259 tx_intr_establish(tc, TXCOMINTR(TX, slot), IST_EDGE, IPL_TTY,
260 txcom_txintr, sc);
261 tx_intr_establish(tc, TXCOMINTR(RXOVERRUN, slot), IST_EDGE, IPL_TTY,
262 txcom_rxintr, sc);
263 tx_intr_establish(tc, TXCOMINTR(TXOVERRUN, slot), IST_EDGE, IPL_TTY,
264 txcom_txintr, sc);
265 tx_intr_establish(tc, TXCOMINTR(FRAMEERR, slot), IST_EDGE, IPL_TTY,
266 txcom_frameerr_intr, sc);
267 tx_intr_establish(tc, TXCOMINTR(PARITYERR, slot), IST_EDGE, IPL_TTY,
268 txcom_parityerr_intr, sc);
269 tx_intr_establish(tc, TXCOMINTR(BREAK, slot), IST_EDGE, IPL_TTY,
270 txcom_break_intr, sc);
271
272 /*
273 * UARTA has external signal line. (its wiring is platform dependent)
274 */
275 if (IS_COM0(slot)) {
276 /* install DCD, CTS hooks. */
277 config_hook(CONFIG_HOOK_EVENT, CONFIG_HOOK_COM0_DCD,
278 CONFIG_HOOK_EXCLUSIVE, txcom_dcd_hook, sc);
279 config_hook(CONFIG_HOOK_EVENT, CONFIG_HOOK_COM0_CTS,
280 CONFIG_HOOK_EXCLUSIVE, txcom_cts_hook, sc);
281 }
282
283 /*
284 * UARTB can connect IR module
285 */
286 if (IS_COM1(slot)) {
287 struct txcom_attach_args tca;
288 tca.tca_tc = tc;
289 tca.tca_parent = self;
290 config_found(self, &tca, txcom_print);
291 }
292 }
293
294 int
295 txcom_print(void *aux, const char *pnp)
296 {
297 return pnp ? QUIET : UNCONF;
298 }
299
300 void
301 txcom_reset(struct txcom_chip *chip)
302 {
303 tx_chipset_tag_t tc;
304 int slot, ofs;
305 txreg_t reg;
306
307 tc = chip->sc_tc;
308 slot = chip->sc_slot;
309 ofs = TX39_UARTCTRL1_REG(slot);
310
311 /* Supply clock */
312 reg = tx_conf_read(tc, TX39_CLOCKCTRL_REG);
313 reg |= (slot ? TX39_CLOCK_ENUARTBCLK : TX39_CLOCK_ENUARTACLK);
314 tx_conf_write(tc, TX39_CLOCKCTRL_REG, reg);
315
316 /* reset UART module */
317 tx_conf_write(tc, ofs, 0);
318 }
319
320 int
321 txcom_enable(struct txcom_chip *chip, bool console)
322 {
323 tx_chipset_tag_t tc;
324 txreg_t reg;
325 int slot, ofs, timeout;
326
327 tc = chip->sc_tc;
328 slot = chip->sc_slot;
329 ofs = TX39_UARTCTRL1_REG(slot);
330
331 /*
332 * External power supply (if any)
333 * When serial console, Windows CE already powered on it.
334 */
335 if (!console) {
336 config_hook_call(CONFIG_HOOK_POWERCONTROL,
337 CONFIG_HOOK_POWERCONTROL_COM0, PWCTL_ON);
338 delay(3);
339 }
340
341 /* Supply clock */
342 reg = tx_conf_read(tc, TX39_CLOCKCTRL_REG);
343 reg |= (slot ? TX39_CLOCK_ENUARTBCLK : TX39_CLOCK_ENUARTACLK);
344 tx_conf_write(tc, TX39_CLOCKCTRL_REG, reg);
345
346 /*
347 * XXX Disable DMA (DMA not coded yet)
348 */
349 reg = tx_conf_read(tc, ofs);
350 reg &= ~(TX39_UARTCTRL1_ENDMARX | TX39_UARTCTRL1_ENDMATX);
351 tx_conf_write(tc, ofs, reg);
352
353 /* enable */
354 reg = tx_conf_read(tc, ofs);
355 reg |= TX39_UARTCTRL1_ENUART;
356 reg &= ~TX39_UARTCTRL1_ENBREAHALT;
357 tx_conf_write(tc, ofs, reg);
358
359 timeout = 100000;
360
361 while(!(tx_conf_read(tc, ofs) & TX39_UARTCTRL1_UARTON) &&
362 --timeout > 0)
363 ;
364
365 if (timeout == 0 && !cold) {
366 printf("%s never power up\n", __txcom_slotname(slot));
367 return 1;
368 }
369
370 return 0;
371 }
372
373 void
374 txcom_disable(struct txcom_chip *chip)
375 {
376 tx_chipset_tag_t tc;
377 txreg_t reg;
378 int slot;
379
380 tc = chip->sc_tc;
381 slot = chip->sc_slot;
382
383 reg = tx_conf_read(tc, TX39_UARTCTRL1_REG(slot));
384 /* DMA */
385 reg &= ~(TX39_UARTCTRL1_ENDMARX | TX39_UARTCTRL1_ENDMATX);
386
387 /* disable module */
388 reg &= ~TX39_UARTCTRL1_ENUART;
389 tx_conf_write(tc, TX39_UARTCTRL1_REG(slot), reg);
390
391 /* Clock */
392 reg = tx_conf_read(tc, TX39_CLOCKCTRL_REG);
393 reg &= ~(slot ? TX39_CLOCK_ENUARTBCLK : TX39_CLOCK_ENUARTACLK);
394 tx_conf_write(tc, TX39_CLOCKCTRL_REG, reg);
395
396 }
397
398 inline int
399 __txcom_txbufready(struct txcom_chip *chip, int retry)
400 {
401 tx_chipset_tag_t tc = chip->sc_tc;
402 int ofs = TX39_UARTCTRL1_REG(chip->sc_slot);
403
404 do {
405 if (tx_conf_read(tc, ofs) & TX39_UARTCTRL1_EMPTY)
406 return 1;
407 } while(--retry != 0);
408
409 return 0;
410 }
411
412 void
413 txcom_pulse_mode(struct device *dev)
414 {
415 struct txcom_softc *sc = (void*)dev;
416 struct txcom_chip *chip = sc->sc_chip;
417 tx_chipset_tag_t tc = chip->sc_tc;
418 int ofs;
419 txreg_t reg;
420
421 ofs = TX39_UARTCTRL1_REG(chip->sc_slot);
422
423 reg = tx_conf_read(tc, ofs);
424 /* WindowsCE use this setting */
425 reg |= TX39_UARTCTRL1_PULSEOPT1;
426 reg &= ~TX39_UARTCTRL1_PULSEOPT2;
427 reg |= TX39_UARTCTRL1_DTINVERT;
428
429 tx_conf_write(tc, ofs, reg);
430 }
431
432 /*
433 * console
434 */
435 int
436 txcom_cngetc(dev_t dev)
437 {
438 tx_chipset_tag_t tc;
439 int ofs, c, s;
440
441 s = spltty();
442
443 tc = txcom_chip.sc_tc;
444 ofs = TX39_UARTCTRL1_REG(txcom_chip.sc_slot);
445
446 while(!(TX39_UARTCTRL1_RXHOLDFULL & tx_conf_read(tc, ofs)))
447 ;
448
449 c = TX39_UARTRXHOLD_RXDATA(
450 tx_conf_read(tc, TX39_UARTRXHOLD_REG(txcom_chip.sc_slot)));
451
452 if (c == '\r')
453 c = '\n';
454
455 splx(s);
456
457 return c;
458 }
459
460 void
461 txcom_cnputc(dev_t dev, int c)
462 {
463 struct txcom_chip *chip = &txcom_chip;
464 tx_chipset_tag_t tc = chip->sc_tc;
465 int s;
466
467 s = spltty();
468
469 /* Wait for transmitter to empty */
470 __txcom_txbufready(chip, -1);
471
472 tx_conf_write(tc, TX39_UARTTXHOLD_REG(chip->sc_slot),
473 (c & TX39_UARTTXHOLD_TXDATA_MASK));
474
475 __txcom_txbufready(chip, -1);
476
477 splx(s);
478 }
479
480 void
481 txcom_cnpollc(dev_t dev, int on)
482 {
483 }
484
485 void
486 txcom_setmode(struct txcom_chip *chip)
487 {
488 tcflag_t cflag = chip->sc_cflag;
489 int ofs = TX39_UARTCTRL1_REG(chip->sc_slot);
490 txreg_t reg;
491
492 reg = tx_conf_read(chip->sc_tc, ofs);
493 reg &= ~TX39_UARTCTRL1_ENUART;
494 tx_conf_write(chip->sc_tc, ofs, reg);
495
496 switch (ISSET(cflag, CSIZE)) {
497 default:
498 printf("txcom_setmode: CS7, CS8 only. use CS7");
499 /* FALL THROUGH */
500 case CS7:
501 reg |= TX39_UARTCTRL1_BIT7;
502 break;
503 case CS8:
504 reg &= ~TX39_UARTCTRL1_BIT7;
505 break;
506 }
507
508 if (ISSET(cflag, PARENB)) {
509 reg |= TX39_UARTCTRL1_ENPARITY;
510 if (ISSET(cflag, PARODD)) {
511 reg &= ~TX39_UARTCTRL1_EVENPARITY;
512 } else {
513 reg |= TX39_UARTCTRL1_EVENPARITY;
514 }
515 } else {
516 reg &= ~TX39_UARTCTRL1_ENPARITY;
517 }
518
519 if (ISSET(cflag, CSTOPB))
520 reg |= TX39_UARTCTRL1_TWOSTOP;
521 else
522 reg &= ~TX39_UARTCTRL1_TWOSTOP;
523
524 reg |= TX39_UARTCTRL1_ENUART;
525 tx_conf_write(chip->sc_tc, ofs, reg);
526 }
527
528 void
529 txcom_setbaudrate(struct txcom_chip *chip)
530 {
531 int baudrate;
532 int ofs = TX39_UARTCTRL1_REG(chip->sc_slot);
533 txreg_t reg, reg1;
534
535 if (chip->sc_speed == 0)
536 return;
537
538 if (!cold)
539 DPRINTF("%d\n", chip->sc_speed);
540
541 reg1 = tx_conf_read(chip->sc_tc, ofs);
542 reg1 &= ~TX39_UARTCTRL1_ENUART;
543 tx_conf_write(chip->sc_tc, ofs, reg1);
544
545 baudrate = TX39_UARTCLOCKHZ / (chip->sc_speed * 16) - 1;
546 reg = TX39_UARTCTRL2_BAUDRATE_SET(0, baudrate);
547
548 tx_conf_write(chip->sc_tc, TX39_UARTCTRL2_REG(chip->sc_slot), reg);
549
550 reg1 |= TX39_UARTCTRL1_ENUART;
551 tx_conf_write(chip->sc_tc, ofs, reg1);
552 }
553
554 int
555 txcom_cnattach(int slot, int speed, int cflag)
556 {
557 cn_tab = &txcomcons;
558
559 txcom_chip.sc_tc = tx_conf_get_tag();
560 txcom_chip.sc_slot = slot;
561 txcom_chip.sc_cflag = cflag;
562 txcom_chip.sc_speed = speed;
563 txcom_chip.sc_hwflags |= TXCOM_HW_CONSOLE;
564 #if notyet
565 txcom_reset(&txcom_chip);
566 #endif
567 txcom_setmode(&txcom_chip);
568 txcom_setbaudrate(&txcom_chip);
569
570 if (txcom_enable(&txcom_chip, true) != 0)
571 return 1;
572
573 return 0;
574 }
575
576 /*
577 * tty
578 */
579 void
580 txcom_break(struct txcom_softc *sc, int on)
581 {
582 struct txcom_chip *chip = sc->sc_chip;
583
584 tx_conf_write(chip->sc_tc, TX39_UARTTXHOLD_REG(chip->sc_slot),
585 on ? TX39_UARTTXHOLD_BREAK : 0);
586 }
587
588 void
589 txcom_modem(struct txcom_softc *sc, int on)
590 {
591 struct txcom_chip *chip = sc->sc_chip;
592 tx_chipset_tag_t tc = chip->sc_tc;
593 int slot = chip->sc_slot;
594 txreg_t reg;
595
596 /* assert DTR */
597 if (IS_COM0(slot)) {
598 config_hook_call(CONFIG_HOOK_SET,
599 CONFIG_HOOK_COM0_DTR,
600 (void *)on);
601 }
602
603 reg = tx_conf_read(tc, TX39_UARTCTRL1_REG(slot));
604 reg &= ~TX39_UARTCTRL1_ENUART;
605 tx_conf_write(tc, TX39_UARTCTRL1_REG(slot), reg);
606
607 if (on) {
608 reg &= ~TX39_UARTCTRL1_DISTXD;
609 } else {
610 reg |= TX39_UARTCTRL1_DISTXD; /* low UARTTXD */
611 }
612
613 reg |= TX39_UARTCTRL1_ENUART;
614 tx_conf_write(tc, TX39_UARTCTRL1_REG(slot), reg);
615 }
616
617 void
618 txcom_shutdown(struct txcom_softc *sc)
619 {
620 struct tty *tp = sc->sc_tty;
621 int s = spltty();
622
623 /* Clear any break condition set with TIOCSBRK. */
624 txcom_break(sc, 0);
625
626 /*
627 * Hang up if necessary. Wait a bit, so the other side has time to
628 * notice even if we immediately open the port again.
629 */
630 if (ISSET(tp->t_cflag, HUPCL)) {
631 txcom_modem(sc, 0);
632 (void) tsleep(sc, TTIPRI, ttclos, hz);
633 }
634
635
636 /* Turn off interrupts if not the console. */
637 if (!ISSET(sc->sc_chip->sc_hwflags, TXCOM_HW_CONSOLE)) {
638 txcom_disable(sc->sc_chip);
639 }
640
641 splx(s);
642 }
643
644 const char *
645 __txcom_slotname(int slot)
646 {
647 static const char *slotname[] = {"UARTA", "UARTB", "unknown"};
648
649 if (slot != 0 && slot != 1)
650 return slotname[2];
651
652 return slotname[slot];
653 }
654
655 int
656 txcom_frameerr_intr(void *arg)
657 {
658 struct txcom_softc *sc = arg;
659
660 printf("%s frame error\n", __txcom_slotname(sc->sc_chip->sc_slot));
661
662 return 0;
663 }
664
665 int
666 txcom_parityerr_intr(void *arg)
667 {
668 struct txcom_softc *sc = arg;
669
670 printf("%s parity error\n", __txcom_slotname(sc->sc_chip->sc_slot));
671
672 return 0;
673 }
674
675 int
676 txcom_break_intr(void *arg)
677 {
678 struct txcom_softc *sc = arg;
679
680 printf("%s break\n", __txcom_slotname(sc->sc_chip->sc_slot));
681
682 return 0;
683 }
684
685 int
686 txcom_rxintr(void *arg)
687 {
688 struct txcom_softc *sc = arg;
689 struct txcom_chip *chip = sc->sc_chip;
690 u_int8_t c;
691
692 c = TX39_UARTRXHOLD_RXDATA(
693 tx_conf_read(chip->sc_tc,
694 TX39_UARTRXHOLD_REG(chip->sc_slot)));
695
696 sc->sc_rbuf[sc->sc_rbput] = c;
697 sc->sc_rbput = (sc->sc_rbput + 1) % TXCOM_RING_MASK;
698
699 callout_reset(&sc->sc_rxsoft_ch, 1, txcom_rxsoft, sc);
700
701 return 0;
702 }
703
704 void
705 txcom_rxsoft(void *arg)
706 {
707 struct txcom_softc *sc = arg;
708 struct tty *tp = sc->sc_tty;
709 int (*rint)(int, struct tty *);
710 int code;
711 int s, end, get;
712
713 rint = tp->t_linesw->l_rint;
714
715 s = spltty();
716 end = sc->sc_rbput;
717 get = sc->sc_rbget;
718
719 while (get != end) {
720 code = sc->sc_rbuf[get];
721
722 if ((*rint)(code, tp) == -1) {
723 /*
724 * The line discipline's buffer is out of space.
725 */
726 }
727 get = (get + 1) % TXCOM_RING_MASK;
728 }
729 sc->sc_rbget = get;
730
731 splx(s);
732 }
733
734 int
735 txcom_txintr(void *arg)
736 {
737 struct txcom_softc *sc = arg;
738 struct txcom_chip *chip = sc->sc_chip;
739 tx_chipset_tag_t tc = chip->sc_tc;
740
741 if (sc->sc_tbc > 0) {
742 tx_conf_write(tc, TX39_UARTTXHOLD_REG(chip->sc_slot),
743 (*sc->sc_tba &
744 TX39_UARTTXHOLD_TXDATA_MASK));
745 sc->sc_tbc--;
746 sc->sc_tba++;
747 } else {
748 callout_reset(&sc->sc_rxsoft_ch, 1, txcom_txsoft, sc);
749 }
750
751 return 0;
752 }
753
754 void
755 txcom_txsoft(void *arg)
756 {
757 struct txcom_softc *sc = arg;
758 struct tty *tp = sc->sc_tty;
759 int s = spltty();
760
761 CLR(tp->t_state, TS_BUSY);
762 if (ISSET(tp->t_state, TS_FLUSH)) {
763 CLR(tp->t_state, TS_FLUSH);
764 } else {
765 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
766 }
767
768 (*tp->t_linesw->l_start)(tp);
769
770 splx(s);
771 }
772
773 int
774 txcomopen(dev_t dev, int flag, int mode, struct lwp *l)
775 {
776 struct txcom_softc *sc;
777 struct txcom_chip *chip;
778 struct tty *tp;
779 int s, err = ENXIO;
780
781 sc = device_lookup_private(&txcom_cd, minor(dev));
782 if (sc == NULL)
783 return err;
784
785 chip = sc->sc_chip;
786 tp = sc->sc_tty;
787
788 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
789 return (EBUSY);
790
791 s = spltty();
792
793 if (txcom_enable(sc->sc_chip, false)) {
794 splx(s);
795 goto out;
796 }
797 /*
798 * Do the following iff this is a first open.
799 */
800 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
801 struct termios t;
802
803 tp->t_dev = dev;
804
805 t.c_ispeed = 0;
806 if (ISSET(chip->sc_hwflags, TXCOM_HW_CONSOLE)) {
807 t.c_ospeed = chip->sc_speed;
808 t.c_cflag = chip->sc_cflag;
809 } else {
810 t.c_ospeed = TTYDEF_SPEED;
811 t.c_cflag = TTYDEF_CFLAG;
812 }
813
814 if (ISSET(chip->sc_swflags, TIOCFLAG_CLOCAL))
815 SET(t.c_cflag, CLOCAL);
816 if (ISSET(chip->sc_swflags, TIOCFLAG_CRTSCTS))
817 SET(t.c_cflag, CRTSCTS);
818 if (ISSET(chip->sc_swflags, TIOCFLAG_MDMBUF))
819 SET(t.c_cflag, MDMBUF);
820
821 /* Make sure txcomparam() will do something. */
822 tp->t_ospeed = 0;
823 txcomparam(tp, &t);
824
825 tp->t_iflag = TTYDEF_IFLAG;
826 tp->t_oflag = TTYDEF_OFLAG;
827 tp->t_lflag = TTYDEF_LFLAG;
828
829 ttychars(tp);
830 ttsetwater(tp);
831
832 /*
833 * Turn on DTR. We must always do this, even if carrier is not
834 * present, because otherwise we'd have to use TIOCSDTR
835 * immediately after setting CLOCAL, which applications do not
836 * expect. We always assert DTR while the device is open
837 * unless explicitly requested to deassert it.
838 */
839 txcom_modem(sc, 1);
840
841 /* Clear the input ring, and unblock. */
842 sc->sc_rbget = sc->sc_rbput = 0;
843 }
844
845 splx(s);
846 #define TXCOMDIALOUT(x) (minor(x) & 0x80000)
847 if ((err = ttyopen(tp, TXCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK)))) {
848 DPRINTF("ttyopen failed\n");
849 goto out;
850 }
851 if ((err = (*tp->t_linesw->l_open)(dev, tp))) {
852 DPRINTF("line dicipline open failed\n");
853 goto out;
854 }
855
856 return err;
857
858 out:
859 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
860 /*
861 * We failed to open the device, and nobody else had it opened.
862 * Clean up the state as appropriate.
863 */
864 txcom_shutdown(sc);
865 }
866
867 return err;
868
869 }
870
871 int
872 txcomclose(dev_t dev, int flag, int mode, struct lwp *l)
873 {
874 struct txcom_softc *sc = device_lookup_private(&txcom_cd, minor(dev));
875 struct tty *tp = sc->sc_tty;
876
877 /* XXX This is for cons.c. */
878 if (!ISSET(tp->t_state, TS_ISOPEN))
879 return 0;
880
881 (*tp->t_linesw->l_close)(tp, flag);
882 ttyclose(tp);
883
884 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
885 /*
886 * Although we got a last close, the device may still be in
887 * use; e.g. if this was the dialout node, and there are still
888 * processes waiting for carrier on the non-dialout node.
889 */
890 txcom_shutdown(sc);
891 }
892
893 return 0;
894 }
895
896 int
897 txcomread(dev_t dev, struct uio *uio, int flag)
898 {
899 struct txcom_softc *sc = device_lookup_private(&txcom_cd, minor(dev));
900 struct tty *tp = sc->sc_tty;
901
902 return ((*tp->t_linesw->l_read)(tp, uio, flag));
903 }
904
905 int
906 txcomwrite(dev_t dev, struct uio *uio, int flag)
907 {
908 struct txcom_softc *sc = device_lookup_private(&txcom_cd, minor(dev));
909 struct tty *tp = sc->sc_tty;
910
911 return ((*tp->t_linesw->l_write)(tp, uio, flag));
912 }
913
914 int
915 txcompoll(dev_t dev, int events, struct lwp *l)
916 {
917 struct txcom_softc *sc = device_lookup_private(&txcom_cd, minor(dev));
918 struct tty *tp = sc->sc_tty;
919
920 return ((*tp->t_linesw->l_poll)(tp, events, l));
921 }
922
923 struct tty *
924 txcomtty(dev_t dev)
925 {
926 struct txcom_softc *sc = device_lookup_private(&txcom_cd, minor(dev));
927
928 return sc->sc_tty;
929 }
930
931 int
932 txcomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
933 {
934 struct txcom_softc *sc = device_lookup_private(&txcom_cd, minor(dev));
935 struct tty *tp = sc->sc_tty;
936 int s, err;
937
938 err = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
939 if (err != EPASSTHROUGH) {
940 return err;
941 }
942
943 err = ttioctl(tp, cmd, data, flag, l);
944 if (err != EPASSTHROUGH) {
945 return err;
946 }
947
948 err = 0;
949
950 s = spltty();
951
952 switch (cmd) {
953 default:
954 err = EPASSTHROUGH;
955 break;
956
957 case TIOCSBRK:
958 txcom_break(sc, 1);
959 break;
960
961 case TIOCCBRK:
962 txcom_break(sc, 0);
963 break;
964
965 case TIOCSDTR:
966 txcom_modem(sc, 1);
967 break;
968
969 case TIOCCDTR:
970 txcom_modem(sc, 0);
971 break;
972
973 case TIOCGFLAGS:
974 *(int *)data = sc->sc_chip->sc_swflags;
975 break;
976
977 case TIOCSFLAGS:
978 err = kauth_authorize_device_tty(l->l_cred,
979 KAUTH_DEVICE_TTY_PRIVSET, tp);
980 if (err) {
981 break;
982 }
983 sc->sc_chip->sc_swflags = *(int *)data;
984 break;
985
986 }
987
988 splx(s);
989
990 return err;
991 }
992
993 void
994 txcomstop(struct tty *tp, int flag)
995 {
996 struct txcom_softc *sc;
997 int s;
998
999 sc = device_lookup_private(&txcom_cd, minor(tp->t_dev));
1000
1001 s = spltty();
1002
1003 if (ISSET(tp->t_state, TS_BUSY)) {
1004 /* Stop transmitting at the next chunk. */
1005 sc->sc_tbc = 0;
1006 sc->sc_heldtbc = 0;
1007 if (!ISSET(tp->t_state, TS_TTSTOP))
1008 SET(tp->t_state, TS_FLUSH);
1009 }
1010
1011 splx(s);
1012 }
1013
1014 void
1015 txcomstart(struct tty *tp)
1016 {
1017 struct txcom_softc *sc;
1018 struct txcom_chip *chip;
1019 tx_chipset_tag_t tc;
1020 int slot;
1021 int s;
1022
1023 sc = device_lookup_private(&txcom_cd, minor(tp->t_dev));
1024 chip = sc->sc_chip;
1025 tc = chip->sc_tc;
1026 slot = chip->sc_slot;
1027
1028 s = spltty();
1029
1030 if (!__txcom_txbufready(chip, 0) ||
1031 ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1032 goto out;
1033
1034 if (!ttypull(tp))
1035 goto out;
1036
1037 sc->sc_tba = tp->t_outq.c_cf;
1038 sc->sc_tbc = ndqb(&tp->t_outq, 0);
1039 SET(tp->t_state, TS_BUSY);
1040
1041 /* Output the first character of the contiguous buffer. */
1042 tx_conf_write(tc, TX39_UARTTXHOLD_REG(slot),
1043 (*sc->sc_tba & TX39_UARTTXHOLD_TXDATA_MASK));
1044
1045 sc->sc_tbc--;
1046 sc->sc_tba++;
1047
1048 out:
1049 splx(s);
1050 }
1051
1052 /*
1053 * Set TXcom tty parameters from termios.
1054 */
1055 int
1056 txcomparam(struct tty *tp, struct termios *t)
1057 {
1058 struct txcom_softc *sc;
1059 struct txcom_chip *chip;
1060 int ospeed;
1061 int s;
1062
1063 sc = device_lookup_private(&txcom_cd, minor(tp->t_dev));
1064 if (sc == NULL)
1065 return ENXIO;
1066
1067 ospeed = t->c_ospeed;
1068
1069 /* Check requested parameters. */
1070 if (ospeed < 0) {
1071 return EINVAL;
1072 }
1073 if (t->c_ispeed && t->c_ispeed != ospeed) {
1074 return EINVAL;
1075 }
1076
1077 s = spltty();
1078 chip = sc->sc_chip;
1079 /*
1080 * For the console, always force CLOCAL and !HUPCL, so that the port
1081 * is always active.
1082 */
1083 if (ISSET(chip->sc_swflags, TIOCFLAG_SOFTCAR) ||
1084 ISSET(chip->sc_hwflags, TXCOM_HW_CONSOLE)) {
1085 SET(t->c_cflag, CLOCAL);
1086 CLR(t->c_cflag, HUPCL);
1087 }
1088 splx(s);
1089
1090 /*
1091 * If we're not in a mode that assumes a connection is present, then
1092 * ignore carrier changes.
1093 */
1094 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1095 chip->sc_dcd = 0;
1096 else
1097 chip->sc_dcd = 1;
1098
1099 /*
1100 * Only whack the UART when params change.
1101 * Some callers need to clear tp->t_ospeed
1102 * to make sure initialization gets done.
1103 */
1104 if (tp->t_ospeed == ospeed && tp->t_cflag == t->c_cflag) {
1105 return 0;
1106 }
1107
1108 s = spltty();
1109 chip = sc->sc_chip;
1110 chip->sc_speed = ospeed;
1111 chip->sc_cflag = t->c_cflag;
1112
1113 txcom_setmode(chip);
1114 txcom_setbaudrate(chip);
1115
1116 /* And copy to tty. */
1117 tp->t_ispeed = 0;
1118 tp->t_ospeed = chip->sc_speed;
1119 tp->t_cflag = chip->sc_cflag;
1120
1121 /*
1122 * Update the tty layer's idea of the carrier bit, in case we changed
1123 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1124 * explicit request.
1125 */
1126 (void) (*tp->t_linesw->l_modem)(tp, chip->sc_dcd);
1127
1128 /*
1129 * If hardware flow control is disabled, unblock any hard flow
1130 * control state.
1131 */
1132 if (!ISSET(chip->sc_cflag, CHWFLOW)) {
1133 txcomstart(tp);
1134 }
1135
1136 splx(s);
1137
1138 return 0;
1139 }
1140
1141 int
1142 txcom_dcd_hook(void *arg, int type, long id, void *msg)
1143 {
1144 struct txcom_softc *sc = arg;
1145 struct tty *tp = sc->sc_tty;
1146 struct txcom_chip *chip = sc->sc_chip;
1147 int modem = !(int)msg; /* p-edge 1, n-edge 0 */
1148
1149 DPRINTF("DCD %s\n", modem ? "ON" : "OFF");
1150
1151 if (modem && chip->sc_dcd)
1152 (void) (*tp->t_linesw->l_modem)(tp, chip->sc_dcd);
1153
1154 return 0;
1155 }
1156
1157 int
1158 txcom_cts_hook(void *arg, int type, long id, void *msg)
1159 {
1160 struct txcom_softc *sc = arg;
1161 struct tty *tp = sc->sc_tty;
1162 struct txcom_chip *chip = sc->sc_chip;
1163 int clear = !(int)msg; /* p-edge 1, n-edge 0 */
1164
1165 DPRINTF("CTS %s\n", clear ? "ON" : "OFF");
1166
1167 if (chip->sc_msr_cts) {
1168 if (!clear) {
1169 chip->sc_tx_stopped = 1;
1170 } else {
1171 chip->sc_tx_stopped = 0;
1172 (*tp->t_linesw->l_start)(tp);
1173 }
1174 }
1175
1176 return 0;
1177 }
1178
1179 #ifdef TX39UARTDEBUG
1180 void
1181 txcom_dump(struct txcom_chip *chip)
1182 {
1183 tx_chipset_tag_t tc = chip->sc_tc;
1184 int slot = chip->sc_slot;
1185 txreg_t reg;
1186
1187 reg = tx_conf_read(tc, TX39_UARTCTRL1_REG(slot));
1188 #define ISSETPRINT(r, m) \
1189 dbg_bitmask_print(r, TX39_UARTCTRL1_##m, #m)
1190 ISSETPRINT(reg, UARTON);
1191 ISSETPRINT(reg, EMPTY);
1192 ISSETPRINT(reg, PRXHOLDFULL);
1193 ISSETPRINT(reg, RXHOLDFULL);
1194 ISSETPRINT(reg, ENDMARX);
1195 ISSETPRINT(reg, ENDMATX);
1196 ISSETPRINT(reg, TESTMODE);
1197 ISSETPRINT(reg, ENBREAHALT);
1198 ISSETPRINT(reg, ENDMATEST);
1199 ISSETPRINT(reg, ENDMALOOP);
1200 ISSETPRINT(reg, PULSEOPT2);
1201 ISSETPRINT(reg, PULSEOPT1);
1202 ISSETPRINT(reg, DTINVERT);
1203 ISSETPRINT(reg, DISTXD);
1204 ISSETPRINT(reg, TWOSTOP);
1205 ISSETPRINT(reg, LOOPBACK);
1206 ISSETPRINT(reg, BIT7);
1207 ISSETPRINT(reg, EVENPARITY);
1208 ISSETPRINT(reg, ENPARITY);
1209 ISSETPRINT(reg, ENUART);
1210 }
1211 #endif /* TX39UARTDEBUG */
1212