time-orion.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /*
  35. * Free-running clocksource handling.
  36. */
  37. static u64 notrace orion_read_sched_clock(void)
  38. {
  39. return ~readl(timer_base + TIMER0_VAL);
  40. }
  41. /*
  42. * Clockevent handling.
  43. */
  44. static u32 ticks_per_jiffy;
  45. static int orion_clkevt_next_event(unsigned long delta,
  46. struct clock_event_device *dev)
  47. {
  48. /* setup and enable one-shot timer */
  49. writel(delta, timer_base + TIMER1_VAL);
  50. atomic_io_modify(timer_base + TIMER_CTRL,
  51. TIMER1_RELOAD_EN | TIMER1_EN, TIMER1_EN);
  52. return 0;
  53. }
  54. static void orion_clkevt_mode(enum clock_event_mode mode,
  55. struct clock_event_device *dev)
  56. {
  57. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  58. /* setup and enable periodic timer at 1/HZ intervals */
  59. writel(ticks_per_jiffy - 1, timer_base + TIMER1_RELOAD);
  60. writel(ticks_per_jiffy - 1, timer_base + TIMER1_VAL);
  61. atomic_io_modify(timer_base + TIMER_CTRL,
  62. TIMER1_RELOAD_EN | TIMER1_EN,
  63. TIMER1_RELOAD_EN | TIMER1_EN);
  64. } else {
  65. /* disable timer */
  66. atomic_io_modify(timer_base + TIMER_CTRL,
  67. TIMER1_RELOAD_EN | TIMER1_EN, 0);
  68. }
  69. }
  70. static struct clock_event_device orion_clkevt = {
  71. .name = "orion_event",
  72. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  73. .shift = 32,
  74. .rating = 300,
  75. .set_next_event = orion_clkevt_next_event,
  76. .set_mode = orion_clkevt_mode,
  77. };
  78. static irqreturn_t orion_clkevt_irq_handler(int irq, void *dev_id)
  79. {
  80. orion_clkevt.event_handler(&orion_clkevt);
  81. return IRQ_HANDLED;
  82. }
  83. static struct irqaction orion_clkevt_irq = {
  84. .name = "orion_event",
  85. .flags = IRQF_TIMER,
  86. .handler = orion_clkevt_irq_handler,
  87. };
  88. static void __init orion_timer_init(struct device_node *np)
  89. {
  90. struct clk *clk;
  91. int irq;
  92. /* timer registers are shared with watchdog timer */
  93. timer_base = of_iomap(np, 0);
  94. if (!timer_base)
  95. panic("%s: unable to map resource\n", np->name);
  96. clk = of_clk_get(np, 0);
  97. if (IS_ERR(clk))
  98. panic("%s: unable to get clk\n", np->name);
  99. clk_prepare_enable(clk);
  100. /* we are only interested in timer1 irq */
  101. irq = irq_of_parse_and_map(np, 1);
  102. if (irq <= 0)
  103. panic("%s: unable to parse timer1 irq\n", np->name);
  104. /* setup timer0 as free-running clocksource */
  105. writel(~0, timer_base + TIMER0_VAL);
  106. writel(~0, timer_base + TIMER0_RELOAD);
  107. atomic_io_modify(timer_base + TIMER_CTRL,
  108. TIMER0_RELOAD_EN | TIMER0_EN,
  109. TIMER0_RELOAD_EN | TIMER0_EN);
  110. clocksource_mmio_init(timer_base + TIMER0_VAL, "orion_clocksource",
  111. clk_get_rate(clk), 300, 32,
  112. clocksource_mmio_readl_down);
  113. sched_clock_register(orion_read_sched_clock, 32, clk_get_rate(clk));
  114. /* setup timer1 as clockevent timer */
  115. if (setup_irq(irq, &orion_clkevt_irq))
  116. panic("%s: unable to setup irq\n", np->name);
  117. ticks_per_jiffy = (clk_get_rate(clk) + HZ/2) / HZ;
  118. orion_clkevt.cpumask = cpumask_of(0);
  119. orion_clkevt.irq = irq;
  120. clockevents_config_and_register(&orion_clkevt, clk_get_rate(clk),
  121. ORION_ONESHOT_MIN, ORION_ONESHOT_MAX);
  122. }
  123. CLOCKSOURCE_OF_DECLARE(orion_timer, "marvell,orion-timer", orion_timer_init);