time.c 6.4 KB

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