Software APIs
dt_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 darjeeling.
10
*/
11
12
#include "dt/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
base_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_darjeeling_plic_irq_id_t
first_irq
;
28
/**
29
* Alert ID of the first Alert of this instance.
30
*
31
* This value is undefined if the block is not connected to the Alert Handler.
32
*/
33
top_darjeeling_alert_id_t
first_alert
;
34
dt_clock_t
clock
[kDtUartClockCount];
/**< Clock signal connected to each clock port */
35
dt_reset_t
reset
[kDtUartResetCount];
/**< Reset signal connected to each reset port */
36
dt_periph_io_t
periph_io
[kDtUartPeriphIoCount];
/**< Description of each peripheral I/O */
37
}
dt_desc_uart_t
;
38
39
40
41
42
static
const
dt_desc_uart_t
uart_desc[kDtUartCount] = {
43
[
kDtUart0
] = {
44
.inst_id =
kDtInstanceIdUart0
,
45
.base_addr = {
46
[kDtUartRegBlockCore] = 0x30010000,
47
},
48
.first_irq =
kTopDarjeelingPlicIrqIdUart0TxWatermark
,
49
.first_alert =
kTopDarjeelingAlertIdUart0FatalFault
,
50
.clock = {
51
[
kDtUartClockClk
] =
kDtClockIoDiv4
,
52
},
53
.reset = {
54
[
kDtUartResetRst
] =
kDtResetLcIoDiv4
,
55
},
56
.periph_io = {
57
[kDtUartPeriphIoRx] = {
58
.__internal = {
59
.type =
kDtPeriphIoTypeDio
,
60
.dir =
kDtPeriphIoDirIn
,
61
.periph_input_or_direct_pad = kTopDarjeelingDirectPadsUart0Rx,
62
.outsel_or_dt_pad =
kDtPadUart0Rx
,
63
},
64
},
65
[kDtUartPeriphIoTx] = {
66
.__internal = {
67
.type =
kDtPeriphIoTypeDio
,
68
.dir =
kDtPeriphIoDirOut
,
69
.periph_input_or_direct_pad = kTopDarjeelingDirectPadsUart0Tx,
70
.outsel_or_dt_pad =
kDtPadUart0Tx
,
71
},
72
},
73
},
74
},
75
};
76
77
/**
78
* Return a pointer to the `dt_uart_desc_t` structure of the requested
79
* `dt` if it's a valid index. Otherwise, this macro will `return` (i.e. exit
80
* the function) with the provided default value.
81
*/
82
#define TRY_GET_DT(dt, default) ({ if ((dt) < (dt_uart_t)0 || (dt) >= kDtUartCount) return (default); &uart_desc[dt]; })
83
84
dt_uart_t
dt_uart_from_instance_id
(
dt_instance_id_t
inst_id) {
85
if
(inst_id >=
kDtInstanceIdUart0
&& inst_id <=
kDtInstanceIdUart0
) {
86
return
(
dt_uart_t
)(inst_id -
kDtInstanceIdUart0
);
87
}
88
return
(
dt_uart_t
)0;
89
}
90
91
dt_instance_id_t
dt_uart_instance_id
(
92
dt_uart_t
dt) {
93
return
TRY_GET_DT
(dt,
kDtInstanceIdUnknown
)->inst_id;
94
}
95
96
uint32_t
dt_uart_reg_block
(
97
dt_uart_t
dt,
98
dt_uart_reg_block_t
reg_block) {
99
// Return a recognizable address in case of wrong argument.
100
return
TRY_GET_DT
(dt, 0xdeadbeef)->base_addr[reg_block];
101
}
102
103
dt_plic_irq_id_t
dt_uart_irq_to_plic_id
(
104
dt_uart_t
dt,
105
dt_uart_irq_t
irq) {
106
dt_plic_irq_id_t
first_irq =
TRY_GET_DT
(dt, kDtPlicIrqIdNone)->first_irq;
107
if
(first_irq == kDtPlicIrqIdNone) {
108
return
kDtPlicIrqIdNone;
109
}
110
return
(
dt_plic_irq_id_t
)((uint32_t)first_irq + (uint32_t)irq);
111
}
112
113
dt_uart_irq_t
dt_uart_irq_from_plic_id
(
114
dt_uart_t
dt,
115
dt_plic_irq_id_t
irq) {
116
dt_uart_irq_t
count = kDtUartIrqCount;
117
dt_plic_irq_id_t
first_irq =
TRY_GET_DT
(dt, count)->first_irq;
118
if
(first_irq == kDtPlicIrqIdNone) {
119
return
count;
120
}
121
if
(irq < first_irq || irq >= first_irq + (
dt_plic_irq_id_t
)count) {
122
return
count;
123
}
124
return
(
dt_uart_irq_t
)(irq - first_irq);
125
}
126
127
128
dt_alert_id_t
dt_uart_alert_to_alert_id
(
129
dt_uart_t
dt,
130
dt_uart_alert_t
alert) {
131
return
(
dt_alert_id_t
)((uint32_t)uart_desc[dt].first_alert + (uint32_t)alert);
132
}
133
134
dt_uart_alert_t
dt_uart_alert_from_alert_id
(
135
dt_uart_t
dt,
136
dt_alert_id_t
alert) {
137
dt_uart_alert_t
count = kDtUartAlertCount;
138
if
(alert < uart_desc[dt].first_alert || alert >= uart_desc[dt].first_alert + (
dt_alert_id_t
)count) {
139
return
count;
140
}
141
return
(
dt_uart_alert_t
)(alert - uart_desc[dt].first_alert);
142
}
143
144
145
dt_periph_io_t
dt_uart_periph_io
(
146
dt_uart_t
dt,
147
dt_uart_periph_io_t
sig) {
148
// Return a harmless value in case of wrong argument.
149
return
TRY_GET_DT
(dt, kDtPeriphIoConstantHighZ)->periph_io[sig];
150
}
151
152
dt_clock_t
dt_uart_clock
(
153
dt_uart_t
dt,
154
dt_uart_clock_t
clk) {
155
// Return the first clock in case of invalid argument.
156
return
TRY_GET_DT
(dt, (
dt_clock_t
)0)->clock[clk];
157
}
158
159
dt_reset_t
dt_uart_reset
(
160
dt_uart_t
dt,
161
dt_uart_reset_t
rst) {
162
const
dt_uart_reset_t
count = kDtUartResetCount;
163
if
(rst >= count) {
164
return
kDtResetUnknown
;
165
}
166
return
TRY_GET_DT
(dt,
kDtResetUnknown
)->reset[rst];
167
}
168
169
(darjeeling)
hw
top
dt
dt_uart.c
Return to
OpenTitan Documentation