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

4.115
Last change on this file since 89f0794 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 4.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 <ambapp.h>
24#include <rtems/score/profiling.h>
25
26#if SIMSPARC_FAST_IDLE==1
27#define CLOCK_DRIVER_USE_FAST_IDLE 1
28#endif
29
30/*
31 *  The Real Time Clock Counter Timer uses this trap type.
32 */
33
34volatile struct gptimer_regs *LEON3_Timer_Regs = 0;
35static int clkirq;
36
37#define CLOCK_VECTOR LEON_TRAP_TYPE( 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 = set_vector( _new, CLOCK_VECTOR, 1 ); \
108  } while(0)
109
110#define Clock_driver_support_initialize_hardware() \
111  do { \
112    LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].reload = \
113      rtems_configuration_get_microseconds_per_tick() - 1; \
114    \
115    LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].ctrl = \
116      LEON3_GPTIMER_EN | LEON3_GPTIMER_RL | \
117        LEON3_GPTIMER_LD | LEON3_GPTIMER_IRQEN; \
118  } while (0)
119
120#define Clock_driver_support_shutdown_hardware() \
121  do { \
122    LEON_Mask_interrupt(LEON_TRAP_TYPE(clkirq)); \
123    LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].ctrl = 0; \
124  } while (0)
125
126static uint32_t bsp_clock_nanoseconds_since_last_tick(void)
127{
128  uint32_t clicks;
129  uint32_t usecs;
130
131  if ( !LEON3_Timer_Regs )
132    return 0;
133
134  clicks = LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].value;
135
136  if ( LEON_Is_interrupt_pending( clkirq ) ) {
137    clicks = LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].value;
138    usecs = (2*rtems_configuration_get_microseconds_per_tick() - clicks);
139  } else {
140    usecs = (rtems_configuration_get_microseconds_per_tick() - clicks);
141  }
142  return usecs * 1000;
143}
144
145#define Clock_driver_nanoseconds_since_last_tick \
146        bsp_clock_nanoseconds_since_last_tick
147
148#include "../../../shared/clockdrv_shell.h"
Note: See TracBrowser for help on using the repository browser.