timer-integrator-ap.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 void clkevt_set_mode(enum clock_event_mode mode, 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. switch (mode) {
  69. case CLOCK_EVT_MODE_PERIODIC:
  70. /* Enable the timer and start the periodic tick */
  71. writel(timer_reload, clkevt_base + TIMER_LOAD);
  72. ctrl |= TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
  73. writel(ctrl, clkevt_base + TIMER_CTRL);
  74. break;
  75. case CLOCK_EVT_MODE_ONESHOT:
  76. /* Leave the timer disabled, .set_next_event will enable it */
  77. ctrl &= ~TIMER_CTRL_PERIODIC;
  78. writel(ctrl, clkevt_base + TIMER_CTRL);
  79. break;
  80. case CLOCK_EVT_MODE_UNUSED:
  81. case CLOCK_EVT_MODE_SHUTDOWN:
  82. case CLOCK_EVT_MODE_RESUME:
  83. default:
  84. /* Just leave in disabled state */
  85. break;
  86. }
  87. }
  88. static int clkevt_set_next_event(unsigned long next, struct clock_event_device *evt)
  89. {
  90. unsigned long ctrl = readl(clkevt_base + TIMER_CTRL);
  91. writel(ctrl & ~TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  92. writel(next, clkevt_base + TIMER_LOAD);
  93. writel(ctrl | TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
  94. return 0;
  95. }
  96. static struct clock_event_device integrator_clockevent = {
  97. .name = "timer1",
  98. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  99. .set_mode = clkevt_set_mode,
  100. .set_next_event = clkevt_set_next_event,
  101. .rating = 300,
  102. };
  103. static struct irqaction integrator_timer_irq = {
  104. .name = "timer",
  105. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  106. .handler = integrator_timer_interrupt,
  107. .dev_id = &integrator_clockevent,
  108. };
  109. static void integrator_clockevent_init(unsigned long inrate,
  110. void __iomem *base, int irq)
  111. {
  112. unsigned long rate = inrate;
  113. unsigned int ctrl = 0;
  114. clkevt_base = base;
  115. /* Calculate and program a divisor */
  116. if (rate > 0x100000 * HZ) {
  117. rate /= 256;
  118. ctrl |= TIMER_CTRL_DIV256;
  119. } else if (rate > 0x10000 * HZ) {
  120. rate /= 16;
  121. ctrl |= TIMER_CTRL_DIV16;
  122. }
  123. timer_reload = rate / HZ;
  124. writel(ctrl, clkevt_base + TIMER_CTRL);
  125. setup_irq(irq, &integrator_timer_irq);
  126. clockevents_config_and_register(&integrator_clockevent,
  127. rate,
  128. 1,
  129. 0xffffU);
  130. }
  131. static void __init integrator_ap_timer_init_of(struct device_node *node)
  132. {
  133. const char *path;
  134. void __iomem *base;
  135. int err;
  136. int irq;
  137. struct clk *clk;
  138. unsigned long rate;
  139. struct device_node *pri_node;
  140. struct device_node *sec_node;
  141. base = of_io_request_and_map(node, 0, "integrator-timer");
  142. if (IS_ERR(base))
  143. return;
  144. clk = of_clk_get(node, 0);
  145. if (IS_ERR(clk)) {
  146. pr_err("No clock for %s\n", node->name);
  147. return;
  148. }
  149. clk_prepare_enable(clk);
  150. rate = clk_get_rate(clk);
  151. writel(0, base + TIMER_CTRL);
  152. err = of_property_read_string(of_aliases,
  153. "arm,timer-primary", &path);
  154. if (WARN_ON(err))
  155. return;
  156. pri_node = of_find_node_by_path(path);
  157. err = of_property_read_string(of_aliases,
  158. "arm,timer-secondary", &path);
  159. if (WARN_ON(err))
  160. return;
  161. sec_node = of_find_node_by_path(path);
  162. if (node == pri_node) {
  163. /* The primary timer lacks IRQ, use as clocksource */
  164. integrator_clocksource_init(rate, base);
  165. return;
  166. }
  167. if (node == sec_node) {
  168. /* The secondary timer will drive the clock event */
  169. irq = irq_of_parse_and_map(node, 0);
  170. integrator_clockevent_init(rate, base, irq);
  171. return;
  172. }
  173. pr_info("Timer @%p unused\n", base);
  174. clk_disable_unprepare(clk);
  175. }
  176. CLOCKSOURCE_OF_DECLARE(integrator_ap_timer, "arm,integrator-timer",
  177. integrator_ap_timer_init_of);