timer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * (C) Copyright 2007
  3. * Sascha Hauer, Pengutronix
  4. *
  5. * (C) Copyright 2008-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/crm_regs.h>
  30. #include <asm/arch/clock.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. #define timestamp (gd->tbl)
  33. #define lastinc (gd->lastinc)
  34. /* General purpose timers bitfields */
  35. #define GPTCR_SWR (1<<15) /* Software reset */
  36. #define GPTCR_FRR (1<<9) /* Freerun / restart */
  37. #define GPTCR_CLKSOURCE_32 (4<<6) /* Clock source */
  38. #define GPTCR_TEN (1) /* Timer enable */
  39. /*
  40. * "time" is measured in 1 / CONFIG_SYS_HZ seconds,
  41. * "tick" is internal timer period
  42. */
  43. /* ~0.4% error - measured with stop-watch on 100s boot-delay */
  44. static inline unsigned long long tick_to_time(unsigned long long tick)
  45. {
  46. tick *= CONFIG_SYS_HZ;
  47. do_div(tick, MXC_CLK32);
  48. return tick;
  49. }
  50. static inline unsigned long long us_to_tick(unsigned long long us)
  51. {
  52. us = us * MXC_CLK32 + 999999;
  53. do_div(us, 1000000);
  54. return us;
  55. }
  56. /*
  57. * nothing really to do with interrupts, just starts up a counter.
  58. * The 32KHz 32-bit timer overruns in 134217 seconds
  59. */
  60. int timer_init(void)
  61. {
  62. int i;
  63. struct gpt_regs *gpt = (struct gpt_regs *)GPT1_BASE_ADDR;
  64. struct ccm_regs *ccm = (struct ccm_regs *)CCM_BASE_ADDR;
  65. /* setup GP Timer 1 */
  66. writel(GPTCR_SWR, &gpt->ctrl);
  67. writel(readl(&ccm->cgr1) | 3 << MXC_CCM_CGR1_GPT_OFFSET, &ccm->cgr1);
  68. for (i = 0; i < 100; i++)
  69. writel(0, &gpt->ctrl); /* We have no udelay by now */
  70. writel(0, &gpt->pre); /* prescaler = 1 */
  71. /* Freerun Mode, 32KHz input */
  72. writel(readl(&gpt->ctrl) | GPTCR_CLKSOURCE_32 | GPTCR_FRR,
  73. &gpt->ctrl);
  74. writel(readl(&gpt->ctrl) | GPTCR_TEN, &gpt->ctrl);
  75. return 0;
  76. }
  77. unsigned long long get_ticks(void)
  78. {
  79. struct gpt_regs *gpt = (struct gpt_regs *)GPT1_BASE_ADDR;
  80. ulong now = readl(&gpt->counter); /* current tick value */
  81. if (now >= lastinc) {
  82. /*
  83. * normal mode (non roll)
  84. * move stamp forward with absolut diff ticks
  85. */
  86. timestamp += (now - lastinc);
  87. } else {
  88. /* we have rollover of incrementer */
  89. timestamp += (0xFFFFFFFF - lastinc) + now;
  90. }
  91. lastinc = now;
  92. return timestamp;
  93. }
  94. ulong get_timer_masked(void)
  95. {
  96. /*
  97. * get_ticks() returns a long long (64 bit), it wraps in
  98. * 2^64 / MXC_CLK32 = 2^64 / 2^15 = 2^49 ~ 5 * 10^14 (s) ~
  99. * 5 * 10^9 days... and get_ticks() * CONFIG_SYS_HZ wraps in
  100. * 5 * 10^6 days - long enough.
  101. */
  102. return tick_to_time(get_ticks());
  103. }
  104. ulong get_timer(ulong base)
  105. {
  106. return get_timer_masked() - base;
  107. }
  108. /* delay x useconds AND preserve advance timstamp value */
  109. void __udelay(unsigned long usec)
  110. {
  111. unsigned long long tmp;
  112. ulong tmo;
  113. tmo = us_to_tick(usec);
  114. tmp = get_ticks() + tmo; /* get current timestamp */
  115. while (get_ticks() < tmp) /* loop till event */
  116. /*NOP*/;
  117. }
  118. /*
  119. * This function is derived from PowerPC code (timebase clock frequency).
  120. * On ARM it returns the number of timer ticks per second.
  121. */
  122. ulong get_tbclk(void)
  123. {
  124. return MXC_CLK32;
  125. }