/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_phy.c

http://github.com/mirrors/linux · C · 366 lines · 231 code · 40 blank · 95 comment · 14 complexity · 644efecc135cf76d5d894a41fb6cd3a6 MD5 · raw file

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