phy.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * drivers/net/ethernet/ibm/emac/phy.c
  3. *
  4. * Driver for PowerPC 4xx on-chip ethernet controller, PHY support.
  5. * Borrowed from sungem_phy.c, though I only kept the generic MII
  6. * driver for now.
  7. *
  8. * This file should be shared with other drivers or eventually
  9. * merged as the "low level" part of miilib
  10. *
  11. * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
  12. * <benh@kernel.crashing.org>
  13. *
  14. * Based on the arch/ppc version of the driver:
  15. *
  16. * (c) 2003, Benjamin Herrenscmidt (benh@kernel.crashing.org)
  17. * (c) 2004-2005, Eugene Surovegin <ebs@ebshome.net>
  18. *
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/types.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/mii.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/delay.h>
  27. #include "emac.h"
  28. #include "phy.h"
  29. #define phy_read _phy_read
  30. #define phy_write _phy_write
  31. static inline int _phy_read(struct mii_phy *phy, int reg)
  32. {
  33. return phy->mdio_read(phy->dev, phy->address, reg);
  34. }
  35. static inline void _phy_write(struct mii_phy *phy, int reg, int val)
  36. {
  37. phy->mdio_write(phy->dev, phy->address, reg, val);
  38. }
  39. static inline int gpcs_phy_read(struct mii_phy *phy, int reg)
  40. {
  41. return phy->mdio_read(phy->dev, phy->gpcs_address, reg);
  42. }
  43. static inline void gpcs_phy_write(struct mii_phy *phy, int reg, int val)
  44. {
  45. phy->mdio_write(phy->dev, phy->gpcs_address, reg, val);
  46. }
  47. int emac_mii_reset_phy(struct mii_phy *phy)
  48. {
  49. int val;
  50. int limit = 10000;
  51. val = phy_read(phy, MII_BMCR);
  52. val &= ~(BMCR_ISOLATE | BMCR_ANENABLE);
  53. val |= BMCR_RESET;
  54. phy_write(phy, MII_BMCR, val);
  55. udelay(300);
  56. while (--limit) {
  57. val = phy_read(phy, MII_BMCR);
  58. if (val >= 0 && (val & BMCR_RESET) == 0)
  59. break;
  60. udelay(10);
  61. }
  62. if ((val & BMCR_ISOLATE) && limit > 0)
  63. phy_write(phy, MII_BMCR, val & ~BMCR_ISOLATE);
  64. return limit <= 0;
  65. }
  66. int emac_mii_reset_gpcs(struct mii_phy *phy)
  67. {
  68. int val;
  69. int limit = 10000;
  70. val = gpcs_phy_read(phy, MII_BMCR);
  71. val &= ~(BMCR_ISOLATE | BMCR_ANENABLE);
  72. val |= BMCR_RESET;
  73. gpcs_phy_write(phy, MII_BMCR, val);
  74. udelay(300);
  75. while (--limit) {
  76. val = gpcs_phy_read(phy, MII_BMCR);
  77. if (val >= 0 && (val & BMCR_RESET) == 0)
  78. break;
  79. udelay(10);
  80. }
  81. if ((val & BMCR_ISOLATE) && limit > 0)
  82. gpcs_phy_write(phy, MII_BMCR, val & ~BMCR_ISOLATE);
  83. if (limit > 0 && phy->mode == PHY_MODE_SGMII) {
  84. /* Configure GPCS interface to recommended setting for SGMII */
  85. gpcs_phy_write(phy, 0x04, 0x8120); /* AsymPause, FDX */
  86. gpcs_phy_write(phy, 0x07, 0x2801); /* msg_pg, toggle */
  87. gpcs_phy_write(phy, 0x00, 0x0140); /* 1Gbps, FDX */
  88. }
  89. return limit <= 0;
  90. }
  91. static int genmii_setup_aneg(struct mii_phy *phy, u32 advertise)
  92. {
  93. int ctl, adv;
  94. phy->autoneg = AUTONEG_ENABLE;
  95. phy->speed = SPEED_10;
  96. phy->duplex = DUPLEX_HALF;
  97. phy->pause = phy->asym_pause = 0;
  98. phy->advertising = advertise;
  99. ctl = phy_read(phy, MII_BMCR);
  100. if (ctl < 0)
  101. return ctl;
  102. ctl &= ~(BMCR_FULLDPLX | BMCR_SPEED100 | BMCR_SPEED1000 | BMCR_ANENABLE);
  103. /* First clear the PHY */
  104. phy_write(phy, MII_BMCR, ctl);
  105. /* Setup standard advertise */
  106. adv = phy_read(phy, MII_ADVERTISE);
  107. if (adv < 0)
  108. return adv;
  109. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  110. ADVERTISE_PAUSE_ASYM);
  111. if (advertise & ADVERTISED_10baseT_Half)
  112. adv |= ADVERTISE_10HALF;
  113. if (advertise & ADVERTISED_10baseT_Full)
  114. adv |= ADVERTISE_10FULL;
  115. if (advertise & ADVERTISED_100baseT_Half)
  116. adv |= ADVERTISE_100HALF;
  117. if (advertise & ADVERTISED_100baseT_Full)
  118. adv |= ADVERTISE_100FULL;
  119. if (advertise & ADVERTISED_Pause)
  120. adv |= ADVERTISE_PAUSE_CAP;
  121. if (advertise & ADVERTISED_Asym_Pause)
  122. adv |= ADVERTISE_PAUSE_ASYM;
  123. phy_write(phy, MII_ADVERTISE, adv);
  124. if (phy->features &
  125. (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseT_Half)) {
  126. adv = phy_read(phy, MII_CTRL1000);
  127. if (adv < 0)
  128. return adv;
  129. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  130. if (advertise & ADVERTISED_1000baseT_Full)
  131. adv |= ADVERTISE_1000FULL;
  132. if (advertise & ADVERTISED_1000baseT_Half)
  133. adv |= ADVERTISE_1000HALF;
  134. phy_write(phy, MII_CTRL1000, adv);
  135. }
  136. /* Start/Restart aneg */
  137. ctl = phy_read(phy, MII_BMCR);
  138. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  139. phy_write(phy, MII_BMCR, ctl);
  140. return 0;
  141. }
  142. static int genmii_setup_forced(struct mii_phy *phy, int speed, int fd)
  143. {
  144. int ctl;
  145. phy->autoneg = AUTONEG_DISABLE;
  146. phy->speed = speed;
  147. phy->duplex = fd;
  148. phy->pause = phy->asym_pause = 0;
  149. ctl = phy_read(phy, MII_BMCR);
  150. if (ctl < 0)
  151. return ctl;
  152. ctl &= ~(BMCR_FULLDPLX | BMCR_SPEED100 | BMCR_SPEED1000 | BMCR_ANENABLE);
  153. /* First clear the PHY */
  154. phy_write(phy, MII_BMCR, ctl | BMCR_RESET);
  155. /* Select speed & duplex */
  156. switch (speed) {
  157. case SPEED_10:
  158. break;
  159. case SPEED_100:
  160. ctl |= BMCR_SPEED100;
  161. break;
  162. case SPEED_1000:
  163. ctl |= BMCR_SPEED1000;
  164. break;
  165. default:
  166. return -EINVAL;
  167. }
  168. if (fd == DUPLEX_FULL)
  169. ctl |= BMCR_FULLDPLX;
  170. phy_write(phy, MII_BMCR, ctl);
  171. return 0;
  172. }
  173. static int genmii_poll_link(struct mii_phy *phy)
  174. {
  175. int status;
  176. /* Clear latched value with dummy read */
  177. phy_read(phy, MII_BMSR);
  178. status = phy_read(phy, MII_BMSR);
  179. if (status < 0 || (status & BMSR_LSTATUS) == 0)
  180. return 0;
  181. if (phy->autoneg == AUTONEG_ENABLE && !(status & BMSR_ANEGCOMPLETE))
  182. return 0;
  183. return 1;
  184. }
  185. static int genmii_read_link(struct mii_phy *phy)
  186. {
  187. if (phy->autoneg == AUTONEG_ENABLE) {
  188. int glpa = 0;
  189. int lpa = phy_read(phy, MII_LPA) & phy_read(phy, MII_ADVERTISE);
  190. if (lpa < 0)
  191. return lpa;
  192. if (phy->features &
  193. (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseT_Half)) {
  194. int adv = phy_read(phy, MII_CTRL1000);
  195. glpa = phy_read(phy, MII_STAT1000);
  196. if (glpa < 0 || adv < 0)
  197. return adv;
  198. glpa &= adv << 2;
  199. }
  200. phy->speed = SPEED_10;
  201. phy->duplex = DUPLEX_HALF;
  202. phy->pause = phy->asym_pause = 0;
  203. if (glpa & (LPA_1000FULL | LPA_1000HALF)) {
  204. phy->speed = SPEED_1000;
  205. if (glpa & LPA_1000FULL)
  206. phy->duplex = DUPLEX_FULL;
  207. } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
  208. phy->speed = SPEED_100;
  209. if (lpa & LPA_100FULL)
  210. phy->duplex = DUPLEX_FULL;
  211. } else if (lpa & LPA_10FULL)
  212. phy->duplex = DUPLEX_FULL;
  213. if (phy->duplex == DUPLEX_FULL) {
  214. phy->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  215. phy->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  216. }
  217. } else {
  218. int bmcr = phy_read(phy, MII_BMCR);
  219. if (bmcr < 0)
  220. return bmcr;
  221. if (bmcr & BMCR_FULLDPLX)
  222. phy->duplex = DUPLEX_FULL;
  223. else
  224. phy->duplex = DUPLEX_HALF;
  225. if (bmcr & BMCR_SPEED1000)
  226. phy->speed = SPEED_1000;
  227. else if (bmcr & BMCR_SPEED100)
  228. phy->speed = SPEED_100;
  229. else
  230. phy->speed = SPEED_10;
  231. phy->pause = phy->asym_pause = 0;
  232. }
  233. return 0;
  234. }
  235. /* Generic implementation for most 10/100/1000 PHYs */
  236. static struct mii_phy_ops generic_phy_ops = {
  237. .setup_aneg = genmii_setup_aneg,
  238. .setup_forced = genmii_setup_forced,
  239. .poll_link = genmii_poll_link,
  240. .read_link = genmii_read_link
  241. };
  242. static struct mii_phy_def genmii_phy_def = {
  243. .phy_id = 0x00000000,
  244. .phy_id_mask = 0x00000000,
  245. .name = "Generic MII",
  246. .ops = &generic_phy_ops
  247. };
  248. /* CIS8201 */
  249. #define MII_CIS8201_10BTCSR 0x16
  250. #define TENBTCSR_ECHO_DISABLE 0x2000
  251. #define MII_CIS8201_EPCR 0x17
  252. #define EPCR_MODE_MASK 0x3000
  253. #define EPCR_GMII_MODE 0x0000
  254. #define EPCR_RGMII_MODE 0x1000
  255. #define EPCR_TBI_MODE 0x2000
  256. #define EPCR_RTBI_MODE 0x3000
  257. #define MII_CIS8201_ACSR 0x1c
  258. #define ACSR_PIN_PRIO_SELECT 0x0004
  259. static int cis8201_init(struct mii_phy *phy)
  260. {
  261. int epcr;
  262. epcr = phy_read(phy, MII_CIS8201_EPCR);
  263. if (epcr < 0)
  264. return epcr;
  265. epcr &= ~EPCR_MODE_MASK;
  266. switch (phy->mode) {
  267. case PHY_MODE_TBI:
  268. epcr |= EPCR_TBI_MODE;
  269. break;
  270. case PHY_MODE_RTBI:
  271. epcr |= EPCR_RTBI_MODE;
  272. break;
  273. case PHY_MODE_GMII:
  274. epcr |= EPCR_GMII_MODE;
  275. break;
  276. case PHY_MODE_RGMII:
  277. default:
  278. epcr |= EPCR_RGMII_MODE;
  279. }
  280. phy_write(phy, MII_CIS8201_EPCR, epcr);
  281. /* MII regs override strap pins */
  282. phy_write(phy, MII_CIS8201_ACSR,
  283. phy_read(phy, MII_CIS8201_ACSR) | ACSR_PIN_PRIO_SELECT);
  284. /* Disable TX_EN -> CRS echo mode, otherwise 10/HDX doesn't work */
  285. phy_write(phy, MII_CIS8201_10BTCSR,
  286. phy_read(phy, MII_CIS8201_10BTCSR) | TENBTCSR_ECHO_DISABLE);
  287. return 0;
  288. }
  289. static struct mii_phy_ops cis8201_phy_ops = {
  290. .init = cis8201_init,
  291. .setup_aneg = genmii_setup_aneg,
  292. .setup_forced = genmii_setup_forced,
  293. .poll_link = genmii_poll_link,
  294. .read_link = genmii_read_link
  295. };
  296. static struct mii_phy_def cis8201_phy_def = {
  297. .phy_id = 0x000fc410,
  298. .phy_id_mask = 0x000ffff0,
  299. .name = "CIS8201 Gigabit Ethernet",
  300. .ops = &cis8201_phy_ops
  301. };
  302. static struct mii_phy_def bcm5248_phy_def = {
  303. .phy_id = 0x0143bc00,
  304. .phy_id_mask = 0x0ffffff0,
  305. .name = "BCM5248 10/100 SMII Ethernet",
  306. .ops = &generic_phy_ops
  307. };
  308. static int m88e1111_init(struct mii_phy *phy)
  309. {
  310. pr_debug("%s: Marvell 88E1111 Ethernet\n", __func__);
  311. phy_write(phy, 0x14, 0x0ce3);
  312. phy_write(phy, 0x18, 0x4101);
  313. phy_write(phy, 0x09, 0x0e00);
  314. phy_write(phy, 0x04, 0x01e1);
  315. phy_write(phy, 0x00, 0x9140);
  316. phy_write(phy, 0x00, 0x1140);
  317. return 0;
  318. }
  319. static int m88e1112_init(struct mii_phy *phy)
  320. {
  321. /*
  322. * Marvell 88E1112 PHY needs to have the SGMII MAC
  323. * interace (page 2) properly configured to
  324. * communicate with the 460EX/GT GPCS interface.
  325. */
  326. u16 reg_short;
  327. pr_debug("%s: Marvell 88E1112 Ethernet\n", __func__);
  328. /* Set access to Page 2 */
  329. phy_write(phy, 0x16, 0x0002);
  330. phy_write(phy, 0x00, 0x0040); /* 1Gbps */
  331. reg_short = (u16)(phy_read(phy, 0x1a));
  332. reg_short |= 0x8000; /* bypass Auto-Negotiation */
  333. phy_write(phy, 0x1a, reg_short);
  334. emac_mii_reset_phy(phy); /* reset MAC interface */
  335. /* Reset access to Page 0 */
  336. phy_write(phy, 0x16, 0x0000);
  337. return 0;
  338. }
  339. static int et1011c_init(struct mii_phy *phy)
  340. {
  341. u16 reg_short;
  342. reg_short = (u16)(phy_read(phy, 0x16));
  343. reg_short &= ~(0x7);
  344. reg_short |= 0x6; /* RGMII Trace Delay*/
  345. phy_write(phy, 0x16, reg_short);
  346. reg_short = (u16)(phy_read(phy, 0x17));
  347. reg_short &= ~(0x40);
  348. phy_write(phy, 0x17, reg_short);
  349. phy_write(phy, 0x1c, 0x74f0);
  350. return 0;
  351. }
  352. static struct mii_phy_ops et1011c_phy_ops = {
  353. .init = et1011c_init,
  354. .setup_aneg = genmii_setup_aneg,
  355. .setup_forced = genmii_setup_forced,
  356. .poll_link = genmii_poll_link,
  357. .read_link = genmii_read_link
  358. };
  359. static struct mii_phy_def et1011c_phy_def = {
  360. .phy_id = 0x0282f000,
  361. .phy_id_mask = 0x0fffff00,
  362. .name = "ET1011C Gigabit Ethernet",
  363. .ops = &et1011c_phy_ops
  364. };
  365. static struct mii_phy_ops m88e1111_phy_ops = {
  366. .init = m88e1111_init,
  367. .setup_aneg = genmii_setup_aneg,
  368. .setup_forced = genmii_setup_forced,
  369. .poll_link = genmii_poll_link,
  370. .read_link = genmii_read_link
  371. };
  372. static struct mii_phy_def m88e1111_phy_def = {
  373. .phy_id = 0x01410CC0,
  374. .phy_id_mask = 0x0ffffff0,
  375. .name = "Marvell 88E1111 Ethernet",
  376. .ops = &m88e1111_phy_ops,
  377. };
  378. static struct mii_phy_ops m88e1112_phy_ops = {
  379. .init = m88e1112_init,
  380. .setup_aneg = genmii_setup_aneg,
  381. .setup_forced = genmii_setup_forced,
  382. .poll_link = genmii_poll_link,
  383. .read_link = genmii_read_link
  384. };
  385. static struct mii_phy_def m88e1112_phy_def = {
  386. .phy_id = 0x01410C90,
  387. .phy_id_mask = 0x0ffffff0,
  388. .name = "Marvell 88E1112 Ethernet",
  389. .ops = &m88e1112_phy_ops,
  390. };
  391. static struct mii_phy_def *mii_phy_table[] = {
  392. &et1011c_phy_def,
  393. &cis8201_phy_def,
  394. &bcm5248_phy_def,
  395. &m88e1111_phy_def,
  396. &m88e1112_phy_def,
  397. &genmii_phy_def,
  398. NULL
  399. };
  400. int emac_mii_phy_probe(struct mii_phy *phy, int address)
  401. {
  402. struct mii_phy_def *def;
  403. int i;
  404. u32 id;
  405. phy->autoneg = AUTONEG_DISABLE;
  406. phy->advertising = 0;
  407. phy->address = address;
  408. phy->speed = SPEED_10;
  409. phy->duplex = DUPLEX_HALF;
  410. phy->pause = phy->asym_pause = 0;
  411. /* Take PHY out of isolate mode and reset it. */
  412. if (emac_mii_reset_phy(phy))
  413. return -ENODEV;
  414. /* Read ID and find matching entry */
  415. id = (phy_read(phy, MII_PHYSID1) << 16) | phy_read(phy, MII_PHYSID2);
  416. for (i = 0; (def = mii_phy_table[i]) != NULL; i++)
  417. if ((id & def->phy_id_mask) == def->phy_id)
  418. break;
  419. /* Should never be NULL (we have a generic entry), but... */
  420. if (!def)
  421. return -ENODEV;
  422. phy->def = def;
  423. /* Determine PHY features if needed */
  424. phy->features = def->features;
  425. if (!phy->features) {
  426. u16 bmsr = phy_read(phy, MII_BMSR);
  427. if (bmsr & BMSR_ANEGCAPABLE)
  428. phy->features |= SUPPORTED_Autoneg;
  429. if (bmsr & BMSR_10HALF)
  430. phy->features |= SUPPORTED_10baseT_Half;
  431. if (bmsr & BMSR_10FULL)
  432. phy->features |= SUPPORTED_10baseT_Full;
  433. if (bmsr & BMSR_100HALF)
  434. phy->features |= SUPPORTED_100baseT_Half;
  435. if (bmsr & BMSR_100FULL)
  436. phy->features |= SUPPORTED_100baseT_Full;
  437. if (bmsr & BMSR_ESTATEN) {
  438. u16 esr = phy_read(phy, MII_ESTATUS);
  439. if (esr & ESTATUS_1000_TFULL)
  440. phy->features |= SUPPORTED_1000baseT_Full;
  441. if (esr & ESTATUS_1000_THALF)
  442. phy->features |= SUPPORTED_1000baseT_Half;
  443. }
  444. phy->features |= SUPPORTED_MII;
  445. }
  446. /* Setup default advertising */
  447. phy->advertising = phy->features;
  448. return 0;
  449. }
  450. MODULE_LICENSE("GPL");