time.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/io.h>
  33. #include <sh_tmu.h>
  34. static struct tmu_regs *tmu = (struct tmu_regs *)TMU_BASE;
  35. static u16 bit;
  36. static unsigned long last_tcnt;
  37. static unsigned long long overflow_ticks;
  38. unsigned long get_tbclk(void)
  39. {
  40. return get_tmu0_clk_rate() >> ((bit + 1) * 2);
  41. }
  42. static inline unsigned long long tick_to_time(unsigned long long tick)
  43. {
  44. tick *= CONFIG_SYS_HZ;
  45. do_div(tick, get_tbclk());
  46. return tick;
  47. }
  48. static inline unsigned long long usec_to_tick(unsigned long long usec)
  49. {
  50. usec *= get_tbclk();
  51. do_div(usec, 1000000);
  52. return usec;
  53. }
  54. static void tmu_timer_start(unsigned int timer)
  55. {
  56. if (timer > 2)
  57. return;
  58. writeb(readb(&tmu->tstr) | (1 << timer), &tmu->tstr);
  59. }
  60. static void tmu_timer_stop(unsigned int timer)
  61. {
  62. if (timer > 2)
  63. return;
  64. writeb(readb(&tmu->tstr) & ~(1 << timer), &tmu->tstr);
  65. }
  66. int timer_init(void)
  67. {
  68. bit = (ffs(CONFIG_SYS_TMU_CLK_DIV) >> 1) - 1;
  69. writew(readw(&tmu->tcr0) | bit, &tmu->tcr0);
  70. tmu_timer_stop(0);
  71. tmu_timer_start(0);
  72. last_tcnt = 0;
  73. overflow_ticks = 0;
  74. return 0;
  75. }
  76. unsigned long long get_ticks(void)
  77. {
  78. unsigned long tcnt = 0 - readl(&tmu->tcnt0);
  79. if (last_tcnt > tcnt) /* overflow */
  80. overflow_ticks++;
  81. last_tcnt = tcnt;
  82. return (overflow_ticks << 32) | tcnt;
  83. }
  84. void __udelay(unsigned long usec)
  85. {
  86. unsigned long long tmp;
  87. ulong tmo;
  88. tmo = usec_to_tick(usec);
  89. tmp = get_ticks() + tmo; /* get current timestamp */
  90. while (get_ticks() < tmp) /* loop till event */
  91. /*NOP*/;
  92. }
  93. unsigned long get_timer(unsigned long base)
  94. {
  95. /* return msec */
  96. return tick_to_time(get_ticks()) - base;
  97. }
  98. void set_timer(unsigned long t)
  99. {
  100. writel((0 - t), &tmu->tcnt0);
  101. }
  102. void reset_timer(void)
  103. {
  104. tmu_timer_stop(0);
  105. set_timer(0);
  106. tmu_timer_start(0);
  107. }