-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_console.c
146 lines (129 loc) · 3.88 KB
/
serial_console.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright (C) 2018, bzt (bztsrc@github), https://github.com/bztsrc/raspi3-tutorial
* Copyright (C) 2020, Santiago Pagani <[email protected]>
* Copyright (c) 2022, John A. Kressel <[email protected]>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#include <morello/sysregs.h>
#include <flexos/impl/morello.h>
/* PL011 UART registers */
#define UART0_DR ((volatile unsigned int*)(0x2A400000+0x0))
#define UART0_FR ((volatile unsigned int*)(0x2A400000+0x18))
#define UART0_IBRD ((volatile unsigned int*)(0x2A400000+0x24))
#define UART0_FBRD ((volatile unsigned int*)(0x2A400000+0x28))
#define UART0_LCRH ((volatile unsigned int*)(0x2A400000+0x2C))
#define UART0_CR ((volatile unsigned int*)(0x2A400000+0x30))
#define UART0_IMSC ((volatile unsigned int*)(0x2A400000+0x38))
#define UART0_ICR ((volatile unsigned int*)(0x2A400000+0x44))
static char prev_sent_char = '\0';
void *__capability uart_cap;
static void wait_cycles(unsigned int n)
{
if (n) {
while (n--) {
asm volatile("nop");
}
}
}
unsigned int serial_tx_buffer_full(void)
{
// return *UART0_FR&0x20;
uint32_t result = 0;
__asm__ volatile(
"mov w0, %2 \n"
"ldr w1, [%0, #0x18]\n"
"and w1, w1, w0\n"
"str w1, [%1]\n"
:
: "r"(uart_cap), "r"(&result), "i"(0x20)
: "x0", "x1"
);
return result;
}
static unsigned int serial_rx_buffer_empty(void)
{
return *UART0_FR&0x10;
}
/**
* Set baud rate and characteristics (115200 8N1)
*/
void _libmorelloplat_init_serial_console()
{
register unsigned int r;
/* initialize UART */
*UART0_CR = 0; // turn off UART0
*UART0_ICR = 0x7FF; // clear interrupts
/*
*
* IBRD = 27
* FBRD = 9
*
* Calculation:
* UART_CLK = 50MHz
* Baud rate divisor = (50 x (10^6)) / (16 * 115200) = 27.12673611
*
* Therefore, IBRD is 27, for the fractional part, we do the following:
* ((0.12673611 * 64) + 0.5) = 9 (as integer)
* So FBRD is 9
*
*
*/
*UART0_IBRD = 0x1B; // 115200 baud
*UART0_FBRD = 0x9;
*UART0_LCRH = 0b11<<5; // 8n1
*UART0_CR = 0x301; // enable Tx, Rx, FIFO
morello_create_capability_from_ptr((uintptr_t)0x2A400000, (uintptr_t)0x60, ((uintptr_t *)(&(uart_cap))));
}
/**
* Send a character
*/
void _libmorelloplat_serial_putc(char c)
{
if ((c == '\n') && (prev_sent_char != '\r'))
_libmorelloplat_serial_putc('\r');
// Wait until we can send
do{
asm volatile("nop");
} while (serial_tx_buffer_full());
// Write the character to the buffer
// *UART0_DR = c;
__asm__ volatile(
"mov x0, %1 \n"
"str x0, [%0]\n"
:
: "r"(uart_cap), "r"(c)
: "x0"
);
prev_sent_char = c;
}
/**
* Receive a character
*/
int _libmorelloplat_serial_getc(void)
{
if (serial_rx_buffer_empty())
return -1;
char r;
r = (char)(*UART0_DR);
return (int)r;
}