1ee4af569SSimon Arlott /* 2ee4af569SSimon Arlott * Copyright 2012 Simon Arlott 3ee4af569SSimon Arlott * 4ee4af569SSimon Arlott * This program is free software; you can redistribute it and/or modify 5ee4af569SSimon Arlott * it under the terms of the GNU General Public License as published by 6ee4af569SSimon Arlott * the Free Software Foundation; either version 2 of the License, or 7ee4af569SSimon Arlott * (at your option) any later version. 8ee4af569SSimon Arlott * 9ee4af569SSimon Arlott * This program is distributed in the hope that it will be useful, 10ee4af569SSimon Arlott * but WITHOUT ANY WARRANTY; without even the implied warranty of 11ee4af569SSimon Arlott * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12ee4af569SSimon Arlott * GNU General Public License for more details. 13ee4af569SSimon Arlott * 14ee4af569SSimon Arlott * You should have received a copy of the GNU General Public License 15ee4af569SSimon Arlott * along with this program; if not, write to the Free Software 16ee4af569SSimon Arlott * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17ee4af569SSimon Arlott */ 18ee4af569SSimon Arlott 19ee4af569SSimon Arlott #include <linux/bitops.h> 20ee4af569SSimon Arlott #include <linux/clockchips.h> 21ee4af569SSimon Arlott #include <linux/clocksource.h> 22ee4af569SSimon Arlott #include <linux/interrupt.h> 23ee4af569SSimon Arlott #include <linux/irqreturn.h> 24ee4af569SSimon Arlott #include <linux/kernel.h> 25ee4af569SSimon Arlott #include <linux/module.h> 26ee4af569SSimon Arlott #include <linux/of_address.h> 27ee4af569SSimon Arlott #include <linux/of_irq.h> 28ee4af569SSimon Arlott #include <linux/of_platform.h> 29ee4af569SSimon Arlott #include <linux/slab.h> 30ee4af569SSimon Arlott #include <linux/string.h> 3138ff87f7SStephen Boyd #include <linux/sched_clock.h> 32ee4af569SSimon Arlott 33ee4af569SSimon Arlott #include <asm/irq.h> 34ee4af569SSimon Arlott 35ee4af569SSimon Arlott #define REG_CONTROL 0x00 36ee4af569SSimon Arlott #define REG_COUNTER_LO 0x04 37ee4af569SSimon Arlott #define REG_COUNTER_HI 0x08 38ee4af569SSimon Arlott #define REG_COMPARE(n) (0x0c + (n) * 4) 39ee4af569SSimon Arlott #define MAX_TIMER 3 40ee4af569SSimon Arlott #define DEFAULT_TIMER 3 41ee4af569SSimon Arlott 42ee4af569SSimon Arlott struct bcm2835_timer { 43ee4af569SSimon Arlott void __iomem *control; 44ee4af569SSimon Arlott void __iomem *compare; 45ee4af569SSimon Arlott int match_mask; 46ee4af569SSimon Arlott struct clock_event_device evt; 47ee4af569SSimon Arlott struct irqaction act; 48ee4af569SSimon Arlott }; 49ee4af569SSimon Arlott 50ee4af569SSimon Arlott static void __iomem *system_clock __read_mostly; 51ee4af569SSimon Arlott 5218952f20SStephen Boyd static u64 notrace bcm2835_sched_read(void) 53ee4af569SSimon Arlott { 54ee4af569SSimon Arlott return readl_relaxed(system_clock); 55ee4af569SSimon Arlott } 56ee4af569SSimon Arlott 57ee4af569SSimon Arlott static int bcm2835_time_set_next_event(unsigned long event, 58ee4af569SSimon Arlott struct clock_event_device *evt_dev) 59ee4af569SSimon Arlott { 60ee4af569SSimon Arlott struct bcm2835_timer *timer = container_of(evt_dev, 61ee4af569SSimon Arlott struct bcm2835_timer, evt); 62ee4af569SSimon Arlott writel_relaxed(readl_relaxed(system_clock) + event, 63ee4af569SSimon Arlott timer->compare); 64ee4af569SSimon Arlott return 0; 65ee4af569SSimon Arlott } 66ee4af569SSimon Arlott 67ee4af569SSimon Arlott static irqreturn_t bcm2835_time_interrupt(int irq, void *dev_id) 68ee4af569SSimon Arlott { 69ee4af569SSimon Arlott struct bcm2835_timer *timer = dev_id; 70ee4af569SSimon Arlott void (*event_handler)(struct clock_event_device *); 71ee4af569SSimon Arlott if (readl_relaxed(timer->control) & timer->match_mask) { 72ee4af569SSimon Arlott writel_relaxed(timer->match_mask, timer->control); 73ee4af569SSimon Arlott 746aa7de05SMark Rutland event_handler = READ_ONCE(timer->evt.event_handler); 75ee4af569SSimon Arlott if (event_handler) 76ee4af569SSimon Arlott event_handler(&timer->evt); 77ee4af569SSimon Arlott return IRQ_HANDLED; 78ee4af569SSimon Arlott } else { 79ee4af569SSimon Arlott return IRQ_NONE; 80ee4af569SSimon Arlott } 81ee4af569SSimon Arlott } 82ee4af569SSimon Arlott 83524a7f08SDaniel Lezcano static int __init bcm2835_timer_init(struct device_node *node) 84ee4af569SSimon Arlott { 85ee4af569SSimon Arlott void __iomem *base; 86ee4af569SSimon Arlott u32 freq; 87524a7f08SDaniel Lezcano int irq, ret; 88ee4af569SSimon Arlott struct bcm2835_timer *timer; 89ee4af569SSimon Arlott 90ee4af569SSimon Arlott base = of_iomap(node, 0); 91524a7f08SDaniel Lezcano if (!base) { 92ac9ce6d1SRafał Miłecki pr_err("Can't remap registers\n"); 93524a7f08SDaniel Lezcano return -ENXIO; 94524a7f08SDaniel Lezcano } 95ee4af569SSimon Arlott 96524a7f08SDaniel Lezcano ret = of_property_read_u32(node, "clock-frequency", &freq); 97524a7f08SDaniel Lezcano if (ret) { 98ac9ce6d1SRafał Miłecki pr_err("Can't read clock-frequency\n"); 9984c39b8bSArvind Yadav goto err_iounmap; 100524a7f08SDaniel Lezcano } 101ee4af569SSimon Arlott 102ee4af569SSimon Arlott system_clock = base + REG_COUNTER_LO; 10318952f20SStephen Boyd sched_clock_register(bcm2835_sched_read, 32, freq); 104ee4af569SSimon Arlott 105ee4af569SSimon Arlott clocksource_mmio_init(base + REG_COUNTER_LO, node->name, 106ee4af569SSimon Arlott freq, 300, 32, clocksource_mmio_readl_up); 107ee4af569SSimon Arlott 108ee4af569SSimon Arlott irq = irq_of_parse_and_map(node, DEFAULT_TIMER); 109524a7f08SDaniel Lezcano if (irq <= 0) { 110ac9ce6d1SRafał Miłecki pr_err("Can't parse IRQ\n"); 11184c39b8bSArvind Yadav ret = -EINVAL; 11284c39b8bSArvind Yadav goto err_iounmap; 113524a7f08SDaniel Lezcano } 114ee4af569SSimon Arlott 115ee4af569SSimon Arlott timer = kzalloc(sizeof(*timer), GFP_KERNEL); 116524a7f08SDaniel Lezcano if (!timer) { 11784c39b8bSArvind Yadav ret = -ENOMEM; 11884c39b8bSArvind Yadav goto err_iounmap; 119524a7f08SDaniel Lezcano } 120ee4af569SSimon Arlott 121ee4af569SSimon Arlott timer->control = base + REG_CONTROL; 122ee4af569SSimon Arlott timer->compare = base + REG_COMPARE(DEFAULT_TIMER); 123ee4af569SSimon Arlott timer->match_mask = BIT(DEFAULT_TIMER); 124ee4af569SSimon Arlott timer->evt.name = node->name; 125ee4af569SSimon Arlott timer->evt.rating = 300; 126ee4af569SSimon Arlott timer->evt.features = CLOCK_EVT_FEAT_ONESHOT; 127ee4af569SSimon Arlott timer->evt.set_next_event = bcm2835_time_set_next_event; 128ee4af569SSimon Arlott timer->evt.cpumask = cpumask_of(0); 129ee4af569SSimon Arlott timer->act.name = node->name; 130ee4af569SSimon Arlott timer->act.flags = IRQF_TIMER | IRQF_SHARED; 131ee4af569SSimon Arlott timer->act.dev_id = timer; 132ee4af569SSimon Arlott timer->act.handler = bcm2835_time_interrupt; 133ee4af569SSimon Arlott 134524a7f08SDaniel Lezcano ret = setup_irq(irq, &timer->act); 135524a7f08SDaniel Lezcano if (ret) { 136524a7f08SDaniel Lezcano pr_err("Can't set up timer IRQ\n"); 13784c39b8bSArvind Yadav goto err_iounmap; 138524a7f08SDaniel Lezcano } 139ee4af569SSimon Arlott 140ee4af569SSimon Arlott clockevents_config_and_register(&timer->evt, freq, 0xf, 0xffffffff); 141ee4af569SSimon Arlott 142ee4af569SSimon Arlott pr_info("bcm2835: system timer (irq = %d)\n", irq); 143524a7f08SDaniel Lezcano 144524a7f08SDaniel Lezcano return 0; 14584c39b8bSArvind Yadav 14684c39b8bSArvind Yadav err_iounmap: 14784c39b8bSArvind Yadav iounmap(base); 14884c39b8bSArvind Yadav return ret; 149ee4af569SSimon Arlott } 15017273395SDaniel Lezcano TIMER_OF_DECLARE(bcm2835, "brcm,bcm2835-system-timer", 151c1b724f6SStephen Warren bcm2835_timer_init); 152