sun4i_timer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Allwinner A1X SoCs timer handling.
  3. *
  4. * Copyright (C) 2012 Maxime Ripard
  5. *
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * Based on code from
  9. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  10. * Benn Huang <benn@allwinnertech.com>
  11. *
  12. * This file is licensed under the terms of the GNU General Public
  13. * License version 2. This program is licensed "as is" without any
  14. * warranty of any kind, whether express or implied.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clockchips.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/irq.h>
  20. #include <linux/irqreturn.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_irq.h>
  24. #define TIMER_IRQ_EN_REG 0x00
  25. #define TIMER_IRQ_EN(val) (1 << val)
  26. #define TIMER_IRQ_ST_REG 0x04
  27. #define TIMER_CTL_REG(val) (0x10 * val + 0x10)
  28. #define TIMER_CTL_ENABLE (1 << 0)
  29. #define TIMER_CTL_AUTORELOAD (1 << 1)
  30. #define TIMER_CTL_ONESHOT (1 << 7)
  31. #define TIMER_INTVAL_REG(val) (0x10 * val + 0x14)
  32. #define TIMER_CNTVAL_REG(val) (0x10 * val + 0x18)
  33. #define TIMER_SCAL 16
  34. static void __iomem *timer_base;
  35. static void sun4i_clkevt_mode(enum clock_event_mode mode,
  36. struct clock_event_device *clk)
  37. {
  38. u32 u = readl(timer_base + TIMER_CTL_REG(0));
  39. switch (mode) {
  40. case CLOCK_EVT_MODE_PERIODIC:
  41. u &= ~(TIMER_CTL_ONESHOT);
  42. writel(u | TIMER_CTL_ENABLE, timer_base + TIMER_CTL_REG(0));
  43. break;
  44. case CLOCK_EVT_MODE_ONESHOT:
  45. writel(u | TIMER_CTL_ONESHOT, timer_base + TIMER_CTL_REG(0));
  46. break;
  47. case CLOCK_EVT_MODE_UNUSED:
  48. case CLOCK_EVT_MODE_SHUTDOWN:
  49. default:
  50. writel(u & ~(TIMER_CTL_ENABLE), timer_base + TIMER_CTL_REG(0));
  51. break;
  52. }
  53. }
  54. static int sun4i_clkevt_next_event(unsigned long evt,
  55. struct clock_event_device *unused)
  56. {
  57. u32 u = readl(timer_base + TIMER_CTL_REG(0));
  58. writel(evt, timer_base + TIMER_CNTVAL_REG(0));
  59. writel(u | TIMER_CTL_ENABLE | TIMER_CTL_AUTORELOAD,
  60. timer_base + TIMER_CTL_REG(0));
  61. return 0;
  62. }
  63. static struct clock_event_device sun4i_clockevent = {
  64. .name = "sun4i_tick",
  65. .rating = 300,
  66. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  67. .set_mode = sun4i_clkevt_mode,
  68. .set_next_event = sun4i_clkevt_next_event,
  69. };
  70. static irqreturn_t sun4i_timer_interrupt(int irq, void *dev_id)
  71. {
  72. struct clock_event_device *evt = (struct clock_event_device *)dev_id;
  73. writel(0x1, timer_base + TIMER_IRQ_ST_REG);
  74. evt->event_handler(evt);
  75. return IRQ_HANDLED;
  76. }
  77. static struct irqaction sun4i_timer_irq = {
  78. .name = "sun4i_timer0",
  79. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  80. .handler = sun4i_timer_interrupt,
  81. .dev_id = &sun4i_clockevent,
  82. };
  83. static void __init sun4i_timer_init(struct device_node *node)
  84. {
  85. unsigned long rate = 0;
  86. struct clk *clk;
  87. int ret, irq;
  88. u32 val;
  89. timer_base = of_iomap(node, 0);
  90. if (!timer_base)
  91. panic("Can't map registers");
  92. irq = irq_of_parse_and_map(node, 0);
  93. if (irq <= 0)
  94. panic("Can't parse IRQ");
  95. clk = of_clk_get(node, 0);
  96. if (IS_ERR(clk))
  97. panic("Can't get timer clock");
  98. rate = clk_get_rate(clk);
  99. writel(rate / (TIMER_SCAL * HZ),
  100. timer_base + TIMER_INTVAL_REG(0));
  101. /* set clock source to HOSC, 16 pre-division */
  102. val = readl(timer_base + TIMER_CTL_REG(0));
  103. val &= ~(0x07 << 4);
  104. val &= ~(0x03 << 2);
  105. val |= (4 << 4) | (1 << 2);
  106. writel(val, timer_base + TIMER_CTL_REG(0));
  107. /* set mode to auto reload */
  108. val = readl(timer_base + TIMER_CTL_REG(0));
  109. writel(val | TIMER_CTL_AUTORELOAD, timer_base + TIMER_CTL_REG(0));
  110. ret = setup_irq(irq, &sun4i_timer_irq);
  111. if (ret)
  112. pr_warn("failed to setup irq %d\n", irq);
  113. /* Enable timer0 interrupt */
  114. val = readl(timer_base + TIMER_IRQ_EN_REG);
  115. writel(val | TIMER_IRQ_EN(0), timer_base + TIMER_IRQ_EN_REG);
  116. sun4i_clockevent.cpumask = cpumask_of(0);
  117. clockevents_config_and_register(&sun4i_clockevent, rate / TIMER_SCAL,
  118. 0x1, 0xff);
  119. }
  120. CLOCKSOURCE_OF_DECLARE(sun4i, "allwinner,sun4i-timer",
  121. sun4i_timer_init);