time.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * (C) Copyright 2000, 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. /* ------------------------------------------------------------------------- */
  25. /*
  26. * This function is intended for SHORT delays only.
  27. * It will overflow at around 10 seconds @ 400MHz,
  28. * or 20 seconds @ 200MHz.
  29. */
  30. unsigned long usec2ticks(unsigned long usec)
  31. {
  32. ulong ticks;
  33. if (usec < 1000) {
  34. ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
  35. } else {
  36. ticks = ((usec / 10) * (get_tbclk() / 100000));
  37. }
  38. return (ticks);
  39. }
  40. /* ------------------------------------------------------------------------- */
  41. /*
  42. * We implement the delay by converting the delay (the number of
  43. * microseconds to wait) into a number of time base ticks; then we
  44. * watch the time base until it has incremented by that amount.
  45. */
  46. void udelay(unsigned long usec)
  47. {
  48. ulong ticks = usec2ticks (usec);
  49. wait_ticks (ticks);
  50. }
  51. /* ------------------------------------------------------------------------- */
  52. unsigned long ticks2usec(unsigned long ticks)
  53. {
  54. ulong tbclk = get_tbclk();
  55. /* usec = ticks * 1000000 / tbclk
  56. * Multiplication would overflow at ~4.2e3 ticks,
  57. * so we break it up into
  58. * usec = ( ( ticks * 1000) / tbclk ) * 1000;
  59. */
  60. ticks *= 1000L;
  61. ticks /= tbclk;
  62. ticks *= 1000L;
  63. return ((ulong)ticks);
  64. }
  65. /* ------------------------------------------------------------------------- */
  66. int init_timebase (void)
  67. {
  68. #if defined(CONFIG_5xx) || defined(CONFIG_8xx)
  69. volatile immap_t *immap = (immap_t *) CFG_IMMR;
  70. /* unlock */
  71. immap->im_sitk.sitk_tbk = KAPWR_KEY;
  72. #endif
  73. /* reset */
  74. asm ("li 3,0 ; mttbu 3 ; mttbl 3 ;");
  75. #if defined(CONFIG_5xx) || defined(CONFIG_8xx)
  76. /* enable */
  77. immap->im_sit.sit_tbscr |= TBSCR_TBE;
  78. #endif
  79. return (0);
  80. }
  81. /* ------------------------------------------------------------------------- */