bcmmii.c 12 KB

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