dma_timer.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * dma_timer.c -- Freescale ColdFire DMA Timer.
  3. *
  4. * Copyright (C) 2007, Benedikt Spranger <b.spranger@linutronix.de>
  5. * Copyright (C) 2008. Sebastian Siewior, Linutronix
  6. *
  7. */
  8. #include <linux/clocksource.h>
  9. #include <linux/io.h>
  10. #include <asm/machdep.h>
  11. #include <asm/coldfire.h>
  12. #include <asm/mcfpit.h>
  13. #include <asm/mcfsim.h>
  14. #define DMA_TIMER_0 (0x00)
  15. #define DMA_TIMER_1 (0x40)
  16. #define DMA_TIMER_2 (0x80)
  17. #define DMA_TIMER_3 (0xc0)
  18. #define DTMR0 (MCF_IPSBAR + DMA_TIMER_0 + 0x400)
  19. #define DTXMR0 (MCF_IPSBAR + DMA_TIMER_0 + 0x402)
  20. #define DTER0 (MCF_IPSBAR + DMA_TIMER_0 + 0x403)
  21. #define DTRR0 (MCF_IPSBAR + DMA_TIMER_0 + 0x404)
  22. #define DTCR0 (MCF_IPSBAR + DMA_TIMER_0 + 0x408)
  23. #define DTCN0 (MCF_IPSBAR + DMA_TIMER_0 + 0x40c)
  24. #define DMA_FREQ ((MCF_CLK / 2) / 16)
  25. /* DTMR */
  26. #define DMA_DTMR_RESTART (1 << 3)
  27. #define DMA_DTMR_CLK_DIV_1 (1 << 1)
  28. #define DMA_DTMR_CLK_DIV_16 (2 << 1)
  29. #define DMA_DTMR_ENABLE (1 << 0)
  30. static cycle_t cf_dt_get_cycles(struct clocksource *cs)
  31. {
  32. return __raw_readl(DTCN0);
  33. }
  34. static struct clocksource clocksource_cf_dt = {
  35. .name = "coldfire_dma_timer",
  36. .rating = 200,
  37. .read = cf_dt_get_cycles,
  38. .mask = CLOCKSOURCE_MASK(32),
  39. .shift = 20,
  40. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  41. };
  42. static int __init init_cf_dt_clocksource(void)
  43. {
  44. /*
  45. * We setup DMA timer 0 in free run mode. This incrementing counter is
  46. * used as a highly precious clock source. With MCF_CLOCK = 150 MHz we
  47. * get a ~213 ns resolution and the 32bit register will overflow almost
  48. * every 15 minutes.
  49. */
  50. __raw_writeb(0x00, DTXMR0);
  51. __raw_writeb(0x00, DTER0);
  52. __raw_writel(0x00000000, DTRR0);
  53. __raw_writew(DMA_DTMR_CLK_DIV_16 | DMA_DTMR_ENABLE, DTMR0);
  54. clocksource_cf_dt.mult = clocksource_hz2mult(DMA_FREQ,
  55. clocksource_cf_dt.shift);
  56. return clocksource_register(&clocksource_cf_dt);
  57. }
  58. arch_initcall(init_cf_dt_clocksource);
  59. #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
  60. #define CYC2NS_SCALE ((1000000 << CYC2NS_SCALE_FACTOR) / (DMA_FREQ / 1000))
  61. static unsigned long long cycles2ns(unsigned long cycl)
  62. {
  63. return (unsigned long long) ((unsigned long long)cycl *
  64. CYC2NS_SCALE) >> CYC2NS_SCALE_FACTOR;
  65. }
  66. unsigned long long sched_clock(void)
  67. {
  68. unsigned long cycl = __raw_readl(DTCN0);
  69. return cycles2ns(cycl);
  70. }