timer-integrator-ap.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Integrator/AP timer driver
  3. * Copyright (C) 2000-2003 Deep Blue Solutions Ltd
  4. * Copyright (c) 2014, Linaro Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/clocksource.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/clockchips.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/sched_clock.h>
  28. #include "timer-sp.h"
  29. static void __iomem * sched_clk_base;
  30. static u64 notrace integrator_read_sched_clock(void)
  31. {
  32. return -readl(sched_clk_base + TIMER_VALUE);
  33. }
  34. static int integrator_clocksource_init(unsigned long inrate,
  35. void __iomem *base)
  36. {
  37. u32 ctrl = TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC;
  38. unsigned long rate = inrate;
  39. int ret;
  40. if (rate >= 1500000) {
  41. rate /= 16;
  42. ctrl |= TIMER_CTRL_DIV16;
  43. }
  44. writel(0xffff, base + TIMER_LOAD);
  45. writel(ctrl, base + TIMER_CTRL);
  46. ret = clocksource_mmio_init(base + TIMER_VALUE, "timer2",
  47. rate, 200, 16, clocksource_mmio_readl_down);
  48. if (ret)
  49. return ret;
  50. sched_clk_base = base;
  51. sched_clock_register(integrator_read_sched_clock, 16, rate);
  52. return 0;
  53. }
  54. static unsigned long timer_reload;
  55. static void __iomem * clkevt_base;
  56. /*
  57. * IRQ handler for the timer
  58. */
  59. static irqreturn_t integrator_timer_interrupt(int irq, void *dev_id)
  60. {
  61. struct clock_event_device *evt = dev_id;
  62. /* clear the interrupt */
  63. writel(1, clkevt_base + TIMER_INTCLR);
  64. evt->event_handler(evt);
  65. return IRQ_HANDLED;
  66. }
  67. static int clkevt_shutdown(struct clock_event_device *evt)
  68. {
  69. u32 ctrl = readl(clkevt_base + TIMER_CTRL) & ~TIMER_CTRL_ENABLE;
  70. /* Disable timer */
  71. writel(ctrl, clkevt_base + TIMER_CTRL);
  72. return 0;
  73. }
  74. static int clkevt_set_oneshot(struct clock_event_device *evt)
  75. {
  76. u32 ctrl = readl(clkevt_base + TIMER_CTRL) &
  77. ~(TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC);
  78. /* Leave the timer disabled, .set_next_event will enable it */
  79. writel(ctrl, clkevt_base + TIMER_CTRL);
  80. return 0;
  81. }
  82. static int clkevt_set_periodic(struct clock_event_device *evt)
  83. {
  84. u32 ctrl = readl(clkevt_base + TIMER_CTRL) & ~TIMER_CTRL_ENABLE;
  85. /* Disable timer */
  86. writel(ctrl, clkevt_base + TIMER_CTRL);
  87. /* Enable the timer and start the periodic tick */
  88. writel(timer_reload, clkevt_base + TIMER_LOAD);
  89. ctrl |= TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  90. writel(ctrl, clkevt_base + TIMER_CTRL);
  91. return 0;
  92. }
  93. static int clkevt_set_next_event(unsigned long next, struct clock_event_device *evt)
  94. {
  95. unsigned long ctrl = readl(clkevt_base + TIMER_CTRL);
  96. writel(ctrl & ~TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  97. writel(next, clkevt_base + TIMER_LOAD);
  98. writel(ctrl | TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  99. return 0;
  100. }
  101. static struct clock_event_device integrator_clockevent = {
  102. .name = "timer1",
  103. .features = CLOCK_EVT_FEAT_PERIODIC |
  104. CLOCK_EVT_FEAT_ONESHOT,
  105. .set_state_shutdown = clkevt_shutdown,
  106. .set_state_periodic = clkevt_set_periodic,
  107. .set_state_oneshot = clkevt_set_oneshot,
  108. .tick_resume = clkevt_shutdown,
  109. .set_next_event = clkevt_set_next_event,
  110. .rating = 300,
  111. };
  112. static struct irqaction integrator_timer_irq = {
  113. .name = "timer",
  114. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  115. .handler = integrator_timer_interrupt,
  116. .dev_id = &integrator_clockevent,
  117. };
  118. static int integrator_clockevent_init(unsigned long inrate,
  119. void __iomem *base, int irq)
  120. {
  121. unsigned long rate = inrate;
  122. unsigned int ctrl = 0;
  123. int ret;
  124. clkevt_base = base;
  125. /* Calculate and program a divisor */
  126. if (rate > 0x100000 * HZ) {
  127. rate /= 256;
  128. ctrl |= TIMER_CTRL_DIV256;
  129. } else if (rate > 0x10000 * HZ) {
  130. rate /= 16;
  131. ctrl |= TIMER_CTRL_DIV16;
  132. }
  133. timer_reload = rate / HZ;
  134. writel(ctrl, clkevt_base + TIMER_CTRL);
  135. ret = setup_irq(irq, &integrator_timer_irq);
  136. if (ret)
  137. return ret;
  138. clockevents_config_and_register(&integrator_clockevent,
  139. rate,
  140. 1,
  141. 0xffffU);
  142. return 0;
  143. }
  144. static int __init integrator_ap_timer_init_of(struct device_node *node)
  145. {
  146. const char *path;
  147. void __iomem *base;
  148. int err;
  149. int irq;
  150. struct clk *clk;
  151. unsigned long rate;
  152. struct device_node *pri_node;
  153. struct device_node *sec_node;
  154. base = of_io_request_and_map(node, 0, "integrator-timer");
  155. if (IS_ERR(base))
  156. return PTR_ERR(base);
  157. clk = of_clk_get(node, 0);
  158. if (IS_ERR(clk)) {
  159. pr_err("No clock for %s\n", node->name);
  160. return PTR_ERR(clk);
  161. }
  162. clk_prepare_enable(clk);
  163. rate = clk_get_rate(clk);
  164. writel(0, base + TIMER_CTRL);
  165. err = of_property_read_string(of_aliases,
  166. "arm,timer-primary", &path);
  167. if (err) {
  168. pr_warn("Failed to read property");
  169. return err;
  170. }
  171. pri_node = of_find_node_by_path(path);
  172. err = of_property_read_string(of_aliases,
  173. "arm,timer-secondary", &path);
  174. if (err) {
  175. pr_warn("Failed to read property");
  176. return err;
  177. }
  178. sec_node = of_find_node_by_path(path);
  179. if (node == pri_node)
  180. /* The primary timer lacks IRQ, use as clocksource */
  181. return integrator_clocksource_init(rate, base);
  182. if (node == sec_node) {
  183. /* The secondary timer will drive the clock event */
  184. irq = irq_of_parse_and_map(node, 0);
  185. return integrator_clockevent_init(rate, base, irq);
  186. }
  187. pr_info("Timer @%p unused\n", base);
  188. clk_disable_unprepare(clk);
  189. return 0;
  190. }
  191. CLOCKSOURCE_OF_DECLARE(integrator_ap_timer, "arm,integrator-timer",
  192. integrator_ap_timer_init_of);