ff9d44aa6ee27ede0e354cfcefe4998742c57ba0
[openwrt.git] / target / linux / mcs814x / files-3.14 / arch / arm / mach-mcs814x / timer.c
1 /*
2  * Moschip MCS814x timer routines
3  *
4  * Copyright (C) 2012, Florian Fainelli <florian@openwrt.org>
5  *
6  * Licensed under GPLv2
7  */
8 #include <linux/kernel.h>
9 #include <linux/interrupt.h>
10 #include <linux/time.h>
11 #include <linux/timex.h>
12 #include <linux/irq.h>
13 #include <linux/err.h>
14 #include <linux/clk.h>
15 #include <linux/io.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18
19 #include <asm/mach/time.h>
20 #include <mach/mcs814x.h>
21
22 /* Timer block registers */
23 #define TIMER_VAL       0x00
24 #define TIMER_CTL       0x04
25 #define  TIMER_CTL_EN   0x01
26 #define  TIMER_CTL_DBG  0x02
27
28 static u32 last_reload;
29 static u32 timer_correct;
30 static u32 clock_rate;
31 static u32 timer_reload_value;
32 static void __iomem *mcs814x_timer_base;
33
34 static inline u32 ticks2usecs(u32 x)
35 {
36         return x / (clock_rate / 1000000);
37 }
38
39 /*
40  * Returns number of ms since last clock interrupt.  Note that interrupts
41  * will have been disabled by do_gettimeoffset()
42  */
43 static u32 mcs814x_gettimeoffset(void)
44 {
45         u32 ticks = readl_relaxed(mcs814x_timer_base + TIMER_VAL);
46
47         if (ticks < last_reload)
48                 return ticks2usecs(ticks + (u32)(0xffffffff - last_reload));
49         else
50                 return ticks2usecs(ticks - last_reload);
51 }
52
53
54 static irqreturn_t mcs814x_timer_interrupt(int irq, void *dev_id)
55 {
56         u32 count = readl_relaxed(mcs814x_timer_base + TIMER_VAL);
57
58         /* take into account delay up to this moment */
59         last_reload = count + timer_correct + timer_reload_value;
60
61         if (last_reload < timer_reload_value) {
62                 last_reload = timer_reload_value;
63         } else {
64                 if (timer_correct == 0)
65                         timer_correct = readl_relaxed(mcs814x_timer_base + TIMER_VAL) - count;
66         }
67         writel_relaxed(last_reload, mcs814x_timer_base + TIMER_VAL);
68
69         timer_tick();
70
71         return IRQ_HANDLED;
72 }
73
74 static struct irqaction mcs814x_timer_irq = {
75         .name           = "mcs814x-timer",
76         .flags          = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
77         .handler        = mcs814x_timer_interrupt,
78 };
79
80 static struct of_device_id mcs814x_timer_ids[] = {
81         { .compatible = "moschip,mcs814x-timer" },
82         { /* sentinel */ },
83 };
84
85 static void __init mcs814x_of_timer_init(void)
86 {
87         struct device_node *np;
88         const unsigned int *intspec;
89
90         np = of_find_matching_node(NULL, mcs814x_timer_ids);
91         if (!np)
92                 panic("unable to find compatible timer node in dtb");
93
94         mcs814x_timer_base = of_iomap(np, 0);
95         if (!mcs814x_timer_base)
96                 panic("unable to remap timer cpu registers");
97
98         intspec = of_get_property(np, "interrupts", NULL);
99         if (!intspec)
100                 panic("no interrupts property for timer");
101
102         mcs814x_timer_irq.irq = be32_to_cpup(intspec);
103 }
104
105 void __init mcs814x_timer_init(void)
106 {
107         struct clk *clk;
108
109         arch_gettimeoffset = mcs814x_gettimeoffset;
110
111         clk = clk_get_sys("timer0", NULL);
112         if (IS_ERR_OR_NULL(clk))
113                 panic("unable to get timer0 clock");
114
115         clock_rate = clk_get_rate(clk);
116
117         mcs814x_of_timer_init();
118
119         pr_info("Timer frequency: %d (kHz)\n", clock_rate / 1000);
120
121         timer_reload_value = 0xffffffff - (clock_rate / HZ);
122
123         /* disable timer */
124         writel_relaxed(~TIMER_CTL_EN, mcs814x_timer_base + TIMER_CTL);
125         writel_relaxed(timer_reload_value, mcs814x_timer_base + TIMER_VAL);
126         last_reload = timer_reload_value;
127
128         setup_irq(mcs814x_timer_irq.irq, &mcs814x_timer_irq);
129         /* enable timer, stop timer in debug mode */
130         writel_relaxed(TIMER_CTL_EN | TIMER_CTL_DBG,
131                 mcs814x_timer_base + TIMER_CTL);
132 }