interrupts.c 3.1 KB

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