pit.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /***************************************************************************/
  2. /*
  3. * pit.c -- Freescale ColdFire PIT timer. Currently this type of
  4. * hardware timer only exists in the Freescale ColdFire
  5. * 5270/5271, 5282 and other CPUs.
  6. *
  7. * Copyright (C) 1999-2007, Greg Ungerer (gerg@snapgear.com)
  8. * Copyright (C) 2001-2004, SnapGear Inc. (www.snapgear.com)
  9. */
  10. /***************************************************************************/
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/param.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <asm/machdep.h>
  18. #include <asm/io.h>
  19. #include <asm/coldfire.h>
  20. #include <asm/mcfpit.h>
  21. #include <asm/mcfsim.h>
  22. /***************************************************************************/
  23. /*
  24. * By default use timer1 as the system clock timer.
  25. */
  26. #define TA(a) (MCF_IPSBAR + MCFPIT_BASE1 + (a))
  27. /***************************************************************************/
  28. static irqreturn_t hw_tick(int irq, void *dummy)
  29. {
  30. unsigned short pcsr;
  31. /* Reset the ColdFire timer */
  32. pcsr = __raw_readw(TA(MCFPIT_PCSR));
  33. __raw_writew(pcsr | MCFPIT_PCSR_PIF, TA(MCFPIT_PCSR));
  34. return arch_timer_interrupt(irq, dummy);
  35. }
  36. /***************************************************************************/
  37. static struct irqaction coldfire_pit_irq = {
  38. .name = "timer",
  39. .flags = IRQF_DISABLED | IRQF_TIMER,
  40. .handler = hw_tick,
  41. };
  42. void hw_timer_init(void)
  43. {
  44. volatile unsigned char *icrp;
  45. volatile unsigned long *imrp;
  46. setup_irq(MCFINT_VECBASE + MCFINT_PIT1, &coldfire_pit_irq);
  47. icrp = (volatile unsigned char *) (MCF_IPSBAR + MCFICM_INTC0 +
  48. MCFINTC_ICR0 + MCFINT_PIT1);
  49. *icrp = ICR_INTRCONF;
  50. imrp = (volatile unsigned long *) (MCF_IPSBAR + MCFICM_INTC0 + MCFPIT_IMR);
  51. *imrp &= ~MCFPIT_IMR_IBIT;
  52. /* Set up PIT timer 1 as poll clock */
  53. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  54. __raw_writew(((MCF_CLK / 2) / 64) / HZ, TA(MCFPIT_PMR));
  55. __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE | MCFPIT_PCSR_OVW |
  56. MCFPIT_PCSR_RLD | MCFPIT_PCSR_CLK64, TA(MCFPIT_PCSR));
  57. }
  58. /***************************************************************************/
  59. unsigned long hw_timer_offset(void)
  60. {
  61. volatile unsigned long *ipr;
  62. unsigned long pmr, pcntr, offset;
  63. ipr = (volatile unsigned long *) (MCF_IPSBAR + MCFICM_INTC0 + MCFPIT_IMR);
  64. pmr = __raw_readw(TA(MCFPIT_PMR));
  65. pcntr = __raw_readw(TA(MCFPIT_PCNTR));
  66. /*
  67. * If we are still in the first half of the upcount and a
  68. * timer interrupt is pending, then add on a ticks worth of time.
  69. */
  70. offset = ((pmr - pcntr) * (1000000 / HZ)) / pmr;
  71. if ((offset < (1000000 / HZ / 2)) && (*ipr & MCFPIT_IMR_IBIT))
  72. offset += 1000000 / HZ;
  73. return offset;
  74. }
  75. /***************************************************************************/