interrupts.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. return CONFIG_SYS_HZ;
  45. }
  46. void enable_interrupts(void)
  47. {
  48. local_irq_restore(int_flag);
  49. }
  50. int disable_interrupts(void)
  51. {
  52. local_irq_save(int_flag);
  53. return 1;
  54. }
  55. void __udelay(unsigned long usec)
  56. {
  57. unsigned long delay, start, stop;
  58. unsigned long cclk;
  59. cclk = (CONFIG_CCLK_HZ);
  60. while (usec > 1) {
  61. WATCHDOG_RESET();
  62. /*
  63. * how many clock ticks to delay?
  64. * - request(in useconds) * clock_ticks(Hz) / useconds/second
  65. */
  66. if (usec < 1000) {
  67. delay = (usec * (cclk / 244)) >> 12;
  68. usec = 0;
  69. } else {
  70. delay = (1000 * (cclk / 244)) >> 12;
  71. usec -= 1000;
  72. }
  73. asm volatile (" %0 = CYCLES;" : "=r" (start));
  74. do {
  75. asm volatile (" %0 = CYCLES; " : "=r" (stop));
  76. } while (stop - start < delay);
  77. }
  78. return;
  79. }
  80. #define MAX_TIM_LOAD 0xFFFFFFFF
  81. int timer_init(void)
  82. {
  83. bfin_write_TCNTL(0x1);
  84. CSYNC();
  85. bfin_write_TSCALE(0x0);
  86. bfin_write_TCOUNT(MAX_TIM_LOAD);
  87. bfin_write_TPERIOD(MAX_TIM_LOAD);
  88. bfin_write_TCNTL(0x7);
  89. CSYNC();
  90. timestamp = 0;
  91. last_time = 0;
  92. return 0;
  93. }
  94. /*
  95. * Any network command or flash
  96. * command is started get_timer shall
  97. * be called before TCOUNT gets reset,
  98. * to implement the accurate timeouts.
  99. *
  100. * How ever milliconds doesn't return
  101. * the number that has been elapsed from
  102. * the last reset.
  103. *
  104. * As get_timer is used in the u-boot
  105. * only for timeouts this should be
  106. * sufficient
  107. */
  108. ulong get_timer(ulong base)
  109. {
  110. ulong milisec;
  111. /* Number of clocks elapsed */
  112. ulong clocks = (MAX_TIM_LOAD - bfin_read_TCOUNT());
  113. /*
  114. * Find if the TCOUNT is reset
  115. * timestamp gives the number of times
  116. * TCOUNT got reset
  117. */
  118. if (clocks < last_time)
  119. timestamp++;
  120. last_time = clocks;
  121. /* Get the number of milliseconds */
  122. milisec = clocks / (CONFIG_CCLK_HZ / 1000);
  123. /*
  124. * Find the number of millisonds that
  125. * got elapsed before this TCOUNT cycle
  126. */
  127. milisec += timestamp * (MAX_TIM_LOAD / (CONFIG_CCLK_HZ / 1000));
  128. return (milisec - base);
  129. }