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