0004-clocksource-qcom-Move-clocksource-code-out-of-mach-m.patch 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. From 00009eabeb2074bef5c89e576a7a6d827c12c3d9 Mon Sep 17 00:00:00 2001
  2. From: Kumar Gala <[email protected]>
  3. Date: Wed, 29 Jan 2014 16:17:30 -0600
  4. Subject: [PATCH 004/182] clocksource: qcom: Move clocksource code out of
  5. mach-msm
  6. We intend to share the clocksource code for MSM platforms between legacy
  7. and multiplatform supported qcom SoCs.
  8. Acked-by: Olof Johansson <[email protected]>
  9. Signed-off-by: Kumar Gala <[email protected]>
  10. ---
  11. arch/arm/mach-msm/Kconfig | 13 +-
  12. arch/arm/mach-msm/Makefile | 1 -
  13. arch/arm/mach-msm/timer.c | 333 --------------------------------------
  14. drivers/clocksource/Kconfig | 3 +
  15. drivers/clocksource/Makefile | 1 +
  16. drivers/clocksource/qcom-timer.c | 329 +++++++++++++++++++++++++++++++++++++
  17. 6 files changed, 338 insertions(+), 342 deletions(-)
  18. delete mode 100644 arch/arm/mach-msm/timer.c
  19. create mode 100644 drivers/clocksource/qcom-timer.c
  20. --- a/arch/arm/mach-msm/Kconfig
  21. +++ b/arch/arm/mach-msm/Kconfig
  22. @@ -21,7 +21,7 @@ config ARCH_MSM8X60
  23. select CPU_V7
  24. select HAVE_SMP
  25. select MSM_SCM if SMP
  26. - select MSM_TIMER
  27. + select CLKSRC_QCOM
  28. config ARCH_MSM8960
  29. bool "Enable support for MSM8960"
  30. @@ -29,7 +29,7 @@ config ARCH_MSM8960
  31. select CPU_V7
  32. select HAVE_SMP
  33. select MSM_SCM if SMP
  34. - select MSM_TIMER
  35. + select CLKSRC_QCOM
  36. config ARCH_MSM8974
  37. bool "Enable support for MSM8974"
  38. @@ -54,7 +54,7 @@ config ARCH_MSM7X00A
  39. select MACH_TROUT if !MACH_HALIBUT
  40. select MSM_PROC_COMM
  41. select MSM_SMD
  42. - select MSM_TIMER
  43. + select CLKSRC_QCOM
  44. select MSM_SMD_PKG3
  45. config ARCH_MSM7X30
  46. @@ -66,7 +66,7 @@ config ARCH_MSM7X30
  47. select MSM_GPIOMUX
  48. select MSM_PROC_COMM
  49. select MSM_SMD
  50. - select MSM_TIMER
  51. + select CLKSRC_QCOM
  52. select MSM_VIC
  53. config ARCH_QSD8X50
  54. @@ -78,7 +78,7 @@ config ARCH_QSD8X50
  55. select MSM_GPIOMUX
  56. select MSM_PROC_COMM
  57. select MSM_SMD
  58. - select MSM_TIMER
  59. + select CLKSRC_QCOM
  60. select MSM_VIC
  61. endchoice
  62. @@ -153,7 +153,4 @@ config MSM_GPIOMUX
  63. config MSM_SCM
  64. bool
  65. -config MSM_TIMER
  66. - bool
  67. -
  68. endif
  69. --- a/arch/arm/mach-msm/Makefile
  70. +++ b/arch/arm/mach-msm/Makefile
  71. @@ -1,4 +1,3 @@
  72. -obj-$(CONFIG_MSM_TIMER) += timer.o
  73. obj-$(CONFIG_MSM_PROC_COMM) += clock.o
  74. obj-$(CONFIG_MSM_VIC) += irq-vic.o
  75. --- a/arch/arm/mach-msm/timer.c
  76. +++ /dev/null
  77. @@ -1,333 +0,0 @@
  78. -/*
  79. - *
  80. - * Copyright (C) 2007 Google, Inc.
  81. - * Copyright (c) 2009-2012, The Linux Foundation. All rights reserved.
  82. - *
  83. - * This software is licensed under the terms of the GNU General Public
  84. - * License version 2, as published by the Free Software Foundation, and
  85. - * may be copied, distributed, and modified under those terms.
  86. - *
  87. - * This program is distributed in the hope that it will be useful,
  88. - * but WITHOUT ANY WARRANTY; without even the implied warranty of
  89. - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  90. - * GNU General Public License for more details.
  91. - *
  92. - */
  93. -
  94. -#include <linux/clocksource.h>
  95. -#include <linux/clockchips.h>
  96. -#include <linux/cpu.h>
  97. -#include <linux/init.h>
  98. -#include <linux/interrupt.h>
  99. -#include <linux/irq.h>
  100. -#include <linux/io.h>
  101. -#include <linux/of.h>
  102. -#include <linux/of_address.h>
  103. -#include <linux/of_irq.h>
  104. -#include <linux/sched_clock.h>
  105. -
  106. -#include <asm/mach/time.h>
  107. -
  108. -#include "common.h"
  109. -
  110. -#define TIMER_MATCH_VAL 0x0000
  111. -#define TIMER_COUNT_VAL 0x0004
  112. -#define TIMER_ENABLE 0x0008
  113. -#define TIMER_ENABLE_CLR_ON_MATCH_EN BIT(1)
  114. -#define TIMER_ENABLE_EN BIT(0)
  115. -#define TIMER_CLEAR 0x000C
  116. -#define DGT_CLK_CTL 0x10
  117. -#define DGT_CLK_CTL_DIV_4 0x3
  118. -#define TIMER_STS_GPT0_CLR_PEND BIT(10)
  119. -
  120. -#define GPT_HZ 32768
  121. -
  122. -#define MSM_DGT_SHIFT 5
  123. -
  124. -static void __iomem *event_base;
  125. -static void __iomem *sts_base;
  126. -
  127. -static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
  128. -{
  129. - struct clock_event_device *evt = dev_id;
  130. - /* Stop the timer tick */
  131. - if (evt->mode == CLOCK_EVT_MODE_ONESHOT) {
  132. - u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  133. - ctrl &= ~TIMER_ENABLE_EN;
  134. - writel_relaxed(ctrl, event_base + TIMER_ENABLE);
  135. - }
  136. - evt->event_handler(evt);
  137. - return IRQ_HANDLED;
  138. -}
  139. -
  140. -static int msm_timer_set_next_event(unsigned long cycles,
  141. - struct clock_event_device *evt)
  142. -{
  143. - u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  144. -
  145. - ctrl &= ~TIMER_ENABLE_EN;
  146. - writel_relaxed(ctrl, event_base + TIMER_ENABLE);
  147. -
  148. - writel_relaxed(ctrl, event_base + TIMER_CLEAR);
  149. - writel_relaxed(cycles, event_base + TIMER_MATCH_VAL);
  150. -
  151. - if (sts_base)
  152. - while (readl_relaxed(sts_base) & TIMER_STS_GPT0_CLR_PEND)
  153. - cpu_relax();
  154. -
  155. - writel_relaxed(ctrl | TIMER_ENABLE_EN, event_base + TIMER_ENABLE);
  156. - return 0;
  157. -}
  158. -
  159. -static void msm_timer_set_mode(enum clock_event_mode mode,
  160. - struct clock_event_device *evt)
  161. -{
  162. - u32 ctrl;
  163. -
  164. - ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  165. - ctrl &= ~(TIMER_ENABLE_EN | TIMER_ENABLE_CLR_ON_MATCH_EN);
  166. -
  167. - switch (mode) {
  168. - case CLOCK_EVT_MODE_RESUME:
  169. - case CLOCK_EVT_MODE_PERIODIC:
  170. - break;
  171. - case CLOCK_EVT_MODE_ONESHOT:
  172. - /* Timer is enabled in set_next_event */
  173. - break;
  174. - case CLOCK_EVT_MODE_UNUSED:
  175. - case CLOCK_EVT_MODE_SHUTDOWN:
  176. - break;
  177. - }
  178. - writel_relaxed(ctrl, event_base + TIMER_ENABLE);
  179. -}
  180. -
  181. -static struct clock_event_device __percpu *msm_evt;
  182. -
  183. -static void __iomem *source_base;
  184. -
  185. -static notrace cycle_t msm_read_timer_count(struct clocksource *cs)
  186. -{
  187. - return readl_relaxed(source_base + TIMER_COUNT_VAL);
  188. -}
  189. -
  190. -static notrace cycle_t msm_read_timer_count_shift(struct clocksource *cs)
  191. -{
  192. - /*
  193. - * Shift timer count down by a constant due to unreliable lower bits
  194. - * on some targets.
  195. - */
  196. - return msm_read_timer_count(cs) >> MSM_DGT_SHIFT;
  197. -}
  198. -
  199. -static struct clocksource msm_clocksource = {
  200. - .name = "dg_timer",
  201. - .rating = 300,
  202. - .read = msm_read_timer_count,
  203. - .mask = CLOCKSOURCE_MASK(32),
  204. - .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  205. -};
  206. -
  207. -static int msm_timer_irq;
  208. -static int msm_timer_has_ppi;
  209. -
  210. -static int msm_local_timer_setup(struct clock_event_device *evt)
  211. -{
  212. - int cpu = smp_processor_id();
  213. - int err;
  214. -
  215. - evt->irq = msm_timer_irq;
  216. - evt->name = "msm_timer";
  217. - evt->features = CLOCK_EVT_FEAT_ONESHOT;
  218. - evt->rating = 200;
  219. - evt->set_mode = msm_timer_set_mode;
  220. - evt->set_next_event = msm_timer_set_next_event;
  221. - evt->cpumask = cpumask_of(cpu);
  222. -
  223. - clockevents_config_and_register(evt, GPT_HZ, 4, 0xffffffff);
  224. -
  225. - if (msm_timer_has_ppi) {
  226. - enable_percpu_irq(evt->irq, IRQ_TYPE_EDGE_RISING);
  227. - } else {
  228. - err = request_irq(evt->irq, msm_timer_interrupt,
  229. - IRQF_TIMER | IRQF_NOBALANCING |
  230. - IRQF_TRIGGER_RISING, "gp_timer", evt);
  231. - if (err)
  232. - pr_err("request_irq failed\n");
  233. - }
  234. -
  235. - return 0;
  236. -}
  237. -
  238. -static void msm_local_timer_stop(struct clock_event_device *evt)
  239. -{
  240. - evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
  241. - disable_percpu_irq(evt->irq);
  242. -}
  243. -
  244. -static int msm_timer_cpu_notify(struct notifier_block *self,
  245. - unsigned long action, void *hcpu)
  246. -{
  247. - /*
  248. - * Grab cpu pointer in each case to avoid spurious
  249. - * preemptible warnings
  250. - */
  251. - switch (action & ~CPU_TASKS_FROZEN) {
  252. - case CPU_STARTING:
  253. - msm_local_timer_setup(this_cpu_ptr(msm_evt));
  254. - break;
  255. - case CPU_DYING:
  256. - msm_local_timer_stop(this_cpu_ptr(msm_evt));
  257. - break;
  258. - }
  259. -
  260. - return NOTIFY_OK;
  261. -}
  262. -
  263. -static struct notifier_block msm_timer_cpu_nb = {
  264. - .notifier_call = msm_timer_cpu_notify,
  265. -};
  266. -
  267. -static u64 notrace msm_sched_clock_read(void)
  268. -{
  269. - return msm_clocksource.read(&msm_clocksource);
  270. -}
  271. -
  272. -static void __init msm_timer_init(u32 dgt_hz, int sched_bits, int irq,
  273. - bool percpu)
  274. -{
  275. - struct clocksource *cs = &msm_clocksource;
  276. - int res = 0;
  277. -
  278. - msm_timer_irq = irq;
  279. - msm_timer_has_ppi = percpu;
  280. -
  281. - msm_evt = alloc_percpu(struct clock_event_device);
  282. - if (!msm_evt) {
  283. - pr_err("memory allocation failed for clockevents\n");
  284. - goto err;
  285. - }
  286. -
  287. - if (percpu)
  288. - res = request_percpu_irq(irq, msm_timer_interrupt,
  289. - "gp_timer", msm_evt);
  290. -
  291. - if (res) {
  292. - pr_err("request_percpu_irq failed\n");
  293. - } else {
  294. - res = register_cpu_notifier(&msm_timer_cpu_nb);
  295. - if (res) {
  296. - free_percpu_irq(irq, msm_evt);
  297. - goto err;
  298. - }
  299. -
  300. - /* Immediately configure the timer on the boot CPU */
  301. - msm_local_timer_setup(__this_cpu_ptr(msm_evt));
  302. - }
  303. -
  304. -err:
  305. - writel_relaxed(TIMER_ENABLE_EN, source_base + TIMER_ENABLE);
  306. - res = clocksource_register_hz(cs, dgt_hz);
  307. - if (res)
  308. - pr_err("clocksource_register failed\n");
  309. - sched_clock_register(msm_sched_clock_read, sched_bits, dgt_hz);
  310. -}
  311. -
  312. -#ifdef CONFIG_OF
  313. -static void __init msm_dt_timer_init(struct device_node *np)
  314. -{
  315. - u32 freq;
  316. - int irq;
  317. - struct resource res;
  318. - u32 percpu_offset;
  319. - void __iomem *base;
  320. - void __iomem *cpu0_base;
  321. -
  322. - base = of_iomap(np, 0);
  323. - if (!base) {
  324. - pr_err("Failed to map event base\n");
  325. - return;
  326. - }
  327. -
  328. - /* We use GPT0 for the clockevent */
  329. - irq = irq_of_parse_and_map(np, 1);
  330. - if (irq <= 0) {
  331. - pr_err("Can't get irq\n");
  332. - return;
  333. - }
  334. -
  335. - /* We use CPU0's DGT for the clocksource */
  336. - if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
  337. - percpu_offset = 0;
  338. -
  339. - if (of_address_to_resource(np, 0, &res)) {
  340. - pr_err("Failed to parse DGT resource\n");
  341. - return;
  342. - }
  343. -
  344. - cpu0_base = ioremap(res.start + percpu_offset, resource_size(&res));
  345. - if (!cpu0_base) {
  346. - pr_err("Failed to map source base\n");
  347. - return;
  348. - }
  349. -
  350. - if (of_property_read_u32(np, "clock-frequency", &freq)) {
  351. - pr_err("Unknown frequency\n");
  352. - return;
  353. - }
  354. -
  355. - event_base = base + 0x4;
  356. - sts_base = base + 0x88;
  357. - source_base = cpu0_base + 0x24;
  358. - freq /= 4;
  359. - writel_relaxed(DGT_CLK_CTL_DIV_4, source_base + DGT_CLK_CTL);
  360. -
  361. - msm_timer_init(freq, 32, irq, !!percpu_offset);
  362. -}
  363. -CLOCKSOURCE_OF_DECLARE(kpss_timer, "qcom,kpss-timer", msm_dt_timer_init);
  364. -CLOCKSOURCE_OF_DECLARE(scss_timer, "qcom,scss-timer", msm_dt_timer_init);
  365. -#endif
  366. -
  367. -static int __init msm_timer_map(phys_addr_t addr, u32 event, u32 source,
  368. - u32 sts)
  369. -{
  370. - void __iomem *base;
  371. -
  372. - base = ioremap(addr, SZ_256);
  373. - if (!base) {
  374. - pr_err("Failed to map timer base\n");
  375. - return -ENOMEM;
  376. - }
  377. - event_base = base + event;
  378. - source_base = base + source;
  379. - if (sts)
  380. - sts_base = base + sts;
  381. -
  382. - return 0;
  383. -}
  384. -
  385. -void __init msm7x01_timer_init(void)
  386. -{
  387. - struct clocksource *cs = &msm_clocksource;
  388. -
  389. - if (msm_timer_map(0xc0100000, 0x0, 0x10, 0x0))
  390. - return;
  391. - cs->read = msm_read_timer_count_shift;
  392. - cs->mask = CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT));
  393. - /* 600 KHz */
  394. - msm_timer_init(19200000 >> MSM_DGT_SHIFT, 32 - MSM_DGT_SHIFT, 7,
  395. - false);
  396. -}
  397. -
  398. -void __init msm7x30_timer_init(void)
  399. -{
  400. - if (msm_timer_map(0xc0100000, 0x4, 0x24, 0x80))
  401. - return;
  402. - msm_timer_init(24576000 / 4, 32, 1, false);
  403. -}
  404. -
  405. -void __init qsd8x50_timer_init(void)
  406. -{
  407. - if (msm_timer_map(0xAC100000, 0x0, 0x10, 0x34))
  408. - return;
  409. - msm_timer_init(19200000 / 4, 32, 7, false);
  410. -}
  411. --- a/drivers/clocksource/Kconfig
  412. +++ b/drivers/clocksource/Kconfig
  413. @@ -140,3 +140,6 @@ config VF_PIT_TIMER
  414. bool
  415. help
  416. Support for Period Interrupt Timer on Freescale Vybrid Family SoCs.
  417. +
  418. +config CLKSRC_QCOM
  419. + bool
  420. --- a/drivers/clocksource/Makefile
  421. +++ b/drivers/clocksource/Makefile
  422. @@ -32,6 +32,7 @@ obj-$(CONFIG_CLKSRC_EFM32) += time-efm32
  423. obj-$(CONFIG_CLKSRC_EXYNOS_MCT) += exynos_mct.o
  424. obj-$(CONFIG_CLKSRC_SAMSUNG_PWM) += samsung_pwm_timer.o
  425. obj-$(CONFIG_VF_PIT_TIMER) += vf_pit_timer.o
  426. +obj-$(CONFIG_CLKSRC_QCOM) += qcom-timer.o
  427. obj-$(CONFIG_ARM_ARCH_TIMER) += arm_arch_timer.o
  428. obj-$(CONFIG_ARM_GLOBAL_TIMER) += arm_global_timer.o
  429. --- /dev/null
  430. +++ b/drivers/clocksource/qcom-timer.c
  431. @@ -0,0 +1,329 @@
  432. +/*
  433. + *
  434. + * Copyright (C) 2007 Google, Inc.
  435. + * Copyright (c) 2009-2012,2014, The Linux Foundation. All rights reserved.
  436. + *
  437. + * This software is licensed under the terms of the GNU General Public
  438. + * License version 2, as published by the Free Software Foundation, and
  439. + * may be copied, distributed, and modified under those terms.
  440. + *
  441. + * This program is distributed in the hope that it will be useful,
  442. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  443. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  444. + * GNU General Public License for more details.
  445. + *
  446. + */
  447. +
  448. +#include <linux/clocksource.h>
  449. +#include <linux/clockchips.h>
  450. +#include <linux/cpu.h>
  451. +#include <linux/init.h>
  452. +#include <linux/interrupt.h>
  453. +#include <linux/irq.h>
  454. +#include <linux/io.h>
  455. +#include <linux/of.h>
  456. +#include <linux/of_address.h>
  457. +#include <linux/of_irq.h>
  458. +#include <linux/sched_clock.h>
  459. +
  460. +#define TIMER_MATCH_VAL 0x0000
  461. +#define TIMER_COUNT_VAL 0x0004
  462. +#define TIMER_ENABLE 0x0008
  463. +#define TIMER_ENABLE_CLR_ON_MATCH_EN BIT(1)
  464. +#define TIMER_ENABLE_EN BIT(0)
  465. +#define TIMER_CLEAR 0x000C
  466. +#define DGT_CLK_CTL 0x10
  467. +#define DGT_CLK_CTL_DIV_4 0x3
  468. +#define TIMER_STS_GPT0_CLR_PEND BIT(10)
  469. +
  470. +#define GPT_HZ 32768
  471. +
  472. +#define MSM_DGT_SHIFT 5
  473. +
  474. +static void __iomem *event_base;
  475. +static void __iomem *sts_base;
  476. +
  477. +static irqreturn_t msm_timer_interrupt(int irq, void *dev_id)
  478. +{
  479. + struct clock_event_device *evt = dev_id;
  480. + /* Stop the timer tick */
  481. + if (evt->mode == CLOCK_EVT_MODE_ONESHOT) {
  482. + u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  483. + ctrl &= ~TIMER_ENABLE_EN;
  484. + writel_relaxed(ctrl, event_base + TIMER_ENABLE);
  485. + }
  486. + evt->event_handler(evt);
  487. + return IRQ_HANDLED;
  488. +}
  489. +
  490. +static int msm_timer_set_next_event(unsigned long cycles,
  491. + struct clock_event_device *evt)
  492. +{
  493. + u32 ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  494. +
  495. + ctrl &= ~TIMER_ENABLE_EN;
  496. + writel_relaxed(ctrl, event_base + TIMER_ENABLE);
  497. +
  498. + writel_relaxed(ctrl, event_base + TIMER_CLEAR);
  499. + writel_relaxed(cycles, event_base + TIMER_MATCH_VAL);
  500. +
  501. + if (sts_base)
  502. + while (readl_relaxed(sts_base) & TIMER_STS_GPT0_CLR_PEND)
  503. + cpu_relax();
  504. +
  505. + writel_relaxed(ctrl | TIMER_ENABLE_EN, event_base + TIMER_ENABLE);
  506. + return 0;
  507. +}
  508. +
  509. +static void msm_timer_set_mode(enum clock_event_mode mode,
  510. + struct clock_event_device *evt)
  511. +{
  512. + u32 ctrl;
  513. +
  514. + ctrl = readl_relaxed(event_base + TIMER_ENABLE);
  515. + ctrl &= ~(TIMER_ENABLE_EN | TIMER_ENABLE_CLR_ON_MATCH_EN);
  516. +
  517. + switch (mode) {
  518. + case CLOCK_EVT_MODE_RESUME:
  519. + case CLOCK_EVT_MODE_PERIODIC:
  520. + break;
  521. + case CLOCK_EVT_MODE_ONESHOT:
  522. + /* Timer is enabled in set_next_event */
  523. + break;
  524. + case CLOCK_EVT_MODE_UNUSED:
  525. + case CLOCK_EVT_MODE_SHUTDOWN:
  526. + break;
  527. + }
  528. + writel_relaxed(ctrl, event_base + TIMER_ENABLE);
  529. +}
  530. +
  531. +static struct clock_event_device __percpu *msm_evt;
  532. +
  533. +static void __iomem *source_base;
  534. +
  535. +static notrace cycle_t msm_read_timer_count(struct clocksource *cs)
  536. +{
  537. + return readl_relaxed(source_base + TIMER_COUNT_VAL);
  538. +}
  539. +
  540. +static notrace cycle_t msm_read_timer_count_shift(struct clocksource *cs)
  541. +{
  542. + /*
  543. + * Shift timer count down by a constant due to unreliable lower bits
  544. + * on some targets.
  545. + */
  546. + return msm_read_timer_count(cs) >> MSM_DGT_SHIFT;
  547. +}
  548. +
  549. +static struct clocksource msm_clocksource = {
  550. + .name = "dg_timer",
  551. + .rating = 300,
  552. + .read = msm_read_timer_count,
  553. + .mask = CLOCKSOURCE_MASK(32),
  554. + .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  555. +};
  556. +
  557. +static int msm_timer_irq;
  558. +static int msm_timer_has_ppi;
  559. +
  560. +static int msm_local_timer_setup(struct clock_event_device *evt)
  561. +{
  562. + int cpu = smp_processor_id();
  563. + int err;
  564. +
  565. + evt->irq = msm_timer_irq;
  566. + evt->name = "msm_timer";
  567. + evt->features = CLOCK_EVT_FEAT_ONESHOT;
  568. + evt->rating = 200;
  569. + evt->set_mode = msm_timer_set_mode;
  570. + evt->set_next_event = msm_timer_set_next_event;
  571. + evt->cpumask = cpumask_of(cpu);
  572. +
  573. + clockevents_config_and_register(evt, GPT_HZ, 4, 0xffffffff);
  574. +
  575. + if (msm_timer_has_ppi) {
  576. + enable_percpu_irq(evt->irq, IRQ_TYPE_EDGE_RISING);
  577. + } else {
  578. + err = request_irq(evt->irq, msm_timer_interrupt,
  579. + IRQF_TIMER | IRQF_NOBALANCING |
  580. + IRQF_TRIGGER_RISING, "gp_timer", evt);
  581. + if (err)
  582. + pr_err("request_irq failed\n");
  583. + }
  584. +
  585. + return 0;
  586. +}
  587. +
  588. +static void msm_local_timer_stop(struct clock_event_device *evt)
  589. +{
  590. + evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
  591. + disable_percpu_irq(evt->irq);
  592. +}
  593. +
  594. +static int msm_timer_cpu_notify(struct notifier_block *self,
  595. + unsigned long action, void *hcpu)
  596. +{
  597. + /*
  598. + * Grab cpu pointer in each case to avoid spurious
  599. + * preemptible warnings
  600. + */
  601. + switch (action & ~CPU_TASKS_FROZEN) {
  602. + case CPU_STARTING:
  603. + msm_local_timer_setup(this_cpu_ptr(msm_evt));
  604. + break;
  605. + case CPU_DYING:
  606. + msm_local_timer_stop(this_cpu_ptr(msm_evt));
  607. + break;
  608. + }
  609. +
  610. + return NOTIFY_OK;
  611. +}
  612. +
  613. +static struct notifier_block msm_timer_cpu_nb = {
  614. + .notifier_call = msm_timer_cpu_notify,
  615. +};
  616. +
  617. +static u64 notrace msm_sched_clock_read(void)
  618. +{
  619. + return msm_clocksource.read(&msm_clocksource);
  620. +}
  621. +
  622. +static void __init msm_timer_init(u32 dgt_hz, int sched_bits, int irq,
  623. + bool percpu)
  624. +{
  625. + struct clocksource *cs = &msm_clocksource;
  626. + int res = 0;
  627. +
  628. + msm_timer_irq = irq;
  629. + msm_timer_has_ppi = percpu;
  630. +
  631. + msm_evt = alloc_percpu(struct clock_event_device);
  632. + if (!msm_evt) {
  633. + pr_err("memory allocation failed for clockevents\n");
  634. + goto err;
  635. + }
  636. +
  637. + if (percpu)
  638. + res = request_percpu_irq(irq, msm_timer_interrupt,
  639. + "gp_timer", msm_evt);
  640. +
  641. + if (res) {
  642. + pr_err("request_percpu_irq failed\n");
  643. + } else {
  644. + res = register_cpu_notifier(&msm_timer_cpu_nb);
  645. + if (res) {
  646. + free_percpu_irq(irq, msm_evt);
  647. + goto err;
  648. + }
  649. +
  650. + /* Immediately configure the timer on the boot CPU */
  651. + msm_local_timer_setup(__this_cpu_ptr(msm_evt));
  652. + }
  653. +
  654. +err:
  655. + writel_relaxed(TIMER_ENABLE_EN, source_base + TIMER_ENABLE);
  656. + res = clocksource_register_hz(cs, dgt_hz);
  657. + if (res)
  658. + pr_err("clocksource_register failed\n");
  659. + sched_clock_register(msm_sched_clock_read, sched_bits, dgt_hz);
  660. +}
  661. +
  662. +#ifdef CONFIG_OF
  663. +static void __init msm_dt_timer_init(struct device_node *np)
  664. +{
  665. + u32 freq;
  666. + int irq;
  667. + struct resource res;
  668. + u32 percpu_offset;
  669. + void __iomem *base;
  670. + void __iomem *cpu0_base;
  671. +
  672. + base = of_iomap(np, 0);
  673. + if (!base) {
  674. + pr_err("Failed to map event base\n");
  675. + return;
  676. + }
  677. +
  678. + /* We use GPT0 for the clockevent */
  679. + irq = irq_of_parse_and_map(np, 1);
  680. + if (irq <= 0) {
  681. + pr_err("Can't get irq\n");
  682. + return;
  683. + }
  684. +
  685. + /* We use CPU0's DGT for the clocksource */
  686. + if (of_property_read_u32(np, "cpu-offset", &percpu_offset))
  687. + percpu_offset = 0;
  688. +
  689. + if (of_address_to_resource(np, 0, &res)) {
  690. + pr_err("Failed to parse DGT resource\n");
  691. + return;
  692. + }
  693. +
  694. + cpu0_base = ioremap(res.start + percpu_offset, resource_size(&res));
  695. + if (!cpu0_base) {
  696. + pr_err("Failed to map source base\n");
  697. + return;
  698. + }
  699. +
  700. + if (of_property_read_u32(np, "clock-frequency", &freq)) {
  701. + pr_err("Unknown frequency\n");
  702. + return;
  703. + }
  704. +
  705. + event_base = base + 0x4;
  706. + sts_base = base + 0x88;
  707. + source_base = cpu0_base + 0x24;
  708. + freq /= 4;
  709. + writel_relaxed(DGT_CLK_CTL_DIV_4, source_base + DGT_CLK_CTL);
  710. +
  711. + msm_timer_init(freq, 32, irq, !!percpu_offset);
  712. +}
  713. +CLOCKSOURCE_OF_DECLARE(kpss_timer, "qcom,kpss-timer", msm_dt_timer_init);
  714. +CLOCKSOURCE_OF_DECLARE(scss_timer, "qcom,scss-timer", msm_dt_timer_init);
  715. +#endif
  716. +
  717. +static int __init msm_timer_map(phys_addr_t addr, u32 event, u32 source,
  718. + u32 sts)
  719. +{
  720. + void __iomem *base;
  721. +
  722. + base = ioremap(addr, SZ_256);
  723. + if (!base) {
  724. + pr_err("Failed to map timer base\n");
  725. + return -ENOMEM;
  726. + }
  727. + event_base = base + event;
  728. + source_base = base + source;
  729. + if (sts)
  730. + sts_base = base + sts;
  731. +
  732. + return 0;
  733. +}
  734. +
  735. +void __init msm7x01_timer_init(void)
  736. +{
  737. + struct clocksource *cs = &msm_clocksource;
  738. +
  739. + if (msm_timer_map(0xc0100000, 0x0, 0x10, 0x0))
  740. + return;
  741. + cs->read = msm_read_timer_count_shift;
  742. + cs->mask = CLOCKSOURCE_MASK((32 - MSM_DGT_SHIFT));
  743. + /* 600 KHz */
  744. + msm_timer_init(19200000 >> MSM_DGT_SHIFT, 32 - MSM_DGT_SHIFT, 7,
  745. + false);
  746. +}
  747. +
  748. +void __init msm7x30_timer_init(void)
  749. +{
  750. + if (msm_timer_map(0xc0100000, 0x4, 0x24, 0x80))
  751. + return;
  752. + msm_timer_init(24576000 / 4, 32, 1, false);
  753. +}
  754. +
  755. +void __init qsd8x50_timer_init(void)
  756. +{
  757. + if (msm_timer_map(0xAC100000, 0x0, 0x10, 0x34))
  758. + return;
  759. + msm_timer_init(19200000 / 4, 32, 7, false);
  760. +}