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 englishbreakfast.
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 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 */
32
33
34
35
36static const dt_desc_uart_t uart_desc[kDtUartCount] = {
37 [kDtUart0] = {
38 .inst_id = kDtInstanceIdUart0,
39 .base_addr = {
40 [kDtUartRegBlockCore] = 0x40000000,
41 },
43 .clock = {
45 },
46 .reset = {
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,
64 },
65 },
66 },
67 },
68 [kDtUart1] = {
69 .inst_id = kDtInstanceIdUart1,
70 .base_addr = {
71 [kDtUartRegBlockCore] = 0x40010000,
72 },
74 .clock = {
76 },
77 .reset = {
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,
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 || (dt) >= kDtUartCount) return (default); &uart_desc[dt]; })
107
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
119
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)->base_addr[reg_block];
125}
126
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
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
154 dt_uart_t dt,
156 // Return a harmless value in case of wrong argument.
157 return TRY_GET_DT(dt, kDtPeriphIoConstantHighZ)->periph_io[sig];
158}
159
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
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