timer-ti-dm.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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 <clocksource/timer-ti-dm.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. /* Bail out if both clocks point to fck */
  139. if (clk_is_match(parent, timer->fclk))
  140. return 0;
  141. ret = clk_set_parent(timer->fclk, parent);
  142. if (ret < 0)
  143. pr_err("%s: failed to set parent\n", __func__);
  144. clk_put(parent);
  145. return ret;
  146. }
  147. static int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source)
  148. {
  149. int ret;
  150. const char *parent_name;
  151. struct clk *parent;
  152. struct dmtimer_platform_data *pdata;
  153. if (unlikely(!timer) || IS_ERR(timer->fclk))
  154. return -EINVAL;
  155. switch (source) {
  156. case OMAP_TIMER_SRC_SYS_CLK:
  157. parent_name = "timer_sys_ck";
  158. break;
  159. case OMAP_TIMER_SRC_32_KHZ:
  160. parent_name = "timer_32k_ck";
  161. break;
  162. case OMAP_TIMER_SRC_EXT_CLK:
  163. parent_name = "timer_ext_ck";
  164. break;
  165. default:
  166. return -EINVAL;
  167. }
  168. pdata = timer->pdev->dev.platform_data;
  169. /*
  170. * FIXME: Used for OMAP1 devices only because they do not currently
  171. * use the clock framework to set the parent clock. To be removed
  172. * once OMAP1 migrated to using clock framework for dmtimers
  173. */
  174. if (pdata && pdata->set_timer_src)
  175. return pdata->set_timer_src(timer->pdev, source);
  176. #if defined(CONFIG_COMMON_CLK)
  177. /* Check if the clock has configurable parents */
  178. if (clk_hw_get_num_parents(__clk_get_hw(timer->fclk)) < 2)
  179. return 0;
  180. #endif
  181. parent = clk_get(&timer->pdev->dev, parent_name);
  182. if (IS_ERR(parent)) {
  183. pr_err("%s: %s not found\n", __func__, parent_name);
  184. return -EINVAL;
  185. }
  186. ret = clk_set_parent(timer->fclk, parent);
  187. if (ret < 0)
  188. pr_err("%s: failed to set %s as parent\n", __func__,
  189. parent_name);
  190. clk_put(parent);
  191. return ret;
  192. }
  193. static void omap_dm_timer_enable(struct omap_dm_timer *timer)
  194. {
  195. int c;
  196. pm_runtime_get_sync(&timer->pdev->dev);
  197. if (!(timer->capability & OMAP_TIMER_ALWON)) {
  198. if (timer->get_context_loss_count) {
  199. c = timer->get_context_loss_count(&timer->pdev->dev);
  200. if (c != timer->ctx_loss_count) {
  201. omap_timer_restore_context(timer);
  202. timer->ctx_loss_count = c;
  203. }
  204. } else {
  205. omap_timer_restore_context(timer);
  206. }
  207. }
  208. }
  209. static void omap_dm_timer_disable(struct omap_dm_timer *timer)
  210. {
  211. pm_runtime_put_sync(&timer->pdev->dev);
  212. }
  213. static int omap_dm_timer_prepare(struct omap_dm_timer *timer)
  214. {
  215. int rc;
  216. /*
  217. * FIXME: OMAP1 devices do not use the clock framework for dmtimers so
  218. * do not call clk_get() for these devices.
  219. */
  220. if (!(timer->capability & OMAP_TIMER_NEEDS_RESET)) {
  221. timer->fclk = clk_get(&timer->pdev->dev, "fck");
  222. if (WARN_ON_ONCE(IS_ERR(timer->fclk))) {
  223. dev_err(&timer->pdev->dev, ": No fclk handle.\n");
  224. return -EINVAL;
  225. }
  226. }
  227. omap_dm_timer_enable(timer);
  228. if (timer->capability & OMAP_TIMER_NEEDS_RESET) {
  229. rc = omap_dm_timer_reset(timer);
  230. if (rc) {
  231. omap_dm_timer_disable(timer);
  232. return rc;
  233. }
  234. }
  235. __omap_dm_timer_enable_posted(timer);
  236. omap_dm_timer_disable(timer);
  237. rc = omap_dm_timer_of_set_source(timer);
  238. if (rc == -ENODEV)
  239. return omap_dm_timer_set_source(timer, OMAP_TIMER_SRC_32_KHZ);
  240. return rc;
  241. }
  242. static inline u32 omap_dm_timer_reserved_systimer(int id)
  243. {
  244. return (omap_reserved_systimers & (1 << (id - 1))) ? 1 : 0;
  245. }
  246. int omap_dm_timer_reserve_systimer(int id)
  247. {
  248. if (omap_dm_timer_reserved_systimer(id))
  249. return -ENODEV;
  250. omap_reserved_systimers |= (1 << (id - 1));
  251. return 0;
  252. }
  253. static struct omap_dm_timer *_omap_dm_timer_request(int req_type, void *data)
  254. {
  255. struct omap_dm_timer *timer = NULL, *t;
  256. struct device_node *np = NULL;
  257. unsigned long flags;
  258. u32 cap = 0;
  259. int id = 0;
  260. switch (req_type) {
  261. case REQUEST_BY_ID:
  262. id = *(int *)data;
  263. break;
  264. case REQUEST_BY_CAP:
  265. cap = *(u32 *)data;
  266. break;
  267. case REQUEST_BY_NODE:
  268. np = (struct device_node *)data;
  269. break;
  270. default:
  271. /* REQUEST_ANY */
  272. break;
  273. }
  274. spin_lock_irqsave(&dm_timer_lock, flags);
  275. list_for_each_entry(t, &omap_timer_list, node) {
  276. if (t->reserved)
  277. continue;
  278. switch (req_type) {
  279. case REQUEST_BY_ID:
  280. if (id == t->pdev->id) {
  281. timer = t;
  282. timer->reserved = 1;
  283. goto found;
  284. }
  285. break;
  286. case REQUEST_BY_CAP:
  287. if (cap == (t->capability & cap)) {
  288. /*
  289. * If timer is not NULL, we have already found
  290. * one timer. But it was not an exact match
  291. * because it had more capabilities than what
  292. * was required. Therefore, unreserve the last
  293. * timer found and see if this one is a better
  294. * match.
  295. */
  296. if (timer)
  297. timer->reserved = 0;
  298. timer = t;
  299. timer->reserved = 1;
  300. /* Exit loop early if we find an exact match */
  301. if (t->capability == cap)
  302. goto found;
  303. }
  304. break;
  305. case REQUEST_BY_NODE:
  306. if (np == t->pdev->dev.of_node) {
  307. timer = t;
  308. timer->reserved = 1;
  309. goto found;
  310. }
  311. break;
  312. default:
  313. /* REQUEST_ANY */
  314. timer = t;
  315. timer->reserved = 1;
  316. goto found;
  317. }
  318. }
  319. found:
  320. spin_unlock_irqrestore(&dm_timer_lock, flags);
  321. if (timer && omap_dm_timer_prepare(timer)) {
  322. timer->reserved = 0;
  323. timer = NULL;
  324. }
  325. if (!timer)
  326. pr_debug("%s: timer request failed!\n", __func__);
  327. return timer;
  328. }
  329. static struct omap_dm_timer *omap_dm_timer_request(void)
  330. {
  331. return _omap_dm_timer_request(REQUEST_ANY, NULL);
  332. }
  333. static struct omap_dm_timer *omap_dm_timer_request_specific(int id)
  334. {
  335. /* Requesting timer by ID is not supported when device tree is used */
  336. if (of_have_populated_dt()) {
  337. pr_warn("%s: Please use omap_dm_timer_request_by_node()\n",
  338. __func__);
  339. return NULL;
  340. }
  341. return _omap_dm_timer_request(REQUEST_BY_ID, &id);
  342. }
  343. /**
  344. * omap_dm_timer_request_by_cap - Request a timer by capability
  345. * @cap: Bit mask of capabilities to match
  346. *
  347. * Find a timer based upon capabilities bit mask. Callers of this function
  348. * should use the definitions found in the plat/dmtimer.h file under the
  349. * comment "timer capabilities used in hwmod database". Returns pointer to
  350. * timer handle on success and a NULL pointer on failure.
  351. */
  352. struct omap_dm_timer *omap_dm_timer_request_by_cap(u32 cap)
  353. {
  354. return _omap_dm_timer_request(REQUEST_BY_CAP, &cap);
  355. }
  356. /**
  357. * omap_dm_timer_request_by_node - Request a timer by device-tree node
  358. * @np: Pointer to device-tree timer node
  359. *
  360. * Request a timer based upon a device node pointer. Returns pointer to
  361. * timer handle on success and a NULL pointer on failure.
  362. */
  363. static struct omap_dm_timer *omap_dm_timer_request_by_node(struct device_node *np)
  364. {
  365. if (!np)
  366. return NULL;
  367. return _omap_dm_timer_request(REQUEST_BY_NODE, np);
  368. }
  369. static int omap_dm_timer_free(struct omap_dm_timer *timer)
  370. {
  371. if (unlikely(!timer))
  372. return -EINVAL;
  373. clk_put(timer->fclk);
  374. WARN_ON(!timer->reserved);
  375. timer->reserved = 0;
  376. return 0;
  377. }
  378. int omap_dm_timer_get_irq(struct omap_dm_timer *timer)
  379. {
  380. if (timer)
  381. return timer->irq;
  382. return -EINVAL;
  383. }
  384. #if defined(CONFIG_ARCH_OMAP1)
  385. #include <mach/hardware.h>
  386. static struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
  387. {
  388. return NULL;
  389. }
  390. /**
  391. * omap_dm_timer_modify_idlect_mask - Check if any running timers use ARMXOR
  392. * @inputmask: current value of idlect mask
  393. */
  394. __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
  395. {
  396. int i = 0;
  397. struct omap_dm_timer *timer = NULL;
  398. unsigned long flags;
  399. /* If ARMXOR cannot be idled this function call is unnecessary */
  400. if (!(inputmask & (1 << 1)))
  401. return inputmask;
  402. /* If any active timer is using ARMXOR return modified mask */
  403. spin_lock_irqsave(&dm_timer_lock, flags);
  404. list_for_each_entry(timer, &omap_timer_list, node) {
  405. u32 l;
  406. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  407. if (l & OMAP_TIMER_CTRL_ST) {
  408. if (((omap_readl(MOD_CONF_CTRL_1) >> (i * 2)) & 0x03) == 0)
  409. inputmask &= ~(1 << 1);
  410. else
  411. inputmask &= ~(1 << 2);
  412. }
  413. i++;
  414. }
  415. spin_unlock_irqrestore(&dm_timer_lock, flags);
  416. return inputmask;
  417. }
  418. #else
  419. static struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer)
  420. {
  421. if (timer && !IS_ERR(timer->fclk))
  422. return timer->fclk;
  423. return NULL;
  424. }
  425. __u32 omap_dm_timer_modify_idlect_mask(__u32 inputmask)
  426. {
  427. BUG();
  428. return 0;
  429. }
  430. #endif
  431. int omap_dm_timer_trigger(struct omap_dm_timer *timer)
  432. {
  433. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
  434. pr_err("%s: timer not available or enabled.\n", __func__);
  435. return -EINVAL;
  436. }
  437. omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
  438. return 0;
  439. }
  440. static int omap_dm_timer_start(struct omap_dm_timer *timer)
  441. {
  442. u32 l;
  443. if (unlikely(!timer))
  444. return -EINVAL;
  445. omap_dm_timer_enable(timer);
  446. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  447. if (!(l & OMAP_TIMER_CTRL_ST)) {
  448. l |= OMAP_TIMER_CTRL_ST;
  449. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  450. }
  451. /* Save the context */
  452. timer->context.tclr = l;
  453. return 0;
  454. }
  455. static int omap_dm_timer_stop(struct omap_dm_timer *timer)
  456. {
  457. unsigned long rate = 0;
  458. if (unlikely(!timer))
  459. return -EINVAL;
  460. if (!(timer->capability & OMAP_TIMER_NEEDS_RESET))
  461. rate = clk_get_rate(timer->fclk);
  462. __omap_dm_timer_stop(timer, timer->posted, rate);
  463. /*
  464. * Since the register values are computed and written within
  465. * __omap_dm_timer_stop, we need to use read to retrieve the
  466. * context.
  467. */
  468. timer->context.tclr =
  469. omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  470. omap_dm_timer_disable(timer);
  471. return 0;
  472. }
  473. static int omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload,
  474. unsigned int load)
  475. {
  476. u32 l;
  477. if (unlikely(!timer))
  478. return -EINVAL;
  479. omap_dm_timer_enable(timer);
  480. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  481. if (autoreload)
  482. l |= OMAP_TIMER_CTRL_AR;
  483. else
  484. l &= ~OMAP_TIMER_CTRL_AR;
  485. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  486. omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
  487. omap_dm_timer_write_reg(timer, OMAP_TIMER_TRIGGER_REG, 0);
  488. /* Save the context */
  489. timer->context.tclr = l;
  490. timer->context.tldr = load;
  491. omap_dm_timer_disable(timer);
  492. return 0;
  493. }
  494. /* Optimized set_load which removes costly spin wait in timer_start */
  495. int omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload,
  496. unsigned int load)
  497. {
  498. u32 l;
  499. if (unlikely(!timer))
  500. return -EINVAL;
  501. omap_dm_timer_enable(timer);
  502. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  503. if (autoreload) {
  504. l |= OMAP_TIMER_CTRL_AR;
  505. omap_dm_timer_write_reg(timer, OMAP_TIMER_LOAD_REG, load);
  506. } else {
  507. l &= ~OMAP_TIMER_CTRL_AR;
  508. }
  509. l |= OMAP_TIMER_CTRL_ST;
  510. __omap_dm_timer_load_start(timer, l, load, timer->posted);
  511. /* Save the context */
  512. timer->context.tclr = l;
  513. timer->context.tldr = load;
  514. timer->context.tcrr = load;
  515. return 0;
  516. }
  517. static int omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable,
  518. unsigned int match)
  519. {
  520. u32 l;
  521. if (unlikely(!timer))
  522. return -EINVAL;
  523. omap_dm_timer_enable(timer);
  524. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  525. if (enable)
  526. l |= OMAP_TIMER_CTRL_CE;
  527. else
  528. l &= ~OMAP_TIMER_CTRL_CE;
  529. omap_dm_timer_write_reg(timer, OMAP_TIMER_MATCH_REG, match);
  530. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  531. /* Save the context */
  532. timer->context.tclr = l;
  533. timer->context.tmar = match;
  534. omap_dm_timer_disable(timer);
  535. return 0;
  536. }
  537. static int omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on,
  538. int toggle, int trigger)
  539. {
  540. u32 l;
  541. if (unlikely(!timer))
  542. return -EINVAL;
  543. omap_dm_timer_enable(timer);
  544. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  545. l &= ~(OMAP_TIMER_CTRL_GPOCFG | OMAP_TIMER_CTRL_SCPWM |
  546. OMAP_TIMER_CTRL_PT | (0x03 << 10));
  547. if (def_on)
  548. l |= OMAP_TIMER_CTRL_SCPWM;
  549. if (toggle)
  550. l |= OMAP_TIMER_CTRL_PT;
  551. l |= trigger << 10;
  552. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  553. /* Save the context */
  554. timer->context.tclr = l;
  555. omap_dm_timer_disable(timer);
  556. return 0;
  557. }
  558. static int omap_dm_timer_set_prescaler(struct omap_dm_timer *timer,
  559. int prescaler)
  560. {
  561. u32 l;
  562. if (unlikely(!timer) || prescaler < -1 || prescaler > 7)
  563. return -EINVAL;
  564. omap_dm_timer_enable(timer);
  565. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG);
  566. l &= ~(OMAP_TIMER_CTRL_PRE | (0x07 << 2));
  567. if (prescaler >= 0) {
  568. l |= OMAP_TIMER_CTRL_PRE;
  569. l |= prescaler << 2;
  570. }
  571. omap_dm_timer_write_reg(timer, OMAP_TIMER_CTRL_REG, l);
  572. /* Save the context */
  573. timer->context.tclr = l;
  574. omap_dm_timer_disable(timer);
  575. return 0;
  576. }
  577. static int omap_dm_timer_set_int_enable(struct omap_dm_timer *timer,
  578. unsigned int value)
  579. {
  580. if (unlikely(!timer))
  581. return -EINVAL;
  582. omap_dm_timer_enable(timer);
  583. __omap_dm_timer_int_enable(timer, value);
  584. /* Save the context */
  585. timer->context.tier = value;
  586. timer->context.twer = value;
  587. omap_dm_timer_disable(timer);
  588. return 0;
  589. }
  590. /**
  591. * omap_dm_timer_set_int_disable - disable timer interrupts
  592. * @timer: pointer to timer handle
  593. * @mask: bit mask of interrupts to be disabled
  594. *
  595. * Disables the specified timer interrupts for a timer.
  596. */
  597. static int omap_dm_timer_set_int_disable(struct omap_dm_timer *timer, u32 mask)
  598. {
  599. u32 l = mask;
  600. if (unlikely(!timer))
  601. return -EINVAL;
  602. omap_dm_timer_enable(timer);
  603. if (timer->revision == 1)
  604. l = readl_relaxed(timer->irq_ena) & ~mask;
  605. writel_relaxed(l, timer->irq_dis);
  606. l = omap_dm_timer_read_reg(timer, OMAP_TIMER_WAKEUP_EN_REG) & ~mask;
  607. omap_dm_timer_write_reg(timer, OMAP_TIMER_WAKEUP_EN_REG, l);
  608. /* Save the context */
  609. timer->context.tier &= ~mask;
  610. timer->context.twer &= ~mask;
  611. omap_dm_timer_disable(timer);
  612. return 0;
  613. }
  614. static unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer)
  615. {
  616. unsigned int l;
  617. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
  618. pr_err("%s: timer not available or enabled.\n", __func__);
  619. return 0;
  620. }
  621. l = readl_relaxed(timer->irq_stat);
  622. return l;
  623. }
  624. static int omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value)
  625. {
  626. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev)))
  627. return -EINVAL;
  628. __omap_dm_timer_write_status(timer, value);
  629. return 0;
  630. }
  631. static unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer)
  632. {
  633. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
  634. pr_err("%s: timer not iavailable or enabled.\n", __func__);
  635. return 0;
  636. }
  637. return __omap_dm_timer_read_counter(timer, timer->posted);
  638. }
  639. static int omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value)
  640. {
  641. if (unlikely(!timer || pm_runtime_suspended(&timer->pdev->dev))) {
  642. pr_err("%s: timer not available or enabled.\n", __func__);
  643. return -EINVAL;
  644. }
  645. omap_dm_timer_write_reg(timer, OMAP_TIMER_COUNTER_REG, value);
  646. /* Save the context */
  647. timer->context.tcrr = value;
  648. return 0;
  649. }
  650. int omap_dm_timers_active(void)
  651. {
  652. struct omap_dm_timer *timer;
  653. list_for_each_entry(timer, &omap_timer_list, node) {
  654. if (!timer->reserved)
  655. continue;
  656. if (omap_dm_timer_read_reg(timer, OMAP_TIMER_CTRL_REG) &
  657. OMAP_TIMER_CTRL_ST) {
  658. return 1;
  659. }
  660. }
  661. return 0;
  662. }
  663. static const struct of_device_id omap_timer_match[];
  664. /**
  665. * omap_dm_timer_probe - probe function called for every registered device
  666. * @pdev: pointer to current timer platform device
  667. *
  668. * Called by driver framework at the end of device registration for all
  669. * timer devices.
  670. */
  671. static int omap_dm_timer_probe(struct platform_device *pdev)
  672. {
  673. unsigned long flags;
  674. struct omap_dm_timer *timer;
  675. struct resource *mem, *irq;
  676. struct device *dev = &pdev->dev;
  677. const struct dmtimer_platform_data *pdata;
  678. int ret;
  679. pdata = of_device_get_match_data(dev);
  680. if (!pdata)
  681. pdata = dev_get_platdata(dev);
  682. else
  683. dev->platform_data = (void *)pdata;
  684. if (!pdata) {
  685. dev_err(dev, "%s: no platform data.\n", __func__);
  686. return -ENODEV;
  687. }
  688. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  689. if (unlikely(!irq)) {
  690. dev_err(dev, "%s: no IRQ resource.\n", __func__);
  691. return -ENODEV;
  692. }
  693. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  694. if (unlikely(!mem)) {
  695. dev_err(dev, "%s: no memory resource.\n", __func__);
  696. return -ENODEV;
  697. }
  698. timer = devm_kzalloc(dev, sizeof(*timer), GFP_KERNEL);
  699. if (!timer)
  700. return -ENOMEM;
  701. timer->fclk = ERR_PTR(-ENODEV);
  702. timer->io_base = devm_ioremap_resource(dev, mem);
  703. if (IS_ERR(timer->io_base))
  704. return PTR_ERR(timer->io_base);
  705. if (dev->of_node) {
  706. if (of_find_property(dev->of_node, "ti,timer-alwon", NULL))
  707. timer->capability |= OMAP_TIMER_ALWON;
  708. if (of_find_property(dev->of_node, "ti,timer-dsp", NULL))
  709. timer->capability |= OMAP_TIMER_HAS_DSP_IRQ;
  710. if (of_find_property(dev->of_node, "ti,timer-pwm", NULL))
  711. timer->capability |= OMAP_TIMER_HAS_PWM;
  712. if (of_find_property(dev->of_node, "ti,timer-secure", NULL))
  713. timer->capability |= OMAP_TIMER_SECURE;
  714. } else {
  715. timer->id = pdev->id;
  716. timer->capability = pdata->timer_capability;
  717. timer->reserved = omap_dm_timer_reserved_systimer(timer->id);
  718. timer->get_context_loss_count = pdata->get_context_loss_count;
  719. }
  720. if (pdata)
  721. timer->errata = pdata->timer_errata;
  722. timer->irq = irq->start;
  723. timer->pdev = pdev;
  724. pm_runtime_enable(dev);
  725. if (!timer->reserved) {
  726. ret = pm_runtime_get_sync(dev);
  727. if (ret < 0) {
  728. dev_err(dev, "%s: pm_runtime_get_sync failed!\n",
  729. __func__);
  730. goto err_get_sync;
  731. }
  732. __omap_dm_timer_init_regs(timer);
  733. pm_runtime_put(dev);
  734. }
  735. /* add the timer element to the list */
  736. spin_lock_irqsave(&dm_timer_lock, flags);
  737. list_add_tail(&timer->node, &omap_timer_list);
  738. spin_unlock_irqrestore(&dm_timer_lock, flags);
  739. dev_dbg(dev, "Device Probed.\n");
  740. return 0;
  741. err_get_sync:
  742. pm_runtime_put_noidle(dev);
  743. pm_runtime_disable(dev);
  744. return ret;
  745. }
  746. /**
  747. * omap_dm_timer_remove - cleanup a registered timer device
  748. * @pdev: pointer to current timer platform device
  749. *
  750. * Called by driver framework whenever a timer device is unregistered.
  751. * In addition to freeing platform resources it also deletes the timer
  752. * entry from the local list.
  753. */
  754. static int omap_dm_timer_remove(struct platform_device *pdev)
  755. {
  756. struct omap_dm_timer *timer;
  757. unsigned long flags;
  758. int ret = -EINVAL;
  759. spin_lock_irqsave(&dm_timer_lock, flags);
  760. list_for_each_entry(timer, &omap_timer_list, node)
  761. if (!strcmp(dev_name(&timer->pdev->dev),
  762. dev_name(&pdev->dev))) {
  763. list_del(&timer->node);
  764. ret = 0;
  765. break;
  766. }
  767. spin_unlock_irqrestore(&dm_timer_lock, flags);
  768. pm_runtime_disable(&pdev->dev);
  769. return ret;
  770. }
  771. const static struct omap_dm_timer_ops dmtimer_ops = {
  772. .request_by_node = omap_dm_timer_request_by_node,
  773. .request_specific = omap_dm_timer_request_specific,
  774. .request = omap_dm_timer_request,
  775. .set_source = omap_dm_timer_set_source,
  776. .get_irq = omap_dm_timer_get_irq,
  777. .set_int_enable = omap_dm_timer_set_int_enable,
  778. .set_int_disable = omap_dm_timer_set_int_disable,
  779. .free = omap_dm_timer_free,
  780. .enable = omap_dm_timer_enable,
  781. .disable = omap_dm_timer_disable,
  782. .get_fclk = omap_dm_timer_get_fclk,
  783. .start = omap_dm_timer_start,
  784. .stop = omap_dm_timer_stop,
  785. .set_load = omap_dm_timer_set_load,
  786. .set_match = omap_dm_timer_set_match,
  787. .set_pwm = omap_dm_timer_set_pwm,
  788. .set_prescaler = omap_dm_timer_set_prescaler,
  789. .read_counter = omap_dm_timer_read_counter,
  790. .write_counter = omap_dm_timer_write_counter,
  791. .read_status = omap_dm_timer_read_status,
  792. .write_status = omap_dm_timer_write_status,
  793. };
  794. static const struct dmtimer_platform_data omap3plus_pdata = {
  795. .timer_errata = OMAP_TIMER_ERRATA_I103_I767,
  796. .timer_ops = &dmtimer_ops,
  797. };
  798. static const struct of_device_id omap_timer_match[] = {
  799. {
  800. .compatible = "ti,omap2420-timer",
  801. },
  802. {
  803. .compatible = "ti,omap3430-timer",
  804. .data = &omap3plus_pdata,
  805. },
  806. {
  807. .compatible = "ti,omap4430-timer",
  808. .data = &omap3plus_pdata,
  809. },
  810. {
  811. .compatible = "ti,omap5430-timer",
  812. .data = &omap3plus_pdata,
  813. },
  814. {
  815. .compatible = "ti,am335x-timer",
  816. .data = &omap3plus_pdata,
  817. },
  818. {
  819. .compatible = "ti,am335x-timer-1ms",
  820. .data = &omap3plus_pdata,
  821. },
  822. {
  823. .compatible = "ti,dm816-timer",
  824. .data = &omap3plus_pdata,
  825. },
  826. {},
  827. };
  828. MODULE_DEVICE_TABLE(of, omap_timer_match);
  829. static struct platform_driver omap_dm_timer_driver = {
  830. .probe = omap_dm_timer_probe,
  831. .remove = omap_dm_timer_remove,
  832. .driver = {
  833. .name = "omap_timer",
  834. .of_match_table = of_match_ptr(omap_timer_match),
  835. },
  836. };
  837. early_platform_init("earlytimer", &omap_dm_timer_driver);
  838. module_platform_driver(omap_dm_timer_driver);
  839. MODULE_DESCRIPTION("OMAP Dual-Mode Timer Driver");
  840. MODULE_LICENSE("GPL");
  841. MODULE_ALIAS("platform:" DRIVER_NAME);
  842. MODULE_AUTHOR("Texas Instruments Inc");