time.c 5.9 KB

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