ixgbe_phy.c 14 KB

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