2015-03-17 14:50:05 +00:00
|
|
|
/*
|
2019-10-31 08:35:02 +00:00
|
|
|
* Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
|
2015-03-17 14:50:05 +00:00
|
|
|
*
|
2017-05-03 09:38:09 +01:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2015-03-17 14:50:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2018-12-14 00:18:21 +00:00
|
|
|
|
2015-03-17 14:50:05 +00:00
|
|
|
#include <platform_def.h>
|
2018-12-14 00:18:21 +00:00
|
|
|
|
|
|
|
#include <drivers/delay_timer.h>
|
|
|
|
#include <lib/utils_def.h>
|
2015-03-17 14:50:05 +00:00
|
|
|
|
|
|
|
/***********************************************************
|
|
|
|
* The delay timer implementation
|
|
|
|
***********************************************************/
|
2018-02-12 12:36:17 +00:00
|
|
|
static const timer_ops_t *timer_ops;
|
2015-03-17 14:50:05 +00:00
|
|
|
|
|
|
|
/***********************************************************
|
|
|
|
* Delay for the given number of microseconds. The driver must
|
|
|
|
* be initialized before calling this function.
|
|
|
|
***********************************************************/
|
|
|
|
void udelay(uint32_t usec)
|
|
|
|
{
|
2018-09-19 14:23:03 +01:00
|
|
|
assert((timer_ops != NULL) &&
|
|
|
|
(timer_ops->clk_mult != 0U) &&
|
|
|
|
(timer_ops->clk_div != 0U) &&
|
2018-02-12 12:36:17 +00:00
|
|
|
(timer_ops->get_timer_value != NULL));
|
2015-03-17 14:50:05 +00:00
|
|
|
|
2019-10-31 08:35:02 +00:00
|
|
|
uint32_t start, delta;
|
|
|
|
uint64_t total_delta;
|
2016-05-18 10:37:25 +01:00
|
|
|
|
2019-10-31 08:35:02 +00:00
|
|
|
assert(usec < (UINT64_MAX / timer_ops->clk_div));
|
2015-03-17 14:50:05 +00:00
|
|
|
|
2018-02-12 12:36:17 +00:00
|
|
|
start = timer_ops->get_timer_value();
|
2016-05-18 10:37:25 +01:00
|
|
|
|
2018-01-22 22:02:03 +00:00
|
|
|
/* Add an extra tick to avoid delaying less than requested. */
|
2018-02-12 12:36:17 +00:00
|
|
|
total_delta =
|
2019-10-31 08:35:02 +00:00
|
|
|
div_round_up((uint64_t)usec * timer_ops->clk_div,
|
2018-09-19 14:23:03 +01:00
|
|
|
timer_ops->clk_mult) + 1U;
|
2019-10-31 08:35:02 +00:00
|
|
|
/*
|
|
|
|
* Precaution for the total_delta ~ UINT32_MAX and the fact that we
|
|
|
|
* cannot catch every tick of the timer.
|
|
|
|
* For example 100MHz timer over 25MHz APB will miss at least 4 ticks.
|
|
|
|
* 1000U is an arbitrary big number which is believed to be sufficient.
|
|
|
|
*/
|
|
|
|
assert(total_delta < (UINT32_MAX - 1000U));
|
2016-05-18 10:37:25 +01:00
|
|
|
|
2015-03-17 14:50:05 +00:00
|
|
|
do {
|
2016-05-18 10:37:25 +01:00
|
|
|
/*
|
|
|
|
* If the timer value wraps around, the subtraction will
|
|
|
|
* overflow and it will still give the correct result.
|
2019-10-31 08:35:02 +00:00
|
|
|
* delta is decreasing counter
|
2016-05-18 10:37:25 +01:00
|
|
|
*/
|
2019-10-31 08:35:02 +00:00
|
|
|
delta = start - timer_ops->get_timer_value();
|
2016-05-18 10:37:25 +01:00
|
|
|
|
|
|
|
} while (delta < total_delta);
|
2015-03-17 14:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************
|
|
|
|
* Delay for the given number of milliseconds. The driver must
|
|
|
|
* be initialized before calling this function.
|
|
|
|
***********************************************************/
|
|
|
|
void mdelay(uint32_t msec)
|
|
|
|
{
|
2019-10-31 08:35:02 +00:00
|
|
|
assert((msec * 1000UL) < UINT32_MAX);
|
2018-09-19 14:23:03 +01:00
|
|
|
udelay(msec * 1000U);
|
2015-03-17 14:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************
|
|
|
|
* Initialize the timer. The fields in the provided timer
|
|
|
|
* ops pointer must be valid.
|
|
|
|
***********************************************************/
|
|
|
|
void timer_init(const timer_ops_t *ops_ptr)
|
|
|
|
{
|
2018-09-19 14:23:03 +01:00
|
|
|
assert((ops_ptr != NULL) &&
|
|
|
|
(ops_ptr->clk_mult != 0U) &&
|
|
|
|
(ops_ptr->clk_div != 0U) &&
|
2017-06-07 15:42:26 +01:00
|
|
|
(ops_ptr->get_timer_value != NULL));
|
2015-03-17 14:50:05 +00:00
|
|
|
|
2018-02-12 12:36:17 +00:00
|
|
|
timer_ops = ops_ptr;
|
2015-03-17 14:50:05 +00:00
|
|
|
}
|