1 | /* |
---|
2 | * Copyright (c) 2014 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #include <leon.h> |
---|
16 | |
---|
17 | #include <rtems/counter.h> |
---|
18 | |
---|
19 | static CPU_Counter_ticks timestamp_counter_difference( |
---|
20 | CPU_Counter_ticks second, |
---|
21 | CPU_Counter_ticks first |
---|
22 | ) |
---|
23 | { |
---|
24 | return second - first; |
---|
25 | } |
---|
26 | |
---|
27 | static CPU_Counter_ticks clock_counter_difference( |
---|
28 | CPU_Counter_ticks second, |
---|
29 | CPU_Counter_ticks first |
---|
30 | ) |
---|
31 | { |
---|
32 | CPU_Counter_ticks period = rtems_configuration_get_microseconds_per_tick(); |
---|
33 | |
---|
34 | return (first + period - second) % period; |
---|
35 | } |
---|
36 | |
---|
37 | static void gpt_counter_initialize( |
---|
38 | volatile struct gptimer_regs *gpt, |
---|
39 | size_t timer_index, |
---|
40 | uint32_t frequency, |
---|
41 | SPARC_Counter_difference counter_difference |
---|
42 | ) |
---|
43 | { |
---|
44 | _SPARC_Counter_initialize( |
---|
45 | (volatile const uint32_t *) &gpt->timer[timer_index].value, |
---|
46 | counter_difference |
---|
47 | ); |
---|
48 | |
---|
49 | rtems_counter_initialize_converter(frequency); |
---|
50 | } |
---|
51 | |
---|
52 | void leon3_cpu_counter_initialize(void) |
---|
53 | { |
---|
54 | volatile struct irqmp_timestamp_regs *irqmp_ts = |
---|
55 | &LEON3_IrqCtrl_Regs->timestamp[0]; |
---|
56 | unsigned int freq; |
---|
57 | |
---|
58 | if (leon3_irqmp_has_timestamp(irqmp_ts)) { |
---|
59 | /* Use the interrupt controller timestamp counter if available */ |
---|
60 | |
---|
61 | /* Enable interrupt timestamping for an arbitrary interrupt line */ |
---|
62 | irqmp_ts->control = 0x1; |
---|
63 | |
---|
64 | _SPARC_Counter_initialize( |
---|
65 | (volatile const uint32_t *) &irqmp_ts->counter, |
---|
66 | timestamp_counter_difference |
---|
67 | ); |
---|
68 | |
---|
69 | /* Get and set the frequency */ |
---|
70 | rtems_counter_initialize_converter(ambapp_freq_get(&ambapp_plb, irqmp_dev)); |
---|
71 | } else if (LEON3_Timer_Regs != NULL) { |
---|
72 | /* Fall back to the first GPTIMER if available */ |
---|
73 | freq = ambapp_freq_get(&ambapp_plb, timer_dev); |
---|
74 | |
---|
75 | gpt_counter_initialize( |
---|
76 | LEON3_Timer_Regs, |
---|
77 | LEON3_CLOCK_INDEX, |
---|
78 | freq / (LEON3_Timer_Regs->scaler_reload - 1), |
---|
79 | clock_counter_difference |
---|
80 | ); |
---|
81 | } |
---|
82 | } |
---|