wm_io.c 8.1 KB

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