blob: 1751751862da1044084554fde4ebd55e6e58f687 [file] [log] [blame]
Russell Kinga09e64f2008-08-05 16:14:15 +01001/*
2 * arch/arm/plat-omap/include/mach/dmtimer.h
3 *
4 * OMAP Dual-Mode Timers
5 *
Thara Gopinatheddb1262011-02-23 00:14:04 -07006 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
7 * Tarun Kanti DebBarma <tarun.kanti@ti.com>
8 * Thara Gopinath <thara@ti.com>
9 *
10 * Platform device conversion and hwmod support.
11 *
Russell Kinga09e64f2008-08-05 16:14:15 +010012 * Copyright (C) 2005 Nokia Corporation
13 * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com>
14 * PWM and clock framwork support by Timo Teras.
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2 of the License, or (at your
19 * option) any later version.
20 *
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
24 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * You should have received a copy of the GNU General Public License along
31 * with this program; if not, write to the Free Software Foundation, Inc.,
32 * 675 Mass Ave, Cambridge, MA 02139, USA.
33 */
34
Tony Lindgrencaf64f22011-03-29 15:54:48 -070035#include <linux/clk.h>
36#include <linux/delay.h>
Paul Walmsleya7cd4b082011-07-09 18:00:25 -060037#include <linux/io.h>
Tarun Kanti DebBarma97933d62011-09-20 17:00:17 +053038#include <linux/platform_device.h>
Tony Lindgrencaf64f22011-03-29 15:54:48 -070039
Russell Kinga09e64f2008-08-05 16:14:15 +010040#ifndef __ASM_ARCH_DMTIMER_H
41#define __ASM_ARCH_DMTIMER_H
42
43/* clock sources */
44#define OMAP_TIMER_SRC_SYS_CLK 0x00
45#define OMAP_TIMER_SRC_32_KHZ 0x01
46#define OMAP_TIMER_SRC_EXT_CLK 0x02
47
48/* timer interrupt enable bits */
49#define OMAP_TIMER_INT_CAPTURE (1 << 2)
50#define OMAP_TIMER_INT_OVERFLOW (1 << 1)
51#define OMAP_TIMER_INT_MATCH (1 << 0)
52
53/* trigger types */
54#define OMAP_TIMER_TRIGGER_NONE 0x00
55#define OMAP_TIMER_TRIGGER_OVERFLOW 0x01
56#define OMAP_TIMER_TRIGGER_OVERFLOW_AND_COMPARE 0x02
57
Thara Gopinatheddb1262011-02-23 00:14:04 -070058/*
59 * IP revision identifier so that Highlander IP
60 * in OMAP4 can be distinguished.
61 */
62#define OMAP_TIMER_IP_VERSION_1 0x1
Russell Kinga09e64f2008-08-05 16:14:15 +010063struct omap_dm_timer;
64struct clk;
65
Tarun Kanti DebBarma97933d62011-09-20 17:00:17 +053066struct dmtimer_platform_data {
67 int (*set_timer_src)(struct platform_device *pdev, int source);
68 int timer_ip_version;
69 u32 needs_manual_reset:1;
70};
71
Russell Kinga09e64f2008-08-05 16:14:15 +010072struct omap_dm_timer *omap_dm_timer_request(void);
73struct omap_dm_timer *omap_dm_timer_request_specific(int timer_id);
74void omap_dm_timer_free(struct omap_dm_timer *timer);
75void omap_dm_timer_enable(struct omap_dm_timer *timer);
76void omap_dm_timer_disable(struct omap_dm_timer *timer);
77
78int omap_dm_timer_get_irq(struct omap_dm_timer *timer);
79
80u32 omap_dm_timer_modify_idlect_mask(u32 inputmask);
81struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer);
82
83void omap_dm_timer_trigger(struct omap_dm_timer *timer);
84void omap_dm_timer_start(struct omap_dm_timer *timer);
85void omap_dm_timer_stop(struct omap_dm_timer *timer);
86
Paul Walmsleyf2480762009-04-23 21:11:10 -060087int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source);
Russell Kinga09e64f2008-08-05 16:14:15 +010088void omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload, unsigned int value);
89void omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload, unsigned int value);
90void omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable, unsigned int match);
91void omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on, int toggle, int trigger);
92void omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler);
93
94void omap_dm_timer_set_int_enable(struct omap_dm_timer *timer, unsigned int value);
95
96unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer);
97void omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value);
98unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer);
99void omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value);
100
101int omap_dm_timers_active(void);
102
Tony Lindgrenec974892011-03-29 15:54:48 -0700103/*
104 * Do not use the defines below, they are not needed. They should be only
105 * used by dmtimer.c and sys_timer related code.
106 */
107
Tony Lindgrenee17f112011-09-16 15:44:20 -0700108/*
109 * The interrupt registers are different between v1 and v2 ip.
110 * These registers are offsets from timer->iobase.
111 */
112#define OMAP_TIMER_ID_OFFSET 0x00
113#define OMAP_TIMER_OCP_CFG_OFFSET 0x10
114
115#define OMAP_TIMER_V1_SYS_STAT_OFFSET 0x14
116#define OMAP_TIMER_V1_STAT_OFFSET 0x18
117#define OMAP_TIMER_V1_INT_EN_OFFSET 0x1c
118
119#define OMAP_TIMER_V2_IRQSTATUS_RAW 0x24
120#define OMAP_TIMER_V2_IRQSTATUS 0x28
121#define OMAP_TIMER_V2_IRQENABLE_SET 0x2c
122#define OMAP_TIMER_V2_IRQENABLE_CLR 0x30
123
124/*
125 * The functional registers have a different base on v1 and v2 ip.
126 * These registers are offsets from timer->func_base. The func_base
127 * is samae as io_base for v1 and io_base + 0x14 for v2 ip.
128 *
129 */
130#define OMAP_TIMER_V2_FUNC_OFFSET 0x14
131
Tony Lindgrenec974892011-03-29 15:54:48 -0700132#define _OMAP_TIMER_WAKEUP_EN_OFFSET 0x20
133#define _OMAP_TIMER_CTRL_OFFSET 0x24
134#define OMAP_TIMER_CTRL_GPOCFG (1 << 14)
135#define OMAP_TIMER_CTRL_CAPTMODE (1 << 13)
136#define OMAP_TIMER_CTRL_PT (1 << 12)
137#define OMAP_TIMER_CTRL_TCM_LOWTOHIGH (0x1 << 8)
138#define OMAP_TIMER_CTRL_TCM_HIGHTOLOW (0x2 << 8)
139#define OMAP_TIMER_CTRL_TCM_BOTHEDGES (0x3 << 8)
140#define OMAP_TIMER_CTRL_SCPWM (1 << 7)
141#define OMAP_TIMER_CTRL_CE (1 << 6) /* compare enable */
142#define OMAP_TIMER_CTRL_PRE (1 << 5) /* prescaler enable */
143#define OMAP_TIMER_CTRL_PTV_SHIFT 2 /* prescaler value shift */
144#define OMAP_TIMER_CTRL_POSTED (1 << 2)
145#define OMAP_TIMER_CTRL_AR (1 << 1) /* auto-reload enable */
146#define OMAP_TIMER_CTRL_ST (1 << 0) /* start timer */
147#define _OMAP_TIMER_COUNTER_OFFSET 0x28
148#define _OMAP_TIMER_LOAD_OFFSET 0x2c
149#define _OMAP_TIMER_TRIGGER_OFFSET 0x30
150#define _OMAP_TIMER_WRITE_PEND_OFFSET 0x34
151#define WP_NONE 0 /* no write pending bit */
152#define WP_TCLR (1 << 0)
153#define WP_TCRR (1 << 1)
154#define WP_TLDR (1 << 2)
155#define WP_TTGR (1 << 3)
156#define WP_TMAR (1 << 4)
157#define WP_TPIR (1 << 5)
158#define WP_TNIR (1 << 6)
159#define WP_TCVR (1 << 7)
160#define WP_TOCR (1 << 8)
161#define WP_TOWR (1 << 9)
162#define _OMAP_TIMER_MATCH_OFFSET 0x38
163#define _OMAP_TIMER_CAPTURE_OFFSET 0x3c
164#define _OMAP_TIMER_IF_CTRL_OFFSET 0x40
165#define _OMAP_TIMER_CAPTURE2_OFFSET 0x44 /* TCAR2, 34xx only */
166#define _OMAP_TIMER_TICK_POS_OFFSET 0x48 /* TPIR, 34xx only */
167#define _OMAP_TIMER_TICK_NEG_OFFSET 0x4c /* TNIR, 34xx only */
168#define _OMAP_TIMER_TICK_COUNT_OFFSET 0x50 /* TCVR, 34xx only */
169#define _OMAP_TIMER_TICK_INT_MASK_SET_OFFSET 0x54 /* TOCR, 34xx only */
170#define _OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET 0x58 /* TOWR, 34xx only */
171
172/* register offsets with the write pending bit encoded */
173#define WPSHIFT 16
174
Tony Lindgrenec974892011-03-29 15:54:48 -0700175#define OMAP_TIMER_WAKEUP_EN_REG (_OMAP_TIMER_WAKEUP_EN_OFFSET \
176 | (WP_NONE << WPSHIFT))
177
178#define OMAP_TIMER_CTRL_REG (_OMAP_TIMER_CTRL_OFFSET \
179 | (WP_TCLR << WPSHIFT))
180
181#define OMAP_TIMER_COUNTER_REG (_OMAP_TIMER_COUNTER_OFFSET \
182 | (WP_TCRR << WPSHIFT))
183
184#define OMAP_TIMER_LOAD_REG (_OMAP_TIMER_LOAD_OFFSET \
185 | (WP_TLDR << WPSHIFT))
186
187#define OMAP_TIMER_TRIGGER_REG (_OMAP_TIMER_TRIGGER_OFFSET \
188 | (WP_TTGR << WPSHIFT))
189
190#define OMAP_TIMER_WRITE_PEND_REG (_OMAP_TIMER_WRITE_PEND_OFFSET \
191 | (WP_NONE << WPSHIFT))
192
193#define OMAP_TIMER_MATCH_REG (_OMAP_TIMER_MATCH_OFFSET \
194 | (WP_TMAR << WPSHIFT))
195
196#define OMAP_TIMER_CAPTURE_REG (_OMAP_TIMER_CAPTURE_OFFSET \
197 | (WP_NONE << WPSHIFT))
198
199#define OMAP_TIMER_IF_CTRL_REG (_OMAP_TIMER_IF_CTRL_OFFSET \
200 | (WP_NONE << WPSHIFT))
201
202#define OMAP_TIMER_CAPTURE2_REG (_OMAP_TIMER_CAPTURE2_OFFSET \
203 | (WP_NONE << WPSHIFT))
204
205#define OMAP_TIMER_TICK_POS_REG (_OMAP_TIMER_TICK_POS_OFFSET \
206 | (WP_TPIR << WPSHIFT))
207
208#define OMAP_TIMER_TICK_NEG_REG (_OMAP_TIMER_TICK_NEG_OFFSET \
209 | (WP_TNIR << WPSHIFT))
210
211#define OMAP_TIMER_TICK_COUNT_REG (_OMAP_TIMER_TICK_COUNT_OFFSET \
212 | (WP_TCVR << WPSHIFT))
213
214#define OMAP_TIMER_TICK_INT_MASK_SET_REG \
215 (_OMAP_TIMER_TICK_INT_MASK_SET_OFFSET | (WP_TOCR << WPSHIFT))
216
217#define OMAP_TIMER_TICK_INT_MASK_COUNT_REG \
218 (_OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET | (WP_TOWR << WPSHIFT))
219
220struct omap_dm_timer {
221 unsigned long phys_base;
222 int irq;
223#ifdef CONFIG_ARCH_OMAP2PLUS
224 struct clk *iclk, *fclk;
225#endif
Tony Lindgrenee17f112011-09-16 15:44:20 -0700226 void __iomem *io_base;
227 void __iomem *sys_stat; /* TISTAT timer status */
228 void __iomem *irq_stat; /* TISR/IRQSTATUS interrupt status */
229 void __iomem *irq_ena; /* irq enable */
230 void __iomem *irq_dis; /* irq disable, only on v2 ip */
231 void __iomem *pend; /* write pending */
232 void __iomem *func_base; /* function register base */
233
Tony Lindgrenaa561882011-03-29 15:54:48 -0700234 unsigned long rate;
Tony Lindgrenec974892011-03-29 15:54:48 -0700235 unsigned reserved:1;
236 unsigned enabled:1;
237 unsigned posted:1;
Tarun Kanti DebBarma97933d62011-09-20 17:00:17 +0530238 struct platform_device *pdev;
Tony Lindgrenec974892011-03-29 15:54:48 -0700239};
Russell Kinga09e64f2008-08-05 16:14:15 +0100240
Tony Lindgren11a01862011-03-29 15:54:49 -0700241extern u32 sys_timer_reserved;
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700242void omap_dm_timer_prepare(struct omap_dm_timer *timer);
243
Tony Lindgrenee17f112011-09-16 15:44:20 -0700244static inline u32 __omap_dm_timer_read(struct omap_dm_timer *timer, u32 reg,
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700245 int posted)
246{
247 if (posted)
Tony Lindgrenee17f112011-09-16 15:44:20 -0700248 while (__raw_readl(timer->pend) & (reg >> WPSHIFT))
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700249 cpu_relax();
250
Tony Lindgrenee17f112011-09-16 15:44:20 -0700251 return __raw_readl(timer->func_base + (reg & 0xff));
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700252}
253
Tony Lindgrenee17f112011-09-16 15:44:20 -0700254static inline void __omap_dm_timer_write(struct omap_dm_timer *timer,
255 u32 reg, u32 val, int posted)
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700256{
257 if (posted)
Tony Lindgrenee17f112011-09-16 15:44:20 -0700258 while (__raw_readl(timer->pend) & (reg >> WPSHIFT))
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700259 cpu_relax();
260
Tony Lindgrenee17f112011-09-16 15:44:20 -0700261 __raw_writel(val, timer->func_base + (reg & 0xff));
262}
263
264static inline void __omap_dm_timer_init_regs(struct omap_dm_timer *timer)
265{
266 u32 tidr;
267
268 /* Assume v1 ip if bits [31:16] are zero */
269 tidr = __raw_readl(timer->io_base);
270 if (!(tidr >> 16)) {
271 timer->sys_stat = timer->io_base +
272 OMAP_TIMER_V1_SYS_STAT_OFFSET;
273 timer->irq_stat = timer->io_base + OMAP_TIMER_V1_STAT_OFFSET;
274 timer->irq_ena = timer->io_base + OMAP_TIMER_V1_INT_EN_OFFSET;
275 timer->irq_dis = 0;
276 timer->pend = timer->io_base + _OMAP_TIMER_WRITE_PEND_OFFSET;
277 timer->func_base = timer->io_base;
278 } else {
279 timer->sys_stat = 0;
280 timer->irq_stat = timer->io_base + OMAP_TIMER_V2_IRQSTATUS;
281 timer->irq_ena = timer->io_base + OMAP_TIMER_V2_IRQENABLE_SET;
282 timer->irq_dis = timer->io_base + OMAP_TIMER_V2_IRQENABLE_CLR;
283 timer->pend = timer->io_base +
284 _OMAP_TIMER_WRITE_PEND_OFFSET +
285 OMAP_TIMER_V2_FUNC_OFFSET;
286 timer->func_base = timer->io_base + OMAP_TIMER_V2_FUNC_OFFSET;
287 }
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700288}
289
290/* Assumes the source clock has been set by caller */
Tony Lindgrenee17f112011-09-16 15:44:20 -0700291static inline void __omap_dm_timer_reset(struct omap_dm_timer *timer,
292 int autoidle, int wakeup)
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700293{
294 u32 l;
295
Tony Lindgrenee17f112011-09-16 15:44:20 -0700296 l = __raw_readl(timer->io_base + OMAP_TIMER_OCP_CFG_OFFSET);
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700297 l |= 0x02 << 3; /* Set to smart-idle mode */
298 l |= 0x2 << 8; /* Set clock activity to perserve f-clock on idle */
299
300 if (autoidle)
301 l |= 0x1 << 0;
302
303 if (wakeup)
304 l |= 1 << 2;
305
Tony Lindgrenee17f112011-09-16 15:44:20 -0700306 __raw_writel(l, timer->io_base + OMAP_TIMER_OCP_CFG_OFFSET);
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700307
308 /* Match hardware reset default of posted mode */
Tony Lindgrenee17f112011-09-16 15:44:20 -0700309 __omap_dm_timer_write(timer, OMAP_TIMER_IF_CTRL_REG,
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700310 OMAP_TIMER_CTRL_POSTED, 0);
311}
312
313static inline int __omap_dm_timer_set_source(struct clk *timer_fck,
314 struct clk *parent)
315{
316 int ret;
317
318 clk_disable(timer_fck);
319 ret = clk_set_parent(timer_fck, parent);
320 clk_enable(timer_fck);
321
322 /*
323 * When the functional clock disappears, too quick writes seem
324 * to cause an abort. XXX Is this still necessary?
325 */
326 __delay(300000);
327
328 return ret;
329}
330
Tony Lindgrenee17f112011-09-16 15:44:20 -0700331static inline void __omap_dm_timer_stop(struct omap_dm_timer *timer,
332 int posted, unsigned long rate)
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700333{
334 u32 l;
335
Tony Lindgrenee17f112011-09-16 15:44:20 -0700336 l = __omap_dm_timer_read(timer, OMAP_TIMER_CTRL_REG, posted);
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700337 if (l & OMAP_TIMER_CTRL_ST) {
338 l &= ~0x1;
Tony Lindgrenee17f112011-09-16 15:44:20 -0700339 __omap_dm_timer_write(timer, OMAP_TIMER_CTRL_REG, l, posted);
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700340#ifdef CONFIG_ARCH_OMAP2PLUS
341 /* Readback to make sure write has completed */
Tony Lindgrenee17f112011-09-16 15:44:20 -0700342 __omap_dm_timer_read(timer, OMAP_TIMER_CTRL_REG, posted);
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700343 /*
344 * Wait for functional clock period x 3.5 to make sure that
345 * timer is stopped
346 */
347 udelay(3500000 / rate + 1);
348#endif
349 }
350
351 /* Ack possibly pending interrupt */
Tony Lindgrenee17f112011-09-16 15:44:20 -0700352 __raw_writel(OMAP_TIMER_INT_OVERFLOW, timer->irq_stat);
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700353}
354
Tony Lindgrenee17f112011-09-16 15:44:20 -0700355static inline void __omap_dm_timer_load_start(struct omap_dm_timer *timer,
356 u32 ctrl, unsigned int load,
357 int posted)
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700358{
Tony Lindgrenee17f112011-09-16 15:44:20 -0700359 __omap_dm_timer_write(timer, OMAP_TIMER_COUNTER_REG, load, posted);
360 __omap_dm_timer_write(timer, OMAP_TIMER_CTRL_REG, ctrl, posted);
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700361}
362
Tony Lindgrenee17f112011-09-16 15:44:20 -0700363static inline void __omap_dm_timer_int_enable(struct omap_dm_timer *timer,
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700364 unsigned int value)
365{
Tony Lindgrenee17f112011-09-16 15:44:20 -0700366 __raw_writel(value, timer->irq_ena);
367 __omap_dm_timer_write(timer, OMAP_TIMER_WAKEUP_EN_REG, value, 0);
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700368}
369
Tony Lindgrenee17f112011-09-16 15:44:20 -0700370static inline unsigned int
371__omap_dm_timer_read_counter(struct omap_dm_timer *timer, int posted)
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700372{
Tony Lindgrenee17f112011-09-16 15:44:20 -0700373 return __omap_dm_timer_read(timer, OMAP_TIMER_COUNTER_REG, posted);
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700374}
375
Tony Lindgrenee17f112011-09-16 15:44:20 -0700376static inline void __omap_dm_timer_write_status(struct omap_dm_timer *timer,
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700377 unsigned int value)
378{
Tony Lindgrenee17f112011-09-16 15:44:20 -0700379 __raw_writel(value, timer->irq_stat);
Tony Lindgrencaf64f22011-03-29 15:54:48 -0700380}
381
Russell Kinga09e64f2008-08-05 16:14:15 +0100382#endif /* __ASM_ARCH_DMTIMER_H */