timer.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * (C) Copyright 2012 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com>
  3. * (C) Copyright 2012 Renesas Solutions Corp.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/io.h>
  25. #include <asm/arch-armv7/globaltimer.h>
  26. #include <asm/arch/rmobile.h>
  27. static struct globaltimer *global_timer = \
  28. (struct globaltimer *)GLOBAL_TIMER_BASE_ADDR;
  29. #define CLK2MHZ(clk) (clk / 1000 / 1000)
  30. static u64 get_cpu_global_timer(void)
  31. {
  32. u32 low, high;
  33. u64 timer;
  34. u32 old = readl(&global_timer->cnt_h);
  35. while (1) {
  36. low = readl(&global_timer->cnt_l);
  37. high = readl(&global_timer->cnt_h);
  38. if (old == high)
  39. break;
  40. else
  41. old = high;
  42. }
  43. timer = high;
  44. return (u64)((timer << 32) | low);
  45. }
  46. static u64 get_time_us(void)
  47. {
  48. u64 timer = get_cpu_global_timer();
  49. timer = ((timer << 2) + (CLK2MHZ(CONFIG_SYS_CPU_CLK) >> 1));
  50. timer /= (u64)CLK2MHZ(CONFIG_SYS_CPU_CLK);
  51. return timer;
  52. }
  53. static ulong get_time_ms(void)
  54. {
  55. return (ulong)(get_time_us() / 1000);
  56. }
  57. int timer_init(void)
  58. {
  59. writel(0x01, &global_timer->ctl);
  60. return 0;
  61. }
  62. void __udelay(unsigned long usec)
  63. {
  64. u64 start, current;
  65. u64 wait;
  66. start = get_cpu_global_timer();
  67. wait = (u64)((usec * CLK2MHZ(CONFIG_SYS_CPU_CLK)) >> 2);
  68. do {
  69. current = get_cpu_global_timer();
  70. } while ((current - start) < wait);
  71. }
  72. ulong get_timer(ulong base)
  73. {
  74. return get_time_ms() - base;
  75. }
  76. unsigned long long get_ticks(void)
  77. {
  78. return get_cpu_global_timer();
  79. }
  80. ulong get_tbclk(void)
  81. {
  82. return (ulong)(CONFIG_SYS_CPU_CLK >> 2);
  83. }