blob: 54f3d119d99c7b495008ee219538f488106b07d8 [file] [log] [blame]
Mattias Wallin489bcce2011-05-27 10:30:12 +02001/*
2 * Copyright (C) ST-Ericsson SA 2011
3 *
4 * License Terms: GNU General Public License v2
5 * Author: Mattias Wallin <mattias.wallin@stericsson.com> for ST-Ericsson
6 * Author: Sundar Iyer for ST-Ericsson
7 * sched_clock implementation is based on:
8 * plat-nomadik/timer.c Linus Walleij <linus.walleij@stericsson.com>
9 *
10 * DBx500-PRCMU Timer
11 * The PRCMU has 5 timers which are available in a always-on
12 * power domain. We use the Timer 4 for our always-on clock
13 * source on DB8500 and Timer 3 on DB5500.
14 */
15#include <linux/clockchips.h>
16#include <linux/clksrc-dbx500-prcmu.h>
17
18#include <asm/sched_clock.h>
19
Mattias Wallin489bcce2011-05-27 10:30:12 +020020#define RATE_32K 32768
21
22#define TIMER_MODE_CONTINOUS 0x1
23#define TIMER_DOWNCOUNT_VAL 0xffffffff
24
25#define PRCMU_TIMER_REF 0
26#define PRCMU_TIMER_DOWNCOUNT 0x4
27#define PRCMU_TIMER_MODE 0x8
28
29#define SCHED_CLOCK_MIN_WRAP 131072 /* 2^32 / 32768 */
30
Linus Walleijb1e3be062011-10-03 09:30:20 +020031static void __iomem *clksrc_dbx500_timer_base;
Mattias Wallin489bcce2011-05-27 10:30:12 +020032
33static cycle_t clksrc_dbx500_prcmu_read(struct clocksource *cs)
34{
35 u32 count, count2;
36
37 do {
38 count = readl(clksrc_dbx500_timer_base +
39 PRCMU_TIMER_DOWNCOUNT);
40 count2 = readl(clksrc_dbx500_timer_base +
41 PRCMU_TIMER_DOWNCOUNT);
42 } while (count2 != count);
43
44 /* Negate because the timer is a decrementing counter */
45 return ~count;
46}
47
48static struct clocksource clocksource_dbx500_prcmu = {
49 .name = "dbx500-prcmu-timer",
50 .rating = 300,
51 .read = clksrc_dbx500_prcmu_read,
Mattias Wallin489bcce2011-05-27 10:30:12 +020052 .mask = CLOCKSOURCE_MASK(32),
53 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
54};
55
56#ifdef CONFIG_CLKSRC_DBX500_PRCMU_SCHED_CLOCK
Mattias Wallin489bcce2011-05-27 10:30:12 +020057
Linus Walleijcfef0322012-01-02 14:50:15 +010058static u32 notrace dbx500_prcmu_sched_clock_read(void)
Mattias Wallin489bcce2011-05-27 10:30:12 +020059{
Mattias Wallin489bcce2011-05-27 10:30:12 +020060 if (unlikely(!clksrc_dbx500_timer_base))
61 return 0;
62
Linus Walleijcfef0322012-01-02 14:50:15 +010063 return clksrc_dbx500_prcmu_read(&clocksource_dbx500_prcmu);
Mattias Wallin489bcce2011-05-27 10:30:12 +020064}
65
Mattias Wallin489bcce2011-05-27 10:30:12 +020066#endif
67
Linus Walleijb1e3be062011-10-03 09:30:20 +020068void __init clksrc_dbx500_prcmu_init(void __iomem *base)
Mattias Wallin489bcce2011-05-27 10:30:12 +020069{
Linus Walleijb1e3be062011-10-03 09:30:20 +020070 clksrc_dbx500_timer_base = base;
71
Mattias Wallin489bcce2011-05-27 10:30:12 +020072 /*
73 * The A9 sub system expects the timer to be configured as
74 * a continous looping timer.
75 * The PRCMU should configure it but if it for some reason
76 * don't we do it here.
77 */
78 if (readl(clksrc_dbx500_timer_base + PRCMU_TIMER_MODE) !=
79 TIMER_MODE_CONTINOUS) {
80 writel(TIMER_MODE_CONTINOUS,
81 clksrc_dbx500_timer_base + PRCMU_TIMER_MODE);
82 writel(TIMER_DOWNCOUNT_VAL,
83 clksrc_dbx500_timer_base + PRCMU_TIMER_REF);
84 }
85#ifdef CONFIG_CLKSRC_DBX500_PRCMU_SCHED_CLOCK
Linus Walleijcfef0322012-01-02 14:50:15 +010086 setup_sched_clock(dbx500_prcmu_sched_clock_read,
Mattias Wallin489bcce2011-05-27 10:30:12 +020087 32, RATE_32K);
88#endif
Yong Zhang13f0f032011-12-01 15:20:15 +080089 clocksource_register_hz(&clocksource_dbx500_prcmu, RATE_32K);
Mattias Wallin489bcce2011-05-27 10:30:12 +020090}