]> bbs.cooldavid.org Git - net-next-2.6.git/blob - arch/arm/plat-iop/time.c
iop: clockevent support
[net-next-2.6.git] / arch / arm / plat-iop / time.c
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>
21 #include <linux/io.h>
22 #include <linux/clocksource.h>
23 #include <linux/clockchips.h>
24 #include <mach/hardware.h>
25 #include <asm/irq.h>
26 #include <asm/uaccess.h>
27 #include <asm/mach/irq.h>
28 #include <asm/mach/time.h>
29 #include <mach/time.h>
30
31 /*
32  * IOP clocksource (free-running timer 1).
33  */
34 static cycle_t iop_clocksource_read(struct clocksource *unused)
35 {
36         return 0xffffffffu - read_tcr1();
37 }
38
39 static 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
47 static 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
68 /*
69  * IOP clockevents (interrupting timer 0).
70  */
71 static 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
84 static unsigned long ticks_per_jiffy;
85
86 static 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
114 static 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
122 static 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
143 static unsigned long ticks_per_usec;
144 static unsigned long next_jiffy_time;
145
146 unsigned long iop_gettimeoffset(void)
147 {
148         unsigned long offset, temp;
149
150         /* 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"
155         "tst    %0, #(1 << 6)\n\t"
156         "orreq  %0, %0, #(1 << 6)\n\t"
157         "mcreq  p15, 0, %0, c15, c1, 0\n\t"
158 #ifdef CONFIG_CPU_XSCALE
159         "mrceq  p15, 0, %0, c15, c1, 0\n\t"
160         "moveq  %0, %0\n\t"
161         "subeq  pc, pc, #4\n\t"
162 #endif
163         : "=r"(temp) : : "cc");
164
165         offset = next_jiffy_time - read_tcr1();
166
167         return offset / ticks_per_usec;
168 }
169
170 static irqreturn_t
171 iop_timer_interrupt(int irq, void *dev_id)
172 {
173         struct clock_event_device *evt = dev_id;
174
175         write_tisr(1);
176         evt->event_handler(evt);
177         return IRQ_HANDLED;
178 }
179
180 static struct irqaction iop_timer_irq = {
181         .name           = "IOP Timer Tick",
182         .handler        = iop_timer_interrupt,
183         .flags          = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
184         .dev_id         = &iop_clockevent,
185 };
186
187 static unsigned long iop_tick_rate;
188 unsigned long get_iop_tick_rate(void)
189 {
190         return iop_tick_rate;
191 }
192 EXPORT_SYMBOL(get_iop_tick_rate);
193
194 void __init iop_init_time(unsigned long tick_rate)
195 {
196         u32 timer_ctl;
197
198         ticks_per_jiffy = DIV_ROUND_CLOSEST(tick_rate, HZ);
199         ticks_per_usec = tick_rate / 1000000;
200         next_jiffy_time = 0xffffffff;
201         iop_tick_rate = tick_rate;
202
203         timer_ctl = IOP_TMR_EN | IOP_TMR_PRIVILEGED |
204                         IOP_TMR_RELOAD | IOP_TMR_RATIO_1_1;
205
206         /*
207          * Set up interrupting clockevent timer 0.
208          */
209         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);
218         write_trr0(ticks_per_jiffy - 1);
219         write_tcr0(ticks_per_jiffy - 1);
220         write_tmr0(timer_ctl);
221
222         /*
223          * Set up free-running clocksource timer 1.
224          */
225         write_trr1(0xffffffff);
226         write_tcr1(0xffffffff);
227         write_tmr1(timer_ctl);
228         iop_clocksource_set_hz(&iop_clocksource, tick_rate);
229         clocksource_register(&iop_clocksource);
230 }