rdtime.c 676 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018, Anup Patel <anup@brainfault.org>
  4. * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
  5. *
  6. * The riscv_get_time() API implementation that is using the
  7. * standard rdtime instruction.
  8. */
  9. #include <common.h>
  10. /* Implement the API required by RISC-V timer driver */
  11. int riscv_get_time(u64 *time)
  12. {
  13. #ifdef CONFIG_64BIT
  14. u64 n;
  15. __asm__ __volatile__ (
  16. "rdtime %0"
  17. : "=r" (n));
  18. *time = n;
  19. #else
  20. u32 lo, hi, tmp;
  21. __asm__ __volatile__ (
  22. "1:\n"
  23. "rdtimeh %0\n"
  24. "rdtime %1\n"
  25. "rdtimeh %2\n"
  26. "bne %0, %2, 1b"
  27. : "=&r" (hi), "=&r" (lo), "=&r" (tmp));
  28. *time = ((u64)hi << 32) | lo;
  29. #endif
  30. return 0;
  31. }