stmmac_mdio.c 8.3 KB

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