dmtimer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. /*
  53. * IP revision identifier so that Highlander IP
  54. * in OMAP4 can be distinguished.
  55. */
  56. #define OMAP_TIMER_IP_VERSION_1 0x1
  57. /* timer capabilities used in hwmod database */
  58. #define OMAP_TIMER_SECURE 0x80000000
  59. #define OMAP_TIMER_ALWON 0x40000000
  60. #define OMAP_TIMER_HAS_PWM 0x20000000
  61. struct omap_timer_capability_dev_attr {
  62. u32 timer_capability;
  63. };
  64. struct omap_dm_timer;
  65. struct clk;
  66. struct dmtimer_platform_data {
  67. int (*set_timer_src)(struct platform_device *pdev, int source);
  68. int timer_ip_version;
  69. u32 needs_manual_reset:1;
  70. bool reserved;
  71. };
  72. struct omap_dm_timer *omap_dm_timer_request(void);
  73. struct omap_dm_timer *omap_dm_timer_request_specific(int timer_id);
  74. void omap_dm_timer_free(struct omap_dm_timer *timer);
  75. void omap_dm_timer_enable(struct omap_dm_timer *timer);
  76. void omap_dm_timer_disable(struct omap_dm_timer *timer);
  77. int omap_dm_timer_get_irq(struct omap_dm_timer *timer);
  78. u32 omap_dm_timer_modify_idlect_mask(u32 inputmask);
  79. struct clk *omap_dm_timer_get_fclk(struct omap_dm_timer *timer);
  80. void omap_dm_timer_trigger(struct omap_dm_timer *timer);
  81. void omap_dm_timer_start(struct omap_dm_timer *timer);
  82. void omap_dm_timer_stop(struct omap_dm_timer *timer);
  83. int omap_dm_timer_set_source(struct omap_dm_timer *timer, int source);
  84. void omap_dm_timer_set_load(struct omap_dm_timer *timer, int autoreload, unsigned int value);
  85. void omap_dm_timer_set_load_start(struct omap_dm_timer *timer, int autoreload, unsigned int value);
  86. void omap_dm_timer_set_match(struct omap_dm_timer *timer, int enable, unsigned int match);
  87. void omap_dm_timer_set_pwm(struct omap_dm_timer *timer, int def_on, int toggle, int trigger);
  88. void omap_dm_timer_set_prescaler(struct omap_dm_timer *timer, int prescaler);
  89. void omap_dm_timer_set_int_enable(struct omap_dm_timer *timer, unsigned int value);
  90. unsigned int omap_dm_timer_read_status(struct omap_dm_timer *timer);
  91. void omap_dm_timer_write_status(struct omap_dm_timer *timer, unsigned int value);
  92. unsigned int omap_dm_timer_read_counter(struct omap_dm_timer *timer);
  93. void omap_dm_timer_write_counter(struct omap_dm_timer *timer, unsigned int value);
  94. int omap_dm_timers_active(void);
  95. /*
  96. * Do not use the defines below, they are not needed. They should be only
  97. * used by dmtimer.c and sys_timer related code.
  98. */
  99. /*
  100. * The interrupt registers are different between v1 and v2 ip.
  101. * These registers are offsets from timer->iobase.
  102. */
  103. #define OMAP_TIMER_ID_OFFSET 0x00
  104. #define OMAP_TIMER_OCP_CFG_OFFSET 0x10
  105. #define OMAP_TIMER_V1_SYS_STAT_OFFSET 0x14
  106. #define OMAP_TIMER_V1_STAT_OFFSET 0x18
  107. #define OMAP_TIMER_V1_INT_EN_OFFSET 0x1c
  108. #define OMAP_TIMER_V2_IRQSTATUS_RAW 0x24
  109. #define OMAP_TIMER_V2_IRQSTATUS 0x28
  110. #define OMAP_TIMER_V2_IRQENABLE_SET 0x2c
  111. #define OMAP_TIMER_V2_IRQENABLE_CLR 0x30
  112. /*
  113. * The functional registers have a different base on v1 and v2 ip.
  114. * These registers are offsets from timer->func_base. The func_base
  115. * is samae as io_base for v1 and io_base + 0x14 for v2 ip.
  116. *
  117. */
  118. #define OMAP_TIMER_V2_FUNC_OFFSET 0x14
  119. #define _OMAP_TIMER_WAKEUP_EN_OFFSET 0x20
  120. #define _OMAP_TIMER_CTRL_OFFSET 0x24
  121. #define OMAP_TIMER_CTRL_GPOCFG (1 << 14)
  122. #define OMAP_TIMER_CTRL_CAPTMODE (1 << 13)
  123. #define OMAP_TIMER_CTRL_PT (1 << 12)
  124. #define OMAP_TIMER_CTRL_TCM_LOWTOHIGH (0x1 << 8)
  125. #define OMAP_TIMER_CTRL_TCM_HIGHTOLOW (0x2 << 8)
  126. #define OMAP_TIMER_CTRL_TCM_BOTHEDGES (0x3 << 8)
  127. #define OMAP_TIMER_CTRL_SCPWM (1 << 7)
  128. #define OMAP_TIMER_CTRL_CE (1 << 6) /* compare enable */
  129. #define OMAP_TIMER_CTRL_PRE (1 << 5) /* prescaler enable */
  130. #define OMAP_TIMER_CTRL_PTV_SHIFT 2 /* prescaler value shift */
  131. #define OMAP_TIMER_CTRL_POSTED (1 << 2)
  132. #define OMAP_TIMER_CTRL_AR (1 << 1) /* auto-reload enable */
  133. #define OMAP_TIMER_CTRL_ST (1 << 0) /* start timer */
  134. #define _OMAP_TIMER_COUNTER_OFFSET 0x28
  135. #define _OMAP_TIMER_LOAD_OFFSET 0x2c
  136. #define _OMAP_TIMER_TRIGGER_OFFSET 0x30
  137. #define _OMAP_TIMER_WRITE_PEND_OFFSET 0x34
  138. #define WP_NONE 0 /* no write pending bit */
  139. #define WP_TCLR (1 << 0)
  140. #define WP_TCRR (1 << 1)
  141. #define WP_TLDR (1 << 2)
  142. #define WP_TTGR (1 << 3)
  143. #define WP_TMAR (1 << 4)
  144. #define WP_TPIR (1 << 5)
  145. #define WP_TNIR (1 << 6)
  146. #define WP_TCVR (1 << 7)
  147. #define WP_TOCR (1 << 8)
  148. #define WP_TOWR (1 << 9)
  149. #define _OMAP_TIMER_MATCH_OFFSET 0x38
  150. #define _OMAP_TIMER_CAPTURE_OFFSET 0x3c
  151. #define _OMAP_TIMER_IF_CTRL_OFFSET 0x40
  152. #define _OMAP_TIMER_CAPTURE2_OFFSET 0x44 /* TCAR2, 34xx only */
  153. #define _OMAP_TIMER_TICK_POS_OFFSET 0x48 /* TPIR, 34xx only */
  154. #define _OMAP_TIMER_TICK_NEG_OFFSET 0x4c /* TNIR, 34xx only */
  155. #define _OMAP_TIMER_TICK_COUNT_OFFSET 0x50 /* TCVR, 34xx only */
  156. #define _OMAP_TIMER_TICK_INT_MASK_SET_OFFSET 0x54 /* TOCR, 34xx only */
  157. #define _OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET 0x58 /* TOWR, 34xx only */
  158. /* register offsets with the write pending bit encoded */
  159. #define WPSHIFT 16
  160. #define OMAP_TIMER_WAKEUP_EN_REG (_OMAP_TIMER_WAKEUP_EN_OFFSET \
  161. | (WP_NONE << WPSHIFT))
  162. #define OMAP_TIMER_CTRL_REG (_OMAP_TIMER_CTRL_OFFSET \
  163. | (WP_TCLR << WPSHIFT))
  164. #define OMAP_TIMER_COUNTER_REG (_OMAP_TIMER_COUNTER_OFFSET \
  165. | (WP_TCRR << WPSHIFT))
  166. #define OMAP_TIMER_LOAD_REG (_OMAP_TIMER_LOAD_OFFSET \
  167. | (WP_TLDR << WPSHIFT))
  168. #define OMAP_TIMER_TRIGGER_REG (_OMAP_TIMER_TRIGGER_OFFSET \
  169. | (WP_TTGR << WPSHIFT))
  170. #define OMAP_TIMER_WRITE_PEND_REG (_OMAP_TIMER_WRITE_PEND_OFFSET \
  171. | (WP_NONE << WPSHIFT))
  172. #define OMAP_TIMER_MATCH_REG (_OMAP_TIMER_MATCH_OFFSET \
  173. | (WP_TMAR << WPSHIFT))
  174. #define OMAP_TIMER_CAPTURE_REG (_OMAP_TIMER_CAPTURE_OFFSET \
  175. | (WP_NONE << WPSHIFT))
  176. #define OMAP_TIMER_IF_CTRL_REG (_OMAP_TIMER_IF_CTRL_OFFSET \
  177. | (WP_NONE << WPSHIFT))
  178. #define OMAP_TIMER_CAPTURE2_REG (_OMAP_TIMER_CAPTURE2_OFFSET \
  179. | (WP_NONE << WPSHIFT))
  180. #define OMAP_TIMER_TICK_POS_REG (_OMAP_TIMER_TICK_POS_OFFSET \
  181. | (WP_TPIR << WPSHIFT))
  182. #define OMAP_TIMER_TICK_NEG_REG (_OMAP_TIMER_TICK_NEG_OFFSET \
  183. | (WP_TNIR << WPSHIFT))
  184. #define OMAP_TIMER_TICK_COUNT_REG (_OMAP_TIMER_TICK_COUNT_OFFSET \
  185. | (WP_TCVR << WPSHIFT))
  186. #define OMAP_TIMER_TICK_INT_MASK_SET_REG \
  187. (_OMAP_TIMER_TICK_INT_MASK_SET_OFFSET | (WP_TOCR << WPSHIFT))
  188. #define OMAP_TIMER_TICK_INT_MASK_COUNT_REG \
  189. (_OMAP_TIMER_TICK_INT_MASK_COUNT_OFFSET | (WP_TOWR << WPSHIFT))
  190. struct omap_dm_timer {
  191. unsigned long phys_base;
  192. int id;
  193. int irq;
  194. struct clk *iclk, *fclk;
  195. void __iomem *io_base;
  196. void __iomem *sys_stat; /* TISTAT timer status */
  197. void __iomem *irq_stat; /* TISR/IRQSTATUS interrupt status */
  198. void __iomem *irq_ena; /* irq enable */
  199. void __iomem *irq_dis; /* irq disable, only on v2 ip */
  200. void __iomem *pend; /* write pending */
  201. void __iomem *func_base; /* function register base */
  202. unsigned long rate;
  203. unsigned reserved:1;
  204. unsigned posted:1;
  205. struct platform_device *pdev;
  206. struct list_head node;
  207. };
  208. int omap_dm_timer_prepare(struct omap_dm_timer *timer);
  209. static inline u32 __omap_dm_timer_read(struct omap_dm_timer *timer, u32 reg,
  210. int posted)
  211. {
  212. if (posted)
  213. while (__raw_readl(timer->pend) & (reg >> WPSHIFT))
  214. cpu_relax();
  215. return __raw_readl(timer->func_base + (reg & 0xff));
  216. }
  217. static inline void __omap_dm_timer_write(struct omap_dm_timer *timer,
  218. u32 reg, u32 val, int posted)
  219. {
  220. if (posted)
  221. while (__raw_readl(timer->pend) & (reg >> WPSHIFT))
  222. cpu_relax();
  223. __raw_writel(val, timer->func_base + (reg & 0xff));
  224. }
  225. static inline void __omap_dm_timer_init_regs(struct omap_dm_timer *timer)
  226. {
  227. u32 tidr;
  228. /* Assume v1 ip if bits [31:16] are zero */
  229. tidr = __raw_readl(timer->io_base);
  230. if (!(tidr >> 16)) {
  231. timer->sys_stat = timer->io_base +
  232. OMAP_TIMER_V1_SYS_STAT_OFFSET;
  233. timer->irq_stat = timer->io_base + OMAP_TIMER_V1_STAT_OFFSET;
  234. timer->irq_ena = timer->io_base + OMAP_TIMER_V1_INT_EN_OFFSET;
  235. timer->irq_dis = 0;
  236. timer->pend = timer->io_base + _OMAP_TIMER_WRITE_PEND_OFFSET;
  237. timer->func_base = timer->io_base;
  238. } else {
  239. timer->sys_stat = 0;
  240. timer->irq_stat = timer->io_base + OMAP_TIMER_V2_IRQSTATUS;
  241. timer->irq_ena = timer->io_base + OMAP_TIMER_V2_IRQENABLE_SET;
  242. timer->irq_dis = timer->io_base + OMAP_TIMER_V2_IRQENABLE_CLR;
  243. timer->pend = timer->io_base +
  244. _OMAP_TIMER_WRITE_PEND_OFFSET +
  245. OMAP_TIMER_V2_FUNC_OFFSET;
  246. timer->func_base = timer->io_base + OMAP_TIMER_V2_FUNC_OFFSET;
  247. }
  248. }
  249. /* Assumes the source clock has been set by caller */
  250. static inline void __omap_dm_timer_reset(struct omap_dm_timer *timer,
  251. int autoidle, int wakeup)
  252. {
  253. u32 l;
  254. l = __raw_readl(timer->io_base + OMAP_TIMER_OCP_CFG_OFFSET);
  255. l |= 0x02 << 3; /* Set to smart-idle mode */
  256. l |= 0x2 << 8; /* Set clock activity to perserve f-clock on idle */
  257. if (autoidle)
  258. l |= 0x1 << 0;
  259. if (wakeup)
  260. l |= 1 << 2;
  261. __raw_writel(l, timer->io_base + OMAP_TIMER_OCP_CFG_OFFSET);
  262. /* Match hardware reset default of posted mode */
  263. __omap_dm_timer_write(timer, OMAP_TIMER_IF_CTRL_REG,
  264. OMAP_TIMER_CTRL_POSTED, 0);
  265. }
  266. static inline int __omap_dm_timer_set_source(struct clk *timer_fck,
  267. struct clk *parent)
  268. {
  269. int ret;
  270. clk_disable(timer_fck);
  271. ret = clk_set_parent(timer_fck, parent);
  272. clk_enable(timer_fck);
  273. /*
  274. * When the functional clock disappears, too quick writes seem
  275. * to cause an abort. XXX Is this still necessary?
  276. */
  277. __delay(300000);
  278. return ret;
  279. }
  280. static inline void __omap_dm_timer_stop(struct omap_dm_timer *timer,
  281. int posted, unsigned long rate)
  282. {
  283. u32 l;
  284. l = __omap_dm_timer_read(timer, OMAP_TIMER_CTRL_REG, posted);
  285. if (l & OMAP_TIMER_CTRL_ST) {
  286. l &= ~0x1;
  287. __omap_dm_timer_write(timer, OMAP_TIMER_CTRL_REG, l, posted);
  288. #ifdef CONFIG_ARCH_OMAP2PLUS
  289. /* Readback to make sure write has completed */
  290. __omap_dm_timer_read(timer, OMAP_TIMER_CTRL_REG, posted);
  291. /*
  292. * Wait for functional clock period x 3.5 to make sure that
  293. * timer is stopped
  294. */
  295. udelay(3500000 / rate + 1);
  296. #endif
  297. }
  298. /* Ack possibly pending interrupt */
  299. __raw_writel(OMAP_TIMER_INT_OVERFLOW, timer->irq_stat);
  300. }
  301. static inline void __omap_dm_timer_load_start(struct omap_dm_timer *timer,
  302. u32 ctrl, unsigned int load,
  303. int posted)
  304. {
  305. __omap_dm_timer_write(timer, OMAP_TIMER_COUNTER_REG, load, posted);
  306. __omap_dm_timer_write(timer, OMAP_TIMER_CTRL_REG, ctrl, posted);
  307. }
  308. static inline void __omap_dm_timer_int_enable(struct omap_dm_timer *timer,
  309. unsigned int value)
  310. {
  311. __raw_writel(value, timer->irq_ena);
  312. __omap_dm_timer_write(timer, OMAP_TIMER_WAKEUP_EN_REG, value, 0);
  313. }
  314. static inline unsigned int
  315. __omap_dm_timer_read_counter(struct omap_dm_timer *timer, int posted)
  316. {
  317. return __omap_dm_timer_read(timer, OMAP_TIMER_COUNTER_REG, posted);
  318. }
  319. static inline void __omap_dm_timer_write_status(struct omap_dm_timer *timer,
  320. unsigned int value)
  321. {
  322. __raw_writel(value, timer->irq_stat);
  323. }
  324. #endif /* __ASM_ARCH_DMTIMER_H */