xref: /linux/drivers/clocksource/timer-of.c (revision 1a371190a375f98c9b106f758ea41558c3f92556)
19952f691SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2dc11bae7SDaniel Lezcano /*
3dc11bae7SDaniel Lezcano  * Copyright (c) 2017, Linaro Ltd.  All rights reserved.
4dc11bae7SDaniel Lezcano  *
5dc11bae7SDaniel Lezcano  * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
6dc11bae7SDaniel Lezcano  */
7dc11bae7SDaniel Lezcano #include <linux/clk.h>
8dc11bae7SDaniel Lezcano #include <linux/interrupt.h>
9dc11bae7SDaniel Lezcano #include <linux/of.h>
10dc11bae7SDaniel Lezcano #include <linux/of_address.h>
11dc11bae7SDaniel Lezcano #include <linux/of_irq.h>
12dc11bae7SDaniel Lezcano #include <linux/slab.h>
13dc11bae7SDaniel Lezcano 
14dc11bae7SDaniel Lezcano #include "timer-of.h"
15dc11bae7SDaniel Lezcano 
16cf7f46b9SDaniel Lezcano /**
17cf7f46b9SDaniel Lezcano  * timer_of_irq_exit - Release the interrupt
18cf7f46b9SDaniel Lezcano  * @of_irq: an of_timer_irq structure pointer
19cf7f46b9SDaniel Lezcano  *
20cf7f46b9SDaniel Lezcano  * Free the irq resource
21cf7f46b9SDaniel Lezcano  */
timer_of_irq_exit(struct of_timer_irq * of_irq)225bbf4ad9SDaniel Lezcano static __init void timer_of_irq_exit(struct of_timer_irq *of_irq)
23dc11bae7SDaniel Lezcano {
24dc11bae7SDaniel Lezcano 	struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
25dc11bae7SDaniel Lezcano 
26dc11bae7SDaniel Lezcano 	struct clock_event_device *clkevt = &to->clkevt;
27dc11bae7SDaniel Lezcano 
28dc11bae7SDaniel Lezcano 	free_irq(of_irq->irq, clkevt);
29dc11bae7SDaniel Lezcano }
30dc11bae7SDaniel Lezcano 
31cf7f46b9SDaniel Lezcano /**
32cf7f46b9SDaniel Lezcano  * timer_of_irq_init - Request the interrupt
33cf7f46b9SDaniel Lezcano  * @np: a device tree node pointer
34cf7f46b9SDaniel Lezcano  * @of_irq: an of_timer_irq structure pointer
35cf7f46b9SDaniel Lezcano  *
36cf7f46b9SDaniel Lezcano  * Get the interrupt number from the DT from its definition and
37cf7f46b9SDaniel Lezcano  * request it. The interrupt is gotten by falling back the following way:
38cf7f46b9SDaniel Lezcano  *
39cf7f46b9SDaniel Lezcano  * - Get interrupt number by name
40cf7f46b9SDaniel Lezcano  * - Get interrupt number by index
41cf7f46b9SDaniel Lezcano  *
42cf7f46b9SDaniel Lezcano  * Returns 0 on success, < 0 otherwise
43cf7f46b9SDaniel Lezcano  */
timer_of_irq_init(struct device_node * np,struct of_timer_irq * of_irq)445bbf4ad9SDaniel Lezcano static __init int timer_of_irq_init(struct device_node *np,
45dc11bae7SDaniel Lezcano 				    struct of_timer_irq *of_irq)
46dc11bae7SDaniel Lezcano {
47dc11bae7SDaniel Lezcano 	int ret;
48dc11bae7SDaniel Lezcano 	struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
49dc11bae7SDaniel Lezcano 	struct clock_event_device *clkevt = &to->clkevt;
50dc11bae7SDaniel Lezcano 
5132f2fea6SSergei Shtylyov 	if (of_irq->name) {
5232f2fea6SSergei Shtylyov 		of_irq->irq = ret = of_irq_get_byname(np, of_irq->name);
5332f2fea6SSergei Shtylyov 		if (ret < 0) {
54ccb80012SGeert Uytterhoeven 			pr_err("Failed to get interrupt %s for %pOF\n",
55ccb80012SGeert Uytterhoeven 			       of_irq->name, np);
5632f2fea6SSergei Shtylyov 			return ret;
5732f2fea6SSergei Shtylyov 		}
5832f2fea6SSergei Shtylyov 	} else	{
5932f2fea6SSergei Shtylyov 		of_irq->irq = irq_of_parse_and_map(np, of_irq->index);
6032f2fea6SSergei Shtylyov 	}
61dc11bae7SDaniel Lezcano 	if (!of_irq->irq) {
62469869d1SRob Herring 		pr_err("Failed to map interrupt for %pOF\n", np);
63dc11bae7SDaniel Lezcano 		return -EINVAL;
64dc11bae7SDaniel Lezcano 	}
65dc11bae7SDaniel Lezcano 
66*471ef0b5SDaniel Lezcano 	ret = request_irq(of_irq->irq, of_irq->handler,
67dc11bae7SDaniel Lezcano 			  of_irq->flags ? of_irq->flags : IRQF_TIMER,
68dc11bae7SDaniel Lezcano 			  np->full_name, clkevt);
69dc11bae7SDaniel Lezcano 	if (ret) {
70469869d1SRob Herring 		pr_err("Failed to request irq %d for %pOF\n", of_irq->irq, np);
71dc11bae7SDaniel Lezcano 		return ret;
72dc11bae7SDaniel Lezcano 	}
73dc11bae7SDaniel Lezcano 
74dc11bae7SDaniel Lezcano 	clkevt->irq = of_irq->irq;
75dc11bae7SDaniel Lezcano 
76dc11bae7SDaniel Lezcano 	return 0;
77dc11bae7SDaniel Lezcano }
78dc11bae7SDaniel Lezcano 
79cf7f46b9SDaniel Lezcano /**
80cf7f46b9SDaniel Lezcano  * timer_of_clk_exit - Release the clock resources
81cf7f46b9SDaniel Lezcano  * @of_clk: a of_timer_clk structure pointer
82cf7f46b9SDaniel Lezcano  *
83cf7f46b9SDaniel Lezcano  * Disables and releases the refcount on the clk
84cf7f46b9SDaniel Lezcano  */
timer_of_clk_exit(struct of_timer_clk * of_clk)855bbf4ad9SDaniel Lezcano static __init void timer_of_clk_exit(struct of_timer_clk *of_clk)
86dc11bae7SDaniel Lezcano {
87dc11bae7SDaniel Lezcano 	of_clk->rate = 0;
88dc11bae7SDaniel Lezcano 	clk_disable_unprepare(of_clk->clk);
89dc11bae7SDaniel Lezcano 	clk_put(of_clk->clk);
90dc11bae7SDaniel Lezcano }
91dc11bae7SDaniel Lezcano 
92cf7f46b9SDaniel Lezcano /**
93cf7f46b9SDaniel Lezcano  * timer_of_clk_init - Initialize the clock resources
94cf7f46b9SDaniel Lezcano  * @np: a device tree node pointer
95cf7f46b9SDaniel Lezcano  * @of_clk: a of_timer_clk structure pointer
96cf7f46b9SDaniel Lezcano  *
97cf7f46b9SDaniel Lezcano  * Get the clock by name or by index, enable it and get the rate
98cf7f46b9SDaniel Lezcano  *
99cf7f46b9SDaniel Lezcano  * Returns 0 on success, < 0 otherwise
100cf7f46b9SDaniel Lezcano  */
timer_of_clk_init(struct device_node * np,struct of_timer_clk * of_clk)1015bbf4ad9SDaniel Lezcano static __init int timer_of_clk_init(struct device_node *np,
102dc11bae7SDaniel Lezcano 				    struct of_timer_clk *of_clk)
103dc11bae7SDaniel Lezcano {
104dc11bae7SDaniel Lezcano 	int ret;
105dc11bae7SDaniel Lezcano 
106dc11bae7SDaniel Lezcano 	of_clk->clk = of_clk->name ? of_clk_get_by_name(np, of_clk->name) :
107dc11bae7SDaniel Lezcano 		of_clk_get(np, of_clk->index);
108dc11bae7SDaniel Lezcano 	if (IS_ERR(of_clk->clk)) {
10976371977SJon Hunter 		ret = PTR_ERR(of_clk->clk);
11076371977SJon Hunter 		if (ret != -EPROBE_DEFER)
111469869d1SRob Herring 			pr_err("Failed to get clock for %pOF\n", np);
11276371977SJon Hunter 		goto out;
113dc11bae7SDaniel Lezcano 	}
114dc11bae7SDaniel Lezcano 
115dc11bae7SDaniel Lezcano 	ret = clk_prepare_enable(of_clk->clk);
116dc11bae7SDaniel Lezcano 	if (ret) {
117469869d1SRob Herring 		pr_err("Failed for enable clock for %pOF\n", np);
118dc11bae7SDaniel Lezcano 		goto out_clk_put;
119dc11bae7SDaniel Lezcano 	}
120dc11bae7SDaniel Lezcano 
121dc11bae7SDaniel Lezcano 	of_clk->rate = clk_get_rate(of_clk->clk);
122dc11bae7SDaniel Lezcano 	if (!of_clk->rate) {
123dc11bae7SDaniel Lezcano 		ret = -EINVAL;
124469869d1SRob Herring 		pr_err("Failed to get clock rate for %pOF\n", np);
125dc11bae7SDaniel Lezcano 		goto out_clk_disable;
126dc11bae7SDaniel Lezcano 	}
127dc11bae7SDaniel Lezcano 
128dc11bae7SDaniel Lezcano 	of_clk->period = DIV_ROUND_UP(of_clk->rate, HZ);
129dc11bae7SDaniel Lezcano out:
130dc11bae7SDaniel Lezcano 	return ret;
131dc11bae7SDaniel Lezcano 
132dc11bae7SDaniel Lezcano out_clk_disable:
133dc11bae7SDaniel Lezcano 	clk_disable_unprepare(of_clk->clk);
134dc11bae7SDaniel Lezcano out_clk_put:
135dc11bae7SDaniel Lezcano 	clk_put(of_clk->clk);
136dc11bae7SDaniel Lezcano 
137dc11bae7SDaniel Lezcano 	goto out;
138dc11bae7SDaniel Lezcano }
139dc11bae7SDaniel Lezcano 
timer_of_base_exit(struct of_timer_base * of_base)1405bbf4ad9SDaniel Lezcano static __init void timer_of_base_exit(struct of_timer_base *of_base)
141dc11bae7SDaniel Lezcano {
142dc11bae7SDaniel Lezcano 	iounmap(of_base->base);
143dc11bae7SDaniel Lezcano }
144dc11bae7SDaniel Lezcano 
timer_of_base_init(struct device_node * np,struct of_timer_base * of_base)1455bbf4ad9SDaniel Lezcano static __init int timer_of_base_init(struct device_node *np,
146dc11bae7SDaniel Lezcano 				     struct of_timer_base *of_base)
147dc11bae7SDaniel Lezcano {
1489aea417aSDaniel Lezcano 	of_base->base = of_base->name ?
1499aea417aSDaniel Lezcano 		of_io_request_and_map(np, of_base->index, of_base->name) :
1509aea417aSDaniel Lezcano 		of_iomap(np, of_base->index);
1514467b8baSGuillaume Ranquet 	if (IS_ERR_OR_NULL(of_base->base)) {
1524467b8baSGuillaume Ranquet 		pr_err("Failed to iomap (%s:%s)\n", np->name, of_base->name);
1534467b8baSGuillaume Ranquet 		return of_base->base ? PTR_ERR(of_base->base) : -ENOMEM;
154dc11bae7SDaniel Lezcano 	}
155dc11bae7SDaniel Lezcano 
156dc11bae7SDaniel Lezcano 	return 0;
157dc11bae7SDaniel Lezcano }
158dc11bae7SDaniel Lezcano 
timer_of_init(struct device_node * np,struct timer_of * to)159dc11bae7SDaniel Lezcano int __init timer_of_init(struct device_node *np, struct timer_of *to)
160dc11bae7SDaniel Lezcano {
161b7dcc4eaSArnd Bergmann 	int ret = -EINVAL;
162dc11bae7SDaniel Lezcano 	int flags = 0;
163dc11bae7SDaniel Lezcano 
164dc11bae7SDaniel Lezcano 	if (to->flags & TIMER_OF_BASE) {
1655bbf4ad9SDaniel Lezcano 		ret = timer_of_base_init(np, &to->of_base);
166dc11bae7SDaniel Lezcano 		if (ret)
167dc11bae7SDaniel Lezcano 			goto out_fail;
168dc11bae7SDaniel Lezcano 		flags |= TIMER_OF_BASE;
169dc11bae7SDaniel Lezcano 	}
170dc11bae7SDaniel Lezcano 
171dc11bae7SDaniel Lezcano 	if (to->flags & TIMER_OF_CLOCK) {
1725bbf4ad9SDaniel Lezcano 		ret = timer_of_clk_init(np, &to->of_clk);
173dc11bae7SDaniel Lezcano 		if (ret)
174dc11bae7SDaniel Lezcano 			goto out_fail;
175dc11bae7SDaniel Lezcano 		flags |= TIMER_OF_CLOCK;
176dc11bae7SDaniel Lezcano 	}
177dc11bae7SDaniel Lezcano 
178dc11bae7SDaniel Lezcano 	if (to->flags & TIMER_OF_IRQ) {
1795bbf4ad9SDaniel Lezcano 		ret = timer_of_irq_init(np, &to->of_irq);
180dc11bae7SDaniel Lezcano 		if (ret)
181dc11bae7SDaniel Lezcano 			goto out_fail;
182dc11bae7SDaniel Lezcano 		flags |= TIMER_OF_IRQ;
183dc11bae7SDaniel Lezcano 	}
184dc11bae7SDaniel Lezcano 
185dc11bae7SDaniel Lezcano 	if (!to->clkevt.name)
1864411464dSGeert Uytterhoeven 		to->clkevt.name = np->full_name;
1871c63c1c0SDaniel Lezcano 
1881c63c1c0SDaniel Lezcano 	to->np = np;
1891c63c1c0SDaniel Lezcano 
190dc11bae7SDaniel Lezcano 	return ret;
191dc11bae7SDaniel Lezcano 
192dc11bae7SDaniel Lezcano out_fail:
193dc11bae7SDaniel Lezcano 	if (flags & TIMER_OF_IRQ)
1945bbf4ad9SDaniel Lezcano 		timer_of_irq_exit(&to->of_irq);
195dc11bae7SDaniel Lezcano 
196dc11bae7SDaniel Lezcano 	if (flags & TIMER_OF_CLOCK)
1975bbf4ad9SDaniel Lezcano 		timer_of_clk_exit(&to->of_clk);
198dc11bae7SDaniel Lezcano 
199dc11bae7SDaniel Lezcano 	if (flags & TIMER_OF_BASE)
2005bbf4ad9SDaniel Lezcano 		timer_of_base_exit(&to->of_base);
201b7dcc4eaSArnd Bergmann 	return ret;
202dc11bae7SDaniel Lezcano }
203f48729a9SBenjamin Gaignard 
204558de282SBenjamin Gaignard /**
2054bf07f65SIngo Molnar  * timer_of_cleanup - release timer_of resources
206558de282SBenjamin Gaignard  * @to: timer_of structure
207558de282SBenjamin Gaignard  *
2084bf07f65SIngo Molnar  * Release the resources that has been used in timer_of_init().
209558de282SBenjamin Gaignard  * This function should be called in init error cases
210558de282SBenjamin Gaignard  */
timer_of_cleanup(struct timer_of * to)211558de282SBenjamin Gaignard void __init timer_of_cleanup(struct timer_of *to)
212f48729a9SBenjamin Gaignard {
213f48729a9SBenjamin Gaignard 	if (to->flags & TIMER_OF_IRQ)
2145bbf4ad9SDaniel Lezcano 		timer_of_irq_exit(&to->of_irq);
215f48729a9SBenjamin Gaignard 
216f48729a9SBenjamin Gaignard 	if (to->flags & TIMER_OF_CLOCK)
2175bbf4ad9SDaniel Lezcano 		timer_of_clk_exit(&to->of_clk);
218f48729a9SBenjamin Gaignard 
219f48729a9SBenjamin Gaignard 	if (to->flags & TIMER_OF_BASE)
2205bbf4ad9SDaniel Lezcano 		timer_of_base_exit(&to->of_base);
221f48729a9SBenjamin Gaignard }
222