sbi_timer.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #include <sbi/riscv_asm.h>
  10. #include <sbi/riscv_encoding.h>
  11. #include <sbi/sbi_platform.h>
  12. #include <sbi/sbi_timer.h>
  13. #if __riscv_xlen == 32
  14. u64 get_ticks(void)
  15. {
  16. u32 lo, hi, tmp;
  17. __asm__ __volatile__ (
  18. "1:\n"
  19. "rdtimeh %0\n"
  20. "rdtime %1\n"
  21. "rdtimeh %2\n"
  22. "bne %0, %2, 1b"
  23. : "=&r" (hi), "=&r" (lo), "=&r" (tmp));
  24. return ((u64)hi << 32) | lo;
  25. }
  26. #else
  27. u64 get_ticks(void)
  28. {
  29. unsigned long n;
  30. __asm__ __volatile__ (
  31. "rdtime %0"
  32. : "=r" (n));
  33. return n;
  34. }
  35. #endif
  36. u64 sbi_timer_value(struct sbi_scratch *scratch)
  37. {
  38. const struct sbi_platform *plat = sbi_platform_ptr(scratch);
  39. if (sbi_platform_has_timer_value(plat))
  40. return sbi_platform_timer_value(plat);
  41. else
  42. return get_ticks();
  43. }
  44. void sbi_timer_event_stop(struct sbi_scratch *scratch)
  45. {
  46. sbi_platform_timer_event_stop(sbi_platform_ptr(scratch));
  47. }
  48. void sbi_timer_event_start(struct sbi_scratch *scratch, u64 next_event)
  49. {
  50. sbi_platform_timer_event_start(sbi_platform_ptr(scratch),
  51. next_event);
  52. csr_clear(CSR_MIP, MIP_STIP);
  53. csr_set(CSR_MIE, MIP_MTIP);
  54. }
  55. void sbi_timer_process(struct sbi_scratch *scratch)
  56. {
  57. csr_clear(CSR_MIE, MIP_MTIP);
  58. csr_set(CSR_MIP, MIP_STIP);
  59. }
  60. int sbi_timer_init(struct sbi_scratch *scratch, bool cold_boot)
  61. {
  62. return sbi_platform_timer_init(sbi_platform_ptr(scratch),
  63. cold_boot);
  64. }