bcm2835_timer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2012 Simon Arlott
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/bcm2835_timer.h>
  19. #include <linux/bitops.h>
  20. #include <linux/clockchips.h>
  21. #include <linux/clocksource.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irqreturn.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/of_address.h>
  27. #include <linux/of_irq.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/slab.h>
  30. #include <linux/string.h>
  31. #include <asm/sched_clock.h>
  32. #include <asm/irq.h>
  33. #define REG_CONTROL 0x00
  34. #define REG_COUNTER_LO 0x04
  35. #define REG_COUNTER_HI 0x08
  36. #define REG_COMPARE(n) (0x0c + (n) * 4)
  37. #define MAX_TIMER 3
  38. #define DEFAULT_TIMER 3
  39. struct bcm2835_timer {
  40. void __iomem *control;
  41. void __iomem *compare;
  42. int match_mask;
  43. struct clock_event_device evt;
  44. struct irqaction act;
  45. };
  46. static void __iomem *system_clock __read_mostly;
  47. static u32 notrace bcm2835_sched_read(void)
  48. {
  49. return readl_relaxed(system_clock);
  50. }
  51. static void bcm2835_time_set_mode(enum clock_event_mode mode,
  52. struct clock_event_device *evt_dev)
  53. {
  54. switch (mode) {
  55. case CLOCK_EVT_MODE_ONESHOT:
  56. case CLOCK_EVT_MODE_UNUSED:
  57. case CLOCK_EVT_MODE_SHUTDOWN:
  58. case CLOCK_EVT_MODE_RESUME:
  59. break;
  60. default:
  61. WARN(1, "%s: unhandled event mode %d\n", __func__, mode);
  62. break;
  63. }
  64. }
  65. static int bcm2835_time_set_next_event(unsigned long event,
  66. struct clock_event_device *evt_dev)
  67. {
  68. struct bcm2835_timer *timer = container_of(evt_dev,
  69. struct bcm2835_timer, evt);
  70. writel_relaxed(readl_relaxed(system_clock) + event,
  71. timer->compare);
  72. return 0;
  73. }
  74. static irqreturn_t bcm2835_time_interrupt(int irq, void *dev_id)
  75. {
  76. struct bcm2835_timer *timer = dev_id;
  77. void (*event_handler)(struct clock_event_device *);
  78. if (readl_relaxed(timer->control) & timer->match_mask) {
  79. writel_relaxed(timer->match_mask, timer->control);
  80. event_handler = ACCESS_ONCE(timer->evt.event_handler);
  81. if (event_handler)
  82. event_handler(&timer->evt);
  83. return IRQ_HANDLED;
  84. } else {
  85. return IRQ_NONE;
  86. }
  87. }
  88. static struct of_device_id bcm2835_time_match[] __initconst = {
  89. { .compatible = "brcm,bcm2835-system-timer" },
  90. {}
  91. };
  92. static void __init bcm2835_time_init(void)
  93. {
  94. struct device_node *node;
  95. void __iomem *base;
  96. u32 freq;
  97. int irq;
  98. struct bcm2835_timer *timer;
  99. node = of_find_matching_node(NULL, bcm2835_time_match);
  100. if (!node)
  101. panic("No bcm2835 timer node");
  102. base = of_iomap(node, 0);
  103. if (!base)
  104. panic("Can't remap registers");
  105. if (of_property_read_u32(node, "clock-frequency", &freq))
  106. panic("Can't read clock-frequency");
  107. system_clock = base + REG_COUNTER_LO;
  108. setup_sched_clock(bcm2835_sched_read, 32, freq);
  109. clocksource_mmio_init(base + REG_COUNTER_LO, node->name,
  110. freq, 300, 32, clocksource_mmio_readl_up);
  111. irq = irq_of_parse_and_map(node, DEFAULT_TIMER);
  112. if (irq <= 0)
  113. panic("Can't parse IRQ");
  114. timer = kzalloc(sizeof(*timer), GFP_KERNEL);
  115. if (!timer)
  116. panic("Can't allocate timer struct\n");
  117. timer->control = base + REG_CONTROL;
  118. timer->compare = base + REG_COMPARE(DEFAULT_TIMER);
  119. timer->match_mask = BIT(DEFAULT_TIMER);
  120. timer->evt.name = node->name;
  121. timer->evt.rating = 300;
  122. timer->evt.features = CLOCK_EVT_FEAT_ONESHOT;
  123. timer->evt.set_mode = bcm2835_time_set_mode;
  124. timer->evt.set_next_event = bcm2835_time_set_next_event;
  125. timer->evt.cpumask = cpumask_of(0);
  126. timer->act.name = node->name;
  127. timer->act.flags = IRQF_TIMER | IRQF_SHARED;
  128. timer->act.dev_id = timer;
  129. timer->act.handler = bcm2835_time_interrupt;
  130. if (setup_irq(irq, &timer->act))
  131. panic("Can't set up timer IRQ\n");
  132. clockevents_config_and_register(&timer->evt, freq, 0xf, 0xffffffff);
  133. pr_info("bcm2835: system timer (irq = %d)\n", irq);
  134. }
  135. struct sys_timer bcm2835_timer = {
  136. .init = bcm2835_time_init,
  137. };