gptimers.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. static struct bfin_gptimer_regs * const timer_regs[MAX_BLACKFIN_GPTIMERS] =
  24. {
  25. (void *)TIMER0_CONFIG,
  26. (void *)TIMER1_CONFIG,
  27. (void *)TIMER2_CONFIG,
  28. #if (MAX_BLACKFIN_GPTIMERS > 3)
  29. (void *)TIMER3_CONFIG,
  30. (void *)TIMER4_CONFIG,
  31. (void *)TIMER5_CONFIG,
  32. (void *)TIMER6_CONFIG,
  33. (void *)TIMER7_CONFIG,
  34. # if (MAX_BLACKFIN_GPTIMERS > 8)
  35. (void *)TIMER8_CONFIG,
  36. (void *)TIMER9_CONFIG,
  37. (void *)TIMER10_CONFIG,
  38. # if (MAX_BLACKFIN_GPTIMERS > 11)
  39. (void *)TIMER11_CONFIG,
  40. # endif
  41. # endif
  42. #endif
  43. };
  44. static struct bfin_gptimer_group_regs * const group_regs[BFIN_TIMER_NUM_GROUP] =
  45. {
  46. (void *)TIMER0_GROUP_REG,
  47. #if (MAX_BLACKFIN_GPTIMERS > 8)
  48. (void *)TIMER8_GROUP_REG,
  49. #endif
  50. };
  51. static uint32_t const trun_mask[MAX_BLACKFIN_GPTIMERS] =
  52. {
  53. TIMER_STATUS_TRUN0,
  54. TIMER_STATUS_TRUN1,
  55. TIMER_STATUS_TRUN2,
  56. #if (MAX_BLACKFIN_GPTIMERS > 3)
  57. TIMER_STATUS_TRUN3,
  58. TIMER_STATUS_TRUN4,
  59. TIMER_STATUS_TRUN5,
  60. TIMER_STATUS_TRUN6,
  61. TIMER_STATUS_TRUN7,
  62. # if (MAX_BLACKFIN_GPTIMERS > 8)
  63. TIMER_STATUS_TRUN8,
  64. TIMER_STATUS_TRUN9,
  65. TIMER_STATUS_TRUN10,
  66. # if (MAX_BLACKFIN_GPTIMERS > 11)
  67. TIMER_STATUS_TRUN11,
  68. # endif
  69. # endif
  70. #endif
  71. };
  72. static uint32_t const tovf_mask[MAX_BLACKFIN_GPTIMERS] =
  73. {
  74. TIMER_STATUS_TOVF0,
  75. TIMER_STATUS_TOVF1,
  76. TIMER_STATUS_TOVF2,
  77. #if (MAX_BLACKFIN_GPTIMERS > 3)
  78. TIMER_STATUS_TOVF3,
  79. TIMER_STATUS_TOVF4,
  80. TIMER_STATUS_TOVF5,
  81. TIMER_STATUS_TOVF6,
  82. TIMER_STATUS_TOVF7,
  83. # if (MAX_BLACKFIN_GPTIMERS > 8)
  84. TIMER_STATUS_TOVF8,
  85. TIMER_STATUS_TOVF9,
  86. TIMER_STATUS_TOVF10,
  87. # if (MAX_BLACKFIN_GPTIMERS > 11)
  88. TIMER_STATUS_TOVF11,
  89. # endif
  90. # endif
  91. #endif
  92. };
  93. static uint32_t const timil_mask[MAX_BLACKFIN_GPTIMERS] =
  94. {
  95. TIMER_STATUS_TIMIL0,
  96. TIMER_STATUS_TIMIL1,
  97. TIMER_STATUS_TIMIL2,
  98. #if (MAX_BLACKFIN_GPTIMERS > 3)
  99. TIMER_STATUS_TIMIL3,
  100. TIMER_STATUS_TIMIL4,
  101. TIMER_STATUS_TIMIL5,
  102. TIMER_STATUS_TIMIL6,
  103. TIMER_STATUS_TIMIL7,
  104. # if (MAX_BLACKFIN_GPTIMERS > 8)
  105. TIMER_STATUS_TIMIL8,
  106. TIMER_STATUS_TIMIL9,
  107. TIMER_STATUS_TIMIL10,
  108. # if (MAX_BLACKFIN_GPTIMERS > 11)
  109. TIMER_STATUS_TIMIL11,
  110. # endif
  111. # endif
  112. #endif
  113. };
  114. void set_gptimer_pwidth(unsigned int timer_id, uint32_t value)
  115. {
  116. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  117. timer_regs[timer_id]->width = value;
  118. SSYNC();
  119. }
  120. EXPORT_SYMBOL(set_gptimer_pwidth);
  121. uint32_t get_gptimer_pwidth(unsigned int timer_id)
  122. {
  123. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  124. return timer_regs[timer_id]->width;
  125. }
  126. EXPORT_SYMBOL(get_gptimer_pwidth);
  127. void set_gptimer_period(unsigned int timer_id, uint32_t period)
  128. {
  129. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  130. timer_regs[timer_id]->period = period;
  131. SSYNC();
  132. }
  133. EXPORT_SYMBOL(set_gptimer_period);
  134. uint32_t get_gptimer_period(unsigned int timer_id)
  135. {
  136. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  137. return timer_regs[timer_id]->period;
  138. }
  139. EXPORT_SYMBOL(get_gptimer_period);
  140. uint32_t get_gptimer_count(unsigned int timer_id)
  141. {
  142. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  143. return timer_regs[timer_id]->counter;
  144. }
  145. EXPORT_SYMBOL(get_gptimer_count);
  146. uint32_t get_gptimer_status(unsigned int group)
  147. {
  148. tassert(group < BFIN_TIMER_NUM_GROUP);
  149. return group_regs[group]->status;
  150. }
  151. EXPORT_SYMBOL(get_gptimer_status);
  152. void set_gptimer_status(unsigned int group, uint32_t value)
  153. {
  154. tassert(group < BFIN_TIMER_NUM_GROUP);
  155. group_regs[group]->status = value;
  156. SSYNC();
  157. }
  158. EXPORT_SYMBOL(set_gptimer_status);
  159. int get_gptimer_intr(unsigned int timer_id)
  160. {
  161. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  162. return !!(group_regs[BFIN_TIMER_OCTET(timer_id)]->status & timil_mask[timer_id]);
  163. }
  164. EXPORT_SYMBOL(get_gptimer_intr);
  165. void clear_gptimer_intr(unsigned int timer_id)
  166. {
  167. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  168. group_regs[BFIN_TIMER_OCTET(timer_id)]->status = timil_mask[timer_id];
  169. }
  170. EXPORT_SYMBOL(clear_gptimer_intr);
  171. int get_gptimer_over(unsigned int timer_id)
  172. {
  173. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  174. return !!(group_regs[BFIN_TIMER_OCTET(timer_id)]->status & tovf_mask[timer_id]);
  175. }
  176. EXPORT_SYMBOL(get_gptimer_over);
  177. void clear_gptimer_over(unsigned int timer_id)
  178. {
  179. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  180. group_regs[BFIN_TIMER_OCTET(timer_id)]->status = tovf_mask[timer_id];
  181. }
  182. EXPORT_SYMBOL(clear_gptimer_over);
  183. int get_gptimer_run(unsigned int timer_id)
  184. {
  185. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  186. return !!(group_regs[BFIN_TIMER_OCTET(timer_id)]->status & trun_mask[timer_id]);
  187. }
  188. EXPORT_SYMBOL(get_gptimer_run);
  189. void set_gptimer_config(unsigned 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(unsigned 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. static 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. }
  223. void disable_gptimers(uint16_t mask)
  224. {
  225. int i;
  226. _disable_gptimers(mask);
  227. for (i = 0; i < MAX_BLACKFIN_GPTIMERS; ++i)
  228. if (mask & (1 << i))
  229. group_regs[BFIN_TIMER_OCTET(i)]->status = trun_mask[i];
  230. SSYNC();
  231. }
  232. EXPORT_SYMBOL(disable_gptimers);
  233. void disable_gptimers_sync(uint16_t mask)
  234. {
  235. _disable_gptimers(mask);
  236. SSYNC();
  237. }
  238. EXPORT_SYMBOL(disable_gptimers_sync);
  239. void set_gptimer_pulse_hi(unsigned int timer_id)
  240. {
  241. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  242. timer_regs[timer_id]->config |= TIMER_PULSE_HI;
  243. SSYNC();
  244. }
  245. EXPORT_SYMBOL(set_gptimer_pulse_hi);
  246. void clear_gptimer_pulse_hi(unsigned int timer_id)
  247. {
  248. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  249. timer_regs[timer_id]->config &= ~TIMER_PULSE_HI;
  250. SSYNC();
  251. }
  252. EXPORT_SYMBOL(clear_gptimer_pulse_hi);
  253. uint16_t get_enabled_gptimers(void)
  254. {
  255. int i;
  256. uint16_t result = 0;
  257. for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i)
  258. result |= (group_regs[i]->enable << (i << 3));
  259. return result;
  260. }
  261. EXPORT_SYMBOL(get_enabled_gptimers);
  262. MODULE_AUTHOR("Axel Weiss (awe@aglaia-gmbh.de)");
  263. MODULE_DESCRIPTION("Blackfin General Purpose Timers API");
  264. MODULE_LICENSE("GPL");