smsc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. int timeout = 50000;
  66. /* set "all capable" mode and reset the phy */
  67. rc |= MII_LAN83C185_MODE_ALL;
  68. phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
  69. phy_write(phydev, MII_BMCR, BMCR_RESET);
  70. /* wait end of reset (max 500 ms) */
  71. do {
  72. udelay(10);
  73. if (timeout-- == 0)
  74. return -1;
  75. rc = phy_read(phydev, MII_BMCR);
  76. } while (rc & BMCR_RESET);
  77. }
  78. return 0;
  79. }
  80. static int lan911x_config_init(struct phy_device *phydev)
  81. {
  82. return smsc_phy_ack_interrupt(phydev);
  83. }
  84. /*
  85. * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
  86. * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
  87. * unstable detection of plugging in Ethernet cable.
  88. * This workaround disables Energy Detect Power-Down mode and waiting for
  89. * response on link pulses to detect presence of plugged Ethernet cable.
  90. * The Energy Detect Power-Down mode is enabled again in the end of procedure to
  91. * save approximately 220 mW of power if cable is unplugged.
  92. */
  93. static int lan87xx_read_status(struct phy_device *phydev)
  94. {
  95. struct smsc_phy_priv *priv = phydev->priv;
  96. int err = genphy_read_status(phydev);
  97. if (!phydev->link && priv->energy_enable) {
  98. int i;
  99. /* Disable EDPD to wake up PHY */
  100. int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  101. if (rc < 0)
  102. return rc;
  103. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  104. rc & ~MII_LAN83C185_EDPWRDOWN);
  105. if (rc < 0)
  106. return rc;
  107. /* Wait max 640 ms to detect energy */
  108. for (i = 0; i < 64; i++) {
  109. /* Sleep to allow link test pulses to be sent */
  110. msleep(10);
  111. rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  112. if (rc < 0)
  113. return rc;
  114. if (rc & MII_LAN83C185_ENERGYON)
  115. break;
  116. }
  117. /* Re-enable EDPD */
  118. rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  119. if (rc < 0)
  120. return rc;
  121. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  122. rc | MII_LAN83C185_EDPWRDOWN);
  123. if (rc < 0)
  124. return rc;
  125. }
  126. return err;
  127. }
  128. static int smsc_phy_probe(struct phy_device *phydev)
  129. {
  130. struct device *dev = &phydev->mdio.dev;
  131. struct device_node *of_node = dev->of_node;
  132. struct smsc_phy_priv *priv;
  133. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  134. if (!priv)
  135. return -ENOMEM;
  136. priv->energy_enable = true;
  137. if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
  138. priv->energy_enable = false;
  139. phydev->priv = priv;
  140. return 0;
  141. }
  142. static struct phy_driver smsc_phy_driver[] = {
  143. {
  144. .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
  145. .phy_id_mask = 0xfffffff0,
  146. .name = "SMSC LAN83C185",
  147. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  148. | SUPPORTED_Asym_Pause),
  149. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  150. .probe = smsc_phy_probe,
  151. /* basic functions */
  152. .config_aneg = genphy_config_aneg,
  153. .read_status = genphy_read_status,
  154. .config_init = smsc_phy_config_init,
  155. .soft_reset = smsc_phy_reset,
  156. /* IRQ related */
  157. .ack_interrupt = smsc_phy_ack_interrupt,
  158. .config_intr = smsc_phy_config_intr,
  159. .suspend = genphy_suspend,
  160. .resume = genphy_resume,
  161. }, {
  162. .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
  163. .phy_id_mask = 0xfffffff0,
  164. .name = "SMSC LAN8187",
  165. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  166. | SUPPORTED_Asym_Pause),
  167. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  168. .probe = smsc_phy_probe,
  169. /* basic functions */
  170. .config_aneg = genphy_config_aneg,
  171. .read_status = genphy_read_status,
  172. .config_init = smsc_phy_config_init,
  173. .soft_reset = smsc_phy_reset,
  174. /* IRQ related */
  175. .ack_interrupt = smsc_phy_ack_interrupt,
  176. .config_intr = smsc_phy_config_intr,
  177. .suspend = genphy_suspend,
  178. .resume = genphy_resume,
  179. }, {
  180. .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
  181. .phy_id_mask = 0xfffffff0,
  182. .name = "SMSC LAN8700",
  183. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  184. | SUPPORTED_Asym_Pause),
  185. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  186. .probe = smsc_phy_probe,
  187. /* basic functions */
  188. .config_aneg = genphy_config_aneg,
  189. .read_status = lan87xx_read_status,
  190. .config_init = smsc_phy_config_init,
  191. .soft_reset = smsc_phy_reset,
  192. /* IRQ related */
  193. .ack_interrupt = smsc_phy_ack_interrupt,
  194. .config_intr = smsc_phy_config_intr,
  195. .suspend = genphy_suspend,
  196. .resume = genphy_resume,
  197. }, {
  198. .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
  199. .phy_id_mask = 0xfffffff0,
  200. .name = "SMSC LAN911x Internal PHY",
  201. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  202. | SUPPORTED_Asym_Pause),
  203. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  204. .probe = smsc_phy_probe,
  205. /* basic functions */
  206. .config_aneg = genphy_config_aneg,
  207. .read_status = genphy_read_status,
  208. .config_init = lan911x_config_init,
  209. /* IRQ related */
  210. .ack_interrupt = smsc_phy_ack_interrupt,
  211. .config_intr = smsc_phy_config_intr,
  212. .suspend = genphy_suspend,
  213. .resume = genphy_resume,
  214. }, {
  215. .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
  216. .phy_id_mask = 0xfffffff0,
  217. .name = "SMSC LAN8710/LAN8720",
  218. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  219. | SUPPORTED_Asym_Pause),
  220. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  221. .probe = smsc_phy_probe,
  222. /* basic functions */
  223. .config_aneg = genphy_config_aneg,
  224. .read_status = lan87xx_read_status,
  225. .config_init = smsc_phy_config_init,
  226. .soft_reset = smsc_phy_reset,
  227. /* IRQ related */
  228. .ack_interrupt = smsc_phy_ack_interrupt,
  229. .config_intr = smsc_phy_config_intr,
  230. .suspend = genphy_suspend,
  231. .resume = genphy_resume,
  232. }, {
  233. .phy_id = 0x0007c110,
  234. .phy_id_mask = 0xfffffff0,
  235. .name = "SMSC LAN8740",
  236. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  237. | SUPPORTED_Asym_Pause),
  238. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  239. .probe = smsc_phy_probe,
  240. /* basic functions */
  241. .config_aneg = genphy_config_aneg,
  242. .read_status = lan87xx_read_status,
  243. .config_init = smsc_phy_config_init,
  244. .soft_reset = smsc_phy_reset,
  245. /* IRQ related */
  246. .ack_interrupt = smsc_phy_ack_interrupt,
  247. .config_intr = smsc_phy_config_intr,
  248. .suspend = genphy_suspend,
  249. .resume = genphy_resume,
  250. } };
  251. module_phy_driver(smsc_phy_driver);
  252. MODULE_DESCRIPTION("SMSC PHY driver");
  253. MODULE_AUTHOR("Herbert Valerio Riedel");
  254. MODULE_LICENSE("GPL");
  255. static struct mdio_device_id __maybe_unused smsc_tbl[] = {
  256. { 0x0007c0a0, 0xfffffff0 },
  257. { 0x0007c0b0, 0xfffffff0 },
  258. { 0x0007c0c0, 0xfffffff0 },
  259. { 0x0007c0d0, 0xfffffff0 },
  260. { 0x0007c0f0, 0xfffffff0 },
  261. { 0x0007c110, 0xfffffff0 },
  262. { }
  263. };
  264. MODULE_DEVICE_TABLE(mdio, smsc_tbl);