time.c 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * linux/arch/arm/mach-mmp/time.c
 *
 *   Support for clocksource and clockevents
 *
 * Copyright (C) 2008 Marvell International Ltd.
 * All rights reserved.
 *
 *   2008-04-11: Jason Chagas <Jason.chagas@marvell.com>
 *   2008-10-08: Bin Yang <bin.yang@marvell.com>
 *
L
Lucas De Marchi 已提交
12
 * The timers module actually includes three timers, each timer with up to
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * three match comparators. Timer #0 is used here in free-running mode as
 * the clock source, and match comparator #1 used as clock event device.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/clockchips.h>

#include <linux/io.h>
#include <linux/irq.h>
H
Haojian Zhuang 已提交
28 29 30
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
31
#include <linux/sched_clock.h>
32 33 34

#include <mach/addr-map.h>
#include <mach/regs-timers.h>
35
#include <mach/regs-apbc.h>
36
#include <mach/irqs.h>
37 38
#include <mach/cputype.h>
#include <asm/mach/time.h>
39 40 41

#include "clock.h"

42 43 44 45 46 47
#ifdef CONFIG_CPU_MMP2
#define MMP_CLOCK_FREQ		6500000
#else
#define MMP_CLOCK_FREQ		3250000
#endif

48 49 50 51 52
#define TIMERS_VIRT_BASE	TIMERS1_VIRT_BASE

#define MAX_DELTA		(0xfffffffe)
#define MIN_DELTA		(16)

H
Haojian Zhuang 已提交
53 54
static void __iomem *mmp_timer_base = TIMERS_VIRT_BASE;

55 56 57 58 59 60 61
/*
 * FIXME: the timer needs some delay to stablize the counter capture
 */
static inline uint32_t timer_read(void)
{
	int delay = 100;

H
Haojian Zhuang 已提交
62
	__raw_writel(1, mmp_timer_base + TMR_CVWR(1));
63 64 65 66

	while (delay--)
		cpu_relax();

H
Haojian Zhuang 已提交
67
	return __raw_readl(mmp_timer_base + TMR_CVWR(1));
68 69
}

70
static u64 notrace mmp_read_sched_clock(void)
71
{
72
	return timer_read();
73 74 75 76 77 78
}

static irqreturn_t timer_interrupt(int irq, void *dev_id)
{
	struct clock_event_device *c = dev_id;

79 80 81
	/*
	 * Clear pending interrupt status.
	 */
H
Haojian Zhuang 已提交
82
	__raw_writel(0x01, mmp_timer_base + TMR_ICR(0));
83 84 85 86

	/*
	 * Disable timer 0.
	 */
H
Haojian Zhuang 已提交
87
	__raw_writel(0x02, mmp_timer_base + TMR_CER);
88

89
	c->event_handler(c);
90

91 92 93 94 95 96
	return IRQ_HANDLED;
}

static int timer_set_next_event(unsigned long delta,
				struct clock_event_device *dev)
{
97
	unsigned long flags;
98 99 100

	local_irq_save(flags);

101 102 103
	/*
	 * Disable timer 0.
	 */
H
Haojian Zhuang 已提交
104
	__raw_writel(0x02, mmp_timer_base + TMR_CER);
105 106 107 108

	/*
	 * Clear and enable timer match 0 interrupt.
	 */
H
Haojian Zhuang 已提交
109 110
	__raw_writel(0x01, mmp_timer_base + TMR_ICR(0));
	__raw_writel(0x01, mmp_timer_base + TMR_IER(0));
111

112 113 114
	/*
	 * Setup new clockevent timer value.
	 */
H
Haojian Zhuang 已提交
115
	__raw_writel(delta - 1, mmp_timer_base + TMR_TN_MM(0, 0));
116 117 118 119

	/*
	 * Enable timer 0.
	 */
H
Haojian Zhuang 已提交
120
	__raw_writel(0x03, mmp_timer_base + TMR_CER);
121 122

	local_irq_restore(flags);
123

124 125 126 127 128 129 130 131 132 133 134 135 136 137
	return 0;
}

static void timer_set_mode(enum clock_event_mode mode,
			   struct clock_event_device *dev)
{
	unsigned long flags;

	local_irq_save(flags);
	switch (mode) {
	case CLOCK_EVT_MODE_ONESHOT:
	case CLOCK_EVT_MODE_UNUSED:
	case CLOCK_EVT_MODE_SHUTDOWN:
		/* disable the matching interrupt */
H
Haojian Zhuang 已提交
138
		__raw_writel(0x00, mmp_timer_base + TMR_IER(0));
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
		break;
	case CLOCK_EVT_MODE_RESUME:
	case CLOCK_EVT_MODE_PERIODIC:
		break;
	}
	local_irq_restore(flags);
}

static struct clock_event_device ckevt = {
	.name		= "clockevent",
	.features	= CLOCK_EVT_FEAT_ONESHOT,
	.rating		= 200,
	.set_next_event	= timer_set_next_event,
	.set_mode	= timer_set_mode,
};

155
static cycle_t clksrc_read(struct clocksource *cs)
156 157 158 159 160 161 162 163 164 165 166 167 168 169
{
	return timer_read();
}

static struct clocksource cksrc = {
	.name		= "clocksource",
	.rating		= 200,
	.read		= clksrc_read,
	.mask		= CLOCKSOURCE_MASK(32),
	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
};

static void __init timer_config(void)
{
H
Haojian Zhuang 已提交
170
	uint32_t ccr = __raw_readl(mmp_timer_base + TMR_CCR);
171

H
Haojian Zhuang 已提交
172
	__raw_writel(0x0, mmp_timer_base + TMR_CER); /* disable */
173

174 175
	ccr &= (cpu_is_mmp2()) ? (TMR_CCR_CS_0(0) | TMR_CCR_CS_1(0)) :
		(TMR_CCR_CS_0(3) | TMR_CCR_CS_1(3));
H
Haojian Zhuang 已提交
176
	__raw_writel(ccr, mmp_timer_base + TMR_CCR);
177

178
	/* set timer 0 to periodic mode, and timer 1 to free-running mode */
H
Haojian Zhuang 已提交
179
	__raw_writel(0x2, mmp_timer_base + TMR_CMR);
180

H
Haojian Zhuang 已提交
181 182 183
	__raw_writel(0x1, mmp_timer_base + TMR_PLCR(0)); /* periodic */
	__raw_writel(0x7, mmp_timer_base + TMR_ICR(0));  /* clear status */
	__raw_writel(0x0, mmp_timer_base + TMR_IER(0));
184

H
Haojian Zhuang 已提交
185 186 187
	__raw_writel(0x0, mmp_timer_base + TMR_PLCR(1)); /* free-running */
	__raw_writel(0x7, mmp_timer_base + TMR_ICR(1));  /* clear status */
	__raw_writel(0x0, mmp_timer_base + TMR_IER(1));
188

189
	/* enable timer 1 counter */
H
Haojian Zhuang 已提交
190
	__raw_writel(0x2, mmp_timer_base + TMR_CER);
191 192 193 194
}

static struct irqaction timer_irq = {
	.name		= "timer",
195
	.flags		= IRQF_TIMER | IRQF_IRQPOLL,
196 197 198 199 200 201 202 203
	.handler	= timer_interrupt,
	.dev_id		= &ckevt,
};

void __init timer_init(int irq)
{
	timer_config();

204
	sched_clock_register(mmp_read_sched_clock, 32, MMP_CLOCK_FREQ);
205 206 207 208 209

	ckevt.cpumask = cpumask_of(0);

	setup_irq(irq, &timer_irq);

210 211
	clocksource_register_hz(&cksrc, MMP_CLOCK_FREQ);
	clockevents_config_and_register(&ckevt, MMP_CLOCK_FREQ,
212
					MIN_DELTA, MAX_DELTA);
213
}
H
Haojian Zhuang 已提交
214 215

#ifdef CONFIG_OF
216
static const struct of_device_id mmp_timer_dt_ids[] = {
H
Haojian Zhuang 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	{ .compatible = "mrvl,mmp-timer", },
	{}
};

void __init mmp_dt_init_timer(void)
{
	struct device_node *np;
	int irq, ret;

	np = of_find_matching_node(NULL, mmp_timer_dt_ids);
	if (!np) {
		ret = -ENODEV;
		goto out;
	}

	irq = irq_of_parse_and_map(np, 0);
	if (!irq) {
		ret = -EINVAL;
		goto out;
	}
	mmp_timer_base = of_iomap(np, 0);
	if (!mmp_timer_base) {
		ret = -ENOMEM;
		goto out;
	}
	timer_init(irq);
	return;
out:
	pr_err("Failed to get timer from device tree with error:%d\n", ret);
}
#endif