time.c 2.8 KB

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