10fdebc5eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a7ed099fSArnd Bergmann /*
3a7ed099fSArnd Bergmann * arch/arm/plat-spear/time.c
4a7ed099fSArnd Bergmann *
5a7ed099fSArnd Bergmann * Copyright (C) 2010 ST Microelectronics
69cc23682SViresh Kumar * Shiraz Hashim<shiraz.linux.kernel@gmail.com>
7a7ed099fSArnd Bergmann */
8a7ed099fSArnd Bergmann
9a7ed099fSArnd Bergmann #include <linux/clk.h>
10a7ed099fSArnd Bergmann #include <linux/clockchips.h>
11a7ed099fSArnd Bergmann #include <linux/clocksource.h>
12a7ed099fSArnd Bergmann #include <linux/err.h>
13a7ed099fSArnd Bergmann #include <linux/init.h>
14a7ed099fSArnd Bergmann #include <linux/interrupt.h>
15a7ed099fSArnd Bergmann #include <linux/ioport.h>
16a7ed099fSArnd Bergmann #include <linux/io.h>
17a7ed099fSArnd Bergmann #include <linux/kernel.h>
18a7ed099fSArnd Bergmann #include <linux/of_irq.h>
19a7ed099fSArnd Bergmann #include <linux/of_address.h>
20a7ed099fSArnd Bergmann #include <linux/time.h>
21a7ed099fSArnd Bergmann #include <linux/irq.h>
22a7ed099fSArnd Bergmann #include <asm/mach/time.h>
232b9c613cSArnd Bergmann #include "generic.h"
24a7ed099fSArnd Bergmann
25a7ed099fSArnd Bergmann /*
26a7ed099fSArnd Bergmann * We would use TIMER0 and TIMER1 as clockevent and clocksource.
27a7ed099fSArnd Bergmann * Timer0 and Timer1 both belong to same gpt block in cpu subbsystem. Further
28a7ed099fSArnd Bergmann * they share same functional clock. Any change in one's functional clock will
29a7ed099fSArnd Bergmann * also affect other timer.
30a7ed099fSArnd Bergmann */
31a7ed099fSArnd Bergmann
32a7ed099fSArnd Bergmann #define CLKEVT 0 /* gpt0, channel0 as clockevent */
33a7ed099fSArnd Bergmann #define CLKSRC 1 /* gpt0, channel1 as clocksource */
34a7ed099fSArnd Bergmann
35a7ed099fSArnd Bergmann /* Register offsets, x is channel number */
36a7ed099fSArnd Bergmann #define CR(x) ((x) * 0x80 + 0x80)
37a7ed099fSArnd Bergmann #define IR(x) ((x) * 0x80 + 0x84)
38a7ed099fSArnd Bergmann #define LOAD(x) ((x) * 0x80 + 0x88)
39a7ed099fSArnd Bergmann #define COUNT(x) ((x) * 0x80 + 0x8C)
40a7ed099fSArnd Bergmann
41a7ed099fSArnd Bergmann /* Reg bit definitions */
42a7ed099fSArnd Bergmann #define CTRL_INT_ENABLE 0x0100
43a7ed099fSArnd Bergmann #define CTRL_ENABLE 0x0020
44a7ed099fSArnd Bergmann #define CTRL_ONE_SHOT 0x0010
45a7ed099fSArnd Bergmann
46a7ed099fSArnd Bergmann #define CTRL_PRESCALER1 0x0
47a7ed099fSArnd Bergmann #define CTRL_PRESCALER2 0x1
48a7ed099fSArnd Bergmann #define CTRL_PRESCALER4 0x2
49a7ed099fSArnd Bergmann #define CTRL_PRESCALER8 0x3
50a7ed099fSArnd Bergmann #define CTRL_PRESCALER16 0x4
51a7ed099fSArnd Bergmann #define CTRL_PRESCALER32 0x5
52a7ed099fSArnd Bergmann #define CTRL_PRESCALER64 0x6
53a7ed099fSArnd Bergmann #define CTRL_PRESCALER128 0x7
54a7ed099fSArnd Bergmann #define CTRL_PRESCALER256 0x8
55a7ed099fSArnd Bergmann
56a7ed099fSArnd Bergmann #define INT_STATUS 0x1
57a7ed099fSArnd Bergmann
58a7ed099fSArnd Bergmann /*
59a7ed099fSArnd Bergmann * Minimum clocksource/clockevent timer range in seconds
60a7ed099fSArnd Bergmann */
61a7ed099fSArnd Bergmann #define SPEAR_MIN_RANGE 4
62a7ed099fSArnd Bergmann
63a7ed099fSArnd Bergmann static __iomem void *gpt_base;
64a7ed099fSArnd Bergmann static struct clk *gpt_clk;
65a7ed099fSArnd Bergmann
66a7ed099fSArnd Bergmann static int clockevent_next_event(unsigned long evt,
67a7ed099fSArnd Bergmann struct clock_event_device *clk_event_dev);
68a7ed099fSArnd Bergmann
spear_clocksource_init(void)691be5f692SAlex Elder static void __init spear_clocksource_init(void)
70a7ed099fSArnd Bergmann {
71a7ed099fSArnd Bergmann u32 tick_rate;
72a7ed099fSArnd Bergmann u16 val;
73a7ed099fSArnd Bergmann
74a7ed099fSArnd Bergmann /* program the prescaler (/256)*/
75a7ed099fSArnd Bergmann writew(CTRL_PRESCALER256, gpt_base + CR(CLKSRC));
76a7ed099fSArnd Bergmann
77a7ed099fSArnd Bergmann /* find out actual clock driving Timer */
78a7ed099fSArnd Bergmann tick_rate = clk_get_rate(gpt_clk);
79a7ed099fSArnd Bergmann tick_rate >>= CTRL_PRESCALER256;
80a7ed099fSArnd Bergmann
81a7ed099fSArnd Bergmann writew(0xFFFF, gpt_base + LOAD(CLKSRC));
82a7ed099fSArnd Bergmann
83a7ed099fSArnd Bergmann val = readw(gpt_base + CR(CLKSRC));
84a7ed099fSArnd Bergmann val &= ~CTRL_ONE_SHOT; /* autoreload mode */
85a7ed099fSArnd Bergmann val |= CTRL_ENABLE ;
86a7ed099fSArnd Bergmann writew(val, gpt_base + CR(CLKSRC));
87a7ed099fSArnd Bergmann
88a7ed099fSArnd Bergmann /* register the clocksource */
89a7ed099fSArnd Bergmann clocksource_mmio_init(gpt_base + COUNT(CLKSRC), "tmr1", tick_rate,
90a7ed099fSArnd Bergmann 200, 16, clocksource_mmio_readw_up);
91a7ed099fSArnd Bergmann }
92a7ed099fSArnd Bergmann
spear_timer_shutdown(struct clock_event_device * evt)93*80b55772SSteven Rostedt (Google) static inline void spear_timer_shutdown(struct clock_event_device *evt)
947639c0b8SViresh Kumar {
957639c0b8SViresh Kumar u16 val = readw(gpt_base + CR(CLKEVT));
96a7ed099fSArnd Bergmann
977639c0b8SViresh Kumar /* stop the timer */
987639c0b8SViresh Kumar val &= ~CTRL_ENABLE;
997639c0b8SViresh Kumar writew(val, gpt_base + CR(CLKEVT));
1007639c0b8SViresh Kumar }
1017639c0b8SViresh Kumar
spear_shutdown(struct clock_event_device * evt)1027639c0b8SViresh Kumar static int spear_shutdown(struct clock_event_device *evt)
1037639c0b8SViresh Kumar {
104*80b55772SSteven Rostedt (Google) spear_timer_shutdown(evt);
1057639c0b8SViresh Kumar
1067639c0b8SViresh Kumar return 0;
1077639c0b8SViresh Kumar }
1087639c0b8SViresh Kumar
spear_set_oneshot(struct clock_event_device * evt)1097639c0b8SViresh Kumar static int spear_set_oneshot(struct clock_event_device *evt)
1107639c0b8SViresh Kumar {
1117639c0b8SViresh Kumar u16 val;
1127639c0b8SViresh Kumar
1137639c0b8SViresh Kumar /* stop the timer */
114*80b55772SSteven Rostedt (Google) spear_timer_shutdown(evt);
1157639c0b8SViresh Kumar
1167639c0b8SViresh Kumar val = readw(gpt_base + CR(CLKEVT));
1177639c0b8SViresh Kumar val |= CTRL_ONE_SHOT;
1187639c0b8SViresh Kumar writew(val, gpt_base + CR(CLKEVT));
1197639c0b8SViresh Kumar
1207639c0b8SViresh Kumar return 0;
1217639c0b8SViresh Kumar }
1227639c0b8SViresh Kumar
spear_set_periodic(struct clock_event_device * evt)1237639c0b8SViresh Kumar static int spear_set_periodic(struct clock_event_device *evt)
124a7ed099fSArnd Bergmann {
125a7ed099fSArnd Bergmann u32 period;
126a7ed099fSArnd Bergmann u16 val;
127a7ed099fSArnd Bergmann
128a7ed099fSArnd Bergmann /* stop the timer */
129*80b55772SSteven Rostedt (Google) spear_timer_shutdown(evt);
130a7ed099fSArnd Bergmann
131a7ed099fSArnd Bergmann period = clk_get_rate(gpt_clk) / HZ;
132a7ed099fSArnd Bergmann period >>= CTRL_PRESCALER16;
133a7ed099fSArnd Bergmann writew(period, gpt_base + LOAD(CLKEVT));
134a7ed099fSArnd Bergmann
135a7ed099fSArnd Bergmann val = readw(gpt_base + CR(CLKEVT));
136a7ed099fSArnd Bergmann val &= ~CTRL_ONE_SHOT;
137a7ed099fSArnd Bergmann val |= CTRL_ENABLE | CTRL_INT_ENABLE;
138a7ed099fSArnd Bergmann writew(val, gpt_base + CR(CLKEVT));
139a7ed099fSArnd Bergmann
1407639c0b8SViresh Kumar return 0;
141a7ed099fSArnd Bergmann }
1427639c0b8SViresh Kumar
1437639c0b8SViresh Kumar static struct clock_event_device clkevt = {
1447639c0b8SViresh Kumar .name = "tmr0",
1457639c0b8SViresh Kumar .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
1467639c0b8SViresh Kumar .set_state_shutdown = spear_shutdown,
1477639c0b8SViresh Kumar .set_state_periodic = spear_set_periodic,
1487639c0b8SViresh Kumar .set_state_oneshot = spear_set_oneshot,
1497639c0b8SViresh Kumar .tick_resume = spear_shutdown,
1507639c0b8SViresh Kumar .set_next_event = clockevent_next_event,
1517639c0b8SViresh Kumar .shift = 0, /* to be computed */
1527639c0b8SViresh Kumar };
153a7ed099fSArnd Bergmann
clockevent_next_event(unsigned long cycles,struct clock_event_device * clk_event_dev)154a7ed099fSArnd Bergmann static int clockevent_next_event(unsigned long cycles,
155a7ed099fSArnd Bergmann struct clock_event_device *clk_event_dev)
156a7ed099fSArnd Bergmann {
157a7ed099fSArnd Bergmann u16 val = readw(gpt_base + CR(CLKEVT));
158a7ed099fSArnd Bergmann
159a7ed099fSArnd Bergmann if (val & CTRL_ENABLE)
160a7ed099fSArnd Bergmann writew(val & ~CTRL_ENABLE, gpt_base + CR(CLKEVT));
161a7ed099fSArnd Bergmann
162a7ed099fSArnd Bergmann writew(cycles, gpt_base + LOAD(CLKEVT));
163a7ed099fSArnd Bergmann
164a7ed099fSArnd Bergmann val |= CTRL_ENABLE | CTRL_INT_ENABLE;
165a7ed099fSArnd Bergmann writew(val, gpt_base + CR(CLKEVT));
166a7ed099fSArnd Bergmann
167a7ed099fSArnd Bergmann return 0;
168a7ed099fSArnd Bergmann }
169a7ed099fSArnd Bergmann
spear_timer_interrupt(int irq,void * dev_id)170a7ed099fSArnd Bergmann static irqreturn_t spear_timer_interrupt(int irq, void *dev_id)
171a7ed099fSArnd Bergmann {
172a7ed099fSArnd Bergmann struct clock_event_device *evt = &clkevt;
173a7ed099fSArnd Bergmann
174a7ed099fSArnd Bergmann writew(INT_STATUS, gpt_base + IR(CLKEVT));
175a7ed099fSArnd Bergmann
176a7ed099fSArnd Bergmann evt->event_handler(evt);
177a7ed099fSArnd Bergmann
178a7ed099fSArnd Bergmann return IRQ_HANDLED;
179a7ed099fSArnd Bergmann }
180a7ed099fSArnd Bergmann
spear_clockevent_init(int irq)181a7ed099fSArnd Bergmann static void __init spear_clockevent_init(int irq)
182a7ed099fSArnd Bergmann {
183a7ed099fSArnd Bergmann u32 tick_rate;
184a7ed099fSArnd Bergmann
185a7ed099fSArnd Bergmann /* program the prescaler */
186a7ed099fSArnd Bergmann writew(CTRL_PRESCALER16, gpt_base + CR(CLKEVT));
187a7ed099fSArnd Bergmann
188a7ed099fSArnd Bergmann tick_rate = clk_get_rate(gpt_clk);
189a7ed099fSArnd Bergmann tick_rate >>= CTRL_PRESCALER16;
190a7ed099fSArnd Bergmann
191a7ed099fSArnd Bergmann clkevt.cpumask = cpumask_of(0);
192a7ed099fSArnd Bergmann
193a7ed099fSArnd Bergmann clockevents_config_and_register(&clkevt, tick_rate, 3, 0xfff0);
194a7ed099fSArnd Bergmann
195c84e4899Safzal mohammed if (request_irq(irq, spear_timer_interrupt, IRQF_TIMER, "timer", NULL))
196c84e4899Safzal mohammed pr_err("Failed to request irq %d (timer)\n", irq);
197a7ed099fSArnd Bergmann }
198a7ed099fSArnd Bergmann
1990527873bSArnd Bergmann static const struct of_device_id timer_of_match[] __initconst = {
200a7ed099fSArnd Bergmann { .compatible = "st,spear-timer", },
201a7ed099fSArnd Bergmann { },
202a7ed099fSArnd Bergmann };
203a7ed099fSArnd Bergmann
spear_setup_of_timer(void)204a7ed099fSArnd Bergmann void __init spear_setup_of_timer(void)
205a7ed099fSArnd Bergmann {
206a7ed099fSArnd Bergmann struct device_node *np;
207a7ed099fSArnd Bergmann int irq, ret;
208a7ed099fSArnd Bergmann
209a7ed099fSArnd Bergmann np = of_find_matching_node(NULL, timer_of_match);
210a7ed099fSArnd Bergmann if (!np) {
211a7ed099fSArnd Bergmann pr_err("%s: No timer passed via DT\n", __func__);
212a7ed099fSArnd Bergmann return;
213a7ed099fSArnd Bergmann }
214a7ed099fSArnd Bergmann
215a7ed099fSArnd Bergmann irq = irq_of_parse_and_map(np, 0);
216a7ed099fSArnd Bergmann if (!irq) {
217a7ed099fSArnd Bergmann pr_err("%s: No irq passed for timer via DT\n", __func__);
2182c629dd2SLiang He goto err_put_np;
219a7ed099fSArnd Bergmann }
220a7ed099fSArnd Bergmann
221a7ed099fSArnd Bergmann gpt_base = of_iomap(np, 0);
222a7ed099fSArnd Bergmann if (!gpt_base) {
223a7ed099fSArnd Bergmann pr_err("%s: of iomap failed\n", __func__);
2242c629dd2SLiang He goto err_put_np;
225a7ed099fSArnd Bergmann }
226a7ed099fSArnd Bergmann
227a7ed099fSArnd Bergmann gpt_clk = clk_get_sys("gpt0", NULL);
228ce340961SChristophe JAILLET if (IS_ERR(gpt_clk)) {
229a7ed099fSArnd Bergmann pr_err("%s:couldn't get clk for gpt\n", __func__);
230a7ed099fSArnd Bergmann goto err_iomap;
231a7ed099fSArnd Bergmann }
232a7ed099fSArnd Bergmann
233a7ed099fSArnd Bergmann ret = clk_prepare_enable(gpt_clk);
234a7ed099fSArnd Bergmann if (ret < 0) {
235a7ed099fSArnd Bergmann pr_err("%s:couldn't prepare-enable gpt clock\n", __func__);
236a7ed099fSArnd Bergmann goto err_prepare_enable_clk;
237a7ed099fSArnd Bergmann }
238a7ed099fSArnd Bergmann
2392c629dd2SLiang He of_node_put(np);
2402c629dd2SLiang He
241a7ed099fSArnd Bergmann spear_clockevent_init(irq);
242a7ed099fSArnd Bergmann spear_clocksource_init();
243a7ed099fSArnd Bergmann
244a7ed099fSArnd Bergmann return;
245a7ed099fSArnd Bergmann
246a7ed099fSArnd Bergmann err_prepare_enable_clk:
247a7ed099fSArnd Bergmann clk_put(gpt_clk);
248a7ed099fSArnd Bergmann err_iomap:
249a7ed099fSArnd Bergmann iounmap(gpt_base);
2502c629dd2SLiang He err_put_np:
2512c629dd2SLiang He of_node_put(np);
252a7ed099fSArnd Bergmann }
253