1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /**
3*4882a593Smuzhiyun * timer-ti-32k.c - OMAP2 32k Timer Support
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2009 Nokia Corporation
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Update to use new clocksource/clockevent layers
8*4882a593Smuzhiyun * Author: Kevin Hilman, MontaVista Software, Inc. <source@mvista.com>
9*4882a593Smuzhiyun * Copyright (C) 2007 MontaVista Software, Inc.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Original driver:
12*4882a593Smuzhiyun * Copyright (C) 2005 Nokia Corporation
13*4882a593Smuzhiyun * Author: Paul Mundt <paul.mundt@nokia.com>
14*4882a593Smuzhiyun * Juha Yrjölä <juha.yrjola@nokia.com>
15*4882a593Smuzhiyun * OMAP Dual-mode timer framework support by Timo Teras
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * Some parts based off of TI's 24xx code:
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * Copyright (C) 2004-2009 Texas Instruments, Inc.
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * Roughly modelled after the OMAP1 MPU timer code.
22*4882a593Smuzhiyun * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include <linux/clk.h>
28*4882a593Smuzhiyun #include <linux/init.h>
29*4882a593Smuzhiyun #include <linux/time.h>
30*4882a593Smuzhiyun #include <linux/sched_clock.h>
31*4882a593Smuzhiyun #include <linux/clocksource.h>
32*4882a593Smuzhiyun #include <linux/of.h>
33*4882a593Smuzhiyun #include <linux/of_address.h>
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun * 32KHz clocksource ... always available, on pretty most chips except
37*4882a593Smuzhiyun * OMAP 730 and 1510. Other timers could be used as clocksources, with
38*4882a593Smuzhiyun * higher resolution in free-running counter modes (e.g. 12 MHz xtal),
39*4882a593Smuzhiyun * but systems won't necessarily want to spend resources that way.
40*4882a593Smuzhiyun */
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #define OMAP2_32KSYNCNT_REV_OFF 0x0
43*4882a593Smuzhiyun #define OMAP2_32KSYNCNT_REV_SCHEME (0x3 << 30)
44*4882a593Smuzhiyun #define OMAP2_32KSYNCNT_CR_OFF_LOW 0x10
45*4882a593Smuzhiyun #define OMAP2_32KSYNCNT_CR_OFF_HIGH 0x30
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun struct ti_32k {
48*4882a593Smuzhiyun void __iomem *base;
49*4882a593Smuzhiyun void __iomem *counter;
50*4882a593Smuzhiyun struct clocksource cs;
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun
to_ti_32k(struct clocksource * cs)53*4882a593Smuzhiyun static inline struct ti_32k *to_ti_32k(struct clocksource *cs)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun return container_of(cs, struct ti_32k, cs);
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
ti_32k_read_cycles(struct clocksource * cs)58*4882a593Smuzhiyun static u64 notrace ti_32k_read_cycles(struct clocksource *cs)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun struct ti_32k *ti = to_ti_32k(cs);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun return (u64)readl_relaxed(ti->counter);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun static struct ti_32k ti_32k_timer = {
66*4882a593Smuzhiyun .cs = {
67*4882a593Smuzhiyun .name = "32k_counter",
68*4882a593Smuzhiyun .rating = 250,
69*4882a593Smuzhiyun .read = ti_32k_read_cycles,
70*4882a593Smuzhiyun .mask = CLOCKSOURCE_MASK(32),
71*4882a593Smuzhiyun .flags = CLOCK_SOURCE_IS_CONTINUOUS,
72*4882a593Smuzhiyun },
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun
omap_32k_read_sched_clock(void)75*4882a593Smuzhiyun static u64 notrace omap_32k_read_sched_clock(void)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun return ti_32k_read_cycles(&ti_32k_timer.cs);
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
ti_32k_timer_enable_clock(struct device_node * np,const char * name)80*4882a593Smuzhiyun static void __init ti_32k_timer_enable_clock(struct device_node *np,
81*4882a593Smuzhiyun const char *name)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun struct clk *clock;
84*4882a593Smuzhiyun int error;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun clock = of_clk_get_by_name(np->parent, name);
87*4882a593Smuzhiyun if (IS_ERR(clock)) {
88*4882a593Smuzhiyun /* Only some SoCs have a separate interface clock */
89*4882a593Smuzhiyun if (PTR_ERR(clock) == -EINVAL && !strncmp("ick", name, 3))
90*4882a593Smuzhiyun return;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun pr_warn("%s: could not get clock %s %li\n",
93*4882a593Smuzhiyun __func__, name, PTR_ERR(clock));
94*4882a593Smuzhiyun return;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun error = clk_prepare_enable(clock);
98*4882a593Smuzhiyun if (error) {
99*4882a593Smuzhiyun pr_warn("%s: could not enable %s: %i\n",
100*4882a593Smuzhiyun __func__, name, error);
101*4882a593Smuzhiyun return;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
ti_32k_timer_module_init(struct device_node * np,void __iomem * base)105*4882a593Smuzhiyun static void __init ti_32k_timer_module_init(struct device_node *np,
106*4882a593Smuzhiyun void __iomem *base)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun void __iomem *sysc = base + 4;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun if (!of_device_is_compatible(np->parent, "ti,sysc"))
111*4882a593Smuzhiyun return;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun ti_32k_timer_enable_clock(np, "fck");
114*4882a593Smuzhiyun ti_32k_timer_enable_clock(np, "ick");
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /*
117*4882a593Smuzhiyun * Force idle module as wkup domain is active with MPU.
118*4882a593Smuzhiyun * No need to tag the module disabled for ti-sysc probe.
119*4882a593Smuzhiyun */
120*4882a593Smuzhiyun writel_relaxed(0, sysc);
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
ti_32k_timer_init(struct device_node * np)123*4882a593Smuzhiyun static int __init ti_32k_timer_init(struct device_node *np)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun int ret;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun ti_32k_timer.base = of_iomap(np, 0);
128*4882a593Smuzhiyun if (!ti_32k_timer.base) {
129*4882a593Smuzhiyun pr_err("Can't ioremap 32k timer base\n");
130*4882a593Smuzhiyun return -ENXIO;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun if (!of_machine_is_compatible("ti,am43"))
134*4882a593Smuzhiyun ti_32k_timer.cs.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun ti_32k_timer.counter = ti_32k_timer.base;
137*4882a593Smuzhiyun ti_32k_timer_module_init(np, ti_32k_timer.base);
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /*
140*4882a593Smuzhiyun * 32k sync Counter IP register offsets vary between the highlander
141*4882a593Smuzhiyun * version and the legacy ones.
142*4882a593Smuzhiyun *
143*4882a593Smuzhiyun * The 'SCHEME' bits(30-31) of the revision register is used to identify
144*4882a593Smuzhiyun * the version.
145*4882a593Smuzhiyun */
146*4882a593Smuzhiyun if (readl_relaxed(ti_32k_timer.base + OMAP2_32KSYNCNT_REV_OFF) &
147*4882a593Smuzhiyun OMAP2_32KSYNCNT_REV_SCHEME)
148*4882a593Smuzhiyun ti_32k_timer.counter += OMAP2_32KSYNCNT_CR_OFF_HIGH;
149*4882a593Smuzhiyun else
150*4882a593Smuzhiyun ti_32k_timer.counter += OMAP2_32KSYNCNT_CR_OFF_LOW;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun pr_info("OMAP clocksource: 32k_counter at 32768 Hz\n");
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun ret = clocksource_register_hz(&ti_32k_timer.cs, 32768);
155*4882a593Smuzhiyun if (ret) {
156*4882a593Smuzhiyun pr_err("32k_counter: can't register clocksource\n");
157*4882a593Smuzhiyun return ret;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun sched_clock_register(omap_32k_read_sched_clock, 32, 32768);
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun return 0;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun TIMER_OF_DECLARE(ti_32k_timer, "ti,omap-counter32k",
165*4882a593Smuzhiyun ti_32k_timer_init);
166