blob: 0a7fb2440e2909490769a3613d2878411f420068 [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>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070017#include <linux/sched_clock.h>
Mattias Wallin489bcce2011-05-27 10:30:12 +020018
Mattias Wallin489bcce2011-05-27 10:30:12 +020019#define RATE_32K 32768
20
21#define TIMER_MODE_CONTINOUS 0x1
22#define TIMER_DOWNCOUNT_VAL 0xffffffff
23
24#define PRCMU_TIMER_REF 0
25#define PRCMU_TIMER_DOWNCOUNT 0x4
26#define PRCMU_TIMER_MODE 0x8
27
28#define SCHED_CLOCK_MIN_WRAP 131072 /* 2^32 / 32768 */
29
Linus Walleijb1e3be062011-10-03 09:30:20 +020030static void __iomem *clksrc_dbx500_timer_base;
Mattias Wallin489bcce2011-05-27 10:30:12 +020031
32static cycle_t clksrc_dbx500_prcmu_read(struct clocksource *cs)
33{
34 u32 count, count2;
35
36 do {
37 count = readl(clksrc_dbx500_timer_base +
38 PRCMU_TIMER_DOWNCOUNT);
39 count2 = readl(clksrc_dbx500_timer_base +
40 PRCMU_TIMER_DOWNCOUNT);
41 } while (count2 != count);
42
43 /* Negate because the timer is a decrementing counter */
44 return ~count;
45}
46
47static struct clocksource clocksource_dbx500_prcmu = {
48 .name = "dbx500-prcmu-timer",
49 .rating = 300,
50 .read = clksrc_dbx500_prcmu_read,
Mattias Wallin489bcce2011-05-27 10:30:12 +020051 .mask = CLOCKSOURCE_MASK(32),
52 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
53};
54
55#ifdef CONFIG_CLKSRC_DBX500_PRCMU_SCHED_CLOCK
Mattias Wallin489bcce2011-05-27 10:30:12 +020056
Linus Walleijcfef0322012-01-02 14:50:15 +010057static u32 notrace dbx500_prcmu_sched_clock_read(void)
Mattias Wallin489bcce2011-05-27 10:30:12 +020058{
Mattias Wallin489bcce2011-05-27 10:30:12 +020059 if (unlikely(!clksrc_dbx500_timer_base))
60 return 0;
61
Linus Walleijcfef0322012-01-02 14:50:15 +010062 return clksrc_dbx500_prcmu_read(&clocksource_dbx500_prcmu);
Mattias Wallin489bcce2011-05-27 10:30:12 +020063}
64
Mattias Wallin489bcce2011-05-27 10:30:12 +020065#endif
66
Linus Walleijb1e3be062011-10-03 09:30:20 +020067void __init clksrc_dbx500_prcmu_init(void __iomem *base)
Mattias Wallin489bcce2011-05-27 10:30:12 +020068{
Linus Walleijb1e3be062011-10-03 09:30:20 +020069 clksrc_dbx500_timer_base = base;
70
Mattias Wallin489bcce2011-05-27 10:30:12 +020071 /*
72 * The A9 sub system expects the timer to be configured as
73 * a continous looping timer.
74 * The PRCMU should configure it but if it for some reason
75 * don't we do it here.
76 */
77 if (readl(clksrc_dbx500_timer_base + PRCMU_TIMER_MODE) !=
78 TIMER_MODE_CONTINOUS) {
79 writel(TIMER_MODE_CONTINOUS,
80 clksrc_dbx500_timer_base + PRCMU_TIMER_MODE);
81 writel(TIMER_DOWNCOUNT_VAL,
82 clksrc_dbx500_timer_base + PRCMU_TIMER_REF);
83 }
84#ifdef CONFIG_CLKSRC_DBX500_PRCMU_SCHED_CLOCK
Linus Walleijcfef0322012-01-02 14:50:15 +010085 setup_sched_clock(dbx500_prcmu_sched_clock_read,
Mattias Wallin489bcce2011-05-27 10:30:12 +020086 32, RATE_32K);
87#endif
Yong Zhang13f0f032011-12-01 15:20:15 +080088 clocksource_register_hz(&clocksource_dbx500_prcmu, RATE_32K);
Mattias Wallin489bcce2011-05-27 10:30:12 +020089}