timer.c 3.5 KB

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