timer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * (C) Copyright 2007
  3. * Sascha Hauer, Pengutronix
  4. *
  5. * (C) Copyright 2009 Freescale Semiconductor, Inc.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <asm/io.h>
  27. #include <div64.h>
  28. #include <asm/arch/imx-regs.h>
  29. #include <asm/arch/clock.h>
  30. /* General purpose timers registers */
  31. struct mxc_gpt {
  32. unsigned int control;
  33. unsigned int prescaler;
  34. unsigned int status;
  35. unsigned int nouse[6];
  36. unsigned int counter;
  37. };
  38. static struct mxc_gpt *cur_gpt = (struct mxc_gpt *)GPT1_BASE_ADDR;
  39. /* General purpose timers bitfields */
  40. #define GPTCR_SWR (1 << 15) /* Software reset */
  41. #define GPTCR_FRR (1 << 9) /* Freerun / restart */
  42. #define GPTCR_CLKSOURCE_32 (4 << 6) /* Clock source */
  43. #define GPTCR_TEN 1 /* Timer enable */
  44. DECLARE_GLOBAL_DATA_PTR;
  45. #define timestamp (gd->arch.tbl)
  46. #define lastinc (gd->lastinc)
  47. static inline unsigned long long tick_to_time(unsigned long long tick)
  48. {
  49. tick *= CONFIG_SYS_HZ;
  50. do_div(tick, MXC_CLK32);
  51. return tick;
  52. }
  53. static inline unsigned long long us_to_tick(unsigned long long usec)
  54. {
  55. usec = usec * MXC_CLK32 + 999999;
  56. do_div(usec, 1000000);
  57. return usec;
  58. }
  59. int timer_init(void)
  60. {
  61. int i;
  62. ulong val;
  63. /* setup GP Timer 1 */
  64. __raw_writel(GPTCR_SWR, &cur_gpt->control);
  65. /* We have no udelay by now */
  66. for (i = 0; i < 100; i++)
  67. __raw_writel(0, &cur_gpt->control);
  68. __raw_writel(0, &cur_gpt->prescaler); /* 32Khz */
  69. /* Freerun Mode, PERCLK1 input */
  70. i = __raw_readl(&cur_gpt->control);
  71. __raw_writel(i | GPTCR_CLKSOURCE_32 | GPTCR_TEN, &cur_gpt->control);
  72. val = __raw_readl(&cur_gpt->counter);
  73. lastinc = val / (MXC_CLK32 / CONFIG_SYS_HZ);
  74. timestamp = 0;
  75. return 0;
  76. }
  77. unsigned long long get_ticks(void)
  78. {
  79. ulong now = __raw_readl(&cur_gpt->counter); /* current tick value */
  80. if (now >= lastinc) {
  81. /*
  82. * normal mode (non roll)
  83. * move stamp forward with absolut diff ticks
  84. */
  85. timestamp += (now - lastinc);
  86. } else {
  87. /* we have rollover of incrementer */
  88. timestamp += (0xFFFFFFFF - lastinc) + now;
  89. }
  90. lastinc = now;
  91. return timestamp;
  92. }
  93. ulong get_timer_masked(void)
  94. {
  95. /*
  96. * get_ticks() returns a long long (64 bit), it wraps in
  97. * 2^64 / MXC_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
  98. * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
  99. * 5 * 10^6 days - long enough.
  100. */
  101. return tick_to_time(get_ticks());
  102. }
  103. ulong get_timer(ulong base)
  104. {
  105. return get_timer_masked() - base;
  106. }
  107. /* delay x useconds AND preserve advance timstamp value */
  108. void __udelay(unsigned long usec)
  109. {
  110. unsigned long long tmp;
  111. ulong tmo;
  112. tmo = us_to_tick(usec);
  113. tmp = get_ticks() + tmo; /* get current timestamp */
  114. while (get_ticks() < tmp) /* loop till event */
  115. /*NOP*/;
  116. }
  117. /*
  118. * This function is derived from PowerPC code (timebase clock frequency).
  119. * On ARM it returns the number of timer ticks per second.
  120. */
  121. ulong get_tbclk(void)
  122. {
  123. return MXC_CLK32;
  124. }