dmtimer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * arch/arm/plat-omap/include/plat/dmtimer.h
  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. * Platform device conversion and hwmod support.
  11. *
  12. * Copyright (C) 2005 Nokia Corporation
  13. * Author: Lauri Leukkunen <lauri.leukkunen@nokia.com>
  14. * PWM and clock framwork support by Timo Teras.
  15. *
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU General Public License as published by the
  18. * Free Software Foundation; either version 2 of the License, or (at your
  19. * option) any later version.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  24. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. * You should have received a copy of the GNU General Public License along
  31. * with this program; if not, write to the Free Software Foundation, Inc.,
  32. * 675 Mass Ave, Cambridge, MA 02139, USA.
  33. */
  34. #include <linux/clk.h>
  35. #include <linux/delay.h>
  36. #include <linux/io.h>
  37. #include <linux/platform_device.h>
  38. #ifndef __ASM_ARCH_DMTIMER_H
  39. #define __ASM_ARCH_DMTIMER_H
  40. /* clock sources */
  41. #define OMAP_TIMER_SRC_SYS_CLK 0x00
  42. #define OMAP_TIMER_SRC_32_KHZ 0x01
  43. #define OMAP_TIMER_SRC_EXT_CLK 0x02
  44. /* timer interrupt enable bits */
  45. #define OMAP_TIMER_INT_CAPTURE (1 << 2)
  46. #define OMAP_TIMER_INT_OVERFLOW (1 << 1)
  47. #define OMAP_TIMER_INT_MATCH (1 << 0)
  48. /* trigger types */
  49. #define OMAP_TIMER_TRIGGER_NONE 0x00
  50. #define OMAP_TIMER_TRIGGER_OVERFLOW 0x01
  51. #define OMAP_TIMER_TRIGGER_OVERFLOW_AND_COMPARE 0x02
  52. /* posted mode types */
  53. #define OMAP_TIMER_NONPOSTED 0x00
  54. #define OMAP_TIMER_POSTED 0x01
  55. /* timer capabilities used in hwmod database */
  56. #define OMAP_TIMER_SECURE 0x80000000
  57. #define OMAP_TIMER_ALWON 0x40000000
  58. #define OMAP_TIMER_HAS_PWM 0x20000000
  59. #define OMAP_TIMER_NEEDS_RESET 0x10000000
  60. #define OMAP_TIMER_HAS_DSP_IRQ 0x08000000
  61. /*
  62. * timer errata flags
  63. *
  64. * Errata i103/i767 impacts all OMAP3/4/5 devices including AM33xx. This
  65. * errata prevents us from using posted mode on these devices, unless the
  66. * timer counter register is never read. For more details please refer to
  67. * the OMAP3/4/5 errata documents.
  68. */
  69. #define OMAP_TIMER_ERRATA_I103_I767 0x80000000
  70. struct omap_timer_capability_dev_attr {
  71. u32 timer_capability;
  72. };
  73. struct omap_dm_timer;
  74. struct timer_regs {
  75. u32 tidr;
  76. u32 tier;
  77. u32 twer;
  78. u32 tclr;
  79. u32 tcrr;
  80. u32 tldr;
  81. u32 ttrg;
  82. u32 twps;
  83. u32 tmar;
  84. u32 tcar1;
  85. u32 tsicr;
  86. u32 tcar2;
  87. u32 tpir;
  88. u32 tnir;
  89. u32 tcvr;
  90. u32 tocr;
  91. u32 towr;
  92. };
  93. struct dmtimer_platform_data {
  94. /* set_timer_src - Only used for OMAP1 devices */
  95. int (*set_timer_src)(struct platform_device *pdev, int source);
  96. u32 timer_errata;
  97. u32 timer_capability;
  98. int (*get_context_loss_count)(struct device *);
  99. };
  100. int omap_dm_timer_reserve_systimer(int id);
  101. struct omap_dm_timer *omap_dm_timer_request(void);
  102. struct omap_dm_timer *omap_dm_timer_request_specific(int timer_id);
  103. struct omap_dm_timer *omap_dm_timer_request_by_cap(u32 cap);
  104. int omap_dm_timer_free(struct omap_dm_timer *timer);
  105. void omap_dm_timer_enable(struct omap_dm_timer *timer);
  106. void omap_dm_timer_disable(struct omap_dm_timer *timer);
  107. int omap_dm_timer_get_irq(struct omap_dm_timer *timer);
  108. u32 omap_dm_timer_modify_idlect_mask(u32 inputmask);
  109. struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer);
  110. int omap_dm_timer_trigger(struct omap_dm_timer *timer);
  111. int omap_dm_timer_start(struct omap_dm_timer *timer);
  112. int omap_dm_timer_stop(struct omap_dm_timer *timer);
  113. int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source);
  114. int omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload, unsigned int value);
  115. int omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload, unsigned int value);
  116. int omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable, unsigned int match);
  117. int omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on, int toggle, int trigger);
  118. int omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler);
  119. int omap_dm_timer_set_int_enable(struct omap_dm_timer *timer, unsigned int value);
  120. int omap_dm_timer_set_int_disable(struct omap_dm_timer *timer, u32 mask);
  121. unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer);
  122. int omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value);
  123. unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer);
  124. int omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value);
  125. int omap_dm_timers_active(void);
  126. /*
  127. * Do not use the defines below, they are not needed. They should be only
  128. * used by dmtimer.c and sys_timer related code.
  129. */
  130. /*
  131. * The interrupt registers are different between v1 and v2 ip.
  132. * These registers are offsets from timer->iobase.
  133. */
  134. #define OMAP_TIMER_ID_OFFSET 0x00
  135. #define OMAP_TIMER_OCP_CFG_OFFSET 0x10
  136. #define OMAP_TIMER_V1_SYS_STAT_OFFSET 0x14
  137. #define OMAP_TIMER_V1_STAT_OFFSET 0x18
  138. #define OMAP_TIMER_V1_INT_EN_OFFSET 0x1c
  139. #define OMAP_TIMER_V2_IRQSTATUS_RAW 0x24
  140. #define OMAP_TIMER_V2_IRQSTATUS 0x28
  141. #define OMAP_TIMER_V2_IRQENABLE_SET 0x2c
  142. #define OMAP_TIMER_V2_IRQENABLE_CLR 0x30
  143. /*
  144. * The functional registers have a different base on v1 and v2 ip.
  145. * These registers are offsets from timer->func_base. The func_base
  146. * is samae as io_base for v1 and io_base + 0x14 for v2 ip.
  147. *
  148. */
  149. #define OMAP_TIMER_V2_FUNC_OFFSET 0x14
  150. #define _OMAP_TIMER_WAKEUP_EN_OFFSET 0x20
  151. #define _OMAP_TIMER_CTRL_OFFSET 0x24
  152. #define OMAP_TIMER_CTRL_GPOCFG (1 << 14)
  153. #define OMAP_TIMER_CTRL_CAPTMODE (1 << 13)
  154. #define OMAP_TIMER_CTRL_PT (1 << 12)
  155. #define OMAP_TIMER_CTRL_TCM_LOWTOHIGH (0x1 << 8)
  156. #define OMAP_TIMER_CTRL_TCM_HIGHTOLOW (0x2 << 8)
  157. #define OMAP_TIMER_CTRL_TCM_BOTHEDGES (0x3 << 8)
  158. #define OMAP_TIMER_CTRL_SCPWM (1 << 7)
  159. #define OMAP_TIMER_CTRL_CE (1 << 6) /* compare enable */
  160. #define OMAP_TIMER_CTRL_PRE (1 << 5) /* prescaler enable */
  161. #define OMAP_TIMER_CTRL_PTV_SHIFT 2 /* prescaler value shift */
  162. #define OMAP_TIMER_CTRL_POSTED (1 << 2)
  163. #define OMAP_TIMER_CTRL_AR (1 << 1) /* auto-reload enable */
  164. #define OMAP_TIMER_CTRL_ST (1 << 0) /* start timer */
  165. #define _OMAP_TIMER_COUNTER_OFFSET 0x28
  166. #define _OMAP_TIMER_LOAD_OFFSET 0x2c
  167. #define _OMAP_TIMER_TRIGGER_OFFSET 0x30
  168. #define _OMAP_TIMER_WRITE_PEND_OFFSET 0x34
  169. #define WP_NONE 0 /* no write pending bit */
  170. #define WP_TCLR (1 << 0)
  171. #define WP_TCRR (1 << 1)
  172. #define WP_TLDR (1 << 2)
  173. #define WP_TTGR (1 << 3)
  174. #define WP_TMAR (1 << 4)
  175. #define WP_TPIR (1 << 5)
  176. #define WP_TNIR (1 << 6)
  177. #define WP_TCVR (1 << 7)
  178. #define WP_TOCR (1 << 8)
  179. #define WP_TOWR (1 << 9)
  180. #define _OMAP_TIMER_MATCH_OFFSET 0x38
  181. #define _OMAP_TIMER_CAPTURE_OFFSET 0x3c
  182. #define _OMAP_TIMER_IF_CTRL_OFFSET 0x40
  183. #define _OMAP_TIMER_CAPTURE2_OFFSET 0x44 /* TCAR2, 34xx only */
  184. #define _OMAP_TIMER_TICK_POS_OFFSET 0x48 /* TPIR, 34xx only */
  185. #define _OMAP_TIMER_TICK_NEG_OFFSET 0x4c /* TNIR, 34xx only */
  186. #define _OMAP_TIMER_TICK_COUNT_OFFSET 0x50 /* TCVR, 34xx only */
  187. #define _OMAP_TIMER_TICK_INT_MASK_SET_OFFSET 0x54 /* TOCR, 34xx only */
  188. #define _OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET 0x58 /* TOWR, 34xx only */
  189. /* register offsets with the write pending bit encoded */
  190. #define WPSHIFT 16
  191. #define OMAP_TIMER_WAKEUP_EN_REG (_OMAP_TIMER_WAKEUP_EN_OFFSET \
  192. | (WP_NONE << WPSHIFT))
  193. #define OMAP_TIMER_CTRL_REG (_OMAP_TIMER_CTRL_OFFSET \
  194. | (WP_TCLR << WPSHIFT))
  195. #define OMAP_TIMER_COUNTER_REG (_OMAP_TIMER_COUNTER_OFFSET \
  196. | (WP_TCRR << WPSHIFT))
  197. #define OMAP_TIMER_LOAD_REG (_OMAP_TIMER_LOAD_OFFSET \
  198. | (WP_TLDR << WPSHIFT))
  199. #define OMAP_TIMER_TRIGGER_REG (_OMAP_TIMER_TRIGGER_OFFSET \
  200. | (WP_TTGR << WPSHIFT))
  201. #define OMAP_TIMER_WRITE_PEND_REG (_OMAP_TIMER_WRITE_PEND_OFFSET \
  202. | (WP_NONE << WPSHIFT))
  203. #define OMAP_TIMER_MATCH_REG (_OMAP_TIMER_MATCH_OFFSET \
  204. | (WP_TMAR << WPSHIFT))
  205. #define OMAP_TIMER_CAPTURE_REG (_OMAP_TIMER_CAPTURE_OFFSET \
  206. | (WP_NONE << WPSHIFT))
  207. #define OMAP_TIMER_IF_CTRL_REG (_OMAP_TIMER_IF_CTRL_OFFSET \
  208. | (WP_NONE << WPSHIFT))
  209. #define OMAP_TIMER_CAPTURE2_REG (_OMAP_TIMER_CAPTURE2_OFFSET \
  210. | (WP_NONE << WPSHIFT))
  211. #define OMAP_TIMER_TICK_POS_REG (_OMAP_TIMER_TICK_POS_OFFSET \
  212. | (WP_TPIR << WPSHIFT))
  213. #define OMAP_TIMER_TICK_NEG_REG (_OMAP_TIMER_TICK_NEG_OFFSET \
  214. | (WP_TNIR << WPSHIFT))
  215. #define OMAP_TIMER_TICK_COUNT_REG (_OMAP_TIMER_TICK_COUNT_OFFSET \
  216. | (WP_TCVR << WPSHIFT))
  217. #define OMAP_TIMER_TICK_INT_MASK_SET_REG \
  218. (_OMAP_TIMER_TICK_INT_MASK_SET_OFFSET | (WP_TOCR << WPSHIFT))
  219. #define OMAP_TIMER_TICK_INT_MASK_COUNT_REG \
  220. (_OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET | (WP_TOWR << WPSHIFT))
  221. struct omap_dm_timer {
  222. unsigned long phys_base;
  223. int id;
  224. int irq;
  225. struct clk *fclk;
  226. void __iomem *io_base;
  227. void __iomem *sys_stat; /* TISTAT timer status */
  228. void __iomem *irq_stat; /* TISR/IRQSTATUS interrupt status */
  229. void __iomem *irq_ena; /* irq enable */
  230. void __iomem *irq_dis; /* irq disable, only on v2 ip */
  231. void __iomem *pend; /* write pending */
  232. void __iomem *func_base; /* function register base */
  233. unsigned long rate;
  234. unsigned reserved:1;
  235. unsigned posted:1;
  236. struct timer_regs context;
  237. int (*get_context_loss_count)(struct device *);
  238. int ctx_loss_count;
  239. int revision;
  240. u32 capability;
  241. u32 errata;
  242. struct platform_device *pdev;
  243. struct list_head node;
  244. };
  245. int omap_dm_timer_prepare(struct omap_dm_timer *timer);
  246. static inline u32 __omap_dm_timer_read(struct omap_dm_timer *timer, u32 reg,
  247. int posted)
  248. {
  249. if (posted)
  250. while (__raw_readl(timer->pend) & (reg >> WPSHIFT))
  251. cpu_relax();
  252. return __raw_readl(timer->func_base + (reg & 0xff));
  253. }
  254. static inline void __omap_dm_timer_write(struct omap_dm_timer *timer,
  255. u32 reg, u32 val, int posted)
  256. {
  257. if (posted)
  258. while (__raw_readl(timer->pend) & (reg >> WPSHIFT))
  259. cpu_relax();
  260. __raw_writel(val, timer->func_base + (reg & 0xff));
  261. }
  262. static inline void __omap_dm_timer_init_regs(struct omap_dm_timer *timer)
  263. {
  264. u32 tidr;
  265. /* Assume v1 ip if bits [31:16] are zero */
  266. tidr = __raw_readl(timer->io_base);
  267. if (!(tidr >> 16)) {
  268. timer->revision = 1;
  269. timer->sys_stat = timer->io_base +
  270. OMAP_TIMER_V1_SYS_STAT_OFFSET;
  271. timer->irq_stat = timer->io_base + OMAP_TIMER_V1_STAT_OFFSET;
  272. timer->irq_ena = timer->io_base + OMAP_TIMER_V1_INT_EN_OFFSET;
  273. timer->irq_dis = timer->io_base + OMAP_TIMER_V1_INT_EN_OFFSET;
  274. timer->pend = timer->io_base + _OMAP_TIMER_WRITE_PEND_OFFSET;
  275. timer->func_base = timer->io_base;
  276. } else {
  277. timer->revision = 2;
  278. timer->sys_stat = NULL;
  279. timer->irq_stat = timer->io_base + OMAP_TIMER_V2_IRQSTATUS;
  280. timer->irq_ena = timer->io_base + OMAP_TIMER_V2_IRQENABLE_SET;
  281. timer->irq_dis = timer->io_base + OMAP_TIMER_V2_IRQENABLE_CLR;
  282. timer->pend = timer->io_base +
  283. _OMAP_TIMER_WRITE_PEND_OFFSET +
  284. OMAP_TIMER_V2_FUNC_OFFSET;
  285. timer->func_base = timer->io_base + OMAP_TIMER_V2_FUNC_OFFSET;
  286. }
  287. }
  288. /* Assumes the source clock has been set by caller */
  289. static inline void __omap_dm_timer_reset(struct omap_dm_timer *timer,
  290. int autoidle, int wakeup)
  291. {
  292. u32 l;
  293. l = __raw_readl(timer->io_base + OMAP_TIMER_OCP_CFG_OFFSET);
  294. l |= 0x02 << 3; /* Set to smart-idle mode */
  295. l |= 0x2 << 8; /* Set clock activity to perserve f-clock on idle */
  296. if (autoidle)
  297. l |= 0x1 << 0;
  298. if (wakeup)
  299. l |= 1 << 2;
  300. __raw_writel(l, timer->io_base + OMAP_TIMER_OCP_CFG_OFFSET);
  301. }
  302. /*
  303. * __omap_dm_timer_enable_posted - enables write posted mode
  304. * @timer: pointer to timer instance handle
  305. *
  306. * Enables the write posted mode for the timer. When posted mode is enabled
  307. * writes to certain timer registers are immediately acknowledged by the
  308. * internal bus and hence prevents stalling the CPU waiting for the write to
  309. * complete. Enabling this feature can improve performance for writing to the
  310. * timer registers.
  311. */
  312. static inline void __omap_dm_timer_enable_posted(struct omap_dm_timer *timer)
  313. {
  314. if (timer->posted)
  315. return;
  316. if (timer->errata & OMAP_TIMER_ERRATA_I103_I767)
  317. return;
  318. __omap_dm_timer_write(timer, OMAP_TIMER_IF_CTRL_REG,
  319. OMAP_TIMER_CTRL_POSTED, 0);
  320. timer->context.tsicr = OMAP_TIMER_CTRL_POSTED;
  321. timer->posted = OMAP_TIMER_POSTED;
  322. }
  323. /**
  324. * __omap_dm_timer_override_errata - override errata flags for a timer
  325. * @timer: pointer to timer handle
  326. * @errata: errata flags to be ignored
  327. *
  328. * For a given timer, override a timer errata by clearing the flags
  329. * specified by the errata argument. A specific erratum should only be
  330. * overridden for a timer if the timer is used in such a way the erratum
  331. * has no impact.
  332. */
  333. static inline void __omap_dm_timer_override_errata(struct omap_dm_timer *timer,
  334. u32 errata)
  335. {
  336. timer->errata &= ~errata;
  337. }
  338. static inline int __omap_dm_timer_set_source(struct clk *timer_fck,
  339. struct clk *parent)
  340. {
  341. int ret;
  342. clk_disable(timer_fck);
  343. ret = clk_set_parent(timer_fck, parent);
  344. clk_enable(timer_fck);
  345. /*
  346. * When the functional clock disappears, too quick writes seem
  347. * to cause an abort. XXX Is this still necessary?
  348. */
  349. __delay(300000);
  350. return ret;
  351. }
  352. static inline void __omap_dm_timer_stop(struct omap_dm_timer *timer,
  353. int posted, unsigned long rate)
  354. {
  355. u32 l;
  356. l = __omap_dm_timer_read(timer, OMAP_TIMER_CTRL_REG, posted);
  357. if (l & OMAP_TIMER_CTRL_ST) {
  358. l &= ~0x1;
  359. __omap_dm_timer_write(timer, OMAP_TIMER_CTRL_REG, l, posted);
  360. #ifdef CONFIG_ARCH_OMAP2PLUS
  361. /* Readback to make sure write has completed */
  362. __omap_dm_timer_read(timer, OMAP_TIMER_CTRL_REG, posted);
  363. /*
  364. * Wait for functional clock period x 3.5 to make sure that
  365. * timer is stopped
  366. */
  367. udelay(3500000 / rate + 1);
  368. #endif
  369. }
  370. /* Ack possibly pending interrupt */
  371. __raw_writel(OMAP_TIMER_INT_OVERFLOW, timer->irq_stat);
  372. }
  373. static inline void __omap_dm_timer_load_start(struct omap_dm_timer *timer,
  374. u32 ctrl, unsigned int load,
  375. int posted)
  376. {
  377. __omap_dm_timer_write(timer, OMAP_TIMER_COUNTER_REG, load, posted);
  378. __omap_dm_timer_write(timer, OMAP_TIMER_CTRL_REG, ctrl, posted);
  379. }
  380. static inline void __omap_dm_timer_int_enable(struct omap_dm_timer *timer,
  381. unsigned int value)
  382. {
  383. __raw_writel(value, timer->irq_ena);
  384. __omap_dm_timer_write(timer, OMAP_TIMER_WAKEUP_EN_REG, value, 0);
  385. }
  386. static inline unsigned int
  387. __omap_dm_timer_read_counter(struct omap_dm_timer *timer, int posted)
  388. {
  389. return __omap_dm_timer_read(timer, OMAP_TIMER_COUNTER_REG, posted);
  390. }
  391. static inline void __omap_dm_timer_write_status(struct omap_dm_timer *timer,
  392. unsigned int value)
  393. {
  394. __raw_writel(value, timer->irq_stat);
  395. }
  396. #endif /* __ASM_ARCH_DMTIMER_H */