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 earlgrey.
10 */
11
12#include "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 base_addr[kDtSpiDeviceRegBlockCount]; /**< 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[kDtSpiDeviceClockCount]; /**< Clock signal connected to each clock port */
35 dt_reset_t reset[kDtSpiDeviceResetCount]; /**< Reset signal connected to each reset port */
36 dt_periph_io_t periph_io[kDtSpiDevicePeriphIoCount]; /**< Description of each peripheral I/O */
38
39
40
41
42static const dt_desc_spi_device_t spi_device_desc[kDtSpiDeviceCount] = {
43 [kDtSpiDevice] = {
44 .inst_id = kDtInstanceIdSpiDevice,
45 .base_addr = {
46 [kDtSpiDeviceRegBlockCore] = 0x40050000,
47 },
50 .clock = {
52 },
53 .reset = {
55 },
56 .periph_io = {
57 [kDtSpiDevicePeriphIoSck] = {
58 .__internal = {
59 .type = kDtPeriphIoTypeDio,
60 .dir = kDtPeriphIoDirIn,
61 .periph_input_or_direct_pad = kTopEarlgreyDirectPadsSpiDeviceSck,
62 .outsel_or_dt_pad = kDtPadSpiDeviceSck,
63 },
64 },
65 [kDtSpiDevicePeriphIoCsb] = {
66 .__internal = {
67 .type = kDtPeriphIoTypeDio,
68 .dir = kDtPeriphIoDirIn,
69 .periph_input_or_direct_pad = kTopEarlgreyDirectPadsSpiDeviceCsb,
70 .outsel_or_dt_pad = kDtPadSpiDeviceCsb,
71 },
72 },
73 [kDtSpiDevicePeriphIoTpmCsb] = {
74 .__internal = {
75 .type = kDtPeriphIoTypeMio,
76 .dir = kDtPeriphIoDirIn,
77 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInSpiDeviceTpmCsb,
78 .outsel_or_dt_pad = 0,
79 },
80 },
81 [kDtSpiDevicePeriphIoSd0] = {
82 .__internal = {
83 .type = kDtPeriphIoTypeDio,
85 .periph_input_or_direct_pad = kTopEarlgreyDirectPadsSpiDeviceSd0,
86 .outsel_or_dt_pad = kDtPadSpiDeviceSd0,
87 },
88 },
89 [kDtSpiDevicePeriphIoSd1] = {
90 .__internal = {
91 .type = kDtPeriphIoTypeDio,
93 .periph_input_or_direct_pad = kTopEarlgreyDirectPadsSpiDeviceSd1,
94 .outsel_or_dt_pad = kDtPadSpiDeviceSd1,
95 },
96 },
97 [kDtSpiDevicePeriphIoSd2] = {
98 .__internal = {
99 .type = kDtPeriphIoTypeDio,
100 .dir = kDtPeriphIoDirInout,
101 .periph_input_or_direct_pad = kTopEarlgreyDirectPadsSpiDeviceSd2,
102 .outsel_or_dt_pad = kDtPadSpiDeviceSd2,
103 },
104 },
105 [kDtSpiDevicePeriphIoSd3] = {
106 .__internal = {
107 .type = kDtPeriphIoTypeDio,
108 .dir = kDtPeriphIoDirInout,
109 .periph_input_or_direct_pad = kTopEarlgreyDirectPadsSpiDeviceSd3,
110 .outsel_or_dt_pad = kDtPadSpiDeviceSd3,
111 },
112 },
113 },
114 },
115};
116
117/**
118 * Return a pointer to the `dt_spi_device_desc_t` structure of the requested
119 * `dt` if it's a valid index. Otherwise, this macro will `return` (i.e. exit
120 * the function) with the provided default value.
121 */
122#define TRY_GET_DT(dt, default) ({ if ((dt) < (dt_spi_device_t)0 || (dt) >= kDtSpiDeviceCount) return (default); &spi_device_desc[dt]; })
123
130
135
138 dt_spi_device_reg_block_t reg_block) {
139 // Return a recognizable address in case of wrong argument.
140 return TRY_GET_DT(dt, 0xdeadbeef)->base_addr[reg_block];
141}
142
146 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, kDtPlicIrqIdNone)->first_irq;
147 if (first_irq == kDtPlicIrqIdNone) {
148 return kDtPlicIrqIdNone;
149 }
150 return (dt_plic_irq_id_t)((uint32_t)first_irq + (uint32_t)irq);
151}
152
155 dt_plic_irq_id_t irq) {
156 dt_spi_device_irq_t count = kDtSpiDeviceIrqCount;
157 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, count)->first_irq;
158 if (first_irq == kDtPlicIrqIdNone) {
159 return count;
160 }
161 if (irq < first_irq || irq >= first_irq + (dt_plic_irq_id_t)count) {
162 return count;
163 }
164 return (dt_spi_device_irq_t)(irq - first_irq);
165}
166
167
170 dt_spi_device_alert_t alert) {
171 return (dt_alert_id_t)((uint32_t)spi_device_desc[dt].first_alert + (uint32_t)alert);
172}
173
176 dt_alert_id_t alert) {
177 dt_spi_device_alert_t count = kDtSpiDeviceAlertCount;
178 if (alert < spi_device_desc[dt].first_alert || alert >= spi_device_desc[dt].first_alert + (dt_alert_id_t)count) {
179 return count;
180 }
181 return (dt_spi_device_alert_t)(alert - spi_device_desc[dt].first_alert);
182}
183
184
188 // Return a harmless value in case of wrong argument.
189 return TRY_GET_DT(dt, kDtPeriphIoConstantHighZ)->periph_io[sig];
190}
191
195 // Return the first clock in case of invalid argument.
196 return TRY_GET_DT(dt, (dt_clock_t)0)->clock[clk];
197}
198
202 const dt_spi_device_reset_t count = kDtSpiDeviceResetCount;
203 if (rst >= count) {
204 return kDtResetUnknown;
205 }
206 return TRY_GET_DT(dt, kDtResetUnknown)->reset[rst];
207}
208
209