dp83867.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Driver for the Texas Instruments DP83867 PHY
  3. *
  4. * Copyright (C) 2015 Texas Instruments Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/ethtool.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mii.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/phy.h>
  21. #include <dt-bindings/net/ti-dp83867.h>
  22. #define DP83867_PHY_ID 0x2000a231
  23. #define DP83867_DEVADDR 0x1f
  24. #define MII_DP83867_PHYCTRL 0x10
  25. #define MII_DP83867_MICR 0x12
  26. #define MII_DP83867_ISR 0x13
  27. #define DP83867_CTRL 0x1f
  28. #define DP83867_CFG3 0x1e
  29. /* Extended Registers */
  30. #define DP83867_RGMIICTL 0x0032
  31. #define DP83867_RGMIIDCTL 0x0086
  32. #define DP83867_SW_RESET BIT(15)
  33. #define DP83867_SW_RESTART BIT(14)
  34. /* MICR Interrupt bits */
  35. #define MII_DP83867_MICR_AN_ERR_INT_EN BIT(15)
  36. #define MII_DP83867_MICR_SPEED_CHNG_INT_EN BIT(14)
  37. #define MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN BIT(13)
  38. #define MII_DP83867_MICR_PAGE_RXD_INT_EN BIT(12)
  39. #define MII_DP83867_MICR_AUTONEG_COMP_INT_EN BIT(11)
  40. #define MII_DP83867_MICR_LINK_STS_CHNG_INT_EN BIT(10)
  41. #define MII_DP83867_MICR_FALSE_CARRIER_INT_EN BIT(8)
  42. #define MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN BIT(4)
  43. #define MII_DP83867_MICR_WOL_INT_EN BIT(3)
  44. #define MII_DP83867_MICR_XGMII_ERR_INT_EN BIT(2)
  45. #define MII_DP83867_MICR_POL_CHNG_INT_EN BIT(1)
  46. #define MII_DP83867_MICR_JABBER_INT_EN BIT(0)
  47. /* RGMIICTL bits */
  48. #define DP83867_RGMII_TX_CLK_DELAY_EN BIT(1)
  49. #define DP83867_RGMII_RX_CLK_DELAY_EN BIT(0)
  50. /* PHY CTRL bits */
  51. #define DP83867_PHYCR_FIFO_DEPTH_SHIFT 14
  52. /* RGMIIDCTL bits */
  53. #define DP83867_RGMII_TX_CLK_DELAY_SHIFT 4
  54. struct dp83867_private {
  55. int rx_id_delay;
  56. int tx_id_delay;
  57. int fifo_depth;
  58. };
  59. static int dp83867_ack_interrupt(struct phy_device *phydev)
  60. {
  61. int err = phy_read(phydev, MII_DP83867_ISR);
  62. if (err < 0)
  63. return err;
  64. return 0;
  65. }
  66. static int dp83867_config_intr(struct phy_device *phydev)
  67. {
  68. int micr_status;
  69. if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
  70. micr_status = phy_read(phydev, MII_DP83867_MICR);
  71. if (micr_status < 0)
  72. return micr_status;
  73. micr_status |=
  74. (MII_DP83867_MICR_AN_ERR_INT_EN |
  75. MII_DP83867_MICR_SPEED_CHNG_INT_EN |
  76. MII_DP83867_MICR_AUTONEG_COMP_INT_EN |
  77. MII_DP83867_MICR_LINK_STS_CHNG_INT_EN |
  78. MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN |
  79. MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN);
  80. return phy_write(phydev, MII_DP83867_MICR, micr_status);
  81. }
  82. micr_status = 0x0;
  83. return phy_write(phydev, MII_DP83867_MICR, micr_status);
  84. }
  85. #ifdef CONFIG_OF_MDIO
  86. static int dp83867_of_init(struct phy_device *phydev)
  87. {
  88. struct dp83867_private *dp83867 = phydev->priv;
  89. struct device *dev = &phydev->dev;
  90. struct device_node *of_node = dev->of_node;
  91. int ret;
  92. if (!of_node && dev->parent->of_node)
  93. of_node = dev->parent->of_node;
  94. if (!phydev->dev.of_node)
  95. return -ENODEV;
  96. ret = of_property_read_u32(of_node, "ti,rx-internal-delay",
  97. &dp83867->rx_id_delay);
  98. if (ret)
  99. return ret;
  100. ret = of_property_read_u32(of_node, "ti,tx-internal-delay",
  101. &dp83867->tx_id_delay);
  102. if (ret)
  103. return ret;
  104. return of_property_read_u32(of_node, "ti,fifo-depth",
  105. &dp83867->fifo_depth);
  106. }
  107. #else
  108. static int dp83867_of_init(struct phy_device *phydev)
  109. {
  110. return 0;
  111. }
  112. #endif /* CONFIG_OF_MDIO */
  113. static int dp83867_config_init(struct phy_device *phydev)
  114. {
  115. struct dp83867_private *dp83867;
  116. int ret;
  117. u16 val, delay;
  118. if (!phydev->priv) {
  119. dp83867 = devm_kzalloc(&phydev->dev, sizeof(*dp83867),
  120. GFP_KERNEL);
  121. if (!dp83867)
  122. return -ENOMEM;
  123. phydev->priv = dp83867;
  124. ret = dp83867_of_init(phydev);
  125. if (ret)
  126. return ret;
  127. } else {
  128. dp83867 = (struct dp83867_private *)phydev->priv;
  129. }
  130. if (phy_interface_is_rgmii(phydev)) {
  131. ret = phy_write(phydev, MII_DP83867_PHYCTRL,
  132. (dp83867->fifo_depth << DP83867_PHYCR_FIFO_DEPTH_SHIFT));
  133. if (ret)
  134. return ret;
  135. }
  136. if ((phydev->interface >= PHY_INTERFACE_MODE_RGMII_ID) &&
  137. (phydev->interface <= PHY_INTERFACE_MODE_RGMII_RXID)) {
  138. val = phy_read_mmd_indirect(phydev, DP83867_RGMIICTL,
  139. DP83867_DEVADDR, phydev->addr);
  140. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
  141. val |= (DP83867_RGMII_TX_CLK_DELAY_EN | DP83867_RGMII_RX_CLK_DELAY_EN);
  142. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
  143. val |= DP83867_RGMII_TX_CLK_DELAY_EN;
  144. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
  145. val |= DP83867_RGMII_RX_CLK_DELAY_EN;
  146. phy_write_mmd_indirect(phydev, DP83867_RGMIICTL,
  147. DP83867_DEVADDR, phydev->addr, val);
  148. delay = (dp83867->rx_id_delay |
  149. (dp83867->tx_id_delay << DP83867_RGMII_TX_CLK_DELAY_SHIFT));
  150. phy_write_mmd_indirect(phydev, DP83867_RGMIIDCTL,
  151. DP83867_DEVADDR, phydev->addr, delay);
  152. }
  153. /* Enable Interrupt output INT_OE in CFG3 register */
  154. if (phy_interrupt_is_valid(phydev)) {
  155. val = phy_read(phydev, DP83867_CFG3);
  156. val |= BIT(7);
  157. phy_write(phydev, DP83867_CFG3, val);
  158. }
  159. return 0;
  160. }
  161. static int dp83867_phy_reset(struct phy_device *phydev)
  162. {
  163. int err;
  164. err = phy_write(phydev, DP83867_CTRL, DP83867_SW_RESET);
  165. if (err < 0)
  166. return err;
  167. return dp83867_config_init(phydev);
  168. }
  169. static struct phy_driver dp83867_driver[] = {
  170. {
  171. .phy_id = DP83867_PHY_ID,
  172. .phy_id_mask = 0xfffffff0,
  173. .name = "TI DP83867",
  174. .features = PHY_GBIT_FEATURES,
  175. .flags = PHY_HAS_INTERRUPT,
  176. .config_init = dp83867_config_init,
  177. .soft_reset = dp83867_phy_reset,
  178. /* IRQ related */
  179. .ack_interrupt = dp83867_ack_interrupt,
  180. .config_intr = dp83867_config_intr,
  181. .config_aneg = genphy_config_aneg,
  182. .read_status = genphy_read_status,
  183. .suspend = genphy_suspend,
  184. .resume = genphy_resume,
  185. .driver = {.owner = THIS_MODULE,}
  186. },
  187. };
  188. module_phy_driver(dp83867_driver);
  189. static struct mdio_device_id __maybe_unused dp83867_tbl[] = {
  190. { DP83867_PHY_ID, 0xfffffff0 },
  191. { }
  192. };
  193. MODULE_DEVICE_TABLE(mdio, dp83867_tbl);
  194. MODULE_DESCRIPTION("Texas Instruments DP83867 PHY driver");
  195. MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com");
  196. MODULE_LICENSE("GPL");