ixgbe_phy.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux driver
  3. Copyright(c) 1999 - 2007 Intel Corporation.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. Linux NICS <linux.nics@intel.com>
  18. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  19. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  20. *******************************************************************************/
  21. #include <linux/pci.h>
  22. #include <linux/delay.h>
  23. #include <linux/sched.h>
  24. #include "ixgbe_common.h"
  25. #include "ixgbe_phy.h"
  26. static enum ixgbe_phy_type ixgbe_get_phy_type_from_id(u32 phy_id);
  27. static s32 ixgbe_get_phy_id(struct ixgbe_hw *hw);
  28. static bool ixgbe_validate_phy_addr(struct ixgbe_hw *hw, u32 phy_addr);
  29. static s32 ixgbe_write_phy_reg(struct ixgbe_hw *hw, u32 reg_addr,
  30. u32 device_type, u16 phy_data);
  31. /**
  32. * ixgbe_identify_phy - Get physical layer module
  33. * @hw: pointer to hardware structure
  34. *
  35. * Determines the physical layer module found on the current adapter.
  36. **/
  37. s32 ixgbe_identify_phy(struct ixgbe_hw *hw)
  38. {
  39. s32 status = IXGBE_ERR_PHY_ADDR_INVALID;
  40. u32 phy_addr;
  41. for (phy_addr = 0; phy_addr < IXGBE_MAX_PHY_ADDR; phy_addr++) {
  42. if (ixgbe_validate_phy_addr(hw, phy_addr)) {
  43. hw->phy.addr = phy_addr;
  44. ixgbe_get_phy_id(hw);
  45. hw->phy.type = ixgbe_get_phy_type_from_id(hw->phy.id);
  46. status = 0;
  47. break;
  48. }
  49. }
  50. return status;
  51. }
  52. /**
  53. * ixgbe_validate_phy_addr - Determines phy address is valid
  54. * @hw: pointer to hardware structure
  55. *
  56. **/
  57. static bool ixgbe_validate_phy_addr(struct ixgbe_hw *hw, u32 phy_addr)
  58. {
  59. u16 phy_id = 0;
  60. bool valid = false;
  61. hw->phy.addr = phy_addr;
  62. ixgbe_read_phy_reg(hw,
  63. IXGBE_MDIO_PHY_ID_HIGH,
  64. IXGBE_MDIO_PMA_PMD_DEV_TYPE,
  65. &phy_id);
  66. if (phy_id != 0xFFFF && phy_id != 0x0)
  67. valid = true;
  68. return valid;
  69. }
  70. /**
  71. * ixgbe_get_phy_id - Get the phy type
  72. * @hw: pointer to hardware structure
  73. *
  74. **/
  75. static s32 ixgbe_get_phy_id(struct ixgbe_hw *hw)
  76. {
  77. u32 status;
  78. u16 phy_id_high = 0;
  79. u16 phy_id_low = 0;
  80. status = ixgbe_read_phy_reg(hw,
  81. IXGBE_MDIO_PHY_ID_HIGH,
  82. IXGBE_MDIO_PMA_PMD_DEV_TYPE,
  83. &phy_id_high);
  84. if (status == 0) {
  85. hw->phy.id = (u32)(phy_id_high << 16);
  86. status = ixgbe_read_phy_reg(hw,
  87. IXGBE_MDIO_PHY_ID_LOW,
  88. IXGBE_MDIO_PMA_PMD_DEV_TYPE,
  89. &phy_id_low);
  90. hw->phy.id |= (u32)(phy_id_low & IXGBE_PHY_REVISION_MASK);
  91. hw->phy.revision = (u32)(phy_id_low & ~IXGBE_PHY_REVISION_MASK);
  92. }
  93. return status;
  94. }
  95. /**
  96. * ixgbe_get_phy_type_from_id - Get the phy type
  97. * @hw: pointer to hardware structure
  98. *
  99. **/
  100. static enum ixgbe_phy_type ixgbe_get_phy_type_from_id(u32 phy_id)
  101. {
  102. enum ixgbe_phy_type phy_type;
  103. switch (phy_id) {
  104. case TN1010_PHY_ID:
  105. phy_type = ixgbe_phy_tn;
  106. break;
  107. case QT2022_PHY_ID:
  108. phy_type = ixgbe_phy_qt;
  109. break;
  110. default:
  111. phy_type = ixgbe_phy_unknown;
  112. break;
  113. }
  114. return phy_type;
  115. }
  116. /**
  117. * ixgbe_reset_phy - Performs a PHY reset
  118. * @hw: pointer to hardware structure
  119. **/
  120. s32 ixgbe_reset_phy(struct ixgbe_hw *hw)
  121. {
  122. /*
  123. * Perform soft PHY reset to the PHY_XS.
  124. * This will cause a soft reset to the PHY
  125. */
  126. return ixgbe_write_phy_reg(hw, IXGBE_MDIO_PHY_XS_CONTROL,
  127. IXGBE_MDIO_PHY_XS_DEV_TYPE,
  128. IXGBE_MDIO_PHY_XS_RESET);
  129. }
  130. /**
  131. * ixgbe_read_phy_reg - Reads a value from a specified PHY register
  132. * @hw: pointer to hardware structure
  133. * @reg_addr: 32 bit address of PHY register to read
  134. * @phy_data: Pointer to read data from PHY register
  135. **/
  136. s32 ixgbe_read_phy_reg(struct ixgbe_hw *hw, u32 reg_addr,
  137. u32 device_type, u16 *phy_data)
  138. {
  139. u32 command;
  140. u32 i;
  141. u32 timeout = 10;
  142. u32 data;
  143. s32 status = 0;
  144. u16 gssr;
  145. if (IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1)
  146. gssr = IXGBE_GSSR_PHY1_SM;
  147. else
  148. gssr = IXGBE_GSSR_PHY0_SM;
  149. if (ixgbe_acquire_swfw_sync(hw, gssr) != 0)
  150. status = IXGBE_ERR_SWFW_SYNC;
  151. if (status == 0) {
  152. /* Setup and write the address cycle command */
  153. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  154. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  155. (hw->phy.addr << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  156. (IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND));
  157. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  158. /*
  159. * Check every 10 usec to see if the address cycle completed.
  160. * The MDI Command bit will clear when the operation is
  161. * complete
  162. */
  163. for (i = 0; i < timeout; i++) {
  164. udelay(10);
  165. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  166. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  167. break;
  168. }
  169. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  170. hw_dbg(hw, "PHY address command did not complete.\n");
  171. status = IXGBE_ERR_PHY;
  172. }
  173. if (status == 0) {
  174. /*
  175. * Address cycle complete, setup and write the read
  176. * command
  177. */
  178. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  179. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  180. (hw->phy.addr << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  181. (IXGBE_MSCA_READ | IXGBE_MSCA_MDI_COMMAND));
  182. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  183. /*
  184. * Check every 10 usec to see if the address cycle
  185. * completed. The MDI Command bit will clear when the
  186. * operation is complete
  187. */
  188. for (i = 0; i < timeout; i++) {
  189. udelay(10);
  190. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  191. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  192. break;
  193. }
  194. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  195. hw_dbg(hw,
  196. "PHY read command didn't complete\n");
  197. status = IXGBE_ERR_PHY;
  198. } else {
  199. /*
  200. * Read operation is complete. Get the data
  201. * from MSRWD
  202. */
  203. data = IXGBE_READ_REG(hw, IXGBE_MSRWD);
  204. data >>= IXGBE_MSRWD_READ_DATA_SHIFT;
  205. *phy_data = (u16)(data);
  206. }
  207. }
  208. ixgbe_release_swfw_sync(hw, gssr);
  209. }
  210. return status;
  211. }
  212. /**
  213. * ixgbe_write_phy_reg - Writes a value to specified PHY register
  214. * @hw: pointer to hardware structure
  215. * @reg_addr: 32 bit PHY register to write
  216. * @device_type: 5 bit device type
  217. * @phy_data: Data to write to the PHY register
  218. **/
  219. static s32 ixgbe_write_phy_reg(struct ixgbe_hw *hw, u32 reg_addr,
  220. u32 device_type, u16 phy_data)
  221. {
  222. u32 command;
  223. u32 i;
  224. u32 timeout = 10;
  225. s32 status = 0;
  226. u16 gssr;
  227. if (IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1)
  228. gssr = IXGBE_GSSR_PHY1_SM;
  229. else
  230. gssr = IXGBE_GSSR_PHY0_SM;
  231. if (ixgbe_acquire_swfw_sync(hw, gssr) != 0)
  232. status = IXGBE_ERR_SWFW_SYNC;
  233. if (status == 0) {
  234. /* Put the data in the MDI single read and write data register*/
  235. IXGBE_WRITE_REG(hw, IXGBE_MSRWD, (u32)phy_data);
  236. /* Setup and write the address cycle command */
  237. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  238. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  239. (hw->phy.addr << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  240. (IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND));
  241. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  242. /*
  243. * Check every 10 usec to see if the address cycle completed.
  244. * The MDI Command bit will clear when the operation is
  245. * complete
  246. */
  247. for (i = 0; i < timeout; i++) {
  248. udelay(10);
  249. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  250. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0) {
  251. hw_dbg(hw, "PHY address cmd didn't complete\n");
  252. break;
  253. }
  254. }
  255. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0)
  256. status = IXGBE_ERR_PHY;
  257. if (status == 0) {
  258. /*
  259. * Address cycle complete, setup and write the write
  260. * command
  261. */
  262. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  263. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  264. (hw->phy.addr << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  265. (IXGBE_MSCA_WRITE | IXGBE_MSCA_MDI_COMMAND));
  266. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  267. /*
  268. * Check every 10 usec to see if the address cycle
  269. * completed. The MDI Command bit will clear when the
  270. * operation is complete
  271. */
  272. for (i = 0; i < timeout; i++) {
  273. udelay(10);
  274. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  275. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0) {
  276. hw_dbg(hw, "PHY write command did not "
  277. "complete.\n");
  278. break;
  279. }
  280. }
  281. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0)
  282. status = IXGBE_ERR_PHY;
  283. }
  284. ixgbe_release_swfw_sync(hw, gssr);
  285. }
  286. return status;
  287. }
  288. /**
  289. * ixgbe_setup_tnx_phy_link - Set and restart autoneg
  290. * @hw: pointer to hardware structure
  291. *
  292. * Restart autonegotiation and PHY and waits for completion.
  293. **/
  294. s32 ixgbe_setup_tnx_phy_link(struct ixgbe_hw *hw)
  295. {
  296. s32 status = IXGBE_NOT_IMPLEMENTED;
  297. u32 time_out;
  298. u32 max_time_out = 10;
  299. u16 autoneg_speed_selection_register = 0x10;
  300. u16 autoneg_restart_mask = 0x0200;
  301. u16 autoneg_complete_mask = 0x0020;
  302. u16 autoneg_reg = 0;
  303. /*
  304. * Set advertisement settings in PHY based on autoneg_advertised
  305. * settings. If autoneg_advertised = 0, then advertise default values
  306. * txn devices cannot be "forced" to a autoneg 10G and fail. But can
  307. * for a 1G.
  308. */
  309. ixgbe_read_phy_reg(hw,
  310. autoneg_speed_selection_register,
  311. IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
  312. &autoneg_reg);
  313. if (hw->phy.autoneg_advertised == IXGBE_LINK_SPEED_1GB_FULL)
  314. autoneg_reg &= 0xEFFF; /* 0 in bit 12 is 1G operation */
  315. else
  316. autoneg_reg |= 0x1000; /* 1 in bit 12 is 10G/1G operation */
  317. ixgbe_write_phy_reg(hw,
  318. autoneg_speed_selection_register,
  319. IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
  320. autoneg_reg);
  321. /* Restart PHY autonegotiation and wait for completion */
  322. ixgbe_read_phy_reg(hw,
  323. IXGBE_MDIO_AUTO_NEG_CONTROL,
  324. IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
  325. &autoneg_reg);
  326. autoneg_reg |= autoneg_restart_mask;
  327. ixgbe_write_phy_reg(hw,
  328. IXGBE_MDIO_AUTO_NEG_CONTROL,
  329. IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
  330. autoneg_reg);
  331. /* Wait for autonegotiation to finish */
  332. for (time_out = 0; time_out < max_time_out; time_out++) {
  333. udelay(10);
  334. /* Restart PHY autonegotiation and wait for completion */
  335. status = ixgbe_read_phy_reg(hw,
  336. IXGBE_MDIO_AUTO_NEG_STATUS,
  337. IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
  338. &autoneg_reg);
  339. autoneg_reg &= autoneg_complete_mask;
  340. if (autoneg_reg == autoneg_complete_mask) {
  341. status = 0;
  342. break;
  343. }
  344. }
  345. if (time_out == max_time_out)
  346. status = IXGBE_ERR_LINK_SETUP;
  347. return status;
  348. }
  349. /**
  350. * ixgbe_check_tnx_phy_link - Determine link and speed status
  351. * @hw: pointer to hardware structure
  352. *
  353. * Reads the VS1 register to determine if link is up and the current speed for
  354. * the PHY.
  355. **/
  356. s32 ixgbe_check_tnx_phy_link(struct ixgbe_hw *hw, u32 *speed,
  357. bool *link_up)
  358. {
  359. s32 status = 0;
  360. u32 time_out;
  361. u32 max_time_out = 10;
  362. u16 phy_link = 0;
  363. u16 phy_speed = 0;
  364. u16 phy_data = 0;
  365. /* Initialize speed and link to default case */
  366. *link_up = false;
  367. *speed = IXGBE_LINK_SPEED_10GB_FULL;
  368. /*
  369. * Check current speed and link status of the PHY register.
  370. * This is a vendor specific register and may have to
  371. * be changed for other copper PHYs.
  372. */
  373. for (time_out = 0; time_out < max_time_out; time_out++) {
  374. udelay(10);
  375. if (phy_link == IXGBE_MDIO_VENDOR_SPECIFIC_1_LINK_STATUS) {
  376. *link_up = true;
  377. if (phy_speed ==
  378. IXGBE_MDIO_VENDOR_SPECIFIC_1_SPEED_STATUS)
  379. *speed = IXGBE_LINK_SPEED_1GB_FULL;
  380. break;
  381. } else {
  382. status = ixgbe_read_phy_reg(hw,
  383. IXGBE_MDIO_VENDOR_SPECIFIC_1_STATUS,
  384. IXGBE_MDIO_VENDOR_SPECIFIC_1_DEV_TYPE,
  385. &phy_data);
  386. phy_link = phy_data &
  387. IXGBE_MDIO_VENDOR_SPECIFIC_1_LINK_STATUS;
  388. phy_speed = phy_data &
  389. IXGBE_MDIO_VENDOR_SPECIFIC_1_SPEED_STATUS;
  390. }
  391. }
  392. return status;
  393. }
  394. /**
  395. * ixgbe_setup_tnx_phy_link_speed - Sets the auto advertised capabilities
  396. * @hw: pointer to hardware structure
  397. * @speed: new link speed
  398. * @autoneg: true if autonegotiation enabled
  399. **/
  400. s32 ixgbe_setup_tnx_phy_link_speed(struct ixgbe_hw *hw, u32 speed,
  401. bool autoneg,
  402. bool autoneg_wait_to_complete)
  403. {
  404. /*
  405. * Clear autoneg_advertised and set new values based on input link
  406. * speed.
  407. */
  408. hw->phy.autoneg_advertised = 0;
  409. if (speed & IXGBE_LINK_SPEED_10GB_FULL)
  410. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10GB_FULL;
  411. if (speed & IXGBE_LINK_SPEED_1GB_FULL)
  412. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_1GB_FULL;
  413. /* Setup link based on the new speed settings */
  414. ixgbe_setup_tnx_phy_link(hw);
  415. return 0;
  416. }