delay_timer.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2019, Linaro Limited
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #ifndef DELAY_TIMER_H
  8. #define DELAY_TIMER_H
  9. #include <stdbool.h>
  10. #include <stdint.h>
  11. #include <arch_helpers.h>
  12. /********************************************************************
  13. * A simple timer driver providing synchronous delay functionality.
  14. * The driver must be initialized with a structure that provides a
  15. * function pointer to return the timer value and a clock
  16. * multiplier/divider. The ratio of the multiplier and the divider is
  17. * the clock period in microseconds.
  18. ********************************************************************/
  19. typedef struct timer_ops {
  20. uint32_t (*get_timer_value)(void);
  21. uint32_t clk_mult;
  22. uint32_t clk_div;
  23. } timer_ops_t;
  24. static inline uint64_t timeout_cnt_us2cnt(uint32_t us)
  25. {
  26. return ((uint64_t)us * (uint64_t)read_cntfrq_el0()) / 1000000ULL;
  27. }
  28. static inline uint64_t timeout_init_us(uint32_t us)
  29. {
  30. uint64_t cnt = timeout_cnt_us2cnt(us);
  31. cnt += read_cntpct_el0();
  32. return cnt;
  33. }
  34. static inline bool timeout_elapsed(uint64_t expire_cnt)
  35. {
  36. return read_cntpct_el0() > expire_cnt;
  37. }
  38. void mdelay(uint32_t msec);
  39. void udelay(uint32_t usec);
  40. void timer_init(const timer_ops_t *ops_ptr);
  41. #endif /* DELAY_TIMER_H */