source: rtems/bsps/arm/shared/clock/clock-a9mpcore.c @ 65870044

Last change on this file since 65870044 was 65870044, checked in by Sebastian Huber <sebastian.huber@…>, on Apr 18, 2019 at 4:47:01 AM

score: Add _SMP_Broadcast_action()

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/*
2 * Copyright (c) 2013, 2016 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <info@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#include <bsp.h>
16#include <bsp/fatal.h>
17#include <bsp/irq.h>
18#include <bsp/irq-generic.h>
19#include <bsp/arm-a9mpcore-regs.h>
20#include <bsp/arm-a9mpcore-clock.h>
21#include <rtems/timecounter.h>
22#include <rtems/score/smpimpl.h>
23
24#define A9MPCORE_GT ((volatile a9mpcore_gt *) BSP_ARM_A9MPCORE_GT_BASE)
25
26static struct timecounter a9mpcore_tc;
27
28/* This is defined in dev/clock/clockimpl.h */
29void Clock_isr(rtems_irq_hdl_param arg);
30
31__attribute__ ((weak)) uint32_t a9mpcore_clock_periphclk(void)
32{
33  /* default to the BSP option. */
34  return BSP_ARM_A9MPCORE_PERIPHCLK;
35}
36
37static void a9mpcore_clock_at_tick(void)
38{
39  volatile a9mpcore_gt *gt = A9MPCORE_GT;
40
41  gt->irqst = A9MPCORE_GT_IRQST_EFLG;
42}
43
44static void a9mpcore_clock_handler_install(void)
45{
46  rtems_status_code sc;
47
48  sc = rtems_interrupt_handler_install(
49    A9MPCORE_IRQ_GT,
50    "Clock",
51    RTEMS_INTERRUPT_UNIQUE,
52    (rtems_interrupt_handler) Clock_isr,
53    NULL
54  );
55  if (sc != RTEMS_SUCCESSFUL) {
56    bsp_fatal(BSP_ARM_A9MPCORE_FATAL_CLOCK_IRQ_INSTALL);
57  }
58}
59
60static uint64_t a9mpcore_clock_get_counter(volatile a9mpcore_gt *gt)
61{
62  uint32_t cl;
63  uint32_t cu1;
64  uint32_t cu2;
65
66  do {
67    cu1 = gt->cntrupper;
68    cl = gt->cntrlower;
69    cu2 = gt->cntrupper;
70  } while (cu1 != cu2);
71
72  return ((uint64_t) cu2 << 32) | cl;
73}
74
75static uint32_t a9mpcore_clock_get_timecount(struct timecounter *tc)
76{
77  volatile a9mpcore_gt *gt = A9MPCORE_GT;
78
79  return gt->cntrlower;
80}
81
82static void a9mpcore_clock_gt_init(
83  volatile a9mpcore_gt *gt,
84  uint64_t cmpval,
85  uint32_t interval
86)
87{
88  gt->cmpvallower = (uint32_t) cmpval;
89  gt->cmpvalupper = (uint32_t) (cmpval >> 32);
90  gt->autoinc = interval;
91  gt->ctrl = A9MPCORE_GT_CTRL_AUTOINC_EN
92    | A9MPCORE_GT_CTRL_IRQ_EN
93    | A9MPCORE_GT_CTRL_COMP_EN
94    | A9MPCORE_GT_CTRL_TMR_EN;
95}
96
97#if defined(RTEMS_SMP) && !defined(CLOCK_DRIVER_USE_ONLY_BOOT_PROCESSOR)
98typedef struct {
99  uint64_t cmpval;
100  uint32_t interval;
101} a9mpcore_clock_init_data;
102
103static void a9mpcore_clock_secondary_action(void *arg)
104{
105  if (!_Per_CPU_Is_boot_processor(_Per_CPU_Get())) {
106    volatile a9mpcore_gt *gt = A9MPCORE_GT;
107    a9mpcore_clock_init_data *init_data = arg;
108
109    a9mpcore_clock_gt_init(gt, init_data->cmpval, init_data->interval);
110    bsp_interrupt_vector_enable(A9MPCORE_IRQ_GT);
111  }
112}
113#endif
114
115static void a9mpcore_clock_secondary_initialization(
116  volatile a9mpcore_gt *gt,
117  uint64_t cmpval,
118  uint32_t interval
119)
120{
121#if defined(RTEMS_SMP) && !defined(CLOCK_DRIVER_USE_ONLY_BOOT_PROCESSOR)
122  a9mpcore_clock_init_data init_data = {
123    .cmpval = cmpval,
124    .interval = interval
125  };
126
127  _SMP_Broadcast_action(a9mpcore_clock_secondary_action, &init_data);
128
129  if (cmpval - a9mpcore_clock_get_counter(gt) >= interval) {
130    bsp_fatal(BSP_ARM_A9MPCORE_FATAL_CLOCK_SMP_INIT);
131  }
132#endif
133}
134
135static void a9mpcore_clock_initialize(void)
136{
137  volatile a9mpcore_gt *gt = A9MPCORE_GT;
138  uint64_t periphclk = a9mpcore_clock_periphclk();
139  uint64_t us_per_tick = rtems_configuration_get_microseconds_per_tick();
140  uint32_t interval = (uint32_t) ((periphclk * us_per_tick) / 1000000);
141  uint64_t cmpval;
142
143  gt->ctrl &= A9MPCORE_GT_CTRL_TMR_EN;
144  gt->irqst = A9MPCORE_GT_IRQST_EFLG;
145
146  cmpval = a9mpcore_clock_get_counter(gt);
147  cmpval += interval;
148
149  a9mpcore_clock_gt_init(gt, cmpval, interval);
150  a9mpcore_clock_secondary_initialization(gt, cmpval, interval);
151
152  a9mpcore_tc.tc_get_timecount = a9mpcore_clock_get_timecount;
153  a9mpcore_tc.tc_counter_mask = 0xffffffff;
154  a9mpcore_tc.tc_frequency = periphclk;
155  a9mpcore_tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
156  rtems_timecounter_install(&a9mpcore_tc);
157}
158
159uint32_t _CPU_Counter_frequency(void)
160{
161  return a9mpcore_clock_periphclk();
162}
163
164CPU_Counter_ticks _CPU_Counter_read(void)
165{
166  volatile a9mpcore_gt *gt = A9MPCORE_GT;
167
168  return gt->cntrlower;
169}
170
171#define Clock_driver_support_at_tick() \
172  a9mpcore_clock_at_tick()
173
174#define Clock_driver_support_initialize_hardware() \
175  a9mpcore_clock_initialize()
176
177#define Clock_driver_support_install_isr(isr) \
178  a9mpcore_clock_handler_install()
179
180/* Include shared source clock driver code */
181#include "../../shared/dev/clock/clockimpl.h"
Note: See TracBrowser for help on using the repository browser.