stmmac_mdio.c 11 KB

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