time.c 9.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * DaVinci timer subsystem
 *
 * Author: Kevin Hilman, MontaVista Software, Inc. <source@mvista.com>
 *
 * 2007 (c) MontaVista Software, Inc. This file is licensed under
 * the terms of the GNU General Public License version 2. This program
 * is licensed "as is" without any warranty of any kind, whether express
 * or implied.
 */
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/clocksource.h>
#include <linux/clockchips.h>
#include <linux/spinlock.h>
18
#include <linux/io.h>
19 20 21
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/device.h>
22
#include <linux/platform_device.h>
23

24
#include <mach/hardware.h>
25 26 27 28 29
#include <asm/system.h>
#include <asm/irq.h>
#include <asm/mach/irq.h>
#include <asm/mach/time.h>
#include <asm/errno.h>
30
#include <mach/io.h>
31
#include <mach/cputype.h>
32
#include <mach/time.h>
33
#include "clock.h"
34 35

static struct clock_event_device clockevent_davinci;
36
static unsigned int davinci_clock_tick_rate;
37 38 39 40 41 42 43

#define DAVINCI_WDOG_BASE   (IO_PHYS + 0x21C00)

/*
 * This driver configures the 2 64-bit count-up timers as 4 independent
 * 32-bit count-up timers used as follows:
 */
44 45 46 47 48

enum {
	TID_CLOCKEVENT,
	TID_CLOCKSOURCE,
};
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

/* Timer register offsets */
#define PID12                        0x0
#define TIM12                        0x10
#define TIM34                        0x14
#define PRD12                        0x18
#define PRD34                        0x1c
#define TCR                          0x20
#define TGCR                         0x24
#define WDTCR                        0x28

/* Timer register bitfields */
#define TCR_ENAMODE_DISABLE          0x0
#define TCR_ENAMODE_ONESHOT          0x1
#define TCR_ENAMODE_PERIODIC         0x2
#define TCR_ENAMODE_MASK             0x3

#define TGCR_TIMMODE_SHIFT           2
#define TGCR_TIMMODE_64BIT_GP        0x0
#define TGCR_TIMMODE_32BIT_UNCHAINED 0x1
#define TGCR_TIMMODE_64BIT_WDOG      0x2
#define TGCR_TIMMODE_32BIT_CHAINED   0x3

#define TGCR_TIM12RS_SHIFT           0
#define TGCR_TIM34RS_SHIFT           1
#define TGCR_RESET                   0x0
#define TGCR_UNRESET                 0x1
#define TGCR_RESET_MASK              0x3

#define WDTCR_WDEN_SHIFT             14
#define WDTCR_WDEN_DISABLE           0x0
#define WDTCR_WDEN_ENABLE            0x1
#define WDTCR_WDKEY_SHIFT            16
#define WDTCR_WDKEY_SEQ0             0xa5c6
#define WDTCR_WDKEY_SEQ1             0xda7e

struct timer_s {
	char *name;
	unsigned int id;
	unsigned long period;
	unsigned long opts;
90 91 92
	void __iomem *base;
	unsigned long tim_off;
	unsigned long prd_off;
93 94 95 96 97 98 99 100 101 102
	unsigned long enamode_shift;
	struct irqaction irqaction;
};
static struct timer_s timers[];

/* values for 'opts' field of struct timer_s */
#define TIMER_OPTS_DISABLED   0x00
#define TIMER_OPTS_ONESHOT    0x01
#define TIMER_OPTS_PERIODIC   0x02

103 104 105 106 107 108 109
static char *id_to_name[] = {
	[T0_BOT]	= "timer0_0",
	[T0_TOP]	= "timer0_1",
	[T1_BOT]	= "timer1_0",
	[T1_TOP]	= "timer1_1",
};

110 111
static int timer32_config(struct timer_s *t)
{
112
	u32 tcr = __raw_readl(t->base + TCR);
113 114 115

	/* disable timer */
	tcr &= ~(TCR_ENAMODE_MASK << t->enamode_shift);
116
	__raw_writel(tcr, t->base + TCR);
117 118

	/* reset counter to zero, set new period */
119 120
	__raw_writel(0, t->base + t->tim_off);
	__raw_writel(t->period, t->base + t->prd_off);
121 122 123 124 125 126 127 128

	/* Set enable mode */
	if (t->opts & TIMER_OPTS_ONESHOT) {
		tcr |= TCR_ENAMODE_ONESHOT << t->enamode_shift;
	} else if (t->opts & TIMER_OPTS_PERIODIC) {
		tcr |= TCR_ENAMODE_PERIODIC << t->enamode_shift;
	}

129
	__raw_writel(tcr, t->base + TCR);
130 131 132 133 134
	return 0;
}

static inline u32 timer32_read(struct timer_s *t)
{
135
	return __raw_readl(t->base + t->tim_off);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
}

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

	evt->event_handler(evt);
	return IRQ_HANDLED;
}

/* called when 32-bit counter wraps */
static irqreturn_t freerun_interrupt(int irq, void *dev_id)
{
	return IRQ_HANDLED;
}

static struct timer_s timers[] = {
	[TID_CLOCKEVENT] = {
		.name      = "clockevent",
		.opts      = TIMER_OPTS_DISABLED,
		.irqaction = {
			.flags   = IRQF_DISABLED | IRQF_TIMER,
			.handler = timer_interrupt,
		}
	},
	[TID_CLOCKSOURCE] = {
		.name       = "free-run counter",
		.period     = ~0,
		.opts       = TIMER_OPTS_PERIODIC,
		.irqaction = {
			.flags   = IRQF_DISABLED | IRQF_TIMER,
			.handler = freerun_interrupt,
		}
	},
};

static void __init timer_init(void)
{
174 175
	struct davinci_soc_info *soc_info = &davinci_soc_info;
	struct davinci_timer_instance *dtip = soc_info->timer_info->timers;
176 177 178 179
	int i;

	/* Global init of each 64-bit timer as a whole */
	for(i=0; i<2; i++) {
180
		u32 tgcr;
181
		void __iomem *base = dtip[i].base;
182 183

		/* Disabled, Internal clock source */
184
		__raw_writel(0, base + TCR);
185 186 187

		/* reset both timers, no pre-scaler for timer34 */
		tgcr = 0;
188
		__raw_writel(tgcr, base + TGCR);
189 190 191

		/* Set both timers to unchained 32-bit */
		tgcr = TGCR_TIMMODE_32BIT_UNCHAINED << TGCR_TIMMODE_SHIFT;
192
		__raw_writel(tgcr, base + TGCR);
193 194 195 196

		/* Unreset timers */
		tgcr |= (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) |
			(TGCR_UNRESET << TGCR_TIM34RS_SHIFT);
197
		__raw_writel(tgcr, base + TGCR);
198 199

		/* Init both counters to zero */
200 201
		__raw_writel(0, base + TIM12);
		__raw_writel(0, base + TIM34);
202 203 204 205 206
	}

	/* Init of each timer as a 32-bit timer */
	for (i=0; i< ARRAY_SIZE(timers); i++) {
		struct timer_s *t = &timers[i];
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
		int timer = ID_TO_TIMER(t->id);
		u32 irq;

		t->base = dtip[timer].base;

		if (IS_TIMER_BOT(t->id)) {
			t->enamode_shift = 6;
			t->tim_off = TIM12;
			t->prd_off = PRD12;
			irq = dtip[timer].bottom_irq;
		} else {
			t->enamode_shift = 22;
			t->tim_off = TIM34;
			t->prd_off = PRD34;
			irq = dtip[timer].top_irq;
222
		}
223 224 225 226 227 228 229 230

		/* Register interrupt */
		t->irqaction.name = t->name;
		t->irqaction.dev_id = (void *)t;
		if (t->irqaction.handler != NULL)
			setup_irq(irq, &t->irqaction);

		timer32_config(&timers[i]);
231 232 233 234 235 236
	}
}

/*
 * clocksource
 */
237
static cycle_t read_cycles(struct clocksource *cs)
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
{
	struct timer_s *t = &timers[TID_CLOCKSOURCE];

	return (cycles_t)timer32_read(t);
}

static struct clocksource clocksource_davinci = {
	.rating		= 300,
	.read		= read_cycles,
	.mask		= CLOCKSOURCE_MASK(32),
	.shift		= 24,
	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
};

/*
 * clockevent
 */
static int davinci_set_next_event(unsigned long cycles,
				  struct clock_event_device *evt)
{
	struct timer_s *t = &timers[TID_CLOCKEVENT];

	t->period = cycles;
	timer32_config(t);
	return 0;
}

static void davinci_set_mode(enum clock_event_mode mode,
			     struct clock_event_device *evt)
{
	struct timer_s *t = &timers[TID_CLOCKEVENT];

	switch (mode) {
	case CLOCK_EVT_MODE_PERIODIC:
272
		t->period = davinci_clock_tick_rate / (HZ);
273 274 275 276 277 278 279 280 281 282
		t->opts = TIMER_OPTS_PERIODIC;
		timer32_config(t);
		break;
	case CLOCK_EVT_MODE_ONESHOT:
		t->opts = TIMER_OPTS_ONESHOT;
		break;
	case CLOCK_EVT_MODE_UNUSED:
	case CLOCK_EVT_MODE_SHUTDOWN:
		t->opts = TIMER_OPTS_DISABLED;
		break;
T
Thomas Gleixner 已提交
283 284
	case CLOCK_EVT_MODE_RESUME:
		break;
285 286 287 288 289 290 291 292 293 294 295 296 297
	}
}

static struct clock_event_device clockevent_davinci = {
	.features       = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
	.shift		= 32,
	.set_next_event	= davinci_set_next_event,
	.set_mode	= davinci_set_mode,
};


static void __init davinci_timer_init(void)
{
298
	struct clk *timer_clk;
299
	struct davinci_soc_info *soc_info = &davinci_soc_info;
300

301 302 303
	static char err[] __initdata = KERN_ERR
		"%s: can't register clocksource!\n";

304 305 306
	timers[TID_CLOCKEVENT].id = soc_info->timer_info->clockevent_id;
	timers[TID_CLOCKSOURCE].id = soc_info->timer_info->clocksource_id;

307 308 309
	/* init timer hw */
	timer_init();

310 311 312 313 314 315
	timer_clk = clk_get(NULL, "timer0");
	BUG_ON(IS_ERR(timer_clk));
	clk_enable(timer_clk);

	davinci_clock_tick_rate = clk_get_rate(timer_clk);

316
	/* setup clocksource */
317
	clocksource_davinci.name = id_to_name[timers[TID_CLOCKSOURCE].id];
318
	clocksource_davinci.mult =
319
		clocksource_khz2mult(davinci_clock_tick_rate/1000,
320 321 322 323 324
				     clocksource_davinci.shift);
	if (clocksource_register(&clocksource_davinci))
		printk(err, clocksource_davinci.name);

	/* setup clockevent */
325
	clockevent_davinci.name = id_to_name[timers[TID_CLOCKEVENT].id];
326
	clockevent_davinci.mult = div_sc(davinci_clock_tick_rate, NSEC_PER_SEC,
327 328 329 330 331 332
					 clockevent_davinci.shift);
	clockevent_davinci.max_delta_ns =
		clockevent_delta2ns(0xfffffffe, &clockevent_davinci);
	clockevent_davinci.min_delta_ns =
		clockevent_delta2ns(1, &clockevent_davinci);

333
	clockevent_davinci.cpumask = cpumask_of(0);
334 335 336 337 338 339 340 341 342
	clockevents_register_device(&clockevent_davinci);
}

struct sys_timer davinci_timer = {
	.init   = davinci_timer_init,
};


/* reset board using watchdog timer */
343 344
void davinci_watchdog_reset(void)
{
345 346
	u32 tgcr, wdtcr;
	void __iomem *base = IO_ADDRESS(DAVINCI_WDOG_BASE);
347 348
	struct clk *wd_clk;

349
	wd_clk = clk_get(&davinci_wdt_device.dev, NULL);
350 351 352
	if (WARN_ON(IS_ERR(wd_clk)))
		return;
	clk_enable(wd_clk);
353 354

	/* disable, internal clock source */
355
	__raw_writel(0, base + TCR);
356 357 358

	/* reset timer, set mode to 64-bit watchdog, and unreset */
	tgcr = 0;
359
	__raw_writel(tgcr, base + TCR);
360 361 362
	tgcr = TGCR_TIMMODE_64BIT_WDOG << TGCR_TIMMODE_SHIFT;
	tgcr |= (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) |
		(TGCR_UNRESET << TGCR_TIM34RS_SHIFT);
363
	__raw_writel(tgcr, base + TCR);
364 365

	/* clear counter and period regs */
366 367 368 369
	__raw_writel(0, base + TIM12);
	__raw_writel(0, base + TIM34);
	__raw_writel(0, base + PRD12);
	__raw_writel(0, base + PRD34);
370 371

	/* enable */
372
	wdtcr = __raw_readl(base + WDTCR);
373
	wdtcr |= WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT;
374
	__raw_writel(wdtcr, base + WDTCR);
375 376 377 378

	/* put watchdog in pre-active state */
	wdtcr = (WDTCR_WDKEY_SEQ0 << WDTCR_WDKEY_SHIFT) |
		(WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT);
379
	__raw_writel(wdtcr, base + WDTCR);
380 381 382 383

	/* put watchdog in active state */
	wdtcr = (WDTCR_WDKEY_SEQ1 << WDTCR_WDKEY_SHIFT) |
		(WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT);
384
	__raw_writel(wdtcr, base + WDTCR);
385 386 387 388

	/* write an invalid value to the WDKEY field to trigger
	 * a watchdog reset */
	wdtcr = 0x00004000;
389
	__raw_writel(wdtcr, base + WDTCR);
390
}