smsc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * drivers/net/phy/smsc.c
  3. *
  4. * Driver for SMSC PHYs
  5. *
  6. * Author: Herbert Valerio Riedel
  7. *
  8. * Copyright (c) 2006 Herbert Valerio Riedel <hvr@gnu.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * Support added for SMSC LAN8187 and LAN8700 by steve.glendinning@shawell.net
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/mii.h>
  21. #include <linux/ethtool.h>
  22. #include <linux/phy.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/smscphy.h>
  25. struct smsc_phy_priv {
  26. bool energy_enable;
  27. };
  28. static int smsc_phy_config_intr(struct phy_device *phydev)
  29. {
  30. int rc = phy_write (phydev, MII_LAN83C185_IM,
  31. ((PHY_INTERRUPT_ENABLED == phydev->interrupts)
  32. ? MII_LAN83C185_ISF_INT_PHYLIB_EVENTS
  33. : 0));
  34. return rc < 0 ? rc : 0;
  35. }
  36. static int smsc_phy_ack_interrupt(struct phy_device *phydev)
  37. {
  38. int rc = phy_read (phydev, MII_LAN83C185_ISF);
  39. return rc < 0 ? rc : 0;
  40. }
  41. static int smsc_phy_config_init(struct phy_device *phydev)
  42. {
  43. struct smsc_phy_priv *priv = phydev->priv;
  44. int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  45. if (rc < 0)
  46. return rc;
  47. if (priv->energy_enable) {
  48. /* Enable energy detect mode for this SMSC Transceivers */
  49. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  50. rc | MII_LAN83C185_EDPWRDOWN);
  51. if (rc < 0)
  52. return rc;
  53. }
  54. return smsc_phy_ack_interrupt(phydev);
  55. }
  56. static int smsc_phy_reset(struct phy_device *phydev)
  57. {
  58. int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
  59. if (rc < 0)
  60. return rc;
  61. /* If the SMSC PHY is in power down mode, then set it
  62. * in all capable mode before using it.
  63. */
  64. if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
  65. /* set "all capable" mode */
  66. rc |= MII_LAN83C185_MODE_ALL;
  67. phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
  68. }
  69. /* reset the phy */
  70. return genphy_soft_reset(phydev);
  71. }
  72. static int lan911x_config_init(struct phy_device *phydev)
  73. {
  74. return smsc_phy_ack_interrupt(phydev);
  75. }
  76. /*
  77. * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
  78. * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
  79. * unstable detection of plugging in Ethernet cable.
  80. * This workaround disables Energy Detect Power-Down mode and waiting for
  81. * response on link pulses to detect presence of plugged Ethernet cable.
  82. * The Energy Detect Power-Down mode is enabled again in the end of procedure to
  83. * save approximately 220 mW of power if cable is unplugged.
  84. */
  85. static int lan87xx_read_status(struct phy_device *phydev)
  86. {
  87. struct smsc_phy_priv *priv = phydev->priv;
  88. int err = genphy_read_status(phydev);
  89. if (!phydev->link && priv->energy_enable) {
  90. int i;
  91. /* Disable EDPD to wake up PHY */
  92. int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  93. if (rc < 0)
  94. return rc;
  95. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  96. rc & ~MII_LAN83C185_EDPWRDOWN);
  97. if (rc < 0)
  98. return rc;
  99. /* Wait max 640 ms to detect energy */
  100. for (i = 0; i < 64; i++) {
  101. /* Sleep to allow link test pulses to be sent */
  102. msleep(10);
  103. rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  104. if (rc < 0)
  105. return rc;
  106. if (rc & MII_LAN83C185_ENERGYON)
  107. break;
  108. }
  109. /* Re-enable EDPD */
  110. rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  111. if (rc < 0)
  112. return rc;
  113. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  114. rc | MII_LAN83C185_EDPWRDOWN);
  115. if (rc < 0)
  116. return rc;
  117. }
  118. return err;
  119. }
  120. static int smsc_phy_probe(struct phy_device *phydev)
  121. {
  122. struct device *dev = &phydev->mdio.dev;
  123. struct device_node *of_node = dev->of_node;
  124. struct smsc_phy_priv *priv;
  125. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  126. if (!priv)
  127. return -ENOMEM;
  128. priv->energy_enable = true;
  129. if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
  130. priv->energy_enable = false;
  131. phydev->priv = priv;
  132. return 0;
  133. }
  134. static struct phy_driver smsc_phy_driver[] = {
  135. {
  136. .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
  137. .phy_id_mask = 0xfffffff0,
  138. .name = "SMSC LAN83C185",
  139. .features = PHY_BASIC_FEATURES,
  140. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  141. .probe = smsc_phy_probe,
  142. /* basic functions */
  143. .config_aneg = genphy_config_aneg,
  144. .read_status = genphy_read_status,
  145. .config_init = smsc_phy_config_init,
  146. .soft_reset = smsc_phy_reset,
  147. /* IRQ related */
  148. .ack_interrupt = smsc_phy_ack_interrupt,
  149. .config_intr = smsc_phy_config_intr,
  150. .suspend = genphy_suspend,
  151. .resume = genphy_resume,
  152. }, {
  153. .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
  154. .phy_id_mask = 0xfffffff0,
  155. .name = "SMSC LAN8187",
  156. .features = PHY_BASIC_FEATURES,
  157. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  158. .probe = smsc_phy_probe,
  159. /* basic functions */
  160. .config_aneg = genphy_config_aneg,
  161. .read_status = genphy_read_status,
  162. .config_init = smsc_phy_config_init,
  163. .soft_reset = smsc_phy_reset,
  164. /* IRQ related */
  165. .ack_interrupt = smsc_phy_ack_interrupt,
  166. .config_intr = smsc_phy_config_intr,
  167. .suspend = genphy_suspend,
  168. .resume = genphy_resume,
  169. }, {
  170. .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
  171. .phy_id_mask = 0xfffffff0,
  172. .name = "SMSC LAN8700",
  173. .features = PHY_BASIC_FEATURES,
  174. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  175. .probe = smsc_phy_probe,
  176. /* basic functions */
  177. .config_aneg = genphy_config_aneg,
  178. .read_status = lan87xx_read_status,
  179. .config_init = smsc_phy_config_init,
  180. .soft_reset = smsc_phy_reset,
  181. /* IRQ related */
  182. .ack_interrupt = smsc_phy_ack_interrupt,
  183. .config_intr = smsc_phy_config_intr,
  184. .suspend = genphy_suspend,
  185. .resume = genphy_resume,
  186. }, {
  187. .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
  188. .phy_id_mask = 0xfffffff0,
  189. .name = "SMSC LAN911x Internal PHY",
  190. .features = PHY_BASIC_FEATURES,
  191. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  192. .probe = smsc_phy_probe,
  193. /* basic functions */
  194. .config_aneg = genphy_config_aneg,
  195. .read_status = genphy_read_status,
  196. .config_init = lan911x_config_init,
  197. /* IRQ related */
  198. .ack_interrupt = smsc_phy_ack_interrupt,
  199. .config_intr = smsc_phy_config_intr,
  200. .suspend = genphy_suspend,
  201. .resume = genphy_resume,
  202. }, {
  203. .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
  204. .phy_id_mask = 0xfffffff0,
  205. .name = "SMSC LAN8710/LAN8720",
  206. .features = PHY_BASIC_FEATURES,
  207. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  208. .probe = smsc_phy_probe,
  209. /* basic functions */
  210. .config_aneg = genphy_config_aneg,
  211. .read_status = lan87xx_read_status,
  212. .config_init = smsc_phy_config_init,
  213. .soft_reset = smsc_phy_reset,
  214. /* IRQ related */
  215. .ack_interrupt = smsc_phy_ack_interrupt,
  216. .config_intr = smsc_phy_config_intr,
  217. .suspend = genphy_suspend,
  218. .resume = genphy_resume,
  219. }, {
  220. .phy_id = 0x0007c110,
  221. .phy_id_mask = 0xfffffff0,
  222. .name = "SMSC LAN8740",
  223. .features = PHY_BASIC_FEATURES,
  224. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  225. .probe = smsc_phy_probe,
  226. /* basic functions */
  227. .config_aneg = genphy_config_aneg,
  228. .read_status = lan87xx_read_status,
  229. .config_init = smsc_phy_config_init,
  230. .soft_reset = smsc_phy_reset,
  231. /* IRQ related */
  232. .ack_interrupt = smsc_phy_ack_interrupt,
  233. .config_intr = smsc_phy_config_intr,
  234. .suspend = genphy_suspend,
  235. .resume = genphy_resume,
  236. } };
  237. module_phy_driver(smsc_phy_driver);
  238. MODULE_DESCRIPTION("SMSC PHY driver");
  239. MODULE_AUTHOR("Herbert Valerio Riedel");
  240. MODULE_LICENSE("GPL");
  241. static struct mdio_device_id __maybe_unused smsc_tbl[] = {
  242. { 0x0007c0a0, 0xfffffff0 },
  243. { 0x0007c0b0, 0xfffffff0 },
  244. { 0x0007c0c0, 0xfffffff0 },
  245. { 0x0007c0d0, 0xfffffff0 },
  246. { 0x0007c0f0, 0xfffffff0 },
  247. { 0x0007c110, 0xfffffff0 },
  248. { }
  249. };
  250. MODULE_DEVICE_TABLE(mdio, smsc_tbl);