pit.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/io.h>
  18. #include <asm/coldfire.h>
  19. #include <asm/mcfpit.h>
  20. #include <asm/mcfsim.h>
  21. /***************************************************************************/
  22. /*
  23. * By default use timer1 as the system clock timer.
  24. */
  25. #define TA(a) (MCF_IPSBAR + MCFPIT_BASE1 + (a))
  26. /***************************************************************************/
  27. void coldfire_pit_tick(void)
  28. {
  29. unsigned short pcsr;
  30. /* Reset the ColdFire timer */
  31. pcsr = __raw_readw(TA(MCFPIT_PCSR));
  32. __raw_writew(pcsr | MCFPIT_PCSR_PIF, TA(MCFPIT_PCSR));
  33. }
  34. /***************************************************************************/
  35. static struct irqaction coldfire_pit_irq = {
  36. .name = "timer",
  37. .flags = IRQF_DISABLED | IRQF_TIMER,
  38. };
  39. void coldfire_pit_init(irq_handler_t handler)
  40. {
  41. volatile unsigned char *icrp;
  42. volatile unsigned long *imrp;
  43. coldfire_pit_irq.handler = handler;
  44. setup_irq(MCFINT_VECBASE + MCFINT_PIT1, &coldfire_pit_irq);
  45. icrp = (volatile unsigned char *) (MCF_IPSBAR + MCFICM_INTC0 +
  46. MCFINTC_ICR0 + MCFINT_PIT1);
  47. *icrp = ICR_INTRCONF;
  48. imrp = (volatile unsigned long *) (MCF_IPSBAR + MCFICM_INTC0 + MCFPIT_IMR);
  49. *imrp &= ~MCFPIT_IMR_IBIT;
  50. /* Set up PIT timer 1 as poll clock */
  51. __raw_writew(MCFPIT_PCSR_DISABLE, TA(MCFPIT_PCSR));
  52. __raw_writew(((MCF_CLK / 2) / 64) / HZ, TA(MCFPIT_PMR));
  53. __raw_writew(MCFPIT_PCSR_EN | MCFPIT_PCSR_PIE | MCFPIT_PCSR_OVW |
  54. MCFPIT_PCSR_RLD | MCFPIT_PCSR_CLK64, TA(MCFPIT_PCSR));
  55. }
  56. /***************************************************************************/
  57. unsigned long coldfire_pit_offset(void)
  58. {
  59. volatile unsigned long *ipr;
  60. unsigned long pmr, pcntr, offset;
  61. ipr = (volatile unsigned long *) (MCF_IPSBAR + MCFICM_INTC0 + MCFPIT_IMR);
  62. pmr = __raw_readw(TA(MCFPIT_PMR));
  63. pcntr = __raw_readw(TA(MCFPIT_PCNTR));
  64. /*
  65. * If we are still in the first half of the upcount and a
  66. * timer interupt is pending, then add on a ticks worth of time.
  67. */
  68. offset = ((pmr - pcntr) * (1000000 / HZ)) / pmr;
  69. if ((offset < (1000000 / HZ / 2)) && (*ipr & MCFPIT_IMR_IBIT))
  70. offset += 1000000 / HZ;
  71. return offset;
  72. }
  73. /***************************************************************************/