timer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * (C) Copyright 2010
  3. * Michael Schwingen, michael@schwingen.org
  4. *
  5. * (C) Copyright 2006
  6. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  7. *
  8. * (C) Copyright 2002
  9. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  10. * Marius Groeger <mgroeger@sysgo.de>
  11. *
  12. * (C) Copyright 2002
  13. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  14. * Alex Zuepke <azu@sysgo.de>
  15. *
  16. * See file CREDITS for list of people who contributed to this
  17. * project.
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License as
  21. * published by the Free Software Foundation; either version 2 of
  22. * the License, or (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  32. * MA 02111-1307 USA
  33. */
  34. #include <common.h>
  35. #include <asm/arch/ixp425.h>
  36. #include <asm/io.h>
  37. #include <div64.h>
  38. DECLARE_GLOBAL_DATA_PTR;
  39. /*
  40. * The IXP42x time-stamp timer runs at 2*OSC_IN (66.666MHz when using a
  41. * 33.333MHz crystal).
  42. */
  43. static inline unsigned long long tick_to_time(unsigned long long tick)
  44. {
  45. tick *= CONFIG_SYS_HZ;
  46. do_div(tick, CONFIG_IXP425_TIMER_CLK);
  47. return tick;
  48. }
  49. static inline unsigned long long time_to_tick(unsigned long long time)
  50. {
  51. time *= CONFIG_IXP425_TIMER_CLK;
  52. do_div(time, CONFIG_SYS_HZ);
  53. return time;
  54. }
  55. static inline unsigned long long us_to_tick(unsigned long long us)
  56. {
  57. us = us * CONFIG_IXP425_TIMER_CLK + 999999;
  58. do_div(us, 1000000);
  59. return us;
  60. }
  61. unsigned long long get_ticks(void)
  62. {
  63. ulong now = readl(IXP425_OSTS_B);
  64. if (readl(IXP425_OSST) & IXP425_OSST_TIMER_TS_PEND) {
  65. /* rollover of timestamp timer register */
  66. gd->arch.timestamp += (0xFFFFFFFF - gd->arch.lastinc) + now + 1;
  67. writel(IXP425_OSST_TIMER_TS_PEND, IXP425_OSST);
  68. } else {
  69. /* move stamp forward with absolut diff ticks */
  70. gd->arch.timestamp += (now - gd->arch.lastinc);
  71. }
  72. gd->arch.lastinc = now;
  73. return gd->arch.timestamp;
  74. }
  75. void reset_timer_masked(void)
  76. {
  77. /* capture current timestamp counter */
  78. gd->arch.lastinc = readl(IXP425_OSTS_B);
  79. /* start "advancing" time stamp from 0 */
  80. gd->arch.timestamp = 0;
  81. }
  82. ulong get_timer_masked(void)
  83. {
  84. return tick_to_time(get_ticks());
  85. }
  86. ulong get_timer(ulong base)
  87. {
  88. return get_timer_masked() - base;
  89. }
  90. /* delay x useconds AND preserve advance timestamp value */
  91. void __udelay(unsigned long usec)
  92. {
  93. unsigned long long tmp;
  94. tmp = get_ticks() + us_to_tick(usec);
  95. while (get_ticks() < tmp)
  96. ;
  97. }
  98. int timer_init(void)
  99. {
  100. writel(IXP425_OSST_TIMER_TS_PEND, IXP425_OSST);
  101. return 0;
  102. }