blob: 091d8dfa2287a3259069f3b32d5bf1174fcfcfc1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Thomas Choufb798b12015-10-09 13:46:34 +08002/*
3 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
Thomas Choufb798b12015-10-09 13:46:34 +08004 */
5
6#ifndef _TIMER_H_
7#define _TIMER_H_
8
Ovidiu Panait49e17df2022-10-12 08:36:54 +03009#define timer_get_ops(dev) ((struct timer_ops *)(dev)->driver->ops)
10
Sean Andersonb7109652020-10-07 14:37:43 -040011/**
Simon Glass7c1974b2023-01-15 14:15:42 -070012 * dm_timer_init() - set up a timer for time keeping
13 *
14 * Sets up gd->timer if the device is not already bound, making sure it is
15 * probed and ready for use
16 *
17 * On success, inits gd->timer so that lib/timer can use it for future reference
Mugunthan V N6f89d042016-01-16 21:33:58 +053018 *
Simon Glass7c1974b2023-01-15 14:15:42 -070019 * Returns: 0 on success, -EAGAIN if driver model is not ready yet, -ENODEV if
20 * no timer could be found, other error if the timer could not be bound or
21 * probed
Mugunthan V N6f89d042016-01-16 21:33:58 +053022 */
23int dm_timer_init(void);
24
Sean Anderson738ff532020-09-28 10:52:22 -040025/**
26 * timer_timebase_fallback() - Helper for timers using timebase fallback
27 * @dev: A timer partially-probed timer device
28 *
29 * This is a helper function designed for timers which need to fall back on the
30 * cpu's timebase. This function is designed to be called during the driver's
31 * probe(). If there is a clocks or clock-frequency property in the timer's
32 * binding, then it will be used. Otherwise, the timebase of the current cpu
33 * will be used. This is initialized by the cpu driver, and usually gotten from
34 * ``/cpus/timebase-frequency`` or ``/cpus/cpu@X/timebase-frequency``.
35 *
36 * Return: 0 if OK, or negative error code on failure
37 */
38int timer_timebase_fallback(struct udevice *dev);
39
Sean Andersonb7109652020-10-07 14:37:43 -040040/**
41 * timer_conv_64() - convert 32-bit counter value to 64-bit
Bin Mengab841b62015-11-24 13:31:17 -070042 * @count: 32-bit counter value
Sean Andersonb7109652020-10-07 14:37:43 -040043 *
44 * Return: 64-bit counter value
Bin Mengab841b62015-11-24 13:31:17 -070045 */
46u64 timer_conv_64(u32 count);
47
Sean Andersonb7109652020-10-07 14:37:43 -040048/**
49 * timer_get_count() - Get the current timer count
Bin Meng8a7b8642015-11-13 00:11:14 -080050 * @dev: The timer device
Thomas Choufb798b12015-10-09 13:46:34 +080051 * @count: pointer that returns the current timer count
Sean Andersonb7109652020-10-07 14:37:43 -040052 *
53 * Return: 0 if OK, -ve on error
Thomas Choufb798b12015-10-09 13:46:34 +080054 */
Bin Mengab841b62015-11-24 13:31:17 -070055int timer_get_count(struct udevice *dev, u64 *count);
Bin Meng8a7b8642015-11-13 00:11:14 -080056
Sean Andersonb7109652020-10-07 14:37:43 -040057/**
Heinrich Schuchardt9a047562023-09-07 09:54:42 +020058 * timer_get_rate() - Get the timer input clock frequency in Hz
Bin Meng8a7b8642015-11-13 00:11:14 -080059 * @dev: The timer device
Sean Andersonb7109652020-10-07 14:37:43 -040060 *
Heinrich Schuchardt9a047562023-09-07 09:54:42 +020061 * Return: the timer input clock frequency in Hz
Thomas Choufb798b12015-10-09 13:46:34 +080062 */
63unsigned long timer_get_rate(struct udevice *dev);
64
Sean Andersonb7109652020-10-07 14:37:43 -040065/**
Bin Meng8a7b8642015-11-13 00:11:14 -080066 * struct timer_ops - Driver model timer operations
Thomas Choufb798b12015-10-09 13:46:34 +080067 *
Bin Meng8a7b8642015-11-13 00:11:14 -080068 * The uclass interface is implemented by all timer devices which use
Thomas Choufb798b12015-10-09 13:46:34 +080069 * driver model.
70 */
71struct timer_ops {
Sean Andersonb7109652020-10-07 14:37:43 -040072 /**
73 * @get_count: Get the current timer count
Thomas Choufb798b12015-10-09 13:46:34 +080074 *
Bin Meng8a7b8642015-11-13 00:11:14 -080075 * @dev: The timer device
Sean Andersonb7109652020-10-07 14:37:43 -040076 *
Sean Anderson947fc2d2020-10-07 14:37:44 -040077 * This function may be called at any time after the driver is probed.
78 * All necessary initialization must be completed by the time probe()
79 * returns. The count returned by this functions should be monotonic.
80 * This function must succeed.
Sean Andersonb7109652020-10-07 14:37:43 -040081 *
Sean Anderson947fc2d2020-10-07 14:37:44 -040082 * Return: The current 64-bit timer count
Thomas Choufb798b12015-10-09 13:46:34 +080083 */
Sean Anderson947fc2d2020-10-07 14:37:44 -040084 u64 (*get_count)(struct udevice *dev);
Thomas Choufb798b12015-10-09 13:46:34 +080085};
86
Sean Andersonb7109652020-10-07 14:37:43 -040087/**
Thomas Choufb798b12015-10-09 13:46:34 +080088 * struct timer_dev_priv - information about a device used by the uclass
89 *
Heinrich Schuchardt9a047562023-09-07 09:54:42 +020090 * @clock_rate: the timer input clock frequency in Hz
Thomas Choufb798b12015-10-09 13:46:34 +080091 */
92struct timer_dev_priv {
93 unsigned long clock_rate;
94};
95
Simon Glass32f6c172016-02-24 09:14:49 -070096/**
97 * timer_early_get_count() - Implement timer_get_count() before driver model
98 *
Sean Andersonb7109652020-10-07 14:37:43 -040099 * If ``CONFIG_TIMER_EARLY`` is enabled, this function wil be called to return
Simon Glass32f6c172016-02-24 09:14:49 -0700100 * the current timer value before the proper driver model timer is ready.
101 * It should be implemented by one of the timer values. This is mostly useful
102 * for tracing.
103 */
104u64 timer_early_get_count(void);
105
106/**
107 * timer_early_get_rate() - Get the timer rate before driver model
108 *
Sean Andersonb7109652020-10-07 14:37:43 -0400109 * If ``CONFIG_TIMER_EARLY`` is enabled, this function wil be called to return
Simon Glass32f6c172016-02-24 09:14:49 -0700110 * the current timer rate in Hz before the proper driver model timer is ready.
111 * It should be implemented by one of the timer values. This is mostly useful
112 * for tracing. This corresponds to the clock_rate value in struct
113 * timer_dev_priv.
114 */
115unsigned long timer_early_get_rate(void);
116
Thomas Choufb798b12015-10-09 13:46:34 +0800117#endif /* _TIMER_H_ */