timer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #define TIMER_LOAD_VAL 0xffffffff
  34. DECLARE_GLOBAL_DATA_PTR;
  35. #define timestamp (gd->arch.tbl)
  36. #define lastdec (gd->lastinc)
  37. /*
  38. * This driver uses 1kHz clock source.
  39. */
  40. #define MX28_INCREMENTER_HZ 1000
  41. static inline unsigned long tick_to_time(unsigned long tick)
  42. {
  43. return tick / (MX28_INCREMENTER_HZ / CONFIG_SYS_HZ);
  44. }
  45. static inline unsigned long time_to_tick(unsigned long time)
  46. {
  47. return time * (MX28_INCREMENTER_HZ / CONFIG_SYS_HZ);
  48. }
  49. /* Calculate how many ticks happen in "us" microseconds */
  50. static inline unsigned long us_to_tick(unsigned long us)
  51. {
  52. return (us * MX28_INCREMENTER_HZ) / 1000000;
  53. }
  54. int timer_init(void)
  55. {
  56. struct mxs_timrot_regs *timrot_regs =
  57. (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
  58. /* Reset Timers and Rotary Encoder module */
  59. mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
  60. /* Set fixed_count to 0 */
  61. writel(0, &timrot_regs->hw_timrot_fixed_count0);
  62. /* Set UPDATE bit and 1Khz frequency */
  63. writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD |
  64. TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL,
  65. &timrot_regs->hw_timrot_timctrl0);
  66. /* Set fixed_count to maximal value */
  67. writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
  68. return 0;
  69. }
  70. unsigned long long get_ticks(void)
  71. {
  72. struct mxs_timrot_regs *timrot_regs =
  73. (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
  74. /* Current tick value */
  75. uint32_t now = readl(&timrot_regs->hw_timrot_running_count0);
  76. if (lastdec >= now) {
  77. /*
  78. * normal mode (non roll)
  79. * move stamp forward with absolut diff ticks
  80. */
  81. timestamp += (lastdec - now);
  82. } else {
  83. /* we have rollover of decrementer */
  84. timestamp += (TIMER_LOAD_VAL - now) + lastdec;
  85. }
  86. lastdec = now;
  87. return timestamp;
  88. }
  89. ulong get_timer_masked(void)
  90. {
  91. return tick_to_time(get_ticks());
  92. }
  93. ulong get_timer(ulong base)
  94. {
  95. return get_timer_masked() - base;
  96. }
  97. /* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
  98. #define MX28_HW_DIGCTL_MICROSECONDS 0x8001c0c0
  99. void __udelay(unsigned long usec)
  100. {
  101. uint32_t old, new, incr;
  102. uint32_t counter = 0;
  103. old = readl(MX28_HW_DIGCTL_MICROSECONDS);
  104. while (counter < usec) {
  105. new = readl(MX28_HW_DIGCTL_MICROSECONDS);
  106. /* Check if the timer wrapped. */
  107. if (new < old) {
  108. incr = 0xffffffff - old;
  109. incr += new;
  110. } else {
  111. incr = new - old;
  112. }
  113. /*
  114. * Check if we are close to the maximum time and the counter
  115. * would wrap if incremented. If that's the case, break out
  116. * from the loop as the requested delay time passed.
  117. */
  118. if (counter + incr < counter)
  119. break;
  120. counter += incr;
  121. old = new;
  122. }
  123. }
  124. ulong get_tbclk(void)
  125. {
  126. return MX28_INCREMENTER_HZ;
  127. }