interrupts.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * U-Boot - interrupts.c Interrupt related routines
  3. *
  4. * Copyright (c) 2005-2008 Analog Devices Inc.
  5. *
  6. * This file is based on interrupts.c
  7. * Copyright 1996 Roman Zippel
  8. * Copyright 1999 D. Jeff Dionne <jeff@uclinux.org>
  9. * Copyright 2000-2001 Lineo, Inc. D. Jefff Dionne <jeff@lineo.ca>
  10. * Copyright 2002 Arcturus Networks Inc. MaTed <mated@sympatico.ca>
  11. * Copyright 2003 Metrowerks/Motorola
  12. * Copyright 2003 Bas Vermeulen <bas@buyways.nl>,
  13. * BuyWays B.V. (www.buyways.nl)
  14. *
  15. * (C) Copyright 2000-2004
  16. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  17. *
  18. * Licensed under the GPL-2 or later.
  19. */
  20. #include <common.h>
  21. #include <config.h>
  22. #include <watchdog.h>
  23. #include <asm/blackfin.h>
  24. #include "cpu.h"
  25. static ulong timestamp;
  26. static ulong last_time;
  27. static int int_flag;
  28. int irq_flags; /* needed by asm-blackfin/system.h */
  29. /* Functions just to satisfy the linker */
  30. /*
  31. * This function is derived from PowerPC code (read timebase as long long).
  32. * On Blackfin it just returns the timer value.
  33. */
  34. unsigned long long get_ticks(void)
  35. {
  36. return get_timer(0);
  37. }
  38. /*
  39. * This function is derived from PowerPC code (timebase clock frequency).
  40. * On Blackfin it returns the number of timer ticks per second.
  41. */
  42. ulong get_tbclk(void)
  43. {
  44. ulong tbclk;
  45. tbclk = CONFIG_SYS_HZ;
  46. return tbclk;
  47. }
  48. void enable_interrupts(void)
  49. {
  50. local_irq_restore(int_flag);
  51. }
  52. int disable_interrupts(void)
  53. {
  54. local_irq_save(int_flag);
  55. return 1;
  56. }
  57. void __udelay(unsigned long usec)
  58. {
  59. unsigned long delay, start, stop;
  60. unsigned long cclk;
  61. cclk = (CONFIG_CCLK_HZ);
  62. while (usec > 1) {
  63. WATCHDOG_RESET();
  64. /*
  65. * how many clock ticks to delay?
  66. * - request(in useconds) * clock_ticks(Hz) / useconds/second
  67. */
  68. if (usec < 1000) {
  69. delay = (usec * (cclk / 244)) >> 12;
  70. usec = 0;
  71. } else {
  72. delay = (1000 * (cclk / 244)) >> 12;
  73. usec -= 1000;
  74. }
  75. asm volatile (" %0 = CYCLES;" : "=r" (start));
  76. do {
  77. asm volatile (" %0 = CYCLES; " : "=r" (stop));
  78. } while (stop - start < delay);
  79. }
  80. return;
  81. }
  82. #define MAX_TIM_LOAD 0xFFFFFFFF
  83. int timer_init(void)
  84. {
  85. bfin_write_TCNTL(0x1);
  86. CSYNC();
  87. bfin_write_TSCALE(0x0);
  88. bfin_write_TCOUNT(MAX_TIM_LOAD);
  89. bfin_write_TPERIOD(MAX_TIM_LOAD);
  90. bfin_write_TCNTL(0x7);
  91. CSYNC();
  92. timestamp = 0;
  93. last_time = 0;
  94. return 0;
  95. }
  96. /*
  97. * Any network command or flash
  98. * command is started get_timer shall
  99. * be called before TCOUNT gets reset,
  100. * to implement the accurate timeouts.
  101. *
  102. * How ever milliconds doesn't return
  103. * the number that has been elapsed from
  104. * the last reset.
  105. *
  106. * As get_timer is used in the u-boot
  107. * only for timeouts this should be
  108. * sufficient
  109. */
  110. ulong get_timer(ulong base)
  111. {
  112. ulong milisec;
  113. /* Number of clocks elapsed */
  114. ulong clocks = (MAX_TIM_LOAD - bfin_read_TCOUNT());
  115. /*
  116. * Find if the TCOUNT is reset
  117. * timestamp gives the number of times
  118. * TCOUNT got reset
  119. */
  120. if (clocks < last_time)
  121. timestamp++;
  122. last_time = clocks;
  123. /* Get the number of milliseconds */
  124. milisec = clocks / (CONFIG_CCLK_HZ / 1000);
  125. /*
  126. * Find the number of millisonds that
  127. * got elapsed before this TCOUNT cycle
  128. */
  129. milisec += timestamp * (MAX_TIM_LOAD / (CONFIG_CCLK_HZ / 1000));
  130. return (milisec - base);
  131. }