dw_apb_timer_of.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2012 Altera Corporation
  3. * Copyright (c) 2011 Picochip Ltd., Jamie Iles
  4. *
  5. * Modified from mach-picoxcell/time.c
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  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, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/dw_apb_timer.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/clk.h>
  25. #include <linux/reset.h>
  26. #include <linux/sched_clock.h>
  27. static void __init timer_get_base_and_rate(struct device_node *np,
  28. void __iomem **base, u32 *rate)
  29. {
  30. struct clk *timer_clk;
  31. struct clk *pclk;
  32. struct reset_control *rstc;
  33. *base = of_iomap(np, 0);
  34. if (!*base)
  35. panic("Unable to map regs for %pOFn", np);
  36. /*
  37. * Reset the timer if the reset control is available, wiping
  38. * out the state the firmware may have left it
  39. */
  40. rstc = of_reset_control_get(np, NULL);
  41. if (!IS_ERR(rstc)) {
  42. reset_control_assert(rstc);
  43. reset_control_deassert(rstc);
  44. }
  45. /*
  46. * Not all implementations use a periphal clock, so don't panic
  47. * if it's not present
  48. */
  49. pclk = of_clk_get_by_name(np, "pclk");
  50. if (!IS_ERR(pclk))
  51. if (clk_prepare_enable(pclk))
  52. pr_warn("pclk for %pOFn is present, but could not be activated\n",
  53. np);
  54. timer_clk = of_clk_get_by_name(np, "timer");
  55. if (IS_ERR(timer_clk))
  56. goto try_clock_freq;
  57. if (!clk_prepare_enable(timer_clk)) {
  58. *rate = clk_get_rate(timer_clk);
  59. return;
  60. }
  61. try_clock_freq:
  62. if (of_property_read_u32(np, "clock-freq", rate) &&
  63. of_property_read_u32(np, "clock-frequency", rate))
  64. panic("No clock nor clock-frequency property for %pOFn", np);
  65. }
  66. static void __init add_clockevent(struct device_node *event_timer)
  67. {
  68. void __iomem *iobase;
  69. struct dw_apb_clock_event_device *ced;
  70. u32 irq, rate;
  71. irq = irq_of_parse_and_map(event_timer, 0);
  72. if (irq == 0)
  73. panic("No IRQ for clock event timer");
  74. timer_get_base_and_rate(event_timer, &iobase, &rate);
  75. ced = dw_apb_clockevent_init(0, event_timer->name, 300, iobase, irq,
  76. rate);
  77. if (!ced)
  78. panic("Unable to initialise clockevent device");
  79. dw_apb_clockevent_register(ced);
  80. }
  81. static void __iomem *sched_io_base;
  82. static u32 sched_rate;
  83. static void __init add_clocksource(struct device_node *source_timer)
  84. {
  85. void __iomem *iobase;
  86. struct dw_apb_clocksource *cs;
  87. u32 rate;
  88. timer_get_base_and_rate(source_timer, &iobase, &rate);
  89. cs = dw_apb_clocksource_init(300, source_timer->name, iobase, rate);
  90. if (!cs)
  91. panic("Unable to initialise clocksource device");
  92. dw_apb_clocksource_start(cs);
  93. dw_apb_clocksource_register(cs);
  94. /*
  95. * Fallback to use the clocksource as sched_clock if no separate
  96. * timer is found. sched_io_base then points to the current_value
  97. * register of the clocksource timer.
  98. */
  99. sched_io_base = iobase + 0x04;
  100. sched_rate = rate;
  101. }
  102. static u64 notrace read_sched_clock(void)
  103. {
  104. return ~readl_relaxed(sched_io_base);
  105. }
  106. static const struct of_device_id sptimer_ids[] __initconst = {
  107. { .compatible = "picochip,pc3x2-rtc" },
  108. { /* Sentinel */ },
  109. };
  110. static void __init init_sched_clock(void)
  111. {
  112. struct device_node *sched_timer;
  113. sched_timer = of_find_matching_node(NULL, sptimer_ids);
  114. if (sched_timer) {
  115. timer_get_base_and_rate(sched_timer, &sched_io_base,
  116. &sched_rate);
  117. of_node_put(sched_timer);
  118. }
  119. sched_clock_register(read_sched_clock, 32, sched_rate);
  120. }
  121. #ifdef CONFIG_ARM
  122. static unsigned long dw_apb_delay_timer_read(void)
  123. {
  124. return ~readl_relaxed(sched_io_base);
  125. }
  126. static struct delay_timer dw_apb_delay_timer = {
  127. .read_current_timer = dw_apb_delay_timer_read,
  128. };
  129. #endif
  130. static int num_called;
  131. static int __init dw_apb_timer_init(struct device_node *timer)
  132. {
  133. switch (num_called) {
  134. case 0:
  135. pr_debug("%s: found clockevent timer\n", __func__);
  136. add_clockevent(timer);
  137. break;
  138. case 1:
  139. pr_debug("%s: found clocksource timer\n", __func__);
  140. add_clocksource(timer);
  141. init_sched_clock();
  142. #ifdef CONFIG_ARM
  143. dw_apb_delay_timer.freq = sched_rate;
  144. register_current_timer_delay(&dw_apb_delay_timer);
  145. #endif
  146. break;
  147. default:
  148. break;
  149. }
  150. num_called++;
  151. return 0;
  152. }
  153. TIMER_OF_DECLARE(pc3x2_timer, "picochip,pc3x2-timer", dw_apb_timer_init);
  154. TIMER_OF_DECLARE(apb_timer_osc, "snps,dw-apb-timer-osc", dw_apb_timer_init);
  155. TIMER_OF_DECLARE(apb_timer_sp, "snps,dw-apb-timer-sp", dw_apb_timer_init);
  156. TIMER_OF_DECLARE(apb_timer, "snps,dw-apb-timer", dw_apb_timer_init);