timer.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*****************************************************************
  2. * timer.c
  3. * by Zhiyi Huang, [email protected]
  4. * University of Otago
  5. *
  6. ********************************************************************/
  7. // The System Timer peripheral
  8. #include "types.h"
  9. #include "defs.h"
  10. #include "param.h"
  11. #include "memlayout.h"
  12. #include "proc.h"
  13. #include "traps.h"
  14. #include "arm.h"
  15. #include "spinlock.h"
  16. #define TIMER_REGS_BASE 0xFE003000
  17. #define CONTROL_STATUS 0x0 // control/status
  18. #define COUNTER_LO 0x4 // the time-stamp lower 32 bits
  19. #define COUNTER_HI 0x8 // the time-stamp higher 32 bits
  20. #define COMPARE0 0xc // compare 0
  21. #define COMPARE1 0x10 // compare 1
  22. #define COMPARE2 0x14 // compare 2
  23. #define COMPARE3 0x18 // compare 3
  24. #define TIMER_FREQ 10000 // interrupt 100 times/sec.
  25. void
  26. enabletimer3irq(void)
  27. {
  28. intctrlregs *ip;
  29. ip = (intctrlregs *)INT_REGS_BASE;
  30. ip->gpuenable[0] |= 1 << IRQ_TIMER3; // enable the system timer3 irq
  31. }
  32. void
  33. timer3init(void)
  34. {
  35. uint v;
  36. enabletimer3irq();
  37. v = inw(TIMER_REGS_BASE+COUNTER_LO);
  38. v += TIMER_FREQ;
  39. outw(TIMER_REGS_BASE+COMPARE3, v);
  40. ticks = 0;
  41. }
  42. void
  43. timer3intr(void)
  44. {
  45. uint v;
  46. //cprintf("timer3 interrupt: %x\n", inw(TIMER_REGS_BASE+CONTROL_STATUS));
  47. outw(TIMER_REGS_BASE+CONTROL_STATUS, (1 << IRQ_TIMER3)); // clear timer3 irq
  48. ticks++;
  49. wakeup(&ticks);
  50. // reset the value of compare3
  51. v=inw(TIMER_REGS_BASE+COUNTER_LO);
  52. v += TIMER_FREQ;
  53. outw(TIMER_REGS_BASE+COMPARE3, v);
  54. }
  55. void
  56. delay(uint m)
  57. {
  58. unsigned long long t;
  59. if(m == 0) return;
  60. t = getsystemtime() + m;
  61. while(t != getsystemtime());
  62. return;
  63. }