1 /* Remote serial interface for local (hardwired) serial ports for GO32.
2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
4 Contributed by Nigel Stephens, Algorithmics Ltd. (nigel@algor.co.uk).
6 This version uses DPMI interrupts to handle buffered i/o
7 without the separate "asynctsr" program.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 * NS16550 UART registers
31 #define COM1ADDR 0x3f8
32 #define COM2ADDR 0x2f8
33 #define COM3ADDR 0x3e8
34 #define COM4ADDR 0x3e0
36 #define com_data 0 /* data register (R/W) */
37 #define com_dlbl 0 /* divisor latch low (W) */
38 #define com_ier 1 /* interrupt enable (W) */
39 #define com_dlbh 1 /* divisor latch high (W) */
40 #define com_iir 2 /* interrupt identification (R) */
41 #define com_fifo 2 /* FIFO control (W) */
42 #define com_lctl 3 /* line control register (R/W) */
43 #define com_cfcr 3 /* line control register (R/W) */
44 #define com_mcr 4 /* modem control register (R/W) */
45 #define com_lsr 5 /* line status register (R/W) */
46 #define com_msr 6 /* modem status register (R/W) */
49 * Constants for computing 16 bit baud rate divisor (lower byte
50 * in com_dlbl, upper in com_dlbh) from 1.8432MHz crystal. Divisor is
51 * 1.8432 MHz / (16 * X) for X bps. If the baud rate can't be set
52 * to within +- (desired_rate*SPEED_TOLERANCE/1000) bps, we fail.
54 #define COMTICK (1843200/16)
55 #define SPEED_TOLERANCE 30 /* thousandths; real == desired +- 3.0% */
57 /* interrupt enable register */
58 #define IER_ERXRDY 0x1 /* int on rx ready */
59 #define IER_ETXRDY 0x2 /* int on tx ready */
60 #define IER_ERLS 0x4 /* int on line status change */
61 #define IER_EMSC 0x8 /* int on modem status change */
63 /* interrupt identification register */
64 #define IIR_FIFO_MASK 0xc0 /* set if FIFOs are enabled */
65 #define IIR_IMASK 0xf /* interrupt cause mask */
66 #define IIR_NOPEND 0x1 /* nothing pending */
67 #define IIR_RLS 0x6 /* receive line status */
68 #define IIR_RXRDY 0x4 /* receive ready */
69 #define IIR_RXTOUT 0xc /* receive timeout */
70 #define IIR_TXRDY 0x2 /* transmit ready */
71 #define IIR_MLSC 0x0 /* modem status */
74 /* fifo control register */
75 #define FIFO_ENABLE 0x01 /* enable fifo */
76 #define FIFO_RCV_RST 0x02 /* reset receive fifo */
77 #define FIFO_XMT_RST 0x04 /* reset transmit fifo */
78 #define FIFO_DMA_MODE 0x08 /* enable dma mode */
79 #define FIFO_TRIGGER_1 0x00 /* trigger at 1 char */
80 #define FIFO_TRIGGER_4 0x40 /* trigger at 4 chars */
81 #define FIFO_TRIGGER_8 0x80 /* trigger at 8 chars */
82 #define FIFO_TRIGGER_14 0xc0 /* trigger at 14 chars */
84 /* character format control register */
85 #define CFCR_DLAB 0x80 /* divisor latch */
86 #define CFCR_SBREAK 0x40 /* send break */
87 #define CFCR_PZERO 0x30 /* zero parity */
88 #define CFCR_PONE 0x20 /* one parity */
89 #define CFCR_PEVEN 0x10 /* even parity */
90 #define CFCR_PODD 0x00 /* odd parity */
91 #define CFCR_PENAB 0x08 /* parity enable */
92 #define CFCR_STOPB 0x04 /* 2 stop bits */
93 #define CFCR_8BITS 0x03 /* 8 data bits */
94 #define CFCR_7BITS 0x02 /* 7 data bits */
95 #define CFCR_6BITS 0x01 /* 6 data bits */
96 #define CFCR_5BITS 0x00 /* 5 data bits */
98 /* modem control register */
99 #define MCR_LOOPBACK 0x10 /* loopback */
100 #define MCR_IENABLE 0x08 /* output 2 = int enable */
101 #define MCR_DRS 0x04 /* output 1 = xxx */
102 #define MCR_RTS 0x02 /* enable RTS */
103 #define MCR_DTR 0x01 /* enable DTR */
105 /* line status register */
106 #define LSR_RCV_FIFO 0x80 /* error in receive fifo */
107 #define LSR_TSRE 0x40 /* transmitter empty */
108 #define LSR_TXRDY 0x20 /* transmitter ready */
109 #define LSR_BI 0x10 /* break detected */
110 #define LSR_FE 0x08 /* framing error */
111 #define LSR_PE 0x04 /* parity error */
112 #define LSR_OE 0x02 /* overrun error */
113 #define LSR_RXRDY 0x01 /* receiver ready */
114 #define LSR_RCV_MASK 0x1f
116 /* modem status register */
121 #define MSR_DDCD 0x08
122 #define MSR_TERI 0x04
123 #define MSR_DDSR 0x02
124 #define MSR_DCTS 0x01
130 typedef unsigned long u_long;
132 /* 16550 rx fifo trigger point */
133 #define FIFO_TRIGGER FIFO_TRIGGER_4
135 /* input buffer size */
148 static size_t cnts[NCNT];
149 static char *cntnames[NCNT] =
151 /* h/w interrupt counts. */
152 "mlsc", "nopend", "txrdy", "?3",
153 "rxrdy", "?5", "rls", "?7",
154 "?8", "?9", "?a", "?b",
155 "rxtout", "?d", "?e", "?f",
157 "rxcnt", "txcnt", "stray", "swoflo"
160 #define COUNT(x) cnts[x]++
165 /* Main interrupt controller port addresses. */
166 #define ICU_BASE 0x20
167 #define ICU_OCW2 (ICU_BASE + 0)
168 #define ICU_MASK (ICU_BASE + 1)
170 /* Original interrupt controller mask register. */
171 unsigned char icu_oldmask;
173 /* Maximum of 8 interrupts (we don't handle the slave icu yet). */
176 static struct intrupt
179 struct dos_ttystate *port;
180 _go32_dpmi_seginfo old_rmhandler;
181 _go32_dpmi_seginfo old_pmhandler;
182 _go32_dpmi_seginfo new_rmhandler;
183 _go32_dpmi_seginfo new_pmhandler;
184 _go32_dpmi_registers regs;
189 static struct dos_ttystate
194 struct intrupt *intrupt;
197 unsigned char cbuf[CBSIZE];
201 unsigned char old_mcr;
210 COM1ADDR, 4, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
214 COM2ADDR, 3, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
218 COM3ADDR, 4, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
222 COM4ADDR, 3, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
226 static int dos_open (struct serial *scb, const char *name);
227 static void dos_raw (struct serial *scb);
228 static int dos_readchar (struct serial *scb, int timeout);
229 static int dos_setbaudrate (struct serial *scb, int rate);
230 static int dos_write (struct serial *scb, const void *buf, size_t count);
231 static void dos_close (struct serial *scb);
232 static serial_ttystate dos_get_tty_state (struct serial *scb);
233 static int dos_set_tty_state (struct serial *scb, serial_ttystate state);
234 static int dos_baudconv (int rate);
236 #define inb(p,a) inportb((p)->base + (a))
237 #define outb(p,a,v) outportb((p)->base + (a), (v))
238 #define disable() asm volatile ("cli");
239 #define enable() asm volatile ("sti");
243 dos_getc (volatile struct dos_ttystate *port)
247 if (port->count == 0)
250 c = port->cbuf[port->first];
252 port->first = (port->first + 1) & (CBSIZE - 1);
260 dos_putc (int c, struct dos_ttystate *port)
262 if (port->count >= CBSIZE - 1)
264 port->cbuf[(port->first + port->count) & (CBSIZE - 1)] = c;
274 struct dos_ttystate *port;
275 unsigned char iir, lsr, c;
277 disable (); /* Paranoia */
278 outportb (ICU_OCW2, 0x20); /* End-Of-Interrupt */
283 port = intrupts[irq].port;
287 return; /* not open */
292 iir = inb (port, com_iir) & IIR_IMASK;
297 lsr = inb (port, com_lsr);
307 c = inb (port, com_data);
308 if (lsr & (LSR_BI | LSR_FE | LSR_PE | LSR_OE))
310 if (lsr & (LSR_BI | LSR_FE))
312 else if (lsr & LSR_PE)
318 if (dos_putc (c, port) < 0)
327 while ((lsr = inb (port, com_lsr)) & LSR_RXRDY);
331 /* could be used to flowcontrol Tx */
332 port->msr = inb (port, com_msr);
340 /* No more pending interrupts, all done. */
344 /* Unexpected interrupt, ignore. */
351 #define ISRNAME(x) dos_comisr##x
352 #define ISR(x) static void ISRNAME(x)(void) {dos_comisr(x);}
354 ISR (0) ISR (1) ISR (2) ISR (3) /* OK */
355 ISR (4) ISR (5) ISR (6) ISR (7) /* OK */
357 typedef void (*isr_t) (void);
359 static isr_t isrs[NINTR] =
361 ISRNAME (0), ISRNAME (1), ISRNAME (2), ISRNAME (3),
362 ISRNAME (4), ISRNAME (5), ISRNAME (6), ISRNAME (7)
367 static struct intrupt *
368 dos_hookirq (unsigned int irq)
370 struct intrupt *intr;
377 intr = &intrupts[irq];
384 /* Setup real mode handler. */
385 _go32_dpmi_get_real_mode_interrupt_vector (vec, &intr->old_rmhandler);
387 intr->new_rmhandler.pm_selector = _go32_my_cs ();
388 intr->new_rmhandler.pm_offset = (u_long) isr;
389 if (_go32_dpmi_allocate_real_mode_callback_iret (&intr->new_rmhandler,
395 if (_go32_dpmi_set_real_mode_interrupt_vector (vec, &intr->new_rmhandler))
400 /* Setup protected mode handler. */
401 _go32_dpmi_get_protected_mode_interrupt_vector (vec, &intr->old_pmhandler);
403 intr->new_pmhandler.pm_selector = _go32_my_cs ();
404 intr->new_pmhandler.pm_offset = (u_long) isr;
405 _go32_dpmi_allocate_iret_wrapper (&intr->new_pmhandler);
407 if (_go32_dpmi_set_protected_mode_interrupt_vector (vec,
408 &intr->new_pmhandler))
413 /* Setup interrupt controller mask. */
415 outportb (ICU_MASK, inportb (ICU_MASK) & ~(1 << irq));
424 dos_unhookirq (struct intrupt *intr)
426 unsigned int irq, vec;
429 irq = intr - intrupts;
432 /* Restore old interrupt mask bit. */
435 outportb (ICU_MASK, inportb (ICU_MASK) | (mask & icu_oldmask));
438 /* Remove real mode handler. */
439 _go32_dpmi_set_real_mode_interrupt_vector (vec, &intr->old_rmhandler);
440 _go32_dpmi_free_real_mode_callback (&intr->new_rmhandler);
442 /* Remove protected mode handler. */
443 _go32_dpmi_set_protected_mode_interrupt_vector (vec, &intr->old_pmhandler);
444 _go32_dpmi_free_iret_wrapper (&intr->new_pmhandler);
451 dos_open (struct serial *scb, const char *name)
453 struct dos_ttystate *port;
456 if (strncasecmp (name, "/dev/", 5) == 0)
458 else if (strncasecmp (name, "\\dev\\", 5) == 0)
461 if (strlen (name) != 4 || strncasecmp (name, "com", 3) != 0)
467 if (name[3] < '1' || name[3] > '4')
473 /* FIXME: this is a Bad Idea (tm)! One should *never* invent file
474 handles, since they might be already used by other files/devices.
475 The Right Way to do this is to create a real handle by dup()'ing
476 some existing one. */
479 if (port->refcnt++ > 0)
481 /* Device already opened another user. Just point at it. */
486 /* Force access to ID reg. */
487 outb (port, com_cfcr, 0);
488 outb (port, com_iir, 0);
489 for (i = 0; i < 17; i++)
491 if ((inb (port, com_iir) & 0x38) == 0)
493 (void) inb (port, com_data); /* clear recv */
499 /* Disable all interrupts in chip. */
500 outb (port, com_ier, 0);
502 /* Tentatively enable 16550 fifo, and see if it responds. */
503 outb (port, com_fifo,
504 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER);
506 port->fifo = ((inb (port, com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK);
508 /* clear pending status reports. */
509 (void) inb (port, com_lsr);
510 (void) inb (port, com_msr);
512 /* Enable external interrupt gate (to avoid floating IRQ). */
513 outb (port, com_mcr, MCR_IENABLE);
515 /* Hook up interrupt handler and initialise icu. */
516 port->intrupt = dos_hookirq (port->irq);
519 outb (port, com_mcr, 0);
520 outb (port, com_fifo, 0);
528 port->intrupt->port = port;
531 /* Clear rx buffer, tx busy flag and overflow count. */
532 port->first = port->count = 0;
536 /* Set default baud rate and mode: 9600,8,n,1 */
537 i = dos_baudconv (port->baudrate = 9600);
538 outb (port, com_cfcr, CFCR_DLAB);
539 outb (port, com_dlbl, i & 0xff);
540 outb (port, com_dlbh, i >> 8);
541 outb (port, com_cfcr, CFCR_8BITS);
543 /* Enable all interrupts. */
544 outb (port, com_ier, IER_ETXRDY | IER_ERXRDY | IER_ERLS | IER_EMSC);
546 /* Enable DTR & RTS. */
547 outb (port, com_mcr, MCR_DTR | MCR_RTS | MCR_IENABLE);
556 dos_close (struct serial *scb)
558 struct dos_ttystate *port;
559 struct intrupt *intrupt;
564 port = &ports[scb->fd];
566 if (port->refcnt-- > 1)
569 if (!(intrupt = port->intrupt))
572 /* Disable interrupts, fifo, flow control. */
576 outb (port, com_fifo, 0);
577 outb (port, com_ier, 0);
580 /* Unhook handler, and disable interrupt gate. */
581 dos_unhookirq (intrupt);
582 outb (port, com_mcr, 0);
584 /* Check for overflow errors. */
587 fprintf_unfiltered (gdb_stderr,
588 "Serial input overruns occurred.\n");
589 fprintf_unfiltered (gdb_stderr, "This system %s handle %d baud.\n",
590 port->fifo ? "cannot" : "needs a 16550 to",
598 dos_noop (struct serial *scb)
604 dos_raw (struct serial *scb)
606 /* Always in raw mode. */
610 dos_readchar (struct serial *scb, int timeout)
612 struct dos_ttystate *port = &ports[scb->fd];
616 then = rawclock () + (timeout * RAWHZ);
617 while ((c = dos_getc (port)) < 0)
621 if (timeout >= 0 && (rawclock () - then) >= 0)
622 return SERIAL_TIMEOUT;
629 static serial_ttystate
630 dos_get_tty_state (struct serial *scb)
632 struct dos_ttystate *port = &ports[scb->fd];
633 struct dos_ttystate *state;
635 /* Are they asking about a port we opened? */
636 if (port->refcnt <= 0)
638 /* We've never heard about this port. We should fail this call,
639 unless they are asking about one of the 3 standard handles,
640 in which case we pretend the handle was open by us if it is
641 connected to a terminal device. This is beacuse Unix
642 terminals use the serial interface, so GDB expects the
643 standard handles to go through here. */
644 if (scb->fd >= 3 || !isatty (scb->fd))
648 state = XNEW (struct dos_ttystate);
650 return (serial_ttystate) state;
653 static serial_ttystate
654 dos_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
656 struct dos_ttystate *state;
658 state = XNEW (struct dos_ttystate);
659 *state = *(struct dos_ttystate *) ttystate;
661 return (serial_ttystate) state;
665 dos_set_tty_state (struct serial *scb, serial_ttystate ttystate)
667 struct dos_ttystate *state;
669 state = (struct dos_ttystate *) ttystate;
670 dos_setbaudrate (scb, state->baudrate);
675 dos_noflush_set_tty_state (struct serial *scb, serial_ttystate new_ttystate,
676 serial_ttystate old_ttystate)
678 struct dos_ttystate *state;
680 state = (struct dos_ttystate *) new_ttystate;
681 dos_setbaudrate (scb, state->baudrate);
686 dos_flush_input (struct serial *scb)
688 struct dos_ttystate *port = &ports[scb->fd];
691 port->first = port->count = 0;
693 outb (port, com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_TRIGGER);
699 dos_print_tty_state (struct serial *scb, serial_ttystate ttystate,
700 struct ui_file *stream)
702 /* Nothing to print. */
707 dos_baudconv (int rate)
714 #define divrnd(n, q) (((n) * 2 / (q) + 1) / 2) /* Divide and round off. */
715 x = divrnd (COMTICK, rate);
719 err = divrnd (1000 * COMTICK, x * rate) - 1000;
722 if (err > SPEED_TOLERANCE)
730 dos_setbaudrate (struct serial *scb, int rate)
732 struct dos_ttystate *port = &ports[scb->fd];
734 if (port->baudrate != rate)
739 x = dos_baudconv (rate);
742 fprintf_unfiltered (gdb_stderr, "%d: impossible baudrate\n", rate);
748 cfcr = inb (port, com_cfcr);
750 outb (port, com_cfcr, CFCR_DLAB);
751 outb (port, com_dlbl, x & 0xff);
752 outb (port, com_dlbh, x >> 8);
753 outb (port, com_cfcr, cfcr);
754 port->baudrate = rate;
762 dos_setstopbits (struct serial *scb, int num)
764 struct dos_ttystate *port = &ports[scb->fd];
768 cfcr = inb (port, com_cfcr);
772 case SERIAL_1_STOPBITS:
773 outb (port, com_cfcr, cfcr & ~CFCR_STOPB);
775 case SERIAL_1_AND_A_HALF_STOPBITS:
776 case SERIAL_2_STOPBITS:
777 outb (port, com_cfcr, cfcr | CFCR_STOPB);
789 dos_write (struct serial *scb, const void *buf, size_t count)
791 volatile struct dos_ttystate *port = &ports[scb->fd];
792 size_t fifosize = port->fifo ? 16 : 1;
795 const char *str = buf;
801 /* Send the data, fifosize bytes at a time. */
802 cnt = fifosize > count ? count : fifosize;
804 /* Francisco Pastor <fpastor.etra-id@etra.es> says OUTSB messes
805 up the communications with UARTs with FIFOs. */
806 #ifdef UART_FIFO_WORKS
807 outportsb (port->base + com_data, str, cnt);
811 for ( ; cnt > 0; cnt--, count--)
812 outportb (port->base + com_data, *str++);
817 /* Wait for transmission to complete (max 1 sec). */
818 then = rawclock () + RAWHZ;
821 if ((rawclock () - then) >= 0)
833 dos_sendbreak (struct serial *scb)
835 volatile struct dos_ttystate *port = &ports[scb->fd];
839 cfcr = inb (port, com_cfcr);
840 outb (port, com_cfcr, cfcr | CFCR_SBREAK);
843 then = rawclock () + RAWHZ / 4;
844 while ((rawclock () - then) < 0)
847 outb (port, com_cfcr, cfcr);
852 static const struct serial_ops dos_ops =
857 NULL, /* fdopen, not implemented */
860 dos_noop, /* flush output */
868 dos_noflush_set_tty_state,
872 dos_noop, /* Wait for output to drain. */
873 (void (*)(struct serial *, int))NULL /* Change into async mode. */
877 gdb_pipe (int pdes[2])
879 /* No support for pipes. */
885 dos_info (char *arg, int from_tty)
887 struct dos_ttystate *port;
892 for (port = ports; port < &ports[4]; port++)
894 if (port->baudrate == 0)
896 printf_filtered ("Port:\tCOM%ld (%sactive)\n", (long)(port - ports) + 1,
897 port->intrupt ? "" : "not ");
898 printf_filtered ("Addr:\t0x%03x (irq %d)\n", port->base, port->irq);
899 printf_filtered ("16550:\t%s\n", port->fifo ? "yes" : "no");
900 printf_filtered ("Speed:\t%d baud\n", port->baudrate);
901 printf_filtered ("Errs:\tframing %d parity %d overflow %d\n\n",
902 port->ferr, port->perr, port->oflo);
906 printf_filtered ("\nTotal interrupts: %d\n", intrcnt);
907 for (i = 0; i < NCNT; i++)
909 printf_filtered ("%s:\t%lu\n", cntnames[i], (unsigned long) cnts[i]);
913 /* -Wmissing-prototypes */
914 extern initialize_file_ftype _initialize_ser_dos;
917 _initialize_ser_dos (void)
919 serial_add_interface (&dos_ops);
921 /* Save original interrupt mask register. */
922 icu_oldmask = inportb (ICU_MASK);
924 /* Mark fixed motherboard irqs as inuse. */
925 intrupts[0].inuse = /* timer tick */
926 intrupts[1].inuse = /* keyboard */
927 intrupts[2].inuse = 1; /* slave icu */
929 add_setshow_zinteger_cmd ("com1base", class_obscure, &ports[0].base, _("\
930 Set COM1 base i/o port address."), _("\
931 Show COM1 base i/o port address."), NULL,
933 NULL, /* FIXME: i18n: */
934 &setlist, &showlist);
936 add_setshow_zinteger_cmd ("com1irq", class_obscure, &ports[0].irq, _("\
937 Set COM1 interrupt request."), _("\
938 Show COM1 interrupt request."), NULL,
940 NULL, /* FIXME: i18n: */
941 &setlist, &showlist);
943 add_setshow_zinteger_cmd ("com2base", class_obscure, &ports[1].base, _("\
944 Set COM2 base i/o port address."), _("\
945 Show COM2 base i/o port address."), NULL,
947 NULL, /* FIXME: i18n: */
948 &setlist, &showlist);
950 add_setshow_zinteger_cmd ("com2irq", class_obscure, &ports[1].irq, _("\
951 Set COM2 interrupt request."), _("\
952 Show COM2 interrupt request."), NULL,
954 NULL, /* FIXME: i18n: */
955 &setlist, &showlist);
957 add_setshow_zinteger_cmd ("com3base", class_obscure, &ports[2].base, _("\
958 Set COM3 base i/o port address."), _("\
959 Show COM3 base i/o port address."), NULL,
961 NULL, /* FIXME: i18n: */
962 &setlist, &showlist);
964 add_setshow_zinteger_cmd ("com3irq", class_obscure, &ports[2].irq, _("\
965 Set COM3 interrupt request."), _("\
966 Show COM3 interrupt request."), NULL,
968 NULL, /* FIXME: i18n: */
969 &setlist, &showlist);
971 add_setshow_zinteger_cmd ("com4base", class_obscure, &ports[3].base, _("\
972 Set COM4 base i/o port address."), _("\
973 Show COM4 base i/o port address."), NULL,
975 NULL, /* FIXME: i18n: */
976 &setlist, &showlist);
978 add_setshow_zinteger_cmd ("com4irq", class_obscure, &ports[3].irq, _("\
979 Set COM4 interrupt request."), _("\
980 Show COM4 interrupt request."), NULL,
982 NULL, /* FIXME: i18n: */
983 &setlist, &showlist);
985 add_info ("serial", dos_info,
986 _("Print DOS serial port status."));