timer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <asm/io.h>
  14. #include <asm/arch/imx-regs.h>
  15. #include <asm/arch/sys_proto.h>
  16. /* Maximum fixed count */
  17. #if defined(CONFIG_MX23)
  18. #define TIMER_LOAD_VAL 0xffff
  19. #elif defined(CONFIG_MX28)
  20. #define TIMER_LOAD_VAL 0xffffffff
  21. #endif
  22. DECLARE_GLOBAL_DATA_PTR;
  23. #define timestamp (gd->arch.tbl)
  24. #define lastdec (gd->arch.lastinc)
  25. /*
  26. * This driver uses 1kHz clock source.
  27. */
  28. #define MXS_INCREMENTER_HZ 1000
  29. static inline unsigned long tick_to_time(unsigned long tick)
  30. {
  31. return tick / (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
  32. }
  33. static inline unsigned long time_to_tick(unsigned long time)
  34. {
  35. return time * (MXS_INCREMENTER_HZ / CONFIG_SYS_HZ);
  36. }
  37. /* Calculate how many ticks happen in "us" microseconds */
  38. static inline unsigned long us_to_tick(unsigned long us)
  39. {
  40. return (us * MXS_INCREMENTER_HZ) / 1000000;
  41. }
  42. int timer_init(void)
  43. {
  44. struct mxs_timrot_regs *timrot_regs =
  45. (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
  46. /* Reset Timers and Rotary Encoder module */
  47. mxs_reset_block(&timrot_regs->hw_timrot_rotctrl_reg);
  48. /* Set fixed_count to 0 */
  49. #if defined(CONFIG_MX23)
  50. writel(0, &timrot_regs->hw_timrot_timcount0);
  51. #elif defined(CONFIG_MX28)
  52. writel(0, &timrot_regs->hw_timrot_fixed_count0);
  53. #endif
  54. /* Set UPDATE bit and 1Khz frequency */
  55. writel(TIMROT_TIMCTRLn_UPDATE | TIMROT_TIMCTRLn_RELOAD |
  56. TIMROT_TIMCTRLn_SELECT_1KHZ_XTAL,
  57. &timrot_regs->hw_timrot_timctrl0);
  58. /* Set fixed_count to maximal value */
  59. #if defined(CONFIG_MX23)
  60. writel(TIMER_LOAD_VAL - 1, &timrot_regs->hw_timrot_timcount0);
  61. #elif defined(CONFIG_MX28)
  62. writel(TIMER_LOAD_VAL, &timrot_regs->hw_timrot_fixed_count0);
  63. #endif
  64. return 0;
  65. }
  66. unsigned long long get_ticks(void)
  67. {
  68. struct mxs_timrot_regs *timrot_regs =
  69. (struct mxs_timrot_regs *)MXS_TIMROT_BASE;
  70. uint32_t now;
  71. /* Current tick value */
  72. #if defined(CONFIG_MX23)
  73. /* Upper bits are the valid ones. */
  74. now = readl(&timrot_regs->hw_timrot_timcount0) >>
  75. TIMROT_RUNNING_COUNTn_RUNNING_COUNT_OFFSET;
  76. #elif defined(CONFIG_MX28)
  77. now = readl(&timrot_regs->hw_timrot_running_count0);
  78. #else
  79. #error "Don't know how to read timrot_regs"
  80. #endif
  81. if (lastdec >= now) {
  82. /*
  83. * normal mode (non roll)
  84. * move stamp forward with absolut diff ticks
  85. */
  86. timestamp += (lastdec - now);
  87. } else {
  88. /* we have rollover of decrementer */
  89. timestamp += (TIMER_LOAD_VAL - now) + lastdec;
  90. }
  91. lastdec = now;
  92. return timestamp;
  93. }
  94. ulong get_timer_masked(void)
  95. {
  96. return tick_to_time(get_ticks());
  97. }
  98. ulong get_timer(ulong base)
  99. {
  100. return get_timer_masked() - base;
  101. }
  102. /* We use the HW_DIGCTL_MICROSECONDS register for sub-millisecond timer. */
  103. #define MXS_HW_DIGCTL_MICROSECONDS 0x8001c0c0
  104. void __udelay(unsigned long usec)
  105. {
  106. uint32_t old, new, incr;
  107. uint32_t counter = 0;
  108. old = readl(MXS_HW_DIGCTL_MICROSECONDS);
  109. while (counter < usec) {
  110. new = readl(MXS_HW_DIGCTL_MICROSECONDS);
  111. /* Check if the timer wrapped. */
  112. if (new < old) {
  113. incr = 0xffffffff - old;
  114. incr += new;
  115. } else {
  116. incr = new - old;
  117. }
  118. /*
  119. * Check if we are close to the maximum time and the counter
  120. * would wrap if incremented. If that's the case, break out
  121. * from the loop as the requested delay time passed.
  122. */
  123. if (counter + incr < counter)
  124. break;
  125. counter += incr;
  126. old = new;
  127. }
  128. }
  129. ulong get_tbclk(void)
  130. {
  131. return MXS_INCREMENTER_HZ;
  132. }