timer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
  3. * JZ4740 platform timer support
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. */
  15. #ifndef __ASM_MACH_JZ4740_TIMER
  16. #define __ASM_MACH_JZ4740_TIMER
  17. #define JZ_REG_TIMER_STOP 0x0C
  18. #define JZ_REG_TIMER_STOP_SET 0x1C
  19. #define JZ_REG_TIMER_STOP_CLEAR 0x2C
  20. #define JZ_REG_TIMER_ENABLE 0x00
  21. #define JZ_REG_TIMER_ENABLE_SET 0x04
  22. #define JZ_REG_TIMER_ENABLE_CLEAR 0x08
  23. #define JZ_REG_TIMER_FLAG 0x10
  24. #define JZ_REG_TIMER_FLAG_SET 0x14
  25. #define JZ_REG_TIMER_FLAG_CLEAR 0x18
  26. #define JZ_REG_TIMER_MASK 0x20
  27. #define JZ_REG_TIMER_MASK_SET 0x24
  28. #define JZ_REG_TIMER_MASK_CLEAR 0x28
  29. #define JZ_REG_TIMER_DFR(x) (((x) * 0x10) + 0x30)
  30. #define JZ_REG_TIMER_DHR(x) (((x) * 0x10) + 0x34)
  31. #define JZ_REG_TIMER_CNT(x) (((x) * 0x10) + 0x38)
  32. #define JZ_REG_TIMER_CTRL(x) (((x) * 0x10) + 0x3C)
  33. #define JZ_TIMER_IRQ_HALF(x) BIT((x) + 0x10)
  34. #define JZ_TIMER_IRQ_FULL(x) BIT(x)
  35. #define JZ_TIMER_CTRL_PWM_ABBRUPT_SHUTDOWN BIT(9)
  36. #define JZ_TIMER_CTRL_PWM_ACTIVE_LOW BIT(8)
  37. #define JZ_TIMER_CTRL_PWM_ENABLE BIT(7)
  38. #define JZ_TIMER_CTRL_PRESCALE_MASK 0x1c
  39. #define JZ_TIMER_CTRL_PRESCALE_OFFSET 0x3
  40. #define JZ_TIMER_CTRL_PRESCALE_1 (0 << 3)
  41. #define JZ_TIMER_CTRL_PRESCALE_4 (1 << 3)
  42. #define JZ_TIMER_CTRL_PRESCALE_16 (2 << 3)
  43. #define JZ_TIMER_CTRL_PRESCALE_64 (3 << 3)
  44. #define JZ_TIMER_CTRL_PRESCALE_256 (4 << 3)
  45. #define JZ_TIMER_CTRL_PRESCALE_1024 (5 << 3)
  46. #define JZ_TIMER_CTRL_PRESCALER(x) ((x) << JZ_TIMER_CTRL_PRESCALE_OFFSET)
  47. #define JZ_TIMER_CTRL_SRC_EXT BIT(2)
  48. #define JZ_TIMER_CTRL_SRC_RTC BIT(1)
  49. #define JZ_TIMER_CTRL_SRC_PCLK BIT(0)
  50. extern void __iomem *jz4740_timer_base;
  51. void __init jz4740_timer_init(void);
  52. void jz4740_timer_enable_watchdog(void);
  53. void jz4740_timer_disable_watchdog(void);
  54. static inline void jz4740_timer_stop(unsigned int timer)
  55. {
  56. writel(BIT(timer), jz4740_timer_base + JZ_REG_TIMER_STOP_SET);
  57. }
  58. static inline void jz4740_timer_start(unsigned int timer)
  59. {
  60. writel(BIT(timer), jz4740_timer_base + JZ_REG_TIMER_STOP_CLEAR);
  61. }
  62. static inline bool jz4740_timer_is_enabled(unsigned int timer)
  63. {
  64. return readb(jz4740_timer_base + JZ_REG_TIMER_ENABLE) & BIT(timer);
  65. }
  66. static inline void jz4740_timer_enable(unsigned int timer)
  67. {
  68. writeb(BIT(timer), jz4740_timer_base + JZ_REG_TIMER_ENABLE_SET);
  69. }
  70. static inline void jz4740_timer_disable(unsigned int timer)
  71. {
  72. writeb(BIT(timer), jz4740_timer_base + JZ_REG_TIMER_ENABLE_CLEAR);
  73. }
  74. static inline void jz4740_timer_set_period(unsigned int timer, uint16_t period)
  75. {
  76. writew(period, jz4740_timer_base + JZ_REG_TIMER_DFR(timer));
  77. }
  78. static inline void jz4740_timer_set_duty(unsigned int timer, uint16_t duty)
  79. {
  80. writew(duty, jz4740_timer_base + JZ_REG_TIMER_DHR(timer));
  81. }
  82. static inline void jz4740_timer_set_count(unsigned int timer, uint16_t count)
  83. {
  84. writew(count, jz4740_timer_base + JZ_REG_TIMER_CNT(timer));
  85. }
  86. static inline uint16_t jz4740_timer_get_count(unsigned int timer)
  87. {
  88. return readw(jz4740_timer_base + JZ_REG_TIMER_CNT(timer));
  89. }
  90. static inline void jz4740_timer_ack_full(unsigned int timer)
  91. {
  92. writel(JZ_TIMER_IRQ_FULL(timer), jz4740_timer_base + JZ_REG_TIMER_FLAG_CLEAR);
  93. }
  94. static inline void jz4740_timer_irq_full_enable(unsigned int timer)
  95. {
  96. writel(JZ_TIMER_IRQ_FULL(timer), jz4740_timer_base + JZ_REG_TIMER_FLAG_CLEAR);
  97. writel(JZ_TIMER_IRQ_FULL(timer), jz4740_timer_base + JZ_REG_TIMER_MASK_CLEAR);
  98. }
  99. static inline void jz4740_timer_irq_full_disable(unsigned int timer)
  100. {
  101. writel(JZ_TIMER_IRQ_FULL(timer), jz4740_timer_base + JZ_REG_TIMER_MASK_SET);
  102. }
  103. static inline void jz4740_timer_set_ctrl(unsigned int timer, uint16_t ctrl)
  104. {
  105. writew(ctrl, jz4740_timer_base + JZ_REG_TIMER_CTRL(timer));
  106. }
  107. static inline uint16_t jz4740_timer_get_ctrl(unsigned int timer)
  108. {
  109. return readw(jz4740_timer_base + JZ_REG_TIMER_CTRL(timer));
  110. }
  111. #endif