timer-vt8500.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * arch/arm/mach-vt8500/timer.c
  4. *
  5. * Copyright (C) 2012 Tony Prisk <[email protected]>
  6. * Copyright (C) 2010 Alexey Charkov <[email protected]>
  7. */
  8. /*
  9. * This file is copied and modified from the original timer.c provided by
  10. * Alexey Charkov. Minor changes have been made for Device Tree Support.
  11. */
  12. #include <linux/io.h>
  13. #include <linux/irq.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/clocksource.h>
  16. #include <linux/clockchips.h>
  17. #include <linux/delay.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #define VT8500_TIMER_OFFSET 0x0100
  22. #define VT8500_TIMER_HZ 3000000
  23. #define TIMER_MATCH_VAL 0x0000
  24. #define TIMER_COUNT_VAL 0x0010
  25. #define TIMER_STATUS_VAL 0x0014
  26. #define TIMER_IER_VAL 0x001c /* interrupt enable */
  27. #define TIMER_CTRL_VAL 0x0020
  28. #define TIMER_AS_VAL 0x0024 /* access status */
  29. #define TIMER_COUNT_R_ACTIVE (1 << 5) /* not ready for read */
  30. #define TIMER_COUNT_W_ACTIVE (1 << 4) /* not ready for write */
  31. #define TIMER_MATCH_W_ACTIVE (1 << 0) /* not ready for write */
  32. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  33. #define MIN_OSCR_DELTA 16
  34. static void __iomem *regbase;
  35. static u64 vt8500_timer_read(struct clocksource *cs)
  36. {
  37. int loops = msecs_to_loops(10);
  38. writel(3, regbase + TIMER_CTRL_VAL);
  39. while ((readl((regbase + TIMER_AS_VAL)) & TIMER_COUNT_R_ACTIVE)
  40. && --loops)
  41. cpu_relax();
  42. return readl(regbase + TIMER_COUNT_VAL);
  43. }
  44. static struct clocksource clocksource = {
  45. .name = "vt8500_timer",
  46. .rating = 200,
  47. .read = vt8500_timer_read,
  48. .mask = CLOCKSOURCE_MASK(32),
  49. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  50. };
  51. static int vt8500_timer_set_next_event(unsigned long cycles,
  52. struct clock_event_device *evt)
  53. {
  54. int loops = msecs_to_loops(10);
  55. u64 alarm = clocksource.read(&clocksource) + cycles;
  56. while ((readl(regbase + TIMER_AS_VAL) & TIMER_MATCH_W_ACTIVE)
  57. && --loops)
  58. cpu_relax();
  59. writel((unsigned long)alarm, regbase + TIMER_MATCH_VAL);
  60. if ((signed)(alarm - clocksource.read(&clocksource)) <= MIN_OSCR_DELTA)
  61. return -ETIME;
  62. writel(1, regbase + TIMER_IER_VAL);
  63. return 0;
  64. }
  65. static int vt8500_shutdown(struct clock_event_device *evt)
  66. {
  67. writel(readl(regbase + TIMER_CTRL_VAL) | 1, regbase + TIMER_CTRL_VAL);
  68. writel(0, regbase + TIMER_IER_VAL);
  69. return 0;
  70. }
  71. static struct clock_event_device clockevent = {
  72. .name = "vt8500_timer",
  73. .features = CLOCK_EVT_FEAT_ONESHOT,
  74. .rating = 200,
  75. .set_next_event = vt8500_timer_set_next_event,
  76. .set_state_shutdown = vt8500_shutdown,
  77. .set_state_oneshot = vt8500_shutdown,
  78. };
  79. static irqreturn_t vt8500_timer_interrupt(int irq, void *dev_id)
  80. {
  81. struct clock_event_device *evt = dev_id;
  82. writel(0xf, regbase + TIMER_STATUS_VAL);
  83. evt->event_handler(evt);
  84. return IRQ_HANDLED;
  85. }
  86. static int __init vt8500_timer_init(struct device_node *np)
  87. {
  88. int timer_irq, ret;
  89. regbase = of_iomap(np, 0);
  90. if (!regbase) {
  91. pr_err("%s: Missing iobase description in Device Tree\n",
  92. __func__);
  93. return -ENXIO;
  94. }
  95. timer_irq = irq_of_parse_and_map(np, 0);
  96. if (!timer_irq) {
  97. pr_err("%s: Missing irq description in Device Tree\n",
  98. __func__);
  99. return -EINVAL;
  100. }
  101. writel(1, regbase + TIMER_CTRL_VAL);
  102. writel(0xf, regbase + TIMER_STATUS_VAL);
  103. writel(~0, regbase + TIMER_MATCH_VAL);
  104. ret = clocksource_register_hz(&clocksource, VT8500_TIMER_HZ);
  105. if (ret) {
  106. pr_err("%s: clocksource_register failed for %s\n",
  107. __func__, clocksource.name);
  108. return ret;
  109. }
  110. clockevent.cpumask = cpumask_of(0);
  111. ret = request_irq(timer_irq, vt8500_timer_interrupt,
  112. IRQF_TIMER | IRQF_IRQPOLL, "vt8500_timer",
  113. &clockevent);
  114. if (ret) {
  115. pr_err("%s: setup_irq failed for %s\n", __func__,
  116. clockevent.name);
  117. return ret;
  118. }
  119. clockevents_config_and_register(&clockevent, VT8500_TIMER_HZ,
  120. MIN_OSCR_DELTA * 2, 0xf0000000);
  121. return 0;
  122. }
  123. TIMER_OF_DECLARE(vt8500, "via,vt8500-timer", vt8500_timer_init);