[ARM] tegra: spdif/i2s audio: fixes
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-tegra / timer.c
1 /*
2  * arch/arch/mach-tegra/timer.c
3  *
4  * Copyright (C) 2010 Google, Inc.
5  *
6  * Author:
7  *      Colin Cross <ccross@google.com>
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20 #include <linux/init.h>
21 #include <linux/time.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/clockchips.h>
25 #include <linux/clocksource.h>
26 #include <linux/clk.h>
27 #include <linux/io.h>
28 #include <linux/cnt32_to_63.h>
29
30 #include <asm/mach/time.h>
31 #include <asm/mach/time.h>
32 #include <asm/localtimer.h>
33
34 #include <mach/iomap.h>
35 #include <mach/irqs.h>
36 #include <mach/suspend.h>
37
38 #include "board.h"
39 #include "clock.h"
40 #include "power.h"
41
42 #define RTC_SECONDS             0x08
43 #define RTC_SHADOW_SECONDS      0x0c
44 #define RTC_MILLISECONDS        0x10
45
46 #define TIMERUS_CNTR_1US 0x10
47 #define TIMERUS_USEC_CFG 0x14
48 #define TIMERUS_CNTR_FREEZE 0x4c
49
50 #define TIMER1_BASE 0x0
51 #define TIMER2_BASE 0x8
52 #define TIMER3_BASE 0x50
53 #define TIMER4_BASE 0x58
54
55 #define TIMER_PTV 0x0
56 #define TIMER_PCR 0x4
57
58 static void __iomem *timer_base = IO_ADDRESS(TEGRA_TMR1_BASE);
59 static void __iomem *rtc_base = IO_ADDRESS(TEGRA_RTC_BASE);
60
61 #define timer_writel(value, reg) \
62         __raw_writel(value, (u32)timer_base + (reg))
63 #define timer_readl(reg) \
64         __raw_readl((u32)timer_base + (reg))
65
66 static int tegra_timer_set_next_event(unsigned long cycles,
67                                          struct clock_event_device *evt)
68 {
69         u32 reg;
70
71         reg = 0x80000000 | ((cycles > 1) ? (cycles-1) : 0);
72         timer_writel(reg, TIMER3_BASE + TIMER_PTV);
73
74         return 0;
75 }
76
77 static void tegra_timer_set_mode(enum clock_event_mode mode,
78                                     struct clock_event_device *evt)
79 {
80         u32 reg;
81
82         timer_writel(0, TIMER3_BASE + TIMER_PTV);
83
84         switch (mode) {
85         case CLOCK_EVT_MODE_PERIODIC:
86                 reg = 0xC0000000 | ((1000000/HZ)-1);
87                 timer_writel(reg, TIMER3_BASE + TIMER_PTV);
88                 break;
89         case CLOCK_EVT_MODE_ONESHOT:
90                 break;
91         case CLOCK_EVT_MODE_UNUSED:
92         case CLOCK_EVT_MODE_SHUTDOWN:
93         case CLOCK_EVT_MODE_RESUME:
94                 break;
95         }
96 }
97
98 static u64 tegra_us_clocksource_offset;
99 static u64 tegra_us_resume_offset;
100 static cycle_t tegra_clocksource_us_read(struct clocksource *cs)
101 {
102         return tegra_us_clocksource_offset +
103                 cnt32_to_63(timer_readl(TIMERUS_CNTR_1US));
104 }
105
106 void tegra_clocksource_us_suspend(struct clocksource *cs)
107 {
108         tegra_us_resume_offset = tegra_clocksource_us_read(cs) -
109                 tegra_rtc_read_ms() * 1000;
110 }
111
112 void tegra_clocksource_us_resume(struct clocksource *cs)
113 {
114         tegra_us_clocksource_offset += tegra_us_resume_offset +
115                 tegra_rtc_read_ms() * 1000 -
116                 tegra_clocksource_us_read(cs);
117 }
118
119 static struct clock_event_device tegra_clockevent = {
120         .name           = "timer0",
121         .rating         = 300,
122         .features       = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
123         .set_next_event = tegra_timer_set_next_event,
124         .set_mode       = tegra_timer_set_mode,
125 };
126
127 static struct clocksource tegra_clocksource_us = {
128         .name   = "timer_us",
129         .rating = 300,
130         .read   = tegra_clocksource_us_read,
131         .suspend= tegra_clocksource_us_suspend,
132         .resume = tegra_clocksource_us_resume,
133         .mask   = 0x7FFFFFFFFFFFFFFFULL,
134         .flags  = CLOCK_SOURCE_IS_CONTINUOUS,
135 };
136
137 unsigned long long sched_clock(void)
138 {
139         return tegra_clocksource_us.read(&tegra_clocksource_us) * 1000;
140 }
141
142
143 /*
144  * tegra_rtc_read - Reads the Tegra RTC registers
145  * Care must be taken that this funciton is not called while the
146  * tegra_rtc driver could be executing to avoid race conditions
147  * on the RTC shadow register
148  */
149 u64 tegra_rtc_read_ms(void)
150 {
151         u32 ms = readl(rtc_base + RTC_MILLISECONDS);
152         u32 s = readl(rtc_base + RTC_SHADOW_SECONDS);
153         return (u64)s * 1000 + ms;
154 }
155
156 /*
157  * read_persistent_clock -  Return time from a persistent clock.
158  *
159  * Reads the time from a source which isn't disabled during PM, the
160  * 32k sync timer.  Convert the cycles elapsed since last read into
161  * nsecs and adds to a monotonically increasing timespec.
162  * Care must be taken that this funciton is not called while the
163  * tegra_rtc driver could be executing to avoid race conditions
164  * on the RTC shadow register
165  */
166 static struct timespec persistent_ts;
167 static u64 persistent_ms, last_persistent_ms;
168 void read_persistent_clock(struct timespec *ts)
169 {
170         u64 delta;
171         struct timespec *tsp = &persistent_ts;
172
173         last_persistent_ms = persistent_ms;
174         persistent_ms = tegra_rtc_read_ms();
175         delta = persistent_ms - last_persistent_ms;
176
177         timespec_add_ns(tsp, delta * 1000000);
178         *ts = *tsp;
179 }
180
181 static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id)
182 {
183         struct clock_event_device *evt = (struct clock_event_device *)dev_id;
184         timer_writel(1<<30, TIMER3_BASE + TIMER_PCR);
185         evt->event_handler(evt);
186         return IRQ_HANDLED;
187 }
188
189 static struct irqaction tegra_timer_irq = {
190         .name           = "timer0",
191         .flags          = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH,
192         .handler        = tegra_timer_interrupt,
193         .dev_id         = &tegra_clockevent,
194         .irq            = INT_TMR3,
195 };
196
197 static irqreturn_t tegra_lp2wake_interrupt(int irq, void *dev_id)
198 {
199         timer_writel(1<<30, TIMER4_BASE + TIMER_PCR);
200         return IRQ_HANDLED;
201 }
202
203 static struct irqaction tegra_lp2wake_irq = {
204         .name           = "timer_lp2wake",
205         .flags          = IRQF_DISABLED,
206         .handler        = tegra_lp2wake_interrupt,
207         .dev_id         = NULL,
208         .irq            = INT_TMR4,
209 };
210
211 static void __init tegra_init_timer(void)
212 {
213         unsigned long rate = clk_measure_input_freq();
214         int ret;
215
216 #ifdef CONFIG_HAVE_ARM_TWD
217         twd_base = IO_ADDRESS(TEGRA_ARM_PERIF_BASE + 0x600);
218 #endif
219
220         switch (rate) {
221         case 12000000:
222                 timer_writel(0x000b, TIMERUS_USEC_CFG);
223                 break;
224         case 13000000:
225                 timer_writel(0x000c, TIMERUS_USEC_CFG);
226                 break;
227         case 19200000:
228                 timer_writel(0x045f, TIMERUS_USEC_CFG);
229                 break;
230         case 26000000:
231                 timer_writel(0x0019, TIMERUS_USEC_CFG);
232                 break;
233         default:
234                 WARN(1, "Unknown clock rate");
235         }
236
237         if (clocksource_register_hz(&tegra_clocksource_us, 1000000)) {
238                 printk(KERN_ERR "Failed to register us clocksource\n");
239                 BUG();
240         }
241
242         ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq);
243         if (ret) {
244                 printk(KERN_ERR "Failed to register timer IRQ: %d\n", ret);
245                 BUG();
246         }
247
248         ret = setup_irq(tegra_lp2wake_irq.irq, &tegra_lp2wake_irq);
249         if (ret) {
250                 printk(KERN_ERR "Failed to register LP2 timer IRQ: %d\n", ret);
251                 BUG();
252         }
253
254         clockevents_calc_mult_shift(&tegra_clockevent, 1000000, 5);
255         tegra_clockevent.max_delta_ns =
256                 clockevent_delta2ns(0x1fffffff, &tegra_clockevent);
257         tegra_clockevent.min_delta_ns =
258                 clockevent_delta2ns(0x1, &tegra_clockevent);
259         tegra_clockevent.cpumask = cpu_all_mask;
260         tegra_clockevent.irq = tegra_timer_irq.irq;
261         clockevents_register_device(&tegra_clockevent);
262
263         return;
264 }
265
266 struct sys_timer tegra_timer = {
267         .init = tegra_init_timer,
268 };
269
270 void tegra_lp2_set_trigger(unsigned long cycles)
271 {
272         timer_writel(0, TIMER4_BASE + TIMER_PTV);
273         if (cycles) {
274                 u32 reg = 0x80000000ul | min(0x1ffffffful, cycles);
275                 timer_writel(reg, TIMER4_BASE + TIMER_PTV);
276         }
277 }
278 EXPORT_SYMBOL(tegra_lp2_set_trigger);
279
280 unsigned long tegra_lp2_timer_remain(void)
281 {
282         return timer_readl(TIMER4_BASE + TIMER_PCR) & 0x1ffffffful;
283 }
284
285 static u32 usec_config;
286 void tegra_timer_suspend(void)
287 {
288         usec_config = timer_readl(TIMERUS_USEC_CFG);
289 }
290
291 void tegra_timer_resume(void)
292 {
293         timer_writel(usec_config, TIMERUS_USEC_CFG);
294 }