timer-integrator-ap.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 void 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. if (rate >= 1500000) {
  40. rate /= 16;
  41. ctrl |= TIMER_CTRL_DIV16;
  42. }
  43. writel(0xffff, base + TIMER_LOAD);
  44. writel(ctrl, base + TIMER_CTRL);
  45. clocksource_mmio_init(base + TIMER_VALUE, "timer2",
  46. rate, 200, 16, clocksource_mmio_readl_down);
  47. sched_clk_base = base;
  48. sched_clock_register(integrator_read_sched_clock, 16, rate);
  49. }
  50. static unsigned long timer_reload;
  51. static void __iomem * clkevt_base;
  52. /*
  53. * IRQ handler for the timer
  54. */
  55. static irqreturn_t integrator_timer_interrupt(int irq, void *dev_id)
  56. {
  57. struct clock_event_device *evt = dev_id;
  58. /* clear the interrupt */
  59. writel(1, clkevt_base + TIMER_INTCLR);
  60. evt->event_handler(evt);
  61. return IRQ_HANDLED;
  62. }
  63. static int clkevt_shutdown(struct clock_event_device *evt)
  64. {
  65. u32 ctrl = readl(clkevt_base + TIMER_CTRL) & ~TIMER_CTRL_ENABLE;
  66. /* Disable timer */
  67. writel(ctrl, clkevt_base + TIMER_CTRL);
  68. return 0;
  69. }
  70. static int clkevt_set_oneshot(struct clock_event_device *evt)
  71. {
  72. u32 ctrl = readl(clkevt_base + TIMER_CTRL) &
  73. ~(TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC);
  74. /* Leave the timer disabled, .set_next_event will enable it */
  75. writel(ctrl, clkevt_base + TIMER_CTRL);
  76. return 0;
  77. }
  78. static int clkevt_set_periodic(struct clock_event_device *evt)
  79. {
  80. u32 ctrl = readl(clkevt_base + TIMER_CTRL) & ~TIMER_CTRL_ENABLE;
  81. /* Disable timer */
  82. writel(ctrl, clkevt_base + TIMER_CTRL);
  83. /* Enable the timer and start the periodic tick */
  84. writel(timer_reload, clkevt_base + TIMER_LOAD);
  85. ctrl |= TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  86. writel(ctrl, clkevt_base + TIMER_CTRL);
  87. return 0;
  88. }
  89. static int clkevt_set_next_event(unsigned long next, struct clock_event_device *evt)
  90. {
  91. unsigned long ctrl = readl(clkevt_base + TIMER_CTRL);
  92. writel(ctrl & ~TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  93. writel(next, clkevt_base + TIMER_LOAD);
  94. writel(ctrl | TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  95. return 0;
  96. }
  97. static struct clock_event_device integrator_clockevent = {
  98. .name = "timer1",
  99. .features = CLOCK_EVT_FEAT_PERIODIC |
  100. CLOCK_EVT_FEAT_ONESHOT,
  101. .set_state_shutdown = clkevt_shutdown,
  102. .set_state_periodic = clkevt_set_periodic,
  103. .set_state_oneshot = clkevt_set_oneshot,
  104. .tick_resume = clkevt_shutdown,
  105. .set_next_event = clkevt_set_next_event,
  106. .rating = 300,
  107. };
  108. static struct irqaction integrator_timer_irq = {
  109. .name = "timer",
  110. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  111. .handler = integrator_timer_interrupt,
  112. .dev_id = &integrator_clockevent,
  113. };
  114. static void integrator_clockevent_init(unsigned long inrate,
  115. void __iomem *base, int irq)
  116. {
  117. unsigned long rate = inrate;
  118. unsigned int ctrl = 0;
  119. clkevt_base = base;
  120. /* Calculate and program a divisor */
  121. if (rate > 0x100000 * HZ) {
  122. rate /= 256;
  123. ctrl |= TIMER_CTRL_DIV256;
  124. } else if (rate > 0x10000 * HZ) {
  125. rate /= 16;
  126. ctrl |= TIMER_CTRL_DIV16;
  127. }
  128. timer_reload = rate / HZ;
  129. writel(ctrl, clkevt_base + TIMER_CTRL);
  130. setup_irq(irq, &integrator_timer_irq);
  131. clockevents_config_and_register(&integrator_clockevent,
  132. rate,
  133. 1,
  134. 0xffffU);
  135. }
  136. static void __init integrator_ap_timer_init_of(struct device_node *node)
  137. {
  138. const char *path;
  139. void __iomem *base;
  140. int err;
  141. int irq;
  142. struct clk *clk;
  143. unsigned long rate;
  144. struct device_node *pri_node;
  145. struct device_node *sec_node;
  146. base = of_io_request_and_map(node, 0, "integrator-timer");
  147. if (IS_ERR(base))
  148. return;
  149. clk = of_clk_get(node, 0);
  150. if (IS_ERR(clk)) {
  151. pr_err("No clock for %s\n", node->name);
  152. return;
  153. }
  154. clk_prepare_enable(clk);
  155. rate = clk_get_rate(clk);
  156. writel(0, base + TIMER_CTRL);
  157. err = of_property_read_string(of_aliases,
  158. "arm,timer-primary", &path);
  159. if (WARN_ON(err))
  160. return;
  161. pri_node = of_find_node_by_path(path);
  162. err = of_property_read_string(of_aliases,
  163. "arm,timer-secondary", &path);
  164. if (WARN_ON(err))
  165. return;
  166. sec_node = of_find_node_by_path(path);
  167. if (node == pri_node) {
  168. /* The primary timer lacks IRQ, use as clocksource */
  169. integrator_clocksource_init(rate, base);
  170. return;
  171. }
  172. if (node == sec_node) {
  173. /* The secondary timer will drive the clock event */
  174. irq = irq_of_parse_and_map(node, 0);
  175. integrator_clockevent_init(rate, base, irq);
  176. return;
  177. }
  178. pr_info("Timer @%p unused\n", base);
  179. clk_disable_unprepare(clk);
  180. }
  181. CLOCKSOURCE_OF_DECLARE(integrator_ap_timer, "arm,integrator-timer",
  182. integrator_ap_timer_init_of);