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 englishbreakfast.
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 dt_clock_t clock[kDtSpiDeviceClockCount]; /**< Clock signal connected to each clock port */
29 dt_reset_t reset[kDtSpiDeviceResetCount]; /**< Reset signal connected to each reset port */
30 dt_periph_io_t periph_io[kDtSpiDevicePeriphIoCount]; /**< Description of each peripheral I/O */
32
33
34
35
36static const dt_desc_spi_device_t spi_device_desc[kDtSpiDeviceCount] = {
37 [kDtSpiDevice] = {
38 .inst_id = kDtInstanceIdSpiDevice,
39 .base_addr = {
40 [kDtSpiDeviceRegBlockCore] = 0x40050000,
41 },
43 .clock = {
45 },
46 .reset = {
48 },
49 .periph_io = {
50 [kDtSpiDevicePeriphIoSck] = {
51 .__internal = {
52 .type = kDtPeriphIoTypeDio,
53 .dir = kDtPeriphIoDirIn,
54 .periph_input_or_direct_pad = kTopEnglishbreakfastDirectPadsSpiDeviceSck,
55 .outsel_or_dt_pad = kDtPadSpiDeviceSck,
56 },
57 },
58 [kDtSpiDevicePeriphIoCsb] = {
59 .__internal = {
60 .type = kDtPeriphIoTypeDio,
61 .dir = kDtPeriphIoDirIn,
62 .periph_input_or_direct_pad = kTopEnglishbreakfastDirectPadsSpiDeviceCsb,
63 .outsel_or_dt_pad = kDtPadSpiDeviceCsb,
64 },
65 },
66 [kDtSpiDevicePeriphIoTpmCsb] = {
67 .__internal = {
70 .periph_input_or_direct_pad = 0,
71 .outsel_or_dt_pad = 0,
72 },
73 },
74 [kDtSpiDevicePeriphIoSd0] = {
75 .__internal = {
76 .type = kDtPeriphIoTypeDio,
78 .periph_input_or_direct_pad = kTopEnglishbreakfastDirectPadsSpiDeviceSd0,
79 .outsel_or_dt_pad = kDtPadSpiDeviceSd0,
80 },
81 },
82 [kDtSpiDevicePeriphIoSd1] = {
83 .__internal = {
84 .type = kDtPeriphIoTypeDio,
86 .periph_input_or_direct_pad = kTopEnglishbreakfastDirectPadsSpiDeviceSd1,
87 .outsel_or_dt_pad = kDtPadSpiDeviceSd1,
88 },
89 },
90 [kDtSpiDevicePeriphIoSd2] = {
91 .__internal = {
92 .type = kDtPeriphIoTypeDio,
94 .periph_input_or_direct_pad = kTopEnglishbreakfastDirectPadsSpiDeviceSd2,
95 .outsel_or_dt_pad = kDtPadSpiDeviceSd2,
96 },
97 },
98 [kDtSpiDevicePeriphIoSd3] = {
99 .__internal = {
100 .type = kDtPeriphIoTypeDio,
101 .dir = kDtPeriphIoDirInout,
102 .periph_input_or_direct_pad = kTopEnglishbreakfastDirectPadsSpiDeviceSd3,
103 .outsel_or_dt_pad = kDtPadSpiDeviceSd3,
104 },
105 },
106 },
107 },
108};
109
110/**
111 * Return a pointer to the `dt_spi_device_desc_t` structure of the requested
112 * `dt` if it's a valid index. Otherwise, this macro will `return` (i.e. exit
113 * the function) with the provided default value.
114 */
115#define TRY_GET_DT(dt, default) ({ if ((dt) < (dt_spi_device_t)0 || (dt) >= kDtSpiDeviceCount) return (default); &spi_device_desc[dt]; })
116
123
128
131 dt_spi_device_reg_block_t reg_block) {
132 // Return a recognizable address in case of wrong argument.
133 return TRY_GET_DT(dt, 0xdeadbeef)->base_addr[reg_block];
134}
135
139 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, kDtPlicIrqIdNone)->first_irq;
140 if (first_irq == kDtPlicIrqIdNone) {
141 return kDtPlicIrqIdNone;
142 }
143 return (dt_plic_irq_id_t)((uint32_t)first_irq + (uint32_t)irq);
144}
145
148 dt_plic_irq_id_t irq) {
149 dt_spi_device_irq_t count = kDtSpiDeviceIrqCount;
150 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, count)->first_irq;
151 if (first_irq == kDtPlicIrqIdNone) {
152 return count;
153 }
154 if (irq < first_irq || irq >= first_irq + (dt_plic_irq_id_t)count) {
155 return count;
156 }
157 return (dt_spi_device_irq_t)(irq - first_irq);
158}
159
160
161
165 // Return a harmless value in case of wrong argument.
166 return TRY_GET_DT(dt, kDtPeriphIoConstantHighZ)->periph_io[sig];
167}
168
172 // Return the first clock in case of invalid argument.
173 return TRY_GET_DT(dt, (dt_clock_t)0)->clock[clk];
174}
175
179 const dt_spi_device_reset_t count = kDtSpiDeviceResetCount;
180 if (rst >= count) {
181 return kDtResetUnknown;
182 }
183 return TRY_GET_DT(dt, kDtResetUnknown)->reset[rst];
184}
185
186