blob: 7cdc5161ff2bda7e939c6ebcf1232bd91a964bb1 [file] [log] [blame]
Lennert Buytenhek48388b22006-09-18 23:18:16 +01001/*
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>
Rabin Vincenta5542a02010-12-04 06:20:52 +010021#include <linux/sched.h>
Russell Kingfced80c2008-09-06 12:10:45 +010022#include <linux/io.h>
Mikael Petterssona91549a2009-10-29 11:46:54 -070023#include <linux/clocksource.h>
Mikael Pettersson469d30442009-10-29 11:46:54 -070024#include <linux/clockchips.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010025#include <mach/hardware.h>
Lennert Buytenhek48388b22006-09-18 23:18:16 +010026#include <asm/irq.h>
Russell King08f26b12010-12-15 21:52:10 +000027#include <asm/sched_clock.h>
Lennert Buytenhek48388b22006-09-18 23:18:16 +010028#include <asm/uaccess.h>
29#include <asm/mach/irq.h>
30#include <asm/mach/time.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010031#include <mach/time.h>
Lennert Buytenhek48388b22006-09-18 23:18:16 +010032
Mikael Petterssona91549a2009-10-29 11:46:54 -070033/*
Linus Walleij7d633972010-06-02 09:08:55 +010034 * Minimum clocksource/clockevent timer range in seconds
35 */
36#define IOP_MIN_RANGE 4
37
38/*
Mikael Petterssona91549a2009-10-29 11:46:54 -070039 * IOP clocksource (free-running timer 1).
40 */
Rabin Vincenta5542a02010-12-04 06:20:52 +010041static cycle_t notrace iop_clocksource_read(struct clocksource *unused)
Mikael Petterssona91549a2009-10-29 11:46:54 -070042{
43 return 0xffffffffu - read_tcr1();
44}
45
46static struct clocksource iop_clocksource = {
47 .name = "iop_timer1",
48 .rating = 300,
49 .read = iop_clocksource_read,
50 .mask = CLOCKSOURCE_MASK(32),
51 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
52};
53
Russell King08f26b12010-12-15 21:52:10 +000054static DEFINE_CLOCK_DATA(cd);
55
Mikael Pettersson469d30442009-10-29 11:46:54 -070056/*
Mikael Pettersson345a3222009-10-29 11:46:56 -070057 * IOP sched_clock() implementation via its clocksource.
58 */
Russell King5e06b642010-12-15 19:19:25 +000059unsigned long long notrace sched_clock(void)
Mikael Pettersson345a3222009-10-29 11:46:56 -070060{
Russell King08f26b12010-12-15 21:52:10 +000061 u32 cyc = 0xffffffffu - read_tcr1();
62 return cyc_to_sched_clock(&cd, cyc, (u32)~0);
63}
Mikael Pettersson345a3222009-10-29 11:46:56 -070064
Russell King08f26b12010-12-15 21:52:10 +000065static void notrace iop_update_sched_clock(void)
66{
67 u32 cyc = 0xffffffffu - read_tcr1();
68 update_sched_clock(&cd, cyc, (u32)~0);
Mikael Pettersson345a3222009-10-29 11:46:56 -070069}
70
71/*
Mikael Pettersson469d30442009-10-29 11:46:54 -070072 * IOP clockevents (interrupting timer 0).
73 */
74static int iop_set_next_event(unsigned long delta,
75 struct clock_event_device *unused)
76{
77 u32 tmr = IOP_TMR_PRIVILEGED | IOP_TMR_RATIO_1_1;
78
79 BUG_ON(delta == 0);
80 write_tmr0(tmr & ~(IOP_TMR_EN | IOP_TMR_RELOAD));
81 write_tcr0(delta);
82 write_tmr0((tmr & ~IOP_TMR_RELOAD) | IOP_TMR_EN);
83
84 return 0;
85}
86
Lennert Buytenhek48388b22006-09-18 23:18:16 +010087static unsigned long ticks_per_jiffy;
Mikael Pettersson469d30442009-10-29 11:46:54 -070088
89static void iop_set_mode(enum clock_event_mode mode,
90 struct clock_event_device *unused)
91{
92 u32 tmr = read_tmr0();
93
94 switch (mode) {
95 case CLOCK_EVT_MODE_PERIODIC:
96 write_tmr0(tmr & ~IOP_TMR_EN);
97 write_tcr0(ticks_per_jiffy - 1);
Russell King40cc5242010-12-19 15:43:34 +000098 write_trr0(ticks_per_jiffy - 1);
Mikael Pettersson469d30442009-10-29 11:46:54 -070099 tmr |= (IOP_TMR_RELOAD | IOP_TMR_EN);
100 break;
101 case CLOCK_EVT_MODE_ONESHOT:
102 /* ->set_next_event sets period and enables timer */
103 tmr &= ~(IOP_TMR_RELOAD | IOP_TMR_EN);
104 break;
105 case CLOCK_EVT_MODE_RESUME:
106 tmr |= IOP_TMR_EN;
107 break;
108 case CLOCK_EVT_MODE_SHUTDOWN:
109 case CLOCK_EVT_MODE_UNUSED:
110 default:
111 tmr &= ~IOP_TMR_EN;
112 break;
113 }
114
115 write_tmr0(tmr);
116}
117
118static struct clock_event_device iop_clockevent = {
119 .name = "iop_timer0",
120 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
121 .rating = 300,
122 .set_next_event = iop_set_next_event,
123 .set_mode = iop_set_mode,
124};
125
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100126static irqreturn_t
Dan Williams3668b452007-02-13 17:13:34 +0100127iop_timer_interrupt(int irq, void *dev_id)
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100128{
Mikael Pettersson469d30442009-10-29 11:46:54 -0700129 struct clock_event_device *evt = dev_id;
130
Dan Williams3668b452007-02-13 17:13:34 +0100131 write_tisr(1);
Mikael Pettersson469d30442009-10-29 11:46:54 -0700132 evt->event_handler(evt);
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100133 return IRQ_HANDLED;
134}
135
Dan Williams3668b452007-02-13 17:13:34 +0100136static struct irqaction iop_timer_irq = {
137 .name = "IOP Timer Tick",
138 .handler = iop_timer_interrupt,
Bernhard Walleb30faba2007-05-08 00:35:39 -0700139 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Mikael Pettersson469d30442009-10-29 11:46:54 -0700140 .dev_id = &iop_clockevent,
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100141};
142
Dan Williams70c14ff2007-07-20 02:07:26 +0100143static unsigned long iop_tick_rate;
144unsigned long get_iop_tick_rate(void)
145{
146 return iop_tick_rate;
147}
148EXPORT_SYMBOL(get_iop_tick_rate);
149
Dan Williams3668b452007-02-13 17:13:34 +0100150void __init iop_init_time(unsigned long tick_rate)
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100151{
152 u32 timer_ctl;
153
Russell King08f26b12010-12-15 21:52:10 +0000154 init_sched_clock(&cd, iop_update_sched_clock, 32, tick_rate);
155
Julia Lawalla6928382009-08-02 10:46:45 +0200156 ticks_per_jiffy = DIV_ROUND_CLOSEST(tick_rate, HZ);
Dan Williams70c14ff2007-07-20 02:07:26 +0100157 iop_tick_rate = tick_rate;
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100158
Dan Williams3668b452007-02-13 17:13:34 +0100159 timer_ctl = IOP_TMR_EN | IOP_TMR_PRIVILEGED |
160 IOP_TMR_RELOAD | IOP_TMR_RATIO_1_1;
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100161
162 /*
Mikael Pettersson469d30442009-10-29 11:46:54 -0700163 * Set up interrupting clockevent timer 0.
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100164 */
Mikael Pettersson469d30442009-10-29 11:46:54 -0700165 write_tmr0(timer_ctl & ~IOP_TMR_EN);
Russell King40cc5242010-12-19 15:43:34 +0000166 write_tisr(1);
Mikael Pettersson469d30442009-10-29 11:46:54 -0700167 setup_irq(IRQ_IOP_TIMER0, &iop_timer_irq);
Linus Walleij7d633972010-06-02 09:08:55 +0100168 clockevents_calc_mult_shift(&iop_clockevent,
169 tick_rate, IOP_MIN_RANGE);
Mikael Pettersson469d30442009-10-29 11:46:54 -0700170 iop_clockevent.max_delta_ns =
171 clockevent_delta2ns(0xfffffffe, &iop_clockevent);
172 iop_clockevent.min_delta_ns =
173 clockevent_delta2ns(0xf, &iop_clockevent);
174 iop_clockevent.cpumask = cpumask_of(0);
175 clockevents_register_device(&iop_clockevent);
Mikael Petterssona91549a2009-10-29 11:46:54 -0700176
177 /*
178 * Set up free-running clocksource timer 1.
179 */
Dan Williams3668b452007-02-13 17:13:34 +0100180 write_trr1(0xffffffff);
Mikael Petterssona91549a2009-10-29 11:46:54 -0700181 write_tcr1(0xffffffff);
Dan Williams3668b452007-02-13 17:13:34 +0100182 write_tmr1(timer_ctl);
Russell Kingd28b116b2010-12-13 13:20:23 +0000183 clocksource_register_hz(&iop_clocksource, tick_rate);
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100184}