phy.c 13 KB

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