1 /* Remote serial interface for local (hardwired) serial ports for GO32.
2 Copyright (C) 1992-2014 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/>. */
31 * NS16550 UART registers
34 #define COM1ADDR 0x3f8
35 #define COM2ADDR 0x2f8
36 #define COM3ADDR 0x3e8
37 #define COM4ADDR 0x3e0
39 #define com_data 0 /* data register (R/W) */
40 #define com_dlbl 0 /* divisor latch low (W) */
41 #define com_ier 1 /* interrupt enable (W) */
42 #define com_dlbh 1 /* divisor latch high (W) */
43 #define com_iir 2 /* interrupt identification (R) */
44 #define com_fifo 2 /* FIFO control (W) */
45 #define com_lctl 3 /* line control register (R/W) */
46 #define com_cfcr 3 /* line control register (R/W) */
47 #define com_mcr 4 /* modem control register (R/W) */
48 #define com_lsr 5 /* line status register (R/W) */
49 #define com_msr 6 /* modem status register (R/W) */
52 * Constants for computing 16 bit baud rate divisor (lower byte
53 * in com_dlbl, upper in com_dlbh) from 1.8432MHz crystal. Divisor is
54 * 1.8432 MHz / (16 * X) for X bps. If the baud rate can't be set
55 * to within +- (desired_rate*SPEED_TOLERANCE/1000) bps, we fail.
57 #define COMTICK (1843200/16)
58 #define SPEED_TOLERANCE 30 /* thousandths; real == desired +- 3.0% */
60 /* interrupt enable register */
61 #define IER_ERXRDY 0x1 /* int on rx ready */
62 #define IER_ETXRDY 0x2 /* int on tx ready */
63 #define IER_ERLS 0x4 /* int on line status change */
64 #define IER_EMSC 0x8 /* int on modem status change */
66 /* interrupt identification register */
67 #define IIR_FIFO_MASK 0xc0 /* set if FIFOs are enabled */
68 #define IIR_IMASK 0xf /* interrupt cause mask */
69 #define IIR_NOPEND 0x1 /* nothing pending */
70 #define IIR_RLS 0x6 /* receive line status */
71 #define IIR_RXRDY 0x4 /* receive ready */
72 #define IIR_RXTOUT 0xc /* receive timeout */
73 #define IIR_TXRDY 0x2 /* transmit ready */
74 #define IIR_MLSC 0x0 /* modem status */
77 /* fifo control register */
78 #define FIFO_ENABLE 0x01 /* enable fifo */
79 #define FIFO_RCV_RST 0x02 /* reset receive fifo */
80 #define FIFO_XMT_RST 0x04 /* reset transmit fifo */
81 #define FIFO_DMA_MODE 0x08 /* enable dma mode */
82 #define FIFO_TRIGGER_1 0x00 /* trigger at 1 char */
83 #define FIFO_TRIGGER_4 0x40 /* trigger at 4 chars */
84 #define FIFO_TRIGGER_8 0x80 /* trigger at 8 chars */
85 #define FIFO_TRIGGER_14 0xc0 /* trigger at 14 chars */
87 /* character format control register */
88 #define CFCR_DLAB 0x80 /* divisor latch */
89 #define CFCR_SBREAK 0x40 /* send break */
90 #define CFCR_PZERO 0x30 /* zero parity */
91 #define CFCR_PONE 0x20 /* one parity */
92 #define CFCR_PEVEN 0x10 /* even parity */
93 #define CFCR_PODD 0x00 /* odd parity */
94 #define CFCR_PENAB 0x08 /* parity enable */
95 #define CFCR_STOPB 0x04 /* 2 stop bits */
96 #define CFCR_8BITS 0x03 /* 8 data bits */
97 #define CFCR_7BITS 0x02 /* 7 data bits */
98 #define CFCR_6BITS 0x01 /* 6 data bits */
99 #define CFCR_5BITS 0x00 /* 5 data bits */
101 /* modem control register */
102 #define MCR_LOOPBACK 0x10 /* loopback */
103 #define MCR_IENABLE 0x08 /* output 2 = int enable */
104 #define MCR_DRS 0x04 /* output 1 = xxx */
105 #define MCR_RTS 0x02 /* enable RTS */
106 #define MCR_DTR 0x01 /* enable DTR */
108 /* line status register */
109 #define LSR_RCV_FIFO 0x80 /* error in receive fifo */
110 #define LSR_TSRE 0x40 /* transmitter empty */
111 #define LSR_TXRDY 0x20 /* transmitter ready */
112 #define LSR_BI 0x10 /* break detected */
113 #define LSR_FE 0x08 /* framing error */
114 #define LSR_PE 0x04 /* parity error */
115 #define LSR_OE 0x02 /* overrun error */
116 #define LSR_RXRDY 0x01 /* receiver ready */
117 #define LSR_RCV_MASK 0x1f
119 /* modem status register */
124 #define MSR_DDCD 0x08
125 #define MSR_TERI 0x04
126 #define MSR_DDSR 0x02
127 #define MSR_DCTS 0x01
133 typedef unsigned long u_long;
135 /* 16550 rx fifo trigger point */
136 #define FIFO_TRIGGER FIFO_TRIGGER_4
138 /* input buffer size */
151 static size_t cnts[NCNT];
152 static char *cntnames[NCNT] =
154 /* h/w interrupt counts. */
155 "mlsc", "nopend", "txrdy", "?3",
156 "rxrdy", "?5", "rls", "?7",
157 "?8", "?9", "?a", "?b",
158 "rxtout", "?d", "?e", "?f",
160 "rxcnt", "txcnt", "stray", "swoflo"
163 #define COUNT(x) cnts[x]++
168 /* Main interrupt controller port addresses. */
169 #define ICU_BASE 0x20
170 #define ICU_OCW2 (ICU_BASE + 0)
171 #define ICU_MASK (ICU_BASE + 1)
173 /* Original interrupt controller mask register. */
174 unsigned char icu_oldmask;
176 /* Maximum of 8 interrupts (we don't handle the slave icu yet). */
179 static struct intrupt
182 struct dos_ttystate *port;
183 _go32_dpmi_seginfo old_rmhandler;
184 _go32_dpmi_seginfo old_pmhandler;
185 _go32_dpmi_seginfo new_rmhandler;
186 _go32_dpmi_seginfo new_pmhandler;
187 _go32_dpmi_registers regs;
192 static struct dos_ttystate
197 struct intrupt *intrupt;
200 unsigned char cbuf[CBSIZE];
204 unsigned char old_mcr;
213 COM1ADDR, 4, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
217 COM2ADDR, 3, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
221 COM3ADDR, 4, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
225 COM4ADDR, 3, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
229 static int dos_open (struct serial *scb, const char *name);
230 static void dos_raw (struct serial *scb);
231 static int dos_readchar (struct serial *scb, int timeout);
232 static int dos_setbaudrate (struct serial *scb, int rate);
233 static int dos_write (struct serial *scb, const void *buf, size_t count);
234 static void dos_close (struct serial *scb);
235 static serial_ttystate dos_get_tty_state (struct serial *scb);
236 static int dos_set_tty_state (struct serial *scb, serial_ttystate state);
237 static int dos_baudconv (int rate);
239 #define inb(p,a) inportb((p)->base + (a))
240 #define outb(p,a,v) outportb((p)->base + (a), (v))
241 #define disable() asm volatile ("cli");
242 #define enable() asm volatile ("sti");
246 dos_getc (volatile struct dos_ttystate *port)
250 if (port->count == 0)
253 c = port->cbuf[port->first];
255 port->first = (port->first + 1) & (CBSIZE - 1);
263 dos_putc (int c, struct dos_ttystate *port)
265 if (port->count >= CBSIZE - 1)
267 port->cbuf[(port->first + port->count) & (CBSIZE - 1)] = c;
277 struct dos_ttystate *port;
278 unsigned char iir, lsr, c;
280 disable (); /* Paranoia */
281 outportb (ICU_OCW2, 0x20); /* End-Of-Interrupt */
286 port = intrupts[irq].port;
290 return; /* not open */
295 iir = inb (port, com_iir) & IIR_IMASK;
300 lsr = inb (port, com_lsr);
310 c = inb (port, com_data);
311 if (lsr & (LSR_BI | LSR_FE | LSR_PE | LSR_OE))
313 if (lsr & (LSR_BI | LSR_FE))
315 else if (lsr & LSR_PE)
321 if (dos_putc (c, port) < 0)
330 while ((lsr = inb (port, com_lsr)) & LSR_RXRDY);
334 /* could be used to flowcontrol Tx */
335 port->msr = inb (port, com_msr);
343 /* No more pending interrupts, all done. */
347 /* Unexpected interrupt, ignore. */
354 #define ISRNAME(x) dos_comisr##x
355 #define ISR(x) static void ISRNAME(x)(void) {dos_comisr(x);}
357 ISR (0) ISR (1) ISR (2) ISR (3) /* OK */
358 ISR (4) ISR (5) ISR (6) ISR (7) /* OK */
360 typedef void (*isr_t) (void);
362 static isr_t isrs[NINTR] =
364 ISRNAME (0), ISRNAME (1), ISRNAME (2), ISRNAME (3),
365 ISRNAME (4), ISRNAME (5), ISRNAME (6), ISRNAME (7)
370 static struct intrupt *
371 dos_hookirq (unsigned int irq)
373 struct intrupt *intr;
380 intr = &intrupts[irq];
387 /* Setup real mode handler. */
388 _go32_dpmi_get_real_mode_interrupt_vector (vec, &intr->old_rmhandler);
390 intr->new_rmhandler.pm_selector = _go32_my_cs ();
391 intr->new_rmhandler.pm_offset = (u_long) isr;
392 if (_go32_dpmi_allocate_real_mode_callback_iret (&intr->new_rmhandler,
398 if (_go32_dpmi_set_real_mode_interrupt_vector (vec, &intr->new_rmhandler))
403 /* Setup protected mode handler. */
404 _go32_dpmi_get_protected_mode_interrupt_vector (vec, &intr->old_pmhandler);
406 intr->new_pmhandler.pm_selector = _go32_my_cs ();
407 intr->new_pmhandler.pm_offset = (u_long) isr;
408 _go32_dpmi_allocate_iret_wrapper (&intr->new_pmhandler);
410 if (_go32_dpmi_set_protected_mode_interrupt_vector (vec,
411 &intr->new_pmhandler))
416 /* Setup interrupt controller mask. */
418 outportb (ICU_MASK, inportb (ICU_MASK) & ~(1 << irq));
427 dos_unhookirq (struct intrupt *intr)
429 unsigned int irq, vec;
432 irq = intr - intrupts;
435 /* Restore old interrupt mask bit. */
438 outportb (ICU_MASK, inportb (ICU_MASK) | (mask & icu_oldmask));
441 /* Remove real mode handler. */
442 _go32_dpmi_set_real_mode_interrupt_vector (vec, &intr->old_rmhandler);
443 _go32_dpmi_free_real_mode_callback (&intr->new_rmhandler);
445 /* Remove protected mode handler. */
446 _go32_dpmi_set_protected_mode_interrupt_vector (vec, &intr->old_pmhandler);
447 _go32_dpmi_free_iret_wrapper (&intr->new_pmhandler);
454 dos_open (struct serial *scb, const char *name)
456 struct dos_ttystate *port;
459 if (strncasecmp (name, "/dev/", 5) == 0)
461 else if (strncasecmp (name, "\\dev\\", 5) == 0)
464 if (strlen (name) != 4 || strncasecmp (name, "com", 3) != 0)
470 if (name[3] < '1' || name[3] > '4')
476 /* FIXME: this is a Bad Idea (tm)! One should *never* invent file
477 handles, since they might be already used by other files/devices.
478 The Right Way to do this is to create a real handle by dup()'ing
479 some existing one. */
482 if (port->refcnt++ > 0)
484 /* Device already opened another user. Just point at it. */
489 /* Force access to ID reg. */
490 outb (port, com_cfcr, 0);
491 outb (port, com_iir, 0);
492 for (i = 0; i < 17; i++)
494 if ((inb (port, com_iir) & 0x38) == 0)
496 (void) inb (port, com_data); /* clear recv */
502 /* Disable all interrupts in chip. */
503 outb (port, com_ier, 0);
505 /* Tentatively enable 16550 fifo, and see if it responds. */
506 outb (port, com_fifo,
507 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER);
509 port->fifo = ((inb (port, com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK);
511 /* clear pending status reports. */
512 (void) inb (port, com_lsr);
513 (void) inb (port, com_msr);
515 /* Enable external interrupt gate (to avoid floating IRQ). */
516 outb (port, com_mcr, MCR_IENABLE);
518 /* Hook up interrupt handler and initialise icu. */
519 port->intrupt = dos_hookirq (port->irq);
522 outb (port, com_mcr, 0);
523 outb (port, com_fifo, 0);
531 port->intrupt->port = port;
534 /* Clear rx buffer, tx busy flag and overflow count. */
535 port->first = port->count = 0;
539 /* Set default baud rate and mode: 9600,8,n,1 */
540 i = dos_baudconv (port->baudrate = 9600);
541 outb (port, com_cfcr, CFCR_DLAB);
542 outb (port, com_dlbl, i & 0xff);
543 outb (port, com_dlbh, i >> 8);
544 outb (port, com_cfcr, CFCR_8BITS);
546 /* Enable all interrupts. */
547 outb (port, com_ier, IER_ETXRDY | IER_ERXRDY | IER_ERLS | IER_EMSC);
549 /* Enable DTR & RTS. */
550 outb (port, com_mcr, MCR_DTR | MCR_RTS | MCR_IENABLE);
559 dos_close (struct serial *scb)
561 struct dos_ttystate *port;
562 struct intrupt *intrupt;
567 port = &ports[scb->fd];
569 if (port->refcnt-- > 1)
572 if (!(intrupt = port->intrupt))
575 /* Disable interrupts, fifo, flow control. */
579 outb (port, com_fifo, 0);
580 outb (port, com_ier, 0);
583 /* Unhook handler, and disable interrupt gate. */
584 dos_unhookirq (intrupt);
585 outb (port, com_mcr, 0);
587 /* Check for overflow errors. */
590 fprintf_unfiltered (gdb_stderr,
591 "Serial input overruns occurred.\n");
592 fprintf_unfiltered (gdb_stderr, "This system %s handle %d baud.\n",
593 port->fifo ? "cannot" : "needs a 16550 to",
601 dos_noop (struct serial *scb)
607 dos_raw (struct serial *scb)
609 /* Always in raw mode. */
613 dos_readchar (struct serial *scb, int timeout)
615 struct dos_ttystate *port = &ports[scb->fd];
619 then = rawclock () + (timeout * RAWHZ);
620 while ((c = dos_getc (port)) < 0)
622 if (timeout >= 0 && (rawclock () - then) >= 0)
623 return SERIAL_TIMEOUT;
630 static serial_ttystate
631 dos_get_tty_state (struct serial *scb)
633 struct dos_ttystate *port = &ports[scb->fd];
634 struct dos_ttystate *state;
636 /* Are they asking about a port we opened? */
637 if (port->refcnt <= 0)
639 /* We've never heard about this port. We should fail this call,
640 unless they are asking about one of the 3 standard handles,
641 in which case we pretend the handle was open by us if it is
642 connected to a terminal device. This is beacuse Unix
643 terminals use the serial interface, so GDB expects the
644 standard handles to go through here. */
645 if (scb->fd >= 3 || !isatty (scb->fd))
649 state = (struct dos_ttystate *) xmalloc (sizeof *state);
651 return (serial_ttystate) state;
654 static serial_ttystate
655 dos_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
657 struct dos_ttystate *state;
659 state = (struct dos_ttystate *) xmalloc (sizeof *state);
660 *state = *(struct dos_ttystate *) ttystate;
662 return (serial_ttystate) state;
666 dos_set_tty_state (struct serial *scb, serial_ttystate ttystate)
668 struct dos_ttystate *state;
670 state = (struct dos_ttystate *) ttystate;
671 dos_setbaudrate (scb, state->baudrate);
676 dos_noflush_set_tty_state (struct serial *scb, serial_ttystate new_ttystate,
677 serial_ttystate old_ttystate)
679 struct dos_ttystate *state;
681 state = (struct dos_ttystate *) new_ttystate;
682 dos_setbaudrate (scb, state->baudrate);
687 dos_flush_input (struct serial *scb)
689 struct dos_ttystate *port = &ports[scb->fd];
692 port->first = port->count = 0;
694 outb (port, com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_TRIGGER);
700 dos_print_tty_state (struct serial *scb, serial_ttystate ttystate,
701 struct ui_file *stream)
703 /* Nothing to print. */
708 dos_baudconv (int rate)
715 #define divrnd(n, q) (((n) * 2 / (q) + 1) / 2) /* Divide and round off. */
716 x = divrnd (COMTICK, rate);
720 err = divrnd (1000 * COMTICK, x * rate) - 1000;
723 if (err > SPEED_TOLERANCE)
731 dos_setbaudrate (struct serial *scb, int rate)
733 struct dos_ttystate *port = &ports[scb->fd];
735 if (port->baudrate != rate)
740 x = dos_baudconv (rate);
743 fprintf_unfiltered (gdb_stderr, "%d: impossible baudrate\n", rate);
749 cfcr = inb (port, com_cfcr);
751 outb (port, com_cfcr, CFCR_DLAB);
752 outb (port, com_dlbl, x & 0xff);
753 outb (port, com_dlbh, x >> 8);
754 outb (port, com_cfcr, cfcr);
755 port->baudrate = rate;
763 dos_setstopbits (struct serial *scb, int num)
765 struct dos_ttystate *port = &ports[scb->fd];
769 cfcr = inb (port, com_cfcr);
773 case SERIAL_1_STOPBITS:
774 outb (port, com_cfcr, cfcr & ~CFCR_STOPB);
776 case SERIAL_1_AND_A_HALF_STOPBITS:
777 case SERIAL_2_STOPBITS:
778 outb (port, com_cfcr, cfcr | CFCR_STOPB);
790 dos_write (struct serial *scb, const void *buf, size_t count)
792 volatile struct dos_ttystate *port = &ports[scb->fd];
793 size_t fifosize = port->fifo ? 16 : 1;
796 const char *str = buf;
800 /* Send the data, fifosize bytes at a time. */
801 cnt = fifosize > count ? count : fifosize;
803 /* Francisco Pastor <fpastor.etra-id@etra.es> says OUTSB messes
804 up the communications with UARTs with FIFOs. */
805 #ifdef UART_FIFO_WORKS
806 outportsb (port->base + com_data, str, cnt);
810 for ( ; cnt > 0; cnt--, count--)
811 outportb (port->base + com_data, *str++);
816 /* Wait for transmission to complete (max 1 sec). */
817 then = rawclock () + RAWHZ;
820 if ((rawclock () - then) >= 0)
832 dos_sendbreak (struct serial *scb)
834 volatile struct dos_ttystate *port = &ports[scb->fd];
838 cfcr = inb (port, com_cfcr);
839 outb (port, com_cfcr, cfcr | CFCR_SBREAK);
842 then = rawclock () + RAWHZ / 4;
843 while ((rawclock () - then) < 0)
846 outb (port, com_cfcr, cfcr);
851 static const struct serial_ops dos_ops =
856 NULL, /* fdopen, not implemented */
859 dos_noop, /* flush output */
867 dos_noflush_set_tty_state,
870 dos_noop, /* Wait for output to drain. */
871 (void (*)(struct serial *, int))NULL /* Change into async mode. */
875 gdb_pipe (int pdes[2])
877 /* No support for pipes. */
883 dos_info (char *arg, int from_tty)
885 struct dos_ttystate *port;
890 for (port = ports; port < &ports[4]; port++)
892 if (port->baudrate == 0)
894 printf_filtered ("Port:\tCOM%ld (%sactive)\n", (long)(port - ports) + 1,
895 port->intrupt ? "" : "not ");
896 printf_filtered ("Addr:\t0x%03x (irq %d)\n", port->base, port->irq);
897 printf_filtered ("16550:\t%s\n", port->fifo ? "yes" : "no");
898 printf_filtered ("Speed:\t%d baud\n", port->baudrate);
899 printf_filtered ("Errs:\tframing %d parity %d overflow %d\n\n",
900 port->ferr, port->perr, port->oflo);
904 printf_filtered ("\nTotal interrupts: %d\n", intrcnt);
905 for (i = 0; i < NCNT; i++)
907 printf_filtered ("%s:\t%lu\n", cntnames[i], (unsigned long) cnts[i]);
911 /* -Wmissing-prototypes */
912 extern initialize_file_ftype _initialize_ser_dos;
915 _initialize_ser_dos (void)
917 serial_add_interface (&dos_ops);
919 /* Save original interrupt mask register. */
920 icu_oldmask = inportb (ICU_MASK);
922 /* Mark fixed motherboard irqs as inuse. */
923 intrupts[0].inuse = /* timer tick */
924 intrupts[1].inuse = /* keyboard */
925 intrupts[2].inuse = 1; /* slave icu */
927 add_setshow_zinteger_cmd ("com1base", class_obscure, &ports[0].base, _("\
928 Set COM1 base i/o port address."), _("\
929 Show COM1 base i/o port address."), NULL,
931 NULL, /* FIXME: i18n: */
932 &setlist, &showlist);
934 add_setshow_zinteger_cmd ("com1irq", class_obscure, &ports[0].irq, _("\
935 Set COM1 interrupt request."), _("\
936 Show COM1 interrupt request."), NULL,
938 NULL, /* FIXME: i18n: */
939 &setlist, &showlist);
941 add_setshow_zinteger_cmd ("com2base", class_obscure, &ports[1].base, _("\
942 Set COM2 base i/o port address."), _("\
943 Show COM2 base i/o port address."), NULL,
945 NULL, /* FIXME: i18n: */
946 &setlist, &showlist);
948 add_setshow_zinteger_cmd ("com2irq", class_obscure, &ports[1].irq, _("\
949 Set COM2 interrupt request."), _("\
950 Show COM2 interrupt request."), NULL,
952 NULL, /* FIXME: i18n: */
953 &setlist, &showlist);
955 add_setshow_zinteger_cmd ("com3base", class_obscure, &ports[2].base, _("\
956 Set COM3 base i/o port address."), _("\
957 Show COM3 base i/o port address."), NULL,
959 NULL, /* FIXME: i18n: */
960 &setlist, &showlist);
962 add_setshow_zinteger_cmd ("com3irq", class_obscure, &ports[2].irq, _("\
963 Set COM3 interrupt request."), _("\
964 Show COM3 interrupt request."), NULL,
966 NULL, /* FIXME: i18n: */
967 &setlist, &showlist);
969 add_setshow_zinteger_cmd ("com4base", class_obscure, &ports[3].base, _("\
970 Set COM4 base i/o port address."), _("\
971 Show COM4 base i/o port address."), NULL,
973 NULL, /* FIXME: i18n: */
974 &setlist, &showlist);
976 add_setshow_zinteger_cmd ("com4irq", class_obscure, &ports[3].irq, _("\
977 Set COM4 interrupt request."), _("\
978 Show COM4 interrupt request."), NULL,
980 NULL, /* FIXME: i18n: */
981 &setlist, &showlist);
983 add_info ("serial", dos_info,
984 _("Print DOS serial port status."));