ixgbe_phy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 QT2022_PHY_ID:
  103. phy_type = ixgbe_phy_qt;
  104. break;
  105. default:
  106. phy_type = ixgbe_phy_unknown;
  107. break;
  108. }
  109. return phy_type;
  110. }
  111. /**
  112. * ixgbe_reset_phy_generic - Performs a PHY reset
  113. * @hw: pointer to hardware structure
  114. **/
  115. s32 ixgbe_reset_phy_generic(struct ixgbe_hw *hw)
  116. {
  117. /*
  118. * Perform soft PHY reset to the PHY_XS.
  119. * This will cause a soft reset to the PHY
  120. */
  121. return hw->phy.ops.write_reg(hw, IXGBE_MDIO_PHY_XS_CONTROL,
  122. IXGBE_MDIO_PHY_XS_DEV_TYPE,
  123. IXGBE_MDIO_PHY_XS_RESET);
  124. }
  125. /**
  126. * ixgbe_read_phy_reg_generic - Reads a value from a specified PHY register
  127. * @hw: pointer to hardware structure
  128. * @reg_addr: 32 bit address of PHY register to read
  129. * @phy_data: Pointer to read data from PHY register
  130. **/
  131. s32 ixgbe_read_phy_reg_generic(struct ixgbe_hw *hw, u32 reg_addr,
  132. u32 device_type, u16 *phy_data)
  133. {
  134. u32 command;
  135. u32 i;
  136. u32 data;
  137. s32 status = 0;
  138. u16 gssr;
  139. if (IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1)
  140. gssr = IXGBE_GSSR_PHY1_SM;
  141. else
  142. gssr = IXGBE_GSSR_PHY0_SM;
  143. if (ixgbe_acquire_swfw_sync(hw, gssr) != 0)
  144. status = IXGBE_ERR_SWFW_SYNC;
  145. if (status == 0) {
  146. /* Setup and write the address cycle command */
  147. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  148. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  149. (hw->phy.addr << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  150. (IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND));
  151. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  152. /*
  153. * Check every 10 usec to see if the address cycle completed.
  154. * The MDI Command bit will clear when the operation is
  155. * complete
  156. */
  157. for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
  158. udelay(10);
  159. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  160. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  161. break;
  162. }
  163. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  164. hw_dbg(hw, "PHY address command did not complete.\n");
  165. status = IXGBE_ERR_PHY;
  166. }
  167. if (status == 0) {
  168. /*
  169. * Address cycle complete, setup and write the read
  170. * command
  171. */
  172. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  173. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  174. (hw->phy.addr << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  175. (IXGBE_MSCA_READ | IXGBE_MSCA_MDI_COMMAND));
  176. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  177. /*
  178. * Check every 10 usec to see if the address cycle
  179. * completed. The MDI Command bit will clear when the
  180. * operation is complete
  181. */
  182. for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
  183. udelay(10);
  184. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  185. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  186. break;
  187. }
  188. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  189. hw_dbg(hw, "PHY read command didn't complete\n");
  190. status = IXGBE_ERR_PHY;
  191. } else {
  192. /*
  193. * Read operation is complete. Get the data
  194. * from MSRWD
  195. */
  196. data = IXGBE_READ_REG(hw, IXGBE_MSRWD);
  197. data >>= IXGBE_MSRWD_READ_DATA_SHIFT;
  198. *phy_data = (u16)(data);
  199. }
  200. }
  201. ixgbe_release_swfw_sync(hw, gssr);
  202. }
  203. return status;
  204. }
  205. /**
  206. * ixgbe_write_phy_reg_generic - Writes a value to specified PHY register
  207. * @hw: pointer to hardware structure
  208. * @reg_addr: 32 bit PHY register to write
  209. * @device_type: 5 bit device type
  210. * @phy_data: Data to write to the PHY register
  211. **/
  212. s32 ixgbe_write_phy_reg_generic(struct ixgbe_hw *hw, u32 reg_addr,
  213. u32 device_type, u16 phy_data)
  214. {
  215. u32 command;
  216. u32 i;
  217. s32 status = 0;
  218. u16 gssr;
  219. if (IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1)
  220. gssr = IXGBE_GSSR_PHY1_SM;
  221. else
  222. gssr = IXGBE_GSSR_PHY0_SM;
  223. if (ixgbe_acquire_swfw_sync(hw, gssr) != 0)
  224. status = IXGBE_ERR_SWFW_SYNC;
  225. if (status == 0) {
  226. /* Put the data in the MDI single read and write data register*/
  227. IXGBE_WRITE_REG(hw, IXGBE_MSRWD, (u32)phy_data);
  228. /* Setup and write the address cycle command */
  229. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  230. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  231. (hw->phy.addr << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  232. (IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND));
  233. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  234. /*
  235. * Check every 10 usec to see if the address cycle completed.
  236. * The MDI Command bit will clear when the operation is
  237. * complete
  238. */
  239. for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
  240. udelay(10);
  241. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  242. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  243. break;
  244. }
  245. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  246. hw_dbg(hw, "PHY address cmd didn't complete\n");
  247. status = IXGBE_ERR_PHY;
  248. }
  249. if (status == 0) {
  250. /*
  251. * Address cycle complete, setup and write the write
  252. * command
  253. */
  254. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  255. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  256. (hw->phy.addr << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  257. (IXGBE_MSCA_WRITE | IXGBE_MSCA_MDI_COMMAND));
  258. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  259. /*
  260. * Check every 10 usec to see if the address cycle
  261. * completed. The MDI Command bit will clear when the
  262. * operation is complete
  263. */
  264. for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
  265. udelay(10);
  266. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  267. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  268. break;
  269. }
  270. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  271. hw_dbg(hw, "PHY address cmd didn't complete\n");
  272. status = IXGBE_ERR_PHY;
  273. }
  274. }
  275. ixgbe_release_swfw_sync(hw, gssr);
  276. }
  277. return status;
  278. }
  279. /**
  280. * ixgbe_setup_phy_link_generic - Set and restart autoneg
  281. * @hw: pointer to hardware structure
  282. *
  283. * Restart autonegotiation and PHY and waits for completion.
  284. **/
  285. s32 ixgbe_setup_phy_link_generic(struct ixgbe_hw *hw)
  286. {
  287. s32 status = IXGBE_NOT_IMPLEMENTED;
  288. u32 time_out;
  289. u32 max_time_out = 10;
  290. u16 autoneg_reg = IXGBE_MII_AUTONEG_REG;
  291. /*
  292. * Set advertisement settings in PHY based on autoneg_advertised
  293. * settings. If autoneg_advertised = 0, then advertise default values
  294. * tnx devices cannot be "forced" to a autoneg 10G and fail. But can
  295. * for a 1G.
  296. */
  297. hw->phy.ops.read_reg(hw, IXGBE_MII_SPEED_SELECTION_REG,
  298. IXGBE_MDIO_AUTO_NEG_DEV_TYPE, &autoneg_reg);
  299. if (hw->phy.autoneg_advertised == IXGBE_LINK_SPEED_1GB_FULL)
  300. autoneg_reg &= 0xEFFF; /* 0 in bit 12 is 1G operation */
  301. else
  302. autoneg_reg |= 0x1000; /* 1 in bit 12 is 10G/1G operation */
  303. hw->phy.ops.write_reg(hw, IXGBE_MII_SPEED_SELECTION_REG,
  304. IXGBE_MDIO_AUTO_NEG_DEV_TYPE, autoneg_reg);
  305. /* Restart PHY autonegotiation and wait for completion */
  306. hw->phy.ops.read_reg(hw, IXGBE_MDIO_AUTO_NEG_CONTROL,
  307. IXGBE_MDIO_AUTO_NEG_DEV_TYPE, &autoneg_reg);
  308. autoneg_reg |= IXGBE_MII_RESTART;
  309. hw->phy.ops.write_reg(hw, IXGBE_MDIO_AUTO_NEG_CONTROL,
  310. IXGBE_MDIO_AUTO_NEG_DEV_TYPE, autoneg_reg);
  311. /* Wait for autonegotiation to finish */
  312. for (time_out = 0; time_out < max_time_out; time_out++) {
  313. udelay(10);
  314. /* Restart PHY autonegotiation and wait for completion */
  315. status = hw->phy.ops.read_reg(hw, IXGBE_MDIO_AUTO_NEG_STATUS,
  316. IXGBE_MDIO_AUTO_NEG_DEV_TYPE,
  317. &autoneg_reg);
  318. autoneg_reg &= IXGBE_MII_AUTONEG_COMPLETE;
  319. if (autoneg_reg == IXGBE_MII_AUTONEG_COMPLETE) {
  320. status = 0;
  321. break;
  322. }
  323. }
  324. if (time_out == max_time_out)
  325. status = IXGBE_ERR_LINK_SETUP;
  326. return status;
  327. }
  328. /**
  329. * ixgbe_setup_phy_link_speed_generic - Sets the auto advertised capabilities
  330. * @hw: pointer to hardware structure
  331. * @speed: new link speed
  332. * @autoneg: true if autonegotiation enabled
  333. **/
  334. s32 ixgbe_setup_phy_link_speed_generic(struct ixgbe_hw *hw,
  335. ixgbe_link_speed speed,
  336. bool autoneg,
  337. bool autoneg_wait_to_complete)
  338. {
  339. /*
  340. * Clear autoneg_advertised and set new values based on input link
  341. * speed.
  342. */
  343. hw->phy.autoneg_advertised = 0;
  344. if (speed & IXGBE_LINK_SPEED_10GB_FULL)
  345. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10GB_FULL;
  346. if (speed & IXGBE_LINK_SPEED_1GB_FULL)
  347. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_1GB_FULL;
  348. /* Setup link based on the new speed settings */
  349. hw->phy.ops.setup_link(hw);
  350. return 0;
  351. }