timer-of.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017, Linaro Ltd. All rights reserved.
  4. *
  5. * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of_irq.h>
  12. #include <linux/slab.h>
  13. #include "timer-of.h"
  14. /**
  15. * timer_of_irq_exit - Release the interrupt
  16. * @of_irq: an of_timer_irq structure pointer
  17. *
  18. * Free the irq resource
  19. */
  20. static __init void timer_of_irq_exit(struct of_timer_irq *of_irq)
  21. {
  22. struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
  23. struct clock_event_device *clkevt = &to->clkevt;
  24. free_irq(of_irq->irq, clkevt);
  25. }
  26. /**
  27. * timer_of_irq_init - Request the interrupt
  28. * @np: a device tree node pointer
  29. * @of_irq: an of_timer_irq structure pointer
  30. *
  31. * Get the interrupt number from the DT from its definition and
  32. * request it. The interrupt is gotten by falling back the following way:
  33. *
  34. * - Get interrupt number by name
  35. * - Get interrupt number by index
  36. *
  37. * Returns 0 on success, < 0 otherwise
  38. */
  39. static __init int timer_of_irq_init(struct device_node *np,
  40. struct of_timer_irq *of_irq)
  41. {
  42. int ret;
  43. struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
  44. struct clock_event_device *clkevt = &to->clkevt;
  45. if (of_irq->name) {
  46. of_irq->irq = ret = of_irq_get_byname(np, of_irq->name);
  47. if (ret < 0) {
  48. pr_err("Failed to get interrupt %s for %pOF\n",
  49. of_irq->name, np);
  50. return ret;
  51. }
  52. } else {
  53. of_irq->irq = irq_of_parse_and_map(np, of_irq->index);
  54. }
  55. if (!of_irq->irq) {
  56. pr_err("Failed to map interrupt for %pOF\n", np);
  57. return -EINVAL;
  58. }
  59. ret = request_irq(of_irq->irq, of_irq->handler,
  60. of_irq->flags ? of_irq->flags : IRQF_TIMER,
  61. np->full_name, clkevt);
  62. if (ret) {
  63. pr_err("Failed to request irq %d for %pOF\n", of_irq->irq, np);
  64. return ret;
  65. }
  66. clkevt->irq = of_irq->irq;
  67. return 0;
  68. }
  69. /**
  70. * timer_of_clk_exit - Release the clock resources
  71. * @of_clk: a of_timer_clk structure pointer
  72. *
  73. * Disables and releases the refcount on the clk
  74. */
  75. static __init void timer_of_clk_exit(struct of_timer_clk *of_clk)
  76. {
  77. of_clk->rate = 0;
  78. clk_disable_unprepare(of_clk->clk);
  79. clk_put(of_clk->clk);
  80. }
  81. /**
  82. * timer_of_clk_init - Initialize the clock resources
  83. * @np: a device tree node pointer
  84. * @of_clk: a of_timer_clk structure pointer
  85. *
  86. * Get the clock by name or by index, enable it and get the rate
  87. *
  88. * Returns 0 on success, < 0 otherwise
  89. */
  90. static __init int timer_of_clk_init(struct device_node *np,
  91. struct of_timer_clk *of_clk)
  92. {
  93. int ret;
  94. of_clk->clk = of_clk->name ? of_clk_get_by_name(np, of_clk->name) :
  95. of_clk_get(np, of_clk->index);
  96. if (IS_ERR(of_clk->clk)) {
  97. ret = PTR_ERR(of_clk->clk);
  98. if (ret != -EPROBE_DEFER)
  99. pr_err("Failed to get clock for %pOF\n", np);
  100. goto out;
  101. }
  102. ret = clk_prepare_enable(of_clk->clk);
  103. if (ret) {
  104. pr_err("Failed for enable clock for %pOF\n", np);
  105. goto out_clk_put;
  106. }
  107. of_clk->rate = clk_get_rate(of_clk->clk);
  108. if (!of_clk->rate) {
  109. ret = -EINVAL;
  110. pr_err("Failed to get clock rate for %pOF\n", np);
  111. goto out_clk_disable;
  112. }
  113. of_clk->period = DIV_ROUND_UP(of_clk->rate, HZ);
  114. out:
  115. return ret;
  116. out_clk_disable:
  117. clk_disable_unprepare(of_clk->clk);
  118. out_clk_put:
  119. clk_put(of_clk->clk);
  120. goto out;
  121. }
  122. static __init void timer_of_base_exit(struct of_timer_base *of_base)
  123. {
  124. iounmap(of_base->base);
  125. }
  126. static __init int timer_of_base_init(struct device_node *np,
  127. struct of_timer_base *of_base)
  128. {
  129. of_base->base = of_base->name ?
  130. of_io_request_and_map(np, of_base->index, of_base->name) :
  131. of_iomap(np, of_base->index);
  132. if (IS_ERR_OR_NULL(of_base->base)) {
  133. pr_err("Failed to iomap (%s:%s)\n", np->name, of_base->name);
  134. return of_base->base ? PTR_ERR(of_base->base) : -ENOMEM;
  135. }
  136. return 0;
  137. }
  138. int __init timer_of_init(struct device_node *np, struct timer_of *to)
  139. {
  140. int ret = -EINVAL;
  141. int flags = 0;
  142. if (to->flags & TIMER_OF_BASE) {
  143. ret = timer_of_base_init(np, &to->of_base);
  144. if (ret)
  145. goto out_fail;
  146. flags |= TIMER_OF_BASE;
  147. }
  148. if (to->flags & TIMER_OF_CLOCK) {
  149. ret = timer_of_clk_init(np, &to->of_clk);
  150. if (ret)
  151. goto out_fail;
  152. flags |= TIMER_OF_CLOCK;
  153. }
  154. if (to->flags & TIMER_OF_IRQ) {
  155. ret = timer_of_irq_init(np, &to->of_irq);
  156. if (ret)
  157. goto out_fail;
  158. flags |= TIMER_OF_IRQ;
  159. }
  160. if (!to->clkevt.name)
  161. to->clkevt.name = np->full_name;
  162. to->np = np;
  163. return ret;
  164. out_fail:
  165. if (flags & TIMER_OF_IRQ)
  166. timer_of_irq_exit(&to->of_irq);
  167. if (flags & TIMER_OF_CLOCK)
  168. timer_of_clk_exit(&to->of_clk);
  169. if (flags & TIMER_OF_BASE)
  170. timer_of_base_exit(&to->of_base);
  171. return ret;
  172. }
  173. /**
  174. * timer_of_cleanup - release timer_of resources
  175. * @to: timer_of structure
  176. *
  177. * Release the resources that has been used in timer_of_init().
  178. * This function should be called in init error cases
  179. */
  180. void __init timer_of_cleanup(struct timer_of *to)
  181. {
  182. if (to->flags & TIMER_OF_IRQ)
  183. timer_of_irq_exit(&to->of_irq);
  184. if (to->flags & TIMER_OF_CLOCK)
  185. timer_of_clk_exit(&to->of_clk);
  186. if (to->flags & TIMER_OF_BASE)
  187. timer_of_base_exit(&to->of_base);
  188. }