stmmac_mdio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 "dwxgmac2.h"
  26. #include "stmmac.h"
  27. #define MII_BUSY 0x00000001
  28. #define MII_WRITE 0x00000002
  29. /* GMAC4 defines */
  30. #define MII_GMAC4_GOC_SHIFT 2
  31. #define MII_GMAC4_WRITE (1 << MII_GMAC4_GOC_SHIFT)
  32. #define MII_GMAC4_READ (3 << MII_GMAC4_GOC_SHIFT)
  33. /* XGMAC defines */
  34. #define MII_XGMAC_SADDR BIT(18)
  35. #define MII_XGMAC_CMD_SHIFT 16
  36. #define MII_XGMAC_WRITE (1 << MII_XGMAC_CMD_SHIFT)
  37. #define MII_XGMAC_READ (3 << MII_XGMAC_CMD_SHIFT)
  38. #define MII_XGMAC_BUSY BIT(22)
  39. #define MII_XGMAC_MAX_C22ADDR 3
  40. #define MII_XGMAC_C22P_MASK GENMASK(MII_XGMAC_MAX_C22ADDR, 0)
  41. static int stmmac_xgmac2_c22_format(struct stmmac_priv *priv, int phyaddr,
  42. int phyreg, u32 *hw_addr)
  43. {
  44. unsigned int mii_data = priv->hw->mii.data;
  45. u32 tmp;
  46. /* HW does not support C22 addr >= 4 */
  47. if (phyaddr > MII_XGMAC_MAX_C22ADDR)
  48. return -ENODEV;
  49. /* Wait until any existing MII operation is complete */
  50. if (readl_poll_timeout(priv->ioaddr + mii_data, tmp,
  51. !(tmp & MII_XGMAC_BUSY), 100, 10000))
  52. return -EBUSY;
  53. /* Set port as Clause 22 */
  54. tmp = readl(priv->ioaddr + XGMAC_MDIO_C22P);
  55. tmp &= ~MII_XGMAC_C22P_MASK;
  56. tmp |= BIT(phyaddr);
  57. writel(tmp, priv->ioaddr + XGMAC_MDIO_C22P);
  58. *hw_addr = (phyaddr << 16) | (phyreg & 0x1f);
  59. return 0;
  60. }
  61. static int stmmac_xgmac2_mdio_read(struct mii_bus *bus, int phyaddr, int phyreg)
  62. {
  63. struct net_device *ndev = bus->priv;
  64. struct stmmac_priv *priv = netdev_priv(ndev);
  65. unsigned int mii_address = priv->hw->mii.addr;
  66. unsigned int mii_data = priv->hw->mii.data;
  67. u32 tmp, addr, value = MII_XGMAC_BUSY;
  68. int ret;
  69. if (phyreg & MII_ADDR_C45) {
  70. return -EOPNOTSUPP;
  71. } else {
  72. ret = stmmac_xgmac2_c22_format(priv, phyaddr, phyreg, &addr);
  73. if (ret)
  74. return ret;
  75. }
  76. value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
  77. & priv->hw->mii.clk_csr_mask;
  78. value |= MII_XGMAC_SADDR | MII_XGMAC_READ;
  79. /* Wait until any existing MII operation is complete */
  80. if (readl_poll_timeout(priv->ioaddr + mii_data, tmp,
  81. !(tmp & MII_XGMAC_BUSY), 100, 10000))
  82. return -EBUSY;
  83. /* Set the MII address register to read */
  84. writel(addr, priv->ioaddr + mii_address);
  85. writel(value, priv->ioaddr + mii_data);
  86. /* Wait until any existing MII operation is complete */
  87. if (readl_poll_timeout(priv->ioaddr + mii_data, tmp,
  88. !(tmp & MII_XGMAC_BUSY), 100, 10000))
  89. return -EBUSY;
  90. /* Read the data from the MII data register */
  91. return readl(priv->ioaddr + mii_data) & GENMASK(15, 0);
  92. }
  93. static int stmmac_xgmac2_mdio_write(struct mii_bus *bus, int phyaddr,
  94. int phyreg, u16 phydata)
  95. {
  96. struct net_device *ndev = bus->priv;
  97. struct stmmac_priv *priv = netdev_priv(ndev);
  98. unsigned int mii_address = priv->hw->mii.addr;
  99. unsigned int mii_data = priv->hw->mii.data;
  100. u32 addr, tmp, value = MII_XGMAC_BUSY;
  101. int ret;
  102. if (phyreg & MII_ADDR_C45) {
  103. return -EOPNOTSUPP;
  104. } else {
  105. ret = stmmac_xgmac2_c22_format(priv, phyaddr, phyreg, &addr);
  106. if (ret)
  107. return ret;
  108. }
  109. value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
  110. & priv->hw->mii.clk_csr_mask;
  111. value |= phydata | MII_XGMAC_SADDR;
  112. value |= MII_XGMAC_WRITE;
  113. /* Wait until any existing MII operation is complete */
  114. if (readl_poll_timeout(priv->ioaddr + mii_data, tmp,
  115. !(tmp & MII_XGMAC_BUSY), 100, 10000))
  116. return -EBUSY;
  117. /* Set the MII address register to write */
  118. writel(addr, priv->ioaddr + mii_address);
  119. writel(value, priv->ioaddr + mii_data);
  120. /* Wait until any existing MII operation is complete */
  121. return readl_poll_timeout(priv->ioaddr + mii_data, tmp,
  122. !(tmp & MII_XGMAC_BUSY), 100, 10000);
  123. }
  124. /**
  125. * stmmac_mdio_read
  126. * @bus: points to the mii_bus structure
  127. * @phyaddr: MII addr
  128. * @phyreg: MII reg
  129. * Description: it reads data from the MII register from within the phy device.
  130. * For the 7111 GMAC, we must set the bit 0 in the MII address register while
  131. * accessing the PHY registers.
  132. * Fortunately, it seems this has no drawback for the 7109 MAC.
  133. */
  134. static int stmmac_mdio_read(struct mii_bus *bus, int phyaddr, int phyreg)
  135. {
  136. struct net_device *ndev = bus->priv;
  137. struct stmmac_priv *priv = netdev_priv(ndev);
  138. unsigned int mii_address = priv->hw->mii.addr;
  139. unsigned int mii_data = priv->hw->mii.data;
  140. u32 v;
  141. int data;
  142. u32 value = MII_BUSY;
  143. value |= (phyaddr << priv->hw->mii.addr_shift)
  144. & priv->hw->mii.addr_mask;
  145. value |= (phyreg << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask;
  146. value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
  147. & priv->hw->mii.clk_csr_mask;
  148. if (priv->plat->has_gmac4)
  149. value |= MII_GMAC4_READ;
  150. if (readl_poll_timeout(priv->ioaddr + mii_address, v, !(v & MII_BUSY),
  151. 100, 10000))
  152. return -EBUSY;
  153. writel(value, priv->ioaddr + mii_address);
  154. if (readl_poll_timeout(priv->ioaddr + mii_address, v, !(v & MII_BUSY),
  155. 100, 10000))
  156. return -EBUSY;
  157. /* Read the data from the MII data register */
  158. data = (int)readl(priv->ioaddr + mii_data);
  159. return data;
  160. }
  161. /**
  162. * stmmac_mdio_write
  163. * @bus: points to the mii_bus structure
  164. * @phyaddr: MII addr
  165. * @phyreg: MII reg
  166. * @phydata: phy data
  167. * Description: it writes the data into the MII register from within the device.
  168. */
  169. static int stmmac_mdio_write(struct mii_bus *bus, int phyaddr, int phyreg,
  170. u16 phydata)
  171. {
  172. struct net_device *ndev = bus->priv;
  173. struct stmmac_priv *priv = netdev_priv(ndev);
  174. unsigned int mii_address = priv->hw->mii.addr;
  175. unsigned int mii_data = priv->hw->mii.data;
  176. u32 v;
  177. u32 value = MII_BUSY;
  178. value |= (phyaddr << priv->hw->mii.addr_shift)
  179. & priv->hw->mii.addr_mask;
  180. value |= (phyreg << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask;
  181. value |= (priv->clk_csr << priv->hw->mii.clk_csr_shift)
  182. & priv->hw->mii.clk_csr_mask;
  183. if (priv->plat->has_gmac4)
  184. value |= MII_GMAC4_WRITE;
  185. else
  186. value |= MII_WRITE;
  187. /* Wait until any existing MII operation is complete */
  188. if (readl_poll_timeout(priv->ioaddr + mii_address, v, !(v & MII_BUSY),
  189. 100, 10000))
  190. return -EBUSY;
  191. /* Set the MII address register to write */
  192. writel(phydata, priv->ioaddr + mii_data);
  193. writel(value, priv->ioaddr + mii_address);
  194. /* Wait until any existing MII operation is complete */
  195. return readl_poll_timeout(priv->ioaddr + mii_address, v, !(v & MII_BUSY),
  196. 100, 10000);
  197. }
  198. /**
  199. * stmmac_mdio_reset
  200. * @bus: points to the mii_bus structure
  201. * Description: reset the MII bus
  202. */
  203. int stmmac_mdio_reset(struct mii_bus *bus)
  204. {
  205. #if IS_ENABLED(CONFIG_STMMAC_PLATFORM)
  206. struct net_device *ndev = bus->priv;
  207. struct stmmac_priv *priv = netdev_priv(ndev);
  208. unsigned int mii_address = priv->hw->mii.addr;
  209. struct stmmac_mdio_bus_data *data = priv->plat->mdio_bus_data;
  210. #ifdef CONFIG_OF
  211. if (priv->device->of_node) {
  212. if (data->reset_gpio < 0) {
  213. struct device_node *np = priv->device->of_node;
  214. if (!np)
  215. return 0;
  216. data->reset_gpio = of_get_named_gpio(np,
  217. "snps,reset-gpio", 0);
  218. if (data->reset_gpio < 0)
  219. return 0;
  220. data->active_low = of_property_read_bool(np,
  221. "snps,reset-active-low");
  222. of_property_read_u32_array(np,
  223. "snps,reset-delays-us", data->delays, 3);
  224. if (devm_gpio_request(priv->device, data->reset_gpio,
  225. "mdio-reset"))
  226. return 0;
  227. }
  228. gpio_direction_output(data->reset_gpio,
  229. data->active_low ? 1 : 0);
  230. if (data->delays[0])
  231. msleep(DIV_ROUND_UP(data->delays[0], 1000));
  232. gpio_set_value(data->reset_gpio, data->active_low ? 0 : 1);
  233. if (data->delays[1])
  234. msleep(DIV_ROUND_UP(data->delays[1], 1000));
  235. gpio_set_value(data->reset_gpio, data->active_low ? 1 : 0);
  236. if (data->delays[2])
  237. msleep(DIV_ROUND_UP(data->delays[2], 1000));
  238. }
  239. #endif
  240. if (data->phy_reset) {
  241. netdev_dbg(ndev, "stmmac_mdio_reset: calling phy_reset\n");
  242. data->phy_reset(priv->plat->bsp_priv);
  243. }
  244. /* This is a workaround for problems with the STE101P PHY.
  245. * It doesn't complete its reset until at least one clock cycle
  246. * on MDC, so perform a dummy mdio read. To be updated for GMAC4
  247. * if needed.
  248. */
  249. if (!priv->plat->has_gmac4)
  250. writel(0, priv->ioaddr + mii_address);
  251. #endif
  252. return 0;
  253. }
  254. /**
  255. * stmmac_mdio_register
  256. * @ndev: net device structure
  257. * Description: it registers the MII bus
  258. */
  259. int stmmac_mdio_register(struct net_device *ndev)
  260. {
  261. int err = 0;
  262. struct mii_bus *new_bus;
  263. struct stmmac_priv *priv = netdev_priv(ndev);
  264. struct stmmac_mdio_bus_data *mdio_bus_data = priv->plat->mdio_bus_data;
  265. struct device_node *mdio_node = priv->plat->mdio_node;
  266. struct device *dev = ndev->dev.parent;
  267. int addr, found, max_addr;
  268. if (!mdio_bus_data)
  269. return 0;
  270. new_bus = mdiobus_alloc();
  271. if (!new_bus)
  272. return -ENOMEM;
  273. if (mdio_bus_data->irqs)
  274. memcpy(new_bus->irq, mdio_bus_data->irqs, sizeof(new_bus->irq));
  275. #ifdef CONFIG_OF
  276. if (priv->device->of_node)
  277. mdio_bus_data->reset_gpio = -1;
  278. #endif
  279. new_bus->name = "stmmac";
  280. if (priv->plat->has_xgmac) {
  281. new_bus->read = &stmmac_xgmac2_mdio_read;
  282. new_bus->write = &stmmac_xgmac2_mdio_write;
  283. /* Right now only C22 phys are supported */
  284. max_addr = MII_XGMAC_MAX_C22ADDR + 1;
  285. /* Check if DT specified an unsupported phy addr */
  286. if (priv->plat->phy_addr > MII_XGMAC_MAX_C22ADDR)
  287. dev_err(dev, "Unsupported phy_addr (max=%d)\n",
  288. MII_XGMAC_MAX_C22ADDR);
  289. } else {
  290. new_bus->read = &stmmac_mdio_read;
  291. new_bus->write = &stmmac_mdio_write;
  292. max_addr = PHY_MAX_ADDR;
  293. }
  294. new_bus->reset = &stmmac_mdio_reset;
  295. snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  296. new_bus->name, priv->plat->bus_id);
  297. new_bus->priv = ndev;
  298. new_bus->phy_mask = mdio_bus_data->phy_mask;
  299. new_bus->parent = priv->device;
  300. err = of_mdiobus_register(new_bus, mdio_node);
  301. if (err != 0) {
  302. dev_err(dev, "Cannot register the MDIO bus\n");
  303. goto bus_register_fail;
  304. }
  305. if (priv->plat->phy_node || mdio_node)
  306. goto bus_register_done;
  307. found = 0;
  308. for (addr = 0; addr < max_addr; addr++) {
  309. struct phy_device *phydev = mdiobus_get_phy(new_bus, addr);
  310. if (!phydev)
  311. continue;
  312. /*
  313. * If an IRQ was provided to be assigned after
  314. * the bus probe, do it here.
  315. */
  316. if (!mdio_bus_data->irqs &&
  317. (mdio_bus_data->probed_phy_irq > 0)) {
  318. new_bus->irq[addr] = mdio_bus_data->probed_phy_irq;
  319. phydev->irq = mdio_bus_data->probed_phy_irq;
  320. }
  321. /*
  322. * If we're going to bind the MAC to this PHY bus,
  323. * and no PHY number was provided to the MAC,
  324. * use the one probed here.
  325. */
  326. if (priv->plat->phy_addr == -1)
  327. priv->plat->phy_addr = addr;
  328. phy_attached_info(phydev);
  329. found = 1;
  330. }
  331. if (!found && !mdio_node) {
  332. dev_warn(dev, "No PHY found\n");
  333. mdiobus_unregister(new_bus);
  334. mdiobus_free(new_bus);
  335. return -ENODEV;
  336. }
  337. bus_register_done:
  338. priv->mii = new_bus;
  339. return 0;
  340. bus_register_fail:
  341. mdiobus_free(new_bus);
  342. return err;
  343. }
  344. /**
  345. * stmmac_mdio_unregister
  346. * @ndev: net device structure
  347. * Description: it unregisters the MII bus
  348. */
  349. int stmmac_mdio_unregister(struct net_device *ndev)
  350. {
  351. struct stmmac_priv *priv = netdev_priv(ndev);
  352. if (!priv->mii)
  353. return 0;
  354. mdiobus_unregister(priv->mii);
  355. priv->mii->priv = NULL;
  356. mdiobus_free(priv->mii);
  357. priv->mii = NULL;
  358. return 0;
  359. }