time.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Time related functions for Hexagon architecture
  3. *
  4. * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/clockchips.h>
  22. #include <linux/clocksource.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/err.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/ioport.h>
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/module.h>
  31. #include <asm/timer-regs.h>
  32. #include <asm/hexagon_vm.h>
  33. /*
  34. * For the clocksource we need:
  35. * pcycle frequency (600MHz)
  36. * For the loops_per_jiffy we need:
  37. * thread/cpu frequency (100MHz)
  38. * And for the timer, we need:
  39. * sleep clock rate
  40. */
  41. cycles_t pcycle_freq_mhz;
  42. cycles_t thread_freq_mhz;
  43. cycles_t sleep_clk_freq;
  44. static struct resource rtos_timer_resources[] = {
  45. {
  46. .start = RTOS_TIMER_REGS_ADDR,
  47. .end = RTOS_TIMER_REGS_ADDR+PAGE_SIZE-1,
  48. .flags = IORESOURCE_MEM,
  49. },
  50. };
  51. static struct platform_device rtos_timer_device = {
  52. .name = "rtos_timer",
  53. .id = -1,
  54. .num_resources = ARRAY_SIZE(rtos_timer_resources),
  55. .resource = rtos_timer_resources,
  56. };
  57. /* A lot of this stuff should move into a platform specific section. */
  58. struct adsp_hw_timer_struct {
  59. u32 match; /* Match value */
  60. u32 count;
  61. u32 enable; /* [1] - CLR_ON_MATCH_EN, [0] - EN */
  62. u32 clear; /* one-shot register that clears the count */
  63. };
  64. /* Look for "TCX0" for related constants. */
  65. static __iomem struct adsp_hw_timer_struct *rtos_timer;
  66. static cycle_t timer_get_cycles(struct clocksource *cs)
  67. {
  68. return (cycle_t) __vmgettime();
  69. }
  70. static struct clocksource hexagon_clocksource = {
  71. .name = "pcycles",
  72. .rating = 250,
  73. .read = timer_get_cycles,
  74. .mask = CLOCKSOURCE_MASK(64),
  75. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  76. };
  77. static int set_next_event(unsigned long delta, struct clock_event_device *evt)
  78. {
  79. /* Assuming the timer will be disabled when we enter here. */
  80. iowrite32(1, &rtos_timer->clear);
  81. iowrite32(0, &rtos_timer->clear);
  82. iowrite32(delta, &rtos_timer->match);
  83. iowrite32(1 << TIMER_ENABLE, &rtos_timer->enable);
  84. return 0;
  85. }
  86. #ifdef CONFIG_SMP
  87. /* Broadcast mechanism */
  88. static void broadcast(const struct cpumask *mask)
  89. {
  90. send_ipi(mask, IPI_TIMER);
  91. }
  92. #endif
  93. /* XXX Implement set_state_shutdown() */
  94. static struct clock_event_device hexagon_clockevent_dev = {
  95. .name = "clockevent",
  96. .features = CLOCK_EVT_FEAT_ONESHOT,
  97. .rating = 400,
  98. .irq = RTOS_TIMER_INT,
  99. .set_next_event = set_next_event,
  100. #ifdef CONFIG_SMP
  101. .broadcast = broadcast,
  102. #endif
  103. };
  104. #ifdef CONFIG_SMP
  105. static DEFINE_PER_CPU(struct clock_event_device, clock_events);
  106. void setup_percpu_clockdev(void)
  107. {
  108. int cpu = smp_processor_id();
  109. struct clock_event_device *ce_dev = &hexagon_clockevent_dev;
  110. struct clock_event_device *dummy_clock_dev =
  111. &per_cpu(clock_events, cpu);
  112. memcpy(dummy_clock_dev, ce_dev, sizeof(*dummy_clock_dev));
  113. INIT_LIST_HEAD(&dummy_clock_dev->list);
  114. dummy_clock_dev->features = CLOCK_EVT_FEAT_DUMMY;
  115. dummy_clock_dev->cpumask = cpumask_of(cpu);
  116. clockevents_register_device(dummy_clock_dev);
  117. }
  118. /* Called from smp.c for each CPU's timer ipi call */
  119. void ipi_timer(void)
  120. {
  121. int cpu = smp_processor_id();
  122. struct clock_event_device *ce_dev = &per_cpu(clock_events, cpu);
  123. ce_dev->event_handler(ce_dev);
  124. }
  125. #endif /* CONFIG_SMP */
  126. static irqreturn_t timer_interrupt(int irq, void *devid)
  127. {
  128. struct clock_event_device *ce_dev = &hexagon_clockevent_dev;
  129. iowrite32(0, &rtos_timer->enable);
  130. ce_dev->event_handler(ce_dev);
  131. return IRQ_HANDLED;
  132. }
  133. /* This should also be pulled from devtree */
  134. static struct irqaction rtos_timer_intdesc = {
  135. .handler = timer_interrupt,
  136. .flags = IRQF_TIMER | IRQF_TRIGGER_RISING,
  137. .name = "rtos_timer"
  138. };
  139. /*
  140. * time_init_deferred - called by start_kernel to set up timer/clock source
  141. *
  142. * Install the IRQ handler for the clock, setup timers.
  143. * This is done late, as that way, we can use ioremap().
  144. *
  145. * This runs just before the delay loop is calibrated, and
  146. * is used for delay calibration.
  147. */
  148. void __init time_init_deferred(void)
  149. {
  150. struct resource *resource = NULL;
  151. struct clock_event_device *ce_dev = &hexagon_clockevent_dev;
  152. ce_dev->cpumask = cpu_all_mask;
  153. if (!resource)
  154. resource = rtos_timer_device.resource;
  155. /* ioremap here means this has to run later, after paging init */
  156. rtos_timer = ioremap(resource->start, resource_size(resource));
  157. if (!rtos_timer) {
  158. release_mem_region(resource->start, resource_size(resource));
  159. }
  160. clocksource_register_khz(&hexagon_clocksource, pcycle_freq_mhz * 1000);
  161. /* Note: the sim generic RTOS clock is apparently really 18750Hz */
  162. /*
  163. * Last arg is some guaranteed seconds for which the conversion will
  164. * work without overflow.
  165. */
  166. clockevents_calc_mult_shift(ce_dev, sleep_clk_freq, 4);
  167. ce_dev->max_delta_ns = clockevent_delta2ns(0x7fffffff, ce_dev);
  168. ce_dev->min_delta_ns = clockevent_delta2ns(0xf, ce_dev);
  169. #ifdef CONFIG_SMP
  170. setup_percpu_clockdev();
  171. #endif
  172. clockevents_register_device(ce_dev);
  173. setup_irq(ce_dev->irq, &rtos_timer_intdesc);
  174. }
  175. void __init time_init(void)
  176. {
  177. late_time_init = time_init_deferred;
  178. }
  179. void __delay(unsigned long cycles)
  180. {
  181. unsigned long long start = __vmgettime();
  182. while ((__vmgettime() - start) < cycles)
  183. cpu_relax();
  184. }
  185. EXPORT_SYMBOL(__delay);
  186. /*
  187. * This could become parametric or perhaps even computed at run-time,
  188. * but for now we take the observed simulator jitter.
  189. */
  190. static long long fudgefactor = 350; /* Maybe lower if kernel optimized. */
  191. void __udelay(unsigned long usecs)
  192. {
  193. unsigned long long start = __vmgettime();
  194. unsigned long long finish = (pcycle_freq_mhz * usecs) - fudgefactor;
  195. while ((__vmgettime() - start) < finish)
  196. cpu_relax(); /* not sure how this improves readability */
  197. }
  198. EXPORT_SYMBOL(__udelay);