time-orion.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Marvell Orion SoC timer handling.
  3. *
  4. * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. *
  10. * Timer 0 is used as free-running clocksource, while timer 1 is
  11. * used as clock_event_device.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/bitops.h>
  15. #include <linux/clk.h>
  16. #include <linux/clockchips.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/sched_clock.h>
  22. #define TIMER_CTRL 0x00
  23. #define TIMER0_EN BIT(0)
  24. #define TIMER0_RELOAD_EN BIT(1)
  25. #define TIMER1_EN BIT(2)
  26. #define TIMER1_RELOAD_EN BIT(3)
  27. #define TIMER0_RELOAD 0x10
  28. #define TIMER0_VAL 0x14
  29. #define TIMER1_RELOAD 0x18
  30. #define TIMER1_VAL 0x1c
  31. #define ORION_ONESHOT_MIN 1
  32. #define ORION_ONESHOT_MAX 0xfffffffe
  33. static void __iomem *timer_base;
  34. static DEFINE_SPINLOCK(timer_ctrl_lock);
  35. /*
  36. * Thread-safe access to TIMER_CTRL register
  37. * (shared with watchdog timer)
  38. */
  39. void orion_timer_ctrl_clrset(u32 clr, u32 set)
  40. {
  41. spin_lock(&timer_ctrl_lock);
  42. writel((readl(timer_base + TIMER_CTRL) & ~clr) | set,
  43. timer_base + TIMER_CTRL);
  44. spin_unlock(&timer_ctrl_lock);
  45. }
  46. EXPORT_SYMBOL(orion_timer_ctrl_clrset);
  47. /*
  48. * Free-running clocksource handling.
  49. */
  50. static u32 notrace orion_read_sched_clock(void)
  51. {
  52. return ~readl(timer_base + TIMER0_VAL);
  53. }
  54. /*
  55. * Clockevent handling.
  56. */
  57. static u32 ticks_per_jiffy;
  58. static int orion_clkevt_next_event(unsigned long delta,
  59. struct clock_event_device *dev)
  60. {
  61. /* setup and enable one-shot timer */
  62. writel(delta, timer_base + TIMER1_VAL);
  63. orion_timer_ctrl_clrset(TIMER1_RELOAD_EN, TIMER1_EN);
  64. return 0;
  65. }
  66. static void orion_clkevt_mode(enum clock_event_mode mode,
  67. struct clock_event_device *dev)
  68. {
  69. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  70. /* setup and enable periodic timer at 1/HZ intervals */
  71. writel(ticks_per_jiffy - 1, timer_base + TIMER1_RELOAD);
  72. writel(ticks_per_jiffy - 1, timer_base + TIMER1_VAL);
  73. orion_timer_ctrl_clrset(0, TIMER1_RELOAD_EN | TIMER1_EN);
  74. } else {
  75. /* disable timer */
  76. orion_timer_ctrl_clrset(TIMER1_RELOAD_EN | TIMER1_EN, 0);
  77. }
  78. }
  79. static struct clock_event_device orion_clkevt = {
  80. .name = "orion_event",
  81. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  82. .shift = 32,
  83. .rating = 300,
  84. .set_next_event = orion_clkevt_next_event,
  85. .set_mode = orion_clkevt_mode,
  86. };
  87. static irqreturn_t orion_clkevt_irq_handler(int irq, void *dev_id)
  88. {
  89. orion_clkevt.event_handler(&orion_clkevt);
  90. return IRQ_HANDLED;
  91. }
  92. static struct irqaction orion_clkevt_irq = {
  93. .name = "orion_event",
  94. .flags = IRQF_TIMER,
  95. .handler = orion_clkevt_irq_handler,
  96. };
  97. static void __init orion_timer_init(struct device_node *np)
  98. {
  99. struct clk *clk;
  100. int irq;
  101. /* timer registers are shared with watchdog timer */
  102. timer_base = of_iomap(np, 0);
  103. if (!timer_base)
  104. panic("%s: unable to map resource\n", np->name);
  105. clk = of_clk_get(np, 0);
  106. if (IS_ERR(clk))
  107. panic("%s: unable to get clk\n", np->name);
  108. clk_prepare_enable(clk);
  109. /* we are only interested in timer1 irq */
  110. irq = irq_of_parse_and_map(np, 1);
  111. if (irq <= 0)
  112. panic("%s: unable to parse timer1 irq\n", np->name);
  113. /* setup timer0 as free-running clocksource */
  114. writel(~0, timer_base + TIMER0_VAL);
  115. writel(~0, timer_base + TIMER0_RELOAD);
  116. orion_timer_ctrl_clrset(0, TIMER0_RELOAD_EN | TIMER0_EN);
  117. clocksource_mmio_init(timer_base + TIMER0_VAL, "orion_clocksource",
  118. clk_get_rate(clk), 300, 32,
  119. clocksource_mmio_readl_down);
  120. setup_sched_clock(orion_read_sched_clock, 32, clk_get_rate(clk));
  121. /* setup timer1 as clockevent timer */
  122. if (setup_irq(irq, &orion_clkevt_irq))
  123. panic("%s: unable to setup irq\n", np->name);
  124. ticks_per_jiffy = (clk_get_rate(clk) + HZ/2) / HZ;
  125. orion_clkevt.cpumask = cpumask_of(0);
  126. orion_clkevt.irq = irq;
  127. clockevents_config_and_register(&orion_clkevt, clk_get_rate(clk),
  128. ORION_ONESHOT_MIN, ORION_ONESHOT_MAX);
  129. }
  130. CLOCKSOURCE_OF_DECLARE(orion_timer, "marvell,orion-timer", orion_timer_init);