timer.c 2.4 KB

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