Software APIs
dt_i2c.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 i2c and top earlgrey.
10 */
11
12#include "hw/top/dt/dt_i2c.h"
13
14
15
16/**
17 * Description of instances.
18 */
19typedef struct dt_desc_i2c {
20 dt_instance_id_t inst_id; /**< Instance ID */
21 uint32_t reg_addr[kDtI2cRegBlockCount]; /**< Base address of each register block */
22 uint32_t mem_addr[kDtI2cMemoryCount]; /**< Base address of each memory */
23 uint32_t mem_size[kDtI2cMemoryCount]; /**< Size in bytes of each memory */
24 /**
25 * PLIC ID of the first IRQ of this instance
26 *
27 * This can be `kDtPlicIrqIdNone` if the block is not connected to the PLIC.
28 */
30 /**
31 * Alert ID of the first Alert of this instance.
32 *
33 * This value is undefined if the block is not connected to the Alert Handler.
34 */
36 dt_clock_t clock[kDtI2cClockCount]; /**< Clock signal connected to each clock port */
37 dt_reset_t reset[kDtI2cResetCount]; /**< Reset signal connected to each reset port */
38 dt_periph_io_t periph_io[kDtI2cPeriphIoCount]; /**< Description of each peripheral I/O */
40
41
42
43
44static const dt_desc_i2c_t i2c_desc[kDtI2cCount] = {
45 [kDtI2c0] = {
46 .inst_id = kDtInstanceIdI2c0,
47 .reg_addr = {
48 [kDtI2cRegBlockCore] = 0x40080000,
49 },
50 .mem_addr = {
51 },
52 .mem_size = {
53 },
56 .clock = {
58 },
59 .reset = {
61 },
62 .periph_io = {
63 [kDtI2cPeriphIoSda] = {
64 .__internal = {
65 .type = kDtPeriphIoTypeMio,
67 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInI2c0Sda,
68 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselI2c0Sda,
69 },
70 },
71 [kDtI2cPeriphIoScl] = {
72 .__internal = {
73 .type = kDtPeriphIoTypeMio,
75 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInI2c0Scl,
76 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselI2c0Scl,
77 },
78 },
79 },
80 },
81 [kDtI2c1] = {
82 .inst_id = kDtInstanceIdI2c1,
83 .reg_addr = {
84 [kDtI2cRegBlockCore] = 0x40090000,
85 },
86 .mem_addr = {
87 },
88 .mem_size = {
89 },
92 .clock = {
94 },
95 .reset = {
97 },
98 .periph_io = {
99 [kDtI2cPeriphIoSda] = {
100 .__internal = {
101 .type = kDtPeriphIoTypeMio,
102 .dir = kDtPeriphIoDirInout,
103 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInI2c1Sda,
104 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselI2c1Sda,
105 },
106 },
107 [kDtI2cPeriphIoScl] = {
108 .__internal = {
109 .type = kDtPeriphIoTypeMio,
110 .dir = kDtPeriphIoDirInout,
111 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInI2c1Scl,
112 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselI2c1Scl,
113 },
114 },
115 },
116 },
117 [kDtI2c2] = {
118 .inst_id = kDtInstanceIdI2c2,
119 .reg_addr = {
120 [kDtI2cRegBlockCore] = 0x400A0000,
121 },
122 .mem_addr = {
123 },
124 .mem_size = {
125 },
128 .clock = {
130 },
131 .reset = {
133 },
134 .periph_io = {
135 [kDtI2cPeriphIoSda] = {
136 .__internal = {
137 .type = kDtPeriphIoTypeMio,
138 .dir = kDtPeriphIoDirInout,
139 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInI2c2Sda,
140 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselI2c2Sda,
141 },
142 },
143 [kDtI2cPeriphIoScl] = {
144 .__internal = {
145 .type = kDtPeriphIoTypeMio,
146 .dir = kDtPeriphIoDirInout,
147 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInI2c2Scl,
148 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselI2c2Scl,
149 },
150 },
151 },
152 },
153};
154
155/**
156 * Return a pointer to the `dt_i2c_desc_t` structure of the requested
157 * `dt` if it's a valid index. Otherwise, this macro will `return` (i.e. exit
158 * the function) with the provided default value.
159 */
160#define TRY_GET_DT(dt, default) ({ if ((dt) < (dt_i2c_t)0 || (dt) >= kDtI2cCount) return (default); &i2c_desc[dt]; })
161
163 if (inst_id >= kDtInstanceIdI2c0 && inst_id <= kDtInstanceIdI2c2) {
164 return (dt_i2c_t)(inst_id - kDtInstanceIdI2c0);
165 }
166 return (dt_i2c_t)0;
167}
168
173
175 dt_i2c_t dt,
176 dt_i2c_reg_block_t reg_block) {
177 // Return a recognizable address in case of wrong argument.
178 return TRY_GET_DT(dt, 0xdeadbeef)->reg_addr[reg_block];
179}
180
182 dt_i2c_t dt,
183 dt_i2c_memory_t mem) {
184 // Return a recognizable address in case of wrong argument.
185 return TRY_GET_DT(dt, 0xdeadbeef)->mem_addr[mem];
186}
187
189 dt_i2c_t dt,
190 dt_i2c_memory_t mem) {
191 // Return an empty size in case of wrong argument.
192 return TRY_GET_DT(dt, 0)->mem_size[mem];
193}
194
196 dt_i2c_t dt,
197 dt_i2c_irq_t irq) {
198 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, kDtPlicIrqIdNone)->first_irq;
199 if (first_irq == kDtPlicIrqIdNone) {
200 return kDtPlicIrqIdNone;
201 }
202 return (dt_plic_irq_id_t)((uint32_t)first_irq + (uint32_t)irq);
203}
204
206 dt_i2c_t dt,
207 dt_plic_irq_id_t irq) {
208 dt_i2c_irq_t count = kDtI2cIrqCount;
209 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, count)->first_irq;
210 if (first_irq == kDtPlicIrqIdNone) {
211 return count;
212 }
213 if (irq < first_irq || irq >= first_irq + (dt_plic_irq_id_t)count) {
214 return count;
215 }
216 return (dt_i2c_irq_t)(irq - first_irq);
217}
218
219
221 dt_i2c_t dt,
222 dt_i2c_alert_t alert) {
223 return (dt_alert_id_t)((uint32_t)i2c_desc[dt].first_alert + (uint32_t)alert);
224}
225
227 dt_i2c_t dt,
228 dt_alert_id_t alert) {
229 dt_i2c_alert_t count = kDtI2cAlertCount;
230 if (alert < i2c_desc[dt].first_alert || alert >= i2c_desc[dt].first_alert + (dt_alert_id_t)count) {
231 return count;
232 }
233 return (dt_i2c_alert_t)(alert - i2c_desc[dt].first_alert);
234}
235
236
238 dt_i2c_t dt,
239 dt_i2c_periph_io_t sig) {
240 // Return a harmless value in case of wrong argument.
241 return TRY_GET_DT(dt, kDtPeriphIoConstantHighZ)->periph_io[sig];
242}
243
245 dt_i2c_t dt,
246 dt_i2c_clock_t clk) {
247 // Return the first clock in case of invalid argument.
248 return TRY_GET_DT(dt, (dt_clock_t)0)->clock[clk];
249}
250
252 dt_i2c_t dt,
253 dt_i2c_reset_t rst) {
254 const dt_i2c_reset_t count = kDtI2cResetCount;
255 if (rst >= count) {
256 return kDtResetUnknown;
257 }
258 return TRY_GET_DT(dt, kDtResetUnknown)->reset[rst];
259}
260
261