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 darjeeling.
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] = 0x30300000,
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 = kTopDarjeelingDirectPadsSpiHost0Sck,
62 .outsel_or_dt_pad = kDtPadSpiHost0Sck,
63 },
64 },
65 [kDtSpiHostPeriphIoCsb] = {
66 .__internal = {
67 .type = kDtPeriphIoTypeDio,
68 .dir = kDtPeriphIoDirOut,
69 .periph_input_or_direct_pad = kTopDarjeelingDirectPadsSpiHost0Csb,
70 .outsel_or_dt_pad = kDtPadSpiHost0Csb,
71 },
72 },
73 [kDtSpiHostPeriphIoSd0] = {
74 .__internal = {
75 .type = kDtPeriphIoTypeDio,
77 .periph_input_or_direct_pad = kTopDarjeelingDirectPadsSpiHost0Sd0,
78 .outsel_or_dt_pad = kDtPadSpiHost0Sd0,
79 },
80 },
81 [kDtSpiHostPeriphIoSd1] = {
82 .__internal = {
83 .type = kDtPeriphIoTypeDio,
85 .periph_input_or_direct_pad = kTopDarjeelingDirectPadsSpiHost0Sd1,
86 .outsel_or_dt_pad = kDtPadSpiHost0Sd1,
87 },
88 },
89 [kDtSpiHostPeriphIoSd2] = {
90 .__internal = {
91 .type = kDtPeriphIoTypeDio,
93 .periph_input_or_direct_pad = kTopDarjeelingDirectPadsSpiHost0Sd2,
94 .outsel_or_dt_pad = kDtPadSpiHost0Sd2,
95 },
96 },
97 [kDtSpiHostPeriphIoSd3] = {
98 .__internal = {
99 .type = kDtPeriphIoTypeDio,
100 .dir = kDtPeriphIoDirInout,
101 .periph_input_or_direct_pad = kTopDarjeelingDirectPadsSpiHost0Sd3,
102 .outsel_or_dt_pad = kDtPadSpiHost0Sd3,
103 },
104 },
105 },
106 },
107};
108
109/**
110 * Return a pointer to the `dt_spi_host_desc_t` structure of the requested
111 * `dt` if it's a valid index. Otherwise, this macro will `return` (i.e. exit
112 * the function) with the provided default value.
113 */
114#define TRY_GET_DT(dt, default) ({ if ((dt) < (dt_spi_host_t)0 || (dt) >= kDtSpiHostCount) return (default); &spi_host_desc[dt]; })
115
117 if (inst_id >= kDtInstanceIdSpiHost0 && inst_id <= kDtInstanceIdSpiHost0) {
118 return (dt_spi_host_t)(inst_id - kDtInstanceIdSpiHost0);
119 }
120 return (dt_spi_host_t)0;
121}
122
127
129 dt_spi_host_t dt,
130 dt_spi_host_reg_block_t reg_block) {
131 // Return a recognizable address in case of wrong argument.
132 return TRY_GET_DT(dt, 0xdeadbeef)->base_addr[reg_block];
133}
134
136 dt_spi_host_t dt,
137 dt_spi_host_irq_t irq) {
138 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, kDtPlicIrqIdNone)->first_irq;
139 if (first_irq == kDtPlicIrqIdNone) {
140 return kDtPlicIrqIdNone;
141 }
142 return (dt_plic_irq_id_t)((uint32_t)first_irq + (uint32_t)irq);
143}
144
146 dt_spi_host_t dt,
147 dt_plic_irq_id_t irq) {
148 dt_spi_host_irq_t count = kDtSpiHostIrqCount;
149 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, count)->first_irq;
150 if (first_irq == kDtPlicIrqIdNone) {
151 return count;
152 }
153 if (irq < first_irq || irq >= first_irq + (dt_plic_irq_id_t)count) {
154 return count;
155 }
156 return (dt_spi_host_irq_t)(irq - first_irq);
157}
158
159
161 dt_spi_host_t dt,
162 dt_spi_host_alert_t alert) {
163 return (dt_alert_id_t)((uint32_t)spi_host_desc[dt].first_alert + (uint32_t)alert);
164}
165
167 dt_spi_host_t dt,
168 dt_alert_id_t alert) {
169 dt_spi_host_alert_t count = kDtSpiHostAlertCount;
170 if (alert < spi_host_desc[dt].first_alert || alert >= spi_host_desc[dt].first_alert + (dt_alert_id_t)count) {
171 return count;
172 }
173 return (dt_spi_host_alert_t)(alert - spi_host_desc[dt].first_alert);
174}
175
176
178 dt_spi_host_t dt,
180 // Return a harmless value in case of wrong argument.
181 return TRY_GET_DT(dt, kDtPeriphIoConstantHighZ)->periph_io[sig];
182}
183
185 dt_spi_host_t dt,
187 // Return the first clock in case of invalid argument.
188 return TRY_GET_DT(dt, (dt_clock_t)0)->clock[clk];
189}
190
192 dt_spi_host_t dt,
194 const dt_spi_host_reset_t count = kDtSpiHostResetCount;
195 if (rst >= count) {
196 return kDtResetUnknown;
197 }
198 return TRY_GET_DT(dt, kDtResetUnknown)->reset[rst];
199}
200
201