[mcs814x] add Moschip MSC814x support
[openwrt/svn-archive/archive.git] / target / linux / mcs814x / files-3.3 / 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/hardware.h>
21
22 /* Timer block registers */
23 #define TIMER_VAL 0x00
24 #define TIMER_CTL 0x04
25
26 static u32 last_reload;
27 static u32 timer_correct;
28 static u32 clock_rate;
29 static u32 timer_reload_value;
30 static void __iomem *mcs814x_timer_base;
31
32 static inline unsigned long ticks2usecs(u32 x)
33 {
34 return x / (clock_rate / 1000000);
35 }
36
37 /*
38 * Returns number of ms since last clock interrupt. Note that interrupts
39 * will have been disabled by do_gettimeoffset()
40 */
41 static unsigned long mcs814x_gettimeoffset(void)
42 {
43 u32 ticks = __raw_readl(mcs814x_timer_base + TIMER_VAL);
44
45 if (ticks < last_reload)
46 return ticks2usecs(ticks + (u32)(0xffffffff - last_reload));
47 else
48 return ticks2usecs(ticks - last_reload);
49 }
50
51
52 static irqreturn_t mcs814x_timer_interrupt(int irq, void *dev_id)
53 {
54 u32 count = __raw_readl(mcs814x_timer_base + TIMER_VAL);
55
56 /* take into account delay up to this moment */
57 last_reload = count + timer_correct + timer_reload_value;
58
59 if (last_reload < timer_reload_value) {
60 last_reload = timer_reload_value;
61 } else {
62 if (timer_correct == 0)
63 timer_correct = __raw_readl(mcs814x_timer_base + TIMER_VAL) - count;
64 }
65 __raw_writel(last_reload, mcs814x_timer_base + TIMER_VAL);
66
67 timer_tick();
68
69 return IRQ_HANDLED;
70 }
71
72 static struct irqaction mcs814x_timer_irq = {
73 .name = "mcs814x-timer",
74 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
75 .handler = mcs814x_timer_interrupt,
76 };
77
78 static struct of_device_id mcs814x_timer_ids[] = {
79 { .compatible = "moschip,mcs814x-timer" },
80 { /* sentinel */ },
81 };
82
83 static void __init mcs814x_of_timer_init(void)
84 {
85 struct device_node *np;
86 const unsigned int *intspec;
87
88 np = of_find_matching_node(NULL, mcs814x_timer_ids);
89 if (!np)
90 panic("unable to find compatible timer node in dtb");
91
92 mcs814x_timer_base = of_iomap(np, 0);
93 if (!mcs814x_timer_base)
94 panic("unable to remap timer cpu registers");
95
96 intspec = of_get_property(np, "interrupts", NULL);
97 if (!intspec)
98 panic("no interrupts property for timer");
99
100 mcs814x_timer_irq.irq = be32_to_cpup(intspec);
101 }
102
103 static void __init mcs814x_timer_init(void)
104 {
105 struct clk *clk;
106
107 clk = clk_get_sys("timer0", NULL);
108 if (IS_ERR_OR_NULL(clk))
109 panic("unable to get timer0 clock");
110
111 clock_rate = clk_get_rate(clk);
112 clk_put(clk);
113
114 mcs814x_of_timer_init();
115
116 pr_info("Timer frequency: %d (kHz)\n", clock_rate / 1000);
117
118 timer_reload_value = 0xffffffff - (clock_rate / HZ);
119
120 /* disable timer */
121 __raw_writel(0, mcs814x_timer_base + TIMER_CTL);
122 __raw_writel(timer_reload_value, mcs814x_timer_base + TIMER_VAL);
123 last_reload = timer_reload_value;
124
125 setup_irq(mcs814x_timer_irq.irq, &mcs814x_timer_irq);
126 /* enable timer, stop timer in debug mode */
127 __raw_writel(0x03, mcs814x_timer_base + TIMER_CTL);
128 }
129
130 struct sys_timer mcs814x_timer = {
131 .init = mcs814x_timer_init,
132 .offset = mcs814x_gettimeoffset,
133 };