timer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2007
  4. * Sascha Hauer, Pengutronix
  5. *
  6. * (C) Copyright 2009 Freescale Semiconductor, Inc.
  7. */
  8. #include <common.h>
  9. #include <asm/io.h>
  10. #include <div64.h>
  11. #include <asm/arch/imx-regs.h>
  12. #include <asm/arch/clock.h>
  13. #include <asm/arch/sys_proto.h>
  14. /* General purpose timers registers */
  15. struct mxc_gpt {
  16. unsigned int control;
  17. unsigned int prescaler;
  18. unsigned int status;
  19. unsigned int nouse[6];
  20. unsigned int counter;
  21. };
  22. static struct mxc_gpt *cur_gpt = (struct mxc_gpt *)GPT1_BASE_ADDR;
  23. /* General purpose timers bitfields */
  24. #define GPTCR_SWR (1 << 15) /* Software reset */
  25. #define GPTCR_24MEN (1 << 10) /* Enable 24MHz clock input */
  26. #define GPTCR_FRR (1 << 9) /* Freerun / restart */
  27. #define GPTCR_CLKSOURCE_32 (4 << 6) /* Clock source 32khz */
  28. #define GPTCR_CLKSOURCE_OSC (5 << 6) /* Clock source OSC */
  29. #define GPTCR_CLKSOURCE_PRE (1 << 6) /* Clock source PRECLK */
  30. #define GPTCR_CLKSOURCE_MASK (0x7 << 6)
  31. #define GPTCR_TEN 1 /* Timer enable */
  32. #define GPTPR_PRESCALER24M_SHIFT 12
  33. #define GPTPR_PRESCALER24M_MASK (0xF << GPTPR_PRESCALER24M_SHIFT)
  34. static inline int gpt_has_clk_source_osc(void)
  35. {
  36. #if defined(CONFIG_MX6)
  37. if (((is_mx6dq()) && (soc_rev() > CHIP_REV_1_0)) ||
  38. is_mx6dqp() || is_mx6sdl() || is_mx6sx() || is_mx6ul() ||
  39. is_mx6ull() || is_mx6sll())
  40. return 1;
  41. return 0;
  42. #else
  43. return 0;
  44. #endif
  45. }
  46. static inline ulong gpt_get_clk(void)
  47. {
  48. #ifdef CONFIG_MXC_GPT_HCLK
  49. if (gpt_has_clk_source_osc())
  50. return MXC_HCLK >> 3;
  51. else
  52. return mxc_get_clock(MXC_IPG_PERCLK);
  53. #else
  54. return MXC_CLK32;
  55. #endif
  56. }
  57. int timer_init(void)
  58. {
  59. int i;
  60. /* setup GP Timer 1 */
  61. __raw_writel(GPTCR_SWR, &cur_gpt->control);
  62. /* We have no udelay by now */
  63. __raw_writel(0, &cur_gpt->control);
  64. i = __raw_readl(&cur_gpt->control);
  65. i &= ~GPTCR_CLKSOURCE_MASK;
  66. #ifdef CONFIG_MXC_GPT_HCLK
  67. if (gpt_has_clk_source_osc()) {
  68. i |= GPTCR_CLKSOURCE_OSC | GPTCR_TEN;
  69. /*
  70. * For DL/S, SX, UL, ULL, SLL set 24Mhz OSC
  71. * Enable bit and prescaler
  72. */
  73. if (is_mx6sdl() || is_mx6sx() || is_mx6ul() || is_mx6ull() ||
  74. is_mx6sll()) {
  75. i |= GPTCR_24MEN;
  76. /* Produce 3Mhz clock */
  77. __raw_writel((7 << GPTPR_PRESCALER24M_SHIFT),
  78. &cur_gpt->prescaler);
  79. }
  80. } else {
  81. i |= GPTCR_CLKSOURCE_PRE | GPTCR_TEN;
  82. }
  83. #else
  84. __raw_writel(0, &cur_gpt->prescaler); /* 32Khz */
  85. i |= GPTCR_CLKSOURCE_32 | GPTCR_TEN;
  86. #endif
  87. __raw_writel(i, &cur_gpt->control);
  88. return 0;
  89. }
  90. unsigned long timer_read_counter(void)
  91. {
  92. return __raw_readl(&cur_gpt->counter); /* current tick value */
  93. }
  94. /*
  95. * This function is derived from PowerPC code (timebase clock frequency).
  96. * On ARM it returns the number of timer ticks per second.
  97. */
  98. ulong get_tbclk(void)
  99. {
  100. return gpt_get_clk();
  101. }
  102. /*
  103. * This function is intended for SHORT delays only.
  104. * It will overflow at around 10 seconds @ 400MHz,
  105. * or 20 seconds @ 200MHz.
  106. */
  107. unsigned long usec2ticks(unsigned long _usec)
  108. {
  109. unsigned long long usec = _usec;
  110. usec *= get_tbclk();
  111. usec += 999999;
  112. do_div(usec, 1000000);
  113. return usec;
  114. }