1 /*
2 * TimeSync API driver.
3 *
4 * Copyright 2016 Google Inc.
5 * Copyright 2016 Linaro Ltd.
6 *
7 * Released under the GPLv2 only.
8 *
9 * This code reads directly from an ARMv7 memory-mapped timer that lives in
10 * MMIO space. Since this counter lives inside of MMIO space its shared between
11 * cores and that means we don't have to worry about issues like TSC on x86
12 * where each time-stamp-counter (TSC) is local to a particular core.
13 *
14 * Register-level access code is based on
15 * drivers/clocksource/arm_arch_timer.c
16 */
17 #include <linux/cpufreq.h>
18 #include <linux/of_platform.h>
19
20 #include "greybus.h"
21 #include "arche_platform.h"
22
23 #define DEFAULT_FRAMETIME_CLOCK_HZ 19200000
24
25 static u32 gb_timesync_clock_frequency;
26 int (*arche_platform_change_state_cb)(enum arche_platform_state state,
27 struct gb_timesync_svc *pdata);
28 EXPORT_SYMBOL_GPL(arche_platform_change_state_cb);
29
gb_timesync_platform_get_counter(void)30 u64 gb_timesync_platform_get_counter(void)
31 {
32 return (u64)get_cycles();
33 }
34
gb_timesync_platform_get_clock_rate(void)35 u32 gb_timesync_platform_get_clock_rate(void)
36 {
37 if (unlikely(!gb_timesync_clock_frequency)) {
38 gb_timesync_clock_frequency = cpufreq_get(0);
39 if (!gb_timesync_clock_frequency)
40 gb_timesync_clock_frequency = DEFAULT_FRAMETIME_CLOCK_HZ;
41 }
42
43 return gb_timesync_clock_frequency;
44 }
45
gb_timesync_platform_lock_bus(struct gb_timesync_svc * pdata)46 int gb_timesync_platform_lock_bus(struct gb_timesync_svc *pdata)
47 {
48 if (!arche_platform_change_state_cb)
49 return 0;
50
51 return arche_platform_change_state_cb(ARCHE_PLATFORM_STATE_TIME_SYNC,
52 pdata);
53 }
54
gb_timesync_platform_unlock_bus(void)55 void gb_timesync_platform_unlock_bus(void)
56 {
57 if (!arche_platform_change_state_cb)
58 return;
59
60 arche_platform_change_state_cb(ARCHE_PLATFORM_STATE_ACTIVE, NULL);
61 }
62
63 static const struct of_device_id arch_timer_of_match[] = {
64 { .compatible = "google,greybus-frame-time-counter", },
65 {},
66 };
67
gb_timesync_platform_init(void)68 int __init gb_timesync_platform_init(void)
69 {
70 struct device_node *np;
71
72 np = of_find_matching_node(NULL, arch_timer_of_match);
73 if (!np) {
74 /* Tolerate not finding to allow BBB etc to continue */
75 pr_warn("Unable to find a compatible ARMv7 timer\n");
76 return 0;
77 }
78
79 if (of_property_read_u32(np, "clock-frequency",
80 &gb_timesync_clock_frequency)) {
81 pr_err("Unable to find timer clock-frequency\n");
82 return -ENODEV;
83 }
84
85 return 0;
86 }
87
gb_timesync_platform_exit(void)88 void gb_timesync_platform_exit(void) {}
89