smsc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/mii.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/of.h>
  19. #include <linux/phy.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/smscphy.h>
  22. struct smsc_hw_stat {
  23. const char *string;
  24. u8 reg;
  25. u8 bits;
  26. };
  27. static struct smsc_hw_stat smsc_hw_stats[] = {
  28. { "phy_symbol_errors", 26, 16},
  29. };
  30. struct smsc_phy_priv {
  31. bool energy_enable;
  32. };
  33. static int smsc_phy_config_intr(struct phy_device *phydev)
  34. {
  35. int rc = phy_write (phydev, MII_LAN83C185_IM,
  36. ((PHY_INTERRUPT_ENABLED == phydev->interrupts)
  37. ? MII_LAN83C185_ISF_INT_PHYLIB_EVENTS
  38. : 0));
  39. return rc < 0 ? rc : 0;
  40. }
  41. static int smsc_phy_ack_interrupt(struct phy_device *phydev)
  42. {
  43. int rc = phy_read (phydev, MII_LAN83C185_ISF);
  44. return rc < 0 ? rc : 0;
  45. }
  46. static int smsc_phy_config_init(struct phy_device *phydev)
  47. {
  48. struct smsc_phy_priv *priv = phydev->priv;
  49. int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  50. if (rc < 0)
  51. return rc;
  52. if (priv->energy_enable) {
  53. /* Enable energy detect mode for this SMSC Transceivers */
  54. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  55. rc | MII_LAN83C185_EDPWRDOWN);
  56. if (rc < 0)
  57. return rc;
  58. }
  59. return smsc_phy_ack_interrupt(phydev);
  60. }
  61. static int smsc_phy_reset(struct phy_device *phydev)
  62. {
  63. int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
  64. if (rc < 0)
  65. return rc;
  66. /* If the SMSC PHY is in power down mode, then set it
  67. * in all capable mode before using it.
  68. */
  69. if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
  70. /* set "all capable" mode */
  71. rc |= MII_LAN83C185_MODE_ALL;
  72. phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
  73. }
  74. /* reset the phy */
  75. return genphy_soft_reset(phydev);
  76. }
  77. static int lan911x_config_init(struct phy_device *phydev)
  78. {
  79. return smsc_phy_ack_interrupt(phydev);
  80. }
  81. /*
  82. * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
  83. * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
  84. * unstable detection of plugging in Ethernet cable.
  85. * This workaround disables Energy Detect Power-Down mode and waiting for
  86. * response on link pulses to detect presence of plugged Ethernet cable.
  87. * The Energy Detect Power-Down mode is enabled again in the end of procedure to
  88. * save approximately 220 mW of power if cable is unplugged.
  89. */
  90. static int lan87xx_read_status(struct phy_device *phydev)
  91. {
  92. struct smsc_phy_priv *priv = phydev->priv;
  93. int err = genphy_read_status(phydev);
  94. if (!phydev->link && priv->energy_enable) {
  95. int i;
  96. /* Disable EDPD to wake up PHY */
  97. int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  98. if (rc < 0)
  99. return rc;
  100. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  101. rc & ~MII_LAN83C185_EDPWRDOWN);
  102. if (rc < 0)
  103. return rc;
  104. /* Wait max 640 ms to detect energy */
  105. for (i = 0; i < 64; i++) {
  106. /* Sleep to allow link test pulses to be sent */
  107. msleep(10);
  108. rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  109. if (rc < 0)
  110. return rc;
  111. if (rc & MII_LAN83C185_ENERGYON)
  112. break;
  113. }
  114. /* Re-enable EDPD */
  115. rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  116. if (rc < 0)
  117. return rc;
  118. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  119. rc | MII_LAN83C185_EDPWRDOWN);
  120. if (rc < 0)
  121. return rc;
  122. }
  123. return err;
  124. }
  125. static int smsc_get_sset_count(struct phy_device *phydev)
  126. {
  127. return ARRAY_SIZE(smsc_hw_stats);
  128. }
  129. static void smsc_get_strings(struct phy_device *phydev, u8 *data)
  130. {
  131. int i;
  132. for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
  133. strncpy(data + i * ETH_GSTRING_LEN,
  134. smsc_hw_stats[i].string, ETH_GSTRING_LEN);
  135. }
  136. }
  137. static u64 smsc_get_stat(struct phy_device *phydev, int i)
  138. {
  139. struct smsc_hw_stat stat = smsc_hw_stats[i];
  140. int val;
  141. u64 ret;
  142. val = phy_read(phydev, stat.reg);
  143. if (val < 0)
  144. ret = U64_MAX;
  145. else
  146. ret = val;
  147. return ret;
  148. }
  149. static void smsc_get_stats(struct phy_device *phydev,
  150. struct ethtool_stats *stats, u64 *data)
  151. {
  152. int i;
  153. for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
  154. data[i] = smsc_get_stat(phydev, i);
  155. }
  156. static int smsc_phy_probe(struct phy_device *phydev)
  157. {
  158. struct device *dev = &phydev->mdio.dev;
  159. struct device_node *of_node = dev->of_node;
  160. struct smsc_phy_priv *priv;
  161. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  162. if (!priv)
  163. return -ENOMEM;
  164. priv->energy_enable = true;
  165. if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
  166. priv->energy_enable = false;
  167. phydev->priv = priv;
  168. return 0;
  169. }
  170. static struct phy_driver smsc_phy_driver[] = {
  171. {
  172. .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
  173. .phy_id_mask = 0xfffffff0,
  174. .name = "SMSC LAN83C185",
  175. /* PHY_BASIC_FEATURES */
  176. .probe = smsc_phy_probe,
  177. /* basic functions */
  178. .config_init = smsc_phy_config_init,
  179. .soft_reset = smsc_phy_reset,
  180. /* IRQ related */
  181. .ack_interrupt = smsc_phy_ack_interrupt,
  182. .config_intr = smsc_phy_config_intr,
  183. .suspend = genphy_suspend,
  184. .resume = genphy_resume,
  185. }, {
  186. .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
  187. .phy_id_mask = 0xfffffff0,
  188. .name = "SMSC LAN8187",
  189. /* PHY_BASIC_FEATURES */
  190. .probe = smsc_phy_probe,
  191. /* basic functions */
  192. .config_init = smsc_phy_config_init,
  193. .soft_reset = smsc_phy_reset,
  194. /* IRQ related */
  195. .ack_interrupt = smsc_phy_ack_interrupt,
  196. .config_intr = smsc_phy_config_intr,
  197. /* Statistics */
  198. .get_sset_count = smsc_get_sset_count,
  199. .get_strings = smsc_get_strings,
  200. .get_stats = smsc_get_stats,
  201. .suspend = genphy_suspend,
  202. .resume = genphy_resume,
  203. }, {
  204. .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
  205. .phy_id_mask = 0xfffffff0,
  206. .name = "SMSC LAN8700",
  207. /* PHY_BASIC_FEATURES */
  208. .probe = smsc_phy_probe,
  209. /* basic functions */
  210. .read_status = lan87xx_read_status,
  211. .config_init = smsc_phy_config_init,
  212. .soft_reset = smsc_phy_reset,
  213. /* IRQ related */
  214. .ack_interrupt = smsc_phy_ack_interrupt,
  215. .config_intr = smsc_phy_config_intr,
  216. /* Statistics */
  217. .get_sset_count = smsc_get_sset_count,
  218. .get_strings = smsc_get_strings,
  219. .get_stats = smsc_get_stats,
  220. .suspend = genphy_suspend,
  221. .resume = genphy_resume,
  222. }, {
  223. .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
  224. .phy_id_mask = 0xfffffff0,
  225. .name = "SMSC LAN911x Internal PHY",
  226. /* PHY_BASIC_FEATURES */
  227. .probe = smsc_phy_probe,
  228. /* basic functions */
  229. .config_init = lan911x_config_init,
  230. /* IRQ related */
  231. .ack_interrupt = smsc_phy_ack_interrupt,
  232. .config_intr = smsc_phy_config_intr,
  233. .suspend = genphy_suspend,
  234. .resume = genphy_resume,
  235. }, {
  236. .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
  237. .phy_id_mask = 0xfffffff0,
  238. .name = "SMSC LAN8710/LAN8720",
  239. /* PHY_BASIC_FEATURES */
  240. .flags = PHY_RST_AFTER_CLK_EN,
  241. .probe = smsc_phy_probe,
  242. /* basic functions */
  243. .read_status = lan87xx_read_status,
  244. .config_init = smsc_phy_config_init,
  245. .soft_reset = smsc_phy_reset,
  246. /* IRQ related */
  247. .ack_interrupt = smsc_phy_ack_interrupt,
  248. .config_intr = smsc_phy_config_intr,
  249. /* Statistics */
  250. .get_sset_count = smsc_get_sset_count,
  251. .get_strings = smsc_get_strings,
  252. .get_stats = smsc_get_stats,
  253. .suspend = genphy_suspend,
  254. .resume = genphy_resume,
  255. }, {
  256. .phy_id = 0x0007c110,
  257. .phy_id_mask = 0xfffffff0,
  258. .name = "SMSC LAN8740",
  259. /* PHY_BASIC_FEATURES */
  260. .flags = PHY_RST_AFTER_CLK_EN,
  261. .probe = smsc_phy_probe,
  262. /* basic functions */
  263. .read_status = lan87xx_read_status,
  264. .config_init = smsc_phy_config_init,
  265. .soft_reset = smsc_phy_reset,
  266. /* IRQ related */
  267. .ack_interrupt = smsc_phy_ack_interrupt,
  268. .config_intr = smsc_phy_config_intr,
  269. /* Statistics */
  270. .get_sset_count = smsc_get_sset_count,
  271. .get_strings = smsc_get_strings,
  272. .get_stats = smsc_get_stats,
  273. .suspend = genphy_suspend,
  274. .resume = genphy_resume,
  275. } };
  276. module_phy_driver(smsc_phy_driver);
  277. MODULE_DESCRIPTION("SMSC PHY driver");
  278. MODULE_AUTHOR("Herbert Valerio Riedel");
  279. MODULE_LICENSE("GPL");
  280. static struct mdio_device_id __maybe_unused smsc_tbl[] = {
  281. { 0x0007c0a0, 0xfffffff0 },
  282. { 0x0007c0b0, 0xfffffff0 },
  283. { 0x0007c0c0, 0xfffffff0 },
  284. { 0x0007c0d0, 0xfffffff0 },
  285. { 0x0007c0f0, 0xfffffff0 },
  286. { 0x0007c110, 0xfffffff0 },
  287. { }
  288. };
  289. MODULE_DEVICE_TABLE(mdio, smsc_tbl);