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