1 | /* |
---|
2 | * Clock Tick Device Driver |
---|
3 | * |
---|
4 | * This routine initializes LEON timer 1 which used for the clock tick. |
---|
5 | * |
---|
6 | * The tick frequency is directly programmed to the configured number of |
---|
7 | * microseconds per tick. |
---|
8 | * |
---|
9 | * COPYRIGHT (c) 1989-2006. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * Modified for LEON3 BSP. |
---|
13 | * COPYRIGHT (c) 2004. |
---|
14 | * Gaisler Research. |
---|
15 | * |
---|
16 | * Copyright (c) 2014, 2016 embedded brains GmbH |
---|
17 | * |
---|
18 | * The license and distribution terms for this file may be |
---|
19 | * found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.org/license/LICENSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #include <bsp.h> |
---|
24 | #include <bspopts.h> |
---|
25 | #include <bsp/fatal.h> |
---|
26 | #include <rtems/rtems/intr.h> |
---|
27 | #include <ambapp.h> |
---|
28 | #include <rtems/score/profiling.h> |
---|
29 | #include <rtems/timecounter.h> |
---|
30 | |
---|
31 | /* The LEON3 BSP Timer driver can rely on the Driver Manager if the |
---|
32 | * DrvMgr is initialized during startup. Otherwise the classic driver |
---|
33 | * must be used. |
---|
34 | * |
---|
35 | * The DrvMgr Clock driver is located in the shared/timer directory |
---|
36 | */ |
---|
37 | #ifndef RTEMS_DRVMGR_STARTUP |
---|
38 | |
---|
39 | /* LEON3 Timer system interrupt number */ |
---|
40 | static int clkirq; |
---|
41 | |
---|
42 | static void (*leon3_tc_tick)(void); |
---|
43 | |
---|
44 | static rtems_timecounter_simple leon3_tc; |
---|
45 | |
---|
46 | #ifndef RTEMS_SMP |
---|
47 | static uint32_t leon3_tc_get(rtems_timecounter_simple *tc) |
---|
48 | { |
---|
49 | return LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].value; |
---|
50 | } |
---|
51 | |
---|
52 | static bool leon3_tc_is_pending(rtems_timecounter_simple *tc) |
---|
53 | { |
---|
54 | return LEON_Is_interrupt_pending(clkirq); |
---|
55 | } |
---|
56 | |
---|
57 | static void leon3_tc_at_tick( rtems_timecounter_simple *tc ) |
---|
58 | { |
---|
59 | /* Nothing to do */ |
---|
60 | } |
---|
61 | |
---|
62 | static uint32_t leon3_tc_get_timecount(struct timecounter *tc) |
---|
63 | { |
---|
64 | return rtems_timecounter_simple_downcounter_get( |
---|
65 | tc, |
---|
66 | leon3_tc_get, |
---|
67 | leon3_tc_is_pending |
---|
68 | ); |
---|
69 | } |
---|
70 | |
---|
71 | static void leon3_tc_tick_simple(void) |
---|
72 | { |
---|
73 | rtems_timecounter_simple_downcounter_tick( |
---|
74 | &leon3_tc, |
---|
75 | leon3_tc_get, |
---|
76 | leon3_tc_at_tick |
---|
77 | ); |
---|
78 | } |
---|
79 | #endif |
---|
80 | |
---|
81 | static uint32_t leon3_tc_get_timecount_up_counter(struct timecounter *tc) |
---|
82 | { |
---|
83 | return leon3_up_counter_low(); |
---|
84 | } |
---|
85 | |
---|
86 | static uint32_t leon3_tc_get_timecount_irqmp(struct timecounter *tc) |
---|
87 | { |
---|
88 | return LEON3_IrqCtrl_Regs->timestamp[0].counter; |
---|
89 | } |
---|
90 | |
---|
91 | #ifdef RTEMS_SMP |
---|
92 | static uint32_t leon3_tc_get_timecount_second_timer(struct timecounter *tc) |
---|
93 | { |
---|
94 | return 0xffffffff - LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX + 1].value; |
---|
95 | } |
---|
96 | #endif |
---|
97 | |
---|
98 | #ifdef RTEMS_PROFILING |
---|
99 | #define IRQMP_TIMESTAMP_S1_S2 ((1U << 25) | (1U << 26)) |
---|
100 | |
---|
101 | static void leon3_tc_tick_irqmp_timestamp(void) |
---|
102 | { |
---|
103 | volatile struct irqmp_timestamp_regs *irqmp_ts = |
---|
104 | &LEON3_IrqCtrl_Regs->timestamp[0]; |
---|
105 | unsigned int first = irqmp_ts->assertion; |
---|
106 | unsigned int second = irqmp_ts->counter; |
---|
107 | |
---|
108 | irqmp_ts->control |= IRQMP_TIMESTAMP_S1_S2; |
---|
109 | |
---|
110 | _Profiling_Update_max_interrupt_delay(_Per_CPU_Get(), second - first); |
---|
111 | |
---|
112 | rtems_timecounter_tick(); |
---|
113 | } |
---|
114 | #endif |
---|
115 | |
---|
116 | static void leon3_tc_tick_irqmp_timestamp_init(void) |
---|
117 | { |
---|
118 | #ifdef RTEMS_PROFILING |
---|
119 | /* |
---|
120 | * Ignore the first clock interrupt, since it contains the sequential system |
---|
121 | * initialization time. Do the timestamp initialization on the fly. |
---|
122 | */ |
---|
123 | |
---|
124 | #ifdef RTEMS_SMP |
---|
125 | static Atomic_Uint counter = ATOMIC_INITIALIZER_UINT(0); |
---|
126 | |
---|
127 | bool done = |
---|
128 | _Atomic_Fetch_add_uint(&counter, 1, ATOMIC_ORDER_RELAXED) |
---|
129 | == rtems_get_processor_count() - 1; |
---|
130 | #else |
---|
131 | bool done = true; |
---|
132 | #endif |
---|
133 | |
---|
134 | volatile struct irqmp_timestamp_regs *irqmp_ts = |
---|
135 | &LEON3_IrqCtrl_Regs->timestamp[0]; |
---|
136 | unsigned int ks = 1U << 5; |
---|
137 | |
---|
138 | irqmp_ts->control = ks | IRQMP_TIMESTAMP_S1_S2 | (unsigned int) clkirq; |
---|
139 | |
---|
140 | if (done) { |
---|
141 | leon3_tc_tick = leon3_tc_tick_irqmp_timestamp; |
---|
142 | } |
---|
143 | #endif |
---|
144 | |
---|
145 | rtems_timecounter_tick(); |
---|
146 | } |
---|
147 | |
---|
148 | #ifdef RTEMS_SMP |
---|
149 | static void leon3_tc_tick_second_timer(void) |
---|
150 | { |
---|
151 | rtems_timecounter_tick(); |
---|
152 | } |
---|
153 | #endif |
---|
154 | |
---|
155 | static void leon3_tc_do_tick(void) |
---|
156 | { |
---|
157 | (*leon3_tc_tick)(); |
---|
158 | } |
---|
159 | |
---|
160 | #define Adjust_clkirq_for_node() do { clkirq += LEON3_CLOCK_INDEX; } while(0) |
---|
161 | |
---|
162 | #define Clock_driver_support_find_timer() \ |
---|
163 | do { \ |
---|
164 | /* Assume timer found during BSP initialization */ \ |
---|
165 | if (LEON3_Timer_Regs) { \ |
---|
166 | clkirq = (LEON3_Timer_Regs->cfg & 0xf8) >> 3; \ |
---|
167 | \ |
---|
168 | Adjust_clkirq_for_node(); \ |
---|
169 | } \ |
---|
170 | } while (0) |
---|
171 | |
---|
172 | #define Clock_driver_support_install_isr( _new, _old ) \ |
---|
173 | do { \ |
---|
174 | (_old) = NULL; \ |
---|
175 | bsp_clock_handler_install(_new); \ |
---|
176 | } while(0) |
---|
177 | |
---|
178 | static void bsp_clock_handler_install(rtems_isr *new) |
---|
179 | { |
---|
180 | rtems_status_code sc; |
---|
181 | |
---|
182 | sc = rtems_interrupt_handler_install( |
---|
183 | clkirq, |
---|
184 | "Clock", |
---|
185 | RTEMS_INTERRUPT_UNIQUE, |
---|
186 | new, |
---|
187 | NULL |
---|
188 | ); |
---|
189 | if (sc != RTEMS_SUCCESSFUL) { |
---|
190 | rtems_fatal(RTEMS_FATAL_SOURCE_BSP, LEON3_FATAL_CLOCK_INITIALIZATION); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | #define Clock_driver_support_set_interrupt_affinity(online_processors) \ |
---|
195 | bsp_interrupt_set_affinity(clkirq, online_processors) |
---|
196 | |
---|
197 | static void leon3_clock_initialize(void) |
---|
198 | { |
---|
199 | volatile struct irqmp_timestamp_regs *irqmp_ts; |
---|
200 | volatile struct gptimer_regs *gpt; |
---|
201 | |
---|
202 | irqmp_ts = &LEON3_IrqCtrl_Regs->timestamp[0]; |
---|
203 | gpt = LEON3_Timer_Regs; |
---|
204 | |
---|
205 | gpt->timer[LEON3_CLOCK_INDEX].reload = |
---|
206 | rtems_configuration_get_microseconds_per_tick() - 1; |
---|
207 | gpt->timer[LEON3_CLOCK_INDEX].ctrl = |
---|
208 | GPTIMER_TIMER_CTRL_EN | GPTIMER_TIMER_CTRL_RS | |
---|
209 | GPTIMER_TIMER_CTRL_LD | GPTIMER_TIMER_CTRL_IE; |
---|
210 | |
---|
211 | leon3_up_counter_enable(); |
---|
212 | |
---|
213 | if (leon3_up_counter_is_available()) { |
---|
214 | /* Use the LEON4 up-counter if available */ |
---|
215 | leon3_tc.tc.tc_get_timecount = leon3_tc_get_timecount_up_counter; |
---|
216 | leon3_tc.tc.tc_counter_mask = 0xffffffff; |
---|
217 | leon3_tc.tc.tc_frequency = leon3_up_counter_frequency(); |
---|
218 | leon3_tc.tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER; |
---|
219 | |
---|
220 | #ifdef RTEMS_PROFILING |
---|
221 | if (!leon3_irqmp_has_timestamp(irqmp_ts)) { |
---|
222 | bsp_fatal(LEON3_FATAL_CLOCK_NO_IRQMP_TIMESTAMP_SUPPORT); |
---|
223 | } |
---|
224 | #endif |
---|
225 | |
---|
226 | leon3_tc_tick = leon3_tc_tick_irqmp_timestamp_init; |
---|
227 | rtems_timecounter_install(&leon3_tc.tc); |
---|
228 | } else if (leon3_irqmp_has_timestamp(irqmp_ts)) { |
---|
229 | /* Use the interrupt controller timestamp counter if available */ |
---|
230 | leon3_tc.tc.tc_get_timecount = leon3_tc_get_timecount_irqmp; |
---|
231 | leon3_tc.tc.tc_counter_mask = 0xffffffff; |
---|
232 | leon3_tc.tc.tc_frequency = ambapp_freq_get(&ambapp_plb, LEON3_Timer_Adev); |
---|
233 | leon3_tc.tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER; |
---|
234 | leon3_tc_tick = leon3_tc_tick_irqmp_timestamp_init; |
---|
235 | rtems_timecounter_install(&leon3_tc.tc); |
---|
236 | } else { |
---|
237 | #ifdef RTEMS_SMP |
---|
238 | /* |
---|
239 | * The GR712RC for example has no timestamp unit in the interrupt |
---|
240 | * controller. At least on SMP configurations we must use a second timer |
---|
241 | * in free running mode for the timecounter. |
---|
242 | */ |
---|
243 | gpt->timer[LEON3_CLOCK_INDEX + 1].ctrl = |
---|
244 | GPTIMER_TIMER_CTRL_EN | GPTIMER_TIMER_CTRL_IE; |
---|
245 | leon3_tc.tc.tc_get_timecount = leon3_tc_get_timecount_second_timer; |
---|
246 | leon3_tc.tc.tc_counter_mask = 0xffffffff; |
---|
247 | leon3_tc.tc.tc_frequency = LEON3_GPTIMER_0_FREQUENCY_SET_BY_BOOT_LOADER; |
---|
248 | leon3_tc.tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER; |
---|
249 | leon3_tc_tick = leon3_tc_tick_second_timer; |
---|
250 | rtems_timecounter_install(&leon3_tc.tc); |
---|
251 | #else |
---|
252 | leon3_tc_tick = leon3_tc_tick_simple; |
---|
253 | rtems_timecounter_simple_install( |
---|
254 | &leon3_tc, |
---|
255 | LEON3_GPTIMER_0_FREQUENCY_SET_BY_BOOT_LOADER, |
---|
256 | rtems_configuration_get_microseconds_per_tick(), |
---|
257 | leon3_tc_get_timecount |
---|
258 | ); |
---|
259 | #endif |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | #define Clock_driver_support_initialize_hardware() \ |
---|
264 | leon3_clock_initialize() |
---|
265 | |
---|
266 | #define Clock_driver_support_shutdown_hardware() \ |
---|
267 | do { \ |
---|
268 | LEON_Mask_interrupt(LEON_TRAP_TYPE(clkirq)); \ |
---|
269 | LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].ctrl = 0; \ |
---|
270 | } while (0) |
---|
271 | |
---|
272 | #define Clock_driver_timecounter_tick() leon3_tc_do_tick() |
---|
273 | |
---|
274 | #include "../../../shared/clockdrv_shell.h" |
---|
275 | |
---|
276 | #endif |
---|