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