time.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/mipsregs.h>
  9. static unsigned long timestamp;
  10. /* how many counter cycles in a jiffy */
  11. #define CYCLES_PER_JIFFY \
  12. (CONFIG_SYS_MIPS_TIMER_FREQ + CONFIG_SYS_HZ / 2) / CONFIG_SYS_HZ
  13. /*
  14. * timer without interrupts
  15. */
  16. int timer_init(void)
  17. {
  18. /* Set up the timer for the first expiration. */
  19. write_c0_compare(read_c0_count() + CYCLES_PER_JIFFY);
  20. return 0;
  21. }
  22. ulong get_timer(ulong base)
  23. {
  24. unsigned int count;
  25. unsigned int expirelo = read_c0_compare();
  26. /* Check to see if we have missed any timestamps. */
  27. count = read_c0_count();
  28. while ((count - expirelo) < 0x7fffffff) {
  29. expirelo += CYCLES_PER_JIFFY;
  30. timestamp++;
  31. }
  32. write_c0_compare(expirelo);
  33. return timestamp - base;
  34. }
  35. void __udelay(unsigned long usec)
  36. {
  37. unsigned int tmo;
  38. tmo = read_c0_count() + (usec * (CONFIG_SYS_MIPS_TIMER_FREQ / 1000000));
  39. while ((tmo - read_c0_count()) < 0x7fffffff)
  40. /*NOP*/;
  41. }
  42. /*
  43. * This function is derived from PowerPC code (read timebase as long long).
  44. * On MIPS it just returns the timer value.
  45. */
  46. unsigned long long get_ticks(void)
  47. {
  48. return get_timer(0);
  49. }
  50. /*
  51. * This function is derived from PowerPC code (timebase clock frequency).
  52. * On MIPS it returns the number of timer ticks per second.
  53. */
  54. ulong get_tbclk(void)
  55. {
  56. return CONFIG_SYS_HZ;
  57. }