xgbe-mdio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * AMD 10Gb Ethernet driver
  3. *
  4. * This file is available to you under your choice of the following two
  5. * licenses:
  6. *
  7. * License 1: GPLv2
  8. *
  9. * Copyright (c) 2014 Advanced Micro Devices, Inc.
  10. *
  11. * This file is free software; you may copy, redistribute and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This file is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * This file incorporates work covered by the following copyright and
  25. * permission notice:
  26. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  27. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  28. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  29. * and you.
  30. *
  31. * The Software IS NOT an item of Licensed Software or Licensed Product
  32. * under any End User Software License Agreement or Agreement for Licensed
  33. * Product with Synopsys or any supplement thereto. Permission is hereby
  34. * granted, free of charge, to any person obtaining a copy of this software
  35. * annotated with this license and the Software, to deal in the Software
  36. * without restriction, including without limitation the rights to use,
  37. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  38. * of the Software, and to permit persons to whom the Software is furnished
  39. * to do so, subject to the following conditions:
  40. *
  41. * The above copyright notice and this permission notice shall be included
  42. * in all copies or substantial portions of the Software.
  43. *
  44. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  45. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  46. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  47. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  48. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  54. * THE POSSIBILITY OF SUCH DAMAGE.
  55. *
  56. *
  57. * License 2: Modified BSD
  58. *
  59. * Copyright (c) 2014 Advanced Micro Devices, Inc.
  60. * All rights reserved.
  61. *
  62. * Redistribution and use in source and binary forms, with or without
  63. * modification, are permitted provided that the following conditions are met:
  64. * * Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. * * Redistributions in binary form must reproduce the above copyright
  67. * notice, this list of conditions and the following disclaimer in the
  68. * documentation and/or other materials provided with the distribution.
  69. * * Neither the name of Advanced Micro Devices, Inc. nor the
  70. * names of its contributors may be used to endorse or promote products
  71. * derived from this software without specific prior written permission.
  72. *
  73. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  74. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  75. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  76. * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  77. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  79. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  80. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  81. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  82. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  83. *
  84. * This file incorporates work covered by the following copyright and
  85. * permission notice:
  86. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  87. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  88. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  89. * and you.
  90. *
  91. * The Software IS NOT an item of Licensed Software or Licensed Product
  92. * under any End User Software License Agreement or Agreement for Licensed
  93. * Product with Synopsys or any supplement thereto. Permission is hereby
  94. * granted, free of charge, to any person obtaining a copy of this software
  95. * annotated with this license and the Software, to deal in the Software
  96. * without restriction, including without limitation the rights to use,
  97. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  98. * of the Software, and to permit persons to whom the Software is furnished
  99. * to do so, subject to the following conditions:
  100. *
  101. * The above copyright notice and this permission notice shall be included
  102. * in all copies or substantial portions of the Software.
  103. *
  104. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  105. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  106. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  107. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  108. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  109. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  110. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  111. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  112. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  113. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  114. * THE POSSIBILITY OF SUCH DAMAGE.
  115. */
  116. #include <linux/module.h>
  117. #include <linux/kmod.h>
  118. #include <linux/mdio.h>
  119. #include <linux/phy.h>
  120. #include <linux/of.h>
  121. #include "xgbe.h"
  122. #include "xgbe-common.h"
  123. static int xgbe_mdio_read(struct mii_bus *mii, int prtad, int mmd_reg)
  124. {
  125. struct xgbe_prv_data *pdata = mii->priv;
  126. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  127. int mmd_data;
  128. DBGPR_MDIO("-->xgbe_mdio_read: prtad=%#x mmd_reg=%#x\n",
  129. prtad, mmd_reg);
  130. mmd_data = hw_if->read_mmd_regs(pdata, prtad, mmd_reg);
  131. DBGPR_MDIO("<--xgbe_mdio_read: mmd_data=%#x\n", mmd_data);
  132. return mmd_data;
  133. }
  134. static int xgbe_mdio_write(struct mii_bus *mii, int prtad, int mmd_reg,
  135. u16 mmd_val)
  136. {
  137. struct xgbe_prv_data *pdata = mii->priv;
  138. struct xgbe_hw_if *hw_if = &pdata->hw_if;
  139. int mmd_data = mmd_val;
  140. DBGPR_MDIO("-->xgbe_mdio_write: prtad=%#x mmd_reg=%#x mmd_data=%#x\n",
  141. prtad, mmd_reg, mmd_data);
  142. hw_if->write_mmd_regs(pdata, prtad, mmd_reg, mmd_data);
  143. DBGPR_MDIO("<--xgbe_mdio_write\n");
  144. return 0;
  145. }
  146. void xgbe_dump_phy_registers(struct xgbe_prv_data *pdata)
  147. {
  148. struct device *dev = pdata->dev;
  149. struct phy_device *phydev = pdata->mii->phy_map[XGBE_PRTAD];
  150. int i;
  151. dev_alert(dev, "\n************* PHY Reg dump **********************\n");
  152. dev_alert(dev, "PCS Control Reg (%#04x) = %#04x\n", MDIO_CTRL1,
  153. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1));
  154. dev_alert(dev, "PCS Status Reg (%#04x) = %#04x\n", MDIO_STAT1,
  155. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_STAT1));
  156. dev_alert(dev, "Phy Id (PHYS ID 1 %#04x)= %#04x\n", MDIO_DEVID1,
  157. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVID1));
  158. dev_alert(dev, "Phy Id (PHYS ID 2 %#04x)= %#04x\n", MDIO_DEVID2,
  159. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVID2));
  160. dev_alert(dev, "Devices in Package (%#04x)= %#04x\n", MDIO_DEVS1,
  161. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVS1));
  162. dev_alert(dev, "Devices in Package (%#04x)= %#04x\n", MDIO_DEVS2,
  163. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVS2));
  164. dev_alert(dev, "Auto-Neg Control Reg (%#04x) = %#04x\n", MDIO_CTRL1,
  165. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_CTRL1));
  166. dev_alert(dev, "Auto-Neg Status Reg (%#04x) = %#04x\n", MDIO_STAT1,
  167. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_STAT1));
  168. dev_alert(dev, "Auto-Neg Ad Reg 1 (%#04x) = %#04x\n",
  169. MDIO_AN_ADVERTISE,
  170. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE));
  171. dev_alert(dev, "Auto-Neg Ad Reg 2 (%#04x) = %#04x\n",
  172. MDIO_AN_ADVERTISE + 1,
  173. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1));
  174. dev_alert(dev, "Auto-Neg Ad Reg 3 (%#04x) = %#04x\n",
  175. MDIO_AN_ADVERTISE + 2,
  176. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2));
  177. dev_alert(dev, "Auto-Neg Completion Reg (%#04x) = %#04x\n",
  178. MDIO_AN_COMP_STAT,
  179. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_COMP_STAT));
  180. dev_alert(dev, "MMD Device Mask = %#x\n",
  181. phydev->c45_ids.devices_in_package);
  182. for (i = 0; i < ARRAY_SIZE(phydev->c45_ids.device_ids); i++)
  183. dev_alert(dev, " MMD %d: ID = %#08x\n", i,
  184. phydev->c45_ids.device_ids[i]);
  185. dev_alert(dev, "\n*************************************************\n");
  186. }
  187. int xgbe_mdio_register(struct xgbe_prv_data *pdata)
  188. {
  189. struct mii_bus *mii;
  190. struct phy_device *phydev;
  191. int ret = 0;
  192. DBGPR("-->xgbe_mdio_register\n");
  193. mii = mdiobus_alloc();
  194. if (!mii) {
  195. dev_err(pdata->dev, "mdiobus_alloc failed\n");
  196. return -ENOMEM;
  197. }
  198. /* Register on the MDIO bus (don't probe any PHYs) */
  199. mii->name = XGBE_PHY_NAME;
  200. mii->read = xgbe_mdio_read;
  201. mii->write = xgbe_mdio_write;
  202. snprintf(mii->id, sizeof(mii->id), "%s", pdata->mii_bus_id);
  203. mii->priv = pdata;
  204. mii->phy_mask = ~0;
  205. mii->parent = pdata->dev;
  206. ret = mdiobus_register(mii);
  207. if (ret) {
  208. dev_err(pdata->dev, "mdiobus_register failed\n");
  209. goto err_mdiobus_alloc;
  210. }
  211. DBGPR(" mdiobus_register succeeded for %s\n", pdata->mii_bus_id);
  212. /* Probe the PCS using Clause 45 */
  213. phydev = get_phy_device(mii, XGBE_PRTAD, true);
  214. if (IS_ERR(phydev) || !phydev ||
  215. !phydev->c45_ids.device_ids[MDIO_MMD_PCS]) {
  216. dev_err(pdata->dev, "get_phy_device failed\n");
  217. ret = phydev ? PTR_ERR(phydev) : -ENOLINK;
  218. goto err_mdiobus_register;
  219. }
  220. request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT,
  221. MDIO_ID_ARGS(phydev->c45_ids.device_ids[MDIO_MMD_PCS]));
  222. ret = phy_device_register(phydev);
  223. if (ret) {
  224. dev_err(pdata->dev, "phy_device_register failed\n");
  225. goto err_phy_device;
  226. }
  227. if (!phydev->dev.driver) {
  228. dev_err(pdata->dev, "phy driver probe failed\n");
  229. ret = -EIO;
  230. goto err_phy_device;
  231. }
  232. /* Add a reference to the PHY driver so it can't be unloaded */
  233. pdata->phy_module = phydev->dev.driver->owner;
  234. if (!try_module_get(pdata->phy_module)) {
  235. dev_err(pdata->dev, "try_module_get failed\n");
  236. ret = -EIO;
  237. goto err_phy_device;
  238. }
  239. pdata->mii = mii;
  240. pdata->mdio_mmd = MDIO_MMD_PCS;
  241. phydev->autoneg = pdata->default_autoneg;
  242. if (phydev->autoneg == AUTONEG_DISABLE) {
  243. phydev->speed = pdata->default_speed;
  244. phydev->duplex = DUPLEX_FULL;
  245. phydev->advertising &= ~ADVERTISED_Autoneg;
  246. }
  247. pdata->phydev = phydev;
  248. DBGPHY_REGS(pdata);
  249. DBGPR("<--xgbe_mdio_register\n");
  250. return 0;
  251. err_phy_device:
  252. phy_device_free(phydev);
  253. err_mdiobus_register:
  254. mdiobus_unregister(mii);
  255. err_mdiobus_alloc:
  256. mdiobus_free(mii);
  257. return ret;
  258. }
  259. void xgbe_mdio_unregister(struct xgbe_prv_data *pdata)
  260. {
  261. DBGPR("-->xgbe_mdio_unregister\n");
  262. pdata->phydev = NULL;
  263. module_put(pdata->phy_module);
  264. pdata->phy_module = NULL;
  265. mdiobus_unregister(pdata->mii);
  266. pdata->mii->priv = NULL;
  267. mdiobus_free(pdata->mii);
  268. pdata->mii = NULL;
  269. DBGPR("<--xgbe_mdio_unregister\n");
  270. }