Debian Codegraph @

purgatory/arch/i386/console-x86.c

raw
1 #include <stdint.h>
2 #include <arch/io.h>
3 #include <purgatory.h>
4
5 /*
6 * VGA
7 * =============================================================================
8 */
9
10 /* Simple VGA output */
11
12 #define VGABASE ((void *)0xb8000)
13
14 #define MAX_YPOS 25
15 #define MAX_XPOS 80
16
17 static int current_ypos = 1, current_xpos = 0;
18 uint8_t console_vga = 0;
19
20 static void putchar_vga(int ch)
21 {
22 int i, k, j;
23 if (!console_vga)
24 return;
25
26 if (current_ypos >= MAX_YPOS) {
27 /* scroll 1 line up */
28 for (k = 1, j = 0; k < MAX_YPOS; k++, j++) {
29 for (i = 0; i < MAX_XPOS; i++) {
30 writew(readw(VGABASE + 2*(MAX_XPOS*k + i)),
31 VGABASE + 2*(MAX_XPOS*j + i));
32 }
33 }
34 for (i = 0; i < MAX_XPOS; i++)
35 writew(0x720, VGABASE + 2*(MAX_XPOS*j + i));
36 current_ypos = MAX_YPOS-1;
37 }
38 if (ch == '\n') {
39 current_xpos = 0;
40 current_ypos++;
41 } else if (ch != '\r') {
42 writew(((0x7 << 8) | (unsigned short) ch),
43 VGABASE + 2*(MAX_XPOS*current_ypos +
44 current_xpos++));
45 if (current_xpos >= MAX_XPOS) {
46 current_xpos = 0;
47 current_ypos++;
48 }
49 }
50 }
51
52 /*
53 * Serial
54 * =============================================================================
55 */
56
57 /* Base Address */
58 uint8_t console_serial = 0;
59 uint16_t serial_base = 0x3f8; /* TTYS0 */
60 uint32_t serial_baud = 0;
61
62 #define XMTRDY 0x20
63
64 #define DLAB 0x80
65
66 #define TXR 0 /* Transmit register (WRITE) */
67 #define TBR 0 /* Transmit register (WRITE) */
68 #define RXR 0 /* Receive register (READ) */
69 #define IER 1 /* Interrupt Enable */
70 #define IIR 2 /* Interrupt ID */
71 #define FCR 2 /* FIFO control */
72 #define LCR 3 /* Line control */
73 #define MCR 4 /* Modem control */
74 #define LSR 5 /* Line Status */
75 #define MSR 6 /* Modem Status */
76 #define DLL 0 /* Divisor Latch Low */
77 #define DLH 1 /* Divisor latch High */
78
79 static void serial_init(void)
80 {
81 static int initialized = 0;
82 if (!initialized) {
83 unsigned lcr;
84 outb(0x3, serial_base + LCR); /* 8n1 */
85 outb(0, serial_base + IER); /* no interrupt */
86 outb(0, serial_base + FCR); /* no fifo */
87 outb(0x3, serial_base + MCR); /* DTR + RTS */
88
89 lcr = inb(serial_base + LCR);
90 outb(lcr | DLAB, serial_base + LCR);
91 /* By default don't change the serial port baud rate */
92 if (serial_baud) {
93 unsigned divisor = 115200 / serial_baud;
94 outb(divisor & 0xff, serial_base + DLL);
95 outb((divisor >> 8) & 0xff, serial_base + DLH);
96 }
97 outb(lcr & ~DLAB, serial_base + LCR);
98 initialized = 1;
99 }
100 }
101
102 static void serial_tx_byte(unsigned byte)
103 {
104 /* Ensure the serial port is initialized */
105 serial_init();
106
107 /* Wait until I can send a byte */
108 while((inb(serial_base + LSR) & 0x20) == 0)
109 ;
110 outb(byte, serial_base + TBR);
111 /* Wait until the byte is transmitted */
112 while(!(inb(serial_base + LSR) & 0x40))
113 ;
114 }
115
116 static void putchar_serial(int ch)
117 {
118 if (!console_serial) {
119 return;
120 }
121 if (ch == '\n') {
122 serial_tx_byte('\r');
123 }
124 serial_tx_byte(ch);
125 }
126
127 /* Generic wrapper function */
128
129 void putchar(int ch)
130 {
131 putchar_vga(ch);
132 putchar_serial(ch);
133 }
134
135