smsc.c 6.9 KB

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