stmmac_mdio.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*******************************************************************************
  2. STMMAC Ethernet Driver -- MDIO bus implementation
  3. Provides Bus interface for MII registers
  4. Copyright (C) 2007-2009 STMicroelectronics Ltd
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms and conditions of the GNU General Public License,
  7. version 2, as published by the Free Software Foundation.
  8. This program is distributed in the hope it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. You should have received a copy of the GNU General Public License along with
  13. this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  15. The full GNU General Public License is included in this distribution in
  16. the file called "COPYING".
  17. Author: Carl Shaw <carl.shaw@st.com>
  18. Maintainer: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  19. *******************************************************************************/
  20. #include <linux/mii.h>
  21. #include <linux/phy.h>
  22. #include <linux/slab.h>
  23. #include "stmmac.h"
  24. #define MII_BUSY 0x00000001
  25. #define MII_WRITE 0x00000002
  26. /**
  27. * stmmac_mdio_read
  28. * @bus: points to the mii_bus structure
  29. * @phyaddr: MII addr reg bits 15-11
  30. * @phyreg: MII addr reg bits 10-6
  31. * Description: it reads data from the MII register from within the phy device.
  32. * For the 7111 GMAC, we must set the bit 0 in the MII address register while
  33. * accessing the PHY registers.
  34. * Fortunately, it seems this has no drawback for the 7109 MAC.
  35. */
  36. static int stmmac_mdio_read(struct mii_bus *bus, int phyaddr, int phyreg)
  37. {
  38. struct net_device *ndev = bus->priv;
  39. struct stmmac_priv *priv = netdev_priv(ndev);
  40. unsigned int mii_address = priv->hw->mii.addr;
  41. unsigned int mii_data = priv->hw->mii.data;
  42. int data;
  43. u16 regValue = (((phyaddr << 11) & (0x0000F800)) |
  44. ((phyreg << 6) & (0x000007C0)));
  45. regValue |= MII_BUSY | ((priv->plat->clk_csr & 7) << 2);
  46. do {} while (((readl(priv->ioaddr + mii_address)) & MII_BUSY) == 1);
  47. writel(regValue, priv->ioaddr + mii_address);
  48. do {} while (((readl(priv->ioaddr + mii_address)) & MII_BUSY) == 1);
  49. /* Read the data from the MII data register */
  50. data = (int)readl(priv->ioaddr + mii_data);
  51. return data;
  52. }
  53. /**
  54. * stmmac_mdio_write
  55. * @bus: points to the mii_bus structure
  56. * @phyaddr: MII addr reg bits 15-11
  57. * @phyreg: MII addr reg bits 10-6
  58. * @phydata: phy data
  59. * Description: it writes the data into the MII register from within the device.
  60. */
  61. static int stmmac_mdio_write(struct mii_bus *bus, int phyaddr, int phyreg,
  62. u16 phydata)
  63. {
  64. struct net_device *ndev = bus->priv;
  65. struct stmmac_priv *priv = netdev_priv(ndev);
  66. unsigned int mii_address = priv->hw->mii.addr;
  67. unsigned int mii_data = priv->hw->mii.data;
  68. u16 value =
  69. (((phyaddr << 11) & (0x0000F800)) | ((phyreg << 6) & (0x000007C0)))
  70. | MII_WRITE;
  71. value |= MII_BUSY | ((priv->plat->clk_csr & 7) << 2);
  72. /* Wait until any existing MII operation is complete */
  73. do {} while (((readl(priv->ioaddr + mii_address)) & MII_BUSY) == 1);
  74. /* Set the MII address register to write */
  75. writel(phydata, priv->ioaddr + mii_data);
  76. writel(value, priv->ioaddr + mii_address);
  77. /* Wait until any existing MII operation is complete */
  78. do {} while (((readl(priv->ioaddr + mii_address)) & MII_BUSY) == 1);
  79. return 0;
  80. }
  81. /**
  82. * stmmac_mdio_reset
  83. * @bus: points to the mii_bus structure
  84. * Description: reset the MII bus
  85. */
  86. static int stmmac_mdio_reset(struct mii_bus *bus)
  87. {
  88. struct net_device *ndev = bus->priv;
  89. struct stmmac_priv *priv = netdev_priv(ndev);
  90. unsigned int mii_address = priv->hw->mii.addr;
  91. if (priv->phy_reset) {
  92. pr_debug("stmmac_mdio_reset: calling phy_reset\n");
  93. priv->phy_reset(priv->plat->bsp_priv);
  94. }
  95. /* This is a workaround for problems with the STE101P PHY.
  96. * It doesn't complete its reset until at least one clock cycle
  97. * on MDC, so perform a dummy mdio read.
  98. */
  99. writel(0, priv->ioaddr + mii_address);
  100. return 0;
  101. }
  102. /**
  103. * stmmac_mdio_register
  104. * @ndev: net device structure
  105. * Description: it registers the MII bus
  106. */
  107. int stmmac_mdio_register(struct net_device *ndev)
  108. {
  109. int err = 0;
  110. struct mii_bus *new_bus;
  111. int *irqlist;
  112. struct stmmac_priv *priv = netdev_priv(ndev);
  113. int addr, found;
  114. new_bus = mdiobus_alloc();
  115. if (new_bus == NULL)
  116. return -ENOMEM;
  117. irqlist = kzalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  118. if (irqlist == NULL) {
  119. err = -ENOMEM;
  120. goto irqlist_alloc_fail;
  121. }
  122. /* Assign IRQ to phy at address phy_addr */
  123. if (priv->phy_addr != -1)
  124. irqlist[priv->phy_addr] = priv->phy_irq;
  125. new_bus->name = "STMMAC MII Bus";
  126. new_bus->read = &stmmac_mdio_read;
  127. new_bus->write = &stmmac_mdio_write;
  128. new_bus->reset = &stmmac_mdio_reset;
  129. snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", priv->plat->bus_id);
  130. new_bus->priv = ndev;
  131. new_bus->irq = irqlist;
  132. new_bus->phy_mask = priv->phy_mask;
  133. new_bus->parent = priv->device;
  134. err = mdiobus_register(new_bus);
  135. if (err != 0) {
  136. pr_err("%s: Cannot register as MDIO bus\n", new_bus->name);
  137. goto bus_register_fail;
  138. }
  139. priv->mii = new_bus;
  140. found = 0;
  141. for (addr = 0; addr < 32; addr++) {
  142. struct phy_device *phydev = new_bus->phy_map[addr];
  143. if (phydev) {
  144. if (priv->phy_addr == -1) {
  145. priv->phy_addr = addr;
  146. phydev->irq = priv->phy_irq;
  147. irqlist[addr] = priv->phy_irq;
  148. }
  149. pr_info("%s: PHY ID %08x at %d IRQ %d (%s)%s\n",
  150. ndev->name, phydev->phy_id, addr,
  151. phydev->irq, dev_name(&phydev->dev),
  152. (addr == priv->phy_addr) ? " active" : "");
  153. found = 1;
  154. }
  155. }
  156. if (!found)
  157. pr_warning("%s: No PHY found\n", ndev->name);
  158. return 0;
  159. bus_register_fail:
  160. kfree(irqlist);
  161. irqlist_alloc_fail:
  162. kfree(new_bus);
  163. return err;
  164. }
  165. /**
  166. * stmmac_mdio_unregister
  167. * @ndev: net device structure
  168. * Description: it unregisters the MII bus
  169. */
  170. int stmmac_mdio_unregister(struct net_device *ndev)
  171. {
  172. struct stmmac_priv *priv = netdev_priv(ndev);
  173. mdiobus_unregister(priv->mii);
  174. priv->mii->priv = NULL;
  175. kfree(priv->mii);
  176. return 0;
  177. }