smsc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 | SUPPORTED_Pause
  140. | SUPPORTED_Asym_Pause),
  141. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  142. .probe = smsc_phy_probe,
  143. /* basic functions */
  144. .config_aneg = genphy_config_aneg,
  145. .read_status = genphy_read_status,
  146. .config_init = smsc_phy_config_init,
  147. .soft_reset = smsc_phy_reset,
  148. /* IRQ related */
  149. .ack_interrupt = smsc_phy_ack_interrupt,
  150. .config_intr = smsc_phy_config_intr,
  151. .suspend = genphy_suspend,
  152. .resume = genphy_resume,
  153. }, {
  154. .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
  155. .phy_id_mask = 0xfffffff0,
  156. .name = "SMSC LAN8187",
  157. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  158. | SUPPORTED_Asym_Pause),
  159. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  160. .probe = smsc_phy_probe,
  161. /* basic functions */
  162. .config_aneg = genphy_config_aneg,
  163. .read_status = genphy_read_status,
  164. .config_init = smsc_phy_config_init,
  165. .soft_reset = smsc_phy_reset,
  166. /* IRQ related */
  167. .ack_interrupt = smsc_phy_ack_interrupt,
  168. .config_intr = smsc_phy_config_intr,
  169. .suspend = genphy_suspend,
  170. .resume = genphy_resume,
  171. }, {
  172. .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
  173. .phy_id_mask = 0xfffffff0,
  174. .name = "SMSC LAN8700",
  175. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  176. | SUPPORTED_Asym_Pause),
  177. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  178. .probe = smsc_phy_probe,
  179. /* basic functions */
  180. .config_aneg = genphy_config_aneg,
  181. .read_status = lan87xx_read_status,
  182. .config_init = smsc_phy_config_init,
  183. .soft_reset = smsc_phy_reset,
  184. /* IRQ related */
  185. .ack_interrupt = smsc_phy_ack_interrupt,
  186. .config_intr = smsc_phy_config_intr,
  187. .suspend = genphy_suspend,
  188. .resume = genphy_resume,
  189. }, {
  190. .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
  191. .phy_id_mask = 0xfffffff0,
  192. .name = "SMSC LAN911x Internal PHY",
  193. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  194. | SUPPORTED_Asym_Pause),
  195. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  196. .probe = smsc_phy_probe,
  197. /* basic functions */
  198. .config_aneg = genphy_config_aneg,
  199. .read_status = genphy_read_status,
  200. .config_init = lan911x_config_init,
  201. /* IRQ related */
  202. .ack_interrupt = smsc_phy_ack_interrupt,
  203. .config_intr = smsc_phy_config_intr,
  204. .suspend = genphy_suspend,
  205. .resume = genphy_resume,
  206. }, {
  207. .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
  208. .phy_id_mask = 0xfffffff0,
  209. .name = "SMSC LAN8710/LAN8720",
  210. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  211. | SUPPORTED_Asym_Pause),
  212. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  213. .probe = smsc_phy_probe,
  214. /* basic functions */
  215. .config_aneg = genphy_config_aneg,
  216. .read_status = lan87xx_read_status,
  217. .config_init = smsc_phy_config_init,
  218. .soft_reset = smsc_phy_reset,
  219. /* IRQ related */
  220. .ack_interrupt = smsc_phy_ack_interrupt,
  221. .config_intr = smsc_phy_config_intr,
  222. .suspend = genphy_suspend,
  223. .resume = genphy_resume,
  224. }, {
  225. .phy_id = 0x0007c110,
  226. .phy_id_mask = 0xfffffff0,
  227. .name = "SMSC LAN8740",
  228. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  229. | SUPPORTED_Asym_Pause),
  230. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  231. .probe = smsc_phy_probe,
  232. /* basic functions */
  233. .config_aneg = genphy_config_aneg,
  234. .read_status = lan87xx_read_status,
  235. .config_init = smsc_phy_config_init,
  236. .soft_reset = smsc_phy_reset,
  237. /* IRQ related */
  238. .ack_interrupt = smsc_phy_ack_interrupt,
  239. .config_intr = smsc_phy_config_intr,
  240. .suspend = genphy_suspend,
  241. .resume = genphy_resume,
  242. } };
  243. module_phy_driver(smsc_phy_driver);
  244. MODULE_DESCRIPTION("SMSC PHY driver");
  245. MODULE_AUTHOR("Herbert Valerio Riedel");
  246. MODULE_LICENSE("GPL");
  247. static struct mdio_device_id __maybe_unused smsc_tbl[] = {
  248. { 0x0007c0a0, 0xfffffff0 },
  249. { 0x0007c0b0, 0xfffffff0 },
  250. { 0x0007c0c0, 0xfffffff0 },
  251. { 0x0007c0d0, 0xfffffff0 },
  252. { 0x0007c0f0, 0xfffffff0 },
  253. { 0x0007c110, 0xfffffff0 },
  254. { }
  255. };
  256. MODULE_DEVICE_TABLE(mdio, smsc_tbl);