timer.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2013 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <asm/io.h>
  7. #include <div64.h>
  8. #include <asm/arch/imx-regs.h>
  9. #include <asm/arch/clock.h>
  10. static struct pit_reg *cur_pit = (struct pit_reg *)PIT_BASE_ADDR;
  11. DECLARE_GLOBAL_DATA_PTR;
  12. #define TIMER_LOAD_VAL 0xffffffff
  13. static inline unsigned long long tick_to_time(unsigned long long tick)
  14. {
  15. tick *= CONFIG_SYS_HZ;
  16. do_div(tick, mxc_get_clock(MXC_IPG_CLK));
  17. return tick;
  18. }
  19. static inline unsigned long long us_to_tick(unsigned long long usec)
  20. {
  21. usec = usec * mxc_get_clock(MXC_IPG_CLK) + 999999;
  22. do_div(usec, 1000000);
  23. return usec;
  24. }
  25. int timer_init(void)
  26. {
  27. __raw_writel(0, &cur_pit->mcr);
  28. __raw_writel(TIMER_LOAD_VAL, &cur_pit->ldval1);
  29. __raw_writel(0, &cur_pit->tctrl1);
  30. __raw_writel(1, &cur_pit->tctrl1);
  31. gd->arch.tbl = 0;
  32. gd->arch.tbu = 0;
  33. return 0;
  34. }
  35. unsigned long long get_ticks(void)
  36. {
  37. ulong now = TIMER_LOAD_VAL - __raw_readl(&cur_pit->cval1);
  38. /* increment tbu if tbl has rolled over */
  39. if (now < gd->arch.tbl)
  40. gd->arch.tbu++;
  41. gd->arch.tbl = now;
  42. return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
  43. }
  44. ulong get_timer_masked(void)
  45. {
  46. return tick_to_time(get_ticks());
  47. }
  48. ulong get_timer(ulong base)
  49. {
  50. return get_timer_masked() - base;
  51. }
  52. /* delay x useconds AND preserve advance timstamp value */
  53. void __udelay(unsigned long usec)
  54. {
  55. unsigned long long start;
  56. ulong tmo;
  57. start = get_ticks(); /* get current timestamp */
  58. tmo = us_to_tick(usec); /* convert usecs to ticks */
  59. while ((get_ticks() - start) < tmo)
  60. ; /* loop till time has passed */
  61. }
  62. /*
  63. * This function is derived from PowerPC code (timebase clock frequency).
  64. * On ARM it returns the number of timer ticks per second.
  65. */
  66. ulong get_tbclk(void)
  67. {
  68. return mxc_get_clock(MXC_IPG_CLK);
  69. }