0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/clocksource.h>
0014 #include <linux/init.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/irq.h>
0017 #include <linux/io.h>
0018
0019 #include <mach/hardware.h>
0020 #include <asm/hardware/ioc.h>
0021
0022 #include <asm/mach/time.h>
0023
0024 #define RPC_CLOCK_FREQ 2000000
0025 #define RPC_LATCH DIV_ROUND_CLOSEST(RPC_CLOCK_FREQ, HZ)
0026
0027 static u32 ioc_time;
0028
0029 static u64 ioc_timer_read(struct clocksource *cs)
0030 {
0031 unsigned int count1, count2, status;
0032 unsigned long flags;
0033 u32 ticks;
0034
0035 local_irq_save(flags);
0036 ioc_writeb (0, IOC_T0LATCH);
0037 barrier ();
0038 count1 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
0039 barrier ();
0040 status = ioc_readb(IOC_IRQREQA);
0041 barrier ();
0042 ioc_writeb (0, IOC_T0LATCH);
0043 barrier ();
0044 count2 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
0045 ticks = ioc_time + RPC_LATCH - count2;
0046 local_irq_restore(flags);
0047
0048 if (count2 < count1) {
0049
0050
0051
0052
0053 if (status & (1 << 5))
0054 ticks += RPC_LATCH;
0055 } else if (count2 > count1) {
0056
0057
0058
0059
0060
0061 ticks += RPC_LATCH;
0062 }
0063
0064 return ticks;
0065 }
0066
0067 static struct clocksource ioctime_clocksource = {
0068 .read = ioc_timer_read,
0069 .mask = CLOCKSOURCE_MASK(32),
0070 .rating = 100,
0071 };
0072
0073 void __init ioctime_init(void)
0074 {
0075 ioc_writeb(RPC_LATCH & 255, IOC_T0LTCHL);
0076 ioc_writeb(RPC_LATCH >> 8, IOC_T0LTCHH);
0077 ioc_writeb(0, IOC_T0GO);
0078 }
0079
0080 static irqreturn_t
0081 ioc_timer_interrupt(int irq, void *dev_id)
0082 {
0083 ioc_time += RPC_LATCH;
0084 legacy_timer_tick(1);
0085 return IRQ_HANDLED;
0086 }
0087
0088
0089
0090
0091 void __init ioc_timer_init(void)
0092 {
0093 WARN_ON(clocksource_register_hz(&ioctime_clocksource, RPC_CLOCK_FREQ));
0094 ioctime_init();
0095 if (request_irq(IRQ_TIMER0, ioc_timer_interrupt, 0, "timer", NULL))
0096 pr_err("Failed to request irq %d (timer)\n", IRQ_TIMER0);
0097 }