interrupts.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <asm/div64.h>
  24. #include <asm/errno.h>
  25. #include <asm/io.h>
  26. #include <asm/processor.h>
  27. #include <asm/sysreg.h>
  28. #include <asm/arch/memory-map.h>
  29. #define HANDLER_MASK 0x00ffffff
  30. #define INTLEV_SHIFT 30
  31. #define INTLEV_MASK 0x00000003
  32. DECLARE_GLOBAL_DATA_PTR;
  33. /* Incremented whenever COUNT reaches 0xffffffff by timer_interrupt_handler */
  34. volatile unsigned long timer_overflow;
  35. /*
  36. * Instead of dividing by get_tbclk(), multiply by this constant and
  37. * right-shift the result by 32 bits.
  38. */
  39. static unsigned long tb_factor;
  40. unsigned long get_tbclk(void)
  41. {
  42. return gd->cpu_hz;
  43. }
  44. unsigned long long get_ticks(void)
  45. {
  46. unsigned long lo, hi_now, hi_prev;
  47. do {
  48. hi_prev = timer_overflow;
  49. lo = sysreg_read(COUNT);
  50. hi_now = timer_overflow;
  51. } while (hi_prev != hi_now);
  52. return ((unsigned long long)hi_now << 32) | lo;
  53. }
  54. void reset_timer(void)
  55. {
  56. sysreg_write(COUNT, 0);
  57. cpu_sync_pipeline(); /* process any pending interrupts */
  58. timer_overflow = 0;
  59. }
  60. unsigned long get_timer(unsigned long base)
  61. {
  62. u64 now = get_ticks();
  63. now *= tb_factor;
  64. return (unsigned long)(now >> 32) - base;
  65. }
  66. void set_timer(unsigned long t)
  67. {
  68. unsigned long long ticks = t;
  69. unsigned long lo, hi, hi_new;
  70. ticks = (ticks * get_tbclk()) / CFG_HZ;
  71. hi = ticks >> 32;
  72. lo = ticks & 0xffffffffUL;
  73. do {
  74. timer_overflow = hi;
  75. sysreg_write(COUNT, lo);
  76. hi_new = timer_overflow;
  77. } while (hi_new != hi);
  78. }
  79. /*
  80. * For short delays only. It will overflow after a few seconds.
  81. */
  82. void udelay(unsigned long usec)
  83. {
  84. unsigned long now, end;
  85. now = sysreg_read(COUNT);
  86. end = ((usec * (get_tbclk() / 10000)) + 50) / 100;
  87. end += now;
  88. while (now > end)
  89. now = sysreg_read(COUNT);
  90. while (now < end)
  91. now = sysreg_read(COUNT);
  92. }
  93. static int set_interrupt_handler(unsigned int nr, void (*handler)(void),
  94. unsigned int priority)
  95. {
  96. extern void _evba(void);
  97. unsigned long intpr;
  98. unsigned long handler_addr = (unsigned long)handler;
  99. handler_addr -= (unsigned long)&_evba;
  100. if ((handler_addr & HANDLER_MASK) != handler_addr
  101. || (priority & INTLEV_MASK) != priority)
  102. return -EINVAL;
  103. intpr = (handler_addr & HANDLER_MASK);
  104. intpr |= (priority & INTLEV_MASK) << INTLEV_SHIFT;
  105. writel(intpr, (void *)INTC_BASE + 4 * nr);
  106. return 0;
  107. }
  108. void timer_init(void)
  109. {
  110. extern void timer_interrupt_handler(void);
  111. u64 tmp;
  112. sysreg_write(COUNT, 0);
  113. tmp = (u64)CFG_HZ << 32;
  114. tmp += gd->cpu_hz / 2;
  115. do_div(tmp, gd->cpu_hz);
  116. tb_factor = (u32)tmp;
  117. if (set_interrupt_handler(0, &timer_interrupt_handler, 3))
  118. return;
  119. /* For all practical purposes, this gives us an overflow interrupt */
  120. sysreg_write(COMPARE, 0xffffffff);
  121. }