timer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * (C) Copyright 2003
  3. * Texas Instruments <www.ti.com>
  4. *
  5. * (C) Copyright 2002
  6. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Marius Groeger <mgroeger@sysgo.de>
  8. *
  9. * (C) Copyright 2002
  10. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  11. * Alex Zuepke <azu@sysgo.de>
  12. *
  13. * (C) Copyright 2002-2004
  14. * Gary Jennejohn, DENX Software Engineering, <garyj@denx.de>
  15. *
  16. * (C) Copyright 2004
  17. * Philippe Robin, ARM Ltd. <philippe.robin@arm.com>
  18. *
  19. * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
  20. *
  21. * See file CREDITS for list of people who contributed to this
  22. * project.
  23. *
  24. * This program is free software; you can redistribute it and/or
  25. * modify it under the terms of the GNU General Public License as
  26. * published by the Free Software Foundation; either version 2 of
  27. * the License, or (at your option) any later version.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU General Public License
  35. * along with this program; if not, write to the Free Software
  36. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  37. * MA 02111-1307 USA
  38. */
  39. #include <common.h>
  40. #include <asm/io.h>
  41. #include <asm/arch/timer_defs.h>
  42. #include <div64.h>
  43. DECLARE_GLOBAL_DATA_PTR;
  44. static struct davinci_timer * const timer =
  45. (struct davinci_timer *)CONFIG_SYS_TIMERBASE;
  46. #define TIMER_LOAD_VAL 0xffffffff
  47. #define TIM_CLK_DIV 16
  48. int timer_init(void)
  49. {
  50. /* We are using timer34 in unchained 32-bit mode, full speed */
  51. writel(0x0, &timer->tcr);
  52. writel(0x0, &timer->tgcr);
  53. writel(0x06 | ((TIM_CLK_DIV - 1) << 8), &timer->tgcr);
  54. writel(0x0, &timer->tim34);
  55. writel(TIMER_LOAD_VAL, &timer->prd34);
  56. writel(2 << 22, &timer->tcr);
  57. gd->arch.timer_rate_hz = CONFIG_SYS_HZ_CLOCK / TIM_CLK_DIV;
  58. gd->arch.timer_reset_value = 0;
  59. return(0);
  60. }
  61. /*
  62. * Get the current 64 bit timer tick count
  63. */
  64. unsigned long long get_ticks(void)
  65. {
  66. unsigned long now = readl(&timer->tim34);
  67. /* increment tbu if tbl has rolled over */
  68. if (now < gd->arch.tbl)
  69. gd->arch.tbu++;
  70. gd->arch.tbl = now;
  71. return (((unsigned long long)gd->arch.tbu) << 32) | gd->arch.tbl;
  72. }
  73. ulong get_timer(ulong base)
  74. {
  75. unsigned long long timer_diff;
  76. timer_diff = get_ticks() - gd->arch.timer_reset_value;
  77. return lldiv(timer_diff,
  78. (gd->arch.timer_rate_hz / CONFIG_SYS_HZ)) - base;
  79. }
  80. void __udelay(unsigned long usec)
  81. {
  82. unsigned long long endtime;
  83. endtime = lldiv((unsigned long long)usec * gd->arch.timer_rate_hz,
  84. 1000000UL);
  85. endtime += get_ticks();
  86. while (get_ticks() < endtime)
  87. ;
  88. }
  89. /*
  90. * This function is derived from PowerPC code (timebase clock frequency).
  91. * On ARM it returns the number of timer ticks per second.
  92. */
  93. ulong get_tbclk(void)
  94. {
  95. return gd->arch.timer_rate_hz;
  96. }
  97. #ifdef CONFIG_HW_WATCHDOG
  98. static struct davinci_timer * const wdttimer =
  99. (struct davinci_timer *)CONFIG_SYS_WDTTIMERBASE;
  100. /*
  101. * See prufw2.pdf for using Timer as a WDT
  102. */
  103. void davinci_hw_watchdog_enable(void)
  104. {
  105. writel(0x0, &wdttimer->tcr);
  106. writel(0x0, &wdttimer->tgcr);
  107. /* TIMMODE = 2h */
  108. writel(0x08 | 0x03 | ((TIM_CLK_DIV - 1) << 8), &wdttimer->tgcr);
  109. writel(CONFIG_SYS_WDT_PERIOD_LOW, &wdttimer->prd12);
  110. writel(CONFIG_SYS_WDT_PERIOD_HIGH, &wdttimer->prd34);
  111. writel(2 << 22, &wdttimer->tcr);
  112. writel(0x0, &wdttimer->tim12);
  113. writel(0x0, &wdttimer->tim34);
  114. /* set WDEN bit, WDKEY 0xa5c6 */
  115. writel(0xa5c64000, &wdttimer->wdtcr);
  116. /* clear counter register */
  117. writel(0xda7e4000, &wdttimer->wdtcr);
  118. }
  119. void davinci_hw_watchdog_reset(void)
  120. {
  121. writel(0xa5c64000, &wdttimer->wdtcr);
  122. writel(0xda7e4000, &wdttimer->wdtcr);
  123. }
  124. #endif