blob: aed301bfa2f9d5ddb3c4aef71bd3b9789e24f076 [file] [log] [blame]
Paul Walmsleyaa218da2010-10-08 11:40:19 -06001/*
2 * OMAP 32ksynctimer/counter_32k-related code
3 *
4 * Copyright (C) 2009 Texas Instruments
5 * Copyright (C) 2010 Nokia Corporation
6 * Tony Lindgren <tony@atomide.com>
7 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * NOTE: This timer is not the same timer as the old OMAP1 MPU timer.
14 */
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/clk.h>
18#include <linux/io.h>
Russell King5e06b642010-12-15 19:19:25 +000019#include <linux/sched.h>
Paul Walmsleyaa218da2010-10-08 11:40:19 -060020
21#include <plat/common.h>
22#include <plat/board.h>
23
24#include <plat/clock.h>
25
26
27/*
28 * 32KHz clocksource ... always available, on pretty most chips except
29 * OMAP 730 and 1510. Other timers could be used as clocksources, with
30 * higher resolution in free-running counter modes (e.g. 12 MHz xtal),
31 * but systems won't necessarily want to spend resources that way.
32 */
33
34#define OMAP16XX_TIMER_32K_SYNCHRONIZED 0xfffbc410
35
36#if !(defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP15XX))
37
38#include <linux/clocksource.h>
39
40/*
41 * offset_32k holds the init time counter value. It is then subtracted
42 * from every counter read to achieve a counter that counts time from the
43 * kernel boot (needed for sched_clock()).
44 */
45static u32 offset_32k __read_mostly;
46
47#ifdef CONFIG_ARCH_OMAP16XX
Russell King5e06b642010-12-15 19:19:25 +000048static cycle_t notrace omap16xx_32k_read(struct clocksource *cs)
Paul Walmsleyaa218da2010-10-08 11:40:19 -060049{
50 return omap_readl(OMAP16XX_TIMER_32K_SYNCHRONIZED) - offset_32k;
51}
52#else
53#define omap16xx_32k_read NULL
54#endif
55
56#ifdef CONFIG_ARCH_OMAP2420
Russell King5e06b642010-12-15 19:19:25 +000057static cycle_t notrace omap2420_32k_read(struct clocksource *cs)
Paul Walmsleyaa218da2010-10-08 11:40:19 -060058{
59 return omap_readl(OMAP2420_32KSYNCT_BASE + 0x10) - offset_32k;
60}
61#else
62#define omap2420_32k_read NULL
63#endif
64
65#ifdef CONFIG_ARCH_OMAP2430
Russell King5e06b642010-12-15 19:19:25 +000066static cycle_t notrace omap2430_32k_read(struct clocksource *cs)
Paul Walmsleyaa218da2010-10-08 11:40:19 -060067{
68 return omap_readl(OMAP2430_32KSYNCT_BASE + 0x10) - offset_32k;
69}
70#else
71#define omap2430_32k_read NULL
72#endif
73
74#ifdef CONFIG_ARCH_OMAP3
Russell King5e06b642010-12-15 19:19:25 +000075static cycle_t notrace omap34xx_32k_read(struct clocksource *cs)
Paul Walmsleyaa218da2010-10-08 11:40:19 -060076{
77 return omap_readl(OMAP3430_32KSYNCT_BASE + 0x10) - offset_32k;
78}
79#else
80#define omap34xx_32k_read NULL
81#endif
82
83#ifdef CONFIG_ARCH_OMAP4
Russell King5e06b642010-12-15 19:19:25 +000084static cycle_t notrace omap44xx_32k_read(struct clocksource *cs)
Paul Walmsleyaa218da2010-10-08 11:40:19 -060085{
86 return omap_readl(OMAP4430_32KSYNCT_BASE + 0x10) - offset_32k;
87}
88#else
89#define omap44xx_32k_read NULL
90#endif
91
92/*
93 * Kernel assumes that sched_clock can be called early but may not have
94 * things ready yet.
95 */
Russell King5e06b642010-12-15 19:19:25 +000096static cycle_t notrace omap_32k_read_dummy(struct clocksource *cs)
Paul Walmsleyaa218da2010-10-08 11:40:19 -060097{
98 return 0;
99}
100
101static struct clocksource clocksource_32k = {
102 .name = "32k_counter",
103 .rating = 250,
104 .read = omap_32k_read_dummy,
105 .mask = CLOCKSOURCE_MASK(32),
Paul Walmsleyaa218da2010-10-08 11:40:19 -0600106 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
107};
108
109/*
110 * Returns current time from boot in nsecs. It's OK for this to wrap
111 * around for now, as it's just a relative time stamp.
112 */
Russell King5e06b642010-12-15 19:19:25 +0000113unsigned long long notrace sched_clock(void)
Paul Walmsleyaa218da2010-10-08 11:40:19 -0600114{
115 return clocksource_cyc2ns(clocksource_32k.read(&clocksource_32k),
116 clocksource_32k.mult, clocksource_32k.shift);
117}
118
119/**
120 * read_persistent_clock - Return time from a persistent clock.
121 *
122 * Reads the time from a source which isn't disabled during PM, the
123 * 32k sync timer. Convert the cycles elapsed since last read into
124 * nsecs and adds to a monotonically increasing timespec.
125 */
126static struct timespec persistent_ts;
127static cycles_t cycles, last_cycles;
128void read_persistent_clock(struct timespec *ts)
129{
130 unsigned long long nsecs;
131 cycles_t delta;
132 struct timespec *tsp = &persistent_ts;
133
134 last_cycles = cycles;
135 cycles = clocksource_32k.read(&clocksource_32k);
136 delta = cycles - last_cycles;
137
138 nsecs = clocksource_cyc2ns(delta,
139 clocksource_32k.mult, clocksource_32k.shift);
140
141 timespec_add_ns(tsp, nsecs);
142 *ts = *tsp;
143}
144
145static int __init omap_init_clocksource_32k(void)
146{
147 static char err[] __initdata = KERN_ERR
148 "%s: can't register clocksource!\n";
149
150 if (cpu_is_omap16xx() || cpu_class_is_omap2()) {
151 struct clk *sync_32k_ick;
152
153 if (cpu_is_omap16xx())
154 clocksource_32k.read = omap16xx_32k_read;
155 else if (cpu_is_omap2420())
156 clocksource_32k.read = omap2420_32k_read;
157 else if (cpu_is_omap2430())
158 clocksource_32k.read = omap2430_32k_read;
159 else if (cpu_is_omap34xx())
160 clocksource_32k.read = omap34xx_32k_read;
161 else if (cpu_is_omap44xx())
162 clocksource_32k.read = omap44xx_32k_read;
163 else
164 return -ENODEV;
165
166 sync_32k_ick = clk_get(NULL, "omap_32ksync_ick");
167 if (sync_32k_ick)
168 clk_enable(sync_32k_ick);
169
Paul Walmsleyaa218da2010-10-08 11:40:19 -0600170 offset_32k = clocksource_32k.read(&clocksource_32k);
171
Russell King8437c252010-12-13 13:18:44 +0000172 if (clocksource_register_hz(&clocksource_32k, 32768))
Paul Walmsleyaa218da2010-10-08 11:40:19 -0600173 printk(err, clocksource_32k.name);
174 }
175 return 0;
176}
177arch_initcall(omap_init_clocksource_32k);
178
179#endif /* !(defined(CONFIG_ARCH_OMAP730) || defined(CONFIG_ARCH_OMAP15XX)) */
180