timer-ti-dm-systimer.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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. struct resource res;
  210. if (!dmtimer_is_preferred(np))
  211. continue;
  212. if (!of_property_read_bool(np, "ti,timer-alwon"))
  213. continue;
  214. if (of_address_to_resource(np, 0, &res))
  215. continue;
  216. pa = res.start;
  217. /* Quirky omap3 boards must use dmtimer12 */
  218. if (quirk_unreliable_oscillator && pa == 0x48318000)
  219. continue;
  220. of_node_put(np);
  221. break;
  222. }
  223. /* Usually no need for dmtimer clocksource if we have counter32 */
  224. if (counter_32k >= 0) {
  225. clockevent = pa;
  226. clocksource = 0;
  227. } else {
  228. clocksource = pa;
  229. clockevent = DMTIMER_INST_DONT_CARE;
  230. }
  231. }
  232. /* Finds the first usable dmtimer, used for the don't care case */
  233. static u32 __init dmtimer_systimer_find_first_available(void)
  234. {
  235. struct device_node *np;
  236. u32 pa = 0;
  237. for_each_matching_node(np, dmtimer_match_table) {
  238. struct resource res;
  239. if (!dmtimer_is_preferred(np))
  240. continue;
  241. if (of_address_to_resource(np, 0, &res))
  242. continue;
  243. if (res.start == clocksource || res.start == clockevent)
  244. continue;
  245. pa = res.start;
  246. of_node_put(np);
  247. break;
  248. }
  249. return pa;
  250. }
  251. /* Selects the best clocksource and clockevent to use */
  252. static void __init dmtimer_systimer_select_best(void)
  253. {
  254. dmtimer_systimer_check_counter32k();
  255. dmtimer_systimer_assign_alwon();
  256. if (clockevent == DMTIMER_INST_DONT_CARE)
  257. clockevent = dmtimer_systimer_find_first_available();
  258. pr_debug("%s: counter_32k: %i clocksource: %08x clockevent: %08x\n",
  259. __func__, counter_32k, clocksource, clockevent);
  260. }
  261. /* Interface clocks are only available on some SoCs variants */
  262. static int __init dmtimer_systimer_init_clock(struct dmtimer_systimer *t,
  263. struct device_node *np,
  264. const char *name,
  265. unsigned long *rate)
  266. {
  267. struct clk *clock;
  268. unsigned long r;
  269. bool is_ick = false;
  270. int error;
  271. is_ick = !strncmp(name, "ick", 3);
  272. clock = of_clk_get_by_name(np, name);
  273. if ((PTR_ERR(clock) == -EINVAL) && is_ick)
  274. return 0;
  275. else if (IS_ERR(clock))
  276. return PTR_ERR(clock);
  277. error = clk_prepare_enable(clock);
  278. if (error)
  279. return error;
  280. r = clk_get_rate(clock);
  281. if (!r) {
  282. clk_disable_unprepare(clock);
  283. return -ENODEV;
  284. }
  285. if (is_ick)
  286. t->ick = clock;
  287. else
  288. t->fck = clock;
  289. *rate = r;
  290. return 0;
  291. }
  292. static int __init dmtimer_systimer_setup(struct device_node *np,
  293. struct dmtimer_systimer *t)
  294. {
  295. unsigned long rate;
  296. u8 regbase;
  297. int error;
  298. if (!of_device_is_compatible(np->parent, "ti,sysc"))
  299. return -EINVAL;
  300. t->base = of_iomap(np, 0);
  301. if (!t->base)
  302. return -ENXIO;
  303. /*
  304. * Enable optional assigned-clock-parents configured at the timer
  305. * node level. For regular device drivers, this is done automatically
  306. * by bus related code such as platform_drv_probe().
  307. */
  308. error = of_clk_set_defaults(np, false);
  309. if (error < 0)
  310. pr_err("%s: clock source init failed: %i\n", __func__, error);
  311. /* For ti-sysc, we have timer clocks at the parent module level */
  312. error = dmtimer_systimer_init_clock(t, np->parent, "fck", &rate);
  313. if (error)
  314. goto err_unmap;
  315. t->rate = rate;
  316. error = dmtimer_systimer_init_clock(t, np->parent, "ick", &rate);
  317. if (error)
  318. goto err_unmap;
  319. if (dmtimer_systimer_revision1(t)) {
  320. t->irq_stat = OMAP_TIMER_V1_STAT_OFFSET;
  321. t->irq_ena = OMAP_TIMER_V1_INT_EN_OFFSET;
  322. t->pend = _OMAP_TIMER_WRITE_PEND_OFFSET;
  323. regbase = 0;
  324. } else {
  325. t->irq_stat = OMAP_TIMER_V2_IRQSTATUS;
  326. t->irq_ena = OMAP_TIMER_V2_IRQENABLE_SET;
  327. regbase = OMAP_TIMER_V2_FUNC_OFFSET;
  328. t->pend = regbase + _OMAP_TIMER_WRITE_PEND_OFFSET;
  329. }
  330. t->sysc = OMAP_TIMER_OCP_CFG_OFFSET;
  331. t->load = regbase + _OMAP_TIMER_LOAD_OFFSET;
  332. t->counter = regbase + _OMAP_TIMER_COUNTER_OFFSET;
  333. t->ctrl = regbase + _OMAP_TIMER_CTRL_OFFSET;
  334. t->wakeup = regbase + _OMAP_TIMER_WAKEUP_EN_OFFSET;
  335. t->ifctrl = regbase + _OMAP_TIMER_IF_CTRL_OFFSET;
  336. dmtimer_systimer_reset(t);
  337. dmtimer_systimer_enable(t);
  338. pr_debug("dmtimer rev %08x sysc %08x\n", readl_relaxed(t->base),
  339. readl_relaxed(t->base + t->sysc));
  340. return 0;
  341. err_unmap:
  342. iounmap(t->base);
  343. return error;
  344. }
  345. /* Clockevent */
  346. static struct dmtimer_clockevent *
  347. to_dmtimer_clockevent(struct clock_event_device *clockevent)
  348. {
  349. return container_of(clockevent, struct dmtimer_clockevent, dev);
  350. }
  351. static irqreturn_t dmtimer_clockevent_interrupt(int irq, void *data)
  352. {
  353. struct dmtimer_clockevent *clkevt = data;
  354. struct dmtimer_systimer *t = &clkevt->t;
  355. writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->irq_stat);
  356. clkevt->dev.event_handler(&clkevt->dev);
  357. return IRQ_HANDLED;
  358. }
  359. static int dmtimer_set_next_event(unsigned long cycles,
  360. struct clock_event_device *evt)
  361. {
  362. struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
  363. struct dmtimer_systimer *t = &clkevt->t;
  364. void __iomem *pend = t->base + t->pend;
  365. while (readl_relaxed(pend) & WP_TCRR)
  366. cpu_relax();
  367. writel_relaxed(0xffffffff - cycles, t->base + t->counter);
  368. while (readl_relaxed(pend) & WP_TCLR)
  369. cpu_relax();
  370. writel_relaxed(OMAP_TIMER_CTRL_ST, t->base + t->ctrl);
  371. return 0;
  372. }
  373. static int dmtimer_clockevent_shutdown(struct clock_event_device *evt)
  374. {
  375. struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
  376. struct dmtimer_systimer *t = &clkevt->t;
  377. void __iomem *ctrl = t->base + t->ctrl;
  378. u32 l;
  379. l = readl_relaxed(ctrl);
  380. if (l & OMAP_TIMER_CTRL_ST) {
  381. l &= ~BIT(0);
  382. writel_relaxed(l, ctrl);
  383. /* Flush posted write */
  384. l = readl_relaxed(ctrl);
  385. /* Wait for functional clock period x 3.5 */
  386. udelay(3500000 / t->rate + 1);
  387. }
  388. writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->irq_stat);
  389. return 0;
  390. }
  391. static int dmtimer_set_periodic(struct clock_event_device *evt)
  392. {
  393. struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
  394. struct dmtimer_systimer *t = &clkevt->t;
  395. void __iomem *pend = t->base + t->pend;
  396. dmtimer_clockevent_shutdown(evt);
  397. /* Looks like we need to first set the load value separately */
  398. while (readl_relaxed(pend) & WP_TLDR)
  399. cpu_relax();
  400. writel_relaxed(clkevt->period, t->base + t->load);
  401. while (readl_relaxed(pend) & WP_TCRR)
  402. cpu_relax();
  403. writel_relaxed(clkevt->period, t->base + t->counter);
  404. while (readl_relaxed(pend) & WP_TCLR)
  405. cpu_relax();
  406. writel_relaxed(OMAP_TIMER_CTRL_AR | OMAP_TIMER_CTRL_ST,
  407. t->base + t->ctrl);
  408. return 0;
  409. }
  410. static void omap_clockevent_idle(struct clock_event_device *evt)
  411. {
  412. struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
  413. struct dmtimer_systimer *t = &clkevt->t;
  414. dmtimer_systimer_disable(t);
  415. clk_disable(t->fck);
  416. }
  417. static void omap_clockevent_unidle(struct clock_event_device *evt)
  418. {
  419. struct dmtimer_clockevent *clkevt = to_dmtimer_clockevent(evt);
  420. struct dmtimer_systimer *t = &clkevt->t;
  421. int error;
  422. error = clk_enable(t->fck);
  423. if (error)
  424. pr_err("could not enable timer fck on resume: %i\n", error);
  425. dmtimer_systimer_enable(t);
  426. writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->irq_ena);
  427. writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->wakeup);
  428. }
  429. static int __init dmtimer_clkevt_init_common(struct dmtimer_clockevent *clkevt,
  430. struct device_node *np,
  431. unsigned int features,
  432. const struct cpumask *cpumask,
  433. const char *name,
  434. int rating)
  435. {
  436. struct clock_event_device *dev;
  437. struct dmtimer_systimer *t;
  438. int error;
  439. t = &clkevt->t;
  440. dev = &clkevt->dev;
  441. /*
  442. * We mostly use cpuidle_coupled with ARM local timers for runtime,
  443. * so there's probably no use for CLOCK_EVT_FEAT_DYNIRQ here.
  444. */
  445. dev->features = features;
  446. dev->rating = rating;
  447. dev->set_next_event = dmtimer_set_next_event;
  448. dev->set_state_shutdown = dmtimer_clockevent_shutdown;
  449. dev->set_state_periodic = dmtimer_set_periodic;
  450. dev->set_state_oneshot = dmtimer_clockevent_shutdown;
  451. dev->set_state_oneshot_stopped = dmtimer_clockevent_shutdown;
  452. dev->tick_resume = dmtimer_clockevent_shutdown;
  453. dev->cpumask = cpumask;
  454. dev->irq = irq_of_parse_and_map(np, 0);
  455. if (!dev->irq)
  456. return -ENXIO;
  457. error = dmtimer_systimer_setup(np, &clkevt->t);
  458. if (error)
  459. return error;
  460. clkevt->period = 0xffffffff - DIV_ROUND_CLOSEST(t->rate, HZ);
  461. /*
  462. * For clock-event timers we never read the timer counter and
  463. * so we are not impacted by errata i103 and i767. Therefore,
  464. * we can safely ignore this errata for clock-event timers.
  465. */
  466. writel_relaxed(OMAP_TIMER_CTRL_POSTED, t->base + t->ifctrl);
  467. error = request_irq(dev->irq, dmtimer_clockevent_interrupt,
  468. IRQF_TIMER, name, clkevt);
  469. if (error)
  470. goto err_out_unmap;
  471. writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->irq_ena);
  472. writel_relaxed(OMAP_TIMER_INT_OVERFLOW, t->base + t->wakeup);
  473. pr_info("TI gptimer %s: %s%lu Hz at %pOF\n",
  474. name, of_property_read_bool(np, "ti,timer-alwon") ?
  475. "always-on " : "", t->rate, np->parent);
  476. return 0;
  477. err_out_unmap:
  478. iounmap(t->base);
  479. return error;
  480. }
  481. static int __init dmtimer_clockevent_init(struct device_node *np)
  482. {
  483. struct dmtimer_clockevent *clkevt;
  484. int error;
  485. clkevt = kzalloc(sizeof(*clkevt), GFP_KERNEL);
  486. if (!clkevt)
  487. return -ENOMEM;
  488. error = dmtimer_clkevt_init_common(clkevt, np,
  489. CLOCK_EVT_FEAT_PERIODIC |
  490. CLOCK_EVT_FEAT_ONESHOT,
  491. cpu_possible_mask, "clockevent",
  492. 300);
  493. if (error)
  494. goto err_out_free;
  495. clockevents_config_and_register(&clkevt->dev, clkevt->t.rate,
  496. 3, /* Timer internal resync latency */
  497. 0xffffffff);
  498. if (of_machine_is_compatible("ti,am33xx") ||
  499. of_machine_is_compatible("ti,am43")) {
  500. clkevt->dev.suspend = omap_clockevent_idle;
  501. clkevt->dev.resume = omap_clockevent_unidle;
  502. }
  503. return 0;
  504. err_out_free:
  505. kfree(clkevt);
  506. return error;
  507. }
  508. /* Dmtimer as percpu timer. See dra7 ARM architected timer wrap erratum i940 */
  509. static DEFINE_PER_CPU(struct dmtimer_clockevent, dmtimer_percpu_timer);
  510. static int __init dmtimer_percpu_timer_init(struct device_node *np, int cpu)
  511. {
  512. struct dmtimer_clockevent *clkevt;
  513. int error;
  514. if (!cpu_possible(cpu))
  515. return -EINVAL;
  516. if (!of_property_read_bool(np->parent, "ti,no-reset-on-init") ||
  517. !of_property_read_bool(np->parent, "ti,no-idle"))
  518. pr_warn("Incomplete dtb for percpu dmtimer %pOF\n", np->parent);
  519. clkevt = per_cpu_ptr(&dmtimer_percpu_timer, cpu);
  520. error = dmtimer_clkevt_init_common(clkevt, np, CLOCK_EVT_FEAT_ONESHOT,
  521. cpumask_of(cpu), "percpu-dmtimer",
  522. 500);
  523. if (error)
  524. return error;
  525. return 0;
  526. }
  527. /* See TRM for timer internal resynch latency */
  528. static int omap_dmtimer_starting_cpu(unsigned int cpu)
  529. {
  530. struct dmtimer_clockevent *clkevt = per_cpu_ptr(&dmtimer_percpu_timer, cpu);
  531. struct clock_event_device *dev = &clkevt->dev;
  532. struct dmtimer_systimer *t = &clkevt->t;
  533. clockevents_config_and_register(dev, t->rate, 3, ULONG_MAX);
  534. irq_force_affinity(dev->irq, cpumask_of(cpu));
  535. return 0;
  536. }
  537. static int __init dmtimer_percpu_timer_startup(void)
  538. {
  539. struct dmtimer_clockevent *clkevt = per_cpu_ptr(&dmtimer_percpu_timer, 0);
  540. struct dmtimer_systimer *t = &clkevt->t;
  541. if (t->sysc) {
  542. cpuhp_setup_state(CPUHP_AP_TI_GP_TIMER_STARTING,
  543. "clockevents/omap/gptimer:starting",
  544. omap_dmtimer_starting_cpu, NULL);
  545. }
  546. return 0;
  547. }
  548. subsys_initcall(dmtimer_percpu_timer_startup);
  549. static int __init dmtimer_percpu_quirk_init(struct device_node *np, u32 pa)
  550. {
  551. struct device_node *arm_timer __free(device_node) =
  552. of_find_compatible_node(NULL, NULL, "arm,armv7-timer");
  553. if (of_device_is_available(arm_timer)) {
  554. pr_warn_once("ARM architected timer wrap issue i940 detected\n");
  555. return 0;
  556. }
  557. if (pa == 0x4882c000) /* dra7 dmtimer15 */
  558. return dmtimer_percpu_timer_init(np, 0);
  559. else if (pa == 0x4882e000) /* dra7 dmtimer16 */
  560. return dmtimer_percpu_timer_init(np, 1);
  561. return 0;
  562. }
  563. /* Clocksource */
  564. static struct dmtimer_clocksource *
  565. to_dmtimer_clocksource(struct clocksource *cs)
  566. {
  567. return container_of(cs, struct dmtimer_clocksource, dev);
  568. }
  569. static u64 dmtimer_clocksource_read_cycles(struct clocksource *cs)
  570. {
  571. struct dmtimer_clocksource *clksrc = to_dmtimer_clocksource(cs);
  572. struct dmtimer_systimer *t = &clksrc->t;
  573. return (u64)readl_relaxed(t->base + t->counter);
  574. }
  575. static void __iomem *dmtimer_sched_clock_counter;
  576. static u64 notrace dmtimer_read_sched_clock(void)
  577. {
  578. return readl_relaxed(dmtimer_sched_clock_counter);
  579. }
  580. static void dmtimer_clocksource_suspend(struct clocksource *cs)
  581. {
  582. struct dmtimer_clocksource *clksrc = to_dmtimer_clocksource(cs);
  583. struct dmtimer_systimer *t = &clksrc->t;
  584. clksrc->loadval = readl_relaxed(t->base + t->counter);
  585. dmtimer_systimer_disable(t);
  586. clk_disable(t->fck);
  587. }
  588. static void dmtimer_clocksource_resume(struct clocksource *cs)
  589. {
  590. struct dmtimer_clocksource *clksrc = to_dmtimer_clocksource(cs);
  591. struct dmtimer_systimer *t = &clksrc->t;
  592. int error;
  593. error = clk_enable(t->fck);
  594. if (error)
  595. pr_err("could not enable timer fck on resume: %i\n", error);
  596. dmtimer_systimer_enable(t);
  597. writel_relaxed(clksrc->loadval, t->base + t->counter);
  598. writel_relaxed(OMAP_TIMER_CTRL_ST | OMAP_TIMER_CTRL_AR,
  599. t->base + t->ctrl);
  600. }
  601. static int __init dmtimer_clocksource_init(struct device_node *np)
  602. {
  603. struct dmtimer_clocksource *clksrc;
  604. struct dmtimer_systimer *t;
  605. struct clocksource *dev;
  606. int error;
  607. clksrc = kzalloc(sizeof(*clksrc), GFP_KERNEL);
  608. if (!clksrc)
  609. return -ENOMEM;
  610. dev = &clksrc->dev;
  611. t = &clksrc->t;
  612. error = dmtimer_systimer_setup(np, t);
  613. if (error)
  614. goto err_out_free;
  615. dev->name = "dmtimer";
  616. dev->rating = 300;
  617. dev->read = dmtimer_clocksource_read_cycles;
  618. dev->mask = CLOCKSOURCE_MASK(32);
  619. dev->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  620. /* Unlike for clockevent, legacy code sets suspend only for am4 */
  621. if (of_machine_is_compatible("ti,am43")) {
  622. dev->suspend = dmtimer_clocksource_suspend;
  623. dev->resume = dmtimer_clocksource_resume;
  624. }
  625. writel_relaxed(0, t->base + t->counter);
  626. writel_relaxed(OMAP_TIMER_CTRL_ST | OMAP_TIMER_CTRL_AR,
  627. t->base + t->ctrl);
  628. pr_info("TI gptimer clocksource: %s%pOF\n",
  629. of_property_read_bool(np, "ti,timer-alwon") ?
  630. "always-on " : "", np->parent);
  631. if (!dmtimer_sched_clock_counter) {
  632. dmtimer_sched_clock_counter = t->base + t->counter;
  633. sched_clock_register(dmtimer_read_sched_clock, 32, t->rate);
  634. }
  635. if (clocksource_register_hz(dev, t->rate))
  636. pr_err("Could not register clocksource %pOF\n", np);
  637. return 0;
  638. err_out_free:
  639. kfree(clksrc);
  640. return -ENODEV;
  641. }
  642. /*
  643. * To detect between a clocksource and clockevent, we assume the device tree
  644. * has no interrupts configured for a clocksource timer.
  645. */
  646. static int __init dmtimer_systimer_init(struct device_node *np)
  647. {
  648. struct resource res;
  649. u32 pa;
  650. /* One time init for the preferred timer configuration */
  651. if (!clocksource && !clockevent)
  652. dmtimer_systimer_select_best();
  653. if (!clocksource && !clockevent) {
  654. pr_err("%s: unable to detect system timers, update dtb?\n",
  655. __func__);
  656. return -EINVAL;
  657. }
  658. of_address_to_resource(np, 0, &res);
  659. pa = (u32)res.start;
  660. if (!pa)
  661. return -EINVAL;
  662. if (counter_32k <= 0 && clocksource == pa)
  663. return dmtimer_clocksource_init(np);
  664. if (clockevent == pa)
  665. return dmtimer_clockevent_init(np);
  666. if (of_machine_is_compatible("ti,dra7"))
  667. return dmtimer_percpu_quirk_init(np, pa);
  668. return 0;
  669. }
  670. TIMER_OF_DECLARE(systimer_omap2, "ti,omap2420-timer", dmtimer_systimer_init);
  671. TIMER_OF_DECLARE(systimer_omap3, "ti,omap3430-timer", dmtimer_systimer_init);
  672. TIMER_OF_DECLARE(systimer_omap4, "ti,omap4430-timer", dmtimer_systimer_init);
  673. TIMER_OF_DECLARE(systimer_omap5, "ti,omap5430-timer", dmtimer_systimer_init);
  674. TIMER_OF_DECLARE(systimer_am33x, "ti,am335x-timer", dmtimer_systimer_init);
  675. TIMER_OF_DECLARE(systimer_am3ms, "ti,am335x-timer-1ms", dmtimer_systimer_init);
  676. TIMER_OF_DECLARE(systimer_dm814, "ti,dm814-timer", dmtimer_systimer_init);
  677. TIMER_OF_DECLARE(systimer_dm816, "ti,dm816-timer", dmtimer_systimer_init);