ixgbe_phy.c 12 KB

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