stmmac_mdio.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 <linux/of.h>
  24. #include <linux/of_gpio.h>
  25. #include <linux/of_mdio.h>
  26. #include <asm/io.h>
  27. #include "stmmac.h"
  28. #define MII_BUSY 0x00000001
  29. #define MII_WRITE 0x00000002
  30. /* GMAC4 defines */
  31. #define MII_GMAC4_GOC_SHIFT 2
  32. #define MII_GMAC4_WRITE (1 << MII_GMAC4_GOC_SHIFT)
  33. #define MII_GMAC4_READ (3 << MII_GMAC4_GOC_SHIFT)
  34. static int stmmac_mdio_busy_wait(void __iomem *ioaddr, unsigned int mii_addr)
  35. {
  36. unsigned long curr;
  37. unsigned long finish = jiffies + 3 * HZ;
  38. do {
  39. curr = jiffies;
  40. if (readl(ioaddr + mii_addr) & MII_BUSY)
  41. cpu_relax();
  42. else
  43. return 0;
  44. } while (!time_after_eq(curr, finish));
  45. return -EBUSY;
  46. }
  47. /**
  48. * stmmac_mdio_read
  49. * @bus: points to the mii_bus structure
  50. * @phyaddr: MII addr
  51. * @phyreg: MII reg
  52. * Description: it reads data from the MII register from within the phy device.
  53. * For the 7111 GMAC, we must set the bit 0 in the MII address register while
  54. * accessing the PHY registers.
  55. * Fortunately, it seems this has no drawback for the 7109 MAC.
  56. */
  57. static int stmmac_mdio_read(struct mii_bus *bus, int phyaddr, int phyreg)
  58. {
  59. struct net_device *ndev = bus->priv;
  60. struct stmmac_priv *priv = netdev_priv(ndev);
  61. unsigned int mii_address = priv->hw->mii.addr;
  62. unsigned int mii_data = priv->hw->mii.data;
  63. int data;
  64. u32 value = MII_BUSY;
  65. value |= (phyaddr << priv->hw->mii.addr_shift)
  66. & priv->hw->mii.addr_mask;
  67. value |= (phyreg << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask;
  68. value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
  69. & priv->hw->mii.clk_csr_mask;
  70. if (priv->plat->has_gmac4)
  71. value |= MII_GMAC4_READ;
  72. if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address))
  73. return -EBUSY;
  74. writel(value, priv->ioaddr + mii_address);
  75. if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address))
  76. return -EBUSY;
  77. /* Read the data from the MII data register */
  78. data = (int)readl(priv->ioaddr + mii_data);
  79. return data;
  80. }
  81. /**
  82. * stmmac_mdio_write
  83. * @bus: points to the mii_bus structure
  84. * @phyaddr: MII addr
  85. * @phyreg: MII reg
  86. * @phydata: phy data
  87. * Description: it writes the data into the MII register from within the device.
  88. */
  89. static int stmmac_mdio_write(struct mii_bus *bus, int phyaddr, int phyreg,
  90. u16 phydata)
  91. {
  92. struct net_device *ndev = bus->priv;
  93. struct stmmac_priv *priv = netdev_priv(ndev);
  94. unsigned int mii_address = priv->hw->mii.addr;
  95. unsigned int mii_data = priv->hw->mii.data;
  96. u32 value = MII_BUSY;
  97. value |= (phyaddr << priv->hw->mii.addr_shift)
  98. & priv->hw->mii.addr_mask;
  99. value |= (phyreg << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask;
  100. value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
  101. & priv->hw->mii.clk_csr_mask;
  102. if (priv->plat->has_gmac4)
  103. value |= MII_GMAC4_WRITE;
  104. else
  105. value |= MII_WRITE;
  106. /* Wait until any existing MII operation is complete */
  107. if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address))
  108. return -EBUSY;
  109. /* Set the MII address register to write */
  110. writel(phydata, priv->ioaddr + mii_data);
  111. writel(value, priv->ioaddr + mii_address);
  112. /* Wait until any existing MII operation is complete */
  113. return stmmac_mdio_busy_wait(priv->ioaddr, mii_address);
  114. }
  115. /**
  116. * stmmac_mdio_reset
  117. * @bus: points to the mii_bus structure
  118. * Description: reset the MII bus
  119. */
  120. int stmmac_mdio_reset(struct mii_bus *bus)
  121. {
  122. #if defined(CONFIG_STMMAC_PLATFORM)
  123. struct net_device *ndev = bus->priv;
  124. struct stmmac_priv *priv = netdev_priv(ndev);
  125. unsigned int mii_address = priv->hw->mii.addr;
  126. struct stmmac_mdio_bus_data *data = priv->plat->mdio_bus_data;
  127. #ifdef CONFIG_OF
  128. if (priv->device->of_node) {
  129. if (data->reset_gpio < 0) {
  130. struct device_node *np = priv->device->of_node;
  131. if (!np)
  132. return 0;
  133. data->reset_gpio = of_get_named_gpio(np,
  134. "snps,reset-gpio", 0);
  135. if (data->reset_gpio < 0)
  136. return 0;
  137. data->active_low = of_property_read_bool(np,
  138. "snps,reset-active-low");
  139. of_property_read_u32_array(np,
  140. "snps,reset-delays-us", data->delays, 3);
  141. if (gpio_request(data->reset_gpio, "mdio-reset"))
  142. return 0;
  143. }
  144. gpio_direction_output(data->reset_gpio,
  145. data->active_low ? 1 : 0);
  146. if (data->delays[0])
  147. msleep(DIV_ROUND_UP(data->delays[0], 1000));
  148. gpio_set_value(data->reset_gpio, data->active_low ? 0 : 1);
  149. if (data->delays[1])
  150. msleep(DIV_ROUND_UP(data->delays[1], 1000));
  151. gpio_set_value(data->reset_gpio, data->active_low ? 1 : 0);
  152. if (data->delays[2])
  153. msleep(DIV_ROUND_UP(data->delays[2], 1000));
  154. }
  155. #endif
  156. if (data->phy_reset) {
  157. netdev_dbg(ndev, "stmmac_mdio_reset: calling phy_reset\n");
  158. data->phy_reset(priv->plat->bsp_priv);
  159. }
  160. /* This is a workaround for problems with the STE101P PHY.
  161. * It doesn't complete its reset until at least one clock cycle
  162. * on MDC, so perform a dummy mdio read. To be upadted for GMAC4
  163. * if needed.
  164. */
  165. if (!priv->plat->has_gmac4)
  166. writel(0, priv->ioaddr + mii_address);
  167. #endif
  168. return 0;
  169. }
  170. /**
  171. * stmmac_mdio_register
  172. * @ndev: net device structure
  173. * Description: it registers the MII bus
  174. */
  175. int stmmac_mdio_register(struct net_device *ndev)
  176. {
  177. int err = 0;
  178. struct mii_bus *new_bus;
  179. struct stmmac_priv *priv = netdev_priv(ndev);
  180. struct stmmac_mdio_bus_data *mdio_bus_data = priv->plat->mdio_bus_data;
  181. struct device_node *mdio_node = priv->plat->mdio_node;
  182. int addr, found;
  183. if (!mdio_bus_data)
  184. return 0;
  185. new_bus = mdiobus_alloc();
  186. if (new_bus == NULL)
  187. return -ENOMEM;
  188. if (mdio_bus_data->irqs)
  189. memcpy(new_bus->irq, mdio_bus_data->irqs, sizeof(new_bus->irq));
  190. #ifdef CONFIG_OF
  191. if (priv->device->of_node)
  192. mdio_bus_data->reset_gpio = -1;
  193. #endif
  194. new_bus->name = "stmmac";
  195. new_bus->read = &stmmac_mdio_read;
  196. new_bus->write = &stmmac_mdio_write;
  197. new_bus->reset = &stmmac_mdio_reset;
  198. snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  199. new_bus->name, priv->plat->bus_id);
  200. new_bus->priv = ndev;
  201. new_bus->phy_mask = mdio_bus_data->phy_mask;
  202. new_bus->parent = priv->device;
  203. if (mdio_node)
  204. err = of_mdiobus_register(new_bus, mdio_node);
  205. else
  206. err = mdiobus_register(new_bus);
  207. if (err != 0) {
  208. netdev_err(ndev, "Cannot register the MDIO bus\n");
  209. goto bus_register_fail;
  210. }
  211. if (priv->plat->phy_node || mdio_node)
  212. goto bus_register_done;
  213. found = 0;
  214. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  215. struct phy_device *phydev = mdiobus_get_phy(new_bus, addr);
  216. if (phydev) {
  217. int act = 0;
  218. char irq_num[4];
  219. char *irq_str;
  220. /*
  221. * If an IRQ was provided to be assigned after
  222. * the bus probe, do it here.
  223. */
  224. if ((mdio_bus_data->irqs == NULL) &&
  225. (mdio_bus_data->probed_phy_irq > 0)) {
  226. new_bus->irq[addr] =
  227. mdio_bus_data->probed_phy_irq;
  228. phydev->irq = mdio_bus_data->probed_phy_irq;
  229. }
  230. /*
  231. * If we're going to bind the MAC to this PHY bus,
  232. * and no PHY number was provided to the MAC,
  233. * use the one probed here.
  234. */
  235. if (priv->plat->phy_addr == -1)
  236. priv->plat->phy_addr = addr;
  237. act = (priv->plat->phy_addr == addr);
  238. switch (phydev->irq) {
  239. case PHY_POLL:
  240. irq_str = "POLL";
  241. break;
  242. case PHY_IGNORE_INTERRUPT:
  243. irq_str = "IGNORE";
  244. break;
  245. default:
  246. sprintf(irq_num, "%d", phydev->irq);
  247. irq_str = irq_num;
  248. break;
  249. }
  250. netdev_info(ndev, "PHY ID %08x at %d IRQ %s (%s)%s\n",
  251. phydev->phy_id, addr,
  252. irq_str, phydev_name(phydev),
  253. act ? " active" : "");
  254. found = 1;
  255. }
  256. }
  257. if (!found && !mdio_node) {
  258. netdev_warn(ndev, "No PHY found\n");
  259. mdiobus_unregister(new_bus);
  260. mdiobus_free(new_bus);
  261. return -ENODEV;
  262. }
  263. bus_register_done:
  264. priv->mii = new_bus;
  265. return 0;
  266. bus_register_fail:
  267. mdiobus_free(new_bus);
  268. return err;
  269. }
  270. /**
  271. * stmmac_mdio_unregister
  272. * @ndev: net device structure
  273. * Description: it unregisters the MII bus
  274. */
  275. int stmmac_mdio_unregister(struct net_device *ndev)
  276. {
  277. struct stmmac_priv *priv = netdev_priv(ndev);
  278. if (!priv->mii)
  279. return 0;
  280. mdiobus_unregister(priv->mii);
  281. priv->mii->priv = NULL;
  282. mdiobus_free(priv->mii);
  283. priv->mii = NULL;
  284. return 0;
  285. }