timer.c 3.1 KB

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