sungem_phy.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /*
  2. * PHY drivers for the sungem ethernet driver.
  3. *
  4. * This file could be shared with other drivers.
  5. *
  6. * (c) 2002-2007, Benjamin Herrenscmidt (benh@kernel.crashing.org)
  7. *
  8. * TODO:
  9. * - Add support for PHYs that provide an IRQ line
  10. * - Eventually moved the entire polling state machine in
  11. * there (out of the eth driver), so that it can easily be
  12. * skipped on PHYs that implement it in hardware.
  13. * - On LXT971 & BCM5201, Apple uses some chip specific regs
  14. * to read the link status. Figure out why and if it makes
  15. * sense to do the same (magic aneg ?)
  16. * - Apple has some additional power management code for some
  17. * Broadcom PHYs that they "hide" from the OpenSource version
  18. * of darwin, still need to reverse engineer that
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/types.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/mii.h>
  27. #include <linux/ethtool.h>
  28. #include <linux/delay.h>
  29. #ifdef CONFIG_PPC_PMAC
  30. #include <asm/prom.h>
  31. #endif
  32. #include "sungem_phy.h"
  33. /* Link modes of the BCM5400 PHY */
  34. static const int phy_BCM5400_link_table[8][3] = {
  35. { 0, 0, 0 }, /* No link */
  36. { 0, 0, 0 }, /* 10BT Half Duplex */
  37. { 1, 0, 0 }, /* 10BT Full Duplex */
  38. { 0, 1, 0 }, /* 100BT Half Duplex */
  39. { 0, 1, 0 }, /* 100BT Half Duplex */
  40. { 1, 1, 0 }, /* 100BT Full Duplex*/
  41. { 1, 0, 1 }, /* 1000BT */
  42. { 1, 0, 1 }, /* 1000BT */
  43. };
  44. static inline int __phy_read(struct mii_phy* phy, int id, int reg)
  45. {
  46. return phy->mdio_read(phy->dev, id, reg);
  47. }
  48. static inline void __phy_write(struct mii_phy* phy, int id, int reg, int val)
  49. {
  50. phy->mdio_write(phy->dev, id, reg, val);
  51. }
  52. static inline int phy_read(struct mii_phy* phy, int reg)
  53. {
  54. return phy->mdio_read(phy->dev, phy->mii_id, reg);
  55. }
  56. static inline void phy_write(struct mii_phy* phy, int reg, int val)
  57. {
  58. phy->mdio_write(phy->dev, phy->mii_id, reg, val);
  59. }
  60. static int reset_one_mii_phy(struct mii_phy* phy, int phy_id)
  61. {
  62. u16 val;
  63. int limit = 10000;
  64. val = __phy_read(phy, phy_id, MII_BMCR);
  65. val &= ~(BMCR_ISOLATE | BMCR_PDOWN);
  66. val |= BMCR_RESET;
  67. __phy_write(phy, phy_id, MII_BMCR, val);
  68. udelay(100);
  69. while (limit--) {
  70. val = __phy_read(phy, phy_id, MII_BMCR);
  71. if ((val & BMCR_RESET) == 0)
  72. break;
  73. udelay(10);
  74. }
  75. if ((val & BMCR_ISOLATE) && limit > 0)
  76. __phy_write(phy, phy_id, MII_BMCR, val & ~BMCR_ISOLATE);
  77. return (limit <= 0);
  78. }
  79. static int bcm5201_init(struct mii_phy* phy)
  80. {
  81. u16 data;
  82. data = phy_read(phy, MII_BCM5201_MULTIPHY);
  83. data &= ~MII_BCM5201_MULTIPHY_SUPERISOLATE;
  84. phy_write(phy, MII_BCM5201_MULTIPHY, data);
  85. phy_write(phy, MII_BCM5201_INTERRUPT, 0);
  86. return 0;
  87. }
  88. static int bcm5201_suspend(struct mii_phy* phy)
  89. {
  90. phy_write(phy, MII_BCM5201_INTERRUPT, 0);
  91. phy_write(phy, MII_BCM5201_MULTIPHY, MII_BCM5201_MULTIPHY_SUPERISOLATE);
  92. return 0;
  93. }
  94. static int bcm5221_init(struct mii_phy* phy)
  95. {
  96. u16 data;
  97. data = phy_read(phy, MII_BCM5221_TEST);
  98. phy_write(phy, MII_BCM5221_TEST,
  99. data | MII_BCM5221_TEST_ENABLE_SHADOWS);
  100. data = phy_read(phy, MII_BCM5221_SHDOW_AUX_STAT2);
  101. phy_write(phy, MII_BCM5221_SHDOW_AUX_STAT2,
  102. data | MII_BCM5221_SHDOW_AUX_STAT2_APD);
  103. data = phy_read(phy, MII_BCM5221_SHDOW_AUX_MODE4);
  104. phy_write(phy, MII_BCM5221_SHDOW_AUX_MODE4,
  105. data | MII_BCM5221_SHDOW_AUX_MODE4_CLKLOPWR);
  106. data = phy_read(phy, MII_BCM5221_TEST);
  107. phy_write(phy, MII_BCM5221_TEST,
  108. data & ~MII_BCM5221_TEST_ENABLE_SHADOWS);
  109. return 0;
  110. }
  111. static int bcm5221_suspend(struct mii_phy* phy)
  112. {
  113. u16 data;
  114. data = phy_read(phy, MII_BCM5221_TEST);
  115. phy_write(phy, MII_BCM5221_TEST,
  116. data | MII_BCM5221_TEST_ENABLE_SHADOWS);
  117. data = phy_read(phy, MII_BCM5221_SHDOW_AUX_MODE4);
  118. phy_write(phy, MII_BCM5221_SHDOW_AUX_MODE4,
  119. data | MII_BCM5221_SHDOW_AUX_MODE4_IDDQMODE);
  120. return 0;
  121. }
  122. static int bcm5241_init(struct mii_phy* phy)
  123. {
  124. u16 data;
  125. data = phy_read(phy, MII_BCM5221_TEST);
  126. phy_write(phy, MII_BCM5221_TEST,
  127. data | MII_BCM5221_TEST_ENABLE_SHADOWS);
  128. data = phy_read(phy, MII_BCM5221_SHDOW_AUX_STAT2);
  129. phy_write(phy, MII_BCM5221_SHDOW_AUX_STAT2,
  130. data | MII_BCM5221_SHDOW_AUX_STAT2_APD);
  131. data = phy_read(phy, MII_BCM5221_SHDOW_AUX_MODE4);
  132. phy_write(phy, MII_BCM5221_SHDOW_AUX_MODE4,
  133. data & ~MII_BCM5241_SHDOW_AUX_MODE4_STANDBYPWR);
  134. data = phy_read(phy, MII_BCM5221_TEST);
  135. phy_write(phy, MII_BCM5221_TEST,
  136. data & ~MII_BCM5221_TEST_ENABLE_SHADOWS);
  137. return 0;
  138. }
  139. static int bcm5241_suspend(struct mii_phy* phy)
  140. {
  141. u16 data;
  142. data = phy_read(phy, MII_BCM5221_TEST);
  143. phy_write(phy, MII_BCM5221_TEST,
  144. data | MII_BCM5221_TEST_ENABLE_SHADOWS);
  145. data = phy_read(phy, MII_BCM5221_SHDOW_AUX_MODE4);
  146. phy_write(phy, MII_BCM5221_SHDOW_AUX_MODE4,
  147. data | MII_BCM5241_SHDOW_AUX_MODE4_STANDBYPWR);
  148. return 0;
  149. }
  150. static int bcm5400_init(struct mii_phy* phy)
  151. {
  152. u16 data;
  153. /* Configure for gigabit full duplex */
  154. data = phy_read(phy, MII_BCM5400_AUXCONTROL);
  155. data |= MII_BCM5400_AUXCONTROL_PWR10BASET;
  156. phy_write(phy, MII_BCM5400_AUXCONTROL, data);
  157. data = phy_read(phy, MII_BCM5400_GB_CONTROL);
  158. data |= MII_BCM5400_GB_CONTROL_FULLDUPLEXCAP;
  159. phy_write(phy, MII_BCM5400_GB_CONTROL, data);
  160. udelay(100);
  161. /* Reset and configure cascaded 10/100 PHY */
  162. (void)reset_one_mii_phy(phy, 0x1f);
  163. data = __phy_read(phy, 0x1f, MII_BCM5201_MULTIPHY);
  164. data |= MII_BCM5201_MULTIPHY_SERIALMODE;
  165. __phy_write(phy, 0x1f, MII_BCM5201_MULTIPHY, data);
  166. data = phy_read(phy, MII_BCM5400_AUXCONTROL);
  167. data &= ~MII_BCM5400_AUXCONTROL_PWR10BASET;
  168. phy_write(phy, MII_BCM5400_AUXCONTROL, data);
  169. return 0;
  170. }
  171. static int bcm5400_suspend(struct mii_phy* phy)
  172. {
  173. #if 0 /* Commented out in Darwin... someone has those dawn docs ? */
  174. phy_write(phy, MII_BMCR, BMCR_PDOWN);
  175. #endif
  176. return 0;
  177. }
  178. static int bcm5401_init(struct mii_phy* phy)
  179. {
  180. u16 data;
  181. int rev;
  182. rev = phy_read(phy, MII_PHYSID2) & 0x000f;
  183. if (rev == 0 || rev == 3) {
  184. /* Some revisions of 5401 appear to need this
  185. * initialisation sequence to disable, according
  186. * to OF, "tap power management"
  187. *
  188. * WARNING ! OF and Darwin don't agree on the
  189. * register addresses. OF seem to interpret the
  190. * register numbers below as decimal
  191. *
  192. * Note: This should (and does) match tg3_init_5401phy_dsp
  193. * in the tg3.c driver. -DaveM
  194. */
  195. phy_write(phy, 0x18, 0x0c20);
  196. phy_write(phy, 0x17, 0x0012);
  197. phy_write(phy, 0x15, 0x1804);
  198. phy_write(phy, 0x17, 0x0013);
  199. phy_write(phy, 0x15, 0x1204);
  200. phy_write(phy, 0x17, 0x8006);
  201. phy_write(phy, 0x15, 0x0132);
  202. phy_write(phy, 0x17, 0x8006);
  203. phy_write(phy, 0x15, 0x0232);
  204. phy_write(phy, 0x17, 0x201f);
  205. phy_write(phy, 0x15, 0x0a20);
  206. }
  207. /* Configure for gigabit full duplex */
  208. data = phy_read(phy, MII_BCM5400_GB_CONTROL);
  209. data |= MII_BCM5400_GB_CONTROL_FULLDUPLEXCAP;
  210. phy_write(phy, MII_BCM5400_GB_CONTROL, data);
  211. udelay(10);
  212. /* Reset and configure cascaded 10/100 PHY */
  213. (void)reset_one_mii_phy(phy, 0x1f);
  214. data = __phy_read(phy, 0x1f, MII_BCM5201_MULTIPHY);
  215. data |= MII_BCM5201_MULTIPHY_SERIALMODE;
  216. __phy_write(phy, 0x1f, MII_BCM5201_MULTIPHY, data);
  217. return 0;
  218. }
  219. static int bcm5401_suspend(struct mii_phy* phy)
  220. {
  221. #if 0 /* Commented out in Darwin... someone has those dawn docs ? */
  222. phy_write(phy, MII_BMCR, BMCR_PDOWN);
  223. #endif
  224. return 0;
  225. }
  226. static int bcm5411_init(struct mii_phy* phy)
  227. {
  228. u16 data;
  229. /* Here's some more Apple black magic to setup
  230. * some voltage stuffs.
  231. */
  232. phy_write(phy, 0x1c, 0x8c23);
  233. phy_write(phy, 0x1c, 0x8ca3);
  234. phy_write(phy, 0x1c, 0x8c23);
  235. /* Here, Apple seems to want to reset it, do
  236. * it as well
  237. */
  238. phy_write(phy, MII_BMCR, BMCR_RESET);
  239. phy_write(phy, MII_BMCR, 0x1340);
  240. data = phy_read(phy, MII_BCM5400_GB_CONTROL);
  241. data |= MII_BCM5400_GB_CONTROL_FULLDUPLEXCAP;
  242. phy_write(phy, MII_BCM5400_GB_CONTROL, data);
  243. udelay(10);
  244. /* Reset and configure cascaded 10/100 PHY */
  245. (void)reset_one_mii_phy(phy, 0x1f);
  246. return 0;
  247. }
  248. static int generic_suspend(struct mii_phy* phy)
  249. {
  250. phy_write(phy, MII_BMCR, BMCR_PDOWN);
  251. return 0;
  252. }
  253. static int bcm5421_init(struct mii_phy* phy)
  254. {
  255. u16 data;
  256. unsigned int id;
  257. id = (phy_read(phy, MII_PHYSID1) << 16 | phy_read(phy, MII_PHYSID2));
  258. /* Revision 0 of 5421 needs some fixups */
  259. if (id == 0x002060e0) {
  260. /* This is borrowed from MacOS
  261. */
  262. phy_write(phy, 0x18, 0x1007);
  263. data = phy_read(phy, 0x18);
  264. phy_write(phy, 0x18, data | 0x0400);
  265. phy_write(phy, 0x18, 0x0007);
  266. data = phy_read(phy, 0x18);
  267. phy_write(phy, 0x18, data | 0x0800);
  268. phy_write(phy, 0x17, 0x000a);
  269. data = phy_read(phy, 0x15);
  270. phy_write(phy, 0x15, data | 0x0200);
  271. }
  272. /* Pick up some init code from OF for K2 version */
  273. if ((id & 0xfffffff0) == 0x002062e0) {
  274. phy_write(phy, 4, 0x01e1);
  275. phy_write(phy, 9, 0x0300);
  276. }
  277. /* Check if we can enable automatic low power */
  278. #ifdef CONFIG_PPC_PMAC
  279. if (phy->platform_data) {
  280. struct device_node *np = of_get_parent(phy->platform_data);
  281. int can_low_power = 1;
  282. if (np == NULL || get_property(np, "no-autolowpower", NULL))
  283. can_low_power = 0;
  284. if (can_low_power) {
  285. /* Enable automatic low-power */
  286. phy_write(phy, 0x1c, 0x9002);
  287. phy_write(phy, 0x1c, 0xa821);
  288. phy_write(phy, 0x1c, 0x941d);
  289. }
  290. }
  291. #endif /* CONFIG_PPC_PMAC */
  292. return 0;
  293. }
  294. static int bcm5421_enable_fiber(struct mii_phy* phy)
  295. {
  296. /* enable fiber mode */
  297. phy_write(phy, MII_NCONFIG, 0x9020);
  298. /* LEDs active in both modes, autosense prio = fiber */
  299. phy_write(phy, MII_NCONFIG, 0x945f);
  300. /* switch off fibre autoneg */
  301. phy_write(phy, MII_NCONFIG, 0xfc01);
  302. phy_write(phy, 0x0b, 0x0004);
  303. return 0;
  304. }
  305. static int bcm5461_enable_fiber(struct mii_phy* phy)
  306. {
  307. phy_write(phy, MII_NCONFIG, 0xfc0c);
  308. phy_write(phy, MII_BMCR, 0x4140);
  309. phy_write(phy, MII_NCONFIG, 0xfc0b);
  310. phy_write(phy, MII_BMCR, 0x0140);
  311. return 0;
  312. }
  313. static int bcm54xx_setup_aneg(struct mii_phy *phy, u32 advertise)
  314. {
  315. u16 ctl, adv;
  316. phy->autoneg = 1;
  317. phy->speed = SPEED_10;
  318. phy->duplex = DUPLEX_HALF;
  319. phy->pause = 0;
  320. phy->advertising = advertise;
  321. /* Setup standard advertise */
  322. adv = phy_read(phy, MII_ADVERTISE);
  323. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
  324. if (advertise & ADVERTISED_10baseT_Half)
  325. adv |= ADVERTISE_10HALF;
  326. if (advertise & ADVERTISED_10baseT_Full)
  327. adv |= ADVERTISE_10FULL;
  328. if (advertise & ADVERTISED_100baseT_Half)
  329. adv |= ADVERTISE_100HALF;
  330. if (advertise & ADVERTISED_100baseT_Full)
  331. adv |= ADVERTISE_100FULL;
  332. if (advertise & ADVERTISED_Pause)
  333. adv |= ADVERTISE_PAUSE_CAP;
  334. if (advertise & ADVERTISED_Asym_Pause)
  335. adv |= ADVERTISE_PAUSE_ASYM;
  336. phy_write(phy, MII_ADVERTISE, adv);
  337. /* Setup 1000BT advertise */
  338. adv = phy_read(phy, MII_1000BASETCONTROL);
  339. adv &= ~(MII_1000BASETCONTROL_FULLDUPLEXCAP|MII_1000BASETCONTROL_HALFDUPLEXCAP);
  340. if (advertise & SUPPORTED_1000baseT_Half)
  341. adv |= MII_1000BASETCONTROL_HALFDUPLEXCAP;
  342. if (advertise & SUPPORTED_1000baseT_Full)
  343. adv |= MII_1000BASETCONTROL_FULLDUPLEXCAP;
  344. phy_write(phy, MII_1000BASETCONTROL, adv);
  345. /* Start/Restart aneg */
  346. ctl = phy_read(phy, MII_BMCR);
  347. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  348. phy_write(phy, MII_BMCR, ctl);
  349. return 0;
  350. }
  351. static int bcm54xx_setup_forced(struct mii_phy *phy, int speed, int fd)
  352. {
  353. u16 ctl;
  354. phy->autoneg = 0;
  355. phy->speed = speed;
  356. phy->duplex = fd;
  357. phy->pause = 0;
  358. ctl = phy_read(phy, MII_BMCR);
  359. ctl &= ~(BMCR_FULLDPLX|BMCR_SPEED100|BMCR_SPD2|BMCR_ANENABLE);
  360. /* First reset the PHY */
  361. phy_write(phy, MII_BMCR, ctl | BMCR_RESET);
  362. /* Select speed & duplex */
  363. switch(speed) {
  364. case SPEED_10:
  365. break;
  366. case SPEED_100:
  367. ctl |= BMCR_SPEED100;
  368. break;
  369. case SPEED_1000:
  370. ctl |= BMCR_SPD2;
  371. }
  372. if (fd == DUPLEX_FULL)
  373. ctl |= BMCR_FULLDPLX;
  374. // XXX Should we set the sungem to GII now on 1000BT ?
  375. phy_write(phy, MII_BMCR, ctl);
  376. return 0;
  377. }
  378. static int bcm54xx_read_link(struct mii_phy *phy)
  379. {
  380. int link_mode;
  381. u16 val;
  382. if (phy->autoneg) {
  383. val = phy_read(phy, MII_BCM5400_AUXSTATUS);
  384. link_mode = ((val & MII_BCM5400_AUXSTATUS_LINKMODE_MASK) >>
  385. MII_BCM5400_AUXSTATUS_LINKMODE_SHIFT);
  386. phy->duplex = phy_BCM5400_link_table[link_mode][0] ?
  387. DUPLEX_FULL : DUPLEX_HALF;
  388. phy->speed = phy_BCM5400_link_table[link_mode][2] ?
  389. SPEED_1000 :
  390. (phy_BCM5400_link_table[link_mode][1] ?
  391. SPEED_100 : SPEED_10);
  392. val = phy_read(phy, MII_LPA);
  393. phy->pause = (phy->duplex == DUPLEX_FULL) &&
  394. ((val & LPA_PAUSE) != 0);
  395. }
  396. /* On non-aneg, we assume what we put in BMCR is the speed,
  397. * though magic-aneg shouldn't prevent this case from occurring
  398. */
  399. return 0;
  400. }
  401. static int marvell88e1111_init(struct mii_phy* phy)
  402. {
  403. u16 rev;
  404. /* magic init sequence for rev 0 */
  405. rev = phy_read(phy, MII_PHYSID2) & 0x000f;
  406. if (rev == 0) {
  407. phy_write(phy, 0x1d, 0x000a);
  408. phy_write(phy, 0x1e, 0x0821);
  409. phy_write(phy, 0x1d, 0x0006);
  410. phy_write(phy, 0x1e, 0x8600);
  411. phy_write(phy, 0x1d, 0x000b);
  412. phy_write(phy, 0x1e, 0x0100);
  413. phy_write(phy, 0x1d, 0x0004);
  414. phy_write(phy, 0x1e, 0x4850);
  415. }
  416. return 0;
  417. }
  418. static int marvell_setup_aneg(struct mii_phy *phy, u32 advertise)
  419. {
  420. u16 ctl, adv;
  421. phy->autoneg = 1;
  422. phy->speed = SPEED_10;
  423. phy->duplex = DUPLEX_HALF;
  424. phy->pause = 0;
  425. phy->advertising = advertise;
  426. /* Setup standard advertise */
  427. adv = phy_read(phy, MII_ADVERTISE);
  428. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
  429. if (advertise & ADVERTISED_10baseT_Half)
  430. adv |= ADVERTISE_10HALF;
  431. if (advertise & ADVERTISED_10baseT_Full)
  432. adv |= ADVERTISE_10FULL;
  433. if (advertise & ADVERTISED_100baseT_Half)
  434. adv |= ADVERTISE_100HALF;
  435. if (advertise & ADVERTISED_100baseT_Full)
  436. adv |= ADVERTISE_100FULL;
  437. if (advertise & ADVERTISED_Pause)
  438. adv |= ADVERTISE_PAUSE_CAP;
  439. if (advertise & ADVERTISED_Asym_Pause)
  440. adv |= ADVERTISE_PAUSE_ASYM;
  441. phy_write(phy, MII_ADVERTISE, adv);
  442. /* Setup 1000BT advertise & enable crossover detect
  443. * XXX How do we advertise 1000BT ? Darwin source is
  444. * confusing here, they read from specific control and
  445. * write to control... Someone has specs for those
  446. * beasts ?
  447. */
  448. adv = phy_read(phy, MII_M1011_PHY_SPEC_CONTROL);
  449. adv |= MII_M1011_PHY_SPEC_CONTROL_AUTO_MDIX;
  450. adv &= ~(MII_1000BASETCONTROL_FULLDUPLEXCAP |
  451. MII_1000BASETCONTROL_HALFDUPLEXCAP);
  452. if (advertise & SUPPORTED_1000baseT_Half)
  453. adv |= MII_1000BASETCONTROL_HALFDUPLEXCAP;
  454. if (advertise & SUPPORTED_1000baseT_Full)
  455. adv |= MII_1000BASETCONTROL_FULLDUPLEXCAP;
  456. phy_write(phy, MII_1000BASETCONTROL, adv);
  457. /* Start/Restart aneg */
  458. ctl = phy_read(phy, MII_BMCR);
  459. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  460. phy_write(phy, MII_BMCR, ctl);
  461. return 0;
  462. }
  463. static int marvell_setup_forced(struct mii_phy *phy, int speed, int fd)
  464. {
  465. u16 ctl, ctl2;
  466. phy->autoneg = 0;
  467. phy->speed = speed;
  468. phy->duplex = fd;
  469. phy->pause = 0;
  470. ctl = phy_read(phy, MII_BMCR);
  471. ctl &= ~(BMCR_FULLDPLX|BMCR_SPEED100|BMCR_SPD2|BMCR_ANENABLE);
  472. ctl |= BMCR_RESET;
  473. /* Select speed & duplex */
  474. switch(speed) {
  475. case SPEED_10:
  476. break;
  477. case SPEED_100:
  478. ctl |= BMCR_SPEED100;
  479. break;
  480. /* I'm not sure about the one below, again, Darwin source is
  481. * quite confusing and I lack chip specs
  482. */
  483. case SPEED_1000:
  484. ctl |= BMCR_SPD2;
  485. }
  486. if (fd == DUPLEX_FULL)
  487. ctl |= BMCR_FULLDPLX;
  488. /* Disable crossover. Again, the way Apple does it is strange,
  489. * though I don't assume they are wrong ;)
  490. */
  491. ctl2 = phy_read(phy, MII_M1011_PHY_SPEC_CONTROL);
  492. ctl2 &= ~(MII_M1011_PHY_SPEC_CONTROL_MANUAL_MDIX |
  493. MII_M1011_PHY_SPEC_CONTROL_AUTO_MDIX |
  494. MII_1000BASETCONTROL_FULLDUPLEXCAP |
  495. MII_1000BASETCONTROL_HALFDUPLEXCAP);
  496. if (speed == SPEED_1000)
  497. ctl2 |= (fd == DUPLEX_FULL) ?
  498. MII_1000BASETCONTROL_FULLDUPLEXCAP :
  499. MII_1000BASETCONTROL_HALFDUPLEXCAP;
  500. phy_write(phy, MII_1000BASETCONTROL, ctl2);
  501. // XXX Should we set the sungem to GII now on 1000BT ?
  502. phy_write(phy, MII_BMCR, ctl);
  503. return 0;
  504. }
  505. static int marvell_read_link(struct mii_phy *phy)
  506. {
  507. u16 status, pmask;
  508. if (phy->autoneg) {
  509. status = phy_read(phy, MII_M1011_PHY_SPEC_STATUS);
  510. if ((status & MII_M1011_PHY_SPEC_STATUS_RESOLVED) == 0)
  511. return -EAGAIN;
  512. if (status & MII_M1011_PHY_SPEC_STATUS_1000)
  513. phy->speed = SPEED_1000;
  514. else if (status & MII_M1011_PHY_SPEC_STATUS_100)
  515. phy->speed = SPEED_100;
  516. else
  517. phy->speed = SPEED_10;
  518. if (status & MII_M1011_PHY_SPEC_STATUS_FULLDUPLEX)
  519. phy->duplex = DUPLEX_FULL;
  520. else
  521. phy->duplex = DUPLEX_HALF;
  522. pmask = MII_M1011_PHY_SPEC_STATUS_TX_PAUSE |
  523. MII_M1011_PHY_SPEC_STATUS_RX_PAUSE;
  524. phy->pause = (status & pmask) == pmask;
  525. }
  526. /* On non-aneg, we assume what we put in BMCR is the speed,
  527. * though magic-aneg shouldn't prevent this case from occurring
  528. */
  529. return 0;
  530. }
  531. static int genmii_setup_aneg(struct mii_phy *phy, u32 advertise)
  532. {
  533. u16 ctl, adv;
  534. phy->autoneg = 1;
  535. phy->speed = SPEED_10;
  536. phy->duplex = DUPLEX_HALF;
  537. phy->pause = 0;
  538. phy->advertising = advertise;
  539. /* Setup standard advertise */
  540. adv = phy_read(phy, MII_ADVERTISE);
  541. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
  542. if (advertise & ADVERTISED_10baseT_Half)
  543. adv |= ADVERTISE_10HALF;
  544. if (advertise & ADVERTISED_10baseT_Full)
  545. adv |= ADVERTISE_10FULL;
  546. if (advertise & ADVERTISED_100baseT_Half)
  547. adv |= ADVERTISE_100HALF;
  548. if (advertise & ADVERTISED_100baseT_Full)
  549. adv |= ADVERTISE_100FULL;
  550. if (advertise & ADVERTISED_Pause)
  551. adv |= ADVERTISE_PAUSE_CAP;
  552. if (advertise & ADVERTISED_Asym_Pause)
  553. adv |= ADVERTISE_PAUSE_ASYM;
  554. phy_write(phy, MII_ADVERTISE, adv);
  555. /* Start/Restart aneg */
  556. ctl = phy_read(phy, MII_BMCR);
  557. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  558. phy_write(phy, MII_BMCR, ctl);
  559. return 0;
  560. }
  561. static int genmii_setup_forced(struct mii_phy *phy, int speed, int fd)
  562. {
  563. u16 ctl;
  564. phy->autoneg = 0;
  565. phy->speed = speed;
  566. phy->duplex = fd;
  567. phy->pause = 0;
  568. ctl = phy_read(phy, MII_BMCR);
  569. ctl &= ~(BMCR_FULLDPLX|BMCR_SPEED100|BMCR_ANENABLE);
  570. /* First reset the PHY */
  571. phy_write(phy, MII_BMCR, ctl | BMCR_RESET);
  572. /* Select speed & duplex */
  573. switch(speed) {
  574. case SPEED_10:
  575. break;
  576. case SPEED_100:
  577. ctl |= BMCR_SPEED100;
  578. break;
  579. case SPEED_1000:
  580. default:
  581. return -EINVAL;
  582. }
  583. if (fd == DUPLEX_FULL)
  584. ctl |= BMCR_FULLDPLX;
  585. phy_write(phy, MII_BMCR, ctl);
  586. return 0;
  587. }
  588. static int genmii_poll_link(struct mii_phy *phy)
  589. {
  590. u16 status;
  591. (void)phy_read(phy, MII_BMSR);
  592. status = phy_read(phy, MII_BMSR);
  593. if ((status & BMSR_LSTATUS) == 0)
  594. return 0;
  595. if (phy->autoneg && !(status & BMSR_ANEGCOMPLETE))
  596. return 0;
  597. return 1;
  598. }
  599. static int genmii_read_link(struct mii_phy *phy)
  600. {
  601. u16 lpa;
  602. if (phy->autoneg) {
  603. lpa = phy_read(phy, MII_LPA);
  604. if (lpa & (LPA_10FULL | LPA_100FULL))
  605. phy->duplex = DUPLEX_FULL;
  606. else
  607. phy->duplex = DUPLEX_HALF;
  608. if (lpa & (LPA_100FULL | LPA_100HALF))
  609. phy->speed = SPEED_100;
  610. else
  611. phy->speed = SPEED_10;
  612. phy->pause = (phy->duplex == DUPLEX_FULL) &&
  613. ((lpa & LPA_PAUSE) != 0);
  614. }
  615. /* On non-aneg, we assume what we put in BMCR is the speed,
  616. * though magic-aneg shouldn't prevent this case from occurring
  617. */
  618. return 0;
  619. }
  620. #define MII_BASIC_FEATURES \
  621. (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | \
  622. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | \
  623. SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII | \
  624. SUPPORTED_Pause)
  625. /* On gigabit capable PHYs, we advertise Pause support but not asym pause
  626. * support for now as I'm not sure it's supported and Darwin doesn't do
  627. * it neither. --BenH.
  628. */
  629. #define MII_GBIT_FEATURES \
  630. (MII_BASIC_FEATURES | \
  631. SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full)
  632. /* Broadcom BCM 5201 */
  633. static struct mii_phy_ops bcm5201_phy_ops = {
  634. .init = bcm5201_init,
  635. .suspend = bcm5201_suspend,
  636. .setup_aneg = genmii_setup_aneg,
  637. .setup_forced = genmii_setup_forced,
  638. .poll_link = genmii_poll_link,
  639. .read_link = genmii_read_link,
  640. };
  641. static struct mii_phy_def bcm5201_phy_def = {
  642. .phy_id = 0x00406210,
  643. .phy_id_mask = 0xfffffff0,
  644. .name = "BCM5201",
  645. .features = MII_BASIC_FEATURES,
  646. .magic_aneg = 1,
  647. .ops = &bcm5201_phy_ops
  648. };
  649. /* Broadcom BCM 5221 */
  650. static struct mii_phy_ops bcm5221_phy_ops = {
  651. .suspend = bcm5221_suspend,
  652. .init = bcm5221_init,
  653. .setup_aneg = genmii_setup_aneg,
  654. .setup_forced = genmii_setup_forced,
  655. .poll_link = genmii_poll_link,
  656. .read_link = genmii_read_link,
  657. };
  658. static struct mii_phy_def bcm5221_phy_def = {
  659. .phy_id = 0x004061e0,
  660. .phy_id_mask = 0xfffffff0,
  661. .name = "BCM5221",
  662. .features = MII_BASIC_FEATURES,
  663. .magic_aneg = 1,
  664. .ops = &bcm5221_phy_ops
  665. };
  666. /* Broadcom BCM 5241 */
  667. static struct mii_phy_ops bcm5241_phy_ops = {
  668. .suspend = bcm5241_suspend,
  669. .init = bcm5241_init,
  670. .setup_aneg = genmii_setup_aneg,
  671. .setup_forced = genmii_setup_forced,
  672. .poll_link = genmii_poll_link,
  673. .read_link = genmii_read_link,
  674. };
  675. static struct mii_phy_def bcm5241_phy_def = {
  676. .phy_id = 0x0143bc30,
  677. .phy_id_mask = 0xfffffff0,
  678. .name = "BCM5241",
  679. .features = MII_BASIC_FEATURES,
  680. .magic_aneg = 1,
  681. .ops = &bcm5241_phy_ops
  682. };
  683. /* Broadcom BCM 5400 */
  684. static struct mii_phy_ops bcm5400_phy_ops = {
  685. .init = bcm5400_init,
  686. .suspend = bcm5400_suspend,
  687. .setup_aneg = bcm54xx_setup_aneg,
  688. .setup_forced = bcm54xx_setup_forced,
  689. .poll_link = genmii_poll_link,
  690. .read_link = bcm54xx_read_link,
  691. };
  692. static struct mii_phy_def bcm5400_phy_def = {
  693. .phy_id = 0x00206040,
  694. .phy_id_mask = 0xfffffff0,
  695. .name = "BCM5400",
  696. .features = MII_GBIT_FEATURES,
  697. .magic_aneg = 1,
  698. .ops = &bcm5400_phy_ops
  699. };
  700. /* Broadcom BCM 5401 */
  701. static struct mii_phy_ops bcm5401_phy_ops = {
  702. .init = bcm5401_init,
  703. .suspend = bcm5401_suspend,
  704. .setup_aneg = bcm54xx_setup_aneg,
  705. .setup_forced = bcm54xx_setup_forced,
  706. .poll_link = genmii_poll_link,
  707. .read_link = bcm54xx_read_link,
  708. };
  709. static struct mii_phy_def bcm5401_phy_def = {
  710. .phy_id = 0x00206050,
  711. .phy_id_mask = 0xfffffff0,
  712. .name = "BCM5401",
  713. .features = MII_GBIT_FEATURES,
  714. .magic_aneg = 1,
  715. .ops = &bcm5401_phy_ops
  716. };
  717. /* Broadcom BCM 5411 */
  718. static struct mii_phy_ops bcm5411_phy_ops = {
  719. .init = bcm5411_init,
  720. .suspend = generic_suspend,
  721. .setup_aneg = bcm54xx_setup_aneg,
  722. .setup_forced = bcm54xx_setup_forced,
  723. .poll_link = genmii_poll_link,
  724. .read_link = bcm54xx_read_link,
  725. };
  726. static struct mii_phy_def bcm5411_phy_def = {
  727. .phy_id = 0x00206070,
  728. .phy_id_mask = 0xfffffff0,
  729. .name = "BCM5411",
  730. .features = MII_GBIT_FEATURES,
  731. .magic_aneg = 1,
  732. .ops = &bcm5411_phy_ops
  733. };
  734. /* Broadcom BCM 5421 */
  735. static struct mii_phy_ops bcm5421_phy_ops = {
  736. .init = bcm5421_init,
  737. .suspend = generic_suspend,
  738. .setup_aneg = bcm54xx_setup_aneg,
  739. .setup_forced = bcm54xx_setup_forced,
  740. .poll_link = genmii_poll_link,
  741. .read_link = bcm54xx_read_link,
  742. .enable_fiber = bcm5421_enable_fiber,
  743. };
  744. static struct mii_phy_def bcm5421_phy_def = {
  745. .phy_id = 0x002060e0,
  746. .phy_id_mask = 0xfffffff0,
  747. .name = "BCM5421",
  748. .features = MII_GBIT_FEATURES,
  749. .magic_aneg = 1,
  750. .ops = &bcm5421_phy_ops
  751. };
  752. /* Broadcom BCM 5421 built-in K2 */
  753. static struct mii_phy_ops bcm5421k2_phy_ops = {
  754. .init = bcm5421_init,
  755. .suspend = generic_suspend,
  756. .setup_aneg = bcm54xx_setup_aneg,
  757. .setup_forced = bcm54xx_setup_forced,
  758. .poll_link = genmii_poll_link,
  759. .read_link = bcm54xx_read_link,
  760. };
  761. static struct mii_phy_def bcm5421k2_phy_def = {
  762. .phy_id = 0x002062e0,
  763. .phy_id_mask = 0xfffffff0,
  764. .name = "BCM5421-K2",
  765. .features = MII_GBIT_FEATURES,
  766. .magic_aneg = 1,
  767. .ops = &bcm5421k2_phy_ops
  768. };
  769. static struct mii_phy_ops bcm5461_phy_ops = {
  770. .init = bcm5421_init,
  771. .suspend = generic_suspend,
  772. .setup_aneg = bcm54xx_setup_aneg,
  773. .setup_forced = bcm54xx_setup_forced,
  774. .poll_link = genmii_poll_link,
  775. .read_link = bcm54xx_read_link,
  776. .enable_fiber = bcm5461_enable_fiber,
  777. };
  778. static struct mii_phy_def bcm5461_phy_def = {
  779. .phy_id = 0x002060c0,
  780. .phy_id_mask = 0xfffffff0,
  781. .name = "BCM5461",
  782. .features = MII_GBIT_FEATURES,
  783. .magic_aneg = 1,
  784. .ops = &bcm5461_phy_ops
  785. };
  786. /* Broadcom BCM 5462 built-in Vesta */
  787. static struct mii_phy_ops bcm5462V_phy_ops = {
  788. .init = bcm5421_init,
  789. .suspend = generic_suspend,
  790. .setup_aneg = bcm54xx_setup_aneg,
  791. .setup_forced = bcm54xx_setup_forced,
  792. .poll_link = genmii_poll_link,
  793. .read_link = bcm54xx_read_link,
  794. };
  795. static struct mii_phy_def bcm5462V_phy_def = {
  796. .phy_id = 0x002060d0,
  797. .phy_id_mask = 0xfffffff0,
  798. .name = "BCM5462-Vesta",
  799. .features = MII_GBIT_FEATURES,
  800. .magic_aneg = 1,
  801. .ops = &bcm5462V_phy_ops
  802. };
  803. /* Marvell 88E1101 amd 88E1111 */
  804. static struct mii_phy_ops marvell88e1101_phy_ops = {
  805. .suspend = generic_suspend,
  806. .setup_aneg = marvell_setup_aneg,
  807. .setup_forced = marvell_setup_forced,
  808. .poll_link = genmii_poll_link,
  809. .read_link = marvell_read_link
  810. };
  811. static struct mii_phy_ops marvell88e1111_phy_ops = {
  812. .init = marvell88e1111_init,
  813. .suspend = generic_suspend,
  814. .setup_aneg = marvell_setup_aneg,
  815. .setup_forced = marvell_setup_forced,
  816. .poll_link = genmii_poll_link,
  817. .read_link = marvell_read_link
  818. };
  819. /* two revs in darwin for the 88e1101 ... I could use a datasheet
  820. * to get the proper names...
  821. */
  822. static struct mii_phy_def marvell88e1101v1_phy_def = {
  823. .phy_id = 0x01410c20,
  824. .phy_id_mask = 0xfffffff0,
  825. .name = "Marvell 88E1101v1",
  826. .features = MII_GBIT_FEATURES,
  827. .magic_aneg = 1,
  828. .ops = &marvell88e1101_phy_ops
  829. };
  830. static struct mii_phy_def marvell88e1101v2_phy_def = {
  831. .phy_id = 0x01410c60,
  832. .phy_id_mask = 0xfffffff0,
  833. .name = "Marvell 88E1101v2",
  834. .features = MII_GBIT_FEATURES,
  835. .magic_aneg = 1,
  836. .ops = &marvell88e1101_phy_ops
  837. };
  838. static struct mii_phy_def marvell88e1111_phy_def = {
  839. .phy_id = 0x01410cc0,
  840. .phy_id_mask = 0xfffffff0,
  841. .name = "Marvell 88E1111",
  842. .features = MII_GBIT_FEATURES,
  843. .magic_aneg = 1,
  844. .ops = &marvell88e1111_phy_ops
  845. };
  846. /* Generic implementation for most 10/100 PHYs */
  847. static struct mii_phy_ops generic_phy_ops = {
  848. .setup_aneg = genmii_setup_aneg,
  849. .setup_forced = genmii_setup_forced,
  850. .poll_link = genmii_poll_link,
  851. .read_link = genmii_read_link
  852. };
  853. static struct mii_phy_def genmii_phy_def = {
  854. .phy_id = 0x00000000,
  855. .phy_id_mask = 0x00000000,
  856. .name = "Generic MII",
  857. .features = MII_BASIC_FEATURES,
  858. .magic_aneg = 0,
  859. .ops = &generic_phy_ops
  860. };
  861. static struct mii_phy_def* mii_phy_table[] = {
  862. &bcm5201_phy_def,
  863. &bcm5221_phy_def,
  864. &bcm5241_phy_def,
  865. &bcm5400_phy_def,
  866. &bcm5401_phy_def,
  867. &bcm5411_phy_def,
  868. &bcm5421_phy_def,
  869. &bcm5421k2_phy_def,
  870. &bcm5461_phy_def,
  871. &bcm5462V_phy_def,
  872. &marvell88e1101v1_phy_def,
  873. &marvell88e1101v2_phy_def,
  874. &marvell88e1111_phy_def,
  875. &genmii_phy_def,
  876. NULL
  877. };
  878. int mii_phy_probe(struct mii_phy *phy, int mii_id)
  879. {
  880. int rc;
  881. u32 id;
  882. struct mii_phy_def* def;
  883. int i;
  884. /* We do not reset the mii_phy structure as the driver
  885. * may re-probe the PHY regulary
  886. */
  887. phy->mii_id = mii_id;
  888. /* Take PHY out of isloate mode and reset it. */
  889. rc = reset_one_mii_phy(phy, mii_id);
  890. if (rc)
  891. goto fail;
  892. /* Read ID and find matching entry */
  893. id = (phy_read(phy, MII_PHYSID1) << 16 | phy_read(phy, MII_PHYSID2));
  894. printk(KERN_DEBUG "PHY ID: %x, addr: %x\n", id, mii_id);
  895. for (i=0; (def = mii_phy_table[i]) != NULL; i++)
  896. if ((id & def->phy_id_mask) == def->phy_id)
  897. break;
  898. /* Should never be NULL (we have a generic entry), but... */
  899. if (def == NULL)
  900. goto fail;
  901. phy->def = def;
  902. return 0;
  903. fail:
  904. phy->speed = 0;
  905. phy->duplex = 0;
  906. phy->pause = 0;
  907. phy->advertising = 0;
  908. return -ENODEV;
  909. }
  910. EXPORT_SYMBOL(mii_phy_probe);
  911. MODULE_LICENSE("GPL");