clksrc-dbx500-prcmu.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2011
  3. *
  4. * License Terms: GNU General Public License v2
  5. * Author: Mattias Wallin <mattias.wallin@stericsson.com> for ST-Ericsson
  6. * Author: Sundar Iyer for ST-Ericsson
  7. * sched_clock implementation is based on:
  8. * plat-nomadik/timer.c Linus Walleij <linus.walleij@stericsson.com>
  9. *
  10. * DBx500-PRCMU Timer
  11. * The PRCMU has 5 timers which are available in a always-on
  12. * power domain. We use the Timer 4 for our always-on clock
  13. * source on DB8500.
  14. */
  15. #include <linux/clockchips.h>
  16. #include <linux/clksrc-dbx500-prcmu.h>
  17. #include <linux/sched_clock.h>
  18. #define RATE_32K 32768
  19. #define TIMER_MODE_CONTINOUS 0x1
  20. #define TIMER_DOWNCOUNT_VAL 0xffffffff
  21. #define PRCMU_TIMER_REF 0
  22. #define PRCMU_TIMER_DOWNCOUNT 0x4
  23. #define PRCMU_TIMER_MODE 0x8
  24. #define SCHED_CLOCK_MIN_WRAP 131072 /* 2^32 / 32768 */
  25. static void __iomem *clksrc_dbx500_timer_base;
  26. static cycle_t notrace clksrc_dbx500_prcmu_read(struct clocksource *cs)
  27. {
  28. void __iomem *base = clksrc_dbx500_timer_base;
  29. u32 count, count2;
  30. do {
  31. count = readl_relaxed(base + PRCMU_TIMER_DOWNCOUNT);
  32. count2 = readl_relaxed(base + PRCMU_TIMER_DOWNCOUNT);
  33. } while (count2 != count);
  34. /* Negate because the timer is a decrementing counter */
  35. return ~count;
  36. }
  37. static struct clocksource clocksource_dbx500_prcmu = {
  38. .name = "dbx500-prcmu-timer",
  39. .rating = 300,
  40. .read = clksrc_dbx500_prcmu_read,
  41. .mask = CLOCKSOURCE_MASK(32),
  42. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  43. };
  44. #ifdef CONFIG_CLKSRC_DBX500_PRCMU_SCHED_CLOCK
  45. static u64 notrace dbx500_prcmu_sched_clock_read(void)
  46. {
  47. if (unlikely(!clksrc_dbx500_timer_base))
  48. return 0;
  49. return clksrc_dbx500_prcmu_read(&clocksource_dbx500_prcmu);
  50. }
  51. #endif
  52. void __init clksrc_dbx500_prcmu_init(void __iomem *base)
  53. {
  54. clksrc_dbx500_timer_base = base;
  55. /*
  56. * The A9 sub system expects the timer to be configured as
  57. * a continous looping timer.
  58. * The PRCMU should configure it but if it for some reason
  59. * don't we do it here.
  60. */
  61. if (readl(clksrc_dbx500_timer_base + PRCMU_TIMER_MODE) !=
  62. TIMER_MODE_CONTINOUS) {
  63. writel(TIMER_MODE_CONTINOUS,
  64. clksrc_dbx500_timer_base + PRCMU_TIMER_MODE);
  65. writel(TIMER_DOWNCOUNT_VAL,
  66. clksrc_dbx500_timer_base + PRCMU_TIMER_REF);
  67. }
  68. #ifdef CONFIG_CLKSRC_DBX500_PRCMU_SCHED_CLOCK
  69. sched_clock_register(dbx500_prcmu_sched_clock_read, 32, RATE_32K);
  70. #endif
  71. clocksource_register_hz(&clocksource_dbx500_prcmu, RATE_32K);
  72. }