bcmmii.c 18 KB

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