blob: 6c8a02ad98e33f9a374fd62c58c5ea7630427f0c [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/*
32 * IOP clocksource (free-running timer 1).
33 */
34static cycle_t iop_clocksource_read(struct clocksource *unused)
35{
36 return 0xffffffffu - read_tcr1();
37}
38
39static struct clocksource iop_clocksource = {
40 .name = "iop_timer1",
41 .rating = 300,
42 .read = iop_clocksource_read,
43 .mask = CLOCKSOURCE_MASK(32),
44 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
45};
46
47static void __init iop_clocksource_set_hz(struct clocksource *cs, unsigned int hz)
48{
49 u64 temp;
50 u32 shift;
51
52 /* Find shift and mult values for hz. */
53 shift = 32;
54 do {
55 temp = (u64) NSEC_PER_SEC << shift;
56 do_div(temp, hz);
57 if ((temp >> 32) == 0)
58 break;
59 } while (--shift != 0);
60
61 cs->shift = shift;
62 cs->mult = (u32) temp;
63
64 printk(KERN_INFO "clocksource: %s uses shift %u mult %#x\n",
65 cs->name, cs->shift, cs->mult);
66}
67
Mikael Pettersson469d30442009-10-29 11:46:54 -070068/*
Mikael Pettersson345a3222009-10-29 11:46:56 -070069 * IOP sched_clock() implementation via its clocksource.
70 */
71unsigned long long sched_clock(void)
72{
73 cycle_t cyc = iop_clocksource_read(NULL);
74 struct clocksource *cs = &iop_clocksource;
75
76 return clocksource_cyc2ns(cyc, cs->mult, cs->shift);
77}
78
79/*
Mikael Pettersson469d30442009-10-29 11:46:54 -070080 * IOP clockevents (interrupting timer 0).
81 */
82static int iop_set_next_event(unsigned long delta,
83 struct clock_event_device *unused)
84{
85 u32 tmr = IOP_TMR_PRIVILEGED | IOP_TMR_RATIO_1_1;
86
87 BUG_ON(delta == 0);
88 write_tmr0(tmr & ~(IOP_TMR_EN | IOP_TMR_RELOAD));
89 write_tcr0(delta);
90 write_tmr0((tmr & ~IOP_TMR_RELOAD) | IOP_TMR_EN);
91
92 return 0;
93}
94
Lennert Buytenhek48388b22006-09-18 23:18:16 +010095static unsigned long ticks_per_jiffy;
Mikael Pettersson469d30442009-10-29 11:46:54 -070096
97static void iop_set_mode(enum clock_event_mode mode,
98 struct clock_event_device *unused)
99{
100 u32 tmr = read_tmr0();
101
102 switch (mode) {
103 case CLOCK_EVT_MODE_PERIODIC:
104 write_tmr0(tmr & ~IOP_TMR_EN);
105 write_tcr0(ticks_per_jiffy - 1);
106 tmr |= (IOP_TMR_RELOAD | IOP_TMR_EN);
107 break;
108 case CLOCK_EVT_MODE_ONESHOT:
109 /* ->set_next_event sets period and enables timer */
110 tmr &= ~(IOP_TMR_RELOAD | IOP_TMR_EN);
111 break;
112 case CLOCK_EVT_MODE_RESUME:
113 tmr |= IOP_TMR_EN;
114 break;
115 case CLOCK_EVT_MODE_SHUTDOWN:
116 case CLOCK_EVT_MODE_UNUSED:
117 default:
118 tmr &= ~IOP_TMR_EN;
119 break;
120 }
121
122 write_tmr0(tmr);
123}
124
125static struct clock_event_device iop_clockevent = {
126 .name = "iop_timer0",
127 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
128 .rating = 300,
129 .set_next_event = iop_set_next_event,
130 .set_mode = iop_set_mode,
131};
132
133static void __init iop_clockevent_set_hz(struct clock_event_device *ce, unsigned int hz)
134{
135 u64 temp;
136 u32 shift;
137
138 /* Find shift and mult values for hz. */
139 shift = 32;
140 do {
141 temp = (u64) hz << shift;
142 do_div(temp, NSEC_PER_SEC);
143 if ((temp >> 32) == 0)
144 break;
145 } while (--shift != 0);
146
147 ce->shift = shift;
148 ce->mult = (u32) temp;
149
150 printk(KERN_INFO "clockevent: %s uses shift %u mult %#lx\n",
151 ce->name, ce->shift, ce->mult);
152}
153
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100154static irqreturn_t
Dan Williams3668b452007-02-13 17:13:34 +0100155iop_timer_interrupt(int irq, void *dev_id)
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100156{
Mikael Pettersson469d30442009-10-29 11:46:54 -0700157 struct clock_event_device *evt = dev_id;
158
Dan Williams3668b452007-02-13 17:13:34 +0100159 write_tisr(1);
Mikael Pettersson469d30442009-10-29 11:46:54 -0700160 evt->event_handler(evt);
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100161 return IRQ_HANDLED;
162}
163
Dan Williams3668b452007-02-13 17:13:34 +0100164static struct irqaction iop_timer_irq = {
165 .name = "IOP Timer Tick",
166 .handler = iop_timer_interrupt,
Bernhard Walleb30faba2007-05-08 00:35:39 -0700167 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Mikael Pettersson469d30442009-10-29 11:46:54 -0700168 .dev_id = &iop_clockevent,
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100169};
170
Dan Williams70c14ff2007-07-20 02:07:26 +0100171static unsigned long iop_tick_rate;
172unsigned long get_iop_tick_rate(void)
173{
174 return iop_tick_rate;
175}
176EXPORT_SYMBOL(get_iop_tick_rate);
177
Dan Williams3668b452007-02-13 17:13:34 +0100178void __init iop_init_time(unsigned long tick_rate)
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100179{
180 u32 timer_ctl;
181
Julia Lawalla6928382009-08-02 10:46:45 +0200182 ticks_per_jiffy = DIV_ROUND_CLOSEST(tick_rate, HZ);
Dan Williams70c14ff2007-07-20 02:07:26 +0100183 iop_tick_rate = tick_rate;
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100184
Dan Williams3668b452007-02-13 17:13:34 +0100185 timer_ctl = IOP_TMR_EN | IOP_TMR_PRIVILEGED |
186 IOP_TMR_RELOAD | IOP_TMR_RATIO_1_1;
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100187
188 /*
Mikael Pettersson469d30442009-10-29 11:46:54 -0700189 * Set up interrupting clockevent timer 0.
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100190 */
Mikael Pettersson469d30442009-10-29 11:46:54 -0700191 write_tmr0(timer_ctl & ~IOP_TMR_EN);
192 setup_irq(IRQ_IOP_TIMER0, &iop_timer_irq);
193 iop_clockevent_set_hz(&iop_clockevent, tick_rate);
194 iop_clockevent.max_delta_ns =
195 clockevent_delta2ns(0xfffffffe, &iop_clockevent);
196 iop_clockevent.min_delta_ns =
197 clockevent_delta2ns(0xf, &iop_clockevent);
198 iop_clockevent.cpumask = cpumask_of(0);
199 clockevents_register_device(&iop_clockevent);
Dan Williams3668b452007-02-13 17:13:34 +0100200 write_trr0(ticks_per_jiffy - 1);
Mikael Pettersson469d30442009-10-29 11:46:54 -0700201 write_tcr0(ticks_per_jiffy - 1);
Dan Williams3668b452007-02-13 17:13:34 +0100202 write_tmr0(timer_ctl);
Mikael Petterssona91549a2009-10-29 11:46:54 -0700203
204 /*
205 * Set up free-running clocksource timer 1.
206 */
Dan Williams3668b452007-02-13 17:13:34 +0100207 write_trr1(0xffffffff);
Mikael Petterssona91549a2009-10-29 11:46:54 -0700208 write_tcr1(0xffffffff);
Dan Williams3668b452007-02-13 17:13:34 +0100209 write_tmr1(timer_ctl);
Mikael Petterssona91549a2009-10-29 11:46:54 -0700210 iop_clocksource_set_hz(&iop_clocksource, tick_rate);
211 clocksource_register(&iop_clocksource);
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100212}