MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / arch / arm / mach-moxart / timer.c
blob53a0543e1cde8fe522c8b31e09941982e98b8caa
1 /*
2 * time.c Timer functions for cpe
3 */
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>
12 #include <asm/io.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>
18 #define MAX_TIMER 2
19 #define USED_TIMER 1
20 #define SET_COUNTER (APB_CLK / HZ)
22 cpe_time_reg_t *TimerBase[] =
24 0,
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))
34 return 0;
36 switch(timer)
38 case 1:
39 t->Tm1En=1;
40 t->Tm1OfEn=1;
41 #if 1 // add by Victor Yu. 06-01-2005
42 t->Tm1Clock = 0; // use PCLK
43 #endif
44 break;
45 case 2:
46 t->Tm2En=1;
47 t->Tm2OfEn=1;
48 #if 1 // add by Victor Yu. 06-01-2005
49 t->Tm2Clock = 0; // use PCLK
50 #endif
51 break;
52 case 3:
53 t->Tm3En=1;
54 t->Tm3OfEn=1;
55 #if 1 // add by Victor Yu. 06-01-2005
56 t->Tm3Clock = 0; // use PCLK
57 #endif
58 break;
60 default:
61 break;
64 return 1;
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))
74 return 0;
76 switch(timer)
78 case 1:
79 t->Tm1En=0;
80 t->Tm1OfEn=0;
81 break;
82 case 2:
83 t->Tm2En=0;
84 t->Tm2OfEn=0;
85 break;
86 case 3:
87 t->Tm3En=0;
88 t->Tm3OfEn=0;
89 break;
91 default:
92 break;
96 return 1;
98 #endif
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;
113 #endif
116 // --------------------------------------------------------------------
117 // warning:
118 // timer = 1, 2, 3
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) ) {
136 #else
137 if ( *(volatile unsigned int *)(CPE_TIMER1_VA_BASE+TIMER_INTR_STATE) ) {
138 //printk("[has timer interrupt pending , %d !]\n", offsetticks);
139 #endif
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
145 return offsetticks;
146 #else // 02-26-2007
147 return 0;
148 #endif // 02-26-2007
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;
158 #endif
159 return IRQ_HANDLED;
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);
174 #else
175 cpe_timer_set_reload(USED_TIMER, SET_COUNTER);
176 cpe_timer_set_counter(USED_TIMER, SET_COUNTER);
177 #endif
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);
188 #endif
190 struct sys_timer moxa_timer = {
191 .init = moxa_time_init,
192 .offset = cpe_gettimeoffset,