aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/clocksource
diff options
context:
space:
mode:
authorMaxime Ripard <maxime.ripard@free-electrons.com>2015-03-31 06:12:25 -0400
committerIngo Molnar <mingo@kernel.org>2015-03-31 11:53:58 -0400
commit4a59058f0b09682200c04b1db236b4a3b92128d7 (patch)
tree8bdfcb34da43fda402fb00f050c5aa49a8fa9edc /drivers/clocksource
parenta45860d0ba433c217d359fa2cc2a4984d18ce263 (diff)
clocksource/drivers/sun5i: Refactor the current code
Refactor the code in order to remove the global variables and split the clock source and clock events registration in order to ease the addition of the clock notifiers needed to handle the parent clock rate changes. Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com> Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org> Cc: linux-arm-kernel@lists.infradead.org Link: http://lkml.kernel.org/r/1427796746-373-4-git-send-email-daniel.lezcano@linaro.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'drivers/clocksource')
-rw-r--r--drivers/clocksource/timer-sun5i.c231
1 files changed, 166 insertions, 65 deletions
diff --git a/drivers/clocksource/timer-sun5i.c b/drivers/clocksource/timer-sun5i.c
index 87f7b810d8a8..23300901405b 100644
--- a/drivers/clocksource/timer-sun5i.c
+++ b/drivers/clocksource/timer-sun5i.c
@@ -17,6 +17,7 @@
17#include <linux/irq.h> 17#include <linux/irq.h>
18#include <linux/irqreturn.h> 18#include <linux/irqreturn.h>
19#include <linux/reset.h> 19#include <linux/reset.h>
20#include <linux/slab.h>
20#include <linux/of.h> 21#include <linux/of.h>
21#include <linux/of_address.h> 22#include <linux/of_address.h>
22#include <linux/of_irq.h> 23#include <linux/of_irq.h>
@@ -36,8 +37,27 @@
36 37
37#define TIMER_SYNC_TICKS 3 38#define TIMER_SYNC_TICKS 3
38 39
39static void __iomem *timer_base; 40struct sun5i_timer {
40static u32 ticks_per_jiffy; 41 void __iomem *base;
42 struct clk *clk;
43 u32 ticks_per_jiffy;
44};
45
46struct sun5i_timer_clksrc {
47 struct sun5i_timer timer;
48 struct clocksource clksrc;
49};
50
51#define to_sun5i_timer_clksrc(x) \
52 container_of(x, struct sun5i_timer_clksrc, clksrc)
53
54struct sun5i_timer_clkevt {
55 struct sun5i_timer timer;
56 struct clock_event_device clkevt;
57};
58
59#define to_sun5i_timer_clkevt(x) \
60 container_of(x, struct sun5i_timer_clkevt, clkevt)
41 61
42/* 62/*
43 * When we disable a timer, we need to wait at least for 2 cycles of 63 * When we disable a timer, we need to wait at least for 2 cycles of
@@ -45,30 +65,30 @@ static u32 ticks_per_jiffy;
45 * that is already setup and runs at the same frequency than the other 65 * that is already setup and runs at the same frequency than the other
46 * timers, and we never will be disabled. 66 * timers, and we never will be disabled.
47 */ 67 */
48static void sun5i_clkevt_sync(void) 68static void sun5i_clkevt_sync(struct sun5i_timer_clkevt *ce)
49{ 69{
50 u32 old = readl(timer_base + TIMER_CNTVAL_LO_REG(1)); 70 u32 old = readl(ce->timer.base + TIMER_CNTVAL_LO_REG(1));
51 71
52 while ((old - readl(timer_base + TIMER_CNTVAL_LO_REG(1))) < TIMER_SYNC_TICKS) 72 while ((old - readl(ce->timer.base + TIMER_CNTVAL_LO_REG(1))) < TIMER_SYNC_TICKS)
53 cpu_relax(); 73 cpu_relax();
54} 74}
55 75
56static void sun5i_clkevt_time_stop(u8 timer) 76static void sun5i_clkevt_time_stop(struct sun5i_timer_clkevt *ce, u8 timer)
57{ 77{
58 u32 val = readl(timer_base + TIMER_CTL_REG(timer)); 78 u32 val = readl(ce->timer.base + TIMER_CTL_REG(timer));
59 writel(val & ~TIMER_CTL_ENABLE, timer_base + TIMER_CTL_REG(timer)); 79 writel(val & ~TIMER_CTL_ENABLE, ce->timer.base + TIMER_CTL_REG(timer));
60 80
61 sun5i_clkevt_sync(); 81 sun5i_clkevt_sync(ce);
62} 82}
63 83
64static void sun5i_clkevt_time_setup(u8 timer, u32 delay) 84static void sun5i_clkevt_time_setup(struct sun5i_timer_clkevt *ce, u8 timer, u32 delay)
65{ 85{
66 writel(delay, timer_base + TIMER_INTVAL_LO_REG(timer)); 86 writel(delay, ce->timer.base + TIMER_INTVAL_LO_REG(timer));
67} 87}
68 88
69static void sun5i_clkevt_time_start(u8 timer, bool periodic) 89static void sun5i_clkevt_time_start(struct sun5i_timer_clkevt *ce, u8 timer, bool periodic)
70{ 90{
71 u32 val = readl(timer_base + TIMER_CTL_REG(timer)); 91 u32 val = readl(ce->timer.base + TIMER_CTL_REG(timer));
72 92
73 if (periodic) 93 if (periodic)
74 val &= ~TIMER_CTL_ONESHOT; 94 val &= ~TIMER_CTL_ONESHOT;
@@ -76,66 +96,170 @@ static void sun5i_clkevt_time_start(u8 timer, bool periodic)
76 val |= TIMER_CTL_ONESHOT; 96 val |= TIMER_CTL_ONESHOT;
77 97
78 writel(val | TIMER_CTL_ENABLE | TIMER_CTL_RELOAD, 98 writel(val | TIMER_CTL_ENABLE | TIMER_CTL_RELOAD,
79 timer_base + TIMER_CTL_REG(timer)); 99 ce->timer.base + TIMER_CTL_REG(timer));
80} 100}
81 101
82static void sun5i_clkevt_mode(enum clock_event_mode mode, 102static void sun5i_clkevt_mode(enum clock_event_mode mode,
83 struct clock_event_device *clk) 103 struct clock_event_device *clkevt)
84{ 104{
105 struct sun5i_timer_clkevt *ce = to_sun5i_timer_clkevt(clkevt);
106
85 switch (mode) { 107 switch (mode) {
86 case CLOCK_EVT_MODE_PERIODIC: 108 case CLOCK_EVT_MODE_PERIODIC:
87 sun5i_clkevt_time_stop(0); 109 sun5i_clkevt_time_stop(ce, 0);
88 sun5i_clkevt_time_setup(0, ticks_per_jiffy); 110 sun5i_clkevt_time_setup(ce, 0, ce->timer.ticks_per_jiffy);
89 sun5i_clkevt_time_start(0, true); 111 sun5i_clkevt_time_start(ce, 0, true);
90 break; 112 break;
91 case CLOCK_EVT_MODE_ONESHOT: 113 case CLOCK_EVT_MODE_ONESHOT:
92 sun5i_clkevt_time_stop(0); 114 sun5i_clkevt_time_stop(ce, 0);
93 sun5i_clkevt_time_start(0, false); 115 sun5i_clkevt_time_start(ce, 0, false);
94 break; 116 break;
95 case CLOCK_EVT_MODE_UNUSED: 117 case CLOCK_EVT_MODE_UNUSED:
96 case CLOCK_EVT_MODE_SHUTDOWN: 118 case CLOCK_EVT_MODE_SHUTDOWN:
97 default: 119 default:
98 sun5i_clkevt_time_stop(0); 120 sun5i_clkevt_time_stop(ce, 0);
99 break; 121 break;
100 } 122 }
101} 123}
102 124
103static int sun5i_clkevt_next_event(unsigned long evt, 125static int sun5i_clkevt_next_event(unsigned long evt,
104 struct clock_event_device *unused) 126 struct clock_event_device *clkevt)
105{ 127{
106 sun5i_clkevt_time_stop(0); 128 struct sun5i_timer_clkevt *ce = to_sun5i_timer_clkevt(clkevt);
107 sun5i_clkevt_time_setup(0, evt - TIMER_SYNC_TICKS); 129
108 sun5i_clkevt_time_start(0, false); 130 sun5i_clkevt_time_stop(ce, 0);
131 sun5i_clkevt_time_setup(ce, 0, evt - TIMER_SYNC_TICKS);
132 sun5i_clkevt_time_start(ce, 0, false);
109 133
110 return 0; 134 return 0;
111} 135}
112 136
113static struct clock_event_device sun5i_clockevent = {
114 .name = "sun5i_tick",
115 .rating = 340,
116 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
117 .set_mode = sun5i_clkevt_mode,
118 .set_next_event = sun5i_clkevt_next_event,
119};
120
121
122static irqreturn_t sun5i_timer_interrupt(int irq, void *dev_id) 137static irqreturn_t sun5i_timer_interrupt(int irq, void *dev_id)
123{ 138{
124 struct clock_event_device *evt = (struct clock_event_device *)dev_id; 139 struct sun5i_timer_clkevt *ce = (struct sun5i_timer_clkevt *)dev_id;
125 140
126 writel(0x1, timer_base + TIMER_IRQ_ST_REG); 141 writel(0x1, ce->timer.base + TIMER_IRQ_ST_REG);
127 evt->event_handler(evt); 142 ce->clkevt.event_handler(&ce->clkevt);
128 143
129 return IRQ_HANDLED; 144 return IRQ_HANDLED;
130} 145}
131 146
147static cycle_t sun5i_clksrc_read(struct clocksource *clksrc)
148{
149 struct sun5i_timer_clksrc *cs = to_sun5i_timer_clksrc(clksrc);
150
151 return ~readl(cs->timer.base + TIMER_CNTVAL_LO_REG(1));
152}
153
154static int __init sun5i_setup_clocksource(struct device_node *node,
155 void __iomem *base,
156 struct clk *clk, int irq)
157{
158 struct sun5i_timer_clksrc *cs;
159 unsigned long rate;
160 int ret;
161
162 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
163 if (!cs)
164 return -ENOMEM;
165
166 ret = clk_prepare_enable(clk);
167 if (ret) {
168 pr_err("Couldn't enable parent clock\n");
169 goto err_free;
170 }
171
172 rate = clk_get_rate(clk);
173
174 cs->timer.base = base;
175 cs->timer.clk = clk;
176
177 writel(~0, base + TIMER_INTVAL_LO_REG(1));
178 writel(TIMER_CTL_ENABLE | TIMER_CTL_RELOAD,
179 base + TIMER_CTL_REG(1));
180
181 cs->clksrc.name = node->name;
182 cs->clksrc.rating = 340;
183 cs->clksrc.read = sun5i_clksrc_read;
184 cs->clksrc.mask = CLOCKSOURCE_MASK(32);
185 cs->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
186
187 ret = clocksource_register_hz(&cs->clksrc, rate);
188 if (ret) {
189 pr_err("Couldn't register clock source.\n");
190 goto err_disable_clk;
191 }
192
193 return 0;
194
195err_disable_clk:
196 clk_disable_unprepare(clk);
197err_free:
198 kfree(cs);
199 return ret;
200}
201
202static int __init sun5i_setup_clockevent(struct device_node *node, void __iomem *base,
203 struct clk *clk, int irq)
204{
205 struct sun5i_timer_clkevt *ce;
206 unsigned long rate;
207 int ret;
208 u32 val;
209
210 ce = kzalloc(sizeof(*ce), GFP_KERNEL);
211 if (!ce)
212 return -ENOMEM;
213
214 ret = clk_prepare_enable(clk);
215 if (ret) {
216 pr_err("Couldn't enable parent clock\n");
217 goto err_free;
218 }
219
220 rate = clk_get_rate(clk);
221
222 ce->timer.base = base;
223 ce->timer.ticks_per_jiffy = DIV_ROUND_UP(rate, HZ);
224 ce->timer.clk = clk;
225
226 ce->clkevt.name = node->name;
227 ce->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
228 ce->clkevt.set_next_event = sun5i_clkevt_next_event;
229 ce->clkevt.set_mode = sun5i_clkevt_mode;
230 ce->clkevt.rating = 340;
231 ce->clkevt.irq = irq;
232 ce->clkevt.cpumask = cpu_possible_mask;
233
234 /* Enable timer0 interrupt */
235 val = readl(base + TIMER_IRQ_EN_REG);
236 writel(val | TIMER_IRQ_EN(0), base + TIMER_IRQ_EN_REG);
237
238 clockevents_config_and_register(&ce->clkevt, rate,
239 TIMER_SYNC_TICKS, 0xffffffff);
240
241 ret = request_irq(irq, sun5i_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
242 "sun5i_timer0", ce);
243 if (ret) {
244 pr_err("Unable to register interrupt\n");
245 goto err_disable_clk;
246 }
247
248 return 0;
249
250err_disable_clk:
251 clk_disable_unprepare(clk);
252err_free:
253 kfree(ce);
254 return ret;
255}
256
132static void __init sun5i_timer_init(struct device_node *node) 257static void __init sun5i_timer_init(struct device_node *node)
133{ 258{
134 struct reset_control *rstc; 259 struct reset_control *rstc;
135 unsigned long rate; 260 void __iomem *timer_base;
136 struct clk *clk; 261 struct clk *clk;
137 int ret, irq; 262 int irq;
138 u32 val;
139 263
140 timer_base = of_io_request_and_map(node, 0, of_node_full_name(node)); 264 timer_base = of_io_request_and_map(node, 0, of_node_full_name(node));
141 if (!timer_base) 265 if (!timer_base)
@@ -148,36 +272,13 @@ static void __init sun5i_timer_init(struct device_node *node)
148 clk = of_clk_get(node, 0); 272 clk = of_clk_get(node, 0);
149 if (IS_ERR(clk)) 273 if (IS_ERR(clk))
150 panic("Can't get timer clock"); 274 panic("Can't get timer clock");
151 clk_prepare_enable(clk);
152 rate = clk_get_rate(clk);
153 275
154 rstc = of_reset_control_get(node, NULL); 276 rstc = of_reset_control_get(node, NULL);
155 if (!IS_ERR(rstc)) 277 if (!IS_ERR(rstc))
156 reset_control_deassert(rstc); 278 reset_control_deassert(rstc);
157 279
158 writel(~0, timer_base + TIMER_INTVAL_LO_REG(1)); 280 sun5i_setup_clocksource(node, timer_base, clk, irq);
159 writel(TIMER_CTL_ENABLE | TIMER_CTL_RELOAD, 281 sun5i_setup_clockevent(node, timer_base, clk, irq);
160 timer_base + TIMER_CTL_REG(1));
161
162 clocksource_mmio_init(timer_base + TIMER_CNTVAL_LO_REG(1), node->name,
163 rate, 340, 32, clocksource_mmio_readl_down);
164
165 ticks_per_jiffy = DIV_ROUND_UP(rate, HZ);
166
167 /* Enable timer0 interrupt */
168 val = readl(timer_base + TIMER_IRQ_EN_REG);
169 writel(val | TIMER_IRQ_EN(0), timer_base + TIMER_IRQ_EN_REG);
170
171 sun5i_clockevent.cpumask = cpu_possible_mask;
172 sun5i_clockevent.irq = irq;
173
174 clockevents_config_and_register(&sun5i_clockevent, rate,
175 TIMER_SYNC_TICKS, 0xffffffff);
176
177 ret = request_irq(irq, sun5i_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
178 "sun5i_timer0", &sun5i_clockevent);
179 if (ret)
180 pr_warn("failed to setup irq %d\n", irq);
181} 282}
182CLOCKSOURCE_OF_DECLARE(sun5i_a13, "allwinner,sun5i-a13-hstimer", 283CLOCKSOURCE_OF_DECLARE(sun5i_a13, "allwinner,sun5i-a13-hstimer",
183 sun5i_timer_init); 284 sun5i_timer_init);