time.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef CONFIG_WD_PERIOD
  25. # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default*/
  26. #endif
  27. /* ------------------------------------------------------------------------- */
  28. /*
  29. * This function is intended for SHORT delays only.
  30. * It will overflow at around 10 seconds @ 400MHz,
  31. * or 20 seconds @ 200MHz.
  32. */
  33. unsigned long usec2ticks(unsigned long usec)
  34. {
  35. ulong ticks;
  36. if (usec < 1000) {
  37. ticks = ((usec * (get_tbclk()/1000)) + 500) / 1000;
  38. } else {
  39. ticks = ((usec / 10) * (get_tbclk() / 100000));
  40. }
  41. return (ticks);
  42. }
  43. /* ------------------------------------------------------------------------- */
  44. /*
  45. * We implement the delay by converting the delay (the number of
  46. * microseconds to wait) into a number of time base ticks; then we
  47. * watch the time base until it has incremented by that amount.
  48. */
  49. void udelay(unsigned long usec)
  50. {
  51. ulong ticks, kv;
  52. do {
  53. kv = usec > CONFIG_WD_PERIOD ? CONFIG_WD_PERIOD : usec;
  54. ticks = usec2ticks (kv);
  55. wait_ticks (ticks);
  56. usec -= kv;
  57. } while(usec);
  58. }
  59. /* ------------------------------------------------------------------------- */
  60. unsigned long ticks2usec(unsigned long ticks)
  61. {
  62. ulong tbclk = get_tbclk();
  63. /* usec = ticks * 1000000 / tbclk
  64. * Multiplication would overflow at ~4.2e3 ticks,
  65. * so we break it up into
  66. * usec = ( ( ticks * 1000) / tbclk ) * 1000;
  67. */
  68. ticks *= 1000L;
  69. ticks /= tbclk;
  70. ticks *= 1000L;
  71. return ((ulong)ticks);
  72. }
  73. /* ------------------------------------------------------------------------- */
  74. int init_timebase (void)
  75. {
  76. #if defined(CONFIG_5xx) || defined(CONFIG_8xx)
  77. volatile immap_t *immap = (immap_t *) CFG_IMMR;
  78. /* unlock */
  79. immap->im_sitk.sitk_tbk = KAPWR_KEY;
  80. #endif
  81. /* reset */
  82. asm ("li 3,0 ; mttbu 3 ; mttbl 3 ;");
  83. #if defined(CONFIG_5xx) || defined(CONFIG_8xx)
  84. /* enable */
  85. immap->im_sit.sit_tbscr |= TBSCR_TBE;
  86. #endif
  87. return (0);
  88. }
  89. /* ------------------------------------------------------------------------- */