halmac_pcie_8821c.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2016 - 2018 Realtek Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. ******************************************************************************/
  15. #include "halmac_pcie_8821c.h"
  16. #include "halmac_pwr_seq_8821c.h"
  17. #include "../halmac_init_88xx.h"
  18. #include "../halmac_common_88xx.h"
  19. #include "../halmac_pcie_88xx.h"
  20. #include "halmac_8821c_cfg.h"
  21. #if (HALMAC_8821C_SUPPORT && HALMAC_PCIE_SUPPORT)
  22. #define INTF_INTGRA_MINREF 90
  23. #define INTF_INTGRA_HOSTREF 100
  24. static u16
  25. get_target(struct halmac_adapter *adapter);
  26. static enum halmac_ret_status
  27. freerun_delay_us(struct halmac_adapter *adapter, u16 delay);
  28. /**
  29. * mac_pwr_switch_pcie_8821c() - switch mac power
  30. * @adapter : the adapter of halmac
  31. * @pwr : power state
  32. * Author : KaiYuan Chang / Ivan Lin
  33. * Return : enum halmac_ret_status
  34. * More details of status code can be found in prototype document
  35. */
  36. enum halmac_ret_status
  37. mac_pwr_switch_pcie_8821c(struct halmac_adapter *adapter,
  38. enum halmac_mac_power pwr)
  39. {
  40. u8 value8;
  41. u8 rpwm;
  42. struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
  43. enum halmac_ret_status status;
  44. PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
  45. PLTFM_MSG_TRACE("[TRACE]pwr = %x\n", pwr);
  46. PLTFM_MSG_TRACE("[TRACE]8821C pwr seq ver = %s\n",
  47. HALMAC_8821C_PWR_SEQ_VER);
  48. adapter->rpwm = HALMAC_REG_R8(REG_PCIE_HRPWM1_V1);
  49. /* Check FW still exist or not */
  50. if (HALMAC_REG_R16(REG_MCUFW_CTRL) == 0xC078) {
  51. /* Leave 32K */
  52. rpwm = (u8)((adapter->rpwm ^ BIT(7)) & 0x80);
  53. HALMAC_REG_W8(REG_PCIE_HRPWM1_V1, rpwm);
  54. }
  55. value8 = HALMAC_REG_R8(REG_CR);
  56. if (value8 == 0xEA)
  57. adapter->halmac_state.mac_pwr = HALMAC_MAC_POWER_OFF;
  58. else
  59. adapter->halmac_state.mac_pwr = HALMAC_MAC_POWER_ON;
  60. /* Check if power switch is needed */
  61. if (pwr == HALMAC_MAC_POWER_ON &&
  62. adapter->halmac_state.mac_pwr == HALMAC_MAC_POWER_ON) {
  63. PLTFM_MSG_WARN("[WARN]power state unchange!!\n");
  64. return HALMAC_RET_PWR_UNCHANGE;
  65. }
  66. if (pwr == HALMAC_MAC_POWER_OFF) {
  67. status = trxdma_check_idle_88xx(adapter);
  68. if (status != HALMAC_RET_SUCCESS)
  69. return status;
  70. if (pwr_seq_parser_88xx(adapter, card_dis_flow_8821c) !=
  71. HALMAC_RET_SUCCESS) {
  72. PLTFM_MSG_ERR("[ERR]Handle power off cmd error\n");
  73. return HALMAC_RET_POWER_OFF_FAIL;
  74. }
  75. adapter->halmac_state.mac_pwr = HALMAC_MAC_POWER_OFF;
  76. adapter->halmac_state.dlfw_state = HALMAC_DLFW_NONE;
  77. init_adapter_dynamic_param_88xx(adapter);
  78. } else {
  79. if (pwr_seq_parser_88xx(adapter, card_en_flow_8821c) !=
  80. HALMAC_RET_SUCCESS) {
  81. PLTFM_MSG_ERR("[ERR]Handle power on cmd error\n");
  82. return HALMAC_RET_POWER_ON_FAIL;
  83. }
  84. adapter->halmac_state.mac_pwr = HALMAC_MAC_POWER_ON;
  85. }
  86. PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
  87. return HALMAC_RET_SUCCESS;
  88. }
  89. /**
  90. * halmac_pcie_switch_8821c() - pcie gen1/gen2 switch
  91. * @adapter : the adapter of halmac
  92. * @cfg : gen1/gen2 selection
  93. * Author : KaiYuan Chang / Ivan Lin
  94. * Return : enum halmac_ret_status
  95. * More details of status code can be found in prototype document
  96. */
  97. enum halmac_ret_status
  98. pcie_switch_8821c(struct halmac_adapter *adapter, enum halmac_pcie_cfg cfg)
  99. {
  100. return HALMAC_RET_SUCCESS;
  101. }
  102. /**
  103. * phy_cfg_pcie_8821c() - phy config
  104. * @adapter : the adapter of halmac
  105. * Author : KaiYuan Chang / Ivan Lin
  106. * Return : enum halmac_ret_status
  107. * More details of status code can be found in prototype document
  108. */
  109. enum halmac_ret_status
  110. phy_cfg_pcie_8821c(struct halmac_adapter *adapter,
  111. enum halmac_intf_phy_platform pltfm)
  112. {
  113. enum halmac_ret_status status = HALMAC_RET_SUCCESS;
  114. PLTFM_MSG_TRACE("[TRACE]%s ===>\n", __func__);
  115. status = parse_intf_phy_88xx(adapter, pcie_gen1_phy_param_8821c, pltfm,
  116. HAL_INTF_PHY_PCIE_GEN1);
  117. if (status != HALMAC_RET_SUCCESS)
  118. return status;
  119. status = parse_intf_phy_88xx(adapter, pcie_gen2_phy_param_8821c, pltfm,
  120. HAL_INTF_PHY_PCIE_GEN2);
  121. if (status != HALMAC_RET_SUCCESS)
  122. return status;
  123. PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
  124. return HALMAC_RET_SUCCESS;
  125. }
  126. /**
  127. * intf_tun_pcie_8821c() - pcie interface fine tuning
  128. * @adapter : the adapter of halmac
  129. * Author : Rick Liu
  130. * Return : enum halmac_ret_status
  131. * More details of status code can be found in prototype document
  132. */
  133. enum halmac_ret_status
  134. intf_tun_pcie_8821c(struct halmac_adapter *adapter)
  135. {
  136. return HALMAC_RET_SUCCESS;
  137. }
  138. enum halmac_ret_status
  139. auto_refclk_cal_8821c_pcie(struct halmac_adapter *adapter)
  140. {
  141. u8 bdr_ori;
  142. u16 tmp_u16;
  143. u16 div_set;
  144. u16 mgn_tmp;
  145. u16 mgn_set;
  146. u16 tar;
  147. enum halmac_ret_status status = HALMAC_RET_SUCCESS;
  148. u8 l1_flag = 0;
  149. #if (INTF_INTGRA_HOSTREF <= INTF_INTGRA_MINREF || 0 >= INTF_INTGRA_MINREF)
  150. return status;
  151. #endif
  152. /* Disable L1BD */
  153. bdr_ori = dbi_r8_88xx(adapter, PCIE_L1_BACKDOOR);
  154. if (bdr_ori & (BIT(4) | BIT(3))) {
  155. status = dbi_w8_88xx(adapter, PCIE_L1_BACKDOOR,
  156. bdr_ori & ~(BIT(4) | BIT(3)));
  157. if (status != HALMAC_RET_SUCCESS)
  158. return status;
  159. l1_flag = 1;
  160. }
  161. /* Disable function */
  162. tmp_u16 = mdio_read_88xx(adapter, RAC_CTRL_PPR,
  163. HAL_INTF_PHY_PCIE_GEN1);
  164. if (tmp_u16 & BIT(9)) {
  165. status = mdio_write_88xx(adapter, RAC_CTRL_PPR,
  166. tmp_u16 & ~(BIT(9)),
  167. HAL_INTF_PHY_PCIE_GEN1);
  168. if (status != HALMAC_RET_SUCCESS)
  169. return status;
  170. }
  171. if (adapter->pcie_refautok_en == 0) {
  172. if (l1_flag == 1)
  173. status = dbi_w8_88xx(adapter, PCIE_L1_BACKDOOR,
  174. bdr_ori);
  175. return status;
  176. }
  177. /* Set div */
  178. tmp_u16 = mdio_read_88xx(adapter, RAC_CTRL_PPR, HAL_INTF_PHY_PCIE_GEN1);
  179. status = mdio_write_88xx(adapter, RAC_CTRL_PPR,
  180. tmp_u16 & ~(BIT(7) | BIT(6)),
  181. HAL_INTF_PHY_PCIE_GEN1);
  182. if (status != HALMAC_RET_SUCCESS)
  183. return status;
  184. /* Obtain div and margin */
  185. tar = get_target(adapter);
  186. if (tar == 0xFFFF)
  187. return HALMAC_RET_FAIL;
  188. mgn_tmp = tar * INTF_INTGRA_HOSTREF / INTF_INTGRA_MINREF - tar;
  189. if (mgn_tmp >= 128) {
  190. div_set = 0x0003;
  191. mgn_set = 0x000F;
  192. } else if (mgn_tmp >= 64) {
  193. div_set = 0x0003;
  194. mgn_set = mgn_tmp >> 3;
  195. } else if (mgn_tmp >= 32) {
  196. div_set = 0x0002;
  197. mgn_set = mgn_tmp >> 2;
  198. } else if (mgn_tmp >= 16) {
  199. div_set = 0x0001;
  200. mgn_set = mgn_tmp >> 1;
  201. } else if (mgn_tmp == 0) {
  202. div_set = 0x0000;
  203. mgn_set = 0x0001;
  204. } else {
  205. div_set = 0x0000;
  206. mgn_set = mgn_tmp;
  207. }
  208. /* Set div, margin, target*/
  209. tmp_u16 = mdio_read_88xx(adapter, RAC_CTRL_PPR, HAL_INTF_PHY_PCIE_GEN1);
  210. tmp_u16 = (tmp_u16 & ~(BIT(7) | BIT(6))) | (div_set << 6);
  211. status = mdio_write_88xx(adapter, RAC_CTRL_PPR,
  212. tmp_u16, HAL_INTF_PHY_PCIE_GEN1);
  213. if (status != HALMAC_RET_SUCCESS)
  214. return status;
  215. tar = get_target(adapter);
  216. if (tar == 0xFFFF)
  217. return HALMAC_RET_FAIL;
  218. PLTFM_MSG_TRACE("[TRACE]target = 0x%X, div = 0x%X, margin = 0x%X\n",
  219. tar, div_set, mgn_set);
  220. status = mdio_write_88xx(adapter, RAC_SET_PPR,
  221. (tar & 0x0FFF) | (mgn_set << 12),
  222. HAL_INTF_PHY_PCIE_GEN1);
  223. if (status != HALMAC_RET_SUCCESS)
  224. return status;
  225. /* Enable function */
  226. tmp_u16 = mdio_read_88xx(adapter, RAC_CTRL_PPR, HAL_INTF_PHY_PCIE_GEN1);
  227. status = mdio_write_88xx(adapter, RAC_CTRL_PPR, tmp_u16 | BIT(9),
  228. HAL_INTF_PHY_PCIE_GEN1);
  229. if (status != HALMAC_RET_SUCCESS)
  230. return status;
  231. PLTFM_MSG_TRACE("[TRACE]%s <===\n", __func__);
  232. /* Set L1BD to ori */
  233. if (l1_flag == 1)
  234. status = dbi_w8_88xx(adapter, PCIE_L1_BACKDOOR, bdr_ori);
  235. return status;
  236. }
  237. static u16
  238. get_target(struct halmac_adapter *adapter)
  239. {
  240. u16 tmp_u16;
  241. u16 tar;
  242. enum halmac_ret_status status = HALMAC_RET_SUCCESS;
  243. /* Enable counter */
  244. tmp_u16 = mdio_read_88xx(adapter, RAC_CTRL_PPR, HAL_INTF_PHY_PCIE_GEN1);
  245. status = mdio_write_88xx(adapter, RAC_CTRL_PPR,
  246. tmp_u16 | BIT(11), HAL_INTF_PHY_PCIE_GEN1);
  247. if (status != HALMAC_RET_SUCCESS)
  248. return 0xFFFF;
  249. /* Obtain target */
  250. status = freerun_delay_us(adapter, 300);
  251. if (status != HALMAC_RET_SUCCESS)
  252. return 0xFFFF;
  253. tar = mdio_read_88xx(adapter, RAC_TRG_PPR, HAL_INTF_PHY_PCIE_GEN1);
  254. if (tar == 0) {
  255. PLTFM_MSG_ERR("[ERR]Get target failed.\n");
  256. return 0xFFFF;
  257. }
  258. /* Disable counter */
  259. tmp_u16 = mdio_read_88xx(adapter, RAC_CTRL_PPR, HAL_INTF_PHY_PCIE_GEN1);
  260. status = mdio_write_88xx(adapter, RAC_CTRL_PPR,
  261. tmp_u16 & ~(BIT(11)), HAL_INTF_PHY_PCIE_GEN1);
  262. if (status != HALMAC_RET_SUCCESS)
  263. return 0xFFFF;
  264. return tar;
  265. }
  266. static enum halmac_ret_status
  267. freerun_delay_us(struct halmac_adapter *adapter, u16 delay)
  268. {
  269. u16 count;
  270. u8 mc_ori;
  271. u32 frcnt_ori;
  272. u32 frcnt_cmp;
  273. u8 frcnt_onflg = 0;
  274. u32 cmp_val;
  275. struct halmac_api *api = (struct halmac_api *)adapter->halmac_api;
  276. enum halmac_ret_status status = HALMAC_RET_SUCCESS;
  277. /* Enable free-run counter */
  278. mc_ori = HALMAC_REG_R8(REG_MISC_CTRL);
  279. if ((mc_ori & BIT(3)) == 0) {
  280. status = HALMAC_REG_W8(REG_MISC_CTRL, mc_ori | BIT(3));
  281. if (status != HALMAC_RET_SUCCESS)
  282. return status;
  283. frcnt_onflg = 1;
  284. }
  285. /* counting delay */
  286. count = 20;
  287. frcnt_ori = HALMAC_REG_R32(REG_FREERUN_CNT);
  288. PLTFM_MSG_TRACE("[TRACE]free_ori = 0x%X\n", frcnt_ori);
  289. do {
  290. PLTFM_DELAY_US(100);
  291. count--;
  292. frcnt_cmp = HALMAC_REG_R32(REG_FREERUN_CNT);
  293. PLTFM_MSG_TRACE("[TRACE]Count=0x%X, free_cmp=0x%X\n"
  294. , count, frcnt_cmp);
  295. if (frcnt_cmp >= frcnt_ori)
  296. cmp_val = frcnt_cmp - frcnt_ori;
  297. else
  298. cmp_val = 0xFFFFFFFF - frcnt_ori + frcnt_cmp;
  299. } while ((count > 0) && (cmp_val < delay));
  300. /* Reset freerun counter */
  301. if (frcnt_onflg != 1)
  302. return status;
  303. status = HALMAC_REG_W8(REG_MISC_CTRL, mc_ori);
  304. if (status != HALMAC_RET_SUCCESS)
  305. return status;
  306. status = HALMAC_REG_W8(REG_DUAL_TSF_RST,
  307. HALMAC_REG_R8(REG_DUAL_TSF_RST) | BIT(5));
  308. return status;
  309. }
  310. #endif /* HALMAC_8821C_SUPPORT */