Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[deliverable/linux.git] / arch / arm / plat-iop / time.c
CommitLineData
48388b2a
LB
1/*
2 * arch/arm/plat-iop/time.c
3 *
4 * Timer code for IOP32x and IOP33x based systems
5 *
6 * Author: Deepak Saxena <dsaxena@mvista.com>
7 *
8 * Copyright 2002-2003 MontaVista Software Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 */
15
16#include <linux/kernel.h>
17#include <linux/interrupt.h>
18#include <linux/time.h>
19#include <linux/init.h>
20#include <linux/timex.h>
a5542a0f 21#include <linux/sched.h>
fced80c7 22#include <linux/io.h>
a91549a8 23#include <linux/clocksource.h>
469d3044 24#include <linux/clockchips.h>
a09e64fb 25#include <mach/hardware.h>
48388b2a
LB
26#include <asm/irq.h>
27#include <asm/uaccess.h>
28#include <asm/mach/irq.h>
29#include <asm/mach/time.h>
a09e64fb 30#include <mach/time.h>
48388b2a 31
7d633975
LW
32/*
33 * Minimum clocksource/clockevent timer range in seconds
34 */
35#define IOP_MIN_RANGE 4
36
a91549a8
MP
37/*
38 * IOP clocksource (free-running timer 1).
39 */
a5542a0f 40static cycle_t notrace iop_clocksource_read(struct clocksource *unused)
a91549a8
MP
41{
42 return 0xffffffffu - read_tcr1();
43}
44
45static struct clocksource iop_clocksource = {
46 .name = "iop_timer1",
47 .rating = 300,
48 .read = iop_clocksource_read,
49 .mask = CLOCKSOURCE_MASK(32),
50 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
51};
52
345a3229
MP
53/*
54 * IOP sched_clock() implementation via its clocksource.
55 */
56unsigned long long sched_clock(void)
57{
58 cycle_t cyc = iop_clocksource_read(NULL);
59 struct clocksource *cs = &iop_clocksource;
60
61 return clocksource_cyc2ns(cyc, cs->mult, cs->shift);
62}
63
469d3044
MP
64/*
65 * IOP clockevents (interrupting timer 0).
66 */
67static int iop_set_next_event(unsigned long delta,
68 struct clock_event_device *unused)
69{
70 u32 tmr = IOP_TMR_PRIVILEGED | IOP_TMR_RATIO_1_1;
71
72 BUG_ON(delta == 0);
73 write_tmr0(tmr & ~(IOP_TMR_EN | IOP_TMR_RELOAD));
74 write_tcr0(delta);
75 write_tmr0((tmr & ~IOP_TMR_RELOAD) | IOP_TMR_EN);
76
77 return 0;
78}
79
48388b2a 80static unsigned long ticks_per_jiffy;
469d3044
MP
81
82static void iop_set_mode(enum clock_event_mode mode,
83 struct clock_event_device *unused)
84{
85 u32 tmr = read_tmr0();
86
87 switch (mode) {
88 case CLOCK_EVT_MODE_PERIODIC:
89 write_tmr0(tmr & ~IOP_TMR_EN);
90 write_tcr0(ticks_per_jiffy - 1);
91 tmr |= (IOP_TMR_RELOAD | IOP_TMR_EN);
92 break;
93 case CLOCK_EVT_MODE_ONESHOT:
94 /* ->set_next_event sets period and enables timer */
95 tmr &= ~(IOP_TMR_RELOAD | IOP_TMR_EN);
96 break;
97 case CLOCK_EVT_MODE_RESUME:
98 tmr |= IOP_TMR_EN;
99 break;
100 case CLOCK_EVT_MODE_SHUTDOWN:
101 case CLOCK_EVT_MODE_UNUSED:
102 default:
103 tmr &= ~IOP_TMR_EN;
104 break;
105 }
106
107 write_tmr0(tmr);
108}
109
110static struct clock_event_device iop_clockevent = {
111 .name = "iop_timer0",
112 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
113 .rating = 300,
114 .set_next_event = iop_set_next_event,
115 .set_mode = iop_set_mode,
116};
117
48388b2a 118static irqreturn_t
3668b45d 119iop_timer_interrupt(int irq, void *dev_id)
48388b2a 120{
469d3044 121 struct clock_event_device *evt = dev_id;
48388b2a 122
469d3044
MP
123 write_tisr(1);
124 evt->event_handler(evt);
48388b2a
LB
125 return IRQ_HANDLED;
126}
127
3668b45d
DW
128static struct irqaction iop_timer_irq = {
129 .name = "IOP Timer Tick",
130 .handler = iop_timer_interrupt,
b30fabad 131 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
469d3044 132 .dev_id = &iop_clockevent,
48388b2a
LB
133};
134
70c14ff0
DW
135static unsigned long iop_tick_rate;
136unsigned long get_iop_tick_rate(void)
137{
138 return iop_tick_rate;
139}
140EXPORT_SYMBOL(get_iop_tick_rate);
141
3668b45d 142void __init iop_init_time(unsigned long tick_rate)
48388b2a
LB
143{
144 u32 timer_ctl;
145
a692838d 146 ticks_per_jiffy = DIV_ROUND_CLOSEST(tick_rate, HZ);
70c14ff0 147 iop_tick_rate = tick_rate;
48388b2a 148
3668b45d
DW
149 timer_ctl = IOP_TMR_EN | IOP_TMR_PRIVILEGED |
150 IOP_TMR_RELOAD | IOP_TMR_RATIO_1_1;
48388b2a
LB
151
152 /*
469d3044 153 * Set up interrupting clockevent timer 0.
48388b2a 154 */
469d3044
MP
155 write_tmr0(timer_ctl & ~IOP_TMR_EN);
156 setup_irq(IRQ_IOP_TIMER0, &iop_timer_irq);
7d633975
LW
157 clockevents_calc_mult_shift(&iop_clockevent,
158 tick_rate, IOP_MIN_RANGE);
469d3044
MP
159 iop_clockevent.max_delta_ns =
160 clockevent_delta2ns(0xfffffffe, &iop_clockevent);
161 iop_clockevent.min_delta_ns =
162 clockevent_delta2ns(0xf, &iop_clockevent);
163 iop_clockevent.cpumask = cpumask_of(0);
164 clockevents_register_device(&iop_clockevent);
3668b45d 165 write_trr0(ticks_per_jiffy - 1);
469d3044 166 write_tcr0(ticks_per_jiffy - 1);
3668b45d 167 write_tmr0(timer_ctl);
a91549a8
MP
168
169 /*
170 * Set up free-running clocksource timer 1.
171 */
3668b45d 172 write_trr1(0xffffffff);
a91549a8 173 write_tcr1(0xffffffff);
3668b45d 174 write_tmr1(timer_ctl);
7d633975
LW
175 clocksource_calc_mult_shift(&iop_clocksource, tick_rate,
176 IOP_MIN_RANGE);
a91549a8 177 clocksource_register(&iop_clocksource);
48388b2a 178}
This page took 0.314395 seconds and 5 git commands to generate.