Software APIs
dt_spi_device.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_device and top darjeeling.
10 */
11
12#include "hw/top/dt/dt_spi_device.h"
13
14
15
16/**
17 * Description of instances.
18 */
19typedef struct dt_desc_spi_device {
20 dt_instance_id_t inst_id; /**< Instance ID */
21 uint32_t reg_addr[kDtSpiDeviceRegBlockCount]; /**< Base address of each register block */
22 uint32_t mem_addr[kDtSpiDeviceMemoryCount]; /**< Base address of each memory */
23 uint32_t mem_size[kDtSpiDeviceMemoryCount]; /**< 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[kDtSpiDeviceClockCount]; /**< Clock signal connected to each clock port */
37 dt_reset_t reset[kDtSpiDeviceResetCount]; /**< Reset signal connected to each reset port */
38 dt_periph_io_t periph_io[kDtSpiDevicePeriphIoCount]; /**< Description of each peripheral I/O */
40
41
42
43
44static const dt_desc_spi_device_t spi_device_desc[kDtSpiDeviceCount] = {
45 [kDtSpiDevice] = {
46 .inst_id = kDtInstanceIdSpiDevice,
47 .reg_addr = {
48 [kDtSpiDeviceRegBlockCore] = 0x30310000,
49 },
50 .mem_addr = {
51 },
52 .mem_size = {
53 },
56 .clock = {
58 },
59 .reset = {
61 },
62 .periph_io = {
63 [kDtSpiDevicePeriphIoSck] = {
64 .__internal = {
65 .type = kDtPeriphIoTypeDio,
66 .dir = kDtPeriphIoDirIn,
67 .periph_input_or_direct_pad = kTopDarjeelingDirectPadsSpiDeviceSck,
68 .outsel_or_dt_pad = kDtPadSpiDeviceSck,
69 },
70 },
71 [kDtSpiDevicePeriphIoCsb] = {
72 .__internal = {
73 .type = kDtPeriphIoTypeDio,
74 .dir = kDtPeriphIoDirIn,
75 .periph_input_or_direct_pad = kTopDarjeelingDirectPadsSpiDeviceCsb,
76 .outsel_or_dt_pad = kDtPadSpiDeviceCsb,
77 },
78 },
79 [kDtSpiDevicePeriphIoTpmCsb] = {
80 .__internal = {
81 .type = kDtPeriphIoTypeDio,
82 .dir = kDtPeriphIoDirIn,
83 .periph_input_or_direct_pad = kTopDarjeelingDirectPadsSpiDeviceTpmCsb,
84 .outsel_or_dt_pad = kDtPadSpiDeviceTpmCsb,
85 },
86 },
87 [kDtSpiDevicePeriphIoSd0] = {
88 .__internal = {
89 .type = kDtPeriphIoTypeDio,
91 .periph_input_or_direct_pad = kTopDarjeelingDirectPadsSpiDeviceSd0,
92 .outsel_or_dt_pad = kDtPadSpiDeviceSd0,
93 },
94 },
95 [kDtSpiDevicePeriphIoSd1] = {
96 .__internal = {
97 .type = kDtPeriphIoTypeDio,
99 .periph_input_or_direct_pad = kTopDarjeelingDirectPadsSpiDeviceSd1,
100 .outsel_or_dt_pad = kDtPadSpiDeviceSd1,
101 },
102 },
103 [kDtSpiDevicePeriphIoSd2] = {
104 .__internal = {
105 .type = kDtPeriphIoTypeDio,
106 .dir = kDtPeriphIoDirInout,
107 .periph_input_or_direct_pad = kTopDarjeelingDirectPadsSpiDeviceSd2,
108 .outsel_or_dt_pad = kDtPadSpiDeviceSd2,
109 },
110 },
111 [kDtSpiDevicePeriphIoSd3] = {
112 .__internal = {
113 .type = kDtPeriphIoTypeDio,
114 .dir = kDtPeriphIoDirInout,
115 .periph_input_or_direct_pad = kTopDarjeelingDirectPadsSpiDeviceSd3,
116 .outsel_or_dt_pad = kDtPadSpiDeviceSd3,
117 },
118 },
119 },
120 },
121};
122
123/**
124 * Return a pointer to the `dt_spi_device_desc_t` structure of the requested
125 * `dt` if it's a valid index. Otherwise, this macro will `return` (i.e. exit
126 * the function) with the provided default value.
127 */
128#define TRY_GET_DT(dt, default) ({ if ((dt) < (dt_spi_device_t)0 || (dt) >= kDtSpiDeviceCount) return (default); &spi_device_desc[dt]; })
129
136
141
144 dt_spi_device_reg_block_t reg_block) {
145 // Return a recognizable address in case of wrong argument.
146 return TRY_GET_DT(dt, 0xdeadbeef)->reg_addr[reg_block];
147}
148
152 // Return a recognizable address in case of wrong argument.
153 return TRY_GET_DT(dt, 0xdeadbeef)->mem_addr[mem];
154}
155
159 // Return an empty size in case of wrong argument.
160 return TRY_GET_DT(dt, 0)->mem_size[mem];
161}
162
166 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, kDtPlicIrqIdNone)->first_irq;
167 if (first_irq == kDtPlicIrqIdNone) {
168 return kDtPlicIrqIdNone;
169 }
170 return (dt_plic_irq_id_t)((uint32_t)first_irq + (uint32_t)irq);
171}
172
175 dt_plic_irq_id_t irq) {
176 dt_spi_device_irq_t count = kDtSpiDeviceIrqCount;
177 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, count)->first_irq;
178 if (first_irq == kDtPlicIrqIdNone) {
179 return count;
180 }
181 if (irq < first_irq || irq >= first_irq + (dt_plic_irq_id_t)count) {
182 return count;
183 }
184 return (dt_spi_device_irq_t)(irq - first_irq);
185}
186
187
190 dt_spi_device_alert_t alert) {
191 return (dt_alert_id_t)((uint32_t)spi_device_desc[dt].first_alert + (uint32_t)alert);
192}
193
196 dt_alert_id_t alert) {
197 dt_spi_device_alert_t count = kDtSpiDeviceAlertCount;
198 if (alert < spi_device_desc[dt].first_alert || alert >= spi_device_desc[dt].first_alert + (dt_alert_id_t)count) {
199 return count;
200 }
201 return (dt_spi_device_alert_t)(alert - spi_device_desc[dt].first_alert);
202}
203
204
208 // Return a harmless value in case of wrong argument.
209 return TRY_GET_DT(dt, kDtPeriphIoConstantHighZ)->periph_io[sig];
210}
211
215 // Return the first clock in case of invalid argument.
216 return TRY_GET_DT(dt, (dt_clock_t)0)->clock[clk];
217}
218
222 const dt_spi_device_reset_t count = kDtSpiDeviceResetCount;
223 if (rst >= count) {
224 return kDtResetUnknown;
225 }
226 return TRY_GET_DT(dt, kDtResetUnknown)->reset[rst];
227}
228
229