systick-timer.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ARM Cortex M3/M4/M7 SysTick timer driver
  4. * (C) Copyright 2017 Renesas Electronics Europe Ltd
  5. *
  6. * Based on arch/arm/mach-stm32/stm32f1/timer.c
  7. * (C) Copyright 2015
  8. * Kamil Lulko, <kamil.lulko@gmail.com>
  9. *
  10. * Copyright 2015 ATS Advanced Telematics Systems GmbH
  11. * Copyright 2015 Konsulko Group, Matt Porter <mporter@konsulko.com>
  12. *
  13. * The SysTick timer is a 24-bit count down timer. The clock can be either the
  14. * CPU clock or a reference clock. Since the timer will wrap around very quickly
  15. * when using the CPU clock, and we do not handle the timer interrupts, it is
  16. * expected that this driver is only ever used with a slow reference clock.
  17. *
  18. * The number of reference clock ticks that correspond to 10ms is normally
  19. * defined in the SysTick Calibration register's TENMS field. However, on some
  20. * devices this is wrong, so this driver allows the clock rate to be defined
  21. * using CONFIG_SYS_HZ_CLOCK.
  22. */
  23. #include <common.h>
  24. #include <init.h>
  25. #include <time.h>
  26. #include <asm/global_data.h>
  27. #include <asm/io.h>
  28. #include <linux/bitops.h>
  29. DECLARE_GLOBAL_DATA_PTR;
  30. /* SysTick Base Address - fixed for all Cortex M3, M4 and M7 devices */
  31. #define SYSTICK_BASE 0xE000E010
  32. struct cm3_systick {
  33. uint32_t ctrl;
  34. uint32_t reload_val;
  35. uint32_t current_val;
  36. uint32_t calibration;
  37. };
  38. #define TIMER_MAX_VAL 0x00FFFFFF
  39. #define SYSTICK_CTRL_EN BIT(0)
  40. /* Clock source: 0 = Ref clock, 1 = CPU clock */
  41. #define SYSTICK_CTRL_CPU_CLK BIT(2)
  42. #define SYSTICK_CAL_NOREF BIT(31)
  43. #define SYSTICK_CAL_SKEW BIT(30)
  44. #define SYSTICK_CAL_TENMS_MASK 0x00FFFFFF
  45. /* read the 24-bit timer */
  46. static ulong read_timer(void)
  47. {
  48. struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
  49. /* The timer counts down, therefore convert to an incrementing timer */
  50. return TIMER_MAX_VAL - readl(&systick->current_val);
  51. }
  52. int timer_init(void)
  53. {
  54. struct cm3_systick *systick = (struct cm3_systick *)SYSTICK_BASE;
  55. u32 cal;
  56. writel(TIMER_MAX_VAL, &systick->reload_val);
  57. /* Any write to current_val reg clears it to 0 */
  58. writel(0, &systick->current_val);
  59. cal = readl(&systick->calibration);
  60. if (cal & SYSTICK_CAL_NOREF)
  61. /* Use CPU clock, no interrupts */
  62. writel(SYSTICK_CTRL_EN | SYSTICK_CTRL_CPU_CLK, &systick->ctrl);
  63. else
  64. /* Use external clock, no interrupts */
  65. writel(SYSTICK_CTRL_EN, &systick->ctrl);
  66. /*
  67. * If the TENMS field is inexact or wrong, specify the clock rate using
  68. * CONFIG_SYS_HZ_CLOCK.
  69. */
  70. #if defined(CONFIG_SYS_HZ_CLOCK)
  71. gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK;
  72. #else
  73. gd->arch.timer_rate_hz = (cal & SYSTICK_CAL_TENMS_MASK) * 100;
  74. #endif
  75. gd->arch.tbl = 0;
  76. gd->arch.tbu = 0;
  77. gd->arch.lastinc = read_timer();
  78. return 0;
  79. }
  80. /* return milli-seconds timer value */
  81. ulong get_timer(ulong base)
  82. {
  83. unsigned long long t = get_ticks() * 1000;
  84. return (ulong)((t / gd->arch.timer_rate_hz)) - base;
  85. }
  86. unsigned long long get_ticks(void)
  87. {
  88. u32 now = read_timer();
  89. if (now >= gd->arch.lastinc)
  90. gd->arch.tbl += (now - gd->arch.lastinc);
  91. else
  92. gd->arch.tbl += (TIMER_MAX_VAL - gd->arch.lastinc) + now;
  93. gd->arch.lastinc = now;
  94. return gd->arch.tbl;
  95. }
  96. ulong get_tbclk(void)
  97. {
  98. return gd->arch.timer_rate_hz;
  99. }