tango_xtal.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <linux/clocksource.h>
  2. #include <linux/sched_clock.h>
  3. #include <linux/of_address.h>
  4. #include <linux/printk.h>
  5. #include <linux/delay.h>
  6. #include <linux/init.h>
  7. #include <linux/clk.h>
  8. static void __iomem *xtal_in_cnt;
  9. static struct delay_timer delay_timer;
  10. static unsigned long notrace read_xtal_counter(void)
  11. {
  12. return readl_relaxed(xtal_in_cnt);
  13. }
  14. static u64 notrace read_sched_clock(void)
  15. {
  16. return read_xtal_counter();
  17. }
  18. static cycle_t read_clocksource(struct clocksource *cs)
  19. {
  20. return read_xtal_counter();
  21. }
  22. static struct clocksource tango_xtal = {
  23. .name = "tango-xtal",
  24. .rating = 350,
  25. .read = read_clocksource,
  26. .mask = CLOCKSOURCE_MASK(32),
  27. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  28. };
  29. static void __init tango_clocksource_init(struct device_node *np)
  30. {
  31. struct clk *clk;
  32. int xtal_freq, ret;
  33. xtal_in_cnt = of_iomap(np, 0);
  34. if (xtal_in_cnt == NULL) {
  35. pr_err("%s: invalid address\n", np->full_name);
  36. return;
  37. }
  38. clk = of_clk_get(np, 0);
  39. if (IS_ERR(clk)) {
  40. pr_err("%s: invalid clock\n", np->full_name);
  41. return;
  42. }
  43. xtal_freq = clk_get_rate(clk);
  44. delay_timer.freq = xtal_freq;
  45. delay_timer.read_current_timer = read_xtal_counter;
  46. ret = clocksource_register_hz(&tango_xtal, xtal_freq);
  47. if (ret != 0) {
  48. pr_err("%s: registration failed\n", np->full_name);
  49. return;
  50. }
  51. sched_clock_register(read_sched_clock, 32, xtal_freq);
  52. register_current_timer_delay(&delay_timer);
  53. }
  54. CLOCKSOURCE_OF_DECLARE(tango, "sigma,tick-counter", tango_clocksource_init);