timer.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * (C) Copyright 2009
  3. * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
  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/hardware.h>
  26. #include <asm/arch/spr_gpt.h>
  27. #include <asm/arch/spr_misc.h>
  28. #define GPT_RESOLUTION (CONFIG_SPEAR_HZ_CLOCK / CONFIG_SPEAR_HZ)
  29. #define READ_TIMER() (readl(&gpt_regs_p->count) & GPT_FREE_RUNNING)
  30. static struct gpt_regs *const gpt_regs_p =
  31. (struct gpt_regs *)CONFIG_SPEAR_TIMERBASE;
  32. static struct misc_regs *const misc_regs_p =
  33. (struct misc_regs *)CONFIG_SPEAR_MISCBASE;
  34. DECLARE_GLOBAL_DATA_PTR;
  35. #define timestamp gd->tbl
  36. #define lastdec gd->lastinc
  37. int timer_init(void)
  38. {
  39. u32 synth;
  40. /* Prescaler setting */
  41. #if defined(CONFIG_SPEAR3XX)
  42. writel(MISC_PRSC_CFG, &misc_regs_p->prsc2_clk_cfg);
  43. synth = MISC_GPT4SYNTH;
  44. #elif defined(CONFIG_SPEAR600)
  45. writel(MISC_PRSC_CFG, &misc_regs_p->prsc1_clk_cfg);
  46. synth = MISC_GPT3SYNTH;
  47. #else
  48. # error Incorrect config. Can only be spear{600|300|310|320}
  49. #endif
  50. writel(readl(&misc_regs_p->periph_clk_cfg) | synth,
  51. &misc_regs_p->periph_clk_cfg);
  52. /* disable timers */
  53. writel(GPT_PRESCALER_1 | GPT_MODE_AUTO_RELOAD, &gpt_regs_p->control);
  54. /* load value for free running */
  55. writel(GPT_FREE_RUNNING, &gpt_regs_p->compare);
  56. /* auto reload, start timer */
  57. writel(readl(&gpt_regs_p->control) | GPT_ENABLE, &gpt_regs_p->control);
  58. reset_timer_masked();
  59. return 0;
  60. }
  61. /*
  62. * timer without interrupts
  63. */
  64. void reset_timer(void)
  65. {
  66. reset_timer_masked();
  67. }
  68. ulong get_timer(ulong base)
  69. {
  70. return (get_timer_masked() / GPT_RESOLUTION) - base;
  71. }
  72. void set_timer(ulong t)
  73. {
  74. timestamp = t;
  75. }
  76. void __udelay(unsigned long usec)
  77. {
  78. ulong tmo;
  79. ulong start = get_timer_masked();
  80. ulong tenudelcnt = CONFIG_SPEAR_HZ_CLOCK / (1000 * 100);
  81. ulong rndoff;
  82. rndoff = (usec % 10) ? 1 : 0;
  83. /* tenudelcnt timer tick gives 10 microsecconds delay */
  84. tmo = ((usec / 10) + rndoff) * tenudelcnt;
  85. while ((ulong) (get_timer_masked() - start) < tmo)
  86. ;
  87. }
  88. void reset_timer_masked(void)
  89. {
  90. /* reset time */
  91. lastdec = READ_TIMER();
  92. timestamp = 0;
  93. }
  94. ulong get_timer_masked(void)
  95. {
  96. ulong now = READ_TIMER();
  97. if (now >= lastdec) {
  98. /* normal mode */
  99. timestamp += now - lastdec;
  100. } else {
  101. /* we have an overflow ... */
  102. timestamp += now + GPT_FREE_RUNNING - lastdec;
  103. }
  104. lastdec = now;
  105. return timestamp;
  106. }
  107. void udelay_masked(unsigned long usec)
  108. {
  109. return udelay(usec);
  110. }
  111. /*
  112. * This function is derived from PowerPC code (read timebase as long long).
  113. * On ARM it just returns the timer value.
  114. */
  115. unsigned long long get_ticks(void)
  116. {
  117. return get_timer(0);
  118. }
  119. /*
  120. * This function is derived from PowerPC code (timebase clock frequency).
  121. * On ARM it returns the number of timer ticks per second.
  122. */
  123. ulong get_tbclk(void)
  124. {
  125. return CONFIG_SPEAR_HZ;
  126. }