interrupts.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/platform.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. static const struct device *intc_dev;
  41. unsigned long get_tbclk(void)
  42. {
  43. return gd->cpu_hz;
  44. }
  45. unsigned long long get_ticks(void)
  46. {
  47. unsigned long lo, hi_now, hi_prev;
  48. do {
  49. hi_prev = timer_overflow;
  50. lo = sysreg_read(COUNT);
  51. hi_now = timer_overflow;
  52. } while (hi_prev != hi_now);
  53. return ((unsigned long long)hi_now << 32) | lo;
  54. }
  55. void reset_timer(void)
  56. {
  57. sysreg_write(COUNT, 0);
  58. cpu_sync_pipeline(); /* process any pending interrupts */
  59. timer_overflow = 0;
  60. }
  61. unsigned long get_timer(unsigned long base)
  62. {
  63. u64 now = get_ticks();
  64. now *= tb_factor;
  65. return (unsigned long)(now >> 32) - base;
  66. }
  67. void set_timer(unsigned long t)
  68. {
  69. unsigned long long ticks = t;
  70. unsigned long lo, hi, hi_new;
  71. ticks = (ticks * get_tbclk()) / CFG_HZ;
  72. hi = ticks >> 32;
  73. lo = ticks & 0xffffffffUL;
  74. do {
  75. timer_overflow = hi;
  76. sysreg_write(COUNT, lo);
  77. hi_new = timer_overflow;
  78. } while (hi_new != hi);
  79. }
  80. /*
  81. * For short delays only. It will overflow after a few seconds.
  82. */
  83. void udelay(unsigned long usec)
  84. {
  85. unsigned long now, end;
  86. now = sysreg_read(COUNT);
  87. end = ((usec * (get_tbclk() / 10000)) + 50) / 100;
  88. end += now;
  89. while (now > end)
  90. now = sysreg_read(COUNT);
  91. while (now < end)
  92. now = sysreg_read(COUNT);
  93. }
  94. static int set_interrupt_handler(unsigned int nr, void (*handler)(void),
  95. unsigned int priority)
  96. {
  97. unsigned long intpr;
  98. unsigned long handler_addr = (unsigned long)handler;
  99. if ((handler_addr & HANDLER_MASK) != handler_addr
  100. || (priority & INTLEV_MASK) != priority)
  101. return -EINVAL;
  102. intpr = (handler_addr & HANDLER_MASK);
  103. intpr |= (priority & INTLEV_MASK) << INTLEV_SHIFT;
  104. writel(intpr, intc_dev->regs + 4 * nr);
  105. return 0;
  106. }
  107. void timer_init(void)
  108. {
  109. extern void timer_interrupt_handler(void);
  110. u64 tmp;
  111. sysreg_write(COUNT, 0);
  112. tmp = (u64)CFG_HZ << 32;
  113. tmp += gd->cpu_hz / 2;
  114. do_div(tmp, gd->cpu_hz);
  115. tb_factor = (u32)tmp;
  116. intc_dev = get_device(DEVICE_INTC);
  117. if (!intc_dev
  118. || set_interrupt_handler(0, &timer_interrupt_handler, 3))
  119. return;
  120. /* For all practical purposes, this gives us an overflow interrupt */
  121. sysreg_write(COMPARE, 0xffffffff);
  122. }