timer.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Marvell PXA2xx/3xx timer driver
  3. *
  4. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <asm/arch/pxa-regs.h>
  25. #include <asm/io.h>
  26. #include <common.h>
  27. #include <div64.h>
  28. DECLARE_GLOBAL_DATA_PTR;
  29. #define TIMER_LOAD_VAL 0xffffffff
  30. #define timestamp (gd->arch.tbl)
  31. #define lastinc (gd->arch.lastinc)
  32. #if defined(CONFIG_CPU_PXA27X) || defined(CONFIG_CPU_MONAHANS)
  33. #define TIMER_FREQ_HZ 3250000
  34. #elif defined(CONFIG_CPU_PXA25X)
  35. #define TIMER_FREQ_HZ 3686400
  36. #else
  37. #error "Timer frequency unknown - please config PXA CPU type"
  38. #endif
  39. static unsigned long long tick_to_time(unsigned long long tick)
  40. {
  41. return tick * CONFIG_SYS_HZ / TIMER_FREQ_HZ;
  42. }
  43. static unsigned long long us_to_tick(unsigned long long us)
  44. {
  45. return (us * TIMER_FREQ_HZ) / 1000000;
  46. }
  47. int timer_init(void)
  48. {
  49. writel(0, OSCR);
  50. return 0;
  51. }
  52. unsigned long long get_ticks(void)
  53. {
  54. /* Current tick value */
  55. uint32_t now = readl(OSCR);
  56. if (now >= lastinc) {
  57. /*
  58. * Normal mode (non roll)
  59. * Move stamp forward with absolute diff ticks
  60. */
  61. timestamp += (now - lastinc);
  62. } else {
  63. /* We have rollover of incrementer */
  64. timestamp += (TIMER_LOAD_VAL - lastinc) + now;
  65. }
  66. lastinc = now;
  67. return timestamp;
  68. }
  69. ulong get_timer(ulong base)
  70. {
  71. return tick_to_time(get_ticks()) - base;
  72. }
  73. void __udelay(unsigned long usec)
  74. {
  75. unsigned long long tmp;
  76. ulong tmo;
  77. tmo = us_to_tick(usec);
  78. tmp = get_ticks() + tmo; /* get current timestamp */
  79. while (get_ticks() < tmp) /* loop till event */
  80. /*NOP*/;
  81. }
  82. ulong get_tbclk(void)
  83. {
  84. return TIMER_FREQ_HZ;
  85. }