mxl-gpy.c 25 KB

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