1 /* Remote serial interface for local (hardwired) serial ports for GO32.
2 Copyright 1992, 1993, 2000 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 2 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, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA. */
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 */
142 /* return raw 18Hz clock count */
143 extern long rawclock (void);
155 static int cnts
[NCNT
];
156 static char *cntnames
[NCNT
] =
158 /* h/w interrupt counts. */
159 "mlsc", "nopend", "txrdy", "?3",
160 "rxrdy", "?5", "rls", "?7",
161 "?8", "?9", "?a", "?b",
162 "rxtout", "?d", "?e", "?f",
164 "rxcnt", "txcnt", "stray", "swoflo"
167 #define COUNT(x) cnts[x]++
172 /* Main interrupt controller port addresses. */
173 #define ICU_BASE 0x20
174 #define ICU_OCW2 (ICU_BASE + 0)
175 #define ICU_MASK (ICU_BASE + 1)
177 /* Original interrupt controller mask register. */
178 unsigned char icu_oldmask
;
180 /* Maximum of 8 interrupts (we don't handle the slave icu yet). */
183 static struct intrupt
186 struct dos_ttystate
*port
;
187 _go32_dpmi_seginfo old_rmhandler
;
188 _go32_dpmi_seginfo old_pmhandler
;
189 _go32_dpmi_seginfo new_rmhandler
;
190 _go32_dpmi_seginfo new_pmhandler
;
191 _go32_dpmi_registers regs
;
196 static struct dos_ttystate
201 struct intrupt
*intrupt
;
204 unsigned char cbuf
[CBSIZE
];
208 unsigned char old_mcr
;
217 COM1ADDR
, 4, 0, NULL
, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
221 COM2ADDR
, 3, 0, NULL
, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
225 COM3ADDR
, 4, 0, NULL
, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
229 COM4ADDR
, 3, 0, NULL
, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
233 static int dos_open (serial_t scb
, const char *name
);
234 static void dos_raw (serial_t scb
);
235 static int dos_readchar (serial_t scb
, int timeout
);
236 static int dos_setbaudrate (serial_t scb
, int rate
);
237 static int dos_write (serial_t scb
, const char *str
, int len
);
238 static void dos_close (serial_t scb
);
239 static serial_ttystate
dos_get_tty_state (serial_t scb
);
240 static int dos_set_tty_state (serial_t scb
, serial_ttystate state
);
241 static int dos_baudconv (int rate
);
243 #define inb(p,a) inportb((p)->base + (a))
244 #define outb(p,a,v) outportb((p)->base + (a), (v))
245 #define disable() asm volatile ("cli");
246 #define enable() asm volatile ("sti");
250 dos_getc (volatile struct dos_ttystate
*port
)
254 if (port
->count
== 0)
257 c
= port
->cbuf
[port
->first
];
259 port
->first
= (port
->first
+ 1) & (CBSIZE
- 1);
267 dos_putc (int c
, struct dos_ttystate
*port
)
269 if (port
->count
>= CBSIZE
- 1)
271 port
->cbuf
[(port
->first
+ port
->count
) & (CBSIZE
- 1)] = c
;
281 struct dos_ttystate
*port
;
282 unsigned char iir
, lsr
, c
;
284 disable (); /* Paranoia */
285 outportb (ICU_OCW2
, 0x20); /* End-Of-Interrupt */
290 port
= intrupts
[irq
].port
;
294 return; /* not open */
299 iir
= inb (port
, com_iir
) & IIR_IMASK
;
304 lsr
= inb (port
, com_lsr
);
314 c
= inb (port
, com_data
);
315 if (lsr
& (LSR_BI
| LSR_FE
| LSR_PE
| LSR_OE
))
317 if (lsr
& (LSR_BI
| LSR_FE
))
319 else if (lsr
& LSR_PE
)
325 if (dos_putc (c
, port
) < 0)
334 while ((lsr
= inb (port
, com_lsr
)) & LSR_RXRDY
);
338 /* could be used to flowcontrol Tx */
339 port
->msr
= inb (port
, com_msr
);
347 /* no more pending interrupts, all done */
351 /* unexpected interrupt, ignore */
359 #define ISRNAME(x) dos_comisr##x
361 #define ISRNAME(x) dos_comisr/**/x
363 #define ISR(x) static void ISRNAME(x)() {dos_comisr(x);}
365 ISR (0) ISR (1) ISR (2) ISR (3)
366 ISR (4) ISR (5) ISR (6) ISR (7)
368 typedef void (*isr_t
) ();
370 static isr_t isrs
[NINTR
] =
372 ISRNAME (0), ISRNAME (1), ISRNAME (2), ISRNAME (3),
373 ISRNAME (4), ISRNAME (5), ISRNAME (6), ISRNAME (7)
378 static struct intrupt
*
382 struct intrupt
*intr
;
389 intr
= &intrupts
[irq
];
396 /* setup real mode handler */
397 _go32_dpmi_get_real_mode_interrupt_vector (vec
, &intr
->old_rmhandler
);
399 intr
->new_rmhandler
.pm_selector
= _go32_my_cs ();
400 intr
->new_rmhandler
.pm_offset
= (u_long
) isr
;
401 if (_go32_dpmi_allocate_real_mode_callback_iret (&intr
->new_rmhandler
,
407 if (_go32_dpmi_set_real_mode_interrupt_vector (vec
, &intr
->new_rmhandler
))
412 /* setup protected mode handler */
413 _go32_dpmi_get_protected_mode_interrupt_vector (vec
, &intr
->old_pmhandler
);
415 intr
->new_pmhandler
.pm_selector
= _go32_my_cs ();
416 intr
->new_pmhandler
.pm_offset
= (u_long
) isr
;
417 _go32_dpmi_allocate_iret_wrapper (&intr
->new_pmhandler
);
419 if (_go32_dpmi_set_protected_mode_interrupt_vector (vec
, &intr
->new_pmhandler
))
424 /* setup interrupt controller mask */
426 outportb (ICU_MASK
, inportb (ICU_MASK
) & ~(1 << irq
));
435 dos_unhookirq (struct intrupt
*intr
)
437 unsigned int irq
, vec
;
440 irq
= intr
- intrupts
;
443 /* restore old interrupt mask bit */
446 outportb (ICU_MASK
, inportb (ICU_MASK
) | (mask
& icu_oldmask
));
449 /* remove real mode handler */
450 _go32_dpmi_set_real_mode_interrupt_vector (vec
, &intr
->old_rmhandler
);
451 _go32_dpmi_free_real_mode_callback (&intr
->new_rmhandler
);
453 /* remove protected mode handler */
454 _go32_dpmi_set_protected_mode_interrupt_vector (vec
, &intr
->old_pmhandler
);
455 _go32_dpmi_free_iret_wrapper (&intr
->new_pmhandler
);
462 dos_open (serial_t scb
, const char *name
)
464 struct dos_ttystate
*port
;
467 if (strncasecmp (name
, "/dev/", 5) == 0)
469 else if (strncasecmp (name
, "\\dev\\", 5) == 0)
472 if (strlen (name
) != 4 || strncasecmp (name
, "com", 3) != 0)
478 if (name
[3] < '1' || name
[3] > '4')
484 /* FIXME: this is a Bad Idea (tm)! One should *never* invent file
485 handles, since they might be already used by other files/devices.
486 The Right Way to do this is to create a real handle by dup()'ing
487 some existing one. */
490 if (port
->refcnt
++ > 0)
492 /* Device already opened another user. Just point at it. */
497 /* force access to ID reg */
498 outb (port
, com_cfcr
, 0);
499 outb (port
, com_iir
, 0);
500 for (i
= 0; i
< 17; i
++)
502 if ((inb (port
, com_iir
) & 0x38) == 0)
504 (void) inb (port
, com_data
); /* clear recv */
510 /* disable all interrupts in chip */
511 outb (port
, com_ier
, 0);
513 /* tentatively enable 16550 fifo, and see if it responds */
514 outb (port
, com_fifo
, FIFO_ENABLE
| FIFO_RCV_RST
| FIFO_XMT_RST
| FIFO_TRIGGER
);
516 port
->fifo
= ((inb (port
, com_iir
) & IIR_FIFO_MASK
) == IIR_FIFO_MASK
);
518 /* clear pending status reports. */
519 (void) inb (port
, com_lsr
);
520 (void) inb (port
, com_msr
);
522 /* enable external interrupt gate (to avoid floating IRQ) */
523 outb (port
, com_mcr
, MCR_IENABLE
);
525 /* hook up interrupt handler and initialise icu */
526 port
->intrupt
= dos_hookirq (port
->irq
);
529 outb (port
, com_mcr
, 0);
530 outb (port
, com_fifo
, 0);
538 port
->intrupt
->port
= port
;
541 /* clear rx buffer, tx busy flag and overflow count */
542 port
->first
= port
->count
= 0;
546 /* set default baud rate and mode: 9600,8,n,1 */
547 i
= dos_baudconv (port
->baudrate
= 9600);
548 outb (port
, com_cfcr
, CFCR_DLAB
);
549 outb (port
, com_dlbl
, i
& 0xff);
550 outb (port
, com_dlbh
, i
>> 8);
551 outb (port
, com_cfcr
, CFCR_8BITS
);
553 /* enable all interrupts */
554 outb (port
, com_ier
, IER_ETXRDY
| IER_ERXRDY
| IER_ERLS
| IER_EMSC
);
556 /* enable DTR & RTS */
557 outb (port
, com_mcr
, MCR_DTR
| MCR_RTS
| MCR_IENABLE
);
566 dos_close (serial_t scb
)
568 struct dos_ttystate
*port
;
569 struct intrupt
*intrupt
;
574 port
= &ports
[scb
->fd
];
576 if (port
->refcnt
-- > 1)
579 if (!(intrupt
= port
->intrupt
))
582 /* disable interrupts, fifo, flow control */
586 outb (port
, com_fifo
, 0);
587 outb (port
, com_ier
, 0);
590 /* unhook handler, and disable interrupt gate */
591 dos_unhookirq (intrupt
);
592 outb (port
, com_mcr
, 0);
594 /* Check for overflow errors */
597 fprintf_unfiltered (gdb_stderr
,
598 "Serial input overruns occurred.\n");
599 fprintf_unfiltered (gdb_stderr
, "This system %s handle %d baud.\n",
600 port
->fifo
? "cannot" : "needs a 16550 to",
608 dos_noop (serial_t scb ATTRIBUTE_UNUSED
)
614 dos_raw (serial_t scb ATTRIBUTE_UNUSED
)
616 /* Always in raw mode */
620 dos_readchar (serial_t scb
, int timeout
)
622 struct dos_ttystate
*port
= &ports
[scb
->fd
];
626 then
= rawclock () + (timeout
* RAWHZ
);
627 while ((c
= dos_getc (port
)) < 0)
629 if (timeout
>= 0 && (rawclock () - then
) >= 0)
630 return SERIAL_TIMEOUT
;
638 static serial_ttystate
639 dos_get_tty_state (serial_t scb
)
641 struct dos_ttystate
*port
= &ports
[scb
->fd
];
642 struct dos_ttystate
*state
;
644 /* Are they asking about a port we opened? */
645 if (port
->refcnt
<= 0)
647 /* We've never heard about this port. We should fail this call,
648 unless they are asking about one of the 3 standard handles,
649 in which case we pretend the handle was open by us if it is
650 connected to a terminal device. This is beacuse Unix
651 terminals use the serial interface, so GDB expects the
652 standard handles to go through here. */
653 if (scb
->fd
>= 3 || !isatty (scb
->fd
))
657 state
= (struct dos_ttystate
*) xmalloc (sizeof *state
);
659 return (serial_ttystate
) state
;
663 dos_set_tty_state (serial_t scb
, serial_ttystate ttystate
)
665 struct dos_ttystate
*state
;
667 state
= (struct dos_ttystate
*) ttystate
;
668 dos_setbaudrate (scb
, state
->baudrate
);
673 dos_noflush_set_tty_state (serial_t scb
, serial_ttystate new_ttystate
,
674 serial_ttystate old_ttystate ATTRIBUTE_UNUSED
)
676 struct dos_ttystate
*state
;
678 state
= (struct dos_ttystate
*) new_ttystate
;
679 dos_setbaudrate (scb
, state
->baudrate
);
684 dos_flush_input (serial_t scb
)
686 struct dos_ttystate
*port
= &ports
[scb
->fd
];
688 port
->first
= port
->count
= 0;
690 outb (port
, com_fifo
, FIFO_ENABLE
| FIFO_RCV_RST
| FIFO_TRIGGER
);
696 dos_print_tty_state (serial_t scb ATTRIBUTE_UNUSED
,
697 serial_ttystate ttystate ATTRIBUTE_UNUSED
,
698 struct ui_file
*stream ATTRIBUTE_UNUSED
)
700 /* Nothing to print */
705 dos_baudconv (int rate
)
712 #define divrnd(n, q) (((n) * 2 / (q) + 1) / 2) /* divide and round off */
713 x
= divrnd (COMTICK
, rate
);
717 err
= divrnd (1000 * COMTICK
, x
* rate
) - 1000;
720 if (err
> SPEED_TOLERANCE
)
728 dos_setbaudrate (serial_t scb
, int rate
)
730 struct dos_ttystate
*port
= &ports
[scb
->fd
];
732 if (port
->baudrate
!= rate
)
737 x
= dos_baudconv (rate
);
740 fprintf_unfiltered (gdb_stderr
, "%d: impossible baudrate\n", rate
);
746 cfcr
= inb (port
, com_cfcr
);
748 outb (port
, com_cfcr
, CFCR_DLAB
);
749 outb (port
, com_dlbl
, x
& 0xff);
750 outb (port
, com_dlbh
, x
>> 8);
751 outb (port
, com_cfcr
, cfcr
);
752 port
->baudrate
= rate
;
760 dos_setstopbits (serial_t scb
, int num
)
762 struct dos_ttystate
*port
= &ports
[scb
->fd
];
766 cfcr
= inb (port
, com_cfcr
);
770 case SERIAL_1_STOPBITS
:
771 outb (port
, com_cfcr
, cfcr
& ~CFCR_STOPB
);
773 case SERIAL_1_AND_A_HALF_STOPBITS
:
774 case SERIAL_2_STOPBITS
:
775 outb (port
, com_cfcr
, cfcr
| CFCR_STOPB
);
787 dos_write (serial_t scb
, const char *str
, int len
)
789 volatile struct dos_ttystate
*port
= &ports
[scb
->fd
];
790 int fifosize
= port
->fifo
? 16 : 1;
796 /* send the data, fifosize bytes at a time */
797 cnt
= fifosize
> len
? len
: fifosize
;
799 outportsb (port
->base
+ com_data
, str
, cnt
);
805 /* wait for transmission to complete (max 1 sec) */
806 then
= rawclock () + RAWHZ
;
809 if ((rawclock () - then
) >= 0)
821 dos_sendbreak (serial_t scb
)
823 volatile struct dos_ttystate
*port
= &ports
[scb
->fd
];
827 cfcr
= inb (port
, com_cfcr
);
828 outb (port
, com_cfcr
, cfcr
| CFCR_SBREAK
);
831 then
= rawclock () + RAWHZ
/ 4;
832 while ((rawclock () - then
) < 0)
835 outb (port
, com_cfcr
, cfcr
);
840 static struct serial_ops dos_ops
=
848 dos_noop
, /* flush output */
855 dos_noflush_set_tty_state
,
858 dos_noop
, /* wait for output to drain */
859 (void (*)(serial_t
, int))NULL
/* change into async mode */
864 dos_info (char *arg ATTRIBUTE_UNUSED
, int from_tty ATTRIBUTE_UNUSED
)
866 struct dos_ttystate
*port
;
871 for (port
= ports
; port
< &ports
[4]; port
++)
873 if (port
->baudrate
== 0)
875 printf_filtered ("Port:\tCOM%ld (%sactive)\n", (long)(port
- ports
) + 1,
876 port
->intrupt
? "" : "not ");
877 printf_filtered ("Addr:\t0x%03x (irq %d)\n", port
->base
, port
->irq
);
878 printf_filtered ("16550:\t%s\n", port
->fifo
? "yes" : "no");
879 printf_filtered ("Speed:\t%d baud\n", port
->baudrate
);
880 printf_filtered ("Errs:\tframing %d parity %d overflow %d\n\n",
881 port
->ferr
, port
->perr
, port
->oflo
);
885 printf_filtered ("\nTotal interrupts: %d\n", intrcnt
);
886 for (i
= 0; i
< NCNT
; i
++)
888 printf_filtered ("%s:\t%d\n", cntnames
[i
], cnts
[i
]);
894 _initialize_ser_dos (void)
896 serial_add_interface (&dos_ops
);
898 /* Save original interrupt mask register. */
899 icu_oldmask
= inportb (ICU_MASK
);
901 /* Mark fixed motherboard irqs as inuse. */
902 intrupts
[0].inuse
= /* timer tick */
903 intrupts
[1].inuse
= /* keyboard */
904 intrupts
[2].inuse
= 1; /* slave icu */
907 add_set_cmd ("com1base", class_obscure
, var_zinteger
,
908 (char *) &ports
[0].base
,
909 "Set COM1 base i/o port address.",
914 add_set_cmd ("com1irq", class_obscure
, var_zinteger
,
915 (char *) &ports
[0].irq
,
916 "Set COM1 interrupt request.",
921 add_set_cmd ("com2base", class_obscure
, var_zinteger
,
922 (char *) &ports
[1].base
,
923 "Set COM2 base i/o port address.",
928 add_set_cmd ("com2irq", class_obscure
, var_zinteger
,
929 (char *) &ports
[1].irq
,
930 "Set COM2 interrupt request.",
935 add_set_cmd ("com3base", class_obscure
, var_zinteger
,
936 (char *) &ports
[2].base
,
937 "Set COM3 base i/o port address.",
942 add_set_cmd ("com3irq", class_obscure
, var_zinteger
,
943 (char *) &ports
[2].irq
,
944 "Set COM3 interrupt request.",
949 add_set_cmd ("com4base", class_obscure
, var_zinteger
,
950 (char *) &ports
[3].base
,
951 "Set COM4 base i/o port address.",
956 add_set_cmd ("com4irq", class_obscure
, var_zinteger
,
957 (char *) &ports
[3].irq
,
958 "Set COM4 interrupt request.",
962 add_info ("serial", dos_info
,
963 "Print DOS serial port status.");