timer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2003
  4. * Texas Instruments <www.ti.com>
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Marius Groeger <mgroeger@sysgo.de>
  9. *
  10. * (C) Copyright 2002
  11. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  12. * Alex Zuepke <azu@sysgo.de>
  13. *
  14. * (C) Copyright 2002-2004
  15. * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
  16. *
  17. * (C) Copyright 2004
  18. * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
  19. *
  20. * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
  21. */
  22. #include <common.h>
  23. #include <init.h>
  24. #include <time.h>
  25. #include <asm/global_data.h>
  26. #include <asm/io.h>
  27. #include <asm/arch/timer_defs.h>
  28. #include <div64.h>
  29. #include <linux/delay.h>
  30. DECLARE_GLOBAL_DATA_PTR;
  31. static struct davinci_timer * const timer =
  32. (struct davinci_timer *)CONFIG_SYS_TIMERBASE;
  33. #define TIMER_LOAD_VAL 0xffffffff
  34. #define TIM_CLK_DIV 16
  35. int timer_init(void)
  36. {
  37. /* We are using timer34 in unchained 32-bit mode, full speed */
  38. writel(0x0, &timer->tcr);
  39. writel(0x0, &timer->tgcr);
  40. writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &timer->tgcr);
  41. writel(0x0, &timer->tim34);
  42. writel(TIMER_LOAD_VAL, &timer->prd34);
  43. writel(2 << 22, &timer->tcr);
  44. gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK / TIM_CLK_DIV;
  45. gd->arch.timer_reset_value = 0;
  46. return(0);
  47. }
  48. /*
  49. * Get the current 64 bit timer tick count
  50. */
  51. unsigned long long get_ticks(void)
  52. {
  53. unsigned long now = readl(&timer->tim34);
  54. /* increment tbu if tbl has rolled over */
  55. if (now < gd->arch.tbl)
  56. gd->arch.tbu++;
  57. gd->arch.tbl = now;
  58. return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
  59. }
  60. ulong get_timer(ulong base)
  61. {
  62. unsigned long long timer_diff;
  63. timer_diff = get_ticks() - gd->arch.timer_reset_value;
  64. return lldiv(timer_diff,
  65. (gd->arch.timer_rate_hz / CONFIG_SYS_HZ)) - base;
  66. }
  67. void __udelay(unsigned long usec)
  68. {
  69. unsigned long long endtime;
  70. endtime = lldiv((unsigned long long)usec * gd->arch.timer_rate_hz,
  71. 1000000UL);
  72. endtime += get_ticks();
  73. while (get_ticks() < endtime)
  74. ;
  75. }
  76. /*
  77. * This function is derived from PowerPC code (timebase clock frequency).
  78. * On ARM it returns the number of timer ticks per second.
  79. */
  80. ulong get_tbclk(void)
  81. {
  82. return gd->arch.timer_rate_hz;
  83. }
  84. #ifdef CONFIG_HW_WATCHDOG
  85. static struct davinci_timer * const wdttimer =
  86. (struct davinci_timer *)CONFIG_SYS_WDTTIMERBASE;
  87. /*
  88. * See prufw2.pdf for using Timer as a WDT
  89. */
  90. void davinci_hw_watchdog_enable(void)
  91. {
  92. writel(0x0, &wdttimer->tcr);
  93. writel(0x0, &wdttimer->tgcr);
  94. /* TIMMODE = 2h */
  95. writel(0x08 | 0x03 | ((TIM_CLK_DIV - 1) << 8), &wdttimer->tgcr);
  96. writel(CONFIG_SYS_WDT_PERIOD_LOW, &wdttimer->prd12);
  97. writel(CONFIG_SYS_WDT_PERIOD_HIGH, &wdttimer->prd34);
  98. writel(2 << 22, &wdttimer->tcr);
  99. writel(0x0, &wdttimer->tim12);
  100. writel(0x0, &wdttimer->tim34);
  101. /* set WDEN bit, WDKEY 0xa5c6 */
  102. writel(0xa5c64000, &wdttimer->wdtcr);
  103. /* clear counter register */
  104. writel(0xda7e4000, &wdttimer->wdtcr);
  105. }
  106. void davinci_hw_watchdog_reset(void)
  107. {
  108. writel(0xa5c64000, &wdttimer->wdtcr);
  109. writel(0xda7e4000, &wdttimer->wdtcr);
  110. }
  111. #endif