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

Last change on this file since 7fdf48a was 7fdf48a, checked in by Sebastian Huber <sebastian.huber@…>, on Apr 28, 2019 at 12:29:21 PM

score: Add _SMP_Othercast_action()

  • Property mode set to 100644
File size: 4.3 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  volatile a9mpcore_gt *gt = A9MPCORE_GT;
106  a9mpcore_clock_init_data *init_data = arg;
107
108  a9mpcore_clock_gt_init(gt, init_data->cmpval, init_data->interval);
109  bsp_interrupt_vector_enable(A9MPCORE_IRQ_GT);
110}
111#endif
112
113static void a9mpcore_clock_secondary_initialization(
114  volatile a9mpcore_gt *gt,
115  uint64_t cmpval,
116  uint32_t interval
117)
118{
119#if defined(RTEMS_SMP) && !defined(CLOCK_DRIVER_USE_ONLY_BOOT_PROCESSOR)
120  a9mpcore_clock_init_data init_data = {
121    .cmpval = cmpval,
122    .interval = interval
123  };
124
125  _SMP_Othercast_action(a9mpcore_clock_secondary_action, &init_data);
126
127  if (cmpval - a9mpcore_clock_get_counter(gt) >= interval) {
128    bsp_fatal(BSP_ARM_A9MPCORE_FATAL_CLOCK_SMP_INIT);
129  }
130#endif
131}
132
133static void a9mpcore_clock_initialize(void)
134{
135  volatile a9mpcore_gt *gt = A9MPCORE_GT;
136  uint64_t periphclk = a9mpcore_clock_periphclk();
137  uint64_t us_per_tick = rtems_configuration_get_microseconds_per_tick();
138  uint32_t interval = (uint32_t) ((periphclk * us_per_tick) / 1000000);
139  uint64_t cmpval;
140
141  gt->ctrl &= A9MPCORE_GT_CTRL_TMR_EN;
142  gt->irqst = A9MPCORE_GT_IRQST_EFLG;
143
144  cmpval = a9mpcore_clock_get_counter(gt);
145  cmpval += interval;
146
147  a9mpcore_clock_gt_init(gt, cmpval, interval);
148  a9mpcore_clock_secondary_initialization(gt, cmpval, interval);
149
150  a9mpcore_tc.tc_get_timecount = a9mpcore_clock_get_timecount;
151  a9mpcore_tc.tc_counter_mask = 0xffffffff;
152  a9mpcore_tc.tc_frequency = periphclk;
153  a9mpcore_tc.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
154  rtems_timecounter_install(&a9mpcore_tc);
155}
156
157uint32_t _CPU_Counter_frequency(void)
158{
159  return a9mpcore_clock_periphclk();
160}
161
162CPU_Counter_ticks _CPU_Counter_read(void)
163{
164  volatile a9mpcore_gt *gt = A9MPCORE_GT;
165
166  return gt->cntrlower;
167}
168
169#define Clock_driver_support_at_tick() \
170  a9mpcore_clock_at_tick()
171
172#define Clock_driver_support_initialize_hardware() \
173  a9mpcore_clock_initialize()
174
175#define Clock_driver_support_install_isr(isr) \
176  a9mpcore_clock_handler_install()
177
178/* Include shared source clock driver code */
179#include "../../shared/dev/clock/clockimpl.h"
Note: See TracBrowser for help on using the repository browser.