timer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * (C) Copyright 2007-2008
  3. * Stelian Pop <stelian@popies.net>
  4. * Lead Tech Design <www.leadtechdesign.com>
  5. *
  6. * (C) Copyright 2013
  7. * Bo Shen <voice.shen@atmel.com>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <asm/io.h>
  29. #include <asm/arch/hardware.h>
  30. #include <asm/arch/at91_pit.h>
  31. #include <asm/arch/at91_pmc.h>
  32. #include <asm/arch/clk.h>
  33. #include <div64.h>
  34. #if !defined(CONFIG_AT91FAMILY)
  35. # error You need to define CONFIG_AT91FAMILY in your board config!
  36. #endif
  37. DECLARE_GLOBAL_DATA_PTR;
  38. /*
  39. * We're using the SAMA5D3x PITC in 32 bit mode, by
  40. * setting the 20 bit counter period to its maximum (0xfffff).
  41. * (See the relevant data sheets to understand that this really works)
  42. *
  43. * We do also mimic the typical powerpc way of incrementing
  44. * two 32 bit registers called tbl and tbu.
  45. *
  46. * Those registers increment at 1/16 the main clock rate.
  47. */
  48. #define TIMER_LOAD_VAL 0xfffff
  49. static inline unsigned long long tick_to_time(unsigned long long tick)
  50. {
  51. tick *= CONFIG_SYS_HZ;
  52. do_div(tick, gd->arch.timer_rate_hz);
  53. return tick;
  54. }
  55. static inline unsigned long long usec_to_tick(unsigned long long usec)
  56. {
  57. usec *= gd->arch.timer_rate_hz;
  58. do_div(usec, 1000000);
  59. return usec;
  60. }
  61. /*
  62. * Use the PITC in full 32 bit incrementing mode
  63. */
  64. int timer_init(void)
  65. {
  66. at91_pit_t *pit = (at91_pit_t *)ATMEL_BASE_PIT;
  67. /* Enable PITC Clock */
  68. at91_periph_clk_enable(ATMEL_ID_SYS);
  69. /* Enable PITC */
  70. writel(TIMER_LOAD_VAL | AT91_PIT_MR_EN , &pit->mr);
  71. gd->arch.timer_rate_hz = gd->arch.mck_rate_hz / 16;
  72. gd->arch.tbu = 0;
  73. gd->arch.tbl = 0;
  74. return 0;
  75. }
  76. /*
  77. * Get the current 64 bit timer tick count
  78. */
  79. unsigned long long get_ticks(void)
  80. {
  81. at91_pit_t *pit = (at91_pit_t *)ATMEL_BASE_PIT;
  82. ulong now = readl(&pit->piir);
  83. /* increment tbu if tbl has rolled over */
  84. if (now < gd->arch.tbl)
  85. gd->arch.tbu++;
  86. gd->arch.tbl = now;
  87. return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
  88. }
  89. void __udelay(unsigned long usec)
  90. {
  91. unsigned long long start;
  92. ulong tmo;
  93. start = get_ticks(); /* get current timestamp */
  94. tmo = usec_to_tick(usec); /* convert usecs to ticks */
  95. while ((get_ticks() - start) < tmo)
  96. ; /* loop till time has passed */
  97. }
  98. /*
  99. * get_timer(base) can be used to check for timeouts or
  100. * to measure elasped time relative to an event:
  101. *
  102. * ulong start_time = get_timer(0) sets start_time to the current
  103. * time value.
  104. * get_timer(start_time) returns the time elapsed since then.
  105. *
  106. * The time is used in CONFIG_SYS_HZ units!
  107. */
  108. ulong get_timer(ulong base)
  109. {
  110. return tick_to_time(get_ticks()) - base;
  111. }
  112. /*
  113. * Return the number of timer ticks per second.
  114. */
  115. ulong get_tbclk(void)
  116. {
  117. return gd->arch.timer_rate_hz;
  118. }