bcmmii.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. bcmgenet_sys_writel(priv,
  260. PORT_MODE_EXT_GPHY, SYS_PORT_CTRL);
  261. break;
  262. default:
  263. dev_err(kdev, "unknown phy mode: %d\n", priv->phy_interface);
  264. return -EINVAL;
  265. }
  266. /* This is an external PHY (xMII), so we need to enable the RGMII
  267. * block for the interface to work
  268. */
  269. if (priv->ext_phy) {
  270. reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
  271. reg |= RGMII_MODE_EN | id_mode_dis;
  272. bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
  273. }
  274. if (init)
  275. dev_info(kdev, "configuring instance for %s\n", phy_name);
  276. return 0;
  277. }
  278. static int bcmgenet_mii_probe(struct net_device *dev)
  279. {
  280. struct bcmgenet_priv *priv = netdev_priv(dev);
  281. struct device_node *dn = priv->pdev->dev.of_node;
  282. struct phy_device *phydev;
  283. u32 phy_flags;
  284. int ret;
  285. /* Communicate the integrated PHY revision */
  286. phy_flags = priv->gphy_rev;
  287. /* Initialize link state variables that bcmgenet_mii_setup() uses */
  288. priv->old_link = -1;
  289. priv->old_speed = -1;
  290. priv->old_duplex = -1;
  291. priv->old_pause = -1;
  292. if (dn) {
  293. if (priv->phydev) {
  294. pr_info("PHY already attached\n");
  295. return 0;
  296. }
  297. /* In the case of a fixed PHY, the DT node associated
  298. * to the PHY is the Ethernet MAC DT node.
  299. */
  300. if (!priv->phy_dn && of_phy_is_fixed_link(dn)) {
  301. ret = of_phy_register_fixed_link(dn);
  302. if (ret)
  303. return ret;
  304. priv->phy_dn = of_node_get(dn);
  305. }
  306. phydev = of_phy_connect(dev, priv->phy_dn, bcmgenet_mii_setup,
  307. phy_flags, priv->phy_interface);
  308. if (!phydev) {
  309. pr_err("could not attach to PHY\n");
  310. return -ENODEV;
  311. }
  312. } else {
  313. phydev = priv->phydev;
  314. phydev->dev_flags = phy_flags;
  315. ret = phy_connect_direct(dev, phydev, bcmgenet_mii_setup,
  316. priv->phy_interface);
  317. if (ret) {
  318. pr_err("could not attach to PHY\n");
  319. return -ENODEV;
  320. }
  321. }
  322. priv->phydev = phydev;
  323. /* Configure port multiplexer based on what the probed PHY device since
  324. * reading the 'max-speed' property determines the maximum supported
  325. * PHY speed which is needed for bcmgenet_mii_config() to configure
  326. * things appropriately.
  327. */
  328. ret = bcmgenet_mii_config(dev, true);
  329. if (ret) {
  330. phy_disconnect(priv->phydev);
  331. return ret;
  332. }
  333. phydev->advertising = phydev->supported;
  334. /* The internal PHY has its link interrupts routed to the
  335. * Ethernet MAC ISRs
  336. */
  337. if (phy_is_internal(priv->phydev))
  338. priv->mii_bus->irq[phydev->addr] = PHY_IGNORE_INTERRUPT;
  339. else
  340. priv->mii_bus->irq[phydev->addr] = PHY_POLL;
  341. pr_info("attached PHY at address %d [%s]\n",
  342. phydev->addr, phydev->drv->name);
  343. return 0;
  344. }
  345. static int bcmgenet_mii_alloc(struct bcmgenet_priv *priv)
  346. {
  347. struct mii_bus *bus;
  348. if (priv->mii_bus)
  349. return 0;
  350. priv->mii_bus = mdiobus_alloc();
  351. if (!priv->mii_bus) {
  352. pr_err("failed to allocate\n");
  353. return -ENOMEM;
  354. }
  355. bus = priv->mii_bus;
  356. bus->priv = priv->dev;
  357. bus->name = "bcmgenet MII bus";
  358. bus->parent = &priv->pdev->dev;
  359. bus->read = bcmgenet_mii_read;
  360. bus->write = bcmgenet_mii_write;
  361. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d",
  362. priv->pdev->name, priv->pdev->id);
  363. bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
  364. if (!bus->irq) {
  365. mdiobus_free(priv->mii_bus);
  366. return -ENOMEM;
  367. }
  368. return 0;
  369. }
  370. static int bcmgenet_mii_of_init(struct bcmgenet_priv *priv)
  371. {
  372. struct device_node *dn = priv->pdev->dev.of_node;
  373. struct device *kdev = &priv->pdev->dev;
  374. struct device_node *mdio_dn;
  375. char *compat;
  376. int ret;
  377. compat = kasprintf(GFP_KERNEL, "brcm,genet-mdio-v%d", priv->version);
  378. if (!compat)
  379. return -ENOMEM;
  380. mdio_dn = of_find_compatible_node(dn, NULL, compat);
  381. kfree(compat);
  382. if (!mdio_dn) {
  383. dev_err(kdev, "unable to find MDIO bus node\n");
  384. return -ENODEV;
  385. }
  386. ret = of_mdiobus_register(priv->mii_bus, mdio_dn);
  387. if (ret) {
  388. dev_err(kdev, "failed to register MDIO bus\n");
  389. return ret;
  390. }
  391. /* Fetch the PHY phandle */
  392. priv->phy_dn = of_parse_phandle(dn, "phy-handle", 0);
  393. /* Get the link mode */
  394. priv->phy_interface = of_get_phy_mode(dn);
  395. return 0;
  396. }
  397. static int bcmgenet_fixed_phy_link_update(struct net_device *dev,
  398. struct fixed_phy_status *status)
  399. {
  400. if (dev && dev->phydev && status)
  401. status->link = dev->phydev->link;
  402. return 0;
  403. }
  404. static int bcmgenet_mii_pd_init(struct bcmgenet_priv *priv)
  405. {
  406. struct device *kdev = &priv->pdev->dev;
  407. struct bcmgenet_platform_data *pd = kdev->platform_data;
  408. struct mii_bus *mdio = priv->mii_bus;
  409. struct phy_device *phydev;
  410. int ret;
  411. if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
  412. /*
  413. * Internal or external PHY with MDIO access
  414. */
  415. if (pd->phy_address >= 0 && pd->phy_address < PHY_MAX_ADDR)
  416. mdio->phy_mask = ~(1 << pd->phy_address);
  417. else
  418. mdio->phy_mask = 0;
  419. ret = mdiobus_register(mdio);
  420. if (ret) {
  421. dev_err(kdev, "failed to register MDIO bus\n");
  422. return ret;
  423. }
  424. if (pd->phy_address >= 0 && pd->phy_address < PHY_MAX_ADDR)
  425. phydev = mdio->phy_map[pd->phy_address];
  426. else
  427. phydev = phy_find_first(mdio);
  428. if (!phydev) {
  429. dev_err(kdev, "failed to register PHY device\n");
  430. mdiobus_unregister(mdio);
  431. return -ENODEV;
  432. }
  433. } else {
  434. /*
  435. * MoCA port or no MDIO access.
  436. * Use fixed PHY to represent the link layer.
  437. */
  438. struct fixed_phy_status fphy_status = {
  439. .link = 1,
  440. .speed = pd->phy_speed,
  441. .duplex = pd->phy_duplex,
  442. .pause = 0,
  443. .asym_pause = 0,
  444. };
  445. phydev = fixed_phy_register(PHY_POLL, &fphy_status, NULL);
  446. if (!phydev || IS_ERR(phydev)) {
  447. dev_err(kdev, "failed to register fixed PHY device\n");
  448. return -ENODEV;
  449. }
  450. if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET) {
  451. ret = fixed_phy_set_link_update(
  452. phydev, bcmgenet_fixed_phy_link_update);
  453. if (!ret)
  454. phydev->link = 0;
  455. }
  456. }
  457. priv->phydev = phydev;
  458. priv->phy_interface = pd->phy_interface;
  459. return 0;
  460. }
  461. static int bcmgenet_mii_bus_init(struct bcmgenet_priv *priv)
  462. {
  463. struct device_node *dn = priv->pdev->dev.of_node;
  464. if (dn)
  465. return bcmgenet_mii_of_init(priv);
  466. else
  467. return bcmgenet_mii_pd_init(priv);
  468. }
  469. int bcmgenet_mii_init(struct net_device *dev)
  470. {
  471. struct bcmgenet_priv *priv = netdev_priv(dev);
  472. int ret;
  473. ret = bcmgenet_mii_alloc(priv);
  474. if (ret)
  475. return ret;
  476. ret = bcmgenet_mii_bus_init(priv);
  477. if (ret)
  478. goto out_free;
  479. ret = bcmgenet_mii_probe(dev);
  480. if (ret)
  481. goto out;
  482. return 0;
  483. out:
  484. of_node_put(priv->phy_dn);
  485. mdiobus_unregister(priv->mii_bus);
  486. out_free:
  487. kfree(priv->mii_bus->irq);
  488. mdiobus_free(priv->mii_bus);
  489. return ret;
  490. }
  491. void bcmgenet_mii_exit(struct net_device *dev)
  492. {
  493. struct bcmgenet_priv *priv = netdev_priv(dev);
  494. of_node_put(priv->phy_dn);
  495. mdiobus_unregister(priv->mii_bus);
  496. kfree(priv->mii_bus->irq);
  497. mdiobus_free(priv->mii_bus);
  498. }