stmmac_mdio.c 8.6 KB

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