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> |
| 29 | #include <linux/clocksource.h> |
| 30 | #include <linux/clockchips.h> |
Paul Mundt | 46a12f7 | 2009-05-03 17:57:17 +0900 | [diff] [blame] | 31 | #include <linux/sh_timer.h> |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 32 | |
| 33 | struct sh_cmt_priv { |
| 34 | void __iomem *mapbase; |
| 35 | struct clk *clk; |
| 36 | unsigned long width; /* 16 or 32 bit version of hardware block */ |
| 37 | unsigned long overflow_bit; |
| 38 | unsigned long clear_bits; |
| 39 | struct irqaction irqaction; |
| 40 | struct platform_device *pdev; |
| 41 | |
| 42 | unsigned long flags; |
| 43 | unsigned long match_value; |
| 44 | unsigned long next_match_value; |
| 45 | unsigned long max_match_value; |
| 46 | unsigned long rate; |
| 47 | spinlock_t lock; |
| 48 | struct clock_event_device ced; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 49 | struct clocksource cs; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 50 | unsigned long total_cycles; |
| 51 | }; |
| 52 | |
| 53 | static DEFINE_SPINLOCK(sh_cmt_lock); |
| 54 | |
| 55 | #define CMSTR -1 /* shared register */ |
| 56 | #define CMCSR 0 /* channel register */ |
| 57 | #define CMCNT 1 /* channel register */ |
| 58 | #define CMCOR 2 /* channel register */ |
| 59 | |
| 60 | static inline unsigned long sh_cmt_read(struct sh_cmt_priv *p, int reg_nr) |
| 61 | { |
Paul Mundt | 46a12f7 | 2009-05-03 17:57:17 +0900 | [diff] [blame] | 62 | struct sh_timer_config *cfg = p->pdev->dev.platform_data; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 63 | void __iomem *base = p->mapbase; |
| 64 | unsigned long offs; |
| 65 | |
| 66 | if (reg_nr == CMSTR) { |
| 67 | offs = 0; |
| 68 | base -= cfg->channel_offset; |
| 69 | } else |
| 70 | offs = reg_nr; |
| 71 | |
| 72 | if (p->width == 16) |
| 73 | offs <<= 1; |
| 74 | else { |
| 75 | offs <<= 2; |
| 76 | if ((reg_nr == CMCNT) || (reg_nr == CMCOR)) |
| 77 | return ioread32(base + offs); |
| 78 | } |
| 79 | |
| 80 | return ioread16(base + offs); |
| 81 | } |
| 82 | |
| 83 | static inline void sh_cmt_write(struct sh_cmt_priv *p, int reg_nr, |
| 84 | unsigned long value) |
| 85 | { |
Paul Mundt | 46a12f7 | 2009-05-03 17:57:17 +0900 | [diff] [blame] | 86 | struct sh_timer_config *cfg = p->pdev->dev.platform_data; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 87 | void __iomem *base = p->mapbase; |
| 88 | unsigned long offs; |
| 89 | |
| 90 | if (reg_nr == CMSTR) { |
| 91 | offs = 0; |
| 92 | base -= cfg->channel_offset; |
| 93 | } else |
| 94 | offs = reg_nr; |
| 95 | |
| 96 | if (p->width == 16) |
| 97 | offs <<= 1; |
| 98 | else { |
| 99 | offs <<= 2; |
| 100 | if ((reg_nr == CMCNT) || (reg_nr == CMCOR)) { |
| 101 | iowrite32(value, base + offs); |
| 102 | return; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | iowrite16(value, base + offs); |
| 107 | } |
| 108 | |
| 109 | static unsigned long sh_cmt_get_counter(struct sh_cmt_priv *p, |
| 110 | int *has_wrapped) |
| 111 | { |
| 112 | unsigned long v1, v2, v3; |
Magnus Damm | 5b644c7 | 2009-04-28 08:17:54 +0000 | [diff] [blame] | 113 | int o1, o2; |
| 114 | |
| 115 | o1 = sh_cmt_read(p, CMCSR) & p->overflow_bit; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 116 | |
| 117 | /* Make sure the timer value is stable. Stolen from acpi_pm.c */ |
| 118 | do { |
Magnus Damm | 5b644c7 | 2009-04-28 08:17:54 +0000 | [diff] [blame] | 119 | o2 = o1; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 120 | v1 = sh_cmt_read(p, CMCNT); |
| 121 | v2 = sh_cmt_read(p, CMCNT); |
| 122 | v3 = sh_cmt_read(p, CMCNT); |
Magnus Damm | 5b644c7 | 2009-04-28 08:17:54 +0000 | [diff] [blame] | 123 | o1 = sh_cmt_read(p, CMCSR) & p->overflow_bit; |
| 124 | } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3) |
| 125 | || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2))); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 126 | |
Magnus Damm | 5b644c7 | 2009-04-28 08:17:54 +0000 | [diff] [blame] | 127 | *has_wrapped = o1; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 128 | return v2; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | static void sh_cmt_start_stop_ch(struct sh_cmt_priv *p, int start) |
| 133 | { |
Paul Mundt | 46a12f7 | 2009-05-03 17:57:17 +0900 | [diff] [blame] | 134 | struct sh_timer_config *cfg = p->pdev->dev.platform_data; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 135 | unsigned long flags, value; |
| 136 | |
| 137 | /* start stop register shared by multiple timer channels */ |
| 138 | spin_lock_irqsave(&sh_cmt_lock, flags); |
| 139 | value = sh_cmt_read(p, CMSTR); |
| 140 | |
| 141 | if (start) |
| 142 | value |= 1 << cfg->timer_bit; |
| 143 | else |
| 144 | value &= ~(1 << cfg->timer_bit); |
| 145 | |
| 146 | sh_cmt_write(p, CMSTR, value); |
| 147 | spin_unlock_irqrestore(&sh_cmt_lock, flags); |
| 148 | } |
| 149 | |
| 150 | static int sh_cmt_enable(struct sh_cmt_priv *p, unsigned long *rate) |
| 151 | { |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 152 | int ret; |
| 153 | |
| 154 | /* enable clock */ |
| 155 | ret = clk_enable(p->clk); |
| 156 | if (ret) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 157 | dev_err(&p->pdev->dev, "cannot enable clock\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 158 | return ret; |
| 159 | } |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 160 | |
| 161 | /* make sure channel is disabled */ |
| 162 | sh_cmt_start_stop_ch(p, 0); |
| 163 | |
| 164 | /* configure channel, periodic mode and maximum timeout */ |
Magnus Damm | 3014f47 | 2009-04-29 14:50:37 +0000 | [diff] [blame] | 165 | if (p->width == 16) { |
| 166 | *rate = clk_get_rate(p->clk) / 512; |
| 167 | sh_cmt_write(p, CMCSR, 0x43); |
| 168 | } else { |
| 169 | *rate = clk_get_rate(p->clk) / 8; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 170 | sh_cmt_write(p, CMCSR, 0x01a4); |
Magnus Damm | 3014f47 | 2009-04-29 14:50:37 +0000 | [diff] [blame] | 171 | } |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 172 | |
| 173 | sh_cmt_write(p, CMCOR, 0xffffffff); |
| 174 | sh_cmt_write(p, CMCNT, 0); |
| 175 | |
| 176 | /* enable channel */ |
| 177 | sh_cmt_start_stop_ch(p, 1); |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static void sh_cmt_disable(struct sh_cmt_priv *p) |
| 182 | { |
| 183 | /* disable channel */ |
| 184 | sh_cmt_start_stop_ch(p, 0); |
| 185 | |
Magnus Damm | be890a1 | 2009-06-17 05:04:04 +0000 | [diff] [blame] | 186 | /* disable interrupts in CMT block */ |
| 187 | sh_cmt_write(p, CMCSR, 0); |
| 188 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 189 | /* stop clock */ |
| 190 | clk_disable(p->clk); |
| 191 | } |
| 192 | |
| 193 | /* private flags */ |
| 194 | #define FLAG_CLOCKEVENT (1 << 0) |
| 195 | #define FLAG_CLOCKSOURCE (1 << 1) |
| 196 | #define FLAG_REPROGRAM (1 << 2) |
| 197 | #define FLAG_SKIPEVENT (1 << 3) |
| 198 | #define FLAG_IRQCONTEXT (1 << 4) |
| 199 | |
| 200 | static void sh_cmt_clock_event_program_verify(struct sh_cmt_priv *p, |
| 201 | int absolute) |
| 202 | { |
| 203 | unsigned long new_match; |
| 204 | unsigned long value = p->next_match_value; |
| 205 | unsigned long delay = 0; |
| 206 | unsigned long now = 0; |
| 207 | int has_wrapped; |
| 208 | |
| 209 | now = sh_cmt_get_counter(p, &has_wrapped); |
| 210 | p->flags |= FLAG_REPROGRAM; /* force reprogram */ |
| 211 | |
| 212 | if (has_wrapped) { |
| 213 | /* we're competing with the interrupt handler. |
| 214 | * -> let the interrupt handler reprogram the timer. |
| 215 | * -> interrupt number two handles the event. |
| 216 | */ |
| 217 | p->flags |= FLAG_SKIPEVENT; |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | if (absolute) |
| 222 | now = 0; |
| 223 | |
| 224 | do { |
| 225 | /* reprogram the timer hardware, |
| 226 | * but don't save the new match value yet. |
| 227 | */ |
| 228 | new_match = now + value + delay; |
| 229 | if (new_match > p->max_match_value) |
| 230 | new_match = p->max_match_value; |
| 231 | |
| 232 | sh_cmt_write(p, CMCOR, new_match); |
| 233 | |
| 234 | now = sh_cmt_get_counter(p, &has_wrapped); |
| 235 | if (has_wrapped && (new_match > p->match_value)) { |
| 236 | /* we are changing to a greater match value, |
| 237 | * so this wrap must be caused by the counter |
| 238 | * matching the old value. |
| 239 | * -> first interrupt reprograms the timer. |
| 240 | * -> interrupt number two handles the event. |
| 241 | */ |
| 242 | p->flags |= FLAG_SKIPEVENT; |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | if (has_wrapped) { |
| 247 | /* we are changing to a smaller match value, |
| 248 | * so the wrap must be caused by the counter |
| 249 | * matching the new value. |
| 250 | * -> save programmed match value. |
| 251 | * -> let isr handle the event. |
| 252 | */ |
| 253 | p->match_value = new_match; |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | /* be safe: verify hardware settings */ |
| 258 | if (now < new_match) { |
| 259 | /* timer value is below match value, all good. |
| 260 | * this makes sure we won't miss any match events. |
| 261 | * -> save programmed match value. |
| 262 | * -> let isr handle the event. |
| 263 | */ |
| 264 | p->match_value = new_match; |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | /* the counter has reached a value greater |
| 269 | * than our new match value. and since the |
| 270 | * has_wrapped flag isn't set we must have |
| 271 | * programmed a too close event. |
| 272 | * -> increase delay and retry. |
| 273 | */ |
| 274 | if (delay) |
| 275 | delay <<= 1; |
| 276 | else |
| 277 | delay = 1; |
| 278 | |
| 279 | if (!delay) |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 280 | dev_warn(&p->pdev->dev, "too long delay\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 281 | |
| 282 | } while (delay); |
| 283 | } |
| 284 | |
| 285 | static void sh_cmt_set_next(struct sh_cmt_priv *p, unsigned long delta) |
| 286 | { |
| 287 | unsigned long flags; |
| 288 | |
| 289 | if (delta > p->max_match_value) |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 290 | dev_warn(&p->pdev->dev, "delta out of range\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 291 | |
| 292 | spin_lock_irqsave(&p->lock, flags); |
| 293 | p->next_match_value = delta; |
| 294 | sh_cmt_clock_event_program_verify(p, 0); |
| 295 | spin_unlock_irqrestore(&p->lock, flags); |
| 296 | } |
| 297 | |
| 298 | static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id) |
| 299 | { |
| 300 | struct sh_cmt_priv *p = dev_id; |
| 301 | |
| 302 | /* clear flags */ |
| 303 | sh_cmt_write(p, CMCSR, sh_cmt_read(p, CMCSR) & p->clear_bits); |
| 304 | |
| 305 | /* update clock source counter to begin with if enabled |
| 306 | * the wrap flag should be cleared by the timer specific |
| 307 | * isr before we end up here. |
| 308 | */ |
| 309 | if (p->flags & FLAG_CLOCKSOURCE) |
| 310 | p->total_cycles += p->match_value; |
| 311 | |
| 312 | if (!(p->flags & FLAG_REPROGRAM)) |
| 313 | p->next_match_value = p->max_match_value; |
| 314 | |
| 315 | p->flags |= FLAG_IRQCONTEXT; |
| 316 | |
| 317 | if (p->flags & FLAG_CLOCKEVENT) { |
| 318 | if (!(p->flags & FLAG_SKIPEVENT)) { |
| 319 | if (p->ced.mode == CLOCK_EVT_MODE_ONESHOT) { |
| 320 | p->next_match_value = p->max_match_value; |
| 321 | p->flags |= FLAG_REPROGRAM; |
| 322 | } |
| 323 | |
| 324 | p->ced.event_handler(&p->ced); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | p->flags &= ~FLAG_SKIPEVENT; |
| 329 | |
| 330 | if (p->flags & FLAG_REPROGRAM) { |
| 331 | p->flags &= ~FLAG_REPROGRAM; |
| 332 | sh_cmt_clock_event_program_verify(p, 1); |
| 333 | |
| 334 | if (p->flags & FLAG_CLOCKEVENT) |
| 335 | if ((p->ced.mode == CLOCK_EVT_MODE_SHUTDOWN) |
| 336 | || (p->match_value == p->next_match_value)) |
| 337 | p->flags &= ~FLAG_REPROGRAM; |
| 338 | } |
| 339 | |
| 340 | p->flags &= ~FLAG_IRQCONTEXT; |
| 341 | |
| 342 | return IRQ_HANDLED; |
| 343 | } |
| 344 | |
| 345 | static int sh_cmt_start(struct sh_cmt_priv *p, unsigned long flag) |
| 346 | { |
| 347 | int ret = 0; |
| 348 | unsigned long flags; |
| 349 | |
| 350 | spin_lock_irqsave(&p->lock, flags); |
| 351 | |
| 352 | if (!(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE))) |
| 353 | ret = sh_cmt_enable(p, &p->rate); |
| 354 | |
| 355 | if (ret) |
| 356 | goto out; |
| 357 | p->flags |= flag; |
| 358 | |
| 359 | /* setup timeout if no clockevent */ |
| 360 | if ((flag == FLAG_CLOCKSOURCE) && (!(p->flags & FLAG_CLOCKEVENT))) |
| 361 | sh_cmt_set_next(p, p->max_match_value); |
| 362 | out: |
| 363 | spin_unlock_irqrestore(&p->lock, flags); |
| 364 | |
| 365 | return ret; |
| 366 | } |
| 367 | |
| 368 | static void sh_cmt_stop(struct sh_cmt_priv *p, unsigned long flag) |
| 369 | { |
| 370 | unsigned long flags; |
| 371 | unsigned long f; |
| 372 | |
| 373 | spin_lock_irqsave(&p->lock, flags); |
| 374 | |
| 375 | f = p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE); |
| 376 | p->flags &= ~flag; |
| 377 | |
| 378 | if (f && !(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE))) |
| 379 | sh_cmt_disable(p); |
| 380 | |
| 381 | /* adjust the timeout to maximum if only clocksource left */ |
| 382 | if ((flag == FLAG_CLOCKEVENT) && (p->flags & FLAG_CLOCKSOURCE)) |
| 383 | sh_cmt_set_next(p, p->max_match_value); |
| 384 | |
| 385 | spin_unlock_irqrestore(&p->lock, flags); |
| 386 | } |
| 387 | |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 388 | static struct sh_cmt_priv *cs_to_sh_cmt(struct clocksource *cs) |
| 389 | { |
| 390 | return container_of(cs, struct sh_cmt_priv, cs); |
| 391 | } |
| 392 | |
| 393 | static cycle_t sh_cmt_clocksource_read(struct clocksource *cs) |
| 394 | { |
| 395 | struct sh_cmt_priv *p = cs_to_sh_cmt(cs); |
| 396 | unsigned long flags, raw; |
| 397 | unsigned long value; |
| 398 | int has_wrapped; |
| 399 | |
| 400 | spin_lock_irqsave(&p->lock, flags); |
| 401 | value = p->total_cycles; |
| 402 | raw = sh_cmt_get_counter(p, &has_wrapped); |
| 403 | |
| 404 | if (unlikely(has_wrapped)) |
Magnus Damm | 5b644c7 | 2009-04-28 08:17:54 +0000 | [diff] [blame] | 405 | raw += p->match_value; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 406 | spin_unlock_irqrestore(&p->lock, flags); |
| 407 | |
| 408 | return value + raw; |
| 409 | } |
| 410 | |
| 411 | static int sh_cmt_clocksource_enable(struct clocksource *cs) |
| 412 | { |
| 413 | struct sh_cmt_priv *p = cs_to_sh_cmt(cs); |
| 414 | int ret; |
| 415 | |
| 416 | p->total_cycles = 0; |
| 417 | |
| 418 | ret = sh_cmt_start(p, FLAG_CLOCKSOURCE); |
| 419 | if (ret) |
| 420 | return ret; |
| 421 | |
| 422 | /* TODO: calculate good shift from rate and counter bit width */ |
| 423 | cs->shift = 0; |
| 424 | cs->mult = clocksource_hz2mult(p->rate, cs->shift); |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | static void sh_cmt_clocksource_disable(struct clocksource *cs) |
| 429 | { |
| 430 | sh_cmt_stop(cs_to_sh_cmt(cs), FLAG_CLOCKSOURCE); |
| 431 | } |
| 432 | |
Magnus Damm | c816288 | 2010-02-02 14:41:40 -0800 | [diff] [blame] | 433 | static void sh_cmt_clocksource_resume(struct clocksource *cs) |
| 434 | { |
| 435 | sh_cmt_start(cs_to_sh_cmt(cs), FLAG_CLOCKSOURCE); |
| 436 | } |
| 437 | |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 438 | static int sh_cmt_register_clocksource(struct sh_cmt_priv *p, |
| 439 | char *name, unsigned long rating) |
| 440 | { |
| 441 | struct clocksource *cs = &p->cs; |
| 442 | |
| 443 | cs->name = name; |
| 444 | cs->rating = rating; |
| 445 | cs->read = sh_cmt_clocksource_read; |
| 446 | cs->enable = sh_cmt_clocksource_enable; |
| 447 | cs->disable = sh_cmt_clocksource_disable; |
Magnus Damm | c816288 | 2010-02-02 14:41:40 -0800 | [diff] [blame] | 448 | cs->suspend = sh_cmt_clocksource_disable; |
| 449 | cs->resume = sh_cmt_clocksource_resume; |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 450 | cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8); |
| 451 | cs->flags = CLOCK_SOURCE_IS_CONTINUOUS; |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 452 | dev_info(&p->pdev->dev, "used as clock source\n"); |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 453 | clocksource_register(cs); |
| 454 | return 0; |
| 455 | } |
| 456 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 457 | static struct sh_cmt_priv *ced_to_sh_cmt(struct clock_event_device *ced) |
| 458 | { |
| 459 | return container_of(ced, struct sh_cmt_priv, ced); |
| 460 | } |
| 461 | |
| 462 | static void sh_cmt_clock_event_start(struct sh_cmt_priv *p, int periodic) |
| 463 | { |
| 464 | struct clock_event_device *ced = &p->ced; |
| 465 | |
| 466 | sh_cmt_start(p, FLAG_CLOCKEVENT); |
| 467 | |
| 468 | /* TODO: calculate good shift from rate and counter bit width */ |
| 469 | |
| 470 | ced->shift = 32; |
| 471 | ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift); |
| 472 | ced->max_delta_ns = clockevent_delta2ns(p->max_match_value, ced); |
| 473 | ced->min_delta_ns = clockevent_delta2ns(0x1f, ced); |
| 474 | |
| 475 | if (periodic) |
| 476 | sh_cmt_set_next(p, (p->rate + HZ/2) / HZ); |
| 477 | else |
| 478 | sh_cmt_set_next(p, p->max_match_value); |
| 479 | } |
| 480 | |
| 481 | static void sh_cmt_clock_event_mode(enum clock_event_mode mode, |
| 482 | struct clock_event_device *ced) |
| 483 | { |
| 484 | struct sh_cmt_priv *p = ced_to_sh_cmt(ced); |
| 485 | |
| 486 | /* deal with old setting first */ |
| 487 | switch (ced->mode) { |
| 488 | case CLOCK_EVT_MODE_PERIODIC: |
| 489 | case CLOCK_EVT_MODE_ONESHOT: |
| 490 | sh_cmt_stop(p, FLAG_CLOCKEVENT); |
| 491 | break; |
| 492 | default: |
| 493 | break; |
| 494 | } |
| 495 | |
| 496 | switch (mode) { |
| 497 | case CLOCK_EVT_MODE_PERIODIC: |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 498 | dev_info(&p->pdev->dev, "used for periodic clock events\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 499 | sh_cmt_clock_event_start(p, 1); |
| 500 | break; |
| 501 | case CLOCK_EVT_MODE_ONESHOT: |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 502 | dev_info(&p->pdev->dev, "used for oneshot clock events\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 503 | sh_cmt_clock_event_start(p, 0); |
| 504 | break; |
| 505 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 506 | case CLOCK_EVT_MODE_UNUSED: |
| 507 | sh_cmt_stop(p, FLAG_CLOCKEVENT); |
| 508 | break; |
| 509 | default: |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | static int sh_cmt_clock_event_next(unsigned long delta, |
| 515 | struct clock_event_device *ced) |
| 516 | { |
| 517 | struct sh_cmt_priv *p = ced_to_sh_cmt(ced); |
| 518 | |
| 519 | BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT); |
| 520 | if (likely(p->flags & FLAG_IRQCONTEXT)) |
| 521 | p->next_match_value = delta; |
| 522 | else |
| 523 | sh_cmt_set_next(p, delta); |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | static void sh_cmt_register_clockevent(struct sh_cmt_priv *p, |
| 529 | char *name, unsigned long rating) |
| 530 | { |
| 531 | struct clock_event_device *ced = &p->ced; |
| 532 | |
| 533 | memset(ced, 0, sizeof(*ced)); |
| 534 | |
| 535 | ced->name = name; |
| 536 | ced->features = CLOCK_EVT_FEAT_PERIODIC; |
| 537 | ced->features |= CLOCK_EVT_FEAT_ONESHOT; |
| 538 | ced->rating = rating; |
| 539 | ced->cpumask = cpumask_of(0); |
| 540 | ced->set_next_event = sh_cmt_clock_event_next; |
| 541 | ced->set_mode = sh_cmt_clock_event_mode; |
| 542 | |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 543 | dev_info(&p->pdev->dev, "used for clock events\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 544 | clockevents_register_device(ced); |
| 545 | } |
| 546 | |
Paul Mundt | d1fcc0a | 2009-05-03 18:05:42 +0900 | [diff] [blame] | 547 | static int sh_cmt_register(struct sh_cmt_priv *p, char *name, |
| 548 | unsigned long clockevent_rating, |
| 549 | unsigned long clocksource_rating) |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 550 | { |
| 551 | if (p->width == (sizeof(p->max_match_value) * 8)) |
| 552 | p->max_match_value = ~0; |
| 553 | else |
| 554 | p->max_match_value = (1 << p->width) - 1; |
| 555 | |
| 556 | p->match_value = p->max_match_value; |
| 557 | spin_lock_init(&p->lock); |
| 558 | |
| 559 | if (clockevent_rating) |
| 560 | sh_cmt_register_clockevent(p, name, clockevent_rating); |
| 561 | |
Magnus Damm | 19bdc9d | 2009-04-17 05:26:31 +0000 | [diff] [blame] | 562 | if (clocksource_rating) |
| 563 | sh_cmt_register_clocksource(p, name, clocksource_rating); |
| 564 | |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static int sh_cmt_setup(struct sh_cmt_priv *p, struct platform_device *pdev) |
| 569 | { |
Paul Mundt | 46a12f7 | 2009-05-03 17:57:17 +0900 | [diff] [blame] | 570 | struct sh_timer_config *cfg = pdev->dev.platform_data; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 571 | struct resource *res; |
| 572 | int irq, ret; |
| 573 | ret = -ENXIO; |
| 574 | |
| 575 | memset(p, 0, sizeof(*p)); |
| 576 | p->pdev = pdev; |
| 577 | |
| 578 | if (!cfg) { |
| 579 | dev_err(&p->pdev->dev, "missing platform data\n"); |
| 580 | goto err0; |
| 581 | } |
| 582 | |
| 583 | platform_set_drvdata(pdev, p); |
| 584 | |
| 585 | res = platform_get_resource(p->pdev, IORESOURCE_MEM, 0); |
| 586 | if (!res) { |
| 587 | dev_err(&p->pdev->dev, "failed to get I/O memory\n"); |
| 588 | goto err0; |
| 589 | } |
| 590 | |
| 591 | irq = platform_get_irq(p->pdev, 0); |
| 592 | if (irq < 0) { |
| 593 | dev_err(&p->pdev->dev, "failed to get irq\n"); |
| 594 | goto err0; |
| 595 | } |
| 596 | |
| 597 | /* map memory, let mapbase point to our channel */ |
| 598 | p->mapbase = ioremap_nocache(res->start, resource_size(res)); |
| 599 | if (p->mapbase == NULL) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 600 | dev_err(&p->pdev->dev, "failed to remap I/O memory\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 601 | goto err0; |
| 602 | } |
| 603 | |
| 604 | /* request irq using setup_irq() (too early for request_irq()) */ |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 605 | p->irqaction.name = dev_name(&p->pdev->dev); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 606 | p->irqaction.handler = sh_cmt_interrupt; |
| 607 | p->irqaction.dev_id = p; |
| 608 | p->irqaction.flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 609 | |
| 610 | /* get hold of clock */ |
| 611 | p->clk = clk_get(&p->pdev->dev, cfg->clk); |
| 612 | if (IS_ERR(p->clk)) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 613 | dev_err(&p->pdev->dev, "cannot get clock\n"); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 614 | ret = PTR_ERR(p->clk); |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 615 | goto err1; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | if (resource_size(res) == 6) { |
| 619 | p->width = 16; |
| 620 | p->overflow_bit = 0x80; |
Magnus Damm | 3014f47 | 2009-04-29 14:50:37 +0000 | [diff] [blame] | 621 | p->clear_bits = ~0x80; |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 622 | } else { |
| 623 | p->width = 32; |
| 624 | p->overflow_bit = 0x8000; |
| 625 | p->clear_bits = ~0xc000; |
| 626 | } |
| 627 | |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 628 | ret = sh_cmt_register(p, (char *)dev_name(&p->pdev->dev), |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 629 | cfg->clockevent_rating, |
| 630 | cfg->clocksource_rating); |
| 631 | if (ret) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 632 | dev_err(&p->pdev->dev, "registration failed\n"); |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 633 | goto err1; |
| 634 | } |
| 635 | |
| 636 | ret = setup_irq(irq, &p->irqaction); |
| 637 | if (ret) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 638 | dev_err(&p->pdev->dev, "failed to request irq %d\n", irq); |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 639 | goto err1; |
| 640 | } |
| 641 | |
| 642 | return 0; |
| 643 | |
| 644 | err1: |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 645 | iounmap(p->mapbase); |
Paul Mundt | da64c2a | 2010-02-25 16:37:46 +0900 | [diff] [blame] | 646 | err0: |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | static int __devinit sh_cmt_probe(struct platform_device *pdev) |
| 651 | { |
| 652 | struct sh_cmt_priv *p = platform_get_drvdata(pdev); |
| 653 | int ret; |
| 654 | |
Magnus Damm | e475eed | 2009-04-15 10:50:04 +0000 | [diff] [blame] | 655 | if (p) { |
Paul Mundt | 214a607 | 2010-03-10 16:26:25 +0900 | [diff] [blame] | 656 | dev_info(&pdev->dev, "kept as earlytimer\n"); |
Magnus Damm | e475eed | 2009-04-15 10:50:04 +0000 | [diff] [blame] | 657 | return 0; |
| 658 | } |
| 659 | |
Magnus Damm | 8e0b842 | 2009-04-28 08:19:50 +0000 | [diff] [blame] | 660 | p = kmalloc(sizeof(*p), GFP_KERNEL); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 661 | if (p == NULL) { |
| 662 | dev_err(&pdev->dev, "failed to allocate driver data\n"); |
| 663 | return -ENOMEM; |
| 664 | } |
| 665 | |
| 666 | ret = sh_cmt_setup(p, pdev); |
| 667 | if (ret) { |
Magnus Damm | 8e0b842 | 2009-04-28 08:19:50 +0000 | [diff] [blame] | 668 | kfree(p); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 669 | platform_set_drvdata(pdev, NULL); |
| 670 | } |
| 671 | return ret; |
| 672 | } |
| 673 | |
| 674 | static int __devexit sh_cmt_remove(struct platform_device *pdev) |
| 675 | { |
| 676 | return -EBUSY; /* cannot unregister clockevent and clocksource */ |
| 677 | } |
| 678 | |
| 679 | static struct platform_driver sh_cmt_device_driver = { |
| 680 | .probe = sh_cmt_probe, |
| 681 | .remove = __devexit_p(sh_cmt_remove), |
| 682 | .driver = { |
| 683 | .name = "sh_cmt", |
| 684 | } |
| 685 | }; |
| 686 | |
| 687 | static int __init sh_cmt_init(void) |
| 688 | { |
| 689 | return platform_driver_register(&sh_cmt_device_driver); |
| 690 | } |
| 691 | |
| 692 | static void __exit sh_cmt_exit(void) |
| 693 | { |
| 694 | platform_driver_unregister(&sh_cmt_device_driver); |
| 695 | } |
| 696 | |
Magnus Damm | e475eed | 2009-04-15 10:50:04 +0000 | [diff] [blame] | 697 | early_platform_init("earlytimer", &sh_cmt_device_driver); |
Magnus Damm | 3fb1b6a | 2009-01-22 09:55:59 +0000 | [diff] [blame] | 698 | module_init(sh_cmt_init); |
| 699 | module_exit(sh_cmt_exit); |
| 700 | |
| 701 | MODULE_AUTHOR("Magnus Damm"); |
| 702 | MODULE_DESCRIPTION("SuperH CMT Timer Driver"); |
| 703 | MODULE_LICENSE("GPL v2"); |