sp804_delay_timer.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <drivers/arm/sp804_delay_timer.h>
  8. #include <drivers/delay_timer.h>
  9. #include <lib/mmio.h>
  10. uintptr_t sp804_base_addr;
  11. #define SP804_TIMER1_LOAD (sp804_base_addr + 0x000)
  12. #define SP804_TIMER1_VALUE (sp804_base_addr + 0x004)
  13. #define SP804_TIMER1_CONTROL (sp804_base_addr + 0x008)
  14. #define SP804_TIMER1_BGLOAD (sp804_base_addr + 0x018)
  15. #define TIMER_CTRL_ONESHOT (1 << 0)
  16. #define TIMER_CTRL_32BIT (1 << 1)
  17. #define TIMER_CTRL_DIV1 (0 << 2)
  18. #define TIMER_CTRL_DIV16 (1 << 2)
  19. #define TIMER_CTRL_DIV256 (2 << 2)
  20. #define TIMER_CTRL_IE (1 << 5)
  21. #define TIMER_CTRL_PERIODIC (1 << 6)
  22. #define TIMER_CTRL_ENABLE (1 << 7)
  23. /********************************************************************
  24. * The SP804 timer delay function
  25. ********************************************************************/
  26. uint32_t sp804_get_timer_value(void)
  27. {
  28. return mmio_read_32(SP804_TIMER1_VALUE);
  29. }
  30. /********************************************************************
  31. * Initialize the 1st timer in the SP804 dual timer with a base
  32. * address and a timer ops
  33. ********************************************************************/
  34. void sp804_timer_ops_init(uintptr_t base_addr, const timer_ops_t *ops)
  35. {
  36. assert(base_addr != 0);
  37. assert(ops != 0 && ops->get_timer_value == sp804_get_timer_value);
  38. sp804_base_addr = base_addr;
  39. timer_init(ops);
  40. /* disable timer1 */
  41. mmio_write_32(SP804_TIMER1_CONTROL, 0);
  42. mmio_write_32(SP804_TIMER1_LOAD, UINT32_MAX);
  43. mmio_write_32(SP804_TIMER1_VALUE, UINT32_MAX);
  44. /* enable as a free running 32-bit counter */
  45. mmio_write_32(SP804_TIMER1_CONTROL,
  46. TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE);
  47. }