blob: b4c8a023a2d44016789ece94a04332cb0fb90ce0 [file] [log] [blame]
Noam Camusa5322452015-10-17 22:37:30 +03001/*
2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include <linux/interrupt.h>
34#include <linux/clocksource.h>
35#include <linux/clockchips.h>
36#include <linux/clk.h>
37#include <linux/of.h>
38#include <linux/of_irq.h>
39#include <linux/cpu.h>
40#include <soc/nps/common.h>
41
42#define NPS_MSU_TICK_LOW 0xC8
43#define NPS_CLUSTER_OFFSET 8
44#define NPS_CLUSTER_NUM 16
45
46/* This array is per cluster of CPUs (Each NPS400 cluster got 256 CPUs) */
47static void *nps_msu_reg_low_addr[NPS_CLUSTER_NUM] __read_mostly;
48
Noam Camus0465fb42016-11-16 08:31:12 +020049static int __init nps_get_timer_clk(struct device_node *node,
50 unsigned long *timer_freq,
51 struct clk **clk)
52{
53 int ret;
54
55 *clk = of_clk_get(node, 0);
56 if (IS_ERR(*clk)) {
57 pr_err("timer missing clk");
58 return PTR_ERR(*clk);
59 }
60
61 ret = clk_prepare_enable(*clk);
62 if (ret) {
63 pr_err("Couldn't enable parent clk\n");
64 clk_put(*clk);
65 return ret;
66 }
67
68 *timer_freq = clk_get_rate(*clk);
69 if (!(*timer_freq)) {
70 pr_err("Couldn't get clk rate\n");
71 clk_disable_unprepare(*clk);
72 clk_put(*clk);
73 return -EINVAL;
74 }
75
76 return 0;
77}
Noam Camusa5322452015-10-17 22:37:30 +030078
79static cycle_t nps_clksrc_read(struct clocksource *clksrc)
80{
81 int cluster = raw_smp_processor_id() >> NPS_CLUSTER_OFFSET;
82
83 return (cycle_t)ioread32be(nps_msu_reg_low_addr[cluster]);
84}
85
Noam Camus0465fb42016-11-16 08:31:12 +020086static int __init nps_setup_clocksource(struct device_node *node)
Noam Camusa5322452015-10-17 22:37:30 +030087{
88 int ret, cluster;
Noam Camus0465fb42016-11-16 08:31:12 +020089 struct clk *clk;
90 unsigned long nps_timer1_freq;
91
Noam Camusa5322452015-10-17 22:37:30 +030092
93 for (cluster = 0; cluster < NPS_CLUSTER_NUM; cluster++)
94 nps_msu_reg_low_addr[cluster] =
95 nps_host_reg((cluster << NPS_CLUSTER_OFFSET),
Noam Camus0465fb42016-11-16 08:31:12 +020096 NPS_MSU_BLKID, NPS_MSU_TICK_LOW);
Noam Camusa5322452015-10-17 22:37:30 +030097
Noam Camus0465fb42016-11-16 08:31:12 +020098 ret = nps_get_timer_clk(node, &nps_timer1_freq, &clk);
99 if (ret)
Daniel Lezcano2d9b65062016-06-15 14:16:11 +0200100 return ret;
Noam Camusa5322452015-10-17 22:37:30 +0300101
Noam Camus0465fb42016-11-16 08:31:12 +0200102 ret = clocksource_mmio_init(nps_msu_reg_low_addr, "nps-tick",
103 nps_timer1_freq, 300, 32, nps_clksrc_read);
Noam Camusa5322452015-10-17 22:37:30 +0300104 if (ret) {
105 pr_err("Couldn't register clock source.\n");
106 clk_disable_unprepare(clk);
107 }
Daniel Lezcano2d9b65062016-06-15 14:16:11 +0200108
109 return ret;
Noam Camusa5322452015-10-17 22:37:30 +0300110}
111
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200112CLOCKSOURCE_OF_DECLARE(ezchip_nps400_clksrc, "ezchip,nps400-timer",
Noam Camus0465fb42016-11-16 08:31:12 +0200113 nps_setup_clocksource);
Noam Camus60263dc2016-11-17 09:12:43 +0200114CLOCKSOURCE_OF_DECLARE(ezchip_nps400_clk_src, "ezchip,nps400-timer1",
115 nps_setup_clocksource);
116
117#ifdef CONFIG_EZNPS_MTM_EXT
118#include <soc/nps/mtm.h>
119
120/* Timer related Aux registers */
121#define NPS_REG_TIMER0_TSI 0xFFFFF850
122#define NPS_REG_TIMER0_LIMIT 0x23
123#define NPS_REG_TIMER0_CTRL 0x22
124#define NPS_REG_TIMER0_CNT 0x21
125
126/*
127 * Interrupt Enabled (IE) - re-arm the timer
128 * Not Halted (NH) - is cleared when working with JTAG (for debug)
129 */
130#define TIMER0_CTRL_IE BIT(0)
131#define TIMER0_CTRL_NH BIT(1)
132
133static unsigned long nps_timer0_freq;
134static unsigned long nps_timer0_irq;
135
136static void nps_clkevent_rm_thread(void)
137{
138 int thread;
139 unsigned int cflags, enabled_threads;
140
141 hw_schd_save(&cflags);
142
143 enabled_threads = read_aux_reg(NPS_REG_TIMER0_TSI);
144
145 /* remove thread from TSI1 */
146 thread = read_aux_reg(CTOP_AUX_THREAD_ID);
147 enabled_threads &= ~(1 << thread);
148 write_aux_reg(NPS_REG_TIMER0_TSI, enabled_threads);
149
150 /* Acknowledge and if needed re-arm the timer */
151 if (!enabled_threads)
152 write_aux_reg(NPS_REG_TIMER0_CTRL, TIMER0_CTRL_NH);
153 else
154 write_aux_reg(NPS_REG_TIMER0_CTRL,
155 TIMER0_CTRL_IE | TIMER0_CTRL_NH);
156
157 hw_schd_restore(cflags);
158}
159
160static void nps_clkevent_add_thread(unsigned long delta)
161{
162 int thread;
163 unsigned int cflags, enabled_threads;
164
165 hw_schd_save(&cflags);
166
167 /* add thread to TSI1 */
168 thread = read_aux_reg(CTOP_AUX_THREAD_ID);
169 enabled_threads = read_aux_reg(NPS_REG_TIMER0_TSI);
170 enabled_threads |= (1 << thread);
171 write_aux_reg(NPS_REG_TIMER0_TSI, enabled_threads);
172
173 /* set next timer event */
174 write_aux_reg(NPS_REG_TIMER0_LIMIT, delta);
175 write_aux_reg(NPS_REG_TIMER0_CNT, 0);
176 write_aux_reg(NPS_REG_TIMER0_CTRL,
177 TIMER0_CTRL_IE | TIMER0_CTRL_NH);
178
179 hw_schd_restore(cflags);
180}
181
182/*
183 * Whenever anyone tries to change modes, we just mask interrupts
184 * and wait for the next event to get set.
185 */
186static int nps_clkevent_set_state(struct clock_event_device *dev)
187{
188 nps_clkevent_rm_thread();
189 disable_percpu_irq(nps_timer0_irq);
190
191 return 0;
192}
193
194static int nps_clkevent_set_next_event(unsigned long delta,
195 struct clock_event_device *dev)
196{
197 nps_clkevent_add_thread(delta);
198 enable_percpu_irq(nps_timer0_irq, IRQ_TYPE_NONE);
199
200 return 0;
201}
202
203static DEFINE_PER_CPU(struct clock_event_device, nps_clockevent_device) = {
204 .name = "NPS Timer0",
205 .features = CLOCK_EVT_FEAT_ONESHOT,
206 .rating = 300,
207 .set_next_event = nps_clkevent_set_next_event,
208 .set_state_oneshot = nps_clkevent_set_state,
209 .set_state_oneshot_stopped = nps_clkevent_set_state,
210 .set_state_shutdown = nps_clkevent_set_state,
211 .tick_resume = nps_clkevent_set_state,
212};
213
214static irqreturn_t timer_irq_handler(int irq, void *dev_id)
215{
216 struct clock_event_device *evt = dev_id;
217
218 nps_clkevent_rm_thread();
219 evt->event_handler(evt);
220
221 return IRQ_HANDLED;
222}
223
224static int nps_timer_starting_cpu(unsigned int cpu)
225{
226 struct clock_event_device *evt = this_cpu_ptr(&nps_clockevent_device);
227
228 evt->cpumask = cpumask_of(smp_processor_id());
229
230 clockevents_config_and_register(evt, nps_timer0_freq, 0, ULONG_MAX);
231 enable_percpu_irq(nps_timer0_irq, IRQ_TYPE_NONE);
232
233 return 0;
234}
235
236static int nps_timer_dying_cpu(unsigned int cpu)
237{
238 disable_percpu_irq(nps_timer0_irq);
239 return 0;
240}
241
242static int __init nps_setup_clockevent(struct device_node *node)
243{
244 struct clk *clk;
245 int ret;
246
247 nps_timer0_irq = irq_of_parse_and_map(node, 0);
248 if (nps_timer0_irq <= 0) {
249 pr_err("clockevent: missing irq");
250 return -EINVAL;
251 }
252
253 ret = nps_get_timer_clk(node, &nps_timer0_freq, &clk);
254 if (ret)
255 return ret;
256
257 /* Needs apriori irq_set_percpu_devid() done in intc map function */
258 ret = request_percpu_irq(nps_timer0_irq, timer_irq_handler,
259 "Timer0 (per-cpu-tick)",
260 &nps_clockevent_device);
261 if (ret) {
262 pr_err("Couldn't request irq\n");
263 clk_disable_unprepare(clk);
264 return ret;
265 }
266
267 ret = cpuhp_setup_state(CPUHP_AP_ARC_TIMER_STARTING,
268 "clockevents/nps:starting",
269 nps_timer_starting_cpu,
270 nps_timer_dying_cpu);
271 if (ret) {
272 pr_err("Failed to setup hotplug state");
273 clk_disable_unprepare(clk);
274 free_percpu_irq(nps_timer0_irq, &nps_clockevent_device);
275 return ret;
276 }
277
278 return 0;
279}
280
281CLOCKSOURCE_OF_DECLARE(ezchip_nps400_clk_evt, "ezchip,nps400-timer0",
282 nps_setup_clockevent);
283#endif /* CONFIG_EZNPS_MTM_EXT */