time.c 1.7 KB

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