Software APIs
dt_usbdev.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 usbdev and top earlgrey.
10 */
11
12#include "dt/dt_usbdev.h"
13
14
15
16/**
17 * Description of instances.
18 */
19typedef struct dt_desc_usbdev {
20 dt_instance_id_t inst_id; /**< Instance ID */
21 uint32_t base_addr[kDtUsbdevRegBlockCount]; /**< 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[kDtUsbdevClockCount]; /**< Clock signal connected to each clock port */
35 dt_reset_t reset[kDtUsbdevResetCount]; /**< Reset signal connected to each reset port */
36 dt_periph_io_t periph_io[kDtUsbdevPeriphIoCount]; /**< Description of each peripheral I/O */
38
39
40
41
42static const dt_desc_usbdev_t usbdev_desc[kDtUsbdevCount] = {
43 [kDtUsbdev] = {
44 .inst_id = kDtInstanceIdUsbdev,
45 .base_addr = {
46 [kDtUsbdevRegBlockCore] = 0x40320000,
47 },
50 .clock = {
53 },
54 .reset = {
57 },
58 .periph_io = {
59 [kDtUsbdevPeriphIoSense] = {
60 .__internal = {
61 .type = kDtPeriphIoTypeMio,
62 .dir = kDtPeriphIoDirIn,
63 .periph_input_or_direct_pad = kTopEarlgreyPinmuxPeripheralInUsbdevSense,
64 .outsel_or_dt_pad = 0,
65 },
66 },
67 [kDtUsbdevPeriphIoUsbDp] = {
68 .__internal = {
71 .periph_input_or_direct_pad = 0,
72 .outsel_or_dt_pad = 0,
73 },
74 },
75 [kDtUsbdevPeriphIoUsbDn] = {
76 .__internal = {
79 .periph_input_or_direct_pad = 0,
80 .outsel_or_dt_pad = 0,
81 },
82 },
83 },
84 },
85};
86
87/**
88 * Return a pointer to the `dt_usbdev_desc_t` structure of the requested
89 * `dt` if it's a valid index. Otherwise, this macro will `return` (i.e. exit
90 * the function) with the provided default value.
91 */
92#define TRY_GET_DT(dt, default) ({ if ((dt) < (dt_usbdev_t)0 || (dt) >= kDtUsbdevCount) return (default); &usbdev_desc[dt]; })
93
95 if (inst_id >= kDtInstanceIdUsbdev && inst_id <= kDtInstanceIdUsbdev) {
96 return (dt_usbdev_t)(inst_id - kDtInstanceIdUsbdev);
97 }
98 return (dt_usbdev_t)0;
99}
100
105
107 dt_usbdev_t dt,
108 dt_usbdev_reg_block_t reg_block) {
109 // Return a recognizable address in case of wrong argument.
110 return TRY_GET_DT(dt, 0xdeadbeef)->base_addr[reg_block];
111}
112
114 dt_usbdev_t dt,
115 dt_usbdev_irq_t irq) {
116 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, kDtPlicIrqIdNone)->first_irq;
117 if (first_irq == kDtPlicIrqIdNone) {
118 return kDtPlicIrqIdNone;
119 }
120 return (dt_plic_irq_id_t)((uint32_t)first_irq + (uint32_t)irq);
121}
122
124 dt_usbdev_t dt,
125 dt_plic_irq_id_t irq) {
126 dt_usbdev_irq_t count = kDtUsbdevIrqCount;
127 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, count)->first_irq;
128 if (first_irq == kDtPlicIrqIdNone) {
129 return count;
130 }
131 if (irq < first_irq || irq >= first_irq + (dt_plic_irq_id_t)count) {
132 return count;
133 }
134 return (dt_usbdev_irq_t)(irq - first_irq);
135}
136
137
139 dt_usbdev_t dt,
140 dt_usbdev_alert_t alert) {
141 return (dt_alert_id_t)((uint32_t)usbdev_desc[dt].first_alert + (uint32_t)alert);
142}
143
145 dt_usbdev_t dt,
146 dt_alert_id_t alert) {
147 dt_usbdev_alert_t count = kDtUsbdevAlertCount;
148 if (alert < usbdev_desc[dt].first_alert || alert >= usbdev_desc[dt].first_alert + (dt_alert_id_t)count) {
149 return count;
150 }
151 return (dt_usbdev_alert_t)(alert - usbdev_desc[dt].first_alert);
152}
153
154
156 dt_usbdev_t dt,
158 // Return a harmless value in case of wrong argument.
159 return TRY_GET_DT(dt, kDtPeriphIoConstantHighZ)->periph_io[sig];
160}
161
163 dt_usbdev_t dt,
164 dt_usbdev_clock_t clk) {
165 // Return the first clock in case of invalid argument.
166 return TRY_GET_DT(dt, (dt_clock_t)0)->clock[clk];
167}
168
170 dt_usbdev_t dt,
171 dt_usbdev_reset_t rst) {
172 const dt_usbdev_reset_t count = kDtUsbdevResetCount;
173 if (rst >= count) {
174 return kDtResetUnknown;
175 }
176 return TRY_GET_DT(dt, kDtResetUnknown)->reset[rst];
177}
178
179