time.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #if defined(CONFIG_5xx)
  56. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  57. /* unlock */
  58. immap->im_sitk.sitk_tbk = KAPWR_KEY;
  59. #endif
  60. /* reset */
  61. asm volatile("li %0,0 ; mttbu %0 ; mttbl %0;"
  62. : "=&r"(temp) );
  63. #if defined(CONFIG_5xx)
  64. /* enable */
  65. immap->im_sit.sit_tbscr |= TBSCR_TBE;
  66. #endif
  67. return (0);
  68. }
  69. /* ------------------------------------------------------------------------- */