bcmmii.c 12 KB

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