smsc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #ifndef UINT64_MAX
  142. #define UINT64_MAX (u64)(~((u64)0))
  143. #endif
  144. static u64 smsc_get_stat(struct phy_device *phydev, int i)
  145. {
  146. struct smsc_hw_stat stat = smsc_hw_stats[i];
  147. int val;
  148. u64 ret;
  149. val = phy_read(phydev, stat.reg);
  150. if (val < 0)
  151. ret = UINT64_MAX;
  152. else
  153. ret = val;
  154. return ret;
  155. }
  156. static void smsc_get_stats(struct phy_device *phydev,
  157. struct ethtool_stats *stats, u64 *data)
  158. {
  159. int i;
  160. for (i = 0; i < ARRAY_SIZE(smsc_hw_stats); i++)
  161. data[i] = smsc_get_stat(phydev, i);
  162. }
  163. static int smsc_phy_probe(struct phy_device *phydev)
  164. {
  165. struct device *dev = &phydev->mdio.dev;
  166. struct device_node *of_node = dev->of_node;
  167. struct smsc_phy_priv *priv;
  168. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  169. if (!priv)
  170. return -ENOMEM;
  171. priv->energy_enable = true;
  172. if (of_property_read_bool(of_node, "smsc,disable-energy-detect"))
  173. priv->energy_enable = false;
  174. phydev->priv = priv;
  175. return 0;
  176. }
  177. static struct phy_driver smsc_phy_driver[] = {
  178. {
  179. .phy_id = 0x0007c0a0, /* OUI=0x00800f, Model#=0x0a */
  180. .phy_id_mask = 0xfffffff0,
  181. .name = "SMSC LAN83C185",
  182. .features = PHY_BASIC_FEATURES,
  183. .flags = PHY_HAS_INTERRUPT,
  184. .probe = smsc_phy_probe,
  185. /* basic functions */
  186. .config_aneg = genphy_config_aneg,
  187. .read_status = genphy_read_status,
  188. .config_init = smsc_phy_config_init,
  189. .soft_reset = smsc_phy_reset,
  190. /* IRQ related */
  191. .ack_interrupt = smsc_phy_ack_interrupt,
  192. .config_intr = smsc_phy_config_intr,
  193. .suspend = genphy_suspend,
  194. .resume = genphy_resume,
  195. }, {
  196. .phy_id = 0x0007c0b0, /* OUI=0x00800f, Model#=0x0b */
  197. .phy_id_mask = 0xfffffff0,
  198. .name = "SMSC LAN8187",
  199. .features = PHY_BASIC_FEATURES,
  200. .flags = PHY_HAS_INTERRUPT,
  201. .probe = smsc_phy_probe,
  202. /* basic functions */
  203. .config_aneg = genphy_config_aneg,
  204. .read_status = genphy_read_status,
  205. .config_init = smsc_phy_config_init,
  206. .soft_reset = smsc_phy_reset,
  207. /* IRQ related */
  208. .ack_interrupt = smsc_phy_ack_interrupt,
  209. .config_intr = smsc_phy_config_intr,
  210. /* Statistics */
  211. .get_sset_count = smsc_get_sset_count,
  212. .get_strings = smsc_get_strings,
  213. .get_stats = smsc_get_stats,
  214. .suspend = genphy_suspend,
  215. .resume = genphy_resume,
  216. }, {
  217. .phy_id = 0x0007c0c0, /* OUI=0x00800f, Model#=0x0c */
  218. .phy_id_mask = 0xfffffff0,
  219. .name = "SMSC LAN8700",
  220. .features = PHY_BASIC_FEATURES,
  221. .flags = PHY_HAS_INTERRUPT,
  222. .probe = smsc_phy_probe,
  223. /* basic functions */
  224. .config_aneg = genphy_config_aneg,
  225. .read_status = lan87xx_read_status,
  226. .config_init = smsc_phy_config_init,
  227. .soft_reset = smsc_phy_reset,
  228. /* IRQ related */
  229. .ack_interrupt = smsc_phy_ack_interrupt,
  230. .config_intr = smsc_phy_config_intr,
  231. /* Statistics */
  232. .get_sset_count = smsc_get_sset_count,
  233. .get_strings = smsc_get_strings,
  234. .get_stats = smsc_get_stats,
  235. .suspend = genphy_suspend,
  236. .resume = genphy_resume,
  237. }, {
  238. .phy_id = 0x0007c0d0, /* OUI=0x00800f, Model#=0x0d */
  239. .phy_id_mask = 0xfffffff0,
  240. .name = "SMSC LAN911x Internal PHY",
  241. .features = PHY_BASIC_FEATURES,
  242. .flags = PHY_HAS_INTERRUPT,
  243. .probe = smsc_phy_probe,
  244. /* basic functions */
  245. .config_aneg = genphy_config_aneg,
  246. .read_status = genphy_read_status,
  247. .config_init = lan911x_config_init,
  248. /* IRQ related */
  249. .ack_interrupt = smsc_phy_ack_interrupt,
  250. .config_intr = smsc_phy_config_intr,
  251. .suspend = genphy_suspend,
  252. .resume = genphy_resume,
  253. }, {
  254. .phy_id = 0x0007c0f0, /* OUI=0x00800f, Model#=0x0f */
  255. .phy_id_mask = 0xfffffff0,
  256. .name = "SMSC LAN8710/LAN8720",
  257. .features = PHY_BASIC_FEATURES,
  258. .flags = PHY_HAS_INTERRUPT,
  259. .probe = smsc_phy_probe,
  260. /* basic functions */
  261. .config_aneg = genphy_config_aneg,
  262. .read_status = lan87xx_read_status,
  263. .config_init = smsc_phy_config_init,
  264. .soft_reset = smsc_phy_reset,
  265. /* IRQ related */
  266. .ack_interrupt = smsc_phy_ack_interrupt,
  267. .config_intr = smsc_phy_config_intr,
  268. /* Statistics */
  269. .get_sset_count = smsc_get_sset_count,
  270. .get_strings = smsc_get_strings,
  271. .get_stats = smsc_get_stats,
  272. .suspend = genphy_suspend,
  273. .resume = genphy_resume,
  274. }, {
  275. .phy_id = 0x0007c110,
  276. .phy_id_mask = 0xfffffff0,
  277. .name = "SMSC LAN8740",
  278. .features = PHY_BASIC_FEATURES,
  279. .flags = PHY_HAS_INTERRUPT,
  280. .probe = smsc_phy_probe,
  281. /* basic functions */
  282. .config_aneg = genphy_config_aneg,
  283. .read_status = lan87xx_read_status,
  284. .config_init = smsc_phy_config_init,
  285. .soft_reset = smsc_phy_reset,
  286. /* IRQ related */
  287. .ack_interrupt = smsc_phy_ack_interrupt,
  288. .config_intr = smsc_phy_config_intr,
  289. /* Statistics */
  290. .get_sset_count = smsc_get_sset_count,
  291. .get_strings = smsc_get_strings,
  292. .get_stats = smsc_get_stats,
  293. .suspend = genphy_suspend,
  294. .resume = genphy_resume,
  295. } };
  296. module_phy_driver(smsc_phy_driver);
  297. MODULE_DESCRIPTION("SMSC PHY driver");
  298. MODULE_AUTHOR("Herbert Valerio Riedel");
  299. MODULE_LICENSE("GPL");
  300. static struct mdio_device_id __maybe_unused smsc_tbl[] = {
  301. { 0x0007c0a0, 0xfffffff0 },
  302. { 0x0007c0b0, 0xfffffff0 },
  303. { 0x0007c0c0, 0xfffffff0 },
  304. { 0x0007c0d0, 0xfffffff0 },
  305. { 0x0007c0f0, 0xfffffff0 },
  306. { 0x0007c110, 0xfffffff0 },
  307. { }
  308. };
  309. MODULE_DEVICE_TABLE(mdio, smsc_tbl);