timer-ti-dm-systimer.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/clk.h>
  3. #include <linux/clocksource.h>
  4. #include <linux/clockchips.h>
  5. #include <linux/cpuhotplug.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/io.h>
  8. #include <linux/iopoll.h>
  9. #include <linux/err.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <linux/of_irq.h>
  13. #include <linux/sched_clock.h>
  14. #include <linux/clk/clk-conf.h>
  15. #include <clocksource/timer-ti-dm.h>
  16. #include <dt-bindings/bus/ti-sysc.h>
  17. /* For type1, set SYSC_OMAP2_CLOCKACTIVITY for fck off on idle, l4 clock on */
  18. #define DMTIMER_TYPE1_ENABLE ((1 << 9) | (SYSC_IDLE_SMART << 3) | \
  19. SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_AUTOIDLE)
  20. #define DMTIMER_TYPE1_DISABLE (SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE)
  21. #define DMTIMER_TYPE2_ENABLE (SYSC_IDLE_SMART_WKUP << 2)
  22. #define DMTIMER_RESET_WAIT 100000
  23. #define DMTIMER_INST_DONT_CARE ~0U
  24. static int counter_32k;
  25. static u32 clocksource;
  26. static u32 clockevent;
  27. /*
  28. * Subset of the timer registers we use. Note that the register offsets
  29. * depend on the timer revision detected.
  30. */
  31. struct dmtimer_systimer {
  32. void __iomem *base;
  33. u8 sysc;
  34. u8 irq_stat;
  35. u8 irq_ena;
  36. u8 pend;
  37. u8 load;
  38. u8 counter;
  39. u8 ctrl;
  40. u8 wakeup;
  41. u8 ifctrl;
  42. struct clk *fck;
  43. struct clk *ick;
  44. unsigned long rate;
  45. };
  46. struct dmtimer_clockevent {
  47. struct clock_event_device dev;
  48. struct dmtimer_systimer t;
  49. u32 period;
  50. };
  51. struct dmtimer_clocksource {
  52. struct clocksource dev;
  53. struct dmtimer_systimer t;
  54. unsigned int loadval;
  55. };
  56. /* Assumes v1 ip if bits [31:16] are zero */
  57. static bool dmtimer_systimer_revision1(struct dmtimer_systimer *t)
  58. {
  59. u32 tidr = readl_relaxed(t->base);
  60. return !(tidr >> 16);
  61. }
  62. static void dmtimer_systimer_enable(struct dmtimer_systimer *t)
  63. {
  64. u32 val;
  65. if (dmtimer_systimer_revision1(t))
  66. val = DMTIMER_TYPE1_ENABLE;
  67. else
  68. val = DMTIMER_TYPE2_ENABLE;
  69. writel_relaxed(val, t->base + t->sysc);
  70. }
  71. static void dmtimer_systimer_disable(struct dmtimer_systimer *t)
  72. {
  73. if (!dmtimer_systimer_revision1(t))
  74. return;
  75. writel_relaxed(DMTIMER_TYPE1_DISABLE, t->base + t->sysc);
  76. }
  77. static int __init dmtimer_systimer_type1_reset(struct dmtimer_systimer *t)
  78. {
  79. void __iomem *syss = t->base + OMAP_TIMER_V1_SYS_STAT_OFFSET;
  80. int ret;
  81. u32 l;
  82. dmtimer_systimer_enable(t);
  83. writel_relaxed(BIT(1) | BIT(2), t->base + t->ifctrl);
  84. ret = readl_poll_timeout_atomic(syss, l, l & BIT(0), 100,
  85. DMTIMER_RESET_WAIT);
  86. return ret;
  87. }
  88. /* Note we must use io_base instead of func_base for type2 OCP regs */
  89. static int __init dmtimer_systimer_type2_reset(struct dmtimer_systimer *t)
  90. {
  91. void __iomem *sysc = t->base + t->sysc;
  92. u32 l;
  93. dmtimer_systimer_enable(t);
  94. l = readl_relaxed(sysc);
  95. l |= BIT(0);
  96. writel_relaxed(l, sysc);
  97. return readl_poll_timeout_atomic(sysc, l, !(l & BIT(0)), 100,
  98. DMTIMER_RESET_WAIT);
  99. }
  100. static int __init dmtimer_systimer_reset(struct dmtimer_systimer *t)
  101. {
  102. int ret;
  103. if (dmtimer_systimer_revision1(t))
  104. ret = dmtimer_systimer_type1_reset(t);
  105. else
  106. ret = dmtimer_systimer_type2_reset(t);
  107. if (ret < 0) {
  108. pr_err("%s failed with %i\n", __func__, ret);
  109. return ret;
  110. }
  111. return 0;
  112. }
  113. static const struct of_device_id counter_match_table[] = {
  114. { .compatible = "ti,omap-counter32k" },
  115. { /* Sentinel */ },
  116. };
  117. /*
  118. * Check if the SoC als has a usable working 32 KiHz counter. The 32 KiHz
  119. * counter is handled by timer-ti-32k, but we need to detect it as it
  120. * affects the preferred dmtimer system timer configuration. There is
  121. * typically no use for a dmtimer clocksource if the 32 KiHz counter is
  122. * present, except on am437x as described below.
  123. */
  124. static void __init dmtimer_systimer_check_counter32k(void)
  125. {
  126. struct device_node *np;
  127. if (counter_32k)
  128. return;
  129. np = of_find_matching_node(NULL, counter_match_table);
  130. if (!np) {
  131. counter_32k = -ENODEV;
  132. return;
  133. }
  134. if (of_device_is_available(np))
  135. counter_32k = 1;
  136. else
  137. counter_32k = -ENODEV;
  138. of_node_put(np);
  139. }
  140. static const struct of_device_id dmtimer_match_table[] = {
  141. { .compatible = "ti,omap2420-timer", },
  142. { .compatible = "ti,omap3430-timer", },
  143. { .compatible = "ti,omap4430-timer", },
  144. { .compatible = "ti,omap5430-timer", },
  145. { .compatible = "ti,am335x-timer", },
  146. { .compatible = "ti,am335x-timer-1ms", },
  147. { .compatible = "ti,dm814-timer", },
  148. { .compatible = "ti,dm816-timer", },
  149. { /* Sentinel */ },
  150. };
  151. /*
  152. * Checks that system timers are configured to not reset and idle during
  153. * the generic timer-ti-dm device driver probe. And that the system timer
  154. * source clocks are properly configured. Also, let's not hog any DSP and
  155. * PWM capable timers unnecessarily as system timers.
  156. */
  157. static bool __init dmtimer_is_preferred(struct device_node *np)
  158. {
  159. if (!of_device_is_available(np))
  160. return false;
  161. if (!of_property_read_bool(np->parent,
  162. "ti,no-reset-on-init"))
  163. return false;
  164. if (!of_property_read_bool(np->parent, "ti,no-idle"))
  165. return false;
  166. /* Secure gptimer12 is always clocked with a fixed source */
  167. if (!of_property_read_bool(np, "ti,timer-secure")) {
  168. if (!of_property_read_bool(np, "assigned-clocks"))
  169. return false;
  170. if (!of_property_read_bool(np, "assigned-clock-parents"))
  171. return false;
  172. }
  173. if (of_property_read_bool(np, "ti,timer-dsp"))
  174. return false;
  175. if (of_property_read_bool(np, "ti,timer-pwm"))
  176. return false;
  177. return true;
  178. }
  179. /*
  180. * Finds the first available usable always-on timer, and assigns it to either
  181. * clockevent or clocksource depending if the counter_32k is available on the
  182. * SoC or not.
  183. *
  184. * Some omap3 boards with unreliable oscillator must not use the counter_32k
  185. * or dmtimer1 with 32 KiHz source. Additionally, the boards with unreliable
  186. * oscillator should really set counter_32k as disabled, and delete dmtimer1
  187. * ti,always-on property, but let's not count on it. For these quirky cases,
  188. * we prefer using the always-on secure dmtimer12 with the internal 32 KiHz
  189. * clock as the clocksource, and any available dmtimer as clockevent.
  190. *
  191. * For am437x, we are using am335x style dmtimer clocksource. It is unclear
  192. * if this quirk handling is really needed, but let's change it separately
  193. * based on testing as it might cause side effects.
  194. */
  195. static void __init dmtimer_systimer_assign_alwon(void)
  196. {
  197. struct device_node *np;
  198. u32 pa = 0;
  199. bool quirk_unreliable_oscillator = false;
  200. /* Quirk unreliable 32 KiHz oscillator with incomplete dts */
  201. if (of_machine_is_compatible("ti,omap3-beagle-ab4")) {
  202. quirk_unreliable_oscillator = true;
  203. counter_32k = -ENODEV;
  204. }
  205. /* Quirk am437x using am335x style dmtimer clocksource */
  206. if (of_machine_is_compatible("ti,am43"))
  207. counter_32k = -ENODEV;
  208. for_each_matching_node(np, dmtimer_match_table) {
  209. if (!dmtimer_is_preferred(np))
  210. continue;
  211. if (of_property_read_bool(np, "ti,timer-alwon")) {
  212. const __be32 *addr;
  213. addr = of_get_address(np, 0, NULL, NULL);
  214. pa = of_translate_address(np, addr);
  215. if (pa) {
  216. /* Quirky omap3 boards must use dmtimer12 */
  217. if (quirk_unreliable_oscillator &&
  218. pa == 0x48318000)
  219. continue;
  220. of_node_put(np);
  221. break;
  222. }
  223. }
  224. }
  225. /* Usually no need for dmtimer clocksource if we have counter32 */
  226. if (counter_32k >= 0) {
  227. clockevent = pa;
  228. clocksource = 0;
  229. } else {
  230. clocksource = pa;
  231. clockevent = DMTIMER_INST_DONT_CARE;
  232. }
  233. }
  234. /* Finds the first usable dmtimer, used for the don't care case */
  235. static u32 __init dmtimer_systimer_find_first_available(void)
  236. {
  237. struct device_node *np;
  238. const __be32 *addr;
  239. u32 pa = 0;
  240. for_each_matching_node(np, dmtimer_match_table) {
  241. if (!dmtimer_is_preferred(np))
  242. continue;
  243. addr = of_get_address(np, 0, NULL, NULL);
  244. pa = of_translate_address(np, addr);
  245. if (pa) {
  246. if (pa == clocksource || pa == clockevent) {
  247. pa = 0;
  248. continue;
  249. }
  250. of_node_put(np);
  251. break;
  252. }
  253. }
  254. return pa;
  255. }
  256. /* Selects the best clocksource and clockevent to use */
  257. static void __init dmtimer_systimer_select_best(void)
  258. {
  259. dmtimer_systimer_check_counter32k();
  260. dmtimer_systimer_assign_alwon();
  261. if (clockevent == DMTIMER_INST_DONT_CARE)
  262. clockevent = dmtimer_systimer_find_first_available();
  263. pr_debug("%s: counter_32k: %i clocksource: %08x clockevent: %08x\n",
  264. __func__, counter_32k, clocksource, clockevent);
  265. }
  266. /* Interface clocks are only available on some SoCs variants */
  267. static int __init dmtimer_systimer_init_clock(struct dmtimer_systimer *t,
  268. struct device_node *np,
  269. const char *name,
  270. unsigned long *rate)
  271. {
  272. struct clk *clock;
  273. unsigned long r;
  274. bool is_ick = false;
  275. int error;
  276. is_ick = !strncmp(name, "ick", 3);
  277. clock = of_clk_get_by_name(np, name);
  278. if ((PTR_ERR(clock) == -EINVAL) && is_ick)
  279. return 0;
  280. else if (IS_ERR(clock))
  281. return PTR_ERR(clock);
  282. error = clk_prepare_enable(clock);
  283. if (error)
  284. return error;
  285. r = clk_get_rate(clock);
  286. if (!r) {
  287. clk_disable_unprepare(clock);
  288. return -ENODEV;
  289. }
  290. if (is_ick)
  291. t->ick = clock;
  292. else
  293. t->fck = clock;
  294. *rate = r;
  295. return 0;
  296. }
  297. static int __init dmtimer_systimer_setup(struct device_node *np,
  298. struct dmtimer_systimer *t)
  299. {
  300. unsigned long rate;
  301. u8 regbase;
  302. int error;
  303. if (!of_device_is_compatible(np->parent, "ti,sysc"))
  304. return -EINVAL;
  305. t->base = of_iomap(np, 0);
  306. if (!t->base)
  307. return -ENXIO;
  308. /*
  309. * Enable optional assigned-clock-parents configured at the timer
  310. * node level. For regular device drivers, this is done automatically
  311. * by bus related code such as platform_drv_probe().
  312. */
  313. error = of_clk_set_defaults(np, false);
  314. if (error < 0)
  315. pr_err("%s: clock source init failed: %i\n", __func__, error);
  316. /* For ti-sysc, we have timer clocks at the parent module level */
  317. error = dmtimer_systimer_init_clock(t, np->parent, "fck", &rate);
  318. if (error)
  319. goto err_unmap;
  320. t->rate = rate;
  321. error = dmtimer_systimer_init_clock(t, np->parent, "ick", &rate);
  322. if (error)
  323. goto err_unmap;
  324. if (dmtimer_systimer_revision1(t)) {
  325. t->irq_stat = OMAP_TIMER_V1_STAT_OFFSET;
  326. t->irq_ena = OMAP_TIMER_V1_INT_EN_OFFSET;
  327. t->pend = _OMAP_TIMER_WRITE_PEND_OFFSET;
  328. regbase = 0;
  329. } else {
  330. t->irq_stat = OMAP_TIMER_V2_IRQSTATUS;
  331. t->irq_ena = OMAP_TIMER_V2_IRQENABLE_SET;
  332. regbase = OMAP_TIMER_V2_FUNC_OFFSET;
  333. t->pend = regbase + _OMAP_TIMER_WRITE_PEND_OFFSET;
  334. }
  335. t->sysc = OMAP_TIMER_OCP_CFG_OFFSET;
  336. t->load = regbase + _OMAP_TIMER_LOAD_OFFSET;
  337. t->counter = regbase + _OMAP_TIMER_COUNTER_OFFSET;
  338. t->ctrl = regbase + _OMAP_TIMER_CTRL_OFFSET;
  339. t->wakeup = regbase + _OMAP_TIMER_WAKEUP_EN_OFFSET;
  340. t->ifctrl = regbase + _OMAP_TIMER_IF_CTRL_OFFSET;
  341. dmtimer_systimer_reset(t);
  342. dmtimer_systimer_enable(t);
  343. pr_debug("dmtimer rev %08x sysc %08x\n", readl_relaxed(t->base),
  344. readl_relaxed(t->base + t->sysc));
  345. return 0;
  346. err_unmap:
  347. iounmap(t->base);
  348. return error;
  349. }
  350. /* Clockevent */
  351. static struct dmtimer_clockevent *
  352. to_dmtimer_clockevent(struct clock_event_device *clockevent)
  353. {
  354. return container_of(clockevent, struct dmtimer_clockevent, dev);
  355. }
  356. static irqreturn_t dmtimer_clockevent_interrupt(int irq, void *data)
  357. {
  358. struct dmtimer_clockevent *clkevt = data;
  359. struct dmtimer_systimer *t = &clkevt->t;
  360. writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->irq_stat);
  361. clkevt->dev.event_handler(&clkevt->dev);
  362. return IRQ_HANDLED;
  363. }
  364. static int dmtimer_set_next_event(unsigned long cycles,
  365. struct clock_event_device *evt)
  366. {
  367. struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
  368. struct dmtimer_systimer *t = &clkevt->t;
  369. void __iomem *pend = t->base + t->pend;
  370. while (readl_relaxed(pend) & WP_TCRR)
  371. cpu_relax();
  372. writel_relaxed(0xffffffff - cycles, t->base + t->counter);
  373. while (readl_relaxed(pend) & WP_TCLR)
  374. cpu_relax();
  375. writel_relaxed(OMAP_TIMER_CTRL_ST, t->base + t->ctrl);
  376. return 0;
  377. }
  378. static int dmtimer_clockevent_shutdown(struct clock_event_device *evt)
  379. {
  380. struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
  381. struct dmtimer_systimer *t = &clkevt->t;
  382. void __iomem *ctrl = t->base + t->ctrl;
  383. u32 l;
  384. l = readl_relaxed(ctrl);
  385. if (l & OMAP_TIMER_CTRL_ST) {
  386. l &= ~BIT(0);
  387. writel_relaxed(l, ctrl);
  388. /* Flush posted write */
  389. l = readl_relaxed(ctrl);
  390. /* Wait for functional clock period x 3.5 */
  391. udelay(3500000 / t->rate + 1);
  392. }
  393. writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->irq_stat);
  394. return 0;
  395. }
  396. static int dmtimer_set_periodic(struct clock_event_device *evt)
  397. {
  398. struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
  399. struct dmtimer_systimer *t = &clkevt->t;
  400. void __iomem *pend = t->base + t->pend;
  401. dmtimer_clockevent_shutdown(evt);
  402. /* Looks like we need to first set the load value separately */
  403. while (readl_relaxed(pend) & WP_TLDR)
  404. cpu_relax();
  405. writel_relaxed(clkevt->period, t->base + t->load);
  406. while (readl_relaxed(pend) & WP_TCRR)
  407. cpu_relax();
  408. writel_relaxed(clkevt->period, t->base + t->counter);
  409. while (readl_relaxed(pend) & WP_TCLR)
  410. cpu_relax();
  411. writel_relaxed(OMAP_TIMER_CTRL_AR | OMAP_TIMER_CTRL_ST,
  412. t->base + t->ctrl);
  413. return 0;
  414. }
  415. static void omap_clockevent_idle(struct clock_event_device *evt)
  416. {
  417. struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
  418. struct dmtimer_systimer *t = &clkevt->t;
  419. dmtimer_systimer_disable(t);
  420. clk_disable(t->fck);
  421. }
  422. static void omap_clockevent_unidle(struct clock_event_device *evt)
  423. {
  424. struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
  425. struct dmtimer_systimer *t = &clkevt->t;
  426. int error;
  427. error = clk_enable(t->fck);
  428. if (error)
  429. pr_err("could not enable timer fck on resume: %i\n", error);
  430. dmtimer_systimer_enable(t);
  431. writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->irq_ena);
  432. writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->wakeup);
  433. }
  434. static int __init dmtimer_clkevt_init_common(struct dmtimer_clockevent *clkevt,
  435. struct device_node *np,
  436. unsigned int features,
  437. const struct cpumask *cpumask,
  438. const char *name,
  439. int rating)
  440. {
  441. struct clock_event_device *dev;
  442. struct dmtimer_systimer *t;
  443. int error;
  444. t = &clkevt->t;
  445. dev = &clkevt->dev;
  446. /*
  447. * We mostly use cpuidle_coupled with ARM local timers for runtime,
  448. * so there's probably no use for CLOCK_EVT_FEAT_DYNIRQ here.
  449. */
  450. dev->features = features;
  451. dev->rating = rating;
  452. dev->set_next_event = dmtimer_set_next_event;
  453. dev->set_state_shutdown = dmtimer_clockevent_shutdown;
  454. dev->set_state_periodic = dmtimer_set_periodic;
  455. dev->set_state_oneshot = dmtimer_clockevent_shutdown;
  456. dev->set_state_oneshot_stopped = dmtimer_clockevent_shutdown;
  457. dev->tick_resume = dmtimer_clockevent_shutdown;
  458. dev->cpumask = cpumask;
  459. dev->irq = irq_of_parse_and_map(np, 0);
  460. if (!dev->irq)
  461. return -ENXIO;
  462. error = dmtimer_systimer_setup(np, &clkevt->t);
  463. if (error)
  464. return error;
  465. clkevt->period = 0xffffffff - DIV_ROUND_CLOSEST(t->rate, HZ);
  466. /*
  467. * For clock-event timers we never read the timer counter and
  468. * so we are not impacted by errata i103 and i767. Therefore,
  469. * we can safely ignore this errata for clock-event timers.
  470. */
  471. writel_relaxed(OMAP_TIMER_CTRL_POSTED, t->base + t->ifctrl);
  472. error = request_irq(dev->irq, dmtimer_clockevent_interrupt,
  473. IRQF_TIMER, name, clkevt);
  474. if (error)
  475. goto err_out_unmap;
  476. writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->irq_ena);
  477. writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->wakeup);
  478. pr_info("TI gptimer %s: %s%lu Hz at %pOF\n",
  479. name, of_find_property(np, "ti,timer-alwon", NULL) ?
  480. "always-on " : "", t->rate, np->parent);
  481. return 0;
  482. err_out_unmap:
  483. iounmap(t->base);
  484. return error;
  485. }
  486. static int __init dmtimer_clockevent_init(struct device_node *np)
  487. {
  488. struct dmtimer_clockevent *clkevt;
  489. int error;
  490. clkevt = kzalloc(sizeof(*clkevt), GFP_KERNEL);
  491. if (!clkevt)
  492. return -ENOMEM;
  493. error = dmtimer_clkevt_init_common(clkevt, np,
  494. CLOCK_EVT_FEAT_PERIODIC |
  495. CLOCK_EVT_FEAT_ONESHOT,
  496. cpu_possible_mask, "clockevent",
  497. 300);
  498. if (error)
  499. goto err_out_free;
  500. clockevents_config_and_register(&clkevt->dev, clkevt->t.rate,
  501. 3, /* Timer internal resync latency */
  502. 0xffffffff);
  503. if (of_machine_is_compatible("ti,am33xx") ||
  504. of_machine_is_compatible("ti,am43")) {
  505. clkevt->dev.suspend = omap_clockevent_idle;
  506. clkevt->dev.resume = omap_clockevent_unidle;
  507. }
  508. return 0;
  509. err_out_free:
  510. kfree(clkevt);
  511. return error;
  512. }
  513. /* Dmtimer as percpu timer. See dra7 ARM architected timer wrap erratum i940 */
  514. static DEFINE_PER_CPU(struct dmtimer_clockevent, dmtimer_percpu_timer);
  515. static int __init dmtimer_percpu_timer_init(struct device_node *np, int cpu)
  516. {
  517. struct dmtimer_clockevent *clkevt;
  518. int error;
  519. if (!cpu_possible(cpu))
  520. return -EINVAL;
  521. if (!of_property_read_bool(np->parent, "ti,no-reset-on-init") ||
  522. !of_property_read_bool(np->parent, "ti,no-idle"))
  523. pr_warn("Incomplete dtb for percpu dmtimer %pOF\n", np->parent);
  524. clkevt = per_cpu_ptr(&dmtimer_percpu_timer, cpu);
  525. error = dmtimer_clkevt_init_common(clkevt, np, CLOCK_EVT_FEAT_ONESHOT,
  526. cpumask_of(cpu), "percpu-dmtimer",
  527. 500);
  528. if (error)
  529. return error;
  530. return 0;
  531. }
  532. /* See TRM for timer internal resynch latency */
  533. static int omap_dmtimer_starting_cpu(unsigned int cpu)
  534. {
  535. struct dmtimer_clockevent *clkevt = per_cpu_ptr(&dmtimer_percpu_timer, cpu);
  536. struct clock_event_device *dev = &clkevt->dev;
  537. struct dmtimer_systimer *t = &clkevt->t;
  538. clockevents_config_and_register(dev, t->rate, 3, ULONG_MAX);
  539. irq_force_affinity(dev->irq, cpumask_of(cpu));
  540. return 0;
  541. }
  542. static int __init dmtimer_percpu_timer_startup(void)
  543. {
  544. struct dmtimer_clockevent *clkevt = per_cpu_ptr(&dmtimer_percpu_timer, 0);
  545. struct dmtimer_systimer *t = &clkevt->t;
  546. if (t->sysc) {
  547. cpuhp_setup_state(CPUHP_AP_TI_GP_TIMER_STARTING,
  548. "clockevents/omap/gptimer:starting",
  549. omap_dmtimer_starting_cpu, NULL);
  550. }
  551. return 0;
  552. }
  553. subsys_initcall(dmtimer_percpu_timer_startup);
  554. static int __init dmtimer_percpu_quirk_init(struct device_node *np, u32 pa)
  555. {
  556. struct device_node *arm_timer;
  557. arm_timer = of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
  558. if (of_device_is_available(arm_timer)) {
  559. pr_warn_once("ARM architected timer wrap issue i940 detected\n");
  560. return 0;
  561. }
  562. if (pa == 0x4882c000) /* dra7 dmtimer15 */
  563. return dmtimer_percpu_timer_init(np, 0);
  564. else if (pa == 0x4882e000) /* dra7 dmtimer16 */
  565. return dmtimer_percpu_timer_init(np, 1);
  566. return 0;
  567. }
  568. /* Clocksource */
  569. static struct dmtimer_clocksource *
  570. to_dmtimer_clocksource(struct clocksource *cs)
  571. {
  572. return container_of(cs, struct dmtimer_clocksource, dev);
  573. }
  574. static u64 dmtimer_clocksource_read_cycles(struct clocksource *cs)
  575. {
  576. struct dmtimer_clocksource *clksrc = to_dmtimer_clocksource(cs);
  577. struct dmtimer_systimer *t = &clksrc->t;
  578. return (u64)readl_relaxed(t->base + t->counter);
  579. }
  580. static void __iomem *dmtimer_sched_clock_counter;
  581. static u64 notrace dmtimer_read_sched_clock(void)
  582. {
  583. return readl_relaxed(dmtimer_sched_clock_counter);
  584. }
  585. static void dmtimer_clocksource_suspend(struct clocksource *cs)
  586. {
  587. struct dmtimer_clocksource *clksrc = to_dmtimer_clocksource(cs);
  588. struct dmtimer_systimer *t = &clksrc->t;
  589. clksrc->loadval = readl_relaxed(t->base + t->counter);
  590. dmtimer_systimer_disable(t);
  591. clk_disable(t->fck);
  592. }
  593. static void dmtimer_clocksource_resume(struct clocksource *cs)
  594. {
  595. struct dmtimer_clocksource *clksrc = to_dmtimer_clocksource(cs);
  596. struct dmtimer_systimer *t = &clksrc->t;
  597. int error;
  598. error = clk_enable(t->fck);
  599. if (error)
  600. pr_err("could not enable timer fck on resume: %i\n", error);
  601. dmtimer_systimer_enable(t);
  602. writel_relaxed(clksrc->loadval, t->base + t->counter);
  603. writel_relaxed(OMAP_TIMER_CTRL_ST | OMAP_TIMER_CTRL_AR,
  604. t->base + t->ctrl);
  605. }
  606. static int __init dmtimer_clocksource_init(struct device_node *np)
  607. {
  608. struct dmtimer_clocksource *clksrc;
  609. struct dmtimer_systimer *t;
  610. struct clocksource *dev;
  611. int error;
  612. clksrc = kzalloc(sizeof(*clksrc), GFP_KERNEL);
  613. if (!clksrc)
  614. return -ENOMEM;
  615. dev = &clksrc->dev;
  616. t = &clksrc->t;
  617. error = dmtimer_systimer_setup(np, t);
  618. if (error)
  619. goto err_out_free;
  620. dev->name = "dmtimer";
  621. dev->rating = 300;
  622. dev->read = dmtimer_clocksource_read_cycles;
  623. dev->mask = CLOCKSOURCE_MASK(32);
  624. dev->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  625. /* Unlike for clockevent, legacy code sets suspend only for am4 */
  626. if (of_machine_is_compatible("ti,am43")) {
  627. dev->suspend = dmtimer_clocksource_suspend;
  628. dev->resume = dmtimer_clocksource_resume;
  629. }
  630. writel_relaxed(0, t->base + t->counter);
  631. writel_relaxed(OMAP_TIMER_CTRL_ST | OMAP_TIMER_CTRL_AR,
  632. t->base + t->ctrl);
  633. pr_info("TI gptimer clocksource: %s%pOF\n",
  634. of_find_property(np, "ti,timer-alwon", NULL) ?
  635. "always-on " : "", np->parent);
  636. if (!dmtimer_sched_clock_counter) {
  637. dmtimer_sched_clock_counter = t->base + t->counter;
  638. sched_clock_register(dmtimer_read_sched_clock, 32, t->rate);
  639. }
  640. if (clocksource_register_hz(dev, t->rate))
  641. pr_err("Could not register clocksource %pOF\n", np);
  642. return 0;
  643. err_out_free:
  644. kfree(clksrc);
  645. return -ENODEV;
  646. }
  647. /*
  648. * To detect between a clocksource and clockevent, we assume the device tree
  649. * has no interrupts configured for a clocksource timer.
  650. */
  651. static int __init dmtimer_systimer_init(struct device_node *np)
  652. {
  653. const __be32 *addr;
  654. u32 pa;
  655. /* One time init for the preferred timer configuration */
  656. if (!clocksource && !clockevent)
  657. dmtimer_systimer_select_best();
  658. if (!clocksource && !clockevent) {
  659. pr_err("%s: unable to detect system timers, update dtb?\n",
  660. __func__);
  661. return -EINVAL;
  662. }
  663. addr = of_get_address(np, 0, NULL, NULL);
  664. pa = of_translate_address(np, addr);
  665. if (!pa)
  666. return -EINVAL;
  667. if (counter_32k <= 0 && clocksource == pa)
  668. return dmtimer_clocksource_init(np);
  669. if (clockevent == pa)
  670. return dmtimer_clockevent_init(np);
  671. if (of_machine_is_compatible("ti,dra7"))
  672. return dmtimer_percpu_quirk_init(np, pa);
  673. return 0;
  674. }
  675. TIMER_OF_DECLARE(systimer_omap2, "ti,omap2420-timer", dmtimer_systimer_init);
  676. TIMER_OF_DECLARE(systimer_omap3, "ti,omap3430-timer", dmtimer_systimer_init);
  677. TIMER_OF_DECLARE(systimer_omap4, "ti,omap4430-timer", dmtimer_systimer_init);
  678. TIMER_OF_DECLARE(systimer_omap5, "ti,omap5430-timer", dmtimer_systimer_init);
  679. TIMER_OF_DECLARE(systimer_am33x, "ti,am335x-timer", dmtimer_systimer_init);
  680. TIMER_OF_DECLARE(systimer_am3ms, "ti,am335x-timer-1ms", dmtimer_systimer_init);
  681. TIMER_OF_DECLARE(systimer_dm814, "ti,dm814-timer", dmtimer_systimer_init);
  682. TIMER_OF_DECLARE(systimer_dm816, "ti,dm816-timer", dmtimer_systimer_init);