blob: feac3579fec5abcf9adbc41e392018126aa760e5 [file] [log] [blame]
Ryan Harkin32539fc2015-03-17 14:50:05 +00001/*
Roberto Vargas777dd432018-02-12 12:36:17 +00002 * Copyright (c) 2015-2018, 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>
8#include <delay_timer.h>
9#include <platform_def.h>
Julius Wernerb0a432d2018-01-22 14:02:03 -080010#include <utils_def.h>
Ryan Harkin32539fc2015-03-17 14:50:05 +000011
12/***********************************************************
13 * The delay timer implementation
14 ***********************************************************/
Roberto Vargas777dd432018-02-12 12:36:17 +000015static const timer_ops_t *timer_ops;
Ryan Harkin32539fc2015-03-17 14:50:05 +000016
17/***********************************************************
18 * Delay for the given number of microseconds. The driver must
19 * be initialized before calling this function.
20 ***********************************************************/
21void udelay(uint32_t usec)
22{
Sathees Balya006b15e2018-09-19 14:23:03 +010023 assert((timer_ops != NULL) &&
24 (timer_ops->clk_mult != 0U) &&
25 (timer_ops->clk_div != 0U) &&
Roberto Vargas777dd432018-02-12 12:36:17 +000026 (timer_ops->get_timer_value != NULL));
Ryan Harkin32539fc2015-03-17 14:50:05 +000027
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010028 uint32_t start, delta, total_delta;
Ryan Harkin32539fc2015-03-17 14:50:05 +000029
Sathees Balya006b15e2018-09-19 14:23:03 +010030 assert(usec < (UINT32_MAX / timer_ops->clk_div));
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010031
Roberto Vargas777dd432018-02-12 12:36:17 +000032 start = timer_ops->get_timer_value();
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010033
Julius Wernerb0a432d2018-01-22 14:02:03 -080034 /* Add an extra tick to avoid delaying less than requested. */
Roberto Vargas777dd432018-02-12 12:36:17 +000035 total_delta =
Sathees Balya006b15e2018-09-19 14:23:03 +010036 div_round_up(usec * timer_ops->clk_div,
37 timer_ops->clk_mult) + 1U;
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010038
Ryan Harkin32539fc2015-03-17 14:50:05 +000039 do {
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010040 /*
41 * If the timer value wraps around, the subtraction will
42 * overflow and it will still give the correct result.
43 */
Roberto Vargas777dd432018-02-12 12:36:17 +000044 delta = start - timer_ops->get_timer_value(); /* Decreasing counter */
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010045
46 } while (delta < total_delta);
Ryan Harkin32539fc2015-03-17 14:50:05 +000047}
48
49/***********************************************************
50 * Delay for the given number of milliseconds. The driver must
51 * be initialized before calling this function.
52 ***********************************************************/
53void mdelay(uint32_t msec)
54{
Sathees Balya006b15e2018-09-19 14:23:03 +010055 udelay(msec * 1000U);
Ryan Harkin32539fc2015-03-17 14:50:05 +000056}
57
58/***********************************************************
59 * Initialize the timer. The fields in the provided timer
60 * ops pointer must be valid.
61 ***********************************************************/
62void timer_init(const timer_ops_t *ops_ptr)
63{
Sathees Balya006b15e2018-09-19 14:23:03 +010064 assert((ops_ptr != NULL) &&
65 (ops_ptr->clk_mult != 0U) &&
66 (ops_ptr->clk_div != 0U) &&
Etienne Carrieref4eecb22017-06-07 16:42:26 +020067 (ops_ptr->get_timer_value != NULL));
Ryan Harkin32539fc2015-03-17 14:50:05 +000068
Roberto Vargas777dd432018-02-12 12:36:17 +000069 timer_ops = ops_ptr;
Ryan Harkin32539fc2015-03-17 14:50:05 +000070}