blob: e7d0e05cd83a1a237b294db5a336f5aa2611e9f3 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2009 Corey Tabaka
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <stdarg.h>
24#include <reg.h>
25#include <stdio.h>
26#include <kernel/thread.h>
27#include <arch/x86.h>
28#include <lib/cbuf.h>
29#include <platform/interrupts.h>
30#include <platform/pc/memmap.h>
31#include <platform/console.h>
32#include <platform/keyboard.h>
33#include <platform/debug.h>
34
35static int uart_baud_rate = 115200;
36static int uart_io_port = 0x3f8;
37
38static cbuf_t uart_rx_buf;
39
40static enum handler_return uart_irq_handler(void *arg)
41{
42 unsigned char c;
43 bool resched = false;
44
45 while (inp(uart_io_port + 5) & (1<<0)) {
46 c = inp(uart_io_port + 0);
47 cbuf_write_char(&uart_rx_buf, c, false);
48 resched = true;
49 }
50
51 return resched ? INT_RESCHEDULE : INT_NO_RESCHEDULE;
52}
53
54void platform_init_uart(void)
55{
56 /* configure the uart */
57 int divisor = 115200 / uart_baud_rate;
58
59 /* get basic config done so that tx functions */
60 outp(uart_io_port + 3, 0x80); // set up to load divisor latch
61 outp(uart_io_port + 0, divisor & 0xff); // lsb
62 outp(uart_io_port + 1, divisor >> 8); // msb
63 outp(uart_io_port + 3, 3); // 8N1
64 outp(uart_io_port + 2, 0x07); // enable FIFO, clear, 14-byte threshold
65}
66
67void uart_init(void)
68{
69 /* finish uart init to get rx going */
70 cbuf_initialize(&uart_rx_buf, 16);
71
72 register_int_handler(0x24, uart_irq_handler, NULL);
73 unmask_interrupt(0x24);
74
75 outp(uart_io_port + 1, 0x1); // enable receive data available interrupt
76}
77
78void uart_putc(char c)
79{
80 while ((inp(uart_io_port + 5) & (1<<6)) == 0)
81 ;
82 outp(uart_io_port + 0, c);
83}
84
85int uart_getc(char *c, bool wait)
86{
87 return cbuf_read_char(&uart_rx_buf, c, wait);
88}
89
90void platform_dputc(char c)
91{
92 if (c == '\n')
93 platform_dputc('\r');
94#if WITH_CGA_CONSOLE
95 cputc(c);
96#else
97 uart_putc(c);
98#endif
99}
100
101int platform_dgetc(char *c, bool wait)
102{
103#if WITH_CGA_CONSOLE
104 int ret = platform_read_key(c);
105 //if (ret < 0)
106 // arch_idle();
107#else
108 int ret = uart_getc(c, wait);
109#endif
110
111 return ret;
112}
113
114void platform_halt(void)
115{
116 for (;;) {
117 x86_cli();
118 x86_hlt();
119 }
120}
121