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;
143 struct notifier_block fclk_nb;
144 unsigned long fclk_rate;
147 static u32 omap_reserved_systimers;
148 static LIST_HEAD(omap_timer_list);
149 static DEFINE_SPINLOCK(dm_timer_lock);
159 * dmtimer_read - read timer registers in posted and non-posted mode
160 * @timer: timer pointer over which read operation to perform
161 * @reg: lowest byte holds the register offset
163 * The posted mode bit is encoded in reg. Note that in posted mode, write
164 * pending bit must be checked. Otherwise a read of a non completed write
165 * will produce an error.
167 static inline u32 dmtimer_read(struct dmtimer *timer, u32 reg)
174 /* Wait for a possible write pending bit in posted mode */
175 if (wp && timer->posted)
176 while (readl_relaxed(timer->pend) & wp)
179 return readl_relaxed(timer->func_base + offset);
183 * dmtimer_write - write timer registers in posted and non-posted mode
184 * @timer: timer pointer over which write operation is to perform
185 * @reg: lowest byte holds the register offset
186 * @val: data to write into the register
188 * The posted mode bit is encoded in reg. Note that in posted mode, the write
189 * pending bit must be checked. Otherwise a write on a register which has a
190 * pending write will be lost.
192 static inline void dmtimer_write(struct dmtimer *timer, u32 reg, u32 val)
199 /* Wait for a possible write pending bit in posted mode */
200 if (wp && timer->posted)
201 while (readl_relaxed(timer->pend) & wp)
204 writel_relaxed(val, timer->func_base + offset);
207 static inline void __omap_dm_timer_init_regs(struct dmtimer *timer)
211 /* Assume v1 ip if bits [31:16] are zero */
212 tidr = readl_relaxed(timer->io_base);
215 timer->irq_stat = OMAP_TIMER_V1_STAT_OFFSET;
216 timer->irq_ena = OMAP_TIMER_V1_INT_EN_OFFSET;
217 timer->irq_dis = OMAP_TIMER_V1_INT_EN_OFFSET;
218 timer->pend = timer->io_base + _OMAP_TIMER_WRITE_PEND_OFFSET;
219 timer->func_base = timer->io_base;
222 timer->irq_stat = OMAP_TIMER_V2_IRQSTATUS - OMAP_TIMER_V2_FUNC_OFFSET;
223 timer->irq_ena = OMAP_TIMER_V2_IRQENABLE_SET - OMAP_TIMER_V2_FUNC_OFFSET;
224 timer->irq_dis = OMAP_TIMER_V2_IRQENABLE_CLR - OMAP_TIMER_V2_FUNC_OFFSET;
225 timer->pend = timer->io_base +
226 _OMAP_TIMER_WRITE_PEND_OFFSET +
227 OMAP_TIMER_V2_FUNC_OFFSET;
228 timer->func_base = timer->io_base + OMAP_TIMER_V2_FUNC_OFFSET;
233 * __omap_dm_timer_enable_posted - enables write posted mode
234 * @timer: pointer to timer instance handle
236 * Enables the write posted mode for the timer. When posted mode is enabled
237 * writes to certain timer registers are immediately acknowledged by the
238 * internal bus and hence prevents stalling the CPU waiting for the write to
239 * complete. Enabling this feature can improve performance for writing to the
242 static inline void __omap_dm_timer_enable_posted(struct dmtimer *timer)
247 if (timer->errata & OMAP_TIMER_ERRATA_I103_I767) {
248 timer->posted = OMAP_TIMER_NONPOSTED;
249 dmtimer_write(timer, OMAP_TIMER_IF_CTRL_REG, 0);
253 dmtimer_write(timer, OMAP_TIMER_IF_CTRL_REG, OMAP_TIMER_CTRL_POSTED);
254 timer->context.tsicr = OMAP_TIMER_CTRL_POSTED;
255 timer->posted = OMAP_TIMER_POSTED;
258 static inline void __omap_dm_timer_stop(struct dmtimer *timer)
262 l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
263 if (l & OMAP_TIMER_CTRL_ST) {
265 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
266 #ifdef CONFIG_ARCH_OMAP2PLUS
267 /* Readback to make sure write has completed */
268 dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
270 * Wait for functional clock period x 3.5 to make sure that
273 udelay(3500000 / timer->fclk_rate + 1);
277 /* Ack possibly pending interrupt */
278 dmtimer_write(timer, timer->irq_stat, OMAP_TIMER_INT_OVERFLOW);
281 static inline void __omap_dm_timer_int_enable(struct dmtimer *timer,
284 dmtimer_write(timer, timer->irq_ena, value);
285 dmtimer_write(timer, OMAP_TIMER_WAKEUP_EN_REG, value);
288 static inline unsigned int
289 __omap_dm_timer_read_counter(struct dmtimer *timer)
291 return dmtimer_read(timer, OMAP_TIMER_COUNTER_REG);
294 static inline void __omap_dm_timer_write_status(struct dmtimer *timer,
297 dmtimer_write(timer, timer->irq_stat, value);
300 static void omap_timer_restore_context(struct dmtimer *timer)
302 dmtimer_write(timer, OMAP_TIMER_OCP_CFG_OFFSET, timer->context.ocp_cfg);
304 dmtimer_write(timer, OMAP_TIMER_WAKEUP_EN_REG, timer->context.twer);
305 dmtimer_write(timer, OMAP_TIMER_COUNTER_REG, timer->context.tcrr);
306 dmtimer_write(timer, OMAP_TIMER_LOAD_REG, timer->context.tldr);
307 dmtimer_write(timer, OMAP_TIMER_MATCH_REG, timer->context.tmar);
308 dmtimer_write(timer, OMAP_TIMER_IF_CTRL_REG, timer->context.tsicr);
309 dmtimer_write(timer, timer->irq_ena, timer->context.tier);
310 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, timer->context.tclr);
313 static void omap_timer_save_context(struct dmtimer *timer)
315 timer->context.ocp_cfg = dmtimer_read(timer, OMAP_TIMER_OCP_CFG_OFFSET);
317 timer->context.tclr = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
318 timer->context.twer = dmtimer_read(timer, OMAP_TIMER_WAKEUP_EN_REG);
319 timer->context.tldr = dmtimer_read(timer, OMAP_TIMER_LOAD_REG);
320 timer->context.tmar = dmtimer_read(timer, OMAP_TIMER_MATCH_REG);
321 timer->context.tier = dmtimer_read(timer, timer->irq_ena);
322 timer->context.tsicr = dmtimer_read(timer, OMAP_TIMER_IF_CTRL_REG);
325 static int omap_timer_context_notifier(struct notifier_block *nb,
326 unsigned long cmd, void *v)
328 struct dmtimer *timer;
330 timer = container_of(nb, struct dmtimer, nb);
333 case CPU_CLUSTER_PM_ENTER:
334 if ((timer->capability & OMAP_TIMER_ALWON) ||
335 !atomic_read(&timer->enabled))
337 omap_timer_save_context(timer);
339 case CPU_CLUSTER_PM_ENTER_FAILED: /* No need to restore context */
341 case CPU_CLUSTER_PM_EXIT:
342 if ((timer->capability & OMAP_TIMER_ALWON) ||
343 !atomic_read(&timer->enabled))
345 omap_timer_restore_context(timer);
352 static int omap_timer_fclk_notifier(struct notifier_block *nb,
353 unsigned long event, void *data)
355 struct clk_notifier_data *clk_data = data;
356 struct dmtimer *timer = container_of(nb, struct dmtimer, fclk_nb);
359 case POST_RATE_CHANGE:
360 timer->fclk_rate = clk_data->new_rate;
367 static int omap_dm_timer_reset(struct dmtimer *timer)
369 u32 l, timeout = 100000;
371 if (timer->revision != 1)
374 dmtimer_write(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
377 l = dmtimer_read(timer, OMAP_TIMER_V1_SYS_STAT_OFFSET);
378 } while (!l && timeout--);
381 dev_err(&timer->pdev->dev, "Timer failed to reset\n");
385 /* Configure timer for smart-idle mode */
386 l = dmtimer_read(timer, OMAP_TIMER_OCP_CFG_OFFSET);
388 dmtimer_write(timer, OMAP_TIMER_OCP_CFG_OFFSET, l);
396 * Functions exposed to PWM and remoteproc drivers via platform_data.
397 * Do not use these in the driver, these will get deprecated and will
398 * will be replaced by Linux generic framework functions such as
399 * chained interrupts and clock framework.
401 static struct dmtimer *to_dmtimer(struct omap_dm_timer *cookie)
406 return container_of(cookie, struct dmtimer, cookie);
409 static int omap_dm_timer_set_source(struct omap_dm_timer *cookie, int source)
412 const char *parent_name;
414 struct dmtimer_platform_data *pdata;
415 struct dmtimer *timer;
417 timer = to_dmtimer(cookie);
418 if (unlikely(!timer) || IS_ERR(timer->fclk))
422 case OMAP_TIMER_SRC_SYS_CLK:
423 parent_name = "timer_sys_ck";
425 case OMAP_TIMER_SRC_32_KHZ:
426 parent_name = "timer_32k_ck";
428 case OMAP_TIMER_SRC_EXT_CLK:
429 parent_name = "timer_ext_ck";
435 pdata = timer->pdev->dev.platform_data;
438 * FIXME: Used for OMAP1 devices only because they do not currently
439 * use the clock framework to set the parent clock. To be removed
440 * once OMAP1 migrated to using clock framework for dmtimers
442 if (timer->omap1 && pdata && pdata->set_timer_src)
443 return pdata->set_timer_src(timer->pdev, source);
445 #if defined(CONFIG_COMMON_CLK)
446 /* Check if the clock has configurable parents */
447 if (clk_hw_get_num_parents(__clk_get_hw(timer->fclk)) < 2)
451 parent = clk_get(&timer->pdev->dev, parent_name);
452 if (IS_ERR(parent)) {
453 pr_err("%s: %s not found\n", __func__, parent_name);
457 ret = clk_set_parent(timer->fclk, parent);
459 pr_err("%s: failed to set %s as parent\n", __func__,
467 static void omap_dm_timer_enable(struct omap_dm_timer *cookie)
469 struct dmtimer *timer = to_dmtimer(cookie);
470 struct device *dev = &timer->pdev->dev;
473 rc = pm_runtime_resume_and_get(dev);
475 dev_err(dev, "could not enable timer\n");
478 static void omap_dm_timer_disable(struct omap_dm_timer *cookie)
480 struct dmtimer *timer = to_dmtimer(cookie);
481 struct device *dev = &timer->pdev->dev;
483 pm_runtime_put_sync(dev);
486 static int omap_dm_timer_prepare(struct dmtimer *timer)
488 struct device *dev = &timer->pdev->dev;
491 rc = pm_runtime_resume_and_get(dev);
495 if (timer->capability & OMAP_TIMER_NEEDS_RESET) {
496 rc = omap_dm_timer_reset(timer);
498 pm_runtime_put_sync(dev);
503 __omap_dm_timer_enable_posted(timer);
504 pm_runtime_put_sync(dev);
509 static inline u32 omap_dm_timer_reserved_systimer(int id)
511 return (omap_reserved_systimers & (1 << (id - 1))) ? 1 : 0;
514 static struct dmtimer *_omap_dm_timer_request(int req_type, void *data)
516 struct dmtimer *timer = NULL, *t;
517 struct device_node *np = NULL;
529 case REQUEST_BY_NODE:
530 np = (struct device_node *)data;
537 spin_lock_irqsave(&dm_timer_lock, flags);
538 list_for_each_entry(t, &omap_timer_list, node) {
544 if (id == t->pdev->id) {
551 if (cap == (t->capability & cap)) {
553 * If timer is not NULL, we have already found
554 * one timer. But it was not an exact match
555 * because it had more capabilities than what
556 * was required. Therefore, unreserve the last
557 * timer found and see if this one is a better
565 /* Exit loop early if we find an exact match */
566 if (t->capability == cap)
570 case REQUEST_BY_NODE:
571 if (np == t->pdev->dev.of_node) {
585 spin_unlock_irqrestore(&dm_timer_lock, flags);
587 if (timer && omap_dm_timer_prepare(timer)) {
593 pr_debug("%s: timer request failed!\n", __func__);
598 static struct omap_dm_timer *omap_dm_timer_request(void)
600 struct dmtimer *timer;
602 timer = _omap_dm_timer_request(REQUEST_ANY, NULL);
606 return &timer->cookie;
609 static struct omap_dm_timer *omap_dm_timer_request_specific(int id)
611 struct dmtimer *timer;
613 /* Requesting timer by ID is not supported when device tree is used */
614 if (of_have_populated_dt()) {
615 pr_warn("%s: Please use omap_dm_timer_request_by_node()\n",
620 timer = _omap_dm_timer_request(REQUEST_BY_ID, &id);
624 return &timer->cookie;
628 * omap_dm_timer_request_by_node - Request a timer by device-tree node
629 * @np: Pointer to device-tree timer node
631 * Request a timer based upon a device node pointer. Returns pointer to
632 * timer handle on success and a NULL pointer on failure.
634 static struct omap_dm_timer *omap_dm_timer_request_by_node(struct device_node *np)
636 struct dmtimer *timer;
641 timer = _omap_dm_timer_request(REQUEST_BY_NODE, np);
645 return &timer->cookie;
648 static int omap_dm_timer_free(struct omap_dm_timer *cookie)
650 struct dmtimer *timer;
654 timer = to_dmtimer(cookie);
655 if (unlikely(!timer))
658 WARN_ON(!timer->reserved);
661 dev = &timer->pdev->dev;
662 rc = pm_runtime_resume_and_get(dev);
666 /* Clear timer configuration */
667 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, 0);
669 pm_runtime_put_sync(dev);
674 static int omap_dm_timer_get_irq(struct omap_dm_timer *cookie)
676 struct dmtimer *timer = to_dmtimer(cookie);
682 #if defined(CONFIG_ARCH_OMAP1)
683 #include <linux/soc/ti/omap1-io.h>
685 static struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *cookie)
691 * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
692 * @inputmask: current value of idlect mask
694 __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
697 struct dmtimer *timer = NULL;
700 /* If ARMXOR cannot be idled this function call is unnecessary */
701 if (!(inputmask & (1 << 1)))
704 /* If any active timer is using ARMXOR return modified mask */
705 spin_lock_irqsave(&dm_timer_lock, flags);
706 list_for_each_entry(timer, &omap_timer_list, node) {
709 l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
710 if (l & OMAP_TIMER_CTRL_ST) {
711 if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
712 inputmask &= ~(1 << 1);
714 inputmask &= ~(1 << 2);
718 spin_unlock_irqrestore(&dm_timer_lock, flags);
725 static struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *cookie)
727 struct dmtimer *timer = to_dmtimer(cookie);
729 if (timer && !IS_ERR(timer->fclk))
734 __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
743 static int omap_dm_timer_start(struct omap_dm_timer *cookie)
745 struct dmtimer *timer;
750 timer = to_dmtimer(cookie);
751 if (unlikely(!timer))
754 dev = &timer->pdev->dev;
756 rc = pm_runtime_resume_and_get(dev);
760 l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
761 if (!(l & OMAP_TIMER_CTRL_ST)) {
762 l |= OMAP_TIMER_CTRL_ST;
763 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
769 static int omap_dm_timer_stop(struct omap_dm_timer *cookie)
771 struct dmtimer *timer;
774 timer = to_dmtimer(cookie);
775 if (unlikely(!timer))
778 dev = &timer->pdev->dev;
780 __omap_dm_timer_stop(timer);
782 pm_runtime_put_sync(dev);
787 static int omap_dm_timer_set_load(struct omap_dm_timer *cookie,
790 struct dmtimer *timer;
794 timer = to_dmtimer(cookie);
795 if (unlikely(!timer))
798 dev = &timer->pdev->dev;
799 rc = pm_runtime_resume_and_get(dev);
803 dmtimer_write(timer, OMAP_TIMER_LOAD_REG, load);
805 pm_runtime_put_sync(dev);
810 static int omap_dm_timer_set_match(struct omap_dm_timer *cookie, int enable,
813 struct dmtimer *timer;
818 timer = to_dmtimer(cookie);
819 if (unlikely(!timer))
822 dev = &timer->pdev->dev;
823 rc = pm_runtime_resume_and_get(dev);
827 l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
829 l |= OMAP_TIMER_CTRL_CE;
831 l &= ~OMAP_TIMER_CTRL_CE;
832 dmtimer_write(timer, OMAP_TIMER_MATCH_REG, match);
833 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
835 pm_runtime_put_sync(dev);
840 static int omap_dm_timer_set_pwm(struct omap_dm_timer *cookie, int def_on,
841 int toggle, int trigger, int autoreload)
843 struct dmtimer *timer;
848 timer = to_dmtimer(cookie);
849 if (unlikely(!timer))
852 dev = &timer->pdev->dev;
853 rc = pm_runtime_resume_and_get(dev);
857 l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
858 l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
859 OMAP_TIMER_CTRL_PT | (0x03 << 10) | OMAP_TIMER_CTRL_AR);
861 l |= OMAP_TIMER_CTRL_SCPWM;
863 l |= OMAP_TIMER_CTRL_PT;
866 l |= OMAP_TIMER_CTRL_AR;
867 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
869 pm_runtime_put_sync(dev);
874 static int omap_dm_timer_get_pwm_status(struct omap_dm_timer *cookie)
876 struct dmtimer *timer;
881 timer = to_dmtimer(cookie);
882 if (unlikely(!timer))
885 dev = &timer->pdev->dev;
886 rc = pm_runtime_resume_and_get(dev);
890 l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
892 pm_runtime_put_sync(dev);
897 static int omap_dm_timer_set_prescaler(struct omap_dm_timer *cookie,
900 struct dmtimer *timer;
905 timer = to_dmtimer(cookie);
906 if (unlikely(!timer) || prescaler < -1 || prescaler > 7)
909 dev = &timer->pdev->dev;
910 rc = pm_runtime_resume_and_get(dev);
914 l = dmtimer_read(timer, OMAP_TIMER_CTRL_REG);
915 l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
916 if (prescaler >= 0) {
917 l |= OMAP_TIMER_CTRL_PRE;
920 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, l);
922 pm_runtime_put_sync(dev);
927 static int omap_dm_timer_set_int_enable(struct omap_dm_timer *cookie,
930 struct dmtimer *timer;
934 timer = to_dmtimer(cookie);
935 if (unlikely(!timer))
938 dev = &timer->pdev->dev;
939 rc = pm_runtime_resume_and_get(dev);
943 __omap_dm_timer_int_enable(timer, value);
945 pm_runtime_put_sync(dev);
951 * omap_dm_timer_set_int_disable - disable timer interrupts
952 * @cookie: pointer to timer cookie
953 * @mask: bit mask of interrupts to be disabled
955 * Disables the specified timer interrupts for a timer.
957 static int omap_dm_timer_set_int_disable(struct omap_dm_timer *cookie, u32 mask)
959 struct dmtimer *timer;
964 timer = to_dmtimer(cookie);
965 if (unlikely(!timer))
968 dev = &timer->pdev->dev;
969 rc = pm_runtime_resume_and_get(dev);
973 if (timer->revision == 1)
974 l = dmtimer_read(timer, timer->irq_ena) & ~mask;
976 dmtimer_write(timer, timer->irq_dis, l);
977 l = dmtimer_read(timer, OMAP_TIMER_WAKEUP_EN_REG) & ~mask;
978 dmtimer_write(timer, OMAP_TIMER_WAKEUP_EN_REG, l);
980 pm_runtime_put_sync(dev);
985 static unsigned int omap_dm_timer_read_status(struct omap_dm_timer *cookie)
987 struct dmtimer *timer;
990 timer = to_dmtimer(cookie);
991 if (unlikely(!timer || !atomic_read(&timer->enabled))) {
992 pr_err("%s: timer not available or enabled.\n", __func__);
996 l = dmtimer_read(timer, timer->irq_stat);
1001 static int omap_dm_timer_write_status(struct omap_dm_timer *cookie, unsigned int value)
1003 struct dmtimer *timer;
1005 timer = to_dmtimer(cookie);
1006 if (unlikely(!timer || !atomic_read(&timer->enabled)))
1009 __omap_dm_timer_write_status(timer, value);
1014 static unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *cookie)
1016 struct dmtimer *timer;
1018 timer = to_dmtimer(cookie);
1019 if (unlikely(!timer || !atomic_read(&timer->enabled))) {
1020 pr_err("%s: timer not iavailable or enabled.\n", __func__);
1024 return __omap_dm_timer_read_counter(timer);
1027 static int omap_dm_timer_write_counter(struct omap_dm_timer *cookie, unsigned int value)
1029 struct dmtimer *timer;
1031 timer = to_dmtimer(cookie);
1032 if (unlikely(!timer || !atomic_read(&timer->enabled))) {
1033 pr_err("%s: timer not available or enabled.\n", __func__);
1037 dmtimer_write(timer, OMAP_TIMER_COUNTER_REG, value);
1039 /* Save the context */
1040 timer->context.tcrr = value;
1044 static int __maybe_unused omap_dm_timer_runtime_suspend(struct device *dev)
1046 struct dmtimer *timer = dev_get_drvdata(dev);
1048 atomic_set(&timer->enabled, 0);
1050 if (timer->capability & OMAP_TIMER_ALWON || !timer->func_base)
1053 omap_timer_save_context(timer);
1058 static int __maybe_unused omap_dm_timer_runtime_resume(struct device *dev)
1060 struct dmtimer *timer = dev_get_drvdata(dev);
1062 if (!(timer->capability & OMAP_TIMER_ALWON) && timer->func_base)
1063 omap_timer_restore_context(timer);
1065 atomic_set(&timer->enabled, 1);
1070 static const struct dev_pm_ops omap_dm_timer_pm_ops = {
1071 SET_RUNTIME_PM_OPS(omap_dm_timer_runtime_suspend,
1072 omap_dm_timer_runtime_resume, NULL)
1075 static const struct of_device_id omap_timer_match[];
1078 * omap_dm_timer_probe - probe function called for every registered device
1079 * @pdev: pointer to current timer platform device
1081 * Called by driver framework at the end of device registration for all
1084 static int omap_dm_timer_probe(struct platform_device *pdev)
1086 unsigned long flags;
1087 struct dmtimer *timer;
1088 struct device *dev = &pdev->dev;
1089 const struct dmtimer_platform_data *pdata;
1092 pdata = of_device_get_match_data(dev);
1094 pdata = dev_get_platdata(dev);
1096 dev->platform_data = (void *)pdata;
1099 dev_err(dev, "%s: no platform data.\n", __func__);
1103 timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL);
1107 timer->irq = platform_get_irq(pdev, 0);
1111 timer->io_base = devm_platform_ioremap_resource(pdev, 0);
1112 if (IS_ERR(timer->io_base))
1113 return PTR_ERR(timer->io_base);
1115 platform_set_drvdata(pdev, timer);
1118 if (of_property_read_bool(dev->of_node, "ti,timer-alwon"))
1119 timer->capability |= OMAP_TIMER_ALWON;
1120 if (of_property_read_bool(dev->of_node, "ti,timer-dsp"))
1121 timer->capability |= OMAP_TIMER_HAS_DSP_IRQ;
1122 if (of_property_read_bool(dev->of_node, "ti,timer-pwm"))
1123 timer->capability |= OMAP_TIMER_HAS_PWM;
1124 if (of_property_read_bool(dev->of_node, "ti,timer-secure"))
1125 timer->capability |= OMAP_TIMER_SECURE;
1127 timer->id = pdev->id;
1128 timer->capability = pdata->timer_capability;
1129 timer->reserved = omap_dm_timer_reserved_systimer(timer->id);
1132 timer->omap1 = timer->capability & OMAP_TIMER_NEEDS_RESET;
1134 /* OMAP1 devices do not yet use the clock framework for dmtimers */
1135 if (!timer->omap1) {
1136 timer->fclk = devm_clk_get(dev, "fck");
1137 if (IS_ERR(timer->fclk))
1138 return PTR_ERR(timer->fclk);
1140 timer->fclk_nb.notifier_call = omap_timer_fclk_notifier;
1141 ret = devm_clk_notifier_register(dev, timer->fclk,
1146 timer->fclk_rate = clk_get_rate(timer->fclk);
1148 timer->fclk = ERR_PTR(-ENODEV);
1151 if (!(timer->capability & OMAP_TIMER_ALWON)) {
1152 timer->nb.notifier_call = omap_timer_context_notifier;
1153 cpu_pm_register_notifier(&timer->nb);
1156 timer->errata = pdata->timer_errata;
1160 pm_runtime_enable(dev);
1162 if (!timer->reserved) {
1163 ret = pm_runtime_resume_and_get(dev);
1165 dev_err(dev, "%s: pm_runtime_get_sync failed!\n",
1169 __omap_dm_timer_init_regs(timer);
1171 /* Clear timer configuration */
1172 dmtimer_write(timer, OMAP_TIMER_CTRL_REG, 0);
1174 pm_runtime_put(dev);
1177 /* add the timer element to the list */
1178 spin_lock_irqsave(&dm_timer_lock, flags);
1179 list_add_tail(&timer->node, &omap_timer_list);
1180 spin_unlock_irqrestore(&dm_timer_lock, flags);
1182 dev_dbg(dev, "Device Probed.\n");
1187 pm_runtime_disable(dev);
1192 * omap_dm_timer_remove - cleanup a registered timer device
1193 * @pdev: pointer to current timer platform device
1195 * Called by driver framework whenever a timer device is unregistered.
1196 * In addition to freeing platform resources it also deletes the timer
1197 * entry from the local list.
1199 static void omap_dm_timer_remove(struct platform_device *pdev)
1201 struct dmtimer *timer;
1202 unsigned long flags;
1205 spin_lock_irqsave(&dm_timer_lock, flags);
1206 list_for_each_entry(timer, &omap_timer_list, node)
1207 if (!strcmp(dev_name(&timer->pdev->dev),
1208 dev_name(&pdev->dev))) {
1209 if (!(timer->capability & OMAP_TIMER_ALWON))
1210 cpu_pm_unregister_notifier(&timer->nb);
1211 list_del(&timer->node);
1215 spin_unlock_irqrestore(&dm_timer_lock, flags);
1217 pm_runtime_disable(&pdev->dev);
1220 dev_err(&pdev->dev, "Unable to determine timer entry in list of drivers on remove\n");
1223 static const struct omap_dm_timer_ops dmtimer_ops = {
1224 .request_by_node = omap_dm_timer_request_by_node,
1225 .request_specific = omap_dm_timer_request_specific,
1226 .request = omap_dm_timer_request,
1227 .set_source = omap_dm_timer_set_source,
1228 .get_irq = omap_dm_timer_get_irq,
1229 .set_int_enable = omap_dm_timer_set_int_enable,
1230 .set_int_disable = omap_dm_timer_set_int_disable,
1231 .free = omap_dm_timer_free,
1232 .enable = omap_dm_timer_enable,
1233 .disable = omap_dm_timer_disable,
1234 .get_fclk = omap_dm_timer_get_fclk,
1235 .start = omap_dm_timer_start,
1236 .stop = omap_dm_timer_stop,
1237 .set_load = omap_dm_timer_set_load,
1238 .set_match = omap_dm_timer_set_match,
1239 .set_pwm = omap_dm_timer_set_pwm,
1240 .get_pwm_status = omap_dm_timer_get_pwm_status,
1241 .set_prescaler = omap_dm_timer_set_prescaler,
1242 .read_counter = omap_dm_timer_read_counter,
1243 .write_counter = omap_dm_timer_write_counter,
1244 .read_status = omap_dm_timer_read_status,
1245 .write_status = omap_dm_timer_write_status,
1248 static const struct dmtimer_platform_data omap3plus_pdata = {
1249 .timer_errata = OMAP_TIMER_ERRATA_I103_I767,
1250 .timer_ops = &dmtimer_ops,
1253 static const struct dmtimer_platform_data am6_pdata = {
1254 .timer_ops = &dmtimer_ops,
1257 static const struct of_device_id omap_timer_match[] = {
1259 .compatible = "ti,omap2420-timer",
1262 .compatible = "ti,omap3430-timer",
1263 .data = &omap3plus_pdata,
1266 .compatible = "ti,omap4430-timer",
1267 .data = &omap3plus_pdata,
1270 .compatible = "ti,omap5430-timer",
1271 .data = &omap3plus_pdata,
1274 .compatible = "ti,am335x-timer",
1275 .data = &omap3plus_pdata,
1278 .compatible = "ti,am335x-timer-1ms",
1279 .data = &omap3plus_pdata,
1282 .compatible = "ti,dm816-timer",
1283 .data = &omap3plus_pdata,
1286 .compatible = "ti,am654-timer",
1291 MODULE_DEVICE_TABLE(of, omap_timer_match);
1293 static struct platform_driver omap_dm_timer_driver = {
1294 .probe = omap_dm_timer_probe,
1295 .remove_new = omap_dm_timer_remove,
1297 .name = "omap_timer",
1298 .of_match_table = omap_timer_match,
1299 .pm = &omap_dm_timer_pm_ops,
1303 module_platform_driver(omap_dm_timer_driver);
1305 MODULE_DESCRIPTION("OMAP Dual-Mode Timer Driver");
1306 MODULE_AUTHOR("Texas Instruments Inc");