hns_dsaf_misc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*
  2. * Copyright (c) 2014-2015 Hisilicon Limited.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include "hns_dsaf_mac.h"
  10. #include "hns_dsaf_misc.h"
  11. #include "hns_dsaf_ppe.h"
  12. #include "hns_dsaf_reg.h"
  13. enum _dsm_op_index {
  14. HNS_OP_RESET_FUNC = 0x1,
  15. HNS_OP_SERDES_LP_FUNC = 0x2,
  16. HNS_OP_LED_SET_FUNC = 0x3,
  17. HNS_OP_GET_PORT_TYPE_FUNC = 0x4,
  18. HNS_OP_GET_SFP_STAT_FUNC = 0x5,
  19. HNS_OP_LOCATE_LED_SET_FUNC = 0x6,
  20. };
  21. enum _dsm_rst_type {
  22. HNS_DSAF_RESET_FUNC = 0x1,
  23. HNS_PPE_RESET_FUNC = 0x2,
  24. HNS_XGE_RESET_FUNC = 0x4,
  25. HNS_GE_RESET_FUNC = 0x5,
  26. HNS_DSAF_CHN_RESET_FUNC = 0x6,
  27. HNS_ROCE_RESET_FUNC = 0x7,
  28. };
  29. static const guid_t hns_dsaf_acpi_dsm_guid =
  30. GUID_INIT(0x1A85AA1A, 0xE293, 0x415E,
  31. 0x8E, 0x28, 0x8D, 0x69, 0x0A, 0x0F, 0x82, 0x0A);
  32. static void dsaf_write_sub(struct dsaf_device *dsaf_dev, u32 reg, u32 val)
  33. {
  34. if (dsaf_dev->sub_ctrl)
  35. dsaf_write_syscon(dsaf_dev->sub_ctrl, reg, val);
  36. else
  37. dsaf_write_reg(dsaf_dev->sc_base, reg, val);
  38. }
  39. static u32 dsaf_read_sub(struct dsaf_device *dsaf_dev, u32 reg)
  40. {
  41. u32 ret = 0;
  42. int err;
  43. if (dsaf_dev->sub_ctrl) {
  44. err = dsaf_read_syscon(dsaf_dev->sub_ctrl, reg, &ret);
  45. if (err)
  46. dev_err(dsaf_dev->dev, "dsaf_read_syscon error %d!\n",
  47. err);
  48. } else {
  49. ret = dsaf_read_reg(dsaf_dev->sc_base, reg);
  50. }
  51. return ret;
  52. }
  53. static void hns_dsaf_acpi_ledctrl_by_port(struct hns_mac_cb *mac_cb, u8 op_type,
  54. u32 link, u32 port, u32 act)
  55. {
  56. union acpi_object *obj;
  57. union acpi_object obj_args[3], argv4;
  58. obj_args[0].integer.type = ACPI_TYPE_INTEGER;
  59. obj_args[0].integer.value = link;
  60. obj_args[1].integer.type = ACPI_TYPE_INTEGER;
  61. obj_args[1].integer.value = port;
  62. obj_args[2].integer.type = ACPI_TYPE_INTEGER;
  63. obj_args[2].integer.value = act;
  64. argv4.type = ACPI_TYPE_PACKAGE;
  65. argv4.package.count = 3;
  66. argv4.package.elements = obj_args;
  67. obj = acpi_evaluate_dsm(ACPI_HANDLE(mac_cb->dev),
  68. &hns_dsaf_acpi_dsm_guid, 0, op_type, &argv4);
  69. if (!obj) {
  70. dev_warn(mac_cb->dev, "ledctrl fail, link:%d port:%d act:%d!\n",
  71. link, port, act);
  72. return;
  73. }
  74. ACPI_FREE(obj);
  75. }
  76. static void hns_dsaf_acpi_locate_ledctrl_by_port(struct hns_mac_cb *mac_cb,
  77. u8 op_type, u32 locate,
  78. u32 port)
  79. {
  80. union acpi_object obj_args[2], argv4;
  81. union acpi_object *obj;
  82. obj_args[0].integer.type = ACPI_TYPE_INTEGER;
  83. obj_args[0].integer.value = locate;
  84. obj_args[1].integer.type = ACPI_TYPE_INTEGER;
  85. obj_args[1].integer.value = port;
  86. argv4.type = ACPI_TYPE_PACKAGE;
  87. argv4.package.count = 2;
  88. argv4.package.elements = obj_args;
  89. obj = acpi_evaluate_dsm(ACPI_HANDLE(mac_cb->dev),
  90. &hns_dsaf_acpi_dsm_guid, 0, op_type, &argv4);
  91. if (!obj) {
  92. dev_err(mac_cb->dev, "ledctrl fail, locate:%d port:%d!\n",
  93. locate, port);
  94. return;
  95. }
  96. ACPI_FREE(obj);
  97. }
  98. static void hns_cpld_set_led(struct hns_mac_cb *mac_cb, int link_status,
  99. u16 speed, int data)
  100. {
  101. int speed_reg = 0;
  102. u8 value;
  103. if (!mac_cb) {
  104. pr_err("sfp_led_opt mac_dev is null!\n");
  105. return;
  106. }
  107. if (!mac_cb->cpld_ctrl) {
  108. dev_err(mac_cb->dev, "mac_id=%d, cpld syscon is null !\n",
  109. mac_cb->mac_id);
  110. return;
  111. }
  112. if (speed == MAC_SPEED_10000)
  113. speed_reg = 1;
  114. value = mac_cb->cpld_led_value;
  115. if (link_status) {
  116. dsaf_set_bit(value, DSAF_LED_LINK_B, link_status);
  117. dsaf_set_field(value, DSAF_LED_SPEED_M,
  118. DSAF_LED_SPEED_S, speed_reg);
  119. dsaf_set_bit(value, DSAF_LED_DATA_B, data);
  120. if (value != mac_cb->cpld_led_value) {
  121. dsaf_write_syscon(mac_cb->cpld_ctrl,
  122. mac_cb->cpld_ctrl_reg, value);
  123. mac_cb->cpld_led_value = value;
  124. }
  125. } else {
  126. value = (mac_cb->cpld_led_value) & (0x1 << DSAF_LED_ANCHOR_B);
  127. dsaf_write_syscon(mac_cb->cpld_ctrl,
  128. mac_cb->cpld_ctrl_reg, value);
  129. mac_cb->cpld_led_value = value;
  130. }
  131. }
  132. static void hns_cpld_set_led_acpi(struct hns_mac_cb *mac_cb, int link_status,
  133. u16 speed, int data)
  134. {
  135. if (!mac_cb) {
  136. pr_err("cpld_led_set mac_cb is null!\n");
  137. return;
  138. }
  139. hns_dsaf_acpi_ledctrl_by_port(mac_cb, HNS_OP_LED_SET_FUNC,
  140. link_status, mac_cb->mac_id, data);
  141. }
  142. static void cpld_led_reset(struct hns_mac_cb *mac_cb)
  143. {
  144. if (!mac_cb || !mac_cb->cpld_ctrl)
  145. return;
  146. dsaf_write_syscon(mac_cb->cpld_ctrl, mac_cb->cpld_ctrl_reg,
  147. CPLD_LED_DEFAULT_VALUE);
  148. mac_cb->cpld_led_value = CPLD_LED_DEFAULT_VALUE;
  149. }
  150. static void cpld_led_reset_acpi(struct hns_mac_cb *mac_cb)
  151. {
  152. if (!mac_cb) {
  153. pr_err("cpld_led_reset mac_cb is null!\n");
  154. return;
  155. }
  156. if (mac_cb->media_type != HNAE_MEDIA_TYPE_FIBER)
  157. return;
  158. hns_dsaf_acpi_ledctrl_by_port(mac_cb, HNS_OP_LED_SET_FUNC,
  159. 0, mac_cb->mac_id, 0);
  160. }
  161. static int cpld_set_led_id(struct hns_mac_cb *mac_cb,
  162. enum hnae_led_state status)
  163. {
  164. u32 val = 0;
  165. int ret;
  166. if (!mac_cb->cpld_ctrl)
  167. return 0;
  168. switch (status) {
  169. case HNAE_LED_ACTIVE:
  170. ret = dsaf_read_syscon(mac_cb->cpld_ctrl, mac_cb->cpld_ctrl_reg,
  171. &val);
  172. if (ret)
  173. return ret;
  174. dsaf_set_bit(val, DSAF_LED_ANCHOR_B, CPLD_LED_ON_VALUE);
  175. dsaf_write_syscon(mac_cb->cpld_ctrl, mac_cb->cpld_ctrl_reg,
  176. val);
  177. mac_cb->cpld_led_value = val;
  178. break;
  179. case HNAE_LED_INACTIVE:
  180. dsaf_set_bit(mac_cb->cpld_led_value, DSAF_LED_ANCHOR_B,
  181. CPLD_LED_DEFAULT_VALUE);
  182. dsaf_write_syscon(mac_cb->cpld_ctrl, mac_cb->cpld_ctrl_reg,
  183. mac_cb->cpld_led_value);
  184. break;
  185. default:
  186. dev_err(mac_cb->dev, "invalid led state: %d!", status);
  187. return -EINVAL;
  188. }
  189. return 0;
  190. }
  191. static int cpld_set_led_id_acpi(struct hns_mac_cb *mac_cb,
  192. enum hnae_led_state status)
  193. {
  194. switch (status) {
  195. case HNAE_LED_ACTIVE:
  196. hns_dsaf_acpi_locate_ledctrl_by_port(mac_cb,
  197. HNS_OP_LOCATE_LED_SET_FUNC,
  198. CPLD_LED_ON_VALUE,
  199. mac_cb->mac_id);
  200. break;
  201. case HNAE_LED_INACTIVE:
  202. hns_dsaf_acpi_locate_ledctrl_by_port(mac_cb,
  203. HNS_OP_LOCATE_LED_SET_FUNC,
  204. CPLD_LED_DEFAULT_VALUE,
  205. mac_cb->mac_id);
  206. break;
  207. default:
  208. dev_err(mac_cb->dev, "invalid led state: %d!", status);
  209. return -EINVAL;
  210. }
  211. return 0;
  212. }
  213. #define RESET_REQ_OR_DREQ 1
  214. static void hns_dsaf_acpi_srst_by_port(struct dsaf_device *dsaf_dev, u8 op_type,
  215. u32 port_type, u32 port, u32 val)
  216. {
  217. union acpi_object *obj;
  218. union acpi_object obj_args[3], argv4;
  219. obj_args[0].integer.type = ACPI_TYPE_INTEGER;
  220. obj_args[0].integer.value = port_type;
  221. obj_args[1].integer.type = ACPI_TYPE_INTEGER;
  222. obj_args[1].integer.value = port;
  223. obj_args[2].integer.type = ACPI_TYPE_INTEGER;
  224. obj_args[2].integer.value = val;
  225. argv4.type = ACPI_TYPE_PACKAGE;
  226. argv4.package.count = 3;
  227. argv4.package.elements = obj_args;
  228. obj = acpi_evaluate_dsm(ACPI_HANDLE(dsaf_dev->dev),
  229. &hns_dsaf_acpi_dsm_guid, 0, op_type, &argv4);
  230. if (!obj) {
  231. dev_warn(dsaf_dev->dev, "reset port_type%d port%d fail!",
  232. port_type, port);
  233. return;
  234. }
  235. ACPI_FREE(obj);
  236. }
  237. static void hns_dsaf_rst(struct dsaf_device *dsaf_dev, bool dereset)
  238. {
  239. u32 xbar_reg_addr;
  240. u32 nt_reg_addr;
  241. if (!dereset) {
  242. xbar_reg_addr = DSAF_SUB_SC_XBAR_RESET_REQ_REG;
  243. nt_reg_addr = DSAF_SUB_SC_NT_RESET_REQ_REG;
  244. } else {
  245. xbar_reg_addr = DSAF_SUB_SC_XBAR_RESET_DREQ_REG;
  246. nt_reg_addr = DSAF_SUB_SC_NT_RESET_DREQ_REG;
  247. }
  248. dsaf_write_sub(dsaf_dev, xbar_reg_addr, RESET_REQ_OR_DREQ);
  249. dsaf_write_sub(dsaf_dev, nt_reg_addr, RESET_REQ_OR_DREQ);
  250. }
  251. static void hns_dsaf_rst_acpi(struct dsaf_device *dsaf_dev, bool dereset)
  252. {
  253. hns_dsaf_acpi_srst_by_port(dsaf_dev, HNS_OP_RESET_FUNC,
  254. HNS_DSAF_RESET_FUNC,
  255. 0, dereset);
  256. }
  257. static void hns_dsaf_xge_srst_by_port(struct dsaf_device *dsaf_dev, u32 port,
  258. bool dereset)
  259. {
  260. u32 reg_val = 0;
  261. u32 reg_addr;
  262. if (port >= DSAF_XGE_NUM)
  263. return;
  264. reg_val |= RESET_REQ_OR_DREQ;
  265. reg_val |= 0x2082082 << dsaf_dev->mac_cb[port]->port_rst_off;
  266. if (!dereset)
  267. reg_addr = DSAF_SUB_SC_XGE_RESET_REQ_REG;
  268. else
  269. reg_addr = DSAF_SUB_SC_XGE_RESET_DREQ_REG;
  270. dsaf_write_sub(dsaf_dev, reg_addr, reg_val);
  271. }
  272. static void hns_dsaf_xge_srst_by_port_acpi(struct dsaf_device *dsaf_dev,
  273. u32 port, bool dereset)
  274. {
  275. hns_dsaf_acpi_srst_by_port(dsaf_dev, HNS_OP_RESET_FUNC,
  276. HNS_XGE_RESET_FUNC, port, dereset);
  277. }
  278. /**
  279. * hns_dsaf_srst_chns - reset dsaf channels
  280. * @dsaf_dev: dsaf device struct pointer
  281. * @msk: xbar channels mask value:
  282. * bit0-5 for xge0-5
  283. * bit6-11 for ppe0-5
  284. * bit12-17 for roce0-5
  285. * bit18-19 for com/dfx
  286. * @enable: false - request reset , true - drop reset
  287. */
  288. static void
  289. hns_dsaf_srst_chns(struct dsaf_device *dsaf_dev, u32 msk, bool dereset)
  290. {
  291. u32 reg_addr;
  292. if (!dereset)
  293. reg_addr = DSAF_SUB_SC_DSAF_RESET_REQ_REG;
  294. else
  295. reg_addr = DSAF_SUB_SC_DSAF_RESET_DREQ_REG;
  296. dsaf_write_sub(dsaf_dev, reg_addr, msk);
  297. }
  298. /**
  299. * hns_dsaf_srst_chns - reset dsaf channels
  300. * @dsaf_dev: dsaf device struct pointer
  301. * @msk: xbar channels mask value:
  302. * bit0-5 for xge0-5
  303. * bit6-11 for ppe0-5
  304. * bit12-17 for roce0-5
  305. * bit18-19 for com/dfx
  306. * @enable: false - request reset , true - drop reset
  307. */
  308. static void
  309. hns_dsaf_srst_chns_acpi(struct dsaf_device *dsaf_dev, u32 msk, bool dereset)
  310. {
  311. hns_dsaf_acpi_srst_by_port(dsaf_dev, HNS_OP_RESET_FUNC,
  312. HNS_DSAF_CHN_RESET_FUNC,
  313. msk, dereset);
  314. }
  315. static void hns_dsaf_roce_srst(struct dsaf_device *dsaf_dev, bool dereset)
  316. {
  317. if (!dereset) {
  318. dsaf_write_sub(dsaf_dev, DSAF_SUB_SC_ROCEE_RESET_REQ_REG, 1);
  319. } else {
  320. dsaf_write_sub(dsaf_dev,
  321. DSAF_SUB_SC_ROCEE_CLK_DIS_REG, 1);
  322. dsaf_write_sub(dsaf_dev,
  323. DSAF_SUB_SC_ROCEE_RESET_DREQ_REG, 1);
  324. msleep(20);
  325. dsaf_write_sub(dsaf_dev, DSAF_SUB_SC_ROCEE_CLK_EN_REG, 1);
  326. }
  327. }
  328. static void hns_dsaf_roce_srst_acpi(struct dsaf_device *dsaf_dev, bool dereset)
  329. {
  330. hns_dsaf_acpi_srst_by_port(dsaf_dev, HNS_OP_RESET_FUNC,
  331. HNS_ROCE_RESET_FUNC, 0, dereset);
  332. }
  333. static void hns_dsaf_ge_srst_by_port(struct dsaf_device *dsaf_dev, u32 port,
  334. bool dereset)
  335. {
  336. u32 reg_val_1;
  337. u32 reg_val_2;
  338. u32 port_rst_off;
  339. if (port >= DSAF_GE_NUM)
  340. return;
  341. if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) {
  342. reg_val_1 = 0x1 << port;
  343. port_rst_off = dsaf_dev->mac_cb[port]->port_rst_off;
  344. /* there is difference between V1 and V2 in register.*/
  345. reg_val_2 = AE_IS_VER1(dsaf_dev->dsaf_ver) ?
  346. 0x1041041 : 0x2082082;
  347. reg_val_2 <<= port_rst_off;
  348. if (!dereset) {
  349. dsaf_write_sub(dsaf_dev, DSAF_SUB_SC_GE_RESET_REQ1_REG,
  350. reg_val_1);
  351. dsaf_write_sub(dsaf_dev, DSAF_SUB_SC_GE_RESET_REQ0_REG,
  352. reg_val_2);
  353. } else {
  354. dsaf_write_sub(dsaf_dev, DSAF_SUB_SC_GE_RESET_DREQ0_REG,
  355. reg_val_2);
  356. dsaf_write_sub(dsaf_dev, DSAF_SUB_SC_GE_RESET_DREQ1_REG,
  357. reg_val_1);
  358. }
  359. } else {
  360. reg_val_1 = 0x15540;
  361. reg_val_2 = AE_IS_VER1(dsaf_dev->dsaf_ver) ? 0x100 : 0x40;
  362. reg_val_1 <<= dsaf_dev->reset_offset;
  363. reg_val_2 <<= dsaf_dev->reset_offset;
  364. if (!dereset) {
  365. dsaf_write_sub(dsaf_dev, DSAF_SUB_SC_GE_RESET_REQ1_REG,
  366. reg_val_1);
  367. dsaf_write_sub(dsaf_dev, DSAF_SUB_SC_PPE_RESET_REQ_REG,
  368. reg_val_2);
  369. } else {
  370. dsaf_write_sub(dsaf_dev, DSAF_SUB_SC_GE_RESET_DREQ1_REG,
  371. reg_val_1);
  372. dsaf_write_sub(dsaf_dev, DSAF_SUB_SC_PPE_RESET_DREQ_REG,
  373. reg_val_2);
  374. }
  375. }
  376. }
  377. static void hns_dsaf_ge_srst_by_port_acpi(struct dsaf_device *dsaf_dev,
  378. u32 port, bool dereset)
  379. {
  380. hns_dsaf_acpi_srst_by_port(dsaf_dev, HNS_OP_RESET_FUNC,
  381. HNS_GE_RESET_FUNC, port, dereset);
  382. }
  383. static void hns_ppe_srst_by_port(struct dsaf_device *dsaf_dev, u32 port,
  384. bool dereset)
  385. {
  386. u32 reg_val = 0;
  387. u32 reg_addr;
  388. reg_val |= RESET_REQ_OR_DREQ << dsaf_dev->mac_cb[port]->port_rst_off;
  389. if (!dereset)
  390. reg_addr = DSAF_SUB_SC_PPE_RESET_REQ_REG;
  391. else
  392. reg_addr = DSAF_SUB_SC_PPE_RESET_DREQ_REG;
  393. dsaf_write_sub(dsaf_dev, reg_addr, reg_val);
  394. }
  395. static void
  396. hns_ppe_srst_by_port_acpi(struct dsaf_device *dsaf_dev, u32 port, bool dereset)
  397. {
  398. hns_dsaf_acpi_srst_by_port(dsaf_dev, HNS_OP_RESET_FUNC,
  399. HNS_PPE_RESET_FUNC, port, dereset);
  400. }
  401. static void hns_ppe_com_srst(struct dsaf_device *dsaf_dev, bool dereset)
  402. {
  403. u32 reg_val;
  404. u32 reg_addr;
  405. if (!(dev_of_node(dsaf_dev->dev)))
  406. return;
  407. if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) {
  408. reg_val = RESET_REQ_OR_DREQ;
  409. if (!dereset)
  410. reg_addr = DSAF_SUB_SC_RCB_PPE_COM_RESET_REQ_REG;
  411. else
  412. reg_addr = DSAF_SUB_SC_RCB_PPE_COM_RESET_DREQ_REG;
  413. } else {
  414. reg_val = 0x100 << dsaf_dev->reset_offset;
  415. if (!dereset)
  416. reg_addr = DSAF_SUB_SC_PPE_RESET_REQ_REG;
  417. else
  418. reg_addr = DSAF_SUB_SC_PPE_RESET_DREQ_REG;
  419. }
  420. dsaf_write_sub(dsaf_dev, reg_addr, reg_val);
  421. }
  422. /**
  423. * hns_mac_get_sds_mode - get phy ifterface form serdes mode
  424. * @mac_cb: mac control block
  425. * retuen phy interface
  426. */
  427. static phy_interface_t hns_mac_get_phy_if(struct hns_mac_cb *mac_cb)
  428. {
  429. u32 mode;
  430. u32 reg;
  431. bool is_ver1 = AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver);
  432. int mac_id = mac_cb->mac_id;
  433. phy_interface_t phy_if;
  434. if (is_ver1) {
  435. if (HNS_DSAF_IS_DEBUG(mac_cb->dsaf_dev))
  436. return PHY_INTERFACE_MODE_SGMII;
  437. if (mac_id >= 0 && mac_id <= 3)
  438. reg = HNS_MAC_HILINK4_REG;
  439. else
  440. reg = HNS_MAC_HILINK3_REG;
  441. } else{
  442. if (!HNS_DSAF_IS_DEBUG(mac_cb->dsaf_dev) && mac_id <= 3)
  443. reg = HNS_MAC_HILINK4V2_REG;
  444. else
  445. reg = HNS_MAC_HILINK3V2_REG;
  446. }
  447. mode = dsaf_read_sub(mac_cb->dsaf_dev, reg);
  448. if (dsaf_get_bit(mode, mac_cb->port_mode_off))
  449. phy_if = PHY_INTERFACE_MODE_XGMII;
  450. else
  451. phy_if = PHY_INTERFACE_MODE_SGMII;
  452. return phy_if;
  453. }
  454. static phy_interface_t hns_mac_get_phy_if_acpi(struct hns_mac_cb *mac_cb)
  455. {
  456. phy_interface_t phy_if = PHY_INTERFACE_MODE_NA;
  457. union acpi_object *obj;
  458. union acpi_object obj_args, argv4;
  459. obj_args.integer.type = ACPI_TYPE_INTEGER;
  460. obj_args.integer.value = mac_cb->mac_id;
  461. argv4.type = ACPI_TYPE_PACKAGE,
  462. argv4.package.count = 1,
  463. argv4.package.elements = &obj_args,
  464. obj = acpi_evaluate_dsm(ACPI_HANDLE(mac_cb->dev),
  465. &hns_dsaf_acpi_dsm_guid, 0,
  466. HNS_OP_GET_PORT_TYPE_FUNC, &argv4);
  467. if (!obj || obj->type != ACPI_TYPE_INTEGER)
  468. return phy_if;
  469. phy_if = obj->integer.value ?
  470. PHY_INTERFACE_MODE_XGMII : PHY_INTERFACE_MODE_SGMII;
  471. dev_dbg(mac_cb->dev, "mac_id=%d, phy_if=%d\n", mac_cb->mac_id, phy_if);
  472. ACPI_FREE(obj);
  473. return phy_if;
  474. }
  475. static int hns_mac_get_sfp_prsnt(struct hns_mac_cb *mac_cb, int *sfp_prsnt)
  476. {
  477. u32 val = 0;
  478. int ret;
  479. if (!mac_cb->cpld_ctrl)
  480. return -ENODEV;
  481. ret = dsaf_read_syscon(mac_cb->cpld_ctrl,
  482. mac_cb->cpld_ctrl_reg + MAC_SFP_PORT_OFFSET,
  483. &val);
  484. if (ret)
  485. return ret;
  486. *sfp_prsnt = !val;
  487. return 0;
  488. }
  489. static int hns_mac_get_sfp_prsnt_acpi(struct hns_mac_cb *mac_cb, int *sfp_prsnt)
  490. {
  491. union acpi_object *obj;
  492. union acpi_object obj_args, argv4;
  493. obj_args.integer.type = ACPI_TYPE_INTEGER;
  494. obj_args.integer.value = mac_cb->mac_id;
  495. argv4.type = ACPI_TYPE_PACKAGE,
  496. argv4.package.count = 1,
  497. argv4.package.elements = &obj_args,
  498. obj = acpi_evaluate_dsm(ACPI_HANDLE(mac_cb->dev),
  499. &hns_dsaf_acpi_dsm_guid, 0,
  500. HNS_OP_GET_SFP_STAT_FUNC, &argv4);
  501. if (!obj || obj->type != ACPI_TYPE_INTEGER)
  502. return -ENODEV;
  503. *sfp_prsnt = obj->integer.value;
  504. ACPI_FREE(obj);
  505. return 0;
  506. }
  507. /**
  508. * hns_mac_config_sds_loopback - set loop back for serdes
  509. * @mac_cb: mac control block
  510. * retuen 0 == success
  511. */
  512. static int hns_mac_config_sds_loopback(struct hns_mac_cb *mac_cb, bool en)
  513. {
  514. const u8 lane_id[] = {
  515. 0, /* mac 0 -> lane 0 */
  516. 1, /* mac 1 -> lane 1 */
  517. 2, /* mac 2 -> lane 2 */
  518. 3, /* mac 3 -> lane 3 */
  519. 2, /* mac 4 -> lane 2 */
  520. 3, /* mac 5 -> lane 3 */
  521. 0, /* mac 6 -> lane 0 */
  522. 1 /* mac 7 -> lane 1 */
  523. };
  524. #define RX_CSR(lane, reg) ((0x4080 + (reg) * 0x0002 + (lane) * 0x0200) * 2)
  525. u64 reg_offset = RX_CSR(lane_id[mac_cb->mac_id], 0);
  526. int sfp_prsnt = 0;
  527. int ret = hns_mac_get_sfp_prsnt(mac_cb, &sfp_prsnt);
  528. if (!mac_cb->phy_dev) {
  529. if (ret)
  530. pr_info("please confirm sfp is present or not\n");
  531. else
  532. if (!sfp_prsnt)
  533. pr_info("no sfp in this eth\n");
  534. }
  535. if (mac_cb->serdes_ctrl) {
  536. u32 origin = 0;
  537. if (!AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver)) {
  538. #define HILINK_ACCESS_SEL_CFG 0x40008
  539. /* hilink4 & hilink3 use the same xge training and
  540. * xge u adaptor. There is a hilink access sel cfg
  541. * register to select which one to be configed
  542. */
  543. if ((!HNS_DSAF_IS_DEBUG(mac_cb->dsaf_dev)) &&
  544. (mac_cb->mac_id <= 3))
  545. dsaf_write_syscon(mac_cb->serdes_ctrl,
  546. HILINK_ACCESS_SEL_CFG, 0);
  547. else
  548. dsaf_write_syscon(mac_cb->serdes_ctrl,
  549. HILINK_ACCESS_SEL_CFG, 3);
  550. }
  551. ret = dsaf_read_syscon(mac_cb->serdes_ctrl, reg_offset,
  552. &origin);
  553. if (ret)
  554. return ret;
  555. dsaf_set_field(origin, 1ull << 10, 10, en);
  556. dsaf_write_syscon(mac_cb->serdes_ctrl, reg_offset, origin);
  557. } else {
  558. u8 *base_addr = (u8 *)mac_cb->serdes_vaddr +
  559. (mac_cb->mac_id <= 3 ? 0x00280000 : 0x00200000);
  560. dsaf_set_reg_field(base_addr, reg_offset, 1ull << 10, 10, en);
  561. }
  562. return 0;
  563. }
  564. static int
  565. hns_mac_config_sds_loopback_acpi(struct hns_mac_cb *mac_cb, bool en)
  566. {
  567. union acpi_object *obj;
  568. union acpi_object obj_args[3], argv4;
  569. obj_args[0].integer.type = ACPI_TYPE_INTEGER;
  570. obj_args[0].integer.value = mac_cb->mac_id;
  571. obj_args[1].integer.type = ACPI_TYPE_INTEGER;
  572. obj_args[1].integer.value = !!en;
  573. argv4.type = ACPI_TYPE_PACKAGE;
  574. argv4.package.count = 2;
  575. argv4.package.elements = obj_args;
  576. obj = acpi_evaluate_dsm(ACPI_HANDLE(mac_cb->dsaf_dev->dev),
  577. &hns_dsaf_acpi_dsm_guid, 0,
  578. HNS_OP_SERDES_LP_FUNC, &argv4);
  579. if (!obj) {
  580. dev_warn(mac_cb->dsaf_dev->dev, "set port%d serdes lp fail!",
  581. mac_cb->mac_id);
  582. return -ENOTSUPP;
  583. }
  584. ACPI_FREE(obj);
  585. return 0;
  586. }
  587. struct dsaf_misc_op *hns_misc_op_get(struct dsaf_device *dsaf_dev)
  588. {
  589. struct dsaf_misc_op *misc_op;
  590. misc_op = devm_kzalloc(dsaf_dev->dev, sizeof(*misc_op), GFP_KERNEL);
  591. if (!misc_op)
  592. return NULL;
  593. if (dev_of_node(dsaf_dev->dev)) {
  594. misc_op->cpld_set_led = hns_cpld_set_led;
  595. misc_op->cpld_reset_led = cpld_led_reset;
  596. misc_op->cpld_set_led_id = cpld_set_led_id;
  597. misc_op->dsaf_reset = hns_dsaf_rst;
  598. misc_op->xge_srst = hns_dsaf_xge_srst_by_port;
  599. misc_op->ge_srst = hns_dsaf_ge_srst_by_port;
  600. misc_op->ppe_srst = hns_ppe_srst_by_port;
  601. misc_op->ppe_comm_srst = hns_ppe_com_srst;
  602. misc_op->hns_dsaf_srst_chns = hns_dsaf_srst_chns;
  603. misc_op->hns_dsaf_roce_srst = hns_dsaf_roce_srst;
  604. misc_op->get_phy_if = hns_mac_get_phy_if;
  605. misc_op->get_sfp_prsnt = hns_mac_get_sfp_prsnt;
  606. misc_op->cfg_serdes_loopback = hns_mac_config_sds_loopback;
  607. } else if (is_acpi_node(dsaf_dev->dev->fwnode)) {
  608. misc_op->cpld_set_led = hns_cpld_set_led_acpi;
  609. misc_op->cpld_reset_led = cpld_led_reset_acpi;
  610. misc_op->cpld_set_led_id = cpld_set_led_id_acpi;
  611. misc_op->dsaf_reset = hns_dsaf_rst_acpi;
  612. misc_op->xge_srst = hns_dsaf_xge_srst_by_port_acpi;
  613. misc_op->ge_srst = hns_dsaf_ge_srst_by_port_acpi;
  614. misc_op->ppe_srst = hns_ppe_srst_by_port_acpi;
  615. misc_op->ppe_comm_srst = hns_ppe_com_srst;
  616. misc_op->hns_dsaf_srst_chns = hns_dsaf_srst_chns_acpi;
  617. misc_op->hns_dsaf_roce_srst = hns_dsaf_roce_srst_acpi;
  618. misc_op->get_phy_if = hns_mac_get_phy_if_acpi;
  619. misc_op->get_sfp_prsnt = hns_mac_get_sfp_prsnt_acpi;
  620. misc_op->cfg_serdes_loopback = hns_mac_config_sds_loopback_acpi;
  621. } else {
  622. devm_kfree(dsaf_dev->dev, (void *)misc_op);
  623. misc_op = NULL;
  624. }
  625. return (void *)misc_op;
  626. }
  627. static int hns_dsaf_dev_match(struct device *dev, void *fwnode)
  628. {
  629. return dev->fwnode == fwnode;
  630. }
  631. struct
  632. platform_device *hns_dsaf_find_platform_device(struct fwnode_handle *fwnode)
  633. {
  634. struct device *dev;
  635. dev = bus_find_device(&platform_bus_type, NULL,
  636. fwnode, hns_dsaf_dev_match);
  637. return dev ? to_platform_device(dev) : NULL;
  638. }