mii.c 13 KB

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