summaryrefslogtreecommitdiffstats
path: root/bsps/shared/dev/clock/xil-ttc.c
blob: 624845d71caaf19f5807f2ccc3922c48d0a772c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup RTEMSDriverClockXilTTC
 *
 * @brief This source file contains a Clock Driver implementation using the
 *   Xilinx Triple Timer Counter (TTC).
 */

/*
 * Copyright (C) 2024 embedded brains GmbH & Co. KG
 * Copyright (C) 2023 Reflex Aerospace GmbH
 *
 * Written by Philip Kirkpatrick <p.kirkpatrick@reflexaerospace.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <bsp.h>
#include <bsp/irq.h>
#include <bsp/fatal.h>
#include <dev/clock/xttcps_hw.h>
#include <rtems/sysinit.h>
#include <rtems/timecounter.h>

#if XTTCPS_COUNT_VALUE_MASK != UINT32_MAX
#error "unexpected XTTCPS_COUNT_VALUE_MASK value"
#endif

/**
 * @defgroup RTEMSDriverClockXilTTC \
 *   Xilinx Triple Timer Counter (TTC) Clock Driver
 *
 * @ingroup RTEMSDriverClockImpl
 *
 * @brief This group contains the Xilinx Triple Timer Counter (TTC) Clock
 *   Driver implementation.
 *
 * @{
 */

uint32_t _CPU_Counter_frequency( void )
{
  return XIL_CLOCK_TTC_REFERENCE_CLOCK;
}

CPU_Counter_ticks _CPU_Counter_read(void)
{
  return XTtcPs_ReadReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_COUNT_VALUE_OFFSET);
}

static void xil_ttc_initialize(void)
{
  /* Do not use a prescaler to get a high resolution time source */
  XTtcPs_WriteReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_CLK_CNTRL_OFFSET, 0);

  /* Disable interupts */
  XTtcPs_WriteReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_IER_OFFSET, 0);

  /*
   * Enable the timer, do not enable waveform output, increment up, use
   * overflow mode, enable match mode.
   */
  XTtcPs_WriteReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_CNT_CNTRL_OFFSET,
                  XTTCPS_CNT_CNTRL_EN_WAVE_MASK | XTTCPS_CNT_CNTRL_MATCH_MASK);
}

RTEMS_SYSINIT_ITEM(
  xil_ttc_initialize,
  RTEMS_SYSINIT_CPU_COUNTER,
  RTEMS_SYSINIT_ORDER_MIDDLE
);

typedef struct {
  struct timecounter base;
  uint32_t irq_match_interval;
} xil_ttc_timecounter;

static xil_ttc_timecounter xil_ttc_clock_instance;

static uint32_t xil_ttc_get_timecount(struct timecounter *tc)
{
  (void) tc;
  return XTtcPs_ReadReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_COUNT_VALUE_OFFSET);
}

static void xil_ttc_clock_driver_support_initialize_hardware(void)
{
  xil_ttc_timecounter *tc;
  uint64_t frequency;
  uint32_t irq_match_interval;
  uint32_t count;

  tc = &xil_ttc_clock_instance;
  frequency = XIL_CLOCK_TTC_REFERENCE_CLOCK;
  irq_match_interval = (uint32_t)
    ((frequency * rtems_configuration_get_microseconds_per_tick()) / 1000000);

  /* Setup match register to generate clock interrupts */
  count = XTtcPs_ReadReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_COUNT_VALUE_OFFSET);
  XTtcPs_WriteReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_MATCH_0_OFFSET,
                  count + irq_match_interval);

  /* Clear interupts (clear on read) */
  (void) XTtcPs_ReadReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_ISR_OFFSET);

  /* Enable interupt for match register */
  XTtcPs_WriteReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_IER_OFFSET,
                  XTTCPS_IXR_MATCH_0_MASK);

  /* Install timecounter */
  tc->irq_match_interval = irq_match_interval;
  tc->base.tc_counter_mask = UINT32_MAX;
  tc->base.tc_frequency = XIL_CLOCK_TTC_REFERENCE_CLOCK;
  tc->base.tc_get_timecount = xil_ttc_get_timecount;
  tc->base.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
  rtems_timecounter_install(&tc->base);
}

static void xil_ttc_clock_driver_support_at_tick(xil_ttc_timecounter *tc)
{
  uint32_t irq_match_interval;
  uint32_t count;
  uint32_t match;

  irq_match_interval = tc->irq_match_interval;

  /* Update match register */
  match = XTtcPs_ReadReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_MATCH_0_OFFSET);
  match += irq_match_interval;
  XTtcPs_WriteReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_MATCH_0_OFFSET, match);

  /* Clear interupts (clear on read) */
  (void) XTtcPs_ReadReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_ISR_OFFSET);

  /* Check that the new match value is in the future */
  count = XTtcPs_ReadReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_COUNT_VALUE_OFFSET);

  while (RTEMS_PREDICT_FALSE(match - count > irq_match_interval)) {
    /*
     * Tick misses may happen if interrupts are disabled for an extremly long
     * period or while debugging.
     */
    rtems_timecounter_tick();

    /* Update match register */
    match += irq_match_interval;
    XTtcPs_WriteReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_MATCH_0_OFFSET, match);

    /* Clear interupts (clear on read) */
    (void) XTtcPs_ReadReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_ISR_OFFSET);

    /* Maybe the new match value is now in the future */
    count = XTtcPs_ReadReg(XIL_CLOCK_TTC_BASE_ADDR, XTTCPS_COUNT_VALUE_OFFSET);
  }
}

static rtems_interrupt_entry xil_ttc_interrupt_entry;

static void xil_ttc_clock_driver_support_install_isr(
  rtems_interrupt_handler handler
)
{
  rtems_status_code sc;

  rtems_interrupt_entry_initialize(
    &xil_ttc_interrupt_entry,
    handler,
    &xil_ttc_clock_instance,
    "Clock"
  );
  sc = rtems_interrupt_entry_install(
    XIL_CLOCK_TTC_IRQ,
    RTEMS_INTERRUPT_UNIQUE,
    &xil_ttc_interrupt_entry
  );
  if ( sc != RTEMS_SUCCESSFUL ) {
    bsp_fatal(XIL_FATAL_TTC_IRQ_INSTALL);
  }
}

#define Clock_driver_support_at_tick(arg) \
  xil_ttc_clock_driver_support_at_tick(arg)

#define Clock_driver_support_initialize_hardware \
  xil_ttc_clock_driver_support_initialize_hardware

#define Clock_driver_support_install_isr(isr) \
  xil_ttc_clock_driver_support_install_isr(isr)

/** @} */

#include "../../../shared/dev/clock/clockimpl.h"