1 From 00009eabeb2074bef5c89e576a7a6d827c12c3d9 Mon Sep 17 00:00:00 2001
2 From: Kumar Gala <galak@codeaurora.org>
3 Date: Wed, 29 Jan 2014 16:17:30 -0600
4 Subject: [PATCH 004/182] clocksource: qcom: Move clocksource code out of
7 We intend to share the clocksource code for MSM platforms between legacy
8 and multiplatform supported qcom SoCs.
10 Acked-by: Olof Johansson <olof@lixom.net>
11 Signed-off-by: Kumar Gala <galak@codeaurora.org>
13 arch/arm/mach-msm/Kconfig | 13 +-
14 arch/arm/mach-msm/Makefile | 1 -
15 arch/arm/mach-msm/timer.c | 333 --------------------------------------
16 drivers/clocksource/Kconfig | 3 +
17 drivers/clocksource/Makefile | 1 +
18 drivers/clocksource/qcom-timer.c | 329 +++++++++++++++++++++++++++++++++++++
19 6 files changed, 338 insertions(+), 342 deletions(-)
20 delete mode 100644 arch/arm/mach-msm/timer.c
21 create mode 100644 drivers/clocksource/qcom-timer.c
23 diff --git a/arch/arm/mach-msm/Kconfig b/arch/arm/mach-msm/Kconfig
24 index 9625cf3..3c4eca7 100644
25 --- a/arch/arm/mach-msm/Kconfig
26 +++ b/arch/arm/mach-msm/Kconfig
27 @@ -21,7 +21,7 @@ config ARCH_MSM8X60
35 bool "Enable support for MSM8960"
36 @@ -29,7 +29,7 @@ config ARCH_MSM8960
44 bool "Enable support for MSM8974"
45 @@ -54,7 +54,7 @@ config ARCH_MSM7X00A
46 select MACH_TROUT if !MACH_HALIBUT
54 @@ -66,7 +66,7 @@ config ARCH_MSM7X30
63 @@ -78,7 +78,7 @@ config ARCH_QSD8X50
72 @@ -153,7 +153,4 @@ config MSM_GPIOMUX
80 diff --git a/arch/arm/mach-msm/Makefile b/arch/arm/mach-msm/Makefile
81 index 8327f60..04b1bee 100644
82 --- a/arch/arm/mach-msm/Makefile
83 +++ b/arch/arm/mach-msm/Makefile
85 -obj-$(CONFIG_MSM_TIMER) += timer.o
86 obj-$(CONFIG_MSM_PROC_COMM) += clock.o
88 obj-$(CONFIG_MSM_VIC) += irq-vic.o
89 diff --git a/arch/arm/mach-msm/timer.c b/arch/arm/mach-msm/timer.c
90 deleted file mode 100644
91 index fd16449..0000000
92 --- a/arch/arm/mach-msm/timer.c
97 - * Copyright (C) 2007 Google, Inc.
98 - * Copyright (c) 2009-2012, The Linux Foundation. All rights reserved.
100 - * This software is licensed under the terms of the GNU General Public
101 - * License version 2, as published by the Free Software Foundation, and
102 - * may be copied, distributed, and modified under those terms.
104 - * This program is distributed in the hope that it will be useful,
105 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
106 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
107 - * GNU General Public License for more details.
111 -#include <linux/clocksource.h>
112 -#include <linux/clockchips.h>
113 -#include <linux/cpu.h>
114 -#include <linux/init.h>
115 -#include <linux/interrupt.h>
116 -#include <linux/irq.h>
117 -#include <linux/io.h>
118 -#include <linux/of.h>
119 -#include <linux/of_address.h>
120 -#include <linux/of_irq.h>
121 -#include <linux/sched_clock.h>
123 -#include <asm/mach/time.h>
127 -#define TIMER_MATCH_VAL 0x0000
128 -#define TIMER_COUNT_VAL 0x0004
129 -#define TIMER_ENABLE 0x0008
130 -#define TIMER_ENABLE_CLR_ON_MATCH_EN BIT(1)
131 -#define TIMER_ENABLE_EN BIT(0)
132 -#define TIMER_CLEAR 0x000C
133 -#define DGT_CLK_CTL 0x10
134 -#define DGT_CLK_CTL_DIV_4 0x3
135 -#define TIMER_STS_GPT0_CLR_PEND BIT(10)
137 -#define GPT_HZ 32768
139 -#define MSM_DGT_SHIFT 5
141 -static void __iomem *event_base;
142 -static void __iomem *sts_base;
144 -static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
146 - struct clock_event_device *evt = dev_id;
147 - /* Stop the timer tick */
148 - if (evt->mode == CLOCK_EVT_MODE_ONESHOT) {
149 - u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
150 - ctrl &= ~TIMER_ENABLE_EN;
151 - writel_relaxed(ctrl, event_base + TIMER_ENABLE);
153 - evt->event_handler(evt);
154 - return IRQ_HANDLED;
157 -static int msm_timer_set_next_event(unsigned long cycles,
158 - struct clock_event_device *evt)
160 - u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
162 - ctrl &= ~TIMER_ENABLE_EN;
163 - writel_relaxed(ctrl, event_base + TIMER_ENABLE);
165 - writel_relaxed(ctrl, event_base + TIMER_CLEAR);
166 - writel_relaxed(cycles, event_base + TIMER_MATCH_VAL);
169 - while (readl_relaxed(sts_base) & TIMER_STS_GPT0_CLR_PEND)
172 - writel_relaxed(ctrl | TIMER_ENABLE_EN, event_base + TIMER_ENABLE);
176 -static void msm_timer_set_mode(enum clock_event_mode mode,
177 - struct clock_event_device *evt)
181 - ctrl = readl_relaxed(event_base + TIMER_ENABLE);
182 - ctrl &= ~(TIMER_ENABLE_EN | TIMER_ENABLE_CLR_ON_MATCH_EN);
185 - case CLOCK_EVT_MODE_RESUME:
186 - case CLOCK_EVT_MODE_PERIODIC:
188 - case CLOCK_EVT_MODE_ONESHOT:
189 - /* Timer is enabled in set_next_event */
191 - case CLOCK_EVT_MODE_UNUSED:
192 - case CLOCK_EVT_MODE_SHUTDOWN:
195 - writel_relaxed(ctrl, event_base + TIMER_ENABLE);
198 -static struct clock_event_device __percpu *msm_evt;
200 -static void __iomem *source_base;
202 -static notrace cycle_t msm_read_timer_count(struct clocksource *cs)
204 - return readl_relaxed(source_base + TIMER_COUNT_VAL);
207 -static notrace cycle_t msm_read_timer_count_shift(struct clocksource *cs)
210 - * Shift timer count down by a constant due to unreliable lower bits
213 - return msm_read_timer_count(cs) >> MSM_DGT_SHIFT;
216 -static struct clocksource msm_clocksource = {
217 - .name = "dg_timer",
219 - .read = msm_read_timer_count,
220 - .mask = CLOCKSOURCE_MASK(32),
221 - .flags = CLOCK_SOURCE_IS_CONTINUOUS,
224 -static int msm_timer_irq;
225 -static int msm_timer_has_ppi;
227 -static int msm_local_timer_setup(struct clock_event_device *evt)
229 - int cpu = smp_processor_id();
232 - evt->irq = msm_timer_irq;
233 - evt->name = "msm_timer";
234 - evt->features = CLOCK_EVT_FEAT_ONESHOT;
236 - evt->set_mode = msm_timer_set_mode;
237 - evt->set_next_event = msm_timer_set_next_event;
238 - evt->cpumask = cpumask_of(cpu);
240 - clockevents_config_and_register(evt, GPT_HZ, 4, 0xffffffff);
242 - if (msm_timer_has_ppi) {
243 - enable_percpu_irq(evt->irq, IRQ_TYPE_EDGE_RISING);
245 - err = request_irq(evt->irq, msm_timer_interrupt,
246 - IRQF_TIMER | IRQF_NOBALANCING |
247 - IRQF_TRIGGER_RISING, "gp_timer", evt);
249 - pr_err("request_irq failed\n");
255 -static void msm_local_timer_stop(struct clock_event_device *evt)
257 - evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
258 - disable_percpu_irq(evt->irq);
261 -static int msm_timer_cpu_notify(struct notifier_block *self,
262 - unsigned long action, void *hcpu)
265 - * Grab cpu pointer in each case to avoid spurious
266 - * preemptible warnings
268 - switch (action & ~CPU_TASKS_FROZEN) {
270 - msm_local_timer_setup(this_cpu_ptr(msm_evt));
273 - msm_local_timer_stop(this_cpu_ptr(msm_evt));
280 -static struct notifier_block msm_timer_cpu_nb = {
281 - .notifier_call = msm_timer_cpu_notify,
284 -static u64 notrace msm_sched_clock_read(void)
286 - return msm_clocksource.read(&msm_clocksource);
289 -static void __init msm_timer_init(u32 dgt_hz, int sched_bits, int irq,
292 - struct clocksource *cs = &msm_clocksource;
295 - msm_timer_irq = irq;
296 - msm_timer_has_ppi = percpu;
298 - msm_evt = alloc_percpu(struct clock_event_device);
300 - pr_err("memory allocation failed for clockevents\n");
305 - res = request_percpu_irq(irq, msm_timer_interrupt,
306 - "gp_timer", msm_evt);
309 - pr_err("request_percpu_irq failed\n");
311 - res = register_cpu_notifier(&msm_timer_cpu_nb);
313 - free_percpu_irq(irq, msm_evt);
317 - /* Immediately configure the timer on the boot CPU */
318 - msm_local_timer_setup(__this_cpu_ptr(msm_evt));
322 - writel_relaxed(TIMER_ENABLE_EN, source_base + TIMER_ENABLE);
323 - res = clocksource_register_hz(cs, dgt_hz);
325 - pr_err("clocksource_register failed\n");
326 - sched_clock_register(msm_sched_clock_read, sched_bits, dgt_hz);
330 -static void __init msm_dt_timer_init(struct device_node *np)
334 - struct resource res;
336 - void __iomem *base;
337 - void __iomem *cpu0_base;
339 - base = of_iomap(np, 0);
341 - pr_err("Failed to map event base\n");
345 - /* We use GPT0 for the clockevent */
346 - irq = irq_of_parse_and_map(np, 1);
348 - pr_err("Can't get irq\n");
352 - /* We use CPU0's DGT for the clocksource */
353 - if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
356 - if (of_address_to_resource(np, 0, &res)) {
357 - pr_err("Failed to parse DGT resource\n");
361 - cpu0_base = ioremap(res.start + percpu_offset, resource_size(&res));
363 - pr_err("Failed to map source base\n");
367 - if (of_property_read_u32(np, "clock-frequency", &freq)) {
368 - pr_err("Unknown frequency\n");
372 - event_base = base + 0x4;
373 - sts_base = base + 0x88;
374 - source_base = cpu0_base + 0x24;
376 - writel_relaxed(DGT_CLK_CTL_DIV_4, source_base + DGT_CLK_CTL);
378 - msm_timer_init(freq, 32, irq, !!percpu_offset);
380 -CLOCKSOURCE_OF_DECLARE(kpss_timer, "qcom,kpss-timer", msm_dt_timer_init);
381 -CLOCKSOURCE_OF_DECLARE(scss_timer, "qcom,scss-timer", msm_dt_timer_init);
384 -static int __init msm_timer_map(phys_addr_t addr, u32 event, u32 source,
387 - void __iomem *base;
389 - base = ioremap(addr, SZ_256);
391 - pr_err("Failed to map timer base\n");
394 - event_base = base + event;
395 - source_base = base + source;
397 - sts_base = base + sts;
402 -void __init msm7x01_timer_init(void)
404 - struct clocksource *cs = &msm_clocksource;
406 - if (msm_timer_map(0xc0100000, 0x0, 0x10, 0x0))
408 - cs->read = msm_read_timer_count_shift;
409 - cs->mask = CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT));
411 - msm_timer_init(19200000 >> MSM_DGT_SHIFT, 32 - MSM_DGT_SHIFT, 7,
415 -void __init msm7x30_timer_init(void)
417 - if (msm_timer_map(0xc0100000, 0x4, 0x24, 0x80))
419 - msm_timer_init(24576000 / 4, 32, 1, false);
422 -void __init qsd8x50_timer_init(void)
424 - if (msm_timer_map(0xAC100000, 0x0, 0x10, 0x34))
426 - msm_timer_init(19200000 / 4, 32, 7, false);
428 diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig
429 index cd6950f..6510ec4 100644
430 --- a/drivers/clocksource/Kconfig
431 +++ b/drivers/clocksource/Kconfig
432 @@ -140,3 +140,6 @@ config VF_PIT_TIMER
435 Support for Period Interrupt Timer on Freescale Vybrid Family SoCs.
439 diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile
440 index c7ca50a..2e0c0cc 100644
441 --- a/drivers/clocksource/Makefile
442 +++ b/drivers/clocksource/Makefile
443 @@ -32,6 +32,7 @@ obj-$(CONFIG_CLKSRC_EFM32) += time-efm32.o
444 obj-$(CONFIG_CLKSRC_EXYNOS_MCT) += exynos_mct.o
445 obj-$(CONFIG_CLKSRC_SAMSUNG_PWM) += samsung_pwm_timer.o
446 obj-$(CONFIG_VF_PIT_TIMER) += vf_pit_timer.o
447 +obj-$(CONFIG_CLKSRC_QCOM) += qcom-timer.o
449 obj-$(CONFIG_ARM_ARCH_TIMER) += arm_arch_timer.o
450 obj-$(CONFIG_ARM_GLOBAL_TIMER) += arm_global_timer.o
451 diff --git a/drivers/clocksource/qcom-timer.c b/drivers/clocksource/qcom-timer.c
453 index 0000000..dca829e
455 +++ b/drivers/clocksource/qcom-timer.c
459 + * Copyright (C) 2007 Google, Inc.
460 + * Copyright (c) 2009-2012,2014, The Linux Foundation. All rights reserved.
462 + * This software is licensed under the terms of the GNU General Public
463 + * License version 2, as published by the Free Software Foundation, and
464 + * may be copied, distributed, and modified under those terms.
466 + * This program is distributed in the hope that it will be useful,
467 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
468 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
469 + * GNU General Public License for more details.
473 +#include <linux/clocksource.h>
474 +#include <linux/clockchips.h>
475 +#include <linux/cpu.h>
476 +#include <linux/init.h>
477 +#include <linux/interrupt.h>
478 +#include <linux/irq.h>
479 +#include <linux/io.h>
480 +#include <linux/of.h>
481 +#include <linux/of_address.h>
482 +#include <linux/of_irq.h>
483 +#include <linux/sched_clock.h>
485 +#define TIMER_MATCH_VAL 0x0000
486 +#define TIMER_COUNT_VAL 0x0004
487 +#define TIMER_ENABLE 0x0008
488 +#define TIMER_ENABLE_CLR_ON_MATCH_EN BIT(1)
489 +#define TIMER_ENABLE_EN BIT(0)
490 +#define TIMER_CLEAR 0x000C
491 +#define DGT_CLK_CTL 0x10
492 +#define DGT_CLK_CTL_DIV_4 0x3
493 +#define TIMER_STS_GPT0_CLR_PEND BIT(10)
495 +#define GPT_HZ 32768
497 +#define MSM_DGT_SHIFT 5
499 +static void __iomem *event_base;
500 +static void __iomem *sts_base;
502 +static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
504 + struct clock_event_device *evt = dev_id;
505 + /* Stop the timer tick */
506 + if (evt->mode == CLOCK_EVT_MODE_ONESHOT) {
507 + u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
508 + ctrl &= ~TIMER_ENABLE_EN;
509 + writel_relaxed(ctrl, event_base + TIMER_ENABLE);
511 + evt->event_handler(evt);
512 + return IRQ_HANDLED;
515 +static int msm_timer_set_next_event(unsigned long cycles,
516 + struct clock_event_device *evt)
518 + u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
520 + ctrl &= ~TIMER_ENABLE_EN;
521 + writel_relaxed(ctrl, event_base + TIMER_ENABLE);
523 + writel_relaxed(ctrl, event_base + TIMER_CLEAR);
524 + writel_relaxed(cycles, event_base + TIMER_MATCH_VAL);
527 + while (readl_relaxed(sts_base) & TIMER_STS_GPT0_CLR_PEND)
530 + writel_relaxed(ctrl | TIMER_ENABLE_EN, event_base + TIMER_ENABLE);
534 +static void msm_timer_set_mode(enum clock_event_mode mode,
535 + struct clock_event_device *evt)
539 + ctrl = readl_relaxed(event_base + TIMER_ENABLE);
540 + ctrl &= ~(TIMER_ENABLE_EN | TIMER_ENABLE_CLR_ON_MATCH_EN);
543 + case CLOCK_EVT_MODE_RESUME:
544 + case CLOCK_EVT_MODE_PERIODIC:
546 + case CLOCK_EVT_MODE_ONESHOT:
547 + /* Timer is enabled in set_next_event */
549 + case CLOCK_EVT_MODE_UNUSED:
550 + case CLOCK_EVT_MODE_SHUTDOWN:
553 + writel_relaxed(ctrl, event_base + TIMER_ENABLE);
556 +static struct clock_event_device __percpu *msm_evt;
558 +static void __iomem *source_base;
560 +static notrace cycle_t msm_read_timer_count(struct clocksource *cs)
562 + return readl_relaxed(source_base + TIMER_COUNT_VAL);
565 +static notrace cycle_t msm_read_timer_count_shift(struct clocksource *cs)
568 + * Shift timer count down by a constant due to unreliable lower bits
571 + return msm_read_timer_count(cs) >> MSM_DGT_SHIFT;
574 +static struct clocksource msm_clocksource = {
575 + .name = "dg_timer",
577 + .read = msm_read_timer_count,
578 + .mask = CLOCKSOURCE_MASK(32),
579 + .flags = CLOCK_SOURCE_IS_CONTINUOUS,
582 +static int msm_timer_irq;
583 +static int msm_timer_has_ppi;
585 +static int msm_local_timer_setup(struct clock_event_device *evt)
587 + int cpu = smp_processor_id();
590 + evt->irq = msm_timer_irq;
591 + evt->name = "msm_timer";
592 + evt->features = CLOCK_EVT_FEAT_ONESHOT;
594 + evt->set_mode = msm_timer_set_mode;
595 + evt->set_next_event = msm_timer_set_next_event;
596 + evt->cpumask = cpumask_of(cpu);
598 + clockevents_config_and_register(evt, GPT_HZ, 4, 0xffffffff);
600 + if (msm_timer_has_ppi) {
601 + enable_percpu_irq(evt->irq, IRQ_TYPE_EDGE_RISING);
603 + err = request_irq(evt->irq, msm_timer_interrupt,
604 + IRQF_TIMER | IRQF_NOBALANCING |
605 + IRQF_TRIGGER_RISING, "gp_timer", evt);
607 + pr_err("request_irq failed\n");
613 +static void msm_local_timer_stop(struct clock_event_device *evt)
615 + evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
616 + disable_percpu_irq(evt->irq);
619 +static int msm_timer_cpu_notify(struct notifier_block *self,
620 + unsigned long action, void *hcpu)
623 + * Grab cpu pointer in each case to avoid spurious
624 + * preemptible warnings
626 + switch (action & ~CPU_TASKS_FROZEN) {
628 + msm_local_timer_setup(this_cpu_ptr(msm_evt));
631 + msm_local_timer_stop(this_cpu_ptr(msm_evt));
638 +static struct notifier_block msm_timer_cpu_nb = {
639 + .notifier_call = msm_timer_cpu_notify,
642 +static u64 notrace msm_sched_clock_read(void)
644 + return msm_clocksource.read(&msm_clocksource);
647 +static void __init msm_timer_init(u32 dgt_hz, int sched_bits, int irq,
650 + struct clocksource *cs = &msm_clocksource;
653 + msm_timer_irq = irq;
654 + msm_timer_has_ppi = percpu;
656 + msm_evt = alloc_percpu(struct clock_event_device);
658 + pr_err("memory allocation failed for clockevents\n");
663 + res = request_percpu_irq(irq, msm_timer_interrupt,
664 + "gp_timer", msm_evt);
667 + pr_err("request_percpu_irq failed\n");
669 + res = register_cpu_notifier(&msm_timer_cpu_nb);
671 + free_percpu_irq(irq, msm_evt);
675 + /* Immediately configure the timer on the boot CPU */
676 + msm_local_timer_setup(__this_cpu_ptr(msm_evt));
680 + writel_relaxed(TIMER_ENABLE_EN, source_base + TIMER_ENABLE);
681 + res = clocksource_register_hz(cs, dgt_hz);
683 + pr_err("clocksource_register failed\n");
684 + sched_clock_register(msm_sched_clock_read, sched_bits, dgt_hz);
688 +static void __init msm_dt_timer_init(struct device_node *np)
692 + struct resource res;
694 + void __iomem *base;
695 + void __iomem *cpu0_base;
697 + base = of_iomap(np, 0);
699 + pr_err("Failed to map event base\n");
703 + /* We use GPT0 for the clockevent */
704 + irq = irq_of_parse_and_map(np, 1);
706 + pr_err("Can't get irq\n");
710 + /* We use CPU0's DGT for the clocksource */
711 + if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
714 + if (of_address_to_resource(np, 0, &res)) {
715 + pr_err("Failed to parse DGT resource\n");
719 + cpu0_base = ioremap(res.start + percpu_offset, resource_size(&res));
721 + pr_err("Failed to map source base\n");
725 + if (of_property_read_u32(np, "clock-frequency", &freq)) {
726 + pr_err("Unknown frequency\n");
730 + event_base = base + 0x4;
731 + sts_base = base + 0x88;
732 + source_base = cpu0_base + 0x24;
734 + writel_relaxed(DGT_CLK_CTL_DIV_4, source_base + DGT_CLK_CTL);
736 + msm_timer_init(freq, 32, irq, !!percpu_offset);
738 +CLOCKSOURCE_OF_DECLARE(kpss_timer, "qcom,kpss-timer", msm_dt_timer_init);
739 +CLOCKSOURCE_OF_DECLARE(scss_timer, "qcom,scss-timer", msm_dt_timer_init);
742 +static int __init msm_timer_map(phys_addr_t addr, u32 event, u32 source,
745 + void __iomem *base;
747 + base = ioremap(addr, SZ_256);
749 + pr_err("Failed to map timer base\n");
752 + event_base = base + event;
753 + source_base = base + source;
755 + sts_base = base + sts;
760 +void __init msm7x01_timer_init(void)
762 + struct clocksource *cs = &msm_clocksource;
764 + if (msm_timer_map(0xc0100000, 0x0, 0x10, 0x0))
766 + cs->read = msm_read_timer_count_shift;
767 + cs->mask = CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT));
769 + msm_timer_init(19200000 >> MSM_DGT_SHIFT, 32 - MSM_DGT_SHIFT, 7,
773 +void __init msm7x30_timer_init(void)
775 + if (msm_timer_map(0xc0100000, 0x4, 0x24, 0x80))
777 + msm_timer_init(24576000 / 4, 32, 1, false);
780 +void __init qsd8x50_timer_init(void)
782 + if (msm_timer_map(0xAC100000, 0x0, 0x10, 0x34))
784 + msm_timer_init(19200000 / 4, 32, 7, false);