owl-timer.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Actions Semi Owl timer
  3. *
  4. * Copyright 2012 Actions Semi Inc.
  5. * Author: Actions Semi, Inc.
  6. *
  7. * Copyright (c) 2017 SUSE Linux GmbH
  8. * Author: Andreas Färber
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/clockchips.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/irqreturn.h>
  20. #include <linux/sched_clock.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_irq.h>
  24. #define OWL_Tx_CTL 0x0
  25. #define OWL_Tx_CMP 0x4
  26. #define OWL_Tx_VAL 0x8
  27. #define OWL_Tx_CTL_PD BIT(0)
  28. #define OWL_Tx_CTL_INTEN BIT(1)
  29. #define OWL_Tx_CTL_EN BIT(2)
  30. static void __iomem *owl_timer_base;
  31. static void __iomem *owl_clksrc_base;
  32. static void __iomem *owl_clkevt_base;
  33. static inline void owl_timer_reset(void __iomem *base)
  34. {
  35. writel(0, base + OWL_Tx_CTL);
  36. writel(0, base + OWL_Tx_VAL);
  37. writel(0, base + OWL_Tx_CMP);
  38. }
  39. static inline void owl_timer_set_enabled(void __iomem *base, bool enabled)
  40. {
  41. u32 ctl = readl(base + OWL_Tx_CTL);
  42. /* PD bit is cleared when set */
  43. ctl &= ~OWL_Tx_CTL_PD;
  44. if (enabled)
  45. ctl |= OWL_Tx_CTL_EN;
  46. else
  47. ctl &= ~OWL_Tx_CTL_EN;
  48. writel(ctl, base + OWL_Tx_CTL);
  49. }
  50. static u64 notrace owl_timer_sched_read(void)
  51. {
  52. return (u64)readl(owl_clksrc_base + OWL_Tx_VAL);
  53. }
  54. static int owl_timer_set_state_shutdown(struct clock_event_device *evt)
  55. {
  56. owl_timer_set_enabled(owl_clkevt_base, false);
  57. return 0;
  58. }
  59. static int owl_timer_set_state_oneshot(struct clock_event_device *evt)
  60. {
  61. owl_timer_reset(owl_clkevt_base);
  62. return 0;
  63. }
  64. static int owl_timer_tick_resume(struct clock_event_device *evt)
  65. {
  66. return 0;
  67. }
  68. static int owl_timer_set_next_event(unsigned long evt,
  69. struct clock_event_device *ev)
  70. {
  71. void __iomem *base = owl_clkevt_base;
  72. owl_timer_set_enabled(base, false);
  73. writel(OWL_Tx_CTL_INTEN, base + OWL_Tx_CTL);
  74. writel(0, base + OWL_Tx_VAL);
  75. writel(evt, base + OWL_Tx_CMP);
  76. owl_timer_set_enabled(base, true);
  77. return 0;
  78. }
  79. static struct clock_event_device owl_clockevent = {
  80. .name = "owl_tick",
  81. .rating = 200,
  82. .features = CLOCK_EVT_FEAT_ONESHOT |
  83. CLOCK_EVT_FEAT_DYNIRQ,
  84. .set_state_shutdown = owl_timer_set_state_shutdown,
  85. .set_state_oneshot = owl_timer_set_state_oneshot,
  86. .tick_resume = owl_timer_tick_resume,
  87. .set_next_event = owl_timer_set_next_event,
  88. };
  89. static irqreturn_t owl_timer1_interrupt(int irq, void *dev_id)
  90. {
  91. struct clock_event_device *evt = (struct clock_event_device *)dev_id;
  92. writel(OWL_Tx_CTL_PD, owl_clkevt_base + OWL_Tx_CTL);
  93. evt->event_handler(evt);
  94. return IRQ_HANDLED;
  95. }
  96. static int __init owl_timer_init(struct device_node *node)
  97. {
  98. struct clk *clk;
  99. unsigned long rate;
  100. int timer1_irq, ret;
  101. owl_timer_base = of_io_request_and_map(node, 0, "owl-timer");
  102. if (IS_ERR(owl_timer_base)) {
  103. pr_err("Can't map timer registers\n");
  104. return PTR_ERR(owl_timer_base);
  105. }
  106. owl_clksrc_base = owl_timer_base + 0x08;
  107. owl_clkevt_base = owl_timer_base + 0x14;
  108. timer1_irq = of_irq_get_byname(node, "timer1");
  109. if (timer1_irq <= 0) {
  110. pr_err("Can't parse timer1 IRQ\n");
  111. return -EINVAL;
  112. }
  113. clk = of_clk_get(node, 0);
  114. if (IS_ERR(clk))
  115. return PTR_ERR(clk);
  116. rate = clk_get_rate(clk);
  117. owl_timer_reset(owl_clksrc_base);
  118. owl_timer_set_enabled(owl_clksrc_base, true);
  119. sched_clock_register(owl_timer_sched_read, 32, rate);
  120. clocksource_mmio_init(owl_clksrc_base + OWL_Tx_VAL, node->name,
  121. rate, 200, 32, clocksource_mmio_readl_up);
  122. owl_timer_reset(owl_clkevt_base);
  123. ret = request_irq(timer1_irq, owl_timer1_interrupt, IRQF_TIMER,
  124. "owl-timer", &owl_clockevent);
  125. if (ret) {
  126. pr_err("failed to request irq %d\n", timer1_irq);
  127. return ret;
  128. }
  129. owl_clockevent.cpumask = cpumask_of(0);
  130. owl_clockevent.irq = timer1_irq;
  131. clockevents_config_and_register(&owl_clockevent, rate,
  132. 0xf, 0xffffffff);
  133. return 0;
  134. }
  135. TIMER_OF_DECLARE(owl_s500, "actions,s500-timer", owl_timer_init);
  136. TIMER_OF_DECLARE(owl_s700, "actions,s700-timer", owl_timer_init);
  137. TIMER_OF_DECLARE(owl_s900, "actions,s900-timer", owl_timer_init);