timer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * (C) Copyright 2010,2011
  3. * NVIDIA Corporation <www.nvidia.com>
  4. *
  5. * (C) Copyright 2008
  6. * Texas Instruments
  7. *
  8. * Richard Woodruff <r-woodruff2@ti.com>
  9. * Syed Moahmmed Khasim <khasim@ti.com>
  10. *
  11. * (C) Copyright 2002
  12. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  13. * Marius Groeger <mgroeger@sysgo.de>
  14. * Alex Zuepke <azu@sysgo.de>
  15. *
  16. * (C) Copyright 2002
  17. * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
  18. *
  19. * See file CREDITS for list of people who contributed to this
  20. * project.
  21. *
  22. * This program is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU General Public License as
  24. * published by the Free Software Foundation; either version 2 of
  25. * the License, or (at your option) any later version.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU General Public License
  33. * along with this program; if not, write to the Free Software
  34. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  35. * MA 02111-1307 USA
  36. */
  37. #include <common.h>
  38. #include <asm/io.h>
  39. #include <asm/arch/tegra.h>
  40. #include <asm/arch-tegra/timer.h>
  41. DECLARE_GLOBAL_DATA_PTR;
  42. /* counter runs at 1MHz */
  43. #define TIMER_CLK 1000000
  44. #define TIMER_LOAD_VAL 0xffffffff
  45. /* timer without interrupts */
  46. ulong get_timer(ulong base)
  47. {
  48. return get_timer_masked() - base;
  49. }
  50. /* delay x useconds */
  51. void __udelay(unsigned long usec)
  52. {
  53. long tmo = usec * (TIMER_CLK / 1000) / 1000;
  54. unsigned long now, last = timer_get_us();
  55. while (tmo > 0) {
  56. now = timer_get_us();
  57. if (last > now) /* count up timer overflow */
  58. tmo -= TIMER_LOAD_VAL - last + now;
  59. else
  60. tmo -= now - last;
  61. last = now;
  62. }
  63. }
  64. ulong get_timer_masked(void)
  65. {
  66. ulong now;
  67. /* current tick value */
  68. now = timer_get_us() / (TIMER_CLK / CONFIG_SYS_HZ);
  69. if (now >= gd->lastinc) /* normal mode (non roll) */
  70. /* move stamp forward with absolute diff ticks */
  71. gd->arch.tbl += (now - gd->lastinc);
  72. else /* we have rollover of incrementer */
  73. gd->arch.tbl += ((TIMER_LOAD_VAL / (TIMER_CLK / CONFIG_SYS_HZ))
  74. - gd->lastinc) + now;
  75. gd->lastinc = now;
  76. return gd->arch.tbl;
  77. }
  78. /*
  79. * This function is derived from PowerPC code (read timebase as long long).
  80. * On ARM it just returns the timer value.
  81. */
  82. unsigned long long get_ticks(void)
  83. {
  84. return get_timer(0);
  85. }
  86. /*
  87. * This function is derived from PowerPC code (timebase clock frequency).
  88. * On ARM it returns the number of timer ticks per second.
  89. */
  90. ulong get_tbclk(void)
  91. {
  92. return CONFIG_SYS_HZ;
  93. }
  94. unsigned long timer_get_us(void)
  95. {
  96. struct timerus *timer_base = (struct timerus *)NV_PA_TMRUS_BASE;
  97. return readl(&timer_base->cntr_1us);
  98. }