mii.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. mii.c: MII interface library
  3. Maintained by Jeff Garzik <jgarzik@pobox.com>
  4. Copyright 2001,2002 Jeff Garzik
  5. Various code came from myson803.c and other files by
  6. Donald Becker. Copyright:
  7. Written 1998-2002 by Donald Becker.
  8. This software may be used and distributed according
  9. to the terms of the GNU General Public License (GPL),
  10. incorporated herein by reference. Drivers based on
  11. or derived from this code fall under the GPL and must
  12. retain the authorship, copyright and license notice.
  13. This file is not a complete program and may only be
  14. used when the entire operating system is licensed
  15. under the GPL.
  16. The author may be reached as becker@scyld.com, or C/O
  17. Scyld Computing Corporation
  18. 410 Severn Ave., Suite 210
  19. Annapolis MD 21403
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/mdio.h>
  26. static u32 mii_get_an(struct mii_if_info *mii, u16 addr)
  27. {
  28. u32 result = 0;
  29. int advert;
  30. advert = mii->mdio_read(mii->dev, mii->phy_id, addr);
  31. if (advert & LPA_LPACK)
  32. result |= ADVERTISED_Autoneg;
  33. if (advert & ADVERTISE_10HALF)
  34. result |= ADVERTISED_10baseT_Half;
  35. if (advert & ADVERTISE_10FULL)
  36. result |= ADVERTISED_10baseT_Full;
  37. if (advert & ADVERTISE_100HALF)
  38. result |= ADVERTISED_100baseT_Half;
  39. if (advert & ADVERTISE_100FULL)
  40. result |= ADVERTISED_100baseT_Full;
  41. if (advert & ADVERTISE_PAUSE_CAP)
  42. result |= ADVERTISED_Pause;
  43. if (advert & ADVERTISE_PAUSE_ASYM)
  44. result |= ADVERTISED_Asym_Pause;
  45. return result;
  46. }
  47. /**
  48. * mii_ethtool_gset - get settings that are specified in @ecmd
  49. * @mii: MII interface
  50. * @ecmd: requested ethtool_cmd
  51. *
  52. * The @ecmd parameter is expected to have been cleared before calling
  53. * mii_ethtool_gset().
  54. *
  55. * Returns 0 for success, negative on error.
  56. */
  57. int mii_ethtool_gset(struct mii_if_info *mii, struct ethtool_cmd *ecmd)
  58. {
  59. struct net_device *dev = mii->dev;
  60. u16 bmcr, bmsr, ctrl1000 = 0, stat1000 = 0;
  61. u32 nego;
  62. ecmd->supported =
  63. (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  64. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
  65. SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
  66. if (mii->supports_gmii)
  67. ecmd->supported |= SUPPORTED_1000baseT_Half |
  68. SUPPORTED_1000baseT_Full;
  69. /* only supports twisted-pair */
  70. ecmd->port = PORT_MII;
  71. /* only supports internal transceiver */
  72. ecmd->transceiver = XCVR_INTERNAL;
  73. /* this isn't fully supported at higher layers */
  74. ecmd->phy_address = mii->phy_id;
  75. ecmd->mdio_support = MDIO_SUPPORTS_C22;
  76. ecmd->advertising = ADVERTISED_TP | ADVERTISED_MII;
  77. bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
  78. bmsr = mii->mdio_read(dev, mii->phy_id, MII_BMSR);
  79. if (mii->supports_gmii) {
  80. ctrl1000 = mii->mdio_read(dev, mii->phy_id, MII_CTRL1000);
  81. stat1000 = mii->mdio_read(dev, mii->phy_id, MII_STAT1000);
  82. }
  83. if (bmcr & BMCR_ANENABLE) {
  84. ecmd->advertising |= ADVERTISED_Autoneg;
  85. ecmd->autoneg = AUTONEG_ENABLE;
  86. ecmd->advertising |= mii_get_an(mii, MII_ADVERTISE);
  87. if (ctrl1000 & ADVERTISE_1000HALF)
  88. ecmd->advertising |= ADVERTISED_1000baseT_Half;
  89. if (ctrl1000 & ADVERTISE_1000FULL)
  90. ecmd->advertising |= ADVERTISED_1000baseT_Full;
  91. if (bmsr & BMSR_ANEGCOMPLETE) {
  92. ecmd->lp_advertising = mii_get_an(mii, MII_LPA);
  93. if (stat1000 & LPA_1000HALF)
  94. ecmd->lp_advertising |=
  95. ADVERTISED_1000baseT_Half;
  96. if (stat1000 & LPA_1000FULL)
  97. ecmd->lp_advertising |=
  98. ADVERTISED_1000baseT_Full;
  99. } else {
  100. ecmd->lp_advertising = 0;
  101. }
  102. nego = ecmd->advertising & ecmd->lp_advertising;
  103. if (nego & (ADVERTISED_1000baseT_Full |
  104. ADVERTISED_1000baseT_Half)) {
  105. ethtool_cmd_speed_set(ecmd, SPEED_1000);
  106. ecmd->duplex = !!(nego & ADVERTISED_1000baseT_Full);
  107. } else if (nego & (ADVERTISED_100baseT_Full |
  108. ADVERTISED_100baseT_Half)) {
  109. ethtool_cmd_speed_set(ecmd, SPEED_100);
  110. ecmd->duplex = !!(nego & ADVERTISED_100baseT_Full);
  111. } else {
  112. ethtool_cmd_speed_set(ecmd, SPEED_10);
  113. ecmd->duplex = !!(nego & ADVERTISED_10baseT_Full);
  114. }
  115. } else {
  116. ecmd->autoneg = AUTONEG_DISABLE;
  117. ethtool_cmd_speed_set(ecmd,
  118. ((bmcr & BMCR_SPEED1000 &&
  119. (bmcr & BMCR_SPEED100) == 0) ?
  120. SPEED_1000 :
  121. ((bmcr & BMCR_SPEED100) ?
  122. SPEED_100 : SPEED_10)));
  123. ecmd->duplex = (bmcr & BMCR_FULLDPLX) ? DUPLEX_FULL : DUPLEX_HALF;
  124. }
  125. mii->full_duplex = ecmd->duplex;
  126. /* ignore maxtxpkt, maxrxpkt for now */
  127. return 0;
  128. }
  129. /**
  130. * mii_ethtool_sset - set settings that are specified in @ecmd
  131. * @mii: MII interface
  132. * @ecmd: requested ethtool_cmd
  133. *
  134. * Returns 0 for success, negative on error.
  135. */
  136. int mii_ethtool_sset(struct mii_if_info *mii, struct ethtool_cmd *ecmd)
  137. {
  138. struct net_device *dev = mii->dev;
  139. u32 speed = ethtool_cmd_speed(ecmd);
  140. if (speed != SPEED_10 &&
  141. speed != SPEED_100 &&
  142. speed != SPEED_1000)
  143. return -EINVAL;
  144. if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
  145. return -EINVAL;
  146. if (ecmd->port != PORT_MII)
  147. return -EINVAL;
  148. if (ecmd->transceiver != XCVR_INTERNAL)
  149. return -EINVAL;
  150. if (ecmd->phy_address != mii->phy_id)
  151. return -EINVAL;
  152. if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
  153. return -EINVAL;
  154. if ((speed == SPEED_1000) && (!mii->supports_gmii))
  155. return -EINVAL;
  156. /* ignore supported, maxtxpkt, maxrxpkt */
  157. if (ecmd->autoneg == AUTONEG_ENABLE) {
  158. u32 bmcr, advert, tmp;
  159. u32 advert2 = 0, tmp2 = 0;
  160. if ((ecmd->advertising & (ADVERTISED_10baseT_Half |
  161. ADVERTISED_10baseT_Full |
  162. ADVERTISED_100baseT_Half |
  163. ADVERTISED_100baseT_Full |
  164. ADVERTISED_1000baseT_Half |
  165. ADVERTISED_1000baseT_Full)) == 0)
  166. return -EINVAL;
  167. /* advertise only what has been requested */
  168. advert = mii->mdio_read(dev, mii->phy_id, MII_ADVERTISE);
  169. tmp = advert & ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
  170. if (mii->supports_gmii) {
  171. advert2 = mii->mdio_read(dev, mii->phy_id, MII_CTRL1000);
  172. tmp2 = advert2 & ~(ADVERTISE_1000HALF | ADVERTISE_1000FULL);
  173. }
  174. if (ecmd->advertising & ADVERTISED_10baseT_Half)
  175. tmp |= ADVERTISE_10HALF;
  176. if (ecmd->advertising & ADVERTISED_10baseT_Full)
  177. tmp |= ADVERTISE_10FULL;
  178. if (ecmd->advertising & ADVERTISED_100baseT_Half)
  179. tmp |= ADVERTISE_100HALF;
  180. if (ecmd->advertising & ADVERTISED_100baseT_Full)
  181. tmp |= ADVERTISE_100FULL;
  182. if (mii->supports_gmii) {
  183. if (ecmd->advertising & ADVERTISED_1000baseT_Half)
  184. tmp2 |= ADVERTISE_1000HALF;
  185. if (ecmd->advertising & ADVERTISED_1000baseT_Full)
  186. tmp2 |= ADVERTISE_1000FULL;
  187. }
  188. if (advert != tmp) {
  189. mii->mdio_write(dev, mii->phy_id, MII_ADVERTISE, tmp);
  190. mii->advertising = tmp;
  191. }
  192. if ((mii->supports_gmii) && (advert2 != tmp2))
  193. mii->mdio_write(dev, mii->phy_id, MII_CTRL1000, tmp2);
  194. /* turn on autonegotiation, and force a renegotiate */
  195. bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
  196. bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
  197. mii->mdio_write(dev, mii->phy_id, MII_BMCR, bmcr);
  198. mii->force_media = 0;
  199. } else {
  200. u32 bmcr, tmp;
  201. /* turn off auto negotiation, set speed and duplexity */
  202. bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
  203. tmp = bmcr & ~(BMCR_ANENABLE | BMCR_SPEED100 |
  204. BMCR_SPEED1000 | BMCR_FULLDPLX);
  205. if (speed == SPEED_1000)
  206. tmp |= BMCR_SPEED1000;
  207. else if (speed == SPEED_100)
  208. tmp |= BMCR_SPEED100;
  209. if (ecmd->duplex == DUPLEX_FULL) {
  210. tmp |= BMCR_FULLDPLX;
  211. mii->full_duplex = 1;
  212. } else
  213. mii->full_duplex = 0;
  214. if (bmcr != tmp)
  215. mii->mdio_write(dev, mii->phy_id, MII_BMCR, tmp);
  216. mii->force_media = 1;
  217. }
  218. return 0;
  219. }
  220. /**
  221. * mii_check_gmii_support - check if the MII supports Gb interfaces
  222. * @mii: the MII interface
  223. */
  224. int mii_check_gmii_support(struct mii_if_info *mii)
  225. {
  226. int reg;
  227. reg = mii->mdio_read(mii->dev, mii->phy_id, MII_BMSR);
  228. if (reg & BMSR_ESTATEN) {
  229. reg = mii->mdio_read(mii->dev, mii->phy_id, MII_ESTATUS);
  230. if (reg & (ESTATUS_1000_TFULL | ESTATUS_1000_THALF))
  231. return 1;
  232. }
  233. return 0;
  234. }
  235. /**
  236. * mii_link_ok - is link status up/ok
  237. * @mii: the MII interface
  238. *
  239. * Returns 1 if the MII reports link status up/ok, 0 otherwise.
  240. */
  241. int mii_link_ok (struct mii_if_info *mii)
  242. {
  243. /* first, a dummy read, needed to latch some MII phys */
  244. mii->mdio_read(mii->dev, mii->phy_id, MII_BMSR);
  245. if (mii->mdio_read(mii->dev, mii->phy_id, MII_BMSR) & BMSR_LSTATUS)
  246. return 1;
  247. return 0;
  248. }
  249. /**
  250. * mii_nway_restart - restart NWay (autonegotiation) for this interface
  251. * @mii: the MII interface
  252. *
  253. * Returns 0 on success, negative on error.
  254. */
  255. int mii_nway_restart (struct mii_if_info *mii)
  256. {
  257. int bmcr;
  258. int r = -EINVAL;
  259. /* if autoneg is off, it's an error */
  260. bmcr = mii->mdio_read(mii->dev, mii->phy_id, MII_BMCR);
  261. if (bmcr & BMCR_ANENABLE) {
  262. bmcr |= BMCR_ANRESTART;
  263. mii->mdio_write(mii->dev, mii->phy_id, MII_BMCR, bmcr);
  264. r = 0;
  265. }
  266. return r;
  267. }
  268. /**
  269. * mii_check_link - check MII link status
  270. * @mii: MII interface
  271. *
  272. * If the link status changed (previous != current), call
  273. * netif_carrier_on() if current link status is Up or call
  274. * netif_carrier_off() if current link status is Down.
  275. */
  276. void mii_check_link (struct mii_if_info *mii)
  277. {
  278. int cur_link = mii_link_ok(mii);
  279. int prev_link = netif_carrier_ok(mii->dev);
  280. if (cur_link && !prev_link)
  281. netif_carrier_on(mii->dev);
  282. else if (prev_link && !cur_link)
  283. netif_carrier_off(mii->dev);
  284. }
  285. /**
  286. * mii_check_media - check the MII interface for a duplex change
  287. * @mii: the MII interface
  288. * @ok_to_print: OK to print link up/down messages
  289. * @init_media: OK to save duplex mode in @mii
  290. *
  291. * Returns 1 if the duplex mode changed, 0 if not.
  292. * If the media type is forced, always returns 0.
  293. */
  294. unsigned int mii_check_media (struct mii_if_info *mii,
  295. unsigned int ok_to_print,
  296. unsigned int init_media)
  297. {
  298. unsigned int old_carrier, new_carrier;
  299. int advertise, lpa, media, duplex;
  300. int lpa2 = 0;
  301. /* if forced media, go no further */
  302. if (mii->force_media)
  303. return 0; /* duplex did not change */
  304. /* check current and old link status */
  305. old_carrier = netif_carrier_ok(mii->dev) ? 1 : 0;
  306. new_carrier = (unsigned int) mii_link_ok(mii);
  307. /* if carrier state did not change, this is a "bounce",
  308. * just exit as everything is already set correctly
  309. */
  310. if ((!init_media) && (old_carrier == new_carrier))
  311. return 0; /* duplex did not change */
  312. /* no carrier, nothing much to do */
  313. if (!new_carrier) {
  314. netif_carrier_off(mii->dev);
  315. if (ok_to_print)
  316. netdev_info(mii->dev, "link down\n");
  317. return 0; /* duplex did not change */
  318. }
  319. /*
  320. * we have carrier, see who's on the other end
  321. */
  322. netif_carrier_on(mii->dev);
  323. /* get MII advertise and LPA values */
  324. if ((!init_media) && (mii->advertising))
  325. advertise = mii->advertising;
  326. else {
  327. advertise = mii->mdio_read(mii->dev, mii->phy_id, MII_ADVERTISE);
  328. mii->advertising = advertise;
  329. }
  330. lpa = mii->mdio_read(mii->dev, mii->phy_id, MII_LPA);
  331. if (mii->supports_gmii)
  332. lpa2 = mii->mdio_read(mii->dev, mii->phy_id, MII_STAT1000);
  333. /* figure out media and duplex from advertise and LPA values */
  334. media = mii_nway_result(lpa & advertise);
  335. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  336. if (lpa2 & LPA_1000FULL)
  337. duplex = 1;
  338. if (ok_to_print)
  339. netdev_info(mii->dev, "link up, %uMbps, %s-duplex, lpa 0x%04X\n",
  340. lpa2 & (LPA_1000FULL | LPA_1000HALF) ? 1000 :
  341. media & (ADVERTISE_100FULL | ADVERTISE_100HALF) ?
  342. 100 : 10,
  343. duplex ? "full" : "half",
  344. lpa);
  345. if ((init_media) || (mii->full_duplex != duplex)) {
  346. mii->full_duplex = duplex;
  347. return 1; /* duplex changed */
  348. }
  349. return 0; /* duplex did not change */
  350. }
  351. /**
  352. * generic_mii_ioctl - main MII ioctl interface
  353. * @mii_if: the MII interface
  354. * @mii_data: MII ioctl data structure
  355. * @cmd: MII ioctl command
  356. * @duplex_chg_out: pointer to @duplex_changed status if there was no
  357. * ioctl error
  358. *
  359. * Returns 0 on success, negative on error.
  360. */
  361. int generic_mii_ioctl(struct mii_if_info *mii_if,
  362. struct mii_ioctl_data *mii_data, int cmd,
  363. unsigned int *duplex_chg_out)
  364. {
  365. int rc = 0;
  366. unsigned int duplex_changed = 0;
  367. if (duplex_chg_out)
  368. *duplex_chg_out = 0;
  369. mii_data->phy_id &= mii_if->phy_id_mask;
  370. mii_data->reg_num &= mii_if->reg_num_mask;
  371. switch(cmd) {
  372. case SIOCGMIIPHY:
  373. mii_data->phy_id = mii_if->phy_id;
  374. /* fall through */
  375. case SIOCGMIIREG:
  376. mii_data->val_out =
  377. mii_if->mdio_read(mii_if->dev, mii_data->phy_id,
  378. mii_data->reg_num);
  379. break;
  380. case SIOCSMIIREG: {
  381. u16 val = mii_data->val_in;
  382. if (mii_data->phy_id == mii_if->phy_id) {
  383. switch(mii_data->reg_num) {
  384. case MII_BMCR: {
  385. unsigned int new_duplex = 0;
  386. if (val & (BMCR_RESET|BMCR_ANENABLE))
  387. mii_if->force_media = 0;
  388. else
  389. mii_if->force_media = 1;
  390. if (mii_if->force_media &&
  391. (val & BMCR_FULLDPLX))
  392. new_duplex = 1;
  393. if (mii_if->full_duplex != new_duplex) {
  394. duplex_changed = 1;
  395. mii_if->full_duplex = new_duplex;
  396. }
  397. break;
  398. }
  399. case MII_ADVERTISE:
  400. mii_if->advertising = val;
  401. break;
  402. default:
  403. /* do nothing */
  404. break;
  405. }
  406. }
  407. mii_if->mdio_write(mii_if->dev, mii_data->phy_id,
  408. mii_data->reg_num, val);
  409. break;
  410. }
  411. default:
  412. rc = -EOPNOTSUPP;
  413. break;
  414. }
  415. if ((rc == 0) && (duplex_chg_out) && (duplex_changed))
  416. *duplex_chg_out = 1;
  417. return rc;
  418. }
  419. MODULE_AUTHOR ("Jeff Garzik <jgarzik@pobox.com>");
  420. MODULE_DESCRIPTION ("MII hardware support library");
  421. MODULE_LICENSE("GPL");
  422. EXPORT_SYMBOL(mii_link_ok);
  423. EXPORT_SYMBOL(mii_nway_restart);
  424. EXPORT_SYMBOL(mii_ethtool_gset);
  425. EXPORT_SYMBOL(mii_ethtool_sset);
  426. EXPORT_SYMBOL(mii_check_link);
  427. EXPORT_SYMBOL(mii_check_media);
  428. EXPORT_SYMBOL(mii_check_gmii_support);
  429. EXPORT_SYMBOL(generic_mii_ioctl);