Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SuperH Timer Support - CMT |
| 3 | * |
| 4 | * Copyright (C) 2008 Magnus Damm |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | #include <linux/init.h> |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/ioport.h> |
| 25 | #include <linux/io.h> |
| 26 | #include <linux/clk.h> |
| 27 | #include <linux/irq.h> |
| 28 | #include <linux/err.h> |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 29 | #include <linux/delay.h> |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 30 | #include <linux/clocksource.h> |
| 31 | #include <linux/clockchips.h> |
Paul Mundt | 46a12f7 | 2009-05-03 17:57:17 +0900 | [diff] [blame] | 32 | #include <linux/sh_timer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Paul Gortmaker | 7deeab5 | 2011-07-03 13:36:22 -0400 | [diff] [blame] | 34 | #include <linux/module.h> |
Rafael J. Wysocki | 615a445 | 2012-03-13 22:40:06 +0100 | [diff] [blame] | 35 | #include <linux/pm_domain.h> |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 36 | #include <linux/pm_runtime.h> |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 37 | |
| 38 | struct sh_cmt_priv { |
| 39 | void __iomem *mapbase; |
| 40 | struct clk *clk; |
| 41 | unsigned long width; /* 16 or 32 bit version of hardware block */ |
| 42 | unsigned long overflow_bit; |
| 43 | unsigned long clear_bits; |
| 44 | struct irqaction irqaction; |
| 45 | struct platform_device *pdev; |
| 46 | |
| 47 | unsigned long flags; |
| 48 | unsigned long match_value; |
| 49 | unsigned long next_match_value; |
| 50 | unsigned long max_match_value; |
| 51 | unsigned long rate; |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 52 | raw_spinlock_t lock; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 53 | struct clock_event_device ced; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 54 | struct clocksource cs; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 55 | unsigned long total_cycles; |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 56 | bool cs_enabled; |
Magnus Damm | a6a912c | 2012-12-14 14:54:19 +0900 | [diff] [blame] | 57 | |
Magnus Damm | cccd704 | 2012-12-14 14:54:28 +0900 | [diff] [blame^] | 58 | /* callbacks for CMSTR and CMCSR access */ |
| 59 | unsigned long (*read_control)(void __iomem *base, unsigned long offs); |
| 60 | void (*write_control)(void __iomem *base, unsigned long offs, |
| 61 | unsigned long value); |
| 62 | |
Magnus Damm | a6a912c | 2012-12-14 14:54:19 +0900 | [diff] [blame] | 63 | /* callbacks for CMCNT and CMCOR access */ |
| 64 | unsigned long (*read_count)(void __iomem *base, unsigned long offs); |
| 65 | void (*write_count)(void __iomem *base, unsigned long offs, |
| 66 | unsigned long value); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Magnus Damm | a6a912c | 2012-12-14 14:54:19 +0900 | [diff] [blame] | 69 | static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs) |
Magnus Damm | 587acb3 | 2012-12-14 14:54:10 +0900 | [diff] [blame] | 70 | { |
| 71 | return ioread16(base + (offs << 1)); |
| 72 | } |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 73 | |
Magnus Damm | a6a912c | 2012-12-14 14:54:19 +0900 | [diff] [blame] | 74 | static unsigned long sh_cmt_read32(void __iomem *base, unsigned long offs) |
| 75 | { |
| 76 | return ioread32(base + (offs << 2)); |
| 77 | } |
| 78 | |
| 79 | static void sh_cmt_write16(void __iomem *base, unsigned long offs, |
| 80 | unsigned long value) |
Magnus Damm | 587acb3 | 2012-12-14 14:54:10 +0900 | [diff] [blame] | 81 | { |
| 82 | iowrite16(value, base + (offs << 1)); |
| 83 | } |
| 84 | |
Magnus Damm | a6a912c | 2012-12-14 14:54:19 +0900 | [diff] [blame] | 85 | static void sh_cmt_write32(void __iomem *base, unsigned long offs, |
| 86 | unsigned long value) |
| 87 | { |
| 88 | iowrite32(value, base + (offs << 2)); |
| 89 | } |
| 90 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 91 | #define CMCSR 0 /* channel register */ |
| 92 | #define CMCNT 1 /* channel register */ |
| 93 | #define CMCOR 2 /* channel register */ |
| 94 | |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 95 | static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_priv *p) |
| 96 | { |
Magnus Damm | 587acb3 | 2012-12-14 14:54:10 +0900 | [diff] [blame] | 97 | struct sh_timer_config *cfg = p->pdev->dev.platform_data; |
| 98 | |
Magnus Damm | cccd704 | 2012-12-14 14:54:28 +0900 | [diff] [blame^] | 99 | return p->read_control(p->mapbase - cfg->channel_offset, 0); |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_priv *p) |
| 103 | { |
Magnus Damm | cccd704 | 2012-12-14 14:54:28 +0900 | [diff] [blame^] | 104 | return p->read_control(p->mapbase, CMCSR); |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_priv *p) |
| 108 | { |
Magnus Damm | a6a912c | 2012-12-14 14:54:19 +0900 | [diff] [blame] | 109 | return p->read_count(p->mapbase, CMCNT); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 112 | static inline void sh_cmt_write_cmstr(struct sh_cmt_priv *p, |
| 113 | unsigned long value) |
| 114 | { |
Magnus Damm | 587acb3 | 2012-12-14 14:54:10 +0900 | [diff] [blame] | 115 | struct sh_timer_config *cfg = p->pdev->dev.platform_data; |
| 116 | |
Magnus Damm | cccd704 | 2012-12-14 14:54:28 +0900 | [diff] [blame^] | 117 | p->write_control(p->mapbase - cfg->channel_offset, 0, value); |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static inline void sh_cmt_write_cmcsr(struct sh_cmt_priv *p, |
| 121 | unsigned long value) |
| 122 | { |
Magnus Damm | cccd704 | 2012-12-14 14:54:28 +0900 | [diff] [blame^] | 123 | p->write_control(p->mapbase, CMCSR, value); |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static inline void sh_cmt_write_cmcnt(struct sh_cmt_priv *p, |
| 127 | unsigned long value) |
| 128 | { |
Magnus Damm | a6a912c | 2012-12-14 14:54:19 +0900 | [diff] [blame] | 129 | p->write_count(p->mapbase, CMCNT, value); |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static inline void sh_cmt_write_cmcor(struct sh_cmt_priv *p, |
| 133 | unsigned long value) |
| 134 | { |
Magnus Damm | a6a912c | 2012-12-14 14:54:19 +0900 | [diff] [blame] | 135 | p->write_count(p->mapbase, CMCOR, value); |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 136 | } |
| 137 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 138 | static unsigned long sh_cmt_get_counter(struct sh_cmt_priv *p, |
| 139 | int *has_wrapped) |
| 140 | { |
| 141 | unsigned long v1, v2, v3; |
Magnus Damm | 5b644c7 | 2009-04-28 08:17:54 +0000 | [diff] [blame] | 142 | int o1, o2; |
| 143 | |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 144 | o1 = sh_cmt_read_cmcsr(p) & p->overflow_bit; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 145 | |
| 146 | /* Make sure the timer value is stable. Stolen from acpi_pm.c */ |
| 147 | do { |
Magnus Damm | 5b644c7 | 2009-04-28 08:17:54 +0000 | [diff] [blame] | 148 | o2 = o1; |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 149 | v1 = sh_cmt_read_cmcnt(p); |
| 150 | v2 = sh_cmt_read_cmcnt(p); |
| 151 | v3 = sh_cmt_read_cmcnt(p); |
| 152 | o1 = sh_cmt_read_cmcsr(p) & p->overflow_bit; |
Magnus Damm | 5b644c7 | 2009-04-28 08:17:54 +0000 | [diff] [blame] | 153 | } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3) |
| 154 | || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2))); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 155 | |
Magnus Damm | 5b644c7 | 2009-04-28 08:17:54 +0000 | [diff] [blame] | 156 | *has_wrapped = o1; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 157 | return v2; |
| 158 | } |
| 159 | |
Magnus Damm | 587acb3 | 2012-12-14 14:54:10 +0900 | [diff] [blame] | 160 | static DEFINE_RAW_SPINLOCK(sh_cmt_lock); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 161 | |
| 162 | static void sh_cmt_start_stop_ch(struct sh_cmt_priv *p, int start) |
| 163 | { |
Paul Mundt | 46a12f7 | 2009-05-03 17:57:17 +0900 | [diff] [blame] | 164 | struct sh_timer_config *cfg = p->pdev->dev.platform_data; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 165 | unsigned long flags, value; |
| 166 | |
| 167 | /* start stop register shared by multiple timer channels */ |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 168 | raw_spin_lock_irqsave(&sh_cmt_lock, flags); |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 169 | value = sh_cmt_read_cmstr(p); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 170 | |
| 171 | if (start) |
| 172 | value |= 1 << cfg->timer_bit; |
| 173 | else |
| 174 | value &= ~(1 << cfg->timer_bit); |
| 175 | |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 176 | sh_cmt_write_cmstr(p, value); |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 177 | raw_spin_unlock_irqrestore(&sh_cmt_lock, flags); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static int sh_cmt_enable(struct sh_cmt_priv *p, unsigned long *rate) |
| 181 | { |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 182 | int k, ret; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 183 | |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 184 | pm_runtime_get_sync(&p->pdev->dev); |
| 185 | dev_pm_syscore_device(&p->pdev->dev, true); |
| 186 | |
Paul Mundt | 9436b4a | 2011-05-31 15:26:42 +0900 | [diff] [blame] | 187 | /* enable clock */ |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 188 | ret = clk_enable(p->clk); |
| 189 | if (ret) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 190 | dev_err(&p->pdev->dev, "cannot enable clock\n"); |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 191 | goto err0; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 192 | } |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 193 | |
| 194 | /* make sure channel is disabled */ |
| 195 | sh_cmt_start_stop_ch(p, 0); |
| 196 | |
| 197 | /* configure channel, periodic mode and maximum timeout */ |
Magnus Damm | 3014f47 | 2009-04-29 14:50:37 +0000 | [diff] [blame] | 198 | if (p->width == 16) { |
| 199 | *rate = clk_get_rate(p->clk) / 512; |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 200 | sh_cmt_write_cmcsr(p, 0x43); |
Magnus Damm | 3014f47 | 2009-04-29 14:50:37 +0000 | [diff] [blame] | 201 | } else { |
| 202 | *rate = clk_get_rate(p->clk) / 8; |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 203 | sh_cmt_write_cmcsr(p, 0x01a4); |
Magnus Damm | 3014f47 | 2009-04-29 14:50:37 +0000 | [diff] [blame] | 204 | } |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 205 | |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 206 | sh_cmt_write_cmcor(p, 0xffffffff); |
| 207 | sh_cmt_write_cmcnt(p, 0); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 208 | |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 209 | /* |
| 210 | * According to the sh73a0 user's manual, as CMCNT can be operated |
| 211 | * only by the RCLK (Pseudo 32 KHz), there's one restriction on |
| 212 | * modifying CMCNT register; two RCLK cycles are necessary before |
| 213 | * this register is either read or any modification of the value |
| 214 | * it holds is reflected in the LSI's actual operation. |
| 215 | * |
| 216 | * While at it, we're supposed to clear out the CMCNT as of this |
| 217 | * moment, so make sure it's processed properly here. This will |
| 218 | * take RCLKx2 at maximum. |
| 219 | */ |
| 220 | for (k = 0; k < 100; k++) { |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 221 | if (!sh_cmt_read_cmcnt(p)) |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 222 | break; |
| 223 | udelay(1); |
| 224 | } |
| 225 | |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 226 | if (sh_cmt_read_cmcnt(p)) { |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 227 | dev_err(&p->pdev->dev, "cannot clear CMCNT\n"); |
| 228 | ret = -ETIMEDOUT; |
| 229 | goto err1; |
| 230 | } |
| 231 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 232 | /* enable channel */ |
| 233 | sh_cmt_start_stop_ch(p, 1); |
| 234 | return 0; |
Magnus Damm | 3f7e5e2 | 2011-07-13 07:59:48 +0000 | [diff] [blame] | 235 | err1: |
| 236 | /* stop clock */ |
| 237 | clk_disable(p->clk); |
| 238 | |
| 239 | err0: |
| 240 | return ret; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static void sh_cmt_disable(struct sh_cmt_priv *p) |
| 244 | { |
| 245 | /* disable channel */ |
| 246 | sh_cmt_start_stop_ch(p, 0); |
| 247 | |
Magnus Damm | be890a1 | 2009-06-17 05:04:04 +0000 | [diff] [blame] | 248 | /* disable interrupts in CMT block */ |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 249 | sh_cmt_write_cmcsr(p, 0); |
Magnus Damm | be890a1 | 2009-06-17 05:04:04 +0000 | [diff] [blame] | 250 | |
Paul Mundt | 9436b4a | 2011-05-31 15:26:42 +0900 | [diff] [blame] | 251 | /* stop clock */ |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 252 | clk_disable(p->clk); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 253 | |
| 254 | dev_pm_syscore_device(&p->pdev->dev, false); |
| 255 | pm_runtime_put(&p->pdev->dev); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* private flags */ |
| 259 | #define FLAG_CLOCKEVENT (1 << 0) |
| 260 | #define FLAG_CLOCKSOURCE (1 << 1) |
| 261 | #define FLAG_REPROGRAM (1 << 2) |
| 262 | #define FLAG_SKIPEVENT (1 << 3) |
| 263 | #define FLAG_IRQCONTEXT (1 << 4) |
| 264 | |
| 265 | static void sh_cmt_clock_event_program_verify(struct sh_cmt_priv *p, |
| 266 | int absolute) |
| 267 | { |
| 268 | unsigned long new_match; |
| 269 | unsigned long value = p->next_match_value; |
| 270 | unsigned long delay = 0; |
| 271 | unsigned long now = 0; |
| 272 | int has_wrapped; |
| 273 | |
| 274 | now = sh_cmt_get_counter(p, &has_wrapped); |
| 275 | p->flags |= FLAG_REPROGRAM; /* force reprogram */ |
| 276 | |
| 277 | if (has_wrapped) { |
| 278 | /* we're competing with the interrupt handler. |
| 279 | * -> let the interrupt handler reprogram the timer. |
| 280 | * -> interrupt number two handles the event. |
| 281 | */ |
| 282 | p->flags |= FLAG_SKIPEVENT; |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | if (absolute) |
| 287 | now = 0; |
| 288 | |
| 289 | do { |
| 290 | /* reprogram the timer hardware, |
| 291 | * but don't save the new match value yet. |
| 292 | */ |
| 293 | new_match = now + value + delay; |
| 294 | if (new_match > p->max_match_value) |
| 295 | new_match = p->max_match_value; |
| 296 | |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 297 | sh_cmt_write_cmcor(p, new_match); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 298 | |
| 299 | now = sh_cmt_get_counter(p, &has_wrapped); |
| 300 | if (has_wrapped && (new_match > p->match_value)) { |
| 301 | /* we are changing to a greater match value, |
| 302 | * so this wrap must be caused by the counter |
| 303 | * matching the old value. |
| 304 | * -> first interrupt reprograms the timer. |
| 305 | * -> interrupt number two handles the event. |
| 306 | */ |
| 307 | p->flags |= FLAG_SKIPEVENT; |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | if (has_wrapped) { |
| 312 | /* we are changing to a smaller match value, |
| 313 | * so the wrap must be caused by the counter |
| 314 | * matching the new value. |
| 315 | * -> save programmed match value. |
| 316 | * -> let isr handle the event. |
| 317 | */ |
| 318 | p->match_value = new_match; |
| 319 | break; |
| 320 | } |
| 321 | |
| 322 | /* be safe: verify hardware settings */ |
| 323 | if (now < new_match) { |
| 324 | /* timer value is below match value, all good. |
| 325 | * this makes sure we won't miss any match events. |
| 326 | * -> save programmed match value. |
| 327 | * -> let isr handle the event. |
| 328 | */ |
| 329 | p->match_value = new_match; |
| 330 | break; |
| 331 | } |
| 332 | |
| 333 | /* the counter has reached a value greater |
| 334 | * than our new match value. and since the |
| 335 | * has_wrapped flag isn't set we must have |
| 336 | * programmed a too close event. |
| 337 | * -> increase delay and retry. |
| 338 | */ |
| 339 | if (delay) |
| 340 | delay <<= 1; |
| 341 | else |
| 342 | delay = 1; |
| 343 | |
| 344 | if (!delay) |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 345 | dev_warn(&p->pdev->dev, "too long delay\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 346 | |
| 347 | } while (delay); |
| 348 | } |
| 349 | |
Takashi YOSHII | 65ada54 | 2010-12-17 07:25:09 +0000 | [diff] [blame] | 350 | static void __sh_cmt_set_next(struct sh_cmt_priv *p, unsigned long delta) |
| 351 | { |
| 352 | if (delta > p->max_match_value) |
| 353 | dev_warn(&p->pdev->dev, "delta out of range\n"); |
| 354 | |
| 355 | p->next_match_value = delta; |
| 356 | sh_cmt_clock_event_program_verify(p, 0); |
| 357 | } |
| 358 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 359 | static void sh_cmt_set_next(struct sh_cmt_priv *p, unsigned long delta) |
| 360 | { |
| 361 | unsigned long flags; |
| 362 | |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 363 | raw_spin_lock_irqsave(&p->lock, flags); |
Takashi YOSHII | 65ada54 | 2010-12-17 07:25:09 +0000 | [diff] [blame] | 364 | __sh_cmt_set_next(p, delta); |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 365 | raw_spin_unlock_irqrestore(&p->lock, flags); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id) |
| 369 | { |
| 370 | struct sh_cmt_priv *p = dev_id; |
| 371 | |
| 372 | /* clear flags */ |
Magnus Damm | 1b56b96 | 2012-12-14 14:54:00 +0900 | [diff] [blame] | 373 | sh_cmt_write_cmcsr(p, sh_cmt_read_cmcsr(p) & p->clear_bits); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 374 | |
| 375 | /* update clock source counter to begin with if enabled |
| 376 | * the wrap flag should be cleared by the timer specific |
| 377 | * isr before we end up here. |
| 378 | */ |
| 379 | if (p->flags & FLAG_CLOCKSOURCE) |
Magnus Damm | 4380947 | 2010-08-04 04:31:38 +0000 | [diff] [blame] | 380 | p->total_cycles += p->match_value + 1; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 381 | |
| 382 | if (!(p->flags & FLAG_REPROGRAM)) |
| 383 | p->next_match_value = p->max_match_value; |
| 384 | |
| 385 | p->flags |= FLAG_IRQCONTEXT; |
| 386 | |
| 387 | if (p->flags & FLAG_CLOCKEVENT) { |
| 388 | if (!(p->flags & FLAG_SKIPEVENT)) { |
| 389 | if (p->ced.mode == CLOCK_EVT_MODE_ONESHOT) { |
| 390 | p->next_match_value = p->max_match_value; |
| 391 | p->flags |= FLAG_REPROGRAM; |
| 392 | } |
| 393 | |
| 394 | p->ced.event_handler(&p->ced); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | p->flags &= ~FLAG_SKIPEVENT; |
| 399 | |
| 400 | if (p->flags & FLAG_REPROGRAM) { |
| 401 | p->flags &= ~FLAG_REPROGRAM; |
| 402 | sh_cmt_clock_event_program_verify(p, 1); |
| 403 | |
| 404 | if (p->flags & FLAG_CLOCKEVENT) |
| 405 | if ((p->ced.mode == CLOCK_EVT_MODE_SHUTDOWN) |
| 406 | || (p->match_value == p->next_match_value)) |
| 407 | p->flags &= ~FLAG_REPROGRAM; |
| 408 | } |
| 409 | |
| 410 | p->flags &= ~FLAG_IRQCONTEXT; |
| 411 | |
| 412 | return IRQ_HANDLED; |
| 413 | } |
| 414 | |
| 415 | static int sh_cmt_start(struct sh_cmt_priv *p, unsigned long flag) |
| 416 | { |
| 417 | int ret = 0; |
| 418 | unsigned long flags; |
| 419 | |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 420 | raw_spin_lock_irqsave(&p->lock, flags); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 421 | |
| 422 | if (!(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE))) |
| 423 | ret = sh_cmt_enable(p, &p->rate); |
| 424 | |
| 425 | if (ret) |
| 426 | goto out; |
| 427 | p->flags |= flag; |
| 428 | |
| 429 | /* setup timeout if no clockevent */ |
| 430 | if ((flag == FLAG_CLOCKSOURCE) && (!(p->flags & FLAG_CLOCKEVENT))) |
Takashi YOSHII | 65ada54 | 2010-12-17 07:25:09 +0000 | [diff] [blame] | 431 | __sh_cmt_set_next(p, p->max_match_value); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 432 | out: |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 433 | raw_spin_unlock_irqrestore(&p->lock, flags); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 434 | |
| 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | static void sh_cmt_stop(struct sh_cmt_priv *p, unsigned long flag) |
| 439 | { |
| 440 | unsigned long flags; |
| 441 | unsigned long f; |
| 442 | |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 443 | raw_spin_lock_irqsave(&p->lock, flags); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 444 | |
| 445 | f = p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE); |
| 446 | p->flags &= ~flag; |
| 447 | |
| 448 | if (f && !(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE))) |
| 449 | sh_cmt_disable(p); |
| 450 | |
| 451 | /* adjust the timeout to maximum if only clocksource left */ |
| 452 | if ((flag == FLAG_CLOCKEVENT) && (p->flags & FLAG_CLOCKSOURCE)) |
Takashi YOSHII | 65ada54 | 2010-12-17 07:25:09 +0000 | [diff] [blame] | 453 | __sh_cmt_set_next(p, p->max_match_value); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 454 | |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 455 | raw_spin_unlock_irqrestore(&p->lock, flags); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 458 | static struct sh_cmt_priv *cs_to_sh_cmt(struct clocksource *cs) |
| 459 | { |
| 460 | return container_of(cs, struct sh_cmt_priv, cs); |
| 461 | } |
| 462 | |
| 463 | static cycle_t sh_cmt_clocksource_read(struct clocksource *cs) |
| 464 | { |
| 465 | struct sh_cmt_priv *p = cs_to_sh_cmt(cs); |
| 466 | unsigned long flags, raw; |
| 467 | unsigned long value; |
| 468 | int has_wrapped; |
| 469 | |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 470 | raw_spin_lock_irqsave(&p->lock, flags); |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 471 | value = p->total_cycles; |
| 472 | raw = sh_cmt_get_counter(p, &has_wrapped); |
| 473 | |
| 474 | if (unlikely(has_wrapped)) |
Magnus Damm | 4380947 | 2010-08-04 04:31:38 +0000 | [diff] [blame] | 475 | raw += p->match_value + 1; |
Paul Mundt | 7d0c399 | 2012-05-25 13:36:43 +0900 | [diff] [blame] | 476 | raw_spin_unlock_irqrestore(&p->lock, flags); |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 477 | |
| 478 | return value + raw; |
| 479 | } |
| 480 | |
| 481 | static int sh_cmt_clocksource_enable(struct clocksource *cs) |
| 482 | { |
Magnus Damm | 3593f5f | 2011-04-25 22:32:11 +0900 | [diff] [blame] | 483 | int ret; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 484 | struct sh_cmt_priv *p = cs_to_sh_cmt(cs); |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 485 | |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 486 | WARN_ON(p->cs_enabled); |
| 487 | |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 488 | p->total_cycles = 0; |
| 489 | |
Magnus Damm | 3593f5f | 2011-04-25 22:32:11 +0900 | [diff] [blame] | 490 | ret = sh_cmt_start(p, FLAG_CLOCKSOURCE); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 491 | if (!ret) { |
Magnus Damm | 3593f5f | 2011-04-25 22:32:11 +0900 | [diff] [blame] | 492 | __clocksource_updatefreq_hz(cs, p->rate); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 493 | p->cs_enabled = true; |
| 494 | } |
Magnus Damm | 3593f5f | 2011-04-25 22:32:11 +0900 | [diff] [blame] | 495 | return ret; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | static void sh_cmt_clocksource_disable(struct clocksource *cs) |
| 499 | { |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 500 | struct sh_cmt_priv *p = cs_to_sh_cmt(cs); |
| 501 | |
| 502 | WARN_ON(!p->cs_enabled); |
| 503 | |
| 504 | sh_cmt_stop(p, FLAG_CLOCKSOURCE); |
| 505 | p->cs_enabled = false; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 508 | static void sh_cmt_clocksource_suspend(struct clocksource *cs) |
| 509 | { |
| 510 | struct sh_cmt_priv *p = cs_to_sh_cmt(cs); |
| 511 | |
| 512 | sh_cmt_stop(p, FLAG_CLOCKSOURCE); |
| 513 | pm_genpd_syscore_poweroff(&p->pdev->dev); |
| 514 | } |
| 515 | |
Magnus Damm | c816288 | 2010-02-02 14:41:40 -0800 | [diff] [blame] | 516 | static void sh_cmt_clocksource_resume(struct clocksource *cs) |
| 517 | { |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 518 | struct sh_cmt_priv *p = cs_to_sh_cmt(cs); |
| 519 | |
| 520 | pm_genpd_syscore_poweron(&p->pdev->dev); |
| 521 | sh_cmt_start(p, FLAG_CLOCKSOURCE); |
Magnus Damm | c816288 | 2010-02-02 14:41:40 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 524 | static int sh_cmt_register_clocksource(struct sh_cmt_priv *p, |
| 525 | char *name, unsigned long rating) |
| 526 | { |
| 527 | struct clocksource *cs = &p->cs; |
| 528 | |
| 529 | cs->name = name; |
| 530 | cs->rating = rating; |
| 531 | cs->read = sh_cmt_clocksource_read; |
| 532 | cs->enable = sh_cmt_clocksource_enable; |
| 533 | cs->disable = sh_cmt_clocksource_disable; |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 534 | cs->suspend = sh_cmt_clocksource_suspend; |
Magnus Damm | c816288 | 2010-02-02 14:41:40 -0800 | [diff] [blame] | 535 | cs->resume = sh_cmt_clocksource_resume; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 536 | cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8); |
| 537 | cs->flags = CLOCK_SOURCE_IS_CONTINUOUS; |
Paul Mundt | f4d7c35 | 2010-06-02 17:10:44 +0900 | [diff] [blame] | 538 | |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 539 | dev_info(&p->pdev->dev, "used as clock source\n"); |
Paul Mundt | f4d7c35 | 2010-06-02 17:10:44 +0900 | [diff] [blame] | 540 | |
Magnus Damm | 3593f5f | 2011-04-25 22:32:11 +0900 | [diff] [blame] | 541 | /* Register with dummy 1 Hz value, gets updated in ->enable() */ |
| 542 | clocksource_register_hz(cs, 1); |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 543 | return 0; |
| 544 | } |
| 545 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 546 | static struct sh_cmt_priv *ced_to_sh_cmt(struct clock_event_device *ced) |
| 547 | { |
| 548 | return container_of(ced, struct sh_cmt_priv, ced); |
| 549 | } |
| 550 | |
| 551 | static void sh_cmt_clock_event_start(struct sh_cmt_priv *p, int periodic) |
| 552 | { |
| 553 | struct clock_event_device *ced = &p->ced; |
| 554 | |
| 555 | sh_cmt_start(p, FLAG_CLOCKEVENT); |
| 556 | |
| 557 | /* TODO: calculate good shift from rate and counter bit width */ |
| 558 | |
| 559 | ced->shift = 32; |
| 560 | ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift); |
| 561 | ced->max_delta_ns = clockevent_delta2ns(p->max_match_value, ced); |
| 562 | ced->min_delta_ns = clockevent_delta2ns(0x1f, ced); |
| 563 | |
| 564 | if (periodic) |
Magnus Damm | 4380947 | 2010-08-04 04:31:38 +0000 | [diff] [blame] | 565 | sh_cmt_set_next(p, ((p->rate + HZ/2) / HZ) - 1); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 566 | else |
| 567 | sh_cmt_set_next(p, p->max_match_value); |
| 568 | } |
| 569 | |
| 570 | static void sh_cmt_clock_event_mode(enum clock_event_mode mode, |
| 571 | struct clock_event_device *ced) |
| 572 | { |
| 573 | struct sh_cmt_priv *p = ced_to_sh_cmt(ced); |
| 574 | |
| 575 | /* deal with old setting first */ |
| 576 | switch (ced->mode) { |
| 577 | case CLOCK_EVT_MODE_PERIODIC: |
| 578 | case CLOCK_EVT_MODE_ONESHOT: |
| 579 | sh_cmt_stop(p, FLAG_CLOCKEVENT); |
| 580 | break; |
| 581 | default: |
| 582 | break; |
| 583 | } |
| 584 | |
| 585 | switch (mode) { |
| 586 | case CLOCK_EVT_MODE_PERIODIC: |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 587 | dev_info(&p->pdev->dev, "used for periodic clock events\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 588 | sh_cmt_clock_event_start(p, 1); |
| 589 | break; |
| 590 | case CLOCK_EVT_MODE_ONESHOT: |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 591 | dev_info(&p->pdev->dev, "used for oneshot clock events\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 592 | sh_cmt_clock_event_start(p, 0); |
| 593 | break; |
| 594 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 595 | case CLOCK_EVT_MODE_UNUSED: |
| 596 | sh_cmt_stop(p, FLAG_CLOCKEVENT); |
| 597 | break; |
| 598 | default: |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | static int sh_cmt_clock_event_next(unsigned long delta, |
| 604 | struct clock_event_device *ced) |
| 605 | { |
| 606 | struct sh_cmt_priv *p = ced_to_sh_cmt(ced); |
| 607 | |
| 608 | BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT); |
| 609 | if (likely(p->flags & FLAG_IRQCONTEXT)) |
Magnus Damm | 4380947 | 2010-08-04 04:31:38 +0000 | [diff] [blame] | 610 | p->next_match_value = delta - 1; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 611 | else |
Magnus Damm | 4380947 | 2010-08-04 04:31:38 +0000 | [diff] [blame] | 612 | sh_cmt_set_next(p, delta - 1); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 617 | static void sh_cmt_clock_event_suspend(struct clock_event_device *ced) |
| 618 | { |
| 619 | pm_genpd_syscore_poweroff(&ced_to_sh_cmt(ced)->pdev->dev); |
| 620 | } |
| 621 | |
| 622 | static void sh_cmt_clock_event_resume(struct clock_event_device *ced) |
| 623 | { |
| 624 | pm_genpd_syscore_poweron(&ced_to_sh_cmt(ced)->pdev->dev); |
| 625 | } |
| 626 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 627 | static void sh_cmt_register_clockevent(struct sh_cmt_priv *p, |
| 628 | char *name, unsigned long rating) |
| 629 | { |
| 630 | struct clock_event_device *ced = &p->ced; |
| 631 | |
| 632 | memset(ced, 0, sizeof(*ced)); |
| 633 | |
| 634 | ced->name = name; |
| 635 | ced->features = CLOCK_EVT_FEAT_PERIODIC; |
| 636 | ced->features |= CLOCK_EVT_FEAT_ONESHOT; |
| 637 | ced->rating = rating; |
| 638 | ced->cpumask = cpumask_of(0); |
| 639 | ced->set_next_event = sh_cmt_clock_event_next; |
| 640 | ced->set_mode = sh_cmt_clock_event_mode; |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 641 | ced->suspend = sh_cmt_clock_event_suspend; |
| 642 | ced->resume = sh_cmt_clock_event_resume; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 643 | |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 644 | dev_info(&p->pdev->dev, "used for clock events\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 645 | clockevents_register_device(ced); |
| 646 | } |
| 647 | |
Paul Mundt | d1fcc0a | 2009-05-03 18:05:42 +0900 | [diff] [blame] | 648 | static int sh_cmt_register(struct sh_cmt_priv *p, char *name, |
| 649 | unsigned long clockevent_rating, |
| 650 | unsigned long clocksource_rating) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 651 | { |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 652 | if (clockevent_rating) |
| 653 | sh_cmt_register_clockevent(p, name, clockevent_rating); |
| 654 | |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 655 | if (clocksource_rating) |
| 656 | sh_cmt_register_clocksource(p, name, clocksource_rating); |
| 657 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static int sh_cmt_setup(struct sh_cmt_priv *p, struct platform_device *pdev) |
| 662 | { |
Paul Mundt | 46a12f7 | 2009-05-03 17:57:17 +0900 | [diff] [blame] | 663 | struct sh_timer_config *cfg = pdev->dev.platform_data; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 664 | struct resource *res; |
| 665 | int irq, ret; |
| 666 | ret = -ENXIO; |
| 667 | |
| 668 | memset(p, 0, sizeof(*p)); |
| 669 | p->pdev = pdev; |
| 670 | |
| 671 | if (!cfg) { |
| 672 | dev_err(&p->pdev->dev, "missing platform data\n"); |
| 673 | goto err0; |
| 674 | } |
| 675 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 676 | res = platform_get_resource(p->pdev, IORESOURCE_MEM, 0); |
| 677 | if (!res) { |
| 678 | dev_err(&p->pdev->dev, "failed to get I/O memory\n"); |
| 679 | goto err0; |
| 680 | } |
| 681 | |
| 682 | irq = platform_get_irq(p->pdev, 0); |
| 683 | if (irq < 0) { |
| 684 | dev_err(&p->pdev->dev, "failed to get irq\n"); |
| 685 | goto err0; |
| 686 | } |
| 687 | |
| 688 | /* map memory, let mapbase point to our channel */ |
| 689 | p->mapbase = ioremap_nocache(res->start, resource_size(res)); |
| 690 | if (p->mapbase == NULL) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 691 | dev_err(&p->pdev->dev, "failed to remap I/O memory\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 692 | goto err0; |
| 693 | } |
| 694 | |
| 695 | /* request irq using setup_irq() (too early for request_irq()) */ |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 696 | p->irqaction.name = dev_name(&p->pdev->dev); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 697 | p->irqaction.handler = sh_cmt_interrupt; |
| 698 | p->irqaction.dev_id = p; |
Paul Mundt | fecf066 | 2010-04-15 11:59:28 +0900 | [diff] [blame] | 699 | p->irqaction.flags = IRQF_DISABLED | IRQF_TIMER | \ |
| 700 | IRQF_IRQPOLL | IRQF_NOBALANCING; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 701 | |
| 702 | /* get hold of clock */ |
Paul Mundt | c2a25e8 | 2010-03-29 16:55:43 +0900 | [diff] [blame] | 703 | p->clk = clk_get(&p->pdev->dev, "cmt_fck"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 704 | if (IS_ERR(p->clk)) { |
Magnus Damm | 03ff858 | 2010-10-13 07:36:38 +0000 | [diff] [blame] | 705 | dev_err(&p->pdev->dev, "cannot get clock\n"); |
| 706 | ret = PTR_ERR(p->clk); |
| 707 | goto err1; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Magnus Damm | cccd704 | 2012-12-14 14:54:28 +0900 | [diff] [blame^] | 710 | p->read_control = sh_cmt_read16; |
| 711 | p->write_control = sh_cmt_write16; |
| 712 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 713 | if (resource_size(res) == 6) { |
| 714 | p->width = 16; |
Magnus Damm | a6a912c | 2012-12-14 14:54:19 +0900 | [diff] [blame] | 715 | p->read_count = sh_cmt_read16; |
| 716 | p->write_count = sh_cmt_write16; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 717 | p->overflow_bit = 0x80; |
Magnus Damm | 3014f47 | 2009-04-29 14:50:37 +0000 | [diff] [blame] | 718 | p->clear_bits = ~0x80; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 719 | } else { |
| 720 | p->width = 32; |
Magnus Damm | a6a912c | 2012-12-14 14:54:19 +0900 | [diff] [blame] | 721 | p->read_count = sh_cmt_read32; |
| 722 | p->write_count = sh_cmt_write32; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 723 | p->overflow_bit = 0x8000; |
| 724 | p->clear_bits = ~0xc000; |
| 725 | } |
| 726 | |
Magnus Damm | 44a10f9 | 2012-12-14 14:53:41 +0900 | [diff] [blame] | 727 | if (p->width == (sizeof(p->max_match_value) * 8)) |
| 728 | p->max_match_value = ~0; |
| 729 | else |
| 730 | p->max_match_value = (1 << p->width) - 1; |
| 731 | |
| 732 | p->match_value = p->max_match_value; |
| 733 | raw_spin_lock_init(&p->lock); |
| 734 | |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 735 | ret = sh_cmt_register(p, (char *)dev_name(&p->pdev->dev), |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 736 | cfg->clockevent_rating, |
| 737 | cfg->clocksource_rating); |
| 738 | if (ret) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 739 | dev_err(&p->pdev->dev, "registration failed\n"); |
Magnus Damm | 2fd61b3 | 2012-12-14 14:53:32 +0900 | [diff] [blame] | 740 | goto err2; |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 741 | } |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 742 | p->cs_enabled = false; |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 743 | |
| 744 | ret = setup_irq(irq, &p->irqaction); |
| 745 | if (ret) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 746 | dev_err(&p->pdev->dev, "failed to request irq %d\n", irq); |
Magnus Damm | 2fd61b3 | 2012-12-14 14:53:32 +0900 | [diff] [blame] | 747 | goto err2; |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 748 | } |
| 749 | |
Magnus Damm | adccc69 | 2012-12-14 14:53:51 +0900 | [diff] [blame] | 750 | platform_set_drvdata(pdev, p); |
| 751 | |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 752 | return 0; |
Magnus Damm | 2fd61b3 | 2012-12-14 14:53:32 +0900 | [diff] [blame] | 753 | err2: |
| 754 | clk_put(p->clk); |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 755 | |
| 756 | err1: |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 757 | iounmap(p->mapbase); |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 758 | err0: |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 759 | return ret; |
| 760 | } |
| 761 | |
Greg Kroah-Hartman | 1850514 | 2012-12-21 15:11:38 -0800 | [diff] [blame] | 762 | static int sh_cmt_probe(struct platform_device *pdev) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 763 | { |
| 764 | struct sh_cmt_priv *p = platform_get_drvdata(pdev); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 765 | struct sh_timer_config *cfg = pdev->dev.platform_data; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 766 | int ret; |
| 767 | |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 768 | if (!is_early_platform_device(pdev)) { |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 769 | pm_runtime_set_active(&pdev->dev); |
| 770 | pm_runtime_enable(&pdev->dev); |
Rafael J. Wysocki | 9bb5ec8 | 2012-08-06 01:43:03 +0200 | [diff] [blame] | 771 | } |
Rafael J. Wysocki | 615a445 | 2012-03-13 22:40:06 +0100 | [diff] [blame] | 772 | |
Magnus Damm | e475eed | 2009-04-15 10:50:04 +0000 | [diff] [blame] | 773 | if (p) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 774 | dev_info(&pdev->dev, "kept as earlytimer\n"); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 775 | goto out; |
Magnus Damm | e475eed | 2009-04-15 10:50:04 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Magnus Damm | 8e0b842 | 2009-04-28 08:19:50 +0000 | [diff] [blame] | 778 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 779 | if (p == NULL) { |
| 780 | dev_err(&pdev->dev, "failed to allocate driver data\n"); |
| 781 | return -ENOMEM; |
| 782 | } |
| 783 | |
| 784 | ret = sh_cmt_setup(p, pdev); |
| 785 | if (ret) { |
Magnus Damm | 8e0b842 | 2009-04-28 08:19:50 +0000 | [diff] [blame] | 786 | kfree(p); |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 787 | pm_runtime_idle(&pdev->dev); |
| 788 | return ret; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 789 | } |
Rafael J. Wysocki | bad8138 | 2012-08-06 01:48:57 +0200 | [diff] [blame] | 790 | if (is_early_platform_device(pdev)) |
| 791 | return 0; |
| 792 | |
| 793 | out: |
| 794 | if (cfg->clockevent_rating || cfg->clocksource_rating) |
| 795 | pm_runtime_irq_safe(&pdev->dev); |
| 796 | else |
| 797 | pm_runtime_idle(&pdev->dev); |
| 798 | |
| 799 | return 0; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Greg Kroah-Hartman | 1850514 | 2012-12-21 15:11:38 -0800 | [diff] [blame] | 802 | static int sh_cmt_remove(struct platform_device *pdev) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 803 | { |
| 804 | return -EBUSY; /* cannot unregister clockevent and clocksource */ |
| 805 | } |
| 806 | |
| 807 | static struct platform_driver sh_cmt_device_driver = { |
| 808 | .probe = sh_cmt_probe, |
Greg Kroah-Hartman | 1850514 | 2012-12-21 15:11:38 -0800 | [diff] [blame] | 809 | .remove = sh_cmt_remove, |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 810 | .driver = { |
| 811 | .name = "sh_cmt", |
| 812 | } |
| 813 | }; |
| 814 | |
| 815 | static int __init sh_cmt_init(void) |
| 816 | { |
| 817 | return platform_driver_register(&sh_cmt_device_driver); |
| 818 | } |
| 819 | |
| 820 | static void __exit sh_cmt_exit(void) |
| 821 | { |
| 822 | platform_driver_unregister(&sh_cmt_device_driver); |
| 823 | } |
| 824 | |
Magnus Damm | e475eed | 2009-04-15 10:50:04 +0000 | [diff] [blame] | 825 | early_platform_init("earlytimer", &sh_cmt_device_driver); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 826 | module_init(sh_cmt_init); |
| 827 | module_exit(sh_cmt_exit); |
| 828 | |
| 829 | MODULE_AUTHOR("Magnus Damm"); |
| 830 | MODULE_DESCRIPTION("SuperH CMT Timer Driver"); |
| 831 | MODULE_LICENSE("GPL v2"); |