time.c 1.7 KB

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