gptimers.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * gptimers.c - Blackfin General Purpose Timer core API
  3. *
  4. * Copyright (c) 2005-2008 Analog Devices Inc.
  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 <linux/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. # if (MAX_BLACKFIN_GPTIMERS > 8)
  49. (GPTIMER_timer_regs *)TIMER8_CONFIG,
  50. (GPTIMER_timer_regs *)TIMER9_CONFIG,
  51. (GPTIMER_timer_regs *)TIMER10_CONFIG,
  52. # if (MAX_BLACKFIN_GPTIMERS > 11)
  53. (GPTIMER_timer_regs *)TIMER11_CONFIG,
  54. # endif
  55. # endif
  56. #endif
  57. };
  58. static volatile GPTIMER_group_regs *const group_regs[BFIN_TIMER_NUM_GROUP] =
  59. {
  60. (GPTIMER_group_regs *)TIMER0_GROUP_REG,
  61. #if (MAX_BLACKFIN_GPTIMERS > 8)
  62. (GPTIMER_group_regs *)TIMER8_GROUP_REG,
  63. #endif
  64. };
  65. static uint32_t const trun_mask[MAX_BLACKFIN_GPTIMERS] =
  66. {
  67. TIMER_STATUS_TRUN0,
  68. TIMER_STATUS_TRUN1,
  69. TIMER_STATUS_TRUN2,
  70. #if (MAX_BLACKFIN_GPTIMERS > 3)
  71. TIMER_STATUS_TRUN3,
  72. TIMER_STATUS_TRUN4,
  73. TIMER_STATUS_TRUN5,
  74. TIMER_STATUS_TRUN6,
  75. TIMER_STATUS_TRUN7,
  76. # if (MAX_BLACKFIN_GPTIMERS > 8)
  77. TIMER_STATUS_TRUN8,
  78. TIMER_STATUS_TRUN9,
  79. TIMER_STATUS_TRUN10,
  80. # if (MAX_BLACKFIN_GPTIMERS > 11)
  81. TIMER_STATUS_TRUN11,
  82. # endif
  83. # endif
  84. #endif
  85. };
  86. static uint32_t const tovf_mask[MAX_BLACKFIN_GPTIMERS] =
  87. {
  88. TIMER_STATUS_TOVF0,
  89. TIMER_STATUS_TOVF1,
  90. TIMER_STATUS_TOVF2,
  91. #if (MAX_BLACKFIN_GPTIMERS > 3)
  92. TIMER_STATUS_TOVF3,
  93. TIMER_STATUS_TOVF4,
  94. TIMER_STATUS_TOVF5,
  95. TIMER_STATUS_TOVF6,
  96. TIMER_STATUS_TOVF7,
  97. # if (MAX_BLACKFIN_GPTIMERS > 8)
  98. TIMER_STATUS_TOVF8,
  99. TIMER_STATUS_TOVF9,
  100. TIMER_STATUS_TOVF10,
  101. # if (MAX_BLACKFIN_GPTIMERS > 11)
  102. TIMER_STATUS_TOVF11,
  103. # endif
  104. # endif
  105. #endif
  106. };
  107. static uint32_t const timil_mask[MAX_BLACKFIN_GPTIMERS] =
  108. {
  109. TIMER_STATUS_TIMIL0,
  110. TIMER_STATUS_TIMIL1,
  111. TIMER_STATUS_TIMIL2,
  112. #if (MAX_BLACKFIN_GPTIMERS > 3)
  113. TIMER_STATUS_TIMIL3,
  114. TIMER_STATUS_TIMIL4,
  115. TIMER_STATUS_TIMIL5,
  116. TIMER_STATUS_TIMIL6,
  117. TIMER_STATUS_TIMIL7,
  118. # if (MAX_BLACKFIN_GPTIMERS > 8)
  119. TIMER_STATUS_TIMIL8,
  120. TIMER_STATUS_TIMIL9,
  121. TIMER_STATUS_TIMIL10,
  122. # if (MAX_BLACKFIN_GPTIMERS > 11)
  123. TIMER_STATUS_TIMIL11,
  124. # endif
  125. # endif
  126. #endif
  127. };
  128. void set_gptimer_pwidth(int timer_id, uint32_t value)
  129. {
  130. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  131. timer_regs[timer_id]->width = value;
  132. SSYNC();
  133. }
  134. EXPORT_SYMBOL(set_gptimer_pwidth);
  135. uint32_t get_gptimer_pwidth(int timer_id)
  136. {
  137. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  138. return timer_regs[timer_id]->width;
  139. }
  140. EXPORT_SYMBOL(get_gptimer_pwidth);
  141. void set_gptimer_period(int timer_id, uint32_t period)
  142. {
  143. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  144. timer_regs[timer_id]->period = period;
  145. SSYNC();
  146. }
  147. EXPORT_SYMBOL(set_gptimer_period);
  148. uint32_t get_gptimer_period(int timer_id)
  149. {
  150. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  151. return timer_regs[timer_id]->period;
  152. }
  153. EXPORT_SYMBOL(get_gptimer_period);
  154. uint32_t get_gptimer_count(int timer_id)
  155. {
  156. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  157. return timer_regs[timer_id]->counter;
  158. }
  159. EXPORT_SYMBOL(get_gptimer_count);
  160. uint32_t get_gptimer_status(int group)
  161. {
  162. tassert(group < BFIN_TIMER_NUM_GROUP);
  163. return group_regs[group]->status;
  164. }
  165. EXPORT_SYMBOL(get_gptimer_status);
  166. void set_gptimer_status(int group, uint32_t value)
  167. {
  168. tassert(group < BFIN_TIMER_NUM_GROUP);
  169. group_regs[group]->status = value;
  170. SSYNC();
  171. }
  172. EXPORT_SYMBOL(set_gptimer_status);
  173. uint16_t get_gptimer_intr(int timer_id)
  174. {
  175. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  176. return (group_regs[BFIN_TIMER_OCTET(timer_id)]->status & timil_mask[timer_id]) ? 1 : 0;
  177. }
  178. EXPORT_SYMBOL(get_gptimer_intr);
  179. void clear_gptimer_intr(int timer_id)
  180. {
  181. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  182. group_regs[BFIN_TIMER_OCTET(timer_id)]->status = timil_mask[timer_id];
  183. }
  184. EXPORT_SYMBOL(clear_gptimer_intr);
  185. uint16_t get_gptimer_over(int timer_id)
  186. {
  187. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  188. return (group_regs[BFIN_TIMER_OCTET(timer_id)]->status & tovf_mask[timer_id]) ? 1 : 0;
  189. }
  190. EXPORT_SYMBOL(get_gptimer_over);
  191. void clear_gptimer_over(int timer_id)
  192. {
  193. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  194. group_regs[BFIN_TIMER_OCTET(timer_id)]->status = tovf_mask[timer_id];
  195. }
  196. EXPORT_SYMBOL(clear_gptimer_over);
  197. void set_gptimer_config(int timer_id, uint16_t config)
  198. {
  199. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  200. timer_regs[timer_id]->config = config;
  201. SSYNC();
  202. }
  203. EXPORT_SYMBOL(set_gptimer_config);
  204. uint16_t get_gptimer_config(int timer_id)
  205. {
  206. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  207. return timer_regs[timer_id]->config;
  208. }
  209. EXPORT_SYMBOL(get_gptimer_config);
  210. void enable_gptimers(uint16_t mask)
  211. {
  212. int i;
  213. tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
  214. for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
  215. group_regs[i]->enable = mask & 0xFF;
  216. mask >>= 8;
  217. }
  218. SSYNC();
  219. }
  220. EXPORT_SYMBOL(enable_gptimers);
  221. void disable_gptimers(uint16_t mask)
  222. {
  223. int i;
  224. uint16_t m = mask;
  225. tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
  226. for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
  227. group_regs[i]->disable = m & 0xFF;
  228. m >>= 8;
  229. }
  230. for (i = 0; i < MAX_BLACKFIN_GPTIMERS; ++i)
  231. if (mask & (1 << i))
  232. group_regs[BFIN_TIMER_OCTET(i)]->status |= trun_mask[i];
  233. SSYNC();
  234. }
  235. EXPORT_SYMBOL(disable_gptimers);
  236. void set_gptimer_pulse_hi(int timer_id)
  237. {
  238. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  239. timer_regs[timer_id]->config |= TIMER_PULSE_HI;
  240. SSYNC();
  241. }
  242. EXPORT_SYMBOL(set_gptimer_pulse_hi);
  243. void clear_gptimer_pulse_hi(int timer_id)
  244. {
  245. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  246. timer_regs[timer_id]->config &= ~TIMER_PULSE_HI;
  247. SSYNC();
  248. }
  249. EXPORT_SYMBOL(clear_gptimer_pulse_hi);
  250. uint16_t get_enabled_gptimers(void)
  251. {
  252. int i;
  253. uint16_t result = 0;
  254. for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i)
  255. result |= (group_regs[i]->enable << (i << 3));
  256. return result;
  257. }
  258. EXPORT_SYMBOL(get_enabled_gptimers);
  259. MODULE_AUTHOR("Axel Weiss (awe@aglaia-gmbh.de)");
  260. MODULE_DESCRIPTION("Blackfin General Purpose Timers API");
  261. MODULE_LICENSE("GPL");