You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

e1_timer.c 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2003 Yannis Mitsos and George Thanos
  3. * {gmitsos@gthanos}@telecom.ntua.gr
  4. * Released under GPL2, see the file COPYING in the top directory
  5. *
  6. */
  7. #include "etherboot.h"
  8. #include "timer.h"
  9. #include "e132_xs_board.h"
  10. #include "init.h"
  11. /* get timer returns the contents of the timer */
  12. static inline unsigned long get_timer(void)
  13. {
  14. unsigned long result;
  15. __asm__ __volatile__("
  16. ORI SR, 0x20
  17. mov %0, TR"
  18. : "=l"(result));
  19. return result;
  20. }
  21. /* ------ Calibrate the TSC -------
  22. * Time how long it takes to excute a loop that runs in known time.
  23. * And find the convertion needed to get to CLOCK_TICK_RATE
  24. */
  25. static unsigned long configure_timer(void)
  26. {
  27. unsigned long TPR_value; /* Timer Prescalar Value */
  28. TPR_value = 0x000C00000;
  29. asm volatile ("
  30. FETCH 4
  31. ORI SR, 0x20
  32. MOV TPR, %0
  33. ORI SR, 0x20
  34. MOVI TR, 0x0"
  35. : /* no outputs */
  36. : "l" (TPR_value)
  37. );
  38. printf("The time prescaler register is set to: <%#x>\n",TPR_value);
  39. return (1);
  40. }
  41. static unsigned long clocks_per_tick;
  42. static void setup_timers(void)
  43. {
  44. if (!clocks_per_tick) {
  45. clocks_per_tick = configure_timer();
  46. }
  47. }
  48. unsigned long currticks(void)
  49. {
  50. return get_timer()/clocks_per_tick;
  51. }
  52. static unsigned long timer_timeout;
  53. static int __timer_running(void)
  54. {
  55. return get_timer() < timer_timeout;
  56. }
  57. void udelay(unsigned int usecs)
  58. {
  59. unsigned long now;
  60. now = get_timer();
  61. timer_timeout = now + usecs * ((clocks_per_tick * TICKS_PER_SEC)/(1000*1000));
  62. while(__timer_running());
  63. }
  64. void ndelay(unsigned int nsecs)
  65. {
  66. unsigned long now;
  67. now = get_timer();
  68. timer_timeout = now + nsecs * ((clocks_per_tick * TICKS_PER_SEC)/(1000*1000*1000));
  69. while(__timer_running());
  70. }
  71. void load_timer2(unsigned int timer2_ticks)
  72. {
  73. unsigned long now;
  74. unsigned long clocks;
  75. now = get_timer();
  76. clocks = timer2_ticks * ((clocks_per_tick * TICKS_PER_SEC)/CLOCK_TICK_RATE);
  77. timer_timeout = now + clocks;
  78. }
  79. int timer2_running(void)
  80. {
  81. return __timer_running();
  82. }
  83. INIT_FN ( INIT_TIMERS, setup_timers, NULL, NULL );