bcm2835_timer.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2012 Simon Arlott
  4. */
  5. #include <linux/bitops.h>
  6. #include <linux/clockchips.h>
  7. #include <linux/clocksource.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/irqreturn.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <linux/sched_clock.h>
  18. #include <asm/irq.h>
  19. #define REG_CONTROL 0x00
  20. #define REG_COUNTER_LO 0x04
  21. #define REG_COUNTER_HI 0x08
  22. #define REG_COMPARE(n) (0x0c + (n) * 4)
  23. #define MAX_TIMER 3
  24. #define DEFAULT_TIMER 3
  25. struct bcm2835_timer {
  26. void __iomem *control;
  27. void __iomem *compare;
  28. int match_mask;
  29. struct clock_event_device evt;
  30. };
  31. static void __iomem *system_clock __read_mostly;
  32. static u64 notrace bcm2835_sched_read(void)
  33. {
  34. return readl_relaxed(system_clock);
  35. }
  36. static int bcm2835_time_set_next_event(unsigned long event,
  37. struct clock_event_device *evt_dev)
  38. {
  39. struct bcm2835_timer *timer = container_of(evt_dev,
  40. struct bcm2835_timer, evt);
  41. writel_relaxed(readl_relaxed(system_clock) + event,
  42. timer->compare);
  43. return 0;
  44. }
  45. static irqreturn_t bcm2835_time_interrupt(int irq, void *dev_id)
  46. {
  47. struct bcm2835_timer *timer = dev_id;
  48. void (*event_handler)(struct clock_event_device *);
  49. if (readl_relaxed(timer->control) & timer->match_mask) {
  50. writel_relaxed(timer->match_mask, timer->control);
  51. event_handler = READ_ONCE(timer->evt.event_handler);
  52. if (event_handler)
  53. event_handler(&timer->evt);
  54. return IRQ_HANDLED;
  55. } else {
  56. return IRQ_NONE;
  57. }
  58. }
  59. static int __init bcm2835_timer_init(struct device_node *node)
  60. {
  61. void __iomem *base;
  62. u32 freq;
  63. int irq, ret;
  64. struct bcm2835_timer *timer;
  65. base = of_iomap(node, 0);
  66. if (!base) {
  67. pr_err("Can't remap registers\n");
  68. return -ENXIO;
  69. }
  70. ret = of_property_read_u32(node, "clock-frequency", &freq);
  71. if (ret) {
  72. pr_err("Can't read clock-frequency\n");
  73. goto err_iounmap;
  74. }
  75. system_clock = base + REG_COUNTER_LO;
  76. sched_clock_register(bcm2835_sched_read, 32, freq);
  77. clocksource_mmio_init(base + REG_COUNTER_LO, node->name,
  78. freq, 300, 32, clocksource_mmio_readl_up);
  79. irq = irq_of_parse_and_map(node, DEFAULT_TIMER);
  80. if (irq <= 0) {
  81. pr_err("Can't parse IRQ\n");
  82. ret = -EINVAL;
  83. goto err_iounmap;
  84. }
  85. timer = kzalloc(sizeof(*timer), GFP_KERNEL);
  86. if (!timer) {
  87. ret = -ENOMEM;
  88. goto err_iounmap;
  89. }
  90. timer->control = base + REG_CONTROL;
  91. timer->compare = base + REG_COMPARE(DEFAULT_TIMER);
  92. timer->match_mask = BIT(DEFAULT_TIMER);
  93. timer->evt.name = node->name;
  94. timer->evt.rating = 300;
  95. timer->evt.features = CLOCK_EVT_FEAT_ONESHOT;
  96. timer->evt.set_next_event = bcm2835_time_set_next_event;
  97. timer->evt.cpumask = cpumask_of(0);
  98. ret = request_irq(irq, bcm2835_time_interrupt, IRQF_TIMER | IRQF_SHARED,
  99. node->name, timer);
  100. if (ret) {
  101. pr_err("Can't set up timer IRQ\n");
  102. goto err_timer_free;
  103. }
  104. clockevents_config_and_register(&timer->evt, freq, 0xf, 0xffffffff);
  105. pr_info("bcm2835: system timer (irq = %d)\n", irq);
  106. return 0;
  107. err_timer_free:
  108. kfree(timer);
  109. err_iounmap:
  110. iounmap(base);
  111. return ret;
  112. }
  113. TIMER_OF_DECLARE(bcm2835, "brcm,bcm2835-system-timer",
  114. bcm2835_timer_init);