timer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * (C) Copyright 2007
  3. * Sascha Hauer, Pengutronix
  4. *
  5. * (C) Copyright 2009 Freescale Semiconductor, Inc.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <asm/io.h>
  27. #include <asm/arch/imx-regs.h>
  28. /* General purpose timers registers */
  29. struct mxc_gpt {
  30. unsigned int control;
  31. unsigned int prescaler;
  32. unsigned int status;
  33. unsigned int nouse[6];
  34. unsigned int counter;
  35. };
  36. static struct mxc_gpt *cur_gpt = (struct mxc_gpt *)GPT1_BASE_ADDR;
  37. /* General purpose timers bitfields */
  38. #define GPTCR_SWR (1<<15) /* Software reset */
  39. #define GPTCR_FRR (1<<9) /* Freerun / restart */
  40. #define GPTCR_CLKSOURCE_32 (4<<6) /* Clock source */
  41. #define GPTCR_TEN (1) /* Timer enable */
  42. DECLARE_GLOBAL_DATA_PTR;
  43. #define timestamp (gd->tbl)
  44. #define lastinc (gd->lastinc)
  45. int timer_init(void)
  46. {
  47. int i;
  48. /* setup GP Timer 1 */
  49. __raw_writel(GPTCR_SWR, &cur_gpt->control);
  50. /* We have no udelay by now */
  51. for (i = 0; i < 100; i++)
  52. __raw_writel(0, &cur_gpt->control);
  53. __raw_writel(0, &cur_gpt->prescaler); /* 32Khz */
  54. /* Freerun Mode, PERCLK1 input */
  55. i = __raw_readl(&cur_gpt->control);
  56. __raw_writel(i | GPTCR_CLKSOURCE_32 | GPTCR_TEN, &cur_gpt->control);
  57. reset_timer_masked();
  58. return 0;
  59. }
  60. void reset_timer(void)
  61. {
  62. reset_timer_masked();
  63. }
  64. void reset_timer_masked(void)
  65. {
  66. ulong val = __raw_readl(&cur_gpt->counter);
  67. lastinc = val / (CONFIG_SYS_MX5_CLK32 / CONFIG_SYS_HZ);
  68. timestamp = 0;
  69. }
  70. ulong get_timer_masked(void)
  71. {
  72. ulong val = __raw_readl(&cur_gpt->counter);
  73. val /= (CONFIG_SYS_MX5_CLK32 / CONFIG_SYS_HZ);
  74. if (val >= lastinc)
  75. timestamp += (val - lastinc);
  76. else
  77. timestamp += ((0xFFFFFFFF / (CONFIG_SYS_MX5_CLK32 / CONFIG_SYS_HZ))
  78. - lastinc) + val;
  79. lastinc = val;
  80. return timestamp;
  81. }
  82. ulong get_timer(ulong base)
  83. {
  84. return get_timer_masked() - base;
  85. }
  86. /* delay x useconds AND preserve advance timestamp value */
  87. void __udelay(unsigned long usec)
  88. {
  89. unsigned long now, start, tmo;
  90. tmo = usec * (CONFIG_SYS_MX5_CLK32 / 1000) / 1000;
  91. if (!tmo)
  92. tmo = 1;
  93. now = start = readl(&cur_gpt->counter);
  94. while ((now - start) < tmo)
  95. now = readl(&cur_gpt->counter);
  96. }