blob: a3fd7bfebdff6c5e972dd0f56ab07495fca65abe [file] [log] [blame]
Ryan Harkin32539fc2015-03-17 14:50:05 +00001/*
Max Shvetsov72a76632019-10-31 08:35:02 +00002 * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
Ryan Harkin32539fc2015-03-17 14:50:05 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Ryan Harkin32539fc2015-03-17 14:50:05 +00005 */
6
7#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
Ryan Harkin32539fc2015-03-17 14:50:05 +00009#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
11#include <drivers/delay_timer.h>
12#include <lib/utils_def.h>
Ryan Harkin32539fc2015-03-17 14:50:05 +000013
14/***********************************************************
15 * The delay timer implementation
16 ***********************************************************/
Roberto Vargas777dd432018-02-12 12:36:17 +000017static const timer_ops_t *timer_ops;
Ryan Harkin32539fc2015-03-17 14:50:05 +000018
19/***********************************************************
20 * Delay for the given number of microseconds. The driver must
21 * be initialized before calling this function.
22 ***********************************************************/
23void udelay(uint32_t usec)
24{
Sathees Balya006b15e2018-09-19 14:23:03 +010025 assert((timer_ops != NULL) &&
26 (timer_ops->clk_mult != 0U) &&
27 (timer_ops->clk_div != 0U) &&
Roberto Vargas777dd432018-02-12 12:36:17 +000028 (timer_ops->get_timer_value != NULL));
Ryan Harkin32539fc2015-03-17 14:50:05 +000029
Max Shvetsov72a76632019-10-31 08:35:02 +000030 uint32_t start, delta;
31 uint64_t total_delta;
Ryan Harkin32539fc2015-03-17 14:50:05 +000032
Max Shvetsov72a76632019-10-31 08:35:02 +000033 assert(usec < (UINT64_MAX / timer_ops->clk_div));
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010034
Roberto Vargas777dd432018-02-12 12:36:17 +000035 start = timer_ops->get_timer_value();
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010036
Julius Wernerb0a432d2018-01-22 14:02:03 -080037 /* Add an extra tick to avoid delaying less than requested. */
Roberto Vargas777dd432018-02-12 12:36:17 +000038 total_delta =
Max Shvetsov72a76632019-10-31 08:35:02 +000039 div_round_up((uint64_t)usec * timer_ops->clk_div,
Sathees Balya006b15e2018-09-19 14:23:03 +010040 timer_ops->clk_mult) + 1U;
Max Shvetsov72a76632019-10-31 08:35:02 +000041 /*
42 * Precaution for the total_delta ~ UINT32_MAX and the fact that we
43 * cannot catch every tick of the timer.
44 * For example 100MHz timer over 25MHz APB will miss at least 4 ticks.
45 * 1000U is an arbitrary big number which is believed to be sufficient.
46 */
47 assert(total_delta < (UINT32_MAX - 1000U));
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010048
Ryan Harkin32539fc2015-03-17 14:50:05 +000049 do {
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010050 /*
51 * If the timer value wraps around, the subtraction will
52 * overflow and it will still give the correct result.
Max Shvetsov72a76632019-10-31 08:35:02 +000053 * delta is decreasing counter
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010054 */
Max Shvetsov72a76632019-10-31 08:35:02 +000055 delta = start - timer_ops->get_timer_value();
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010056
57 } while (delta < total_delta);
Ryan Harkin32539fc2015-03-17 14:50:05 +000058}
59
60/***********************************************************
61 * Delay for the given number of milliseconds. The driver must
62 * be initialized before calling this function.
63 ***********************************************************/
64void mdelay(uint32_t msec)
65{
Max Shvetsov72a76632019-10-31 08:35:02 +000066 assert((msec * 1000UL) < UINT32_MAX);
Sathees Balya006b15e2018-09-19 14:23:03 +010067 udelay(msec * 1000U);
Ryan Harkin32539fc2015-03-17 14:50:05 +000068}
69
70/***********************************************************
71 * Initialize the timer. The fields in the provided timer
72 * ops pointer must be valid.
73 ***********************************************************/
74void timer_init(const timer_ops_t *ops_ptr)
75{
Sathees Balya006b15e2018-09-19 14:23:03 +010076 assert((ops_ptr != NULL) &&
77 (ops_ptr->clk_mult != 0U) &&
78 (ops_ptr->clk_div != 0U) &&
Etienne Carrieref4eecb22017-06-07 16:42:26 +020079 (ops_ptr->get_timer_value != NULL));
Ryan Harkin32539fc2015-03-17 14:50:05 +000080
Roberto Vargas777dd432018-02-12 12:36:17 +000081 timer_ops = ops_ptr;
Ryan Harkin32539fc2015-03-17 14:50:05 +000082}