blob: 587724eb1a50aca7a921020928146e3d31ee1ed3 [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{
Roberto Vargas777dd432018-02-12 12:36:17 +000023 assert(timer_ops != NULL &&
24 (timer_ops->clk_mult != 0) &&
25 (timer_ops->clk_div != 0) &&
26 (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
Roberto Vargas777dd432018-02-12 12:36:17 +000030 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 =
36 div_round_up(usec * timer_ops->clk_div, timer_ops->clk_mult) + 1;
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010037
Ryan Harkin32539fc2015-03-17 14:50:05 +000038 do {
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010039 /*
40 * If the timer value wraps around, the subtraction will
41 * overflow and it will still give the correct result.
42 */
Roberto Vargas777dd432018-02-12 12:36:17 +000043 delta = start - timer_ops->get_timer_value(); /* Decreasing counter */
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010044
45 } while (delta < total_delta);
Ryan Harkin32539fc2015-03-17 14:50:05 +000046}
47
48/***********************************************************
49 * Delay for the given number of milliseconds. The driver must
50 * be initialized before calling this function.
51 ***********************************************************/
52void mdelay(uint32_t msec)
53{
54 udelay(msec*1000);
55}
56
57/***********************************************************
58 * Initialize the timer. The fields in the provided timer
59 * ops pointer must be valid.
60 ***********************************************************/
61void timer_init(const timer_ops_t *ops_ptr)
62{
Etienne Carrieref4eecb22017-06-07 16:42:26 +020063 assert(ops_ptr != NULL &&
Ryan Harkin32539fc2015-03-17 14:50:05 +000064 (ops_ptr->clk_mult != 0) &&
65 (ops_ptr->clk_div != 0) &&
Etienne Carrieref4eecb22017-06-07 16:42:26 +020066 (ops_ptr->get_timer_value != NULL));
Ryan Harkin32539fc2015-03-17 14:50:05 +000067
Roberto Vargas777dd432018-02-12 12:36:17 +000068 timer_ops = ops_ptr;
Ryan Harkin32539fc2015-03-17 14:50:05 +000069}