Add memtest support.
[syslinux-debian/hramrach.git] / memdump / serial.c
blob1c613d177570dd84c8b1692763ec7c5ab0294333
1 #include <stdbool.h>
2 #include <stdio.h>
4 #include "mystuff.h"
5 #include "file.h"
6 #include "io.h"
8 enum {
9 THR = 0,
10 RBR = 0,
11 DLL = 0,
12 DLM = 1,
13 IER = 1,
14 IIR = 2,
15 FCR = 2,
16 LCR = 3,
17 MCR = 4,
18 LSR = 5,
19 MSR = 6,
20 SCR = 7,
23 int serial_init(struct serial_if *sif)
25 uint16_t port = sif->port;
26 uint8_t dll, dlm, lcr;
28 /* Set 115200n81 */
29 outb(0x83, port + LCR);
30 outb(0x01, port + DLL);
31 outb(0x00, port + DLM);
32 (void)inb(port + IER); /* Synchronize */
33 dll = inb(port + DLL);
34 dlm = inb(port + DLM);
35 lcr = inb(port + LCR);
36 outb(0x03, port + LCR);
37 (void)inb(port + IER); /* Synchronize */
39 if (dll != 0x01 || dlm != 0x00 || lcr != 0x83)
40 return -1; /* This doesn't look like a serial port */
42 /* Disable interrupts */
43 outb(port + IER, 0);
45 /* Enable 16550A FIFOs if available */
46 outb(port + FCR, 0x01); /* Enable FIFO */
47 (void)inb(port + IER); /* Synchronize */
48 if (inb(port + IIR) < 0xc0)
49 outb(port + FCR, 0x00); /* Disable FIFOs if non-functional */
50 (void)inb(port + IER); /* Synchronize */
52 return 0;
55 void serial_write(struct serial_if *sif, const void *data, size_t n)
57 uint16_t port = sif->port;
58 const char *p = data;
59 uint8_t lsr;
61 while (n--) {
62 do {
63 lsr = inb(port + LSR);
64 } while (!(lsr & 0x20));
66 outb(*p++, port + THR);
70 void serial_read(struct serial_if *sif, void *data, size_t n)
72 uint16_t port = sif->port;
73 char *p = data;
74 uint8_t lsr;
76 while (n--) {
77 do {
78 lsr = inb(port + LSR);
79 } while (!(lsr & 0x01));
81 *p++ = inb(port + RBR);