timer.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Marvell PXA2xx/3xx timer driver
  3. *
  4. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <asm/arch/pxa-regs.h>
  9. #include <asm/io.h>
  10. #include <common.h>
  11. #include <div64.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. #define TIMER_LOAD_VAL 0xffffffff
  14. #define timestamp (gd->arch.tbl)
  15. #define lastinc (gd->arch.lastinc)
  16. #if defined(CONFIG_CPU_PXA27X) || defined(CONFIG_CPU_MONAHANS)
  17. #define TIMER_FREQ_HZ 3250000
  18. #elif defined(CONFIG_CPU_PXA25X)
  19. #define TIMER_FREQ_HZ 3686400
  20. #else
  21. #error "Timer frequency unknown - please config PXA CPU type"
  22. #endif
  23. static unsigned long long tick_to_time(unsigned long long tick)
  24. {
  25. return lldiv(tick * CONFIG_SYS_HZ, TIMER_FREQ_HZ);
  26. }
  27. static unsigned long long us_to_tick(unsigned long long us)
  28. {
  29. return lldiv(us * TIMER_FREQ_HZ, 1000000);
  30. }
  31. int timer_init(void)
  32. {
  33. writel(0, OSCR);
  34. return 0;
  35. }
  36. unsigned long long get_ticks(void)
  37. {
  38. /* Current tick value */
  39. uint32_t now = readl(OSCR);
  40. if (now >= lastinc) {
  41. /*
  42. * Normal mode (non roll)
  43. * Move stamp forward with absolute diff ticks
  44. */
  45. timestamp += (now - lastinc);
  46. } else {
  47. /* We have rollover of incrementer */
  48. timestamp += (TIMER_LOAD_VAL - lastinc) + now;
  49. }
  50. lastinc = now;
  51. return timestamp;
  52. }
  53. ulong get_timer(ulong base)
  54. {
  55. return tick_to_time(get_ticks()) - base;
  56. }
  57. void __udelay(unsigned long usec)
  58. {
  59. unsigned long long tmp;
  60. ulong tmo;
  61. tmo = us_to_tick(usec);
  62. tmp = get_ticks() + tmo; /* get current timestamp */
  63. while (get_ticks() < tmp) /* loop till event */
  64. /*NOP*/;
  65. }
  66. ulong get_tbclk(void)
  67. {
  68. return TIMER_FREQ_HZ;
  69. }