blob: a550e96394ab366dd52ec93e4e45f0546289aa84 [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/*
69 * IOP clockevents (interrupting timer 0).
70 */
71static int iop_set_next_event(unsigned long delta,
72 struct clock_event_device *unused)
73{
74 u32 tmr = IOP_TMR_PRIVILEGED | IOP_TMR_RATIO_1_1;
75
76 BUG_ON(delta == 0);
77 write_tmr0(tmr & ~(IOP_TMR_EN | IOP_TMR_RELOAD));
78 write_tcr0(delta);
79 write_tmr0((tmr & ~IOP_TMR_RELOAD) | IOP_TMR_EN);
80
81 return 0;
82}
83
Lennert Buytenhek48388b22006-09-18 23:18:16 +010084static unsigned long ticks_per_jiffy;
Mikael Pettersson469d30442009-10-29 11:46:54 -070085
86static void iop_set_mode(enum clock_event_mode mode,
87 struct clock_event_device *unused)
88{
89 u32 tmr = read_tmr0();
90
91 switch (mode) {
92 case CLOCK_EVT_MODE_PERIODIC:
93 write_tmr0(tmr & ~IOP_TMR_EN);
94 write_tcr0(ticks_per_jiffy - 1);
95 tmr |= (IOP_TMR_RELOAD | IOP_TMR_EN);
96 break;
97 case CLOCK_EVT_MODE_ONESHOT:
98 /* ->set_next_event sets period and enables timer */
99 tmr &= ~(IOP_TMR_RELOAD | IOP_TMR_EN);
100 break;
101 case CLOCK_EVT_MODE_RESUME:
102 tmr |= IOP_TMR_EN;
103 break;
104 case CLOCK_EVT_MODE_SHUTDOWN:
105 case CLOCK_EVT_MODE_UNUSED:
106 default:
107 tmr &= ~IOP_TMR_EN;
108 break;
109 }
110
111 write_tmr0(tmr);
112}
113
114static struct clock_event_device iop_clockevent = {
115 .name = "iop_timer0",
116 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
117 .rating = 300,
118 .set_next_event = iop_set_next_event,
119 .set_mode = iop_set_mode,
120};
121
122static void __init iop_clockevent_set_hz(struct clock_event_device *ce, unsigned int hz)
123{
124 u64 temp;
125 u32 shift;
126
127 /* Find shift and mult values for hz. */
128 shift = 32;
129 do {
130 temp = (u64) hz << shift;
131 do_div(temp, NSEC_PER_SEC);
132 if ((temp >> 32) == 0)
133 break;
134 } while (--shift != 0);
135
136 ce->shift = shift;
137 ce->mult = (u32) temp;
138
139 printk(KERN_INFO "clockevent: %s uses shift %u mult %#lx\n",
140 ce->name, ce->shift, ce->mult);
141}
142
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100143static unsigned long ticks_per_usec;
144static unsigned long next_jiffy_time;
145
Dan Williams3668b452007-02-13 17:13:34 +0100146unsigned long iop_gettimeoffset(void)
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100147{
Dan Williams8903fcc2007-04-29 09:31:21 +0100148 unsigned long offset, temp;
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100149
Dan Williams3668b452007-02-13 17:13:34 +0100150 /* enable cp6, if necessary, to avoid taking the overhead of an
151 * undefined instruction trap
152 */
153 asm volatile (
154 "mrc p15, 0, %0, c15, c1, 0\n\t"
Dan Williams8903fcc2007-04-29 09:31:21 +0100155 "tst %0, #(1 << 6)\n\t"
Dan Williams3668b452007-02-13 17:13:34 +0100156 "orreq %0, %0, #(1 << 6)\n\t"
157 "mcreq p15, 0, %0, c15, c1, 0\n\t"
Dan Williams8903fcc2007-04-29 09:31:21 +0100158#ifdef CONFIG_CPU_XSCALE
Dan Williams3668b452007-02-13 17:13:34 +0100159 "mrceq p15, 0, %0, c15, c1, 0\n\t"
160 "moveq %0, %0\n\t"
161 "subeq pc, pc, #4\n\t"
162#endif
Dan Williams8903fcc2007-04-29 09:31:21 +0100163 : "=r"(temp) : : "cc");
Dan Williams3668b452007-02-13 17:13:34 +0100164
165 offset = next_jiffy_time - read_tcr1();
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100166
167 return offset / ticks_per_usec;
168}
169
170static irqreturn_t
Dan Williams3668b452007-02-13 17:13:34 +0100171iop_timer_interrupt(int irq, void *dev_id)
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100172{
Mikael Pettersson469d30442009-10-29 11:46:54 -0700173 struct clock_event_device *evt = dev_id;
174
Dan Williams3668b452007-02-13 17:13:34 +0100175 write_tisr(1);
Mikael Pettersson469d30442009-10-29 11:46:54 -0700176 evt->event_handler(evt);
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100177 return IRQ_HANDLED;
178}
179
Dan Williams3668b452007-02-13 17:13:34 +0100180static struct irqaction iop_timer_irq = {
181 .name = "IOP Timer Tick",
182 .handler = iop_timer_interrupt,
Bernhard Walleb30faba2007-05-08 00:35:39 -0700183 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Mikael Pettersson469d30442009-10-29 11:46:54 -0700184 .dev_id = &iop_clockevent,
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100185};
186
Dan Williams70c14ff2007-07-20 02:07:26 +0100187static unsigned long iop_tick_rate;
188unsigned long get_iop_tick_rate(void)
189{
190 return iop_tick_rate;
191}
192EXPORT_SYMBOL(get_iop_tick_rate);
193
Dan Williams3668b452007-02-13 17:13:34 +0100194void __init iop_init_time(unsigned long tick_rate)
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100195{
196 u32 timer_ctl;
197
Julia Lawalla6928382009-08-02 10:46:45 +0200198 ticks_per_jiffy = DIV_ROUND_CLOSEST(tick_rate, HZ);
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100199 ticks_per_usec = tick_rate / 1000000;
200 next_jiffy_time = 0xffffffff;
Dan Williams70c14ff2007-07-20 02:07:26 +0100201 iop_tick_rate = tick_rate;
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100202
Dan Williams3668b452007-02-13 17:13:34 +0100203 timer_ctl = IOP_TMR_EN | IOP_TMR_PRIVILEGED |
204 IOP_TMR_RELOAD | IOP_TMR_RATIO_1_1;
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100205
206 /*
Mikael Pettersson469d30442009-10-29 11:46:54 -0700207 * Set up interrupting clockevent timer 0.
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100208 */
Mikael Pettersson469d30442009-10-29 11:46:54 -0700209 write_tmr0(timer_ctl & ~IOP_TMR_EN);
210 setup_irq(IRQ_IOP_TIMER0, &iop_timer_irq);
211 iop_clockevent_set_hz(&iop_clockevent, tick_rate);
212 iop_clockevent.max_delta_ns =
213 clockevent_delta2ns(0xfffffffe, &iop_clockevent);
214 iop_clockevent.min_delta_ns =
215 clockevent_delta2ns(0xf, &iop_clockevent);
216 iop_clockevent.cpumask = cpumask_of(0);
217 clockevents_register_device(&iop_clockevent);
Dan Williams3668b452007-02-13 17:13:34 +0100218 write_trr0(ticks_per_jiffy - 1);
Mikael Pettersson469d30442009-10-29 11:46:54 -0700219 write_tcr0(ticks_per_jiffy - 1);
Dan Williams3668b452007-02-13 17:13:34 +0100220 write_tmr0(timer_ctl);
Mikael Petterssona91549a2009-10-29 11:46:54 -0700221
222 /*
223 * Set up free-running clocksource timer 1.
224 */
Dan Williams3668b452007-02-13 17:13:34 +0100225 write_trr1(0xffffffff);
Mikael Petterssona91549a2009-10-29 11:46:54 -0700226 write_tcr1(0xffffffff);
Dan Williams3668b452007-02-13 17:13:34 +0100227 write_tmr1(timer_ctl);
Mikael Petterssona91549a2009-10-29 11:46:54 -0700228 iop_clocksource_set_hz(&iop_clocksource, tick_rate);
229 clocksource_register(&iop_clocksource);
Lennert Buytenhek48388b22006-09-18 23:18:16 +0100230}