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 "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 base_addr[kDtI2cRegBlockCount]; /**< 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[kDtI2cClockCount]; /**< Clock signal connected to each clock port */
35 dt_reset_t reset[kDtI2cResetCount]; /**< Reset signal connected to each reset port */
36 dt_periph_io_t periph_io[kDtI2cPeriphIoCount]; /**< Description of each peripheral I/O */
38
39
40
41
42static const dt_desc_i2c_t i2c_desc[kDtI2cCount] = {
43 [kDtI2c0] = {
44 .inst_id = kDtInstanceIdI2c0,
45 .base_addr = {
46 [kDtI2cRegBlockCore] = 0x40080000,
47 },
50 .clock = {
52 },
53 .reset = {
55 },
56 .periph_io = {
57 [kDtI2cPeriphIoSda] = {
58 .__internal = {
59 .type = kDtPeriphIoTypeMio,
61 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInI2c0Sda,
62 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselI2c0Sda,
63 },
64 },
65 [kDtI2cPeriphIoScl] = {
66 .__internal = {
67 .type = kDtPeriphIoTypeMio,
69 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInI2c0Scl,
70 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselI2c0Scl,
71 },
72 },
73 },
74 },
75 [kDtI2c1] = {
76 .inst_id = kDtInstanceIdI2c1,
77 .base_addr = {
78 [kDtI2cRegBlockCore] = 0x40090000,
79 },
82 .clock = {
84 },
85 .reset = {
87 },
88 .periph_io = {
89 [kDtI2cPeriphIoSda] = {
90 .__internal = {
91 .type = kDtPeriphIoTypeMio,
93 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInI2c1Sda,
94 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselI2c1Sda,
95 },
96 },
97 [kDtI2cPeriphIoScl] = {
98 .__internal = {
99 .type = kDtPeriphIoTypeMio,
100 .dir = kDtPeriphIoDirInout,
101 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInI2c1Scl,
102 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselI2c1Scl,
103 },
104 },
105 },
106 },
107 [kDtI2c2] = {
108 .inst_id = kDtInstanceIdI2c2,
109 .base_addr = {
110 [kDtI2cRegBlockCore] = 0x400A0000,
111 },
114 .clock = {
116 },
117 .reset = {
119 },
120 .periph_io = {
121 [kDtI2cPeriphIoSda] = {
122 .__internal = {
123 .type = kDtPeriphIoTypeMio,
124 .dir = kDtPeriphIoDirInout,
125 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInI2c2Sda,
126 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselI2c2Sda,
127 },
128 },
129 [kDtI2cPeriphIoScl] = {
130 .__internal = {
131 .type = kDtPeriphIoTypeMio,
132 .dir = kDtPeriphIoDirInout,
133 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInI2c2Scl,
134 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselI2c2Scl,
135 },
136 },
137 },
138 },
139};
140
141/**
142 * Return a pointer to the `dt_i2c_desc_t` structure of the requested
143 * `dt` if it's a valid index. Otherwise, this macro will `return` (i.e. exit
144 * the function) with the provided default value.
145 */
146#define TRY_GET_DT(dt, default) ({ if ((dt) < (dt_i2c_t)0 || (dt) >= kDtI2cCount) return (default); &i2c_desc[dt]; })
147
149 if (inst_id >= kDtInstanceIdI2c0 && inst_id <= kDtInstanceIdI2c2) {
150 return (dt_i2c_t)(inst_id - kDtInstanceIdI2c0);
151 }
152 return (dt_i2c_t)0;
153}
154
159
161 dt_i2c_t dt,
162 dt_i2c_reg_block_t reg_block) {
163 // Return a recognizable address in case of wrong argument.
164 return TRY_GET_DT(dt, 0xdeadbeef)->base_addr[reg_block];
165}
166
168 dt_i2c_t dt,
169 dt_i2c_irq_t irq) {
170 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, kDtPlicIrqIdNone)->first_irq;
171 if (first_irq == kDtPlicIrqIdNone) {
172 return kDtPlicIrqIdNone;
173 }
174 return (dt_plic_irq_id_t)((uint32_t)first_irq + (uint32_t)irq);
175}
176
178 dt_i2c_t dt,
179 dt_plic_irq_id_t irq) {
180 dt_i2c_irq_t count = kDtI2cIrqCount;
181 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, count)->first_irq;
182 if (first_irq == kDtPlicIrqIdNone) {
183 return count;
184 }
185 if (irq < first_irq || irq >= first_irq + (dt_plic_irq_id_t)count) {
186 return count;
187 }
188 return (dt_i2c_irq_t)(irq - first_irq);
189}
190
191
193 dt_i2c_t dt,
194 dt_i2c_alert_t alert) {
195 return (dt_alert_id_t)((uint32_t)i2c_desc[dt].first_alert + (uint32_t)alert);
196}
197
199 dt_i2c_t dt,
200 dt_alert_id_t alert) {
201 dt_i2c_alert_t count = kDtI2cAlertCount;
202 if (alert < i2c_desc[dt].first_alert || alert >= i2c_desc[dt].first_alert + (dt_alert_id_t)count) {
203 return count;
204 }
205 return (dt_i2c_alert_t)(alert - i2c_desc[dt].first_alert);
206}
207
208
210 dt_i2c_t dt,
211 dt_i2c_periph_io_t sig) {
212 // Return a harmless value in case of wrong argument.
213 return TRY_GET_DT(dt, kDtPeriphIoConstantHighZ)->periph_io[sig];
214}
215
217 dt_i2c_t dt,
218 dt_i2c_clock_t clk) {
219 // Return the first clock in case of invalid argument.
220 return TRY_GET_DT(dt, (dt_clock_t)0)->clock[clk];
221}
222
224 dt_i2c_t dt,
225 dt_i2c_reset_t rst) {
226 const dt_i2c_reset_t count = kDtI2cResetCount;
227 if (rst >= count) {
228 return kDtResetUnknown;
229 }
230 return TRY_GET_DT(dt, kDtResetUnknown)->reset[rst];
231}
232
233