bcmmii.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Broadcom GENET MDIO routines
  3. *
  4. * Copyright (c) 2014-2017 Broadcom
  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 <linux/platform_data/mdio-bcm-unimac.h>
  26. #include "bcmgenet.h"
  27. /* setup netdev link state when PHY link status change and
  28. * update UMAC and RGMII block when link up
  29. */
  30. void bcmgenet_mii_setup(struct net_device *dev)
  31. {
  32. struct bcmgenet_priv *priv = netdev_priv(dev);
  33. struct phy_device *phydev = dev->phydev;
  34. u32 reg, cmd_bits = 0;
  35. bool status_changed = false;
  36. if (priv->old_link != phydev->link) {
  37. status_changed = true;
  38. priv->old_link = phydev->link;
  39. }
  40. if (phydev->link) {
  41. /* check speed/duplex/pause changes */
  42. if (priv->old_speed != phydev->speed) {
  43. status_changed = true;
  44. priv->old_speed = phydev->speed;
  45. }
  46. if (priv->old_duplex != phydev->duplex) {
  47. status_changed = true;
  48. priv->old_duplex = phydev->duplex;
  49. }
  50. if (priv->old_pause != phydev->pause) {
  51. status_changed = true;
  52. priv->old_pause = phydev->pause;
  53. }
  54. /* done if nothing has changed */
  55. if (!status_changed)
  56. return;
  57. /* speed */
  58. if (phydev->speed == SPEED_1000)
  59. cmd_bits = UMAC_SPEED_1000;
  60. else if (phydev->speed == SPEED_100)
  61. cmd_bits = UMAC_SPEED_100;
  62. else
  63. cmd_bits = UMAC_SPEED_10;
  64. cmd_bits <<= CMD_SPEED_SHIFT;
  65. /* duplex */
  66. if (phydev->duplex != DUPLEX_FULL)
  67. cmd_bits |= CMD_HD_EN;
  68. /* pause capability */
  69. if (!phydev->pause)
  70. cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
  71. /*
  72. * Program UMAC and RGMII block based on established
  73. * link speed, duplex, and pause. The speed set in
  74. * umac->cmd tell RGMII block which clock to use for
  75. * transmit -- 25MHz(100Mbps) or 125MHz(1Gbps).
  76. * Receive clock is provided by the PHY.
  77. */
  78. reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
  79. reg &= ~OOB_DISABLE;
  80. reg |= RGMII_LINK;
  81. bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
  82. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  83. reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
  84. CMD_HD_EN |
  85. CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE);
  86. reg |= cmd_bits;
  87. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  88. } else {
  89. /* done if nothing has changed */
  90. if (!status_changed)
  91. return;
  92. /* needed for MoCA fixed PHY to reflect correct link status */
  93. netif_carrier_off(dev);
  94. }
  95. phy_print_status(phydev);
  96. }
  97. static int bcmgenet_fixed_phy_link_update(struct net_device *dev,
  98. struct fixed_phy_status *status)
  99. {
  100. if (dev && dev->phydev && status)
  101. status->link = dev->phydev->link;
  102. return 0;
  103. }
  104. void bcmgenet_phy_power_set(struct net_device *dev, bool enable)
  105. {
  106. struct bcmgenet_priv *priv = netdev_priv(dev);
  107. u32 reg = 0;
  108. /* EXT_GPHY_CTRL is only valid for GENETv4 and onward */
  109. if (GENET_IS_V4(priv)) {
  110. reg = bcmgenet_ext_readl(priv, EXT_GPHY_CTRL);
  111. if (enable) {
  112. reg &= ~EXT_CK25_DIS;
  113. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  114. mdelay(1);
  115. reg &= ~(EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN);
  116. reg |= EXT_GPHY_RESET;
  117. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  118. mdelay(1);
  119. reg &= ~EXT_GPHY_RESET;
  120. } else {
  121. reg |= EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN |
  122. EXT_GPHY_RESET;
  123. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  124. mdelay(1);
  125. reg |= EXT_CK25_DIS;
  126. }
  127. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  128. udelay(60);
  129. } else {
  130. mdelay(1);
  131. }
  132. }
  133. static void bcmgenet_moca_phy_setup(struct bcmgenet_priv *priv)
  134. {
  135. u32 reg;
  136. if (!GENET_IS_V5(priv)) {
  137. /* Speed settings are set in bcmgenet_mii_setup() */
  138. reg = bcmgenet_sys_readl(priv, SYS_PORT_CTRL);
  139. reg |= LED_ACT_SOURCE_MAC;
  140. bcmgenet_sys_writel(priv, reg, SYS_PORT_CTRL);
  141. }
  142. if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
  143. fixed_phy_set_link_update(priv->dev->phydev,
  144. bcmgenet_fixed_phy_link_update);
  145. }
  146. int bcmgenet_mii_config(struct net_device *dev, bool init)
  147. {
  148. struct bcmgenet_priv *priv = netdev_priv(dev);
  149. struct phy_device *phydev = dev->phydev;
  150. struct device *kdev = &priv->pdev->dev;
  151. const char *phy_name = NULL;
  152. u32 id_mode_dis = 0;
  153. u32 port_ctrl;
  154. u32 reg;
  155. priv->ext_phy = !priv->internal_phy &&
  156. (priv->phy_interface != PHY_INTERFACE_MODE_MOCA);
  157. switch (priv->phy_interface) {
  158. case PHY_INTERFACE_MODE_INTERNAL:
  159. case PHY_INTERFACE_MODE_MOCA:
  160. /* Irrespective of the actually configured PHY speed (100 or
  161. * 1000) GENETv4 only has an internal GPHY so we will just end
  162. * up masking the Gigabit features from what we support, not
  163. * switching to the EPHY
  164. */
  165. if (GENET_IS_V4(priv))
  166. port_ctrl = PORT_MODE_INT_GPHY;
  167. else
  168. port_ctrl = PORT_MODE_INT_EPHY;
  169. bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
  170. if (priv->internal_phy) {
  171. phy_name = "internal PHY";
  172. } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
  173. phy_name = "MoCA";
  174. bcmgenet_moca_phy_setup(priv);
  175. }
  176. break;
  177. case PHY_INTERFACE_MODE_MII:
  178. phy_name = "external MII";
  179. phydev->supported &= PHY_BASIC_FEATURES;
  180. bcmgenet_sys_writel(priv,
  181. PORT_MODE_EXT_EPHY, SYS_PORT_CTRL);
  182. break;
  183. case PHY_INTERFACE_MODE_REVMII:
  184. phy_name = "external RvMII";
  185. /* of_mdiobus_register took care of reading the 'max-speed'
  186. * PHY property for us, effectively limiting the PHY supported
  187. * capabilities, use that knowledge to also configure the
  188. * Reverse MII interface correctly.
  189. */
  190. if ((dev->phydev->supported & PHY_BASIC_FEATURES) ==
  191. PHY_BASIC_FEATURES)
  192. port_ctrl = PORT_MODE_EXT_RVMII_25;
  193. else
  194. port_ctrl = PORT_MODE_EXT_RVMII_50;
  195. bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
  196. break;
  197. case PHY_INTERFACE_MODE_RGMII:
  198. /* RGMII_NO_ID: TXC transitions at the same time as TXD
  199. * (requires PCB or receiver-side delay)
  200. * RGMII: Add 2ns delay on TXC (90 degree shift)
  201. *
  202. * ID is implicitly disabled for 100Mbps (RG)MII operation.
  203. */
  204. id_mode_dis = BIT(16);
  205. /* fall through */
  206. case PHY_INTERFACE_MODE_RGMII_TXID:
  207. if (id_mode_dis)
  208. phy_name = "external RGMII (no delay)";
  209. else
  210. phy_name = "external RGMII (TX delay)";
  211. bcmgenet_sys_writel(priv,
  212. PORT_MODE_EXT_GPHY, SYS_PORT_CTRL);
  213. break;
  214. default:
  215. dev_err(kdev, "unknown phy mode: %d\n", priv->phy_interface);
  216. return -EINVAL;
  217. }
  218. /* This is an external PHY (xMII), so we need to enable the RGMII
  219. * block for the interface to work
  220. */
  221. if (priv->ext_phy) {
  222. reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
  223. reg |= RGMII_MODE_EN | id_mode_dis;
  224. bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
  225. }
  226. if (init)
  227. dev_info(kdev, "configuring instance for %s\n", phy_name);
  228. return 0;
  229. }
  230. int bcmgenet_mii_probe(struct net_device *dev)
  231. {
  232. struct bcmgenet_priv *priv = netdev_priv(dev);
  233. struct device_node *dn = priv->pdev->dev.of_node;
  234. struct phy_device *phydev;
  235. u32 phy_flags;
  236. int ret;
  237. /* Communicate the integrated PHY revision */
  238. phy_flags = priv->gphy_rev;
  239. /* Initialize link state variables that bcmgenet_mii_setup() uses */
  240. priv->old_link = -1;
  241. priv->old_speed = -1;
  242. priv->old_duplex = -1;
  243. priv->old_pause = -1;
  244. if (dn) {
  245. phydev = of_phy_connect(dev, priv->phy_dn, bcmgenet_mii_setup,
  246. phy_flags, priv->phy_interface);
  247. if (!phydev) {
  248. pr_err("could not attach to PHY\n");
  249. return -ENODEV;
  250. }
  251. } else {
  252. phydev = dev->phydev;
  253. phydev->dev_flags = phy_flags;
  254. ret = phy_connect_direct(dev, phydev, bcmgenet_mii_setup,
  255. priv->phy_interface);
  256. if (ret) {
  257. pr_err("could not attach to PHY\n");
  258. return -ENODEV;
  259. }
  260. }
  261. /* Configure port multiplexer based on what the probed PHY device since
  262. * reading the 'max-speed' property determines the maximum supported
  263. * PHY speed which is needed for bcmgenet_mii_config() to configure
  264. * things appropriately.
  265. */
  266. ret = bcmgenet_mii_config(dev, true);
  267. if (ret) {
  268. phy_disconnect(dev->phydev);
  269. return ret;
  270. }
  271. phydev->advertising = phydev->supported;
  272. /* The internal PHY has its link interrupts routed to the
  273. * Ethernet MAC ISRs
  274. */
  275. if (priv->internal_phy)
  276. dev->phydev->irq = PHY_IGNORE_INTERRUPT;
  277. return 0;
  278. }
  279. static struct device_node *bcmgenet_mii_of_find_mdio(struct bcmgenet_priv *priv)
  280. {
  281. struct device_node *dn = priv->pdev->dev.of_node;
  282. struct device *kdev = &priv->pdev->dev;
  283. char *compat;
  284. compat = kasprintf(GFP_KERNEL, "brcm,genet-mdio-v%d", priv->version);
  285. if (!compat)
  286. return NULL;
  287. priv->mdio_dn = of_find_compatible_node(dn, NULL, compat);
  288. kfree(compat);
  289. if (!priv->mdio_dn) {
  290. dev_err(kdev, "unable to find MDIO bus node\n");
  291. return NULL;
  292. }
  293. return priv->mdio_dn;
  294. }
  295. static void bcmgenet_mii_pdata_init(struct bcmgenet_priv *priv,
  296. struct unimac_mdio_pdata *ppd)
  297. {
  298. struct device *kdev = &priv->pdev->dev;
  299. struct bcmgenet_platform_data *pd = kdev->platform_data;
  300. if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
  301. /*
  302. * Internal or external PHY with MDIO access
  303. */
  304. if (pd->phy_address >= 0 && pd->phy_address < PHY_MAX_ADDR)
  305. ppd->phy_mask = 1 << pd->phy_address;
  306. else
  307. ppd->phy_mask = 0;
  308. }
  309. }
  310. static int bcmgenet_mii_wait(void *wait_func_data)
  311. {
  312. struct bcmgenet_priv *priv = wait_func_data;
  313. wait_event_timeout(priv->wq,
  314. !(bcmgenet_umac_readl(priv, UMAC_MDIO_CMD)
  315. & MDIO_START_BUSY),
  316. HZ / 100);
  317. return 0;
  318. }
  319. static int bcmgenet_mii_register(struct bcmgenet_priv *priv)
  320. {
  321. struct platform_device *pdev = priv->pdev;
  322. struct bcmgenet_platform_data *pdata = pdev->dev.platform_data;
  323. struct device_node *dn = pdev->dev.of_node;
  324. struct unimac_mdio_pdata ppd;
  325. struct platform_device *ppdev;
  326. struct resource *pres, res;
  327. int id, ret;
  328. pres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  329. memset(&res, 0, sizeof(res));
  330. memset(&ppd, 0, sizeof(ppd));
  331. ppd.wait_func = bcmgenet_mii_wait;
  332. ppd.wait_func_data = priv;
  333. ppd.bus_name = "bcmgenet MII bus";
  334. /* Unimac MDIO bus controller starts at UniMAC offset + MDIO_CMD
  335. * and is 2 * 32-bits word long, 8 bytes total.
  336. */
  337. res.start = pres->start + GENET_UMAC_OFF + UMAC_MDIO_CMD;
  338. res.end = res.start + 8;
  339. res.flags = IORESOURCE_MEM;
  340. if (dn)
  341. id = of_alias_get_id(dn, "eth");
  342. else
  343. id = pdev->id;
  344. ppdev = platform_device_alloc(UNIMAC_MDIO_DRV_NAME, id);
  345. if (!ppdev)
  346. return -ENOMEM;
  347. /* Retain this platform_device pointer for later cleanup */
  348. priv->mii_pdev = ppdev;
  349. ppdev->dev.parent = &pdev->dev;
  350. ppdev->dev.of_node = bcmgenet_mii_of_find_mdio(priv);
  351. if (pdata)
  352. bcmgenet_mii_pdata_init(priv, &ppd);
  353. ret = platform_device_add_resources(ppdev, &res, 1);
  354. if (ret)
  355. goto out;
  356. ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd));
  357. if (ret)
  358. goto out;
  359. ret = platform_device_add(ppdev);
  360. if (ret)
  361. goto out;
  362. return 0;
  363. out:
  364. platform_device_put(ppdev);
  365. return ret;
  366. }
  367. static int bcmgenet_mii_of_init(struct bcmgenet_priv *priv)
  368. {
  369. struct device_node *dn = priv->pdev->dev.of_node;
  370. struct device *kdev = &priv->pdev->dev;
  371. struct phy_device *phydev;
  372. int phy_mode;
  373. int ret;
  374. /* Fetch the PHY phandle */
  375. priv->phy_dn = of_parse_phandle(dn, "phy-handle", 0);
  376. /* In the case of a fixed PHY, the DT node associated
  377. * to the PHY is the Ethernet MAC DT node.
  378. */
  379. if (!priv->phy_dn && of_phy_is_fixed_link(dn)) {
  380. ret = of_phy_register_fixed_link(dn);
  381. if (ret)
  382. return ret;
  383. priv->phy_dn = of_node_get(dn);
  384. }
  385. /* Get the link mode */
  386. phy_mode = of_get_phy_mode(dn);
  387. if (phy_mode < 0) {
  388. dev_err(kdev, "invalid PHY mode property\n");
  389. return phy_mode;
  390. }
  391. priv->phy_interface = phy_mode;
  392. /* We need to specifically look up whether this PHY interface is internal
  393. * or not *before* we even try to probe the PHY driver over MDIO as we
  394. * may have shut down the internal PHY for power saving purposes.
  395. */
  396. if (priv->phy_interface == PHY_INTERFACE_MODE_INTERNAL)
  397. priv->internal_phy = true;
  398. /* Make sure we initialize MoCA PHYs with a link down */
  399. if (phy_mode == PHY_INTERFACE_MODE_MOCA) {
  400. phydev = of_phy_find_device(dn);
  401. if (phydev) {
  402. phydev->link = 0;
  403. put_device(&phydev->mdio.dev);
  404. }
  405. }
  406. return 0;
  407. }
  408. static int bcmgenet_mii_pd_init(struct bcmgenet_priv *priv)
  409. {
  410. struct device *kdev = &priv->pdev->dev;
  411. struct bcmgenet_platform_data *pd = kdev->platform_data;
  412. char phy_name[MII_BUS_ID_SIZE + 3];
  413. char mdio_bus_id[MII_BUS_ID_SIZE];
  414. struct phy_device *phydev;
  415. snprintf(mdio_bus_id, MII_BUS_ID_SIZE, "%s-%d",
  416. UNIMAC_MDIO_DRV_NAME, priv->pdev->id);
  417. if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
  418. snprintf(phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
  419. mdio_bus_id, pd->phy_address);
  420. /*
  421. * Internal or external PHY with MDIO access
  422. */
  423. phydev = phy_attach(priv->dev, phy_name, pd->phy_interface);
  424. if (!phydev) {
  425. dev_err(kdev, "failed to register PHY device\n");
  426. return -ENODEV;
  427. }
  428. } else {
  429. /*
  430. * MoCA port or no MDIO access.
  431. * Use fixed PHY to represent the link layer.
  432. */
  433. struct fixed_phy_status fphy_status = {
  434. .link = 1,
  435. .speed = pd->phy_speed,
  436. .duplex = pd->phy_duplex,
  437. .pause = 0,
  438. .asym_pause = 0,
  439. };
  440. phydev = fixed_phy_register(PHY_POLL, &fphy_status, -1, NULL);
  441. if (!phydev || IS_ERR(phydev)) {
  442. dev_err(kdev, "failed to register fixed PHY device\n");
  443. return -ENODEV;
  444. }
  445. /* Make sure we initialize MoCA PHYs with a link down */
  446. phydev->link = 0;
  447. }
  448. priv->phy_interface = pd->phy_interface;
  449. return 0;
  450. }
  451. static int bcmgenet_mii_bus_init(struct bcmgenet_priv *priv)
  452. {
  453. struct device_node *dn = priv->pdev->dev.of_node;
  454. if (dn)
  455. return bcmgenet_mii_of_init(priv);
  456. else
  457. return bcmgenet_mii_pd_init(priv);
  458. }
  459. int bcmgenet_mii_init(struct net_device *dev)
  460. {
  461. struct bcmgenet_priv *priv = netdev_priv(dev);
  462. int ret;
  463. ret = bcmgenet_mii_register(priv);
  464. if (ret)
  465. return ret;
  466. ret = bcmgenet_mii_bus_init(priv);
  467. if (ret)
  468. goto out;
  469. return 0;
  470. out:
  471. bcmgenet_mii_exit(dev);
  472. return ret;
  473. }
  474. void bcmgenet_mii_exit(struct net_device *dev)
  475. {
  476. struct bcmgenet_priv *priv = netdev_priv(dev);
  477. struct device_node *dn = priv->pdev->dev.of_node;
  478. if (of_phy_is_fixed_link(dn))
  479. of_phy_deregister_fixed_link(dn);
  480. of_node_put(priv->phy_dn);
  481. platform_device_unregister(priv->mii_pdev);
  482. }