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

4.115
Last change on this file since e428dc4a was e428dc4a, checked in by Daniel Hellstrom <daniel@…>, on Dec 12, 2011 at 2:50:57 PM

LEON3: implemented BSP DRVMGR startup initialization

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