dmtimer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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/module.h>
  38. #include <linux/io.h>
  39. #include <linux/slab.h>
  40. #include <linux/err.h>
  41. #include <linux/pm_runtime.h>
  42. #include <plat/dmtimer.h>
  43. #include <mach/hardware.h>
  44. static LIST_HEAD(omap_timer_list);
  45. static DEFINE_SPINLOCK(dm_timer_lock);
  46. /**
  47. * omap_dm_timer_read_reg - read timer registers in posted and non-posted mode
  48. * @timer: timer pointer over which read operation to perform
  49. * @reg: lowest byte holds the register offset
  50. *
  51. * The posted mode bit is encoded in reg. Note that in posted mode write
  52. * pending bit must be checked. Otherwise a read of a non completed write
  53. * will produce an error.
  54. */
  55. static inline u32 omap_dm_timer_read_reg(struct omap_dm_timer *timer, u32 reg)
  56. {
  57. WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET);
  58. return __omap_dm_timer_read(timer, reg, timer->posted);
  59. }
  60. /**
  61. * omap_dm_timer_write_reg - write timer registers in posted and non-posted mode
  62. * @timer: timer pointer over which write operation is to perform
  63. * @reg: lowest byte holds the register offset
  64. * @value: data to write into the register
  65. *
  66. * The posted mode bit is encoded in reg. Note that in posted mode the write
  67. * pending bit must be checked. Otherwise a write on a register which has a
  68. * pending write will be lost.
  69. */
  70. static void omap_dm_timer_write_reg(struct omap_dm_timer *timer, u32 reg,
  71. u32 value)
  72. {
  73. WARN_ON((reg & 0xff) < _OMAP_TIMER_WAKEUP_EN_OFFSET);
  74. __omap_dm_timer_write(timer, reg, value, timer->posted);
  75. }
  76. static void omap_timer_restore_context(struct omap_dm_timer *timer)
  77. {
  78. __raw_writel(timer->context.tiocp_cfg,
  79. timer->io_base + OMAP_TIMER_OCP_CFG_OFFSET);
  80. if (timer->revision == 1)
  81. __raw_writel(timer->context.tistat, timer->sys_stat);
  82. __raw_writel(timer->context.tisr, timer->irq_stat);
  83. omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG,
  84. timer->context.twer);
  85. omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG,
  86. timer->context.tcrr);
  87. omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG,
  88. timer->context.tldr);
  89. omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG,
  90. timer->context.tmar);
  91. omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG,
  92. timer->context.tsicr);
  93. __raw_writel(timer->context.tier, timer->irq_ena);
  94. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG,
  95. timer->context.tclr);
  96. }
  97. static void omap_dm_timer_wait_for_reset(struct omap_dm_timer *timer)
  98. {
  99. int c;
  100. if (!timer->sys_stat)
  101. return;
  102. c = 0;
  103. while (!(__raw_readl(timer->sys_stat) & 1)) {
  104. c++;
  105. if (c > 100000) {
  106. printk(KERN_ERR "Timer failed to reset\n");
  107. return;
  108. }
  109. }
  110. }
  111. static void omap_dm_timer_reset(struct omap_dm_timer *timer)
  112. {
  113. omap_dm_timer_enable(timer);
  114. if (timer->pdev->id != 1) {
  115. omap_dm_timer_write_reg(timer, OMAP_TIMER_IF_CTRL_REG, 0x06);
  116. omap_dm_timer_wait_for_reset(timer);
  117. }
  118. __omap_dm_timer_reset(timer, 0, 0);
  119. omap_dm_timer_disable(timer);
  120. timer->posted = 1;
  121. }
  122. int omap_dm_timer_prepare(struct omap_dm_timer *timer)
  123. {
  124. struct dmtimer_platform_data *pdata = timer->pdev->dev.platform_data;
  125. int ret;
  126. timer->fclk = clk_get(&timer->pdev->dev, "fck");
  127. if (WARN_ON_ONCE(IS_ERR_OR_NULL(timer->fclk))) {
  128. timer->fclk = NULL;
  129. dev_err(&timer->pdev->dev, ": No fclk handle.\n");
  130. return -EINVAL;
  131. }
  132. if (pdata->needs_manual_reset)
  133. omap_dm_timer_reset(timer);
  134. ret = omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
  135. timer->posted = 1;
  136. return ret;
  137. }
  138. struct omap_dm_timer *omap_dm_timer_request(void)
  139. {
  140. struct omap_dm_timer *timer = NULL, *t;
  141. unsigned long flags;
  142. int ret = 0;
  143. spin_lock_irqsave(&dm_timer_lock, flags);
  144. list_for_each_entry(t, &omap_timer_list, node) {
  145. if (t->reserved)
  146. continue;
  147. timer = t;
  148. timer->reserved = 1;
  149. break;
  150. }
  151. if (timer) {
  152. ret = omap_dm_timer_prepare(timer);
  153. if (ret) {
  154. timer->reserved = 0;
  155. timer = NULL;
  156. }
  157. }
  158. spin_unlock_irqrestore(&dm_timer_lock, flags);
  159. if (!timer)
  160. pr_debug("%s: timer request failed!\n", __func__);
  161. return timer;
  162. }
  163. EXPORT_SYMBOL_GPL(omap_dm_timer_request);
  164. struct omap_dm_timer *omap_dm_timer_request_specific(int id)
  165. {
  166. struct omap_dm_timer *timer = NULL, *t;
  167. unsigned long flags;
  168. int ret = 0;
  169. spin_lock_irqsave(&dm_timer_lock, flags);
  170. list_for_each_entry(t, &omap_timer_list, node) {
  171. if (t->pdev->id == id && !t->reserved) {
  172. timer = t;
  173. timer->reserved = 1;
  174. break;
  175. }
  176. }
  177. if (timer) {
  178. ret = omap_dm_timer_prepare(timer);
  179. if (ret) {
  180. timer->reserved = 0;
  181. timer = NULL;
  182. }
  183. }
  184. spin_unlock_irqrestore(&dm_timer_lock, flags);
  185. if (!timer)
  186. pr_debug("%s: timer%d request failed!\n", __func__, id);
  187. return timer;
  188. }
  189. EXPORT_SYMBOL_GPL(omap_dm_timer_request_specific);
  190. int omap_dm_timer_free(struct omap_dm_timer *timer)
  191. {
  192. if (unlikely(!timer))
  193. return -EINVAL;
  194. clk_put(timer->fclk);
  195. WARN_ON(!timer->reserved);
  196. timer->reserved = 0;
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(omap_dm_timer_free);
  200. void omap_dm_timer_enable(struct omap_dm_timer *timer)
  201. {
  202. pm_runtime_get_sync(&timer->pdev->dev);
  203. }
  204. EXPORT_SYMBOL_GPL(omap_dm_timer_enable);
  205. void omap_dm_timer_disable(struct omap_dm_timer *timer)
  206. {
  207. pm_runtime_put_sync(&timer->pdev->dev);
  208. }
  209. EXPORT_SYMBOL_GPL(omap_dm_timer_disable);
  210. int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
  211. {
  212. if (timer)
  213. return timer->irq;
  214. return -EINVAL;
  215. }
  216. EXPORT_SYMBOL_GPL(omap_dm_timer_get_irq);
  217. #if defined(CONFIG_ARCH_OMAP1)
  218. /**
  219. * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
  220. * @inputmask: current value of idlect mask
  221. */
  222. __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
  223. {
  224. int i = 0;
  225. struct omap_dm_timer *timer = NULL;
  226. unsigned long flags;
  227. /* If ARMXOR cannot be idled this function call is unnecessary */
  228. if (!(inputmask & (1 << 1)))
  229. return inputmask;
  230. /* If any active timer is using ARMXOR return modified mask */
  231. spin_lock_irqsave(&dm_timer_lock, flags);
  232. list_for_each_entry(timer, &omap_timer_list, node) {
  233. u32 l;
  234. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  235. if (l & OMAP_TIMER_CTRL_ST) {
  236. if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
  237. inputmask &= ~(1 << 1);
  238. else
  239. inputmask &= ~(1 << 2);
  240. }
  241. i++;
  242. }
  243. spin_unlock_irqrestore(&dm_timer_lock, flags);
  244. return inputmask;
  245. }
  246. EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
  247. #else
  248. struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
  249. {
  250. if (timer)
  251. return timer->fclk;
  252. return NULL;
  253. }
  254. EXPORT_SYMBOL_GPL(omap_dm_timer_get_fclk);
  255. __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
  256. {
  257. BUG();
  258. return 0;
  259. }
  260. EXPORT_SYMBOL_GPL(omap_dm_timer_modify_idlect_mask);
  261. #endif
  262. int omap_dm_timer_trigger(struct omap_dm_timer *timer)
  263. {
  264. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
  265. pr_err("%s: timer not available or enabled.\n", __func__);
  266. return -EINVAL;
  267. }
  268. omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
  269. return 0;
  270. }
  271. EXPORT_SYMBOL_GPL(omap_dm_timer_trigger);
  272. int omap_dm_timer_start(struct omap_dm_timer *timer)
  273. {
  274. u32 l;
  275. if (unlikely(!timer))
  276. return -EINVAL;
  277. omap_dm_timer_enable(timer);
  278. if (timer->loses_context) {
  279. u32 ctx_loss_cnt_after =
  280. timer->get_context_loss_count(&timer->pdev->dev);
  281. if (ctx_loss_cnt_after != timer->ctx_loss_count)
  282. omap_timer_restore_context(timer);
  283. }
  284. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  285. if (!(l & OMAP_TIMER_CTRL_ST)) {
  286. l |= OMAP_TIMER_CTRL_ST;
  287. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  288. }
  289. /* Save the context */
  290. timer->context.tclr = l;
  291. return 0;
  292. }
  293. EXPORT_SYMBOL_GPL(omap_dm_timer_start);
  294. int omap_dm_timer_stop(struct omap_dm_timer *timer)
  295. {
  296. unsigned long rate = 0;
  297. struct dmtimer_platform_data *pdata = timer->pdev->dev.platform_data;
  298. if (unlikely(!timer))
  299. return -EINVAL;
  300. if (!pdata->needs_manual_reset)
  301. rate = clk_get_rate(timer->fclk);
  302. __omap_dm_timer_stop(timer, timer->posted, rate);
  303. if (timer->loses_context && timer->get_context_loss_count)
  304. timer->ctx_loss_count =
  305. timer->get_context_loss_count(&timer->pdev->dev);
  306. /*
  307. * Since the register values are computed and written within
  308. * __omap_dm_timer_stop, we need to use read to retrieve the
  309. * context.
  310. */
  311. timer->context.tclr =
  312. omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  313. timer->context.tisr = __raw_readl(timer->irq_stat);
  314. omap_dm_timer_disable(timer);
  315. return 0;
  316. }
  317. EXPORT_SYMBOL_GPL(omap_dm_timer_stop);
  318. int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
  319. {
  320. int ret;
  321. struct dmtimer_platform_data *pdata;
  322. if (unlikely(!timer))
  323. return -EINVAL;
  324. pdata = timer->pdev->dev.platform_data;
  325. if (source < 0 || source >= 3)
  326. return -EINVAL;
  327. ret = pdata->set_timer_src(timer->pdev, source);
  328. return ret;
  329. }
  330. EXPORT_SYMBOL_GPL(omap_dm_timer_set_source);
  331. int omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
  332. unsigned int load)
  333. {
  334. u32 l;
  335. if (unlikely(!timer))
  336. return -EINVAL;
  337. omap_dm_timer_enable(timer);
  338. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  339. if (autoreload)
  340. l |= OMAP_TIMER_CTRL_AR;
  341. else
  342. l &= ~OMAP_TIMER_CTRL_AR;
  343. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  344. omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
  345. omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
  346. /* Save the context */
  347. timer->context.tclr = l;
  348. timer->context.tldr = load;
  349. omap_dm_timer_disable(timer);
  350. return 0;
  351. }
  352. EXPORT_SYMBOL_GPL(omap_dm_timer_set_load);
  353. /* Optimized set_load which removes costly spin wait in timer_start */
  354. int omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
  355. unsigned int load)
  356. {
  357. u32 l;
  358. if (unlikely(!timer))
  359. return -EINVAL;
  360. omap_dm_timer_enable(timer);
  361. if (timer->loses_context) {
  362. u32 ctx_loss_cnt_after =
  363. timer->get_context_loss_count(&timer->pdev->dev);
  364. if (ctx_loss_cnt_after != timer->ctx_loss_count)
  365. omap_timer_restore_context(timer);
  366. }
  367. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  368. if (autoreload) {
  369. l |= OMAP_TIMER_CTRL_AR;
  370. omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
  371. } else {
  372. l &= ~OMAP_TIMER_CTRL_AR;
  373. }
  374. l |= OMAP_TIMER_CTRL_ST;
  375. __omap_dm_timer_load_start(timer, l, load, timer->posted);
  376. /* Save the context */
  377. timer->context.tclr = l;
  378. timer->context.tldr = load;
  379. timer->context.tcrr = load;
  380. return 0;
  381. }
  382. EXPORT_SYMBOL_GPL(omap_dm_timer_set_load_start);
  383. int omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
  384. unsigned int match)
  385. {
  386. u32 l;
  387. if (unlikely(!timer))
  388. return -EINVAL;
  389. omap_dm_timer_enable(timer);
  390. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  391. if (enable)
  392. l |= OMAP_TIMER_CTRL_CE;
  393. else
  394. l &= ~OMAP_TIMER_CTRL_CE;
  395. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  396. omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
  397. /* Save the context */
  398. timer->context.tclr = l;
  399. timer->context.tmar = match;
  400. omap_dm_timer_disable(timer);
  401. return 0;
  402. }
  403. EXPORT_SYMBOL_GPL(omap_dm_timer_set_match);
  404. int omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
  405. int toggle, int trigger)
  406. {
  407. u32 l;
  408. if (unlikely(!timer))
  409. return -EINVAL;
  410. omap_dm_timer_enable(timer);
  411. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  412. l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
  413. OMAP_TIMER_CTRL_PT | (0x03 << 10));
  414. if (def_on)
  415. l |= OMAP_TIMER_CTRL_SCPWM;
  416. if (toggle)
  417. l |= OMAP_TIMER_CTRL_PT;
  418. l |= trigger << 10;
  419. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  420. /* Save the context */
  421. timer->context.tclr = l;
  422. omap_dm_timer_disable(timer);
  423. return 0;
  424. }
  425. EXPORT_SYMBOL_GPL(omap_dm_timer_set_pwm);
  426. int omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler)
  427. {
  428. u32 l;
  429. if (unlikely(!timer))
  430. return -EINVAL;
  431. omap_dm_timer_enable(timer);
  432. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  433. l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
  434. if (prescaler >= 0x00 && prescaler <= 0x07) {
  435. l |= OMAP_TIMER_CTRL_PRE;
  436. l |= prescaler << 2;
  437. }
  438. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  439. /* Save the context */
  440. timer->context.tclr = l;
  441. omap_dm_timer_disable(timer);
  442. return 0;
  443. }
  444. EXPORT_SYMBOL_GPL(omap_dm_timer_set_prescaler);
  445. int omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
  446. unsigned int value)
  447. {
  448. if (unlikely(!timer))
  449. return -EINVAL;
  450. omap_dm_timer_enable(timer);
  451. __omap_dm_timer_int_enable(timer, value);
  452. /* Save the context */
  453. timer->context.tier = value;
  454. timer->context.twer = value;
  455. omap_dm_timer_disable(timer);
  456. return 0;
  457. }
  458. EXPORT_SYMBOL_GPL(omap_dm_timer_set_int_enable);
  459. unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
  460. {
  461. unsigned int l;
  462. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
  463. pr_err("%s: timer not available or enabled.\n", __func__);
  464. return 0;
  465. }
  466. l = __raw_readl(timer->irq_stat);
  467. return l;
  468. }
  469. EXPORT_SYMBOL_GPL(omap_dm_timer_read_status);
  470. int omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
  471. {
  472. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev)))
  473. return -EINVAL;
  474. __omap_dm_timer_write_status(timer, value);
  475. /* Save the context */
  476. timer->context.tisr = value;
  477. return 0;
  478. }
  479. EXPORT_SYMBOL_GPL(omap_dm_timer_write_status);
  480. unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
  481. {
  482. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
  483. pr_err("%s: timer not iavailable or enabled.\n", __func__);
  484. return 0;
  485. }
  486. return __omap_dm_timer_read_counter(timer, timer->posted);
  487. }
  488. EXPORT_SYMBOL_GPL(omap_dm_timer_read_counter);
  489. int omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
  490. {
  491. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
  492. pr_err("%s: timer not available or enabled.\n", __func__);
  493. return -EINVAL;
  494. }
  495. omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
  496. /* Save the context */
  497. timer->context.tcrr = value;
  498. return 0;
  499. }
  500. EXPORT_SYMBOL_GPL(omap_dm_timer_write_counter);
  501. int omap_dm_timers_active(void)
  502. {
  503. struct omap_dm_timer *timer;
  504. list_for_each_entry(timer, &omap_timer_list, node) {
  505. if (!timer->reserved)
  506. continue;
  507. if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
  508. OMAP_TIMER_CTRL_ST) {
  509. return 1;
  510. }
  511. }
  512. return 0;
  513. }
  514. EXPORT_SYMBOL_GPL(omap_dm_timers_active);
  515. /**
  516. * omap_dm_timer_probe - probe function called for every registered device
  517. * @pdev: pointer to current timer platform device
  518. *
  519. * Called by driver framework at the end of device registration for all
  520. * timer devices.
  521. */
  522. static int __devinit omap_dm_timer_probe(struct platform_device *pdev)
  523. {
  524. int ret;
  525. unsigned long flags;
  526. struct omap_dm_timer *timer;
  527. struct resource *mem, *irq, *ioarea;
  528. struct dmtimer_platform_data *pdata = pdev->dev.platform_data;
  529. if (!pdata) {
  530. dev_err(&pdev->dev, "%s: no platform data.\n", __func__);
  531. return -ENODEV;
  532. }
  533. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  534. if (unlikely(!irq)) {
  535. dev_err(&pdev->dev, "%s: no IRQ resource.\n", __func__);
  536. return -ENODEV;
  537. }
  538. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  539. if (unlikely(!mem)) {
  540. dev_err(&pdev->dev, "%s: no memory resource.\n", __func__);
  541. return -ENODEV;
  542. }
  543. ioarea = request_mem_region(mem->start, resource_size(mem),
  544. pdev->name);
  545. if (!ioarea) {
  546. dev_err(&pdev->dev, "%s: region already claimed.\n", __func__);
  547. return -EBUSY;
  548. }
  549. timer = kzalloc(sizeof(struct omap_dm_timer), GFP_KERNEL);
  550. if (!timer) {
  551. dev_err(&pdev->dev, "%s: no memory for omap_dm_timer.\n",
  552. __func__);
  553. ret = -ENOMEM;
  554. goto err_free_ioregion;
  555. }
  556. timer->io_base = ioremap(mem->start, resource_size(mem));
  557. if (!timer->io_base) {
  558. dev_err(&pdev->dev, "%s: ioremap failed.\n", __func__);
  559. ret = -ENOMEM;
  560. goto err_free_mem;
  561. }
  562. timer->id = pdev->id;
  563. timer->irq = irq->start;
  564. timer->reserved = pdata->reserved;
  565. timer->pdev = pdev;
  566. timer->loses_context = pdata->loses_context;
  567. timer->get_context_loss_count = pdata->get_context_loss_count;
  568. /* Skip pm_runtime_enable for OMAP1 */
  569. if (!pdata->needs_manual_reset) {
  570. pm_runtime_enable(&pdev->dev);
  571. pm_runtime_irq_safe(&pdev->dev);
  572. }
  573. if (!timer->reserved) {
  574. pm_runtime_get_sync(&pdev->dev);
  575. __omap_dm_timer_init_regs(timer);
  576. pm_runtime_put(&pdev->dev);
  577. }
  578. /* add the timer element to the list */
  579. spin_lock_irqsave(&dm_timer_lock, flags);
  580. list_add_tail(&timer->node, &omap_timer_list);
  581. spin_unlock_irqrestore(&dm_timer_lock, flags);
  582. dev_dbg(&pdev->dev, "Device Probed.\n");
  583. return 0;
  584. err_free_mem:
  585. kfree(timer);
  586. err_free_ioregion:
  587. release_mem_region(mem->start, resource_size(mem));
  588. return ret;
  589. }
  590. /**
  591. * omap_dm_timer_remove - cleanup a registered timer device
  592. * @pdev: pointer to current timer platform device
  593. *
  594. * Called by driver framework whenever a timer device is unregistered.
  595. * In addition to freeing platform resources it also deletes the timer
  596. * entry from the local list.
  597. */
  598. static int __devexit omap_dm_timer_remove(struct platform_device *pdev)
  599. {
  600. struct omap_dm_timer *timer;
  601. unsigned long flags;
  602. int ret = -EINVAL;
  603. spin_lock_irqsave(&dm_timer_lock, flags);
  604. list_for_each_entry(timer, &omap_timer_list, node)
  605. if (timer->pdev->id == pdev->id) {
  606. list_del(&timer->node);
  607. kfree(timer);
  608. ret = 0;
  609. break;
  610. }
  611. spin_unlock_irqrestore(&dm_timer_lock, flags);
  612. return ret;
  613. }
  614. static struct platform_driver omap_dm_timer_driver = {
  615. .probe = omap_dm_timer_probe,
  616. .remove = __devexit_p(omap_dm_timer_remove),
  617. .driver = {
  618. .name = "omap_timer",
  619. },
  620. };
  621. static int __init omap_dm_timer_driver_init(void)
  622. {
  623. return platform_driver_register(&omap_dm_timer_driver);
  624. }
  625. static void __exit omap_dm_timer_driver_exit(void)
  626. {
  627. platform_driver_unregister(&omap_dm_timer_driver);
  628. }
  629. early_platform_init("earlytimer", &omap_dm_timer_driver);
  630. module_init(omap_dm_timer_driver_init);
  631. module_exit(omap_dm_timer_driver_exit);
  632. MODULE_DESCRIPTION("OMAP Dual-Mode Timer Driver");
  633. MODULE_LICENSE("GPL");
  634. MODULE_ALIAS("platform:" DRIVER_NAME);
  635. MODULE_AUTHOR("Texas Instruments Inc");