timer-ti-dm.h 13 KB

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