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 */
136 struct timer_regs context;
140 struct platform_device *pdev;
141 struct list_head node;
142 struct notifier_block nb;
145 static u32 omap_reserved_systimers;
146 static LIST_HEAD(omap_timer_list);
147 static DEFINE_SPINLOCK(dm_timer_lock);
157 * dmtimer_read - read timer registers in posted and non-posted mode
158 * @timer: timer pointer over which read operation to perform
159 * @reg: lowest byte holds the register offset
161 * The posted mode bit is encoded in reg. Note that in posted mode, write
162 * pending bit must be checked. Otherwise a read of a non completed write
163 * will produce an error.
165 static inline u32 dmtimer_read(struct dmtimer *timer, u32 reg)
172 /* Wait for a possible write pending bit in posted mode */
173 if (wp && timer->posted)
174 while (readl_relaxed(timer->pend) & wp)
177 return readl_relaxed(timer->func_base + offset);
181 * dmtimer_write - write timer registers in posted and non-posted mode
182 * @timer: timer pointer over which write operation is to perform
183 * @reg: lowest byte holds the register offset
184 * @value: data to write into the register
186 * The posted mode bit is encoded in reg. Note that in posted mode, the write
187 * pending bit must be checked. Otherwise a write on a register which has a
188 * pending write will be lost.
190 static inline void dmtimer_write(struct dmtimer *timer, u32 reg, u32 val)
197 /* Wait for a possible write pending bit in posted mode */
198 if (wp && timer->posted)
199 while (readl_relaxed(timer->pend) & wp)
202 writel_relaxed(val, timer->func_base + offset);
205 static inline void __omap_dm_timer_init_regs(struct dmtimer *timer)
209 /* Assume v1 ip if bits [31:16] are zero */
210 tidr = readl_relaxed(timer->io_base);
213 timer->irq_stat = OMAP_TIMER_V1_STAT_OFFSET;
214 timer->irq_ena = OMAP_TIMER_V1_INT_EN_OFFSET;
215 timer->irq_dis = OMAP_TIMER_V1_INT_EN_OFFSET;
216 timer->pend = timer->io_base + _OMAP_TIMER_WRITE_PEND_OFFSET;
217 timer->func_base = timer->io_base;
220 timer->irq_stat = OMAP_TIMER_V2_IRQSTATUS - OMAP_TIMER_V2_FUNC_OFFSET;
221 timer->irq_ena = OMAP_TIMER_V2_IRQENABLE_SET - OMAP_TIMER_V2_FUNC_OFFSET;
222 timer->irq_dis = OMAP_TIMER_V2_IRQENABLE_CLR - OMAP_TIMER_V2_FUNC_OFFSET;
223 timer->pend = timer->io_base +
224 _OMAP_TIMER_WRITE_PEND_OFFSET +
225 OMAP_TIMER_V2_FUNC_OFFSET;
226 timer->func_base = timer->io_base + OMAP_TIMER_V2_FUNC_OFFSET;
231 * __omap_dm_timer_enable_posted - enables write posted mode
232 * @timer: pointer to timer instance handle
234 * Enables the write posted mode for the timer. When posted mode is enabled
235 * writes to certain timer registers are immediately acknowledged by the
236 * internal bus and hence prevents stalling the CPU waiting for the write to
237 * complete. Enabling this feature can improve performance for writing to the
240 static inline void __omap_dm_timer_enable_posted(struct dmtimer *timer)
245 if (timer->errata & OMAP_TIMER_ERRATA_I103_I767) {
246 timer->posted = OMAP_TIMER_NONPOSTED;
247 dmtimer_write(timer, OMAP_TIMER_IF_CTRL_REG, 0);
251 dmtimer_write(timer, OMAP_TIMER_IF_CTRL_REG, OMAP_TIMER_CTRL_POSTED);
252 timer->context.tsicr = OMAP_TIMER_CTRL_POSTED;
253 timer->posted = OMAP_TIMER_POSTED;
256 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 / 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_dm_timer_reset(struct dmtimer *timer)
353 u32 l, timeout = 100000;
355 if (timer->revision != 1)
358 dmtimer_write(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
361 l = dmtimer_read(timer, OMAP_TIMER_V1_SYS_STAT_OFFSET);
362 } while (!l && timeout--);
365 dev_err(&timer->pdev->dev, "Timer failed to reset\n");
369 /* Configure timer for smart-idle mode */
370 l = dmtimer_read(timer, OMAP_TIMER_OCP_CFG_OFFSET);
372 dmtimer_write(timer, OMAP_TIMER_OCP_CFG_OFFSET, l);
380 * Functions exposed to PWM and remoteproc drivers via platform_data.
381 * Do not use these in the driver, these will get deprecated and will
382 * will be replaced by Linux generic framework functions such as
383 * chained interrupts and clock framework.
385 static struct dmtimer *to_dmtimer(struct omap_dm_timer *cookie)
390 return container_of(cookie, struct dmtimer, cookie);
393 static int omap_dm_timer_set_source(struct omap_dm_timer *cookie, int source)
396 const char *parent_name;
398 struct dmtimer_platform_data *pdata;
399 struct dmtimer *timer;
401 timer = to_dmtimer(cookie);
402 if (unlikely(!timer) || IS_ERR(timer->fclk))
406 case OMAP_TIMER_SRC_SYS_CLK:
407 parent_name = "timer_sys_ck";
409 case OMAP_TIMER_SRC_32_KHZ:
410 parent_name = "timer_32k_ck";
412 case OMAP_TIMER_SRC_EXT_CLK:
413 parent_name = "timer_ext_ck";
419 pdata = timer->pdev->dev.platform_data;
422 * FIXME: Used for OMAP1 devices only because they do not currently
423 * use the clock framework to set the parent clock. To be removed
424 * once OMAP1 migrated to using clock framework for dmtimers
426 if (timer->omap1 && pdata && pdata->set_timer_src)
427 return pdata->set_timer_src(timer->pdev, source);
429 #if defined(CONFIG_COMMON_CLK)
430 /* Check if the clock has configurable parents */
431 if (clk_hw_get_num_parents(__clk_get_hw(timer->fclk)) < 2)
435 parent = clk_get(&timer->pdev->dev, parent_name);
436 if (IS_ERR(parent)) {
437 pr_err("%s: %s not found\n", __func__, parent_name);
441 ret = clk_set_parent(timer->fclk, parent);
443 pr_err("%s: failed to set %s as parent\n", __func__,
451 static void omap_dm_timer_enable(struct omap_dm_timer *cookie)
453 struct dmtimer *timer = to_dmtimer(cookie);
454 struct device *dev = &timer->pdev->dev;
457 rc = pm_runtime_resume_and_get(dev);
459 dev_err(dev, "could not enable timer\n");
462 static void omap_dm_timer_disable(struct omap_dm_timer *cookie)
464 struct dmtimer *timer = to_dmtimer(cookie);
465 struct device *dev = &timer->pdev->dev;
467 pm_runtime_put_sync(dev);
470 static int omap_dm_timer_prepare(struct dmtimer *timer)
472 struct device *dev = &timer->pdev->dev;
475 rc = pm_runtime_resume_and_get(dev);
479 if (timer->capability & OMAP_TIMER_NEEDS_RESET) {
480 rc = omap_dm_timer_reset(timer);
482 pm_runtime_put_sync(dev);
487 __omap_dm_timer_enable_posted(timer);
488 pm_runtime_put_sync(dev);
493 static inline u32 omap_dm_timer_reserved_systimer(int id)
495 return (omap_reserved_systimers & (1 << (id - 1))) ? 1 : 0;
498 static struct dmtimer *_omap_dm_timer_request(int req_type, void *data)
500 struct dmtimer *timer = NULL, *t;
501 struct device_node *np = NULL;
513 case REQUEST_BY_NODE:
514 np = (struct device_node *)data;
521 spin_lock_irqsave(&dm_timer_lock, flags);
522 list_for_each_entry(t, &omap_timer_list, node) {
528 if (id == t->pdev->id) {
535 if (cap == (t->capability & cap)) {
537 * If timer is not NULL, we have already found
538 * one timer. But it was not an exact match
539 * because it had more capabilities than what
540 * was required. Therefore, unreserve the last
541 * timer found and see if this one is a better
549 /* Exit loop early if we find an exact match */
550 if (t->capability == cap)
554 case REQUEST_BY_NODE:
555 if (np == t->pdev->dev.of_node) {
569 spin_unlock_irqrestore(&dm_timer_lock, flags);
571 if (timer && omap_dm_timer_prepare(timer)) {
577 pr_debug("%s: timer request failed!\n", __func__);
582 static struct omap_dm_timer *omap_dm_timer_request(void)
584 struct dmtimer *timer;
586 timer = _omap_dm_timer_request(REQUEST_ANY, NULL);
590 return &timer->cookie;
593 static struct omap_dm_timer *omap_dm_timer_request_specific(int id)
595 struct dmtimer *timer;
597 /* Requesting timer by ID is not supported when device tree is used */
598 if (of_have_populated_dt()) {
599 pr_warn("%s: Please use omap_dm_timer_request_by_node()\n",
604 timer = _omap_dm_timer_request(REQUEST_BY_ID, &id);
608 return &timer->cookie;
612 * omap_dm_timer_request_by_node - Request a timer by device-tree node
613 * @np: Pointer to device-tree timer node
615 * Request a timer based upon a device node pointer. Returns pointer to
616 * timer handle on success and a NULL pointer on failure.
618 static struct omap_dm_timer *omap_dm_timer_request_by_node(struct device_node *np)
620 struct dmtimer *timer;
625 timer = _omap_dm_timer_request(REQUEST_BY_NODE, np);
629 return &timer->cookie;
632 static int omap_dm_timer_free(struct omap_dm_timer *cookie)
634 struct dmtimer *timer;
638 timer = to_dmtimer(cookie);
639 if (unlikely(!timer))
642 WARN_ON(!timer->reserved);
645 dev = &timer->pdev->dev;
646 rc = pm_runtime_resume_and_get(dev);
650 /* Clear timer configuration */
651 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, 0);
653 pm_runtime_put_sync(dev);
658 static int omap_dm_timer_get_irq(struct omap_dm_timer *cookie)
660 struct dmtimer *timer = to_dmtimer(cookie);
666 #if defined(CONFIG_ARCH_OMAP1)
667 #include <linux/soc/ti/omap1-io.h>
669 static struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *cookie)
675 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
676 * @inputmask: current value of idlect mask
678 __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
681 struct dmtimer *timer = NULL;
684 /* If ARMXOR cannot be idled this function call is unnecessary */
685 if (!(inputmask & (1 << 1)))
688 /* If any active timer is using ARMXOR return modified mask */
689 spin_lock_irqsave(&dm_timer_lock, flags);
690 list_for_each_entry(timer, &omap_timer_list, node) {
693 l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
694 if (l & OMAP_TIMER_CTRL_ST) {
695 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
696 inputmask &= ~(1 << 1);
698 inputmask &= ~(1 << 2);
702 spin_unlock_irqrestore(&dm_timer_lock, flags);
709 static struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *cookie)
711 struct dmtimer *timer = to_dmtimer(cookie);
713 if (timer && !IS_ERR(timer->fclk))
718 __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
727 static int omap_dm_timer_start(struct omap_dm_timer *cookie)
729 struct dmtimer *timer;
734 timer = to_dmtimer(cookie);
735 if (unlikely(!timer))
738 dev = &timer->pdev->dev;
740 rc = pm_runtime_resume_and_get(dev);
744 l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
745 if (!(l & OMAP_TIMER_CTRL_ST)) {
746 l |= OMAP_TIMER_CTRL_ST;
747 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
753 static int omap_dm_timer_stop(struct omap_dm_timer *cookie)
755 struct dmtimer *timer;
757 unsigned long rate = 0;
759 timer = to_dmtimer(cookie);
760 if (unlikely(!timer))
763 dev = &timer->pdev->dev;
766 rate = clk_get_rate(timer->fclk);
768 __omap_dm_timer_stop(timer, rate);
770 pm_runtime_put_sync(dev);
775 static int omap_dm_timer_set_load(struct omap_dm_timer *cookie,
778 struct dmtimer *timer;
782 timer = to_dmtimer(cookie);
783 if (unlikely(!timer))
786 dev = &timer->pdev->dev;
787 rc = pm_runtime_resume_and_get(dev);
791 dmtimer_write(timer, OMAP_TIMER_LOAD_REG, load);
793 pm_runtime_put_sync(dev);
798 static int omap_dm_timer_set_match(struct omap_dm_timer *cookie, int enable,
801 struct dmtimer *timer;
806 timer = to_dmtimer(cookie);
807 if (unlikely(!timer))
810 dev = &timer->pdev->dev;
811 rc = pm_runtime_resume_and_get(dev);
815 l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
817 l |= OMAP_TIMER_CTRL_CE;
819 l &= ~OMAP_TIMER_CTRL_CE;
820 dmtimer_write(timer, OMAP_TIMER_MATCH_REG, match);
821 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
823 pm_runtime_put_sync(dev);
828 static int omap_dm_timer_set_pwm(struct omap_dm_timer *cookie, int def_on,
829 int toggle, int trigger, int autoreload)
831 struct dmtimer *timer;
836 timer = to_dmtimer(cookie);
837 if (unlikely(!timer))
840 dev = &timer->pdev->dev;
841 rc = pm_runtime_resume_and_get(dev);
845 l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
846 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
847 OMAP_TIMER_CTRL_PT | (0x03 << 10) | OMAP_TIMER_CTRL_AR);
849 l |= OMAP_TIMER_CTRL_SCPWM;
851 l |= OMAP_TIMER_CTRL_PT;
854 l |= OMAP_TIMER_CTRL_AR;
855 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
857 pm_runtime_put_sync(dev);
862 static int omap_dm_timer_get_pwm_status(struct omap_dm_timer *cookie)
864 struct dmtimer *timer;
869 timer = to_dmtimer(cookie);
870 if (unlikely(!timer))
873 dev = &timer->pdev->dev;
874 rc = pm_runtime_resume_and_get(dev);
878 l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
880 pm_runtime_put_sync(dev);
885 static int omap_dm_timer_set_prescaler(struct omap_dm_timer *cookie,
888 struct dmtimer *timer;
893 timer = to_dmtimer(cookie);
894 if (unlikely(!timer) || prescaler < -1 || prescaler > 7)
897 dev = &timer->pdev->dev;
898 rc = pm_runtime_resume_and_get(dev);
902 l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
903 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
904 if (prescaler >= 0) {
905 l |= OMAP_TIMER_CTRL_PRE;
908 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
910 pm_runtime_put_sync(dev);
915 static int omap_dm_timer_set_int_enable(struct omap_dm_timer *cookie,
918 struct dmtimer *timer;
922 timer = to_dmtimer(cookie);
923 if (unlikely(!timer))
926 dev = &timer->pdev->dev;
927 rc = pm_runtime_resume_and_get(dev);
931 __omap_dm_timer_int_enable(timer, value);
933 pm_runtime_put_sync(dev);
939 * omap_dm_timer_set_int_disable - disable timer interrupts
940 * @timer: pointer to timer handle
941 * @mask: bit mask of interrupts to be disabled
943 * Disables the specified timer interrupts for a timer.
945 static int omap_dm_timer_set_int_disable(struct omap_dm_timer *cookie, u32 mask)
947 struct dmtimer *timer;
952 timer = to_dmtimer(cookie);
953 if (unlikely(!timer))
956 dev = &timer->pdev->dev;
957 rc = pm_runtime_resume_and_get(dev);
961 if (timer->revision == 1)
962 l = dmtimer_read(timer, timer->irq_ena) & ~mask;
964 dmtimer_write(timer, timer->irq_dis, l);
965 l = dmtimer_read(timer, OMAP_TIMER_WAKEUP_EN_REG) & ~mask;
966 dmtimer_write(timer, OMAP_TIMER_WAKEUP_EN_REG, l);
968 pm_runtime_put_sync(dev);
973 static unsigned int omap_dm_timer_read_status(struct omap_dm_timer *cookie)
975 struct dmtimer *timer;
978 timer = to_dmtimer(cookie);
979 if (unlikely(!timer || !atomic_read(&timer->enabled))) {
980 pr_err("%s: timer not available or enabled.\n", __func__);
984 l = dmtimer_read(timer, timer->irq_stat);
989 static int omap_dm_timer_write_status(struct omap_dm_timer *cookie, unsigned int value)
991 struct dmtimer *timer;
993 timer = to_dmtimer(cookie);
994 if (unlikely(!timer || !atomic_read(&timer->enabled)))
997 __omap_dm_timer_write_status(timer, value);
1002 static unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *cookie)
1004 struct dmtimer *timer;
1006 timer = to_dmtimer(cookie);
1007 if (unlikely(!timer || !atomic_read(&timer->enabled))) {
1008 pr_err("%s: timer not iavailable or enabled.\n", __func__);
1012 return __omap_dm_timer_read_counter(timer);
1015 static int omap_dm_timer_write_counter(struct omap_dm_timer *cookie, unsigned int value)
1017 struct dmtimer *timer;
1019 timer = to_dmtimer(cookie);
1020 if (unlikely(!timer || !atomic_read(&timer->enabled))) {
1021 pr_err("%s: timer not available or enabled.\n", __func__);
1025 dmtimer_write(timer, OMAP_TIMER_COUNTER_REG, value);
1027 /* Save the context */
1028 timer->context.tcrr = value;
1032 static int __maybe_unused omap_dm_timer_runtime_suspend(struct device *dev)
1034 struct dmtimer *timer = dev_get_drvdata(dev);
1036 atomic_set(&timer->enabled, 0);
1038 if (timer->capability & OMAP_TIMER_ALWON || !timer->func_base)
1041 omap_timer_save_context(timer);
1046 static int __maybe_unused omap_dm_timer_runtime_resume(struct device *dev)
1048 struct dmtimer *timer = dev_get_drvdata(dev);
1050 if (!(timer->capability & OMAP_TIMER_ALWON) && timer->func_base)
1051 omap_timer_restore_context(timer);
1053 atomic_set(&timer->enabled, 1);
1058 static const struct dev_pm_ops omap_dm_timer_pm_ops = {
1059 SET_RUNTIME_PM_OPS(omap_dm_timer_runtime_suspend,
1060 omap_dm_timer_runtime_resume, NULL)
1063 static const struct of_device_id omap_timer_match[];
1066 * omap_dm_timer_probe - probe function called for every registered device
1067 * @pdev: pointer to current timer platform device
1069 * Called by driver framework at the end of device registration for all
1072 static int omap_dm_timer_probe(struct platform_device *pdev)
1074 unsigned long flags;
1075 struct dmtimer *timer;
1076 struct device *dev = &pdev->dev;
1077 const struct dmtimer_platform_data *pdata;
1080 pdata = of_device_get_match_data(dev);
1082 pdata = dev_get_platdata(dev);
1084 dev->platform_data = (void *)pdata;
1087 dev_err(dev, "%s: no platform data.\n", __func__);
1091 timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL);
1095 timer->irq = platform_get_irq(pdev, 0);
1099 timer->io_base = devm_platform_ioremap_resource(pdev, 0);
1100 if (IS_ERR(timer->io_base))
1101 return PTR_ERR(timer->io_base);
1103 platform_set_drvdata(pdev, timer);
1106 if (of_property_read_bool(dev->of_node, "ti,timer-alwon"))
1107 timer->capability |= OMAP_TIMER_ALWON;
1108 if (of_property_read_bool(dev->of_node, "ti,timer-dsp"))
1109 timer->capability |= OMAP_TIMER_HAS_DSP_IRQ;
1110 if (of_property_read_bool(dev->of_node, "ti,timer-pwm"))
1111 timer->capability |= OMAP_TIMER_HAS_PWM;
1112 if (of_property_read_bool(dev->of_node, "ti,timer-secure"))
1113 timer->capability |= OMAP_TIMER_SECURE;
1115 timer->id = pdev->id;
1116 timer->capability = pdata->timer_capability;
1117 timer->reserved = omap_dm_timer_reserved_systimer(timer->id);
1120 timer->omap1 = timer->capability & OMAP_TIMER_NEEDS_RESET;
1122 /* OMAP1 devices do not yet use the clock framework for dmtimers */
1123 if (!timer->omap1) {
1124 timer->fclk = devm_clk_get(dev, "fck");
1125 if (IS_ERR(timer->fclk))
1126 return PTR_ERR(timer->fclk);
1128 timer->fclk = ERR_PTR(-ENODEV);
1131 if (!(timer->capability & OMAP_TIMER_ALWON)) {
1132 timer->nb.notifier_call = omap_timer_context_notifier;
1133 cpu_pm_register_notifier(&timer->nb);
1136 timer->errata = pdata->timer_errata;
1140 pm_runtime_enable(dev);
1142 if (!timer->reserved) {
1143 ret = pm_runtime_resume_and_get(dev);
1145 dev_err(dev, "%s: pm_runtime_get_sync failed!\n",
1149 __omap_dm_timer_init_regs(timer);
1151 /* Clear timer configuration */
1152 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, 0);
1154 pm_runtime_put(dev);
1157 /* add the timer element to the list */
1158 spin_lock_irqsave(&dm_timer_lock, flags);
1159 list_add_tail(&timer->node, &omap_timer_list);
1160 spin_unlock_irqrestore(&dm_timer_lock, flags);
1162 dev_dbg(dev, "Device Probed.\n");
1167 pm_runtime_disable(dev);
1172 * omap_dm_timer_remove - cleanup a registered timer device
1173 * @pdev: pointer to current timer platform device
1175 * Called by driver framework whenever a timer device is unregistered.
1176 * In addition to freeing platform resources it also deletes the timer
1177 * entry from the local list.
1179 static void omap_dm_timer_remove(struct platform_device *pdev)
1181 struct dmtimer *timer;
1182 unsigned long flags;
1185 spin_lock_irqsave(&dm_timer_lock, flags);
1186 list_for_each_entry(timer, &omap_timer_list, node)
1187 if (!strcmp(dev_name(&timer->pdev->dev),
1188 dev_name(&pdev->dev))) {
1189 if (!(timer->capability & OMAP_TIMER_ALWON))
1190 cpu_pm_unregister_notifier(&timer->nb);
1191 list_del(&timer->node);
1195 spin_unlock_irqrestore(&dm_timer_lock, flags);
1197 pm_runtime_disable(&pdev->dev);
1200 dev_err(&pdev->dev, "Unable to determine timer entry in list of drivers on remove\n");
1203 static const struct omap_dm_timer_ops dmtimer_ops = {
1204 .request_by_node = omap_dm_timer_request_by_node,
1205 .request_specific = omap_dm_timer_request_specific,
1206 .request = omap_dm_timer_request,
1207 .set_source = omap_dm_timer_set_source,
1208 .get_irq = omap_dm_timer_get_irq,
1209 .set_int_enable = omap_dm_timer_set_int_enable,
1210 .set_int_disable = omap_dm_timer_set_int_disable,
1211 .free = omap_dm_timer_free,
1212 .enable = omap_dm_timer_enable,
1213 .disable = omap_dm_timer_disable,
1214 .get_fclk = omap_dm_timer_get_fclk,
1215 .start = omap_dm_timer_start,
1216 .stop = omap_dm_timer_stop,
1217 .set_load = omap_dm_timer_set_load,
1218 .set_match = omap_dm_timer_set_match,
1219 .set_pwm = omap_dm_timer_set_pwm,
1220 .get_pwm_status = omap_dm_timer_get_pwm_status,
1221 .set_prescaler = omap_dm_timer_set_prescaler,
1222 .read_counter = omap_dm_timer_read_counter,
1223 .write_counter = omap_dm_timer_write_counter,
1224 .read_status = omap_dm_timer_read_status,
1225 .write_status = omap_dm_timer_write_status,
1228 static const struct dmtimer_platform_data omap3plus_pdata = {
1229 .timer_errata = OMAP_TIMER_ERRATA_I103_I767,
1230 .timer_ops = &dmtimer_ops,
1233 static const struct dmtimer_platform_data am6_pdata = {
1234 .timer_ops = &dmtimer_ops,
1237 static const struct of_device_id omap_timer_match[] = {
1239 .compatible = "ti,omap2420-timer",
1242 .compatible = "ti,omap3430-timer",
1243 .data = &omap3plus_pdata,
1246 .compatible = "ti,omap4430-timer",
1247 .data = &omap3plus_pdata,
1250 .compatible = "ti,omap5430-timer",
1251 .data = &omap3plus_pdata,
1254 .compatible = "ti,am335x-timer",
1255 .data = &omap3plus_pdata,
1258 .compatible = "ti,am335x-timer-1ms",
1259 .data = &omap3plus_pdata,
1262 .compatible = "ti,dm816-timer",
1263 .data = &omap3plus_pdata,
1266 .compatible = "ti,am654-timer",
1271 MODULE_DEVICE_TABLE(of, omap_timer_match);
1273 static struct platform_driver omap_dm_timer_driver = {
1274 .probe = omap_dm_timer_probe,
1275 .remove_new = omap_dm_timer_remove,
1277 .name = "omap_timer",
1278 .of_match_table = omap_timer_match,
1279 .pm = &omap_dm_timer_pm_ops,
1283 module_platform_driver(omap_dm_timer_driver);
1285 MODULE_DESCRIPTION("OMAP Dual-Mode Timer Driver");
1286 MODULE_AUTHOR("Texas Instruments Inc");