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

Last change on this file since f3b29236 was f3b29236, checked in by Sebastian Huber <sebastian.huber@…>, on Sep 18, 2017 at 6:22:38 AM

bsps: Clock_driver_support_install_isr()

Remove old ISR parameter since is not used by the clock driver shell.
Make an implementation optional.

Update #3139.

  • Property mode set to 100644
File size: 7.2 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 *  Copyright (c) 2014, 2016 embedded brains GmbH
17 *
18 *  The license and distribution terms for this file may be
19 *  found in the file LICENSE in this distribution or at
20 *  http://www.rtems.org/license/LICENSE.
21 */
22
23#include <bsp.h>
24#include <bspopts.h>
25#include <bsp/fatal.h>
26#include <rtems/rtems/intr.h>
27#include <ambapp.h>
28#include <rtems/score/profiling.h>
29#include <rtems/timecounter.h>
30
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
39/* LEON3 Timer system interrupt number */
40static int clkirq;
41
42static void (*leon3_tc_tick)(void);
43
44static rtems_timecounter_simple leon3_tc;
45
46#ifndef RTEMS_SMP
47static uint32_t leon3_tc_get(rtems_timecounter_simple *tc)
48{
49  return LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].value;
50}
51
52static bool leon3_tc_is_pending(rtems_timecounter_simple *tc)
53{
54  return LEON_Is_interrupt_pending(clkirq);
55}
56
57static void leon3_tc_at_tick( rtems_timecounter_simple *tc )
58{
59  /* Nothing to do */
60}
61
62static 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
71static 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
81static uint32_t leon3_tc_get_timecount_up_counter(struct timecounter *tc)
82{
83  return leon3_up_counter_low();
84}
85
86static uint32_t leon3_tc_get_timecount_irqmp(struct timecounter *tc)
87{
88  return LEON3_IrqCtrl_Regs->timestamp[0].counter;
89}
90
91#ifdef RTEMS_SMP
92static 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
98#ifdef RTEMS_PROFILING
99#define IRQMP_TIMESTAMP_S1_S2 ((1U << 25) | (1U << 26))
100
101static void leon3_tc_tick_irqmp_timestamp(void)
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
116static void leon3_tc_tick_irqmp_timestamp_init(void)
117{
118#ifdef RTEMS_PROFILING
119  /*
120   * Ignore the first clock interrupt, since it contains the sequential system
121   * initialization time.  Do the timestamp initialization on the fly.
122   */
123
124#ifdef RTEMS_SMP
125  static Atomic_Uint counter = ATOMIC_INITIALIZER_UINT(0);
126
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
133
134  volatile struct irqmp_timestamp_regs *irqmp_ts =
135    &LEON3_IrqCtrl_Regs->timestamp[0];
136  unsigned int ks = 1U << 5;
137
138  irqmp_ts->control = ks | IRQMP_TIMESTAMP_S1_S2 | (unsigned int) clkirq;
139
140  if (done) {
141    leon3_tc_tick = leon3_tc_tick_irqmp_timestamp;
142  }
143#endif
144
145  rtems_timecounter_tick();
146}
147
148#ifdef RTEMS_SMP
149static void leon3_tc_tick_second_timer(void)
150{
151  rtems_timecounter_tick();
152}
153#endif
154
155static void leon3_tc_do_tick(void)
156{
157  (*leon3_tc_tick)();
158}
159
160#define Adjust_clkirq_for_node() do { clkirq += LEON3_CLOCK_INDEX; } while(0)
161
162#define Clock_driver_support_find_timer() \
163  do { \
164    /* Assume timer found during BSP initialization */ \
165    if (LEON3_Timer_Regs) { \
166      clkirq = (LEON3_Timer_Regs->cfg & 0xf8) >> 3; \
167      \
168      Adjust_clkirq_for_node(); \
169    } \
170  } while (0)
171
172#define Clock_driver_support_install_isr( _new ) \
173  bsp_clock_handler_install(_new)
174
175static 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
191#define Clock_driver_support_set_interrupt_affinity(online_processors) \
192  bsp_interrupt_set_affinity(clkirq, online_processors)
193
194static void leon3_clock_initialize(void)
195{
196  volatile struct irqmp_timestamp_regs *irqmp_ts;
197  volatile struct gptimer_regs *gpt;
198
199  irqmp_ts = &LEON3_IrqCtrl_Regs->timestamp[0];
200  gpt = LEON3_Timer_Regs;
201
202  gpt->timer[LEON3_CLOCK_INDEX].reload =
203    rtems_configuration_get_microseconds_per_tick() - 1;
204  gpt->timer[LEON3_CLOCK_INDEX].ctrl =
205    GPTIMER_TIMER_CTRL_EN | GPTIMER_TIMER_CTRL_RS |
206      GPTIMER_TIMER_CTRL_LD | GPTIMER_TIMER_CTRL_IE;
207
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 */
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;
231    leon3_tc_tick = leon3_tc_tick_irqmp_timestamp_init;
232    rtems_timecounter_install(&leon3_tc.tc);
233  } else {
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     */
240    gpt->timer[LEON3_CLOCK_INDEX + 1].ctrl =
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;
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    );
256#endif
257  }
258}
259
260#define Clock_driver_support_initialize_hardware() \
261  leon3_clock_initialize()
262
263#define Clock_driver_support_shutdown_hardware() \
264  do { \
265    LEON_Mask_interrupt(LEON_TRAP_TYPE(clkirq)); \
266    LEON3_Timer_Regs->timer[LEON3_CLOCK_INDEX].ctrl = 0; \
267  } while (0)
268
269#define Clock_driver_timecounter_tick() leon3_tc_do_tick()
270
271#include "../../../shared/clockdrv_shell.h"
272
273#endif
Note: See TracBrowser for help on using the repository browser.