timer-sp804.c 9.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 *  linux/drivers/clocksource/timer-sp.c
4 5 6 7
 *
 *  Copyright (C) 1999 - 2003 ARM Limited
 *  Copyright (C) 2000 Deep Blue Solutions Ltd
 */
8
#include <linux/clk.h>
9 10
#include <linux/clocksource.h>
#include <linux/clockchips.h>
11
#include <linux/err.h>
12 13 14
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/io.h>
15 16
#include <linux/of.h>
#include <linux/of_address.h>
17
#include <linux/of_clk.h>
18
#include <linux/of_irq.h>
19
#include <linux/sched_clock.h>
20

21
#include "timer-sp.h"
22

23 24 25 26
/* Hisilicon 64-bit timer(a variant of ARM SP804) */
#define HISI_TIMER_1_BASE	0x00
#define HISI_TIMER_2_BASE	0x40
#define HISI_TIMER_LOAD		0x00
27
#define HISI_TIMER_LOAD_H	0x04
28
#define HISI_TIMER_VALUE	0x08
29
#define HISI_TIMER_VALUE_H	0x0c
30 31 32 33 34
#define HISI_TIMER_CTRL		0x10
#define HISI_TIMER_INTCLR	0x14
#define HISI_TIMER_RIS		0x18
#define HISI_TIMER_MIS		0x1c
#define HISI_TIMER_BGLOAD	0x20
35
#define HISI_TIMER_BGLOAD_H	0x24
36 37


38 39 40 41 42 43 44 45 46
struct sp804_timer __initdata arm_sp804_timer = {
	.load		= TIMER_LOAD,
	.value		= TIMER_VALUE,
	.ctrl		= TIMER_CTRL,
	.intclr		= TIMER_INTCLR,
	.timer_base	= {TIMER_1_BASE, TIMER_2_BASE},
	.width		= 32,
};

47 48
struct sp804_timer __initdata hisi_sp804_timer = {
	.load		= HISI_TIMER_LOAD,
49
	.load_h		= HISI_TIMER_LOAD_H,
50
	.value		= HISI_TIMER_VALUE,
51
	.value_h	= HISI_TIMER_VALUE_H,
52 53 54 55 56 57
	.ctrl		= HISI_TIMER_CTRL,
	.intclr		= HISI_TIMER_INTCLR,
	.timer_base	= {HISI_TIMER_1_BASE, HISI_TIMER_2_BASE},
	.width		= 64,
};

58 59
static struct sp804_clkevt sp804_clkevt[NR_TIMERS];

60
static long __init sp804_get_clock_rate(struct clk *clk, const char *name)
61 62 63 64
{
	long rate;
	int err;

65 66 67 68 69 70 71
	if (!clk)
		clk = clk_get_sys("sp804", name);
	if (IS_ERR(clk)) {
		pr_err("sp804: %s clock not found: %ld\n", name, PTR_ERR(clk));
		return PTR_ERR(clk);
	}

72 73
	err = clk_prepare(clk);
	if (err) {
74
		pr_err("sp804: clock failed to prepare: %d\n", err);
75 76 77 78
		clk_put(clk);
		return err;
	}

79 80
	err = clk_enable(clk);
	if (err) {
81
		pr_err("sp804: clock failed to enable: %d\n", err);
82
		clk_unprepare(clk);
83 84 85 86 87 88
		clk_put(clk);
		return err;
	}

	rate = clk_get_rate(clk);
	if (rate < 0) {
89
		pr_err("sp804: clock failed to get rate: %ld\n", rate);
90
		clk_disable(clk);
91
		clk_unprepare(clk);
92 93 94 95 96 97
		clk_put(clk);
	}

	return rate;
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
static struct sp804_clkevt * __init sp804_clkevt_get(void __iomem *base)
{
	int i;

	for (i = 0; i < NR_TIMERS; i++) {
		if (sp804_clkevt[i].base == base)
			return &sp804_clkevt[i];
	}

	/* It's impossible to reach here */
	WARN_ON(1);

	return NULL;
}

static struct sp804_clkevt *sched_clkevt;
114

115
static u64 notrace sp804_read(void)
116
{
117
	return ~readl_relaxed(sched_clkevt->value);
118 119
}

120 121 122 123
int __init sp804_clocksource_and_sched_clock_init(void __iomem *base,
						  const char *name,
						  struct clk *clk,
						  int use_sched_clock)
124
{
125
	long rate;
126
	struct sp804_clkevt *clkevt;
127

128
	rate = sp804_get_clock_rate(clk, name);
129
	if (rate < 0)
130
		return -EINVAL;
131

132 133 134 135 136
	clkevt = sp804_clkevt_get(base);

	writel(0, clkevt->ctrl);
	writel(0xffffffff, clkevt->load);
	writel(0xffffffff, clkevt->value);
137 138 139 140
	if (clkevt->width == 64) {
		writel(0xffffffff, clkevt->load_h);
		writel(0xffffffff, clkevt->value_h);
	}
141
	writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
142
		clkevt->ctrl);
143

144
	clocksource_mmio_init(clkevt->value, name,
145
		rate, 200, 32, clocksource_mmio_readl_down);
146 147

	if (use_sched_clock) {
148
		sched_clkevt = clkevt;
149
		sched_clock_register(sp804_read, 32, rate);
150
	}
151 152

	return 0;
153 154 155
}


156
static struct sp804_clkevt *common_clkevt;
157 158 159 160 161 162 163 164 165

/*
 * IRQ handler for the timer
 */
static irqreturn_t sp804_timer_interrupt(int irq, void *dev_id)
{
	struct clock_event_device *evt = dev_id;

	/* clear the interrupt */
166
	writel(1, common_clkevt->intclr);
167 168 169 170 171 172

	evt->event_handler(evt);

	return IRQ_HANDLED;
}

173
static inline void evt_timer_shutdown(struct clock_event_device *evt)
174
{
175
	writel(0, common_clkevt->ctrl);
176
}
177

178 179
static int sp804_shutdown(struct clock_event_device *evt)
{
180
	evt_timer_shutdown(evt);
181 182
	return 0;
}
183

184 185 186 187
static int sp804_set_periodic(struct clock_event_device *evt)
{
	unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
			     TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
188

189
	evt_timer_shutdown(evt);
190 191
	writel(common_clkevt->reload, common_clkevt->load);
	writel(ctrl, common_clkevt->ctrl);
192
	return 0;
193 194 195 196 197
}

static int sp804_set_next_event(unsigned long next,
	struct clock_event_device *evt)
{
198 199
	unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
			     TIMER_CTRL_ONESHOT | TIMER_CTRL_ENABLE;
200

201 202
	writel(next, common_clkevt->load);
	writel(ctrl, common_clkevt->ctrl);
203 204 205 206 207

	return 0;
}

static struct clock_event_device sp804_clockevent = {
208 209 210 211 212 213 214 215 216
	.features		= CLOCK_EVT_FEAT_PERIODIC |
				  CLOCK_EVT_FEAT_ONESHOT |
				  CLOCK_EVT_FEAT_DYNIRQ,
	.set_state_shutdown	= sp804_shutdown,
	.set_state_periodic	= sp804_set_periodic,
	.set_state_oneshot	= sp804_shutdown,
	.tick_resume		= sp804_shutdown,
	.set_next_event		= sp804_set_next_event,
	.rating			= 300,
217 218
};

219 220
int __init sp804_clockevents_init(void __iomem *base, unsigned int irq,
				  struct clk *clk, const char *name)
221 222
{
	struct clock_event_device *evt = &sp804_clockevent;
223 224
	long rate;

225
	rate = sp804_get_clock_rate(clk, name);
226
	if (rate < 0)
227
		return -EINVAL;
228

229 230
	common_clkevt = sp804_clkevt_get(base);
	common_clkevt->reload = DIV_ROUND_CLOSEST(rate, HZ);
231 232
	evt->name = name;
	evt->irq = irq;
233
	evt->cpumask = cpu_possible_mask;
234

235
	writel(0, common_clkevt->ctrl);
236

237 238 239
	if (request_irq(irq, sp804_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
			"timer", &sp804_clockevent))
		pr_err("%s: request_irq() failed\n", "timer");
240
	clockevents_config_and_register(evt, rate, 0xf, 0xffffffff);
241 242

	return 0;
243
}
244

245 246 247 248 249 250 251 252 253 254 255 256
static void __init sp804_clkevt_init(struct sp804_timer *timer, void __iomem *base)
{
	int i;

	for (i = 0; i < NR_TIMERS; i++) {
		void __iomem *timer_base;
		struct sp804_clkevt *clkevt;

		timer_base = base + timer->timer_base[i];
		clkevt = &sp804_clkevt[i];
		clkevt->base	= timer_base;
		clkevt->load	= timer_base + timer->load;
257
		clkevt->load_h	= timer_base + timer->load_h;
258
		clkevt->value	= timer_base + timer->value;
259
		clkevt->value_h	= timer_base + timer->value_h;
260 261 262 263 264 265 266
		clkevt->ctrl	= timer_base + timer->ctrl;
		clkevt->intclr	= timer_base + timer->intclr;
		clkevt->width	= timer->width;
	}
}

static int __init sp804_of_init(struct device_node *np, struct sp804_timer *timer)
267 268 269
{
	static bool initialized = false;
	void __iomem *base;
270 271
	void __iomem *timer1_base;
	void __iomem *timer2_base;
272
	int irq, ret = -EINVAL;
273 274 275 276
	u32 irq_num = 0;
	struct clk *clk1, *clk2;
	const char *name = of_get_property(np, "compatible", NULL);

277 278 279 280 281
	if (initialized) {
		pr_debug("%pOF: skipping further SP804 timer device\n", np);
		return 0;
	}

282
	base = of_iomap(np, 0);
283 284
	if (!base)
		return -ENXIO;
285

286 287
	timer1_base = base + timer->timer_base[0];
	timer2_base = base + timer->timer_base[1];
288

289
	/* Ensure timers are disabled */
290 291
	writel(0, timer1_base + timer->ctrl);
	writel(0, timer2_base + timer->ctrl);
292 293 294 295 296

	clk1 = of_clk_get(np, 0);
	if (IS_ERR(clk1))
		clk1 = NULL;

297
	/* Get the 2nd clock if the timer has 3 timer clocks */
298
	if (of_clk_get_parent_count(np) == 3) {
299 300
		clk2 = of_clk_get(np, 1);
		if (IS_ERR(clk2)) {
301
			pr_err("sp804: %pOFn clock not found: %d\n", np,
302
				(int)PTR_ERR(clk2));
303
			clk2 = NULL;
304 305 306 307 308 309 310 311
		}
	} else
		clk2 = clk1;

	irq = irq_of_parse_and_map(np, 0);
	if (irq <= 0)
		goto err;

312 313
	sp804_clkevt_init(timer, base);

314 315
	of_property_read_u32(np, "arm,sp804-has-irq", &irq_num);
	if (irq_num == 2) {
316

317
		ret = sp804_clockevents_init(timer2_base, irq, clk2, name);
318 319 320
		if (ret)
			goto err;

321
		ret = sp804_clocksource_and_sched_clock_init(timer1_base,
322
							     name, clk1, 1);
323 324
		if (ret)
			goto err;
325
	} else {
326

327
		ret = sp804_clockevents_init(timer1_base, irq, clk1, name);
328 329 330
		if (ret)
			goto err;

331
		ret = sp804_clocksource_and_sched_clock_init(timer2_base,
332
							     name, clk2, 1);
333 334
		if (ret)
			goto err;
335 336 337
	}
	initialized = true;

338
	return 0;
339 340
err:
	iounmap(base);
341
	return ret;
342
}
343 344 345 346 347 348

static int __init arm_sp804_of_init(struct device_node *np)
{
	return sp804_of_init(np, &arm_sp804_timer);
}
TIMER_OF_DECLARE(sp804, "arm,sp804", arm_sp804_of_init);
349

350 351 352 353 354 355
static int __init hisi_sp804_of_init(struct device_node *np)
{
	return sp804_of_init(np, &hisi_sp804_timer);
}
TIMER_OF_DECLARE(hisi_sp804, "hisilicon,sp804", hisi_sp804_of_init);

356
static int __init integrator_cp_of_init(struct device_node *np)
357 358 359
{
	static int init_count = 0;
	void __iomem *base;
360
	int irq, ret = -EINVAL;
361
	const char *name = of_get_property(np, "compatible", NULL);
362
	struct clk *clk;
363 364

	base = of_iomap(np, 0);
365
	if (!base) {
366
		pr_err("Failed to iomap\n");
367 368 369
		return -ENXIO;
	}

370
	clk = of_clk_get(np, 0);
371
	if (IS_ERR(clk)) {
372
		pr_err("Failed to get clock\n");
373 374
		return PTR_ERR(clk);
	}
375 376

	/* Ensure timer is disabled */
377
	writel(0, base + arm_sp804_timer.ctrl);
378 379 380 381

	if (init_count == 2 || !of_device_is_available(np))
		goto err;

382 383
	sp804_clkevt_init(&arm_sp804_timer, base);

384
	if (!init_count) {
385 386
		ret = sp804_clocksource_and_sched_clock_init(base,
							     name, clk, 0);
387 388 389
		if (ret)
			goto err;
	} else {
390 391 392 393
		irq = irq_of_parse_and_map(np, 0);
		if (irq <= 0)
			goto err;

394
		ret = sp804_clockevents_init(base, irq, clk, name);
395 396
		if (ret)
			goto err;
397 398 399
	}

	init_count++;
400
	return 0;
401 402
err:
	iounmap(base);
403
	return ret;
404
}
405
TIMER_OF_DECLARE(intcp, "arm,integrator-cp-timer", integrator_cp_of_init);