interrupts.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <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/hardware.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. unsigned long get_timer(unsigned long base)
  55. {
  56. u64 now = get_ticks();
  57. now *= tb_factor;
  58. return (unsigned long)(now >> 32) - base;
  59. }
  60. /*
  61. * For short delays only. It will overflow after a few seconds.
  62. */
  63. void __udelay(unsigned long usec)
  64. {
  65. unsigned long cycles;
  66. unsigned long base;
  67. unsigned long now;
  68. base = sysreg_read(COUNT);
  69. cycles = ((usec * (get_tbclk() / 10000)) + 50) / 100;
  70. do {
  71. now = sysreg_read(COUNT);
  72. } while ((now - base) < cycles);
  73. }
  74. static int set_interrupt_handler(unsigned int nr, void (*handler)(void),
  75. unsigned int priority)
  76. {
  77. extern void _evba(void);
  78. unsigned long intpr;
  79. unsigned long handler_addr = (unsigned long)handler;
  80. handler_addr -= (unsigned long)&_evba;
  81. if ((handler_addr & HANDLER_MASK) != handler_addr
  82. || (priority & INTLEV_MASK) != priority)
  83. return -EINVAL;
  84. intpr = (handler_addr & HANDLER_MASK);
  85. intpr |= (priority & INTLEV_MASK) << INTLEV_SHIFT;
  86. writel(intpr, (void *)ATMEL_BASE_INTC + 4 * nr);
  87. return 0;
  88. }
  89. void timer_init(void)
  90. {
  91. extern void timer_interrupt_handler(void);
  92. u64 tmp;
  93. sysreg_write(COUNT, 0);
  94. tmp = (u64)CONFIG_SYS_HZ << 32;
  95. tmp += gd->cpu_hz / 2;
  96. do_div(tmp, gd->cpu_hz);
  97. tb_factor = (u32)tmp;
  98. if (set_interrupt_handler(0, &timer_interrupt_handler, 3))
  99. return;
  100. /* For all practical purposes, this gives us an overflow interrupt */
  101. sysreg_write(COMPARE, 0xffffffff);
  102. }