bcmmii.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. static void bcmgenet_ephy_power_up(struct net_device *dev)
  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. reg &= ~(EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN);
  154. reg |= EXT_GPHY_RESET;
  155. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  156. mdelay(2);
  157. reg &= ~EXT_GPHY_RESET;
  158. bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
  159. udelay(20);
  160. }
  161. static void bcmgenet_internal_phy_setup(struct net_device *dev)
  162. {
  163. struct bcmgenet_priv *priv = netdev_priv(dev);
  164. u32 reg;
  165. /* Power up EPHY */
  166. bcmgenet_ephy_power_up(dev);
  167. /* enable APD */
  168. reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
  169. reg |= EXT_PWR_DN_EN_LD;
  170. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  171. bcmgenet_mii_reset(dev);
  172. }
  173. static void bcmgenet_moca_phy_setup(struct bcmgenet_priv *priv)
  174. {
  175. u32 reg;
  176. /* Speed settings are set in bcmgenet_mii_setup() */
  177. reg = bcmgenet_sys_readl(priv, SYS_PORT_CTRL);
  178. reg |= LED_ACT_SOURCE_MAC;
  179. bcmgenet_sys_writel(priv, reg, SYS_PORT_CTRL);
  180. }
  181. int bcmgenet_mii_config(struct net_device *dev, bool init)
  182. {
  183. struct bcmgenet_priv *priv = netdev_priv(dev);
  184. struct phy_device *phydev = priv->phydev;
  185. struct device *kdev = &priv->pdev->dev;
  186. const char *phy_name = NULL;
  187. u32 id_mode_dis = 0;
  188. u32 port_ctrl;
  189. u32 reg;
  190. priv->ext_phy = !phy_is_internal(priv->phydev) &&
  191. (priv->phy_interface != PHY_INTERFACE_MODE_MOCA);
  192. if (phy_is_internal(priv->phydev))
  193. priv->phy_interface = PHY_INTERFACE_MODE_NA;
  194. switch (priv->phy_interface) {
  195. case PHY_INTERFACE_MODE_NA:
  196. case PHY_INTERFACE_MODE_MOCA:
  197. /* Irrespective of the actually configured PHY speed (100 or
  198. * 1000) GENETv4 only has an internal GPHY so we will just end
  199. * up masking the Gigabit features from what we support, not
  200. * switching to the EPHY
  201. */
  202. if (GENET_IS_V4(priv))
  203. port_ctrl = PORT_MODE_INT_GPHY;
  204. else
  205. port_ctrl = PORT_MODE_INT_EPHY;
  206. bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
  207. if (phy_is_internal(priv->phydev)) {
  208. phy_name = "internal PHY";
  209. bcmgenet_internal_phy_setup(dev);
  210. } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
  211. phy_name = "MoCA";
  212. bcmgenet_moca_phy_setup(priv);
  213. }
  214. break;
  215. case PHY_INTERFACE_MODE_MII:
  216. phy_name = "external MII";
  217. phydev->supported &= PHY_BASIC_FEATURES;
  218. bcmgenet_sys_writel(priv,
  219. PORT_MODE_EXT_EPHY, SYS_PORT_CTRL);
  220. break;
  221. case PHY_INTERFACE_MODE_REVMII:
  222. phy_name = "external RvMII";
  223. /* of_mdiobus_register took care of reading the 'max-speed'
  224. * PHY property for us, effectively limiting the PHY supported
  225. * capabilities, use that knowledge to also configure the
  226. * Reverse MII interface correctly.
  227. */
  228. if ((priv->phydev->supported & PHY_BASIC_FEATURES) ==
  229. PHY_BASIC_FEATURES)
  230. port_ctrl = PORT_MODE_EXT_RVMII_25;
  231. else
  232. port_ctrl = PORT_MODE_EXT_RVMII_50;
  233. bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
  234. break;
  235. case PHY_INTERFACE_MODE_RGMII:
  236. /* RGMII_NO_ID: TXC transitions at the same time as TXD
  237. * (requires PCB or receiver-side delay)
  238. * RGMII: Add 2ns delay on TXC (90 degree shift)
  239. *
  240. * ID is implicitly disabled for 100Mbps (RG)MII operation.
  241. */
  242. id_mode_dis = BIT(16);
  243. /* fall through */
  244. case PHY_INTERFACE_MODE_RGMII_TXID:
  245. if (id_mode_dis)
  246. phy_name = "external RGMII (no delay)";
  247. else
  248. phy_name = "external RGMII (TX delay)";
  249. reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
  250. reg |= RGMII_MODE_EN | id_mode_dis;
  251. bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
  252. bcmgenet_sys_writel(priv,
  253. PORT_MODE_EXT_GPHY, SYS_PORT_CTRL);
  254. break;
  255. default:
  256. dev_err(kdev, "unknown phy mode: %d\n", priv->phy_interface);
  257. return -EINVAL;
  258. }
  259. if (init)
  260. dev_info(kdev, "configuring instance for %s\n", phy_name);
  261. return 0;
  262. }
  263. static int bcmgenet_mii_probe(struct net_device *dev)
  264. {
  265. struct bcmgenet_priv *priv = netdev_priv(dev);
  266. struct device_node *dn = priv->pdev->dev.of_node;
  267. struct phy_device *phydev;
  268. u32 phy_flags;
  269. int ret;
  270. /* Communicate the integrated PHY revision */
  271. phy_flags = priv->gphy_rev;
  272. /* Initialize link state variables that bcmgenet_mii_setup() uses */
  273. priv->old_link = -1;
  274. priv->old_speed = -1;
  275. priv->old_duplex = -1;
  276. priv->old_pause = -1;
  277. if (dn) {
  278. if (priv->phydev) {
  279. pr_info("PHY already attached\n");
  280. return 0;
  281. }
  282. /* In the case of a fixed PHY, the DT node associated
  283. * to the PHY is the Ethernet MAC DT node.
  284. */
  285. if (!priv->phy_dn && of_phy_is_fixed_link(dn)) {
  286. ret = of_phy_register_fixed_link(dn);
  287. if (ret)
  288. return ret;
  289. priv->phy_dn = of_node_get(dn);
  290. }
  291. phydev = of_phy_connect(dev, priv->phy_dn, bcmgenet_mii_setup,
  292. phy_flags, priv->phy_interface);
  293. if (!phydev) {
  294. pr_err("could not attach to PHY\n");
  295. return -ENODEV;
  296. }
  297. } else {
  298. phydev = priv->phydev;
  299. phydev->dev_flags = phy_flags;
  300. ret = phy_connect_direct(dev, phydev, bcmgenet_mii_setup,
  301. priv->phy_interface);
  302. if (ret) {
  303. pr_err("could not attach to PHY\n");
  304. return -ENODEV;
  305. }
  306. }
  307. priv->phydev = phydev;
  308. /* Configure port multiplexer based on what the probed PHY device since
  309. * reading the 'max-speed' property determines the maximum supported
  310. * PHY speed which is needed for bcmgenet_mii_config() to configure
  311. * things appropriately.
  312. */
  313. ret = bcmgenet_mii_config(dev, true);
  314. if (ret) {
  315. phy_disconnect(priv->phydev);
  316. return ret;
  317. }
  318. phydev->advertising = phydev->supported;
  319. /* The internal PHY has its link interrupts routed to the
  320. * Ethernet MAC ISRs
  321. */
  322. if (phy_is_internal(priv->phydev))
  323. priv->mii_bus->irq[phydev->addr] = PHY_IGNORE_INTERRUPT;
  324. else
  325. priv->mii_bus->irq[phydev->addr] = PHY_POLL;
  326. pr_info("attached PHY at address %d [%s]\n",
  327. phydev->addr, phydev->drv->name);
  328. return 0;
  329. }
  330. static int bcmgenet_mii_alloc(struct bcmgenet_priv *priv)
  331. {
  332. struct mii_bus *bus;
  333. if (priv->mii_bus)
  334. return 0;
  335. priv->mii_bus = mdiobus_alloc();
  336. if (!priv->mii_bus) {
  337. pr_err("failed to allocate\n");
  338. return -ENOMEM;
  339. }
  340. bus = priv->mii_bus;
  341. bus->priv = priv->dev;
  342. bus->name = "bcmgenet MII bus";
  343. bus->parent = &priv->pdev->dev;
  344. bus->read = bcmgenet_mii_read;
  345. bus->write = bcmgenet_mii_write;
  346. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d",
  347. priv->pdev->name, priv->pdev->id);
  348. bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
  349. if (!bus->irq) {
  350. mdiobus_free(priv->mii_bus);
  351. return -ENOMEM;
  352. }
  353. return 0;
  354. }
  355. static int bcmgenet_mii_of_init(struct bcmgenet_priv *priv)
  356. {
  357. struct device_node *dn = priv->pdev->dev.of_node;
  358. struct device *kdev = &priv->pdev->dev;
  359. struct device_node *mdio_dn;
  360. char *compat;
  361. int ret;
  362. compat = kasprintf(GFP_KERNEL, "brcm,genet-mdio-v%d", priv->version);
  363. if (!compat)
  364. return -ENOMEM;
  365. mdio_dn = of_find_compatible_node(dn, NULL, compat);
  366. kfree(compat);
  367. if (!mdio_dn) {
  368. dev_err(kdev, "unable to find MDIO bus node\n");
  369. return -ENODEV;
  370. }
  371. ret = of_mdiobus_register(priv->mii_bus, mdio_dn);
  372. if (ret) {
  373. dev_err(kdev, "failed to register MDIO bus\n");
  374. return ret;
  375. }
  376. /* Fetch the PHY phandle */
  377. priv->phy_dn = of_parse_phandle(dn, "phy-handle", 0);
  378. /* Get the link mode */
  379. priv->phy_interface = of_get_phy_mode(dn);
  380. return 0;
  381. }
  382. static int bcmgenet_mii_pd_init(struct bcmgenet_priv *priv)
  383. {
  384. struct device *kdev = &priv->pdev->dev;
  385. struct bcmgenet_platform_data *pd = kdev->platform_data;
  386. struct mii_bus *mdio = priv->mii_bus;
  387. struct phy_device *phydev;
  388. int ret;
  389. if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
  390. /*
  391. * Internal or external PHY with MDIO access
  392. */
  393. if (pd->phy_address >= 0 && pd->phy_address < PHY_MAX_ADDR)
  394. mdio->phy_mask = ~(1 << pd->phy_address);
  395. else
  396. mdio->phy_mask = 0;
  397. ret = mdiobus_register(mdio);
  398. if (ret) {
  399. dev_err(kdev, "failed to register MDIO bus\n");
  400. return ret;
  401. }
  402. if (pd->phy_address >= 0 && pd->phy_address < PHY_MAX_ADDR)
  403. phydev = mdio->phy_map[pd->phy_address];
  404. else
  405. phydev = phy_find_first(mdio);
  406. if (!phydev) {
  407. dev_err(kdev, "failed to register PHY device\n");
  408. mdiobus_unregister(mdio);
  409. return -ENODEV;
  410. }
  411. } else {
  412. /*
  413. * MoCA port or no MDIO access.
  414. * Use fixed PHY to represent the link layer.
  415. */
  416. struct fixed_phy_status fphy_status = {
  417. .link = 1,
  418. .speed = pd->phy_speed,
  419. .duplex = pd->phy_duplex,
  420. .pause = 0,
  421. .asym_pause = 0,
  422. };
  423. phydev = fixed_phy_register(PHY_POLL, &fphy_status, NULL);
  424. if (!phydev || IS_ERR(phydev)) {
  425. dev_err(kdev, "failed to register fixed PHY device\n");
  426. return -ENODEV;
  427. }
  428. }
  429. priv->phydev = phydev;
  430. priv->phy_interface = pd->phy_interface;
  431. return 0;
  432. }
  433. static int bcmgenet_mii_bus_init(struct bcmgenet_priv *priv)
  434. {
  435. struct device_node *dn = priv->pdev->dev.of_node;
  436. if (dn)
  437. return bcmgenet_mii_of_init(priv);
  438. else
  439. return bcmgenet_mii_pd_init(priv);
  440. }
  441. int bcmgenet_mii_init(struct net_device *dev)
  442. {
  443. struct bcmgenet_priv *priv = netdev_priv(dev);
  444. int ret;
  445. ret = bcmgenet_mii_alloc(priv);
  446. if (ret)
  447. return ret;
  448. ret = bcmgenet_mii_bus_init(priv);
  449. if (ret)
  450. goto out_free;
  451. ret = bcmgenet_mii_probe(dev);
  452. if (ret)
  453. goto out;
  454. return 0;
  455. out:
  456. of_node_put(priv->phy_dn);
  457. mdiobus_unregister(priv->mii_bus);
  458. out_free:
  459. kfree(priv->mii_bus->irq);
  460. mdiobus_free(priv->mii_bus);
  461. return ret;
  462. }
  463. void bcmgenet_mii_exit(struct net_device *dev)
  464. {
  465. struct bcmgenet_priv *priv = netdev_priv(dev);
  466. of_node_put(priv->phy_dn);
  467. mdiobus_unregister(priv->mii_bus);
  468. kfree(priv->mii_bus->irq);
  469. mdiobus_free(priv->mii_bus);
  470. }