sandbox_timer.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <timer.h>
  10. #include <os.h>
  11. /* system timer offset in ms */
  12. static unsigned long sandbox_timer_offset;
  13. void sandbox_timer_add_offset(unsigned long offset)
  14. {
  15. sandbox_timer_offset += offset;
  16. }
  17. static int sandbox_timer_get_count(struct udevice *dev, u64 *count)
  18. {
  19. *count = os_get_nsec() / 1000 + sandbox_timer_offset * 1000;
  20. return 0;
  21. }
  22. static int sandbox_timer_probe(struct udevice *dev)
  23. {
  24. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  25. if (!uc_priv->clock_rate)
  26. uc_priv->clock_rate = 1000000;
  27. return 0;
  28. }
  29. static const struct timer_ops sandbox_timer_ops = {
  30. .get_count = sandbox_timer_get_count,
  31. };
  32. static const struct udevice_id sandbox_timer_ids[] = {
  33. { .compatible = "sandbox,timer" },
  34. { }
  35. };
  36. U_BOOT_DRIVER(sandbox_timer) = {
  37. .name = "sandbox_timer",
  38. .id = UCLASS_TIMER,
  39. .of_match = sandbox_timer_ids,
  40. .probe = sandbox_timer_probe,
  41. .ops = &sandbox_timer_ops,
  42. .flags = DM_FLAG_PRE_RELOC,
  43. };
  44. /* This is here in case we don't have a device tree */
  45. U_BOOT_DEVICE(sandbox_timer_non_fdt) = {
  46. .name = "sandbox_timer",
  47. };