Move files out of root into core, dos, and utils
[profile/ivi/syslinux.git] / memdump / serial.c
1 #include "mystuff.h"
2 #include "ymsend.h"
3 #include "io.h"
4
5 enum {
6   THR = 0,
7   RBR = 0,
8   DLL = 0,
9   DLM = 1,
10   IER = 1,
11   IIR = 2,
12   FCR = 2,
13   LCR = 3,
14   MCR = 4,
15   LSR = 5,
16   MSR = 6,
17   SCR = 7,
18 };
19
20 int serial_init(struct serial_if *sif)
21 {
22   uint16_t port = sif->port;
23   uint8_t dll, dlm, lcr;
24
25   /* Set 115200n81 */
26   outb(0x83, port+LCR);
27   outb(0x01, port+DLL);
28   outb(0x00, port+DLM);
29   (void)inb(port+IER);          /* Synchronize */
30   dll = inb(port+DLL);
31   dlm = inb(port+DLM);
32   lcr = inb(port+LCR);
33   outb(0x03, port+LCR);
34   (void)inb(port+IER);          /* Synchronize */
35
36   if (dll != 0x01 || dlm != 0x00 || lcr != 0x83)
37     return -1;                  /* This doesn't look like a serial port */
38
39   /* Disable interrupts */
40   outb(port+IER, 0);
41
42   /* Enable 16550A FIFOs if available */
43   outb(port+FCR, 0x01);         /* Enable FIFO */
44   (void)inb(port+IER);          /* Synchronize */
45   if (inb(port+IIR) < 0xc0)
46     outb(port+FCR, 0x00);       /* Disable FIFOs if non-functional */
47   (void)inb(port+IER);          /* Synchronize */
48
49   return 0;
50 }
51
52 void serial_write(struct serial_if *sif, const void *data, size_t n)
53 {
54   uint16_t port = sif->port;
55   const char *p = data;
56   uint8_t lsr;
57
58   while (n--) {
59     do {
60       lsr = inb(port+LSR);
61     } while (!(lsr & 0x20));
62
63     outb(*p++, port+THR);
64   }
65 }
66
67 void serial_read(struct serial_if *sif, void *data, size_t n)
68 {
69   uint16_t port = sif->port;
70   char *p = data;
71   uint8_t lsr;
72
73   while (n--) {
74     do {
75       lsr = inb(port+LSR);
76     } while (!(lsr & 0x01));
77
78     *p++ = inb(port+RBR);
79   }
80 }