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