dp83867.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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_CFG4 0x0031
  31. #define DP83867_CFG4_SGMII_ANEG_MASK (BIT(5) | BIT(6))
  32. #define DP83867_CFG4_SGMII_ANEG_TIMER_11MS (3 << 5)
  33. #define DP83867_CFG4_SGMII_ANEG_TIMER_800US (2 << 5)
  34. #define DP83867_CFG4_SGMII_ANEG_TIMER_2US (1 << 5)
  35. #define DP83867_CFG4_SGMII_ANEG_TIMER_16MS (0 << 5)
  36. #define DP83867_RGMIICTL 0x0032
  37. #define DP83867_STRAP_STS1 0x006E
  38. #define DP83867_RGMIIDCTL 0x0086
  39. #define DP83867_IO_MUX_CFG 0x0170
  40. #define DP83867_10M_SGMII_CFG 0x016F
  41. #define DP83867_10M_SGMII_RATE_ADAPT_MASK BIT(7)
  42. #define DP83867_SW_RESET BIT(15)
  43. #define DP83867_SW_RESTART BIT(14)
  44. /* MICR Interrupt bits */
  45. #define MII_DP83867_MICR_AN_ERR_INT_EN BIT(15)
  46. #define MII_DP83867_MICR_SPEED_CHNG_INT_EN BIT(14)
  47. #define MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN BIT(13)
  48. #define MII_DP83867_MICR_PAGE_RXD_INT_EN BIT(12)
  49. #define MII_DP83867_MICR_AUTONEG_COMP_INT_EN BIT(11)
  50. #define MII_DP83867_MICR_LINK_STS_CHNG_INT_EN BIT(10)
  51. #define MII_DP83867_MICR_FALSE_CARRIER_INT_EN BIT(8)
  52. #define MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN BIT(4)
  53. #define MII_DP83867_MICR_WOL_INT_EN BIT(3)
  54. #define MII_DP83867_MICR_XGMII_ERR_INT_EN BIT(2)
  55. #define MII_DP83867_MICR_POL_CHNG_INT_EN BIT(1)
  56. #define MII_DP83867_MICR_JABBER_INT_EN BIT(0)
  57. /* RGMIICTL bits */
  58. #define DP83867_RGMII_TX_CLK_DELAY_EN BIT(1)
  59. #define DP83867_RGMII_RX_CLK_DELAY_EN BIT(0)
  60. /* STRAP_STS1 bits */
  61. #define DP83867_STRAP_STS1_RESERVED BIT(11)
  62. /* PHY CTRL bits */
  63. #define DP83867_PHYCR_FIFO_DEPTH_SHIFT 14
  64. #define DP83867_PHYCR_FIFO_DEPTH_MASK (3 << 14)
  65. #define DP83867_PHYCR_RESERVED_MASK BIT(11)
  66. /* RGMIIDCTL bits */
  67. #define DP83867_RGMII_TX_CLK_DELAY_SHIFT 4
  68. /* IO_MUX_CFG bits */
  69. #define DP83867_IO_MUX_CFG_IO_IMPEDANCE_CTRL 0x1f
  70. #define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MAX 0x0
  71. #define DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN 0x1f
  72. #define DP83867_IO_MUX_CFG_CLK_O_SEL_MASK (0x1f << 8)
  73. #define DP83867_IO_MUX_CFG_CLK_O_SEL_SHIFT 8
  74. /* CFG3 bits */
  75. #define DP83867_CFG3_INT_OE BIT(7)
  76. #define DP83867_CFG3_ROBUST_AUTO_MDIX BIT(9)
  77. /* CFG4 bits */
  78. #define DP83867_CFG4_PORT_MIRROR_EN BIT(0)
  79. enum {
  80. DP83867_PORT_MIRROING_KEEP,
  81. DP83867_PORT_MIRROING_EN,
  82. DP83867_PORT_MIRROING_DIS,
  83. };
  84. struct dp83867_private {
  85. int rx_id_delay;
  86. int tx_id_delay;
  87. int fifo_depth;
  88. int io_impedance;
  89. int port_mirroring;
  90. bool rxctrl_strap_quirk;
  91. int clk_output_sel;
  92. };
  93. static int dp83867_ack_interrupt(struct phy_device *phydev)
  94. {
  95. int err = phy_read(phydev, MII_DP83867_ISR);
  96. if (err < 0)
  97. return err;
  98. return 0;
  99. }
  100. static int dp83867_config_intr(struct phy_device *phydev)
  101. {
  102. int micr_status;
  103. if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
  104. micr_status = phy_read(phydev, MII_DP83867_MICR);
  105. if (micr_status < 0)
  106. return micr_status;
  107. micr_status |=
  108. (MII_DP83867_MICR_AN_ERR_INT_EN |
  109. MII_DP83867_MICR_SPEED_CHNG_INT_EN |
  110. MII_DP83867_MICR_AUTONEG_COMP_INT_EN |
  111. MII_DP83867_MICR_LINK_STS_CHNG_INT_EN |
  112. MII_DP83867_MICR_DUP_MODE_CHNG_INT_EN |
  113. MII_DP83867_MICR_SLEEP_MODE_CHNG_INT_EN);
  114. return phy_write(phydev, MII_DP83867_MICR, micr_status);
  115. }
  116. micr_status = 0x0;
  117. return phy_write(phydev, MII_DP83867_MICR, micr_status);
  118. }
  119. static int dp83867_config_port_mirroring(struct phy_device *phydev)
  120. {
  121. struct dp83867_private *dp83867 =
  122. (struct dp83867_private *)phydev->priv;
  123. u16 val;
  124. val = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4);
  125. if (dp83867->port_mirroring == DP83867_PORT_MIRROING_EN)
  126. val |= DP83867_CFG4_PORT_MIRROR_EN;
  127. else
  128. val &= ~DP83867_CFG4_PORT_MIRROR_EN;
  129. phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4, val);
  130. return 0;
  131. }
  132. #ifdef CONFIG_OF_MDIO
  133. static int dp83867_of_init(struct phy_device *phydev)
  134. {
  135. struct dp83867_private *dp83867 = phydev->priv;
  136. struct device *dev = &phydev->mdio.dev;
  137. struct device_node *of_node = dev->of_node;
  138. int ret;
  139. if (!of_node)
  140. return -ENODEV;
  141. dp83867->io_impedance = -EINVAL;
  142. /* Optional configuration */
  143. ret = of_property_read_u32(of_node, "ti,clk-output-sel",
  144. &dp83867->clk_output_sel);
  145. if (ret || dp83867->clk_output_sel > DP83867_CLK_O_SEL_REF_CLK)
  146. /* Keep the default value if ti,clk-output-sel is not set
  147. * or too high
  148. */
  149. dp83867->clk_output_sel = DP83867_CLK_O_SEL_REF_CLK;
  150. if (of_property_read_bool(of_node, "ti,max-output-impedance"))
  151. dp83867->io_impedance = DP83867_IO_MUX_CFG_IO_IMPEDANCE_MAX;
  152. else if (of_property_read_bool(of_node, "ti,min-output-impedance"))
  153. dp83867->io_impedance = DP83867_IO_MUX_CFG_IO_IMPEDANCE_MIN;
  154. dp83867->rxctrl_strap_quirk = of_property_read_bool(of_node,
  155. "ti,dp83867-rxctrl-strap-quirk");
  156. ret = of_property_read_u32(of_node, "ti,rx-internal-delay",
  157. &dp83867->rx_id_delay);
  158. if (ret &&
  159. (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
  160. phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID))
  161. return ret;
  162. ret = of_property_read_u32(of_node, "ti,tx-internal-delay",
  163. &dp83867->tx_id_delay);
  164. if (ret &&
  165. (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
  166. phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID))
  167. return ret;
  168. if (of_property_read_bool(of_node, "enet-phy-lane-swap"))
  169. dp83867->port_mirroring = DP83867_PORT_MIRROING_EN;
  170. if (of_property_read_bool(of_node, "enet-phy-lane-no-swap"))
  171. dp83867->port_mirroring = DP83867_PORT_MIRROING_DIS;
  172. return of_property_read_u32(of_node, "ti,fifo-depth",
  173. &dp83867->fifo_depth);
  174. }
  175. #else
  176. static int dp83867_of_init(struct phy_device *phydev)
  177. {
  178. return 0;
  179. }
  180. #endif /* CONFIG_OF_MDIO */
  181. static int dp83867_config_init(struct phy_device *phydev)
  182. {
  183. struct dp83867_private *dp83867;
  184. int ret, val, bs;
  185. u16 delay;
  186. if (!phydev->priv) {
  187. dp83867 = devm_kzalloc(&phydev->mdio.dev, sizeof(*dp83867),
  188. GFP_KERNEL);
  189. if (!dp83867)
  190. return -ENOMEM;
  191. phydev->priv = dp83867;
  192. ret = dp83867_of_init(phydev);
  193. if (ret)
  194. return ret;
  195. } else {
  196. dp83867 = (struct dp83867_private *)phydev->priv;
  197. }
  198. /* RX_DV/RX_CTRL strapped in mode 1 or mode 2 workaround */
  199. if (dp83867->rxctrl_strap_quirk) {
  200. val = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4);
  201. val &= ~BIT(7);
  202. phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4, val);
  203. }
  204. if (phy_interface_is_rgmii(phydev)) {
  205. val = phy_read(phydev, MII_DP83867_PHYCTRL);
  206. if (val < 0)
  207. return val;
  208. val &= ~DP83867_PHYCR_FIFO_DEPTH_MASK;
  209. val |= (dp83867->fifo_depth << DP83867_PHYCR_FIFO_DEPTH_SHIFT);
  210. /* The code below checks if "port mirroring" N/A MODE4 has been
  211. * enabled during power on bootstrap.
  212. *
  213. * Such N/A mode enabled by mistake can put PHY IC in some
  214. * internal testing mode and disable RGMII transmission.
  215. *
  216. * In this particular case one needs to check STRAP_STS1
  217. * register's bit 11 (marked as RESERVED).
  218. */
  219. bs = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_STRAP_STS1);
  220. if (bs & DP83867_STRAP_STS1_RESERVED)
  221. val &= ~DP83867_PHYCR_RESERVED_MASK;
  222. ret = phy_write(phydev, MII_DP83867_PHYCTRL, val);
  223. if (ret)
  224. return ret;
  225. /* Set up RGMII delays */
  226. val = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_RGMIICTL);
  227. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
  228. val |= (DP83867_RGMII_TX_CLK_DELAY_EN | DP83867_RGMII_RX_CLK_DELAY_EN);
  229. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
  230. val |= DP83867_RGMII_TX_CLK_DELAY_EN;
  231. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
  232. val |= DP83867_RGMII_RX_CLK_DELAY_EN;
  233. phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_RGMIICTL, val);
  234. delay = (dp83867->rx_id_delay |
  235. (dp83867->tx_id_delay << DP83867_RGMII_TX_CLK_DELAY_SHIFT));
  236. phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_RGMIIDCTL,
  237. delay);
  238. if (dp83867->io_impedance >= 0) {
  239. val = phy_read_mmd(phydev, DP83867_DEVADDR,
  240. DP83867_IO_MUX_CFG);
  241. val &= ~DP83867_IO_MUX_CFG_IO_IMPEDANCE_CTRL;
  242. val |= dp83867->io_impedance &
  243. DP83867_IO_MUX_CFG_IO_IMPEDANCE_CTRL;
  244. phy_write_mmd(phydev, DP83867_DEVADDR,
  245. DP83867_IO_MUX_CFG, val);
  246. }
  247. }
  248. if (phydev->interface == PHY_INTERFACE_MODE_SGMII) {
  249. /* For support SPEED_10 in SGMII mode
  250. * DP83867_10M_SGMII_RATE_ADAPT bit
  251. * has to be cleared by software. That
  252. * does not affect SPEED_100 and
  253. * SPEED_1000.
  254. */
  255. val = phy_read_mmd(phydev, DP83867_DEVADDR,
  256. DP83867_10M_SGMII_CFG);
  257. val &= ~DP83867_10M_SGMII_RATE_ADAPT_MASK;
  258. ret = phy_write_mmd(phydev, DP83867_DEVADDR,
  259. DP83867_10M_SGMII_CFG, val);
  260. if (ret)
  261. return ret;
  262. /* After reset SGMII Autoneg timer is set to 2us (bits 6 and 5
  263. * are 01). That is not enough to finalize autoneg on some
  264. * devices. Increase this timer duration to maximum 16ms.
  265. */
  266. val = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4);
  267. val &= ~DP83867_CFG4_SGMII_ANEG_MASK;
  268. val |= DP83867_CFG4_SGMII_ANEG_TIMER_16MS;
  269. ret = phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_CFG4, val);
  270. if (ret)
  271. return ret;
  272. }
  273. val = phy_read(phydev, DP83867_CFG3);
  274. /* Enable Interrupt output INT_OE in CFG3 register */
  275. if (phy_interrupt_is_valid(phydev))
  276. val |= DP83867_CFG3_INT_OE;
  277. val |= DP83867_CFG3_ROBUST_AUTO_MDIX;
  278. phy_write(phydev, DP83867_CFG3, val);
  279. if (dp83867->port_mirroring != DP83867_PORT_MIRROING_KEEP)
  280. dp83867_config_port_mirroring(phydev);
  281. /* Clock output selection if muxing property is set */
  282. if (dp83867->clk_output_sel != DP83867_CLK_O_SEL_REF_CLK) {
  283. val = phy_read_mmd(phydev, DP83867_DEVADDR, DP83867_IO_MUX_CFG);
  284. val &= ~DP83867_IO_MUX_CFG_CLK_O_SEL_MASK;
  285. val |= (dp83867->clk_output_sel << DP83867_IO_MUX_CFG_CLK_O_SEL_SHIFT);
  286. phy_write_mmd(phydev, DP83867_DEVADDR, DP83867_IO_MUX_CFG, val);
  287. }
  288. return 0;
  289. }
  290. static int dp83867_phy_reset(struct phy_device *phydev)
  291. {
  292. int err;
  293. err = phy_write(phydev, DP83867_CTRL, DP83867_SW_RESET);
  294. if (err < 0)
  295. return err;
  296. return dp83867_config_init(phydev);
  297. }
  298. static struct phy_driver dp83867_driver[] = {
  299. {
  300. .phy_id = DP83867_PHY_ID,
  301. .phy_id_mask = 0xfffffff0,
  302. .name = "TI DP83867",
  303. .features = PHY_GBIT_FEATURES,
  304. .flags = PHY_HAS_INTERRUPT,
  305. .config_init = dp83867_config_init,
  306. .soft_reset = dp83867_phy_reset,
  307. /* IRQ related */
  308. .ack_interrupt = dp83867_ack_interrupt,
  309. .config_intr = dp83867_config_intr,
  310. .suspend = genphy_suspend,
  311. .resume = genphy_resume,
  312. },
  313. };
  314. module_phy_driver(dp83867_driver);
  315. static struct mdio_device_id __maybe_unused dp83867_tbl[] = {
  316. { DP83867_PHY_ID, 0xfffffff0 },
  317. { }
  318. };
  319. MODULE_DEVICE_TABLE(mdio, dp83867_tbl);
  320. MODULE_DESCRIPTION("Texas Instruments DP83867 PHY driver");
  321. MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com");
  322. MODULE_LICENSE("GPL");