blob: 8c2996ec32cc2565f7b7bd7b3a00434bd14a1558 [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>
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
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010030 uint32_t start, delta, total_delta;
Ryan Harkin32539fc2015-03-17 14:50:05 +000031
Sathees Balya006b15e2018-09-19 14:23:03 +010032 assert(usec < (UINT32_MAX / timer_ops->clk_div));
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010033
Roberto Vargas777dd432018-02-12 12:36:17 +000034 start = timer_ops->get_timer_value();
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010035
Julius Wernerb0a432d2018-01-22 14:02:03 -080036 /* Add an extra tick to avoid delaying less than requested. */
Roberto Vargas777dd432018-02-12 12:36:17 +000037 total_delta =
Sathees Balya006b15e2018-09-19 14:23:03 +010038 div_round_up(usec * timer_ops->clk_div,
39 timer_ops->clk_mult) + 1U;
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010040
Ryan Harkin32539fc2015-03-17 14:50:05 +000041 do {
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010042 /*
43 * If the timer value wraps around, the subtraction will
44 * overflow and it will still give the correct result.
45 */
Roberto Vargas777dd432018-02-12 12:36:17 +000046 delta = start - timer_ops->get_timer_value(); /* Decreasing counter */
Antonio Nino Diazbcd79b22016-05-18 10:37:25 +010047
48 } while (delta < total_delta);
Ryan Harkin32539fc2015-03-17 14:50:05 +000049}
50
51/***********************************************************
52 * Delay for the given number of milliseconds. The driver must
53 * be initialized before calling this function.
54 ***********************************************************/
55void mdelay(uint32_t msec)
56{
Sathees Balya006b15e2018-09-19 14:23:03 +010057 udelay(msec * 1000U);
Ryan Harkin32539fc2015-03-17 14:50:05 +000058}
59
60/***********************************************************
61 * Initialize the timer. The fields in the provided timer
62 * ops pointer must be valid.
63 ***********************************************************/
64void timer_init(const timer_ops_t *ops_ptr)
65{
Sathees Balya006b15e2018-09-19 14:23:03 +010066 assert((ops_ptr != NULL) &&
67 (ops_ptr->clk_mult != 0U) &&
68 (ops_ptr->clk_div != 0U) &&
Etienne Carrieref4eecb22017-06-07 16:42:26 +020069 (ops_ptr->get_timer_value != NULL));
Ryan Harkin32539fc2015-03-17 14:50:05 +000070
Roberto Vargas777dd432018-02-12 12:36:17 +000071 timer_ops = ops_ptr;
Ryan Harkin32539fc2015-03-17 14:50:05 +000072}