Software APIs
dt_rv_timer.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 rv_timer and top earlgrey.
10 */
11
12#include "dt/dt_rv_timer.h"
13
14
15
16/**
17 * Description of instances.
18 */
19typedef struct dt_desc_rv_timer {
20 dt_instance_id_t inst_id; /**< Instance ID */
21 uint32_t base_addr[kDtRvTimerRegBlockCount]; /**< 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[kDtRvTimerClockCount]; /**< Clock signal connected to each clock port */
35 dt_reset_t reset[kDtRvTimerResetCount]; /**< Reset signal connected to each reset port */
37
38
39
40
41static const dt_desc_rv_timer_t rv_timer_desc[kDtRvTimerCount] = {
42 [kDtRvTimer] = {
43 .inst_id = kDtInstanceIdRvTimer,
44 .base_addr = {
45 [kDtRvTimerRegBlockCore] = 0x40100000,
46 },
49 .clock = {
51 },
52 .reset = {
54 },
55 },
56};
57
58/**
59 * Return a pointer to the `dt_rv_timer_desc_t` structure of the requested
60 * `dt` if it's a valid index. Otherwise, this macro will `return` (i.e. exit
61 * the function) with the provided default value.
62 */
63#define TRY_GET_DT(dt, default) ({ if ((dt) < (dt_rv_timer_t)0 || (dt) >= kDtRvTimerCount) return (default); &rv_timer_desc[dt]; })
64
66 if (inst_id >= kDtInstanceIdRvTimer && inst_id <= kDtInstanceIdRvTimer) {
67 return (dt_rv_timer_t)(inst_id - kDtInstanceIdRvTimer);
68 }
69 return (dt_rv_timer_t)0;
70}
71
76
79 dt_rv_timer_reg_block_t reg_block) {
80 // Return a recognizable address in case of wrong argument.
81 return TRY_GET_DT(dt, 0xdeadbeef)->base_addr[reg_block];
82}
83
87 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, kDtPlicIrqIdNone)->first_irq;
88 if (first_irq == kDtPlicIrqIdNone) {
89 return kDtPlicIrqIdNone;
90 }
91 return (dt_plic_irq_id_t)((uint32_t)first_irq + (uint32_t)irq);
92}
93
96 dt_plic_irq_id_t irq) {
97 dt_rv_timer_irq_t count = kDtRvTimerIrqCount;
98 dt_plic_irq_id_t first_irq = TRY_GET_DT(dt, count)->first_irq;
99 if (first_irq == kDtPlicIrqIdNone) {
100 return count;
101 }
102 if (irq < first_irq || irq >= first_irq + (dt_plic_irq_id_t)count) {
103 return count;
104 }
105 return (dt_rv_timer_irq_t)(irq - first_irq);
106}
107
108
110 dt_rv_timer_t dt,
111 dt_rv_timer_alert_t alert) {
112 return (dt_alert_id_t)((uint32_t)rv_timer_desc[dt].first_alert + (uint32_t)alert);
113}
114
116 dt_rv_timer_t dt,
117 dt_alert_id_t alert) {
118 dt_rv_timer_alert_t count = kDtRvTimerAlertCount;
119 if (alert < rv_timer_desc[dt].first_alert || alert >= rv_timer_desc[dt].first_alert + (dt_alert_id_t)count) {
120 return count;
121 }
122 return (dt_rv_timer_alert_t)(alert - rv_timer_desc[dt].first_alert);
123}
124
125
126
128 dt_rv_timer_t dt,
130 // Return the first clock in case of invalid argument.
131 return TRY_GET_DT(dt, (dt_clock_t)0)->clock[clk];
132}
133
135 dt_rv_timer_t dt,
137 const dt_rv_timer_reset_t count = kDtRvTimerResetCount;
138 if (rst >= count) {
139 return kDtResetUnknown;
140 }
141 return TRY_GET_DT(dt, kDtResetUnknown)->reset[rst];
142}
143
144