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 earlgrey.
10 */
11
12#include "dt/dt_uart.h"
13
14
15
16/**
17 * Description of instances.
18 */
19typedef 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 */
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 */
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 */
38
39
40
41
42static const dt_desc_uart_t uart_desc[kDtUartCount] = {
43 [kDtUart0] = {
44 .inst_id = kDtInstanceIdUart0,
45 .base_addr = {
46 [kDtUartRegBlockCore] = 0x40000000,
47 },
50 .clock = {
52 },
53 .reset = {
55 },
56 .periph_io = {
57 [kDtUartPeriphIoRx] = {
58 .__internal = {
59 .type = kDtPeriphIoTypeMio,
60 .dir = kDtPeriphIoDirIn,
61 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInUart0Rx,
62 .outsel_or_dt_pad = 0,
63 },
64 },
65 [kDtUartPeriphIoTx] = {
66 .__internal = {
67 .type = kDtPeriphIoTypeMio,
68 .dir = kDtPeriphIoDirOut,
69 .periph_input_or_direct_pad = 0,
70 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselUart0Tx,
71 },
72 },
73 },
74 },
75 [kDtUart1] = {
76 .inst_id = kDtInstanceIdUart1,
77 .base_addr = {
78 [kDtUartRegBlockCore] = 0x40010000,
79 },
82 .clock = {
84 },
85 .reset = {
87 },
88 .periph_io = {
89 [kDtUartPeriphIoRx] = {
90 .__internal = {
91 .type = kDtPeriphIoTypeMio,
92 .dir = kDtPeriphIoDirIn,
93 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInUart1Rx,
94 .outsel_or_dt_pad = 0,
95 },
96 },
97 [kDtUartPeriphIoTx] = {
98 .__internal = {
99 .type = kDtPeriphIoTypeMio,
100 .dir = kDtPeriphIoDirOut,
101 .periph_input_or_direct_pad = 0,
102 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselUart1Tx,
103 },
104 },
105 },
106 },
107 [kDtUart2] = {
108 .inst_id = kDtInstanceIdUart2,
109 .base_addr = {
110 [kDtUartRegBlockCore] = 0x40020000,
111 },
114 .clock = {
116 },
117 .reset = {
119 },
120 .periph_io = {
121 [kDtUartPeriphIoRx] = {
122 .__internal = {
123 .type = kDtPeriphIoTypeMio,
124 .dir = kDtPeriphIoDirIn,
125 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInUart2Rx,
126 .outsel_or_dt_pad = 0,
127 },
128 },
129 [kDtUartPeriphIoTx] = {
130 .__internal = {
131 .type = kDtPeriphIoTypeMio,
132 .dir = kDtPeriphIoDirOut,
133 .periph_input_or_direct_pad = 0,
134 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselUart2Tx,
135 },
136 },
137 },
138 },
139 [kDtUart3] = {
140 .inst_id = kDtInstanceIdUart3,
141 .base_addr = {
142 [kDtUartRegBlockCore] = 0x40030000,
143 },
146 .clock = {
148 },
149 .reset = {
151 },
152 .periph_io = {
153 [kDtUartPeriphIoRx] = {
154 .__internal = {
155 .type = kDtPeriphIoTypeMio,
156 .dir = kDtPeriphIoDirIn,
157 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInUart3Rx,
158 .outsel_or_dt_pad = 0,
159 },
160 },
161 [kDtUartPeriphIoTx] = {
162 .__internal = {
163 .type = kDtPeriphIoTypeMio,
164 .dir = kDtPeriphIoDirOut,
165 .periph_input_or_direct_pad = 0,
166 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselUart3Tx,
167 },
168 },
169 },
170 },
171};
172
173/**
174 * Return a pointer to the `dt_uart_desc_t` structure of the requested
175 * `dt` if it's a valid index. Otherwise, this macro will `return` (i.e. exit
176 * the function) with the provided default value.
177 */
178#define TRY_GET_DT(dt, default) ({ if ((dt) < (dt_uart_t)0 || (dt) >= kDtUartCount) return (default); &uart_desc[dt]; })
179
181 if (inst_id >= kDtInstanceIdUart0 && inst_id <= kDtInstanceIdUart3) {
182 return (dt_uart_t)(inst_id - kDtInstanceIdUart0);
183 }
184 return (dt_uart_t)0;
185}
186
191
193 dt_uart_t dt,
194 dt_uart_reg_block_t reg_block) {
195 // Return a recognizable address in case of wrong argument.
196 return TRY_GET_DT(dt, 0xdeadbeef)->base_addr[reg_block];
197}
198
200 dt_uart_t dt,
201 dt_uart_irq_t irq) {
202 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, kDtPlicIrqIdNone)->first_irq;
203 if (first_irq == kDtPlicIrqIdNone) {
204 return kDtPlicIrqIdNone;
205 }
206 return (dt_plic_irq_id_t)((uint32_t)first_irq + (uint32_t)irq);
207}
208
210 dt_uart_t dt,
211 dt_plic_irq_id_t irq) {
212 dt_uart_irq_t count = kDtUartIrqCount;
213 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, count)->first_irq;
214 if (first_irq == kDtPlicIrqIdNone) {
215 return count;
216 }
217 if (irq < first_irq || irq >= first_irq + (dt_plic_irq_id_t)count) {
218 return count;
219 }
220 return (dt_uart_irq_t)(irq - first_irq);
221}
222
223
225 dt_uart_t dt,
226 dt_uart_alert_t alert) {
227 return (dt_alert_id_t)((uint32_t)uart_desc[dt].first_alert + (uint32_t)alert);
228}
229
231 dt_uart_t dt,
232 dt_alert_id_t alert) {
233 dt_uart_alert_t count = kDtUartAlertCount;
234 if (alert < uart_desc[dt].first_alert || alert >= uart_desc[dt].first_alert + (dt_alert_id_t)count) {
235 return count;
236 }
237 return (dt_uart_alert_t)(alert - uart_desc[dt].first_alert);
238}
239
240
242 dt_uart_t dt,
244 // Return a harmless value in case of wrong argument.
245 return TRY_GET_DT(dt, kDtPeriphIoConstantHighZ)->periph_io[sig];
246}
247
249 dt_uart_t dt,
250 dt_uart_clock_t clk) {
251 // Return the first clock in case of invalid argument.
252 return TRY_GET_DT(dt, (dt_clock_t)0)->clock[clk];
253}
254
256 dt_uart_t dt,
257 dt_uart_reset_t rst) {
258 const dt_uart_reset_t count = kDtUartResetCount;
259 if (rst >= count) {
260 return kDtResetUnknown;
261 }
262 return TRY_GET_DT(dt, kDtResetUnknown)->reset[rst];
263}
264
265