time.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * (C) Copyright 2009
  3. * Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
  4. *
  5. * (C) Copyright 2007-2012
  6. * Nobobuhiro Iwamatsu <iwamatsu@nigauri.org>
  7. *
  8. * (C) Copyright 2003
  9. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. #include <common.h>
  30. #include <div64.h>
  31. #include <asm/processor.h>
  32. #include <asm/clk.h>
  33. #include <asm/io.h>
  34. #include <sh_tmu.h>
  35. static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
  36. #define TMU_MAX_COUNTER (~0UL)
  37. static ulong timer_freq;
  38. static unsigned long last_tcnt;
  39. static unsigned long long overflow_ticks;
  40. static inline unsigned long long tick_to_time(unsigned long long tick)
  41. {
  42. tick *= CONFIG_SYS_HZ;
  43. do_div(tick, timer_freq);
  44. return tick;
  45. }
  46. static inline unsigned long long usec_to_tick(unsigned long long usec)
  47. {
  48. usec *= timer_freq;
  49. do_div(usec, 1000000);
  50. return usec;
  51. }
  52. static void tmu_timer_start(unsigned int timer)
  53. {
  54. if (timer > 2)
  55. return;
  56. writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
  57. }
  58. static void tmu_timer_stop(unsigned int timer)
  59. {
  60. if (timer > 2)
  61. return;
  62. writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
  63. }
  64. int timer_init(void)
  65. {
  66. /* Divide clock by CONFIG_SYS_TMU_CLK_DIV */
  67. u16 bit = 0;
  68. switch (CONFIG_SYS_TMU_CLK_DIV) {
  69. case 1024:
  70. bit = 4;
  71. break;
  72. case 256:
  73. bit = 3;
  74. break;
  75. case 64:
  76. bit = 2;
  77. break;
  78. case 16:
  79. bit = 1;
  80. break;
  81. case 4:
  82. default:
  83. break;
  84. }
  85. writew(readw(&tmu->tcr0) | bit, &tmu->tcr0);
  86. /* Calc clock rate */
  87. timer_freq = get_tmu0_clk_rate() >> ((bit + 1) * 2);
  88. tmu_timer_stop(0);
  89. tmu_timer_start(0);
  90. last_tcnt = 0;
  91. overflow_ticks = 0;
  92. return 0;
  93. }
  94. unsigned long long get_ticks(void)
  95. {
  96. unsigned long tcnt = 0 - readl(&tmu->tcnt0);
  97. if (last_tcnt > tcnt) /* overflow */
  98. overflow_ticks++;
  99. last_tcnt = tcnt;
  100. return (overflow_ticks << 32) | tcnt;
  101. }
  102. void __udelay(unsigned long usec)
  103. {
  104. unsigned long long tmp;
  105. ulong tmo;
  106. tmo = usec_to_tick(usec);
  107. tmp = get_ticks() + tmo; /* get current timestamp */
  108. while (get_ticks() < tmp) /* loop till event */
  109. /*NOP*/;
  110. }
  111. unsigned long get_timer(unsigned long base)
  112. {
  113. /* return msec */
  114. return tick_to_time(get_ticks()) - base;
  115. }
  116. unsigned long get_tbclk(void)
  117. {
  118. return timer_freq;
  119. }