timer.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * SPDX-License-Identifier: GPL-2.0+
  17. */
  18. #include <common.h>
  19. #include <asm/arch/ixp425.h>
  20. #include <asm/io.h>
  21. #include <div64.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. /*
  24. * The IXP42x time-stamp timer runs at 2*OSC_IN (66.666MHz when using a
  25. * 33.333MHz crystal).
  26. */
  27. static inline unsigned long long tick_to_time(unsigned long long tick)
  28. {
  29. tick *= CONFIG_SYS_HZ;
  30. do_div(tick, CONFIG_IXP425_TIMER_CLK);
  31. return tick;
  32. }
  33. static inline unsigned long long time_to_tick(unsigned long long time)
  34. {
  35. time *= CONFIG_IXP425_TIMER_CLK;
  36. do_div(time, CONFIG_SYS_HZ);
  37. return time;
  38. }
  39. static inline unsigned long long us_to_tick(unsigned long long us)
  40. {
  41. us = us * CONFIG_IXP425_TIMER_CLK + 999999;
  42. do_div(us, 1000000);
  43. return us;
  44. }
  45. unsigned long long get_ticks(void)
  46. {
  47. ulong now = readl(IXP425_OSTS_B);
  48. if (readl(IXP425_OSST) & IXP425_OSST_TIMER_TS_PEND) {
  49. /* rollover of timestamp timer register */
  50. gd->arch.timestamp += (0xFFFFFFFF - gd->arch.lastinc) + now + 1;
  51. writel(IXP425_OSST_TIMER_TS_PEND, IXP425_OSST);
  52. } else {
  53. /* move stamp forward with absolut diff ticks */
  54. gd->arch.timestamp += (now - gd->arch.lastinc);
  55. }
  56. gd->arch.lastinc = now;
  57. return gd->arch.timestamp;
  58. }
  59. void reset_timer_masked(void)
  60. {
  61. /* capture current timestamp counter */
  62. gd->arch.lastinc = readl(IXP425_OSTS_B);
  63. /* start "advancing" time stamp from 0 */
  64. gd->arch.timestamp = 0;
  65. }
  66. ulong get_timer_masked(void)
  67. {
  68. return tick_to_time(get_ticks());
  69. }
  70. ulong get_timer(ulong base)
  71. {
  72. return get_timer_masked() - base;
  73. }
  74. /* delay x useconds AND preserve advance timestamp value */
  75. void __udelay(unsigned long usec)
  76. {
  77. unsigned long long tmp;
  78. tmp = get_ticks() + us_to_tick(usec);
  79. while (get_ticks() < tmp)
  80. ;
  81. }
  82. int timer_init(void)
  83. {
  84. writel(IXP425_OSST_TIMER_TS_PEND, IXP425_OSST);
  85. return 0;
  86. }