mii.c 12 KB

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