wm_io.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * @file wm_io.c
  3. *
  4. * @brief IO Driver Module
  5. *
  6. * @author lilm
  7. *
  8. * Copyright (c) 2015 Winner Microelectronics Co., Ltd.
  9. */
  10. #include "wm_regs.h"
  11. #include "wm_osal.h"
  12. #include "wm_io.h"
  13. #include "wm_dbg.h"
  14. #include "tls_common.h"
  15. static void io_cfg_option1(enum tls_io_name name)
  16. {
  17. u8 pin;
  18. u16 offset;
  19. if (name >= WM_IO_PB_00)
  20. {
  21. pin = name - WM_IO_PB_00;
  22. offset = TLS_IO_AB_OFFSET;
  23. }
  24. else
  25. {
  26. pin = name;
  27. offset = 0;
  28. }
  29. tls_reg_write32(HR_GPIO_AF_SEL + offset, tls_reg_read32(HR_GPIO_AF_SEL + offset) | BIT(pin)); /* gpio function */
  30. tls_reg_write32(HR_GPIO_AF_S1 + offset, tls_reg_read32(HR_GPIO_AF_S1 + offset) & (~BIT(pin)));
  31. tls_reg_write32(HR_GPIO_AF_S0 + offset, tls_reg_read32(HR_GPIO_AF_S0 + offset) & (~BIT(pin)));
  32. }
  33. static void io_cfg_option2(enum tls_io_name name)
  34. {
  35. u8 pin;
  36. u16 offset;
  37. if (name >= WM_IO_PB_00)
  38. {
  39. pin = name - WM_IO_PB_00;
  40. offset = TLS_IO_AB_OFFSET;
  41. }
  42. else
  43. {
  44. pin = name;
  45. offset = 0;
  46. }
  47. tls_reg_write32(HR_GPIO_AF_SEL + offset, tls_reg_read32(HR_GPIO_AF_SEL + offset) | BIT(pin)); /* gpio function */
  48. tls_reg_write32(HR_GPIO_AF_S1 + offset, tls_reg_read32(HR_GPIO_AF_S1 + offset) & (~BIT(pin)));
  49. tls_reg_write32(HR_GPIO_AF_S0 + offset, tls_reg_read32(HR_GPIO_AF_S0 + offset) | BIT(pin));
  50. }
  51. static void io_cfg_option3(enum tls_io_name name)
  52. {
  53. u8 pin;
  54. u16 offset;
  55. if (name >= WM_IO_PB_00)
  56. {
  57. pin = name - WM_IO_PB_00;
  58. offset = TLS_IO_AB_OFFSET;
  59. }
  60. else
  61. {
  62. pin = name;
  63. offset = 0;
  64. }
  65. tls_reg_write32(HR_GPIO_AF_SEL + offset, tls_reg_read32(HR_GPIO_AF_SEL + offset) | BIT(pin)); /* gpio function */
  66. tls_reg_write32(HR_GPIO_AF_S1 + offset, tls_reg_read32(HR_GPIO_AF_S1 + offset) | BIT(pin));
  67. tls_reg_write32(HR_GPIO_AF_S0 + offset, tls_reg_read32(HR_GPIO_AF_S0 + offset) & (~BIT(pin)));
  68. }
  69. static void io_cfg_option4(enum tls_io_name name)
  70. {
  71. u8 pin;
  72. u16 offset;
  73. if (name >= WM_IO_PB_00)
  74. {
  75. pin = name - WM_IO_PB_00;
  76. offset = TLS_IO_AB_OFFSET;
  77. }
  78. else
  79. {
  80. pin = name;
  81. offset = 0;
  82. }
  83. tls_reg_write32(HR_GPIO_AF_SEL + offset, tls_reg_read32(HR_GPIO_AF_SEL + offset) | BIT(pin)); /* gpio function */
  84. tls_reg_write32(HR_GPIO_AF_S1 + offset, tls_reg_read32(HR_GPIO_AF_S1 + offset) | BIT(pin));
  85. tls_reg_write32(HR_GPIO_AF_S0 + offset, tls_reg_read32(HR_GPIO_AF_S0 + offset) | BIT(pin));
  86. }
  87. static void io_cfg_option5(enum tls_io_name name)
  88. {
  89. u8 pin;
  90. u16 offset;
  91. if (name >= WM_IO_PB_00)
  92. {
  93. pin = name - WM_IO_PB_00;
  94. offset = TLS_IO_AB_OFFSET;
  95. }
  96. else
  97. {
  98. pin = name;
  99. offset = 0;
  100. }
  101. tls_reg_write32(HR_GPIO_AF_SEL + offset, tls_reg_read32(HR_GPIO_AF_SEL + offset) & (~BIT(pin))); /* disable gpio function */
  102. }
  103. static u32 io_pa_option67 = 0;
  104. static u32 io_pb_option67 = 0;
  105. static void io_cfg_option6(enum tls_io_name name)
  106. {
  107. u8 pin;
  108. u16 offset;
  109. if (name >= WM_IO_PB_00)
  110. {
  111. pin = name - WM_IO_PB_00;
  112. offset = TLS_IO_AB_OFFSET;
  113. io_pb_option67 |= BIT(pin);
  114. }
  115. else
  116. {
  117. pin = name;
  118. offset = 0;
  119. io_pa_option67 |= BIT(pin);
  120. }
  121. tls_reg_write32(HR_GPIO_AF_SEL + offset, tls_reg_read32(HR_GPIO_AF_SEL + offset) & (~BIT(pin))); /* disable gpio function */
  122. tls_reg_write32(HR_GPIO_DIR + offset, tls_reg_read32(HR_GPIO_DIR + offset) & (~BIT(pin)));
  123. tls_reg_write32(HR_GPIO_PULLUP_EN + offset, tls_reg_read32(HR_GPIO_PULLUP_EN + offset) | (BIT(pin)));
  124. tls_reg_write32(HR_GPIO_PULLDOWN_EN + offset, tls_reg_read32(HR_GPIO_PULLDOWN_EN + offset) & (~BIT(pin)));
  125. }
  126. static void io_cfg_option7(enum tls_io_name name)
  127. {
  128. u8 pin;
  129. u16 offset;
  130. if (name >= WM_IO_PB_00)
  131. {
  132. pin = name - WM_IO_PB_00;
  133. offset = TLS_IO_AB_OFFSET;
  134. io_pb_option67 &= BIT(pin);
  135. }
  136. else
  137. {
  138. pin = name;
  139. offset = 0;
  140. io_pa_option67 &= BIT(pin);
  141. }
  142. tls_reg_write32(HR_GPIO_AF_SEL + offset, tls_reg_read32(HR_GPIO_AF_SEL + offset) & (~BIT(pin))); /* enable gpio function */
  143. tls_reg_write32(HR_GPIO_DIR + offset, tls_reg_read32(HR_GPIO_DIR + offset) & (~BIT(pin))); /* set input */
  144. tls_reg_write32(HR_GPIO_PULLUP_EN + offset, tls_reg_read32(HR_GPIO_PULLUP_EN + offset) | (BIT(pin))); /* disable pull up */
  145. tls_reg_write32(HR_GPIO_PULLDOWN_EN + offset, tls_reg_read32(HR_GPIO_PULLDOWN_EN + offset) & (~BIT(pin))); /*disable pull down*/
  146. }
  147. /**
  148. * @brief This function is used to config io function
  149. *
  150. * @param[in] name io name
  151. * @param[in] option io function option, value is WM_IO_OPT*_*, also is WM_IO_OPTION1-6
  152. *
  153. * @return None
  154. *
  155. * @note None
  156. */
  157. void tls_io_cfg_set(enum tls_io_name name, u8 option)
  158. {
  159. if (WM_IO_OPTION1 == option)
  160. io_cfg_option1(name);
  161. else if (WM_IO_OPTION2 == option)
  162. io_cfg_option2(name);
  163. else if (WM_IO_OPTION3 == option)
  164. io_cfg_option3(name);
  165. else if (WM_IO_OPTION4 == option)
  166. io_cfg_option4(name);
  167. else if (WM_IO_OPTION5 == option)
  168. io_cfg_option5(name);
  169. else if (WM_IO_OPTION6 == option)
  170. io_cfg_option6(name);
  171. else if (WM_IO_OPTION7 == option)
  172. io_cfg_option7(name);
  173. else
  174. TLS_DBGPRT_IO_ERR("invalid io option.\r\n");
  175. }
  176. /**
  177. * @brief This function is used to get io function config
  178. *
  179. * @param[in] name io name
  180. *
  181. * @retval WM_IO_OPTION1~6 Mapping io function
  182. *
  183. * @note None
  184. */
  185. int tls_io_cfg_get(enum tls_io_name name)
  186. {
  187. u8 pin;
  188. u16 offset;
  189. u32 afsel,afs1,afs0,dir,pullupen, pulldownen;
  190. if (name >= WM_IO_PB_00)
  191. {
  192. pin = name - WM_IO_PB_00;
  193. offset = TLS_IO_AB_OFFSET;
  194. }
  195. else
  196. {
  197. pin = name;
  198. offset = 0;
  199. }
  200. afsel = tls_reg_read32(HR_GPIO_AF_SEL + offset);
  201. afs1 = tls_reg_read32(HR_GPIO_AF_S1 + offset);
  202. afs0 = tls_reg_read32(HR_GPIO_AF_S0 + offset);
  203. dir = tls_reg_read32(HR_GPIO_DIR + offset);
  204. pullupen = tls_reg_read32(HR_GPIO_PULLUP_EN + offset);
  205. pulldownen = tls_reg_read32(HR_GPIO_PULLDOWN_EN + offset);
  206. if(afsel&BIT(pin))
  207. {
  208. if((0==(afs1&BIT(pin))) && (0==(afs0&BIT(pin))))
  209. return WM_IO_OPTION1;
  210. else if((0==(afs1&BIT(pin))) && (afs0&BIT(pin)))
  211. return WM_IO_OPTION2;
  212. else if((afs1&BIT(pin)) && (0==(afs0&BIT(pin))))
  213. return WM_IO_OPTION3;
  214. else if((afs1&BIT(pin)) && (afs0&BIT(pin)))
  215. return WM_IO_OPTION4;
  216. }
  217. else
  218. {
  219. if((!(dir&BIT(pin))) && (pullupen&BIT(pin)) && (!(pulldownen&BIT(pin))))
  220. {
  221. if (offset)
  222. {
  223. if (io_pb_option67 & BIT(pin))
  224. {
  225. return WM_IO_OPTION6;
  226. }
  227. else
  228. {
  229. return WM_IO_OPTION7;
  230. }
  231. }
  232. else
  233. {
  234. if (io_pa_option67 & BIT(pin))
  235. {
  236. return WM_IO_OPTION6;
  237. }
  238. else
  239. {
  240. return WM_IO_OPTION7;
  241. }
  242. }
  243. }
  244. else
  245. return WM_IO_OPTION5;
  246. }
  247. return 0;
  248. }