socfpga_delay_timer.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright (c) 2019-2022, ARM Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2024, Altera Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <arch_helpers.h>
  9. #include <drivers/delay_timer.h>
  10. #include <lib/mmio.h>
  11. #include "socfpga_plat_def.h"
  12. #if PLATFORM_MODEL == PLAT_SOCFPGA_AGILEX
  13. #include "agilex_clock_manager.h"
  14. #elif PLATFORM_MODEL == PLAT_SOCFPGA_N5X
  15. #include "n5x_clock_manager.h"
  16. #elif PLATFORM_MODEL == PLAT_SOCFPGA_STRATIX10
  17. #include "s10_clock_manager.h"
  18. #endif
  19. #define SOCFPGA_GLOBAL_TIMER PLAT_TIMER_BASE_ADDR
  20. #define SOCFPGA_GLOBAL_TIMER_EN 0x3
  21. static timer_ops_t plat_timer_ops;
  22. /********************************************************************
  23. * The timer delay function
  24. ********************************************************************/
  25. static uint32_t socfpga_get_timer_value(void)
  26. {
  27. /*
  28. * Generic delay timer implementation expects the timer to be a down
  29. * counter. We apply bitwise NOT operator to the tick values returned
  30. * by read_cntpct_el0() to simulate the down counter. The value is
  31. * clipped from 64 to 32 bits.
  32. */
  33. return (uint32_t)(~read_cntpct_el0());
  34. }
  35. void socfpga_delay_timer_init_args(void)
  36. {
  37. plat_timer_ops.get_timer_value = socfpga_get_timer_value;
  38. plat_timer_ops.clk_mult = 1;
  39. plat_timer_ops.clk_div = PLAT_SYS_COUNTER_FREQ_IN_MHZ;
  40. timer_init(&plat_timer_ops);
  41. }
  42. void socfpga_delay_timer_init(void)
  43. {
  44. socfpga_delay_timer_init_args();
  45. mmio_write_32(SOCFPGA_GLOBAL_TIMER, SOCFPGA_GLOBAL_TIMER_EN);
  46. asm volatile("msr cntp_ctl_el0, %0" : : "r" (SOCFPGA_GLOBAL_TIMER_EN));
  47. asm volatile("msr cntp_tval_el0, %0" : : "r" (~0));
  48. }