mxl-gpy.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Copyright (C) 2021 Maxlinear Corporation
  3. * Copyright (C) 2020 Intel Corporation
  4. *
  5. * Drivers for Maxlinear Ethernet GPY
  6. *
  7. */
  8. #include <linux/module.h>
  9. #include <linux/bitfield.h>
  10. #include <linux/hwmon.h>
  11. #include <linux/mutex.h>
  12. #include <linux/phy.h>
  13. #include <linux/polynomial.h>
  14. #include <linux/property.h>
  15. #include <linux/netdevice.h>
  16. /* PHY ID */
  17. #define PHY_ID_GPYx15B_MASK 0xFFFFFFFC
  18. #define PHY_ID_GPY21xB_MASK 0xFFFFFFF9
  19. #define PHY_ID_GPY2xx 0x67C9DC00
  20. #define PHY_ID_GPY115B 0x67C9DF00
  21. #define PHY_ID_GPY115C 0x67C9DF10
  22. #define PHY_ID_GPY211B 0x67C9DE08
  23. #define PHY_ID_GPY211C 0x67C9DE10
  24. #define PHY_ID_GPY212B 0x67C9DE09
  25. #define PHY_ID_GPY212C 0x67C9DE20
  26. #define PHY_ID_GPY215B 0x67C9DF04
  27. #define PHY_ID_GPY215C 0x67C9DF20
  28. #define PHY_ID_GPY241B 0x67C9DE40
  29. #define PHY_ID_GPY241BM 0x67C9DE80
  30. #define PHY_ID_GPY245B 0x67C9DEC0
  31. #define PHY_CTL1 0x13
  32. #define PHY_CTL1_MDICD BIT(3)
  33. #define PHY_CTL1_MDIAB BIT(2)
  34. #define PHY_CTL1_AMDIX BIT(0)
  35. #define PHY_MIISTAT 0x18 /* MII state */
  36. #define PHY_IMASK 0x19 /* interrupt mask */
  37. #define PHY_ISTAT 0x1A /* interrupt status */
  38. #define PHY_FWV 0x1E /* firmware version */
  39. #define PHY_MIISTAT_SPD_MASK GENMASK(2, 0)
  40. #define PHY_MIISTAT_DPX BIT(3)
  41. #define PHY_MIISTAT_LS BIT(10)
  42. #define PHY_MIISTAT_SPD_10 0
  43. #define PHY_MIISTAT_SPD_100 1
  44. #define PHY_MIISTAT_SPD_1000 2
  45. #define PHY_MIISTAT_SPD_2500 4
  46. #define PHY_IMASK_WOL BIT(15) /* Wake-on-LAN */
  47. #define PHY_IMASK_ANC BIT(10) /* Auto-Neg complete */
  48. #define PHY_IMASK_ADSC BIT(5) /* Link auto-downspeed detect */
  49. #define PHY_IMASK_DXMC BIT(2) /* Duplex mode change */
  50. #define PHY_IMASK_LSPC BIT(1) /* Link speed change */
  51. #define PHY_IMASK_LSTC BIT(0) /* Link state change */
  52. #define PHY_IMASK_MASK (PHY_IMASK_LSTC | \
  53. PHY_IMASK_LSPC | \
  54. PHY_IMASK_DXMC | \
  55. PHY_IMASK_ADSC | \
  56. PHY_IMASK_ANC)
  57. #define PHY_FWV_REL_MASK BIT(15)
  58. #define PHY_FWV_MAJOR_MASK GENMASK(11, 8)
  59. #define PHY_FWV_MINOR_MASK GENMASK(7, 0)
  60. #define PHY_PMA_MGBT_POLARITY 0x82
  61. #define PHY_MDI_MDI_X_MASK GENMASK(1, 0)
  62. #define PHY_MDI_MDI_X_NORMAL 0x3
  63. #define PHY_MDI_MDI_X_AB 0x2
  64. #define PHY_MDI_MDI_X_CD 0x1
  65. #define PHY_MDI_MDI_X_CROSS 0x0
  66. /* SGMII */
  67. #define VSPEC1_SGMII_CTRL 0x08
  68. #define VSPEC1_SGMII_CTRL_ANEN BIT(12) /* Aneg enable */
  69. #define VSPEC1_SGMII_CTRL_ANRS BIT(9) /* Restart Aneg */
  70. #define VSPEC1_SGMII_ANEN_ANRS (VSPEC1_SGMII_CTRL_ANEN | \
  71. VSPEC1_SGMII_CTRL_ANRS)
  72. /* Temperature sensor */
  73. #define VSPEC1_TEMP_STA 0x0E
  74. #define VSPEC1_TEMP_STA_DATA GENMASK(9, 0)
  75. /* Mailbox */
  76. #define VSPEC1_MBOX_DATA 0x5
  77. #define VSPEC1_MBOX_ADDRLO 0x6
  78. #define VSPEC1_MBOX_CMD 0x7
  79. #define VSPEC1_MBOX_CMD_ADDRHI GENMASK(7, 0)
  80. #define VSPEC1_MBOX_CMD_RD (0 << 8)
  81. #define VSPEC1_MBOX_CMD_READY BIT(15)
  82. /* WoL */
  83. #define VPSPEC2_WOL_CTL 0x0E06
  84. #define VPSPEC2_WOL_AD01 0x0E08
  85. #define VPSPEC2_WOL_AD23 0x0E09
  86. #define VPSPEC2_WOL_AD45 0x0E0A
  87. #define WOL_EN BIT(0)
  88. /* Internal registers, access via mbox */
  89. #define REG_GPIO0_OUT 0xd3ce00
  90. struct gpy_priv {
  91. /* serialize mailbox acesses */
  92. struct mutex mbox_lock;
  93. u8 fw_major;
  94. u8 fw_minor;
  95. u32 wolopts;
  96. /* It takes 3 seconds to fully switch out of loopback mode before
  97. * it can safely re-enter loopback mode. Record the time when
  98. * loopback is disabled. Check and wait if necessary before loopback
  99. * is enabled.
  100. */
  101. u64 lb_dis_to;
  102. };
  103. static const struct {
  104. int major;
  105. int minor;
  106. } ver_need_sgmii_reaneg[] = {
  107. {7, 0x6D},
  108. {8, 0x6D},
  109. {9, 0x73},
  110. };
  111. #if IS_ENABLED(CONFIG_HWMON)
  112. /* The original translation formulae of the temperature (in degrees of Celsius)
  113. * are as follows:
  114. *
  115. * T = -2.5761e-11*(N^4) + 9.7332e-8*(N^3) + -1.9165e-4*(N^2) +
  116. * 3.0762e-1*(N^1) + -5.2156e1
  117. *
  118. * where [-52.156, 137.961]C and N = [0, 1023].
  119. *
  120. * They must be accordingly altered to be suitable for the integer arithmetics.
  121. * The technique is called 'factor redistribution', which just makes sure the
  122. * multiplications and divisions are made so to have a result of the operations
  123. * within the integer numbers limit. In addition we need to translate the
  124. * formulae to accept millidegrees of Celsius. Here what it looks like after
  125. * the alterations:
  126. *
  127. * T = -25761e-12*(N^4) + 97332e-9*(N^3) + -191650e-6*(N^2) +
  128. * 307620e-3*(N^1) + -52156
  129. *
  130. * where T = [-52156, 137961]mC and N = [0, 1023].
  131. */
  132. static const struct polynomial poly_N_to_temp = {
  133. .terms = {
  134. {4, -25761, 1000, 1},
  135. {3, 97332, 1000, 1},
  136. {2, -191650, 1000, 1},
  137. {1, 307620, 1000, 1},
  138. {0, -52156, 1, 1}
  139. }
  140. };
  141. static int gpy_hwmon_read(struct device *dev,
  142. enum hwmon_sensor_types type,
  143. u32 attr, int channel, long *value)
  144. {
  145. struct phy_device *phydev = dev_get_drvdata(dev);
  146. int ret;
  147. ret = phy_read_mmd(phydev, MDIO_MMD_VEND1, VSPEC1_TEMP_STA);
  148. if (ret < 0)
  149. return ret;
  150. if (!ret)
  151. return -ENODATA;
  152. *value = polynomial_calc(&poly_N_to_temp,
  153. FIELD_GET(VSPEC1_TEMP_STA_DATA, ret));
  154. return 0;
  155. }
  156. static umode_t gpy_hwmon_is_visible(const void *data,
  157. enum hwmon_sensor_types type,
  158. u32 attr, int channel)
  159. {
  160. return 0444;
  161. }
  162. static const struct hwmon_channel_info * const gpy_hwmon_info[] = {
  163. HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT),
  164. NULL
  165. };
  166. static const struct hwmon_ops gpy_hwmon_hwmon_ops = {
  167. .is_visible = gpy_hwmon_is_visible,
  168. .read = gpy_hwmon_read,
  169. };
  170. static const struct hwmon_chip_info gpy_hwmon_chip_info = {
  171. .ops = &gpy_hwmon_hwmon_ops,
  172. .info = gpy_hwmon_info,
  173. };
  174. static int gpy_hwmon_register(struct phy_device *phydev)
  175. {
  176. struct device *dev = &phydev->mdio.dev;
  177. struct device *hwmon_dev;
  178. char *hwmon_name;
  179. hwmon_name = devm_hwmon_sanitize_name(dev, dev_name(dev));
  180. if (IS_ERR(hwmon_name))
  181. return PTR_ERR(hwmon_name);
  182. hwmon_dev = devm_hwmon_device_register_with_info(dev, hwmon_name,
  183. phydev,
  184. &gpy_hwmon_chip_info,
  185. NULL);
  186. return PTR_ERR_OR_ZERO(hwmon_dev);
  187. }
  188. #else
  189. static int gpy_hwmon_register(struct phy_device *phydev)
  190. {
  191. return 0;
  192. }
  193. #endif
  194. static int gpy_ack_interrupt(struct phy_device *phydev)
  195. {
  196. int ret;
  197. /* Clear all pending interrupts */
  198. ret = phy_read(phydev, PHY_ISTAT);
  199. return ret < 0 ? ret : 0;
  200. }
  201. static int gpy_mbox_read(struct phy_device *phydev, u32 addr)
  202. {
  203. struct gpy_priv *priv = phydev->priv;
  204. int val, ret;
  205. u16 cmd;
  206. mutex_lock(&priv->mbox_lock);
  207. ret = phy_write_mmd(phydev, MDIO_MMD_VEND1, VSPEC1_MBOX_ADDRLO,
  208. addr);
  209. if (ret)
  210. goto out;
  211. cmd = VSPEC1_MBOX_CMD_RD;
  212. cmd |= FIELD_PREP(VSPEC1_MBOX_CMD_ADDRHI, addr >> 16);
  213. ret = phy_write_mmd(phydev, MDIO_MMD_VEND1, VSPEC1_MBOX_CMD, cmd);
  214. if (ret)
  215. goto out;
  216. /* The mbox read is used in the interrupt workaround. It was observed
  217. * that a read might take up to 2.5ms. This is also the time for which
  218. * the interrupt line is stuck low. To be on the safe side, poll the
  219. * ready bit for 10ms.
  220. */
  221. ret = phy_read_mmd_poll_timeout(phydev, MDIO_MMD_VEND1,
  222. VSPEC1_MBOX_CMD, val,
  223. (val & VSPEC1_MBOX_CMD_READY),
  224. 500, 10000, false);
  225. if (ret)
  226. goto out;
  227. ret = phy_read_mmd(phydev, MDIO_MMD_VEND1, VSPEC1_MBOX_DATA);
  228. out:
  229. mutex_unlock(&priv->mbox_lock);
  230. return ret;
  231. }
  232. static int gpy_config_init(struct phy_device *phydev)
  233. {
  234. /* Nothing to configure. Configuration Requirement Placeholder */
  235. return 0;
  236. }
  237. static int gpy21x_config_init(struct phy_device *phydev)
  238. {
  239. __set_bit(PHY_INTERFACE_MODE_2500BASEX, phydev->possible_interfaces);
  240. __set_bit(PHY_INTERFACE_MODE_SGMII, phydev->possible_interfaces);
  241. return gpy_config_init(phydev);
  242. }
  243. static int gpy_probe(struct phy_device *phydev)
  244. {
  245. struct device *dev = &phydev->mdio.dev;
  246. struct gpy_priv *priv;
  247. int fw_version;
  248. int ret;
  249. if (!phydev->is_c45) {
  250. ret = phy_get_c45_ids(phydev);
  251. if (ret < 0)
  252. return ret;
  253. }
  254. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  255. if (!priv)
  256. return -ENOMEM;
  257. phydev->priv = priv;
  258. mutex_init(&priv->mbox_lock);
  259. if (!device_property_present(dev, "maxlinear,use-broken-interrupts"))
  260. phydev->dev_flags |= PHY_F_NO_IRQ;
  261. fw_version = phy_read(phydev, PHY_FWV);
  262. if (fw_version < 0)
  263. return fw_version;
  264. priv->fw_major = FIELD_GET(PHY_FWV_MAJOR_MASK, fw_version);
  265. priv->fw_minor = FIELD_GET(PHY_FWV_MINOR_MASK, fw_version);
  266. ret = gpy_hwmon_register(phydev);
  267. if (ret)
  268. return ret;
  269. /* Show GPY PHY FW version in dmesg */
  270. phydev_info(phydev, "Firmware Version: %d.%d (0x%04X%s)\n",
  271. priv->fw_major, priv->fw_minor, fw_version,
  272. fw_version & PHY_FWV_REL_MASK ? "" : " test version");
  273. return 0;
  274. }
  275. static bool gpy_sgmii_need_reaneg(struct phy_device *phydev)
  276. {
  277. struct gpy_priv *priv = phydev->priv;
  278. size_t i;
  279. for (i = 0; i < ARRAY_SIZE(ver_need_sgmii_reaneg); i++) {
  280. if (priv->fw_major != ver_need_sgmii_reaneg[i].major)
  281. continue;
  282. if (priv->fw_minor < ver_need_sgmii_reaneg[i].minor)
  283. return true;
  284. break;
  285. }
  286. return false;
  287. }
  288. static bool gpy_2500basex_chk(struct phy_device *phydev)
  289. {
  290. int ret;
  291. ret = phy_read(phydev, PHY_MIISTAT);
  292. if (ret < 0) {
  293. phydev_err(phydev, "Error: MDIO register access failed: %d\n",
  294. ret);
  295. return false;
  296. }
  297. if (!(ret & PHY_MIISTAT_LS) ||
  298. FIELD_GET(PHY_MIISTAT_SPD_MASK, ret) != PHY_MIISTAT_SPD_2500)
  299. return false;
  300. phydev->speed = SPEED_2500;
  301. phydev->interface = PHY_INTERFACE_MODE_2500BASEX;
  302. phy_modify_mmd(phydev, MDIO_MMD_VEND1, VSPEC1_SGMII_CTRL,
  303. VSPEC1_SGMII_CTRL_ANEN, 0);
  304. return true;
  305. }
  306. static bool gpy_sgmii_aneg_en(struct phy_device *phydev)
  307. {
  308. int ret;
  309. ret = phy_read_mmd(phydev, MDIO_MMD_VEND1, VSPEC1_SGMII_CTRL);
  310. if (ret < 0) {
  311. phydev_err(phydev, "Error: MMD register access failed: %d\n",
  312. ret);
  313. return true;
  314. }
  315. return (ret & VSPEC1_SGMII_CTRL_ANEN) ? true : false;
  316. }
  317. static int gpy_config_mdix(struct phy_device *phydev, u8 ctrl)
  318. {
  319. int ret;
  320. u16 val;
  321. switch (ctrl) {
  322. case ETH_TP_MDI_AUTO:
  323. val = PHY_CTL1_AMDIX;
  324. break;
  325. case ETH_TP_MDI_X:
  326. val = (PHY_CTL1_MDIAB | PHY_CTL1_MDICD);
  327. break;
  328. case ETH_TP_MDI:
  329. val = 0;
  330. break;
  331. default:
  332. return 0;
  333. }
  334. ret = phy_modify(phydev, PHY_CTL1, PHY_CTL1_AMDIX | PHY_CTL1_MDIAB |
  335. PHY_CTL1_MDICD, val);
  336. if (ret < 0)
  337. return ret;
  338. return genphy_c45_restart_aneg(phydev);
  339. }
  340. static int gpy_config_aneg(struct phy_device *phydev)
  341. {
  342. bool changed = false;
  343. u32 adv;
  344. int ret;
  345. if (phydev->autoneg == AUTONEG_DISABLE) {
  346. /* Configure half duplex with genphy_setup_forced,
  347. * because genphy_c45_pma_setup_forced does not support.
  348. */
  349. return phydev->duplex != DUPLEX_FULL
  350. ? genphy_setup_forced(phydev)
  351. : genphy_c45_pma_setup_forced(phydev);
  352. }
  353. ret = gpy_config_mdix(phydev, phydev->mdix_ctrl);
  354. if (ret < 0)
  355. return ret;
  356. ret = genphy_c45_an_config_aneg(phydev);
  357. if (ret < 0)
  358. return ret;
  359. if (ret > 0)
  360. changed = true;
  361. adv = linkmode_adv_to_mii_ctrl1000_t(phydev->advertising);
  362. ret = phy_modify_changed(phydev, MII_CTRL1000,
  363. ADVERTISE_1000FULL | ADVERTISE_1000HALF,
  364. adv);
  365. if (ret < 0)
  366. return ret;
  367. if (ret > 0)
  368. changed = true;
  369. ret = genphy_c45_check_and_restart_aneg(phydev, changed);
  370. if (ret < 0)
  371. return ret;
  372. if (phydev->interface == PHY_INTERFACE_MODE_USXGMII ||
  373. phydev->interface == PHY_INTERFACE_MODE_INTERNAL)
  374. return 0;
  375. /* No need to trigger re-ANEG if link speed is 2.5G or SGMII ANEG is
  376. * disabled.
  377. */
  378. if (!gpy_sgmii_need_reaneg(phydev) || gpy_2500basex_chk(phydev) ||
  379. !gpy_sgmii_aneg_en(phydev))
  380. return 0;
  381. /* There is a design constraint in GPY2xx device where SGMII AN is
  382. * only triggered when there is change of speed. If, PHY link
  383. * partner`s speed is still same even after PHY TPI is down and up
  384. * again, SGMII AN is not triggered and hence no new in-band message
  385. * from GPY to MAC side SGMII.
  386. * This could cause an issue during power up, when PHY is up prior to
  387. * MAC. At this condition, once MAC side SGMII is up, MAC side SGMII
  388. * wouldn`t receive new in-band message from GPY with correct link
  389. * status, speed and duplex info.
  390. *
  391. * 1) If PHY is already up and TPI link status is still down (such as
  392. * hard reboot), TPI link status is polled for 4 seconds before
  393. * retriggerring SGMII AN.
  394. * 2) If PHY is already up and TPI link status is also up (such as soft
  395. * reboot), polling of TPI link status is not needed and SGMII AN is
  396. * immediately retriggered.
  397. * 3) Other conditions such as PHY is down, speed change etc, skip
  398. * retriggering SGMII AN. Note: in case of speed change, GPY FW will
  399. * initiate SGMII AN.
  400. */
  401. if (phydev->state != PHY_UP)
  402. return 0;
  403. ret = phy_read_poll_timeout(phydev, MII_BMSR, ret, ret & BMSR_LSTATUS,
  404. 20000, 4000000, false);
  405. if (ret == -ETIMEDOUT)
  406. return 0;
  407. else if (ret < 0)
  408. return ret;
  409. /* Trigger SGMII AN. */
  410. return phy_modify_mmd(phydev, MDIO_MMD_VEND1, VSPEC1_SGMII_CTRL,
  411. VSPEC1_SGMII_CTRL_ANRS, VSPEC1_SGMII_CTRL_ANRS);
  412. }
  413. static int gpy_update_mdix(struct phy_device *phydev)
  414. {
  415. int ret;
  416. ret = phy_read(phydev, PHY_CTL1);
  417. if (ret < 0)
  418. return ret;
  419. if (ret & PHY_CTL1_AMDIX)
  420. phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
  421. else
  422. if (ret & PHY_CTL1_MDICD || ret & PHY_CTL1_MDIAB)
  423. phydev->mdix_ctrl = ETH_TP_MDI_X;
  424. else
  425. phydev->mdix_ctrl = ETH_TP_MDI;
  426. ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, PHY_PMA_MGBT_POLARITY);
  427. if (ret < 0)
  428. return ret;
  429. if ((ret & PHY_MDI_MDI_X_MASK) < PHY_MDI_MDI_X_NORMAL)
  430. phydev->mdix = ETH_TP_MDI_X;
  431. else
  432. phydev->mdix = ETH_TP_MDI;
  433. return 0;
  434. }
  435. static int gpy_update_interface(struct phy_device *phydev)
  436. {
  437. int ret;
  438. /* Interface mode is fixed for USXGMII and integrated PHY */
  439. if (phydev->interface == PHY_INTERFACE_MODE_USXGMII ||
  440. phydev->interface == PHY_INTERFACE_MODE_INTERNAL)
  441. return -EINVAL;
  442. /* Automatically switch SERDES interface between SGMII and 2500-BaseX
  443. * according to speed. Disable ANEG in 2500-BaseX mode.
  444. */
  445. switch (phydev->speed) {
  446. case SPEED_2500:
  447. phydev->interface = PHY_INTERFACE_MODE_2500BASEX;
  448. ret = phy_modify_mmd(phydev, MDIO_MMD_VEND1, VSPEC1_SGMII_CTRL,
  449. VSPEC1_SGMII_CTRL_ANEN, 0);
  450. if (ret < 0) {
  451. phydev_err(phydev,
  452. "Error: Disable of SGMII ANEG failed: %d\n",
  453. ret);
  454. return ret;
  455. }
  456. break;
  457. case SPEED_1000:
  458. case SPEED_100:
  459. case SPEED_10:
  460. phydev->interface = PHY_INTERFACE_MODE_SGMII;
  461. if (gpy_sgmii_aneg_en(phydev))
  462. break;
  463. /* Enable and restart SGMII ANEG for 10/100/1000Mbps link speed
  464. * if ANEG is disabled (in 2500-BaseX mode).
  465. */
  466. ret = phy_modify_mmd(phydev, MDIO_MMD_VEND1, VSPEC1_SGMII_CTRL,
  467. VSPEC1_SGMII_ANEN_ANRS,
  468. VSPEC1_SGMII_ANEN_ANRS);
  469. if (ret < 0) {
  470. phydev_err(phydev,
  471. "Error: Enable of SGMII ANEG failed: %d\n",
  472. ret);
  473. return ret;
  474. }
  475. break;
  476. }
  477. if (phydev->speed == SPEED_2500 || phydev->speed == SPEED_1000) {
  478. ret = genphy_read_master_slave(phydev);
  479. if (ret < 0)
  480. return ret;
  481. }
  482. return gpy_update_mdix(phydev);
  483. }
  484. static int gpy_read_status(struct phy_device *phydev)
  485. {
  486. int ret;
  487. ret = genphy_update_link(phydev);
  488. if (ret)
  489. return ret;
  490. phydev->speed = SPEED_UNKNOWN;
  491. phydev->duplex = DUPLEX_UNKNOWN;
  492. phydev->pause = 0;
  493. phydev->asym_pause = 0;
  494. if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete) {
  495. ret = genphy_c45_read_lpa(phydev);
  496. if (ret < 0)
  497. return ret;
  498. /* Read the link partner's 1G advertisement */
  499. ret = phy_read(phydev, MII_STAT1000);
  500. if (ret < 0)
  501. return ret;
  502. mii_stat1000_mod_linkmode_lpa_t(phydev->lp_advertising, ret);
  503. } else if (phydev->autoneg == AUTONEG_DISABLE) {
  504. linkmode_zero(phydev->lp_advertising);
  505. }
  506. ret = phy_read(phydev, PHY_MIISTAT);
  507. if (ret < 0)
  508. return ret;
  509. phydev->link = (ret & PHY_MIISTAT_LS) ? 1 : 0;
  510. phydev->duplex = (ret & PHY_MIISTAT_DPX) ? DUPLEX_FULL : DUPLEX_HALF;
  511. switch (FIELD_GET(PHY_MIISTAT_SPD_MASK, ret)) {
  512. case PHY_MIISTAT_SPD_10:
  513. phydev->speed = SPEED_10;
  514. break;
  515. case PHY_MIISTAT_SPD_100:
  516. phydev->speed = SPEED_100;
  517. break;
  518. case PHY_MIISTAT_SPD_1000:
  519. phydev->speed = SPEED_1000;
  520. break;
  521. case PHY_MIISTAT_SPD_2500:
  522. phydev->speed = SPEED_2500;
  523. break;
  524. }
  525. if (phydev->link) {
  526. ret = gpy_update_interface(phydev);
  527. if (ret < 0)
  528. return ret;
  529. }
  530. return 0;
  531. }
  532. static int gpy_config_intr(struct phy_device *phydev)
  533. {
  534. struct gpy_priv *priv = phydev->priv;
  535. u16 mask = 0;
  536. int ret;
  537. ret = gpy_ack_interrupt(phydev);
  538. if (ret)
  539. return ret;
  540. if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
  541. mask = PHY_IMASK_MASK;
  542. if (priv->wolopts & WAKE_MAGIC)
  543. mask |= PHY_IMASK_WOL;
  544. if (priv->wolopts & WAKE_PHY)
  545. mask |= PHY_IMASK_LSTC;
  546. return phy_write(phydev, PHY_IMASK, mask);
  547. }
  548. static irqreturn_t gpy_handle_interrupt(struct phy_device *phydev)
  549. {
  550. int reg;
  551. reg = phy_read(phydev, PHY_ISTAT);
  552. if (reg < 0) {
  553. phy_error(phydev);
  554. return IRQ_NONE;
  555. }
  556. if (!(reg & PHY_IMASK_MASK))
  557. return IRQ_NONE;
  558. /* The PHY might leave the interrupt line asserted even after PHY_ISTAT
  559. * is read. To avoid interrupt storms, delay the interrupt handling as
  560. * long as the PHY drives the interrupt line. An internal bus read will
  561. * stall as long as the interrupt line is asserted, thus just read a
  562. * random register here.
  563. * Because we cannot access the internal bus at all while the interrupt
  564. * is driven by the PHY, there is no way to make the interrupt line
  565. * unstuck (e.g. by changing the pinmux to GPIO input) during that time
  566. * frame. Therefore, polling is the best we can do and won't do any more
  567. * harm.
  568. * It was observed that this bug happens on link state and link speed
  569. * changes independent of the firmware version.
  570. */
  571. if (reg & (PHY_IMASK_LSTC | PHY_IMASK_LSPC)) {
  572. reg = gpy_mbox_read(phydev, REG_GPIO0_OUT);
  573. if (reg < 0) {
  574. phy_error(phydev);
  575. return IRQ_NONE;
  576. }
  577. }
  578. phy_trigger_machine(phydev);
  579. return IRQ_HANDLED;
  580. }
  581. static int gpy_set_wol(struct phy_device *phydev,
  582. struct ethtool_wolinfo *wol)
  583. {
  584. struct net_device *attach_dev = phydev->attached_dev;
  585. struct gpy_priv *priv = phydev->priv;
  586. int ret;
  587. if (wol->wolopts & WAKE_MAGIC) {
  588. /* MAC address - Byte0:Byte1:Byte2:Byte3:Byte4:Byte5
  589. * VPSPEC2_WOL_AD45 = Byte0:Byte1
  590. * VPSPEC2_WOL_AD23 = Byte2:Byte3
  591. * VPSPEC2_WOL_AD01 = Byte4:Byte5
  592. */
  593. ret = phy_set_bits_mmd(phydev, MDIO_MMD_VEND2,
  594. VPSPEC2_WOL_AD45,
  595. ((attach_dev->dev_addr[0] << 8) |
  596. attach_dev->dev_addr[1]));
  597. if (ret < 0)
  598. return ret;
  599. ret = phy_set_bits_mmd(phydev, MDIO_MMD_VEND2,
  600. VPSPEC2_WOL_AD23,
  601. ((attach_dev->dev_addr[2] << 8) |
  602. attach_dev->dev_addr[3]));
  603. if (ret < 0)
  604. return ret;
  605. ret = phy_set_bits_mmd(phydev, MDIO_MMD_VEND2,
  606. VPSPEC2_WOL_AD01,
  607. ((attach_dev->dev_addr[4] << 8) |
  608. attach_dev->dev_addr[5]));
  609. if (ret < 0)
  610. return ret;
  611. /* Enable the WOL interrupt */
  612. ret = phy_write(phydev, PHY_IMASK, PHY_IMASK_WOL);
  613. if (ret < 0)
  614. return ret;
  615. /* Enable magic packet matching */
  616. ret = phy_set_bits_mmd(phydev, MDIO_MMD_VEND2,
  617. VPSPEC2_WOL_CTL,
  618. WOL_EN);
  619. if (ret < 0)
  620. return ret;
  621. /* Clear the interrupt status register.
  622. * Only WoL is enabled so clear all.
  623. */
  624. ret = phy_read(phydev, PHY_ISTAT);
  625. if (ret < 0)
  626. return ret;
  627. priv->wolopts |= WAKE_MAGIC;
  628. } else {
  629. /* Disable magic packet matching */
  630. ret = phy_clear_bits_mmd(phydev, MDIO_MMD_VEND2,
  631. VPSPEC2_WOL_CTL,
  632. WOL_EN);
  633. if (ret < 0)
  634. return ret;
  635. /* Disable the WOL interrupt */
  636. ret = phy_clear_bits(phydev, PHY_IMASK, PHY_IMASK_WOL);
  637. if (ret < 0)
  638. return ret;
  639. priv->wolopts &= ~WAKE_MAGIC;
  640. }
  641. if (wol->wolopts & WAKE_PHY) {
  642. /* Enable the link state change interrupt */
  643. ret = phy_set_bits(phydev, PHY_IMASK, PHY_IMASK_LSTC);
  644. if (ret < 0)
  645. return ret;
  646. /* Clear the interrupt status register */
  647. ret = phy_read(phydev, PHY_ISTAT);
  648. if (ret < 0)
  649. return ret;
  650. if (ret & (PHY_IMASK_MASK & ~PHY_IMASK_LSTC))
  651. phy_trigger_machine(phydev);
  652. priv->wolopts |= WAKE_PHY;
  653. return 0;
  654. }
  655. priv->wolopts &= ~WAKE_PHY;
  656. /* Disable the link state change interrupt */
  657. return phy_clear_bits(phydev, PHY_IMASK, PHY_IMASK_LSTC);
  658. }
  659. static void gpy_get_wol(struct phy_device *phydev,
  660. struct ethtool_wolinfo *wol)
  661. {
  662. struct gpy_priv *priv = phydev->priv;
  663. wol->supported = WAKE_MAGIC | WAKE_PHY;
  664. wol->wolopts = priv->wolopts;
  665. }
  666. static int gpy_loopback(struct phy_device *phydev, bool enable)
  667. {
  668. struct gpy_priv *priv = phydev->priv;
  669. u16 set = 0;
  670. int ret;
  671. if (enable) {
  672. u64 now = get_jiffies_64();
  673. /* wait until 3 seconds from last disable */
  674. if (time_before64(now, priv->lb_dis_to))
  675. msleep(jiffies64_to_msecs(priv->lb_dis_to - now));
  676. set = BMCR_LOOPBACK;
  677. }
  678. ret = phy_modify(phydev, MII_BMCR, BMCR_LOOPBACK, set);
  679. if (ret <= 0)
  680. return ret;
  681. if (enable) {
  682. /* It takes some time for PHY device to switch into
  683. * loopback mode.
  684. */
  685. msleep(100);
  686. } else {
  687. priv->lb_dis_to = get_jiffies_64() + HZ * 3;
  688. }
  689. return 0;
  690. }
  691. static int gpy115_loopback(struct phy_device *phydev, bool enable)
  692. {
  693. struct gpy_priv *priv = phydev->priv;
  694. if (enable)
  695. return gpy_loopback(phydev, enable);
  696. if (priv->fw_minor > 0x76)
  697. return gpy_loopback(phydev, 0);
  698. return genphy_soft_reset(phydev);
  699. }
  700. static struct phy_driver gpy_drivers[] = {
  701. {
  702. PHY_ID_MATCH_MODEL(PHY_ID_GPY2xx),
  703. .name = "Maxlinear Ethernet GPY2xx",
  704. .get_features = genphy_c45_pma_read_abilities,
  705. .config_init = gpy_config_init,
  706. .probe = gpy_probe,
  707. .suspend = genphy_suspend,
  708. .resume = genphy_resume,
  709. .config_aneg = gpy_config_aneg,
  710. .aneg_done = genphy_c45_aneg_done,
  711. .read_status = gpy_read_status,
  712. .config_intr = gpy_config_intr,
  713. .handle_interrupt = gpy_handle_interrupt,
  714. .set_wol = gpy_set_wol,
  715. .get_wol = gpy_get_wol,
  716. .set_loopback = gpy_loopback,
  717. },
  718. {
  719. .phy_id = PHY_ID_GPY115B,
  720. .phy_id_mask = PHY_ID_GPYx15B_MASK,
  721. .name = "Maxlinear Ethernet GPY115B",
  722. .get_features = genphy_c45_pma_read_abilities,
  723. .config_init = gpy_config_init,
  724. .probe = gpy_probe,
  725. .suspend = genphy_suspend,
  726. .resume = genphy_resume,
  727. .config_aneg = gpy_config_aneg,
  728. .aneg_done = genphy_c45_aneg_done,
  729. .read_status = gpy_read_status,
  730. .config_intr = gpy_config_intr,
  731. .handle_interrupt = gpy_handle_interrupt,
  732. .set_wol = gpy_set_wol,
  733. .get_wol = gpy_get_wol,
  734. .set_loopback = gpy115_loopback,
  735. },
  736. {
  737. PHY_ID_MATCH_MODEL(PHY_ID_GPY115C),
  738. .name = "Maxlinear Ethernet GPY115C",
  739. .get_features = genphy_c45_pma_read_abilities,
  740. .config_init = gpy_config_init,
  741. .probe = gpy_probe,
  742. .suspend = genphy_suspend,
  743. .resume = genphy_resume,
  744. .config_aneg = gpy_config_aneg,
  745. .aneg_done = genphy_c45_aneg_done,
  746. .read_status = gpy_read_status,
  747. .config_intr = gpy_config_intr,
  748. .handle_interrupt = gpy_handle_interrupt,
  749. .set_wol = gpy_set_wol,
  750. .get_wol = gpy_get_wol,
  751. .set_loopback = gpy115_loopback,
  752. },
  753. {
  754. .phy_id = PHY_ID_GPY211B,
  755. .phy_id_mask = PHY_ID_GPY21xB_MASK,
  756. .name = "Maxlinear Ethernet GPY211B",
  757. .get_features = genphy_c45_pma_read_abilities,
  758. .config_init = gpy21x_config_init,
  759. .probe = gpy_probe,
  760. .suspend = genphy_suspend,
  761. .resume = genphy_resume,
  762. .config_aneg = gpy_config_aneg,
  763. .aneg_done = genphy_c45_aneg_done,
  764. .read_status = gpy_read_status,
  765. .config_intr = gpy_config_intr,
  766. .handle_interrupt = gpy_handle_interrupt,
  767. .set_wol = gpy_set_wol,
  768. .get_wol = gpy_get_wol,
  769. .set_loopback = gpy_loopback,
  770. },
  771. {
  772. PHY_ID_MATCH_MODEL(PHY_ID_GPY211C),
  773. .name = "Maxlinear Ethernet GPY211C",
  774. .get_features = genphy_c45_pma_read_abilities,
  775. .config_init = gpy21x_config_init,
  776. .probe = gpy_probe,
  777. .suspend = genphy_suspend,
  778. .resume = genphy_resume,
  779. .config_aneg = gpy_config_aneg,
  780. .aneg_done = genphy_c45_aneg_done,
  781. .read_status = gpy_read_status,
  782. .config_intr = gpy_config_intr,
  783. .handle_interrupt = gpy_handle_interrupt,
  784. .set_wol = gpy_set_wol,
  785. .get_wol = gpy_get_wol,
  786. .set_loopback = gpy_loopback,
  787. },
  788. {
  789. .phy_id = PHY_ID_GPY212B,
  790. .phy_id_mask = PHY_ID_GPY21xB_MASK,
  791. .name = "Maxlinear Ethernet GPY212B",
  792. .get_features = genphy_c45_pma_read_abilities,
  793. .config_init = gpy21x_config_init,
  794. .probe = gpy_probe,
  795. .suspend = genphy_suspend,
  796. .resume = genphy_resume,
  797. .config_aneg = gpy_config_aneg,
  798. .aneg_done = genphy_c45_aneg_done,
  799. .read_status = gpy_read_status,
  800. .config_intr = gpy_config_intr,
  801. .handle_interrupt = gpy_handle_interrupt,
  802. .set_wol = gpy_set_wol,
  803. .get_wol = gpy_get_wol,
  804. .set_loopback = gpy_loopback,
  805. },
  806. {
  807. PHY_ID_MATCH_MODEL(PHY_ID_GPY212C),
  808. .name = "Maxlinear Ethernet GPY212C",
  809. .get_features = genphy_c45_pma_read_abilities,
  810. .config_init = gpy21x_config_init,
  811. .probe = gpy_probe,
  812. .suspend = genphy_suspend,
  813. .resume = genphy_resume,
  814. .config_aneg = gpy_config_aneg,
  815. .aneg_done = genphy_c45_aneg_done,
  816. .read_status = gpy_read_status,
  817. .config_intr = gpy_config_intr,
  818. .handle_interrupt = gpy_handle_interrupt,
  819. .set_wol = gpy_set_wol,
  820. .get_wol = gpy_get_wol,
  821. .set_loopback = gpy_loopback,
  822. },
  823. {
  824. .phy_id = PHY_ID_GPY215B,
  825. .phy_id_mask = PHY_ID_GPYx15B_MASK,
  826. .name = "Maxlinear Ethernet GPY215B",
  827. .get_features = genphy_c45_pma_read_abilities,
  828. .config_init = gpy21x_config_init,
  829. .probe = gpy_probe,
  830. .suspend = genphy_suspend,
  831. .resume = genphy_resume,
  832. .config_aneg = gpy_config_aneg,
  833. .aneg_done = genphy_c45_aneg_done,
  834. .read_status = gpy_read_status,
  835. .config_intr = gpy_config_intr,
  836. .handle_interrupt = gpy_handle_interrupt,
  837. .set_wol = gpy_set_wol,
  838. .get_wol = gpy_get_wol,
  839. .set_loopback = gpy_loopback,
  840. },
  841. {
  842. PHY_ID_MATCH_MODEL(PHY_ID_GPY215C),
  843. .name = "Maxlinear Ethernet GPY215C",
  844. .get_features = genphy_c45_pma_read_abilities,
  845. .config_init = gpy21x_config_init,
  846. .probe = gpy_probe,
  847. .suspend = genphy_suspend,
  848. .resume = genphy_resume,
  849. .config_aneg = gpy_config_aneg,
  850. .aneg_done = genphy_c45_aneg_done,
  851. .read_status = gpy_read_status,
  852. .config_intr = gpy_config_intr,
  853. .handle_interrupt = gpy_handle_interrupt,
  854. .set_wol = gpy_set_wol,
  855. .get_wol = gpy_get_wol,
  856. .set_loopback = gpy_loopback,
  857. },
  858. {
  859. PHY_ID_MATCH_MODEL(PHY_ID_GPY241B),
  860. .name = "Maxlinear Ethernet GPY241B",
  861. .get_features = genphy_c45_pma_read_abilities,
  862. .config_init = gpy_config_init,
  863. .probe = gpy_probe,
  864. .suspend = genphy_suspend,
  865. .resume = genphy_resume,
  866. .config_aneg = gpy_config_aneg,
  867. .aneg_done = genphy_c45_aneg_done,
  868. .read_status = gpy_read_status,
  869. .config_intr = gpy_config_intr,
  870. .handle_interrupt = gpy_handle_interrupt,
  871. .set_wol = gpy_set_wol,
  872. .get_wol = gpy_get_wol,
  873. .set_loopback = gpy_loopback,
  874. },
  875. {
  876. PHY_ID_MATCH_MODEL(PHY_ID_GPY241BM),
  877. .name = "Maxlinear Ethernet GPY241BM",
  878. .get_features = genphy_c45_pma_read_abilities,
  879. .config_init = gpy_config_init,
  880. .probe = gpy_probe,
  881. .suspend = genphy_suspend,
  882. .resume = genphy_resume,
  883. .config_aneg = gpy_config_aneg,
  884. .aneg_done = genphy_c45_aneg_done,
  885. .read_status = gpy_read_status,
  886. .config_intr = gpy_config_intr,
  887. .handle_interrupt = gpy_handle_interrupt,
  888. .set_wol = gpy_set_wol,
  889. .get_wol = gpy_get_wol,
  890. .set_loopback = gpy_loopback,
  891. },
  892. {
  893. PHY_ID_MATCH_MODEL(PHY_ID_GPY245B),
  894. .name = "Maxlinear Ethernet GPY245B",
  895. .get_features = genphy_c45_pma_read_abilities,
  896. .config_init = gpy_config_init,
  897. .probe = gpy_probe,
  898. .suspend = genphy_suspend,
  899. .resume = genphy_resume,
  900. .config_aneg = gpy_config_aneg,
  901. .aneg_done = genphy_c45_aneg_done,
  902. .read_status = gpy_read_status,
  903. .config_intr = gpy_config_intr,
  904. .handle_interrupt = gpy_handle_interrupt,
  905. .set_wol = gpy_set_wol,
  906. .get_wol = gpy_get_wol,
  907. .set_loopback = gpy_loopback,
  908. },
  909. };
  910. module_phy_driver(gpy_drivers);
  911. static struct mdio_device_id __maybe_unused gpy_tbl[] = {
  912. {PHY_ID_MATCH_MODEL(PHY_ID_GPY2xx)},
  913. {PHY_ID_GPY115B, PHY_ID_GPYx15B_MASK},
  914. {PHY_ID_MATCH_MODEL(PHY_ID_GPY115C)},
  915. {PHY_ID_GPY211B, PHY_ID_GPY21xB_MASK},
  916. {PHY_ID_MATCH_MODEL(PHY_ID_GPY211C)},
  917. {PHY_ID_GPY212B, PHY_ID_GPY21xB_MASK},
  918. {PHY_ID_MATCH_MODEL(PHY_ID_GPY212C)},
  919. {PHY_ID_GPY215B, PHY_ID_GPYx15B_MASK},
  920. {PHY_ID_MATCH_MODEL(PHY_ID_GPY215C)},
  921. {PHY_ID_MATCH_MODEL(PHY_ID_GPY241B)},
  922. {PHY_ID_MATCH_MODEL(PHY_ID_GPY241BM)},
  923. {PHY_ID_MATCH_MODEL(PHY_ID_GPY245B)},
  924. { }
  925. };
  926. MODULE_DEVICE_TABLE(mdio, gpy_tbl);
  927. MODULE_DESCRIPTION("Maxlinear Ethernet GPY Driver");
  928. MODULE_AUTHOR("Xu Liang");
  929. MODULE_LICENSE("GPL");