sandbox_timer.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #define SANDBOX_TIMER_RATE 1000000
  12. /* system timer offset in ms */
  13. static unsigned long sandbox_timer_offset;
  14. void sandbox_timer_add_offset(unsigned long offset)
  15. {
  16. sandbox_timer_offset += offset;
  17. }
  18. u64 notrace timer_early_get_count(void)
  19. {
  20. return os_get_nsec() / 1000 + sandbox_timer_offset * 1000;
  21. }
  22. unsigned long notrace timer_early_get_rate(void)
  23. {
  24. return SANDBOX_TIMER_RATE;
  25. }
  26. static notrace int sandbox_timer_get_count(struct udevice *dev, u64 *count)
  27. {
  28. *count = timer_early_get_count();
  29. return 0;
  30. }
  31. static int sandbox_timer_probe(struct udevice *dev)
  32. {
  33. struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  34. if (!uc_priv->clock_rate)
  35. uc_priv->clock_rate = SANDBOX_TIMER_RATE;
  36. return 0;
  37. }
  38. static const struct timer_ops sandbox_timer_ops = {
  39. .get_count = sandbox_timer_get_count,
  40. };
  41. static const struct udevice_id sandbox_timer_ids[] = {
  42. { .compatible = "sandbox,timer" },
  43. { }
  44. };
  45. U_BOOT_DRIVER(sandbox_timer) = {
  46. .name = "sandbox_timer",
  47. .id = UCLASS_TIMER,
  48. .of_match = sandbox_timer_ids,
  49. .probe = sandbox_timer_probe,
  50. .ops = &sandbox_timer_ops,
  51. .flags = DM_FLAG_PRE_RELOC,
  52. };
  53. /* This is here in case we don't have a device tree */
  54. U_BOOT_DEVICE(sandbox_timer_non_fdt) = {
  55. .name = "sandbox_timer",
  56. };