cevt-txx9.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Based on linux/arch/mips/kernel/cevt-r4k.c,
  7. * linux/arch/mips/jmr3927/rbhma3100/setup.c
  8. *
  9. * Copyright 2001 MontaVista Software Inc.
  10. * Copyright (C) 2000-2001 Toshiba Corporation
  11. * Copyright (C) 2007 MIPS Technologies, Inc.
  12. * Copyright (C) 2007 Ralf Baechle <ralf@linux-mips.org>
  13. */
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <asm/time.h>
  18. #include <asm/txx9tmr.h>
  19. #define TCR_BASE (TXx9_TMTCR_CCDE | TXx9_TMTCR_CRE | TXx9_TMTCR_TMODE_ITVL)
  20. #define TIMER_CCD 0 /* 1/2 */
  21. #define TIMER_CLK(imclk) ((imclk) / (2 << TIMER_CCD))
  22. struct txx9_clocksource {
  23. struct clocksource cs;
  24. struct txx9_tmr_reg __iomem *tmrptr;
  25. };
  26. static cycle_t txx9_cs_read(struct clocksource *cs)
  27. {
  28. struct txx9_clocksource *txx9_cs =
  29. container_of(cs, struct txx9_clocksource, cs);
  30. return __raw_readl(&txx9_cs->tmrptr->trr);
  31. }
  32. /* Use 1 bit smaller width to use full bits in that width */
  33. #define TXX9_CLOCKSOURCE_BITS (TXX9_TIMER_BITS - 1)
  34. static struct txx9_clocksource txx9_clocksource = {
  35. .cs = {
  36. .name = "TXx9",
  37. .rating = 200,
  38. .read = txx9_cs_read,
  39. .mask = CLOCKSOURCE_MASK(TXX9_CLOCKSOURCE_BITS),
  40. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  41. },
  42. };
  43. void __init txx9_clocksource_init(unsigned long baseaddr,
  44. unsigned int imbusclk)
  45. {
  46. struct txx9_tmr_reg __iomem *tmrptr;
  47. clocksource_set_clock(&txx9_clocksource.cs, TIMER_CLK(imbusclk));
  48. clocksource_register(&txx9_clocksource.cs);
  49. tmrptr = ioremap(baseaddr, sizeof(struct txx9_tmr_reg));
  50. __raw_writel(TCR_BASE, &tmrptr->tcr);
  51. __raw_writel(0, &tmrptr->tisr);
  52. __raw_writel(TIMER_CCD, &tmrptr->ccdr);
  53. __raw_writel(TXx9_TMITMR_TZCE, &tmrptr->itmr);
  54. __raw_writel(1 << TXX9_CLOCKSOURCE_BITS, &tmrptr->cpra);
  55. __raw_writel(TCR_BASE | TXx9_TMTCR_TCE, &tmrptr->tcr);
  56. txx9_clocksource.tmrptr = tmrptr;
  57. }
  58. struct txx9_clock_event_device {
  59. struct clock_event_device cd;
  60. struct txx9_tmr_reg __iomem *tmrptr;
  61. };
  62. static void txx9tmr_stop_and_clear(struct txx9_tmr_reg __iomem *tmrptr)
  63. {
  64. /* stop and reset counter */
  65. __raw_writel(TCR_BASE, &tmrptr->tcr);
  66. /* clear pending interrupt */
  67. __raw_writel(0, &tmrptr->tisr);
  68. }
  69. static void txx9tmr_set_mode(enum clock_event_mode mode,
  70. struct clock_event_device *evt)
  71. {
  72. struct txx9_clock_event_device *txx9_cd =
  73. container_of(evt, struct txx9_clock_event_device, cd);
  74. struct txx9_tmr_reg __iomem *tmrptr = txx9_cd->tmrptr;
  75. txx9tmr_stop_and_clear(tmrptr);
  76. switch (mode) {
  77. case CLOCK_EVT_MODE_PERIODIC:
  78. __raw_writel(TXx9_TMITMR_TIIE | TXx9_TMITMR_TZCE,
  79. &tmrptr->itmr);
  80. /* start timer */
  81. __raw_writel(((u64)(NSEC_PER_SEC / HZ) * evt->mult) >>
  82. evt->shift,
  83. &tmrptr->cpra);
  84. __raw_writel(TCR_BASE | TXx9_TMTCR_TCE, &tmrptr->tcr);
  85. break;
  86. case CLOCK_EVT_MODE_SHUTDOWN:
  87. case CLOCK_EVT_MODE_UNUSED:
  88. __raw_writel(0, &tmrptr->itmr);
  89. break;
  90. case CLOCK_EVT_MODE_ONESHOT:
  91. __raw_writel(TXx9_TMITMR_TIIE, &tmrptr->itmr);
  92. break;
  93. case CLOCK_EVT_MODE_RESUME:
  94. __raw_writel(TIMER_CCD, &tmrptr->ccdr);
  95. __raw_writel(0, &tmrptr->itmr);
  96. break;
  97. }
  98. }
  99. static int txx9tmr_set_next_event(unsigned long delta,
  100. struct clock_event_device *evt)
  101. {
  102. struct txx9_clock_event_device *txx9_cd =
  103. container_of(evt, struct txx9_clock_event_device, cd);
  104. struct txx9_tmr_reg __iomem *tmrptr = txx9_cd->tmrptr;
  105. txx9tmr_stop_and_clear(tmrptr);
  106. /* start timer */
  107. __raw_writel(delta, &tmrptr->cpra);
  108. __raw_writel(TCR_BASE | TXx9_TMTCR_TCE, &tmrptr->tcr);
  109. return 0;
  110. }
  111. static struct txx9_clock_event_device txx9_clock_event_device = {
  112. .cd = {
  113. .name = "TXx9",
  114. .features = CLOCK_EVT_FEAT_PERIODIC |
  115. CLOCK_EVT_FEAT_ONESHOT,
  116. .rating = 200,
  117. .set_mode = txx9tmr_set_mode,
  118. .set_next_event = txx9tmr_set_next_event,
  119. },
  120. };
  121. static irqreturn_t txx9tmr_interrupt(int irq, void *dev_id)
  122. {
  123. struct txx9_clock_event_device *txx9_cd = dev_id;
  124. struct clock_event_device *cd = &txx9_cd->cd;
  125. struct txx9_tmr_reg __iomem *tmrptr = txx9_cd->tmrptr;
  126. __raw_writel(0, &tmrptr->tisr); /* ack interrupt */
  127. cd->event_handler(cd);
  128. return IRQ_HANDLED;
  129. }
  130. static struct irqaction txx9tmr_irq = {
  131. .handler = txx9tmr_interrupt,
  132. .flags = IRQF_DISABLED | IRQF_PERCPU | IRQF_TIMER,
  133. .name = "txx9tmr",
  134. .dev_id = &txx9_clock_event_device,
  135. };
  136. void __init txx9_clockevent_init(unsigned long baseaddr, int irq,
  137. unsigned int imbusclk)
  138. {
  139. struct clock_event_device *cd = &txx9_clock_event_device.cd;
  140. struct txx9_tmr_reg __iomem *tmrptr;
  141. tmrptr = ioremap(baseaddr, sizeof(struct txx9_tmr_reg));
  142. txx9tmr_stop_and_clear(tmrptr);
  143. __raw_writel(TIMER_CCD, &tmrptr->ccdr);
  144. __raw_writel(0, &tmrptr->itmr);
  145. txx9_clock_event_device.tmrptr = tmrptr;
  146. clockevent_set_clock(cd, TIMER_CLK(imbusclk));
  147. cd->max_delta_ns =
  148. clockevent_delta2ns(0xffffffff >> (32 - TXX9_TIMER_BITS), cd);
  149. cd->min_delta_ns = clockevent_delta2ns(0xf, cd);
  150. cd->irq = irq;
  151. cd->cpumask = cpumask_of(0),
  152. clockevents_register_device(cd);
  153. setup_irq(irq, &txx9tmr_irq);
  154. printk(KERN_INFO "TXx9: clockevent device at 0x%lx, irq %d\n",
  155. baseaddr, irq);
  156. }
  157. void __init txx9_tmr_init(unsigned long baseaddr)
  158. {
  159. struct txx9_tmr_reg __iomem *tmrptr;
  160. tmrptr = ioremap(baseaddr, sizeof(struct txx9_tmr_reg));
  161. /* Start once to make CounterResetEnable effective */
  162. __raw_writel(TXx9_TMTCR_CRE | TXx9_TMTCR_TCE, &tmrptr->tcr);
  163. /* Stop and reset the counter */
  164. __raw_writel(TXx9_TMTCR_CRE, &tmrptr->tcr);
  165. __raw_writel(0, &tmrptr->tisr);
  166. __raw_writel(0xffffffff, &tmrptr->cpra);
  167. __raw_writel(0, &tmrptr->itmr);
  168. __raw_writel(0, &tmrptr->ccdr);
  169. __raw_writel(0, &tmrptr->pgmr);
  170. iounmap(tmrptr);
  171. }