timer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Weidmüller Interface GmbH & Co. KG
  4. * Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
  5. *
  6. * Copyright (C) 2012 Michal Simek <monstr@monstr.eu>
  7. * Copyright (C) 2011-2017 Xilinx, Inc. All rights reserved.
  8. *
  9. * (C) Copyright 2008
  10. * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de>
  11. *
  12. * (C) Copyright 2004
  13. * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
  14. *
  15. * (C) Copyright 2002-2004
  16. * Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
  17. *
  18. * (C) Copyright 2003
  19. * Texas Instruments <www.ti.com>
  20. *
  21. * (C) Copyright 2002
  22. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  23. * Marius Groeger <mgroeger@sysgo.de>
  24. *
  25. * (C) Copyright 2002
  26. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  27. * Alex Zuepke <azu@sysgo.de>
  28. */
  29. #include <clk.h>
  30. #include <common.h>
  31. #include <div64.h>
  32. #include <dm.h>
  33. #include <time.h>
  34. #include <malloc.h>
  35. #include <asm/io.h>
  36. #include <asm/arch/hardware.h>
  37. #include <asm/arch/clk.h>
  38. DECLARE_GLOBAL_DATA_PTR;
  39. struct scu_timer {
  40. u32 load; /* Timer Load Register */
  41. u32 counter; /* Timer Counter Register */
  42. u32 control; /* Timer Control Register */
  43. };
  44. static struct scu_timer *timer_base =
  45. (struct scu_timer *)ZYNQ_SCUTIMER_BASEADDR;
  46. #define SCUTIMER_CONTROL_PRESCALER_MASK 0x0000FF00 /* Prescaler */
  47. #define SCUTIMER_CONTROL_PRESCALER_SHIFT 8
  48. #define SCUTIMER_CONTROL_AUTO_RELOAD_MASK 0x00000002 /* Auto-reload */
  49. #define SCUTIMER_CONTROL_ENABLE_MASK 0x00000001 /* Timer enable */
  50. #define TIMER_LOAD_VAL 0xFFFFFFFF
  51. #define TIMER_PRESCALE 255
  52. int timer_init(void)
  53. {
  54. const u32 emask = SCUTIMER_CONTROL_AUTO_RELOAD_MASK |
  55. (TIMER_PRESCALE << SCUTIMER_CONTROL_PRESCALER_SHIFT) |
  56. SCUTIMER_CONTROL_ENABLE_MASK;
  57. struct udevice *dev;
  58. struct clk clk;
  59. int ret;
  60. ret = uclass_get_device_by_driver(UCLASS_CLK,
  61. DM_GET_DRIVER(zynq_clk), &dev);
  62. if (ret)
  63. return ret;
  64. clk.id = cpu_6or4x_clk;
  65. ret = clk_request(dev, &clk);
  66. if (ret < 0)
  67. return ret;
  68. gd->cpu_clk = clk_get_rate(&clk);
  69. clk_free(&clk);
  70. gd->arch.timer_rate_hz = (gd->cpu_clk / 2) / (TIMER_PRESCALE + 1);
  71. /* Load the timer counter register */
  72. writel(0xFFFFFFFF, &timer_base->load);
  73. /*
  74. * Start the A9Timer device
  75. * Enable Auto reload mode, Clear prescaler control bits
  76. * Set prescaler value, Enable the decrementer
  77. */
  78. clrsetbits_le32(&timer_base->control, SCUTIMER_CONTROL_PRESCALER_MASK,
  79. emask);
  80. /* Reset time */
  81. gd->arch.lastinc = readl(&timer_base->counter) /
  82. (gd->arch.timer_rate_hz / CONFIG_SYS_HZ);
  83. gd->arch.tbl = 0;
  84. return 0;
  85. }
  86. /*
  87. * This function is derived from PowerPC code (timebase clock frequency).
  88. * On ARM it returns the number of timer ticks per second.
  89. */
  90. ulong get_tbclk(void)
  91. {
  92. return gd->arch.timer_rate_hz;
  93. }