Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000-2001 Deep Blue Solutions |
| 3 | * Copyright (C) 2002 Shane Nay (shane@minirl.com) |
| 4 | * Copyright (C) 2006-2007 Pavel Pisa (ppisa@pikron.com) |
| 5 | * Copyright (C) 2008 Juergen Beisert (kernel@pengutronix.de) |
| 6 | * Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version 2 |
| 11 | * of the License, or (at your option) any later version. |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 20 | * MA 02110-1301, USA. |
| 21 | */ |
| 22 | |
Shawn Guo | 39d1367 | 2012-04-29 00:02:37 +0800 | [diff] [blame] | 23 | #include <linux/err.h> |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/irq.h> |
| 26 | #include <linux/clockchips.h> |
| 27 | #include <linux/clk.h> |
Shawn Guo | eeca6e6 | 2012-08-20 08:51:45 +0800 | [diff] [blame] | 28 | #include <linux/of.h> |
Shawn Guo | be35bd2 | 2013-03-25 14:57:41 +0800 | [diff] [blame] | 29 | #include <linux/of_address.h> |
Shawn Guo | eeca6e6 | 2012-08-20 08:51:45 +0800 | [diff] [blame] | 30 | #include <linux/of_irq.h> |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 31 | |
| 32 | #include <asm/mach/time.h> |
Stanislav Meduna | 67948ad | 2012-11-08 23:39:14 +0100 | [diff] [blame] | 33 | #include <asm/sched_clock.h> |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 34 | #include <mach/mxs.h> |
| 35 | #include <mach/common.h> |
| 36 | |
| 37 | /* |
| 38 | * There are 2 versions of the timrot on Freescale MXS-based SoCs. |
| 39 | * The v1 on MX23 only gets 16 bits counter, while v2 on MX28 |
| 40 | * extends the counter to 32 bits. |
| 41 | * |
| 42 | * The implementation uses two timers, one for clock_event and |
| 43 | * another for clocksource. MX28 uses timrot 0 and 1, while MX23 |
| 44 | * uses 0 and 2. |
| 45 | */ |
| 46 | |
| 47 | #define MX23_TIMROT_VERSION_OFFSET 0x0a0 |
| 48 | #define MX28_TIMROT_VERSION_OFFSET 0x120 |
| 49 | #define BP_TIMROT_MAJOR_VERSION 24 |
| 50 | #define BV_TIMROT_VERSION_1 0x01 |
| 51 | #define BV_TIMROT_VERSION_2 0x02 |
| 52 | #define timrot_is_v1() (timrot_major_version == BV_TIMROT_VERSION_1) |
| 53 | |
| 54 | /* |
| 55 | * There are 4 registers for each timrotv2 instance, and 2 registers |
| 56 | * for each timrotv1. So address step 0x40 in macros below strides |
| 57 | * one instance of timrotv2 while two instances of timrotv1. |
| 58 | * |
| 59 | * As the result, HW_TIMROT_XXXn(1) defines the address of timrot1 |
| 60 | * on MX28 while timrot2 on MX23. |
| 61 | */ |
| 62 | /* common between v1 and v2 */ |
| 63 | #define HW_TIMROT_ROTCTRL 0x00 |
| 64 | #define HW_TIMROT_TIMCTRLn(n) (0x20 + (n) * 0x40) |
| 65 | /* v1 only */ |
| 66 | #define HW_TIMROT_TIMCOUNTn(n) (0x30 + (n) * 0x40) |
| 67 | /* v2 only */ |
| 68 | #define HW_TIMROT_RUNNING_COUNTn(n) (0x30 + (n) * 0x40) |
| 69 | #define HW_TIMROT_FIXED_COUNTn(n) (0x40 + (n) * 0x40) |
| 70 | |
| 71 | #define BM_TIMROT_TIMCTRLn_RELOAD (1 << 6) |
| 72 | #define BM_TIMROT_TIMCTRLn_UPDATE (1 << 7) |
| 73 | #define BM_TIMROT_TIMCTRLn_IRQ_EN (1 << 14) |
| 74 | #define BM_TIMROT_TIMCTRLn_IRQ (1 << 15) |
| 75 | #define BP_TIMROT_TIMCTRLn_SELECT 0 |
Torben Hohn | 2fb318f | 2012-12-21 15:06:15 +0100 | [diff] [blame] | 76 | #define BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL 0x8 |
| 77 | #define BV_TIMROTv2_TIMCTRLn_SELECT__32KHZ_XTAL 0xb |
| 78 | #define BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS 0xf |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 79 | |
| 80 | static struct clock_event_device mxs_clockevent_device; |
| 81 | static enum clock_event_mode mxs_clockevent_mode = CLOCK_EVT_MODE_UNUSED; |
| 82 | |
Shawn Guo | be35bd2 | 2013-03-25 14:57:41 +0800 | [diff] [blame] | 83 | static void __iomem *mxs_timrot_base; |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 84 | static u32 timrot_major_version; |
| 85 | |
| 86 | static inline void timrot_irq_disable(void) |
| 87 | { |
| 88 | __mxs_clrl(BM_TIMROT_TIMCTRLn_IRQ_EN, |
| 89 | mxs_timrot_base + HW_TIMROT_TIMCTRLn(0)); |
| 90 | } |
| 91 | |
| 92 | static inline void timrot_irq_enable(void) |
| 93 | { |
| 94 | __mxs_setl(BM_TIMROT_TIMCTRLn_IRQ_EN, |
| 95 | mxs_timrot_base + HW_TIMROT_TIMCTRLn(0)); |
| 96 | } |
| 97 | |
| 98 | static void timrot_irq_acknowledge(void) |
| 99 | { |
| 100 | __mxs_clrl(BM_TIMROT_TIMCTRLn_IRQ, |
| 101 | mxs_timrot_base + HW_TIMROT_TIMCTRLn(0)); |
| 102 | } |
| 103 | |
| 104 | static cycle_t timrotv1_get_cycles(struct clocksource *cs) |
| 105 | { |
| 106 | return ~((__raw_readl(mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1)) |
| 107 | & 0xffff0000) >> 16); |
| 108 | } |
| 109 | |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 110 | static int timrotv1_set_next_event(unsigned long evt, |
| 111 | struct clock_event_device *dev) |
| 112 | { |
| 113 | /* timrot decrements the count */ |
| 114 | __raw_writel(evt, mxs_timrot_base + HW_TIMROT_TIMCOUNTn(0)); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int timrotv2_set_next_event(unsigned long evt, |
| 120 | struct clock_event_device *dev) |
| 121 | { |
| 122 | /* timrot decrements the count */ |
| 123 | __raw_writel(evt, mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(0)); |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static irqreturn_t mxs_timer_interrupt(int irq, void *dev_id) |
| 129 | { |
| 130 | struct clock_event_device *evt = dev_id; |
| 131 | |
| 132 | timrot_irq_acknowledge(); |
| 133 | evt->event_handler(evt); |
| 134 | |
| 135 | return IRQ_HANDLED; |
| 136 | } |
| 137 | |
| 138 | static struct irqaction mxs_timer_irq = { |
| 139 | .name = "MXS Timer Tick", |
| 140 | .dev_id = &mxs_clockevent_device, |
| 141 | .flags = IRQF_TIMER | IRQF_IRQPOLL, |
| 142 | .handler = mxs_timer_interrupt, |
| 143 | }; |
| 144 | |
| 145 | #ifdef DEBUG |
| 146 | static const char *clock_event_mode_label[] const = { |
| 147 | [CLOCK_EVT_MODE_PERIODIC] = "CLOCK_EVT_MODE_PERIODIC", |
| 148 | [CLOCK_EVT_MODE_ONESHOT] = "CLOCK_EVT_MODE_ONESHOT", |
| 149 | [CLOCK_EVT_MODE_SHUTDOWN] = "CLOCK_EVT_MODE_SHUTDOWN", |
| 150 | [CLOCK_EVT_MODE_UNUSED] = "CLOCK_EVT_MODE_UNUSED" |
| 151 | }; |
| 152 | #endif /* DEBUG */ |
| 153 | |
| 154 | static void mxs_set_mode(enum clock_event_mode mode, |
| 155 | struct clock_event_device *evt) |
| 156 | { |
| 157 | /* Disable interrupt in timer module */ |
| 158 | timrot_irq_disable(); |
| 159 | |
| 160 | if (mode != mxs_clockevent_mode) { |
| 161 | /* Set event time into the furthest future */ |
| 162 | if (timrot_is_v1()) |
| 163 | __raw_writel(0xffff, |
| 164 | mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1)); |
| 165 | else |
| 166 | __raw_writel(0xffffffff, |
| 167 | mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1)); |
| 168 | |
| 169 | /* Clear pending interrupt */ |
| 170 | timrot_irq_acknowledge(); |
| 171 | } |
| 172 | |
| 173 | #ifdef DEBUG |
| 174 | pr_info("%s: changing mode from %s to %s\n", __func__, |
| 175 | clock_event_mode_label[mxs_clockevent_mode], |
| 176 | clock_event_mode_label[mode]); |
| 177 | #endif /* DEBUG */ |
| 178 | |
| 179 | /* Remember timer mode */ |
| 180 | mxs_clockevent_mode = mode; |
| 181 | |
| 182 | switch (mode) { |
| 183 | case CLOCK_EVT_MODE_PERIODIC: |
| 184 | pr_err("%s: Periodic mode is not implemented\n", __func__); |
| 185 | break; |
| 186 | case CLOCK_EVT_MODE_ONESHOT: |
| 187 | timrot_irq_enable(); |
| 188 | break; |
| 189 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 190 | case CLOCK_EVT_MODE_UNUSED: |
| 191 | case CLOCK_EVT_MODE_RESUME: |
| 192 | /* Left event sources disabled, no more interrupts appear */ |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static struct clock_event_device mxs_clockevent_device = { |
| 198 | .name = "mxs_timrot", |
| 199 | .features = CLOCK_EVT_FEAT_ONESHOT, |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 200 | .set_mode = mxs_set_mode, |
| 201 | .set_next_event = timrotv2_set_next_event, |
| 202 | .rating = 200, |
| 203 | }; |
| 204 | |
| 205 | static int __init mxs_clockevent_init(struct clk *timer_clk) |
| 206 | { |
Shawn Guo | 838a2ae | 2013-01-12 11:50:05 +0000 | [diff] [blame] | 207 | if (timrot_is_v1()) |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 208 | mxs_clockevent_device.set_next_event = timrotv1_set_next_event; |
Shawn Guo | 838a2ae | 2013-01-12 11:50:05 +0000 | [diff] [blame] | 209 | mxs_clockevent_device.cpumask = cpumask_of(0); |
| 210 | clockevents_config_and_register(&mxs_clockevent_device, |
Torben Hohn | 93044fa | 2012-12-21 15:06:16 +0100 | [diff] [blame] | 211 | clk_get_rate(timer_clk), |
| 212 | timrot_is_v1() ? 0xf : 0x2, |
Shawn Guo | 838a2ae | 2013-01-12 11:50:05 +0000 | [diff] [blame] | 213 | timrot_is_v1() ? 0xfffe : 0xfffffffe); |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static struct clocksource clocksource_mxs = { |
| 219 | .name = "mxs_timer", |
| 220 | .rating = 200, |
Russell King | 5c61ddc | 2011-05-08 17:21:49 +0100 | [diff] [blame] | 221 | .read = timrotv1_get_cycles, |
| 222 | .mask = CLOCKSOURCE_MASK(16), |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 223 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 224 | }; |
| 225 | |
Stanislav Meduna | 67948ad | 2012-11-08 23:39:14 +0100 | [diff] [blame] | 226 | static u32 notrace mxs_read_sched_clock_v2(void) |
| 227 | { |
| 228 | return ~readl_relaxed(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1)); |
| 229 | } |
| 230 | |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 231 | static int __init mxs_clocksource_init(struct clk *timer_clk) |
| 232 | { |
| 233 | unsigned int c = clk_get_rate(timer_clk); |
| 234 | |
Russell King | 5c61ddc | 2011-05-08 17:21:49 +0100 | [diff] [blame] | 235 | if (timrot_is_v1()) |
| 236 | clocksource_register_hz(&clocksource_mxs, c); |
Stanislav Meduna | 67948ad | 2012-11-08 23:39:14 +0100 | [diff] [blame] | 237 | else { |
Russell King | 5c61ddc | 2011-05-08 17:21:49 +0100 | [diff] [blame] | 238 | clocksource_mmio_init(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1), |
| 239 | "mxs_timer", c, 200, 32, clocksource_mmio_readl_down); |
Stanislav Meduna | 67948ad | 2012-11-08 23:39:14 +0100 | [diff] [blame] | 240 | setup_sched_clock(mxs_read_sched_clock_v2, 32, c); |
| 241 | } |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Shawn Guo | 633ef4c | 2013-03-25 14:53:08 +0800 | [diff] [blame] | 246 | static void __init mxs_timer_init(struct device_node *np) |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 247 | { |
Shawn Guo | 5026092 | 2012-04-29 00:02:41 +0800 | [diff] [blame] | 248 | struct clk *timer_clk; |
Shawn Guo | eeca6e6 | 2012-08-20 08:51:45 +0800 | [diff] [blame] | 249 | int irq; |
| 250 | |
Shawn Guo | be35bd2 | 2013-03-25 14:57:41 +0800 | [diff] [blame] | 251 | mxs_timrot_base = of_iomap(np, 0); |
| 252 | WARN_ON(!mxs_timrot_base); |
| 253 | |
Shawn Guo | 2efb950 | 2013-03-25 22:57:14 +0800 | [diff] [blame] | 254 | timer_clk = of_clk_get(np, 0); |
Shawn Guo | 5026092 | 2012-04-29 00:02:41 +0800 | [diff] [blame] | 255 | if (IS_ERR(timer_clk)) { |
| 256 | pr_err("%s: failed to get clk\n", __func__); |
| 257 | return; |
Shawn Guo | 39d1367 | 2012-04-29 00:02:37 +0800 | [diff] [blame] | 258 | } |
| 259 | |
Shawn Guo | ae68f7a | 2011-12-20 13:50:11 +0800 | [diff] [blame] | 260 | clk_prepare_enable(timer_clk); |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 261 | |
| 262 | /* |
| 263 | * Initialize timers to a known state |
| 264 | */ |
| 265 | mxs_reset_block(mxs_timrot_base + HW_TIMROT_ROTCTRL); |
| 266 | |
| 267 | /* get timrot version */ |
| 268 | timrot_major_version = __raw_readl(mxs_timrot_base + |
Shawn Guo | 220d2f2 | 2013-03-25 19:41:39 +0800 | [diff] [blame^] | 269 | (of_device_is_compatible(np, "fsl,imx23-timrot") ? |
| 270 | MX23_TIMROT_VERSION_OFFSET : |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 271 | MX28_TIMROT_VERSION_OFFSET)); |
| 272 | timrot_major_version >>= BP_TIMROT_MAJOR_VERSION; |
| 273 | |
| 274 | /* one for clock_event */ |
| 275 | __raw_writel((timrot_is_v1() ? |
| 276 | BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL : |
Torben Hohn | 2fb318f | 2012-12-21 15:06:15 +0100 | [diff] [blame] | 277 | BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) | |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 278 | BM_TIMROT_TIMCTRLn_UPDATE | |
| 279 | BM_TIMROT_TIMCTRLn_IRQ_EN, |
| 280 | mxs_timrot_base + HW_TIMROT_TIMCTRLn(0)); |
| 281 | |
| 282 | /* another for clocksource */ |
| 283 | __raw_writel((timrot_is_v1() ? |
| 284 | BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL : |
Torben Hohn | 2fb318f | 2012-12-21 15:06:15 +0100 | [diff] [blame] | 285 | BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) | |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 286 | BM_TIMROT_TIMCTRLn_RELOAD, |
| 287 | mxs_timrot_base + HW_TIMROT_TIMCTRLn(1)); |
| 288 | |
| 289 | /* set clocksource timer fixed count to the maximum */ |
| 290 | if (timrot_is_v1()) |
| 291 | __raw_writel(0xffff, |
| 292 | mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1)); |
| 293 | else |
| 294 | __raw_writel(0xffffffff, |
| 295 | mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1)); |
| 296 | |
| 297 | /* init and register the timer to the framework */ |
| 298 | mxs_clocksource_init(timer_clk); |
| 299 | mxs_clockevent_init(timer_clk); |
| 300 | |
| 301 | /* Make irqs happen */ |
Shawn Guo | eeca6e6 | 2012-08-20 08:51:45 +0800 | [diff] [blame] | 302 | irq = irq_of_parse_and_map(np, 0); |
Shawn Guo | 4e47209 | 2010-12-18 21:39:30 +0800 | [diff] [blame] | 303 | setup_irq(irq, &mxs_timer_irq); |
| 304 | } |
Shawn Guo | 633ef4c | 2013-03-25 14:53:08 +0800 | [diff] [blame] | 305 | CLOCKSOURCE_OF_DECLARE(mxs, "fsl,timrot", mxs_timer_init) |