2 * time.c Timer functions for cpe
4 //#include <linux/config.h>
5 #include <asm/arch/moxa.h>
6 #include <linux/time.h>
7 #include <linux/timex.h>
8 #include <linux/types.h>
9 #include <linux/sched.h>
10 #include <linux/interrupt.h>
11 #include <linux/init.h>
13 #include <asm/arch/hardware.h>
14 #include <asm/arch/cpe_int.h>
15 #include <asm/arch/cpe_time.h>
16 #include <asm-arm/mach/time.h>
20 #define SET_COUNTER (APB_CLK / HZ)
22 cpe_time_reg_t
*TimerBase
[] =
25 (cpe_time_reg_t
*)CPE_TIMER1_VA_BASE
,
26 (cpe_time_reg_t
*)CPE_TIMER2_VA_BASE
29 static int cpe_timer_enable(int timer
)
31 cpe_time_ctrl_t
*t
=(cpe_time_ctrl_t
*)(CPE_TIMER1_VA_BASE
+TIMER_CR
);
33 if ((timer
== 0) || (timer
> MAX_TIMER
))
41 #if 1 // add by Victor Yu. 06-01-2005
42 t
->Tm1Clock
= 0; // use PCLK
48 #if 1 // add by Victor Yu. 06-01-2005
49 t
->Tm2Clock
= 0; // use PCLK
55 #if 1 // add by Victor Yu. 06-01-2005
56 t
->Tm3Clock
= 0; // use PCLK
67 #if 0 // mask by Victor Yu. 01-31-2008
68 /* This routine stops the specified timer hardware. */
69 unsigned int cpe_timer_disable(unsigned int timer
)
71 cpe_time_ctrl_t
*t
=(cpe_time_ctrl_t
*)(CPE_TIMER1_VA_BASE
+ TIMER_CR
);
73 if ((timer
== 0) || (timer
> MAX_TIMER
))
100 static void cpe_timer_set_counter(int timer
, unsigned int value
)
102 volatile cpe_time_reg_t
*t
= TimerBase
[timer
];
103 t
->TimerValue
= value
;
106 static void cpe_timer_set_reload(int timer
, unsigned int value
)
108 volatile cpe_time_reg_t
*t
= TimerBase
[timer
];
109 t
->TimerLoad
= value
;
110 #if 1 // add by Victor Yu. 01-31-2008
111 t
->TimerMatch1
= 0xffffffff;
112 t
->TimerMatch2
= 0xffffffff;
116 // --------------------------------------------------------------------
119 // --------------------------------------------------------------------
120 static unsigned long cpe_timer_get_counter(int timer
)
122 volatile cpe_time_reg_t
*t
= TimerBase
[timer
];
123 return (volatile unsigned long)(t
->TimerValue
);
126 unsigned long cpe_gettimeoffset (void)
128 #if 1 // add by Victor Yu. 02-26-2007
129 unsigned long offsetticks
;
131 offsetticks
= cpe_timer_get_counter(USED_TIMER
);
132 offsetticks
= SET_COUNTER
- offsetticks
;
133 #if 0 // mask by Victor Yu. 01-31-2008
134 if ( (*(volatile unsigned int *)(CPE_IC_VA_BASE
+IRQ_STATUS_REG
) & IRQ_TIMER1
) ||
135 (*(volatile unsigned int *)(CPE_IC_VA_BASE
+IRQ_STATUS_REG
) & IRQ_TIMER1
) ) {
137 if ( *(volatile unsigned int *)(CPE_TIMER1_VA_BASE
+TIMER_INTR_STATE
) ) {
138 //printk("[has timer interrupt pending , %d !]\n", offsetticks);
140 offsetticks
= cpe_timer_get_counter(USED_TIMER
);
141 offsetticks
= SET_COUNTER
- offsetticks
;
142 offsetticks
+= SET_COUNTER
;
144 offsetticks
= offsetticks
/ (APB_CLK
/ 1000000); // tansfer ticks to usec
151 #if 1 // add by Victor Yu. 05-25-2005
152 static irqreturn_t
cpe_timer_interrupt(int irq
, void *dev_id
)
154 //do_timer(regs); // mask by Victor Yu. 06-01-2005
155 timer_tick(); // add by Victor Yu. 06-01-2005
156 #if 1 // add by Victor Yu. 01-31-2008
157 *(volatile unsigned int *)(CPE_TIMER1_VA_BASE
+TIMER_INTR_STATE
) = 0;
162 static struct irqaction cpe_timer_irq
= {
163 .name
= "Moxa CPE timer interrupt",
164 .flags
= SA_INTERRUPT
,
165 .handler
= cpe_timer_interrupt
168 void __init
moxa_time_init(void)
170 //#ifdef TIMER_INC_MODE
171 #if 0 // mask by Victor Yu. 01-31-2008
172 cpe_timer_set_reload(USED_TIMER
, 0xffffffff - SET_COUNTER
);
173 cpe_timer_set_counter(USED_TIMER
, 0xffffffff - SET_COUNTER
);
175 cpe_timer_set_reload(USED_TIMER
, SET_COUNTER
);
176 cpe_timer_set_counter(USED_TIMER
, SET_COUNTER
);
179 if( !cpe_timer_enable(USED_TIMER
) ) {
180 panic("can not enable timer\n");
183 printk("IRQ timer at interrupt number 0x%x clock %d\r\n",IRQ_TIMER1
,APB_CLK
);
184 //moxa_int_set_irq(IRQ_TIMER1, EDGE, L_ACTIVE);
185 cpe_int_set_irq(IRQ_TIMER1
, EDGE
, L_ACTIVE
);
186 setup_irq(IRQ_TIMER1
, &cpe_timer_irq
);
190 struct sys_timer moxa_timer
= {
191 .init
= moxa_time_init
,
192 .offset
= cpe_gettimeoffset
,