gptimers.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * bfin_gptimers.c - derived from bf53x_timers.c
  3. * Driver for General Purpose Timer functions on the Blackfin processor
  4. *
  5. * Copyright (C) 2005 John DeHority
  6. * Copyright (C) 2006 Hella Aglaia GmbH (awe@aglaia-gmbh.de)
  7. *
  8. * Licensed under the GPLv2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <asm/io.h>
  13. #include <asm/blackfin.h>
  14. #include <asm/gptimers.h>
  15. #ifdef DEBUG
  16. # define tassert(expr)
  17. #else
  18. # define tassert(expr) \
  19. if (!(expr)) \
  20. printk(KERN_DEBUG "%s:%s:%i: Assertion failed: " #expr "\n", __FILE__, __func__, __LINE__);
  21. #endif
  22. #define BFIN_TIMER_NUM_GROUP (BFIN_TIMER_OCTET(MAX_BLACKFIN_GPTIMERS - 1) + 1)
  23. typedef struct {
  24. uint16_t config;
  25. uint16_t __pad;
  26. uint32_t counter;
  27. uint32_t period;
  28. uint32_t width;
  29. } GPTIMER_timer_regs;
  30. typedef struct {
  31. uint16_t enable;
  32. uint16_t __pad0;
  33. uint16_t disable;
  34. uint16_t __pad1;
  35. uint32_t status;
  36. } GPTIMER_group_regs;
  37. static volatile GPTIMER_timer_regs *const timer_regs[MAX_BLACKFIN_GPTIMERS] =
  38. {
  39. (GPTIMER_timer_regs *)TIMER0_CONFIG,
  40. (GPTIMER_timer_regs *)TIMER1_CONFIG,
  41. (GPTIMER_timer_regs *)TIMER2_CONFIG,
  42. #if (MAX_BLACKFIN_GPTIMERS > 3)
  43. (GPTIMER_timer_regs *)TIMER3_CONFIG,
  44. (GPTIMER_timer_regs *)TIMER4_CONFIG,
  45. (GPTIMER_timer_regs *)TIMER5_CONFIG,
  46. (GPTIMER_timer_regs *)TIMER6_CONFIG,
  47. (GPTIMER_timer_regs *)TIMER7_CONFIG,
  48. #endif
  49. #if (MAX_BLACKFIN_GPTIMERS > 8)
  50. (GPTIMER_timer_regs *)TIMER8_CONFIG,
  51. (GPTIMER_timer_regs *)TIMER9_CONFIG,
  52. (GPTIMER_timer_regs *)TIMER10_CONFIG,
  53. (GPTIMER_timer_regs *)TIMER11_CONFIG,
  54. #endif
  55. };
  56. static volatile GPTIMER_group_regs *const group_regs[BFIN_TIMER_NUM_GROUP] =
  57. {
  58. (GPTIMER_group_regs *)TIMER0_GROUP_REG,
  59. #if (MAX_BLACKFIN_GPTIMERS > 8)
  60. (GPTIMER_group_regs *)TIMER8_GROUP_REG,
  61. #endif
  62. };
  63. static uint32_t const trun_mask[MAX_BLACKFIN_GPTIMERS] =
  64. {
  65. TIMER_STATUS_TRUN0,
  66. TIMER_STATUS_TRUN1,
  67. TIMER_STATUS_TRUN2,
  68. #if (MAX_BLACKFIN_GPTIMERS > 3)
  69. TIMER_STATUS_TRUN3,
  70. TIMER_STATUS_TRUN4,
  71. TIMER_STATUS_TRUN5,
  72. TIMER_STATUS_TRUN6,
  73. TIMER_STATUS_TRUN7,
  74. #endif
  75. #if (MAX_BLACKFIN_GPTIMERS > 8)
  76. TIMER_STATUS_TRUN8,
  77. TIMER_STATUS_TRUN9,
  78. TIMER_STATUS_TRUN10,
  79. TIMER_STATUS_TRUN11,
  80. #endif
  81. };
  82. static uint32_t const tovf_mask[MAX_BLACKFIN_GPTIMERS] =
  83. {
  84. TIMER_STATUS_TOVF0,
  85. TIMER_STATUS_TOVF1,
  86. TIMER_STATUS_TOVF2,
  87. #if (MAX_BLACKFIN_GPTIMERS > 3)
  88. TIMER_STATUS_TOVF3,
  89. TIMER_STATUS_TOVF4,
  90. TIMER_STATUS_TOVF5,
  91. TIMER_STATUS_TOVF6,
  92. TIMER_STATUS_TOVF7,
  93. #endif
  94. #if (MAX_BLACKFIN_GPTIMERS > 8)
  95. TIMER_STATUS_TOVF8,
  96. TIMER_STATUS_TOVF9,
  97. TIMER_STATUS_TOVF10,
  98. TIMER_STATUS_TOVF11,
  99. #endif
  100. };
  101. static uint32_t const timil_mask[MAX_BLACKFIN_GPTIMERS] =
  102. {
  103. TIMER_STATUS_TIMIL0,
  104. TIMER_STATUS_TIMIL1,
  105. TIMER_STATUS_TIMIL2,
  106. #if (MAX_BLACKFIN_GPTIMERS > 3)
  107. TIMER_STATUS_TIMIL3,
  108. TIMER_STATUS_TIMIL4,
  109. TIMER_STATUS_TIMIL5,
  110. TIMER_STATUS_TIMIL6,
  111. TIMER_STATUS_TIMIL7,
  112. #endif
  113. #if (MAX_BLACKFIN_GPTIMERS > 8)
  114. TIMER_STATUS_TIMIL8,
  115. TIMER_STATUS_TIMIL9,
  116. TIMER_STATUS_TIMIL10,
  117. TIMER_STATUS_TIMIL11,
  118. #endif
  119. };
  120. void set_gptimer_pwidth(int timer_id, uint32_t value)
  121. {
  122. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  123. timer_regs[timer_id]->width = value;
  124. SSYNC();
  125. }
  126. EXPORT_SYMBOL(set_gptimer_pwidth);
  127. uint32_t get_gptimer_pwidth(int timer_id)
  128. {
  129. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  130. return timer_regs[timer_id]->width;
  131. }
  132. EXPORT_SYMBOL(get_gptimer_pwidth);
  133. void set_gptimer_period(int timer_id, uint32_t period)
  134. {
  135. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  136. timer_regs[timer_id]->period = period;
  137. SSYNC();
  138. }
  139. EXPORT_SYMBOL(set_gptimer_period);
  140. uint32_t get_gptimer_period(int timer_id)
  141. {
  142. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  143. return timer_regs[timer_id]->period;
  144. }
  145. EXPORT_SYMBOL(get_gptimer_period);
  146. uint32_t get_gptimer_count(int timer_id)
  147. {
  148. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  149. return timer_regs[timer_id]->counter;
  150. }
  151. EXPORT_SYMBOL(get_gptimer_count);
  152. uint32_t get_gptimer_status(int group)
  153. {
  154. tassert(group < BFIN_TIMER_NUM_GROUP);
  155. return group_regs[group]->status;
  156. }
  157. EXPORT_SYMBOL(get_gptimer_status);
  158. void set_gptimer_status(int group, uint32_t value)
  159. {
  160. tassert(group < BFIN_TIMER_NUM_GROUP);
  161. group_regs[group]->status = value;
  162. SSYNC();
  163. }
  164. EXPORT_SYMBOL(set_gptimer_status);
  165. uint16_t get_gptimer_intr(int timer_id)
  166. {
  167. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  168. return (group_regs[BFIN_TIMER_OCTET(timer_id)]->status & timil_mask[timer_id]) ? 1 : 0;
  169. }
  170. EXPORT_SYMBOL(get_gptimer_intr);
  171. void clear_gptimer_intr(int timer_id)
  172. {
  173. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  174. group_regs[BFIN_TIMER_OCTET(timer_id)]->status = timil_mask[timer_id];
  175. }
  176. EXPORT_SYMBOL(clear_gptimer_intr);
  177. uint16_t get_gptimer_over(int timer_id)
  178. {
  179. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  180. return (group_regs[BFIN_TIMER_OCTET(timer_id)]->status & tovf_mask[timer_id]) ? 1 : 0;
  181. }
  182. EXPORT_SYMBOL(get_gptimer_over);
  183. void clear_gptimer_over(int timer_id)
  184. {
  185. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  186. group_regs[BFIN_TIMER_OCTET(timer_id)]->status = tovf_mask[timer_id];
  187. }
  188. EXPORT_SYMBOL(clear_gptimer_over);
  189. void set_gptimer_config(int timer_id, uint16_t config)
  190. {
  191. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  192. timer_regs[timer_id]->config = config;
  193. SSYNC();
  194. }
  195. EXPORT_SYMBOL(set_gptimer_config);
  196. uint16_t get_gptimer_config(int timer_id)
  197. {
  198. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  199. return timer_regs[timer_id]->config;
  200. }
  201. EXPORT_SYMBOL(get_gptimer_config);
  202. void enable_gptimers(uint16_t mask)
  203. {
  204. int i;
  205. tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
  206. for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
  207. group_regs[i]->enable = mask & 0xFF;
  208. mask >>= 8;
  209. }
  210. SSYNC();
  211. }
  212. EXPORT_SYMBOL(enable_gptimers);
  213. void disable_gptimers(uint16_t mask)
  214. {
  215. int i;
  216. uint16_t m = mask;
  217. tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
  218. for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
  219. group_regs[i]->disable = m & 0xFF;
  220. m >>= 8;
  221. }
  222. for (i = 0; i < MAX_BLACKFIN_GPTIMERS; ++i)
  223. if (mask & (1 << i))
  224. group_regs[BFIN_TIMER_OCTET(i)]->status |= trun_mask[i];
  225. SSYNC();
  226. }
  227. EXPORT_SYMBOL(disable_gptimers);
  228. void set_gptimer_pulse_hi(int timer_id)
  229. {
  230. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  231. timer_regs[timer_id]->config |= TIMER_PULSE_HI;
  232. SSYNC();
  233. }
  234. EXPORT_SYMBOL(set_gptimer_pulse_hi);
  235. void clear_gptimer_pulse_hi(int timer_id)
  236. {
  237. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  238. timer_regs[timer_id]->config &= ~TIMER_PULSE_HI;
  239. SSYNC();
  240. }
  241. EXPORT_SYMBOL(clear_gptimer_pulse_hi);
  242. uint16_t get_enabled_gptimers(void)
  243. {
  244. int i;
  245. uint16_t result = 0;
  246. for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i)
  247. result |= (group_regs[i]->enable << (i << 3));
  248. return result;
  249. }
  250. EXPORT_SYMBOL(get_enabled_gptimers);
  251. MODULE_AUTHOR("Axel Weiss (awe@aglaia-gmbh.de)");
  252. MODULE_DESCRIPTION("Blackfin General Purpose Timers API");
  253. MODULE_LICENSE("GPL");