timer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Freescale i.MX28 timer driver
  3. *
  4. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  5. * on behalf of DENX Software Engineering GmbH
  6. *
  7. * Based on code from LTIB:
  8. * (C) Copyright 2009-2010 Freescale Semiconductor, Inc.
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <common.h>
  29. #include <asm/io.h>
  30. #include <asm/arch/imx-regs.h>
  31. #include <asm/arch/sys_proto.h>
  32. /* Maximum fixed count */
  33. #if defined(CONFIG_MX23)
  34. #define TIMER_LOAD_VAL 0xffff
  35. #elif defined(CONFIG_MX28)
  36. #define TIMER_LOAD_VAL 0xffffffff
  37. #endif
  38. DECLARE_GLOBAL_DATA_PTR;
  39. #define timestamp (gd->arch.tbl)
  40. #define lastdec (gd->arch.lastinc)
  41. /*
  42. * This driver uses 1kHz clock source.
  43. */
  44. #define MXS_INCREMENTER_HZ 1000
  45. static inline unsigned long tick_to_time(unsigned long tick)
  46. {
  47. return tick / (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
  48. }
  49. static inline unsigned long time_to_tick(unsigned long time)
  50. {
  51. return time * (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
  52. }
  53. /* Calculate how many ticks happen in "us" microseconds */
  54. static inline unsigned long us_to_tick(unsigned long us)
  55. {
  56. return (us * MXS_INCREMENTER_HZ) / 1000000;
  57. }
  58. int timer_init(void)
  59. {
  60. struct mxs_timrot_regs *timrot_regs =
  61. (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
  62. /* Reset Timers and Rotary Encoder module */
  63. mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
  64. /* Set fixed_count to 0 */
  65. #if defined(CONFIG_MX23)
  66. writel(0, &timrot_regs->hw_timrot_timcount0);
  67. #elif defined(CONFIG_MX28)
  68. writel(0, &timrot_regs->hw_timrot_fixed_count0);
  69. #endif
  70. /* Set UPDATE bit and 1Khz frequency */
  71. writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD |
  72. TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL,
  73. &timrot_regs->hw_timrot_timctrl0);
  74. /* Set fixed_count to maximal value */
  75. #if defined(CONFIG_MX23)
  76. writel(TIMER_LOAD_VAL - 1, &timrot_regs->hw_timrot_timcount0);
  77. #elif defined(CONFIG_MX28)
  78. writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
  79. #endif
  80. return 0;
  81. }
  82. unsigned long long get_ticks(void)
  83. {
  84. struct mxs_timrot_regs *timrot_regs =
  85. (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
  86. uint32_t now;
  87. /* Current tick value */
  88. #if defined(CONFIG_MX23)
  89. /* Upper bits are the valid ones. */
  90. now = readl(&timrot_regs->hw_timrot_timcount0) >>
  91. TIMROT_RUNNING_COUNTn_RUNNING_COUNT_OFFSET;
  92. #elif defined(CONFIG_MX28)
  93. now = readl(&timrot_regs->hw_timrot_running_count0);
  94. #endif
  95. if (lastdec >= now) {
  96. /*
  97. * normal mode (non roll)
  98. * move stamp forward with absolut diff ticks
  99. */
  100. timestamp += (lastdec - now);
  101. } else {
  102. /* we have rollover of decrementer */
  103. timestamp += (TIMER_LOAD_VAL - now) + lastdec;
  104. }
  105. lastdec = now;
  106. return timestamp;
  107. }
  108. ulong get_timer_masked(void)
  109. {
  110. return tick_to_time(get_ticks());
  111. }
  112. ulong get_timer(ulong base)
  113. {
  114. return get_timer_masked() - base;
  115. }
  116. /* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
  117. #define MXS_HW_DIGCTL_MICROSECONDS 0x8001c0c0
  118. void __udelay(unsigned long usec)
  119. {
  120. uint32_t old, new, incr;
  121. uint32_t counter = 0;
  122. old = readl(MXS_HW_DIGCTL_MICROSECONDS);
  123. while (counter < usec) {
  124. new = readl(MXS_HW_DIGCTL_MICROSECONDS);
  125. /* Check if the timer wrapped. */
  126. if (new < old) {
  127. incr = 0xffffffff - old;
  128. incr += new;
  129. } else {
  130. incr = new - old;
  131. }
  132. /*
  133. * Check if we are close to the maximum time and the counter
  134. * would wrap if incremented. If that's the case, break out
  135. * from the loop as the requested delay time passed.
  136. */
  137. if (counter + incr < counter)
  138. break;
  139. counter += incr;
  140. old = new;
  141. }
  142. }
  143. ulong get_tbclk(void)
  144. {
  145. return MXS_INCREMENTER_HZ;
  146. }