smsc.c 7.6 KB

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