1 /* Remote serial interface for local (hardwired) serial ports for GO32.
2 Copyright (C) 1992, 1993, 2000, 2001, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
7 This version uses DPMI interrupts to handle buffered i/o
8 without the separate "asynctsr" program.
10 This file is part of GDB.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "gdb_string.h"
32 * NS16550 UART registers
35 #define COM1ADDR 0x3f8
36 #define COM2ADDR 0x2f8
37 #define COM3ADDR 0x3e8
38 #define COM4ADDR 0x3e0
40 #define com_data 0 /* data register (R/W) */
41 #define com_dlbl 0 /* divisor latch low (W) */
42 #define com_ier 1 /* interrupt enable (W) */
43 #define com_dlbh 1 /* divisor latch high (W) */
44 #define com_iir 2 /* interrupt identification (R) */
45 #define com_fifo 2 /* FIFO control (W) */
46 #define com_lctl 3 /* line control register (R/W) */
47 #define com_cfcr 3 /* line control register (R/W) */
48 #define com_mcr 4 /* modem control register (R/W) */
49 #define com_lsr 5 /* line status register (R/W) */
50 #define com_msr 6 /* modem status register (R/W) */
53 * Constants for computing 16 bit baud rate divisor (lower byte
54 * in com_dlbl, upper in com_dlbh) from 1.8432MHz crystal. Divisor is
55 * 1.8432 MHz / (16 * X) for X bps. If the baud rate can't be set
56 * to within +- (desired_rate*SPEED_TOLERANCE/1000) bps, we fail.
58 #define COMTICK (1843200/16)
59 #define SPEED_TOLERANCE 30 /* thousandths; real == desired +- 3.0% */
61 /* interrupt enable register */
62 #define IER_ERXRDY 0x1 /* int on rx ready */
63 #define IER_ETXRDY 0x2 /* int on tx ready */
64 #define IER_ERLS 0x4 /* int on line status change */
65 #define IER_EMSC 0x8 /* int on modem status change */
67 /* interrupt identification register */
68 #define IIR_FIFO_MASK 0xc0 /* set if FIFOs are enabled */
69 #define IIR_IMASK 0xf /* interrupt cause mask */
70 #define IIR_NOPEND 0x1 /* nothing pending */
71 #define IIR_RLS 0x6 /* receive line status */
72 #define IIR_RXRDY 0x4 /* receive ready */
73 #define IIR_RXTOUT 0xc /* receive timeout */
74 #define IIR_TXRDY 0x2 /* transmit ready */
75 #define IIR_MLSC 0x0 /* modem status */
78 /* fifo control register */
79 #define FIFO_ENABLE 0x01 /* enable fifo */
80 #define FIFO_RCV_RST 0x02 /* reset receive fifo */
81 #define FIFO_XMT_RST 0x04 /* reset transmit fifo */
82 #define FIFO_DMA_MODE 0x08 /* enable dma mode */
83 #define FIFO_TRIGGER_1 0x00 /* trigger at 1 char */
84 #define FIFO_TRIGGER_4 0x40 /* trigger at 4 chars */
85 #define FIFO_TRIGGER_8 0x80 /* trigger at 8 chars */
86 #define FIFO_TRIGGER_14 0xc0 /* trigger at 14 chars */
88 /* character format control register */
89 #define CFCR_DLAB 0x80 /* divisor latch */
90 #define CFCR_SBREAK 0x40 /* send break */
91 #define CFCR_PZERO 0x30 /* zero parity */
92 #define CFCR_PONE 0x20 /* one parity */
93 #define CFCR_PEVEN 0x10 /* even parity */
94 #define CFCR_PODD 0x00 /* odd parity */
95 #define CFCR_PENAB 0x08 /* parity enable */
96 #define CFCR_STOPB 0x04 /* 2 stop bits */
97 #define CFCR_8BITS 0x03 /* 8 data bits */
98 #define CFCR_7BITS 0x02 /* 7 data bits */
99 #define CFCR_6BITS 0x01 /* 6 data bits */
100 #define CFCR_5BITS 0x00 /* 5 data bits */
102 /* modem control register */
103 #define MCR_LOOPBACK 0x10 /* loopback */
104 #define MCR_IENABLE 0x08 /* output 2 = int enable */
105 #define MCR_DRS 0x04 /* output 1 = xxx */
106 #define MCR_RTS 0x02 /* enable RTS */
107 #define MCR_DTR 0x01 /* enable DTR */
109 /* line status register */
110 #define LSR_RCV_FIFO 0x80 /* error in receive fifo */
111 #define LSR_TSRE 0x40 /* transmitter empty */
112 #define LSR_TXRDY 0x20 /* transmitter ready */
113 #define LSR_BI 0x10 /* break detected */
114 #define LSR_FE 0x08 /* framing error */
115 #define LSR_PE 0x04 /* parity error */
116 #define LSR_OE 0x02 /* overrun error */
117 #define LSR_RXRDY 0x01 /* receiver ready */
118 #define LSR_RCV_MASK 0x1f
120 /* modem status register */
125 #define MSR_DDCD 0x08
126 #define MSR_TERI 0x04
127 #define MSR_DDSR 0x02
128 #define MSR_DCTS 0x01
134 typedef unsigned long u_long;
136 /* 16550 rx fifo trigger point */
137 #define FIFO_TRIGGER FIFO_TRIGGER_4
139 /* input buffer size */
152 static int cnts[NCNT];
153 static char *cntnames[NCNT] =
155 /* h/w interrupt counts. */
156 "mlsc", "nopend", "txrdy", "?3",
157 "rxrdy", "?5", "rls", "?7",
158 "?8", "?9", "?a", "?b",
159 "rxtout", "?d", "?e", "?f",
161 "rxcnt", "txcnt", "stray", "swoflo"
164 #define COUNT(x) cnts[x]++
169 /* Main interrupt controller port addresses. */
170 #define ICU_BASE 0x20
171 #define ICU_OCW2 (ICU_BASE + 0)
172 #define ICU_MASK (ICU_BASE + 1)
174 /* Original interrupt controller mask register. */
175 unsigned char icu_oldmask;
177 /* Maximum of 8 interrupts (we don't handle the slave icu yet). */
180 static struct intrupt
183 struct dos_ttystate *port;
184 _go32_dpmi_seginfo old_rmhandler;
185 _go32_dpmi_seginfo old_pmhandler;
186 _go32_dpmi_seginfo new_rmhandler;
187 _go32_dpmi_seginfo new_pmhandler;
188 _go32_dpmi_registers regs;
193 static struct dos_ttystate
198 struct intrupt *intrupt;
201 unsigned char cbuf[CBSIZE];
205 unsigned char old_mcr;
214 COM1ADDR, 4, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
218 COM2ADDR, 3, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
222 COM3ADDR, 4, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
226 COM4ADDR, 3, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
230 static int dos_open (struct serial *scb, const char *name);
231 static void dos_raw (struct serial *scb);
232 static int dos_readchar (struct serial *scb, int timeout);
233 static int dos_setbaudrate (struct serial *scb, int rate);
234 static int dos_write (struct serial *scb, const char *str, int len);
235 static void dos_close (struct serial *scb);
236 static serial_ttystate dos_get_tty_state (struct serial *scb);
237 static int dos_set_tty_state (struct serial *scb, serial_ttystate state);
238 static int dos_baudconv (int rate);
240 #define inb(p,a) inportb((p)->base + (a))
241 #define outb(p,a,v) outportb((p)->base + (a), (v))
242 #define disable() asm volatile ("cli");
243 #define enable() asm volatile ("sti");
247 dos_getc (volatile struct dos_ttystate *port)
251 if (port->count == 0)
254 c = port->cbuf[port->first];
256 port->first = (port->first + 1) & (CBSIZE - 1);
264 dos_putc (int c, struct dos_ttystate *port)
266 if (port->count >= CBSIZE - 1)
268 port->cbuf[(port->first + port->count) & (CBSIZE - 1)] = c;
278 struct dos_ttystate *port;
279 unsigned char iir, lsr, c;
281 disable (); /* Paranoia */
282 outportb (ICU_OCW2, 0x20); /* End-Of-Interrupt */
287 port = intrupts[irq].port;
291 return; /* not open */
296 iir = inb (port, com_iir) & IIR_IMASK;
301 lsr = inb (port, com_lsr);
311 c = inb (port, com_data);
312 if (lsr & (LSR_BI | LSR_FE | LSR_PE | LSR_OE))
314 if (lsr & (LSR_BI | LSR_FE))
316 else if (lsr & LSR_PE)
322 if (dos_putc (c, port) < 0)
331 while ((lsr = inb (port, com_lsr)) & LSR_RXRDY);
335 /* could be used to flowcontrol Tx */
336 port->msr = inb (port, com_msr);
344 /* no more pending interrupts, all done */
348 /* unexpected interrupt, ignore */
355 #define ISRNAME(x) dos_comisr##x
356 #define ISR(x) static void ISRNAME(x)(void) {dos_comisr(x);}
358 ISR (0) ISR (1) ISR (2) ISR (3) /* OK */
359 ISR (4) ISR (5) ISR (6) ISR (7) /* OK */
361 typedef void (*isr_t) (void);
363 static isr_t isrs[NINTR] =
365 ISRNAME (0), ISRNAME (1), ISRNAME (2), ISRNAME (3),
366 ISRNAME (4), ISRNAME (5), ISRNAME (6), ISRNAME (7)
371 static struct intrupt *
372 dos_hookirq (unsigned int irq)
374 struct intrupt *intr;
381 intr = &intrupts[irq];
388 /* setup real mode handler */
389 _go32_dpmi_get_real_mode_interrupt_vector (vec, &intr->old_rmhandler);
391 intr->new_rmhandler.pm_selector = _go32_my_cs ();
392 intr->new_rmhandler.pm_offset = (u_long) isr;
393 if (_go32_dpmi_allocate_real_mode_callback_iret (&intr->new_rmhandler,
399 if (_go32_dpmi_set_real_mode_interrupt_vector (vec, &intr->new_rmhandler))
404 /* setup protected mode handler */
405 _go32_dpmi_get_protected_mode_interrupt_vector (vec, &intr->old_pmhandler);
407 intr->new_pmhandler.pm_selector = _go32_my_cs ();
408 intr->new_pmhandler.pm_offset = (u_long) isr;
409 _go32_dpmi_allocate_iret_wrapper (&intr->new_pmhandler);
411 if (_go32_dpmi_set_protected_mode_interrupt_vector (vec,
412 &intr->new_pmhandler))
417 /* setup interrupt controller mask */
419 outportb (ICU_MASK, inportb (ICU_MASK) & ~(1 << irq));
428 dos_unhookirq (struct intrupt *intr)
430 unsigned int irq, vec;
433 irq = intr - intrupts;
436 /* restore old interrupt mask bit */
439 outportb (ICU_MASK, inportb (ICU_MASK) | (mask & icu_oldmask));
442 /* remove real mode handler */
443 _go32_dpmi_set_real_mode_interrupt_vector (vec, &intr->old_rmhandler);
444 _go32_dpmi_free_real_mode_callback (&intr->new_rmhandler);
446 /* remove protected mode handler */
447 _go32_dpmi_set_protected_mode_interrupt_vector (vec, &intr->old_pmhandler);
448 _go32_dpmi_free_iret_wrapper (&intr->new_pmhandler);
455 dos_open (struct serial *scb, const char *name)
457 struct dos_ttystate *port;
460 if (strncasecmp (name, "/dev/", 5) == 0)
462 else if (strncasecmp (name, "\\dev\\", 5) == 0)
465 if (strlen (name) != 4 || strncasecmp (name, "com", 3) != 0)
471 if (name[3] < '1' || name[3] > '4')
477 /* FIXME: this is a Bad Idea (tm)! One should *never* invent file
478 handles, since they might be already used by other files/devices.
479 The Right Way to do this is to create a real handle by dup()'ing
480 some existing one. */
483 if (port->refcnt++ > 0)
485 /* Device already opened another user. Just point at it. */
490 /* force access to ID reg */
491 outb (port, com_cfcr, 0);
492 outb (port, com_iir, 0);
493 for (i = 0; i < 17; i++)
495 if ((inb (port, com_iir) & 0x38) == 0)
497 (void) inb (port, com_data); /* clear recv */
503 /* disable all interrupts in chip */
504 outb (port, com_ier, 0);
506 /* tentatively enable 16550 fifo, and see if it responds */
507 outb (port, com_fifo,
508 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER);
510 port->fifo = ((inb (port, com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK);
512 /* clear pending status reports. */
513 (void) inb (port, com_lsr);
514 (void) inb (port, com_msr);
516 /* enable external interrupt gate (to avoid floating IRQ) */
517 outb (port, com_mcr, MCR_IENABLE);
519 /* hook up interrupt handler and initialise icu */
520 port->intrupt = dos_hookirq (port->irq);
523 outb (port, com_mcr, 0);
524 outb (port, com_fifo, 0);
532 port->intrupt->port = port;
535 /* clear rx buffer, tx busy flag and overflow count */
536 port->first = port->count = 0;
540 /* set default baud rate and mode: 9600,8,n,1 */
541 i = dos_baudconv (port->baudrate = 9600);
542 outb (port, com_cfcr, CFCR_DLAB);
543 outb (port, com_dlbl, i & 0xff);
544 outb (port, com_dlbh, i >> 8);
545 outb (port, com_cfcr, CFCR_8BITS);
547 /* enable all interrupts */
548 outb (port, com_ier, IER_ETXRDY | IER_ERXRDY | IER_ERLS | IER_EMSC);
550 /* enable DTR & RTS */
551 outb (port, com_mcr, MCR_DTR | MCR_RTS | MCR_IENABLE);
560 dos_close (struct serial *scb)
562 struct dos_ttystate *port;
563 struct intrupt *intrupt;
568 port = &ports[scb->fd];
570 if (port->refcnt-- > 1)
573 if (!(intrupt = port->intrupt))
576 /* disable interrupts, fifo, flow control */
580 outb (port, com_fifo, 0);
581 outb (port, com_ier, 0);
584 /* unhook handler, and disable interrupt gate */
585 dos_unhookirq (intrupt);
586 outb (port, com_mcr, 0);
588 /* Check for overflow errors */
591 fprintf_unfiltered (gdb_stderr,
592 "Serial input overruns occurred.\n");
593 fprintf_unfiltered (gdb_stderr, "This system %s handle %d baud.\n",
594 port->fifo ? "cannot" : "needs a 16550 to",
602 dos_noop (struct serial *scb)
608 dos_raw (struct serial *scb)
610 /* Always in raw mode */
614 dos_readchar (struct serial *scb, int timeout)
616 struct dos_ttystate *port = &ports[scb->fd];
620 then = rawclock () + (timeout * RAWHZ);
621 while ((c = dos_getc (port)) < 0)
623 if (timeout >= 0 && (rawclock () - then) >= 0)
624 return SERIAL_TIMEOUT;
631 static serial_ttystate
632 dos_get_tty_state (struct serial *scb)
634 struct dos_ttystate *port = &ports[scb->fd];
635 struct dos_ttystate *state;
637 /* Are they asking about a port we opened? */
638 if (port->refcnt <= 0)
640 /* We've never heard about this port. We should fail this call,
641 unless they are asking about one of the 3 standard handles,
642 in which case we pretend the handle was open by us if it is
643 connected to a terminal device. This is beacuse Unix
644 terminals use the serial interface, so GDB expects the
645 standard handles to go through here. */
646 if (scb->fd >= 3 || !isatty (scb->fd))
650 state = (struct dos_ttystate *) xmalloc (sizeof *state);
652 return (serial_ttystate) state;
656 dos_set_tty_state (struct serial *scb, serial_ttystate ttystate)
658 struct dos_ttystate *state;
660 state = (struct dos_ttystate *) ttystate;
661 dos_setbaudrate (scb, state->baudrate);
666 dos_noflush_set_tty_state (struct serial *scb, serial_ttystate new_ttystate,
667 serial_ttystate old_ttystate)
669 struct dos_ttystate *state;
671 state = (struct dos_ttystate *) new_ttystate;
672 dos_setbaudrate (scb, state->baudrate);
677 dos_flush_input (struct serial *scb)
679 struct dos_ttystate *port = &ports[scb->fd];
681 port->first = port->count = 0;
683 outb (port, com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_TRIGGER);
689 dos_print_tty_state (struct serial *scb, serial_ttystate ttystate,
690 struct ui_file *stream)
692 /* Nothing to print */
697 dos_baudconv (int rate)
704 #define divrnd(n, q) (((n) * 2 / (q) + 1) / 2) /* divide and round off */
705 x = divrnd (COMTICK, rate);
709 err = divrnd (1000 * COMTICK, x * rate) - 1000;
712 if (err > SPEED_TOLERANCE)
720 dos_setbaudrate (struct serial *scb, int rate)
722 struct dos_ttystate *port = &ports[scb->fd];
724 if (port->baudrate != rate)
729 x = dos_baudconv (rate);
732 fprintf_unfiltered (gdb_stderr, "%d: impossible baudrate\n", rate);
738 cfcr = inb (port, com_cfcr);
740 outb (port, com_cfcr, CFCR_DLAB);
741 outb (port, com_dlbl, x & 0xff);
742 outb (port, com_dlbh, x >> 8);
743 outb (port, com_cfcr, cfcr);
744 port->baudrate = rate;
752 dos_setstopbits (struct serial *scb, int num)
754 struct dos_ttystate *port = &ports[scb->fd];
758 cfcr = inb (port, com_cfcr);
762 case SERIAL_1_STOPBITS:
763 outb (port, com_cfcr, cfcr & ~CFCR_STOPB);
765 case SERIAL_1_AND_A_HALF_STOPBITS:
766 case SERIAL_2_STOPBITS:
767 outb (port, com_cfcr, cfcr | CFCR_STOPB);
779 dos_write (struct serial *scb, const char *str, int len)
781 volatile struct dos_ttystate *port = &ports[scb->fd];
782 int fifosize = port->fifo ? 16 : 1;
788 /* send the data, fifosize bytes at a time */
789 cnt = fifosize > len ? len : fifosize;
792 up the communications with UARTs with FIFOs. */
793 #ifdef UART_FIFO_WORKS
794 outportsb (port->base + com_data, str, cnt);
798 for ( ; cnt > 0; cnt--, len--)
799 outportb (port->base + com_data, *str++);
804 /* wait for transmission to complete (max 1 sec) */
805 then = rawclock () + RAWHZ;
808 if ((rawclock () - then) >= 0)
820 dos_sendbreak (struct serial *scb)
822 volatile struct dos_ttystate *port = &ports[scb->fd];
826 cfcr = inb (port, com_cfcr);
827 outb (port, com_cfcr, cfcr | CFCR_SBREAK);
830 then = rawclock () + RAWHZ / 4;
831 while ((rawclock () - then) < 0)
834 outb (port, com_cfcr, cfcr);
839 static struct serial_ops dos_ops =
847 dos_noop, /* flush output */
854 dos_noflush_set_tty_state,
857 dos_noop, /* wait for output to drain */
858 (void (*)(struct serial *, int))NULL /* change into async mode */
863 dos_info (char *arg, int from_tty)
865 struct dos_ttystate *port;
870 for (port = ports; port < &ports[4]; port++)
872 if (port->baudrate == 0)
874 printf_filtered ("Port:\tCOM%ld (%sactive)\n", (long)(port - ports) + 1,
875 port->intrupt ? "" : "not ");
876 printf_filtered ("Addr:\t0x%03x (irq %d)\n", port->base, port->irq);
877 printf_filtered ("16550:\t%s\n", port->fifo ? "yes" : "no");
878 printf_filtered ("Speed:\t%d baud\n", port->baudrate);
879 printf_filtered ("Errs:\tframing %d parity %d overflow %d\n\n",
880 port->ferr, port->perr, port->oflo);
884 printf_filtered ("\nTotal interrupts: %d\n", intrcnt);
885 for (i = 0; i < NCNT; i++)
887 printf_filtered ("%s:\t%d\n", cntnames[i], cnts[i]);
893 _initialize_ser_dos (void)
895 serial_add_interface (&dos_ops);
897 /* Save original interrupt mask register. */
898 icu_oldmask = inportb (ICU_MASK);
900 /* Mark fixed motherboard irqs as inuse. */
901 intrupts[0].inuse = /* timer tick */
902 intrupts[1].inuse = /* keyboard */
903 intrupts[2].inuse = 1; /* slave icu */
905 add_setshow_zinteger_cmd ("com1base", class_obscure, &ports[0].base, _("\
906 Set COM1 base i/o port address."), _("\
907 Show COM1 base i/o port address."), NULL,
909 NULL, /* FIXME: i18n: */
910 &setlist, &showlist);
912 add_setshow_zinteger_cmd ("com1irq", class_obscure, &ports[0].irq, _("\
913 Set COM1 interrupt request."), _("\
914 Show COM1 interrupt request."), NULL,
916 NULL, /* FIXME: i18n: */
917 &setlist, &showlist);
919 add_setshow_zinteger_cmd ("com2base", class_obscure, &ports[1].base, _("\
920 Set COM2 base i/o port address."), _("\
921 Show COM2 base i/o port address."), NULL,
923 NULL, /* FIXME: i18n: */
924 &setlist, &showlist);
926 add_setshow_zinteger_cmd ("com2irq", class_obscure, &ports[1].irq, _("\
927 Set COM2 interrupt request."), _("\
928 Show COM2 interrupt request."), NULL,
930 NULL, /* FIXME: i18n: */
931 &setlist, &showlist);
933 add_setshow_zinteger_cmd ("com3base", class_obscure, &ports[2].base, _("\
934 Set COM3 base i/o port address."), _("\
935 Show COM3 base i/o port address."), NULL,
937 NULL, /* FIXME: i18n: */
938 &setlist, &showlist);
940 add_setshow_zinteger_cmd ("com3irq", class_obscure, &ports[2].irq, _("\
941 Set COM3 interrupt request."), _("\
942 Show COM3 interrupt request."), NULL,
944 NULL, /* FIXME: i18n: */
945 &setlist, &showlist);
947 add_setshow_zinteger_cmd ("com4base", class_obscure, &ports[3].base, _("\
948 Set COM4 base i/o port address."), _("\
949 Show COM4 base i/o port address."), NULL,
951 NULL, /* FIXME: i18n: */
952 &setlist, &showlist);
954 add_setshow_zinteger_cmd ("com4irq", class_obscure, &ports[3].irq, _("\
955 Set COM4 interrupt request."), _("\
956 Show COM4 interrupt request."), NULL,
958 NULL, /* FIXME: i18n: */
959 &setlist, &showlist);
961 add_info ("serial", dos_info,
962 _("Print DOS serial port status."));