gptimers.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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(unsigned 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(unsigned 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(unsigned 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(unsigned 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(unsigned 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(unsigned 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(unsigned 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. int get_gptimer_intr(unsigned 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]);
  177. }
  178. EXPORT_SYMBOL(get_gptimer_intr);
  179. void clear_gptimer_intr(unsigned 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. int get_gptimer_over(unsigned 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]);
  189. }
  190. EXPORT_SYMBOL(get_gptimer_over);
  191. void clear_gptimer_over(unsigned 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. int get_gptimer_run(unsigned int timer_id)
  198. {
  199. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  200. return !!(group_regs[BFIN_TIMER_OCTET(timer_id)]->status & trun_mask[timer_id]);
  201. }
  202. EXPORT_SYMBOL(get_gptimer_run);
  203. void set_gptimer_config(unsigned int timer_id, uint16_t config)
  204. {
  205. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  206. timer_regs[timer_id]->config = config;
  207. SSYNC();
  208. }
  209. EXPORT_SYMBOL(set_gptimer_config);
  210. uint16_t get_gptimer_config(unsigned int timer_id)
  211. {
  212. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  213. return timer_regs[timer_id]->config;
  214. }
  215. EXPORT_SYMBOL(get_gptimer_config);
  216. void enable_gptimers(uint16_t mask)
  217. {
  218. int i;
  219. tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
  220. for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
  221. group_regs[i]->enable = mask & 0xFF;
  222. mask >>= 8;
  223. }
  224. SSYNC();
  225. }
  226. EXPORT_SYMBOL(enable_gptimers);
  227. static void _disable_gptimers(uint16_t mask)
  228. {
  229. int i;
  230. uint16_t m = mask;
  231. tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
  232. for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
  233. group_regs[i]->disable = m & 0xFF;
  234. m >>= 8;
  235. }
  236. }
  237. void disable_gptimers(uint16_t mask)
  238. {
  239. int i;
  240. _disable_gptimers(mask);
  241. for (i = 0; i < MAX_BLACKFIN_GPTIMERS; ++i)
  242. if (mask & (1 << i))
  243. group_regs[BFIN_TIMER_OCTET(i)]->status = trun_mask[i];
  244. SSYNC();
  245. }
  246. EXPORT_SYMBOL(disable_gptimers);
  247. void disable_gptimers_sync(uint16_t mask)
  248. {
  249. _disable_gptimers(mask);
  250. SSYNC();
  251. }
  252. EXPORT_SYMBOL(disable_gptimers_sync);
  253. void set_gptimer_pulse_hi(unsigned int timer_id)
  254. {
  255. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  256. timer_regs[timer_id]->config |= TIMER_PULSE_HI;
  257. SSYNC();
  258. }
  259. EXPORT_SYMBOL(set_gptimer_pulse_hi);
  260. void clear_gptimer_pulse_hi(unsigned int timer_id)
  261. {
  262. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  263. timer_regs[timer_id]->config &= ~TIMER_PULSE_HI;
  264. SSYNC();
  265. }
  266. EXPORT_SYMBOL(clear_gptimer_pulse_hi);
  267. uint16_t get_enabled_gptimers(void)
  268. {
  269. int i;
  270. uint16_t result = 0;
  271. for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i)
  272. result |= (group_regs[i]->enable << (i << 3));
  273. return result;
  274. }
  275. EXPORT_SYMBOL(get_enabled_gptimers);
  276. MODULE_AUTHOR("Axel Weiss (awe@aglaia-gmbh.de)");
  277. MODULE_DESCRIPTION("Blackfin General Purpose Timers API");
  278. MODULE_LICENSE("GPL");