timer.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. *
  3. * Copyright (C) 2007 Google, Inc.
  4. * Copyright (c) 2009-2012, The Linux Foundation. All rights reserved.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  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. */
  16. #include <linux/clocksource.h>
  17. #include <linux/clockchips.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/io.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #include <asm/mach/time.h>
  26. #include <asm/localtimer.h>
  27. #include <asm/sched_clock.h>
  28. #include "common.h"
  29. #define TIMER_MATCH_VAL 0x0000
  30. #define TIMER_COUNT_VAL 0x0004
  31. #define TIMER_ENABLE 0x0008
  32. #define TIMER_ENABLE_CLR_ON_MATCH_EN BIT(1)
  33. #define TIMER_ENABLE_EN BIT(0)
  34. #define TIMER_CLEAR 0x000C
  35. #define DGT_CLK_CTL 0x10
  36. #define DGT_CLK_CTL_DIV_4 0x3
  37. #define TIMER_STS_GPT0_CLR_PEND BIT(10)
  38. #define GPT_HZ 32768
  39. #define MSM_DGT_SHIFT 5
  40. static void __iomem *event_base;
  41. static void __iomem *sts_base;
  42. static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
  43. {
  44. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  45. /* Stop the timer tick */
  46. if (evt->mode == CLOCK_EVT_MODE_ONESHOT) {
  47. u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  48. ctrl &= ~TIMER_ENABLE_EN;
  49. writel_relaxed(ctrl, event_base + TIMER_ENABLE);
  50. }
  51. evt->event_handler(evt);
  52. return IRQ_HANDLED;
  53. }
  54. static int msm_timer_set_next_event(unsigned long cycles,
  55. struct clock_event_device *evt)
  56. {
  57. u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  58. writel_relaxed(0, event_base + TIMER_CLEAR);
  59. writel_relaxed(cycles, event_base + TIMER_MATCH_VAL);
  60. if (sts_base)
  61. while (readl_relaxed(sts_base) & TIMER_STS_GPT0_CLR_PEND)
  62. cpu_relax();
  63. writel_relaxed(ctrl | TIMER_ENABLE_EN, event_base + TIMER_ENABLE);
  64. return 0;
  65. }
  66. static void msm_timer_set_mode(enum clock_event_mode mode,
  67. struct clock_event_device *evt)
  68. {
  69. u32 ctrl;
  70. ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  71. ctrl &= ~(TIMER_ENABLE_EN | TIMER_ENABLE_CLR_ON_MATCH_EN);
  72. switch (mode) {
  73. case CLOCK_EVT_MODE_RESUME:
  74. case CLOCK_EVT_MODE_PERIODIC:
  75. break;
  76. case CLOCK_EVT_MODE_ONESHOT:
  77. /* Timer is enabled in set_next_event */
  78. break;
  79. case CLOCK_EVT_MODE_UNUSED:
  80. case CLOCK_EVT_MODE_SHUTDOWN:
  81. break;
  82. }
  83. writel_relaxed(ctrl, event_base + TIMER_ENABLE);
  84. }
  85. static struct clock_event_device msm_clockevent = {
  86. .name = "gp_timer",
  87. .features = CLOCK_EVT_FEAT_ONESHOT,
  88. .rating = 200,
  89. .set_next_event = msm_timer_set_next_event,
  90. .set_mode = msm_timer_set_mode,
  91. };
  92. static union {
  93. struct clock_event_device *evt;
  94. struct clock_event_device * __percpu *percpu_evt;
  95. } msm_evt;
  96. static void __iomem *source_base;
  97. static notrace cycle_t msm_read_timer_count(struct clocksource *cs)
  98. {
  99. return readl_relaxed(source_base + TIMER_COUNT_VAL);
  100. }
  101. static notrace cycle_t msm_read_timer_count_shift(struct clocksource *cs)
  102. {
  103. /*
  104. * Shift timer count down by a constant due to unreliable lower bits
  105. * on some targets.
  106. */
  107. return msm_read_timer_count(cs) >> MSM_DGT_SHIFT;
  108. }
  109. static struct clocksource msm_clocksource = {
  110. .name = "dg_timer",
  111. .rating = 300,
  112. .read = msm_read_timer_count,
  113. .mask = CLOCKSOURCE_MASK(32),
  114. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  115. };
  116. #ifdef CONFIG_LOCAL_TIMERS
  117. static int __cpuinit msm_local_timer_setup(struct clock_event_device *evt)
  118. {
  119. /* Use existing clock_event for cpu 0 */
  120. if (!smp_processor_id())
  121. return 0;
  122. evt->irq = msm_clockevent.irq;
  123. evt->name = "local_timer";
  124. evt->features = msm_clockevent.features;
  125. evt->rating = msm_clockevent.rating;
  126. evt->set_mode = msm_timer_set_mode;
  127. evt->set_next_event = msm_timer_set_next_event;
  128. *__this_cpu_ptr(msm_evt.percpu_evt) = evt;
  129. clockevents_config_and_register(evt, GPT_HZ, 4, 0xf0000000);
  130. enable_percpu_irq(evt->irq, IRQ_TYPE_EDGE_RISING);
  131. return 0;
  132. }
  133. static void msm_local_timer_stop(struct clock_event_device *evt)
  134. {
  135. evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
  136. disable_percpu_irq(evt->irq);
  137. }
  138. static struct local_timer_ops msm_local_timer_ops __cpuinitdata = {
  139. .setup = msm_local_timer_setup,
  140. .stop = msm_local_timer_stop,
  141. };
  142. #endif /* CONFIG_LOCAL_TIMERS */
  143. static notrace u32 msm_sched_clock_read(void)
  144. {
  145. return msm_clocksource.read(&msm_clocksource);
  146. }
  147. static void __init msm_timer_init(u32 dgt_hz, int sched_bits, int irq,
  148. bool percpu)
  149. {
  150. struct clock_event_device *ce = &msm_clockevent;
  151. struct clocksource *cs = &msm_clocksource;
  152. int res;
  153. ce->cpumask = cpumask_of(0);
  154. ce->irq = irq;
  155. clockevents_config_and_register(ce, GPT_HZ, 4, 0xffffffff);
  156. if (percpu) {
  157. msm_evt.percpu_evt = alloc_percpu(struct clock_event_device *);
  158. if (!msm_evt.percpu_evt) {
  159. pr_err("memory allocation failed for %s\n", ce->name);
  160. goto err;
  161. }
  162. *__this_cpu_ptr(msm_evt.percpu_evt) = ce;
  163. res = request_percpu_irq(ce->irq, msm_timer_interrupt,
  164. ce->name, msm_evt.percpu_evt);
  165. if (!res) {
  166. enable_percpu_irq(ce->irq, IRQ_TYPE_EDGE_RISING);
  167. #ifdef CONFIG_LOCAL_TIMERS
  168. local_timer_register(&msm_local_timer_ops);
  169. #endif
  170. }
  171. } else {
  172. msm_evt.evt = ce;
  173. res = request_irq(ce->irq, msm_timer_interrupt,
  174. IRQF_TIMER | IRQF_NOBALANCING |
  175. IRQF_TRIGGER_RISING, ce->name, &msm_evt.evt);
  176. }
  177. if (res)
  178. pr_err("request_irq failed for %s\n", ce->name);
  179. err:
  180. writel_relaxed(TIMER_ENABLE_EN, source_base + TIMER_ENABLE);
  181. res = clocksource_register_hz(cs, dgt_hz);
  182. if (res)
  183. pr_err("clocksource_register failed\n");
  184. setup_sched_clock(msm_sched_clock_read, sched_bits, dgt_hz);
  185. }
  186. #ifdef CONFIG_OF
  187. static const struct of_device_id msm_timer_match[] __initconst = {
  188. { .compatible = "qcom,kpss-timer" },
  189. { .compatible = "qcom,scss-timer" },
  190. { },
  191. };
  192. void __init msm_dt_timer_init(void)
  193. {
  194. struct device_node *np;
  195. u32 freq;
  196. int irq;
  197. struct resource res;
  198. u32 percpu_offset;
  199. void __iomem *base;
  200. void __iomem *cpu0_base;
  201. np = of_find_matching_node(NULL, msm_timer_match);
  202. if (!np) {
  203. pr_err("Can't find msm timer DT node\n");
  204. return;
  205. }
  206. base = of_iomap(np, 0);
  207. if (!base) {
  208. pr_err("Failed to map event base\n");
  209. return;
  210. }
  211. /* We use GPT0 for the clockevent */
  212. irq = irq_of_parse_and_map(np, 1);
  213. if (irq <= 0) {
  214. pr_err("Can't get irq\n");
  215. return;
  216. }
  217. /* We use CPU0's DGT for the clocksource */
  218. if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
  219. percpu_offset = 0;
  220. if (of_address_to_resource(np, 0, &res)) {
  221. pr_err("Failed to parse DGT resource\n");
  222. return;
  223. }
  224. cpu0_base = ioremap(res.start + percpu_offset, resource_size(&res));
  225. if (!cpu0_base) {
  226. pr_err("Failed to map source base\n");
  227. return;
  228. }
  229. if (of_property_read_u32(np, "clock-frequency", &freq)) {
  230. pr_err("Unknown frequency\n");
  231. return;
  232. }
  233. of_node_put(np);
  234. event_base = base + 0x4;
  235. sts_base = base + 0x88;
  236. source_base = cpu0_base + 0x24;
  237. freq /= 4;
  238. writel_relaxed(DGT_CLK_CTL_DIV_4, source_base + DGT_CLK_CTL);
  239. msm_timer_init(freq, 32, irq, !!percpu_offset);
  240. }
  241. #endif
  242. static int __init msm_timer_map(phys_addr_t addr, u32 event, u32 source,
  243. u32 sts)
  244. {
  245. void __iomem *base;
  246. base = ioremap(addr, SZ_256);
  247. if (!base) {
  248. pr_err("Failed to map timer base\n");
  249. return -ENOMEM;
  250. }
  251. event_base = base + event;
  252. source_base = base + source;
  253. if (sts)
  254. sts_base = base + sts;
  255. return 0;
  256. }
  257. void __init msm7x01_timer_init(void)
  258. {
  259. struct clocksource *cs = &msm_clocksource;
  260. if (msm_timer_map(0xc0100000, 0x0, 0x10, 0x0))
  261. return;
  262. cs->read = msm_read_timer_count_shift;
  263. cs->mask = CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT));
  264. /* 600 KHz */
  265. msm_timer_init(19200000 >> MSM_DGT_SHIFT, 32 - MSM_DGT_SHIFT, 7,
  266. false);
  267. }
  268. void __init msm7x30_timer_init(void)
  269. {
  270. if (msm_timer_map(0xc0100000, 0x4, 0x24, 0x80))
  271. return;
  272. msm_timer_init(24576000 / 4, 32, 1, false);
  273. }
  274. void __init qsd8x50_timer_init(void)
  275. {
  276. if (msm_timer_map(0xAC100000, 0x0, 0x10, 0x34))
  277. return;
  278. msm_timer_init(19200000 / 4, 32, 7, false);
  279. }