timer.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * (C) Copyright 2008,2009
  3. * Graeme Russ, <graeme.russ@gmail.com>
  4. *
  5. * (C) Copyright 2002
  6. * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <malloc.h>
  28. #include <asm/io.h>
  29. #include <asm/i8254.h>
  30. #include <asm/ibmpc.h>
  31. struct timer_isr_function {
  32. struct timer_isr_function *next;
  33. timer_fnc_t *isr_func;
  34. };
  35. static struct timer_isr_function *first_timer_isr;
  36. static unsigned long system_ticks;
  37. /*
  38. * register_timer_isr() allows multiple architecture and board specific
  39. * functions to be called every millisecond. Keep the execution time of
  40. * each function as low as possible
  41. */
  42. int register_timer_isr(timer_fnc_t *isr_func)
  43. {
  44. struct timer_isr_function *new_func;
  45. struct timer_isr_function *temp;
  46. int flag;
  47. new_func = malloc(sizeof(struct timer_isr_function));
  48. if (new_func == NULL)
  49. return 1;
  50. new_func->isr_func = isr_func;
  51. new_func->next = NULL;
  52. /*
  53. * Don't allow timer interrupts while the
  54. * linked list is being modified
  55. */
  56. flag = disable_interrupts();
  57. if (first_timer_isr == NULL) {
  58. first_timer_isr = new_func;
  59. } else {
  60. temp = first_timer_isr;
  61. while (temp->next != NULL)
  62. temp = temp->next;
  63. temp->next = new_func;
  64. }
  65. if (flag)
  66. enable_interrupts();
  67. return 0;
  68. }
  69. /*
  70. * timer_isr() MUST be the registered interrupt handler for
  71. */
  72. void timer_isr(void *unused)
  73. {
  74. struct timer_isr_function *temp = first_timer_isr;
  75. system_ticks++;
  76. /* Execute each registered function */
  77. while (temp != NULL) {
  78. temp->isr_func();
  79. temp = temp->next;
  80. }
  81. }
  82. ulong get_timer(ulong base)
  83. {
  84. return system_ticks - base;
  85. }
  86. void timer_set_tsc_base(uint64_t new_base)
  87. {
  88. gd->arch.tsc_base = new_base;
  89. }
  90. uint64_t timer_get_tsc(void)
  91. {
  92. uint64_t time_now;
  93. time_now = rdtsc();
  94. if (!gd->arch.tsc_base)
  95. gd->arch.tsc_base = time_now;
  96. return time_now - gd->arch.tsc_base;
  97. }