smsc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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/of.h>
  23. #include <linux/phy.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/smscphy.h>
  26. struct smsc_hw_stat {
  27. const char *string;
  28. u8 reg;
  29. u8 bits;
  30. };
  31. static struct smsc_hw_stat smsc_hw_stats[] = {
  32. { "phy_symbol_errors", 26, 16},
  33. };
  34. struct smsc_phy_priv {
  35. bool energy_enable;
  36. };
  37. static int smsc_phy_config_intr(struct phy_device *phydev)
  38. {
  39. int rc = phy_write (phydev, MII_LAN83C185_IM,
  40. ((PHY_INTERRUPT_ENABLED == phydev->interrupts)
  41. ? MII_LAN83C185_ISF_INT_PHYLIB_EVENTS
  42. : 0));
  43. return rc < 0 ? rc : 0;
  44. }
  45. static int smsc_phy_ack_interrupt(struct phy_device *phydev)
  46. {
  47. int rc = phy_read (phydev, MII_LAN83C185_ISF);
  48. return rc < 0 ? rc : 0;
  49. }
  50. static int smsc_phy_config_init(struct phy_device *phydev)
  51. {
  52. struct smsc_phy_priv *priv = phydev->priv;
  53. int rc = phy_read(phydev, MII_LAN83C185_CTRL_STATUS);
  54. if (rc < 0)
  55. return rc;
  56. if (priv->energy_enable) {
  57. /* Enable energy detect mode for this SMSC Transceivers */
  58. rc = phy_write(phydev, MII_LAN83C185_CTRL_STATUS,
  59. rc | MII_LAN83C185_EDPWRDOWN);
  60. if (rc < 0)
  61. return rc;
  62. }
  63. return smsc_phy_ack_interrupt(phydev);
  64. }
  65. static int smsc_phy_reset(struct phy_device *phydev)
  66. {
  67. int rc = phy_read(phydev, MII_LAN83C185_SPECIAL_MODES);
  68. if (rc < 0)
  69. return rc;
  70. /* If the SMSC PHY is in power down mode, then set it
  71. * in all capable mode before using it.
  72. */
  73. if ((rc & MII_LAN83C185_MODE_MASK) == MII_LAN83C185_MODE_POWERDOWN) {
  74. /* set "all capable" mode */
  75. rc |= MII_LAN83C185_MODE_ALL;
  76. phy_write(phydev, MII_LAN83C185_SPECIAL_MODES, rc);
  77. }
  78. /* reset the phy */
  79. return genphy_soft_reset(phydev);
  80. }
  81. static int lan911x_config_init(struct phy_device *phydev)
  82. {
  83. return smsc_phy_ack_interrupt(phydev);
  84. }
  85. /*
  86. * The LAN87xx suffers from rare absence of the ENERGYON-bit when Ethernet cable
  87. * plugs in while LAN87xx is in Energy Detect Power-Down mode. This leads to
  88. * unstable detection of plugging in Ethernet cable.
  89. * This workaround disables Energy Detect Power-Down mode and waiting for
  90. * response on link pulses to detect presence of plugged Ethernet cable.
  91. * The Energy Detect Power-Down mode is enabled again in the end of procedure to
  92. * save approximately 220 mW of power if cable is unplugged.
  93. */
  94. static int lan87xx_read_status(struct phy_device *phydev)
  95. {
  96. struct smsc_phy_priv *priv = phydev->priv;
  97. int err = genphy_read_status(phydev);
  98. if (!phydev->link && priv->energy_enable) {
  99. int i;
  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 int smsc_get_sset_count(struct phy_device *phydev)
  130. {
  131. return ARRAY_SIZE(smsc_hw_stats);
  132. }
  133. static void smsc_get_strings(struct phy_device *phydev, u8 *data)
  134. {
  135. int i;
  136. for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++) {
  137. strncpy(data + i * ETH_GSTRING_LEN,
  138. smsc_hw_stats[i].string, ETH_GSTRING_LEN);
  139. }
  140. }
  141. static u64 smsc_get_stat(struct phy_device *phydev, int i)
  142. {
  143. struct smsc_hw_stat stat = smsc_hw_stats[i];
  144. int val;
  145. u64 ret;
  146. val = phy_read(phydev, stat.reg);
  147. if (val < 0)
  148. ret = U64_MAX;
  149. else
  150. ret = val;
  151. return ret;
  152. }
  153. static void smsc_get_stats(struct phy_device *phydev,
  154. struct ethtool_stats *stats, u64 *data)
  155. {
  156. int i;
  157. for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
  158. data[i] = smsc_get_stat(phydev, i);
  159. }
  160. static int smsc_phy_probe(struct phy_device *phydev)
  161. {
  162. struct device *dev = &phydev->mdio.dev;
  163. struct device_node *of_node = dev->of_node;
  164. struct smsc_phy_priv *priv;
  165. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  166. if (!priv)
  167. return -ENOMEM;
  168. priv->energy_enable = true;
  169. if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
  170. priv->energy_enable = false;
  171. phydev->priv = priv;
  172. return 0;
  173. }
  174. static struct phy_driver smsc_phy_driver[] = {
  175. {
  176. .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
  177. .phy_id_mask = 0xfffffff0,
  178. .name = "SMSC LAN83C185",
  179. .features = PHY_BASIC_FEATURES,
  180. .flags = PHY_HAS_INTERRUPT,
  181. .probe = smsc_phy_probe,
  182. /* basic functions */
  183. .config_init = smsc_phy_config_init,
  184. .soft_reset = smsc_phy_reset,
  185. /* IRQ related */
  186. .ack_interrupt = smsc_phy_ack_interrupt,
  187. .config_intr = smsc_phy_config_intr,
  188. .suspend = genphy_suspend,
  189. .resume = genphy_resume,
  190. }, {
  191. .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
  192. .phy_id_mask = 0xfffffff0,
  193. .name = "SMSC LAN8187",
  194. .features = PHY_BASIC_FEATURES,
  195. .flags = PHY_HAS_INTERRUPT,
  196. .probe = smsc_phy_probe,
  197. /* basic functions */
  198. .config_init = smsc_phy_config_init,
  199. .soft_reset = smsc_phy_reset,
  200. /* IRQ related */
  201. .ack_interrupt = smsc_phy_ack_interrupt,
  202. .config_intr = smsc_phy_config_intr,
  203. /* Statistics */
  204. .get_sset_count = smsc_get_sset_count,
  205. .get_strings = smsc_get_strings,
  206. .get_stats = smsc_get_stats,
  207. .suspend = genphy_suspend,
  208. .resume = genphy_resume,
  209. }, {
  210. .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
  211. .phy_id_mask = 0xfffffff0,
  212. .name = "SMSC LAN8700",
  213. .features = PHY_BASIC_FEATURES,
  214. .flags = PHY_HAS_INTERRUPT,
  215. .probe = smsc_phy_probe,
  216. /* basic functions */
  217. .read_status = lan87xx_read_status,
  218. .config_init = smsc_phy_config_init,
  219. .soft_reset = smsc_phy_reset,
  220. /* IRQ related */
  221. .ack_interrupt = smsc_phy_ack_interrupt,
  222. .config_intr = smsc_phy_config_intr,
  223. /* Statistics */
  224. .get_sset_count = smsc_get_sset_count,
  225. .get_strings = smsc_get_strings,
  226. .get_stats = smsc_get_stats,
  227. .suspend = genphy_suspend,
  228. .resume = genphy_resume,
  229. }, {
  230. .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
  231. .phy_id_mask = 0xfffffff0,
  232. .name = "SMSC LAN911x Internal PHY",
  233. .features = PHY_BASIC_FEATURES,
  234. .flags = PHY_HAS_INTERRUPT,
  235. .probe = smsc_phy_probe,
  236. /* basic functions */
  237. .config_init = lan911x_config_init,
  238. /* IRQ related */
  239. .ack_interrupt = smsc_phy_ack_interrupt,
  240. .config_intr = smsc_phy_config_intr,
  241. .suspend = genphy_suspend,
  242. .resume = genphy_resume,
  243. }, {
  244. .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
  245. .phy_id_mask = 0xfffffff0,
  246. .name = "SMSC LAN8710/LAN8720",
  247. .features = PHY_BASIC_FEATURES,
  248. .flags = PHY_HAS_INTERRUPT | PHY_RST_AFTER_CLK_EN,
  249. .probe = smsc_phy_probe,
  250. /* basic functions */
  251. .read_status = lan87xx_read_status,
  252. .config_init = smsc_phy_config_init,
  253. .soft_reset = smsc_phy_reset,
  254. /* IRQ related */
  255. .ack_interrupt = smsc_phy_ack_interrupt,
  256. .config_intr = smsc_phy_config_intr,
  257. /* Statistics */
  258. .get_sset_count = smsc_get_sset_count,
  259. .get_strings = smsc_get_strings,
  260. .get_stats = smsc_get_stats,
  261. .suspend = genphy_suspend,
  262. .resume = genphy_resume,
  263. }, {
  264. .phy_id = 0x0007c110,
  265. .phy_id_mask = 0xfffffff0,
  266. .name = "SMSC LAN8740",
  267. .features = PHY_BASIC_FEATURES,
  268. .flags = PHY_HAS_INTERRUPT,
  269. .probe = smsc_phy_probe,
  270. /* basic functions */
  271. .read_status = lan87xx_read_status,
  272. .config_init = smsc_phy_config_init,
  273. .soft_reset = smsc_phy_reset,
  274. /* IRQ related */
  275. .ack_interrupt = smsc_phy_ack_interrupt,
  276. .config_intr = smsc_phy_config_intr,
  277. /* Statistics */
  278. .get_sset_count = smsc_get_sset_count,
  279. .get_strings = smsc_get_strings,
  280. .get_stats = smsc_get_stats,
  281. .suspend = genphy_suspend,
  282. .resume = genphy_resume,
  283. } };
  284. module_phy_driver(smsc_phy_driver);
  285. MODULE_DESCRIPTION("SMSC PHY driver");
  286. MODULE_AUTHOR("Herbert Valerio Riedel");
  287. MODULE_LICENSE("GPL");
  288. static struct mdio_device_id __maybe_unused smsc_tbl[] = {
  289. { 0x0007c0a0, 0xfffffff0 },
  290. { 0x0007c0b0, 0xfffffff0 },
  291. { 0x0007c0c0, 0xfffffff0 },
  292. { 0x0007c0d0, 0xfffffff0 },
  293. { 0x0007c0f0, 0xfffffff0 },
  294. { 0x0007c110, 0xfffffff0 },
  295. { }
  296. };
  297. MODULE_DEVICE_TABLE(mdio, smsc_tbl);