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