Uwe Kleine-König | 9918cda | 2007-02-16 15:36:55 +0100 | [diff] [blame] | 1 | /* |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 2 | * arch/arm/mach-ns9xxx/time-ns9360.c |
Uwe Kleine-König | 9918cda | 2007-02-16 15:36:55 +0100 | [diff] [blame] | 3 | * |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006,2007 by Digi International Inc. |
Uwe Kleine-König | 9918cda | 2007-02-16 15:36:55 +0100 | [diff] [blame] | 5 | * All rights reserved. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License version 2 as published by |
| 9 | * the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/jiffies.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/irq.h> |
Uwe Kleine-König | cef5975 | 2007-09-30 20:35:48 +0100 | [diff] [blame] | 14 | #include <linux/stringify.h> |
| 15 | #include <linux/clocksource.h> |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 16 | #include <linux/clockchips.h> |
Uwe Kleine-König | cef5975 | 2007-09-30 20:35:48 +0100 | [diff] [blame] | 17 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 18 | #include <mach/processor-ns9360.h> |
| 19 | #include <mach/regs-sys-ns9360.h> |
| 20 | #include <mach/irqs.h> |
| 21 | #include <mach/system.h> |
Uwe Kleine-König | 9918cda | 2007-02-16 15:36:55 +0100 | [diff] [blame] | 22 | #include "generic.h" |
| 23 | |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 24 | #define TIMER_CLOCKSOURCE 0 |
| 25 | #define TIMER_CLOCKEVENT 1 |
| 26 | static u32 latch; |
Uwe Kleine-König | 9918cda | 2007-02-16 15:36:55 +0100 | [diff] [blame] | 27 | |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 28 | static cycle_t ns9360_clocksource_read(struct clocksource *cs) |
Uwe Kleine-König | cef5975 | 2007-09-30 20:35:48 +0100 | [diff] [blame] | 29 | { |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 30 | return __raw_readl(SYS_TR(TIMER_CLOCKSOURCE)); |
Uwe Kleine-König | cef5975 | 2007-09-30 20:35:48 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 33 | static struct clocksource ns9360_clocksource = { |
| 34 | .name = "ns9360-timer" __stringify(TIMER_CLOCKSOURCE), |
Uwe Kleine-König | cef5975 | 2007-09-30 20:35:48 +0100 | [diff] [blame] | 35 | .rating = 300, |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 36 | .read = ns9360_clocksource_read, |
Uwe Kleine-König | cef5975 | 2007-09-30 20:35:48 +0100 | [diff] [blame] | 37 | .mask = CLOCKSOURCE_MASK(32), |
| 38 | .shift = 20, |
| 39 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 40 | }; |
| 41 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 42 | static void ns9360_clockevent_setmode(enum clock_event_mode mode, |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 43 | struct clock_event_device *clk) |
| 44 | { |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 45 | u32 tc = __raw_readl(SYS_TC(TIMER_CLOCKEVENT)); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 46 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 47 | switch (mode) { |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 48 | case CLOCK_EVT_MODE_PERIODIC: |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 49 | __raw_writel(latch, SYS_TRC(TIMER_CLOCKEVENT)); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 50 | REGSET(tc, SYS_TCx, REN, EN); |
| 51 | REGSET(tc, SYS_TCx, INTS, EN); |
| 52 | REGSET(tc, SYS_TCx, TEN, EN); |
| 53 | break; |
| 54 | |
| 55 | case CLOCK_EVT_MODE_ONESHOT: |
| 56 | REGSET(tc, SYS_TCx, REN, DIS); |
| 57 | REGSET(tc, SYS_TCx, INTS, EN); |
| 58 | |
| 59 | /* fall through */ |
| 60 | |
| 61 | case CLOCK_EVT_MODE_UNUSED: |
| 62 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 63 | case CLOCK_EVT_MODE_RESUME: |
| 64 | default: |
| 65 | REGSET(tc, SYS_TCx, TEN, DIS); |
| 66 | break; |
| 67 | } |
| 68 | |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 69 | __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT)); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 72 | static int ns9360_clockevent_setnextevent(unsigned long evt, |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 73 | struct clock_event_device *clk) |
| 74 | { |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 75 | u32 tc = __raw_readl(SYS_TC(TIMER_CLOCKEVENT)); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 76 | |
| 77 | if (REGGET(tc, SYS_TCx, TEN)) { |
| 78 | REGSET(tc, SYS_TCx, TEN, DIS); |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 79 | __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT)); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | REGSET(tc, SYS_TCx, TEN, EN); |
| 83 | |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 84 | __raw_writel(evt, SYS_TRC(TIMER_CLOCKEVENT)); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 85 | |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 86 | __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT)); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 91 | static struct clock_event_device ns9360_clockevent_device = { |
| 92 | .name = "ns9360-timer" __stringify(TIMER_CLOCKEVENT), |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 93 | .shift = 20, |
| 94 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 95 | .set_mode = ns9360_clockevent_setmode, |
| 96 | .set_next_event = ns9360_clockevent_setnextevent, |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 99 | static irqreturn_t ns9360_clockevent_handler(int irq, void *dev_id) |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 100 | { |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 101 | int timerno = irq - IRQ_NS9360_TIMER0; |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 102 | u32 tc; |
| 103 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 104 | struct clock_event_device *evt = &ns9360_clockevent_device; |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 105 | |
| 106 | /* clear irq */ |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 107 | tc = __raw_readl(SYS_TC(timerno)); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 108 | if (REGGET(tc, SYS_TCx, REN) == SYS_TCx_REN_DIS) { |
| 109 | REGSET(tc, SYS_TCx, TEN, DIS); |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 110 | __raw_writel(tc, SYS_TC(timerno)); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 111 | } |
| 112 | REGSET(tc, SYS_TCx, INTC, SET); |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 113 | __raw_writel(tc, SYS_TC(timerno)); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 114 | REGSET(tc, SYS_TCx, INTC, UNSET); |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 115 | __raw_writel(tc, SYS_TC(timerno)); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 116 | |
| 117 | evt->event_handler(evt); |
| 118 | |
| 119 | return IRQ_HANDLED; |
| 120 | } |
| 121 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 122 | static struct irqaction ns9360_clockevent_action = { |
| 123 | .name = "ns9360-timer" __stringify(TIMER_CLOCKEVENT), |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 124 | .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL, |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 125 | .handler = ns9360_clockevent_handler, |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 126 | }; |
| 127 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 128 | static void __init ns9360_timer_init(void) |
Uwe Kleine-König | 9918cda | 2007-02-16 15:36:55 +0100 | [diff] [blame] | 129 | { |
| 130 | int tc; |
| 131 | |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 132 | tc = __raw_readl(SYS_TC(TIMER_CLOCKSOURCE)); |
Uwe Kleine-König | cef5975 | 2007-09-30 20:35:48 +0100 | [diff] [blame] | 133 | if (REGGET(tc, SYS_TCx, TEN)) { |
| 134 | REGSET(tc, SYS_TCx, TEN, DIS); |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 135 | __raw_writel(tc, SYS_TC(TIMER_CLOCKSOURCE)); |
Uwe Kleine-König | cef5975 | 2007-09-30 20:35:48 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 138 | __raw_writel(0, SYS_TRC(TIMER_CLOCKSOURCE)); |
Uwe Kleine-König | cef5975 | 2007-09-30 20:35:48 +0100 | [diff] [blame] | 139 | |
| 140 | REGSET(tc, SYS_TCx, TEN, EN); |
| 141 | REGSET(tc, SYS_TCx, TDBG, STOP); |
| 142 | REGSET(tc, SYS_TCx, TLCS, CPU); |
| 143 | REGSET(tc, SYS_TCx, TM, IEE); |
| 144 | REGSET(tc, SYS_TCx, INTS, DIS); |
| 145 | REGSET(tc, SYS_TCx, UDS, UP); |
| 146 | REGSET(tc, SYS_TCx, TSZ, 32); |
| 147 | REGSET(tc, SYS_TCx, REN, EN); |
| 148 | |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 149 | __raw_writel(tc, SYS_TC(TIMER_CLOCKSOURCE)); |
Uwe Kleine-König | cef5975 | 2007-09-30 20:35:48 +0100 | [diff] [blame] | 150 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 151 | ns9360_clocksource.mult = clocksource_hz2mult(ns9360_cpuclock(), |
| 152 | ns9360_clocksource.shift); |
Uwe Kleine-König | cef5975 | 2007-09-30 20:35:48 +0100 | [diff] [blame] | 153 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 154 | clocksource_register(&ns9360_clocksource); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 155 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 156 | latch = SH_DIV(ns9360_cpuclock(), HZ, 0); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 157 | |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 158 | tc = __raw_readl(SYS_TC(TIMER_CLOCKEVENT)); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 159 | REGSET(tc, SYS_TCx, TEN, DIS); |
| 160 | REGSET(tc, SYS_TCx, TDBG, STOP); |
| 161 | REGSET(tc, SYS_TCx, TLCS, CPU); |
| 162 | REGSET(tc, SYS_TCx, TM, IEE); |
| 163 | REGSET(tc, SYS_TCx, INTS, DIS); |
| 164 | REGSET(tc, SYS_TCx, UDS, DOWN); |
| 165 | REGSET(tc, SYS_TCx, TSZ, 32); |
| 166 | REGSET(tc, SYS_TCx, REN, EN); |
Uwe Kleine-König | 361c7ad | 2007-09-30 20:36:33 +0100 | [diff] [blame] | 167 | __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT)); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 168 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 169 | ns9360_clockevent_device.mult = div_sc(ns9360_cpuclock(), |
| 170 | NSEC_PER_SEC, ns9360_clockevent_device.shift); |
| 171 | ns9360_clockevent_device.max_delta_ns = |
| 172 | clockevent_delta2ns(-1, &ns9360_clockevent_device); |
| 173 | ns9360_clockevent_device.min_delta_ns = |
| 174 | clockevent_delta2ns(1, &ns9360_clockevent_device); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 175 | |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 176 | ns9360_clockevent_device.cpumask = cpumask_of(0); |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 177 | clockevents_register_device(&ns9360_clockevent_device); |
Uwe Kleine-König | c0bb87f | 2007-09-30 20:36:00 +0100 | [diff] [blame] | 178 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 179 | setup_irq(IRQ_NS9360_TIMER0 + TIMER_CLOCKEVENT, |
| 180 | &ns9360_clockevent_action); |
Uwe Kleine-König | 9918cda | 2007-02-16 15:36:55 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Uwe Kleine-König | 724ce5e | 2008-02-15 08:41:06 +0100 | [diff] [blame] | 183 | struct sys_timer ns9360_timer = { |
| 184 | .init = ns9360_timer_init, |
Uwe Kleine-König | 9918cda | 2007-02-16 15:36:55 +0100 | [diff] [blame] | 185 | }; |