Skip to content

Commit cd12d20

Browse files
vlsunilpalmer-dabbelt
authored andcommitted
clocksource/timer-riscv: Refactor riscv_timer_init_dt()
Refactor the timer init function such that few things can be shared by both DT and ACPI based platforms. Co-developed-by: Anup Patel <[email protected]> Signed-off-by: Anup Patel <[email protected]> Signed-off-by: Sunil V L <[email protected]> Acked-by: Rafael J. Wysocki <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Reviewed-by: Conor Dooley <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 7023b9d commit cd12d20

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed

drivers/clocksource/timer-riscv.c

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -124,61 +124,28 @@ static irqreturn_t riscv_timer_interrupt(int irq, void *dev_id)
124124
return IRQ_HANDLED;
125125
}
126126

127-
static int __init riscv_timer_init_dt(struct device_node *n)
127+
static int __init riscv_timer_init_common(void)
128128
{
129-
int cpuid, error;
130-
unsigned long hartid;
131-
struct device_node *child;
129+
int error;
132130
struct irq_domain *domain;
131+
struct fwnode_handle *intc_fwnode = riscv_get_intc_hwnode();
133132

134-
error = riscv_of_processor_hartid(n, &hartid);
135-
if (error < 0) {
136-
pr_warn("Not valid hartid for node [%pOF] error = [%lu]\n",
137-
n, hartid);
138-
return error;
139-
}
140-
141-
cpuid = riscv_hartid_to_cpuid(hartid);
142-
if (cpuid < 0) {
143-
pr_warn("Invalid cpuid for hartid [%lu]\n", hartid);
144-
return cpuid;
145-
}
146-
147-
if (cpuid != smp_processor_id())
148-
return 0;
149-
150-
child = of_find_compatible_node(NULL, NULL, "riscv,timer");
151-
if (child) {
152-
riscv_timer_cannot_wake_cpu = of_property_read_bool(child,
153-
"riscv,timer-cannot-wake-cpu");
154-
of_node_put(child);
155-
}
156-
157-
domain = NULL;
158-
child = of_get_compatible_child(n, "riscv,cpu-intc");
159-
if (!child) {
160-
pr_err("Failed to find INTC node [%pOF]\n", n);
161-
return -ENODEV;
162-
}
163-
domain = irq_find_host(child);
164-
of_node_put(child);
133+
domain = irq_find_matching_fwnode(intc_fwnode, DOMAIN_BUS_ANY);
165134
if (!domain) {
166-
pr_err("Failed to find IRQ domain for node [%pOF]\n", n);
135+
pr_err("Failed to find irq_domain for INTC node [%pfwP]\n",
136+
intc_fwnode);
167137
return -ENODEV;
168138
}
169139

170140
riscv_clock_event_irq = irq_create_mapping(domain, RV_IRQ_TIMER);
171141
if (!riscv_clock_event_irq) {
172-
pr_err("Failed to map timer interrupt for node [%pOF]\n", n);
142+
pr_err("Failed to map timer interrupt for node [%pfwP]\n", intc_fwnode);
173143
return -ENODEV;
174144
}
175145

176-
pr_info("%s: Registering clocksource cpuid [%d] hartid [%lu]\n",
177-
__func__, cpuid, hartid);
178146
error = clocksource_register_hz(&riscv_clocksource, riscv_timebase);
179147
if (error) {
180-
pr_err("RISCV timer register failed [%d] for cpu = [%d]\n",
181-
error, cpuid);
148+
pr_err("RISCV timer registration failed [%d]\n", error);
182149
return error;
183150
}
184151

@@ -207,4 +174,36 @@ static int __init riscv_timer_init_dt(struct device_node *n)
207174
return error;
208175
}
209176

177+
static int __init riscv_timer_init_dt(struct device_node *n)
178+
{
179+
int cpuid, error;
180+
unsigned long hartid;
181+
struct device_node *child;
182+
183+
error = riscv_of_processor_hartid(n, &hartid);
184+
if (error < 0) {
185+
pr_warn("Invalid hartid for node [%pOF] error = [%lu]\n",
186+
n, hartid);
187+
return error;
188+
}
189+
190+
cpuid = riscv_hartid_to_cpuid(hartid);
191+
if (cpuid < 0) {
192+
pr_warn("Invalid cpuid for hartid [%lu]\n", hartid);
193+
return cpuid;
194+
}
195+
196+
if (cpuid != smp_processor_id())
197+
return 0;
198+
199+
child = of_find_compatible_node(NULL, NULL, "riscv,timer");
200+
if (child) {
201+
riscv_timer_cannot_wake_cpu = of_property_read_bool(child,
202+
"riscv,timer-cannot-wake-cpu");
203+
of_node_put(child);
204+
}
205+
206+
return riscv_timer_init_common();
207+
}
208+
210209
TIMER_OF_DECLARE(riscv_timer, "riscv", riscv_timer_init_dt);

0 commit comments

Comments
 (0)