time.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * (C) Copyright 2000, 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. /* ------------------------------------------------------------------------- */
  9. /*
  10. * This function is intended for SHORT delays only.
  11. * It will overflow at around 10 seconds @ 400MHz,
  12. * or 20 seconds @ 200MHz.
  13. */
  14. unsigned long usec2ticks(unsigned long usec)
  15. {
  16. ulong ticks;
  17. if (usec < 1000) {
  18. ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
  19. } else {
  20. ticks = ((usec / 10) * (get_tbclk() / 100000));
  21. }
  22. return (ticks);
  23. }
  24. /* ------------------------------------------------------------------------- */
  25. /*
  26. * We implement the delay by converting the delay (the number of
  27. * microseconds to wait) into a number of time base ticks; then we
  28. * watch the time base until it has incremented by that amount.
  29. */
  30. void __udelay(unsigned long usec)
  31. {
  32. ulong ticks = usec2ticks (usec);
  33. wait_ticks (ticks);
  34. }
  35. /* ------------------------------------------------------------------------- */
  36. #ifndef CONFIG_NAND_SPL
  37. unsigned long ticks2usec(unsigned long ticks)
  38. {
  39. ulong tbclk = get_tbclk();
  40. /* usec = ticks * 1000000 / tbclk
  41. * Multiplication would overflow at ~4.2e3 ticks,
  42. * so we break it up into
  43. * usec = ( ( ticks * 1000) / tbclk ) * 1000;
  44. */
  45. ticks *= 1000L;
  46. ticks /= tbclk;
  47. ticks *= 1000L;
  48. return ((ulong)ticks);
  49. }
  50. #endif
  51. /* ------------------------------------------------------------------------- */
  52. int timer_init(void)
  53. {
  54. unsigned long temp;
  55. /* reset */
  56. asm volatile("li %0,0 ; mttbu %0 ; mttbl %0;"
  57. : "=&r"(temp) );
  58. return (0);
  59. }
  60. /* ------------------------------------------------------------------------- */