1c8a7ba9eSThomas Chou /* 2c8a7ba9eSThomas Chou * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw> 3c8a7ba9eSThomas Chou * 4c8a7ba9eSThomas Chou * SPDX-License-Identifier: GPL-2.0+ 5c8a7ba9eSThomas Chou */ 6c8a7ba9eSThomas Chou 7c8a7ba9eSThomas Chou #include <common.h> 8c8a7ba9eSThomas Chou #include <dm.h> 9c8336975SMugunthan V N #include <dm/lists.h> 10c8336975SMugunthan V N #include <dm/device-internal.h> 11*e842c8b7SPhilipp Tomsich #include <dm/root.h> 12a5acafb2SZakharov Vlad #include <clk.h> 13c8a7ba9eSThomas Chou #include <errno.h> 14c8a7ba9eSThomas Chou #include <timer.h> 15c8a7ba9eSThomas Chou 16579eb5a0SBin Meng DECLARE_GLOBAL_DATA_PTR; 17579eb5a0SBin Meng 18c8a7ba9eSThomas Chou /* 19435ae76eSBin Meng * Implement a timer uclass to work with lib/time.c. The timer is usually 209ca07ebbSBin Meng * a 32/64 bits free-running up counter. The get_rate() method is used to get 21c8a7ba9eSThomas Chou * the input clock frequency of the timer. The get_count() method is used 229ca07ebbSBin Meng * to get the current 64 bits count value. If the hardware is counting down, 23c8a7ba9eSThomas Chou * the value should be inversed inside the method. There may be no real 24c8a7ba9eSThomas Chou * tick, and no timer interrupt. 25c8a7ba9eSThomas Chou */ 26c8a7ba9eSThomas Chou 274f051824SSimon Glass int notrace timer_get_count(struct udevice *dev, u64 *count) 28c8a7ba9eSThomas Chou { 29c8a7ba9eSThomas Chou const struct timer_ops *ops = device_get_ops(dev); 30c8a7ba9eSThomas Chou 31c8a7ba9eSThomas Chou if (!ops->get_count) 32c8a7ba9eSThomas Chou return -ENOSYS; 33c8a7ba9eSThomas Chou 34c8a7ba9eSThomas Chou return ops->get_count(dev, count); 35c8a7ba9eSThomas Chou } 36c8a7ba9eSThomas Chou 374f051824SSimon Glass unsigned long notrace timer_get_rate(struct udevice *dev) 38c8a7ba9eSThomas Chou { 394f051824SSimon Glass struct timer_dev_priv *uc_priv = dev->uclass_priv; 40c8a7ba9eSThomas Chou 41c8a7ba9eSThomas Chou return uc_priv->clock_rate; 42c8a7ba9eSThomas Chou } 43c8a7ba9eSThomas Chou 44579eb5a0SBin Meng static int timer_pre_probe(struct udevice *dev) 45579eb5a0SBin Meng { 46b1a16002SPhilipp Tomsich #if !CONFIG_IS_ENABLED(OF_PLATDATA) 47579eb5a0SBin Meng struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); 48a5acafb2SZakharov Vlad struct clk timer_clk; 49a5acafb2SZakharov Vlad int err; 50a5acafb2SZakharov Vlad ulong ret; 51579eb5a0SBin Meng 52a5acafb2SZakharov Vlad err = clk_get_by_index(dev, 0, &timer_clk); 53a5acafb2SZakharov Vlad if (!err) { 54a5acafb2SZakharov Vlad ret = clk_get_rate(&timer_clk); 55a5acafb2SZakharov Vlad if (IS_ERR_VALUE(ret)) 56a5acafb2SZakharov Vlad return ret; 57a5acafb2SZakharov Vlad uc_priv->clock_rate = ret; 58*e842c8b7SPhilipp Tomsich } else { 59*e842c8b7SPhilipp Tomsich uc_priv->clock_rate = 60*e842c8b7SPhilipp Tomsich dev_read_u32_default(dev, "clock-frequency", 0); 61*e842c8b7SPhilipp Tomsich } 62b1a16002SPhilipp Tomsich #endif 63579eb5a0SBin Meng 64579eb5a0SBin Meng return 0; 65579eb5a0SBin Meng } 66579eb5a0SBin Meng 670a7edce0SStephen Warren static int timer_post_probe(struct udevice *dev) 680a7edce0SStephen Warren { 690a7edce0SStephen Warren struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); 700a7edce0SStephen Warren 710a7edce0SStephen Warren if (!uc_priv->clock_rate) 720a7edce0SStephen Warren return -EINVAL; 730a7edce0SStephen Warren 740a7edce0SStephen Warren return 0; 750a7edce0SStephen Warren } 760a7edce0SStephen Warren 779ca07ebbSBin Meng u64 timer_conv_64(u32 count) 789ca07ebbSBin Meng { 799ca07ebbSBin Meng /* increment tbh if tbl has rolled over */ 809ca07ebbSBin Meng if (count < gd->timebase_l) 819ca07ebbSBin Meng gd->timebase_h++; 829ca07ebbSBin Meng gd->timebase_l = count; 839ca07ebbSBin Meng return ((u64)gd->timebase_h << 32) | gd->timebase_l; 849ca07ebbSBin Meng } 859ca07ebbSBin Meng 86c8336975SMugunthan V N int notrace dm_timer_init(void) 87c8336975SMugunthan V N { 88c8336975SMugunthan V N struct udevice *dev = NULL; 89*e842c8b7SPhilipp Tomsich __maybe_unused ofnode node; 90c8336975SMugunthan V N int ret; 91c8336975SMugunthan V N 92c8336975SMugunthan V N if (gd->timer) 93c8336975SMugunthan V N return 0; 94c8336975SMugunthan V N 95b1a16002SPhilipp Tomsich #if !CONFIG_IS_ENABLED(OF_PLATDATA) 96c8336975SMugunthan V N /* Check for a chosen timer to be used for tick */ 97*e842c8b7SPhilipp Tomsich node = ofnode_get_chosen_node("tick-timer"); 98*e842c8b7SPhilipp Tomsich 99*e842c8b7SPhilipp Tomsich if (ofnode_valid(node) && 100*e842c8b7SPhilipp Tomsich uclass_get_device_by_ofnode(UCLASS_TIMER, node, &dev)) { 101c8336975SMugunthan V N /* 102c8336975SMugunthan V N * If the timer is not marked to be bound before 103c8336975SMugunthan V N * relocation, bind it anyway. 104c8336975SMugunthan V N */ 105*e842c8b7SPhilipp Tomsich if (!lists_bind_fdt(dm_root(), node, &dev)) { 106c8336975SMugunthan V N ret = device_probe(dev); 107c8336975SMugunthan V N if (ret) 108c8336975SMugunthan V N return ret; 109c8336975SMugunthan V N } 110c8336975SMugunthan V N } 111*e842c8b7SPhilipp Tomsich #endif 112*e842c8b7SPhilipp Tomsich 113*e842c8b7SPhilipp Tomsich if (!dev) { 114*e842c8b7SPhilipp Tomsich /* Fall back to the first available timer */ 115*e842c8b7SPhilipp Tomsich ret = uclass_first_device_err(UCLASS_TIMER, &dev); 116*e842c8b7SPhilipp Tomsich if (ret) 117*e842c8b7SPhilipp Tomsich return ret; 118c8336975SMugunthan V N } 119c8336975SMugunthan V N 120c8336975SMugunthan V N if (dev) { 121c8336975SMugunthan V N gd->timer = dev; 122c8336975SMugunthan V N return 0; 123c8336975SMugunthan V N } 124c8336975SMugunthan V N 125c8336975SMugunthan V N return -ENODEV; 126c8336975SMugunthan V N } 127c8336975SMugunthan V N 128c8a7ba9eSThomas Chou UCLASS_DRIVER(timer) = { 129c8a7ba9eSThomas Chou .id = UCLASS_TIMER, 130c8a7ba9eSThomas Chou .name = "timer", 131579eb5a0SBin Meng .pre_probe = timer_pre_probe, 132a5d80113SMugunthan V N .flags = DM_UC_FLAG_SEQ_ALIAS, 1330a7edce0SStephen Warren .post_probe = timer_post_probe, 134c8a7ba9eSThomas Chou .per_device_auto_alloc_size = sizeof(struct timer_dev_priv), 135c8a7ba9eSThomas Chou }; 136