blob: 351188332b889bd232688b50a7b9f1f70783d5f1 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +00002 * Copyright (c) 2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include "timer_mps3.h"
18
alexander31ae9f02022-02-10 16:15:54 +000019#include "log_macros.h"
Kshitij Sisodiad5679cc2022-03-03 16:30:07 +000020#include "smm_mps3.h" /* Memory map for MPS3. */
alexander3c798932021-03-26 21:42:19 +000021
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000022static uint64_t cpu_cycle_count = 0; /* 64-bit cpu cycle counter */
23
24/**
25 * @brief Gets the system tick triggered cycle counter for the CPU.
26 * @return 64-bit counter value.
27 **/
28static uint64_t Get_SysTick_Cycle_Count(void);
29
30/**
31 * SysTick initialisation
32 */
33static int Init_SysTick(void);
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010034
alexander3c798932021-03-26 21:42:19 +000035void timer_reset(void)
36{
37 MPS3_FPGAIO->CLK1HZ = 0;
38 MPS3_FPGAIO->CLK100HZ = 0;
39 MPS3_FPGAIO->COUNTER = 0;
40
41 if (0 != Init_SysTick()) {
42 printf_err("Failed to initialise system tick config\n");
43 }
44 debug("system tick config ready\n");
45}
46
alexander31ae9f02022-02-10 16:15:54 +000047base_time_counter get_time_counter(void)
alexander3c798932021-03-26 21:42:19 +000048{
alexander31ae9f02022-02-10 16:15:54 +000049 base_time_counter t = {
alexander3c798932021-03-26 21:42:19 +000050 .counter_1Hz = MPS3_FPGAIO->CLK1HZ,
51 .counter_100Hz = MPS3_FPGAIO->CLK100HZ,
52 .counter_fpga = MPS3_FPGAIO->COUNTER,
53 .counter_systick = Get_SysTick_Cycle_Count()
54 };
Kshitij Sisodia3c8256d2021-05-24 16:12:40 +010055 debug("Timestamp:\n");
56 debug("\tCounter 1 Hz: %" PRIu32 "\n", t.counter_1Hz);
57 debug("\tCounter 100 Hz: %" PRIu32 "\n", t.counter_100Hz);
58 debug("\tCounter FPGA: %" PRIu32 "\n", t.counter_fpga);
59 debug("\tCounter CPU: %" PRIu64 "\n", t.counter_systick);
alexander3c798932021-03-26 21:42:19 +000060 return t;
61}
62
63/**
64 * Please note, that there are no checks for overflow in this function => if
65 * the time elapsed has been big (in days) this could happen and is currently
66 * not handled.
67 **/
alexander31ae9f02022-02-10 16:15:54 +000068uint32_t get_duration_milliseconds(base_time_counter *start,
69 base_time_counter *end)
alexander3c798932021-03-26 21:42:19 +000070{
71 uint32_t time_elapsed = 0;
72 if (end->counter_100Hz > start->counter_100Hz) {
73 time_elapsed = (end->counter_100Hz - start->counter_100Hz) * 10;
74 } else {
75 time_elapsed = (end->counter_1Hz - start->counter_1Hz) * 1000 +
76 ((0xFFFFFFFF - start->counter_100Hz) + end->counter_100Hz + 1) * 10;
77 }
78
79 /* If the time elapsed is less than 100ms, use microseconds count to be
80 * more precise */
81 if (time_elapsed < 100) {
82 debug("Using the microsecond function instead..\n");
83 return get_duration_microseconds(start, end)/1000;
84 }
85
86 return time_elapsed;
87}
88
89/**
90 * Like the microsecond counterpart, this function could return wrong results when
91 * the counter (MAINCLK) overflows. There are no overflow counters available.
92 **/
alexander31ae9f02022-02-10 16:15:54 +000093uint32_t get_duration_microseconds(base_time_counter *start,
94 base_time_counter *end)
alexander3c798932021-03-26 21:42:19 +000095{
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +000096 const int divisor = get_mps3_core_clock()/1000000;
alexander3c798932021-03-26 21:42:19 +000097 uint32_t time_elapsed = 0;
98 if (end->counter_fpga > start->counter_fpga) {
99 time_elapsed = (end->counter_fpga - start->counter_fpga)/divisor;
100 } else {
101 time_elapsed = ((0xFFFFFFFF - end->counter_fpga)
102 + start->counter_fpga + 1)/divisor;
103 }
104 return time_elapsed;
105}
106
alexander31ae9f02022-02-10 16:15:54 +0000107uint64_t get_cycle_count_diff(base_time_counter *start,
108 base_time_counter *end)
alexander3c798932021-03-26 21:42:19 +0000109{
110 if (start->counter_systick > end->counter_systick) {
111 warn("start > end; counter might have overflown\n");
112 }
113 return end->counter_systick - start->counter_systick;
114}
115
116void start_cycle_counter(void)
117{
118 /* Nothing to do for FPGA */
119}
120
121void stop_cycle_counter(void)
122{
123 /* Nothing to do for FPGA */
124}
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000125
126void SysTick_Handler(void)
127{
128 /* Increment the cycle counter based on load value. */
129 cpu_cycle_count += SysTick->LOAD + 1;
130}
131
132/**
133 * Gets the current SysTick derived counter value
134 */
135static uint64_t Get_SysTick_Cycle_Count(void)
136{
137 uint32_t systick_val;
138
139 NVIC_DisableIRQ(SysTick_IRQn);
140 systick_val = SysTick->VAL & SysTick_VAL_CURRENT_Msk;
141 NVIC_EnableIRQ(SysTick_IRQn);
142
143 return cpu_cycle_count + (SysTick->LOAD - systick_val);
144}
145
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +0000146
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000147/**
148 * SysTick initialisation
149 */
150static int Init_SysTick(void)
151{
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +0000152 const uint32_t ticks_10ms = get_mps3_core_clock()/100 + 1;
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +0000153 int err = 0;
154
155 /* Reset CPU cycle count value. */
156 cpu_cycle_count = 0;
157
158 /* Changing configuration for sys tick => guard from being
159 * interrupted. */
160 NVIC_DisableIRQ(SysTick_IRQn);
161
162 /* SysTick init - this will enable interrupt too. */
163 err = SysTick_Config(ticks_10ms);
164
165 /* Enable interrupt again. */
166 NVIC_EnableIRQ(SysTick_IRQn);
167
168 /* Wait for SysTick to kick off */
169 while (!err && !SysTick->VAL) {
170 __NOP();
171 }
172
173 return err;
174}
Kshitij Sisodiac22e80e2022-03-14 09:26:48 +0000175
176uint32_t get_mps3_core_clock(void)
177{
178 const uint32_t default_clock = 32000000 /* 32 MHz clock */;
179 static int warned_once = 0;
180 if (0 != MPS3_SCC->CFG_ACLK) {
181 if (default_clock != MPS3_SCC->CFG_ACLK) {
182 warn("System clock is different to the MPS3 config set clock.\n");
183 }
184 return MPS3_SCC->CFG_ACLK;
185 }
186
187 if (!warned_once) {
188 warn("MPS3_SCC->CFG_ACLK reads 0. Assuming default clock of %" PRIu32 "\n",
189 default_clock);
190 warned_once = 1;
191 }
192 return default_clock;
193}