timer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * (C) Copyright 2007
  3. * Sascha Hauer, Pengutronix
  4. *
  5. * (C) Copyright 2008-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 bitfields */
  29. #define GPTCR_SWR (1<<15) /* Software reset */
  30. #define GPTCR_FRR (1<<9) /* Freerun / restart */
  31. #define GPTCR_CLKSOURCE_32 (0x100<<6) /* Clock source */
  32. #define GPTCR_CLKSOURCE_IPG (0x001<<6) /* Clock source */
  33. #define GPTCR_TEN (1) /* Timer enable */
  34. #define GPTPR_VAL (66)
  35. int timer_init(void)
  36. {
  37. int i;
  38. struct gpt_regs *gpt = (struct gpt_regs *)GPT1_BASE_ADDR;
  39. /* setup GP Timer 1 */
  40. writel(GPTCR_SWR, &gpt->ctrl);
  41. for (i = 0; i < 100; i++)
  42. writel(0, &gpt->ctrl); /* We have no udelay by now */
  43. writel(GPTPR_VAL, &gpt->pre);
  44. /* Freerun Mode, PERCLK1 input */
  45. writel(readl(&gpt->ctrl) |
  46. GPTCR_CLKSOURCE_IPG | GPTCR_TEN,
  47. &gpt->ctrl);
  48. return 0;
  49. }
  50. void reset_timer_masked(void)
  51. {
  52. struct gpt_regs *gpt = (struct gpt_regs *)GPT1_BASE_ADDR;
  53. writel(0, &gpt->ctrl);
  54. /* Freerun Mode, PERCLK1 input */
  55. writel(GPTCR_CLKSOURCE_IPG | GPTCR_TEN,
  56. &gpt->ctrl);
  57. }
  58. inline ulong get_timer_masked(void)
  59. {
  60. struct gpt_regs *gpt = (struct gpt_regs *)GPT1_BASE_ADDR;
  61. ulong val = readl(&gpt->counter);
  62. return val;
  63. }
  64. ulong get_timer(ulong base)
  65. {
  66. ulong tmp;
  67. tmp = get_timer_masked();
  68. if (tmp <= (base * 1000)) {
  69. /* Overflow */
  70. tmp += (0xffffffff - base);
  71. }
  72. return (tmp / 1000) - base;
  73. }
  74. /*
  75. * delay x useconds AND preserve advance timstamp value
  76. * GPTCNT is now supposed to tick 1 by 1 us.
  77. */
  78. void __udelay(unsigned long usec)
  79. {
  80. ulong tmp;
  81. tmp = get_timer_masked(); /* get current timestamp */
  82. /* if setting this forward will roll time stamp */
  83. if ((usec + tmp + 1) < tmp) {
  84. /* reset "advancing" timestamp to 0, set lastinc value */
  85. reset_timer_masked();
  86. } else {
  87. /* else, set advancing stamp wake up time */
  88. tmp += usec;
  89. }
  90. while (get_timer_masked() < tmp) /* loop till event */
  91. /*NOP*/;
  92. }