Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 2 | /* |
| 3 | * OMAP 32ksynctimer/counter_32k-related code |
| 4 | * |
| 5 | * Copyright (C) 2009 Texas Instruments |
| 6 | * Copyright (C) 2010 Nokia Corporation |
| 7 | * Tony Lindgren <tony@atomide.com> |
| 8 | * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com> |
| 9 | * |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 10 | * NOTE: This timer is not the same timer as the old OMAP1 MPU timer. |
| 11 | */ |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/clk.h> |
Vasiliy Kulikov | cb9675f | 2010-11-26 17:06:02 +0000 | [diff] [blame] | 15 | #include <linux/err.h> |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 16 | #include <linux/io.h> |
Russell King - ARM Linux | 354a183 | 2011-07-10 23:05:34 -0700 | [diff] [blame] | 17 | #include <linux/clocksource.h> |
Stephen Boyd | 38ff87f | 2013-06-01 23:39:40 -0700 | [diff] [blame] | 18 | #include <linux/sched_clock.h> |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 19 | |
Marc Zyngier | bd0493e | 2012-05-05 19:28:44 +0100 | [diff] [blame] | 20 | #include <asm/mach/time.h> |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 21 | |
Paul Walmsley | 6ccc432 | 2012-12-10 11:48:44 -0700 | [diff] [blame] | 22 | #include <plat/counter-32k.h> |
| 23 | |
Vaibhav Hiremath | 1fe97c8 | 2012-05-09 10:07:05 -0700 | [diff] [blame] | 24 | /* OMAP2_32KSYNCNT_CR_OFF: offset of 32ksync counter register */ |
R Sricharan | b009366 | 2012-05-10 14:17:22 +0530 | [diff] [blame] | 25 | #define OMAP2_32KSYNCNT_REV_OFF 0x0 |
| 26 | #define OMAP2_32KSYNCNT_REV_SCHEME (0x3 << 30) |
| 27 | #define OMAP2_32KSYNCNT_CR_OFF_LOW 0x10 |
| 28 | #define OMAP2_32KSYNCNT_CR_OFF_HIGH 0x30 |
Vaibhav Hiremath | 1fe97c8 | 2012-05-09 10:07:05 -0700 | [diff] [blame] | 29 | |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 30 | /* |
| 31 | * 32KHz clocksource ... always available, on pretty most chips except |
| 32 | * OMAP 730 and 1510. Other timers could be used as clocksources, with |
| 33 | * higher resolution in free-running counter modes (e.g. 12 MHz xtal), |
| 34 | * but systems won't necessarily want to spend resources that way. |
| 35 | */ |
Vaibhav Hiremath | 1fe97c8 | 2012-05-09 10:07:05 -0700 | [diff] [blame] | 36 | static void __iomem *sync32k_cnt_reg; |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 37 | |
Stephen Boyd | 8f0678f | 2013-11-15 15:26:23 -0800 | [diff] [blame] | 38 | static u64 notrace omap_32k_read_sched_clock(void) |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 39 | { |
Victor Kamensky | f6f3b50 | 2014-04-15 20:37:48 +0300 | [diff] [blame] | 40 | return sync32k_cnt_reg ? readl_relaxed(sync32k_cnt_reg) : 0; |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
Xunlei Pang | a451570 | 2015-04-01 20:34:24 -0700 | [diff] [blame] | 44 | * omap_read_persistent_clock64 - Return time from a persistent clock. |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 45 | * |
| 46 | * Reads the time from a source which isn't disabled during PM, the |
| 47 | * 32k sync timer. Convert the cycles elapsed since last read into |
Xunlei Pang | a451570 | 2015-04-01 20:34:24 -0700 | [diff] [blame] | 48 | * nsecs and adds to a monotonically increasing timespec64. |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 49 | */ |
Xunlei Pang | a451570 | 2015-04-01 20:34:24 -0700 | [diff] [blame] | 50 | static struct timespec64 persistent_ts; |
Colin Cross | 9d7d6e3 | 2012-10-08 14:01:12 -0700 | [diff] [blame] | 51 | static cycles_t cycles; |
Russell King - ARM Linux | 354a183 | 2011-07-10 23:05:34 -0700 | [diff] [blame] | 52 | static unsigned int persistent_mult, persistent_shift; |
Colin Cross | 9d7d6e3 | 2012-10-08 14:01:12 -0700 | [diff] [blame] | 53 | |
Xunlei Pang | a451570 | 2015-04-01 20:34:24 -0700 | [diff] [blame] | 54 | static void omap_read_persistent_clock64(struct timespec64 *ts) |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 55 | { |
| 56 | unsigned long long nsecs; |
Colin Cross | 9d7d6e3 | 2012-10-08 14:01:12 -0700 | [diff] [blame] | 57 | cycles_t last_cycles; |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 58 | |
| 59 | last_cycles = cycles; |
Victor Kamensky | f6f3b50 | 2014-04-15 20:37:48 +0300 | [diff] [blame] | 60 | cycles = sync32k_cnt_reg ? readl_relaxed(sync32k_cnt_reg) : 0; |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 61 | |
Colin Cross | 9d7d6e3 | 2012-10-08 14:01:12 -0700 | [diff] [blame] | 62 | nsecs = clocksource_cyc2ns(cycles - last_cycles, |
| 63 | persistent_mult, persistent_shift); |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 64 | |
Xunlei Pang | a451570 | 2015-04-01 20:34:24 -0700 | [diff] [blame] | 65 | timespec64_add_ns(&persistent_ts, nsecs); |
Colin Cross | 9d7d6e3 | 2012-10-08 14:01:12 -0700 | [diff] [blame] | 66 | |
| 67 | *ts = persistent_ts; |
Xunlei Pang | a451570 | 2015-04-01 20:34:24 -0700 | [diff] [blame] | 68 | } |
Colin Cross | 9d7d6e3 | 2012-10-08 14:01:12 -0700 | [diff] [blame] | 69 | |
Vaibhav Hiremath | 1fe97c8 | 2012-05-09 10:07:05 -0700 | [diff] [blame] | 70 | /** |
| 71 | * omap_init_clocksource_32k - setup and register counter 32k as a |
| 72 | * kernel clocksource |
| 73 | * @pbase: base addr of counter_32k module |
| 74 | * @size: size of counter_32k to map |
| 75 | * |
| 76 | * Returns 0 upon success or negative error code upon failure. |
| 77 | * |
| 78 | */ |
| 79 | int __init omap_init_clocksource_32k(void __iomem *vbase) |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 80 | { |
Vaibhav Hiremath | 1fe97c8 | 2012-05-09 10:07:05 -0700 | [diff] [blame] | 81 | int ret; |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 82 | |
Vaibhav Hiremath | 1fe97c8 | 2012-05-09 10:07:05 -0700 | [diff] [blame] | 83 | /* |
R Sricharan | b009366 | 2012-05-10 14:17:22 +0530 | [diff] [blame] | 84 | * 32k sync Counter IP register offsets vary between the |
| 85 | * highlander version and the legacy ones. |
| 86 | * The 'SCHEME' bits(30-31) of the revision register is used |
| 87 | * to identify the version. |
Vaibhav Hiremath | 1fe97c8 | 2012-05-09 10:07:05 -0700 | [diff] [blame] | 88 | */ |
Victor Kamensky | f6f3b50 | 2014-04-15 20:37:48 +0300 | [diff] [blame] | 89 | if (readl_relaxed(vbase + OMAP2_32KSYNCNT_REV_OFF) & |
R Sricharan | b009366 | 2012-05-10 14:17:22 +0530 | [diff] [blame] | 90 | OMAP2_32KSYNCNT_REV_SCHEME) |
| 91 | sync32k_cnt_reg = vbase + OMAP2_32KSYNCNT_CR_OFF_HIGH; |
| 92 | else |
| 93 | sync32k_cnt_reg = vbase + OMAP2_32KSYNCNT_CR_OFF_LOW; |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 94 | |
Vaibhav Hiremath | 1fe97c8 | 2012-05-09 10:07:05 -0700 | [diff] [blame] | 95 | /* |
| 96 | * 120000 rough estimate from the calculations in |
John Stultz | fba9e07 | 2015-03-11 21:16:40 -0700 | [diff] [blame] | 97 | * __clocksource_update_freq_scale. |
Vaibhav Hiremath | 1fe97c8 | 2012-05-09 10:07:05 -0700 | [diff] [blame] | 98 | */ |
| 99 | clocks_calc_mult_shift(&persistent_mult, &persistent_shift, |
| 100 | 32768, NSEC_PER_SEC, 120000); |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 101 | |
Vaibhav Hiremath | 1fe97c8 | 2012-05-09 10:07:05 -0700 | [diff] [blame] | 102 | ret = clocksource_mmio_init(sync32k_cnt_reg, "32k_counter", 32768, |
| 103 | 250, 32, clocksource_mmio_readl_up); |
| 104 | if (ret) { |
| 105 | pr_err("32k_counter: can't register clocksource\n"); |
| 106 | return ret; |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 107 | } |
Vaibhav Hiremath | 1fe97c8 | 2012-05-09 10:07:05 -0700 | [diff] [blame] | 108 | |
Stephen Boyd | 8f0678f | 2013-11-15 15:26:23 -0800 | [diff] [blame] | 109 | sched_clock_register(omap_32k_read_sched_clock, 32, 32768); |
Pavel Tatashin | 227e395 | 2018-07-19 16:55:37 -0400 | [diff] [blame] | 110 | register_persistent_clock(omap_read_persistent_clock64); |
Vaibhav Hiremath | 1fe97c8 | 2012-05-09 10:07:05 -0700 | [diff] [blame] | 111 | pr_info("OMAP clocksource: 32k_counter at 32768 Hz\n"); |
| 112 | |
Paul Walmsley | aa218da | 2010-10-08 11:40:19 -0600 | [diff] [blame] | 113 | return 0; |
| 114 | } |