2 * linux/arch/arm/mach-iop3xx/time-iq80310.c
4 * Timer functions for IQ80310 onboard timer
6 * Author: Nicolas Pitre
7 * Copyright: (C) 2001 MontaVista Software Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/kernel.h>
15 #include <linux/interrupt.h>
16 #include <linux/time.h>
17 #include <linux/init.h>
18 #include <linux/timex.h>
20 #include <asm/hardware.h>
23 #include <asm/uaccess.h>
24 #include <asm/mach/irq.h>
26 static void iq80310_write_timer (u_long val)
28 volatile u_char *la0 = (volatile u_char *)IQ80310_TIMER_LA0;
29 volatile u_char *la1 = (volatile u_char *)IQ80310_TIMER_LA1;
30 volatile u_char *la2 = (volatile u_char *)IQ80310_TIMER_LA2;
34 *la2 = (val >> 16) & 0x3f;
37 static u_long iq80310_read_timer (void)
39 volatile u_char *la0 = (volatile u_char *)IQ80310_TIMER_LA0;
40 volatile u_char *la1 = (volatile u_char *)IQ80310_TIMER_LA1;
41 volatile u_char *la2 = (volatile u_char *)IQ80310_TIMER_LA2;
42 volatile u_char *la3 = (volatile u_char *)IQ80310_TIMER_LA3;
43 u_long b0, b1, b2, b3, val;
45 b0 = *la0; b1 = *la1; b2 = *la2; b3 = *la3;
46 b0 = (((b0 & 0x40) >> 1) | (b0 & 0x1f));
47 b1 = (((b1 & 0x40) >> 1) | (b1 & 0x1f));
48 b2 = (((b2 & 0x40) >> 1) | (b2 & 0x1f));
50 val = ((b0 << 0) | (b1 << 6) | (b2 << 12) | (b3 << 18));
55 * IRQs are disabled before entering here from do_gettimeofday().
56 * Note that the counter may wrap. When it does, 'elapsed' will
57 * be small, but we will have a pending interrupt.
59 static unsigned long iq80310_gettimeoffset (void)
61 unsigned long elapsed, usec;
62 unsigned int stat1, stat2;
64 stat1 = *(volatile u8 *)IQ80310_INT_STAT;
65 elapsed = iq80310_read_timer();
66 stat2 = *(volatile u8 *)IQ80310_INT_STAT;
69 * If an interrupt was pending before we read the timer,
70 * we've already wrapped. Factor this into the time.
71 * If an interrupt was pending after we read the timer,
72 * it may have wrapped between checking the interrupt
73 * status and reading the timer. Re-read the timer to
74 * be sure its value is after the wrap.
79 elapsed = LATCH + iq80310_read_timer();
82 * Now convert them to usec.
84 usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
91 iq80310_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
93 volatile u_char *timer_en = (volatile u_char *)IQ80310_TIMER_EN;
95 /* clear timer interrupt */
104 extern unsigned long (*gettimeoffset)(void);
106 static struct irqaction timer_irq = {
108 .handler = iq80310_timer_interrupt,
112 void __init time_init(void)
114 volatile u_char *timer_en = (volatile u_char *)IQ80310_TIMER_EN;
116 gettimeoffset = iq80310_gettimeoffset;
118 setup_irq(IRQ_IQ80310_TIMER, &timer_irq);
121 iq80310_write_timer(LATCH);