Software APIs
dt_spi_host.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 spi_host and top earlgrey.
10 */
11
12#include "dt/dt_spi_host.h"
13
14
15
16/**
17 * Description of instances.
18 */
19typedef struct dt_desc_spi_host {
20 dt_instance_id_t inst_id; /**< Instance ID */
21 uint32_t base_addr[kDtSpiHostRegBlockCount]; /**< 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[kDtSpiHostClockCount]; /**< Clock signal connected to each clock port */
35 dt_reset_t reset[kDtSpiHostResetCount]; /**< Reset signal connected to each reset port */
36 dt_periph_io_t periph_io[kDtSpiHostPeriphIoCount]; /**< Description of each peripheral I/O */
38
39
40
41
42static const dt_desc_spi_host_t spi_host_desc[kDtSpiHostCount] = {
43 [kDtSpiHost0] = {
44 .inst_id = kDtInstanceIdSpiHost0,
45 .base_addr = {
46 [kDtSpiHostRegBlockCore] = 0x40300000,
47 },
50 .clock = {
52 },
53 .reset = {
55 },
56 .periph_io = {
57 [kDtSpiHostPeriphIoSck] = {
58 .__internal = {
59 .type = kDtPeriphIoTypeDio,
60 .dir = kDtPeriphIoDirOut,
61 .periph_input_or_direct_pad = kTopEarlgreyDirectPadsSpiHost0Sck,
62 .outsel_or_dt_pad = kDtPadSpiHost0Sck,
63 },
64 },
65 [kDtSpiHostPeriphIoCsb] = {
66 .__internal = {
67 .type = kDtPeriphIoTypeDio,
68 .dir = kDtPeriphIoDirOut,
69 .periph_input_or_direct_pad = kTopEarlgreyDirectPadsSpiHost0Csb,
70 .outsel_or_dt_pad = kDtPadSpiHost0Csb,
71 },
72 },
73 [kDtSpiHostPeriphIoSd0] = {
74 .__internal = {
75 .type = kDtPeriphIoTypeDio,
77 .periph_input_or_direct_pad = kTopEarlgreyDirectPadsSpiHost0Sd0,
78 .outsel_or_dt_pad = kDtPadSpiHost0Sd0,
79 },
80 },
81 [kDtSpiHostPeriphIoSd1] = {
82 .__internal = {
83 .type = kDtPeriphIoTypeDio,
85 .periph_input_or_direct_pad = kTopEarlgreyDirectPadsSpiHost0Sd1,
86 .outsel_or_dt_pad = kDtPadSpiHost0Sd1,
87 },
88 },
89 [kDtSpiHostPeriphIoSd2] = {
90 .__internal = {
91 .type = kDtPeriphIoTypeDio,
93 .periph_input_or_direct_pad = kTopEarlgreyDirectPadsSpiHost0Sd2,
94 .outsel_or_dt_pad = kDtPadSpiHost0Sd2,
95 },
96 },
97 [kDtSpiHostPeriphIoSd3] = {
98 .__internal = {
99 .type = kDtPeriphIoTypeDio,
100 .dir = kDtPeriphIoDirInout,
101 .periph_input_or_direct_pad = kTopEarlgreyDirectPadsSpiHost0Sd3,
102 .outsel_or_dt_pad = kDtPadSpiHost0Sd3,
103 },
104 },
105 },
106 },
107 [kDtSpiHost1] = {
108 .inst_id = kDtInstanceIdSpiHost1,
109 .base_addr = {
110 [kDtSpiHostRegBlockCore] = 0x40310000,
111 },
114 .clock = {
116 },
117 .reset = {
119 },
120 .periph_io = {
121 [kDtSpiHostPeriphIoSck] = {
122 .__internal = {
123 .type = kDtPeriphIoTypeMio,
124 .dir = kDtPeriphIoDirOut,
125 .periph_input_or_direct_pad = 0,
126 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselSpiHost1Sck,
127 },
128 },
129 [kDtSpiHostPeriphIoCsb] = {
130 .__internal = {
131 .type = kDtPeriphIoTypeMio,
132 .dir = kDtPeriphIoDirOut,
133 .periph_input_or_direct_pad = 0,
134 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselSpiHost1Csb,
135 },
136 },
137 [kDtSpiHostPeriphIoSd0] = {
138 .__internal = {
139 .type = kDtPeriphIoTypeMio,
140 .dir = kDtPeriphIoDirInout,
141 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInSpiHost1Sd0,
142 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselSpiHost1Sd0,
143 },
144 },
145 [kDtSpiHostPeriphIoSd1] = {
146 .__internal = {
147 .type = kDtPeriphIoTypeMio,
148 .dir = kDtPeriphIoDirInout,
149 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInSpiHost1Sd1,
150 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselSpiHost1Sd1,
151 },
152 },
153 [kDtSpiHostPeriphIoSd2] = {
154 .__internal = {
155 .type = kDtPeriphIoTypeMio,
156 .dir = kDtPeriphIoDirInout,
157 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInSpiHost1Sd2,
158 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselSpiHost1Sd2,
159 },
160 },
161 [kDtSpiHostPeriphIoSd3] = {
162 .__internal = {
163 .type = kDtPeriphIoTypeMio,
164 .dir = kDtPeriphIoDirInout,
165 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInSpiHost1Sd3,
166 .outsel_or_dt_pad = kTopEarlgreyPinmuxOutselSpiHost1Sd3,
167 },
168 },
169 },
170 },
171};
172
173/**
174 * Return a pointer to the `dt_spi_host_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_spi_host_t)0 || (dt) >= kDtSpiHostCount) return (default); &spi_host_desc[dt]; })
179
181 if (inst_id >= kDtInstanceIdSpiHost0 && inst_id <= kDtInstanceIdSpiHost1) {
182 return (dt_spi_host_t)(inst_id - kDtInstanceIdSpiHost0);
183 }
184 return (dt_spi_host_t)0;
185}
186
191
193 dt_spi_host_t dt,
194 dt_spi_host_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_spi_host_t dt,
201 dt_spi_host_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_spi_host_t dt,
211 dt_plic_irq_id_t irq) {
212 dt_spi_host_irq_t count = kDtSpiHostIrqCount;
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_spi_host_irq_t)(irq - first_irq);
221}
222
223
225 dt_spi_host_t dt,
226 dt_spi_host_alert_t alert) {
227 return (dt_alert_id_t)((uint32_t)spi_host_desc[dt].first_alert + (uint32_t)alert);
228}
229
231 dt_spi_host_t dt,
232 dt_alert_id_t alert) {
233 dt_spi_host_alert_t count = kDtSpiHostAlertCount;
234 if (alert < spi_host_desc[dt].first_alert || alert >= spi_host_desc[dt].first_alert + (dt_alert_id_t)count) {
235 return count;
236 }
237 return (dt_spi_host_alert_t)(alert - spi_host_desc[dt].first_alert);
238}
239
240
242 dt_spi_host_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_spi_host_t dt,
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_spi_host_t dt,
258 const dt_spi_host_reset_t count = kDtSpiHostResetCount;
259 if (rst >= count) {
260 return kDtResetUnknown;
261 }
262 return TRY_GET_DT(dt, kDtResetUnknown)->reset[rst];
263}
264
265