timer-pistachio.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Pistachio clocksource based on general-purpose timers
  4. *
  5. * Copyright (C) 2015 Imagination Technologies
  6. */
  7. #define pr_fmt(fmt) "%s: " fmt, __func__
  8. #include <linux/clk.h>
  9. #include <linux/clocksource.h>
  10. #include <linux/clockchips.h>
  11. #include <linux/delay.h>
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/sched_clock.h>
  21. #include <linux/time.h>
  22. /* Top level reg */
  23. #define CR_TIMER_CTRL_CFG 0x00
  24. #define TIMER_ME_GLOBAL BIT(0)
  25. #define CR_TIMER_REV 0x10
  26. /* Timer specific registers */
  27. #define TIMER_CFG 0x20
  28. #define TIMER_ME_LOCAL BIT(0)
  29. #define TIMER_RELOAD_VALUE 0x24
  30. #define TIMER_CURRENT_VALUE 0x28
  31. #define TIMER_CURRENT_OVERFLOW_VALUE 0x2C
  32. #define TIMER_IRQ_STATUS 0x30
  33. #define TIMER_IRQ_CLEAR 0x34
  34. #define TIMER_IRQ_MASK 0x38
  35. #define PERIP_TIMER_CONTROL 0x90
  36. /* Timer specific configuration Values */
  37. #define RELOAD_VALUE 0xffffffff
  38. struct pistachio_clocksource {
  39. void __iomem *base;
  40. raw_spinlock_t lock;
  41. struct clocksource cs;
  42. };
  43. static struct pistachio_clocksource pcs_gpt;
  44. #define to_pistachio_clocksource(cs) \
  45. container_of(cs, struct pistachio_clocksource, cs)
  46. static inline u32 gpt_readl(void __iomem *base, u32 offset, u32 gpt_id)
  47. {
  48. return readl(base + 0x20 * gpt_id + offset);
  49. }
  50. static inline void gpt_writel(void __iomem *base, u32 value, u32 offset,
  51. u32 gpt_id)
  52. {
  53. writel(value, base + 0x20 * gpt_id + offset);
  54. }
  55. static u64 notrace
  56. pistachio_clocksource_read_cycles(struct clocksource *cs)
  57. {
  58. struct pistachio_clocksource *pcs = to_pistachio_clocksource(cs);
  59. __maybe_unused u32 overflow;
  60. u32 counter;
  61. unsigned long flags;
  62. /*
  63. * The counter value is only refreshed after the overflow value is read.
  64. * And they must be read in strict order, hence raw spin lock added.
  65. */
  66. raw_spin_lock_irqsave(&pcs->lock, flags);
  67. overflow = gpt_readl(pcs->base, TIMER_CURRENT_OVERFLOW_VALUE, 0);
  68. counter = gpt_readl(pcs->base, TIMER_CURRENT_VALUE, 0);
  69. raw_spin_unlock_irqrestore(&pcs->lock, flags);
  70. return (u64)~counter;
  71. }
  72. static u64 notrace pistachio_read_sched_clock(void)
  73. {
  74. return pistachio_clocksource_read_cycles(&pcs_gpt.cs);
  75. }
  76. static void pistachio_clksrc_set_mode(struct clocksource *cs, int timeridx,
  77. int enable)
  78. {
  79. struct pistachio_clocksource *pcs = to_pistachio_clocksource(cs);
  80. u32 val;
  81. val = gpt_readl(pcs->base, TIMER_CFG, timeridx);
  82. if (enable)
  83. val |= TIMER_ME_LOCAL;
  84. else
  85. val &= ~TIMER_ME_LOCAL;
  86. gpt_writel(pcs->base, val, TIMER_CFG, timeridx);
  87. }
  88. static void pistachio_clksrc_enable(struct clocksource *cs, int timeridx)
  89. {
  90. struct pistachio_clocksource *pcs = to_pistachio_clocksource(cs);
  91. /* Disable GPT local before loading reload value */
  92. pistachio_clksrc_set_mode(cs, timeridx, false);
  93. gpt_writel(pcs->base, RELOAD_VALUE, TIMER_RELOAD_VALUE, timeridx);
  94. pistachio_clksrc_set_mode(cs, timeridx, true);
  95. }
  96. static void pistachio_clksrc_disable(struct clocksource *cs, int timeridx)
  97. {
  98. /* Disable GPT local */
  99. pistachio_clksrc_set_mode(cs, timeridx, false);
  100. }
  101. static int pistachio_clocksource_enable(struct clocksource *cs)
  102. {
  103. pistachio_clksrc_enable(cs, 0);
  104. return 0;
  105. }
  106. static void pistachio_clocksource_disable(struct clocksource *cs)
  107. {
  108. pistachio_clksrc_disable(cs, 0);
  109. }
  110. /* Desirable clock source for pistachio platform */
  111. static struct pistachio_clocksource pcs_gpt = {
  112. .cs = {
  113. .name = "gptimer",
  114. .rating = 300,
  115. .enable = pistachio_clocksource_enable,
  116. .disable = pistachio_clocksource_disable,
  117. .read = pistachio_clocksource_read_cycles,
  118. .mask = CLOCKSOURCE_MASK(32),
  119. .flags = CLOCK_SOURCE_IS_CONTINUOUS |
  120. CLOCK_SOURCE_SUSPEND_NONSTOP,
  121. },
  122. };
  123. static int __init pistachio_clksrc_of_init(struct device_node *node)
  124. {
  125. struct clk *sys_clk, *fast_clk;
  126. struct regmap *periph_regs;
  127. unsigned long rate;
  128. int ret;
  129. pcs_gpt.base = of_iomap(node, 0);
  130. if (!pcs_gpt.base) {
  131. pr_err("cannot iomap\n");
  132. return -ENXIO;
  133. }
  134. periph_regs = syscon_regmap_lookup_by_phandle(node, "img,cr-periph");
  135. if (IS_ERR(periph_regs)) {
  136. pr_err("cannot get peripheral regmap (%ld)\n",
  137. PTR_ERR(periph_regs));
  138. return PTR_ERR(periph_regs);
  139. }
  140. /* Switch to using the fast counter clock */
  141. ret = regmap_update_bits(periph_regs, PERIP_TIMER_CONTROL,
  142. 0xf, 0x0);
  143. if (ret)
  144. return ret;
  145. sys_clk = of_clk_get_by_name(node, "sys");
  146. if (IS_ERR(sys_clk)) {
  147. pr_err("clock get failed (%ld)\n", PTR_ERR(sys_clk));
  148. return PTR_ERR(sys_clk);
  149. }
  150. fast_clk = of_clk_get_by_name(node, "fast");
  151. if (IS_ERR(fast_clk)) {
  152. pr_err("clock get failed (%lu)\n", PTR_ERR(fast_clk));
  153. return PTR_ERR(fast_clk);
  154. }
  155. ret = clk_prepare_enable(sys_clk);
  156. if (ret < 0) {
  157. pr_err("failed to enable clock (%d)\n", ret);
  158. return ret;
  159. }
  160. ret = clk_prepare_enable(fast_clk);
  161. if (ret < 0) {
  162. pr_err("failed to enable clock (%d)\n", ret);
  163. clk_disable_unprepare(sys_clk);
  164. return ret;
  165. }
  166. rate = clk_get_rate(fast_clk);
  167. /* Disable irq's for clocksource usage */
  168. gpt_writel(pcs_gpt.base, 0, TIMER_IRQ_MASK, 0);
  169. gpt_writel(pcs_gpt.base, 0, TIMER_IRQ_MASK, 1);
  170. gpt_writel(pcs_gpt.base, 0, TIMER_IRQ_MASK, 2);
  171. gpt_writel(pcs_gpt.base, 0, TIMER_IRQ_MASK, 3);
  172. /* Enable timer block */
  173. writel(TIMER_ME_GLOBAL, pcs_gpt.base);
  174. raw_spin_lock_init(&pcs_gpt.lock);
  175. sched_clock_register(pistachio_read_sched_clock, 32, rate);
  176. return clocksource_register_hz(&pcs_gpt.cs, rate);
  177. }
  178. TIMER_OF_DECLARE(pistachio_gptimer, "img,pistachio-gptimer",
  179. pistachio_clksrc_of_init);