ralink: update patches
[openwrt/openwrt.git] / target / linux / ramips / patches-3.9 / 0142-clocksource-MIPS-ralink-add-support-for-systick-time.patch
1 From bc350a91da176ae8573c0755c66f5ee1911495bc Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Tue, 30 Apr 2013 12:35:50 +0200
4 Subject: [PATCH 142/164] clocksource: MIPS: ralink: add support for systick
5 timer found on newer ralink SoC
6
7 Signed-off-by: John Crispin <blogic@openwrt.org>
8 ---
9 arch/mips/ralink/Kconfig | 2 +
10 arch/mips/ralink/clk.c | 1 +
11 drivers/clocksource/Kconfig | 6 ++
12 drivers/clocksource/Makefile | 1 +
13 drivers/clocksource/cevt-rt3352.c | 162 +++++++++++++++++++++++++++++++++++++
14 include/linux/clocksource.h | 1 +
15 6 files changed, 173 insertions(+)
16 create mode 100644 drivers/clocksource/cevt-rt3352.c
17
18 diff --git a/arch/mips/ralink/Kconfig b/arch/mips/ralink/Kconfig
19 index 38540a4..3fe032c 100644
20 --- a/arch/mips/ralink/Kconfig
21 +++ b/arch/mips/ralink/Kconfig
22 @@ -14,6 +14,7 @@ choice
23 select USB_ARCH_HAS_HCD
24 select USB_ARCH_HAS_OHCI
25 select USB_ARCH_HAS_EHCI
26 + select CLKEVT_RT3352
27
28 config SOC_RT3883
29 bool "RT3883"
30 @@ -22,6 +23,7 @@ choice
31
32 config SOC_MT7620
33 bool "MT7620"
34 + select CLKEVT_RT3352
35
36 endchoice
37
38 diff --git a/arch/mips/ralink/clk.c b/arch/mips/ralink/clk.c
39 index 8dfa22f..bba0cdf 100644
40 --- a/arch/mips/ralink/clk.c
41 +++ b/arch/mips/ralink/clk.c
42 @@ -69,4 +69,5 @@ void __init plat_time_init(void)
43 pr_info("CPU Clock: %ldMHz\n", clk_get_rate(clk) / 1000000);
44 mips_hpt_frequency = clk_get_rate(clk) / 2;
45 clk_put(clk);
46 + clocksource_of_init();
47 }
48 diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig
49 index e507ab7..1216c16 100644
50 --- a/drivers/clocksource/Kconfig
51 +++ b/drivers/clocksource/Kconfig
52 @@ -7,6 +7,12 @@ config CLKSRC_I8253
53 config CLKEVT_I8253
54 bool
55
56 +config CLKEVT_RT3352
57 + bool
58 + depends on MIPS && RALINK
59 + select CLKSRC_OF
60 + select CLKSRC_MMIO
61 +
62 config I8253_LOCK
63 bool
64
65 diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile
66 index 4d8283a..db47a4e 100644
67 --- a/drivers/clocksource/Makefile
68 +++ b/drivers/clocksource/Makefile
69 @@ -10,6 +10,7 @@ obj-$(CONFIG_SH_TIMER_TMU) += sh_tmu.o
70 obj-$(CONFIG_EM_TIMER_STI) += em_sti.o
71 obj-$(CONFIG_CLKBLD_I8253) += i8253.o
72 obj-$(CONFIG_CLKSRC_MMIO) += mmio.o
73 +obj-$(CONFIG_CLKEVT_RT3352) += cevt-rt3352.o
74 obj-$(CONFIG_DW_APB_TIMER) += dw_apb_timer.o
75 obj-$(CONFIG_DW_APB_TIMER_OF) += dw_apb_timer_of.o
76 obj-$(CONFIG_CLKSRC_NOMADIK_MTU) += nomadik-mtu.o
77 diff --git a/drivers/clocksource/cevt-rt3352.c b/drivers/clocksource/cevt-rt3352.c
78 new file mode 100644
79 index 0000000..bd50edd
80 --- /dev/null
81 +++ b/drivers/clocksource/cevt-rt3352.c
82 @@ -0,0 +1,162 @@
83 +/*
84 + * This file is subject to the terms and conditions of the GNU General Public
85 + * License. See the file "COPYING" in the main directory of this archive
86 + * for more details.
87 + *
88 + * Copyright (C) 2013 by John Crispin <blogic@openwrt.org>
89 + */
90 +
91 +#include <linux/clockchips.h>
92 +#include <linux/clocksource.h>
93 +#include <linux/interrupt.h>
94 +#include <linux/reset.h>
95 +#include <linux/init.h>
96 +#include <linux/of.h>
97 +#include <linux/of_irq.h>
98 +#include <linux/of_address.h>
99 +
100 +#include <asm/mach-ralink/ralink_regs.h>
101 +#include <asm/time.h>
102 +
103 +#define SYSTICK_FREQ (50 * 1000)
104 +
105 +#define SYSTICK_CONFIG 0x00
106 +#define SYSTICK_COMPARE 0x04
107 +#define SYSTICK_COUNT 0x08
108 +
109 +/* route systick irq to mips irq 7 instead of the r4k-timer */
110 +#define CFG_EXT_STK_EN 0x2
111 +/* enable the counter */
112 +#define CFG_CNT_EN 0x1
113 +
114 +struct systick_device {
115 + void __iomem *membase;
116 + struct clock_event_device dev;
117 + int irq_requested;
118 + int freq_scale;
119 +};
120 +
121 +static void systick_set_clock_mode(enum clock_event_mode mode,
122 + struct clock_event_device *evt);
123 +
124 +static int systick_next_event(unsigned long delta,
125 + struct clock_event_device *evt)
126 +{
127 + struct systick_device *sdev = container_of(evt, struct systick_device, dev);
128 + u32 count;
129 +
130 + count = ioread32(sdev->membase + SYSTICK_COUNT);
131 + count = (count + delta) % SYSTICK_FREQ;
132 + iowrite32(count + delta, sdev->membase + SYSTICK_COMPARE);
133 +
134 + return 0;
135 +}
136 +
137 +static void systick_event_handler(struct clock_event_device *dev)
138 +{
139 + /* noting to do here */
140 +}
141 +
142 +static irqreturn_t systick_interrupt(int irq, void *dev_id)
143 +{
144 + struct clock_event_device *dev = (struct clock_event_device *) dev_id;
145 +
146 + dev->event_handler(dev);
147 +
148 + return IRQ_HANDLED;
149 +}
150 +
151 +static struct systick_device systick = {
152 + .dev = {
153 + /* cevt-r4k uses 300, make sure systick gets used if available */
154 + .rating = 310,
155 + .features = CLOCK_EVT_FEAT_ONESHOT,
156 + .set_next_event = systick_next_event,
157 + .set_mode = systick_set_clock_mode,
158 + .event_handler = systick_event_handler,
159 + },
160 +};
161 +
162 +static struct irqaction systick_irqaction = {
163 + .handler = systick_interrupt,
164 + .flags = IRQF_PERCPU | IRQF_TIMER,
165 + .dev_id = &systick.dev,
166 +};
167 +
168 +/* ugly hack */
169 +#ifdef CONFIG_SOC_MT7620
170 +
171 +#define CLK_LUT_CFG 0x40
172 +#define SLEEP_EN BIT(31)
173 +
174 +static inline void mt7620_freq_scaling(struct systick_device *sdev, int status)
175 +{
176 + if (sdev->freq_scale == status)
177 + return;
178 +
179 + sdev->freq_scale = status;
180 +
181 + pr_info("%s: %s autosleep mode\n", systick.dev.name, (status) ? ("enable") : ("disable"));
182 + if (status)
183 + rt_sysc_w32(rt_sysc_r32(CLK_LUT_CFG) | SLEEP_EN, CLK_LUT_CFG);
184 + else
185 + rt_sysc_w32(rt_sysc_r32(CLK_LUT_CFG) & ~SLEEP_EN, CLK_LUT_CFG);
186 +}
187 +#else
188 +static inline void mt7620_freq_scaling(struct systick_device *sdev, int status) {}
189 +#endif
190 +
191 +static void systick_set_clock_mode(enum clock_event_mode mode,
192 + struct clock_event_device *evt)
193 +{
194 + struct systick_device *sdev = container_of(evt, struct systick_device, dev);
195 +
196 + switch (mode) {
197 + case CLOCK_EVT_MODE_ONESHOT:
198 + if (!sdev->irq_requested)
199 + setup_irq(systick.dev.irq, &systick_irqaction);
200 + mt7620_freq_scaling(sdev, 1);
201 + sdev->irq_requested = 1;
202 + iowrite32(CFG_EXT_STK_EN | CFG_CNT_EN, systick.membase + SYSTICK_CONFIG);
203 + break;
204 +
205 + case CLOCK_EVT_MODE_SHUTDOWN:
206 + if (sdev->irq_requested)
207 + free_irq(systick.dev.irq, &systick_irqaction);
208 + mt7620_freq_scaling(sdev, 0);
209 + sdev->irq_requested = 0;
210 + iowrite32(0, systick.membase + SYSTICK_CONFIG);
211 + break;
212 +
213 + default:
214 + pr_err("%s: Unhandeled mips clock_mode\n", systick.dev.name);
215 + break;
216 + }
217 +}
218 +
219 +static void __init ralink_systick_init(struct device_node *np)
220 +{
221 + systick.membase = of_iomap(np, 0);
222 + if (!systick.membase) {
223 + pr_err("%s: of_iomap failed", np->name);
224 + return;
225 + }
226 +
227 + clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
228 + SYSTICK_FREQ, 301, 16, clocksource_mmio_readl_up);
229 +
230 + systick_irqaction.name = np->name;
231 + systick.dev.name = np->name;
232 + clockevent_set_clock(&systick.dev, SYSTICK_FREQ);
233 + systick.dev.max_delta_ns = clockevent_delta2ns(0x7fff, &systick.dev);
234 + systick.dev.min_delta_ns = clockevent_delta2ns(0x3, &systick.dev);
235 + systick.dev.irq = irq_of_parse_and_map(np, 0);
236 + if (!systick.dev.irq)
237 + panic("%s: request_irq failed", np->name);
238 +
239 + clockevents_register_device(&systick.dev);
240 +
241 + pr_info("%s: runing - mult: %d, shift: %d\n", np->name, systick.dev.mult, systick.dev.shift);
242 +}
243 +
244 +CLOCKSOURCE_OF_DECLARE(systick, "ralink,cevt-systick", ralink_systick_init);
245 diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h
246 index 27cfda4..08ed5e1 100644
247 --- a/include/linux/clocksource.h
248 +++ b/include/linux/clocksource.h
249 @@ -340,6 +340,7 @@ extern void clocksource_of_init(void);
250 __used __section(__clksrc_of_table) \
251 = { .compatible = compat, .data = fn };
252 #else
253 +static inline void clocksource_of_init(void) {}
254 #define CLOCKSOURCE_OF_DECLARE(name, compat, fn)
255 #endif
256
257 --
258 1.7.10.4
259