1 /*
2 * Copyright (c) 2011 Aeroflex Gaisler
3 *
4 * BSD license:
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25
26 #include <sys/types.h>
27 #include <sys/time.h>
28 #include <sys/errno.h>
29 #include <asm-leon/leon.h>
30 #include <asm-leon/irq.h>
31 #include <asm-leon/timer.h>
32 #include <asm-leon/leoncompat.h>
33
34 // '''''''''''''''''''''''''''''''''''''''''''''''''''''
35
36 TAILQ_HEAD (timer_queue, timerevent) timers = TAILQ_HEAD_INITIALIZER (timers);
37
38 int
addtimer(struct timerevent * e)39 addtimer (struct timerevent *e)
40 {
41 struct timerevent *next;
42 unsigned long old = leonbare_disable_traps ();
43 TAILQ_FOREACH (next, &timers, n)
44 {
45 if (!GT_TIMESPEC (e->expire, next->expire))
46 break;
47 }
48 if (next)
49 {
50 TAILQ_INSERT_BEFORE (next, e, n);
51 }
52 else
53 {
54 TAILQ_INSERT_TAIL (&timers, e, n);
55 }
56 leonbare_enable_traps (old);
57 }
58
59 extern unsigned long noalarm;
60 void
settimer()61 settimer ()
62 {
63 struct timeval tv, te;
64 struct timerevent *e = TAILQ_FIRST (&timers), *n;
65 while (e)
66 {
67 n = TAILQ_NEXT (e, n);
68 te.tv_sec = e->expire.tv_sec;
69 te.tv_usec = e->expire.tv_nsec / NSEC_PER_USEC;
70 do_gettimeofday (&tv);
71 if (GT_TIMEVAL (te, tv))
72 {
73 MINUS_TIMEVAL (te, te, tv);
74 if (!tv.tv_sec || te.tv_usec <= tick_usec)
75 {
76 if (!noalarm)
77 {
78 //---------------------
79 switch (LEONCOMPAT_VERSION)
80 {
81 case 3:
82 default:
83 LEON3_GpTimer_Regs->e[1].val = 0;
84 LEON3_GpTimer_Regs->e[1].rld = te.tv_usec - 1;
85 LEON3_GpTimer_Regs->e[1].ctrl = 0;
86 LEON3_GpTimer_Regs->e[1].ctrl =
87 LEON3_GPTIMER_EN |
88 LEON3_GPTIMER_LD | LEON3_GPTIMER_IRQEN;
89 break;
90 }
91 }
92 //---------------------
93 }
94 }
95 else
96 {
97 unsigned long old = leonbare_disable_traps ();
98 TAILQ_REMOVE (&timers, e, n);
99 e->handler (e->arg);
100 leonbare_enable_traps (old);
101 }
102 e = n;
103 }
104 }
105
106 int
Timer_getTimer1(unsigned int ** count,unsigned int ** reload,unsigned int ** ctrl)107 Timer_getTimer1 (unsigned int **count, unsigned int **reload,
108 unsigned int **ctrl)
109 {
110 //---------------------
111 switch (LEONCOMPAT_VERSION)
112 {
113 case 3:
114 default:
115 amba_init ();
116 *count = (unsigned int *) &(LEON3_GpTimer_Regs->e[0].val);
117 *reload = (unsigned int *) &(LEON3_GpTimer_Regs->e[0].rld);
118 *ctrl = (unsigned int *) &(LEON3_GpTimer_Regs->e[0].ctrl);
119 break;
120 }
121 //---------------------
122 return 1;
123 }
124
125 int
Timer_getTimer2(unsigned int ** count,unsigned int ** reload,unsigned int ** ctrl)126 Timer_getTimer2 (unsigned int **count, unsigned int **reload,
127 unsigned int **ctrl)
128 {
129 //---------------------
130 switch (LEONCOMPAT_VERSION)
131 {
132 case 3:
133 default:
134 amba_init ();
135 if (!noalarm)
136 {
137 *count = (unsigned int *) &(LEON3_GpTimer_Regs->e[1].val);
138 *reload = (unsigned int *) &(LEON3_GpTimer_Regs->e[1].rld);
139 *ctrl = (unsigned int *) &(LEON3_GpTimer_Regs->e[1].ctrl);
140 break;
141 }
142 return 0;
143 }
144 //---------------------
145 return 1;
146 }
147