cevt-txx9.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <linux/sched_clock.h>
  18. #include <asm/time.h>
  19. #include <asm/txx9tmr.h>
  20. #define TCR_BASE (TXx9_TMTCR_CCDE | TXx9_TMTCR_CRE | TXx9_TMTCR_TMODE_ITVL)
  21. #define TIMER_CCD 0 /* 1/2 */
  22. #define TIMER_CLK(imclk) ((imclk) / (2 << TIMER_CCD))
  23. struct txx9_clocksource {
  24. struct clocksource cs;
  25. struct txx9_tmr_reg __iomem *tmrptr;
  26. };
  27. static cycle_t txx9_cs_read(struct clocksource *cs)
  28. {
  29. struct txx9_clocksource *txx9_cs =
  30. container_of(cs, struct txx9_clocksource, cs);
  31. return __raw_readl(&txx9_cs->tmrptr->trr);
  32. }
  33. /* Use 1 bit smaller width to use full bits in that width */
  34. #define TXX9_CLOCKSOURCE_BITS (TXX9_TIMER_BITS - 1)
  35. static struct txx9_clocksource txx9_clocksource = {
  36. .cs = {
  37. .name = "TXx9",
  38. .rating = 200,
  39. .read = txx9_cs_read,
  40. .mask = CLOCKSOURCE_MASK(TXX9_CLOCKSOURCE_BITS),
  41. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  42. },
  43. };
  44. static u64 notrace txx9_read_sched_clock(void)
  45. {
  46. return __raw_readl(&txx9_clocksource.tmrptr->trr);
  47. }
  48. void __init txx9_clocksource_init(unsigned long baseaddr,
  49. unsigned int imbusclk)
  50. {
  51. struct txx9_tmr_reg __iomem *tmrptr;
  52. clocksource_register_hz(&txx9_clocksource.cs, TIMER_CLK(imbusclk));
  53. tmrptr = ioremap(baseaddr, sizeof(struct txx9_tmr_reg));
  54. __raw_writel(TCR_BASE, &tmrptr->tcr);
  55. __raw_writel(0, &tmrptr->tisr);
  56. __raw_writel(TIMER_CCD, &tmrptr->ccdr);
  57. __raw_writel(TXx9_TMITMR_TZCE, &tmrptr->itmr);
  58. __raw_writel(1 << TXX9_CLOCKSOURCE_BITS, &tmrptr->cpra);
  59. __raw_writel(TCR_BASE | TXx9_TMTCR_TCE, &tmrptr->tcr);
  60. txx9_clocksource.tmrptr = tmrptr;
  61. sched_clock_register(txx9_read_sched_clock, TXX9_CLOCKSOURCE_BITS,
  62. TIMER_CLK(imbusclk));
  63. }
  64. struct txx9_clock_event_device {
  65. struct clock_event_device cd;
  66. struct txx9_tmr_reg __iomem *tmrptr;
  67. };
  68. static void txx9tmr_stop_and_clear(struct txx9_tmr_reg __iomem *tmrptr)
  69. {
  70. /* stop and reset counter */
  71. __raw_writel(TCR_BASE, &tmrptr->tcr);
  72. /* clear pending interrupt */
  73. __raw_writel(0, &tmrptr->tisr);
  74. }
  75. static int txx9tmr_set_state_periodic(struct clock_event_device *evt)
  76. {
  77. struct txx9_clock_event_device *txx9_cd =
  78. container_of(evt, struct txx9_clock_event_device, cd);
  79. struct txx9_tmr_reg __iomem *tmrptr = txx9_cd->tmrptr;
  80. txx9tmr_stop_and_clear(tmrptr);
  81. __raw_writel(TXx9_TMITMR_TIIE | TXx9_TMITMR_TZCE, &tmrptr->itmr);
  82. /* start timer */
  83. __raw_writel(((u64)(NSEC_PER_SEC / HZ) * evt->mult) >> evt->shift,
  84. &tmrptr->cpra);
  85. __raw_writel(TCR_BASE | TXx9_TMTCR_TCE, &tmrptr->tcr);
  86. return 0;
  87. }
  88. static int txx9tmr_set_state_oneshot(struct clock_event_device *evt)
  89. {
  90. struct txx9_clock_event_device *txx9_cd =
  91. container_of(evt, struct txx9_clock_event_device, cd);
  92. struct txx9_tmr_reg __iomem *tmrptr = txx9_cd->tmrptr;
  93. txx9tmr_stop_and_clear(tmrptr);
  94. __raw_writel(TXx9_TMITMR_TIIE, &tmrptr->itmr);
  95. return 0;
  96. }
  97. static int txx9tmr_set_state_shutdown(struct clock_event_device *evt)
  98. {
  99. struct txx9_clock_event_device *txx9_cd =
  100. container_of(evt, struct txx9_clock_event_device, cd);
  101. struct txx9_tmr_reg __iomem *tmrptr = txx9_cd->tmrptr;
  102. txx9tmr_stop_and_clear(tmrptr);
  103. __raw_writel(0, &tmrptr->itmr);
  104. return 0;
  105. }
  106. static int txx9tmr_tick_resume(struct clock_event_device *evt)
  107. {
  108. struct txx9_clock_event_device *txx9_cd =
  109. container_of(evt, struct txx9_clock_event_device, cd);
  110. struct txx9_tmr_reg __iomem *tmrptr = txx9_cd->tmrptr;
  111. txx9tmr_stop_and_clear(tmrptr);
  112. __raw_writel(TIMER_CCD, &tmrptr->ccdr);
  113. __raw_writel(0, &tmrptr->itmr);
  114. return 0;
  115. }
  116. static int txx9tmr_set_next_event(unsigned long delta,
  117. struct clock_event_device *evt)
  118. {
  119. struct txx9_clock_event_device *txx9_cd =
  120. container_of(evt, struct txx9_clock_event_device, cd);
  121. struct txx9_tmr_reg __iomem *tmrptr = txx9_cd->tmrptr;
  122. txx9tmr_stop_and_clear(tmrptr);
  123. /* start timer */
  124. __raw_writel(delta, &tmrptr->cpra);
  125. __raw_writel(TCR_BASE | TXx9_TMTCR_TCE, &tmrptr->tcr);
  126. return 0;
  127. }
  128. static struct txx9_clock_event_device txx9_clock_event_device = {
  129. .cd = {
  130. .name = "TXx9",
  131. .features = CLOCK_EVT_FEAT_PERIODIC |
  132. CLOCK_EVT_FEAT_ONESHOT,
  133. .rating = 200,
  134. .set_state_shutdown = txx9tmr_set_state_shutdown,
  135. .set_state_periodic = txx9tmr_set_state_periodic,
  136. .set_state_oneshot = txx9tmr_set_state_oneshot,
  137. .tick_resume = txx9tmr_tick_resume,
  138. .set_next_event = txx9tmr_set_next_event,
  139. },
  140. };
  141. static irqreturn_t txx9tmr_interrupt(int irq, void *dev_id)
  142. {
  143. struct txx9_clock_event_device *txx9_cd = dev_id;
  144. struct clock_event_device *cd = &txx9_cd->cd;
  145. struct txx9_tmr_reg __iomem *tmrptr = txx9_cd->tmrptr;
  146. __raw_writel(0, &tmrptr->tisr); /* ack interrupt */
  147. cd->event_handler(cd);
  148. return IRQ_HANDLED;
  149. }
  150. static struct irqaction txx9tmr_irq = {
  151. .handler = txx9tmr_interrupt,
  152. .flags = IRQF_PERCPU | IRQF_TIMER,
  153. .name = "txx9tmr",
  154. .dev_id = &txx9_clock_event_device,
  155. };
  156. void __init txx9_clockevent_init(unsigned long baseaddr, int irq,
  157. unsigned int imbusclk)
  158. {
  159. struct clock_event_device *cd = &txx9_clock_event_device.cd;
  160. struct txx9_tmr_reg __iomem *tmrptr;
  161. tmrptr = ioremap(baseaddr, sizeof(struct txx9_tmr_reg));
  162. txx9tmr_stop_and_clear(tmrptr);
  163. __raw_writel(TIMER_CCD, &tmrptr->ccdr);
  164. __raw_writel(0, &tmrptr->itmr);
  165. txx9_clock_event_device.tmrptr = tmrptr;
  166. clockevent_set_clock(cd, TIMER_CLK(imbusclk));
  167. cd->max_delta_ns =
  168. clockevent_delta2ns(0xffffffff >> (32 - TXX9_TIMER_BITS), cd);
  169. cd->min_delta_ns = clockevent_delta2ns(0xf, cd);
  170. cd->irq = irq;
  171. cd->cpumask = cpumask_of(0),
  172. clockevents_register_device(cd);
  173. setup_irq(irq, &txx9tmr_irq);
  174. printk(KERN_INFO "TXx9: clockevent device at 0x%lx, irq %d\n",
  175. baseaddr, irq);
  176. }
  177. void __init txx9_tmr_init(unsigned long baseaddr)
  178. {
  179. struct txx9_tmr_reg __iomem *tmrptr;
  180. tmrptr = ioremap(baseaddr, sizeof(struct txx9_tmr_reg));
  181. /* Start once to make CounterResetEnable effective */
  182. __raw_writel(TXx9_TMTCR_CRE | TXx9_TMTCR_TCE, &tmrptr->tcr);
  183. /* Stop and reset the counter */
  184. __raw_writel(TXx9_TMTCR_CRE, &tmrptr->tcr);
  185. __raw_writel(0, &tmrptr->tisr);
  186. __raw_writel(0xffffffff, &tmrptr->cpra);
  187. __raw_writel(0, &tmrptr->itmr);
  188. __raw_writel(0, &tmrptr->ccdr);
  189. __raw_writel(0, &tmrptr->pgmr);
  190. iounmap(tmrptr);
  191. }