1 // SPDX-License-Identifier: GPL-2.0+
3 * linux/arch/arm/plat-omap/dmtimer.c
5 * OMAP Dual-Mode Timers
7 * Copyright (C) 2010 Texas Instruments Incorporated - https://www.ti.com/
8 * Tarun Kanti DebBarma <tarun.kanti@ti.com>
9 * Thara Gopinath <thara@ti.com>
11 * dmtimer adaptation to platform_driver.
13 * Copyright (C) 2005 Nokia Corporation
14 * OMAP2 support by Juha Yrjola
15 * API improvements and OMAP2 clock framework support by Timo Teras
17 * Copyright (C) 2009 Texas Instruments
18 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
21 #include <linux/clk.h>
22 #include <linux/clk-provider.h>
23 #include <linux/cpu_pm.h>
24 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/err.h>
28 #include <linux/pm_runtime.h>
30 #include <linux/platform_device.h>
31 #include <linux/platform_data/dmtimer-omap.h>
33 #include <clocksource/timer-ti-dm.h>
38 * Errata i103/i767 impacts all OMAP3/4/5 devices including AM33xx. This
39 * errata prevents us from using posted mode on these devices, unless the
40 * timer counter register is never read. For more details please refer to
41 * the OMAP3/4/5 errata documents.
43 #define OMAP_TIMER_ERRATA_I103_I767 0x80000000
45 /* posted mode types */
46 #define OMAP_TIMER_NONPOSTED 0x00
47 #define OMAP_TIMER_POSTED 0x01
49 /* register offsets with the write pending bit encoded */
52 #define OMAP_TIMER_WAKEUP_EN_REG (_OMAP_TIMER_WAKEUP_EN_OFFSET \
53 | (WP_NONE << WPSHIFT))
55 #define OMAP_TIMER_CTRL_REG (_OMAP_TIMER_CTRL_OFFSET \
56 | (WP_TCLR << WPSHIFT))
58 #define OMAP_TIMER_COUNTER_REG (_OMAP_TIMER_COUNTER_OFFSET \
59 | (WP_TCRR << WPSHIFT))
61 #define OMAP_TIMER_LOAD_REG (_OMAP_TIMER_LOAD_OFFSET \
62 | (WP_TLDR << WPSHIFT))
64 #define OMAP_TIMER_TRIGGER_REG (_OMAP_TIMER_TRIGGER_OFFSET \
65 | (WP_TTGR << WPSHIFT))
67 #define OMAP_TIMER_WRITE_PEND_REG (_OMAP_TIMER_WRITE_PEND_OFFSET \
68 | (WP_NONE << WPSHIFT))
70 #define OMAP_TIMER_MATCH_REG (_OMAP_TIMER_MATCH_OFFSET \
71 | (WP_TMAR << WPSHIFT))
73 #define OMAP_TIMER_CAPTURE_REG (_OMAP_TIMER_CAPTURE_OFFSET \
74 | (WP_NONE << WPSHIFT))
76 #define OMAP_TIMER_IF_CTRL_REG (_OMAP_TIMER_IF_CTRL_OFFSET \
77 | (WP_NONE << WPSHIFT))
79 #define OMAP_TIMER_CAPTURE2_REG (_OMAP_TIMER_CAPTURE2_OFFSET \
80 | (WP_NONE << WPSHIFT))
82 #define OMAP_TIMER_TICK_POS_REG (_OMAP_TIMER_TICK_POS_OFFSET \
83 | (WP_TPIR << WPSHIFT))
85 #define OMAP_TIMER_TICK_NEG_REG (_OMAP_TIMER_TICK_NEG_OFFSET \
86 | (WP_TNIR << WPSHIFT))
88 #define OMAP_TIMER_TICK_COUNT_REG (_OMAP_TIMER_TICK_COUNT_OFFSET \
89 | (WP_TCVR << WPSHIFT))
91 #define OMAP_TIMER_TICK_INT_MASK_SET_REG \
92 (_OMAP_TIMER_TICK_INT_MASK_SET_OFFSET | (WP_TOCR << WPSHIFT))
94 #define OMAP_TIMER_TICK_INT_MASK_COUNT_REG \
95 (_OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET | (WP_TOWR << WPSHIFT))
119 struct omap_dm_timer cookie
;
124 void __iomem
*io_base
;
125 int irq_stat
; /* TISR/IRQSTATUS interrupt status */
126 int irq_ena
; /* irq enable */
127 int irq_dis
; /* irq disable, only on v2 ip */
128 void __iomem
*pend
; /* write pending */
129 void __iomem
*func_base
; /* function register base */
135 struct timer_regs context
;
139 struct platform_device
*pdev
;
140 struct list_head node
;
141 struct notifier_block nb
;
142 struct notifier_block fclk_nb
;
143 unsigned long fclk_rate
;
146 static u32 omap_reserved_systimers
;
147 static LIST_HEAD(omap_timer_list
);
148 static DEFINE_SPINLOCK(dm_timer_lock
);
158 * dmtimer_read - read timer registers in posted and non-posted mode
159 * @timer: timer pointer over which read operation to perform
160 * @reg: lowest byte holds the register offset
162 * The posted mode bit is encoded in reg. Note that in posted mode, write
163 * pending bit must be checked. Otherwise a read of a non completed write
164 * will produce an error.
166 static inline u32
dmtimer_read(struct dmtimer
*timer
, u32 reg
)
173 /* Wait for a possible write pending bit in posted mode */
174 if (wp
&& timer
->posted
)
175 while (readl_relaxed(timer
->pend
) & wp
)
178 return readl_relaxed(timer
->func_base
+ offset
);
182 * dmtimer_write - write timer registers in posted and non-posted mode
183 * @timer: timer pointer over which write operation is to perform
184 * @reg: lowest byte holds the register offset
185 * @val: data to write into the register
187 * The posted mode bit is encoded in reg. Note that in posted mode, the write
188 * pending bit must be checked. Otherwise a write on a register which has a
189 * pending write will be lost.
191 static inline void dmtimer_write(struct dmtimer
*timer
, u32 reg
, u32 val
)
198 /* Wait for a possible write pending bit in posted mode */
199 if (wp
&& timer
->posted
)
200 while (readl_relaxed(timer
->pend
) & wp
)
203 writel_relaxed(val
, timer
->func_base
+ offset
);
206 static inline void __omap_dm_timer_init_regs(struct dmtimer
*timer
)
210 /* Assume v1 ip if bits [31:16] are zero */
211 tidr
= readl_relaxed(timer
->io_base
);
214 timer
->irq_stat
= OMAP_TIMER_V1_STAT_OFFSET
;
215 timer
->irq_ena
= OMAP_TIMER_V1_INT_EN_OFFSET
;
216 timer
->irq_dis
= OMAP_TIMER_V1_INT_EN_OFFSET
;
217 timer
->pend
= timer
->io_base
+ _OMAP_TIMER_WRITE_PEND_OFFSET
;
218 timer
->func_base
= timer
->io_base
;
221 timer
->irq_stat
= OMAP_TIMER_V2_IRQSTATUS
- OMAP_TIMER_V2_FUNC_OFFSET
;
222 timer
->irq_ena
= OMAP_TIMER_V2_IRQENABLE_SET
- OMAP_TIMER_V2_FUNC_OFFSET
;
223 timer
->irq_dis
= OMAP_TIMER_V2_IRQENABLE_CLR
- OMAP_TIMER_V2_FUNC_OFFSET
;
224 timer
->pend
= timer
->io_base
+
225 _OMAP_TIMER_WRITE_PEND_OFFSET
+
226 OMAP_TIMER_V2_FUNC_OFFSET
;
227 timer
->func_base
= timer
->io_base
+ OMAP_TIMER_V2_FUNC_OFFSET
;
232 * __omap_dm_timer_enable_posted - enables write posted mode
233 * @timer: pointer to timer instance handle
235 * Enables the write posted mode for the timer. When posted mode is enabled
236 * writes to certain timer registers are immediately acknowledged by the
237 * internal bus and hence prevents stalling the CPU waiting for the write to
238 * complete. Enabling this feature can improve performance for writing to the
241 static inline void __omap_dm_timer_enable_posted(struct dmtimer
*timer
)
246 if (timer
->errata
& OMAP_TIMER_ERRATA_I103_I767
) {
247 timer
->posted
= OMAP_TIMER_NONPOSTED
;
248 dmtimer_write(timer
, OMAP_TIMER_IF_CTRL_REG
, 0);
252 dmtimer_write(timer
, OMAP_TIMER_IF_CTRL_REG
, OMAP_TIMER_CTRL_POSTED
);
253 timer
->context
.tsicr
= OMAP_TIMER_CTRL_POSTED
;
254 timer
->posted
= OMAP_TIMER_POSTED
;
257 static inline void __omap_dm_timer_stop(struct dmtimer
*timer
)
261 l
= dmtimer_read(timer
, OMAP_TIMER_CTRL_REG
);
262 if (l
& OMAP_TIMER_CTRL_ST
) {
264 dmtimer_write(timer
, OMAP_TIMER_CTRL_REG
, l
);
265 #ifdef CONFIG_ARCH_OMAP2PLUS
266 /* Readback to make sure write has completed */
267 dmtimer_read(timer
, OMAP_TIMER_CTRL_REG
);
269 * Wait for functional clock period x 3.5 to make sure that
272 udelay(3500000 / timer
->fclk_rate
+ 1);
276 /* Ack possibly pending interrupt */
277 dmtimer_write(timer
, timer
->irq_stat
, OMAP_TIMER_INT_OVERFLOW
);
280 static inline void __omap_dm_timer_int_enable(struct dmtimer
*timer
,
283 dmtimer_write(timer
, timer
->irq_ena
, value
);
284 dmtimer_write(timer
, OMAP_TIMER_WAKEUP_EN_REG
, value
);
287 static inline unsigned int
288 __omap_dm_timer_read_counter(struct dmtimer
*timer
)
290 return dmtimer_read(timer
, OMAP_TIMER_COUNTER_REG
);
293 static inline void __omap_dm_timer_write_status(struct dmtimer
*timer
,
296 dmtimer_write(timer
, timer
->irq_stat
, value
);
299 static void omap_timer_restore_context(struct dmtimer
*timer
)
301 dmtimer_write(timer
, OMAP_TIMER_OCP_CFG_OFFSET
, timer
->context
.ocp_cfg
);
303 dmtimer_write(timer
, OMAP_TIMER_WAKEUP_EN_REG
, timer
->context
.twer
);
304 dmtimer_write(timer
, OMAP_TIMER_COUNTER_REG
, timer
->context
.tcrr
);
305 dmtimer_write(timer
, OMAP_TIMER_LOAD_REG
, timer
->context
.tldr
);
306 dmtimer_write(timer
, OMAP_TIMER_MATCH_REG
, timer
->context
.tmar
);
307 dmtimer_write(timer
, OMAP_TIMER_IF_CTRL_REG
, timer
->context
.tsicr
);
308 dmtimer_write(timer
, timer
->irq_ena
, timer
->context
.tier
);
309 dmtimer_write(timer
, OMAP_TIMER_CTRL_REG
, timer
->context
.tclr
);
312 static void omap_timer_save_context(struct dmtimer
*timer
)
314 timer
->context
.ocp_cfg
= dmtimer_read(timer
, OMAP_TIMER_OCP_CFG_OFFSET
);
316 timer
->context
.tclr
= dmtimer_read(timer
, OMAP_TIMER_CTRL_REG
);
317 timer
->context
.twer
= dmtimer_read(timer
, OMAP_TIMER_WAKEUP_EN_REG
);
318 timer
->context
.tldr
= dmtimer_read(timer
, OMAP_TIMER_LOAD_REG
);
319 timer
->context
.tmar
= dmtimer_read(timer
, OMAP_TIMER_MATCH_REG
);
320 timer
->context
.tier
= dmtimer_read(timer
, timer
->irq_ena
);
321 timer
->context
.tsicr
= dmtimer_read(timer
, OMAP_TIMER_IF_CTRL_REG
);
324 static int omap_timer_context_notifier(struct notifier_block
*nb
,
325 unsigned long cmd
, void *v
)
327 struct dmtimer
*timer
;
329 timer
= container_of(nb
, struct dmtimer
, nb
);
332 case CPU_CLUSTER_PM_ENTER
:
333 if ((timer
->capability
& OMAP_TIMER_ALWON
) ||
334 !atomic_read(&timer
->enabled
))
336 omap_timer_save_context(timer
);
338 case CPU_CLUSTER_PM_ENTER_FAILED
: /* No need to restore context */
340 case CPU_CLUSTER_PM_EXIT
:
341 if ((timer
->capability
& OMAP_TIMER_ALWON
) ||
342 !atomic_read(&timer
->enabled
))
344 omap_timer_restore_context(timer
);
351 static int omap_timer_fclk_notifier(struct notifier_block
*nb
,
352 unsigned long event
, void *data
)
354 struct clk_notifier_data
*clk_data
= data
;
355 struct dmtimer
*timer
= container_of(nb
, struct dmtimer
, fclk_nb
);
358 case POST_RATE_CHANGE
:
359 timer
->fclk_rate
= clk_data
->new_rate
;
366 static int omap_dm_timer_reset(struct dmtimer
*timer
)
368 u32 l
, timeout
= 100000;
370 if (timer
->revision
!= 1)
373 dmtimer_write(timer
, OMAP_TIMER_IF_CTRL_REG
, 0x06);
376 l
= dmtimer_read(timer
, OMAP_TIMER_V1_SYS_STAT_OFFSET
);
377 } while (!l
&& timeout
--);
380 dev_err(&timer
->pdev
->dev
, "Timer failed to reset\n");
384 /* Configure timer for smart-idle mode */
385 l
= dmtimer_read(timer
, OMAP_TIMER_OCP_CFG_OFFSET
);
387 dmtimer_write(timer
, OMAP_TIMER_OCP_CFG_OFFSET
, l
);
395 * Functions exposed to PWM and remoteproc drivers via platform_data.
396 * Do not use these in the driver, these will get deprecated and will
397 * will be replaced by Linux generic framework functions such as
398 * chained interrupts and clock framework.
400 static struct dmtimer
*to_dmtimer(struct omap_dm_timer
*cookie
)
405 return container_of(cookie
, struct dmtimer
, cookie
);
408 static int omap_dm_timer_set_source(struct omap_dm_timer
*cookie
, int source
)
411 const char *parent_name
;
413 struct dmtimer_platform_data
*pdata
;
414 struct dmtimer
*timer
;
416 timer
= to_dmtimer(cookie
);
417 if (unlikely(!timer
) || IS_ERR(timer
->fclk
))
421 case OMAP_TIMER_SRC_SYS_CLK
:
422 parent_name
= "timer_sys_ck";
424 case OMAP_TIMER_SRC_32_KHZ
:
425 parent_name
= "timer_32k_ck";
427 case OMAP_TIMER_SRC_EXT_CLK
:
428 parent_name
= "timer_ext_ck";
434 pdata
= timer
->pdev
->dev
.platform_data
;
437 * FIXME: Used for OMAP1 devices only because they do not currently
438 * use the clock framework to set the parent clock. To be removed
439 * once OMAP1 migrated to using clock framework for dmtimers
441 if (timer
->omap1
&& pdata
&& pdata
->set_timer_src
)
442 return pdata
->set_timer_src(timer
->pdev
, source
);
444 #if defined(CONFIG_COMMON_CLK)
445 /* Check if the clock has configurable parents */
446 if (clk_hw_get_num_parents(__clk_get_hw(timer
->fclk
)) < 2)
450 parent
= clk_get(&timer
->pdev
->dev
, parent_name
);
451 if (IS_ERR(parent
)) {
452 pr_err("%s: %s not found\n", __func__
, parent_name
);
456 ret
= clk_set_parent(timer
->fclk
, parent
);
458 pr_err("%s: failed to set %s as parent\n", __func__
,
466 static void omap_dm_timer_enable(struct omap_dm_timer
*cookie
)
468 struct dmtimer
*timer
= to_dmtimer(cookie
);
469 struct device
*dev
= &timer
->pdev
->dev
;
472 rc
= pm_runtime_resume_and_get(dev
);
474 dev_err(dev
, "could not enable timer\n");
477 static void omap_dm_timer_disable(struct omap_dm_timer
*cookie
)
479 struct dmtimer
*timer
= to_dmtimer(cookie
);
480 struct device
*dev
= &timer
->pdev
->dev
;
482 pm_runtime_put_sync(dev
);
485 static int omap_dm_timer_prepare(struct dmtimer
*timer
)
487 struct device
*dev
= &timer
->pdev
->dev
;
490 rc
= pm_runtime_resume_and_get(dev
);
494 if (timer
->capability
& OMAP_TIMER_NEEDS_RESET
) {
495 rc
= omap_dm_timer_reset(timer
);
497 pm_runtime_put_sync(dev
);
502 __omap_dm_timer_enable_posted(timer
);
503 pm_runtime_put_sync(dev
);
508 static inline u32
omap_dm_timer_reserved_systimer(int id
)
510 return (omap_reserved_systimers
& (1 << (id
- 1))) ? 1 : 0;
513 static struct dmtimer
*_omap_dm_timer_request(int req_type
, void *data
)
515 struct dmtimer
*timer
= NULL
, *t
;
516 struct device_node
*np
= NULL
;
528 case REQUEST_BY_NODE
:
529 np
= (struct device_node
*)data
;
536 spin_lock_irqsave(&dm_timer_lock
, flags
);
537 list_for_each_entry(t
, &omap_timer_list
, node
) {
543 if (id
== t
->pdev
->id
) {
550 if (cap
== (t
->capability
& cap
)) {
552 * If timer is not NULL, we have already found
553 * one timer. But it was not an exact match
554 * because it had more capabilities than what
555 * was required. Therefore, unreserve the last
556 * timer found and see if this one is a better
564 /* Exit loop early if we find an exact match */
565 if (t
->capability
== cap
)
569 case REQUEST_BY_NODE
:
570 if (np
== t
->pdev
->dev
.of_node
) {
584 spin_unlock_irqrestore(&dm_timer_lock
, flags
);
586 if (timer
&& omap_dm_timer_prepare(timer
)) {
592 pr_debug("%s: timer request failed!\n", __func__
);
597 static struct omap_dm_timer
*omap_dm_timer_request(void)
599 struct dmtimer
*timer
;
601 timer
= _omap_dm_timer_request(REQUEST_ANY
, NULL
);
605 return &timer
->cookie
;
608 static struct omap_dm_timer
*omap_dm_timer_request_specific(int id
)
610 struct dmtimer
*timer
;
612 /* Requesting timer by ID is not supported when device tree is used */
613 if (of_have_populated_dt()) {
614 pr_warn("%s: Please use omap_dm_timer_request_by_node()\n",
619 timer
= _omap_dm_timer_request(REQUEST_BY_ID
, &id
);
623 return &timer
->cookie
;
627 * omap_dm_timer_request_by_node - Request a timer by device-tree node
628 * @np: Pointer to device-tree timer node
630 * Request a timer based upon a device node pointer. Returns pointer to
631 * timer handle on success and a NULL pointer on failure.
633 static struct omap_dm_timer
*omap_dm_timer_request_by_node(struct device_node
*np
)
635 struct dmtimer
*timer
;
640 timer
= _omap_dm_timer_request(REQUEST_BY_NODE
, np
);
644 return &timer
->cookie
;
647 static int omap_dm_timer_free(struct omap_dm_timer
*cookie
)
649 struct dmtimer
*timer
;
653 timer
= to_dmtimer(cookie
);
654 if (unlikely(!timer
))
657 WARN_ON(!timer
->reserved
);
660 dev
= &timer
->pdev
->dev
;
661 rc
= pm_runtime_resume_and_get(dev
);
665 /* Clear timer configuration */
666 dmtimer_write(timer
, OMAP_TIMER_CTRL_REG
, 0);
668 pm_runtime_put_sync(dev
);
673 static int omap_dm_timer_get_irq(struct omap_dm_timer
*cookie
)
675 struct dmtimer
*timer
= to_dmtimer(cookie
);
681 #if defined(CONFIG_ARCH_OMAP1)
682 #include <linux/soc/ti/omap1-io.h>
684 static struct clk
*omap_dm_timer_get_fclk(struct omap_dm_timer
*cookie
)
690 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
691 * @inputmask: current value of idlect mask
693 __u32
omap_dm_timer_modify_idlect_mask(__u32 inputmask
)
696 struct dmtimer
*timer
= NULL
;
699 /* If ARMXOR cannot be idled this function call is unnecessary */
700 if (!(inputmask
& (1 << 1)))
703 /* If any active timer is using ARMXOR return modified mask */
704 spin_lock_irqsave(&dm_timer_lock
, flags
);
705 list_for_each_entry(timer
, &omap_timer_list
, node
) {
708 l
= dmtimer_read(timer
, OMAP_TIMER_CTRL_REG
);
709 if (l
& OMAP_TIMER_CTRL_ST
) {
710 if (((omap_readl(MOD_CONF_CTRL_1
) >> (i
* 2)) & 0x03) == 0)
711 inputmask
&= ~(1 << 1);
713 inputmask
&= ~(1 << 2);
717 spin_unlock_irqrestore(&dm_timer_lock
, flags
);
724 static struct clk
*omap_dm_timer_get_fclk(struct omap_dm_timer
*cookie
)
726 struct dmtimer
*timer
= to_dmtimer(cookie
);
728 if (timer
&& !IS_ERR(timer
->fclk
))
733 __u32
omap_dm_timer_modify_idlect_mask(__u32 inputmask
)
742 static int omap_dm_timer_start(struct omap_dm_timer
*cookie
)
744 struct dmtimer
*timer
;
749 timer
= to_dmtimer(cookie
);
750 if (unlikely(!timer
))
753 dev
= &timer
->pdev
->dev
;
755 rc
= pm_runtime_resume_and_get(dev
);
759 l
= dmtimer_read(timer
, OMAP_TIMER_CTRL_REG
);
760 if (!(l
& OMAP_TIMER_CTRL_ST
)) {
761 l
|= OMAP_TIMER_CTRL_ST
;
762 dmtimer_write(timer
, OMAP_TIMER_CTRL_REG
, l
);
768 static int omap_dm_timer_stop(struct omap_dm_timer
*cookie
)
770 struct dmtimer
*timer
;
773 timer
= to_dmtimer(cookie
);
774 if (unlikely(!timer
))
777 dev
= &timer
->pdev
->dev
;
779 __omap_dm_timer_stop(timer
);
781 pm_runtime_put_sync(dev
);
786 static int omap_dm_timer_set_load(struct omap_dm_timer
*cookie
,
789 struct dmtimer
*timer
;
793 timer
= to_dmtimer(cookie
);
794 if (unlikely(!timer
))
797 dev
= &timer
->pdev
->dev
;
798 rc
= pm_runtime_resume_and_get(dev
);
802 dmtimer_write(timer
, OMAP_TIMER_LOAD_REG
, load
);
804 pm_runtime_put_sync(dev
);
809 static int omap_dm_timer_set_match(struct omap_dm_timer
*cookie
, int enable
,
812 struct dmtimer
*timer
;
817 timer
= to_dmtimer(cookie
);
818 if (unlikely(!timer
))
821 dev
= &timer
->pdev
->dev
;
822 rc
= pm_runtime_resume_and_get(dev
);
826 l
= dmtimer_read(timer
, OMAP_TIMER_CTRL_REG
);
828 l
|= OMAP_TIMER_CTRL_CE
;
830 l
&= ~OMAP_TIMER_CTRL_CE
;
831 dmtimer_write(timer
, OMAP_TIMER_MATCH_REG
, match
);
832 dmtimer_write(timer
, OMAP_TIMER_CTRL_REG
, l
);
834 pm_runtime_put_sync(dev
);
839 static int omap_dm_timer_set_pwm(struct omap_dm_timer
*cookie
, int def_on
,
840 int toggle
, int trigger
, int autoreload
)
842 struct dmtimer
*timer
;
847 timer
= to_dmtimer(cookie
);
848 if (unlikely(!timer
))
851 dev
= &timer
->pdev
->dev
;
852 rc
= pm_runtime_resume_and_get(dev
);
856 l
= dmtimer_read(timer
, OMAP_TIMER_CTRL_REG
);
857 l
&= ~(OMAP_TIMER_CTRL_GPOCFG
| OMAP_TIMER_CTRL_SCPWM
|
858 OMAP_TIMER_CTRL_PT
| (0x03 << 10) | OMAP_TIMER_CTRL_AR
);
860 l
|= OMAP_TIMER_CTRL_SCPWM
;
862 l
|= OMAP_TIMER_CTRL_PT
;
865 l
|= OMAP_TIMER_CTRL_AR
;
866 dmtimer_write(timer
, OMAP_TIMER_CTRL_REG
, l
);
868 pm_runtime_put_sync(dev
);
873 static int omap_dm_timer_get_pwm_status(struct omap_dm_timer
*cookie
)
875 struct dmtimer
*timer
;
880 timer
= to_dmtimer(cookie
);
881 if (unlikely(!timer
))
884 dev
= &timer
->pdev
->dev
;
885 rc
= pm_runtime_resume_and_get(dev
);
889 l
= dmtimer_read(timer
, OMAP_TIMER_CTRL_REG
);
891 pm_runtime_put_sync(dev
);
896 static int omap_dm_timer_set_prescaler(struct omap_dm_timer
*cookie
,
899 struct dmtimer
*timer
;
904 timer
= to_dmtimer(cookie
);
905 if (unlikely(!timer
) || prescaler
< -1 || prescaler
> 7)
908 dev
= &timer
->pdev
->dev
;
909 rc
= pm_runtime_resume_and_get(dev
);
913 l
= dmtimer_read(timer
, OMAP_TIMER_CTRL_REG
);
914 l
&= ~(OMAP_TIMER_CTRL_PRE
| (0x07 << 2));
915 if (prescaler
>= 0) {
916 l
|= OMAP_TIMER_CTRL_PRE
;
919 dmtimer_write(timer
, OMAP_TIMER_CTRL_REG
, l
);
921 pm_runtime_put_sync(dev
);
926 static int omap_dm_timer_set_int_enable(struct omap_dm_timer
*cookie
,
929 struct dmtimer
*timer
;
933 timer
= to_dmtimer(cookie
);
934 if (unlikely(!timer
))
937 dev
= &timer
->pdev
->dev
;
938 rc
= pm_runtime_resume_and_get(dev
);
942 __omap_dm_timer_int_enable(timer
, value
);
944 pm_runtime_put_sync(dev
);
950 * omap_dm_timer_set_int_disable - disable timer interrupts
951 * @cookie: pointer to timer cookie
952 * @mask: bit mask of interrupts to be disabled
954 * Disables the specified timer interrupts for a timer.
956 static int omap_dm_timer_set_int_disable(struct omap_dm_timer
*cookie
, u32 mask
)
958 struct dmtimer
*timer
;
963 timer
= to_dmtimer(cookie
);
964 if (unlikely(!timer
))
967 dev
= &timer
->pdev
->dev
;
968 rc
= pm_runtime_resume_and_get(dev
);
972 if (timer
->revision
== 1)
973 l
= dmtimer_read(timer
, timer
->irq_ena
) & ~mask
;
975 dmtimer_write(timer
, timer
->irq_dis
, l
);
976 l
= dmtimer_read(timer
, OMAP_TIMER_WAKEUP_EN_REG
) & ~mask
;
977 dmtimer_write(timer
, OMAP_TIMER_WAKEUP_EN_REG
, l
);
979 pm_runtime_put_sync(dev
);
984 static unsigned int omap_dm_timer_read_status(struct omap_dm_timer
*cookie
)
986 struct dmtimer
*timer
;
989 timer
= to_dmtimer(cookie
);
990 if (unlikely(!timer
|| !atomic_read(&timer
->enabled
))) {
991 pr_err("%s: timer not available or enabled.\n", __func__
);
995 l
= dmtimer_read(timer
, timer
->irq_stat
);
1000 static int omap_dm_timer_write_status(struct omap_dm_timer
*cookie
, unsigned int value
)
1002 struct dmtimer
*timer
;
1004 timer
= to_dmtimer(cookie
);
1005 if (unlikely(!timer
|| !atomic_read(&timer
->enabled
)))
1008 __omap_dm_timer_write_status(timer
, value
);
1013 static unsigned int omap_dm_timer_read_counter(struct omap_dm_timer
*cookie
)
1015 struct dmtimer
*timer
;
1017 timer
= to_dmtimer(cookie
);
1018 if (unlikely(!timer
|| !atomic_read(&timer
->enabled
))) {
1019 pr_err("%s: timer not iavailable or enabled.\n", __func__
);
1023 return __omap_dm_timer_read_counter(timer
);
1026 static int omap_dm_timer_write_counter(struct omap_dm_timer
*cookie
, unsigned int value
)
1028 struct dmtimer
*timer
;
1030 timer
= to_dmtimer(cookie
);
1031 if (unlikely(!timer
|| !atomic_read(&timer
->enabled
))) {
1032 pr_err("%s: timer not available or enabled.\n", __func__
);
1036 dmtimer_write(timer
, OMAP_TIMER_COUNTER_REG
, value
);
1038 /* Save the context */
1039 timer
->context
.tcrr
= value
;
1043 static int __maybe_unused
omap_dm_timer_runtime_suspend(struct device
*dev
)
1045 struct dmtimer
*timer
= dev_get_drvdata(dev
);
1047 atomic_set(&timer
->enabled
, 0);
1049 if (timer
->capability
& OMAP_TIMER_ALWON
|| !timer
->func_base
)
1052 omap_timer_save_context(timer
);
1057 static int __maybe_unused
omap_dm_timer_runtime_resume(struct device
*dev
)
1059 struct dmtimer
*timer
= dev_get_drvdata(dev
);
1061 if (!(timer
->capability
& OMAP_TIMER_ALWON
) && timer
->func_base
)
1062 omap_timer_restore_context(timer
);
1064 atomic_set(&timer
->enabled
, 1);
1069 static const struct dev_pm_ops omap_dm_timer_pm_ops
= {
1070 SET_RUNTIME_PM_OPS(omap_dm_timer_runtime_suspend
,
1071 omap_dm_timer_runtime_resume
, NULL
)
1074 static const struct of_device_id omap_timer_match
[];
1077 * omap_dm_timer_probe - probe function called for every registered device
1078 * @pdev: pointer to current timer platform device
1080 * Called by driver framework at the end of device registration for all
1083 static int omap_dm_timer_probe(struct platform_device
*pdev
)
1085 unsigned long flags
;
1086 struct dmtimer
*timer
;
1087 struct device
*dev
= &pdev
->dev
;
1088 const struct dmtimer_platform_data
*pdata
;
1091 pdata
= of_device_get_match_data(dev
);
1093 pdata
= dev_get_platdata(dev
);
1095 dev
->platform_data
= (void *)pdata
;
1098 dev_err(dev
, "%s: no platform data.\n", __func__
);
1102 timer
= devm_kzalloc(dev
, sizeof(*timer
), GFP_KERNEL
);
1106 timer
->irq
= platform_get_irq(pdev
, 0);
1107 if (timer
->irq
< 0) {
1108 if (of_property_read_bool(dev
->of_node
, "ti,timer-pwm"))
1109 dev_info(dev
, "Did not find timer interrupt, timer usable in PWM mode only\n");
1114 timer
->io_base
= devm_platform_ioremap_resource(pdev
, 0);
1115 if (IS_ERR(timer
->io_base
))
1116 return PTR_ERR(timer
->io_base
);
1118 platform_set_drvdata(pdev
, timer
);
1121 if (of_property_read_bool(dev
->of_node
, "ti,timer-alwon"))
1122 timer
->capability
|= OMAP_TIMER_ALWON
;
1123 if (of_property_read_bool(dev
->of_node
, "ti,timer-dsp"))
1124 timer
->capability
|= OMAP_TIMER_HAS_DSP_IRQ
;
1125 if (of_property_read_bool(dev
->of_node
, "ti,timer-pwm"))
1126 timer
->capability
|= OMAP_TIMER_HAS_PWM
;
1127 if (of_property_read_bool(dev
->of_node
, "ti,timer-secure"))
1128 timer
->capability
|= OMAP_TIMER_SECURE
;
1130 timer
->id
= pdev
->id
;
1131 timer
->capability
= pdata
->timer_capability
;
1132 timer
->reserved
= omap_dm_timer_reserved_systimer(timer
->id
);
1135 timer
->omap1
= timer
->capability
& OMAP_TIMER_NEEDS_RESET
;
1137 /* OMAP1 devices do not yet use the clock framework for dmtimers */
1138 if (!timer
->omap1
) {
1139 timer
->fclk
= devm_clk_get(dev
, "fck");
1140 if (IS_ERR(timer
->fclk
))
1141 return PTR_ERR(timer
->fclk
);
1143 timer
->fclk_nb
.notifier_call
= omap_timer_fclk_notifier
;
1144 ret
= devm_clk_notifier_register(dev
, timer
->fclk
,
1149 timer
->fclk_rate
= clk_get_rate(timer
->fclk
);
1151 timer
->fclk
= ERR_PTR(-ENODEV
);
1154 if (!(timer
->capability
& OMAP_TIMER_ALWON
)) {
1155 timer
->nb
.notifier_call
= omap_timer_context_notifier
;
1156 cpu_pm_register_notifier(&timer
->nb
);
1159 timer
->errata
= pdata
->timer_errata
;
1163 pm_runtime_enable(dev
);
1165 if (!timer
->reserved
) {
1166 ret
= pm_runtime_resume_and_get(dev
);
1168 dev_err(dev
, "%s: pm_runtime_get_sync failed!\n",
1172 __omap_dm_timer_init_regs(timer
);
1174 /* Clear timer configuration */
1175 dmtimer_write(timer
, OMAP_TIMER_CTRL_REG
, 0);
1177 pm_runtime_put(dev
);
1180 /* add the timer element to the list */
1181 spin_lock_irqsave(&dm_timer_lock
, flags
);
1182 list_add_tail(&timer
->node
, &omap_timer_list
);
1183 spin_unlock_irqrestore(&dm_timer_lock
, flags
);
1185 dev_dbg(dev
, "Device Probed.\n");
1190 pm_runtime_disable(dev
);
1195 * omap_dm_timer_remove - cleanup a registered timer device
1196 * @pdev: pointer to current timer platform device
1198 * Called by driver framework whenever a timer device is unregistered.
1199 * In addition to freeing platform resources it also deletes the timer
1200 * entry from the local list.
1202 static void omap_dm_timer_remove(struct platform_device
*pdev
)
1204 struct dmtimer
*timer
;
1205 unsigned long flags
;
1208 spin_lock_irqsave(&dm_timer_lock
, flags
);
1209 list_for_each_entry(timer
, &omap_timer_list
, node
)
1210 if (!strcmp(dev_name(&timer
->pdev
->dev
),
1211 dev_name(&pdev
->dev
))) {
1212 if (!(timer
->capability
& OMAP_TIMER_ALWON
))
1213 cpu_pm_unregister_notifier(&timer
->nb
);
1214 list_del(&timer
->node
);
1218 spin_unlock_irqrestore(&dm_timer_lock
, flags
);
1220 pm_runtime_disable(&pdev
->dev
);
1223 dev_err(&pdev
->dev
, "Unable to determine timer entry in list of drivers on remove\n");
1226 static const struct omap_dm_timer_ops dmtimer_ops
= {
1227 .request_by_node
= omap_dm_timer_request_by_node
,
1228 .request_specific
= omap_dm_timer_request_specific
,
1229 .request
= omap_dm_timer_request
,
1230 .set_source
= omap_dm_timer_set_source
,
1231 .get_irq
= omap_dm_timer_get_irq
,
1232 .set_int_enable
= omap_dm_timer_set_int_enable
,
1233 .set_int_disable
= omap_dm_timer_set_int_disable
,
1234 .free
= omap_dm_timer_free
,
1235 .enable
= omap_dm_timer_enable
,
1236 .disable
= omap_dm_timer_disable
,
1237 .get_fclk
= omap_dm_timer_get_fclk
,
1238 .start
= omap_dm_timer_start
,
1239 .stop
= omap_dm_timer_stop
,
1240 .set_load
= omap_dm_timer_set_load
,
1241 .set_match
= omap_dm_timer_set_match
,
1242 .set_pwm
= omap_dm_timer_set_pwm
,
1243 .get_pwm_status
= omap_dm_timer_get_pwm_status
,
1244 .set_prescaler
= omap_dm_timer_set_prescaler
,
1245 .read_counter
= omap_dm_timer_read_counter
,
1246 .write_counter
= omap_dm_timer_write_counter
,
1247 .read_status
= omap_dm_timer_read_status
,
1248 .write_status
= omap_dm_timer_write_status
,
1251 static const struct dmtimer_platform_data omap3plus_pdata
= {
1252 .timer_errata
= OMAP_TIMER_ERRATA_I103_I767
,
1253 .timer_ops
= &dmtimer_ops
,
1256 static const struct dmtimer_platform_data am6_pdata
= {
1257 .timer_ops
= &dmtimer_ops
,
1260 static const struct of_device_id omap_timer_match
[] = {
1262 .compatible
= "ti,omap2420-timer",
1265 .compatible
= "ti,omap3430-timer",
1266 .data
= &omap3plus_pdata
,
1269 .compatible
= "ti,omap4430-timer",
1270 .data
= &omap3plus_pdata
,
1273 .compatible
= "ti,omap5430-timer",
1274 .data
= &omap3plus_pdata
,
1277 .compatible
= "ti,am335x-timer",
1278 .data
= &omap3plus_pdata
,
1281 .compatible
= "ti,am335x-timer-1ms",
1282 .data
= &omap3plus_pdata
,
1285 .compatible
= "ti,dm816-timer",
1286 .data
= &omap3plus_pdata
,
1289 .compatible
= "ti,am654-timer",
1294 MODULE_DEVICE_TABLE(of
, omap_timer_match
);
1296 static struct platform_driver omap_dm_timer_driver
= {
1297 .probe
= omap_dm_timer_probe
,
1298 .remove_new
= omap_dm_timer_remove
,
1300 .name
= "omap_timer",
1301 .of_match_table
= omap_timer_match
,
1302 .pm
= &omap_dm_timer_pm_ops
,
1306 module_platform_driver(omap_dm_timer_driver
);
1308 MODULE_DESCRIPTION("OMAP Dual-Mode Timer Driver");
1309 MODULE_AUTHOR("Texas Instruments Inc");