xilinx-timer.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2022 Advanced Micro Devices, Inc
  4. * Michal Simek <michal.simek@amd.com>
  5. *
  6. * (C) Copyright 2007 Michal Simek
  7. * Michal SIMEK <monstr@monstr.eu>
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <timer.h>
  12. #include <regmap.h>
  13. #include <dm/device_compat.h>
  14. #define TIMER_ENABLE_ALL 0x400 /* ENALL */
  15. #define TIMER_PWM 0x200 /* PWMA0 */
  16. #define TIMER_INTERRUPT 0x100 /* T0INT */
  17. #define TIMER_ENABLE 0x080 /* ENT0 */
  18. #define TIMER_ENABLE_INTR 0x040 /* ENIT0 */
  19. #define TIMER_RESET 0x020 /* LOAD0 */
  20. #define TIMER_RELOAD 0x010 /* ARHT0 */
  21. #define TIMER_EXT_CAPTURE 0x008 /* CAPT0 */
  22. #define TIMER_EXT_COMPARE 0x004 /* GENT0 */
  23. #define TIMER_DOWN_COUNT 0x002 /* UDT0 */
  24. #define TIMER_CAPTURE_MODE 0x001 /* MDT0 */
  25. #define TIMER_CONTROL_OFFSET 0
  26. #define TIMER_LOADREG_OFFSET 4
  27. #define TIMER_COUNTER_OFFSET 8
  28. struct xilinx_timer_priv {
  29. struct regmap *regs;
  30. };
  31. static u64 xilinx_timer_get_count(struct udevice *dev)
  32. {
  33. struct xilinx_timer_priv *priv = dev_get_priv(dev);
  34. u32 value;
  35. regmap_read(priv->regs, TIMER_COUNTER_OFFSET, &value);
  36. return timer_conv_64(value);
  37. }
  38. static int xilinx_timer_probe(struct udevice *dev)
  39. {
  40. struct xilinx_timer_priv *priv = dev_get_priv(dev);
  41. int ret;
  42. /* uc_priv->clock_rate has already clock rate */
  43. ret = regmap_init_mem(dev_ofnode(dev), &priv->regs);
  44. if (ret) {
  45. dev_dbg(dev, "failed to get regbase of timer\n");
  46. return ret;
  47. }
  48. regmap_write(priv->regs, TIMER_LOADREG_OFFSET, 0);
  49. regmap_write(priv->regs, TIMER_CONTROL_OFFSET, TIMER_RESET);
  50. regmap_write(priv->regs, TIMER_CONTROL_OFFSET,
  51. TIMER_ENABLE | TIMER_RELOAD);
  52. return 0;
  53. }
  54. static const struct timer_ops xilinx_timer_ops = {
  55. .get_count = xilinx_timer_get_count,
  56. };
  57. static const struct udevice_id xilinx_timer_ids[] = {
  58. { .compatible = "xlnx,xps-timer-1.00.a" },
  59. {}
  60. };
  61. U_BOOT_DRIVER(xilinx_timer) = {
  62. .name = "xilinx_timer",
  63. .id = UCLASS_TIMER,
  64. .of_match = xilinx_timer_ids,
  65. .priv_auto = sizeof(struct xilinx_timer_priv),
  66. .probe = xilinx_timer_probe,
  67. .ops = &xilinx_timer_ops,
  68. };