2 * arch/arm/plat-iop/time.c
4 * Timer code for IOP32x and IOP33x based systems
6 * Author: Deepak Saxena <dsaxena@mvista.com>
8 * Copyright 2002-2003 MontaVista Software Inc.
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.
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>
22 #include <linux/clocksource.h>
23 #include <mach/hardware.h>
25 #include <asm/uaccess.h>
26 #include <asm/mach/irq.h>
27 #include <asm/mach/time.h>
28 #include <mach/time.h>
31 * IOP clocksource (free-running timer 1).
33 static cycle_t iop_clocksource_read(struct clocksource *unused)
35 return 0xffffffffu - read_tcr1();
38 static struct clocksource iop_clocksource = {
41 .read = iop_clocksource_read,
42 .mask = CLOCKSOURCE_MASK(32),
43 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
46 static void __init iop_clocksource_set_hz(struct clocksource *cs, unsigned int hz)
51 /* Find shift and mult values for hz. */
54 temp = (u64) NSEC_PER_SEC << shift;
56 if ((temp >> 32) == 0)
58 } while (--shift != 0);
61 cs->mult = (u32) temp;
63 printk(KERN_INFO "clocksource: %s uses shift %u mult %#x\n",
64 cs->name, cs->shift, cs->mult);
67 static unsigned long ticks_per_jiffy;
68 static unsigned long ticks_per_usec;
69 static unsigned long next_jiffy_time;
71 unsigned long iop_gettimeoffset(void)
73 unsigned long offset, temp;
75 /* enable cp6, if necessary, to avoid taking the overhead of an
76 * undefined instruction trap
79 "mrc p15, 0, %0, c15, c1, 0\n\t"
80 "tst %0, #(1 << 6)\n\t"
81 "orreq %0, %0, #(1 << 6)\n\t"
82 "mcreq p15, 0, %0, c15, c1, 0\n\t"
83 #ifdef CONFIG_CPU_XSCALE
84 "mrceq p15, 0, %0, c15, c1, 0\n\t"
86 "subeq pc, pc, #4\n\t"
88 : "=r"(temp) : : "cc");
90 offset = next_jiffy_time - read_tcr1();
92 return offset / ticks_per_usec;
96 iop_timer_interrupt(int irq, void *dev_id)
100 while ((signed long)(next_jiffy_time - read_tcr1())
101 >= ticks_per_jiffy) {
103 next_jiffy_time -= ticks_per_jiffy;
109 static struct irqaction iop_timer_irq = {
110 .name = "IOP Timer Tick",
111 .handler = iop_timer_interrupt,
112 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
115 static unsigned long iop_tick_rate;
116 unsigned long get_iop_tick_rate(void)
118 return iop_tick_rate;
120 EXPORT_SYMBOL(get_iop_tick_rate);
122 void __init iop_init_time(unsigned long tick_rate)
126 ticks_per_jiffy = DIV_ROUND_CLOSEST(tick_rate, HZ);
127 ticks_per_usec = tick_rate / 1000000;
128 next_jiffy_time = 0xffffffff;
129 iop_tick_rate = tick_rate;
131 timer_ctl = IOP_TMR_EN | IOP_TMR_PRIVILEGED |
132 IOP_TMR_RELOAD | IOP_TMR_RATIO_1_1;
135 * We use timer 0 for our timer interrupt, and timer 1 as
136 * monotonic counter for tracking missed jiffies.
138 write_trr0(ticks_per_jiffy - 1);
139 write_tmr0(timer_ctl);
142 * Set up free-running clocksource timer 1.
144 write_trr1(0xffffffff);
145 write_tcr1(0xffffffff);
146 write_tmr1(timer_ctl);
147 iop_clocksource_set_hz(&iop_clocksource, tick_rate);
148 clocksource_register(&iop_clocksource);
150 setup_irq(IRQ_IOP_TIMER0, &iop_timer_irq);