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> 11c8a7ba9eSThomas Chou #include <errno.h> 12c8a7ba9eSThomas Chou #include <timer.h> 13c8a7ba9eSThomas Chou 14579eb5a0SBin Meng DECLARE_GLOBAL_DATA_PTR; 15579eb5a0SBin Meng 16c8a7ba9eSThomas Chou /* 17435ae76eSBin Meng * Implement a timer uclass to work with lib/time.c. The timer is usually 189ca07ebbSBin Meng * a 32/64 bits free-running up counter. The get_rate() method is used to get 19c8a7ba9eSThomas Chou * the input clock frequency of the timer. The get_count() method is used 209ca07ebbSBin Meng * to get the current 64 bits count value. If the hardware is counting down, 21c8a7ba9eSThomas Chou * the value should be inversed inside the method. There may be no real 22c8a7ba9eSThomas Chou * tick, and no timer interrupt. 23c8a7ba9eSThomas Chou */ 24c8a7ba9eSThomas Chou 259ca07ebbSBin Meng int timer_get_count(struct udevice *dev, u64 *count) 26c8a7ba9eSThomas Chou { 27c8a7ba9eSThomas Chou const struct timer_ops *ops = device_get_ops(dev); 28c8a7ba9eSThomas Chou 29c8a7ba9eSThomas Chou if (!ops->get_count) 30c8a7ba9eSThomas Chou return -ENOSYS; 31c8a7ba9eSThomas Chou 32c8a7ba9eSThomas Chou return ops->get_count(dev, count); 33c8a7ba9eSThomas Chou } 34c8a7ba9eSThomas Chou 35c8a7ba9eSThomas Chou unsigned long timer_get_rate(struct udevice *dev) 36c8a7ba9eSThomas Chou { 37c8a7ba9eSThomas Chou struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); 38c8a7ba9eSThomas Chou 39c8a7ba9eSThomas Chou return uc_priv->clock_rate; 40c8a7ba9eSThomas Chou } 41c8a7ba9eSThomas Chou 42579eb5a0SBin Meng static int timer_pre_probe(struct udevice *dev) 43579eb5a0SBin Meng { 44579eb5a0SBin Meng struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); 45579eb5a0SBin Meng 46579eb5a0SBin Meng uc_priv->clock_rate = fdtdec_get_int(gd->fdt_blob, dev->of_offset, 47579eb5a0SBin Meng "clock-frequency", 0); 48579eb5a0SBin Meng 49579eb5a0SBin Meng return 0; 50579eb5a0SBin Meng } 51579eb5a0SBin Meng 52*0a7edce0SStephen Warren static int timer_post_probe(struct udevice *dev) 53*0a7edce0SStephen Warren { 54*0a7edce0SStephen Warren struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); 55*0a7edce0SStephen Warren 56*0a7edce0SStephen Warren if (!uc_priv->clock_rate) 57*0a7edce0SStephen Warren return -EINVAL; 58*0a7edce0SStephen Warren 59*0a7edce0SStephen Warren return 0; 60*0a7edce0SStephen Warren } 61*0a7edce0SStephen Warren 629ca07ebbSBin Meng u64 timer_conv_64(u32 count) 639ca07ebbSBin Meng { 649ca07ebbSBin Meng /* increment tbh if tbl has rolled over */ 659ca07ebbSBin Meng if (count < gd->timebase_l) 669ca07ebbSBin Meng gd->timebase_h++; 679ca07ebbSBin Meng gd->timebase_l = count; 689ca07ebbSBin Meng return ((u64)gd->timebase_h << 32) | gd->timebase_l; 699ca07ebbSBin Meng } 709ca07ebbSBin Meng 71c8336975SMugunthan V N int notrace dm_timer_init(void) 72c8336975SMugunthan V N { 73c8336975SMugunthan V N const void *blob = gd->fdt_blob; 74c8336975SMugunthan V N struct udevice *dev = NULL; 75c8336975SMugunthan V N int node; 76c8336975SMugunthan V N int ret; 77c8336975SMugunthan V N 78c8336975SMugunthan V N if (gd->timer) 79c8336975SMugunthan V N return 0; 80c8336975SMugunthan V N 81c8336975SMugunthan V N /* Check for a chosen timer to be used for tick */ 82c8336975SMugunthan V N node = fdtdec_get_chosen_node(blob, "tick-timer"); 83c8336975SMugunthan V N if (node < 0) { 84c8336975SMugunthan V N /* No chosen timer, trying first available timer */ 85c8336975SMugunthan V N ret = uclass_first_device(UCLASS_TIMER, &dev); 86c8336975SMugunthan V N if (ret) 87c8336975SMugunthan V N return ret; 88c8336975SMugunthan V N if (!dev) 89c8336975SMugunthan V N return -ENODEV; 90c8336975SMugunthan V N } else { 91c8336975SMugunthan V N if (uclass_get_device_by_of_offset(UCLASS_TIMER, node, &dev)) { 92c8336975SMugunthan V N /* 93c8336975SMugunthan V N * If the timer is not marked to be bound before 94c8336975SMugunthan V N * relocation, bind it anyway. 95c8336975SMugunthan V N */ 96c8336975SMugunthan V N if (node > 0 && 97c8336975SMugunthan V N !lists_bind_fdt(gd->dm_root, blob, node, &dev)) { 98c8336975SMugunthan V N ret = device_probe(dev); 99c8336975SMugunthan V N if (ret) 100c8336975SMugunthan V N return ret; 101c8336975SMugunthan V N } 102c8336975SMugunthan V N } 103c8336975SMugunthan V N } 104c8336975SMugunthan V N 105c8336975SMugunthan V N if (dev) { 106c8336975SMugunthan V N gd->timer = dev; 107c8336975SMugunthan V N return 0; 108c8336975SMugunthan V N } 109c8336975SMugunthan V N 110c8336975SMugunthan V N return -ENODEV; 111c8336975SMugunthan V N } 112c8336975SMugunthan V N 113c8a7ba9eSThomas Chou UCLASS_DRIVER(timer) = { 114c8a7ba9eSThomas Chou .id = UCLASS_TIMER, 115c8a7ba9eSThomas Chou .name = "timer", 116579eb5a0SBin Meng .pre_probe = timer_pre_probe, 117a5d80113SMugunthan V N .flags = DM_UC_FLAG_SEQ_ALIAS, 118*0a7edce0SStephen Warren .post_probe = timer_post_probe, 119c8a7ba9eSThomas Chou .per_device_auto_alloc_size = sizeof(struct timer_dev_priv), 120c8a7ba9eSThomas Chou }; 121