timer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2010 Linaro Limited
  3. * John Rigby <john.rigby@linaro.org>
  4. *
  5. * Based on original from Linux kernel source and
  6. * internal ST-Ericsson U-Boot source.
  7. * (C) Copyright 2009 Alessandro Rubini
  8. * (C) Copyright 2010 ST-Ericsson
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <common.h>
  29. #include <asm/io.h>
  30. #include <asm/arch/hardware.h>
  31. DECLARE_GLOBAL_DATA_PTR;
  32. /*
  33. * The MTU device has some interrupt control registers
  34. * followed by 4 timers.
  35. */
  36. /* The timers */
  37. struct u8500_mtu_timer {
  38. u32 lr; /* Load value */
  39. u32 cv; /* Current value */
  40. u32 cr; /* Control reg */
  41. u32 bglr; /* ??? */
  42. };
  43. /* The MTU that contains the timers */
  44. struct u8500_mtu {
  45. u32 imsc; /* Interrupt mask set/clear */
  46. u32 ris; /* Raw interrupt status */
  47. u32 mis; /* Masked interrupt status */
  48. u32 icr; /* Interrupt clear register */
  49. struct u8500_mtu_timer pt[4];
  50. };
  51. /* bits for the control register */
  52. #define MTU_CR_ONESHOT 0x01 /* if 0 = wraps reloading from BGLR */
  53. #define MTU_CR_32BITS 0x02
  54. #define MTU_CR_PRESCALE_1 0x00
  55. #define MTU_CR_PRESCALE_16 0x04
  56. #define MTU_CR_PRESCALE_256 0x08
  57. #define MTU_CR_PRESCALE_MASK 0x0c
  58. #define MTU_CR_PERIODIC 0x40 /* if 0 = free-running */
  59. #define MTU_CR_ENA 0x80
  60. /*
  61. * The MTU is clocked at 133 MHz by default. (V1 and later)
  62. */
  63. #define TIMER_CLOCK (133 * 1000 * 1000 / 16)
  64. #define COUNT_TO_USEC(x) ((x) * 16 / 133)
  65. #define USEC_TO_COUNT(x) ((x) * 133 / 16)
  66. #define TICKS_PER_HZ (TIMER_CLOCK / CONFIG_SYS_HZ)
  67. #define TICKS_TO_HZ(x) ((x) / TICKS_PER_HZ)
  68. #define TIMER_LOAD_VAL 0xffffffff
  69. /*
  70. * MTU timer to use (from 0 to 3).
  71. */
  72. #define MTU_TIMER 2
  73. static struct u8500_mtu_timer *timer_base =
  74. &((struct u8500_mtu *)U8500_MTU0_BASE_V1)->pt[MTU_TIMER];
  75. /* macro to read the 32 bit timer: since it decrements, we invert read value */
  76. #define READ_TIMER() (~readl(&timer_base->cv))
  77. /* Configure a free-running, auto-wrap counter with /16 prescaler */
  78. int timer_init(void)
  79. {
  80. writel(MTU_CR_ENA | MTU_CR_PRESCALE_16 | MTU_CR_32BITS,
  81. &timer_base->cr);
  82. return 0;
  83. }
  84. ulong get_timer_masked(void)
  85. {
  86. /* current tick value */
  87. ulong now = TICKS_TO_HZ(READ_TIMER());
  88. if (now >= gd->lastinc) /* normal (non rollover) */
  89. gd->tbl += (now - gd->lastinc);
  90. else /* rollover */
  91. gd->tbl += (TICKS_TO_HZ(TIMER_LOAD_VAL) - gd->lastinc) + now;
  92. gd->lastinc = now;
  93. return gd->tbl;
  94. }
  95. /* Delay x useconds */
  96. void __udelay(ulong usec)
  97. {
  98. long tmo = usec * (TIMER_CLOCK / 1000) / 1000;
  99. ulong now, last = READ_TIMER();
  100. while (tmo > 0) {
  101. now = READ_TIMER();
  102. if (now > last) /* normal (non rollover) */
  103. tmo -= now - last;
  104. else /* rollover */
  105. tmo -= TIMER_LOAD_VAL - last + now;
  106. last = now;
  107. }
  108. }
  109. ulong get_timer(ulong base)
  110. {
  111. return get_timer_masked() - base;
  112. }
  113. /*
  114. * Emulation of Power architecture long long timebase.
  115. *
  116. * TODO: Support gd->arch.tbu for real long long timebase.
  117. */
  118. unsigned long long get_ticks(void)
  119. {
  120. return get_timer(0);
  121. }
  122. /*
  123. * Emulation of Power architecture timebase.
  124. * NB: Low resolution compared to Power tbclk.
  125. */
  126. ulong get_tbclk(void)
  127. {
  128. return CONFIG_SYS_HZ;
  129. }