bcmmii.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * Broadcom GENET MDIO routines
  3. *
  4. * Copyright (c) 2014 Broadcom Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/delay.h>
  12. #include <linux/wait.h>
  13. #include <linux/mii.h>
  14. #include <linux/ethtool.h>
  15. #include <linux/bitops.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/phy.h>
  19. #include <linux/phy_fixed.h>
  20. #include <linux/brcmphy.h>
  21. #include <linux/of.h>
  22. #include <linux/of_net.h>
  23. #include <linux/of_mdio.h>
  24. #include <linux/platform_data/bcmgenet.h>
  25. #include "bcmgenet.h"
  26. /* read a value from the MII */
  27. static int bcmgenet_mii_read(struct mii_bus *bus, int phy_id, int location)
  28. {
  29. int ret;
  30. struct net_device *dev = bus->priv;
  31. struct bcmgenet_priv *priv = netdev_priv(dev);
  32. u32 reg;
  33. bcmgenet_umac_writel(priv, (MDIO_RD | (phy_id << MDIO_PMD_SHIFT) |
  34. (location << MDIO_REG_SHIFT)), UMAC_MDIO_CMD);
  35. /* Start MDIO transaction*/
  36. reg = bcmgenet_umac_readl(priv, UMAC_MDIO_CMD);
  37. reg |= MDIO_START_BUSY;
  38. bcmgenet_umac_writel(priv, reg, UMAC_MDIO_CMD);
  39. wait_event_timeout(priv->wq,
  40. !(bcmgenet_umac_readl(priv, UMAC_MDIO_CMD)
  41. & MDIO_START_BUSY),
  42. HZ / 100);
  43. ret = bcmgenet_umac_readl(priv, UMAC_MDIO_CMD);
  44. /* Some broken devices are known not to release the line during
  45. * turn-around, e.g: Broadcom BCM53125 external switches, so check for
  46. * that condition here and ignore the MDIO controller read failure
  47. * indication.
  48. */
  49. if (!(bus->phy_ignore_ta_mask & 1 << phy_id) && (ret & MDIO_READ_FAIL))
  50. return -EIO;
  51. return ret & 0xffff;
  52. }
  53. /* write a value to the MII */
  54. static int bcmgenet_mii_write(struct mii_bus *bus, int phy_id,
  55. int location, u16 val)
  56. {
  57. struct net_device *dev = bus->priv;
  58. struct bcmgenet_priv *priv = netdev_priv(dev);
  59. u32 reg;
  60. bcmgenet_umac_writel(priv, (MDIO_WR | (phy_id << MDIO_PMD_SHIFT) |
  61. (location << MDIO_REG_SHIFT) | (0xffff & val)),
  62. UMAC_MDIO_CMD);
  63. reg = bcmgenet_umac_readl(priv, UMAC_MDIO_CMD);
  64. reg |= MDIO_START_BUSY;
  65. bcmgenet_umac_writel(priv, reg, UMAC_MDIO_CMD);
  66. wait_event_timeout(priv->wq,
  67. !(bcmgenet_umac_readl(priv, UMAC_MDIO_CMD) &
  68. MDIO_START_BUSY),
  69. HZ / 100);
  70. return 0;
  71. }
  72. /* setup netdev link state when PHY link status change and
  73. * update UMAC and RGMII block when link up
  74. */
  75. void bcmgenet_mii_setup(struct net_device *dev)
  76. {
  77. struct bcmgenet_priv *priv = netdev_priv(dev);
  78. struct phy_device *phydev = priv->phydev;
  79. u32 reg, cmd_bits = 0;
  80. bool status_changed = false;
  81. if (priv->old_link != phydev->link) {
  82. status_changed = true;
  83. priv->old_link = phydev->link;
  84. }
  85. if (phydev->link) {
  86. /* check speed/duplex/pause changes */
  87. if (priv->old_speed != phydev->speed) {
  88. status_changed = true;
  89. priv->old_speed = phydev->speed;
  90. }
  91. if (priv->old_duplex != phydev->duplex) {
  92. status_changed = true;
  93. priv->old_duplex = phydev->duplex;
  94. }
  95. if (priv->old_pause != phydev->pause) {
  96. status_changed = true;
  97. priv->old_pause = phydev->pause;
  98. }
  99. /* done if nothing has changed */
  100. if (!status_changed)
  101. return;
  102. /* speed */
  103. if (phydev->speed == SPEED_1000)
  104. cmd_bits = UMAC_SPEED_1000;
  105. else if (phydev->speed == SPEED_100)
  106. cmd_bits = UMAC_SPEED_100;
  107. else
  108. cmd_bits = UMAC_SPEED_10;
  109. cmd_bits <<= CMD_SPEED_SHIFT;
  110. /* duplex */
  111. if (phydev->duplex != DUPLEX_FULL)
  112. cmd_bits |= CMD_HD_EN;
  113. /* pause capability */
  114. if (!phydev->pause)
  115. cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
  116. /*
  117. * Program UMAC and RGMII block based on established
  118. * link speed, duplex, and pause. The speed set in
  119. * umac->cmd tell RGMII block which clock to use for
  120. * transmit -- 25MHz(100Mbps) or 125MHz(1Gbps).
  121. * Receive clock is provided by the PHY.
  122. */
  123. reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
  124. reg &= ~OOB_DISABLE;
  125. reg |= RGMII_LINK;
  126. bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
  127. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  128. reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
  129. CMD_HD_EN |
  130. CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE);
  131. reg |= cmd_bits;
  132. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  133. } else {
  134. /* done if nothing has changed */
  135. if (!status_changed)
  136. return;
  137. /* needed for MoCA fixed PHY to reflect correct link status */
  138. netif_carrier_off(dev);
  139. }
  140. phy_print_status(phydev);
  141. }
  142. static int bcmgenet_fixed_phy_link_update(struct net_device *dev,
  143. struct fixed_phy_status *status)
  144. {
  145. if (dev && dev->phydev && status)
  146. status->link = dev->phydev->link;
  147. return 0;
  148. }
  149. /* Perform a voluntary PHY software reset, since the EPHY is very finicky about
  150. * not doing it and will start corrupting packets
  151. */
  152. void bcmgenet_mii_reset(struct net_device *dev)
  153. {
  154. struct bcmgenet_priv *priv = netdev_priv(dev);
  155. if (GENET_IS_V4(priv))
  156. return;
  157. if (priv->phydev) {
  158. phy_init_hw(priv->phydev);
  159. phy_start_aneg(priv->phydev);
  160. }
  161. }
  162. void bcmgenet_phy_power_set(struct net_device *dev, bool enable)
  163. {
  164. struct bcmgenet_priv *priv = netdev_priv(dev);
  165. u32 reg = 0;
  166. /* EXT_GPHY_CTRL is only valid for GENETv4 and onward */
  167. if (!GENET_IS_V4(priv))
  168. return;
  169. reg = bcmgenet_ext_readl(priv, EXT_GPHY_CTRL);
  170. if (enable) {
  171. reg &= ~EXT_CK25_DIS;
  172. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  173. mdelay(1);
  174. reg &= ~(EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN);
  175. reg |= EXT_GPHY_RESET;
  176. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  177. mdelay(1);
  178. reg &= ~EXT_GPHY_RESET;
  179. } else {
  180. reg |= EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN | EXT_GPHY_RESET;
  181. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  182. mdelay(1);
  183. reg |= EXT_CK25_DIS;
  184. }
  185. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  186. udelay(60);
  187. }
  188. static void bcmgenet_internal_phy_setup(struct net_device *dev)
  189. {
  190. struct bcmgenet_priv *priv = netdev_priv(dev);
  191. u32 reg;
  192. /* Power up PHY */
  193. bcmgenet_phy_power_set(dev, true);
  194. /* enable APD */
  195. reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
  196. reg |= EXT_PWR_DN_EN_LD;
  197. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  198. bcmgenet_mii_reset(dev);
  199. }
  200. static void bcmgenet_moca_phy_setup(struct bcmgenet_priv *priv)
  201. {
  202. u32 reg;
  203. /* Speed settings are set in bcmgenet_mii_setup() */
  204. reg = bcmgenet_sys_readl(priv, SYS_PORT_CTRL);
  205. reg |= LED_ACT_SOURCE_MAC;
  206. bcmgenet_sys_writel(priv, reg, SYS_PORT_CTRL);
  207. if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
  208. fixed_phy_set_link_update(priv->phydev,
  209. bcmgenet_fixed_phy_link_update);
  210. }
  211. int bcmgenet_mii_config(struct net_device *dev)
  212. {
  213. struct bcmgenet_priv *priv = netdev_priv(dev);
  214. struct phy_device *phydev = priv->phydev;
  215. struct device *kdev = &priv->pdev->dev;
  216. const char *phy_name = NULL;
  217. u32 id_mode_dis = 0;
  218. u32 port_ctrl;
  219. u32 reg;
  220. priv->ext_phy = !priv->internal_phy &&
  221. (priv->phy_interface != PHY_INTERFACE_MODE_MOCA);
  222. if (priv->internal_phy)
  223. priv->phy_interface = PHY_INTERFACE_MODE_NA;
  224. switch (priv->phy_interface) {
  225. case PHY_INTERFACE_MODE_NA:
  226. case PHY_INTERFACE_MODE_MOCA:
  227. /* Irrespective of the actually configured PHY speed (100 or
  228. * 1000) GENETv4 only has an internal GPHY so we will just end
  229. * up masking the Gigabit features from what we support, not
  230. * switching to the EPHY
  231. */
  232. if (GENET_IS_V4(priv))
  233. port_ctrl = PORT_MODE_INT_GPHY;
  234. else
  235. port_ctrl = PORT_MODE_INT_EPHY;
  236. bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
  237. if (priv->internal_phy) {
  238. phy_name = "internal PHY";
  239. bcmgenet_internal_phy_setup(dev);
  240. } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
  241. phy_name = "MoCA";
  242. bcmgenet_moca_phy_setup(priv);
  243. }
  244. break;
  245. case PHY_INTERFACE_MODE_MII:
  246. phy_name = "external MII";
  247. phydev->supported &= PHY_BASIC_FEATURES;
  248. bcmgenet_sys_writel(priv,
  249. PORT_MODE_EXT_EPHY, SYS_PORT_CTRL);
  250. break;
  251. case PHY_INTERFACE_MODE_REVMII:
  252. phy_name = "external RvMII";
  253. /* of_mdiobus_register took care of reading the 'max-speed'
  254. * PHY property for us, effectively limiting the PHY supported
  255. * capabilities, use that knowledge to also configure the
  256. * Reverse MII interface correctly.
  257. */
  258. if ((priv->phydev->supported & PHY_BASIC_FEATURES) ==
  259. PHY_BASIC_FEATURES)
  260. port_ctrl = PORT_MODE_EXT_RVMII_25;
  261. else
  262. port_ctrl = PORT_MODE_EXT_RVMII_50;
  263. bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
  264. break;
  265. case PHY_INTERFACE_MODE_RGMII:
  266. /* RGMII_NO_ID: TXC transitions at the same time as TXD
  267. * (requires PCB or receiver-side delay)
  268. * RGMII: Add 2ns delay on TXC (90 degree shift)
  269. *
  270. * ID is implicitly disabled for 100Mbps (RG)MII operation.
  271. */
  272. id_mode_dis = BIT(16);
  273. /* fall through */
  274. case PHY_INTERFACE_MODE_RGMII_TXID:
  275. if (id_mode_dis)
  276. phy_name = "external RGMII (no delay)";
  277. else
  278. phy_name = "external RGMII (TX delay)";
  279. bcmgenet_sys_writel(priv,
  280. PORT_MODE_EXT_GPHY, SYS_PORT_CTRL);
  281. break;
  282. default:
  283. dev_err(kdev, "unknown phy mode: %d\n", priv->phy_interface);
  284. return -EINVAL;
  285. }
  286. /* This is an external PHY (xMII), so we need to enable the RGMII
  287. * block for the interface to work
  288. */
  289. if (priv->ext_phy) {
  290. reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
  291. reg |= RGMII_MODE_EN | id_mode_dis;
  292. bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
  293. }
  294. dev_info_once(kdev, "configuring instance for %s\n", phy_name);
  295. return 0;
  296. }
  297. int bcmgenet_mii_probe(struct net_device *dev)
  298. {
  299. struct bcmgenet_priv *priv = netdev_priv(dev);
  300. struct device_node *dn = priv->pdev->dev.of_node;
  301. struct phy_device *phydev;
  302. u32 phy_flags;
  303. int ret;
  304. /* Communicate the integrated PHY revision */
  305. phy_flags = priv->gphy_rev;
  306. /* Initialize link state variables that bcmgenet_mii_setup() uses */
  307. priv->old_link = -1;
  308. priv->old_speed = -1;
  309. priv->old_duplex = -1;
  310. priv->old_pause = -1;
  311. if (dn) {
  312. phydev = of_phy_connect(dev, priv->phy_dn, bcmgenet_mii_setup,
  313. phy_flags, priv->phy_interface);
  314. if (!phydev) {
  315. pr_err("could not attach to PHY\n");
  316. return -ENODEV;
  317. }
  318. } else {
  319. phydev = priv->phydev;
  320. phydev->dev_flags = phy_flags;
  321. ret = phy_connect_direct(dev, phydev, bcmgenet_mii_setup,
  322. priv->phy_interface);
  323. if (ret) {
  324. pr_err("could not attach to PHY\n");
  325. return -ENODEV;
  326. }
  327. }
  328. priv->phydev = phydev;
  329. /* Configure port multiplexer based on what the probed PHY device since
  330. * reading the 'max-speed' property determines the maximum supported
  331. * PHY speed which is needed for bcmgenet_mii_config() to configure
  332. * things appropriately.
  333. */
  334. ret = bcmgenet_mii_config(dev);
  335. if (ret) {
  336. phy_disconnect(priv->phydev);
  337. return ret;
  338. }
  339. phydev->advertising = phydev->supported;
  340. /* The internal PHY has its link interrupts routed to the
  341. * Ethernet MAC ISRs
  342. */
  343. if (priv->internal_phy)
  344. priv->phydev->irq = PHY_IGNORE_INTERRUPT;
  345. return 0;
  346. }
  347. /* Workaround for integrated BCM7xxx Gigabit PHYs which have a problem with
  348. * their internal MDIO management controller making them fail to successfully
  349. * be read from or written to for the first transaction. We insert a dummy
  350. * BMSR read here to make sure that phy_get_device() and get_phy_id() can
  351. * correctly read the PHY MII_PHYSID1/2 registers and successfully register a
  352. * PHY device for this peripheral.
  353. *
  354. * Once the PHY driver is registered, we can workaround subsequent reads from
  355. * there (e.g: during system-wide power management).
  356. *
  357. * bus->reset is invoked before mdiobus_scan during mdiobus_register and is
  358. * therefore the right location to stick that workaround. Since we do not want
  359. * to read from non-existing PHYs, we either use bus->phy_mask or do a manual
  360. * Device Tree scan to limit the search area.
  361. */
  362. static int bcmgenet_mii_bus_reset(struct mii_bus *bus)
  363. {
  364. struct net_device *dev = bus->priv;
  365. struct bcmgenet_priv *priv = netdev_priv(dev);
  366. struct device_node *np = priv->mdio_dn;
  367. struct device_node *child = NULL;
  368. u32 read_mask = 0;
  369. int addr = 0;
  370. if (!np) {
  371. read_mask = 1 << priv->phy_addr;
  372. } else {
  373. for_each_available_child_of_node(np, child) {
  374. addr = of_mdio_parse_addr(&dev->dev, child);
  375. if (addr < 0)
  376. continue;
  377. read_mask |= 1 << addr;
  378. }
  379. }
  380. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  381. if (read_mask & 1 << addr) {
  382. dev_dbg(&dev->dev, "Workaround for PHY @ %d\n", addr);
  383. mdiobus_read(bus, addr, MII_BMSR);
  384. }
  385. }
  386. return 0;
  387. }
  388. static int bcmgenet_mii_alloc(struct bcmgenet_priv *priv)
  389. {
  390. struct mii_bus *bus;
  391. if (priv->mii_bus)
  392. return 0;
  393. priv->mii_bus = mdiobus_alloc();
  394. if (!priv->mii_bus) {
  395. pr_err("failed to allocate\n");
  396. return -ENOMEM;
  397. }
  398. bus = priv->mii_bus;
  399. bus->priv = priv->dev;
  400. bus->name = "bcmgenet MII bus";
  401. bus->parent = &priv->pdev->dev;
  402. bus->read = bcmgenet_mii_read;
  403. bus->write = bcmgenet_mii_write;
  404. bus->reset = bcmgenet_mii_bus_reset;
  405. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d",
  406. priv->pdev->name, priv->pdev->id);
  407. return 0;
  408. }
  409. static int bcmgenet_mii_of_init(struct bcmgenet_priv *priv)
  410. {
  411. struct device_node *dn = priv->pdev->dev.of_node;
  412. struct device *kdev = &priv->pdev->dev;
  413. const char *phy_mode_str = NULL;
  414. struct phy_device *phydev = NULL;
  415. char *compat;
  416. int phy_mode;
  417. int ret;
  418. compat = kasprintf(GFP_KERNEL, "brcm,genet-mdio-v%d", priv->version);
  419. if (!compat)
  420. return -ENOMEM;
  421. priv->mdio_dn = of_find_compatible_node(dn, NULL, compat);
  422. kfree(compat);
  423. if (!priv->mdio_dn) {
  424. dev_err(kdev, "unable to find MDIO bus node\n");
  425. return -ENODEV;
  426. }
  427. ret = of_mdiobus_register(priv->mii_bus, priv->mdio_dn);
  428. if (ret) {
  429. dev_err(kdev, "failed to register MDIO bus\n");
  430. return ret;
  431. }
  432. /* Fetch the PHY phandle */
  433. priv->phy_dn = of_parse_phandle(dn, "phy-handle", 0);
  434. /* In the case of a fixed PHY, the DT node associated
  435. * to the PHY is the Ethernet MAC DT node.
  436. */
  437. if (!priv->phy_dn && of_phy_is_fixed_link(dn)) {
  438. ret = of_phy_register_fixed_link(dn);
  439. if (ret)
  440. return ret;
  441. priv->phy_dn = of_node_get(dn);
  442. }
  443. /* Get the link mode */
  444. phy_mode = of_get_phy_mode(dn);
  445. priv->phy_interface = phy_mode;
  446. /* We need to specifically look up whether this PHY interface is internal
  447. * or not *before* we even try to probe the PHY driver over MDIO as we
  448. * may have shut down the internal PHY for power saving purposes.
  449. */
  450. if (phy_mode < 0) {
  451. ret = of_property_read_string(dn, "phy-mode", &phy_mode_str);
  452. if (ret < 0) {
  453. dev_err(kdev, "invalid PHY mode property\n");
  454. return ret;
  455. }
  456. priv->phy_interface = PHY_INTERFACE_MODE_NA;
  457. if (!strcasecmp(phy_mode_str, "internal"))
  458. priv->internal_phy = true;
  459. }
  460. /* Make sure we initialize MoCA PHYs with a link down */
  461. if (phy_mode == PHY_INTERFACE_MODE_MOCA) {
  462. phydev = of_phy_find_device(dn);
  463. if (phydev) {
  464. phydev->link = 0;
  465. put_device(&phydev->mdio.dev);
  466. }
  467. }
  468. return 0;
  469. }
  470. static int bcmgenet_mii_pd_init(struct bcmgenet_priv *priv)
  471. {
  472. struct device *kdev = &priv->pdev->dev;
  473. struct bcmgenet_platform_data *pd = kdev->platform_data;
  474. struct mii_bus *mdio = priv->mii_bus;
  475. struct phy_device *phydev;
  476. int ret;
  477. if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
  478. /*
  479. * Internal or external PHY with MDIO access
  480. */
  481. if (pd->phy_address >= 0 && pd->phy_address < PHY_MAX_ADDR)
  482. mdio->phy_mask = ~(1 << pd->phy_address);
  483. else
  484. mdio->phy_mask = 0;
  485. ret = mdiobus_register(mdio);
  486. if (ret) {
  487. dev_err(kdev, "failed to register MDIO bus\n");
  488. return ret;
  489. }
  490. if (pd->phy_address >= 0 && pd->phy_address < PHY_MAX_ADDR)
  491. phydev = mdiobus_get_phy(mdio, pd->phy_address);
  492. else
  493. phydev = phy_find_first(mdio);
  494. if (!phydev) {
  495. dev_err(kdev, "failed to register PHY device\n");
  496. mdiobus_unregister(mdio);
  497. return -ENODEV;
  498. }
  499. } else {
  500. /*
  501. * MoCA port or no MDIO access.
  502. * Use fixed PHY to represent the link layer.
  503. */
  504. struct fixed_phy_status fphy_status = {
  505. .link = 1,
  506. .speed = pd->phy_speed,
  507. .duplex = pd->phy_duplex,
  508. .pause = 0,
  509. .asym_pause = 0,
  510. };
  511. phydev = fixed_phy_register(PHY_POLL, &fphy_status, -1, NULL);
  512. if (!phydev || IS_ERR(phydev)) {
  513. dev_err(kdev, "failed to register fixed PHY device\n");
  514. return -ENODEV;
  515. }
  516. /* Make sure we initialize MoCA PHYs with a link down */
  517. phydev->link = 0;
  518. }
  519. priv->phydev = phydev;
  520. priv->phy_interface = pd->phy_interface;
  521. return 0;
  522. }
  523. static int bcmgenet_mii_bus_init(struct bcmgenet_priv *priv)
  524. {
  525. struct device_node *dn = priv->pdev->dev.of_node;
  526. if (dn)
  527. return bcmgenet_mii_of_init(priv);
  528. else
  529. return bcmgenet_mii_pd_init(priv);
  530. }
  531. int bcmgenet_mii_init(struct net_device *dev)
  532. {
  533. struct bcmgenet_priv *priv = netdev_priv(dev);
  534. struct device_node *dn = priv->pdev->dev.of_node;
  535. int ret;
  536. ret = bcmgenet_mii_alloc(priv);
  537. if (ret)
  538. return ret;
  539. ret = bcmgenet_mii_bus_init(priv);
  540. if (ret)
  541. goto out;
  542. return 0;
  543. out:
  544. if (of_phy_is_fixed_link(dn))
  545. of_phy_deregister_fixed_link(dn);
  546. of_node_put(priv->phy_dn);
  547. mdiobus_unregister(priv->mii_bus);
  548. mdiobus_free(priv->mii_bus);
  549. return ret;
  550. }
  551. void bcmgenet_mii_exit(struct net_device *dev)
  552. {
  553. struct bcmgenet_priv *priv = netdev_priv(dev);
  554. struct device_node *dn = priv->pdev->dev.of_node;
  555. if (of_phy_is_fixed_link(dn))
  556. of_phy_deregister_fixed_link(dn);
  557. of_node_put(priv->phy_dn);
  558. mdiobus_unregister(priv->mii_bus);
  559. mdiobus_free(priv->mii_bus);
  560. }