smsc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. static int smsc_phy_config_intr(struct phy_device *phydev)
  26. {
  27. int rc = phy_write (phydev, MII_LAN83C185_IM,
  28. ((PHY_INTERRUPT_ENABLED == phydev->interrupts)
  29. ? MII_LAN83C185_ISF_INT_PHYLIB_EVENTS
  30. : 0));
  31. return rc < 0 ? rc : 0;
  32. }
  33. static int smsc_phy_ack_interrupt(struct phy_device *phydev)
  34. {
  35. int rc = phy_read (phydev, MII_LAN83C185_ISF);
  36. return rc < 0 ? rc : 0;
  37. }
  38. static int smsc_phy_config_init(struct phy_device *phydev)
  39. {
  40. int __maybe_unused len;
  41. struct device *dev __maybe_unused = &phydev->dev;
  42. struct device_node *of_node __maybe_unused = dev->of_node;
  43. int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  44. int enable_energy = 1;
  45. if (rc < 0)
  46. return rc;
  47. if (of_find_property(of_node, "smsc,disable-energy-detect", &len))
  48. enable_energy = 0;
  49. if (enable_energy) {
  50. /* Enable energy detect mode for this SMSC Transceivers */
  51. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  52. rc | MII_LAN83C185_EDPWRDOWN);
  53. if (rc < 0)
  54. return rc;
  55. }
  56. return smsc_phy_ack_interrupt(phydev);
  57. }
  58. static int smsc_phy_reset(struct phy_device *phydev)
  59. {
  60. int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
  61. if (rc < 0)
  62. return rc;
  63. /* If the SMSC PHY is in power down mode, then set it
  64. * in all capable mode before using it.
  65. */
  66. if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
  67. int timeout = 50000;
  68. /* set "all capable" mode and reset the phy */
  69. rc |= MII_LAN83C185_MODE_ALL;
  70. phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
  71. phy_write(phydev, MII_BMCR, BMCR_RESET);
  72. /* wait end of reset (max 500 ms) */
  73. do {
  74. udelay(10);
  75. if (timeout-- == 0)
  76. return -1;
  77. rc = phy_read(phydev, MII_BMCR);
  78. } while (rc & BMCR_RESET);
  79. }
  80. return 0;
  81. }
  82. static int lan911x_config_init(struct phy_device *phydev)
  83. {
  84. return smsc_phy_ack_interrupt(phydev);
  85. }
  86. /*
  87. * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
  88. * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
  89. * unstable detection of plugging in Ethernet cable.
  90. * This workaround disables Energy Detect Power-Down mode and waiting for
  91. * response on link pulses to detect presence of plugged Ethernet cable.
  92. * The Energy Detect Power-Down mode is enabled again in the end of procedure to
  93. * save approximately 220 mW of power if cable is unplugged.
  94. */
  95. static int lan87xx_read_status(struct phy_device *phydev)
  96. {
  97. int err = genphy_read_status(phydev);
  98. int i;
  99. if (!phydev->link) {
  100. /* Disable EDPD to wake up PHY */
  101. int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  102. if (rc < 0)
  103. return rc;
  104. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  105. rc & ~MII_LAN83C185_EDPWRDOWN);
  106. if (rc < 0)
  107. return rc;
  108. /* Wait max 640 ms to detect energy */
  109. for (i = 0; i < 64; i++) {
  110. /* Sleep to allow link test pulses to be sent */
  111. msleep(10);
  112. rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  113. if (rc < 0)
  114. return rc;
  115. if (rc & MII_LAN83C185_ENERGYON)
  116. break;
  117. }
  118. /* Re-enable EDPD */
  119. rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  120. if (rc < 0)
  121. return rc;
  122. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  123. rc | MII_LAN83C185_EDPWRDOWN);
  124. if (rc < 0)
  125. return rc;
  126. }
  127. return err;
  128. }
  129. static struct phy_driver smsc_phy_driver[] = {
  130. {
  131. .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
  132. .phy_id_mask = 0xfffffff0,
  133. .name = "SMSC LAN83C185",
  134. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  135. | SUPPORTED_Asym_Pause),
  136. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  137. /* basic functions */
  138. .config_aneg = genphy_config_aneg,
  139. .read_status = genphy_read_status,
  140. .config_init = smsc_phy_config_init,
  141. .soft_reset = smsc_phy_reset,
  142. /* IRQ related */
  143. .ack_interrupt = smsc_phy_ack_interrupt,
  144. .config_intr = smsc_phy_config_intr,
  145. .suspend = genphy_suspend,
  146. .resume = genphy_resume,
  147. .driver = { .owner = THIS_MODULE, }
  148. }, {
  149. .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
  150. .phy_id_mask = 0xfffffff0,
  151. .name = "SMSC LAN8187",
  152. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  153. | SUPPORTED_Asym_Pause),
  154. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  155. /* basic functions */
  156. .config_aneg = genphy_config_aneg,
  157. .read_status = genphy_read_status,
  158. .config_init = smsc_phy_config_init,
  159. .soft_reset = smsc_phy_reset,
  160. /* IRQ related */
  161. .ack_interrupt = smsc_phy_ack_interrupt,
  162. .config_intr = smsc_phy_config_intr,
  163. .suspend = genphy_suspend,
  164. .resume = genphy_resume,
  165. .driver = { .owner = THIS_MODULE, }
  166. }, {
  167. .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
  168. .phy_id_mask = 0xfffffff0,
  169. .name = "SMSC LAN8700",
  170. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  171. | SUPPORTED_Asym_Pause),
  172. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  173. /* basic functions */
  174. .config_aneg = genphy_config_aneg,
  175. .read_status = lan87xx_read_status,
  176. .config_init = smsc_phy_config_init,
  177. .soft_reset = smsc_phy_reset,
  178. /* IRQ related */
  179. .ack_interrupt = smsc_phy_ack_interrupt,
  180. .config_intr = smsc_phy_config_intr,
  181. .suspend = genphy_suspend,
  182. .resume = genphy_resume,
  183. .driver = { .owner = THIS_MODULE, }
  184. }, {
  185. .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
  186. .phy_id_mask = 0xfffffff0,
  187. .name = "SMSC LAN911x Internal PHY",
  188. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  189. | SUPPORTED_Asym_Pause),
  190. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  191. /* basic functions */
  192. .config_aneg = genphy_config_aneg,
  193. .read_status = genphy_read_status,
  194. .config_init = lan911x_config_init,
  195. /* IRQ related */
  196. .ack_interrupt = smsc_phy_ack_interrupt,
  197. .config_intr = smsc_phy_config_intr,
  198. .suspend = genphy_suspend,
  199. .resume = genphy_resume,
  200. .driver = { .owner = THIS_MODULE, }
  201. }, {
  202. .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
  203. .phy_id_mask = 0xfffffff0,
  204. .name = "SMSC LAN8710/LAN8720",
  205. .features = (PHY_BASIC_FEATURES | SUPPORTED_Pause
  206. | SUPPORTED_Asym_Pause),
  207. .flags = PHY_HAS_INTERRUPT | PHY_HAS_MAGICANEG,
  208. /* basic functions */
  209. .config_aneg = genphy_config_aneg,
  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. .suspend = genphy_suspend,
  217. .resume = genphy_resume,
  218. .driver = { .owner = THIS_MODULE, }
  219. } };
  220. module_phy_driver(smsc_phy_driver);
  221. MODULE_DESCRIPTION("SMSC PHY driver");
  222. MODULE_AUTHOR("Herbert Valerio Riedel");
  223. MODULE_LICENSE("GPL");
  224. static struct mdio_device_id __maybe_unused smsc_tbl[] = {
  225. { 0x0007c0a0, 0xfffffff0 },
  226. { 0x0007c0b0, 0xfffffff0 },
  227. { 0x0007c0c0, 0xfffffff0 },
  228. { 0x0007c0d0, 0xfffffff0 },
  229. { 0x0007c0f0, 0xfffffff0 },
  230. { }
  231. };
  232. MODULE_DEVICE_TABLE(mdio, smsc_tbl);