[41c9282] | 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 | * |
---|
[29d1fce] | 9 | * COPYRIGHT (c) 1989-2006. |
---|
[41c9282] | 10 | * On-Line Applications Research Corporation (OAR). |
---|
| 11 | * |
---|
| 12 | * Modified for LEON3 BSP. |
---|
| 13 | * COPYRIGHT (c) 2004. |
---|
| 14 | * Gaisler Research. |
---|
| 15 | * |
---|
[a4ff2a2] | 16 | * Copyright (c) 2014, 2016 embedded brains GmbH |
---|
| 17 | * |
---|
[41c9282] | 18 | * The license and distribution terms for this file may be |
---|
| 19 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 20 | * http://www.rtems.org/license/LICENSE. |
---|
[41c9282] | 21 | */ |
---|
| 22 | |
---|
| 23 | #include <bsp.h> |
---|
[29d1fce] | 24 | #include <bspopts.h> |
---|
[a387e944] | 25 | #include <bsp/fatal.h> |
---|
| 26 | #include <rtems/rtems/intr.h> |
---|
[1f7cfbe3] | 27 | #include <ambapp.h> |
---|
[234ecedd] | 28 | #include <rtems/score/profiling.h> |
---|
[75acd9e] | 29 | #include <rtems/timecounter.h> |
---|
[29d1fce] | 30 | |
---|
[e428dc4a] | 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 | |
---|
[9fe249d] | 39 | /* LEON3 Timer system interrupt number */ |
---|
[41c9282] | 40 | static int clkirq; |
---|
| 41 | |
---|
[5a0b27ed] | 42 | static void (*leon3_tc_tick)(void); |
---|
[75acd9e] | 43 | |
---|
| 44 | static rtems_timecounter_simple leon3_tc; |
---|
| 45 | |
---|
[5a0b27ed] | 46 | #ifndef RTEMS_SMP |
---|
[75acd9e] | 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 | |
---|
[5a0b27ed] | 57 | static void leon3_tc_at_tick( rtems_timecounter_simple *tc ) |
---|
| 58 | { |
---|
| 59 | /* Nothing to do */ |
---|
| 60 | } |
---|
| 61 | |
---|
[75acd9e] | 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 | |
---|
[5a0b27ed] | 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 | |
---|
[a4ff2a2] | 81 | static uint32_t leon3_tc_get_timecount_up_counter(struct timecounter *tc) |
---|
| 82 | { |
---|
| 83 | return leon3_up_counter_low(); |
---|
| 84 | } |
---|
| 85 | |
---|
[75acd9e] | 86 | static uint32_t leon3_tc_get_timecount_irqmp(struct timecounter *tc) |
---|
| 87 | { |
---|
| 88 | return LEON3_IrqCtrl_Regs->timestamp[0].counter; |
---|
| 89 | } |
---|
| 90 | |
---|
[5a0b27ed] | 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 | |
---|
[291945f1] | 98 | #ifdef RTEMS_PROFILING |
---|
| 99 | #define IRQMP_TIMESTAMP_S1_S2 ((1U << 25) | (1U << 26)) |
---|
| 100 | |
---|
[5a0b27ed] | 101 | static void leon3_tc_tick_irqmp_timestamp(void) |
---|
[291945f1] | 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) |
---|
[234ecedd] | 117 | { |
---|
| 118 | #ifdef RTEMS_PROFILING |
---|
| 119 | /* |
---|
[291945f1] | 120 | * Ignore the first clock interrupt, since it contains the sequential system |
---|
| 121 | * initialization time. Do the timestamp initialization on the fly. |
---|
[234ecedd] | 122 | */ |
---|
| 123 | |
---|
[291945f1] | 124 | #ifdef RTEMS_SMP |
---|
| 125 | static Atomic_Uint counter = ATOMIC_INITIALIZER_UINT(0); |
---|
[234ecedd] | 126 | |
---|
[291945f1] | 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 |
---|
[234ecedd] | 133 | |
---|
[291945f1] | 134 | volatile struct irqmp_timestamp_regs *irqmp_ts = |
---|
| 135 | &LEON3_IrqCtrl_Regs->timestamp[0]; |
---|
| 136 | unsigned int ks = 1U << 5; |
---|
[234ecedd] | 137 | |
---|
[291945f1] | 138 | irqmp_ts->control = ks | IRQMP_TIMESTAMP_S1_S2 | (unsigned int) clkirq; |
---|
[234ecedd] | 139 | |
---|
[291945f1] | 140 | if (done) { |
---|
| 141 | leon3_tc_tick = leon3_tc_tick_irqmp_timestamp; |
---|
[234ecedd] | 142 | } |
---|
| 143 | #endif |
---|
[5a0b27ed] | 144 | |
---|
| 145 | rtems_timecounter_tick(); |
---|
[234ecedd] | 146 | } |
---|
| 147 | |
---|
[5a0b27ed] | 148 | #ifdef RTEMS_SMP |
---|
| 149 | static void leon3_tc_tick_second_timer(void) |
---|
[76ac1ee3] | 150 | { |
---|
[5a0b27ed] | 151 | rtems_timecounter_tick(); |
---|
[76ac1ee3] | 152 | } |
---|
[5a0b27ed] | 153 | #endif |
---|
[76ac1ee3] | 154 | |
---|
[5a0b27ed] | 155 | static void leon3_tc_do_tick(void) |
---|
[76ac1ee3] | 156 | { |
---|
[5a0b27ed] | 157 | (*leon3_tc_tick)(); |
---|
[76ac1ee3] | 158 | } |
---|
[29d1fce] | 159 | |
---|
[628e739] | 160 | #define Adjust_clkirq_for_node() do { clkirq += LEON3_CLOCK_INDEX; } while(0) |
---|
[7f3c6ce] | 161 | |
---|
[29d1fce] | 162 | #define Clock_driver_support_find_timer() \ |
---|
[921bb59] | 163 | do { \ |
---|
[60b0fd5] | 164 | /* Assume timer found during BSP initialization */ \ |
---|
| 165 | if (LEON3_Timer_Regs) { \ |
---|
[226d48d8] | 166 | clkirq = (LEON3_Timer_Regs->cfg & 0xf8) >> 3; \ |
---|
[921bb59] | 167 | \ |
---|
[7f3c6ce] | 168 | Adjust_clkirq_for_node(); \ |
---|
[29d1fce] | 169 | } \ |
---|
| 170 | } while (0) |
---|
| 171 | |
---|
[f3b29236] | 172 | #define Clock_driver_support_install_isr( _new ) \ |
---|
| 173 | bsp_clock_handler_install(_new) |
---|
[29d1fce] | 174 | |
---|
[a387e944] | 175 | static void bsp_clock_handler_install(rtems_isr *new) |
---|
| 176 | { |
---|
| 177 | rtems_status_code sc; |
---|
| 178 | |
---|
| 179 | sc = rtems_interrupt_handler_install( |
---|
| 180 | clkirq, |
---|
| 181 | "Clock", |
---|
| 182 | RTEMS_INTERRUPT_UNIQUE, |
---|
| 183 | new, |
---|
| 184 | NULL |
---|
| 185 | ); |
---|
| 186 | if (sc != RTEMS_SUCCESSFUL) { |
---|
| 187 | rtems_fatal(RTEMS_FATAL_SOURCE_BSP, LEON3_FATAL_CLOCK_INITIALIZATION); |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
[90d8567] | 191 | #define Clock_driver_support_set_interrupt_affinity(online_processors) \ |
---|
[af207fa9] | 192 | bsp_interrupt_set_affinity(clkirq, online_processors) |
---|
[90d8567] | 193 | |
---|
[75acd9e] | 194 | static void leon3_clock_initialize(void) |
---|
| 195 | { |
---|
[a4ff2a2] | 196 | volatile struct irqmp_timestamp_regs *irqmp_ts; |
---|
| 197 | volatile struct gptimer_regs *gpt; |
---|
[75acd9e] | 198 | |
---|
[a4ff2a2] | 199 | irqmp_ts = &LEON3_IrqCtrl_Regs->timestamp[0]; |
---|
| 200 | gpt = LEON3_Timer_Regs; |
---|
| 201 | |
---|
| 202 | gpt->timer[LEON3_CLOCK_INDEX].reload = |
---|
[75acd9e] | 203 | rtems_configuration_get_microseconds_per_tick() - 1; |
---|
[a4ff2a2] | 204 | gpt->timer[LEON3_CLOCK_INDEX].ctrl = |
---|
[75acd9e] | 205 | GPTIMER_TIMER_CTRL_EN | GPTIMER_TIMER_CTRL_RS | |
---|
| 206 | GPTIMER_TIMER_CTRL_LD | GPTIMER_TIMER_CTRL_IE; |
---|
| 207 | |
---|
[a4ff2a2] | 208 | leon3_up_counter_enable(); |
---|
| 209 | |
---|
| 210 | if (leon3_up_counter_is_available()) { |
---|
| 211 | /* Use the LEON4 up-counter if available */ |
---|
| 212 | leon3_tc.tc.tc_get_timecount = leon3_tc_get_timecount_up_counter; |
---|
| 213 | leon3_tc.tc.tc_counter_mask = 0xffffffff; |
---|
| 214 | leon3_tc.tc.tc_frequency = leon3_up_counter_frequency(); |
---|
| 215 | leon3_tc.tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER; |
---|
| 216 | |
---|
| 217 | #ifdef RTEMS_PROFILING |
---|
| 218 | if (!leon3_irqmp_has_timestamp(irqmp_ts)) { |
---|
| 219 | bsp_fatal(LEON3_FATAL_CLOCK_NO_IRQMP_TIMESTAMP_SUPPORT); |
---|
| 220 | } |
---|
| 221 | #endif |
---|
| 222 | |
---|
| 223 | leon3_tc_tick = leon3_tc_tick_irqmp_timestamp_init; |
---|
| 224 | rtems_timecounter_install(&leon3_tc.tc); |
---|
| 225 | } else if (leon3_irqmp_has_timestamp(irqmp_ts)) { |
---|
| 226 | /* Use the interrupt controller timestamp counter if available */ |
---|
[75acd9e] | 227 | leon3_tc.tc.tc_get_timecount = leon3_tc_get_timecount_irqmp; |
---|
| 228 | leon3_tc.tc.tc_counter_mask = 0xffffffff; |
---|
| 229 | leon3_tc.tc.tc_frequency = ambapp_freq_get(&ambapp_plb, LEON3_Timer_Adev); |
---|
| 230 | leon3_tc.tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER; |
---|
[291945f1] | 231 | leon3_tc_tick = leon3_tc_tick_irqmp_timestamp_init; |
---|
[75acd9e] | 232 | rtems_timecounter_install(&leon3_tc.tc); |
---|
| 233 | } else { |
---|
[5a0b27ed] | 234 | #ifdef RTEMS_SMP |
---|
| 235 | /* |
---|
| 236 | * The GR712RC for example has no timestamp unit in the interrupt |
---|
| 237 | * controller. At least on SMP configurations we must use a second timer |
---|
| 238 | * in free running mode for the timecounter. |
---|
| 239 | */ |
---|
[a4ff2a2] | 240 | gpt->timer[LEON3_CLOCK_INDEX + 1].ctrl = |
---|
[5a0b27ed] | 241 | GPTIMER_TIMER_CTRL_EN | GPTIMER_TIMER_CTRL_IE; |
---|
| 242 | leon3_tc.tc.tc_get_timecount = leon3_tc_get_timecount_second_timer; |
---|
| 243 | leon3_tc.tc.tc_counter_mask = 0xffffffff; |
---|
| 244 | leon3_tc.tc.tc_frequency = LEON3_GPTIMER_0_FREQUENCY_SET_BY_BOOT_LOADER; |
---|
| 245 | leon3_tc.tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER; |
---|
| 246 | leon3_tc_tick = leon3_tc_tick_second_timer; |
---|
| 247 | rtems_timecounter_install(&leon3_tc.tc); |
---|
| 248 | #else |
---|
| 249 | leon3_tc_tick = leon3_tc_tick_simple; |
---|
[75acd9e] | 250 | rtems_timecounter_simple_install( |
---|
| 251 | &leon3_tc, |
---|
| 252 | LEON3_GPTIMER_0_FREQUENCY_SET_BY_BOOT_LOADER, |
---|
| 253 | rtems_configuration_get_microseconds_per_tick(), |
---|
| 254 | leon3_tc_get_timecount |
---|
| 255 | ); |
---|
[5a0b27ed] | 256 | #endif |
---|
[75acd9e] | 257 | } |
---|
| 258 | } |
---|
| 259 | |
---|
[29d1fce] | 260 | #define Clock_driver_support_initialize_hardware() \ |
---|
[75acd9e] | 261 | leon3_clock_initialize() |
---|
[29d1fce] | 262 | |
---|
| 263 | #define Clock_driver_support_shutdown_hardware() \ |
---|
| 264 | do { \ |
---|
| 265 | LEON_Mask_interrupt(LEON_TRAP_TYPE(clkirq)); \ |
---|
[226d48d8] | 266 | LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].ctrl = 0; \ |
---|
[29d1fce] | 267 | } while (0) |
---|
| 268 | |
---|
[5a0b27ed] | 269 | #define Clock_driver_timecounter_tick() leon3_tc_do_tick() |
---|
[0e58c4f] | 270 | |
---|
[8ce272b] | 271 | #include "../../../shared/clockdrv_shell.h" |
---|
[e428dc4a] | 272 | |
---|
| 273 | #endif |
---|