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