gptimers.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. #ifndef CONFIG_BF60x
  23. # define BFIN_TIMER_NUM_GROUP (BFIN_TIMER_OCTET(MAX_BLACKFIN_GPTIMERS - 1) + 1)
  24. #else
  25. # define BFIN_TIMER_NUM_GROUP 1
  26. #endif
  27. static struct bfin_gptimer_regs * const timer_regs[MAX_BLACKFIN_GPTIMERS] =
  28. {
  29. (void *)TIMER0_CONFIG,
  30. (void *)TIMER1_CONFIG,
  31. (void *)TIMER2_CONFIG,
  32. #if (MAX_BLACKFIN_GPTIMERS > 3)
  33. (void *)TIMER3_CONFIG,
  34. (void *)TIMER4_CONFIG,
  35. (void *)TIMER5_CONFIG,
  36. (void *)TIMER6_CONFIG,
  37. (void *)TIMER7_CONFIG,
  38. # if (MAX_BLACKFIN_GPTIMERS > 8)
  39. (void *)TIMER8_CONFIG,
  40. (void *)TIMER9_CONFIG,
  41. (void *)TIMER10_CONFIG,
  42. # if (MAX_BLACKFIN_GPTIMERS > 11)
  43. (void *)TIMER11_CONFIG,
  44. # endif
  45. # endif
  46. #endif
  47. };
  48. static struct bfin_gptimer_group_regs * const group_regs[BFIN_TIMER_NUM_GROUP] =
  49. {
  50. (void *)TIMER0_GROUP_REG,
  51. #if (MAX_BLACKFIN_GPTIMERS > 8)
  52. (void *)TIMER8_GROUP_REG,
  53. #endif
  54. };
  55. static uint32_t const trun_mask[MAX_BLACKFIN_GPTIMERS] =
  56. {
  57. TIMER_STATUS_TRUN0,
  58. TIMER_STATUS_TRUN1,
  59. TIMER_STATUS_TRUN2,
  60. #if (MAX_BLACKFIN_GPTIMERS > 3)
  61. TIMER_STATUS_TRUN3,
  62. TIMER_STATUS_TRUN4,
  63. TIMER_STATUS_TRUN5,
  64. TIMER_STATUS_TRUN6,
  65. TIMER_STATUS_TRUN7,
  66. # if (MAX_BLACKFIN_GPTIMERS > 8)
  67. TIMER_STATUS_TRUN8,
  68. TIMER_STATUS_TRUN9,
  69. TIMER_STATUS_TRUN10,
  70. # if (MAX_BLACKFIN_GPTIMERS > 11)
  71. TIMER_STATUS_TRUN11,
  72. # endif
  73. # endif
  74. #endif
  75. };
  76. static uint32_t const tovf_mask[MAX_BLACKFIN_GPTIMERS] =
  77. {
  78. TIMER_STATUS_TOVF0,
  79. TIMER_STATUS_TOVF1,
  80. TIMER_STATUS_TOVF2,
  81. #if (MAX_BLACKFIN_GPTIMERS > 3)
  82. TIMER_STATUS_TOVF3,
  83. TIMER_STATUS_TOVF4,
  84. TIMER_STATUS_TOVF5,
  85. TIMER_STATUS_TOVF6,
  86. TIMER_STATUS_TOVF7,
  87. # if (MAX_BLACKFIN_GPTIMERS > 8)
  88. TIMER_STATUS_TOVF8,
  89. TIMER_STATUS_TOVF9,
  90. TIMER_STATUS_TOVF10,
  91. # if (MAX_BLACKFIN_GPTIMERS > 11)
  92. TIMER_STATUS_TOVF11,
  93. # endif
  94. # endif
  95. #endif
  96. };
  97. static uint32_t const timil_mask[MAX_BLACKFIN_GPTIMERS] =
  98. {
  99. TIMER_STATUS_TIMIL0,
  100. TIMER_STATUS_TIMIL1,
  101. TIMER_STATUS_TIMIL2,
  102. #if (MAX_BLACKFIN_GPTIMERS > 3)
  103. TIMER_STATUS_TIMIL3,
  104. TIMER_STATUS_TIMIL4,
  105. TIMER_STATUS_TIMIL5,
  106. TIMER_STATUS_TIMIL6,
  107. TIMER_STATUS_TIMIL7,
  108. # if (MAX_BLACKFIN_GPTIMERS > 8)
  109. TIMER_STATUS_TIMIL8,
  110. TIMER_STATUS_TIMIL9,
  111. TIMER_STATUS_TIMIL10,
  112. # if (MAX_BLACKFIN_GPTIMERS > 11)
  113. TIMER_STATUS_TIMIL11,
  114. # endif
  115. # endif
  116. #endif
  117. };
  118. void set_gptimer_pwidth(unsigned int timer_id, uint32_t value)
  119. {
  120. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  121. bfin_write(&timer_regs[timer_id]->width, value);
  122. SSYNC();
  123. }
  124. EXPORT_SYMBOL(set_gptimer_pwidth);
  125. uint32_t get_gptimer_pwidth(unsigned int timer_id)
  126. {
  127. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  128. return bfin_read(&timer_regs[timer_id]->width);
  129. }
  130. EXPORT_SYMBOL(get_gptimer_pwidth);
  131. void set_gptimer_period(unsigned int timer_id, uint32_t period)
  132. {
  133. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  134. bfin_write(&timer_regs[timer_id]->period, period);
  135. SSYNC();
  136. }
  137. EXPORT_SYMBOL(set_gptimer_period);
  138. uint32_t get_gptimer_period(unsigned int timer_id)
  139. {
  140. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  141. return bfin_read(&timer_regs[timer_id]->period);
  142. }
  143. EXPORT_SYMBOL(get_gptimer_period);
  144. uint32_t get_gptimer_count(unsigned int timer_id)
  145. {
  146. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  147. return bfin_read(&timer_regs[timer_id]->counter);
  148. }
  149. EXPORT_SYMBOL(get_gptimer_count);
  150. #ifdef CONFIG_BF60x
  151. void set_gptimer_delay(unsigned int timer_id, uint32_t delay)
  152. {
  153. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  154. bfin_write(&timer_regs[timer_id]->delay, delay);
  155. SSYNC();
  156. }
  157. EXPORT_SYMBOL(set_gptimer_delay);
  158. uint32_t get_gptimer_delay(unsigned int timer_id)
  159. {
  160. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  161. return bfin_read(&timer_regs[timer_id]->delay);
  162. }
  163. EXPORT_SYMBOL(get_gptimer_delay);
  164. #endif
  165. #ifdef CONFIG_BF60x
  166. int get_gptimer_intr(unsigned int timer_id)
  167. {
  168. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  169. return !!(bfin_read(&group_regs[BFIN_TIMER_OCTET(timer_id)]->data_ilat) & timil_mask[timer_id]);
  170. }
  171. EXPORT_SYMBOL(get_gptimer_intr);
  172. void clear_gptimer_intr(unsigned int timer_id)
  173. {
  174. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  175. bfin_write(&group_regs[BFIN_TIMER_OCTET(timer_id)]->data_ilat, timil_mask[timer_id]);
  176. }
  177. EXPORT_SYMBOL(clear_gptimer_intr);
  178. int get_gptimer_over(unsigned int timer_id)
  179. {
  180. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  181. return !!(bfin_read(&group_regs[BFIN_TIMER_OCTET(timer_id)]->stat_ilat) & tovf_mask[timer_id]);
  182. }
  183. EXPORT_SYMBOL(get_gptimer_over);
  184. void clear_gptimer_over(unsigned int timer_id)
  185. {
  186. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  187. bfin_write(&group_regs[BFIN_TIMER_OCTET(timer_id)]->stat_ilat, tovf_mask[timer_id]);
  188. }
  189. EXPORT_SYMBOL(clear_gptimer_over);
  190. int get_gptimer_run(unsigned int timer_id)
  191. {
  192. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  193. return !!(bfin_read(&group_regs[BFIN_TIMER_OCTET(timer_id)]->run) & trun_mask[timer_id]);
  194. }
  195. EXPORT_SYMBOL(get_gptimer_run);
  196. uint32_t get_gptimer_status(unsigned int group)
  197. {
  198. tassert(group < BFIN_TIMER_NUM_GROUP);
  199. return bfin_read(&group_regs[group]->data_ilat);
  200. }
  201. EXPORT_SYMBOL(get_gptimer_status);
  202. void set_gptimer_status(unsigned int group, uint32_t value)
  203. {
  204. tassert(group < BFIN_TIMER_NUM_GROUP);
  205. bfin_write(&group_regs[group]->data_ilat, value);
  206. SSYNC();
  207. }
  208. EXPORT_SYMBOL(set_gptimer_status);
  209. #else
  210. uint32_t get_gptimer_status(unsigned int group)
  211. {
  212. tassert(group < BFIN_TIMER_NUM_GROUP);
  213. return bfin_read(&group_regs[group]->status);
  214. }
  215. EXPORT_SYMBOL(get_gptimer_status);
  216. void set_gptimer_status(unsigned int group, uint32_t value)
  217. {
  218. tassert(group < BFIN_TIMER_NUM_GROUP);
  219. bfin_write(&group_regs[group]->status, value);
  220. SSYNC();
  221. }
  222. EXPORT_SYMBOL(set_gptimer_status);
  223. static uint32_t read_gptimer_status(unsigned int timer_id)
  224. {
  225. return bfin_read(&group_regs[BFIN_TIMER_OCTET(timer_id)]->status);
  226. }
  227. int get_gptimer_intr(unsigned int timer_id)
  228. {
  229. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  230. return !!(read_gptimer_status(timer_id) & timil_mask[timer_id]);
  231. }
  232. EXPORT_SYMBOL(get_gptimer_intr);
  233. void clear_gptimer_intr(unsigned int timer_id)
  234. {
  235. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  236. bfin_write(&group_regs[BFIN_TIMER_OCTET(timer_id)]->status, timil_mask[timer_id]);
  237. }
  238. EXPORT_SYMBOL(clear_gptimer_intr);
  239. int get_gptimer_over(unsigned int timer_id)
  240. {
  241. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  242. return !!(read_gptimer_status(timer_id) & tovf_mask[timer_id]);
  243. }
  244. EXPORT_SYMBOL(get_gptimer_over);
  245. void clear_gptimer_over(unsigned int timer_id)
  246. {
  247. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  248. bfin_write(&group_regs[BFIN_TIMER_OCTET(timer_id)]->status, tovf_mask[timer_id]);
  249. }
  250. EXPORT_SYMBOL(clear_gptimer_over);
  251. int get_gptimer_run(unsigned int timer_id)
  252. {
  253. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  254. return !!(read_gptimer_status(timer_id) & trun_mask[timer_id]);
  255. }
  256. EXPORT_SYMBOL(get_gptimer_run);
  257. #endif
  258. void set_gptimer_config(unsigned int timer_id, uint16_t config)
  259. {
  260. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  261. bfin_write(&timer_regs[timer_id]->config, config);
  262. SSYNC();
  263. }
  264. EXPORT_SYMBOL(set_gptimer_config);
  265. uint16_t get_gptimer_config(unsigned int timer_id)
  266. {
  267. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  268. return bfin_read(&timer_regs[timer_id]->config);
  269. }
  270. EXPORT_SYMBOL(get_gptimer_config);
  271. void enable_gptimers(uint16_t mask)
  272. {
  273. int i;
  274. #ifdef CONFIG_BF60x
  275. uint16_t imask;
  276. imask = bfin_read16(TIMER_DATA_IMSK);
  277. imask &= ~mask;
  278. bfin_write16(TIMER_DATA_IMSK, imask);
  279. #endif
  280. tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
  281. for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
  282. bfin_write(&group_regs[i]->enable, mask & 0xFF);
  283. mask >>= 8;
  284. }
  285. SSYNC();
  286. }
  287. EXPORT_SYMBOL(enable_gptimers);
  288. static void _disable_gptimers(uint16_t mask)
  289. {
  290. int i;
  291. uint16_t m = mask;
  292. tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
  293. for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
  294. bfin_write(&group_regs[i]->disable, m & 0xFF);
  295. m >>= 8;
  296. }
  297. }
  298. void disable_gptimers(uint16_t mask)
  299. {
  300. #ifndef CONFIG_BF60x
  301. int i;
  302. _disable_gptimers(mask);
  303. for (i = 0; i < MAX_BLACKFIN_GPTIMERS; ++i)
  304. if (mask & (1 << i))
  305. bfin_write(&group_regs[BFIN_TIMER_OCTET(i)]->status, trun_mask[i]);
  306. SSYNC();
  307. #else
  308. _disable_gptimers(mask);
  309. #endif
  310. }
  311. EXPORT_SYMBOL(disable_gptimers);
  312. void disable_gptimers_sync(uint16_t mask)
  313. {
  314. _disable_gptimers(mask);
  315. SSYNC();
  316. }
  317. EXPORT_SYMBOL(disable_gptimers_sync);
  318. void set_gptimer_pulse_hi(unsigned int timer_id)
  319. {
  320. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  321. bfin_write_or(&timer_regs[timer_id]->config, TIMER_PULSE_HI);
  322. SSYNC();
  323. }
  324. EXPORT_SYMBOL(set_gptimer_pulse_hi);
  325. void clear_gptimer_pulse_hi(unsigned int timer_id)
  326. {
  327. tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
  328. bfin_write_and(&timer_regs[timer_id]->config, ~TIMER_PULSE_HI);
  329. SSYNC();
  330. }
  331. EXPORT_SYMBOL(clear_gptimer_pulse_hi);
  332. uint16_t get_enabled_gptimers(void)
  333. {
  334. int i;
  335. uint16_t result = 0;
  336. for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i)
  337. result |= (bfin_read(&group_regs[i]->enable) << (i << 3));
  338. return result;
  339. }
  340. EXPORT_SYMBOL(get_enabled_gptimers);
  341. MODULE_AUTHOR("Axel Weiss (awe@aglaia-gmbh.de)");
  342. MODULE_DESCRIPTION("Blackfin General Purpose Timers API");
  343. MODULE_LICENSE("GPL");