Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw> |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef _TIMER_H_ |
| 7 | #define _TIMER_H_ |
| 8 | |
Ovidiu Panait | 1e766a0 | 2022-10-12 08:36:54 +0300 | [diff] [blame] | 9 | #define timer_get_ops(dev) ((struct timer_ops *)(dev)->driver->ops) |
| 10 | |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 11 | /** |
Simon Glass | 4aa5053 | 2023-01-15 14:15:42 -0700 | [diff] [blame] | 12 | * dm_timer_init() - set up a timer for time keeping |
Mugunthan V N | c833697 | 2016-01-16 21:33:58 +0530 | [diff] [blame] | 13 | * |
Simon Glass | 4aa5053 | 2023-01-15 14:15:42 -0700 | [diff] [blame] | 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 |
Mugunthan V N | c833697 | 2016-01-16 21:33:58 +0530 | [diff] [blame] | 22 | */ |
| 23 | int dm_timer_init(void); |
| 24 | |
Sean Anderson | 3576121 | 2020-09-28 10:52:22 -0400 | [diff] [blame] | 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 | */ |
| 38 | int timer_timebase_fallback(struct udevice *dev); |
| 39 | |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 40 | /** |
| 41 | * timer_conv_64() - convert 32-bit counter value to 64-bit |
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 42 | * @count: 32-bit counter value |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 43 | * |
| 44 | * Return: 64-bit counter value |
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 45 | */ |
| 46 | u64 timer_conv_64(u32 count); |
| 47 | |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 48 | /** |
| 49 | * timer_get_count() - Get the current timer count |
Bin Meng | 435ae76 | 2015-11-13 00:11:14 -0800 | [diff] [blame] | 50 | * @dev: The timer device |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 51 | * @count: pointer that returns the current timer count |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 52 | * |
| 53 | * Return: 0 if OK, -ve on error |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 54 | */ |
Bin Meng | 9ca07eb | 2015-11-24 13:31:17 -0700 | [diff] [blame] | 55 | int timer_get_count(struct udevice *dev, u64 *count); |
Bin Meng | 435ae76 | 2015-11-13 00:11:14 -0800 | [diff] [blame] | 56 | |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 57 | /** |
Heinrich Schuchardt | c00b289 | 2023-09-07 09:54:42 +0200 | [diff] [blame] | 58 | * timer_get_rate() - Get the timer input clock frequency in Hz |
Bin Meng | 435ae76 | 2015-11-13 00:11:14 -0800 | [diff] [blame] | 59 | * @dev: The timer device |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 60 | * |
Heinrich Schuchardt | c00b289 | 2023-09-07 09:54:42 +0200 | [diff] [blame] | 61 | * Return: the timer input clock frequency in Hz |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 62 | */ |
| 63 | unsigned long timer_get_rate(struct udevice *dev); |
| 64 | |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 65 | /** |
Bin Meng | 435ae76 | 2015-11-13 00:11:14 -0800 | [diff] [blame] | 66 | * struct timer_ops - Driver model timer operations |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 67 | * |
Bin Meng | 435ae76 | 2015-11-13 00:11:14 -0800 | [diff] [blame] | 68 | * The uclass interface is implemented by all timer devices which use |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 69 | * driver model. |
| 70 | */ |
| 71 | struct timer_ops { |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 72 | /** |
| 73 | * @get_count: Get the current timer count |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 74 | * |
Bin Meng | 435ae76 | 2015-11-13 00:11:14 -0800 | [diff] [blame] | 75 | * @dev: The timer device |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 76 | * |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 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. |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 81 | * |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 82 | * Return: The current 64-bit timer count |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 83 | */ |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 84 | u64 (*get_count)(struct udevice *dev); |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 85 | }; |
| 86 | |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 87 | /** |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 88 | * struct timer_dev_priv - information about a device used by the uclass |
| 89 | * |
Heinrich Schuchardt | c00b289 | 2023-09-07 09:54:42 +0200 | [diff] [blame] | 90 | * @clock_rate: the timer input clock frequency in Hz |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 91 | */ |
| 92 | struct timer_dev_priv { |
| 93 | unsigned long clock_rate; |
| 94 | }; |
| 95 | |
Simon Glass | c95fec3 | 2016-02-24 09:14:49 -0700 | [diff] [blame] | 96 | /** |
| 97 | * timer_early_get_count() - Implement timer_get_count() before driver model |
| 98 | * |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 99 | * If ``CONFIG_TIMER_EARLY`` is enabled, this function wil be called to return |
Simon Glass | c95fec3 | 2016-02-24 09:14:49 -0700 | [diff] [blame] | 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 | */ |
| 104 | u64 timer_early_get_count(void); |
| 105 | |
| 106 | /** |
| 107 | * timer_early_get_rate() - Get the timer rate before driver model |
| 108 | * |
Sean Anderson | aff60ab | 2020-10-07 14:37:43 -0400 | [diff] [blame] | 109 | * If ``CONFIG_TIMER_EARLY`` is enabled, this function wil be called to return |
Simon Glass | c95fec3 | 2016-02-24 09:14:49 -0700 | [diff] [blame] | 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 | */ |
| 115 | unsigned long timer_early_get_rate(void); |
| 116 | |
Thomas Chou | c8a7ba9 | 2015-10-09 13:46:34 +0800 | [diff] [blame] | 117 | #endif /* _TIMER_H_ */ |