bcmmii.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Broadcom GENET MDIO routines
  4. *
  5. * Copyright (c) 2014-2017 Broadcom
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/types.h>
  9. #include <linux/delay.h>
  10. #include <linux/wait.h>
  11. #include <linux/mii.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/bitops.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/phy.h>
  17. #include <linux/phy_fixed.h>
  18. #include <linux/brcmphy.h>
  19. #include <linux/of.h>
  20. #include <linux/of_net.h>
  21. #include <linux/of_mdio.h>
  22. #include <linux/platform_data/bcmgenet.h>
  23. #include <linux/platform_data/mdio-bcm-unimac.h>
  24. #include "bcmgenet.h"
  25. static void bcmgenet_mac_config(struct net_device *dev)
  26. {
  27. struct bcmgenet_priv *priv = netdev_priv(dev);
  28. struct phy_device *phydev = dev->phydev;
  29. u32 reg, cmd_bits = 0;
  30. /* speed */
  31. if (phydev->speed == SPEED_1000)
  32. cmd_bits = CMD_SPEED_1000;
  33. else if (phydev->speed == SPEED_100)
  34. cmd_bits = CMD_SPEED_100;
  35. else
  36. cmd_bits = CMD_SPEED_10;
  37. cmd_bits <<= CMD_SPEED_SHIFT;
  38. /* duplex */
  39. if (phydev->duplex != DUPLEX_FULL) {
  40. cmd_bits |= CMD_HD_EN |
  41. CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
  42. } else {
  43. /* pause capability defaults to Symmetric */
  44. if (priv->autoneg_pause) {
  45. bool tx_pause = 0, rx_pause = 0;
  46. if (phydev->autoneg)
  47. phy_get_pause(phydev, &tx_pause, &rx_pause);
  48. if (!tx_pause)
  49. cmd_bits |= CMD_TX_PAUSE_IGNORE;
  50. if (!rx_pause)
  51. cmd_bits |= CMD_RX_PAUSE_IGNORE;
  52. }
  53. /* Manual override */
  54. if (!priv->rx_pause)
  55. cmd_bits |= CMD_RX_PAUSE_IGNORE;
  56. if (!priv->tx_pause)
  57. cmd_bits |= CMD_TX_PAUSE_IGNORE;
  58. }
  59. /* Program UMAC and RGMII block based on established
  60. * link speed, duplex, and pause. The speed set in
  61. * umac->cmd tell RGMII block which clock to use for
  62. * transmit -- 25MHz(100Mbps) or 125MHz(1Gbps).
  63. * Receive clock is provided by the PHY.
  64. */
  65. reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
  66. reg &= ~OOB_DISABLE;
  67. reg |= RGMII_LINK;
  68. bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
  69. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  70. reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
  71. CMD_HD_EN |
  72. CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE);
  73. reg |= cmd_bits;
  74. if (reg & CMD_SW_RESET) {
  75. reg &= ~CMD_SW_RESET;
  76. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  77. udelay(2);
  78. reg |= CMD_TX_EN | CMD_RX_EN;
  79. }
  80. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  81. priv->eee.eee_active = phy_init_eee(phydev, 0) >= 0;
  82. bcmgenet_eee_enable_set(dev,
  83. priv->eee.eee_enabled && priv->eee.eee_active,
  84. priv->eee.tx_lpi_enabled);
  85. }
  86. /* setup netdev link state when PHY link status change and
  87. * update UMAC and RGMII block when link up
  88. */
  89. void bcmgenet_mii_setup(struct net_device *dev)
  90. {
  91. struct phy_device *phydev = dev->phydev;
  92. if (phydev->link)
  93. bcmgenet_mac_config(dev);
  94. phy_print_status(phydev);
  95. }
  96. static int bcmgenet_fixed_phy_link_update(struct net_device *dev,
  97. struct fixed_phy_status *status)
  98. {
  99. struct bcmgenet_priv *priv;
  100. u32 reg;
  101. if (dev && dev->phydev && status) {
  102. priv = netdev_priv(dev);
  103. reg = bcmgenet_umac_readl(priv, UMAC_MODE);
  104. status->link = !!(reg & MODE_LINK_STATUS);
  105. }
  106. return 0;
  107. }
  108. void bcmgenet_phy_pause_set(struct net_device *dev, bool rx, bool tx)
  109. {
  110. struct phy_device *phydev = dev->phydev;
  111. linkmode_mod_bit(ETHTOOL_LINK_MODE_Pause_BIT, phydev->advertising, rx);
  112. linkmode_mod_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, phydev->advertising,
  113. rx | tx);
  114. phy_start_aneg(phydev);
  115. mutex_lock(&phydev->lock);
  116. if (phydev->link)
  117. bcmgenet_mac_config(dev);
  118. mutex_unlock(&phydev->lock);
  119. }
  120. void bcmgenet_phy_power_set(struct net_device *dev, bool enable)
  121. {
  122. struct bcmgenet_priv *priv = netdev_priv(dev);
  123. u32 reg = 0;
  124. /* EXT_GPHY_CTRL is only valid for GENETv4 and onward */
  125. if (GENET_IS_V4(priv) || priv->ephy_16nm) {
  126. reg = bcmgenet_ext_readl(priv, EXT_GPHY_CTRL);
  127. if (enable) {
  128. reg &= ~EXT_CK25_DIS;
  129. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  130. mdelay(1);
  131. reg &= ~(EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN |
  132. EXT_CFG_IDDQ_GLOBAL_PWR);
  133. reg |= EXT_GPHY_RESET;
  134. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  135. mdelay(1);
  136. reg &= ~EXT_GPHY_RESET;
  137. } else {
  138. reg |= EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN |
  139. EXT_GPHY_RESET | EXT_CFG_IDDQ_GLOBAL_PWR;
  140. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  141. mdelay(1);
  142. reg |= EXT_CK25_DIS;
  143. }
  144. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  145. udelay(60);
  146. } else {
  147. mdelay(1);
  148. }
  149. }
  150. static void bcmgenet_moca_phy_setup(struct bcmgenet_priv *priv)
  151. {
  152. if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
  153. fixed_phy_set_link_update(priv->dev->phydev,
  154. bcmgenet_fixed_phy_link_update);
  155. }
  156. int bcmgenet_mii_config(struct net_device *dev, bool init)
  157. {
  158. struct bcmgenet_priv *priv = netdev_priv(dev);
  159. struct phy_device *phydev = dev->phydev;
  160. struct device *kdev = &priv->pdev->dev;
  161. const char *phy_name = NULL;
  162. u32 id_mode_dis = 0;
  163. u32 port_ctrl;
  164. u32 reg;
  165. switch (priv->phy_interface) {
  166. case PHY_INTERFACE_MODE_INTERNAL:
  167. phy_name = "internal PHY";
  168. fallthrough;
  169. case PHY_INTERFACE_MODE_MOCA:
  170. /* Irrespective of the actually configured PHY speed (100 or
  171. * 1000) GENETv4 only has an internal GPHY so we will just end
  172. * up masking the Gigabit features from what we support, not
  173. * switching to the EPHY
  174. */
  175. if (GENET_IS_V4(priv))
  176. port_ctrl = PORT_MODE_INT_GPHY;
  177. else
  178. port_ctrl = PORT_MODE_INT_EPHY;
  179. if (!phy_name) {
  180. phy_name = "MoCA";
  181. if (!GENET_IS_V5(priv))
  182. port_ctrl |= LED_ACT_SOURCE_MAC;
  183. bcmgenet_moca_phy_setup(priv);
  184. }
  185. break;
  186. case PHY_INTERFACE_MODE_MII:
  187. phy_name = "external MII";
  188. phy_set_max_speed(phydev, SPEED_100);
  189. port_ctrl = PORT_MODE_EXT_EPHY;
  190. break;
  191. case PHY_INTERFACE_MODE_REVMII:
  192. phy_name = "external RvMII";
  193. /* of_mdiobus_register took care of reading the 'max-speed'
  194. * PHY property for us, effectively limiting the PHY supported
  195. * capabilities, use that knowledge to also configure the
  196. * Reverse MII interface correctly.
  197. */
  198. if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  199. dev->phydev->supported))
  200. port_ctrl = PORT_MODE_EXT_RVMII_50;
  201. else
  202. port_ctrl = PORT_MODE_EXT_RVMII_25;
  203. break;
  204. case PHY_INTERFACE_MODE_RGMII:
  205. /* RGMII_NO_ID: TXC transitions at the same time as TXD
  206. * (requires PCB or receiver-side delay)
  207. *
  208. * ID is implicitly disabled for 100Mbps (RG)MII operation.
  209. */
  210. phy_name = "external RGMII (no delay)";
  211. id_mode_dis = BIT(16);
  212. port_ctrl = PORT_MODE_EXT_GPHY;
  213. break;
  214. case PHY_INTERFACE_MODE_RGMII_TXID:
  215. /* RGMII_TXID: Add 2ns delay on TXC (90 degree shift) */
  216. phy_name = "external RGMII (TX delay)";
  217. port_ctrl = PORT_MODE_EXT_GPHY;
  218. break;
  219. case PHY_INTERFACE_MODE_RGMII_RXID:
  220. phy_name = "external RGMII (RX delay)";
  221. port_ctrl = PORT_MODE_EXT_GPHY;
  222. break;
  223. default:
  224. dev_err(kdev, "unknown phy mode: %d\n", priv->phy_interface);
  225. return -EINVAL;
  226. }
  227. bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
  228. priv->ext_phy = !priv->internal_phy &&
  229. (priv->phy_interface != PHY_INTERFACE_MODE_MOCA);
  230. /* This is an external PHY (xMII), so we need to enable the RGMII
  231. * block for the interface to work
  232. */
  233. if (priv->ext_phy) {
  234. reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
  235. reg &= ~ID_MODE_DIS;
  236. reg |= id_mode_dis;
  237. if (GENET_IS_V1(priv) || GENET_IS_V2(priv) || GENET_IS_V3(priv))
  238. reg |= RGMII_MODE_EN_V123;
  239. else
  240. reg |= RGMII_MODE_EN;
  241. bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
  242. }
  243. if (init)
  244. dev_info(kdev, "configuring instance for %s\n", phy_name);
  245. return 0;
  246. }
  247. int bcmgenet_mii_probe(struct net_device *dev)
  248. {
  249. struct bcmgenet_priv *priv = netdev_priv(dev);
  250. struct device *kdev = &priv->pdev->dev;
  251. struct device_node *dn = kdev->of_node;
  252. phy_interface_t phy_iface = priv->phy_interface;
  253. struct phy_device *phydev;
  254. u32 phy_flags = PHY_BRCM_AUTO_PWRDWN_ENABLE |
  255. PHY_BRCM_DIS_TXCRXC_NOENRGY |
  256. PHY_BRCM_IDDQ_SUSPEND;
  257. int ret;
  258. /* Communicate the integrated PHY revision */
  259. if (priv->internal_phy)
  260. phy_flags = priv->gphy_rev;
  261. /* This is an ugly quirk but we have not been correctly interpreting
  262. * the phy_interface values and we have done that across different
  263. * drivers, so at least we are consistent in our mistakes.
  264. *
  265. * When the Generic PHY driver is in use either the PHY has been
  266. * strapped or programmed correctly by the boot loader so we should
  267. * stick to our incorrect interpretation since we have validated it.
  268. *
  269. * Now when a dedicated PHY driver is in use, we need to reverse the
  270. * meaning of the phy_interface_mode values to something that the PHY
  271. * driver will interpret and act on such that we have two mistakes
  272. * canceling themselves so to speak. We only do this for the two
  273. * modes that GENET driver officially supports on Broadcom STB chips:
  274. * PHY_INTERFACE_MODE_RGMII and PHY_INTERFACE_MODE_RGMII_TXID. Other
  275. * modes are not *officially* supported with the boot loader and the
  276. * scripted environment generating Device Tree blobs for those
  277. * platforms.
  278. *
  279. * Note that internal PHY, MoCA and fixed-link configurations are not
  280. * affected because they use different phy_interface_t values or the
  281. * Generic PHY driver.
  282. */
  283. switch (priv->phy_interface) {
  284. case PHY_INTERFACE_MODE_RGMII:
  285. phy_iface = PHY_INTERFACE_MODE_RGMII_ID;
  286. break;
  287. case PHY_INTERFACE_MODE_RGMII_TXID:
  288. phy_iface = PHY_INTERFACE_MODE_RGMII_RXID;
  289. break;
  290. default:
  291. break;
  292. }
  293. if (dn) {
  294. phydev = of_phy_connect(dev, priv->phy_dn, bcmgenet_mii_setup,
  295. phy_flags, phy_iface);
  296. if (!phydev) {
  297. pr_err("could not attach to PHY\n");
  298. return -ENODEV;
  299. }
  300. } else {
  301. if (has_acpi_companion(kdev)) {
  302. char mdio_bus_id[MII_BUS_ID_SIZE];
  303. struct mii_bus *unimacbus;
  304. snprintf(mdio_bus_id, MII_BUS_ID_SIZE, "%s-%d",
  305. UNIMAC_MDIO_DRV_NAME, priv->pdev->id);
  306. unimacbus = mdio_find_bus(mdio_bus_id);
  307. if (!unimacbus) {
  308. pr_err("Unable to find mii\n");
  309. return -ENODEV;
  310. }
  311. phydev = phy_find_first(unimacbus);
  312. put_device(&unimacbus->dev);
  313. if (!phydev) {
  314. pr_err("Unable to find PHY\n");
  315. return -ENODEV;
  316. }
  317. } else {
  318. phydev = dev->phydev;
  319. }
  320. phydev->dev_flags = phy_flags;
  321. ret = phy_connect_direct(dev, phydev, bcmgenet_mii_setup,
  322. phy_iface);
  323. if (ret) {
  324. pr_err("could not attach to PHY\n");
  325. return -ENODEV;
  326. }
  327. }
  328. /* Configure port multiplexer based on what the probed PHY device since
  329. * reading the 'max-speed' property determines the maximum supported
  330. * PHY speed which is needed for bcmgenet_mii_config() to configure
  331. * things appropriately.
  332. */
  333. ret = bcmgenet_mii_config(dev, true);
  334. if (ret) {
  335. phy_disconnect(dev->phydev);
  336. return ret;
  337. }
  338. /* The internal PHY has its link interrupts routed to the
  339. * Ethernet MAC ISRs. On GENETv5 there is a hardware issue
  340. * that prevents the signaling of link UP interrupts when
  341. * the link operates at 10Mbps, so fallback to polling for
  342. * those versions of GENET.
  343. */
  344. if (priv->internal_phy && !GENET_IS_V5(priv))
  345. dev->phydev->irq = PHY_MAC_INTERRUPT;
  346. /* Indicate that the MAC is responsible for PHY PM */
  347. dev->phydev->mac_managed_pm = true;
  348. return 0;
  349. }
  350. static struct device_node *bcmgenet_mii_of_find_mdio(struct bcmgenet_priv *priv)
  351. {
  352. struct device_node *dn = priv->pdev->dev.of_node;
  353. struct device *kdev = &priv->pdev->dev;
  354. char *compat;
  355. compat = kasprintf(GFP_KERNEL, "brcm,genet-mdio-v%d", priv->version);
  356. if (!compat)
  357. return NULL;
  358. priv->mdio_dn = of_get_compatible_child(dn, compat);
  359. kfree(compat);
  360. if (!priv->mdio_dn) {
  361. dev_err(kdev, "unable to find MDIO bus node\n");
  362. return NULL;
  363. }
  364. return priv->mdio_dn;
  365. }
  366. static void bcmgenet_mii_pdata_init(struct bcmgenet_priv *priv,
  367. struct unimac_mdio_pdata *ppd)
  368. {
  369. struct device *kdev = &priv->pdev->dev;
  370. struct bcmgenet_platform_data *pd = kdev->platform_data;
  371. if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
  372. /*
  373. * Internal or external PHY with MDIO access
  374. */
  375. if (pd->phy_address >= 0 && pd->phy_address < PHY_MAX_ADDR)
  376. ppd->phy_mask = 1 << pd->phy_address;
  377. else
  378. ppd->phy_mask = 0;
  379. }
  380. }
  381. static int bcmgenet_mii_wait(void *wait_func_data)
  382. {
  383. struct bcmgenet_priv *priv = wait_func_data;
  384. wait_event_timeout(priv->wq,
  385. !(bcmgenet_umac_readl(priv, UMAC_MDIO_CMD)
  386. & MDIO_START_BUSY),
  387. HZ / 100);
  388. return 0;
  389. }
  390. static int bcmgenet_mii_register(struct bcmgenet_priv *priv)
  391. {
  392. struct platform_device *pdev = priv->pdev;
  393. struct bcmgenet_platform_data *pdata = pdev->dev.platform_data;
  394. struct device_node *dn = pdev->dev.of_node;
  395. struct unimac_mdio_pdata ppd;
  396. struct platform_device *ppdev;
  397. struct resource *pres, res;
  398. int id, ret;
  399. pres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  400. if (!pres) {
  401. dev_err(&pdev->dev, "Invalid resource\n");
  402. return -EINVAL;
  403. }
  404. memset(&res, 0, sizeof(res));
  405. memset(&ppd, 0, sizeof(ppd));
  406. ppd.wait_func = bcmgenet_mii_wait;
  407. ppd.wait_func_data = priv;
  408. ppd.bus_name = "bcmgenet MII bus";
  409. /* Unimac MDIO bus controller starts at UniMAC offset + MDIO_CMD
  410. * and is 2 * 32-bits word long, 8 bytes total.
  411. */
  412. res.start = pres->start + GENET_UMAC_OFF + UMAC_MDIO_CMD;
  413. res.end = res.start + 8;
  414. res.flags = IORESOURCE_MEM;
  415. if (dn)
  416. id = of_alias_get_id(dn, "eth");
  417. else
  418. id = pdev->id;
  419. ppdev = platform_device_alloc(UNIMAC_MDIO_DRV_NAME, id);
  420. if (!ppdev)
  421. return -ENOMEM;
  422. /* Retain this platform_device pointer for later cleanup */
  423. priv->mii_pdev = ppdev;
  424. ppdev->dev.parent = &pdev->dev;
  425. if (dn)
  426. ppdev->dev.of_node = bcmgenet_mii_of_find_mdio(priv);
  427. else if (pdata)
  428. bcmgenet_mii_pdata_init(priv, &ppd);
  429. else
  430. ppd.phy_mask = ~0;
  431. ret = platform_device_add_resources(ppdev, &res, 1);
  432. if (ret)
  433. goto out;
  434. ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd));
  435. if (ret)
  436. goto out;
  437. ret = platform_device_add(ppdev);
  438. if (ret)
  439. goto out;
  440. return 0;
  441. out:
  442. platform_device_put(ppdev);
  443. return ret;
  444. }
  445. static int bcmgenet_phy_interface_init(struct bcmgenet_priv *priv)
  446. {
  447. struct device *kdev = &priv->pdev->dev;
  448. int phy_mode = device_get_phy_mode(kdev);
  449. if (phy_mode < 0) {
  450. dev_err(kdev, "invalid PHY mode property\n");
  451. return phy_mode;
  452. }
  453. priv->phy_interface = phy_mode;
  454. /* We need to specifically look up whether this PHY interface is
  455. * internal or not *before* we even try to probe the PHY driver
  456. * over MDIO as we may have shut down the internal PHY for power
  457. * saving purposes.
  458. */
  459. if (priv->phy_interface == PHY_INTERFACE_MODE_INTERNAL)
  460. priv->internal_phy = true;
  461. return 0;
  462. }
  463. static int bcmgenet_mii_of_init(struct bcmgenet_priv *priv)
  464. {
  465. struct device_node *dn = priv->pdev->dev.of_node;
  466. struct phy_device *phydev;
  467. int ret;
  468. /* Fetch the PHY phandle */
  469. priv->phy_dn = of_parse_phandle(dn, "phy-handle", 0);
  470. /* In the case of a fixed PHY, the DT node associated
  471. * to the PHY is the Ethernet MAC DT node.
  472. */
  473. if (!priv->phy_dn && of_phy_is_fixed_link(dn)) {
  474. ret = of_phy_register_fixed_link(dn);
  475. if (ret)
  476. return ret;
  477. priv->phy_dn = of_node_get(dn);
  478. }
  479. /* Get the link mode */
  480. ret = bcmgenet_phy_interface_init(priv);
  481. if (ret)
  482. return ret;
  483. /* Make sure we initialize MoCA PHYs with a link down */
  484. if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
  485. phydev = of_phy_find_device(dn);
  486. if (phydev) {
  487. phydev->link = 0;
  488. put_device(&phydev->mdio.dev);
  489. }
  490. }
  491. return 0;
  492. }
  493. static int bcmgenet_mii_pd_init(struct bcmgenet_priv *priv)
  494. {
  495. struct device *kdev = &priv->pdev->dev;
  496. struct bcmgenet_platform_data *pd = kdev->platform_data;
  497. char phy_name[MII_BUS_ID_SIZE + 3];
  498. char mdio_bus_id[MII_BUS_ID_SIZE];
  499. struct phy_device *phydev;
  500. snprintf(mdio_bus_id, MII_BUS_ID_SIZE, "%s-%d",
  501. UNIMAC_MDIO_DRV_NAME, priv->pdev->id);
  502. if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
  503. snprintf(phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
  504. mdio_bus_id, pd->phy_address);
  505. /*
  506. * Internal or external PHY with MDIO access
  507. */
  508. phydev = phy_attach(priv->dev, phy_name, pd->phy_interface);
  509. if (IS_ERR(phydev)) {
  510. dev_err(kdev, "failed to register PHY device\n");
  511. return PTR_ERR(phydev);
  512. }
  513. } else {
  514. /*
  515. * MoCA port or no MDIO access.
  516. * Use fixed PHY to represent the link layer.
  517. */
  518. struct fixed_phy_status fphy_status = {
  519. .link = 1,
  520. .speed = pd->phy_speed,
  521. .duplex = pd->phy_duplex,
  522. .pause = 0,
  523. .asym_pause = 0,
  524. };
  525. phydev = fixed_phy_register(PHY_POLL, &fphy_status, NULL);
  526. if (IS_ERR(phydev)) {
  527. dev_err(kdev, "failed to register fixed PHY device\n");
  528. return -ENODEV;
  529. }
  530. /* Make sure we initialize MoCA PHYs with a link down */
  531. phydev->link = 0;
  532. }
  533. priv->phy_interface = pd->phy_interface;
  534. return 0;
  535. }
  536. static int bcmgenet_mii_bus_init(struct bcmgenet_priv *priv)
  537. {
  538. struct device *kdev = &priv->pdev->dev;
  539. struct device_node *dn = kdev->of_node;
  540. if (dn)
  541. return bcmgenet_mii_of_init(priv);
  542. else if (has_acpi_companion(kdev))
  543. return bcmgenet_phy_interface_init(priv);
  544. else
  545. return bcmgenet_mii_pd_init(priv);
  546. }
  547. int bcmgenet_mii_init(struct net_device *dev)
  548. {
  549. struct bcmgenet_priv *priv = netdev_priv(dev);
  550. int ret;
  551. ret = bcmgenet_mii_register(priv);
  552. if (ret)
  553. return ret;
  554. ret = bcmgenet_mii_bus_init(priv);
  555. if (ret)
  556. goto out;
  557. return 0;
  558. out:
  559. bcmgenet_mii_exit(dev);
  560. return ret;
  561. }
  562. void bcmgenet_mii_exit(struct net_device *dev)
  563. {
  564. struct bcmgenet_priv *priv = netdev_priv(dev);
  565. struct device_node *dn = priv->pdev->dev.of_node;
  566. if (of_phy_is_fixed_link(dn))
  567. of_phy_deregister_fixed_link(dn);
  568. of_node_put(priv->phy_dn);
  569. clk_prepare_enable(priv->clk);
  570. platform_device_unregister(priv->mii_pdev);
  571. clk_disable_unprepare(priv->clk);
  572. }