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