timer-of.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2017, Linaro Ltd. All rights reserved.
  3. *
  4. * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/slab.h>
  24. #include "timer-of.h"
  25. static __init void timer_irq_exit(struct of_timer_irq *of_irq)
  26. {
  27. struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
  28. struct clock_event_device *clkevt = &to->clkevt;
  29. of_irq->percpu ? free_percpu_irq(of_irq->irq, clkevt) :
  30. free_irq(of_irq->irq, clkevt);
  31. }
  32. static __init int timer_irq_init(struct device_node *np,
  33. struct of_timer_irq *of_irq)
  34. {
  35. int ret;
  36. struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
  37. struct clock_event_device *clkevt = &to->clkevt;
  38. if (of_irq->name) {
  39. of_irq->irq = ret = of_irq_get_byname(np, of_irq->name);
  40. if (ret < 0) {
  41. pr_err("Failed to get interrupt %s for %s\n",
  42. of_irq->name, np->full_name);
  43. return ret;
  44. }
  45. } else {
  46. of_irq->irq = irq_of_parse_and_map(np, of_irq->index);
  47. }
  48. if (!of_irq->irq) {
  49. pr_err("Failed to map interrupt for %s\n", np->full_name);
  50. return -EINVAL;
  51. }
  52. ret = of_irq->percpu ?
  53. request_percpu_irq(of_irq->irq, of_irq->handler,
  54. np->full_name, clkevt) :
  55. request_irq(of_irq->irq, of_irq->handler,
  56. of_irq->flags ? of_irq->flags : IRQF_TIMER,
  57. np->full_name, clkevt);
  58. if (ret) {
  59. pr_err("Failed to request irq %d for %s\n", of_irq->irq,
  60. np->full_name);
  61. return ret;
  62. }
  63. clkevt->irq = of_irq->irq;
  64. return 0;
  65. }
  66. static __init void timer_clk_exit(struct of_timer_clk *of_clk)
  67. {
  68. of_clk->rate = 0;
  69. clk_disable_unprepare(of_clk->clk);
  70. clk_put(of_clk->clk);
  71. }
  72. static __init int timer_clk_init(struct device_node *np,
  73. struct of_timer_clk *of_clk)
  74. {
  75. int ret;
  76. of_clk->clk = of_clk->name ? of_clk_get_by_name(np, of_clk->name) :
  77. of_clk_get(np, of_clk->index);
  78. if (IS_ERR(of_clk->clk)) {
  79. pr_err("Failed to get clock for %s\n", np->full_name);
  80. return PTR_ERR(of_clk->clk);
  81. }
  82. ret = clk_prepare_enable(of_clk->clk);
  83. if (ret) {
  84. pr_err("Failed for enable clock for %s\n", np->full_name);
  85. goto out_clk_put;
  86. }
  87. of_clk->rate = clk_get_rate(of_clk->clk);
  88. if (!of_clk->rate) {
  89. ret = -EINVAL;
  90. pr_err("Failed to get clock rate for %s\n", np->full_name);
  91. goto out_clk_disable;
  92. }
  93. of_clk->period = DIV_ROUND_UP(of_clk->rate, HZ);
  94. out:
  95. return ret;
  96. out_clk_disable:
  97. clk_disable_unprepare(of_clk->clk);
  98. out_clk_put:
  99. clk_put(of_clk->clk);
  100. goto out;
  101. }
  102. static __init void timer_base_exit(struct of_timer_base *of_base)
  103. {
  104. iounmap(of_base->base);
  105. }
  106. static __init int timer_base_init(struct device_node *np,
  107. struct of_timer_base *of_base)
  108. {
  109. const char *name = of_base->name ? of_base->name : np->full_name;
  110. of_base->base = of_io_request_and_map(np, of_base->index, name);
  111. if (!of_base->base) {
  112. pr_err("Failed to iomap (%s)\n", name);
  113. return -ENXIO;
  114. }
  115. return 0;
  116. }
  117. int __init timer_of_init(struct device_node *np, struct timer_of *to)
  118. {
  119. int ret = -EINVAL;
  120. int flags = 0;
  121. if (to->flags & TIMER_OF_BASE) {
  122. ret = timer_base_init(np, &to->of_base);
  123. if (ret)
  124. goto out_fail;
  125. flags |= TIMER_OF_BASE;
  126. }
  127. if (to->flags & TIMER_OF_CLOCK) {
  128. ret = timer_clk_init(np, &to->of_clk);
  129. if (ret)
  130. goto out_fail;
  131. flags |= TIMER_OF_CLOCK;
  132. }
  133. if (to->flags & TIMER_OF_IRQ) {
  134. ret = timer_irq_init(np, &to->of_irq);
  135. if (ret)
  136. goto out_fail;
  137. flags |= TIMER_OF_IRQ;
  138. }
  139. if (!to->clkevt.name)
  140. to->clkevt.name = np->name;
  141. return ret;
  142. out_fail:
  143. if (flags & TIMER_OF_IRQ)
  144. timer_irq_exit(&to->of_irq);
  145. if (flags & TIMER_OF_CLOCK)
  146. timer_clk_exit(&to->of_clk);
  147. if (flags & TIMER_OF_BASE)
  148. timer_base_exit(&to->of_base);
  149. return ret;
  150. }