2015-03-17 14:50:05 +00:00
|
|
|
/*
|
2018-02-12 12:36:17 +00:00
|
|
|
* Copyright (c) 2015-2018, 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>
|
|
|
|
#include <delay_timer.h>
|
|
|
|
#include <platform_def.h>
|
2018-01-22 22:02:03 +00:00
|
|
|
#include <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-02-12 12:36:17 +00:00
|
|
|
assert(timer_ops != NULL &&
|
|
|
|
(timer_ops->clk_mult != 0) &&
|
|
|
|
(timer_ops->clk_div != 0) &&
|
|
|
|
(timer_ops->get_timer_value != NULL));
|
2015-03-17 14:50:05 +00:00
|
|
|
|
2016-05-18 10:37:25 +01:00
|
|
|
uint32_t start, delta, total_delta;
|
|
|
|
|
2018-02-12 12:36:17 +00:00
|
|
|
assert(usec < UINT32_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 =
|
|
|
|
div_round_up(usec * timer_ops->clk_div, timer_ops->clk_mult) + 1;
|
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.
|
|
|
|
*/
|
2018-02-12 12:36:17 +00:00
|
|
|
delta = start - timer_ops->get_timer_value(); /* Decreasing counter */
|
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)
|
|
|
|
{
|
|
|
|
udelay(msec*1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************
|
|
|
|
* Initialize the timer. The fields in the provided timer
|
|
|
|
* ops pointer must be valid.
|
|
|
|
***********************************************************/
|
|
|
|
void timer_init(const timer_ops_t *ops_ptr)
|
|
|
|
{
|
2017-06-07 15:42:26 +01:00
|
|
|
assert(ops_ptr != NULL &&
|
2015-03-17 14:50:05 +00:00
|
|
|
(ops_ptr->clk_mult != 0) &&
|
|
|
|
(ops_ptr->clk_div != 0) &&
|
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
|
|
|
}
|