timer-sp804.c 8.6 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 27 28 29 30 31 32 33 34
/* 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
#define HISI_TIMER_VALUE	0x08
#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 36 37 38 39 40 41 42 43
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,
};

44 45 46 47 48 49 50 51 52
struct sp804_timer __initdata hisi_sp804_timer = {
	.load		= HISI_TIMER_LOAD,
	.value		= HISI_TIMER_VALUE,
	.ctrl		= HISI_TIMER_CTRL,
	.intclr		= HISI_TIMER_INTCLR,
	.timer_base	= {HISI_TIMER_1_BASE, HISI_TIMER_2_BASE},
	.width		= 64,
};

53 54
static struct sp804_clkevt sp804_clkevt[NR_TIMERS];

55
static long __init sp804_get_clock_rate(struct clk *clk, const char *name)
56 57 58 59
{
	long rate;
	int err;

60 61 62 63 64 65 66
	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);
	}

67 68
	err = clk_prepare(clk);
	if (err) {
69
		pr_err("sp804: clock failed to prepare: %d\n", err);
70 71 72 73
		clk_put(clk);
		return err;
	}

74 75
	err = clk_enable(clk);
	if (err) {
76
		pr_err("sp804: clock failed to enable: %d\n", err);
77
		clk_unprepare(clk);
78 79 80 81 82 83
		clk_put(clk);
		return err;
	}

	rate = clk_get_rate(clk);
	if (rate < 0) {
84
		pr_err("sp804: clock failed to get rate: %ld\n", rate);
85
		clk_disable(clk);
86
		clk_unprepare(clk);
87 88 89 90 91 92
		clk_put(clk);
	}

	return rate;
}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
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;
109

110
static u64 notrace sp804_read(void)
111
{
112
	return ~readl_relaxed(sched_clkevt->value);
113 114
}

115 116 117 118
int __init sp804_clocksource_and_sched_clock_init(void __iomem *base,
						  const char *name,
						  struct clk *clk,
						  int use_sched_clock)
119
{
120
	long rate;
121
	struct sp804_clkevt *clkevt;
122

123
	rate = sp804_get_clock_rate(clk, name);
124
	if (rate < 0)
125
		return -EINVAL;
126

127 128 129 130 131
	clkevt = sp804_clkevt_get(base);

	writel(0, clkevt->ctrl);
	writel(0xffffffff, clkevt->load);
	writel(0xffffffff, clkevt->value);
132
	writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
133
		clkevt->ctrl);
134

135
	clocksource_mmio_init(clkevt->value, name,
136
		rate, 200, 32, clocksource_mmio_readl_down);
137 138

	if (use_sched_clock) {
139
		sched_clkevt = clkevt;
140
		sched_clock_register(sp804_read, 32, rate);
141
	}
142 143

	return 0;
144 145 146
}


147
static struct sp804_clkevt *common_clkevt;
148 149 150 151 152 153 154 155 156

/*
 * 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 */
157
	writel(1, common_clkevt->intclr);
158 159 160 161 162 163

	evt->event_handler(evt);

	return IRQ_HANDLED;
}

164
static inline void timer_shutdown(struct clock_event_device *evt)
165
{
166
	writel(0, common_clkevt->ctrl);
167
}
168

169 170 171 172 173
static int sp804_shutdown(struct clock_event_device *evt)
{
	timer_shutdown(evt);
	return 0;
}
174

175 176 177 178
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;
179

180
	timer_shutdown(evt);
181 182
	writel(common_clkevt->reload, common_clkevt->load);
	writel(ctrl, common_clkevt->ctrl);
183
	return 0;
184 185 186 187 188
}

static int sp804_set_next_event(unsigned long next,
	struct clock_event_device *evt)
{
189 190
	unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE |
			     TIMER_CTRL_ONESHOT | TIMER_CTRL_ENABLE;
191

192 193
	writel(next, common_clkevt->load);
	writel(ctrl, common_clkevt->ctrl);
194 195 196 197 198

	return 0;
}

static struct clock_event_device sp804_clockevent = {
199 200 201 202 203 204 205 206 207
	.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,
208 209
};

210 211
int __init sp804_clockevents_init(void __iomem *base, unsigned int irq,
				  struct clk *clk, const char *name)
212 213
{
	struct clock_event_device *evt = &sp804_clockevent;
214 215
	long rate;

216
	rate = sp804_get_clock_rate(clk, name);
217
	if (rate < 0)
218
		return -EINVAL;
219

220 221
	common_clkevt = sp804_clkevt_get(base);
	common_clkevt->reload = DIV_ROUND_CLOSEST(rate, HZ);
222 223
	evt->name = name;
	evt->irq = irq;
224
	evt->cpumask = cpu_possible_mask;
225

226
	writel(0, common_clkevt->ctrl);
227

228 229 230
	if (request_irq(irq, sp804_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
			"timer", &sp804_clockevent))
		pr_err("%s: request_irq() failed\n", "timer");
231
	clockevents_config_and_register(evt, rate, 0xf, 0xffffffff);
232 233

	return 0;
234
}
235

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
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;
		clkevt->value	= timer_base + timer->value;
		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)
256 257 258
{
	static bool initialized = false;
	void __iomem *base;
259 260
	void __iomem *timer1_base;
	void __iomem *timer2_base;
261
	int irq, ret = -EINVAL;
262 263 264 265 266
	u32 irq_num = 0;
	struct clk *clk1, *clk2;
	const char *name = of_get_property(np, "compatible", NULL);

	base = of_iomap(np, 0);
267 268
	if (!base)
		return -ENXIO;
269

270 271
	timer1_base = base + timer->timer_base[0];
	timer2_base = base + timer->timer_base[1];
272

273
	/* Ensure timers are disabled */
274 275
	writel(0, timer1_base + timer->ctrl);
	writel(0, timer2_base + timer->ctrl);
276

277 278
	if (initialized || !of_device_is_available(np)) {
		ret = -EINVAL;
279
		goto err;
280
	}
281 282 283 284 285

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

286
	/* Get the 2nd clock if the timer has 3 timer clocks */
287
	if (of_clk_get_parent_count(np) == 3) {
288 289
		clk2 = of_clk_get(np, 1);
		if (IS_ERR(clk2)) {
290
			pr_err("sp804: %pOFn clock not found: %d\n", np,
291
				(int)PTR_ERR(clk2));
292
			clk2 = NULL;
293 294 295 296 297 298 299 300
		}
	} else
		clk2 = clk1;

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

301 302
	sp804_clkevt_init(timer, base);

303 304
	of_property_read_u32(np, "arm,sp804-has-irq", &irq_num);
	if (irq_num == 2) {
305

306
		ret = sp804_clockevents_init(timer2_base, irq, clk2, name);
307 308 309
		if (ret)
			goto err;

310
		ret = sp804_clocksource_and_sched_clock_init(timer1_base,
311
							     name, clk1, 1);
312 313
		if (ret)
			goto err;
314
	} else {
315

316
		ret = sp804_clockevents_init(timer1_base, irq, clk1, name);
317 318 319
		if (ret)
			goto err;

320
		ret = sp804_clocksource_and_sched_clock_init(timer2_base,
321
							     name, clk2, 1);
322 323
		if (ret)
			goto err;
324 325 326
	}
	initialized = true;

327
	return 0;
328 329
err:
	iounmap(base);
330
	return ret;
331
}
332 333 334 335 336 337

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);
338

339 340 341 342 343 344
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);

345
static int __init integrator_cp_of_init(struct device_node *np)
346 347 348
{
	static int init_count = 0;
	void __iomem *base;
349
	int irq, ret = -EINVAL;
350
	const char *name = of_get_property(np, "compatible", NULL);
351
	struct clk *clk;
352 353

	base = of_iomap(np, 0);
354
	if (!base) {
355
		pr_err("Failed to iomap\n");
356 357 358
		return -ENXIO;
	}

359
	clk = of_clk_get(np, 0);
360
	if (IS_ERR(clk)) {
361
		pr_err("Failed to get clock\n");
362 363
		return PTR_ERR(clk);
	}
364 365

	/* Ensure timer is disabled */
366
	writel(0, base + arm_sp804_timer.ctrl);
367 368 369 370

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

371 372
	sp804_clkevt_init(&arm_sp804_timer, base);

373
	if (!init_count) {
374 375
		ret = sp804_clocksource_and_sched_clock_init(base,
							     name, clk, 0);
376 377 378
		if (ret)
			goto err;
	} else {
379 380 381 382
		irq = irq_of_parse_and_map(np, 0);
		if (irq <= 0)
			goto err;

383
		ret = sp804_clockevents_init(base, irq, clk, name);
384 385
		if (ret)
			goto err;
386 387 388
	}

	init_count++;
389
	return 0;
390 391
err:
	iounmap(base);
392
	return ret;
393
}
394
TIMER_OF_DECLARE(intcp, "arm,integrator-cp-timer", integrator_cp_of_init);