smsc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * drivers/net/phy/smsc.c
  4. *
  5. * Driver for SMSC PHYs
  6. *
  7. * Author: Herbert Valerio Riedel
  8. *
  9. * Copyright (c) 2006 Herbert Valerio Riedel <hvr@gnu.org>
  10. *
  11. * Support added for SMSC LAN8187 and LAN8700 by steve.glendinning@shawell.net
  12. *
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/mii.h>
  18. #include <linux/ethtool.h>
  19. #include <linux/of.h>
  20. #include <linux/phy.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/crc16.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/smscphy.h>
  25. /* Vendor-specific PHY Definitions */
  26. /* EDPD NLP / crossover time configuration */
  27. #define PHY_EDPD_CONFIG 16
  28. #define PHY_EDPD_CONFIG_EXT_CROSSOVER_ 0x0001
  29. /* Control/Status Indication Register */
  30. #define SPECIAL_CTRL_STS 27
  31. #define SPECIAL_CTRL_STS_OVRRD_AMDIX_ 0x8000
  32. #define SPECIAL_CTRL_STS_AMDIX_ENABLE_ 0x4000
  33. #define SPECIAL_CTRL_STS_AMDIX_STATE_ 0x2000
  34. #define EDPD_MAX_WAIT_DFLT_MS 640
  35. /* interval between phylib state machine runs in ms */
  36. #define PHY_STATE_MACH_MS 1000
  37. struct smsc_hw_stat {
  38. const char *string;
  39. u8 reg;
  40. u8 bits;
  41. };
  42. static struct smsc_hw_stat smsc_hw_stats[] = {
  43. { "phy_symbol_errors", 26, 16},
  44. };
  45. struct smsc_phy_priv {
  46. unsigned int edpd_enable:1;
  47. unsigned int edpd_mode_set_by_user:1;
  48. unsigned int edpd_max_wait_ms;
  49. bool wol_arp;
  50. };
  51. static int smsc_phy_ack_interrupt(struct phy_device *phydev)
  52. {
  53. int rc = phy_read(phydev, MII_LAN83C185_ISF);
  54. return rc < 0 ? rc : 0;
  55. }
  56. int smsc_phy_config_intr(struct phy_device *phydev)
  57. {
  58. int rc;
  59. if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
  60. rc = smsc_phy_ack_interrupt(phydev);
  61. if (rc)
  62. return rc;
  63. rc = phy_write(phydev, MII_LAN83C185_IM,
  64. MII_LAN83C185_ISF_INT_PHYLIB_EVENTS);
  65. } else {
  66. rc = phy_write(phydev, MII_LAN83C185_IM, 0);
  67. if (rc)
  68. return rc;
  69. rc = smsc_phy_ack_interrupt(phydev);
  70. }
  71. return rc < 0 ? rc : 0;
  72. }
  73. EXPORT_SYMBOL_GPL(smsc_phy_config_intr);
  74. static int smsc_phy_config_edpd(struct phy_device *phydev)
  75. {
  76. struct smsc_phy_priv *priv = phydev->priv;
  77. if (priv->edpd_enable)
  78. return phy_set_bits(phydev, MII_LAN83C185_CTRL_STATUS,
  79. MII_LAN83C185_EDPWRDOWN);
  80. else
  81. return phy_clear_bits(phydev, MII_LAN83C185_CTRL_STATUS,
  82. MII_LAN83C185_EDPWRDOWN);
  83. }
  84. irqreturn_t smsc_phy_handle_interrupt(struct phy_device *phydev)
  85. {
  86. int irq_status;
  87. irq_status = phy_read(phydev, MII_LAN83C185_ISF);
  88. if (irq_status < 0) {
  89. if (irq_status != -ENODEV)
  90. phy_error(phydev);
  91. return IRQ_NONE;
  92. }
  93. if (!(irq_status & MII_LAN83C185_ISF_INT_PHYLIB_EVENTS))
  94. return IRQ_NONE;
  95. phy_trigger_machine(phydev);
  96. return IRQ_HANDLED;
  97. }
  98. EXPORT_SYMBOL_GPL(smsc_phy_handle_interrupt);
  99. int smsc_phy_config_init(struct phy_device *phydev)
  100. {
  101. struct smsc_phy_priv *priv = phydev->priv;
  102. if (!priv)
  103. return 0;
  104. /* don't use EDPD in irq mode except overridden by user */
  105. if (!priv->edpd_mode_set_by_user && phydev->irq != PHY_POLL)
  106. priv->edpd_enable = false;
  107. return smsc_phy_config_edpd(phydev);
  108. }
  109. EXPORT_SYMBOL_GPL(smsc_phy_config_init);
  110. static int smsc_phy_reset(struct phy_device *phydev)
  111. {
  112. int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
  113. if (rc < 0)
  114. return rc;
  115. /* If the SMSC PHY is in power down mode, then set it
  116. * in all capable mode before using it.
  117. */
  118. if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
  119. /* set "all capable" mode */
  120. rc |= MII_LAN83C185_MODE_ALL;
  121. phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
  122. }
  123. /* reset the phy */
  124. return genphy_soft_reset(phydev);
  125. }
  126. static int lan87xx_config_aneg(struct phy_device *phydev)
  127. {
  128. u8 mdix_ctrl;
  129. int val;
  130. int rc;
  131. /* When auto-negotiation is disabled (forced mode), the PHY's
  132. * Auto-MDIX will continue toggling the TX/RX pairs.
  133. *
  134. * To establish a stable link, we must select a fixed MDI mode.
  135. * If the user has not specified a fixed MDI mode (i.e., mdix_ctrl is
  136. * 'auto'), we default to ETH_TP_MDI. This choice of a ETH_TP_MDI mode
  137. * mirrors the behavior the hardware would exhibit if the AUTOMDIX_EN
  138. * strap were configured for a fixed MDI connection.
  139. */
  140. if (phydev->autoneg == AUTONEG_DISABLE) {
  141. if (phydev->mdix_ctrl == ETH_TP_MDI_AUTO)
  142. mdix_ctrl = ETH_TP_MDI;
  143. else
  144. mdix_ctrl = phydev->mdix_ctrl;
  145. } else {
  146. mdix_ctrl = phydev->mdix_ctrl;
  147. }
  148. switch (mdix_ctrl) {
  149. case ETH_TP_MDI:
  150. val = SPECIAL_CTRL_STS_OVRRD_AMDIX_;
  151. break;
  152. case ETH_TP_MDI_X:
  153. val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
  154. SPECIAL_CTRL_STS_AMDIX_STATE_;
  155. break;
  156. case ETH_TP_MDI_AUTO:
  157. val = SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
  158. SPECIAL_CTRL_STS_AMDIX_ENABLE_;
  159. break;
  160. default:
  161. return genphy_config_aneg(phydev);
  162. }
  163. rc = phy_read(phydev, SPECIAL_CTRL_STS);
  164. if (rc < 0)
  165. return rc;
  166. rc &= ~(SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
  167. SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
  168. SPECIAL_CTRL_STS_AMDIX_STATE_);
  169. rc |= val;
  170. phy_write(phydev, SPECIAL_CTRL_STS, rc);
  171. phydev->mdix = mdix_ctrl;
  172. return genphy_config_aneg(phydev);
  173. }
  174. static int lan95xx_config_aneg_ext(struct phy_device *phydev)
  175. {
  176. if (phydev->phy_id == 0x0007c0f0) { /* LAN9500A or LAN9505A */
  177. /* Extend Manual AutoMDIX timer */
  178. int rc = phy_set_bits(phydev, PHY_EDPD_CONFIG,
  179. PHY_EDPD_CONFIG_EXT_CROSSOVER_);
  180. if (rc < 0)
  181. return rc;
  182. }
  183. return lan87xx_config_aneg(phydev);
  184. }
  185. /*
  186. * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
  187. * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
  188. * unstable detection of plugging in Ethernet cable.
  189. * This workaround disables Energy Detect Power-Down mode and waiting for
  190. * response on link pulses to detect presence of plugged Ethernet cable.
  191. * The Energy Detect Power-Down mode is enabled again in the end of procedure to
  192. * save approximately 220 mW of power if cable is unplugged.
  193. * The workaround is only applicable to poll mode. Energy Detect Power-Down may
  194. * not be used in interrupt mode lest link change detection becomes unreliable.
  195. */
  196. int lan87xx_read_status(struct phy_device *phydev)
  197. {
  198. struct smsc_phy_priv *priv = phydev->priv;
  199. int err;
  200. err = genphy_read_status(phydev);
  201. if (err)
  202. return err;
  203. if (!phydev->link && priv && priv->edpd_enable &&
  204. priv->edpd_max_wait_ms) {
  205. unsigned int max_wait = priv->edpd_max_wait_ms * 1000;
  206. int rc;
  207. /* Disable EDPD to wake up PHY */
  208. rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  209. if (rc < 0)
  210. return rc;
  211. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  212. rc & ~MII_LAN83C185_EDPWRDOWN);
  213. if (rc < 0)
  214. return rc;
  215. /* Wait max 640 ms to detect energy and the timeout is not
  216. * an actual error.
  217. */
  218. read_poll_timeout(phy_read, rc,
  219. rc & MII_LAN83C185_ENERGYON || rc < 0,
  220. 10000, max_wait, true, phydev,
  221. MII_LAN83C185_CTRL_STATUS);
  222. if (rc < 0)
  223. return rc;
  224. /* Re-enable EDPD */
  225. rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  226. if (rc < 0)
  227. return rc;
  228. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  229. rc | MII_LAN83C185_EDPWRDOWN);
  230. if (rc < 0)
  231. return rc;
  232. }
  233. return err;
  234. }
  235. EXPORT_SYMBOL_GPL(lan87xx_read_status);
  236. static int lan87xx_phy_config_init(struct phy_device *phydev)
  237. {
  238. int rc;
  239. /* The LAN87xx PHY's initial MDI-X mode is determined by the AUTOMDIX_EN
  240. * hardware strap, but the driver cannot read the strap's status. This
  241. * creates an unpredictable initial state.
  242. *
  243. * To ensure consistent and reliable behavior across all boards,
  244. * override the strap configuration on initialization and force the PHY
  245. * into a known state with Auto-MDIX enabled, which is the expected
  246. * default for modern hardware.
  247. */
  248. rc = phy_modify(phydev, SPECIAL_CTRL_STS,
  249. SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
  250. SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
  251. SPECIAL_CTRL_STS_AMDIX_STATE_,
  252. SPECIAL_CTRL_STS_OVRRD_AMDIX_ |
  253. SPECIAL_CTRL_STS_AMDIX_ENABLE_);
  254. if (rc < 0)
  255. return rc;
  256. phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
  257. return smsc_phy_config_init(phydev);
  258. }
  259. static int lan874x_phy_config_init(struct phy_device *phydev)
  260. {
  261. u16 val;
  262. int rc;
  263. /* Setup LED2/nINT/nPME pin to function as nPME. May need user option
  264. * to use LED1/nINT/nPME.
  265. */
  266. val = MII_LAN874X_PHY_PME2_SET;
  267. /* The bits MII_LAN874X_PHY_WOL_PFDA_FR, MII_LAN874X_PHY_WOL_WUFR,
  268. * MII_LAN874X_PHY_WOL_MPR, and MII_LAN874X_PHY_WOL_BCAST_FR need to
  269. * be cleared to de-assert PME signal after a WoL event happens, but
  270. * using PME auto clear gets around that.
  271. */
  272. val |= MII_LAN874X_PHY_PME_SELF_CLEAR;
  273. rc = phy_write_mmd(phydev, MDIO_MMD_PCS, MII_LAN874X_PHY_MMD_WOL_WUCSR,
  274. val);
  275. if (rc < 0)
  276. return rc;
  277. /* set nPME self clear delay time */
  278. rc = phy_write_mmd(phydev, MDIO_MMD_PCS, MII_LAN874X_PHY_MMD_MCFGR,
  279. MII_LAN874X_PHY_PME_SELF_CLEAR_DELAY);
  280. if (rc < 0)
  281. return rc;
  282. return smsc_phy_config_init(phydev);
  283. }
  284. static void lan874x_get_wol(struct phy_device *phydev,
  285. struct ethtool_wolinfo *wol)
  286. {
  287. struct smsc_phy_priv *priv = phydev->priv;
  288. int rc;
  289. wol->supported = (WAKE_UCAST | WAKE_BCAST | WAKE_MAGIC |
  290. WAKE_ARP | WAKE_MCAST);
  291. wol->wolopts = 0;
  292. rc = phy_read_mmd(phydev, MDIO_MMD_PCS, MII_LAN874X_PHY_MMD_WOL_WUCSR);
  293. if (rc < 0)
  294. return;
  295. if (rc & MII_LAN874X_PHY_WOL_PFDAEN)
  296. wol->wolopts |= WAKE_UCAST;
  297. if (rc & MII_LAN874X_PHY_WOL_BCSTEN)
  298. wol->wolopts |= WAKE_BCAST;
  299. if (rc & MII_LAN874X_PHY_WOL_MPEN)
  300. wol->wolopts |= WAKE_MAGIC;
  301. if (rc & MII_LAN874X_PHY_WOL_WUEN) {
  302. if (priv->wol_arp)
  303. wol->wolopts |= WAKE_ARP;
  304. else
  305. wol->wolopts |= WAKE_MCAST;
  306. }
  307. }
  308. static u16 smsc_crc16(const u8 *buffer, size_t len)
  309. {
  310. return bitrev16(crc16(0xFFFF, buffer, len));
  311. }
  312. static int lan874x_chk_wol_pattern(const u8 pattern[], const u16 *mask,
  313. u8 len, u8 *data, u8 *datalen)
  314. {
  315. size_t i, j, k;
  316. int ret = 0;
  317. u16 bits;
  318. /* Pattern filtering can match up to 128 bytes of frame data. There
  319. * are 8 registers to program the 16-bit masks, where each bit means
  320. * the byte will be compared. The frame data will then go through a
  321. * CRC16 calculation for hardware comparison. This helper function
  322. * makes sure only relevant frame data are included in this
  323. * calculation. It provides a warning when the masks and expected
  324. * data size do not match.
  325. */
  326. i = 0;
  327. k = 0;
  328. while (len > 0) {
  329. bits = *mask;
  330. for (j = 0; j < 16; j++, i++, len--) {
  331. /* No more pattern. */
  332. if (!len) {
  333. /* The rest of bitmap is not empty. */
  334. if (bits)
  335. ret = i + 1;
  336. break;
  337. }
  338. if (bits & 1)
  339. data[k++] = pattern[i];
  340. bits >>= 1;
  341. }
  342. mask++;
  343. }
  344. *datalen = k;
  345. return ret;
  346. }
  347. static int lan874x_set_wol_pattern(struct phy_device *phydev, u16 val,
  348. const u8 data[], u8 datalen,
  349. const u16 *mask, u8 masklen)
  350. {
  351. u16 crc, reg;
  352. int rc;
  353. /* Starting pattern offset is set before calling this function. */
  354. val |= MII_LAN874X_PHY_WOL_FILTER_EN;
  355. rc = phy_write_mmd(phydev, MDIO_MMD_PCS,
  356. MII_LAN874X_PHY_MMD_WOL_WUF_CFGA, val);
  357. if (rc < 0)
  358. return rc;
  359. crc = smsc_crc16(data, datalen);
  360. rc = phy_write_mmd(phydev, MDIO_MMD_PCS,
  361. MII_LAN874X_PHY_MMD_WOL_WUF_CFGB, crc);
  362. if (rc < 0)
  363. return rc;
  364. masklen = (masklen + 15) & ~0xf;
  365. reg = MII_LAN874X_PHY_MMD_WOL_WUF_MASK7;
  366. while (masklen >= 16) {
  367. rc = phy_write_mmd(phydev, MDIO_MMD_PCS, reg, *mask);
  368. if (rc < 0)
  369. return rc;
  370. reg--;
  371. mask++;
  372. masklen -= 16;
  373. }
  374. /* Clear out the rest of mask registers. */
  375. while (reg != MII_LAN874X_PHY_MMD_WOL_WUF_MASK0) {
  376. phy_write_mmd(phydev, MDIO_MMD_PCS, reg, 0);
  377. reg--;
  378. }
  379. return rc;
  380. }
  381. static int lan874x_set_wol(struct phy_device *phydev,
  382. struct ethtool_wolinfo *wol)
  383. {
  384. struct net_device *ndev = phydev->attached_dev;
  385. struct smsc_phy_priv *priv = phydev->priv;
  386. u16 val, val_wucsr;
  387. u8 data[128];
  388. u8 datalen;
  389. int rc;
  390. /* lan874x has only one WoL filter pattern */
  391. if ((wol->wolopts & (WAKE_ARP | WAKE_MCAST)) ==
  392. (WAKE_ARP | WAKE_MCAST)) {
  393. phydev_info(phydev,
  394. "lan874x WoL supports one of ARP|MCAST at a time\n");
  395. return -EOPNOTSUPP;
  396. }
  397. rc = phy_read_mmd(phydev, MDIO_MMD_PCS, MII_LAN874X_PHY_MMD_WOL_WUCSR);
  398. if (rc < 0)
  399. return rc;
  400. val_wucsr = rc;
  401. if (wol->wolopts & WAKE_UCAST)
  402. val_wucsr |= MII_LAN874X_PHY_WOL_PFDAEN;
  403. else
  404. val_wucsr &= ~MII_LAN874X_PHY_WOL_PFDAEN;
  405. if (wol->wolopts & WAKE_BCAST)
  406. val_wucsr |= MII_LAN874X_PHY_WOL_BCSTEN;
  407. else
  408. val_wucsr &= ~MII_LAN874X_PHY_WOL_BCSTEN;
  409. if (wol->wolopts & WAKE_MAGIC)
  410. val_wucsr |= MII_LAN874X_PHY_WOL_MPEN;
  411. else
  412. val_wucsr &= ~MII_LAN874X_PHY_WOL_MPEN;
  413. /* Need to use pattern matching */
  414. if (wol->wolopts & (WAKE_ARP | WAKE_MCAST))
  415. val_wucsr |= MII_LAN874X_PHY_WOL_WUEN;
  416. else
  417. val_wucsr &= ~MII_LAN874X_PHY_WOL_WUEN;
  418. if (wol->wolopts & WAKE_ARP) {
  419. const u8 pattern[2] = { 0x08, 0x06 };
  420. const u16 mask[1] = { 0x0003 };
  421. rc = lan874x_chk_wol_pattern(pattern, mask, 2, data,
  422. &datalen);
  423. if (rc)
  424. phydev_dbg(phydev, "pattern not valid at %d\n", rc);
  425. /* Need to match broadcast destination address and provided
  426. * data pattern at offset 12.
  427. */
  428. val = 12 | MII_LAN874X_PHY_WOL_FILTER_BCSTEN;
  429. rc = lan874x_set_wol_pattern(phydev, val, data, datalen, mask,
  430. 2);
  431. if (rc < 0)
  432. return rc;
  433. priv->wol_arp = true;
  434. }
  435. if (wol->wolopts & WAKE_MCAST) {
  436. /* Need to match multicast destination address. */
  437. val = MII_LAN874X_PHY_WOL_FILTER_MCASTTEN;
  438. rc = lan874x_set_wol_pattern(phydev, val, data, 0, NULL, 0);
  439. if (rc < 0)
  440. return rc;
  441. priv->wol_arp = false;
  442. }
  443. if (wol->wolopts & (WAKE_MAGIC | WAKE_UCAST)) {
  444. const u8 *mac = (const u8 *)ndev->dev_addr;
  445. int i, reg;
  446. reg = MII_LAN874X_PHY_MMD_WOL_RX_ADDRC;
  447. for (i = 0; i < 6; i += 2, reg--) {
  448. rc = phy_write_mmd(phydev, MDIO_MMD_PCS, reg,
  449. ((mac[i + 1] << 8) | mac[i]));
  450. if (rc < 0)
  451. return rc;
  452. }
  453. }
  454. rc = phy_write_mmd(phydev, MDIO_MMD_PCS, MII_LAN874X_PHY_MMD_WOL_WUCSR,
  455. val_wucsr);
  456. if (rc < 0)
  457. return rc;
  458. return 0;
  459. }
  460. static int smsc_get_sset_count(struct phy_device *phydev)
  461. {
  462. return ARRAY_SIZE(smsc_hw_stats);
  463. }
  464. static void smsc_get_strings(struct phy_device *phydev, u8 *data)
  465. {
  466. int i;
  467. for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
  468. ethtool_puts(&data, smsc_hw_stats[i].string);
  469. }
  470. static u64 smsc_get_stat(struct phy_device *phydev, int i)
  471. {
  472. struct smsc_hw_stat stat = smsc_hw_stats[i];
  473. int val;
  474. u64 ret;
  475. val = phy_read(phydev, stat.reg);
  476. if (val < 0)
  477. ret = U64_MAX;
  478. else
  479. ret = val;
  480. return ret;
  481. }
  482. static void smsc_get_stats(struct phy_device *phydev,
  483. struct ethtool_stats *stats, u64 *data)
  484. {
  485. int i;
  486. for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
  487. data[i] = smsc_get_stat(phydev, i);
  488. }
  489. static int smsc_phy_get_edpd(struct phy_device *phydev, u16 *edpd)
  490. {
  491. struct smsc_phy_priv *priv = phydev->priv;
  492. if (!priv)
  493. return -EOPNOTSUPP;
  494. if (!priv->edpd_enable)
  495. *edpd = ETHTOOL_PHY_EDPD_DISABLE;
  496. else if (!priv->edpd_max_wait_ms)
  497. *edpd = ETHTOOL_PHY_EDPD_NO_TX;
  498. else
  499. *edpd = PHY_STATE_MACH_MS + priv->edpd_max_wait_ms;
  500. return 0;
  501. }
  502. static int smsc_phy_set_edpd(struct phy_device *phydev, u16 edpd)
  503. {
  504. struct smsc_phy_priv *priv = phydev->priv;
  505. if (!priv)
  506. return -EOPNOTSUPP;
  507. switch (edpd) {
  508. case ETHTOOL_PHY_EDPD_DISABLE:
  509. priv->edpd_enable = false;
  510. break;
  511. case ETHTOOL_PHY_EDPD_NO_TX:
  512. priv->edpd_enable = true;
  513. priv->edpd_max_wait_ms = 0;
  514. break;
  515. case ETHTOOL_PHY_EDPD_DFLT_TX_MSECS:
  516. edpd = PHY_STATE_MACH_MS + EDPD_MAX_WAIT_DFLT_MS;
  517. fallthrough;
  518. default:
  519. if (phydev->irq != PHY_POLL)
  520. return -EOPNOTSUPP;
  521. if (edpd < PHY_STATE_MACH_MS || edpd > PHY_STATE_MACH_MS + 1000)
  522. return -EINVAL;
  523. priv->edpd_enable = true;
  524. priv->edpd_max_wait_ms = edpd - PHY_STATE_MACH_MS;
  525. }
  526. priv->edpd_mode_set_by_user = true;
  527. return smsc_phy_config_edpd(phydev);
  528. }
  529. int smsc_phy_get_tunable(struct phy_device *phydev,
  530. struct ethtool_tunable *tuna, void *data)
  531. {
  532. switch (tuna->id) {
  533. case ETHTOOL_PHY_EDPD:
  534. return smsc_phy_get_edpd(phydev, data);
  535. default:
  536. return -EOPNOTSUPP;
  537. }
  538. }
  539. EXPORT_SYMBOL_GPL(smsc_phy_get_tunable);
  540. int smsc_phy_set_tunable(struct phy_device *phydev,
  541. struct ethtool_tunable *tuna, const void *data)
  542. {
  543. switch (tuna->id) {
  544. case ETHTOOL_PHY_EDPD:
  545. return smsc_phy_set_edpd(phydev, *(u16 *)data);
  546. default:
  547. return -EOPNOTSUPP;
  548. }
  549. }
  550. EXPORT_SYMBOL_GPL(smsc_phy_set_tunable);
  551. int smsc_phy_probe(struct phy_device *phydev)
  552. {
  553. struct device *dev = &phydev->mdio.dev;
  554. struct smsc_phy_priv *priv;
  555. struct clk *refclk;
  556. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  557. if (!priv)
  558. return -ENOMEM;
  559. priv->edpd_enable = true;
  560. priv->edpd_max_wait_ms = EDPD_MAX_WAIT_DFLT_MS;
  561. if (device_property_present(dev, "smsc,disable-energy-detect"))
  562. priv->edpd_enable = false;
  563. phydev->priv = priv;
  564. /* Make clk optional to keep DTB backward compatibility. */
  565. refclk = devm_clk_get_optional_enabled_with_rate(dev, NULL,
  566. 50 * 1000 * 1000);
  567. if (IS_ERR(refclk))
  568. return dev_err_probe(dev, PTR_ERR(refclk),
  569. "Failed to request clock\n");
  570. return 0;
  571. }
  572. EXPORT_SYMBOL_GPL(smsc_phy_probe);
  573. static struct phy_driver smsc_phy_driver[] = {
  574. {
  575. .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
  576. .phy_id_mask = 0xfffffff0,
  577. .name = "SMSC LAN83C185",
  578. /* PHY_BASIC_FEATURES */
  579. .probe = smsc_phy_probe,
  580. /* basic functions */
  581. .config_init = smsc_phy_config_init,
  582. .soft_reset = smsc_phy_reset,
  583. /* IRQ related */
  584. .config_intr = smsc_phy_config_intr,
  585. .handle_interrupt = smsc_phy_handle_interrupt,
  586. .suspend = genphy_suspend,
  587. .resume = genphy_resume,
  588. }, {
  589. .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
  590. .phy_id_mask = 0xfffffff0,
  591. .name = "SMSC LAN8187",
  592. /* PHY_BASIC_FEATURES */
  593. .probe = smsc_phy_probe,
  594. /* basic functions */
  595. .config_init = smsc_phy_config_init,
  596. .soft_reset = smsc_phy_reset,
  597. /* IRQ related */
  598. .config_intr = smsc_phy_config_intr,
  599. .handle_interrupt = smsc_phy_handle_interrupt,
  600. /* Statistics */
  601. .get_sset_count = smsc_get_sset_count,
  602. .get_strings = smsc_get_strings,
  603. .get_stats = smsc_get_stats,
  604. .suspend = genphy_suspend,
  605. .resume = genphy_resume,
  606. }, {
  607. /* This covers internal PHY (phy_id: 0x0007C0C3) for
  608. * LAN9500 (PID: 0x9500), LAN9514 (PID: 0xec00), LAN9505 (PID: 0x9505)
  609. */
  610. .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
  611. .phy_id_mask = 0xfffffff0,
  612. .name = "SMSC LAN8700",
  613. /* PHY_BASIC_FEATURES */
  614. .probe = smsc_phy_probe,
  615. /* basic functions */
  616. .read_status = lan87xx_read_status,
  617. .config_init = lan87xx_phy_config_init,
  618. .soft_reset = smsc_phy_reset,
  619. .config_aneg = lan87xx_config_aneg,
  620. /* IRQ related */
  621. .config_intr = smsc_phy_config_intr,
  622. .handle_interrupt = smsc_phy_handle_interrupt,
  623. /* Statistics */
  624. .get_sset_count = smsc_get_sset_count,
  625. .get_strings = smsc_get_strings,
  626. .get_stats = smsc_get_stats,
  627. .get_tunable = smsc_phy_get_tunable,
  628. .set_tunable = smsc_phy_set_tunable,
  629. .suspend = genphy_suspend,
  630. .resume = genphy_resume,
  631. }, {
  632. .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
  633. .phy_id_mask = 0xfffffff0,
  634. .name = "SMSC LAN911x Internal PHY",
  635. /* PHY_BASIC_FEATURES */
  636. .probe = smsc_phy_probe,
  637. /* IRQ related */
  638. .config_intr = smsc_phy_config_intr,
  639. .handle_interrupt = smsc_phy_handle_interrupt,
  640. .suspend = genphy_suspend,
  641. .resume = genphy_resume,
  642. }, {
  643. /* This covers internal PHY (phy_id: 0x0007C0F0) for
  644. * LAN9500A (PID: 0x9E00), LAN9505A (PID: 0x9E01)
  645. */
  646. .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
  647. .phy_id_mask = 0xfffffff0,
  648. .name = "SMSC LAN8710/LAN8720",
  649. /* PHY_BASIC_FEATURES */
  650. .flags = PHY_RST_AFTER_CLK_EN,
  651. .probe = smsc_phy_probe,
  652. /* basic functions */
  653. .read_status = lan87xx_read_status,
  654. .config_init = smsc_phy_config_init,
  655. .soft_reset = smsc_phy_reset,
  656. .config_aneg = lan95xx_config_aneg_ext,
  657. /* IRQ related */
  658. .config_intr = smsc_phy_config_intr,
  659. .handle_interrupt = smsc_phy_handle_interrupt,
  660. /* Statistics */
  661. .get_sset_count = smsc_get_sset_count,
  662. .get_strings = smsc_get_strings,
  663. .get_stats = smsc_get_stats,
  664. .get_tunable = smsc_phy_get_tunable,
  665. .set_tunable = smsc_phy_set_tunable,
  666. .suspend = genphy_suspend,
  667. .resume = genphy_resume,
  668. }, {
  669. .phy_id = 0x0007c110,
  670. .phy_id_mask = 0xfffffff0,
  671. .name = "SMSC LAN8740",
  672. /* PHY_BASIC_FEATURES */
  673. .flags = PHY_RST_AFTER_CLK_EN,
  674. .probe = smsc_phy_probe,
  675. /* basic functions */
  676. .read_status = lan87xx_read_status,
  677. .config_init = lan874x_phy_config_init,
  678. .soft_reset = smsc_phy_reset,
  679. /* IRQ related */
  680. .config_intr = smsc_phy_config_intr,
  681. .handle_interrupt = smsc_phy_handle_interrupt,
  682. /* Statistics */
  683. .get_sset_count = smsc_get_sset_count,
  684. .get_strings = smsc_get_strings,
  685. .get_stats = smsc_get_stats,
  686. .get_tunable = smsc_phy_get_tunable,
  687. .set_tunable = smsc_phy_set_tunable,
  688. /* WoL */
  689. .set_wol = lan874x_set_wol,
  690. .get_wol = lan874x_get_wol,
  691. .suspend = genphy_suspend,
  692. .resume = genphy_resume,
  693. }, {
  694. .phy_id = 0x0007c130, /* 0x0007c130 and 0x0007c131 */
  695. /* This mask (0xfffffff2) is to differentiate from
  696. * LAN88xx (phy_id 0x0007c132)
  697. * and allows future phy_id revisions.
  698. */
  699. .phy_id_mask = 0xfffffff2,
  700. .name = "Microchip LAN8742",
  701. /* PHY_BASIC_FEATURES */
  702. .flags = PHY_RST_AFTER_CLK_EN,
  703. .probe = smsc_phy_probe,
  704. /* basic functions */
  705. .read_status = lan87xx_read_status,
  706. .config_init = lan874x_phy_config_init,
  707. .soft_reset = smsc_phy_reset,
  708. /* IRQ related */
  709. .config_intr = smsc_phy_config_intr,
  710. .handle_interrupt = smsc_phy_handle_interrupt,
  711. /* Statistics */
  712. .get_sset_count = smsc_get_sset_count,
  713. .get_strings = smsc_get_strings,
  714. .get_stats = smsc_get_stats,
  715. .get_tunable = smsc_phy_get_tunable,
  716. .set_tunable = smsc_phy_set_tunable,
  717. /* WoL */
  718. .set_wol = lan874x_set_wol,
  719. .get_wol = lan874x_get_wol,
  720. .suspend = genphy_suspend,
  721. .resume = genphy_resume,
  722. } };
  723. module_phy_driver(smsc_phy_driver);
  724. MODULE_DESCRIPTION("SMSC PHY driver");
  725. MODULE_AUTHOR("Herbert Valerio Riedel");
  726. MODULE_LICENSE("GPL");
  727. static const struct mdio_device_id __maybe_unused smsc_tbl[] = {
  728. { 0x0007c0a0, 0xfffffff0 },
  729. { 0x0007c0b0, 0xfffffff0 },
  730. { 0x0007c0c0, 0xfffffff0 },
  731. { 0x0007c0d0, 0xfffffff0 },
  732. { 0x0007c0f0, 0xfffffff0 },
  733. { 0x0007c110, 0xfffffff0 },
  734. { 0x0007c130, 0xfffffff2 },
  735. { }
  736. };
  737. MODULE_DEVICE_TABLE(mdio, smsc_tbl);