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