dw_apb_timer_of.c 3.3 KB

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