timer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2013 Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  17. * MA 02111-1307 USA
  18. */
  19. #include <common.h>
  20. #include <asm/io.h>
  21. #include <div64.h>
  22. #include <asm/arch/imx-regs.h>
  23. #include <asm/arch/clock.h>
  24. static struct pit_reg *cur_pit = (struct pit_reg *)PIT_BASE_ADDR;
  25. DECLARE_GLOBAL_DATA_PTR;
  26. #define TIMER_LOAD_VAL 0xffffffff
  27. static inline unsigned long long tick_to_time(unsigned long long tick)
  28. {
  29. tick *= CONFIG_SYS_HZ;
  30. do_div(tick, mxc_get_clock(MXC_IPG_CLK));
  31. return tick;
  32. }
  33. static inline unsigned long long us_to_tick(unsigned long long usec)
  34. {
  35. usec = usec * mxc_get_clock(MXC_IPG_CLK) + 999999;
  36. do_div(usec, 1000000);
  37. return usec;
  38. }
  39. int timer_init(void)
  40. {
  41. __raw_writel(0, &cur_pit->mcr);
  42. __raw_writel(TIMER_LOAD_VAL, &cur_pit->ldval1);
  43. __raw_writel(0, &cur_pit->tctrl1);
  44. __raw_writel(1, &cur_pit->tctrl1);
  45. gd->arch.tbl = 0;
  46. gd->arch.tbu = 0;
  47. return 0;
  48. }
  49. unsigned long long get_ticks(void)
  50. {
  51. ulong now = TIMER_LOAD_VAL - __raw_readl(&cur_pit->cval1);
  52. /* increment tbu if tbl has rolled over */
  53. if (now < gd->arch.tbl)
  54. gd->arch.tbu++;
  55. gd->arch.tbl = now;
  56. return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
  57. }
  58. ulong get_timer_masked(void)
  59. {
  60. return tick_to_time(get_ticks());
  61. }
  62. ulong get_timer(ulong base)
  63. {
  64. return get_timer_masked() - base;
  65. }
  66. /* delay x useconds AND preserve advance timstamp value */
  67. void __udelay(unsigned long usec)
  68. {
  69. unsigned long long start;
  70. ulong tmo;
  71. start = get_ticks(); /* get current timestamp */
  72. tmo = us_to_tick(usec); /* convert usecs to ticks */
  73. while ((get_ticks() - start) < tmo)
  74. ; /* loop till time has passed */
  75. }
  76. /*
  77. * This function is derived from PowerPC code (timebase clock frequency).
  78. * On ARM it returns the number of timer ticks per second.
  79. */
  80. ulong get_tbclk(void)
  81. {
  82. return mxc_get_clock(MXC_IPG_CLK);
  83. }