blob: 85d3e55ca4a9c293c67d4b2557bb52d90f93d22f [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>
Russell Kingfced80c2008-09-06 12:10:45 +010021#include <linux/io.h>
Mikael Petterssona91549a2009-10-29 11:46:54 -070022#include <linux/clocksource.h>
Mikael Pettersson469d30442009-10-29 11:46:54 -070023#include <linux/clockchips.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010024#include <mach/hardware.h>
Lennert Buytenhek48388b22006-09-18 23:18:16 +010025#include <asm/irq.h>
26#include <asm/uaccess.h>
27#include <asm/mach/irq.h>
28#include <asm/mach/time.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/time.h>
Lennert Buytenhek48388b22006-09-18 23:18:16 +010030
Mikael Petterssona91549a2009-10-29 11:46:54 -070031/*
Linus Walleij7d633972010-06-02 09:08:55 +010032 * Minimum clocksource/clockevent timer range in seconds
33 */
34#define IOP_MIN_RANGE 4
35
36/*
Mikael Petterssona91549a2009-10-29 11:46:54 -070037 * IOP clocksource (free-running timer 1).
38 */
39static cycle_t iop_clocksource_read(struct clocksource *unused)
40{
41 return 0xffffffffu - read_tcr1();
42}
43
44static struct clocksource iop_clocksource = {
45 .name = "iop_timer1",
46 .rating = 300,
47 .read = iop_clocksource_read,
48 .mask = CLOCKSOURCE_MASK(32),
49 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
50};
51
Mikael Pettersson469d30442009-10-29 11:46:54 -070052/*
Mikael Pettersson345a3222009-10-29 11:46:56 -070053 * IOP sched_clock() implementation via its clocksource.
54 */
55unsigned long long sched_clock(void)
56{
57 cycle_t cyc = iop_clocksource_read(NULL);
58 struct clocksource *cs = &iop_clocksource;
59
60 return clocksource_cyc2ns(cyc, cs->mult, cs->shift);
61}
62
63/*
Mikael Pettersson469d30442009-10-29 11:46:54 -070064 * IOP clockevents (interrupting timer 0).
65 */
66static int iop_set_next_event(unsigned long delta,
67 struct clock_event_device *unused)
68{
69 u32 tmr = IOP_TMR_PRIVILEGED | IOP_TMR_RATIO_1_1;
70
71 BUG_ON(delta == 0);
72 write_tmr0(tmr & ~(IOP_TMR_EN | IOP_TMR_RELOAD));
73 write_tcr0(delta);
74 write_tmr0((tmr & ~IOP_TMR_RELOAD) | IOP_TMR_EN);
75
76 return 0;
77}
78
Lennert Buytenhek48388b22006-09-18 23:18:16 +010079static unsigned long ticks_per_jiffy;
Mikael Pettersson469d30442009-10-29 11:46:54 -070080
81static void iop_set_mode(enum clock_event_mode mode,
82 struct clock_event_device *unused)
83{
84 u32 tmr = read_tmr0();
85
86 switch (mode) {
87 case CLOCK_EVT_MODE_PERIODIC:
88 write_tmr0(tmr & ~IOP_TMR_EN);
89 write_tcr0(ticks_per_jiffy - 1);
90 tmr |= (IOP_TMR_RELOAD | IOP_TMR_EN);
91 break;
92 case CLOCK_EVT_MODE_ONESHOT:
93 /* ->set_next_event sets period and enables timer */
94 tmr &= ~(IOP_TMR_RELOAD | IOP_TMR_EN);
95 break;
96 case CLOCK_EVT_MODE_RESUME:
97 tmr |= IOP_TMR_EN;
98 break;
99 case CLOCK_EVT_MODE_SHUTDOWN:
100 case CLOCK_EVT_MODE_UNUSED:
101 default:
102 tmr &= ~IOP_TMR_EN;
103 break;
104 }
105
106 write_tmr0(tmr);
107}
108
109static struct clock_event_device iop_clockevent = {
110 .name = "iop_timer0",
111 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
112 .rating = 300,
113 .set_next_event = iop_set_next_event,
114 .set_mode = iop_set_mode,
115};
116
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100117static irqreturn_t
Dan Williams3668b452007-02-13 17:13:34 +0100118iop_timer_interrupt(int irq, void *dev_id)
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100119{
Mikael Pettersson469d30442009-10-29 11:46:54 -0700120 struct clock_event_device *evt = dev_id;
121
Dan Williams3668b452007-02-13 17:13:34 +0100122 write_tisr(1);
Mikael Pettersson469d30442009-10-29 11:46:54 -0700123 evt->event_handler(evt);
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100124 return IRQ_HANDLED;
125}
126
Dan Williams3668b452007-02-13 17:13:34 +0100127static struct irqaction iop_timer_irq = {
128 .name = "IOP Timer Tick",
129 .handler = iop_timer_interrupt,
Bernhard Walleb30faba2007-05-08 00:35:39 -0700130 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Mikael Pettersson469d30442009-10-29 11:46:54 -0700131 .dev_id = &iop_clockevent,
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100132};
133
Dan Williams70c14ff2007-07-20 02:07:26 +0100134static unsigned long iop_tick_rate;
135unsigned long get_iop_tick_rate(void)
136{
137 return iop_tick_rate;
138}
139EXPORT_SYMBOL(get_iop_tick_rate);
140
Dan Williams3668b452007-02-13 17:13:34 +0100141void __init iop_init_time(unsigned long tick_rate)
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100142{
143 u32 timer_ctl;
144
Julia Lawalla6928382009-08-02 10:46:45 +0200145 ticks_per_jiffy = DIV_ROUND_CLOSEST(tick_rate, HZ);
Dan Williams70c14ff2007-07-20 02:07:26 +0100146 iop_tick_rate = tick_rate;
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100147
Dan Williams3668b452007-02-13 17:13:34 +0100148 timer_ctl = IOP_TMR_EN | IOP_TMR_PRIVILEGED |
149 IOP_TMR_RELOAD | IOP_TMR_RATIO_1_1;
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100150
151 /*
Mikael Pettersson469d30442009-10-29 11:46:54 -0700152 * Set up interrupting clockevent timer 0.
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100153 */
Mikael Pettersson469d30442009-10-29 11:46:54 -0700154 write_tmr0(timer_ctl & ~IOP_TMR_EN);
155 setup_irq(IRQ_IOP_TIMER0, &iop_timer_irq);
Linus Walleij7d633972010-06-02 09:08:55 +0100156 clockevents_calc_mult_shift(&iop_clockevent,
157 tick_rate, IOP_MIN_RANGE);
Mikael Pettersson469d30442009-10-29 11:46:54 -0700158 iop_clockevent.max_delta_ns =
159 clockevent_delta2ns(0xfffffffe, &iop_clockevent);
160 iop_clockevent.min_delta_ns =
161 clockevent_delta2ns(0xf, &iop_clockevent);
162 iop_clockevent.cpumask = cpumask_of(0);
163 clockevents_register_device(&iop_clockevent);
Dan Williams3668b452007-02-13 17:13:34 +0100164 write_trr0(ticks_per_jiffy - 1);
Mikael Pettersson469d30442009-10-29 11:46:54 -0700165 write_tcr0(ticks_per_jiffy - 1);
Dan Williams3668b452007-02-13 17:13:34 +0100166 write_tmr0(timer_ctl);
Mikael Petterssona91549a2009-10-29 11:46:54 -0700167
168 /*
169 * Set up free-running clocksource timer 1.
170 */
Dan Williams3668b452007-02-13 17:13:34 +0100171 write_trr1(0xffffffff);
Mikael Petterssona91549a2009-10-29 11:46:54 -0700172 write_tcr1(0xffffffff);
Dan Williams3668b452007-02-13 17:13:34 +0100173 write_tmr1(timer_ctl);
Linus Walleij7d633972010-06-02 09:08:55 +0100174 clocksource_calc_mult_shift(&iop_clocksource, tick_rate,
175 IOP_MIN_RANGE);
Mikael Petterssona91549a2009-10-29 11:46:54 -0700176 clocksource_register(&iop_clocksource);
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100177}