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

4.115
Last change on this file since a387e944 was a387e944, checked in by Daniel Hellstrom <daniel@…>, on 06/13/14 at 08:18:01

LEON3: use interrupt layer in clock driver

Manupilating the interrupt control registers directly instead
of going through the interrupt layer can be deceiving.

  • Property mode set to 100644
File size: 4.3 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
28#if SIMSPARC_FAST_IDLE==1
29#define CLOCK_DRIVER_USE_FAST_IDLE 1
30#endif
31
32/*
33 *  The Real Time Clock Counter Timer uses this trap type.
34 */
35
36volatile struct gptimer_regs *LEON3_Timer_Regs = 0;
37static int clkirq;
38
39static void leon3_clock_profiling_interrupt_delay(void)
40{
41#ifdef RTEMS_PROFILING
42  /*
43   * We need a small state machine to ignore the first clock interrupt, since
44   * it contains the sequential system initialization time.  Do the timestamp
45   * initialization on the fly.
46   */
47  static int state = 1;
48
49  volatile struct irqmp_timestamp_regs *irqmp_ts =
50    &LEON3_IrqCtrl_Regs->timestamp[0];
51  unsigned int s1_s2 = (1U << 25) | (1U << 26);
52
53  if (state == 0) {
54    unsigned int first = irqmp_ts->assertion;
55    unsigned int second = irqmp_ts->counter;
56
57    irqmp_ts->control |= s1_s2;
58
59    _Profiling_Update_max_interrupt_delay(_Per_CPU_Get(), second - first);
60  } else if (state == 1 && leon3_irqmp_has_timestamp(irqmp_ts)) {
61    unsigned int ks = 1U << 5;
62
63    state = 0;
64
65    irqmp_ts->control = ks | s1_s2 | (unsigned int) clkirq;
66  } else if (state == 1) {
67    state = 2;
68  }
69#endif
70}
71
72#define Clock_driver_support_at_tick() \
73  do { \
74    leon3_clock_profiling_interrupt_delay(); \
75  } while (0)
76
77#if defined(RTEMS_MULTIPROCESSING)
78  #define Adjust_clkirq_for_node() \
79    do { \
80      if (rtems_configuration_get_user_multiprocessing_table() != NULL) { \
81        clkirq += LEON3_Cpu_Index; \
82      } \
83    } while(0)
84#else
85  #define Adjust_clkirq_for_node() do { clkirq += LEON3_CLOCK_INDEX; } while(0)
86#endif
87
88#define Clock_driver_support_find_timer() \
89  do { \
90    struct ambapp_dev *adev; \
91    \
92    /* Find first LEON3 GP Timer */ \
93    adev = (void *)ambapp_for_each(&ambapp_plb, (OPTIONS_ALL|OPTIONS_APB_SLVS),\
94              VENDOR_GAISLER, GAISLER_GPTIMER, ambapp_find_by_idx, NULL); \
95    if (adev) { \
96      /* Found APB GPTIMER Timer */ \
97      LEON3_Timer_Regs = (volatile struct gptimer_regs *) \
98                         DEV_TO_APB(adev)->start; \
99      clkirq = (LEON3_Timer_Regs->cfg & 0xf8) >> 3; \
100      \
101      Adjust_clkirq_for_node(); \
102    } \
103  } while (0)
104
105#define Clock_driver_support_install_isr( _new, _old ) \
106  do { \
107    (_old) = NULL; \
108    bsp_clock_handler_install(_new); \
109  } while(0)
110
111static void bsp_clock_handler_install(rtems_isr *new)
112{
113  rtems_status_code sc;
114
115  sc = rtems_interrupt_handler_install(
116    clkirq,
117    "Clock",
118    RTEMS_INTERRUPT_UNIQUE,
119    new,
120    NULL
121  );
122  if (sc != RTEMS_SUCCESSFUL) {
123    rtems_fatal(RTEMS_FATAL_SOURCE_BSP, LEON3_FATAL_CLOCK_INITIALIZATION);
124  }
125}
126
127#define Clock_driver_support_initialize_hardware() \
128  do { \
129    LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].reload = \
130      rtems_configuration_get_microseconds_per_tick() - 1; \
131    \
132    LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].ctrl = \
133      GPTIMER_TIMER_CTRL_EN | GPTIMER_TIMER_CTRL_RS | \
134        GPTIMER_TIMER_CTRL_LD | GPTIMER_TIMER_CTRL_IE; \
135  } while (0)
136
137#define Clock_driver_support_shutdown_hardware() \
138  do { \
139    LEON_Mask_interrupt(LEON_TRAP_TYPE(clkirq)); \
140    LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].ctrl = 0; \
141  } while (0)
142
143static uint32_t bsp_clock_nanoseconds_since_last_tick(void)
144{
145  uint32_t clicks;
146  uint32_t usecs;
147
148  if ( !LEON3_Timer_Regs )
149    return 0;
150
151  clicks = LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].value;
152
153  if ( LEON_Is_interrupt_pending( clkirq ) ) {
154    clicks = LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].value;
155    usecs = (2*rtems_configuration_get_microseconds_per_tick() - clicks);
156  } else {
157    usecs = (rtems_configuration_get_microseconds_per_tick() - clicks);
158  }
159  return usecs * 1000;
160}
161
162#define Clock_driver_nanoseconds_since_last_tick \
163        bsp_clock_nanoseconds_since_last_tick
164
165#include "../../../shared/clockdrv_shell.h"
Note: See TracBrowser for help on using the repository browser.