time.c 4.2 KB
Newer Older
1 2 3 4 5
/*
 * linux/arch/arm/mach-w90x900/time.c
 *
 * Based on linux/arch/arm/plat-s3c24xx/time.c by Ben Dooks
 *
6
 * Copyright (c) 2009 Nuvoton technology corporation
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * All rights reserved.
 *
 * Wan ZongShun <mcuos.com@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/leds.h>
26 27
#include <linux/clocksource.h>
#include <linux/clockchips.h>
28 29 30 31 32 33 34 35

#include <asm/mach-types.h>
#include <asm/mach/irq.h>
#include <asm/mach/time.h>

#include <mach/map.h>
#include <mach/regs-timer.h>

36 37 38 39 40 41 42 43 44 45 46
#define RESETINT	0x1f
#define PERIOD		(0x01 << 27)
#define ONESHOT		(0x00 << 27)
#define COUNTEN		(0x01 << 30)
#define INTEN		(0x01 << 29)

#define TICKS_PER_SEC	100
#define PRESCALE	0x63 /* Divider = prescale + 1 */

unsigned int timer0_load;

47
static void nuc900_clockevent_setmode(enum clock_event_mode mode,
48
		struct clock_event_device *clk)
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
	unsigned int val;

	val = __raw_readl(REG_TCSR0);
	val &= ~(0x03 << 27);

	switch (mode) {
	case CLOCK_EVT_MODE_PERIODIC:
		__raw_writel(timer0_load, REG_TICR0);
		val |= (PERIOD | COUNTEN | INTEN | PRESCALE);
		break;

	case CLOCK_EVT_MODE_ONESHOT:
		val |= (ONESHOT | COUNTEN | INTEN | PRESCALE);
		break;

	case CLOCK_EVT_MODE_UNUSED:
	case CLOCK_EVT_MODE_SHUTDOWN:
	case CLOCK_EVT_MODE_RESUME:
		break;
	}

	__raw_writel(val, REG_TCSR0);
}

74
static int nuc900_clockevent_setnextevent(unsigned long evt,
75 76 77 78 79 80 81 82 83 84
		struct clock_event_device *clk)
{
	unsigned int val;

	__raw_writel(evt, REG_TICR0);

	val = __raw_readl(REG_TCSR0);
	val |= (COUNTEN | INTEN | PRESCALE);
	__raw_writel(val, REG_TCSR0);

85 86 87
	return 0;
}

88 89
static struct clock_event_device nuc900_clockevent_device = {
	.name		= "nuc900-timer0",
90 91
	.shift		= 32,
	.features	= CLOCK_EVT_MODE_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
92 93
	.set_mode	= nuc900_clockevent_setmode,
	.set_next_event	= nuc900_clockevent_setnextevent,
94 95 96
	.rating		= 300,
};

97 98
/*IRQ handler for the timer*/

99
static irqreturn_t nuc900_timer0_interrupt(int irq, void *dev_id)
100
{
101
	struct clock_event_device *evt = &nuc900_clockevent_device;
102

103
	__raw_writel(0x01, REG_TISR); /* clear TIF0 */
104 105

	evt->event_handler(evt);
106 107 108
	return IRQ_HANDLED;
}

109 110
static struct irqaction nuc900_timer0_irq = {
	.name		= "nuc900-timer0",
111
	.flags		= IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
112
	.handler	= nuc900_timer0_interrupt,
113 114
};

115
static void __init nuc900_clockevents_init(unsigned int rate)
116
{
117 118 119 120 121 122 123 124 125
	nuc900_clockevent_device.mult = div_sc(rate, NSEC_PER_SEC,
					nuc900_clockevent_device.shift);
	nuc900_clockevent_device.max_delta_ns = clockevent_delta2ns(0xffffffff,
					&nuc900_clockevent_device);
	nuc900_clockevent_device.min_delta_ns = clockevent_delta2ns(0xf,
					&nuc900_clockevent_device);
	nuc900_clockevent_device.cpumask = cpumask_of(0);

	clockevents_register_device(&nuc900_clockevent_device);
126 127
}

128
static cycle_t nuc900_get_cycles(struct clocksource *cs)
129
{
130
	return ~__raw_readl(REG_TDR1);
131 132
}

133 134
static struct clocksource clocksource_nuc900 = {
	.name	= "nuc900-timer1",
135
	.rating	= 200,
136
	.read	= nuc900_get_cycles,
137 138 139 140 141
	.mask	= CLOCKSOURCE_MASK(32),
	.shift	= 20,
	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
};

142
static void __init nuc900_clocksource_init(unsigned int rate)
143
{
144 145 146 147 148 149 150 151
	unsigned int val;

	__raw_writel(0xffffffff, REG_TICR1);

	val = __raw_readl(REG_TCSR1);
	val |= (COUNTEN | PERIOD);
	__raw_writel(val, REG_TCSR1);

152 153 154
	clocksource_nuc900.mult =
		clocksource_khz2mult((rate / 1000), clocksource_nuc900.shift);
	clocksource_register(&clocksource_nuc900);
155 156
}

157
static void __init nuc900_timer_init(void)
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
{
	struct clk *ck_ext = clk_get(NULL, "ext");
	unsigned int	rate;

	BUG_ON(IS_ERR(ck_ext));

	rate = clk_get_rate(ck_ext);
	clk_put(ck_ext);
	rate = rate / (PRESCALE + 0x01);

	 /* set a known state */
	__raw_writel(0x00, REG_TCSR0);
	__raw_writel(0x00, REG_TCSR1);
	__raw_writel(RESETINT, REG_TISR);
	timer0_load = (rate / TICKS_PER_SEC);

174
	setup_irq(IRQ_TIMER0, &nuc900_timer0_irq);
175

176 177
	nuc900_clocksource_init(rate);
	nuc900_clockevents_init(rate);
178 179
}

180 181
struct sys_timer nuc900_timer = {
	.init		= nuc900_timer_init,
182
};