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> 11a5acafb2SZakharov Vlad #include <clk.h> 12c8a7ba9eSThomas Chou #include <errno.h> 13c8a7ba9eSThomas Chou #include <timer.h> 14c8a7ba9eSThomas Chou 15579eb5a0SBin Meng DECLARE_GLOBAL_DATA_PTR; 16579eb5a0SBin Meng 17c8a7ba9eSThomas Chou /* 18435ae76eSBin Meng * Implement a timer uclass to work with lib/time.c. The timer is usually 199ca07ebbSBin Meng * a 32/64 bits free-running up counter. The get_rate() method is used to get 20c8a7ba9eSThomas Chou * the input clock frequency of the timer. The get_count() method is used 219ca07ebbSBin Meng * to get the current 64 bits count value. If the hardware is counting down, 22c8a7ba9eSThomas Chou * the value should be inversed inside the method. There may be no real 23c8a7ba9eSThomas Chou * tick, and no timer interrupt. 24c8a7ba9eSThomas Chou */ 25c8a7ba9eSThomas Chou 264f051824SSimon Glass int notrace timer_get_count(struct udevice *dev, u64 *count) 27c8a7ba9eSThomas Chou { 28c8a7ba9eSThomas Chou const struct timer_ops *ops = device_get_ops(dev); 29c8a7ba9eSThomas Chou 30c8a7ba9eSThomas Chou if (!ops->get_count) 31c8a7ba9eSThomas Chou return -ENOSYS; 32c8a7ba9eSThomas Chou 33c8a7ba9eSThomas Chou return ops->get_count(dev, count); 34c8a7ba9eSThomas Chou } 35c8a7ba9eSThomas Chou 364f051824SSimon Glass unsigned long notrace timer_get_rate(struct udevice *dev) 37c8a7ba9eSThomas Chou { 384f051824SSimon Glass struct timer_dev_priv *uc_priv = dev->uclass_priv; 39c8a7ba9eSThomas Chou 40c8a7ba9eSThomas Chou return uc_priv->clock_rate; 41c8a7ba9eSThomas Chou } 42c8a7ba9eSThomas Chou 43579eb5a0SBin Meng static int timer_pre_probe(struct udevice *dev) 44579eb5a0SBin Meng { 45*b1a16002SPhilipp Tomsich #if !CONFIG_IS_ENABLED(OF_PLATDATA) 46579eb5a0SBin Meng struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); 47a5acafb2SZakharov Vlad struct clk timer_clk; 48a5acafb2SZakharov Vlad int err; 49a5acafb2SZakharov Vlad ulong ret; 50579eb5a0SBin Meng 51a5acafb2SZakharov Vlad err = clk_get_by_index(dev, 0, &timer_clk); 52a5acafb2SZakharov Vlad if (!err) { 53a5acafb2SZakharov Vlad ret = clk_get_rate(&timer_clk); 54a5acafb2SZakharov Vlad if (IS_ERR_VALUE(ret)) 55a5acafb2SZakharov Vlad return ret; 56a5acafb2SZakharov Vlad uc_priv->clock_rate = ret; 57a5acafb2SZakharov Vlad } else 58a5acafb2SZakharov Vlad uc_priv->clock_rate = fdtdec_get_int(gd->fdt_blob, 59e160f7d4SSimon Glass dev_of_offset(dev), "clock-frequency", 0); 60*b1a16002SPhilipp Tomsich #endif 61579eb5a0SBin Meng 62579eb5a0SBin Meng return 0; 63579eb5a0SBin Meng } 64579eb5a0SBin Meng 650a7edce0SStephen Warren static int timer_post_probe(struct udevice *dev) 660a7edce0SStephen Warren { 670a7edce0SStephen Warren struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev); 680a7edce0SStephen Warren 690a7edce0SStephen Warren if (!uc_priv->clock_rate) 700a7edce0SStephen Warren return -EINVAL; 710a7edce0SStephen Warren 720a7edce0SStephen Warren return 0; 730a7edce0SStephen Warren } 740a7edce0SStephen Warren 759ca07ebbSBin Meng u64 timer_conv_64(u32 count) 769ca07ebbSBin Meng { 779ca07ebbSBin Meng /* increment tbh if tbl has rolled over */ 789ca07ebbSBin Meng if (count < gd->timebase_l) 799ca07ebbSBin Meng gd->timebase_h++; 809ca07ebbSBin Meng gd->timebase_l = count; 819ca07ebbSBin Meng return ((u64)gd->timebase_h << 32) | gd->timebase_l; 829ca07ebbSBin Meng } 839ca07ebbSBin Meng 84c8336975SMugunthan V N int notrace dm_timer_init(void) 85c8336975SMugunthan V N { 86*b1a16002SPhilipp Tomsich __maybe_unused const void *blob = gd->fdt_blob; 87c8336975SMugunthan V N struct udevice *dev = NULL; 88*b1a16002SPhilipp Tomsich int node = -ENOENT; 89c8336975SMugunthan V N int ret; 90c8336975SMugunthan V N 91c8336975SMugunthan V N if (gd->timer) 92c8336975SMugunthan V N return 0; 93c8336975SMugunthan V N 94*b1a16002SPhilipp Tomsich #if !CONFIG_IS_ENABLED(OF_PLATDATA) 95c8336975SMugunthan V N /* Check for a chosen timer to be used for tick */ 96c8336975SMugunthan V N node = fdtdec_get_chosen_node(blob, "tick-timer"); 97*b1a16002SPhilipp Tomsich #endif 98c8336975SMugunthan V N if (node < 0) { 99c8336975SMugunthan V N /* No chosen timer, trying first available timer */ 1003f603cbbSSimon Glass ret = uclass_first_device_err(UCLASS_TIMER, &dev); 101c8336975SMugunthan V N if (ret) 102c8336975SMugunthan V N return ret; 103c8336975SMugunthan V N } else { 104c8336975SMugunthan V N if (uclass_get_device_by_of_offset(UCLASS_TIMER, node, &dev)) { 105c8336975SMugunthan V N /* 106c8336975SMugunthan V N * If the timer is not marked to be bound before 107c8336975SMugunthan V N * relocation, bind it anyway. 108c8336975SMugunthan V N */ 109c8336975SMugunthan V N if (node > 0 && 110f5b5719cSSimon Glass !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node), 111f5b5719cSSimon Glass &dev)) { 112c8336975SMugunthan V N ret = device_probe(dev); 113c8336975SMugunthan V N if (ret) 114c8336975SMugunthan V N return ret; 115c8336975SMugunthan V N } 116c8336975SMugunthan V N } 117c8336975SMugunthan V N } 118c8336975SMugunthan V N 119c8336975SMugunthan V N if (dev) { 120c8336975SMugunthan V N gd->timer = dev; 121c8336975SMugunthan V N return 0; 122c8336975SMugunthan V N } 123c8336975SMugunthan V N 124c8336975SMugunthan V N return -ENODEV; 125c8336975SMugunthan V N } 126c8336975SMugunthan V N 127c8a7ba9eSThomas Chou UCLASS_DRIVER(timer) = { 128c8a7ba9eSThomas Chou .id = UCLASS_TIMER, 129c8a7ba9eSThomas Chou .name = "timer", 130579eb5a0SBin Meng .pre_probe = timer_pre_probe, 131a5d80113SMugunthan V N .flags = DM_UC_FLAG_SEQ_ALIAS, 1320a7edce0SStephen Warren .post_probe = timer_post_probe, 133c8a7ba9eSThomas Chou .per_device_auto_alloc_size = sizeof(struct timer_dev_priv), 134c8a7ba9eSThomas Chou }; 135