coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
uart.c
Go to the documentation of this file.
1
/* SPDX-License-Identifier: GPL-2.0-only */
2
3
#include <
device/mmio.h
>
4
#include <
boot/coreboot_tables.h
>
5
#include <
console/uart.h
>
6
#include <
drivers/uart/uart8250reg.h
>
7
#include <
stdint.h
>
8
9
#include <soc/addressmap.h>
10
#include <soc/pll.h>
11
12
struct
mtk_uart
{
13
union
{
14
uint32_t
thr
;
/* Transmit holding register. */
15
uint32_t
rbr
;
/* Receive buffer register. */
16
uint32_t
dll
;
/* Divisor latch lsb. */
17
};
18
union
{
19
uint32_t
ier
;
/* Interrupt enable register. */
20
uint32_t
dlm
;
/* Divisor latch msb. */
21
};
22
union
{
23
uint32_t
iir
;
/* Interrupt identification register. */
24
uint32_t
fcr
;
/* FIFO control register. */
25
uint32_t
efr
;
/* Enhanced feature register. */
26
};
27
uint32_t
lcr
;
/* Line control register. */
28
union
{
29
uint32_t
mcr
;
/* Modem control register. */
30
uint32_t
xn1
;
/* XON1 */
31
};
32
union
{
33
uint32_t
lsr
;
/* Line status register. */
34
uint32_t
xn2
;
/* XON2 */
35
};
36
union
{
37
uint32_t
msr
;
/* Modem status register. */
38
uint32_t
xf1
;
/* XOFF1 */
39
};
40
union
{
41
uint32_t
scr
;
/* Scratch register. */
42
uint32_t
xf2
;
/* XOFF2 */
43
};
44
uint32_t
autobaud_en
;
/* Enable auto baudrate. */
45
uint32_t
highspeed
;
/* High speed UART. */
46
}
__packed
;
47
48
/* Peripheral Reset and Power Down registers */
49
struct
mtk_peri_globalcon
{
50
uint32_t
rst0
;
51
uint32_t
rst1
;
52
uint32_t
pdn0_set
;
53
uint32_t
pdn1_set
;
54
uint32_t
pdn0_clr
;
55
uint32_t
pdn1_clr
;
56
uint32_t
pdn0_sta
;
57
uint32_t
pdn1_sta
;
58
uint32_t
pdn_md1_set
;
59
uint32_t
pdn_md2_set
;
60
uint32_t
pdn_md1_clr
;
61
uint32_t
pdn_md2_clr
;
62
uint32_t
pdn_md1_sta
;
63
uint32_t
pdn_md2_sta
;
64
uint32_t
pdn_md_mask
;
65
}
__packed
;
66
67
static
struct
mtk_uart
*
const
uart_ptr
= (
void
*)
UART0_BASE
;
68
69
static
void
mtk_uart_tx_flush
(
void
);
70
static
int
mtk_uart_tst_byte
(
void
);
71
72
static
void
mtk_uart_init
(
void
)
73
{
74
/* Use a hardcoded divisor for now. */
75
const
unsigned
int
uartclk =
UART_HZ
;
76
const
unsigned
int
baudrate =
get_uart_baudrate
();
77
const
uint8_t
line_config =
UART8250_LCR_WLS_8
;
/* 8n1 */
78
unsigned
int
highspeed
, quot, divisor, remainder;
79
80
if
(baudrate <= 115200) {
81
highspeed
= 0;
82
quot = 16;
83
}
else
{
84
highspeed
= 2;
85
quot = 4;
86
}
87
88
/* Set divisor DLL and DLH */
89
divisor = uartclk / (quot * baudrate);
90
remainder = uartclk % (quot * baudrate);
91
92
if
(remainder >= (quot / 2) * baudrate)
93
divisor += 1;
94
95
mtk_uart_tx_flush
();
96
97
/* Disable interrupts. */
98
write8
(&
uart_ptr
->
ier
, 0);
99
/* Force DTR and RTS to high. */
100
write8
(&
uart_ptr
->
mcr
,
UART8250_MCR_DTR
|
UART8250_MCR_RTS
);
101
/* Set High speed UART register. */
102
write8
(&
uart_ptr
->
highspeed
,
highspeed
);
103
/* Set line configuration, access divisor latches. */
104
write8
(&
uart_ptr
->
lcr
,
UART8250_LCR_DLAB
| line_config);
105
/* Set the divisor. */
106
write8
(&
uart_ptr
->
dll
, divisor & 0xff);
107
write8
(&
uart_ptr
->
dlm
, (divisor >> 8) & 0xff);
108
/* Hide the divisor latches. */
109
write8
(&
uart_ptr
->
lcr
, line_config);
110
/* Enable FIFOs, and clear receive and transmit. */
111
write8
(&
uart_ptr
->
fcr
,
112
UART8250_FCR_FIFO_EN
|
UART8250_FCR_CLEAR_RCVR
|
113
UART8250_FCR_CLEAR_XMIT
);
114
115
}
116
117
static
void
mtk_uart_tx_byte
(
unsigned
char
data)
118
{
119
while
(!(
read8
(&
uart_ptr
->
lsr
) &
UART8250_LSR_THRE
))
120
;
121
write8
(&
uart_ptr
->
thr
, data);
122
}
123
124
static
void
mtk_uart_tx_flush
(
void
)
125
{
126
while
(!(
read8
(&
uart_ptr
->
lsr
) &
UART8250_LSR_TEMT
))
127
;
128
}
129
130
static
unsigned
char
mtk_uart_rx_byte
(
void
)
131
{
132
if
(!
mtk_uart_tst_byte
())
133
return
0;
134
return
read8
(&
uart_ptr
->
rbr
);
135
}
136
137
static
int
mtk_uart_tst_byte
(
void
)
138
{
139
return
(
read8
(&
uart_ptr
->
lsr
) &
UART8250_LSR_DR
) ==
UART8250_LSR_DR
;
140
}
141
142
void
uart_init
(
unsigned
int
idx)
143
{
144
mtk_uart_init
();
145
}
146
147
unsigned
char
uart_rx_byte
(
unsigned
int
idx)
148
{
149
return
mtk_uart_rx_byte
();
150
}
151
152
void
uart_tx_byte
(
unsigned
int
idx,
unsigned
char
data)
153
{
154
mtk_uart_tx_byte
(data);
155
}
156
157
void
uart_tx_flush
(
unsigned
int
idx)
158
{
159
mtk_uart_tx_flush
();
160
}
161
162
void
uart_fill_lb
(
void
*data)
163
{
164
struct
lb_serial
serial
;
165
serial
.type =
LB_SERIAL_TYPE_MEMORY_MAPPED
;
166
serial
.baseaddr =
UART0_BASE
;
167
serial
.baud =
get_uart_baudrate
();
168
serial
.regwidth = 4;
169
serial
.input_hertz =
UART_HZ
;
170
serial
.uart_pci_addr = CONFIG_UART_PCI_ADDR;
171
lb_add_serial
(&
serial
, data);
172
173
lb_add_console
(
LB_TAG_CONSOLE_SERIAL8250MEM
, data);
174
}
write8
static void write8(void *addr, uint8_t val)
Definition:
mmio.h:30
read8
static uint8_t read8(const void *addr)
Definition:
mmio.h:12
get_uart_baudrate
unsigned int get_uart_baudrate(void)
Definition:
bmcinfo.c:167
LB_TAG_CONSOLE_SERIAL8250MEM
#define LB_TAG_CONSOLE_SERIAL8250MEM
Definition:
coreboot_tables.h:213
LB_SERIAL_TYPE_MEMORY_MAPPED
#define LB_SERIAL_TYPE_MEMORY_MAPPED
Definition:
coreboot_tables.h:179
coreboot_tables.h
lb_add_console
void lb_add_console(uint16_t consoletype, void *data)
Definition:
coreboot_table.c:110
lb_add_serial
void lb_add_serial(struct lb_serial *serial, void *data)
Definition:
coreboot_table.c:94
uart.h
mmio.h
serial
unsigned int serial
Definition:
edid.c:52
uart_init
void uart_init(unsigned int idx)
Definition:
uart.c:13
uart_tx_flush
void uart_tx_flush(unsigned int idx)
Definition:
uart.c:27
uart_rx_byte
unsigned char uart_rx_byte(unsigned int idx)
Definition:
uart.c:17
uart_fill_lb
void uart_fill_lb(void *data)
Definition:
uart.c:31
uart_tx_byte
void uart_tx_byte(unsigned int idx, unsigned char data)
Definition:
uart.c:22
UART_HZ
@ UART_HZ
Definition:
pll.h:247
mtk_uart_tx_flush
static void mtk_uart_tx_flush(void)
Definition:
uart.c:124
mtk_uart_tx_byte
static void mtk_uart_tx_byte(unsigned char data)
Definition:
uart.c:117
mtk_uart_init
static void mtk_uart_init(void)
Definition:
uart.c:72
__packed
struct mtk_uart __packed
mtk_uart_tst_byte
static int mtk_uart_tst_byte(void)
Definition:
uart.c:137
mtk_uart_rx_byte
static unsigned char mtk_uart_rx_byte(void)
Definition:
uart.c:130
uart_ptr
static struct mtk_uart *const uart_ptr
Definition:
uart.c:67
UART0_BASE
#define UART0_BASE
Definition:
addressmap.h:21
stdint.h
uint32_t
unsigned int uint32_t
Definition:
stdint.h:14
uint8_t
unsigned char uint8_t
Definition:
stdint.h:8
lb_serial
Definition:
coreboot_tables.h:175
mtk_peri_globalcon
Definition:
uart.c:49
mtk_peri_globalcon::pdn1_set
uint32_t pdn1_set
Definition:
uart.c:53
mtk_peri_globalcon::pdn0_set
uint32_t pdn0_set
Definition:
uart.c:52
mtk_peri_globalcon::pdn_md2_sta
uint32_t pdn_md2_sta
Definition:
uart.c:63
mtk_peri_globalcon::pdn1_sta
uint32_t pdn1_sta
Definition:
uart.c:57
mtk_peri_globalcon::pdn_md2_set
uint32_t pdn_md2_set
Definition:
uart.c:59
mtk_peri_globalcon::pdn1_clr
uint32_t pdn1_clr
Definition:
uart.c:55
mtk_peri_globalcon::rst1
uint32_t rst1
Definition:
uart.c:51
mtk_peri_globalcon::pdn_md2_clr
uint32_t pdn_md2_clr
Definition:
uart.c:61
mtk_peri_globalcon::pdn_md1_sta
uint32_t pdn_md1_sta
Definition:
uart.c:62
mtk_peri_globalcon::pdn_md1_set
uint32_t pdn_md1_set
Definition:
uart.c:58
mtk_peri_globalcon::pdn0_clr
uint32_t pdn0_clr
Definition:
uart.c:54
mtk_peri_globalcon::pdn_md_mask
uint32_t pdn_md_mask
Definition:
uart.c:64
mtk_peri_globalcon::pdn0_sta
uint32_t pdn0_sta
Definition:
uart.c:56
mtk_peri_globalcon::pdn_md1_clr
uint32_t pdn_md1_clr
Definition:
uart.c:60
mtk_peri_globalcon::rst0
uint32_t rst0
Definition:
uart.c:50
mtk_uart
Definition:
uart.c:12
mtk_uart::iir
uint32_t iir
Definition:
uart.c:23
mtk_uart::efr
uint32_t efr
Definition:
uart.c:25
mtk_uart::mcr
uint32_t mcr
Definition:
uart.c:29
mtk_uart::thr
uint32_t thr
Definition:
uart.c:14
mtk_uart::ier
uint32_t ier
Definition:
uart.c:19
mtk_uart::dlm
uint32_t dlm
Definition:
uart.c:20
mtk_uart::xn2
uint32_t xn2
Definition:
uart.c:34
mtk_uart::xf2
uint32_t xf2
Definition:
uart.c:42
mtk_uart::scr
uint32_t scr
Definition:
uart.c:41
mtk_uart::xn1
uint32_t xn1
Definition:
uart.c:30
mtk_uart::xf1
uint32_t xf1
Definition:
uart.c:38
mtk_uart::autobaud_en
uint32_t autobaud_en
Definition:
uart.c:44
mtk_uart::lsr
uint32_t lsr
Definition:
uart.c:33
mtk_uart::msr
uint32_t msr
Definition:
uart.c:37
mtk_uart::rbr
uint32_t rbr
Definition:
uart.c:15
mtk_uart::dll
uint32_t dll
Definition:
uart.c:16
mtk_uart::highspeed
uint32_t highspeed
Definition:
uart.c:45
mtk_uart::lcr
uint32_t lcr
Definition:
uart.c:27
mtk_uart::fcr
uint32_t fcr
Definition:
uart.c:24
uart8250reg.h
UART8250_LCR_WLS_8
#define UART8250_LCR_WLS_8
Definition:
uart8250reg.h:44
UART8250_LCR_DLAB
#define UART8250_LCR_DLAB
Definition:
uart8250reg.h:50
UART8250_LSR_DR
#define UART8250_LSR_DR
Definition:
uart8250reg.h:67
UART8250_FCR_CLEAR_RCVR
#define UART8250_FCR_CLEAR_RCVR
Definition:
uart8250reg.h:30
UART8250_FCR_CLEAR_XMIT
#define UART8250_FCR_CLEAR_XMIT
Definition:
uart8250reg.h:31
UART8250_LSR_TEMT
#define UART8250_LSR_TEMT
Definition:
uart8250reg.h:73
UART8250_MCR_DTR
#define UART8250_MCR_DTR
Definition:
uart8250reg.h:53
UART8250_FCR_FIFO_EN
#define UART8250_FCR_FIFO_EN
Definition:
uart8250reg.h:29
UART8250_MCR_RTS
#define UART8250_MCR_RTS
Definition:
uart8250reg.h:54
UART8250_LSR_THRE
#define UART8250_LSR_THRE
Definition:
uart8250reg.h:72
src
soc
mediatek
common
uart.c
Generated by
1.9.1