microchip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Microchip Technology
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/mii.h>
  8. #include <linux/ethtool.h>
  9. #include <linux/phy.h>
  10. #include <linux/microchipphy.h>
  11. #include <linux/delay.h>
  12. #include <linux/of.h>
  13. #include <dt-bindings/net/microchip-lan78xx.h>
  14. #define PHY_ID_LAN937X_TX 0x0007c190
  15. #define LAN937X_MODE_CTRL_STATUS_REG 0x11
  16. #define LAN937X_AUTOMDIX_EN BIT(7)
  17. #define LAN937X_MDI_MODE BIT(6)
  18. #define DRIVER_AUTHOR "WOOJUNG HUH <woojung.huh@microchip.com>"
  19. #define DRIVER_DESC "Microchip LAN88XX/LAN937X TX PHY driver"
  20. struct lan88xx_priv {
  21. int chip_id;
  22. int chip_rev;
  23. __u32 wolopts;
  24. };
  25. static int lan88xx_read_page(struct phy_device *phydev)
  26. {
  27. return __phy_read(phydev, LAN88XX_EXT_PAGE_ACCESS);
  28. }
  29. static int lan88xx_write_page(struct phy_device *phydev, int page)
  30. {
  31. return __phy_write(phydev, LAN88XX_EXT_PAGE_ACCESS, page);
  32. }
  33. static int lan88xx_suspend(struct phy_device *phydev)
  34. {
  35. struct lan88xx_priv *priv = phydev->priv;
  36. /* do not power down PHY when WOL is enabled */
  37. if (!priv->wolopts)
  38. genphy_suspend(phydev);
  39. return 0;
  40. }
  41. static int lan88xx_TR_reg_set(struct phy_device *phydev, u16 regaddr,
  42. u32 data)
  43. {
  44. int val, save_page, ret = 0;
  45. u16 buf;
  46. /* Save current page */
  47. save_page = phy_save_page(phydev);
  48. if (save_page < 0) {
  49. phydev_warn(phydev, "Failed to get current page\n");
  50. goto err;
  51. }
  52. /* Switch to TR page */
  53. lan88xx_write_page(phydev, LAN88XX_EXT_PAGE_ACCESS_TR);
  54. ret = __phy_write(phydev, LAN88XX_EXT_PAGE_TR_LOW_DATA,
  55. (data & 0xFFFF));
  56. if (ret < 0) {
  57. phydev_warn(phydev, "Failed to write TR low data\n");
  58. goto err;
  59. }
  60. ret = __phy_write(phydev, LAN88XX_EXT_PAGE_TR_HIGH_DATA,
  61. (data & 0x00FF0000) >> 16);
  62. if (ret < 0) {
  63. phydev_warn(phydev, "Failed to write TR high data\n");
  64. goto err;
  65. }
  66. /* Config control bits [15:13] of register */
  67. buf = (regaddr & ~(0x3 << 13));/* Clr [14:13] to write data in reg */
  68. buf |= 0x8000; /* Set [15] to Packet transmit */
  69. ret = __phy_write(phydev, LAN88XX_EXT_PAGE_TR_CR, buf);
  70. if (ret < 0) {
  71. phydev_warn(phydev, "Failed to write data in reg\n");
  72. goto err;
  73. }
  74. usleep_range(1000, 2000);/* Wait for Data to be written */
  75. val = __phy_read(phydev, LAN88XX_EXT_PAGE_TR_CR);
  76. if (!(val & 0x8000))
  77. phydev_warn(phydev, "TR Register[0x%X] configuration failed\n",
  78. regaddr);
  79. err:
  80. return phy_restore_page(phydev, save_page, ret);
  81. }
  82. static void lan88xx_config_TR_regs(struct phy_device *phydev)
  83. {
  84. int err;
  85. /* Get access to Channel 0x1, Node 0xF , Register 0x01.
  86. * Write 24-bit value 0x12B00A to register. Setting MrvlTrFix1000Kf,
  87. * MrvlTrFix1000Kp, MasterEnableTR bits.
  88. */
  89. err = lan88xx_TR_reg_set(phydev, 0x0F82, 0x12B00A);
  90. if (err < 0)
  91. phydev_warn(phydev, "Failed to Set Register[0x0F82]\n");
  92. /* Get access to Channel b'10, Node b'1101, Register 0x06.
  93. * Write 24-bit value 0xD2C46F to register. Setting SSTrKf1000Slv,
  94. * SSTrKp1000Mas bits.
  95. */
  96. err = lan88xx_TR_reg_set(phydev, 0x168C, 0xD2C46F);
  97. if (err < 0)
  98. phydev_warn(phydev, "Failed to Set Register[0x168C]\n");
  99. /* Get access to Channel b'10, Node b'1111, Register 0x11.
  100. * Write 24-bit value 0x620 to register. Setting rem_upd_done_thresh
  101. * bits
  102. */
  103. err = lan88xx_TR_reg_set(phydev, 0x17A2, 0x620);
  104. if (err < 0)
  105. phydev_warn(phydev, "Failed to Set Register[0x17A2]\n");
  106. /* Get access to Channel b'10, Node b'1101, Register 0x10.
  107. * Write 24-bit value 0xEEFFDD to register. Setting
  108. * eee_TrKp1Long_1000, eee_TrKp2Long_1000, eee_TrKp3Long_1000,
  109. * eee_TrKp1Short_1000,eee_TrKp2Short_1000, eee_TrKp3Short_1000 bits.
  110. */
  111. err = lan88xx_TR_reg_set(phydev, 0x16A0, 0xEEFFDD);
  112. if (err < 0)
  113. phydev_warn(phydev, "Failed to Set Register[0x16A0]\n");
  114. /* Get access to Channel b'10, Node b'1101, Register 0x13.
  115. * Write 24-bit value 0x071448 to register. Setting
  116. * slv_lpi_tr_tmr_val1, slv_lpi_tr_tmr_val2 bits.
  117. */
  118. err = lan88xx_TR_reg_set(phydev, 0x16A6, 0x071448);
  119. if (err < 0)
  120. phydev_warn(phydev, "Failed to Set Register[0x16A6]\n");
  121. /* Get access to Channel b'10, Node b'1101, Register 0x12.
  122. * Write 24-bit value 0x13132F to register. Setting
  123. * slv_sigdet_timer_val1, slv_sigdet_timer_val2 bits.
  124. */
  125. err = lan88xx_TR_reg_set(phydev, 0x16A4, 0x13132F);
  126. if (err < 0)
  127. phydev_warn(phydev, "Failed to Set Register[0x16A4]\n");
  128. /* Get access to Channel b'10, Node b'1101, Register 0x14.
  129. * Write 24-bit value 0x0 to register. Setting eee_3level_delay,
  130. * eee_TrKf_freeze_delay bits.
  131. */
  132. err = lan88xx_TR_reg_set(phydev, 0x16A8, 0x0);
  133. if (err < 0)
  134. phydev_warn(phydev, "Failed to Set Register[0x16A8]\n");
  135. /* Get access to Channel b'01, Node b'1111, Register 0x34.
  136. * Write 24-bit value 0x91B06C to register. Setting
  137. * FastMseSearchThreshLong1000, FastMseSearchThreshShort1000,
  138. * FastMseSearchUpdGain1000 bits.
  139. */
  140. err = lan88xx_TR_reg_set(phydev, 0x0FE8, 0x91B06C);
  141. if (err < 0)
  142. phydev_warn(phydev, "Failed to Set Register[0x0FE8]\n");
  143. /* Get access to Channel b'01, Node b'1111, Register 0x3E.
  144. * Write 24-bit value 0xC0A028 to register. Setting
  145. * FastMseKp2ThreshLong1000, FastMseKp2ThreshShort1000,
  146. * FastMseKp2UpdGain1000, FastMseKp2ExitEn1000 bits.
  147. */
  148. err = lan88xx_TR_reg_set(phydev, 0x0FFC, 0xC0A028);
  149. if (err < 0)
  150. phydev_warn(phydev, "Failed to Set Register[0x0FFC]\n");
  151. /* Get access to Channel b'01, Node b'1111, Register 0x35.
  152. * Write 24-bit value 0x041600 to register. Setting
  153. * FastMseSearchPhShNum1000, FastMseSearchClksPerPh1000,
  154. * FastMsePhChangeDelay1000 bits.
  155. */
  156. err = lan88xx_TR_reg_set(phydev, 0x0FEA, 0x041600);
  157. if (err < 0)
  158. phydev_warn(phydev, "Failed to Set Register[0x0FEA]\n");
  159. /* Get access to Channel b'10, Node b'1101, Register 0x03.
  160. * Write 24-bit value 0x000004 to register. Setting TrFreeze bits.
  161. */
  162. err = lan88xx_TR_reg_set(phydev, 0x1686, 0x000004);
  163. if (err < 0)
  164. phydev_warn(phydev, "Failed to Set Register[0x1686]\n");
  165. }
  166. static int lan88xx_probe(struct phy_device *phydev)
  167. {
  168. struct device *dev = &phydev->mdio.dev;
  169. struct lan88xx_priv *priv;
  170. u32 led_modes[4];
  171. int len;
  172. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  173. if (!priv)
  174. return -ENOMEM;
  175. priv->wolopts = 0;
  176. len = of_property_read_variable_u32_array(dev->of_node,
  177. "microchip,led-modes",
  178. led_modes,
  179. 0,
  180. ARRAY_SIZE(led_modes));
  181. if (len >= 0) {
  182. u32 reg = 0;
  183. int i;
  184. for (i = 0; i < len; i++) {
  185. if (led_modes[i] > 15)
  186. return -EINVAL;
  187. reg |= led_modes[i] << (i * 4);
  188. }
  189. for (; i < ARRAY_SIZE(led_modes); i++)
  190. reg |= LAN78XX_FORCE_LED_OFF << (i * 4);
  191. (void)phy_write(phydev, LAN78XX_PHY_LED_MODE_SELECT, reg);
  192. } else if (len == -EOVERFLOW) {
  193. return -EINVAL;
  194. }
  195. /* these values can be used to identify internal PHY */
  196. priv->chip_id = phy_read_mmd(phydev, 3, LAN88XX_MMD3_CHIP_ID);
  197. priv->chip_rev = phy_read_mmd(phydev, 3, LAN88XX_MMD3_CHIP_REV);
  198. phydev->priv = priv;
  199. return 0;
  200. }
  201. static void lan88xx_remove(struct phy_device *phydev)
  202. {
  203. struct device *dev = &phydev->mdio.dev;
  204. struct lan88xx_priv *priv = phydev->priv;
  205. if (priv)
  206. devm_kfree(dev, priv);
  207. }
  208. static int lan88xx_set_wol(struct phy_device *phydev,
  209. struct ethtool_wolinfo *wol)
  210. {
  211. struct lan88xx_priv *priv = phydev->priv;
  212. priv->wolopts = wol->wolopts;
  213. return 0;
  214. }
  215. static void lan88xx_set_mdix(struct phy_device *phydev)
  216. {
  217. int buf;
  218. int val;
  219. switch (phydev->mdix_ctrl) {
  220. case ETH_TP_MDI:
  221. val = LAN88XX_EXT_MODE_CTRL_MDI_;
  222. break;
  223. case ETH_TP_MDI_X:
  224. val = LAN88XX_EXT_MODE_CTRL_MDI_X_;
  225. break;
  226. case ETH_TP_MDI_AUTO:
  227. val = LAN88XX_EXT_MODE_CTRL_AUTO_MDIX_;
  228. break;
  229. default:
  230. return;
  231. }
  232. phy_write(phydev, LAN88XX_EXT_PAGE_ACCESS, LAN88XX_EXT_PAGE_SPACE_1);
  233. buf = phy_read(phydev, LAN88XX_EXT_MODE_CTRL);
  234. buf &= ~LAN88XX_EXT_MODE_CTRL_MDIX_MASK_;
  235. buf |= val;
  236. phy_write(phydev, LAN88XX_EXT_MODE_CTRL, buf);
  237. phy_write(phydev, LAN88XX_EXT_PAGE_ACCESS, LAN88XX_EXT_PAGE_SPACE_0);
  238. }
  239. static int lan88xx_config_init(struct phy_device *phydev)
  240. {
  241. int val;
  242. /*Zerodetect delay enable */
  243. val = phy_read_mmd(phydev, MDIO_MMD_PCS,
  244. PHY_ARDENNES_MMD_DEV_3_PHY_CFG);
  245. val |= PHY_ARDENNES_MMD_DEV_3_PHY_CFG_ZD_DLY_EN_;
  246. phy_write_mmd(phydev, MDIO_MMD_PCS, PHY_ARDENNES_MMD_DEV_3_PHY_CFG,
  247. val);
  248. /* Config DSP registers */
  249. lan88xx_config_TR_regs(phydev);
  250. return 0;
  251. }
  252. static int lan88xx_config_aneg(struct phy_device *phydev)
  253. {
  254. lan88xx_set_mdix(phydev);
  255. return genphy_config_aneg(phydev);
  256. }
  257. static void lan88xx_link_change_notify(struct phy_device *phydev)
  258. {
  259. int temp;
  260. int ret;
  261. /* Reset PHY to ensure MII_LPA provides up-to-date information. This
  262. * issue is reproducible only after parallel detection, as described
  263. * in IEEE 802.3-2022, Section 28.2.3.1 ("Parallel detection function"),
  264. * where the link partner does not support auto-negotiation.
  265. */
  266. if (phydev->state == PHY_NOLINK) {
  267. ret = phy_init_hw(phydev);
  268. if (ret < 0)
  269. goto link_change_notify_failed;
  270. ret = _phy_start_aneg(phydev);
  271. if (ret < 0)
  272. goto link_change_notify_failed;
  273. }
  274. /* At forced 100 F/H mode, chip may fail to set mode correctly
  275. * when cable is switched between long(~50+m) and short one.
  276. * As workaround, set to 10 before setting to 100
  277. * at forced 100 F/H mode.
  278. */
  279. if (phydev->state == PHY_NOLINK && !phydev->autoneg && phydev->speed == 100) {
  280. /* disable phy interrupt */
  281. temp = phy_read(phydev, LAN88XX_INT_MASK);
  282. temp &= ~LAN88XX_INT_MASK_MDINTPIN_EN_;
  283. phy_write(phydev, LAN88XX_INT_MASK, temp);
  284. temp = phy_read(phydev, MII_BMCR);
  285. temp &= ~(BMCR_SPEED100 | BMCR_SPEED1000);
  286. phy_write(phydev, MII_BMCR, temp); /* set to 10 first */
  287. temp |= BMCR_SPEED100;
  288. phy_write(phydev, MII_BMCR, temp); /* set to 100 later */
  289. /* clear pending interrupt generated while workaround */
  290. temp = phy_read(phydev, LAN88XX_INT_STS);
  291. /* enable phy interrupt back */
  292. temp = phy_read(phydev, LAN88XX_INT_MASK);
  293. temp |= LAN88XX_INT_MASK_MDINTPIN_EN_;
  294. phy_write(phydev, LAN88XX_INT_MASK, temp);
  295. }
  296. return;
  297. link_change_notify_failed:
  298. phydev_err(phydev, "Link change process failed %pe\n", ERR_PTR(ret));
  299. }
  300. /**
  301. * lan937x_tx_read_mdix_status - Read the MDIX status for the LAN937x TX PHY.
  302. * @phydev: Pointer to the phy_device structure.
  303. *
  304. * This function reads the MDIX status of the LAN937x TX PHY and sets the
  305. * mdix_ctrl and mdix fields of the phy_device structure accordingly.
  306. * Note that MDIX status is not supported in AUTO mode, and will be set
  307. * to invalid in such cases.
  308. *
  309. * Return: 0 on success, a negative error code on failure.
  310. */
  311. static int lan937x_tx_read_mdix_status(struct phy_device *phydev)
  312. {
  313. int ret;
  314. ret = phy_read(phydev, LAN937X_MODE_CTRL_STATUS_REG);
  315. if (ret < 0)
  316. return ret;
  317. if (ret & LAN937X_AUTOMDIX_EN) {
  318. phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
  319. /* MDI/MDIX status is unknown */
  320. phydev->mdix = ETH_TP_MDI_INVALID;
  321. } else if (ret & LAN937X_MDI_MODE) {
  322. phydev->mdix_ctrl = ETH_TP_MDI_X;
  323. phydev->mdix = ETH_TP_MDI_X;
  324. } else {
  325. phydev->mdix_ctrl = ETH_TP_MDI;
  326. phydev->mdix = ETH_TP_MDI;
  327. }
  328. return 0;
  329. }
  330. /**
  331. * lan937x_tx_read_status - Read the status for the LAN937x TX PHY.
  332. * @phydev: Pointer to the phy_device structure.
  333. *
  334. * This function reads the status of the LAN937x TX PHY and updates the
  335. * phy_device structure accordingly.
  336. *
  337. * Return: 0 on success, a negative error code on failure.
  338. */
  339. static int lan937x_tx_read_status(struct phy_device *phydev)
  340. {
  341. int ret;
  342. ret = genphy_read_status(phydev);
  343. if (ret < 0)
  344. return ret;
  345. return lan937x_tx_read_mdix_status(phydev);
  346. }
  347. /**
  348. * lan937x_tx_set_mdix - Set the MDIX mode for the LAN937x TX PHY.
  349. * @phydev: Pointer to the phy_device structure.
  350. *
  351. * This function configures the MDIX mode of the LAN937x TX PHY based on the
  352. * mdix_ctrl field of the phy_device structure. The MDIX mode can be set to
  353. * MDI (straight-through), MDIX (crossover), or AUTO (auto-MDIX). If the mode
  354. * is not recognized, it returns 0 without making any changes.
  355. *
  356. * Return: 0 on success, a negative error code on failure.
  357. */
  358. static int lan937x_tx_set_mdix(struct phy_device *phydev)
  359. {
  360. u16 val;
  361. switch (phydev->mdix_ctrl) {
  362. case ETH_TP_MDI:
  363. val = 0;
  364. break;
  365. case ETH_TP_MDI_X:
  366. val = LAN937X_MDI_MODE;
  367. break;
  368. case ETH_TP_MDI_AUTO:
  369. val = LAN937X_AUTOMDIX_EN;
  370. break;
  371. default:
  372. return 0;
  373. }
  374. return phy_modify(phydev, LAN937X_MODE_CTRL_STATUS_REG,
  375. LAN937X_AUTOMDIX_EN | LAN937X_MDI_MODE, val);
  376. }
  377. /**
  378. * lan937x_tx_config_aneg - Configure auto-negotiation and fixed modes for the
  379. * LAN937x TX PHY.
  380. * @phydev: Pointer to the phy_device structure.
  381. *
  382. * This function configures the MDIX mode for the LAN937x TX PHY and then
  383. * proceeds to configure the auto-negotiation or fixed mode settings
  384. * based on the phy_device structure.
  385. *
  386. * Return: 0 on success, a negative error code on failure.
  387. */
  388. static int lan937x_tx_config_aneg(struct phy_device *phydev)
  389. {
  390. int ret;
  391. ret = lan937x_tx_set_mdix(phydev);
  392. if (ret < 0)
  393. return ret;
  394. return genphy_config_aneg(phydev);
  395. }
  396. static struct phy_driver microchip_phy_driver[] = {
  397. {
  398. .phy_id = 0x0007c132,
  399. /* This mask (0xfffffff2) is to differentiate from
  400. * LAN8742 (phy_id 0x0007c130 and 0x0007c131)
  401. * and allows future phy_id revisions.
  402. * These PHYs are integrated in LAN7800 and LAN7850 USB/Ethernet
  403. * controllers.
  404. */
  405. .phy_id_mask = 0xfffffff2,
  406. .name = "Microchip LAN88xx",
  407. /* PHY_GBIT_FEATURES */
  408. .probe = lan88xx_probe,
  409. .remove = lan88xx_remove,
  410. .config_init = lan88xx_config_init,
  411. .config_aneg = lan88xx_config_aneg,
  412. .link_change_notify = lan88xx_link_change_notify,
  413. .soft_reset = genphy_soft_reset,
  414. /* Interrupt handling is broken, do not define related
  415. * functions to force polling.
  416. */
  417. .suspend = lan88xx_suspend,
  418. .resume = genphy_resume,
  419. .set_wol = lan88xx_set_wol,
  420. .read_page = lan88xx_read_page,
  421. .write_page = lan88xx_write_page,
  422. },
  423. {
  424. PHY_ID_MATCH_MODEL(PHY_ID_LAN937X_TX),
  425. .name = "Microchip LAN937x TX",
  426. .suspend = genphy_suspend,
  427. .resume = genphy_resume,
  428. .config_aneg = lan937x_tx_config_aneg,
  429. .read_status = lan937x_tx_read_status,
  430. } };
  431. module_phy_driver(microchip_phy_driver);
  432. static const struct mdio_device_id __maybe_unused microchip_tbl[] = {
  433. { 0x0007c132, 0xfffffff2 },
  434. { PHY_ID_MATCH_MODEL(PHY_ID_LAN937X_TX) },
  435. { }
  436. };
  437. MODULE_DEVICE_TABLE(mdio, microchip_tbl);
  438. MODULE_AUTHOR(DRIVER_AUTHOR);
  439. MODULE_DESCRIPTION(DRIVER_DESC);
  440. MODULE_LICENSE("GPL");