2 * Copyright (C) 2003, 2004 Stefan Reinauer
4 * See the file "COPYING" for further information about
5 * the copyright and warranty status of this work.
9 #include "kernel/kernel.h"
12 #ifdef CONFIG_DEBUG_CONSOLE
14 /* ******************************************************************
15 * serial console functions
16 * ****************************************************************** */
18 #ifdef CONFIG_DEBUG_CONSOLE_SERIAL
20 #define RBR(x) x==2?0x2f8:0x3f8
21 #define THR(x) x==2?0x2f8:0x3f8
22 #define IER(x) x==2?0x2f9:0x3f9
23 #define IIR(x) x==2?0x2fa:0x3fa
24 #define LCR(x) x==2?0x2fb:0x3fb
25 #define MCR(x) x==2?0x2fc:0x3fc
26 #define LSR(x) x==2?0x2fd:0x3fd
27 #define MSR(x) x==2?0x2fe:0x3fe
28 #define SCR(x) x==2?0x2ff:0x3ff
29 #define DLL(x) x==2?0x2f8:0x3f8
30 #define DLM(x) x==2?0x2f9:0x3f9
32 static int uart_charav(int port)
36 return ((inb(LSR(port)) & 1) != 0);
39 static char uart_getchar(int port)
43 while (!uart_charav(port));
44 return ((char) inb(RBR(port)) & 0177);
47 static void uart_putchar(int port, unsigned char c)
52 uart_putchar(port, '\r');
53 while (!(inb(LSR(port)) & 0x20));
57 static void uart_init_line(int port, unsigned long baud)
83 outb(0x87, LCR(port));
84 outb(0x00, DLM(port));
85 outb(baudconst, DLL(port));
86 outb(0x07, LCR(port));
87 outb(0x0f, MCR(port));
89 for (i = 10; i > 0; i--) {
90 if (inb(LSR(port)) == (unsigned int) 0)
96 int uart_init(int port, unsigned long speed)
99 uart_init_line(port, speed);
103 static void serial_putchar(int c)
105 uart_putchar(CONFIG_SERIAL_PORT, (unsigned char) (c & 0xff));
108 static void serial_cls(void)
120 /* ******************************************************************
121 * simple polling video/keyboard console functions
122 * ****************************************************************** */
124 #ifdef CONFIG_DEBUG_CONSOLE_VGA
126 /* raw vga text mode */
127 #define COLUMNS 80 /* The number of columns. */
128 #define LINES 25 /* The number of lines. */
129 #define ATTRIBUTE 7 /* The attribute of an character. */
131 #define VGA_BASE 0xB8000 /* The video memory address. */
133 /* VGA Index and Data Registers */
134 #define VGA_REG_INDEX 0x03D4 /* VGA index register */
135 #define VGA_REG_DATA 0x03D5 /* VGA data register */
137 #define VGA_IDX_CURMSL 0x09 /* cursor maximum scan line */
138 #define VGA_IDX_CURSTART 0x0A /* cursor start */
139 #define VGA_IDX_CUREND 0x0B /* cursor end */
140 #define VGA_IDX_CURLO 0x0F /* cursor position (low 8 bits) */
141 #define VGA_IDX_CURHI 0x0E /* cursor position (high 8 bits) */
143 /* Save the X and Y position. */
144 static int xpos, ypos;
145 /* Point to the video memory. */
146 static volatile unsigned char *video = (unsigned char *) VGA_BASE;
148 static void video_initcursor(void)
151 outb(VGA_IDX_CURMSL, VGA_REG_INDEX);
152 val = inb(VGA_REG_DATA) & 0x1f; /* maximum scan line -1 */
154 outb(VGA_IDX_CURSTART, VGA_REG_INDEX);
155 outb(0, VGA_REG_DATA);
157 outb(VGA_IDX_CUREND, VGA_REG_INDEX);
158 outb(val, VGA_REG_DATA);
163 static void video_poscursor(unsigned int x, unsigned int y)
167 /* Calculate new cursor position as a function of x and y */
168 pos = (y * COLUMNS) + x;
170 /* Output the new position to VGA card */
171 outb(VGA_IDX_CURLO, VGA_REG_INDEX); /* output low 8 bits */
172 outb((u8) (pos), VGA_REG_DATA);
173 outb(VGA_IDX_CURHI, VGA_REG_INDEX); /* output high 8 bits */
174 outb((u8) (pos >> 8), VGA_REG_DATA);
179 static void video_newline(void)
183 if (ypos < LINES - 1) {
187 memmove((void *) video, (void *) (video + 2 * COLUMNS),
188 (LINES - 1) * COLUMNS * 2);
190 for (i = ((LINES - 1) * 2 * COLUMNS);
191 i < 2 * COLUMNS * LINES;) {
193 video[i++] = ATTRIBUTE;
199 /* Put the character C on the screen. */
200 static void video_putchar(int c)
204 if (c == '\n' || c == '\r') {
219 *(video + (xpos + ypos * COLUMNS) * 2) = c & 0xFF;
220 *(video + (xpos + ypos * COLUMNS) * 2 + 1) = ATTRIBUTE;
225 video_poscursor(xpos, ypos);
228 static void video_cls(void)
232 for (i = 0; i < 2 * COLUMNS * LINES;) {
234 video[i++] = ATTRIBUTE;
242 video_poscursor(xpos, ypos);
245 void video_init(void)
247 video=phys_to_virt((unsigned char*)VGA_BASE);
254 static char normal[] = {
255 0x0, 0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-',
256 '=', '\b', '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o',
257 'p', '[', ']', 0xa, 0x0, 'a', 's', 'd', 'f', 'g', 'h', 'j',
258 'k', 'l', ';', 0x27, 0x60, 0x0, 0x5c, 'z', 'x', 'c', 'v', 'b',
259 'n', 'm', ',', '.', '/', 0x0, '*', 0x0, ' ', 0x0, 0x0, 0x0,
260 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
261 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, '0', 0x7f
264 static char shifted[] = {
265 0x0, 0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_',
266 '+', '\b', '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
267 'P', '{', '}', 0xa, 0x0, 'A', 'S', 'D', 'F', 'G', 'H', 'J',
268 'K', 'L', ':', 0x22, '~', 0x0, '|', 'Z', 'X', 'C', 'V', 'B',
269 'N', 'M', '<', '>', '?', 0x0, '*', 0x0, ' ', 0x0, 0x0, 0x0,
270 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, '7', '8',
271 '9', 0x0, '4', '5', '6', 0x0, '1', '2', '3', '0', 0x7f
275 static int key_lshift = 0, key_rshift = 0, key_caps = 0;
277 static char last_key;
279 static void keyboard_cmd(unsigned char cmd, unsigned char val)
282 /* wait until keyboard controller accepts cmds: */
283 while (inb(0x64) & 2);
285 while (inb(0x64) & 2);
288 static char keyboard_poll(void)
312 keyboard_cmd(0xed, 0);
315 keyboard_cmd(0xed, 4); /* set caps led */
321 // void printk(const char *format, ...);
322 printk("extended keycode: %x\n", c);
328 if (c & 0x80) /* unhandled key release */
331 if (key_lshift || key_rshift)
332 return key_caps ? normal[c] : shifted[c];
334 return key_caps ? shifted[c] : normal[c];
339 static int keyboard_dataready(void)
344 last_key = keyboard_poll();
346 return (last_key != 0);
349 static unsigned char keyboard_readdata(void)
352 while (!keyboard_dataready());
360 /* ******************************************************************
361 * common functions, implementing simple concurrent console
362 * ****************************************************************** */
364 int arch_putchar(int c)
366 #ifdef CONFIG_DEBUG_CONSOLE_SERIAL
369 #ifdef CONFIG_DEBUG_CONSOLE_VGA
375 int arch_availchar(void)
377 #ifdef CONFIG_DEBUG_CONSOLE_SERIAL
378 if (uart_charav(CONFIG_SERIAL_PORT))
381 #ifdef CONFIG_DEBUG_CONSOLE_VGA
382 if (keyboard_dataready())
388 int arch_getchar(void)
390 #ifdef CONFIG_DEBUG_CONSOLE_SERIAL
391 if (uart_charav(CONFIG_SERIAL_PORT))
392 return (uart_getchar(CONFIG_SERIAL_PORT));
394 #ifdef CONFIG_DEBUG_CONSOLE_VGA
395 if (keyboard_dataready())
396 return (keyboard_readdata());
403 #ifdef CONFIG_DEBUG_CONSOLE_SERIAL
406 #ifdef CONFIG_DEBUG_CONSOLE_VGA
411 struct _console_ops arch_console_ops = {
412 .putchar = arch_putchar,
413 .availchar = arch_availchar,
414 .getchar = arch_getchar
417 #endif // CONFIG_DEBUG_CONSOLE