dw_apb_timer_of.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/dw_apb_timer.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/sched_clock.h>
  24. static void timer_get_base_and_rate(struct device_node *np,
  25. void __iomem **base, u32 *rate)
  26. {
  27. *base = of_iomap(np, 0);
  28. if (!*base)
  29. panic("Unable to map regs for %s", np->name);
  30. if (of_property_read_u32(np, "clock-freq", rate) &&
  31. of_property_read_u32(np, "clock-frequency", rate))
  32. panic("No clock-frequency property for %s", np->name);
  33. }
  34. static void add_clockevent(struct device_node *event_timer)
  35. {
  36. void __iomem *iobase;
  37. struct dw_apb_clock_event_device *ced;
  38. u32 irq, rate;
  39. irq = irq_of_parse_and_map(event_timer, 0);
  40. if (irq == 0)
  41. panic("No IRQ for clock event timer");
  42. timer_get_base_and_rate(event_timer, &iobase, &rate);
  43. ced = dw_apb_clockevent_init(0, event_timer->name, 300, iobase, irq,
  44. rate);
  45. if (!ced)
  46. panic("Unable to initialise clockevent device");
  47. dw_apb_clockevent_register(ced);
  48. }
  49. static void add_clocksource(struct device_node *source_timer)
  50. {
  51. void __iomem *iobase;
  52. struct dw_apb_clocksource *cs;
  53. u32 rate;
  54. timer_get_base_and_rate(source_timer, &iobase, &rate);
  55. cs = dw_apb_clocksource_init(300, source_timer->name, iobase, rate);
  56. if (!cs)
  57. panic("Unable to initialise clocksource device");
  58. dw_apb_clocksource_start(cs);
  59. dw_apb_clocksource_register(cs);
  60. }
  61. static void __iomem *sched_io_base;
  62. static u32 read_sched_clock(void)
  63. {
  64. return __raw_readl(sched_io_base);
  65. }
  66. static const struct of_device_id sptimer_ids[] __initconst = {
  67. { .compatible = "picochip,pc3x2-rtc" },
  68. { .compatible = "snps,dw-apb-timer-sp" },
  69. { /* Sentinel */ },
  70. };
  71. static void init_sched_clock(void)
  72. {
  73. struct device_node *sched_timer;
  74. u32 rate;
  75. sched_timer = of_find_matching_node(NULL, sptimer_ids);
  76. if (!sched_timer)
  77. panic("No RTC for sched clock to use");
  78. timer_get_base_and_rate(sched_timer, &sched_io_base, &rate);
  79. of_node_put(sched_timer);
  80. setup_sched_clock(read_sched_clock, 32, rate);
  81. }
  82. static const struct of_device_id osctimer_ids[] __initconst = {
  83. { .compatible = "picochip,pc3x2-timer" },
  84. { .compatible = "snps,dw-apb-timer-osc" },
  85. {},
  86. };
  87. void __init dw_apb_timer_init(void)
  88. {
  89. struct device_node *event_timer, *source_timer;
  90. event_timer = of_find_matching_node(NULL, osctimer_ids);
  91. if (!event_timer)
  92. panic("No timer for clockevent");
  93. add_clockevent(event_timer);
  94. source_timer = of_find_matching_node(event_timer, osctimer_ids);
  95. if (!source_timer)
  96. panic("No timer for clocksource");
  97. add_clocksource(source_timer);
  98. of_node_put(source_timer);
  99. init_sched_clock();
  100. }