ramips: fix power LED DTB for wt3020
[openwrt/staging/wigyori.git] / target / linux / ramips / patches-4.14 / 0006-MIPS-ralink-add-cpu-frequency-scaling.patch
1 From bd30f19a006fb52bac80c6463c49dd2f4159f4ac Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 28 Jul 2013 16:26:41 +0200
4 Subject: [PATCH 06/53] MIPS: ralink: add cpu frequency scaling
5
6 This feature will break udelay() and cause the delay loop to have longer delays
7 when the frequency is scaled causing a performance hit.
8
9 Signed-off-by: John Crispin <blogic@openwrt.org>
10 ---
11 arch/mips/ralink/cevt-rt3352.c | 38 ++++++++++++++++++++++++++++++++++++++
12 1 file changed, 38 insertions(+)
13
14 --- a/arch/mips/ralink/cevt-rt3352.c
15 +++ b/arch/mips/ralink/cevt-rt3352.c
16 @@ -29,6 +29,10 @@
17 /* enable the counter */
18 #define CFG_CNT_EN 0x1
19
20 +/* mt7620 frequency scaling defines */
21 +#define CLK_LUT_CFG 0x40
22 +#define SLEEP_EN BIT(31)
23 +
24 struct systick_device {
25 void __iomem *membase;
26 struct clock_event_device dev;
27 @@ -36,21 +40,53 @@ struct systick_device {
28 int freq_scale;
29 };
30
31 +static void (*systick_freq_scaling)(struct systick_device *sdev, int status);
32 +
33 static int systick_set_oneshot(struct clock_event_device *evt);
34 static int systick_shutdown(struct clock_event_device *evt);
35
36 +static inline void mt7620_freq_scaling(struct systick_device *sdev, int status)
37 +{
38 + if (sdev->freq_scale == status)
39 + return;
40 +
41 + sdev->freq_scale = status;
42 +
43 + pr_info("%s: %s autosleep mode\n", sdev->dev.name,
44 + (status) ? ("enable") : ("disable"));
45 + if (status)
46 + rt_sysc_w32(rt_sysc_r32(CLK_LUT_CFG) | SLEEP_EN, CLK_LUT_CFG);
47 + else
48 + rt_sysc_w32(rt_sysc_r32(CLK_LUT_CFG) & ~SLEEP_EN, CLK_LUT_CFG);
49 +}
50 +
51 +static inline unsigned int read_count(struct systick_device *sdev)
52 +{
53 + return ioread32(sdev->membase + SYSTICK_COUNT);
54 +}
55 +
56 +static inline unsigned int read_compare(struct systick_device *sdev)
57 +{
58 + return ioread32(sdev->membase + SYSTICK_COMPARE);
59 +}
60 +
61 +static inline void write_compare(struct systick_device *sdev, unsigned int val)
62 +{
63 + iowrite32(val, sdev->membase + SYSTICK_COMPARE);
64 +}
65 +
66 static int systick_next_event(unsigned long delta,
67 struct clock_event_device *evt)
68 {
69 struct systick_device *sdev;
70 - u32 count;
71 + int res;
72
73 sdev = container_of(evt, struct systick_device, dev);
74 - count = ioread32(sdev->membase + SYSTICK_COUNT);
75 - count = (count + delta) % SYSTICK_FREQ;
76 - iowrite32(count, sdev->membase + SYSTICK_COMPARE);
77 + delta += read_count(sdev);
78 + write_compare(sdev, delta);
79 + res = ((int)(read_count(sdev) - delta) >= 0) ? -ETIME : 0;
80
81 - return 0;
82 + return res;
83 }
84
85 static void systick_event_handler(struct clock_event_device *dev)
86 @@ -60,20 +96,25 @@ static void systick_event_handler(struct
87
88 static irqreturn_t systick_interrupt(int irq, void *dev_id)
89 {
90 - struct clock_event_device *dev = (struct clock_event_device *) dev_id;
91 + int ret = 0;
92 + struct clock_event_device *cdev;
93 + struct systick_device *sdev;
94
95 - dev->event_handler(dev);
96 + if (read_c0_cause() & STATUSF_IP7) {
97 + cdev = (struct clock_event_device *) dev_id;
98 + sdev = container_of(cdev, struct systick_device, dev);
99 +
100 + /* Clear Count/Compare Interrupt */
101 + write_compare(sdev, read_compare(sdev));
102 + cdev->event_handler(cdev);
103 + ret = 1;
104 + }
105
106 - return IRQ_HANDLED;
107 + return IRQ_RETVAL(ret);
108 }
109
110 static struct systick_device systick = {
111 .dev = {
112 - /*
113 - * cevt-r4k uses 300, make sure systick
114 - * gets used if available
115 - */
116 - .rating = 310,
117 .features = CLOCK_EVT_FEAT_ONESHOT,
118 .set_next_event = systick_next_event,
119 .set_state_shutdown = systick_shutdown,
120 @@ -95,9 +136,15 @@ static int systick_shutdown(struct clock
121 sdev = container_of(evt, struct systick_device, dev);
122
123 if (sdev->irq_requested)
124 - free_irq(systick.dev.irq, &systick_irqaction);
125 + remove_irq(systick.dev.irq, &systick_irqaction);
126 sdev->irq_requested = 0;
127 - iowrite32(0, systick.membase + SYSTICK_CONFIG);
128 + iowrite32(CFG_CNT_EN, systick.membase + SYSTICK_CONFIG);
129 +
130 + if (systick_freq_scaling)
131 + systick_freq_scaling(sdev, 0);
132 +
133 + if (systick_freq_scaling)
134 + systick_freq_scaling(sdev, 1);
135
136 return 0;
137 }
138 @@ -117,34 +164,48 @@ static int systick_set_oneshot(struct cl
139 return 0;
140 }
141
142 +static const struct of_device_id systick_match[] = {
143 + { .compatible = "ralink,mt7620a-systick", .data = mt7620_freq_scaling},
144 + {},
145 +};
146 +
147 static int __init ralink_systick_init(struct device_node *np)
148 {
149 + const struct of_device_id *match;
150 + int rating = 200;
151 int ret;
152
153 systick.membase = of_iomap(np, 0);
154 if (!systick.membase)
155 return -ENXIO;
156
157 - systick_irqaction.name = np->name;
158 - systick.dev.name = np->name;
159 - clockevents_calc_mult_shift(&systick.dev, SYSTICK_FREQ, 60);
160 - systick.dev.max_delta_ns = clockevent_delta2ns(0x7fff, &systick.dev);
161 - systick.dev.max_delta_ticks = 0x7fff;
162 - systick.dev.min_delta_ns = clockevent_delta2ns(0x3, &systick.dev);
163 - systick.dev.min_delta_ticks = 0x3;
164 + match = of_match_node(systick_match, np);
165 + if (match) {
166 + systick_freq_scaling = match->data;
167 + /*
168 + * cevt-r4k uses 300, make sure systick
169 + * gets used if available
170 + */
171 + rating = 310;
172 + }
173 +
174 + /* enable counter than register clock source */
175 + iowrite32(CFG_CNT_EN, systick.membase + SYSTICK_CONFIG);
176 + clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
177 + SYSTICK_FREQ, rating, 16, clocksource_mmio_readl_up);
178 +
179 + /* register clock event */
180 systick.dev.irq = irq_of_parse_and_map(np, 0);
181 if (!systick.dev.irq) {
182 pr_err("%s: request_irq failed", np->name);
183 return -EINVAL;
184 }
185
186 - ret = clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
187 - SYSTICK_FREQ, 301, 16,
188 - clocksource_mmio_readl_up);
189 - if (ret)
190 - return ret;
191 -
192 - clockevents_register_device(&systick.dev);
193 + systick_irqaction.name = np->name;
194 + systick.dev.name = np->name;
195 + systick.dev.rating = rating;
196 + systick.dev.cpumask = cpumask_of(0);
197 + clockevents_config_and_register(&systick.dev, SYSTICK_FREQ, 0x3, 0x7fff);
198
199 pr_info("%s: running - mult: %d, shift: %d\n",
200 np->name, systick.dev.mult, systick.dev.shift);