timer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2006
  3. * Ingenic Semiconductor, <jlwei@ingenic.cn>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. */
  20. #include <config.h>
  21. #include <common.h>
  22. #include <asm/io.h>
  23. #include <asm/jz4740.h>
  24. #define TIMER_CHAN 0
  25. #define TIMER_FDATA 0xffff /* Timer full data value */
  26. DECLARE_GLOBAL_DATA_PTR;
  27. static struct jz4740_tcu *tcu = (struct jz4740_tcu *)JZ4740_TCU_BASE;
  28. void reset_timer_masked(void)
  29. {
  30. /* reset time */
  31. gd->lastinc = readl(&tcu->tcnt0);
  32. gd->arch.tbl = 0;
  33. }
  34. ulong get_timer_masked(void)
  35. {
  36. ulong now = readl(&tcu->tcnt0);
  37. if (gd->lastinc <= now)
  38. gd->arch.tbl += now - gd->lastinc; /* normal mode */
  39. else {
  40. /* we have an overflow ... */
  41. gd->arch.tbl += TIMER_FDATA + now - gd->lastinc;
  42. }
  43. gd->lastinc = now;
  44. return gd->arch.tbl;
  45. }
  46. void udelay_masked(unsigned long usec)
  47. {
  48. ulong tmo;
  49. ulong endtime;
  50. signed long diff;
  51. /* normalize */
  52. if (usec >= 1000) {
  53. tmo = usec / 1000;
  54. tmo *= CONFIG_SYS_HZ;
  55. tmo /= 1000;
  56. } else {
  57. if (usec > 1) {
  58. tmo = usec * CONFIG_SYS_HZ;
  59. tmo /= 1000*1000;
  60. } else
  61. tmo = 1;
  62. }
  63. endtime = get_timer_masked() + tmo;
  64. do {
  65. ulong now = get_timer_masked();
  66. diff = endtime - now;
  67. } while (diff >= 0);
  68. }
  69. int timer_init(void)
  70. {
  71. writel(TCU_TCSR_PRESCALE256 | TCU_TCSR_EXT_EN, &tcu->tcsr0);
  72. writel(0, &tcu->tcnt0);
  73. writel(0, &tcu->tdhr0);
  74. writel(TIMER_FDATA, &tcu->tdfr0);
  75. /* mask irqs */
  76. writel((1 << TIMER_CHAN) | (1 << (TIMER_CHAN + 16)), &tcu->tmsr);
  77. writel(1 << TIMER_CHAN, &tcu->tscr); /* enable timer clock */
  78. writeb(1 << TIMER_CHAN, &tcu->tesr); /* start counting up */
  79. gd->lastinc = 0;
  80. gd->arch.tbl = 0;
  81. return 0;
  82. }
  83. void reset_timer(void)
  84. {
  85. reset_timer_masked();
  86. }
  87. ulong get_timer(ulong base)
  88. {
  89. return get_timer_masked() - base;
  90. }
  91. void set_timer(ulong t)
  92. {
  93. gd->arch.tbl = t;
  94. }
  95. void __udelay(unsigned long usec)
  96. {
  97. ulong tmo, tmp;
  98. /* normalize */
  99. if (usec >= 1000) {
  100. tmo = usec / 1000;
  101. tmo *= CONFIG_SYS_HZ;
  102. tmo /= 1000;
  103. } else {
  104. if (usec >= 1) {
  105. tmo = usec * CONFIG_SYS_HZ;
  106. tmo /= 1000 * 1000;
  107. } else
  108. tmo = 1;
  109. }
  110. /* check for rollover during this delay */
  111. tmp = get_timer(0);
  112. if ((tmp + tmo) < tmp)
  113. reset_timer_masked(); /* timer would roll over */
  114. else
  115. tmo += tmp;
  116. while (get_timer_masked() < tmo)
  117. ;
  118. }
  119. /*
  120. * This function is derived from PowerPC code (read timebase as long long).
  121. * On MIPS it just returns the timer value.
  122. */
  123. unsigned long long get_ticks(void)
  124. {
  125. return get_timer(0);
  126. }
  127. /*
  128. * This function is derived from PowerPC code (timebase clock frequency).
  129. * On MIPS it returns the number of timer ticks per second.
  130. */
  131. ulong get_tbclk(void)
  132. {
  133. return CONFIG_SYS_HZ;
  134. }