0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 #include <linux/clk.h>
0028 #include <linux/init.h>
0029 #include <linux/time.h>
0030 #include <linux/sched_clock.h>
0031 #include <linux/clocksource.h>
0032 #include <linux/of.h>
0033 #include <linux/of_address.h>
0034
0035
0036
0037
0038
0039
0040
0041
0042 #define OMAP2_32KSYNCNT_REV_OFF 0x0
0043 #define OMAP2_32KSYNCNT_REV_SCHEME (0x3 << 30)
0044 #define OMAP2_32KSYNCNT_CR_OFF_LOW 0x10
0045 #define OMAP2_32KSYNCNT_CR_OFF_HIGH 0x30
0046
0047 struct ti_32k {
0048 void __iomem *base;
0049 void __iomem *counter;
0050 struct clocksource cs;
0051 };
0052
0053 static inline struct ti_32k *to_ti_32k(struct clocksource *cs)
0054 {
0055 return container_of(cs, struct ti_32k, cs);
0056 }
0057
0058 static u64 notrace ti_32k_read_cycles(struct clocksource *cs)
0059 {
0060 struct ti_32k *ti = to_ti_32k(cs);
0061
0062 return (u64)readl_relaxed(ti->counter);
0063 }
0064
0065 static struct ti_32k ti_32k_timer = {
0066 .cs = {
0067 .name = "32k_counter",
0068 .rating = 250,
0069 .read = ti_32k_read_cycles,
0070 .mask = CLOCKSOURCE_MASK(32),
0071 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
0072 },
0073 };
0074
0075 static u64 notrace omap_32k_read_sched_clock(void)
0076 {
0077 return ti_32k_read_cycles(&ti_32k_timer.cs);
0078 }
0079
0080 static void __init ti_32k_timer_enable_clock(struct device_node *np,
0081 const char *name)
0082 {
0083 struct clk *clock;
0084 int error;
0085
0086 clock = of_clk_get_by_name(np->parent, name);
0087 if (IS_ERR(clock)) {
0088
0089 if (PTR_ERR(clock) == -EINVAL && !strncmp("ick", name, 3))
0090 return;
0091
0092 pr_warn("%s: could not get clock %s %li\n",
0093 __func__, name, PTR_ERR(clock));
0094 return;
0095 }
0096
0097 error = clk_prepare_enable(clock);
0098 if (error) {
0099 pr_warn("%s: could not enable %s: %i\n",
0100 __func__, name, error);
0101 return;
0102 }
0103 }
0104
0105 static void __init ti_32k_timer_module_init(struct device_node *np,
0106 void __iomem *base)
0107 {
0108 void __iomem *sysc = base + 4;
0109
0110 if (!of_device_is_compatible(np->parent, "ti,sysc"))
0111 return;
0112
0113 ti_32k_timer_enable_clock(np, "fck");
0114 ti_32k_timer_enable_clock(np, "ick");
0115
0116
0117
0118
0119
0120 writel_relaxed(0, sysc);
0121 }
0122
0123 static int __init ti_32k_timer_init(struct device_node *np)
0124 {
0125 int ret;
0126
0127 ti_32k_timer.base = of_iomap(np, 0);
0128 if (!ti_32k_timer.base) {
0129 pr_err("Can't ioremap 32k timer base\n");
0130 return -ENXIO;
0131 }
0132
0133 if (!of_machine_is_compatible("ti,am43"))
0134 ti_32k_timer.cs.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
0135
0136 ti_32k_timer.counter = ti_32k_timer.base;
0137 ti_32k_timer_module_init(np, ti_32k_timer.base);
0138
0139
0140
0141
0142
0143
0144
0145
0146 if (readl_relaxed(ti_32k_timer.base + OMAP2_32KSYNCNT_REV_OFF) &
0147 OMAP2_32KSYNCNT_REV_SCHEME)
0148 ti_32k_timer.counter += OMAP2_32KSYNCNT_CR_OFF_HIGH;
0149 else
0150 ti_32k_timer.counter += OMAP2_32KSYNCNT_CR_OFF_LOW;
0151
0152 pr_info("OMAP clocksource: 32k_counter at 32768 Hz\n");
0153
0154 ret = clocksource_register_hz(&ti_32k_timer.cs, 32768);
0155 if (ret) {
0156 pr_err("32k_counter: can't register clocksource\n");
0157 return ret;
0158 }
0159
0160 sched_clock_register(omap_32k_read_sched_clock, 32, 32768);
0161
0162 return 0;
0163 }
0164 TIMER_OF_DECLARE(ti_32k_timer, "ti,omap-counter32k",
0165 ti_32k_timer_init);