blob: 311ce6b2c3adc3bc0375d8219133fd1a81822036 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Thomas Chouc8a7ba92015-10-09 13:46:34 +08002/*
3 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
Thomas Chouc8a7ba92015-10-09 13:46:34 +08004 */
5
6#ifndef _TIMER_H_
7#define _TIMER_H_
8
Ovidiu Panait1e766a02022-10-12 08:36:54 +03009#define timer_get_ops(dev) ((struct timer_ops *)(dev)->driver->ops)
10
Sean Andersonaff60ab2020-10-07 14:37:43 -040011/**
Simon Glass4aa50532023-01-15 14:15:42 -070012 * dm_timer_init() - set up a timer for time keeping
Mugunthan V Nc8336972016-01-16 21:33:58 +053013 *
Simon Glass4aa50532023-01-15 14:15:42 -070014 * 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
18 *
19 * 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 Nc8336972016-01-16 21:33:58 +053022 */
23int dm_timer_init(void);
24
Sean Anderson35761212020-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 Andersonaff60ab2020-10-07 14:37:43 -040040/**
41 * timer_conv_64() - convert 32-bit counter value to 64-bit
Bin Meng9ca07eb2015-11-24 13:31:17 -070042 * @count: 32-bit counter value
Sean Andersonaff60ab2020-10-07 14:37:43 -040043 *
44 * Return: 64-bit counter value
Bin Meng9ca07eb2015-11-24 13:31:17 -070045 */
46u64 timer_conv_64(u32 count);
47
Sean Andersonaff60ab2020-10-07 14:37:43 -040048/**
49 * timer_get_count() - Get the current timer count
Bin Meng435ae762015-11-13 00:11:14 -080050 * @dev: The timer device
Thomas Chouc8a7ba92015-10-09 13:46:34 +080051 * @count: pointer that returns the current timer count
Sean Andersonaff60ab2020-10-07 14:37:43 -040052 *
53 * Return: 0 if OK, -ve on error
Thomas Chouc8a7ba92015-10-09 13:46:34 +080054 */
Bin Meng9ca07eb2015-11-24 13:31:17 -070055int timer_get_count(struct udevice *dev, u64 *count);
Bin Meng435ae762015-11-13 00:11:14 -080056
Sean Andersonaff60ab2020-10-07 14:37:43 -040057/**
58 * timer_get_rate() - Get the timer input clock frequency
Bin Meng435ae762015-11-13 00:11:14 -080059 * @dev: The timer device
Sean Andersonaff60ab2020-10-07 14:37:43 -040060 *
61 * Return: the timer input clock frequency
Thomas Chouc8a7ba92015-10-09 13:46:34 +080062 */
63unsigned long timer_get_rate(struct udevice *dev);
64
Sean Andersonaff60ab2020-10-07 14:37:43 -040065/**
Bin Meng435ae762015-11-13 00:11:14 -080066 * struct timer_ops - Driver model timer operations
Thomas Chouc8a7ba92015-10-09 13:46:34 +080067 *
Bin Meng435ae762015-11-13 00:11:14 -080068 * The uclass interface is implemented by all timer devices which use
Thomas Chouc8a7ba92015-10-09 13:46:34 +080069 * driver model.
70 */
71struct timer_ops {
Sean Andersonaff60ab2020-10-07 14:37:43 -040072 /**
73 * @get_count: Get the current timer count
Thomas Chouc8a7ba92015-10-09 13:46:34 +080074 *
Bin Meng435ae762015-11-13 00:11:14 -080075 * @dev: The timer device
Sean Andersonaff60ab2020-10-07 14:37:43 -040076 *
Sean Anderson8af7bb92020-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 Andersonaff60ab2020-10-07 14:37:43 -040081 *
Sean Anderson8af7bb92020-10-07 14:37:44 -040082 * Return: The current 64-bit timer count
Thomas Chouc8a7ba92015-10-09 13:46:34 +080083 */
Sean Anderson8af7bb92020-10-07 14:37:44 -040084 u64 (*get_count)(struct udevice *dev);
Thomas Chouc8a7ba92015-10-09 13:46:34 +080085};
86
Sean Andersonaff60ab2020-10-07 14:37:43 -040087/**
Thomas Chouc8a7ba92015-10-09 13:46:34 +080088 * struct timer_dev_priv - information about a device used by the uclass
89 *
90 * @clock_rate: the timer input clock frequency
91 */
92struct timer_dev_priv {
93 unsigned long clock_rate;
94};
95
Simon Glassc95fec32016-02-24 09:14:49 -070096/**
97 * timer_early_get_count() - Implement timer_get_count() before driver model
98 *
Sean Andersonaff60ab2020-10-07 14:37:43 -040099 * If ``CONFIG_TIMER_EARLY`` is enabled, this function wil be called to return
Simon Glassc95fec32016-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 Andersonaff60ab2020-10-07 14:37:43 -0400109 * If ``CONFIG_TIMER_EARLY`` is enabled, this function wil be called to return
Simon Glassc95fec32016-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 Chouc8a7ba92015-10-09 13:46:34 +0800117#endif /* _TIMER_H_ */