Stefan Wahren | 64db8bb | 2018-11-10 16:28:39 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2012 Simon Arlott |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 6 | #include <linux/bitops.h> |
| 7 | #include <linux/clockchips.h> |
| 8 | #include <linux/clocksource.h> |
| 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/irqreturn.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/of_address.h> |
| 14 | #include <linux/of_irq.h> |
| 15 | #include <linux/of_platform.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/string.h> |
Stephen Boyd | 38ff87f | 2013-06-01 23:39:40 -0700 | [diff] [blame] | 18 | #include <linux/sched_clock.h> |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 19 | |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 20 | #include <asm/irq.h> |
| 21 | |
| 22 | #define REG_CONTROL 0x00 |
| 23 | #define REG_COUNTER_LO 0x04 |
| 24 | #define REG_COUNTER_HI 0x08 |
| 25 | #define REG_COMPARE(n) (0x0c + (n) * 4) |
| 26 | #define MAX_TIMER 3 |
| 27 | #define DEFAULT_TIMER 3 |
| 28 | |
| 29 | struct bcm2835_timer { |
| 30 | void __iomem *control; |
| 31 | void __iomem *compare; |
| 32 | int match_mask; |
| 33 | struct clock_event_device evt; |
| 34 | struct irqaction act; |
| 35 | }; |
| 36 | |
| 37 | static void __iomem *system_clock __read_mostly; |
| 38 | |
Stephen Boyd | 18952f2 | 2013-07-18 16:21:20 -0700 | [diff] [blame] | 39 | static u64 notrace bcm2835_sched_read(void) |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 40 | { |
| 41 | return readl_relaxed(system_clock); |
| 42 | } |
| 43 | |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 44 | static int bcm2835_time_set_next_event(unsigned long event, |
| 45 | struct clock_event_device *evt_dev) |
| 46 | { |
| 47 | struct bcm2835_timer *timer = container_of(evt_dev, |
| 48 | struct bcm2835_timer, evt); |
| 49 | writel_relaxed(readl_relaxed(system_clock) + event, |
| 50 | timer->compare); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static irqreturn_t bcm2835_time_interrupt(int irq, void *dev_id) |
| 55 | { |
| 56 | struct bcm2835_timer *timer = dev_id; |
| 57 | void (*event_handler)(struct clock_event_device *); |
| 58 | if (readl_relaxed(timer->control) & timer->match_mask) { |
| 59 | writel_relaxed(timer->match_mask, timer->control); |
| 60 | |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 61 | event_handler = READ_ONCE(timer->evt.event_handler); |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 62 | if (event_handler) |
| 63 | event_handler(&timer->evt); |
| 64 | return IRQ_HANDLED; |
| 65 | } else { |
| 66 | return IRQ_NONE; |
| 67 | } |
| 68 | } |
| 69 | |
Daniel Lezcano | 524a7f0 | 2016-06-02 18:46:11 +0200 | [diff] [blame] | 70 | static int __init bcm2835_timer_init(struct device_node *node) |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 71 | { |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 72 | void __iomem *base; |
| 73 | u32 freq; |
Daniel Lezcano | 524a7f0 | 2016-06-02 18:46:11 +0200 | [diff] [blame] | 74 | int irq, ret; |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 75 | struct bcm2835_timer *timer; |
| 76 | |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 77 | base = of_iomap(node, 0); |
Daniel Lezcano | 524a7f0 | 2016-06-02 18:46:11 +0200 | [diff] [blame] | 78 | if (!base) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 79 | pr_err("Can't remap registers\n"); |
Daniel Lezcano | 524a7f0 | 2016-06-02 18:46:11 +0200 | [diff] [blame] | 80 | return -ENXIO; |
| 81 | } |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 82 | |
Daniel Lezcano | 524a7f0 | 2016-06-02 18:46:11 +0200 | [diff] [blame] | 83 | ret = of_property_read_u32(node, "clock-frequency", &freq); |
| 84 | if (ret) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 85 | pr_err("Can't read clock-frequency\n"); |
Arvind Yadav | 84c39b8 | 2016-09-21 23:03:57 +0530 | [diff] [blame] | 86 | goto err_iounmap; |
Daniel Lezcano | 524a7f0 | 2016-06-02 18:46:11 +0200 | [diff] [blame] | 87 | } |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 88 | |
| 89 | system_clock = base + REG_COUNTER_LO; |
Stephen Boyd | 18952f2 | 2013-07-18 16:21:20 -0700 | [diff] [blame] | 90 | sched_clock_register(bcm2835_sched_read, 32, freq); |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 91 | |
| 92 | clocksource_mmio_init(base + REG_COUNTER_LO, node->name, |
| 93 | freq, 300, 32, clocksource_mmio_readl_up); |
| 94 | |
| 95 | irq = irq_of_parse_and_map(node, DEFAULT_TIMER); |
Daniel Lezcano | 524a7f0 | 2016-06-02 18:46:11 +0200 | [diff] [blame] | 96 | if (irq <= 0) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 97 | pr_err("Can't parse IRQ\n"); |
Arvind Yadav | 84c39b8 | 2016-09-21 23:03:57 +0530 | [diff] [blame] | 98 | ret = -EINVAL; |
| 99 | goto err_iounmap; |
Daniel Lezcano | 524a7f0 | 2016-06-02 18:46:11 +0200 | [diff] [blame] | 100 | } |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 101 | |
| 102 | timer = kzalloc(sizeof(*timer), GFP_KERNEL); |
Daniel Lezcano | 524a7f0 | 2016-06-02 18:46:11 +0200 | [diff] [blame] | 103 | if (!timer) { |
Arvind Yadav | 84c39b8 | 2016-09-21 23:03:57 +0530 | [diff] [blame] | 104 | ret = -ENOMEM; |
| 105 | goto err_iounmap; |
Daniel Lezcano | 524a7f0 | 2016-06-02 18:46:11 +0200 | [diff] [blame] | 106 | } |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 107 | |
| 108 | timer->control = base + REG_CONTROL; |
| 109 | timer->compare = base + REG_COMPARE(DEFAULT_TIMER); |
| 110 | timer->match_mask = BIT(DEFAULT_TIMER); |
| 111 | timer->evt.name = node->name; |
| 112 | timer->evt.rating = 300; |
| 113 | timer->evt.features = CLOCK_EVT_FEAT_ONESHOT; |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 114 | timer->evt.set_next_event = bcm2835_time_set_next_event; |
| 115 | timer->evt.cpumask = cpumask_of(0); |
| 116 | timer->act.name = node->name; |
| 117 | timer->act.flags = IRQF_TIMER | IRQF_SHARED; |
| 118 | timer->act.dev_id = timer; |
| 119 | timer->act.handler = bcm2835_time_interrupt; |
| 120 | |
Daniel Lezcano | 524a7f0 | 2016-06-02 18:46:11 +0200 | [diff] [blame] | 121 | ret = setup_irq(irq, &timer->act); |
| 122 | if (ret) { |
| 123 | pr_err("Can't set up timer IRQ\n"); |
Arvind Yadav | 84c39b8 | 2016-09-21 23:03:57 +0530 | [diff] [blame] | 124 | goto err_iounmap; |
Daniel Lezcano | 524a7f0 | 2016-06-02 18:46:11 +0200 | [diff] [blame] | 125 | } |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 126 | |
| 127 | clockevents_config_and_register(&timer->evt, freq, 0xf, 0xffffffff); |
| 128 | |
| 129 | pr_info("bcm2835: system timer (irq = %d)\n", irq); |
Daniel Lezcano | 524a7f0 | 2016-06-02 18:46:11 +0200 | [diff] [blame] | 130 | |
| 131 | return 0; |
Arvind Yadav | 84c39b8 | 2016-09-21 23:03:57 +0530 | [diff] [blame] | 132 | |
| 133 | err_iounmap: |
| 134 | iounmap(base); |
| 135 | return ret; |
Simon Arlott | ee4af56 | 2012-09-10 22:38:35 -0600 | [diff] [blame] | 136 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 137 | TIMER_OF_DECLARE(bcm2835, "brcm,bcm2835-system-timer", |
Stephen Warren | c1b724f | 2013-01-03 20:23:13 -0700 | [diff] [blame] | 138 | bcm2835_timer_init); |