generic_delay_timer.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2020, NVIDIA Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <arch_features.h>
  9. #include <arch_helpers.h>
  10. #include <common/bl_common.h>
  11. #include <common/debug.h>
  12. #include <drivers/delay_timer.h>
  13. #include <drivers/generic_delay_timer.h>
  14. #include <lib/utils_def.h>
  15. #include <plat/common/platform.h>
  16. static timer_ops_t ops;
  17. static uint32_t get_timer_value(void)
  18. {
  19. /*
  20. * Generic delay timer implementation expects the timer to be a down
  21. * counter. We apply bitwise NOT operator to the tick values returned
  22. * by read_cntpct_el0() to simulate the down counter. The value is
  23. * clipped from 64 to 32 bits.
  24. */
  25. return (uint32_t)(~read_cntpct_el0());
  26. }
  27. void generic_delay_timer_init_args(uint32_t mult, uint32_t div)
  28. {
  29. ops.get_timer_value = get_timer_value;
  30. ops.clk_mult = mult;
  31. ops.clk_div = div;
  32. timer_init(&ops);
  33. VERBOSE("Generic delay timer configured with mult=%u and div=%u\n",
  34. mult, div);
  35. }
  36. void generic_delay_timer_init(void)
  37. {
  38. assert(is_armv7_gentimer_present());
  39. /* Value in ticks */
  40. unsigned int mult = MHZ_TICKS_PER_SEC;
  41. /* Value in ticks per second (Hz) */
  42. unsigned int div = plat_get_syscnt_freq2();
  43. /* Reduce multiplier and divider by dividing them repeatedly by 10 */
  44. while (((mult % 10U) == 0U) && ((div % 10U) == 0U)) {
  45. mult /= 10U;
  46. div /= 10U;
  47. }
  48. generic_delay_timer_init_args(mult, div);
  49. }