clps711x-timer.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Cirrus Logic CLPS711X clocksource driver
  3. *
  4. * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/clockchips.h>
  13. #include <linux/clocksource.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/sched_clock.h>
  19. #include <linux/slab.h>
  20. enum {
  21. CLPS711X_CLKSRC_CLOCKSOURCE,
  22. CLPS711X_CLKSRC_CLOCKEVENT,
  23. };
  24. static void __iomem *tcd;
  25. static u64 notrace clps711x_sched_clock_read(void)
  26. {
  27. return ~readw(tcd);
  28. }
  29. static int __init _clps711x_clksrc_init(struct clk *clock, void __iomem *base)
  30. {
  31. unsigned long rate;
  32. if (!base)
  33. return -ENOMEM;
  34. if (IS_ERR(clock))
  35. return PTR_ERR(clock);
  36. rate = clk_get_rate(clock);
  37. tcd = base;
  38. clocksource_mmio_init(tcd, "clps711x-clocksource", rate, 300, 16,
  39. clocksource_mmio_readw_down);
  40. sched_clock_register(clps711x_sched_clock_read, 16, rate);
  41. return 0;
  42. }
  43. static irqreturn_t clps711x_timer_interrupt(int irq, void *dev_id)
  44. {
  45. struct clock_event_device *evt = dev_id;
  46. evt->event_handler(evt);
  47. return IRQ_HANDLED;
  48. }
  49. static void clps711x_clockevent_set_mode(enum clock_event_mode mode,
  50. struct clock_event_device *evt)
  51. {
  52. }
  53. static int __init _clps711x_clkevt_init(struct clk *clock, void __iomem *base,
  54. unsigned int irq)
  55. {
  56. struct clock_event_device *clkevt;
  57. unsigned long rate;
  58. if (!irq)
  59. return -EINVAL;
  60. if (!base)
  61. return -ENOMEM;
  62. if (IS_ERR(clock))
  63. return PTR_ERR(clock);
  64. clkevt = kzalloc(sizeof(*clkevt), GFP_KERNEL);
  65. if (!clkevt)
  66. return -ENOMEM;
  67. rate = clk_get_rate(clock);
  68. /* Set Timer prescaler */
  69. writew(DIV_ROUND_CLOSEST(rate, HZ), base);
  70. clkevt->name = "clps711x-clockevent";
  71. clkevt->rating = 300;
  72. clkevt->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_C3STOP;
  73. clkevt->set_mode = clps711x_clockevent_set_mode;
  74. clkevt->cpumask = cpumask_of(0);
  75. clockevents_config_and_register(clkevt, HZ, 0, 0);
  76. return request_irq(irq, clps711x_timer_interrupt, IRQF_TIMER,
  77. "clps711x-timer", clkevt);
  78. }
  79. void __init clps711x_clksrc_init(void __iomem *tc1_base, void __iomem *tc2_base,
  80. unsigned int irq)
  81. {
  82. struct clk *tc1 = clk_get_sys("clps711x-timer.0", NULL);
  83. struct clk *tc2 = clk_get_sys("clps711x-timer.1", NULL);
  84. BUG_ON(_clps711x_clksrc_init(tc1, tc1_base));
  85. BUG_ON(_clps711x_clkevt_init(tc2, tc2_base, irq));
  86. }
  87. #ifdef CONFIG_CLKSRC_OF
  88. static void __init clps711x_timer_init(struct device_node *np)
  89. {
  90. unsigned int irq = irq_of_parse_and_map(np, 0);
  91. struct clk *clock = of_clk_get(np, 0);
  92. void __iomem *base = of_iomap(np, 0);
  93. switch (of_alias_get_id(np, "timer")) {
  94. case CLPS711X_CLKSRC_CLOCKSOURCE:
  95. BUG_ON(_clps711x_clksrc_init(clock, base));
  96. break;
  97. case CLPS711X_CLKSRC_CLOCKEVENT:
  98. BUG_ON(_clps711x_clkevt_init(clock, base, irq));
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. CLOCKSOURCE_OF_DECLARE(clps711x, "cirrus,clps711x-timer", clps711x_timer_init);
  105. #endif