timer-mediatek-cpux.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * MediaTek SoCs CPUX General Purpose Timer handling
  4. *
  5. * Based on timer-mediatek.c:
  6. * Copyright (C) 2014 Matthias Brugger <[email protected]>
  7. *
  8. * Copyright (C) 2022 Collabora Ltd.
  9. * AngeloGioacchino Del Regno <[email protected]>
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/clockchips.h>
  13. #include <linux/clocksource.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irqreturn.h>
  16. #include <linux/sched_clock.h>
  17. #include <linux/slab.h>
  18. #include "timer-of.h"
  19. #define TIMER_SYNC_TICKS 3
  20. /* cpux mcusys wrapper */
  21. #define CPUX_CON_REG 0x0
  22. #define CPUX_IDX_REG 0x4
  23. /* cpux */
  24. #define CPUX_IDX_GLOBAL_CTRL 0x0
  25. #define CPUX_ENABLE BIT(0)
  26. #define CPUX_CLK_DIV_MASK GENMASK(10, 8)
  27. #define CPUX_CLK_DIV1 BIT(8)
  28. #define CPUX_CLK_DIV2 BIT(9)
  29. #define CPUX_CLK_DIV4 BIT(10)
  30. #define CPUX_IDX_GLOBAL_IRQ 0x30
  31. static u32 mtk_cpux_readl(u32 reg_idx, struct timer_of *to)
  32. {
  33. writel(reg_idx, timer_of_base(to) + CPUX_IDX_REG);
  34. return readl(timer_of_base(to) + CPUX_CON_REG);
  35. }
  36. static void mtk_cpux_writel(u32 val, u32 reg_idx, struct timer_of *to)
  37. {
  38. writel(reg_idx, timer_of_base(to) + CPUX_IDX_REG);
  39. writel(val, timer_of_base(to) + CPUX_CON_REG);
  40. }
  41. static void mtk_cpux_set_irq(struct timer_of *to, bool enable)
  42. {
  43. const unsigned long *irq_mask = cpumask_bits(cpu_possible_mask);
  44. u32 val;
  45. val = mtk_cpux_readl(CPUX_IDX_GLOBAL_IRQ, to);
  46. if (enable)
  47. val |= *irq_mask;
  48. else
  49. val &= ~(*irq_mask);
  50. mtk_cpux_writel(val, CPUX_IDX_GLOBAL_IRQ, to);
  51. }
  52. static int mtk_cpux_clkevt_shutdown(struct clock_event_device *clkevt)
  53. {
  54. /* Clear any irq */
  55. mtk_cpux_set_irq(to_timer_of(clkevt), false);
  56. /*
  57. * Disabling CPUXGPT timer will crash the platform, especially
  58. * if Trusted Firmware is using it (usually, for sleep states),
  59. * so we only mask the IRQ and call it a day.
  60. */
  61. return 0;
  62. }
  63. static int mtk_cpux_clkevt_resume(struct clock_event_device *clkevt)
  64. {
  65. mtk_cpux_set_irq(to_timer_of(clkevt), true);
  66. return 0;
  67. }
  68. static struct timer_of to = {
  69. /*
  70. * There are per-cpu interrupts for the CPUX General Purpose Timer
  71. * but since this timer feeds the AArch64 System Timer we can rely
  72. * on the CPU timer PPIs as well, so we don't declare TIMER_OF_IRQ.
  73. */
  74. .flags = TIMER_OF_BASE | TIMER_OF_CLOCK,
  75. .clkevt = {
  76. .name = "mtk-cpuxgpt",
  77. .cpumask = cpu_possible_mask,
  78. .rating = 10,
  79. .set_state_shutdown = mtk_cpux_clkevt_shutdown,
  80. .tick_resume = mtk_cpux_clkevt_resume,
  81. },
  82. };
  83. static int __init mtk_cpux_init(struct device_node *node)
  84. {
  85. u32 freq, val;
  86. int ret;
  87. /* If this fails, bad things are about to happen... */
  88. ret = timer_of_init(node, &to);
  89. if (ret) {
  90. WARN(1, "Cannot start CPUX timers.\n");
  91. return ret;
  92. }
  93. /*
  94. * Check if we're given a clock with the right frequency for this
  95. * timer, otherwise warn but keep going with the setup anyway, as
  96. * that makes it possible to still boot the kernel, even though
  97. * it may not work correctly (random lockups, etc).
  98. * The reason behind this is that having an early UART may not be
  99. * possible for everyone and this gives a chance to retrieve kmsg
  100. * for eventual debugging even on consumer devices.
  101. */
  102. freq = timer_of_rate(&to);
  103. if (freq > 13000000)
  104. WARN(1, "Requested unsupported timer frequency %u\n", freq);
  105. /* Clock input is 26MHz, set DIV2 to achieve 13MHz clock */
  106. val = mtk_cpux_readl(CPUX_IDX_GLOBAL_CTRL, &to);
  107. val &= ~CPUX_CLK_DIV_MASK;
  108. val |= CPUX_CLK_DIV2;
  109. mtk_cpux_writel(val, CPUX_IDX_GLOBAL_CTRL, &to);
  110. /* Enable all CPUXGPT timers */
  111. val = mtk_cpux_readl(CPUX_IDX_GLOBAL_CTRL, &to);
  112. mtk_cpux_writel(val | CPUX_ENABLE, CPUX_IDX_GLOBAL_CTRL, &to);
  113. clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
  114. TIMER_SYNC_TICKS, 0xffffffff);
  115. return 0;
  116. }
  117. TIMER_OF_DECLARE(mtk_mt6795, "mediatek,mt6795-systimer", mtk_cpux_init);