stmmac_mdio.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/netdevice.h>
  21. #include <linux/mii.h>
  22. #include <linux/phy.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 long ioaddr = ndev->base_addr;
  41. unsigned int mii_address = priv->mac_type->hw.mii.addr;
  42. unsigned int mii_data = priv->mac_type->hw.mii.data;
  43. int data;
  44. u16 regValue = (((phyaddr << 11) & (0x0000F800)) |
  45. ((phyreg << 6) & (0x000007C0)));
  46. regValue |= MII_BUSY; /* in case of GMAC */
  47. do {} while (((readl(ioaddr + mii_address)) & MII_BUSY) == 1);
  48. writel(regValue, ioaddr + mii_address);
  49. do {} while (((readl(ioaddr + mii_address)) & MII_BUSY) == 1);
  50. /* Read the data from the MII data register */
  51. data = (int)readl(ioaddr + mii_data);
  52. return data;
  53. }
  54. /**
  55. * stmmac_mdio_write
  56. * @bus: points to the mii_bus structure
  57. * @phyaddr: MII addr reg bits 15-11
  58. * @phyreg: MII addr reg bits 10-6
  59. * @phydata: phy data
  60. * Description: it writes the data into the MII register from within the device.
  61. */
  62. static int stmmac_mdio_write(struct mii_bus *bus, int phyaddr, int phyreg,
  63. u16 phydata)
  64. {
  65. struct net_device *ndev = bus->priv;
  66. struct stmmac_priv *priv = netdev_priv(ndev);
  67. unsigned long ioaddr = ndev->base_addr;
  68. unsigned int mii_address = priv->mac_type->hw.mii.addr;
  69. unsigned int mii_data = priv->mac_type->hw.mii.data;
  70. u16 value =
  71. (((phyaddr << 11) & (0x0000F800)) | ((phyreg << 6) & (0x000007C0)))
  72. | MII_WRITE;
  73. value |= MII_BUSY;
  74. /* Wait until any existing MII operation is complete */
  75. do {} while (((readl(ioaddr + mii_address)) & MII_BUSY) == 1);
  76. /* Set the MII address register to write */
  77. writel(phydata, ioaddr + mii_data);
  78. writel(value, ioaddr + mii_address);
  79. /* Wait until any existing MII operation is complete */
  80. do {} while (((readl(ioaddr + mii_address)) & MII_BUSY) == 1);
  81. return 0;
  82. }
  83. /**
  84. * stmmac_mdio_reset
  85. * @bus: points to the mii_bus structure
  86. * Description: reset the MII bus
  87. */
  88. static int stmmac_mdio_reset(struct mii_bus *bus)
  89. {
  90. struct net_device *ndev = bus->priv;
  91. struct stmmac_priv *priv = netdev_priv(ndev);
  92. unsigned long ioaddr = ndev->base_addr;
  93. unsigned int mii_address = priv->mac_type->hw.mii.addr;
  94. if (priv->phy_reset) {
  95. pr_debug("stmmac_mdio_reset: calling phy_reset\n");
  96. priv->phy_reset(priv->bsp_priv);
  97. }
  98. /* This is a workaround for problems with the STE101P PHY.
  99. * It doesn't complete its reset until at least one clock cycle
  100. * on MDC, so perform a dummy mdio read.
  101. */
  102. writel(0, ioaddr + mii_address);
  103. return 0;
  104. }
  105. /**
  106. * stmmac_mdio_register
  107. * @ndev: net device structure
  108. * Description: it registers the MII bus
  109. */
  110. int stmmac_mdio_register(struct net_device *ndev)
  111. {
  112. int err = 0;
  113. struct mii_bus *new_bus;
  114. int *irqlist;
  115. struct stmmac_priv *priv = netdev_priv(ndev);
  116. int addr, found;
  117. new_bus = mdiobus_alloc();
  118. if (new_bus == NULL)
  119. return -ENOMEM;
  120. irqlist = kzalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  121. if (irqlist == NULL) {
  122. err = -ENOMEM;
  123. goto irqlist_alloc_fail;
  124. }
  125. /* Assign IRQ to phy at address phy_addr */
  126. if (priv->phy_addr != -1)
  127. irqlist[priv->phy_addr] = priv->phy_irq;
  128. new_bus->name = "STMMAC MII Bus";
  129. new_bus->read = &stmmac_mdio_read;
  130. new_bus->write = &stmmac_mdio_write;
  131. new_bus->reset = &stmmac_mdio_reset;
  132. snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", priv->bus_id);
  133. new_bus->priv = ndev;
  134. new_bus->irq = irqlist;
  135. new_bus->phy_mask = priv->phy_mask;
  136. new_bus->parent = priv->device;
  137. err = mdiobus_register(new_bus);
  138. if (err != 0) {
  139. pr_err("%s: Cannot register as MDIO bus\n", new_bus->name);
  140. goto bus_register_fail;
  141. }
  142. priv->mii = new_bus;
  143. found = 0;
  144. for (addr = 0; addr < 32; addr++) {
  145. struct phy_device *phydev = new_bus->phy_map[addr];
  146. if (phydev) {
  147. if (priv->phy_addr == -1) {
  148. priv->phy_addr = addr;
  149. phydev->irq = priv->phy_irq;
  150. irqlist[addr] = priv->phy_irq;
  151. }
  152. pr_info("%s: PHY ID %08x at %d IRQ %d (%s)%s\n",
  153. ndev->name, phydev->phy_id, addr,
  154. phydev->irq, dev_name(&phydev->dev),
  155. (addr == priv->phy_addr) ? " active" : "");
  156. found = 1;
  157. }
  158. }
  159. if (!found)
  160. pr_warning("%s: No PHY found\n", ndev->name);
  161. return 0;
  162. bus_register_fail:
  163. kfree(irqlist);
  164. irqlist_alloc_fail:
  165. kfree(new_bus);
  166. return err;
  167. }
  168. /**
  169. * stmmac_mdio_unregister
  170. * @ndev: net device structure
  171. * Description: it unregisters the MII bus
  172. */
  173. int stmmac_mdio_unregister(struct net_device *ndev)
  174. {
  175. struct stmmac_priv *priv = netdev_priv(ndev);
  176. mdiobus_unregister(priv->mii);
  177. priv->mii->priv = NULL;
  178. kfree(priv->mii);
  179. return 0;
  180. }