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