ARM: OMAP: Add DMTIMER definitions for posted mode
[pandora-kernel.git] / arch / arm / plat-omap / include / plat / dmtimer.h
1 /*
2  * arch/arm/plat-omap/include/plat/dmtimer.h
3  *
4  * OMAP Dual-Mode Timers
5  *
6  * 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  *
12  * 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
35 #include <linux/clk.h>
36 #include <linux/delay.h>
37 #include <linux/io.h>
38 #include <linux/platform_device.h>
39
40 #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
58 /*
59  * IP revision identifier so that Highlander IP
60  * in OMAP4 can be distinguished.
61  */
62 #define OMAP_TIMER_IP_VERSION_1                        0x1
63
64 /* posted mode types */
65 #define OMAP_TIMER_NONPOSTED                    0x00
66 #define OMAP_TIMER_POSTED                       0x01
67
68 /* timer capabilities used in hwmod database */
69 #define OMAP_TIMER_SECURE                               0x80000000
70 #define OMAP_TIMER_ALWON                                0x40000000
71 #define OMAP_TIMER_HAS_PWM                              0x20000000
72
73 struct omap_timer_capability_dev_attr {
74         u32 timer_capability;
75 };
76
77 struct omap_dm_timer;
78 struct clk;
79
80 struct timer_regs {
81         u32 tidr;
82         u32 tier;
83         u32 twer;
84         u32 tclr;
85         u32 tcrr;
86         u32 tldr;
87         u32 ttrg;
88         u32 twps;
89         u32 tmar;
90         u32 tcar1;
91         u32 tsicr;
92         u32 tcar2;
93         u32 tpir;
94         u32 tnir;
95         u32 tcvr;
96         u32 tocr;
97         u32 towr;
98 };
99
100 struct dmtimer_platform_data {
101         int (*set_timer_src)(struct platform_device *pdev, int source);
102         int timer_ip_version;
103         u32 needs_manual_reset:1;
104         bool reserved;
105
106         bool loses_context;
107
108         int (*get_context_loss_count)(struct device *dev);
109 };
110
111 struct omap_dm_timer *omap_dm_timer_request(void);
112 struct omap_dm_timer *omap_dm_timer_request_specific(int timer_id);
113 int omap_dm_timer_free(struct omap_dm_timer *timer);
114 void omap_dm_timer_enable(struct omap_dm_timer *timer);
115 void omap_dm_timer_disable(struct omap_dm_timer *timer);
116
117 int omap_dm_timer_get_irq(struct omap_dm_timer *timer);
118
119 u32 omap_dm_timer_modify_idlect_mask(u32 inputmask);
120 struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer);
121
122 int omap_dm_timer_trigger(struct omap_dm_timer *timer);
123 int omap_dm_timer_start(struct omap_dm_timer *timer);
124 int omap_dm_timer_stop(struct omap_dm_timer *timer);
125
126 int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source);
127 int omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload, unsigned int value);
128 int omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload, unsigned int value);
129 int omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable, unsigned int match);
130 int omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on, int toggle, int trigger);
131 int omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler);
132
133 int omap_dm_timer_set_int_enable(struct omap_dm_timer *timer, unsigned int value);
134
135 unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer);
136 int omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value);
137 unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer);
138 int omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value);
139
140 int omap_dm_timers_active(void);
141
142 /*
143  * Do not use the defines below, they are not needed. They should be only
144  * used by dmtimer.c and sys_timer related code.
145  */
146
147 /*
148  * The interrupt registers are different between v1 and v2 ip.
149  * These registers are offsets from timer->iobase.
150  */
151 #define OMAP_TIMER_ID_OFFSET            0x00
152 #define OMAP_TIMER_OCP_CFG_OFFSET       0x10
153
154 #define OMAP_TIMER_V1_SYS_STAT_OFFSET   0x14
155 #define OMAP_TIMER_V1_STAT_OFFSET       0x18
156 #define OMAP_TIMER_V1_INT_EN_OFFSET     0x1c
157
158 #define OMAP_TIMER_V2_IRQSTATUS_RAW     0x24
159 #define OMAP_TIMER_V2_IRQSTATUS         0x28
160 #define OMAP_TIMER_V2_IRQENABLE_SET     0x2c
161 #define OMAP_TIMER_V2_IRQENABLE_CLR     0x30
162
163 /*
164  * The functional registers have a different base on v1 and v2 ip.
165  * These registers are offsets from timer->func_base. The func_base
166  * is samae as io_base for v1 and io_base + 0x14 for v2 ip.
167  *
168  */
169 #define OMAP_TIMER_V2_FUNC_OFFSET               0x14
170
171 #define _OMAP_TIMER_WAKEUP_EN_OFFSET    0x20
172 #define _OMAP_TIMER_CTRL_OFFSET         0x24
173 #define         OMAP_TIMER_CTRL_GPOCFG          (1 << 14)
174 #define         OMAP_TIMER_CTRL_CAPTMODE        (1 << 13)
175 #define         OMAP_TIMER_CTRL_PT              (1 << 12)
176 #define         OMAP_TIMER_CTRL_TCM_LOWTOHIGH   (0x1 << 8)
177 #define         OMAP_TIMER_CTRL_TCM_HIGHTOLOW   (0x2 << 8)
178 #define         OMAP_TIMER_CTRL_TCM_BOTHEDGES   (0x3 << 8)
179 #define         OMAP_TIMER_CTRL_SCPWM           (1 << 7)
180 #define         OMAP_TIMER_CTRL_CE              (1 << 6) /* compare enable */
181 #define         OMAP_TIMER_CTRL_PRE             (1 << 5) /* prescaler enable */
182 #define         OMAP_TIMER_CTRL_PTV_SHIFT       2 /* prescaler value shift */
183 #define         OMAP_TIMER_CTRL_POSTED          (1 << 2)
184 #define         OMAP_TIMER_CTRL_AR              (1 << 1) /* auto-reload enable */
185 #define         OMAP_TIMER_CTRL_ST              (1 << 0) /* start timer */
186 #define _OMAP_TIMER_COUNTER_OFFSET      0x28
187 #define _OMAP_TIMER_LOAD_OFFSET         0x2c
188 #define _OMAP_TIMER_TRIGGER_OFFSET      0x30
189 #define _OMAP_TIMER_WRITE_PEND_OFFSET   0x34
190 #define         WP_NONE                 0       /* no write pending bit */
191 #define         WP_TCLR                 (1 << 0)
192 #define         WP_TCRR                 (1 << 1)
193 #define         WP_TLDR                 (1 << 2)
194 #define         WP_TTGR                 (1 << 3)
195 #define         WP_TMAR                 (1 << 4)
196 #define         WP_TPIR                 (1 << 5)
197 #define         WP_TNIR                 (1 << 6)
198 #define         WP_TCVR                 (1 << 7)
199 #define         WP_TOCR                 (1 << 8)
200 #define         WP_TOWR                 (1 << 9)
201 #define _OMAP_TIMER_MATCH_OFFSET        0x38
202 #define _OMAP_TIMER_CAPTURE_OFFSET      0x3c
203 #define _OMAP_TIMER_IF_CTRL_OFFSET      0x40
204 #define _OMAP_TIMER_CAPTURE2_OFFSET             0x44    /* TCAR2, 34xx only */
205 #define _OMAP_TIMER_TICK_POS_OFFSET             0x48    /* TPIR, 34xx only */
206 #define _OMAP_TIMER_TICK_NEG_OFFSET             0x4c    /* TNIR, 34xx only */
207 #define _OMAP_TIMER_TICK_COUNT_OFFSET           0x50    /* TCVR, 34xx only */
208 #define _OMAP_TIMER_TICK_INT_MASK_SET_OFFSET    0x54    /* TOCR, 34xx only */
209 #define _OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET  0x58    /* TOWR, 34xx only */
210
211 /* register offsets with the write pending bit encoded */
212 #define WPSHIFT                                 16
213
214 #define OMAP_TIMER_WAKEUP_EN_REG                (_OMAP_TIMER_WAKEUP_EN_OFFSET \
215                                                         | (WP_NONE << WPSHIFT))
216
217 #define OMAP_TIMER_CTRL_REG                     (_OMAP_TIMER_CTRL_OFFSET \
218                                                         | (WP_TCLR << WPSHIFT))
219
220 #define OMAP_TIMER_COUNTER_REG                  (_OMAP_TIMER_COUNTER_OFFSET \
221                                                         | (WP_TCRR << WPSHIFT))
222
223 #define OMAP_TIMER_LOAD_REG                     (_OMAP_TIMER_LOAD_OFFSET \
224                                                         | (WP_TLDR << WPSHIFT))
225
226 #define OMAP_TIMER_TRIGGER_REG                  (_OMAP_TIMER_TRIGGER_OFFSET \
227                                                         | (WP_TTGR << WPSHIFT))
228
229 #define OMAP_TIMER_WRITE_PEND_REG               (_OMAP_TIMER_WRITE_PEND_OFFSET \
230                                                         | (WP_NONE << WPSHIFT))
231
232 #define OMAP_TIMER_MATCH_REG                    (_OMAP_TIMER_MATCH_OFFSET \
233                                                         | (WP_TMAR << WPSHIFT))
234
235 #define OMAP_TIMER_CAPTURE_REG                  (_OMAP_TIMER_CAPTURE_OFFSET \
236                                                         | (WP_NONE << WPSHIFT))
237
238 #define OMAP_TIMER_IF_CTRL_REG                  (_OMAP_TIMER_IF_CTRL_OFFSET \
239                                                         | (WP_NONE << WPSHIFT))
240
241 #define OMAP_TIMER_CAPTURE2_REG                 (_OMAP_TIMER_CAPTURE2_OFFSET \
242                                                         | (WP_NONE << WPSHIFT))
243
244 #define OMAP_TIMER_TICK_POS_REG                 (_OMAP_TIMER_TICK_POS_OFFSET \
245                                                         | (WP_TPIR << WPSHIFT))
246
247 #define OMAP_TIMER_TICK_NEG_REG                 (_OMAP_TIMER_TICK_NEG_OFFSET \
248                                                         | (WP_TNIR << WPSHIFT))
249
250 #define OMAP_TIMER_TICK_COUNT_REG               (_OMAP_TIMER_TICK_COUNT_OFFSET \
251                                                         | (WP_TCVR << WPSHIFT))
252
253 #define OMAP_TIMER_TICK_INT_MASK_SET_REG                                \
254                 (_OMAP_TIMER_TICK_INT_MASK_SET_OFFSET | (WP_TOCR << WPSHIFT))
255
256 #define OMAP_TIMER_TICK_INT_MASK_COUNT_REG                              \
257                 (_OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET | (WP_TOWR << WPSHIFT))
258
259 struct omap_dm_timer {
260         unsigned long phys_base;
261         int id;
262         int irq;
263         struct clk *fclk;
264
265         void __iomem    *io_base;
266         void __iomem    *sys_stat;      /* TISTAT timer status */
267         void __iomem    *irq_stat;      /* TISR/IRQSTATUS interrupt status */
268         void __iomem    *irq_ena;       /* irq enable */
269         void __iomem    *irq_dis;       /* irq disable, only on v2 ip */
270         void __iomem    *pend;          /* write pending */
271         void __iomem    *func_base;     /* function register base */
272
273         unsigned long rate;
274         unsigned reserved:1;
275         unsigned posted:1;
276         struct timer_regs context;
277         bool loses_context;
278         int ctx_loss_count;
279         int revision;
280         struct platform_device *pdev;
281         struct list_head node;
282
283         int (*get_context_loss_count)(struct device *dev);
284 };
285
286 int omap_dm_timer_prepare(struct omap_dm_timer *timer);
287
288 static inline u32 __omap_dm_timer_read(struct omap_dm_timer *timer, u32 reg,
289                                                 int posted)
290 {
291         if (posted)
292                 while (__raw_readl(timer->pend) & (reg >> WPSHIFT))
293                         cpu_relax();
294
295         return __raw_readl(timer->func_base + (reg & 0xff));
296 }
297
298 static inline void __omap_dm_timer_write(struct omap_dm_timer *timer,
299                                         u32 reg, u32 val, int posted)
300 {
301         if (posted)
302                 while (__raw_readl(timer->pend) & (reg >> WPSHIFT))
303                         cpu_relax();
304
305         __raw_writel(val, timer->func_base + (reg & 0xff));
306 }
307
308 static inline void __omap_dm_timer_init_regs(struct omap_dm_timer *timer)
309 {
310         u32 tidr;
311
312         /* Assume v1 ip if bits [31:16] are zero */
313         tidr = __raw_readl(timer->io_base);
314         if (!(tidr >> 16)) {
315                 timer->revision = 1;
316                 timer->sys_stat = timer->io_base +
317                                 OMAP_TIMER_V1_SYS_STAT_OFFSET;
318                 timer->irq_stat = timer->io_base + OMAP_TIMER_V1_STAT_OFFSET;
319                 timer->irq_ena = timer->io_base + OMAP_TIMER_V1_INT_EN_OFFSET;
320                 timer->irq_dis = 0;
321                 timer->pend = timer->io_base + _OMAP_TIMER_WRITE_PEND_OFFSET;
322                 timer->func_base = timer->io_base;
323         } else {
324                 timer->revision = 2;
325                 timer->sys_stat = 0;
326                 timer->irq_stat = timer->io_base + OMAP_TIMER_V2_IRQSTATUS;
327                 timer->irq_ena = timer->io_base + OMAP_TIMER_V2_IRQENABLE_SET;
328                 timer->irq_dis = timer->io_base + OMAP_TIMER_V2_IRQENABLE_CLR;
329                 timer->pend = timer->io_base +
330                         _OMAP_TIMER_WRITE_PEND_OFFSET +
331                                 OMAP_TIMER_V2_FUNC_OFFSET;
332                 timer->func_base = timer->io_base + OMAP_TIMER_V2_FUNC_OFFSET;
333         }
334 }
335
336 /* Assumes the source clock has been set by caller */
337 static inline void __omap_dm_timer_reset(struct omap_dm_timer *timer,
338                                         int autoidle, int wakeup)
339 {
340         u32 l;
341
342         l = __raw_readl(timer->io_base + OMAP_TIMER_OCP_CFG_OFFSET);
343         l |= 0x02 << 3;  /* Set to smart-idle mode */
344         l |= 0x2 << 8;   /* Set clock activity to perserve f-clock on idle */
345
346         if (autoidle)
347                 l |= 0x1 << 0;
348
349         if (wakeup)
350                 l |= 1 << 2;
351
352         __raw_writel(l, timer->io_base + OMAP_TIMER_OCP_CFG_OFFSET);
353
354         /* Match hardware reset default of posted mode */
355         __omap_dm_timer_write(timer, OMAP_TIMER_IF_CTRL_REG,
356                                         OMAP_TIMER_CTRL_POSTED, 0);
357 }
358
359 static inline int __omap_dm_timer_set_source(struct clk *timer_fck,
360                                                 struct clk *parent)
361 {
362         int ret;
363
364         clk_disable(timer_fck);
365         ret = clk_set_parent(timer_fck, parent);
366         clk_enable(timer_fck);
367
368         /*
369          * When the functional clock disappears, too quick writes seem
370          * to cause an abort. XXX Is this still necessary?
371          */
372         __delay(300000);
373
374         return ret;
375 }
376
377 static inline void __omap_dm_timer_stop(struct omap_dm_timer *timer,
378                                         int posted, unsigned long rate)
379 {
380         u32 l;
381
382         l = __omap_dm_timer_read(timer, OMAP_TIMER_CTRL_REG, posted);
383         if (l & OMAP_TIMER_CTRL_ST) {
384                 l &= ~0x1;
385                 __omap_dm_timer_write(timer, OMAP_TIMER_CTRL_REG, l, posted);
386 #ifdef CONFIG_ARCH_OMAP2PLUS
387                 /* Readback to make sure write has completed */
388                 __omap_dm_timer_read(timer, OMAP_TIMER_CTRL_REG, posted);
389                 /*
390                  * Wait for functional clock period x 3.5 to make sure that
391                  * timer is stopped
392                  */
393                 udelay(3500000 / rate + 1);
394 #endif
395         }
396
397         /* Ack possibly pending interrupt */
398         __raw_writel(OMAP_TIMER_INT_OVERFLOW, timer->irq_stat);
399 }
400
401 static inline void __omap_dm_timer_load_start(struct omap_dm_timer *timer,
402                                                 u32 ctrl, unsigned int load,
403                                                 int posted)
404 {
405         __omap_dm_timer_write(timer, OMAP_TIMER_COUNTER_REG, load, posted);
406         __omap_dm_timer_write(timer, OMAP_TIMER_CTRL_REG, ctrl, posted);
407 }
408
409 static inline void __omap_dm_timer_int_enable(struct omap_dm_timer *timer,
410                                                 unsigned int value)
411 {
412         __raw_writel(value, timer->irq_ena);
413         __omap_dm_timer_write(timer, OMAP_TIMER_WAKEUP_EN_REG, value, 0);
414 }
415
416 static inline unsigned int
417 __omap_dm_timer_read_counter(struct omap_dm_timer *timer, int posted)
418 {
419         return __omap_dm_timer_read(timer, OMAP_TIMER_COUNTER_REG, posted);
420 }
421
422 static inline void __omap_dm_timer_write_status(struct omap_dm_timer *timer,
423                                                 unsigned int value)
424 {
425         __raw_writel(value, timer->irq_stat);
426 }
427
428 #endif /* __ASM_ARCH_DMTIMER_H */