timer.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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/hardware/gic.h>
  27. #include <asm/localtimer.h>
  28. #include <asm/sched_clock.h>
  29. #include "common.h"
  30. #define TIMER_MATCH_VAL 0x0000
  31. #define TIMER_COUNT_VAL 0x0004
  32. #define TIMER_ENABLE 0x0008
  33. #define TIMER_ENABLE_CLR_ON_MATCH_EN BIT(1)
  34. #define TIMER_ENABLE_EN BIT(0)
  35. #define TIMER_CLEAR 0x000C
  36. #define DGT_CLK_CTL_DIV_4 0x3
  37. #define GPT_HZ 32768
  38. #define MSM_DGT_SHIFT 5
  39. static void __iomem *event_base;
  40. static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
  41. {
  42. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  43. /* Stop the timer tick */
  44. if (evt->mode == CLOCK_EVT_MODE_ONESHOT) {
  45. u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  46. ctrl &= ~TIMER_ENABLE_EN;
  47. writel_relaxed(ctrl, event_base + TIMER_ENABLE);
  48. }
  49. evt->event_handler(evt);
  50. return IRQ_HANDLED;
  51. }
  52. static int msm_timer_set_next_event(unsigned long cycles,
  53. struct clock_event_device *evt)
  54. {
  55. u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  56. writel_relaxed(0, event_base + TIMER_CLEAR);
  57. writel_relaxed(cycles, event_base + TIMER_MATCH_VAL);
  58. writel_relaxed(ctrl | TIMER_ENABLE_EN, event_base + TIMER_ENABLE);
  59. return 0;
  60. }
  61. static void msm_timer_set_mode(enum clock_event_mode mode,
  62. struct clock_event_device *evt)
  63. {
  64. u32 ctrl;
  65. ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  66. ctrl &= ~(TIMER_ENABLE_EN | TIMER_ENABLE_CLR_ON_MATCH_EN);
  67. switch (mode) {
  68. case CLOCK_EVT_MODE_RESUME:
  69. case CLOCK_EVT_MODE_PERIODIC:
  70. break;
  71. case CLOCK_EVT_MODE_ONESHOT:
  72. /* Timer is enabled in set_next_event */
  73. break;
  74. case CLOCK_EVT_MODE_UNUSED:
  75. case CLOCK_EVT_MODE_SHUTDOWN:
  76. break;
  77. }
  78. writel_relaxed(ctrl, event_base + TIMER_ENABLE);
  79. }
  80. static struct clock_event_device msm_clockevent = {
  81. .name = "gp_timer",
  82. .features = CLOCK_EVT_FEAT_ONESHOT,
  83. .rating = 200,
  84. .set_next_event = msm_timer_set_next_event,
  85. .set_mode = msm_timer_set_mode,
  86. };
  87. static union {
  88. struct clock_event_device *evt;
  89. struct clock_event_device * __percpu *percpu_evt;
  90. } msm_evt;
  91. static void __iomem *source_base;
  92. static notrace cycle_t msm_read_timer_count(struct clocksource *cs)
  93. {
  94. return readl_relaxed(source_base + TIMER_COUNT_VAL);
  95. }
  96. static notrace cycle_t msm_read_timer_count_shift(struct clocksource *cs)
  97. {
  98. /*
  99. * Shift timer count down by a constant due to unreliable lower bits
  100. * on some targets.
  101. */
  102. return msm_read_timer_count(cs) >> MSM_DGT_SHIFT;
  103. }
  104. static struct clocksource msm_clocksource = {
  105. .name = "dg_timer",
  106. .rating = 300,
  107. .read = msm_read_timer_count,
  108. .mask = CLOCKSOURCE_MASK(32),
  109. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  110. };
  111. #ifdef CONFIG_LOCAL_TIMERS
  112. static int __cpuinit msm_local_timer_setup(struct clock_event_device *evt)
  113. {
  114. /* Use existing clock_event for cpu 0 */
  115. if (!smp_processor_id())
  116. return 0;
  117. writel_relaxed(0, event_base + TIMER_ENABLE);
  118. writel_relaxed(0, event_base + TIMER_CLEAR);
  119. writel_relaxed(~0, event_base + TIMER_MATCH_VAL);
  120. evt->irq = msm_clockevent.irq;
  121. evt->name = "local_timer";
  122. evt->features = msm_clockevent.features;
  123. evt->rating = msm_clockevent.rating;
  124. evt->set_mode = msm_timer_set_mode;
  125. evt->set_next_event = msm_timer_set_next_event;
  126. evt->shift = msm_clockevent.shift;
  127. evt->mult = div_sc(GPT_HZ, NSEC_PER_SEC, evt->shift);
  128. evt->max_delta_ns = clockevent_delta2ns(0xf0000000, evt);
  129. evt->min_delta_ns = clockevent_delta2ns(4, evt);
  130. *__this_cpu_ptr(msm_evt.percpu_evt) = evt;
  131. clockevents_register_device(evt);
  132. enable_percpu_irq(evt->irq, IRQ_TYPE_EDGE_RISING);
  133. return 0;
  134. }
  135. static void msm_local_timer_stop(struct clock_event_device *evt)
  136. {
  137. evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
  138. disable_percpu_irq(evt->irq);
  139. }
  140. static struct local_timer_ops msm_local_timer_ops __cpuinitdata = {
  141. .setup = msm_local_timer_setup,
  142. .stop = msm_local_timer_stop,
  143. };
  144. #endif /* CONFIG_LOCAL_TIMERS */
  145. static notrace u32 msm_sched_clock_read(void)
  146. {
  147. return msm_clocksource.read(&msm_clocksource);
  148. }
  149. static void __init msm_timer_init(u32 dgt_hz, int sched_bits, int irq,
  150. bool percpu)
  151. {
  152. struct clock_event_device *ce = &msm_clockevent;
  153. struct clocksource *cs = &msm_clocksource;
  154. int res;
  155. writel_relaxed(0, event_base + TIMER_ENABLE);
  156. writel_relaxed(0, event_base + TIMER_CLEAR);
  157. writel_relaxed(~0, event_base + TIMER_MATCH_VAL);
  158. ce->cpumask = cpumask_of(0);
  159. ce->irq = irq;
  160. clockevents_config_and_register(ce, GPT_HZ, 4, 0xffffffff);
  161. if (percpu) {
  162. msm_evt.percpu_evt = alloc_percpu(struct clock_event_device *);
  163. if (!msm_evt.percpu_evt) {
  164. pr_err("memory allocation failed for %s\n", ce->name);
  165. goto err;
  166. }
  167. *__this_cpu_ptr(msm_evt.percpu_evt) = ce;
  168. res = request_percpu_irq(ce->irq, msm_timer_interrupt,
  169. ce->name, msm_evt.percpu_evt);
  170. if (!res) {
  171. enable_percpu_irq(ce->irq, IRQ_TYPE_EDGE_RISING);
  172. #ifdef CONFIG_LOCAL_TIMERS
  173. local_timer_register(&msm_local_timer_ops);
  174. #endif
  175. }
  176. } else {
  177. msm_evt.evt = ce;
  178. res = request_irq(ce->irq, msm_timer_interrupt,
  179. IRQF_TIMER | IRQF_NOBALANCING |
  180. IRQF_TRIGGER_RISING, ce->name, &msm_evt.evt);
  181. }
  182. if (res)
  183. pr_err("request_irq failed for %s\n", ce->name);
  184. err:
  185. writel_relaxed(TIMER_ENABLE_EN, source_base + TIMER_ENABLE);
  186. res = clocksource_register_hz(cs, dgt_hz);
  187. if (res)
  188. pr_err("clocksource_register failed\n");
  189. setup_sched_clock(msm_sched_clock_read, sched_bits, dgt_hz);
  190. }
  191. #ifdef CONFIG_OF
  192. static const struct of_device_id msm_dgt_match[] __initconst = {
  193. { .compatible = "qcom,msm-dgt" },
  194. { },
  195. };
  196. static const struct of_device_id msm_gpt_match[] __initconst = {
  197. { .compatible = "qcom,msm-gpt" },
  198. { },
  199. };
  200. static void __init msm_dt_timer_init(void)
  201. {
  202. struct device_node *np;
  203. u32 freq;
  204. int irq;
  205. struct resource res;
  206. u32 percpu_offset;
  207. void __iomem *dgt_clk_ctl;
  208. np = of_find_matching_node(NULL, msm_gpt_match);
  209. if (!np) {
  210. pr_err("Can't find GPT DT node\n");
  211. return;
  212. }
  213. event_base = of_iomap(np, 0);
  214. if (!event_base) {
  215. pr_err("Failed to map event base\n");
  216. return;
  217. }
  218. irq = irq_of_parse_and_map(np, 0);
  219. if (irq <= 0) {
  220. pr_err("Can't get irq\n");
  221. return;
  222. }
  223. of_node_put(np);
  224. np = of_find_matching_node(NULL, msm_dgt_match);
  225. if (!np) {
  226. pr_err("Can't find DGT DT node\n");
  227. return;
  228. }
  229. if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
  230. percpu_offset = 0;
  231. if (of_address_to_resource(np, 0, &res)) {
  232. pr_err("Failed to parse DGT resource\n");
  233. return;
  234. }
  235. source_base = ioremap(res.start + percpu_offset, resource_size(&res));
  236. if (!source_base) {
  237. pr_err("Failed to map source base\n");
  238. return;
  239. }
  240. if (!of_address_to_resource(np, 1, &res)) {
  241. dgt_clk_ctl = ioremap(res.start + percpu_offset,
  242. resource_size(&res));
  243. if (!dgt_clk_ctl) {
  244. pr_err("Failed to map DGT control base\n");
  245. return;
  246. }
  247. writel_relaxed(DGT_CLK_CTL_DIV_4, dgt_clk_ctl);
  248. iounmap(dgt_clk_ctl);
  249. }
  250. if (of_property_read_u32(np, "clock-frequency", &freq)) {
  251. pr_err("Unknown frequency\n");
  252. return;
  253. }
  254. of_node_put(np);
  255. msm_timer_init(freq, 32, irq, !!percpu_offset);
  256. }
  257. struct sys_timer msm_dt_timer = {
  258. .init = msm_dt_timer_init
  259. };
  260. #endif
  261. static int __init msm_timer_map(phys_addr_t event, phys_addr_t source)
  262. {
  263. event_base = ioremap(event, SZ_64);
  264. if (!event_base) {
  265. pr_err("Failed to map event base\n");
  266. return 1;
  267. }
  268. source_base = ioremap(source, SZ_64);
  269. if (!source_base) {
  270. pr_err("Failed to map source base\n");
  271. return 1;
  272. }
  273. return 0;
  274. }
  275. static void __init msm7x01_timer_init(void)
  276. {
  277. struct clocksource *cs = &msm_clocksource;
  278. if (msm_timer_map(0xc0100000, 0xc0100010))
  279. return;
  280. cs->read = msm_read_timer_count_shift;
  281. cs->mask = CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT));
  282. /* 600 KHz */
  283. msm_timer_init(19200000 >> MSM_DGT_SHIFT, 32 - MSM_DGT_SHIFT, 7,
  284. false);
  285. }
  286. struct sys_timer msm7x01_timer = {
  287. .init = msm7x01_timer_init
  288. };
  289. static void __init msm7x30_timer_init(void)
  290. {
  291. if (msm_timer_map(0xc0100004, 0xc0100024))
  292. return;
  293. msm_timer_init(24576000 / 4, 32, 1, false);
  294. }
  295. struct sys_timer msm7x30_timer = {
  296. .init = msm7x30_timer_init
  297. };
  298. static void __init qsd8x50_timer_init(void)
  299. {
  300. if (msm_timer_map(0xAC100000, 0xAC100010))
  301. return;
  302. msm_timer_init(19200000 / 4, 32, 7, false);
  303. }
  304. struct sys_timer qsd8x50_timer = {
  305. .init = qsd8x50_timer_init
  306. };