]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ser-go32.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / ser-go32.c
CommitLineData
c906108c
SS
1/* Remote serial interface for local (hardwired) serial ports for
2 GO32. Copyright 1992, 1993 Free Software Foundation, Inc.
3
4 Contributed by Nigel Stephens, Algorithmics Ltd. (nigel@algor.co.uk).
5
6 This version uses DPMI interrupts to handle buffered i/o
7 without the separate "asynctsr" program.
8
9 This file is part of GDB.
10
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.
15
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.
20
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
c5aa993b
JM
23 Foundation, Inc., 59 Temple Place - Suite 330,
24 Boston, MA 02111-1307, USA. */
c906108c
SS
25
26#include "defs.h"
27#include "gdbcmd.h"
28#include "serial.h"
29
30
31/*
32 * NS16550 UART registers
33 */
34
35#define COM1ADDR 0x3f8
36#define COM2ADDR 0x2f8
37#define COM3ADDR 0x3e8
38#define COM4ADDR 0x3e0
39
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) */
51
52/*
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.
57 */
58#define COMTICK (1843200/16)
59#define SPEED_TOLERANCE 30 /* thousandths; real == desired +- 3.0% */
60
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 */
66
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 */
76
77
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 */
87
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 */
101
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 */
108
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
119
120/* modem status register */
121#define MSR_DCD 0x80
122#define MSR_RI 0x40
123#define MSR_DSR 0x20
124#define MSR_CTS 0x10
125#define MSR_DDCD 0x08
126#define MSR_TERI 0x04
127#define MSR_DDSR 0x02
128#define MSR_DCTS 0x01
129
b83266a0
SS
130#include <dos.h>
131#include <go32.h>
132#include <dpmi.h>
133typedef unsigned long u_long;
c906108c
SS
134
135/* DPMI Communication */
136static union REGS dpmi_regs;
137static struct SREGS dpmi_sregs;
138
139/* 16550 rx fifo trigger point */
140#define FIFO_TRIGGER FIFO_TRIGGER_4
141
142/* input buffer size */
143#define CBSIZE 4096
144
145/* return raw 18Hz clock count */
146extern long rawclock (void);
147
148#define RAWHZ 18
149
150#ifdef DOS_STATS
151#define CNT_RX 16
152#define CNT_TX 17
153#define CNT_STRAY 18
154#define CNT_ORUN 19
155#define NCNT 20
156
c5aa993b
JM
157static int intrcnt;
158static int cnts[NCNT];
159static char *cntnames[NCNT] =
160{
c906108c 161 /* h/w interrupt counts. */
c5aa993b
JM
162 "mlsc", "nopend", "txrdy", "?3",
163 "rxrdy", "?5", "rls", "?7",
164 "?8", "?9", "?a", "?b",
165 "rxtout", "?d", "?e", "?f",
c906108c 166 /* s/w counts. */
c5aa993b 167 "rxcnt", "txcnt", "stray", "swoflo"
c906108c
SS
168};
169
170#define COUNT(x) cnts[x]++
171#else
c5aa993b 172#define COUNT(x)
c906108c
SS
173#endif
174
175/* Main interrupt controller port addresses. */
176#define ICU_BASE 0x20
177#define ICU_OCW2 (ICU_BASE + 0)
178#define ICU_MASK (ICU_BASE + 1)
179
180/* Original interrupt controller mask register. */
c5aa993b 181unsigned char icu_oldmask;
c906108c
SS
182
183/* Maximum of 8 interrupts (we don't handle the slave icu yet). */
184#define NINTR 8
185
186static struct intrupt
c5aa993b
JM
187 {
188 char inuse;
189 struct dos_ttystate *port;
190 _go32_dpmi_seginfo old_rmhandler;
191 _go32_dpmi_seginfo old_pmhandler;
192 _go32_dpmi_seginfo new_rmhandler;
193 _go32_dpmi_seginfo new_pmhandler;
194 _go32_dpmi_registers regs;
195 }
196intrupts[NINTR];
c906108c
SS
197
198
199static struct dos_ttystate
c5aa993b
JM
200 {
201 int base;
202 int irq;
203 int refcnt;
204 struct intrupt *intrupt;
205 int fifo;
206 int baudrate;
207 unsigned char cbuf[CBSIZE];
208 unsigned int first;
209 unsigned int count;
210 int txbusy;
211 unsigned char old_mcr;
212 int ferr;
213 int perr;
214 int oflo;
215 int msr;
216 }
217ports[4] =
c906108c 218{
c5aa993b
JM
219 {
220 COM1ADDR, 4
221 }
222 ,
223 {
224 COM2ADDR, 3
225 }
226 ,
227 {
228 COM3ADDR, 4
229 }
230 ,
231 {
232 COM4ADDR, 3
233 }
c906108c
SS
234};
235
236static int dos_open PARAMS ((serial_t scb, const char *name));
237static void dos_raw PARAMS ((serial_t scb));
238static int dos_readchar PARAMS ((serial_t scb, int timeout));
239static int dos_setbaudrate PARAMS ((serial_t scb, int rate));
240static int dos_write PARAMS ((serial_t scb, const char *str, int len));
241static void dos_close PARAMS ((serial_t scb));
242static serial_ttystate dos_get_tty_state PARAMS ((serial_t scb));
243static int dos_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
244static int dos_baudconv PARAMS ((int rate));
245
246#define inb(p,a) inportb((p)->base + (a))
247#define outb(p,a,v) outportb((p)->base + (a), (v))
248#define disable() asm volatile ("cli");
249#define enable() asm volatile ("sti");
250
251
252static int
253dos_getc (port)
254 volatile struct dos_ttystate *port;
255{
c5aa993b 256 int c;
c906108c 257
c5aa993b
JM
258 if (port->count == 0)
259 return -1;
c906108c 260
c5aa993b
JM
261 c = port->cbuf[port->first];
262 disable ();
263 port->first = (port->first + 1) & (CBSIZE - 1);
264 port->count--;
265 enable ();
266 return c;
c906108c 267}
c906108c 268
c5aa993b
JM
269
270static int
c906108c
SS
271dos_putc (c, port)
272 int c;
273 struct dos_ttystate *port;
274{
c5aa993b
JM
275 if (port->count >= CBSIZE - 1)
276 return -1;
277 port->cbuf[(port->first + port->count) & (CBSIZE - 1)] = c;
278 port->count++;
279 return 0;
c906108c 280}
c906108c
SS
281\f
282
c5aa993b 283
c906108c
SS
284static void
285dos_comisr (irq)
286 int irq;
287{
288 struct dos_ttystate *port;
289 unsigned char iir, lsr, c;
290
291 disable (); /* Paranoia */
292 outportb (ICU_OCW2, 0x20); /* End-Of-Interrupt */
293#ifdef DOS_STATS
294 ++intrcnt;
295#endif
296
297 port = intrupts[irq].port;
c5aa993b 298 if (!port)
c906108c
SS
299 {
300 COUNT (CNT_STRAY);
c5aa993b 301 return; /* not open */
c906108c
SS
302 }
303
304 while (1)
305 {
306 iir = inb (port, com_iir) & IIR_IMASK;
c5aa993b 307 switch (iir)
c906108c 308 {
c5aa993b 309
c906108c
SS
310 case IIR_RLS:
311 lsr = inb (port, com_lsr);
312 goto rx;
c5aa993b 313
c906108c
SS
314 case IIR_RXTOUT:
315 case IIR_RXRDY:
316 lsr = 0;
c5aa993b
JM
317
318 rx:
319 do
c906108c
SS
320 {
321 c = inb (port, com_data);
322 if (lsr & (LSR_BI | LSR_FE | LSR_PE | LSR_OE))
323 {
324 if (lsr & (LSR_BI | LSR_FE))
325 port->ferr++;
326 else if (lsr & LSR_PE)
327 port->perr++;
328 if (lsr & LSR_OE)
329 port->oflo++;
330 }
331
332 if (dos_putc (c, port) < 0)
333 {
334 COUNT (CNT_ORUN);
335 }
336 else
337 {
338 COUNT (CNT_RX);
339 }
340 }
341 while ((lsr = inb (port, com_lsr)) & LSR_RXRDY);
342 break;
c5aa993b 343
c906108c
SS
344 case IIR_MLSC:
345 /* could be used to flowcontrol Tx */
346 port->msr = inb (port, com_msr);
347 break;
c5aa993b 348
c906108c
SS
349 case IIR_TXRDY:
350 port->txbusy = 0;
351 break;
352
353 case IIR_NOPEND:
354 /* no more pending interrupts, all done */
355 return;
356
357 default:
358 /* unexpected interrupt, ignore */
359 break;
360 }
361 COUNT (iir);
c5aa993b 362 }
c906108c
SS
363}
364
365#ifdef __STDC__
366#define ISRNAME(x) dos_comisr##x
367#else
368#define ISRNAME(x) dos_comisr/**/x
369#endif
370#define ISR(x) static void ISRNAME(x)() {dos_comisr(x);}
371
c5aa993b
JM
372ISR (0) ISR (1) ISR (2) ISR (3)
373ISR (4) ISR (5) ISR (6) ISR (7)
c906108c 374
c5aa993b 375 typedef void (*isr_t) ();
c906108c 376
c5aa993b
JM
377 static isr_t isrs[NINTR] =
378 {
379 ISRNAME (0), ISRNAME (1), ISRNAME (2), ISRNAME (3),
380 ISRNAME (4), ISRNAME (5), ISRNAME (6), ISRNAME (7)
c906108c 381};
c906108c
SS
382\f
383
c5aa993b
JM
384
385 static struct intrupt *
386 dos_hookirq (irq)
c906108c
SS
387 unsigned int irq;
388{
389 struct intrupt *intr;
390 unsigned int vec;
391 isr_t isr;
392
393 if (irq >= NINTR)
394 return 0;
395
396 intr = &intrupts[irq];
397 if (intr->inuse)
398 return 0;
c5aa993b 399
c906108c
SS
400 vec = 0x08 + irq;
401 isr = isrs[irq];
402
403 /* setup real mode handler */
404 _go32_dpmi_get_real_mode_interrupt_vector (vec, &intr->old_rmhandler);
405
c5aa993b
JM
406 intr->new_rmhandler.pm_selector = _go32_my_cs ();
407 intr->new_rmhandler.pm_offset = (u_long) isr;
c906108c
SS
408 if (_go32_dpmi_allocate_real_mode_callback_iret (&intr->new_rmhandler,
409 &intr->regs))
410 {
411 return 0;
412 }
413
414 if (_go32_dpmi_set_real_mode_interrupt_vector (vec, &intr->new_rmhandler))
415 {
416 return 0;
417 }
c5aa993b 418
c906108c 419 /* setup protected mode handler */
c5aa993b 420 _go32_dpmi_get_protected_mode_interrupt_vector (vec, &intr->old_pmhandler);
c906108c 421
c5aa993b
JM
422 intr->new_pmhandler.pm_selector = _go32_my_cs ();
423 intr->new_pmhandler.pm_offset = (u_long) isr;
c906108c
SS
424 _go32_dpmi_allocate_iret_wrapper (&intr->new_pmhandler);
425
c5aa993b 426 if (_go32_dpmi_set_protected_mode_interrupt_vector (vec, &intr->new_pmhandler))
c906108c
SS
427 {
428 return 0;
429 }
430
431 /* setup interrupt controller mask */
432 disable ();
433 outportb (ICU_MASK, inportb (ICU_MASK) & ~(1 << irq));
434 enable ();
435
436 intr->inuse = 1;
437 return intr;
438}
439
440
441static void
442dos_unhookirq (intr)
443 struct intrupt *intr;
444{
445 unsigned int irq, vec;
446 unsigned char mask;
447
448 irq = intr - intrupts;
449 vec = 0x08 + irq;
450
451 /* restore old interrupt mask bit */
452 mask = 1 << irq;
453 disable ();
454 outportb (ICU_MASK, inportb (ICU_MASK) | (mask & icu_oldmask));
455 enable ();
456
457 /* remove real mode handler */
458 _go32_dpmi_set_real_mode_interrupt_vector (vec, &intr->old_rmhandler);
459 _go32_dpmi_free_real_mode_callback (&intr->new_rmhandler);
c5aa993b 460
c906108c
SS
461 /* remove protected mode handler */
462 _go32_dpmi_set_protected_mode_interrupt_vector (vec, &intr->old_pmhandler);
463 _go32_dpmi_free_iret_wrapper (&intr->new_pmhandler);
464 intr->inuse = 0;
465}
c906108c
SS
466\f
467
c5aa993b 468
c906108c
SS
469static int
470dos_open (scb, name)
471 serial_t scb;
472 const char *name;
473{
474 struct dos_ttystate *port;
475 int fd, i;
476
477 if (strncasecmp (name, "/dev/", 5) == 0)
478 name += 5;
479 else if (strncasecmp (name, "\\dev\\", 5) == 0)
480 name += 5;
481
482 if (strlen (name) != 4 || strncasecmp (name, "com", 3) != 0)
483 {
484 errno = ENOENT;
485 return -1;
486 }
487
488 if (name[3] < '1' || name[3] > '4')
489 {
490 errno = ENOENT;
491 return -1;
492 }
493
494 fd = name[3] - '1';
495 port = &ports[fd];
496 if (port->refcnt++ > 0)
497 {
498 /* Device already opened another user. Just point at it. */
499 scb->fd = fd;
500 return 0;
501 }
502
503 /* force access to ID reg */
c5aa993b
JM
504 outb (port, com_cfcr, 0);
505 outb (port, com_iir, 0);
506 for (i = 0; i < 17; i++)
507 {
508 if ((inb (port, com_iir) & 0x38) == 0)
509 goto ok;
510 (void) inb (port, com_data); /* clear recv */
511 }
c906108c
SS
512 errno = ENODEV;
513 return -1;
514
515ok:
516 /* disable all interrupts in chip */
c5aa993b 517 outb (port, com_ier, 0);
c906108c
SS
518
519 /* tentatively enable 16550 fifo, and see if it responds */
c5aa993b
JM
520 outb (port, com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER);
521 sleep (1);
522 port->fifo = ((inb (port, com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK);
c906108c
SS
523
524 /* clear pending status reports. */
c5aa993b
JM
525 (void) inb (port, com_lsr);
526 (void) inb (port, com_msr);
c906108c
SS
527
528 /* enable external interrupt gate (to avoid floating IRQ) */
c5aa993b 529 outb (port, com_mcr, MCR_IENABLE);
c906108c
SS
530
531 /* hook up interrupt handler and initialise icu */
532 port->intrupt = dos_hookirq (port->irq);
533 if (!port->intrupt)
534 {
c5aa993b
JM
535 outb (port, com_mcr, 0);
536 outb (port, com_fifo, 0);
c906108c
SS
537 errno = ENODEV;
538 return -1;
539 }
540
541 disable ();
542
543 /* record port */
c5aa993b 544 port->intrupt->port = port;
c906108c
SS
545 scb->fd = fd;
546
547 /* clear rx buffer, tx busy flag and overflow count */
548 port->first = port->count = 0;
549 port->txbusy = 0;
550 port->oflo = 0;
551
552 /* set default baud rate and mode: 9600,8,n,1 */
553 i = dos_baudconv (port->baudrate = 9600);
c5aa993b
JM
554 outb (port, com_cfcr, CFCR_DLAB);
555 outb (port, com_dlbl, i & 0xff);
556 outb (port, com_dlbh, i >> 8);
557 outb (port, com_cfcr, CFCR_8BITS);
c906108c
SS
558
559 /* enable all interrupts */
c5aa993b 560 outb (port, com_ier, IER_ETXRDY | IER_ERXRDY | IER_ERLS | IER_EMSC);
c906108c
SS
561
562 /* enable DTR & RTS */
c5aa993b 563 outb (port, com_mcr, MCR_DTR | MCR_RTS | MCR_IENABLE);
c906108c
SS
564
565 enable ();
566
567 return 0;
568}
569
570
571static void
572dos_close (scb)
573 serial_t scb;
574{
c5aa993b
JM
575 struct dos_ttystate *port;
576 struct intrupt *intrupt;
c906108c 577
c5aa993b
JM
578 if (!scb)
579 return;
580
581 port = &ports[scb->fd];
582
583 if (port->refcnt-- > 1)
584 return;
c906108c 585
c5aa993b
JM
586 if (!(intrupt = port->intrupt))
587 return;
588
589 /* disable interrupts, fifo, flow control */
590 disable ();
591 port->intrupt = 0;
592 intrupt->port = 0;
593 outb (port, com_fifo, 0);
594 outb (port, com_ier, 0);
595 enable ();
596
597 /* unhook handler, and disable interrupt gate */
598 dos_unhookirq (intrupt);
599 outb (port, com_mcr, 0);
600
601 /* Check for overflow errors */
602 if (port->oflo)
603 {
604 fprintf_unfiltered (gdb_stderr,
605 "Serial input overruns occurred.\n");
606 fprintf_unfiltered (gdb_stderr, "This system %s handle %d baud.\n",
607 port->fifo ? "cannot" : "needs a 16550 to",
608 port->baudrate);
609 }
610}
c906108c
SS
611\f
612
c5aa993b 613
c906108c
SS
614static int
615dos_noop (scb)
616 serial_t scb;
617{
618 return 0;
619}
620
621static void
622dos_raw (scb)
623 serial_t scb;
624{
625 /* Always in raw mode */
626}
627
628static int
629dos_readchar (scb, timeout)
630 serial_t scb;
631 int timeout;
632{
633 struct dos_ttystate *port = &ports[scb->fd];
634 long then;
635 int c;
636
c5aa993b 637 then = rawclock () + (timeout * RAWHZ);
c906108c
SS
638 while ((c = dos_getc (port)) < 0)
639 {
640 if (timeout >= 0 && (rawclock () - then) >= 0)
641 return SERIAL_TIMEOUT;
642 notice_quit ();
643 }
644
645 return c;
646}
647
648
649static serial_ttystate
650dos_get_tty_state (scb)
651 serial_t scb;
652{
653 struct dos_ttystate *port = &ports[scb->fd];
654 struct dos_ttystate *state;
655
656 state = (struct dos_ttystate *) xmalloc (sizeof *state);
657 *state = *port;
658 return (serial_ttystate) state;
659}
660
661static int
662dos_set_tty_state (scb, ttystate)
663 serial_t scb;
664 serial_ttystate ttystate;
665{
666 struct dos_ttystate *state;
667
668 state = (struct dos_ttystate *) ttystate;
669 dos_setbaudrate (scb, state->baudrate);
670 return 0;
671}
672
673static int
674dos_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
675 serial_t scb;
676 serial_ttystate new_ttystate;
677 serial_ttystate old_ttystate;
678{
679 struct dos_ttystate *state;
680
681 state = (struct dos_ttystate *) new_ttystate;
682 dos_setbaudrate (scb, state->baudrate);
683 return 0;
684}
685
686static int
687dos_flush_input (scb)
688 serial_t scb;
689{
690 struct dos_ttystate *port = &ports[scb->fd];
c5aa993b 691 disable ();
c906108c
SS
692 port->first = port->count = 0;
693 if (port->fifo)
c5aa993b
JM
694 outb (port, com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_TRIGGER);
695 enable ();
c906108c
SS
696}
697
698static void
699dos_print_tty_state (scb, ttystate)
700 serial_t scb;
701 serial_ttystate ttystate;
702{
703 /* Nothing to print */
704 return;
705}
706
707static int
708dos_baudconv (rate)
709 int rate;
710{
711 long x, err;
c5aa993b
JM
712
713 if (rate <= 0)
c906108c
SS
714 return -1;
715
c5aa993b
JM
716#define divrnd(n, q) (((n) * 2 / (q) + 1) / 2) /* divide and round off */
717 x = divrnd (COMTICK, rate);
c906108c
SS
718 if (x <= 0)
719 return -1;
c5aa993b
JM
720
721 err = divrnd (1000 * COMTICK, x * rate) - 1000;
c906108c
SS
722 if (err < 0)
723 err = -err;
724 if (err > SPEED_TOLERANCE)
725 return -1;
726#undef divrnd
727 return x;
728}
729
730
731static int
732dos_setbaudrate (scb, rate)
733 serial_t scb;
734 int rate;
735{
c5aa993b 736 struct dos_ttystate *port = &ports[scb->fd];
c906108c 737
c5aa993b
JM
738 if (port->baudrate != rate)
739 {
740 int x;
741 unsigned char cfcr;
742
743 x = dos_baudconv (rate);
744 if (x <= 0)
745 {
746 fprintf_unfiltered (gdb_stderr, "%d: impossible baudrate\n", rate);
747 errno = EINVAL;
748 return -1;
749 }
750
751 disable ();
752 cfcr = inb (port, com_cfcr);
753
754 outb (port, com_cfcr, CFCR_DLAB);
755 outb (port, com_dlbl, x & 0xff);
756 outb (port, com_dlbh, x >> 8);
757 outb (port, com_cfcr, cfcr);
758 port->baudrate = rate;
759 enable ();
760 }
761
762 return 0;
c906108c
SS
763}
764
765static int
766dos_setstopbits (scb, num)
767 serial_t scb;
768 int num;
769{
c5aa993b
JM
770 struct dos_ttystate *port = &ports[scb->fd];
771 unsigned char cfcr;
c906108c 772
c5aa993b
JM
773 disable ();
774 cfcr = inb (port, com_cfcr);
775
776 switch (num)
777 {
778 case SERIAL_1_STOPBITS:
779 outb (port, com_cfcr, cfcr & ~CFCR_STOPB);
780 break;
781 case SERIAL_1_AND_A_HALF_STOPBITS:
782 case SERIAL_2_STOPBITS:
783 outb (port, com_cfcr, cfcr | CFCR_STOPB);
784 break;
785 default:
786 enable ();
787 return 1;
788 }
789 enable ();
790
791 return 0;
c906108c
SS
792}
793
794static int
795dos_write (scb, str, len)
796 serial_t scb;
797 const char *str;
798 int len;
799{
800 volatile struct dos_ttystate *port = &ports[scb->fd];
801 int fifosize = port->fifo ? 16 : 1;
802 long then;
803 int cnt;
804
c5aa993b
JM
805 while (len > 0)
806 {
807 /* send the data, fifosize bytes at a time */
808 cnt = fifosize > len ? len : fifosize;
809 port->txbusy = 1;
810 outportsb (port->base + com_data, str, cnt);
811 str += cnt;
812 len -= cnt;
c906108c 813#ifdef DOS_STATS
c5aa993b 814 cnts[CNT_TX] += cnt;
c906108c 815#endif
c5aa993b
JM
816 /* wait for transmission to complete (max 1 sec) */
817 then = rawclock () + RAWHZ;
818 while (port->txbusy)
819 {
820 if ((rawclock () - then) >= 0)
821 {
822 errno = EIO;
823 return SERIAL_ERROR;
824 }
825 }
c906108c
SS
826 }
827 return 0;
828}
829
830
831static int
832dos_sendbreak (scb)
833 serial_t scb;
834{
835 volatile struct dos_ttystate *port = &ports[scb->fd];
836 unsigned char cfcr;
837 long then;
838
c5aa993b
JM
839 cfcr = inb (port, com_cfcr);
840 outb (port, com_cfcr, cfcr | CFCR_SBREAK);
c906108c
SS
841
842 /* 0.25 sec delay */
843 then = rawclock () + RAWHZ / 4;
844 while ((rawclock () - then) < 0)
845 continue;
846
c5aa993b 847 outb (port, com_cfcr, cfcr);
c906108c
SS
848 return 0;
849}
850
851
852static struct serial_ops dos_ops =
853{
854 "hardwire",
855 0,
856 dos_open,
857 dos_close,
858 dos_readchar,
859 dos_write,
860 dos_noop, /* flush output */
861 dos_flush_input,
862 dos_sendbreak,
863 dos_raw,
864 dos_get_tty_state,
865 dos_set_tty_state,
866 dos_print_tty_state,
867 dos_noflush_set_tty_state,
868 dos_setbaudrate,
869 dos_setstopbits,
870 dos_noop, /* wait for output to drain */
871};
872
873
874static void
875dos_info (arg, from_tty)
876 char *arg;
877 int from_tty;
878{
879 struct dos_ttystate *port;
880 int i;
881
c5aa993b 882 for (port = ports; port < &ports[4]; port++)
c906108c
SS
883 {
884 if (port->baudrate == 0)
885 continue;
886 printf_filtered ("Port:\tCOM%d (%sactive)\n", port - ports + 1,
887 port->intrupt ? "" : "not ");
888 printf_filtered ("Addr:\t0x%03x (irq %d)\n", port->base, port->irq);
889 printf_filtered ("16550:\t%s\n", port->fifo ? "yes" : "no");
890 printf_filtered ("Speed:\t%d baud\n", port->baudrate);
c5aa993b 891 printf_filtered ("Errs:\tframing %d parity %d overflow %d\n\n",
c906108c
SS
892 port->ferr, port->perr, port->oflo);
893 }
894
895#ifdef DOS_STATS
896 printf_filtered ("\nTotal interrupts: %d\n", intrcnt);
897 for (i = 0; i < NCNT; i++)
898 if (cnts[i])
899 printf_filtered ("%s:\t%d\n", cntnames[i], cnts[i]);
900#endif
901}
902
903
904void
905_initialize_ser_dos ()
906{
907 struct cmd_list_element *c;
908
909 serial_add_interface (&dos_ops);
910
911 /* Save original interrupt mask register. */
912 icu_oldmask = inportb (ICU_MASK);
913
914 /* Mark fixed motherboard irqs as inuse. */
915 intrupts[0].inuse = /* timer tick */
916 intrupts[1].inuse = /* keyboard */
c5aa993b
JM
917 intrupts[2].inuse = 1; /* slave icu */
918
c906108c 919 add_show_from_set (
c5aa993b
JM
920 add_set_cmd ("com1base", class_obscure, var_zinteger,
921 (char *) &ports[0].base,
922 "Set COM1 base i/o port address.",
923 &setlist),
924 &showlist);
c906108c
SS
925
926 add_show_from_set (
c5aa993b
JM
927 add_set_cmd ("com1irq", class_obscure, var_zinteger,
928 (char *) &ports[0].irq,
929 "Set COM1 interrupt request.",
930 &setlist),
931 &showlist);
c906108c
SS
932
933 add_show_from_set (
c5aa993b
JM
934 add_set_cmd ("com2base", class_obscure, var_zinteger,
935 (char *) &ports[1].base,
936 "Set COM2 base i/o port address.",
937 &setlist),
938 &showlist);
c906108c
SS
939
940 add_show_from_set (
c5aa993b
JM
941 add_set_cmd ("com2irq", class_obscure, var_zinteger,
942 (char *) &ports[1].irq,
943 "Set COM2 interrupt request.",
944 &setlist),
945 &showlist);
c906108c
SS
946
947 add_show_from_set (
c5aa993b
JM
948 add_set_cmd ("com3base", class_obscure, var_zinteger,
949 (char *) &ports[2].base,
950 "Set COM3 base i/o port address.",
951 &setlist),
952 &showlist);
c906108c
SS
953
954 add_show_from_set (
c5aa993b
JM
955 add_set_cmd ("com3irq", class_obscure, var_zinteger,
956 (char *) &ports[2].irq,
957 "Set COM3 interrupt request.",
958 &setlist),
959 &showlist);
c906108c
SS
960
961 add_show_from_set (
c5aa993b
JM
962 add_set_cmd ("com4base", class_obscure, var_zinteger,
963 (char *) &ports[3].base,
964 "Set COM4 base i/o port address.",
965 &setlist),
966 &showlist);
c906108c
SS
967
968 add_show_from_set (
c5aa993b
JM
969 add_set_cmd ("com4irq", class_obscure, var_zinteger,
970 (char *) &ports[3].irq,
971 "Set COM4 interrupt request.",
972 &setlist),
973 &showlist);
c906108c
SS
974
975 add_info ("serial", dos_info,
976 "Print DOS serial port status.");
977}