a69739bb06170ac01ac7cede0472fc2dac80d46f
[openwrt.git] / target / linux / ramips / patches-3.10 / 0011-MIPS-ralink-add-support-for-systick-timer-found-on-n.patch
1 From 3b511d972b556712f89ccc68825c0ec8f398dc5c Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 28 Jul 2013 13:46:09 +0200
4 Subject: [PATCH 11/25] MIPS: ralink: add support for systick timer found on
5  newer ralink SoC
6
7 Newer Ralink SoC (MT7620x and RT5350) have a 50KHz clock that runs independent
8 of the SoC master clock. If we want to automatic frequency scaling to work we
9 need to use the systick timer as the clock source.
10
11 Signed-off-by: John Crispin <blogic@openwrt.org>
12 ---
13  arch/mips/ralink/Kconfig       |    7 ++
14  arch/mips/ralink/Makefile      |    2 +
15  arch/mips/ralink/cevt-rt3352.c |  145 ++++++++++++++++++++++++++++++++++++++++
16  3 files changed, 154 insertions(+)
17  create mode 100644 arch/mips/ralink/cevt-rt3352.c
18
19 Index: linux-3.10.13/arch/mips/ralink/Kconfig
20 ===================================================================
21 --- linux-3.10.13.orig/arch/mips/ralink/Kconfig 2013-09-27 02:18:49.000000000 +0200
22 +++ linux-3.10.13/arch/mips/ralink/Kconfig      2013-10-02 21:40:41.252368892 +0200
23 @@ -1,5 +1,12 @@
24  if RALINK
25  
26 +config CLKEVT_RT3352
27 +       bool "Systick Clockevent source"
28 +       depends on SOC_RT305X || SOC_MT7620
29 +       default y
30 +       select CLKSRC_OF
31 +       select CLKSRC_MMIO
32 +
33  choice
34         prompt "Ralink SoC selection"
35         default SOC_RT305X
36 Index: linux-3.10.13/arch/mips/ralink/Makefile
37 ===================================================================
38 --- linux-3.10.13.orig/arch/mips/ralink/Makefile        2013-10-02 20:46:43.856230578 +0200
39 +++ linux-3.10.13/arch/mips/ralink/Makefile     2013-10-02 21:13:11.584298451 +0200
40 @@ -8,6 +8,8 @@
41  
42  obj-y := prom.o of.o reset.o clk.o irq.o timer.o
43  
44 +obj-$(CONFIG_CLKEVT_RT3352) += cevt-rt3352.o
45 +
46  obj-$(CONFIG_SOC_RT288X) += rt288x.o
47  obj-$(CONFIG_SOC_RT305X) += rt305x.o
48  obj-$(CONFIG_SOC_RT3883) += rt3883.o
49 Index: linux-3.10.13/arch/mips/ralink/cevt-rt3352.c
50 ===================================================================
51 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
52 +++ linux-3.10.13/arch/mips/ralink/cevt-rt3352.c        2013-10-02 21:13:19.396298787 +0200
53 @@ -0,0 +1,145 @@
54 +/*
55 + * This file is subject to the terms and conditions of the GNU General Public
56 + * License.  See the file "COPYING" in the main directory of this archive
57 + * for more details.
58 + *
59 + * Copyright (C) 2013 by John Crispin <blogic@openwrt.org>
60 + */
61 +
62 +#include <linux/clockchips.h>
63 +#include <linux/clocksource.h>
64 +#include <linux/interrupt.h>
65 +#include <linux/reset.h>
66 +#include <linux/init.h>
67 +#include <linux/time.h>
68 +#include <linux/of.h>
69 +#include <linux/of_irq.h>
70 +#include <linux/of_address.h>
71 +
72 +#include <asm/mach-ralink/ralink_regs.h>
73 +
74 +#define SYSTICK_FREQ           (50 * 1000)
75 +
76 +#define SYSTICK_CONFIG         0x00
77 +#define SYSTICK_COMPARE                0x04
78 +#define SYSTICK_COUNT          0x08
79 +
80 +/* route systick irq to mips irq 7 instead of the r4k-timer */
81 +#define CFG_EXT_STK_EN         0x2
82 +/* enable the counter */
83 +#define CFG_CNT_EN             0x1
84 +
85 +struct systick_device {
86 +       void __iomem *membase;
87 +       struct clock_event_device dev;
88 +       int irq_requested;
89 +       int freq_scale;
90 +};
91 +
92 +static void systick_set_clock_mode(enum clock_event_mode mode,
93 +                               struct clock_event_device *evt);
94 +
95 +static int systick_next_event(unsigned long delta,
96 +                               struct clock_event_device *evt)
97 +{
98 +       struct systick_device *sdev;
99 +       u32 count;
100 +
101 +       sdev = container_of(evt, struct systick_device, dev);
102 +       count = ioread32(sdev->membase + SYSTICK_COUNT);
103 +       count = (count + delta) % SYSTICK_FREQ;
104 +       iowrite32(count + delta, sdev->membase + SYSTICK_COMPARE);
105 +
106 +       return 0;
107 +}
108 +
109 +static void systick_event_handler(struct clock_event_device *dev)
110 +{
111 +       /* noting to do here */
112 +}
113 +
114 +static irqreturn_t systick_interrupt(int irq, void *dev_id)
115 +{
116 +       struct clock_event_device *dev = (struct clock_event_device *) dev_id;
117 +
118 +       dev->event_handler(dev);
119 +
120 +       return IRQ_HANDLED;
121 +}
122 +
123 +static struct systick_device systick = {
124 +       .dev = {
125 +               /*
126 +                * cevt-r4k uses 300, make sure systick
127 +                * gets used if available
128 +                */
129 +               .rating         = 310,
130 +               .features       = CLOCK_EVT_FEAT_ONESHOT,
131 +               .set_next_event = systick_next_event,
132 +               .set_mode       = systick_set_clock_mode,
133 +               .event_handler  = systick_event_handler,
134 +       },
135 +};
136 +
137 +static struct irqaction systick_irqaction = {
138 +       .handler = systick_interrupt,
139 +       .flags = IRQF_PERCPU | IRQF_TIMER,
140 +       .dev_id = &systick.dev,
141 +};
142 +
143 +static void systick_set_clock_mode(enum clock_event_mode mode,
144 +                               struct clock_event_device *evt)
145 +{
146 +       struct systick_device *sdev;
147 +
148 +       sdev = container_of(evt, struct systick_device, dev);
149 +
150 +       switch (mode) {
151 +       case CLOCK_EVT_MODE_ONESHOT:
152 +               if (!sdev->irq_requested)
153 +                       setup_irq(systick.dev.irq, &systick_irqaction);
154 +               sdev->irq_requested = 1;
155 +               iowrite32(CFG_EXT_STK_EN | CFG_CNT_EN,
156 +                               systick.membase + SYSTICK_CONFIG);
157 +               break;
158 +
159 +       case CLOCK_EVT_MODE_SHUTDOWN:
160 +               if (sdev->irq_requested)
161 +                       free_irq(systick.dev.irq, &systick_irqaction);
162 +               sdev->irq_requested = 0;
163 +               iowrite32(0, systick.membase + SYSTICK_CONFIG);
164 +               break;
165 +
166 +       default:
167 +               pr_err("%s: Unhandeled mips clock_mode\n", systick.dev.name);
168 +               break;
169 +       }
170 +}
171 +
172 +static void __init ralink_systick_init(struct device_node *np)
173 +{
174 +       systick.membase = of_iomap(np, 0);
175 +       if (!systick.membase)
176 +               return;
177 +
178 +       systick_irqaction.name = np->name;
179 +       systick.dev.name = np->name;
180 +       clockevents_calc_mult_shift(&systick.dev, SYSTICK_FREQ, 60);
181 +       systick.dev.max_delta_ns = clockevent_delta2ns(0x7fff, &systick.dev);
182 +       systick.dev.min_delta_ns = clockevent_delta2ns(0x3, &systick.dev);
183 +       systick.dev.irq = irq_of_parse_and_map(np, 0);
184 +       if (!systick.dev.irq) {
185 +               pr_err("%s: request_irq failed", np->name);
186 +               return;
187 +       }
188 +
189 +       clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
190 +                       SYSTICK_FREQ, 301, 16, clocksource_mmio_readl_up);
191 +
192 +       clockevents_register_device(&systick.dev);
193 +
194 +       pr_info("%s: runing - mult: %d, shift: %d\n",
195 +                       np->name, systick.dev.mult, systick.dev.shift);
196 +}
197 +
198 +CLOCKSOURCE_OF_DECLARE(systick, "ralink,cevt-systick", ralink_systick_init);