1a7ed099fSArnd Bergmann /* 2a7ed099fSArnd Bergmann * arch/arm/plat-spear/time.c 3a7ed099fSArnd Bergmann * 4a7ed099fSArnd Bergmann * Copyright (C) 2010 ST Microelectronics 5a7ed099fSArnd Bergmann * Shiraz Hashim<shiraz.hashim@st.com> 6a7ed099fSArnd Bergmann * 7a7ed099fSArnd Bergmann * This file is licensed under the terms of the GNU General Public 8a7ed099fSArnd Bergmann * License version 2. This program is licensed "as is" without any 9a7ed099fSArnd Bergmann * warranty of any kind, whether express or implied. 10a7ed099fSArnd Bergmann */ 11a7ed099fSArnd Bergmann 12a7ed099fSArnd Bergmann #include <linux/clk.h> 13a7ed099fSArnd Bergmann #include <linux/clockchips.h> 14a7ed099fSArnd Bergmann #include <linux/clocksource.h> 15a7ed099fSArnd Bergmann #include <linux/err.h> 16a7ed099fSArnd Bergmann #include <linux/init.h> 17a7ed099fSArnd Bergmann #include <linux/interrupt.h> 18a7ed099fSArnd Bergmann #include <linux/ioport.h> 19a7ed099fSArnd Bergmann #include <linux/io.h> 20a7ed099fSArnd Bergmann #include <linux/kernel.h> 21a7ed099fSArnd Bergmann #include <linux/of_irq.h> 22a7ed099fSArnd Bergmann #include <linux/of_address.h> 23a7ed099fSArnd Bergmann #include <linux/time.h> 24a7ed099fSArnd Bergmann #include <linux/irq.h> 25a7ed099fSArnd Bergmann #include <asm/mach/time.h> 262b9c613cSArnd Bergmann #include "generic.h" 27a7ed099fSArnd Bergmann 28a7ed099fSArnd Bergmann /* 29a7ed099fSArnd Bergmann * We would use TIMER0 and TIMER1 as clockevent and clocksource. 30a7ed099fSArnd Bergmann * Timer0 and Timer1 both belong to same gpt block in cpu subbsystem. Further 31a7ed099fSArnd Bergmann * they share same functional clock. Any change in one's functional clock will 32a7ed099fSArnd Bergmann * also affect other timer. 33a7ed099fSArnd Bergmann */ 34a7ed099fSArnd Bergmann 35a7ed099fSArnd Bergmann #define CLKEVT 0 /* gpt0, channel0 as clockevent */ 36a7ed099fSArnd Bergmann #define CLKSRC 1 /* gpt0, channel1 as clocksource */ 37a7ed099fSArnd Bergmann 38a7ed099fSArnd Bergmann /* Register offsets, x is channel number */ 39a7ed099fSArnd Bergmann #define CR(x) ((x) * 0x80 + 0x80) 40a7ed099fSArnd Bergmann #define IR(x) ((x) * 0x80 + 0x84) 41a7ed099fSArnd Bergmann #define LOAD(x) ((x) * 0x80 + 0x88) 42a7ed099fSArnd Bergmann #define COUNT(x) ((x) * 0x80 + 0x8C) 43a7ed099fSArnd Bergmann 44a7ed099fSArnd Bergmann /* Reg bit definitions */ 45a7ed099fSArnd Bergmann #define CTRL_INT_ENABLE 0x0100 46a7ed099fSArnd Bergmann #define CTRL_ENABLE 0x0020 47a7ed099fSArnd Bergmann #define CTRL_ONE_SHOT 0x0010 48a7ed099fSArnd Bergmann 49a7ed099fSArnd Bergmann #define CTRL_PRESCALER1 0x0 50a7ed099fSArnd Bergmann #define CTRL_PRESCALER2 0x1 51a7ed099fSArnd Bergmann #define CTRL_PRESCALER4 0x2 52a7ed099fSArnd Bergmann #define CTRL_PRESCALER8 0x3 53a7ed099fSArnd Bergmann #define CTRL_PRESCALER16 0x4 54a7ed099fSArnd Bergmann #define CTRL_PRESCALER32 0x5 55a7ed099fSArnd Bergmann #define CTRL_PRESCALER64 0x6 56a7ed099fSArnd Bergmann #define CTRL_PRESCALER128 0x7 57a7ed099fSArnd Bergmann #define CTRL_PRESCALER256 0x8 58a7ed099fSArnd Bergmann 59a7ed099fSArnd Bergmann #define INT_STATUS 0x1 60a7ed099fSArnd Bergmann 61a7ed099fSArnd Bergmann /* 62a7ed099fSArnd Bergmann * Minimum clocksource/clockevent timer range in seconds 63a7ed099fSArnd Bergmann */ 64a7ed099fSArnd Bergmann #define SPEAR_MIN_RANGE 4 65a7ed099fSArnd Bergmann 66a7ed099fSArnd Bergmann static __iomem void *gpt_base; 67a7ed099fSArnd Bergmann static struct clk *gpt_clk; 68a7ed099fSArnd Bergmann 69a7ed099fSArnd Bergmann static void clockevent_set_mode(enum clock_event_mode mode, 70a7ed099fSArnd Bergmann struct clock_event_device *clk_event_dev); 71a7ed099fSArnd Bergmann static int clockevent_next_event(unsigned long evt, 72a7ed099fSArnd Bergmann struct clock_event_device *clk_event_dev); 73a7ed099fSArnd Bergmann 741be5f692SAlex Elder static void __init spear_clocksource_init(void) 75a7ed099fSArnd Bergmann { 76a7ed099fSArnd Bergmann u32 tick_rate; 77a7ed099fSArnd Bergmann u16 val; 78a7ed099fSArnd Bergmann 79a7ed099fSArnd Bergmann /* program the prescaler (/256)*/ 80a7ed099fSArnd Bergmann writew(CTRL_PRESCALER256, gpt_base + CR(CLKSRC)); 81a7ed099fSArnd Bergmann 82a7ed099fSArnd Bergmann /* find out actual clock driving Timer */ 83a7ed099fSArnd Bergmann tick_rate = clk_get_rate(gpt_clk); 84a7ed099fSArnd Bergmann tick_rate >>= CTRL_PRESCALER256; 85a7ed099fSArnd Bergmann 86a7ed099fSArnd Bergmann writew(0xFFFF, gpt_base + LOAD(CLKSRC)); 87a7ed099fSArnd Bergmann 88a7ed099fSArnd Bergmann val = readw(gpt_base + CR(CLKSRC)); 89a7ed099fSArnd Bergmann val &= ~CTRL_ONE_SHOT; /* autoreload mode */ 90a7ed099fSArnd Bergmann val |= CTRL_ENABLE ; 91a7ed099fSArnd Bergmann writew(val, gpt_base + CR(CLKSRC)); 92a7ed099fSArnd Bergmann 93a7ed099fSArnd Bergmann /* register the clocksource */ 94a7ed099fSArnd Bergmann clocksource_mmio_init(gpt_base + COUNT(CLKSRC), "tmr1", tick_rate, 95a7ed099fSArnd Bergmann 200, 16, clocksource_mmio_readw_up); 96a7ed099fSArnd Bergmann } 97a7ed099fSArnd Bergmann 98a7ed099fSArnd Bergmann static struct clock_event_device clkevt = { 99a7ed099fSArnd Bergmann .name = "tmr0", 100a7ed099fSArnd Bergmann .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, 101a7ed099fSArnd Bergmann .set_mode = clockevent_set_mode, 102a7ed099fSArnd Bergmann .set_next_event = clockevent_next_event, 103a7ed099fSArnd Bergmann .shift = 0, /* to be computed */ 104a7ed099fSArnd Bergmann }; 105a7ed099fSArnd Bergmann 106a7ed099fSArnd Bergmann static void clockevent_set_mode(enum clock_event_mode mode, 107a7ed099fSArnd Bergmann struct clock_event_device *clk_event_dev) 108a7ed099fSArnd Bergmann { 109a7ed099fSArnd Bergmann u32 period; 110a7ed099fSArnd Bergmann u16 val; 111a7ed099fSArnd Bergmann 112a7ed099fSArnd Bergmann /* stop the timer */ 113a7ed099fSArnd Bergmann val = readw(gpt_base + CR(CLKEVT)); 114a7ed099fSArnd Bergmann val &= ~CTRL_ENABLE; 115a7ed099fSArnd Bergmann writew(val, gpt_base + CR(CLKEVT)); 116a7ed099fSArnd Bergmann 117a7ed099fSArnd Bergmann switch (mode) { 118a7ed099fSArnd Bergmann case CLOCK_EVT_MODE_PERIODIC: 119a7ed099fSArnd Bergmann period = clk_get_rate(gpt_clk) / HZ; 120a7ed099fSArnd Bergmann period >>= CTRL_PRESCALER16; 121a7ed099fSArnd Bergmann writew(period, gpt_base + LOAD(CLKEVT)); 122a7ed099fSArnd Bergmann 123a7ed099fSArnd Bergmann val = readw(gpt_base + CR(CLKEVT)); 124a7ed099fSArnd Bergmann val &= ~CTRL_ONE_SHOT; 125a7ed099fSArnd Bergmann val |= CTRL_ENABLE | CTRL_INT_ENABLE; 126a7ed099fSArnd Bergmann writew(val, gpt_base + CR(CLKEVT)); 127a7ed099fSArnd Bergmann 128a7ed099fSArnd Bergmann break; 129a7ed099fSArnd Bergmann case CLOCK_EVT_MODE_ONESHOT: 130a7ed099fSArnd Bergmann val = readw(gpt_base + CR(CLKEVT)); 131a7ed099fSArnd Bergmann val |= CTRL_ONE_SHOT; 132a7ed099fSArnd Bergmann writew(val, gpt_base + CR(CLKEVT)); 133a7ed099fSArnd Bergmann 134a7ed099fSArnd Bergmann break; 135a7ed099fSArnd Bergmann case CLOCK_EVT_MODE_UNUSED: 136a7ed099fSArnd Bergmann case CLOCK_EVT_MODE_SHUTDOWN: 137a7ed099fSArnd Bergmann case CLOCK_EVT_MODE_RESUME: 138a7ed099fSArnd Bergmann 139a7ed099fSArnd Bergmann break; 140a7ed099fSArnd Bergmann default: 141a7ed099fSArnd Bergmann pr_err("Invalid mode requested\n"); 142a7ed099fSArnd Bergmann break; 143a7ed099fSArnd Bergmann } 144a7ed099fSArnd Bergmann } 145a7ed099fSArnd Bergmann 146a7ed099fSArnd Bergmann static int clockevent_next_event(unsigned long cycles, 147a7ed099fSArnd Bergmann struct clock_event_device *clk_event_dev) 148a7ed099fSArnd Bergmann { 149a7ed099fSArnd Bergmann u16 val = readw(gpt_base + CR(CLKEVT)); 150a7ed099fSArnd Bergmann 151a7ed099fSArnd Bergmann if (val & CTRL_ENABLE) 152a7ed099fSArnd Bergmann writew(val & ~CTRL_ENABLE, gpt_base + CR(CLKEVT)); 153a7ed099fSArnd Bergmann 154a7ed099fSArnd Bergmann writew(cycles, gpt_base + LOAD(CLKEVT)); 155a7ed099fSArnd Bergmann 156a7ed099fSArnd Bergmann val |= CTRL_ENABLE | CTRL_INT_ENABLE; 157a7ed099fSArnd Bergmann writew(val, gpt_base + CR(CLKEVT)); 158a7ed099fSArnd Bergmann 159a7ed099fSArnd Bergmann return 0; 160a7ed099fSArnd Bergmann } 161a7ed099fSArnd Bergmann 162a7ed099fSArnd Bergmann static irqreturn_t spear_timer_interrupt(int irq, void *dev_id) 163a7ed099fSArnd Bergmann { 164a7ed099fSArnd Bergmann struct clock_event_device *evt = &clkevt; 165a7ed099fSArnd Bergmann 166a7ed099fSArnd Bergmann writew(INT_STATUS, gpt_base + IR(CLKEVT)); 167a7ed099fSArnd Bergmann 168a7ed099fSArnd Bergmann evt->event_handler(evt); 169a7ed099fSArnd Bergmann 170a7ed099fSArnd Bergmann return IRQ_HANDLED; 171a7ed099fSArnd Bergmann } 172a7ed099fSArnd Bergmann 173a7ed099fSArnd Bergmann static struct irqaction spear_timer_irq = { 174a7ed099fSArnd Bergmann .name = "timer", 17549710fa4SMichael Opdenacker .flags = IRQF_TIMER, 176a7ed099fSArnd Bergmann .handler = spear_timer_interrupt 177a7ed099fSArnd Bergmann }; 178a7ed099fSArnd Bergmann 179a7ed099fSArnd Bergmann static void __init spear_clockevent_init(int irq) 180a7ed099fSArnd Bergmann { 181a7ed099fSArnd Bergmann u32 tick_rate; 182a7ed099fSArnd Bergmann 183a7ed099fSArnd Bergmann /* program the prescaler */ 184a7ed099fSArnd Bergmann writew(CTRL_PRESCALER16, gpt_base + CR(CLKEVT)); 185a7ed099fSArnd Bergmann 186a7ed099fSArnd Bergmann tick_rate = clk_get_rate(gpt_clk); 187a7ed099fSArnd Bergmann tick_rate >>= CTRL_PRESCALER16; 188a7ed099fSArnd Bergmann 189a7ed099fSArnd Bergmann clkevt.cpumask = cpumask_of(0); 190a7ed099fSArnd Bergmann 191a7ed099fSArnd Bergmann clockevents_config_and_register(&clkevt, tick_rate, 3, 0xfff0); 192a7ed099fSArnd Bergmann 193a7ed099fSArnd Bergmann setup_irq(irq, &spear_timer_irq); 194a7ed099fSArnd Bergmann } 195a7ed099fSArnd Bergmann 196a7ed099fSArnd Bergmann const static struct of_device_id timer_of_match[] __initconst = { 197a7ed099fSArnd Bergmann { .compatible = "st,spear-timer", }, 198a7ed099fSArnd Bergmann { }, 199a7ed099fSArnd Bergmann }; 200a7ed099fSArnd Bergmann 201a7ed099fSArnd Bergmann void __init spear_setup_of_timer(void) 202a7ed099fSArnd Bergmann { 203a7ed099fSArnd Bergmann struct device_node *np; 204a7ed099fSArnd Bergmann int irq, ret; 205a7ed099fSArnd Bergmann 206a7ed099fSArnd Bergmann np = of_find_matching_node(NULL, timer_of_match); 207a7ed099fSArnd Bergmann if (!np) { 208a7ed099fSArnd Bergmann pr_err("%s: No timer passed via DT\n", __func__); 209a7ed099fSArnd Bergmann return; 210a7ed099fSArnd Bergmann } 211a7ed099fSArnd Bergmann 212a7ed099fSArnd Bergmann irq = irq_of_parse_and_map(np, 0); 213a7ed099fSArnd Bergmann if (!irq) { 214a7ed099fSArnd Bergmann pr_err("%s: No irq passed for timer via DT\n", __func__); 215a7ed099fSArnd Bergmann return; 216a7ed099fSArnd Bergmann } 217a7ed099fSArnd Bergmann 218a7ed099fSArnd Bergmann gpt_base = of_iomap(np, 0); 219a7ed099fSArnd Bergmann if (!gpt_base) { 220a7ed099fSArnd Bergmann pr_err("%s: of iomap failed\n", __func__); 221a7ed099fSArnd Bergmann return; 222a7ed099fSArnd Bergmann } 223a7ed099fSArnd Bergmann 224a7ed099fSArnd Bergmann gpt_clk = clk_get_sys("gpt0", NULL); 225a7ed099fSArnd Bergmann if (!gpt_clk) { 226a7ed099fSArnd Bergmann pr_err("%s:couldn't get clk for gpt\n", __func__); 227a7ed099fSArnd Bergmann goto err_iomap; 228a7ed099fSArnd Bergmann } 229a7ed099fSArnd Bergmann 230a7ed099fSArnd Bergmann ret = clk_prepare_enable(gpt_clk); 231a7ed099fSArnd Bergmann if (ret < 0) { 232a7ed099fSArnd Bergmann pr_err("%s:couldn't prepare-enable gpt clock\n", __func__); 233a7ed099fSArnd Bergmann goto err_prepare_enable_clk; 234a7ed099fSArnd Bergmann } 235a7ed099fSArnd Bergmann 236a7ed099fSArnd Bergmann spear_clockevent_init(irq); 237a7ed099fSArnd Bergmann spear_clocksource_init(); 238a7ed099fSArnd Bergmann 239a7ed099fSArnd Bergmann return; 240a7ed099fSArnd Bergmann 241a7ed099fSArnd Bergmann err_prepare_enable_clk: 242a7ed099fSArnd Bergmann clk_put(gpt_clk); 243a7ed099fSArnd Bergmann err_iomap: 244a7ed099fSArnd Bergmann iounmap(gpt_base); 245a7ed099fSArnd Bergmann } 246