dmtimer.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /*
  2. * linux/arch/arm/plat-omap/dmtimer.c
  3. *
  4. * OMAP Dual-Mode Timers
  5. *
  6. * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
  7. * Tarun Kanti DebBarma <tarun.kanti@ti.com>
  8. * Thara Gopinath <thara@ti.com>
  9. *
  10. * dmtimer adaptation to platform_driver.
  11. *
  12. * Copyright (C) 2005 Nokia Corporation
  13. * OMAP2 support by Juha Yrjola
  14. * API improvements and OMAP2 clock framework support by Timo Teras
  15. *
  16. * Copyright (C) 2009 Texas Instruments
  17. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  18. *
  19. * This program is free software; you can redistribute it and/or modify it
  20. * under the terms of the GNU General Public License as published by the
  21. * Free Software Foundation; either version 2 of the License, or (at your
  22. * option) any later version.
  23. *
  24. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  27. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  29. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * You should have received a copy of the GNU General Public License along
  34. * with this program; if not, write to the Free Software Foundation, Inc.,
  35. * 675 Mass Ave, Cambridge, MA 02139, USA.
  36. */
  37. #include <linux/clk.h>
  38. #include <linux/clk-provider.h>
  39. #include <linux/module.h>
  40. #include <linux/io.h>
  41. #include <linux/device.h>
  42. #include <linux/err.h>
  43. #include <linux/pm_runtime.h>
  44. #include <linux/of.h>
  45. #include <linux/of_device.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/platform_data/dmtimer-omap.h>
  48. #include <plat/dmtimer.h>
  49. static u32 omap_reserved_systimers;
  50. static LIST_HEAD(omap_timer_list);
  51. static DEFINE_SPINLOCK(dm_timer_lock);
  52. enum {
  53. REQUEST_ANY = 0,
  54. REQUEST_BY_ID,
  55. REQUEST_BY_CAP,
  56. REQUEST_BY_NODE,
  57. };
  58. /**
  59. * omap_dm_timer_read_reg - read timer registers in posted and non-posted mode
  60. * @timer: timer pointer over which read operation to perform
  61. * @reg: lowest byte holds the register offset
  62. *
  63. * The posted mode bit is encoded in reg. Note that in posted mode write
  64. * pending bit must be checked. Otherwise a read of a non completed write
  65. * will produce an error.
  66. */
  67. static inline u32 omap_dm_timer_read_reg(struct omap_dm_timer *timer, u32 reg)
  68. {
  69. WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET);
  70. return __omap_dm_timer_read(timer, reg, timer->posted);
  71. }
  72. /**
  73. * omap_dm_timer_write_reg - write timer registers in posted and non-posted mode
  74. * @timer: timer pointer over which write operation is to perform
  75. * @reg: lowest byte holds the register offset
  76. * @value: data to write into the register
  77. *
  78. * The posted mode bit is encoded in reg. Note that in posted mode the write
  79. * pending bit must be checked. Otherwise a write on a register which has a
  80. * pending write will be lost.
  81. */
  82. static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg,
  83. u32 value)
  84. {
  85. WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET);
  86. __omap_dm_timer_write(timer, reg, value, timer->posted);
  87. }
  88. static void omap_timer_restore_context(struct omap_dm_timer *timer)
  89. {
  90. omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG,
  91. timer->context.twer);
  92. omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG,
  93. timer->context.tcrr);
  94. omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG,
  95. timer->context.tldr);
  96. omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG,
  97. timer->context.tmar);
  98. omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG,
  99. timer->context.tsicr);
  100. writel_relaxed(timer->context.tier, timer->irq_ena);
  101. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG,
  102. timer->context.tclr);
  103. }
  104. static int omap_dm_timer_reset(struct omap_dm_timer *timer)
  105. {
  106. u32 l, timeout = 100000;
  107. if (timer->revision != 1)
  108. return -EINVAL;
  109. omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
  110. do {
  111. l = __omap_dm_timer_read(timer,
  112. OMAP_TIMER_V1_SYS_STAT_OFFSET, 0);
  113. } while (!l && timeout--);
  114. if (!timeout) {
  115. dev_err(&timer->pdev->dev, "Timer failed to reset\n");
  116. return -ETIMEDOUT;
  117. }
  118. /* Configure timer for smart-idle mode */
  119. l = __omap_dm_timer_read(timer, OMAP_TIMER_OCP_CFG_OFFSET, 0);
  120. l |= 0x2 << 0x3;
  121. __omap_dm_timer_write(timer, OMAP_TIMER_OCP_CFG_OFFSET, l, 0);
  122. timer->posted = 0;
  123. return 0;
  124. }
  125. static int omap_dm_timer_of_set_source(struct omap_dm_timer *timer)
  126. {
  127. int ret;
  128. struct clk *parent;
  129. /*
  130. * FIXME: OMAP1 devices do not use the clock framework for dmtimers so
  131. * do not call clk_get() for these devices.
  132. */
  133. if (!timer->fclk)
  134. return -ENODEV;
  135. parent = clk_get(&timer->pdev->dev, NULL);
  136. if (IS_ERR(parent))
  137. return -ENODEV;
  138. ret = clk_set_parent(timer->fclk, parent);
  139. if (ret < 0)
  140. pr_err("%s: failed to set parent\n", __func__);
  141. clk_put(parent);
  142. return ret;
  143. }
  144. static int omap_dm_timer_prepare(struct omap_dm_timer *timer)
  145. {
  146. int rc;
  147. /*
  148. * FIXME: OMAP1 devices do not use the clock framework for dmtimers so
  149. * do not call clk_get() for these devices.
  150. */
  151. if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) {
  152. timer->fclk = clk_get(&timer->pdev->dev, "fck");
  153. if (WARN_ON_ONCE(IS_ERR(timer->fclk))) {
  154. dev_err(&timer->pdev->dev, ": No fclk handle.\n");
  155. return -EINVAL;
  156. }
  157. }
  158. omap_dm_timer_enable(timer);
  159. if (timer->capability & OMAP_TIMER_NEEDS_RESET) {
  160. rc = omap_dm_timer_reset(timer);
  161. if (rc) {
  162. omap_dm_timer_disable(timer);
  163. return rc;
  164. }
  165. }
  166. __omap_dm_timer_enable_posted(timer);
  167. omap_dm_timer_disable(timer);
  168. rc = omap_dm_timer_of_set_source(timer);
  169. if (rc == -ENODEV)
  170. return omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
  171. return rc;
  172. }
  173. static inline u32 omap_dm_timer_reserved_systimer(int id)
  174. {
  175. return (omap_reserved_systimers & (1 << (id - 1))) ? 1 : 0;
  176. }
  177. int omap_dm_timer_reserve_systimer(int id)
  178. {
  179. if (omap_dm_timer_reserved_systimer(id))
  180. return -ENODEV;
  181. omap_reserved_systimers |= (1 << (id - 1));
  182. return 0;
  183. }
  184. static struct omap_dm_timer *_omap_dm_timer_request(int req_type, void *data)
  185. {
  186. struct omap_dm_timer *timer = NULL, *t;
  187. struct device_node *np = NULL;
  188. unsigned long flags;
  189. u32 cap = 0;
  190. int id = 0;
  191. switch (req_type) {
  192. case REQUEST_BY_ID:
  193. id = *(int *)data;
  194. break;
  195. case REQUEST_BY_CAP:
  196. cap = *(u32 *)data;
  197. break;
  198. case REQUEST_BY_NODE:
  199. np = (struct device_node *)data;
  200. break;
  201. default:
  202. /* REQUEST_ANY */
  203. break;
  204. }
  205. spin_lock_irqsave(&dm_timer_lock, flags);
  206. list_for_each_entry(t, &omap_timer_list, node) {
  207. if (t->reserved)
  208. continue;
  209. switch (req_type) {
  210. case REQUEST_BY_ID:
  211. if (id == t->pdev->id) {
  212. timer = t;
  213. timer->reserved = 1;
  214. goto found;
  215. }
  216. break;
  217. case REQUEST_BY_CAP:
  218. if (cap == (t->capability & cap)) {
  219. /*
  220. * If timer is not NULL, we have already found
  221. * one timer but it was not an exact match
  222. * because it had more capabilites that what
  223. * was required. Therefore, unreserve the last
  224. * timer found and see if this one is a better
  225. * match.
  226. */
  227. if (timer)
  228. timer->reserved = 0;
  229. timer = t;
  230. timer->reserved = 1;
  231. /* Exit loop early if we find an exact match */
  232. if (t->capability == cap)
  233. goto found;
  234. }
  235. break;
  236. case REQUEST_BY_NODE:
  237. if (np == t->pdev->dev.of_node) {
  238. timer = t;
  239. timer->reserved = 1;
  240. goto found;
  241. }
  242. break;
  243. default:
  244. /* REQUEST_ANY */
  245. timer = t;
  246. timer->reserved = 1;
  247. goto found;
  248. }
  249. }
  250. found:
  251. spin_unlock_irqrestore(&dm_timer_lock, flags);
  252. if (timer && omap_dm_timer_prepare(timer)) {
  253. timer->reserved = 0;
  254. timer = NULL;
  255. }
  256. if (!timer)
  257. pr_debug("%s: timer request failed!\n", __func__);
  258. return timer;
  259. }
  260. struct omap_dm_timer *omap_dm_timer_request(void)
  261. {
  262. return _omap_dm_timer_request(REQUEST_ANY, NULL);
  263. }
  264. EXPORT_SYMBOL_GPL(omap_dm_timer_request);
  265. struct omap_dm_timer *omap_dm_timer_request_specific(int id)
  266. {
  267. /* Requesting timer by ID is not supported when device tree is used */
  268. if (of_have_populated_dt()) {
  269. pr_warn("%s: Please use omap_dm_timer_request_by_cap/node()\n",
  270. __func__);
  271. return NULL;
  272. }
  273. return _omap_dm_timer_request(REQUEST_BY_ID, &id);
  274. }
  275. EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific);
  276. /**
  277. * omap_dm_timer_request_by_cap - Request a timer by capability
  278. * @cap: Bit mask of capabilities to match
  279. *
  280. * Find a timer based upon capabilities bit mask. Callers of this function
  281. * should use the definitions found in the plat/dmtimer.h file under the
  282. * comment "timer capabilities used in hwmod database". Returns pointer to
  283. * timer handle on success and a NULL pointer on failure.
  284. */
  285. struct omap_dm_timer *omap_dm_timer_request_by_cap(u32 cap)
  286. {
  287. return _omap_dm_timer_request(REQUEST_BY_CAP, &cap);
  288. }
  289. EXPORT_SYMBOL_GPL(omap_dm_timer_request_by_cap);
  290. /**
  291. * omap_dm_timer_request_by_node - Request a timer by device-tree node
  292. * @np: Pointer to device-tree timer node
  293. *
  294. * Request a timer based upon a device node pointer. Returns pointer to
  295. * timer handle on success and a NULL pointer on failure.
  296. */
  297. struct omap_dm_timer *omap_dm_timer_request_by_node(struct device_node *np)
  298. {
  299. if (!np)
  300. return NULL;
  301. return _omap_dm_timer_request(REQUEST_BY_NODE, np);
  302. }
  303. EXPORT_SYMBOL_GPL(omap_dm_timer_request_by_node);
  304. int omap_dm_timer_free(struct omap_dm_timer *timer)
  305. {
  306. if (unlikely(!timer))
  307. return -EINVAL;
  308. clk_put(timer->fclk);
  309. WARN_ON(!timer->reserved);
  310. timer->reserved = 0;
  311. return 0;
  312. }
  313. EXPORT_SYMBOL_GPL(omap_dm_timer_free);
  314. void omap_dm_timer_enable(struct omap_dm_timer *timer)
  315. {
  316. int c;
  317. pm_runtime_get_sync(&timer->pdev->dev);
  318. if (!(timer->capability & OMAP_TIMER_ALWON)) {
  319. if (timer->get_context_loss_count) {
  320. c = timer->get_context_loss_count(&timer->pdev->dev);
  321. if (c != timer->ctx_loss_count) {
  322. omap_timer_restore_context(timer);
  323. timer->ctx_loss_count = c;
  324. }
  325. } else {
  326. omap_timer_restore_context(timer);
  327. }
  328. }
  329. }
  330. EXPORT_SYMBOL_GPL(omap_dm_timer_enable);
  331. void omap_dm_timer_disable(struct omap_dm_timer *timer)
  332. {
  333. pm_runtime_put_sync(&timer->pdev->dev);
  334. }
  335. EXPORT_SYMBOL_GPL(omap_dm_timer_disable);
  336. int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
  337. {
  338. if (timer)
  339. return timer->irq;
  340. return -EINVAL;
  341. }
  342. EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq);
  343. #if defined(CONFIG_ARCH_OMAP1)
  344. #include <mach/hardware.h>
  345. /**
  346. * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
  347. * @inputmask: current value of idlect mask
  348. */
  349. __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
  350. {
  351. int i = 0;
  352. struct omap_dm_timer *timer = NULL;
  353. unsigned long flags;
  354. /* If ARMXOR cannot be idled this function call is unnecessary */
  355. if (!(inputmask & (1 << 1)))
  356. return inputmask;
  357. /* If any active timer is using ARMXOR return modified mask */
  358. spin_lock_irqsave(&dm_timer_lock, flags);
  359. list_for_each_entry(timer, &omap_timer_list, node) {
  360. u32 l;
  361. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  362. if (l & OMAP_TIMER_CTRL_ST) {
  363. if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
  364. inputmask &= ~(1 << 1);
  365. else
  366. inputmask &= ~(1 << 2);
  367. }
  368. i++;
  369. }
  370. spin_unlock_irqrestore(&dm_timer_lock, flags);
  371. return inputmask;
  372. }
  373. EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
  374. #else
  375. struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
  376. {
  377. if (timer && !IS_ERR(timer->fclk))
  378. return timer->fclk;
  379. return NULL;
  380. }
  381. EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk);
  382. __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
  383. {
  384. BUG();
  385. return 0;
  386. }
  387. EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
  388. #endif
  389. int omap_dm_timer_trigger(struct omap_dm_timer *timer)
  390. {
  391. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
  392. pr_err("%s: timer not available or enabled.\n", __func__);
  393. return -EINVAL;
  394. }
  395. omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
  396. return 0;
  397. }
  398. EXPORT_SYMBOL_GPL(omap_dm_timer_trigger);
  399. int omap_dm_timer_start(struct omap_dm_timer *timer)
  400. {
  401. u32 l;
  402. if (unlikely(!timer))
  403. return -EINVAL;
  404. omap_dm_timer_enable(timer);
  405. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  406. if (!(l & OMAP_TIMER_CTRL_ST)) {
  407. l |= OMAP_TIMER_CTRL_ST;
  408. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  409. }
  410. /* Save the context */
  411. timer->context.tclr = l;
  412. return 0;
  413. }
  414. EXPORT_SYMBOL_GPL(omap_dm_timer_start);
  415. int omap_dm_timer_stop(struct omap_dm_timer *timer)
  416. {
  417. unsigned long rate = 0;
  418. if (unlikely(!timer))
  419. return -EINVAL;
  420. if (!(timer->capability & OMAP_TIMER_NEEDS_RESET))
  421. rate = clk_get_rate(timer->fclk);
  422. __omap_dm_timer_stop(timer, timer->posted, rate);
  423. /*
  424. * Since the register values are computed and written within
  425. * __omap_dm_timer_stop, we need to use read to retrieve the
  426. * context.
  427. */
  428. timer->context.tclr =
  429. omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  430. omap_dm_timer_disable(timer);
  431. return 0;
  432. }
  433. EXPORT_SYMBOL_GPL(omap_dm_timer_stop);
  434. int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
  435. {
  436. int ret;
  437. char *parent_name = NULL;
  438. struct clk *parent;
  439. struct dmtimer_platform_data *pdata;
  440. if (unlikely(!timer))
  441. return -EINVAL;
  442. pdata = timer->pdev->dev.platform_data;
  443. if (source < 0 || source >= 3)
  444. return -EINVAL;
  445. /*
  446. * FIXME: Used for OMAP1 devices only because they do not currently
  447. * use the clock framework to set the parent clock. To be removed
  448. * once OMAP1 migrated to using clock framework for dmtimers
  449. */
  450. if (pdata && pdata->set_timer_src)
  451. return pdata->set_timer_src(timer->pdev, source);
  452. if (IS_ERR(timer->fclk))
  453. return -EINVAL;
  454. #if defined(CONFIG_COMMON_CLK)
  455. /* Check if the clock has configurable parents */
  456. if (clk_hw_get_num_parents(__clk_get_hw(timer->fclk)) < 2)
  457. return 0;
  458. #endif
  459. switch (source) {
  460. case OMAP_TIMER_SRC_SYS_CLK:
  461. parent_name = "timer_sys_ck";
  462. break;
  463. case OMAP_TIMER_SRC_32_KHZ:
  464. parent_name = "timer_32k_ck";
  465. break;
  466. case OMAP_TIMER_SRC_EXT_CLK:
  467. parent_name = "timer_ext_ck";
  468. break;
  469. }
  470. parent = clk_get(&timer->pdev->dev, parent_name);
  471. if (IS_ERR(parent)) {
  472. pr_err("%s: %s not found\n", __func__, parent_name);
  473. return -EINVAL;
  474. }
  475. ret = clk_set_parent(timer->fclk, parent);
  476. if (ret < 0)
  477. pr_err("%s: failed to set %s as parent\n", __func__,
  478. parent_name);
  479. clk_put(parent);
  480. return ret;
  481. }
  482. EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
  483. int omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
  484. unsigned int load)
  485. {
  486. u32 l;
  487. if (unlikely(!timer))
  488. return -EINVAL;
  489. omap_dm_timer_enable(timer);
  490. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  491. if (autoreload)
  492. l |= OMAP_TIMER_CTRL_AR;
  493. else
  494. l &= ~OMAP_TIMER_CTRL_AR;
  495. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  496. omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
  497. omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
  498. /* Save the context */
  499. timer->context.tclr = l;
  500. timer->context.tldr = load;
  501. omap_dm_timer_disable(timer);
  502. return 0;
  503. }
  504. EXPORT_SYMBOL_GPL(omap_dm_timer_set_load);
  505. /* Optimized set_load which removes costly spin wait in timer_start */
  506. int omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
  507. unsigned int load)
  508. {
  509. u32 l;
  510. if (unlikely(!timer))
  511. return -EINVAL;
  512. omap_dm_timer_enable(timer);
  513. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  514. if (autoreload) {
  515. l |= OMAP_TIMER_CTRL_AR;
  516. omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
  517. } else {
  518. l &= ~OMAP_TIMER_CTRL_AR;
  519. }
  520. l |= OMAP_TIMER_CTRL_ST;
  521. __omap_dm_timer_load_start(timer, l, load, timer->posted);
  522. /* Save the context */
  523. timer->context.tclr = l;
  524. timer->context.tldr = load;
  525. timer->context.tcrr = load;
  526. return 0;
  527. }
  528. EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start);
  529. int omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
  530. unsigned int match)
  531. {
  532. u32 l;
  533. if (unlikely(!timer))
  534. return -EINVAL;
  535. omap_dm_timer_enable(timer);
  536. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  537. if (enable)
  538. l |= OMAP_TIMER_CTRL_CE;
  539. else
  540. l &= ~OMAP_TIMER_CTRL_CE;
  541. omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
  542. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  543. /* Save the context */
  544. timer->context.tclr = l;
  545. timer->context.tmar = match;
  546. omap_dm_timer_disable(timer);
  547. return 0;
  548. }
  549. EXPORT_SYMBOL_GPL(omap_dm_timer_set_match);
  550. int omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
  551. int toggle, int trigger)
  552. {
  553. u32 l;
  554. if (unlikely(!timer))
  555. return -EINVAL;
  556. omap_dm_timer_enable(timer);
  557. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  558. l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
  559. OMAP_TIMER_CTRL_PT | (0x03 << 10));
  560. if (def_on)
  561. l |= OMAP_TIMER_CTRL_SCPWM;
  562. if (toggle)
  563. l |= OMAP_TIMER_CTRL_PT;
  564. l |= trigger << 10;
  565. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  566. /* Save the context */
  567. timer->context.tclr = l;
  568. omap_dm_timer_disable(timer);
  569. return 0;
  570. }
  571. EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm);
  572. int omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
  573. {
  574. u32 l;
  575. if (unlikely(!timer))
  576. return -EINVAL;
  577. omap_dm_timer_enable(timer);
  578. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  579. l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
  580. if (prescaler >= 0x00 && prescaler <= 0x07) {
  581. l |= OMAP_TIMER_CTRL_PRE;
  582. l |= prescaler << 2;
  583. }
  584. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  585. /* Save the context */
  586. timer->context.tclr = l;
  587. omap_dm_timer_disable(timer);
  588. return 0;
  589. }
  590. EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler);
  591. int omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
  592. unsigned int value)
  593. {
  594. if (unlikely(!timer))
  595. return -EINVAL;
  596. omap_dm_timer_enable(timer);
  597. __omap_dm_timer_int_enable(timer, value);
  598. /* Save the context */
  599. timer->context.tier = value;
  600. timer->context.twer = value;
  601. omap_dm_timer_disable(timer);
  602. return 0;
  603. }
  604. EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable);
  605. /**
  606. * omap_dm_timer_set_int_disable - disable timer interrupts
  607. * @timer: pointer to timer handle
  608. * @mask: bit mask of interrupts to be disabled
  609. *
  610. * Disables the specified timer interrupts for a timer.
  611. */
  612. int omap_dm_timer_set_int_disable(struct omap_dm_timer *timer, u32 mask)
  613. {
  614. u32 l = mask;
  615. if (unlikely(!timer))
  616. return -EINVAL;
  617. omap_dm_timer_enable(timer);
  618. if (timer->revision == 1)
  619. l = readl_relaxed(timer->irq_ena) & ~mask;
  620. writel_relaxed(l, timer->irq_dis);
  621. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_WAKEUP_EN_REG) & ~mask;
  622. omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, l);
  623. /* Save the context */
  624. timer->context.tier &= ~mask;
  625. timer->context.twer &= ~mask;
  626. omap_dm_timer_disable(timer);
  627. return 0;
  628. }
  629. EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_disable);
  630. unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
  631. {
  632. unsigned int l;
  633. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
  634. pr_err("%s: timer not available or enabled.\n", __func__);
  635. return 0;
  636. }
  637. l = readl_relaxed(timer->irq_stat);
  638. return l;
  639. }
  640. EXPORT_SYMBOL_GPL(omap_dm_timer_read_status);
  641. int omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
  642. {
  643. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev)))
  644. return -EINVAL;
  645. __omap_dm_timer_write_status(timer, value);
  646. return 0;
  647. }
  648. EXPORT_SYMBOL_GPL(omap_dm_timer_write_status);
  649. unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
  650. {
  651. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
  652. pr_err("%s: timer not iavailable or enabled.\n", __func__);
  653. return 0;
  654. }
  655. return __omap_dm_timer_read_counter(timer, timer->posted);
  656. }
  657. EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter);
  658. int omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
  659. {
  660. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
  661. pr_err("%s: timer not available or enabled.\n", __func__);
  662. return -EINVAL;
  663. }
  664. omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
  665. /* Save the context */
  666. timer->context.tcrr = value;
  667. return 0;
  668. }
  669. EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter);
  670. int omap_dm_timers_active(void)
  671. {
  672. struct omap_dm_timer *timer;
  673. list_for_each_entry(timer, &omap_timer_list, node) {
  674. if (!timer->reserved)
  675. continue;
  676. if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
  677. OMAP_TIMER_CTRL_ST) {
  678. return 1;
  679. }
  680. }
  681. return 0;
  682. }
  683. EXPORT_SYMBOL_GPL(omap_dm_timers_active);
  684. static const struct of_device_id omap_timer_match[];
  685. /**
  686. * omap_dm_timer_probe - probe function called for every registered device
  687. * @pdev: pointer to current timer platform device
  688. *
  689. * Called by driver framework at the end of device registration for all
  690. * timer devices.
  691. */
  692. static int omap_dm_timer_probe(struct platform_device *pdev)
  693. {
  694. unsigned long flags;
  695. struct omap_dm_timer *timer;
  696. struct resource *mem, *irq;
  697. struct device *dev = &pdev->dev;
  698. const struct of_device_id *match;
  699. const struct dmtimer_platform_data *pdata;
  700. int ret;
  701. match = of_match_device(of_match_ptr(omap_timer_match), dev);
  702. pdata = match ? match->data : dev->platform_data;
  703. if (!pdata && !dev->of_node) {
  704. dev_err(dev, "%s: no platform data.\n", __func__);
  705. return -ENODEV;
  706. }
  707. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  708. if (unlikely(!irq)) {
  709. dev_err(dev, "%s: no IRQ resource.\n", __func__);
  710. return -ENODEV;
  711. }
  712. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  713. if (unlikely(!mem)) {
  714. dev_err(dev, "%s: no memory resource.\n", __func__);
  715. return -ENODEV;
  716. }
  717. timer = devm_kzalloc(dev, sizeof(struct omap_dm_timer), GFP_KERNEL);
  718. if (!timer) {
  719. dev_err(dev, "%s: memory alloc failed!\n", __func__);
  720. return -ENOMEM;
  721. }
  722. timer->fclk = ERR_PTR(-ENODEV);
  723. timer->io_base = devm_ioremap_resource(dev, mem);
  724. if (IS_ERR(timer->io_base))
  725. return PTR_ERR(timer->io_base);
  726. if (dev->of_node) {
  727. if (of_find_property(dev->of_node, "ti,timer-alwon", NULL))
  728. timer->capability |= OMAP_TIMER_ALWON;
  729. if (of_find_property(dev->of_node, "ti,timer-dsp", NULL))
  730. timer->capability |= OMAP_TIMER_HAS_DSP_IRQ;
  731. if (of_find_property(dev->of_node, "ti,timer-pwm", NULL))
  732. timer->capability |= OMAP_TIMER_HAS_PWM;
  733. if (of_find_property(dev->of_node, "ti,timer-secure", NULL))
  734. timer->capability |= OMAP_TIMER_SECURE;
  735. } else {
  736. timer->id = pdev->id;
  737. timer->capability = pdata->timer_capability;
  738. timer->reserved = omap_dm_timer_reserved_systimer(timer->id);
  739. timer->get_context_loss_count = pdata->get_context_loss_count;
  740. }
  741. if (pdata)
  742. timer->errata = pdata->timer_errata;
  743. timer->irq = irq->start;
  744. timer->pdev = pdev;
  745. pm_runtime_enable(dev);
  746. pm_runtime_irq_safe(dev);
  747. if (!timer->reserved) {
  748. ret = pm_runtime_get_sync(dev);
  749. if (ret < 0) {
  750. dev_err(dev, "%s: pm_runtime_get_sync failed!\n",
  751. __func__);
  752. goto err_get_sync;
  753. }
  754. __omap_dm_timer_init_regs(timer);
  755. pm_runtime_put(dev);
  756. }
  757. /* add the timer element to the list */
  758. spin_lock_irqsave(&dm_timer_lock, flags);
  759. list_add_tail(&timer->node, &omap_timer_list);
  760. spin_unlock_irqrestore(&dm_timer_lock, flags);
  761. dev_dbg(dev, "Device Probed.\n");
  762. return 0;
  763. err_get_sync:
  764. pm_runtime_put_noidle(dev);
  765. pm_runtime_disable(dev);
  766. return ret;
  767. }
  768. /**
  769. * omap_dm_timer_remove - cleanup a registered timer device
  770. * @pdev: pointer to current timer platform device
  771. *
  772. * Called by driver framework whenever a timer device is unregistered.
  773. * In addition to freeing platform resources it also deletes the timer
  774. * entry from the local list.
  775. */
  776. static int omap_dm_timer_remove(struct platform_device *pdev)
  777. {
  778. struct omap_dm_timer *timer;
  779. unsigned long flags;
  780. int ret = -EINVAL;
  781. spin_lock_irqsave(&dm_timer_lock, flags);
  782. list_for_each_entry(timer, &omap_timer_list, node)
  783. if (!strcmp(dev_name(&timer->pdev->dev),
  784. dev_name(&pdev->dev))) {
  785. list_del(&timer->node);
  786. ret = 0;
  787. break;
  788. }
  789. spin_unlock_irqrestore(&dm_timer_lock, flags);
  790. pm_runtime_disable(&pdev->dev);
  791. return ret;
  792. }
  793. static const struct dmtimer_platform_data omap3plus_pdata = {
  794. .timer_errata = OMAP_TIMER_ERRATA_I103_I767,
  795. };
  796. static const struct of_device_id omap_timer_match[] = {
  797. {
  798. .compatible = "ti,omap2420-timer",
  799. },
  800. {
  801. .compatible = "ti,omap3430-timer",
  802. .data = &omap3plus_pdata,
  803. },
  804. {
  805. .compatible = "ti,omap4430-timer",
  806. .data = &omap3plus_pdata,
  807. },
  808. {
  809. .compatible = "ti,omap5430-timer",
  810. .data = &omap3plus_pdata,
  811. },
  812. {
  813. .compatible = "ti,am335x-timer",
  814. .data = &omap3plus_pdata,
  815. },
  816. {
  817. .compatible = "ti,am335x-timer-1ms",
  818. .data = &omap3plus_pdata,
  819. },
  820. {
  821. .compatible = "ti,dm816-timer",
  822. .data = &omap3plus_pdata,
  823. },
  824. {},
  825. };
  826. MODULE_DEVICE_TABLE(of, omap_timer_match);
  827. static struct platform_driver omap_dm_timer_driver = {
  828. .probe = omap_dm_timer_probe,
  829. .remove = omap_dm_timer_remove,
  830. .driver = {
  831. .name = "omap_timer",
  832. .of_match_table = of_match_ptr(omap_timer_match),
  833. },
  834. };
  835. early_platform_init("earlytimer", &omap_dm_timer_driver);
  836. module_platform_driver(omap_dm_timer_driver);
  837. MODULE_DESCRIPTION("OMAP Dual-Mode Timer Driver");
  838. MODULE_LICENSE("GPL");
  839. MODULE_ALIAS("platform:" DRIVER_NAME);
  840. MODULE_AUTHOR("Texas Instruments Inc");