time.c 5.9 KB

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