bcmmii.c 14 KB

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