blob: d33a26e28fe1e045f2a8d570d0c7a88871dfb267 [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/**
12 * dm_timer_init() - initialize a timer for time keeping. On success
Mugunthan V Nc8336972016-01-16 21:33:58 +053013 * initializes gd->timer so that lib/timer can use it for future
14 * referrence.
15 *
Sean Andersonaff60ab2020-10-07 14:37:43 -040016 * Return: 0 on success or error number
Mugunthan V Nc8336972016-01-16 21:33:58 +053017 */
18int dm_timer_init(void);
19
Sean Anderson35761212020-09-28 10:52:22 -040020/**
21 * timer_timebase_fallback() - Helper for timers using timebase fallback
22 * @dev: A timer partially-probed timer device
23 *
24 * This is a helper function designed for timers which need to fall back on the
25 * cpu's timebase. This function is designed to be called during the driver's
26 * probe(). If there is a clocks or clock-frequency property in the timer's
27 * binding, then it will be used. Otherwise, the timebase of the current cpu
28 * will be used. This is initialized by the cpu driver, and usually gotten from
29 * ``/cpus/timebase-frequency`` or ``/cpus/cpu@X/timebase-frequency``.
30 *
31 * Return: 0 if OK, or negative error code on failure
32 */
33int timer_timebase_fallback(struct udevice *dev);
34
Sean Andersonaff60ab2020-10-07 14:37:43 -040035/**
36 * timer_conv_64() - convert 32-bit counter value to 64-bit
Bin Meng9ca07eb2015-11-24 13:31:17 -070037 * @count: 32-bit counter value
Sean Andersonaff60ab2020-10-07 14:37:43 -040038 *
39 * Return: 64-bit counter value
Bin Meng9ca07eb2015-11-24 13:31:17 -070040 */
41u64 timer_conv_64(u32 count);
42
Sean Andersonaff60ab2020-10-07 14:37:43 -040043/**
44 * timer_get_count() - Get the current timer count
Bin Meng435ae762015-11-13 00:11:14 -080045 * @dev: The timer device
Thomas Chouc8a7ba92015-10-09 13:46:34 +080046 * @count: pointer that returns the current timer count
Sean Andersonaff60ab2020-10-07 14:37:43 -040047 *
48 * Return: 0 if OK, -ve on error
Thomas Chouc8a7ba92015-10-09 13:46:34 +080049 */
Bin Meng9ca07eb2015-11-24 13:31:17 -070050int timer_get_count(struct udevice *dev, u64 *count);
Bin Meng435ae762015-11-13 00:11:14 -080051
Sean Andersonaff60ab2020-10-07 14:37:43 -040052/**
53 * timer_get_rate() - Get the timer input clock frequency
Bin Meng435ae762015-11-13 00:11:14 -080054 * @dev: The timer device
Sean Andersonaff60ab2020-10-07 14:37:43 -040055 *
56 * Return: the timer input clock frequency
Thomas Chouc8a7ba92015-10-09 13:46:34 +080057 */
58unsigned long timer_get_rate(struct udevice *dev);
59
Sean Andersonaff60ab2020-10-07 14:37:43 -040060/**
Bin Meng435ae762015-11-13 00:11:14 -080061 * struct timer_ops - Driver model timer operations
Thomas Chouc8a7ba92015-10-09 13:46:34 +080062 *
Bin Meng435ae762015-11-13 00:11:14 -080063 * The uclass interface is implemented by all timer devices which use
Thomas Chouc8a7ba92015-10-09 13:46:34 +080064 * driver model.
65 */
66struct timer_ops {
Sean Andersonaff60ab2020-10-07 14:37:43 -040067 /**
68 * @get_count: Get the current timer count
Thomas Chouc8a7ba92015-10-09 13:46:34 +080069 *
Bin Meng435ae762015-11-13 00:11:14 -080070 * @dev: The timer device
Sean Andersonaff60ab2020-10-07 14:37:43 -040071 *
Sean Anderson8af7bb92020-10-07 14:37:44 -040072 * This function may be called at any time after the driver is probed.
73 * All necessary initialization must be completed by the time probe()
74 * returns. The count returned by this functions should be monotonic.
75 * This function must succeed.
Sean Andersonaff60ab2020-10-07 14:37:43 -040076 *
Sean Anderson8af7bb92020-10-07 14:37:44 -040077 * Return: The current 64-bit timer count
Thomas Chouc8a7ba92015-10-09 13:46:34 +080078 */
Sean Anderson8af7bb92020-10-07 14:37:44 -040079 u64 (*get_count)(struct udevice *dev);
Thomas Chouc8a7ba92015-10-09 13:46:34 +080080};
81
Sean Andersonaff60ab2020-10-07 14:37:43 -040082/**
Thomas Chouc8a7ba92015-10-09 13:46:34 +080083 * struct timer_dev_priv - information about a device used by the uclass
84 *
85 * @clock_rate: the timer input clock frequency
86 */
87struct timer_dev_priv {
88 unsigned long clock_rate;
89};
90
Simon Glassc95fec32016-02-24 09:14:49 -070091/**
92 * timer_early_get_count() - Implement timer_get_count() before driver model
93 *
Sean Andersonaff60ab2020-10-07 14:37:43 -040094 * If ``CONFIG_TIMER_EARLY`` is enabled, this function wil be called to return
Simon Glassc95fec32016-02-24 09:14:49 -070095 * the current timer value before the proper driver model timer is ready.
96 * It should be implemented by one of the timer values. This is mostly useful
97 * for tracing.
98 */
99u64 timer_early_get_count(void);
100
101/**
102 * timer_early_get_rate() - Get the timer rate before driver model
103 *
Sean Andersonaff60ab2020-10-07 14:37:43 -0400104 * If ``CONFIG_TIMER_EARLY`` is enabled, this function wil be called to return
Simon Glassc95fec32016-02-24 09:14:49 -0700105 * the current timer rate in Hz before the proper driver model timer is ready.
106 * It should be implemented by one of the timer values. This is mostly useful
107 * for tracing. This corresponds to the clock_rate value in struct
108 * timer_dev_priv.
109 */
110unsigned long timer_early_get_rate(void);
111
Thomas Chouc8a7ba92015-10-09 13:46:34 +0800112#endif /* _TIMER_H_ */