1/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
4 */
5
6#ifndef _TIMER_H_
7#define _TIMER_H_
8
9#define timer_get_ops(dev)	((struct timer_ops *)(dev)->driver->ops)
10
11/**
12 * 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
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
22 */
23int dm_timer_init(void);
24
25/**
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
40/**
41 * timer_conv_64() - convert 32-bit counter value to 64-bit
42 * @count: 32-bit counter value
43 *
44 * Return: 64-bit counter value
45 */
46u64 timer_conv_64(u32 count);
47
48/**
49 * timer_get_count() - Get the current timer count
50 * @dev: The timer device
51 * @count: pointer that returns the current timer count
52 *
53 * Return: 0 if OK, -ve on error
54 */
55int timer_get_count(struct udevice *dev, u64 *count);
56
57/**
58 * timer_get_rate() - Get the timer input clock frequency in Hz
59 * @dev: The timer device
60 *
61 * Return: the timer input clock frequency in Hz
62 */
63unsigned long timer_get_rate(struct udevice *dev);
64
65/**
66 * struct timer_ops - Driver model timer operations
67 *
68 * The uclass interface is implemented by all timer devices which use
69 * driver model.
70 */
71struct timer_ops {
72	/**
73	 * @get_count: Get the current timer count
74	 *
75	 * @dev: The timer device
76	 *
77	 * 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.
81	 *
82	 * Return: The current 64-bit timer count
83	 */
84	u64 (*get_count)(struct udevice *dev);
85};
86
87/**
88 * struct timer_dev_priv - information about a device used by the uclass
89 *
90 * @clock_rate: the timer input clock frequency in Hz
91 */
92struct timer_dev_priv {
93	unsigned long clock_rate;
94};
95
96/**
97 * timer_early_get_count() - Implement timer_get_count() before driver model
98 *
99 * If ``CONFIG_TIMER_EARLY`` is enabled, this function wil be called to return
100 * 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 *
109 * If ``CONFIG_TIMER_EARLY`` is enabled, this function wil be called to return
110 * 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
117#endif	/* _TIMER_H_ */
118