time-tc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (C) 2004-2007 Atmel Corporation
  3. *
  4. * Based on MIPS implementation arch/mips/kernel/time.c
  5. * Copyright 2001 MontaVista Software Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/clocksource.h>
  13. #include <linux/time.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/kernel_stat.h>
  18. #include <linux/errno.h>
  19. #include <linux/init.h>
  20. #include <linux/profile.h>
  21. #include <linux/sysdev.h>
  22. #include <linux/err.h>
  23. #include <asm/div64.h>
  24. #include <asm/sysreg.h>
  25. #include <asm/io.h>
  26. #include <asm/sections.h>
  27. #include <asm/arch/time.h>
  28. /* how many counter cycles in a jiffy? */
  29. static u32 cycles_per_jiffy;
  30. /* the count value for the next timer interrupt */
  31. static u32 expirelo;
  32. /* the I/O registers of the TC module */
  33. static void __iomem *ioregs;
  34. cycle_t read_cycle_count(void)
  35. {
  36. return (cycle_t)timer_read(ioregs, 0, CV);
  37. }
  38. struct clocksource clocksource_avr32 = {
  39. .name = "avr32",
  40. .rating = 342,
  41. .read = read_cycle_count,
  42. .mask = CLOCKSOURCE_MASK(16),
  43. .shift = 16,
  44. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  45. };
  46. static void avr32_timer_ack(void)
  47. {
  48. u16 count = expirelo;
  49. /* Ack this timer interrupt and set the next one, use a u16
  50. * variable so it will wrap around correctly */
  51. count += cycles_per_jiffy;
  52. expirelo = count;
  53. timer_write(ioregs, 0, RC, expirelo);
  54. /* Check to see if we have missed any timer interrupts */
  55. count = timer_read(ioregs, 0, CV);
  56. if ((count - expirelo) < 0x7fff) {
  57. expirelo = count + cycles_per_jiffy;
  58. timer_write(ioregs, 0, RC, expirelo);
  59. }
  60. }
  61. u32 avr32_hpt_read(void)
  62. {
  63. return timer_read(ioregs, 0, CV);
  64. }
  65. static int avr32_timer_calc_div_and_set_jiffies(struct clk *pclk)
  66. {
  67. unsigned int cycles_max = (clocksource_avr32.mask + 1) / 2;
  68. unsigned int divs[] = { 4, 8, 16, 32 };
  69. int divs_size = sizeof(divs) / sizeof(*divs);
  70. int i = 0;
  71. unsigned long count_hz;
  72. unsigned long shift;
  73. unsigned long mult;
  74. int clock_div = -1;
  75. u64 tmp;
  76. shift = clocksource_avr32.shift;
  77. do {
  78. count_hz = clk_get_rate(pclk) / divs[i];
  79. mult = clocksource_hz2mult(count_hz, shift);
  80. clocksource_avr32.mult = mult;
  81. tmp = TICK_NSEC;
  82. tmp <<= shift;
  83. tmp += mult / 2;
  84. do_div(tmp, mult);
  85. cycles_per_jiffy = tmp;
  86. } while (cycles_per_jiffy > cycles_max && ++i < divs_size);
  87. clock_div = i + 1;
  88. if (clock_div > divs_size) {
  89. pr_debug("timer: could not calculate clock divider\n");
  90. return -EFAULT;
  91. }
  92. /* Set the clock divider */
  93. timer_write(ioregs, 0, CMR, TIMER_BF(CMR_TCCLKS, clock_div));
  94. return 0;
  95. }
  96. int avr32_hpt_init(unsigned int count)
  97. {
  98. struct resource *regs;
  99. struct clk *pclk;
  100. int irq = -1;
  101. int ret = 0;
  102. ret = -ENXIO;
  103. irq = platform_get_irq(&at32_systc0_device, 0);
  104. if (irq < 0) {
  105. pr_debug("timer: could not get irq\n");
  106. goto out_error;
  107. }
  108. pclk = clk_get(&at32_systc0_device.dev, "pclk");
  109. if (IS_ERR(pclk)) {
  110. pr_debug("timer: could not get clk: %ld\n", PTR_ERR(pclk));
  111. goto out_error;
  112. }
  113. clk_enable(pclk);
  114. regs = platform_get_resource(&at32_systc0_device, IORESOURCE_MEM, 0);
  115. if (!regs) {
  116. pr_debug("timer: could not get resource\n");
  117. goto out_error_clk;
  118. }
  119. ioregs = ioremap(regs->start, regs->end - regs->start + 1);
  120. if (!ioregs) {
  121. pr_debug("timer: could not get ioregs\n");
  122. goto out_error_clk;
  123. }
  124. ret = avr32_timer_calc_div_and_set_jiffies(pclk);
  125. if (ret)
  126. goto out_error_io;
  127. ret = setup_irq(irq, &timer_irqaction);
  128. if (ret) {
  129. pr_debug("timer: could not request irq %d: %d\n",
  130. irq, ret);
  131. goto out_error_io;
  132. }
  133. expirelo = (timer_read(ioregs, 0, CV) / cycles_per_jiffy + 1)
  134. * cycles_per_jiffy;
  135. /* Enable clock and interrupts on RC compare */
  136. timer_write(ioregs, 0, CCR, TIMER_BIT(CCR_CLKEN));
  137. timer_write(ioregs, 0, IER, TIMER_BIT(IER_CPCS));
  138. /* Set cycles to first interrupt */
  139. timer_write(ioregs, 0, RC, expirelo);
  140. printk(KERN_INFO "timer: AT32AP system timer/counter at 0x%p irq %d\n",
  141. ioregs, irq);
  142. return 0;
  143. out_error_io:
  144. iounmap(ioregs);
  145. out_error_clk:
  146. clk_put(pclk);
  147. out_error:
  148. return ret;
  149. }
  150. int avr32_hpt_start(void)
  151. {
  152. timer_write(ioregs, 0, CCR, TIMER_BIT(CCR_SWTRG));
  153. return 0;
  154. }
  155. irqreturn_t timer_interrupt(int irq, void *dev_id)
  156. {
  157. unsigned int sr = timer_read(ioregs, 0, SR);
  158. if (sr & TIMER_BIT(SR_CPCS)) {
  159. /* ack timer interrupt and try to set next interrupt */
  160. avr32_timer_ack();
  161. /*
  162. * Call the generic timer interrupt handler
  163. */
  164. write_seqlock(&xtime_lock);
  165. do_timer(1);
  166. write_sequnlock(&xtime_lock);
  167. /*
  168. * In UP mode, we call local_timer_interrupt() to do profiling
  169. * and process accounting.
  170. *
  171. * SMP is not supported yet.
  172. */
  173. local_timer_interrupt(irq, dev_id);
  174. return IRQ_HANDLED;
  175. }
  176. return IRQ_NONE;
  177. }