pch_gbe_phy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (C) 1999 - 2010 Intel Corporation.
  3. * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD.
  4. *
  5. * This code was derived from the Intel e1000e Linux driver.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include "pch_gbe.h"
  21. #include "pch_gbe_phy.h"
  22. #define PHY_MAX_REG_ADDRESS 0x1F /* 5 bit address bus (0-0x1F) */
  23. /* PHY 1000 MII Register/Bit Definitions */
  24. /* PHY Registers defined by IEEE */
  25. #define PHY_CONTROL 0x00 /* Control Register */
  26. #define PHY_STATUS 0x01 /* Status Regiser */
  27. #define PHY_ID1 0x02 /* Phy Id Register (word 1) */
  28. #define PHY_ID2 0x03 /* Phy Id Register (word 2) */
  29. #define PHY_AUTONEG_ADV 0x04 /* Autoneg Advertisement */
  30. #define PHY_LP_ABILITY 0x05 /* Link Partner Ability (Base Page) */
  31. #define PHY_AUTONEG_EXP 0x06 /* Autoneg Expansion Register */
  32. #define PHY_NEXT_PAGE_TX 0x07 /* Next Page TX */
  33. #define PHY_LP_NEXT_PAGE 0x08 /* Link Partner Next Page */
  34. #define PHY_1000T_CTRL 0x09 /* 1000Base-T Control Register */
  35. #define PHY_1000T_STATUS 0x0A /* 1000Base-T Status Register */
  36. #define PHY_EXT_STATUS 0x0F /* Extended Status Register */
  37. #define PHY_PHYSP_CONTROL 0x10 /* PHY Specific Control Register */
  38. #define PHY_EXT_PHYSP_CONTROL 0x14 /* Extended PHY Specific Control Register */
  39. #define PHY_LED_CONTROL 0x18 /* LED Control Register */
  40. #define PHY_EXT_PHYSP_STATUS 0x1B /* Extended PHY Specific Status Register */
  41. /* PHY Control Register */
  42. #define MII_CR_SPEED_SELECT_MSB 0x0040 /* bits 6,13: 10=1000, 01=100, 00=10 */
  43. #define MII_CR_COLL_TEST_ENABLE 0x0080 /* Collision test enable */
  44. #define MII_CR_FULL_DUPLEX 0x0100 /* FDX =1, half duplex =0 */
  45. #define MII_CR_RESTART_AUTO_NEG 0x0200 /* Restart auto negotiation */
  46. #define MII_CR_ISOLATE 0x0400 /* Isolate PHY from MII */
  47. #define MII_CR_POWER_DOWN 0x0800 /* Power down */
  48. #define MII_CR_AUTO_NEG_EN 0x1000 /* Auto Neg Enable */
  49. #define MII_CR_SPEED_SELECT_LSB 0x2000 /* bits 6,13: 10=1000, 01=100, 00=10 */
  50. #define MII_CR_LOOPBACK 0x4000 /* 0 = normal, 1 = loopback */
  51. #define MII_CR_RESET 0x8000 /* 0 = normal, 1 = PHY reset */
  52. #define MII_CR_SPEED_1000 0x0040
  53. #define MII_CR_SPEED_100 0x2000
  54. #define MII_CR_SPEED_10 0x0000
  55. /* PHY Status Register */
  56. #define MII_SR_EXTENDED_CAPS 0x0001 /* Extended register capabilities */
  57. #define MII_SR_JABBER_DETECT 0x0002 /* Jabber Detected */
  58. #define MII_SR_LINK_STATUS 0x0004 /* Link Status 1 = link */
  59. #define MII_SR_AUTONEG_CAPS 0x0008 /* Auto Neg Capable */
  60. #define MII_SR_REMOTE_FAULT 0x0010 /* Remote Fault Detect */
  61. #define MII_SR_AUTONEG_COMPLETE 0x0020 /* Auto Neg Complete */
  62. #define MII_SR_PREAMBLE_SUPPRESS 0x0040 /* Preamble may be suppressed */
  63. #define MII_SR_EXTENDED_STATUS 0x0100 /* Ext. status info in Reg 0x0F */
  64. #define MII_SR_100T2_HD_CAPS 0x0200 /* 100T2 Half Duplex Capable */
  65. #define MII_SR_100T2_FD_CAPS 0x0400 /* 100T2 Full Duplex Capable */
  66. #define MII_SR_10T_HD_CAPS 0x0800 /* 10T Half Duplex Capable */
  67. #define MII_SR_10T_FD_CAPS 0x1000 /* 10T Full Duplex Capable */
  68. #define MII_SR_100X_HD_CAPS 0x2000 /* 100X Half Duplex Capable */
  69. #define MII_SR_100X_FD_CAPS 0x4000 /* 100X Full Duplex Capable */
  70. #define MII_SR_100T4_CAPS 0x8000 /* 100T4 Capable */
  71. /* AR8031 PHY Debug Registers */
  72. #define PHY_AR803X_ID 0x00001374
  73. #define PHY_AR8031_DBG_OFF 0x1D
  74. #define PHY_AR8031_DBG_DAT 0x1E
  75. #define PHY_AR8031_SERDES 0x05
  76. #define PHY_AR8031_HIBERNATE 0x0B
  77. #define PHY_AR8031_SERDES_TX_CLK_DLY 0x0100 /* TX clock delay of 2.0ns */
  78. #define PHY_AR8031_PS_HIB_EN 0x8000 /* Hibernate enable */
  79. /* Phy Id Register (word 2) */
  80. #define PHY_REVISION_MASK 0x000F
  81. /* PHY Specific Control Register */
  82. #define PHYSP_CTRL_ASSERT_CRS_TX 0x0800
  83. /* Default value of PHY register */
  84. #define PHY_CONTROL_DEFAULT 0x1140 /* Control Register */
  85. #define PHY_AUTONEG_ADV_DEFAULT 0x01e0 /* Autoneg Advertisement */
  86. #define PHY_NEXT_PAGE_TX_DEFAULT 0x2001 /* Next Page TX */
  87. #define PHY_1000T_CTRL_DEFAULT 0x0300 /* 1000Base-T Control Register */
  88. #define PHY_PHYSP_CONTROL_DEFAULT 0x01EE /* PHY Specific Control Register */
  89. /**
  90. * pch_gbe_phy_get_id - Retrieve the PHY ID and revision
  91. * @hw: Pointer to the HW structure
  92. * Returns
  93. * 0: Successful.
  94. * Negative value: Failed.
  95. */
  96. s32 pch_gbe_phy_get_id(struct pch_gbe_hw *hw)
  97. {
  98. struct pch_gbe_adapter *adapter = pch_gbe_hw_to_adapter(hw);
  99. struct pch_gbe_phy_info *phy = &hw->phy;
  100. s32 ret;
  101. u16 phy_id1;
  102. u16 phy_id2;
  103. ret = pch_gbe_phy_read_reg_miic(hw, PHY_ID1, &phy_id1);
  104. if (ret)
  105. return ret;
  106. ret = pch_gbe_phy_read_reg_miic(hw, PHY_ID2, &phy_id2);
  107. if (ret)
  108. return ret;
  109. /*
  110. * PHY_ID1: [bit15-0:ID(21-6)]
  111. * PHY_ID2: [bit15-10:ID(5-0)][bit9-4:Model][bit3-0:revision]
  112. */
  113. phy->id = (u32)phy_id1;
  114. phy->id = ((phy->id << 6) | ((phy_id2 & 0xFC00) >> 10));
  115. phy->revision = (u32) (phy_id2 & 0x000F);
  116. netdev_dbg(adapter->netdev,
  117. "phy->id : 0x%08x phy->revision : 0x%08x\n",
  118. phy->id, phy->revision);
  119. return 0;
  120. }
  121. /**
  122. * pch_gbe_phy_read_reg_miic - Read MII control register
  123. * @hw: Pointer to the HW structure
  124. * @offset: Register offset to be read
  125. * @data: Pointer to the read data
  126. * Returns
  127. * 0: Successful.
  128. * -EINVAL: Invalid argument.
  129. */
  130. s32 pch_gbe_phy_read_reg_miic(struct pch_gbe_hw *hw, u32 offset, u16 *data)
  131. {
  132. struct pch_gbe_phy_info *phy = &hw->phy;
  133. if (offset > PHY_MAX_REG_ADDRESS) {
  134. struct pch_gbe_adapter *adapter = pch_gbe_hw_to_adapter(hw);
  135. netdev_err(adapter->netdev, "PHY Address %d is out of range\n",
  136. offset);
  137. return -EINVAL;
  138. }
  139. *data = pch_gbe_mac_ctrl_miim(hw, phy->addr, PCH_GBE_HAL_MIIM_READ,
  140. offset, (u16)0);
  141. return 0;
  142. }
  143. /**
  144. * pch_gbe_phy_write_reg_miic - Write MII control register
  145. * @hw: Pointer to the HW structure
  146. * @offset: Register offset to be read
  147. * @data: data to write to register at offset
  148. * Returns
  149. * 0: Successful.
  150. * -EINVAL: Invalid argument.
  151. */
  152. s32 pch_gbe_phy_write_reg_miic(struct pch_gbe_hw *hw, u32 offset, u16 data)
  153. {
  154. struct pch_gbe_phy_info *phy = &hw->phy;
  155. if (offset > PHY_MAX_REG_ADDRESS) {
  156. struct pch_gbe_adapter *adapter = pch_gbe_hw_to_adapter(hw);
  157. netdev_err(adapter->netdev, "PHY Address %d is out of range\n",
  158. offset);
  159. return -EINVAL;
  160. }
  161. pch_gbe_mac_ctrl_miim(hw, phy->addr, PCH_GBE_HAL_MIIM_WRITE,
  162. offset, data);
  163. return 0;
  164. }
  165. /**
  166. * pch_gbe_phy_sw_reset - PHY software reset
  167. * @hw: Pointer to the HW structure
  168. */
  169. void pch_gbe_phy_sw_reset(struct pch_gbe_hw *hw)
  170. {
  171. u16 phy_ctrl;
  172. pch_gbe_phy_read_reg_miic(hw, PHY_CONTROL, &phy_ctrl);
  173. phy_ctrl |= MII_CR_RESET;
  174. pch_gbe_phy_write_reg_miic(hw, PHY_CONTROL, phy_ctrl);
  175. udelay(1);
  176. }
  177. /**
  178. * pch_gbe_phy_hw_reset - PHY hardware reset
  179. * @hw: Pointer to the HW structure
  180. */
  181. void pch_gbe_phy_hw_reset(struct pch_gbe_hw *hw)
  182. {
  183. pch_gbe_phy_write_reg_miic(hw, PHY_CONTROL, PHY_CONTROL_DEFAULT);
  184. pch_gbe_phy_write_reg_miic(hw, PHY_AUTONEG_ADV,
  185. PHY_AUTONEG_ADV_DEFAULT);
  186. pch_gbe_phy_write_reg_miic(hw, PHY_NEXT_PAGE_TX,
  187. PHY_NEXT_PAGE_TX_DEFAULT);
  188. pch_gbe_phy_write_reg_miic(hw, PHY_1000T_CTRL, PHY_1000T_CTRL_DEFAULT);
  189. pch_gbe_phy_write_reg_miic(hw, PHY_PHYSP_CONTROL,
  190. PHY_PHYSP_CONTROL_DEFAULT);
  191. }
  192. /**
  193. * pch_gbe_phy_power_up - restore link in case the phy was powered down
  194. * @hw: Pointer to the HW structure
  195. */
  196. void pch_gbe_phy_power_up(struct pch_gbe_hw *hw)
  197. {
  198. u16 mii_reg;
  199. mii_reg = 0;
  200. /* Just clear the power down bit to wake the phy back up */
  201. /* according to the manual, the phy will retain its
  202. * settings across a power-down/up cycle */
  203. pch_gbe_phy_read_reg_miic(hw, PHY_CONTROL, &mii_reg);
  204. mii_reg &= ~MII_CR_POWER_DOWN;
  205. pch_gbe_phy_write_reg_miic(hw, PHY_CONTROL, mii_reg);
  206. }
  207. /**
  208. * pch_gbe_phy_power_down - Power down PHY
  209. * @hw: Pointer to the HW structure
  210. */
  211. void pch_gbe_phy_power_down(struct pch_gbe_hw *hw)
  212. {
  213. u16 mii_reg;
  214. mii_reg = 0;
  215. /* Power down the PHY so no link is implied when interface is down *
  216. * The PHY cannot be powered down if any of the following is TRUE *
  217. * (a) WoL is enabled
  218. * (b) AMT is active
  219. */
  220. pch_gbe_phy_read_reg_miic(hw, PHY_CONTROL, &mii_reg);
  221. mii_reg |= MII_CR_POWER_DOWN;
  222. pch_gbe_phy_write_reg_miic(hw, PHY_CONTROL, mii_reg);
  223. mdelay(1);
  224. }
  225. /**
  226. * pch_gbe_phy_set_rgmii - RGMII interface setting
  227. * @hw: Pointer to the HW structure
  228. */
  229. void pch_gbe_phy_set_rgmii(struct pch_gbe_hw *hw)
  230. {
  231. pch_gbe_phy_sw_reset(hw);
  232. }
  233. /**
  234. * pch_gbe_phy_tx_clk_delay - Setup TX clock delay via the PHY
  235. * @hw: Pointer to the HW structure
  236. * Returns
  237. * 0: Successful.
  238. * -EINVAL: Invalid argument.
  239. */
  240. static int pch_gbe_phy_tx_clk_delay(struct pch_gbe_hw *hw)
  241. {
  242. /* The RGMII interface requires a ~2ns TX clock delay. This is typically
  243. * done in layout with a longer trace or via PHY strapping, but can also
  244. * be done via PHY configuration registers.
  245. */
  246. struct pch_gbe_adapter *adapter = pch_gbe_hw_to_adapter(hw);
  247. u16 mii_reg;
  248. int ret = 0;
  249. switch (hw->phy.id) {
  250. case PHY_AR803X_ID:
  251. netdev_dbg(adapter->netdev,
  252. "Configuring AR803X PHY for 2ns TX clock delay\n");
  253. pch_gbe_phy_read_reg_miic(hw, PHY_AR8031_DBG_OFF, &mii_reg);
  254. ret = pch_gbe_phy_write_reg_miic(hw, PHY_AR8031_DBG_OFF,
  255. PHY_AR8031_SERDES);
  256. if (ret)
  257. break;
  258. pch_gbe_phy_read_reg_miic(hw, PHY_AR8031_DBG_DAT, &mii_reg);
  259. mii_reg |= PHY_AR8031_SERDES_TX_CLK_DLY;
  260. ret = pch_gbe_phy_write_reg_miic(hw, PHY_AR8031_DBG_DAT,
  261. mii_reg);
  262. break;
  263. default:
  264. netdev_err(adapter->netdev,
  265. "Unknown PHY (%x), could not set TX clock delay\n",
  266. hw->phy.id);
  267. return -EINVAL;
  268. }
  269. if (ret)
  270. netdev_err(adapter->netdev,
  271. "Could not configure tx clock delay for PHY\n");
  272. return ret;
  273. }
  274. /**
  275. * pch_gbe_phy_init_setting - PHY initial setting
  276. * @hw: Pointer to the HW structure
  277. */
  278. void pch_gbe_phy_init_setting(struct pch_gbe_hw *hw)
  279. {
  280. struct pch_gbe_adapter *adapter = pch_gbe_hw_to_adapter(hw);
  281. struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
  282. int ret;
  283. u16 mii_reg;
  284. ret = mii_ethtool_gset(&adapter->mii, &cmd);
  285. if (ret)
  286. netdev_err(adapter->netdev, "Error: mii_ethtool_gset\n");
  287. ethtool_cmd_speed_set(&cmd, hw->mac.link_speed);
  288. cmd.duplex = hw->mac.link_duplex;
  289. cmd.advertising = hw->phy.autoneg_advertised;
  290. cmd.autoneg = hw->mac.autoneg;
  291. pch_gbe_phy_write_reg_miic(hw, MII_BMCR, BMCR_RESET);
  292. ret = mii_ethtool_sset(&adapter->mii, &cmd);
  293. if (ret)
  294. netdev_err(adapter->netdev, "Error: mii_ethtool_sset\n");
  295. pch_gbe_phy_sw_reset(hw);
  296. pch_gbe_phy_read_reg_miic(hw, PHY_PHYSP_CONTROL, &mii_reg);
  297. mii_reg |= PHYSP_CTRL_ASSERT_CRS_TX;
  298. pch_gbe_phy_write_reg_miic(hw, PHY_PHYSP_CONTROL, mii_reg);
  299. /* Setup a TX clock delay on certain platforms */
  300. if (adapter->pdata && adapter->pdata->phy_tx_clk_delay)
  301. pch_gbe_phy_tx_clk_delay(hw);
  302. }
  303. /**
  304. * pch_gbe_phy_disable_hibernate - Disable the PHY low power state
  305. * @hw: Pointer to the HW structure
  306. * Returns
  307. * 0: Successful.
  308. * -EINVAL: Invalid argument.
  309. */
  310. int pch_gbe_phy_disable_hibernate(struct pch_gbe_hw *hw)
  311. {
  312. struct pch_gbe_adapter *adapter = pch_gbe_hw_to_adapter(hw);
  313. u16 mii_reg;
  314. int ret = 0;
  315. switch (hw->phy.id) {
  316. case PHY_AR803X_ID:
  317. netdev_dbg(adapter->netdev,
  318. "Disabling hibernation for AR803X PHY\n");
  319. ret = pch_gbe_phy_write_reg_miic(hw, PHY_AR8031_DBG_OFF,
  320. PHY_AR8031_HIBERNATE);
  321. if (ret)
  322. break;
  323. pch_gbe_phy_read_reg_miic(hw, PHY_AR8031_DBG_DAT, &mii_reg);
  324. mii_reg &= ~PHY_AR8031_PS_HIB_EN;
  325. ret = pch_gbe_phy_write_reg_miic(hw, PHY_AR8031_DBG_DAT,
  326. mii_reg);
  327. break;
  328. default:
  329. netdev_err(adapter->netdev,
  330. "Unknown PHY (%x), could not disable hibernation\n",
  331. hw->phy.id);
  332. return -EINVAL;
  333. }
  334. if (ret)
  335. netdev_err(adapter->netdev,
  336. "Could not disable PHY hibernation\n");
  337. return ret;
  338. }