source: rtems/c/src/lib/libbsp/sparc/leon3/clock/ckinit.c @ 75acd9e

4.115
Last change on this file since 75acd9e was 75acd9e, checked in by Alexander Krutwig <alexander.krutwig@…>, on 04/01/15 at 13:33:25

bsps: Convert clock drivers to use a timecounter

Update #2271.

  • Property mode set to 100644
File size: 5.0 KB
Line 
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 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
18 *  http://www.rtems.org/license/LICENSE.
19 */
20
21#include <bsp.h>
22#include <bspopts.h>
23#include <bsp/fatal.h>
24#include <rtems/rtems/intr.h>
25#include <ambapp.h>
26#include <rtems/score/profiling.h>
27#include <rtems/timecounter.h>
28
29/* The LEON3 BSP Timer driver can rely on the Driver Manager if the
30 * DrvMgr is initialized during startup. Otherwise the classic driver
31 * must be used.
32 *
33 * The DrvMgr Clock driver is located in the shared/timer directory
34 */
35#ifndef RTEMS_DRVMGR_STARTUP
36
37#if SIMSPARC_FAST_IDLE==1
38#define CLOCK_DRIVER_USE_FAST_IDLE 1
39#endif
40
41/* LEON3 Timer system interrupt number */
42static int clkirq;
43
44static bool leon3_tc_use_irqmp;
45
46static rtems_timecounter_simple leon3_tc;
47
48static uint32_t leon3_tc_get(rtems_timecounter_simple *tc)
49{
50  return LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].value;
51}
52
53static bool leon3_tc_is_pending(rtems_timecounter_simple *tc)
54{
55  return LEON_Is_interrupt_pending(clkirq);
56}
57
58static uint32_t leon3_tc_get_timecount(struct timecounter *tc)
59{
60  return rtems_timecounter_simple_downcounter_get(
61    tc,
62    leon3_tc_get,
63    leon3_tc_is_pending
64  );
65}
66
67static uint32_t leon3_tc_get_timecount_irqmp(struct timecounter *tc)
68{
69  return LEON3_IrqCtrl_Regs->timestamp[0].counter;
70}
71
72static void leon3_tc_tick(void)
73{
74  if (leon3_tc_use_irqmp) {
75    rtems_timecounter_tick();
76  } else {
77    rtems_timecounter_simple_downcounter_tick(&leon3_tc, leon3_tc_get);
78  }
79}
80
81static void leon3_clock_profiling_interrupt_delay(void)
82{
83#ifdef RTEMS_PROFILING
84  /*
85   * We need a small state machine to ignore the first clock interrupt, since
86   * it contains the sequential system initialization time.  Do the timestamp
87   * initialization on the fly.
88   */
89  static int state = 1;
90
91  volatile struct irqmp_timestamp_regs *irqmp_ts =
92    &LEON3_IrqCtrl_Regs->timestamp[0];
93  unsigned int s1_s2 = (1U << 25) | (1U << 26);
94
95  if (state == 0) {
96    unsigned int first = irqmp_ts->assertion;
97    unsigned int second = irqmp_ts->counter;
98
99    irqmp_ts->control |= s1_s2;
100
101    _Profiling_Update_max_interrupt_delay(_Per_CPU_Get(), second - first);
102  } else if (state == 1 && leon3_irqmp_has_timestamp(irqmp_ts)) {
103    unsigned int ks = 1U << 5;
104
105    state = 0;
106
107    irqmp_ts->control = ks | s1_s2 | (unsigned int) clkirq;
108  } else if (state == 1) {
109    state = 2;
110  }
111#endif
112}
113
114#define Clock_driver_support_at_tick() \
115  do { \
116    leon3_clock_profiling_interrupt_delay(); \
117  } while (0)
118
119#define Adjust_clkirq_for_node() do { clkirq += LEON3_CLOCK_INDEX; } while(0)
120
121#define Clock_driver_support_find_timer() \
122  do { \
123    /* Assume timer found during BSP initialization */ \
124    if (LEON3_Timer_Regs) { \
125      clkirq = (LEON3_Timer_Regs->cfg & 0xf8) >> 3; \
126      \
127      Adjust_clkirq_for_node(); \
128    } \
129  } while (0)
130
131#define Clock_driver_support_install_isr( _new, _old ) \
132  do { \
133    (_old) = NULL; \
134    bsp_clock_handler_install(_new); \
135  } while(0)
136
137static void bsp_clock_handler_install(rtems_isr *new)
138{
139  rtems_status_code sc;
140
141  sc = rtems_interrupt_handler_install(
142    clkirq,
143    "Clock",
144    RTEMS_INTERRUPT_UNIQUE,
145    new,
146    NULL
147  );
148  if (sc != RTEMS_SUCCESSFUL) {
149    rtems_fatal(RTEMS_FATAL_SOURCE_BSP, LEON3_FATAL_CLOCK_INITIALIZATION);
150  }
151}
152
153static void leon3_clock_initialize(void)
154{
155  volatile struct irqmp_timestamp_regs *irqmp_ts =
156    &LEON3_IrqCtrl_Regs->timestamp[0];
157
158  LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].reload =
159    rtems_configuration_get_microseconds_per_tick() - 1;
160  LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].ctrl =
161    GPTIMER_TIMER_CTRL_EN | GPTIMER_TIMER_CTRL_RS |
162      GPTIMER_TIMER_CTRL_LD | GPTIMER_TIMER_CTRL_IE;
163
164  if (leon3_irqmp_has_timestamp(irqmp_ts)) {
165    leon3_tc.tc.tc_get_timecount = leon3_tc_get_timecount_irqmp;
166    leon3_tc.tc.tc_counter_mask = 0xffffffff;
167    leon3_tc.tc.tc_frequency = ambapp_freq_get(&ambapp_plb, LEON3_Timer_Adev);
168    leon3_tc.tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
169    leon3_tc_use_irqmp = true;
170    rtems_timecounter_install(&leon3_tc.tc);
171  } else {
172    rtems_timecounter_simple_install(
173      &leon3_tc,
174      LEON3_GPTIMER_0_FREQUENCY_SET_BY_BOOT_LOADER,
175      rtems_configuration_get_microseconds_per_tick(),
176      leon3_tc_get_timecount
177    );
178  }
179}
180
181#define Clock_driver_support_initialize_hardware() \
182  leon3_clock_initialize()
183
184#define Clock_driver_support_shutdown_hardware() \
185  do { \
186    LEON_Mask_interrupt(LEON_TRAP_TYPE(clkirq)); \
187    LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].ctrl = 0; \
188  } while (0)
189
190#define Clock_driver_timecounter_tick() leon3_tc_tick()
191
192#include "../../../shared/clockdrv_shell.h"
193
194#endif
Note: See TracBrowser for help on using the repository browser.