time.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2011 Picochip Ltd., Jamie Iles
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * All enquiries to support@picochip.com
  9. */
  10. #include <linux/dw_apb_timer.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/sched.h>
  15. #include <asm/mach/time.h>
  16. #include <asm/sched_clock.h>
  17. #include "common.h"
  18. static void timer_get_base_and_rate(struct device_node *np,
  19. void __iomem **base, u32 *rate)
  20. {
  21. *base = of_iomap(np, 0);
  22. if (!*base)
  23. panic("Unable to map regs for %s", np->name);
  24. if (of_property_read_u32(np, "clock-freq", rate))
  25. panic("No clock-freq property for %s", np->name);
  26. }
  27. static void picoxcell_add_clockevent(struct device_node *event_timer)
  28. {
  29. void __iomem *iobase;
  30. struct dw_apb_clock_event_device *ced;
  31. u32 irq, rate;
  32. irq = irq_of_parse_and_map(event_timer, 0);
  33. if (irq == NO_IRQ)
  34. panic("No IRQ for clock event timer");
  35. timer_get_base_and_rate(event_timer, &iobase, &rate);
  36. ced = dw_apb_clockevent_init(0, event_timer->name, 300, iobase, irq,
  37. rate);
  38. if (!ced)
  39. panic("Unable to initialise clockevent device");
  40. dw_apb_clockevent_register(ced);
  41. }
  42. static void picoxcell_add_clocksource(struct device_node *source_timer)
  43. {
  44. void __iomem *iobase;
  45. struct dw_apb_clocksource *cs;
  46. u32 rate;
  47. timer_get_base_and_rate(source_timer, &iobase, &rate);
  48. cs = dw_apb_clocksource_init(300, source_timer->name, iobase, rate);
  49. if (!cs)
  50. panic("Unable to initialise clocksource device");
  51. dw_apb_clocksource_start(cs);
  52. dw_apb_clocksource_register(cs);
  53. }
  54. static DEFINE_CLOCK_DATA(cd);
  55. static void __iomem *sched_io_base;
  56. unsigned long long notrace sched_clock(void)
  57. {
  58. cycle_t cyc = sched_io_base ? __raw_readl(sched_io_base) : 0;
  59. return cyc_to_sched_clock(&cd, cyc, (u32)~0);
  60. }
  61. static void notrace picoxcell_update_sched_clock(void)
  62. {
  63. cycle_t cyc = sched_io_base ? __raw_readl(sched_io_base) : 0;
  64. update_sched_clock(&cd, cyc, (u32)~0);
  65. }
  66. static const struct of_device_id picoxcell_rtc_ids[] __initconst = {
  67. { .compatible = "picochip,pc3x2-rtc" },
  68. { /* Sentinel */ },
  69. };
  70. static void picoxcell_init_sched_clock(void)
  71. {
  72. struct device_node *sched_timer;
  73. u32 rate;
  74. sched_timer = of_find_matching_node(NULL, picoxcell_rtc_ids);
  75. if (!sched_timer)
  76. panic("No RTC for sched clock to use");
  77. timer_get_base_and_rate(sched_timer, &sched_io_base, &rate);
  78. of_node_put(sched_timer);
  79. init_sched_clock(&cd, picoxcell_update_sched_clock, 32, rate);
  80. }
  81. static const struct of_device_id picoxcell_timer_ids[] __initconst = {
  82. { .compatible = "picochip,pc3x2-timer" },
  83. {},
  84. };
  85. static void __init picoxcell_timer_init(void)
  86. {
  87. struct device_node *event_timer, *source_timer;
  88. event_timer = of_find_matching_node(NULL, picoxcell_timer_ids);
  89. if (!event_timer)
  90. panic("No timer for clockevent");
  91. picoxcell_add_clockevent(event_timer);
  92. source_timer = of_find_matching_node(event_timer, picoxcell_timer_ids);
  93. if (!source_timer)
  94. panic("No timer for clocksource");
  95. picoxcell_add_clocksource(source_timer);
  96. of_node_put(source_timer);
  97. picoxcell_init_sched_clock();
  98. }
  99. struct sys_timer picoxcell_timer = {
  100. .init = picoxcell_timer_init,
  101. };