Software APIs
uart.c
Go to the documentation of this file.
1
// Copyright lowRISC contributors (OpenTitan project).
2
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3
// SPDX-License-Identifier: Apache-2.0
4
//
5
// Device table API auto-generated by `dtgen`
6
7
/**
8
* @file
9
* @brief Device Tables (DT) for IP uart and top englishbreakfast.
10
*/
11
12
#include "hw/top/dt/uart.h"
13
14
15
16
/**
17
* Description of instances.
18
*/
19
typedef
struct
dt_desc_uart
{
20
dt_instance_id_t
inst_id
;
/**< Instance ID */
21
uint32_t
reg_addr
[
kDtUartRegBlockCount
];
/**< Base address of each register block */
22
/**
23
* PLIC ID of the first IRQ of this instance
24
*
25
* This can be `kDtPlicIrqIdNone` if the block is not connected to the PLIC.
26
*/
27
top_englishbreakfast_plic_irq_id_t
first_irq
;
28
dt_clock_t
clock
[
kDtUartClockCount
];
/**< Clock signal connected to each clock port */
29
dt_reset_t
reset
[
kDtUartResetCount
];
/**< Reset signal connected to each reset port */
30
dt_periph_io_t
periph_io
[
kDtUartPeriphIoCount
];
/**< Description of each peripheral I/O */
31
}
dt_desc_uart_t
;
32
33
34
35
36
static
const
dt_desc_uart_t
uart_desc[
kDtUartCount
] = {
37
[
kDtUart0
] = {
38
.inst_id =
kDtInstanceIdUart0
,
39
.reg_addr = {
40
[kDtUartRegBlockCore] = 0x40000000,
41
},
42
.first_irq =
kTopEnglishbreakfastPlicIrqIdUart0TxWatermark
,
43
.clock = {
44
[
kDtUartClockClk
] =
kDtClockIoDiv4
,
45
},
46
.reset = {
47
[
kDtUartResetRst
] =
kDtResetLcIoDiv4
,
48
},
49
.periph_io = {
50
[kDtUartPeriphIoRx] = {
51
.__internal = {
52
.type =
kDtPeriphIoTypeMio
,
53
.dir =
kDtPeriphIoDirIn
,
54
.periph_input_or_direct_pad =
kTopEnglishbreakfastPinmuxPeripheralInUart0Rx
,
55
.outsel_or_dt_pad = 0,
56
},
57
},
58
[kDtUartPeriphIoTx] = {
59
.__internal = {
60
.type =
kDtPeriphIoTypeMio
,
61
.dir =
kDtPeriphIoDirOut
,
62
.periph_input_or_direct_pad = 0,
63
.outsel_or_dt_pad =
kTopEnglishbreakfastPinmuxOutselUart0Tx
,
64
},
65
},
66
},
67
},
68
[
kDtUart1
] = {
69
.inst_id =
kDtInstanceIdUart1
,
70
.reg_addr = {
71
[kDtUartRegBlockCore] = 0x40010000,
72
},
73
.first_irq =
kTopEnglishbreakfastPlicIrqIdUart1TxWatermark
,
74
.clock = {
75
[
kDtUartClockClk
] =
kDtClockIoDiv4
,
76
},
77
.reset = {
78
[
kDtUartResetRst
] =
kDtResetLcIoDiv4
,
79
},
80
.periph_io = {
81
[kDtUartPeriphIoRx] = {
82
.__internal = {
83
.type =
kDtPeriphIoTypeMio
,
84
.dir =
kDtPeriphIoDirIn
,
85
.periph_input_or_direct_pad =
kTopEnglishbreakfastPinmuxPeripheralInUart1Rx
,
86
.outsel_or_dt_pad = 0,
87
},
88
},
89
[kDtUartPeriphIoTx] = {
90
.__internal = {
91
.type =
kDtPeriphIoTypeMio
,
92
.dir =
kDtPeriphIoDirOut
,
93
.periph_input_or_direct_pad = 0,
94
.outsel_or_dt_pad =
kTopEnglishbreakfastPinmuxOutselUart1Tx
,
95
},
96
},
97
},
98
},
99
};
100
101
/**
102
* Return a pointer to the `dt_uart_desc_t` structure of the requested
103
* `dt` if it's a valid index. Otherwise, this macro will `return` (i.e. exit
104
* the function) with the provided default value.
105
*/
106
#define TRY_GET_DT(dt, default) ({ if ((dt) < (dt_uart_t)0 || (int)(dt) >= kDtUartCount) return (default); &uart_desc[dt]; })
107
108
dt_uart_t
dt_uart_from_instance_id
(
dt_instance_id_t
inst_id) {
109
if
(inst_id >=
kDtInstanceIdUart0
&& inst_id <=
kDtInstanceIdUart1
) {
110
return
(
dt_uart_t
)(inst_id -
kDtInstanceIdUart0
);
111
}
112
return
(
dt_uart_t
)0;
113
}
114
115
dt_instance_id_t
dt_uart_instance_id
(
116
dt_uart_t
dt) {
117
return
TRY_GET_DT
(dt,
kDtInstanceIdUnknown
)->inst_id;
118
}
119
120
uint32_t
dt_uart_reg_block
(
121
dt_uart_t
dt,
122
dt_uart_reg_block_t
reg_block) {
123
// Return a recognizable address in case of wrong argument.
124
return
TRY_GET_DT
(dt, 0xdeadbeef)->reg_addr[reg_block];
125
}
126
127
dt_plic_irq_id_t
dt_uart_irq_to_plic_id
(
128
dt_uart_t
dt,
129
dt_uart_irq_t
irq) {
130
dt_plic_irq_id_t
first_irq =
TRY_GET_DT
(dt, kDtPlicIrqIdNone)->first_irq;
131
if
(first_irq == kDtPlicIrqIdNone) {
132
return
kDtPlicIrqIdNone;
133
}
134
return
(
dt_plic_irq_id_t
)((uint32_t)first_irq + (uint32_t)irq);
135
}
136
137
dt_uart_irq_t
dt_uart_irq_from_plic_id
(
138
dt_uart_t
dt,
139
dt_plic_irq_id_t
irq) {
140
dt_uart_irq_t
count =
kDtUartIrqCount
;
141
dt_plic_irq_id_t
first_irq =
TRY_GET_DT
(dt, count)->first_irq;
142
if
(first_irq == kDtPlicIrqIdNone) {
143
return
count;
144
}
145
if
(irq < first_irq || irq >= first_irq + (
dt_plic_irq_id_t
)count) {
146
return
count;
147
}
148
return
(
dt_uart_irq_t
)(irq - first_irq);
149
}
150
151
152
153
dt_periph_io_t
dt_uart_periph_io
(
154
dt_uart_t
dt,
155
dt_uart_periph_io_t
sig) {
156
// Return a harmless value in case of wrong argument.
157
return
TRY_GET_DT
(dt, kDtPeriphIoConstantHighZ)->periph_io[sig];
158
}
159
160
dt_clock_t
dt_uart_clock
(
161
dt_uart_t
dt,
162
dt_uart_clock_t
clk) {
163
// Return the first clock in case of invalid argument.
164
return
TRY_GET_DT
(dt, (
dt_clock_t
)0)->clock[clk];
165
}
166
167
dt_reset_t
dt_uart_reset
(
168
dt_uart_t
dt,
169
dt_uart_reset_t
rst) {
170
const
dt_uart_reset_t
count =
kDtUartResetCount
;
171
if
(rst >= count) {
172
return
kDtResetUnknown
;
173
}
174
return
TRY_GET_DT
(dt,
kDtResetUnknown
)->reset[rst];
175
}
176
177
(englishbreakfast)
hw
top
dt
uart.c
Return to
OpenTitan Documentation