andes_plmt_timer.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2019, Rick Chen <rick@andestech.com>
  4. * Copyright (C) 2020, Sean Anderson <seanga2@gmail.com>
  5. *
  6. * U-Boot syscon driver for Andes's Platform Level Machine Timer (PLMT).
  7. * The PLMT block holds memory-mapped mtime register
  8. * associated with timer tick.
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <timer.h>
  13. #include <asm/io.h>
  14. #include <dm/device-internal.h>
  15. #include <linux/err.h>
  16. /* mtime register */
  17. #define MTIME_REG(base) ((ulong)(base))
  18. static u64 notrace andes_plmt_get_count(struct udevice *dev)
  19. {
  20. return readq((void __iomem *)MTIME_REG(dev_get_priv(dev)));
  21. }
  22. #if CONFIG_IS_ENABLED(RISCV_MMODE) && IS_ENABLED(CONFIG_TIMER_EARLY)
  23. /**
  24. * timer_early_get_rate() - Get the timer rate before driver model
  25. */
  26. unsigned long notrace timer_early_get_rate(void)
  27. {
  28. return RISCV_MMODE_TIMER_FREQ;
  29. }
  30. /**
  31. * timer_early_get_count() - Get the timer count before driver model
  32. *
  33. */
  34. u64 notrace timer_early_get_count(void)
  35. {
  36. return readq((void __iomem *)MTIME_REG(RISCV_MMODE_TIMERBASE));
  37. }
  38. #endif
  39. static const struct timer_ops andes_plmt_ops = {
  40. .get_count = andes_plmt_get_count,
  41. };
  42. static int andes_plmt_probe(struct udevice *dev)
  43. {
  44. dev_set_priv(dev, dev_read_addr_ptr(dev));
  45. if (!dev_get_priv(dev))
  46. return -EINVAL;
  47. return timer_timebase_fallback(dev);
  48. }
  49. static const struct udevice_id andes_plmt_ids[] = {
  50. { .compatible = "andestech,plmt0" },
  51. { }
  52. };
  53. U_BOOT_DRIVER(andes_plmt) = {
  54. .name = "andes_plmt",
  55. .id = UCLASS_TIMER,
  56. .of_match = andes_plmt_ids,
  57. .ops = &andes_plmt_ops,
  58. .probe = andes_plmt_probe,
  59. .flags = DM_FLAG_PRE_RELOC,
  60. };