702-powerpc_ibm_phy_add_dt_parser.patch 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. From 59b394d0d2b2e11687e757820c52d6470d15a9c5 Mon Sep 17 00:00:00 2001
  2. From: Christian Lamparter <[email protected]>
  3. Date: Mon, 13 Jun 2016 15:42:21 +0200
  4. Subject: [PATCH] phy device tree support for emac
  5. ---
  6. drivers/net/ethernet/ibm/emac/core.c | 261 +++++++++++++++++++++++++++++++++++
  7. drivers/net/ethernet/ibm/emac/core.h | 4 +
  8. 2 files changed, 265 insertions(+)
  9. --- a/drivers/net/ethernet/ibm/emac/core.c
  10. +++ b/drivers/net/ethernet/ibm/emac/core.c
  11. @@ -42,6 +42,7 @@
  12. #include <linux/of_address.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/of_net.h>
  15. +#include <linux/of_mdio.h>
  16. #include <linux/slab.h>
  17. #include <asm/processor.h>
  18. @@ -2392,6 +2393,246 @@ static int emac_read_uint_prop(struct de
  19. return 0;
  20. }
  21. +static void emac_adjust_link(struct net_device *ndev)
  22. +{
  23. + struct emac_instance *dev = netdev_priv(ndev);
  24. + struct phy_device *phy = dev->phy_dev;
  25. +
  26. + dev->phy.autoneg = phy->autoneg;
  27. + dev->phy.speed = phy->speed;
  28. + dev->phy.duplex = phy->duplex;
  29. + dev->phy.pause = phy->pause;
  30. + dev->phy.asym_pause = phy->asym_pause;
  31. + dev->phy.advertising = phy->advertising;
  32. +}
  33. +
  34. +static int emac_mii_bus_read(struct mii_bus *bus, int addr, int regnum)
  35. +{
  36. + return emac_mdio_read(bus->priv, addr, regnum);
  37. +}
  38. +
  39. +static int emac_mii_bus_write(struct mii_bus *bus, int addr, int regnum, u16 val)
  40. +{
  41. + emac_mdio_write(bus->priv, addr, regnum, val);
  42. + return 0;
  43. +}
  44. +
  45. +static int emac_mii_bus_reset(struct mii_bus *bus)
  46. +{
  47. + struct emac_instance *dev = netdev_priv(bus->priv);
  48. +
  49. + emac_mii_reset_phy(&dev->phy);
  50. + return 0;
  51. +}
  52. +
  53. +static int emac_mdio_setup_aneg(struct mii_phy *phy, u32 advertise)
  54. +{
  55. + struct net_device *ndev = phy->dev;
  56. + struct emac_instance *dev = netdev_priv(ndev);
  57. +
  58. + dev->phy.autoneg = AUTONEG_ENABLE;
  59. + dev->phy.speed = SPEED_1000;
  60. + dev->phy.duplex = DUPLEX_FULL;
  61. + dev->phy.advertising = advertise;
  62. + phy->autoneg = AUTONEG_ENABLE;
  63. + phy->speed = dev->phy.speed;
  64. + phy->duplex = dev->phy.duplex;
  65. + phy->advertising = advertise;
  66. + return phy_start_aneg(dev->phy_dev);
  67. +}
  68. +
  69. +static int emac_mdio_setup_forced(struct mii_phy *phy, int speed, int fd)
  70. +{
  71. + struct net_device *ndev = phy->dev;
  72. + struct emac_instance *dev = netdev_priv(ndev);
  73. +
  74. + dev->phy.autoneg = AUTONEG_DISABLE;
  75. + dev->phy.speed = speed;
  76. + dev->phy.duplex = fd;
  77. + phy->autoneg = AUTONEG_DISABLE;
  78. + phy->speed = speed;
  79. + phy->duplex = fd;
  80. + return phy_start_aneg(dev->phy_dev);
  81. +}
  82. +
  83. +static int emac_mdio_poll_link(struct mii_phy *phy)
  84. +{
  85. + struct net_device *ndev = phy->dev;
  86. + struct emac_instance *dev = netdev_priv(ndev);
  87. + int res;
  88. +
  89. + res = phy_read_status(dev->phy_dev);
  90. + if (res) {
  91. + dev_err(&dev->ndev->dev, "link update failed (%d).", res);
  92. + return ethtool_op_get_link(ndev);
  93. + }
  94. +
  95. + return dev->phy_dev->link;
  96. +}
  97. +
  98. +static int emac_mdio_read_link(struct mii_phy *phy)
  99. +{
  100. + struct net_device *ndev = phy->dev;
  101. + struct emac_instance *dev = netdev_priv(ndev);
  102. + int res;
  103. +
  104. + res = phy_read_status(dev->phy_dev);
  105. + if (res)
  106. + return res;
  107. +
  108. + dev->phy.speed = phy->speed;
  109. + dev->phy.duplex = phy->duplex;
  110. + dev->phy.pause = phy->pause;
  111. + dev->phy.asym_pause = phy->asym_pause;
  112. + return 0;
  113. +}
  114. +
  115. +static int emac_mdio_init_phy(struct mii_phy *phy)
  116. +{
  117. + struct net_device *ndev = phy->dev;
  118. + struct emac_instance *dev = netdev_priv(ndev);
  119. +
  120. + phy_start(dev->phy_dev);
  121. + dev->phy.autoneg = phy->autoneg;
  122. + dev->phy.speed = phy->speed;
  123. + dev->phy.duplex = phy->duplex;
  124. + dev->phy.advertising = phy->advertising;
  125. + dev->phy.pause = phy->pause;
  126. + dev->phy.asym_pause = phy->asym_pause;
  127. +
  128. + return phy_init_hw(dev->phy_dev);
  129. +}
  130. +
  131. +static const struct mii_phy_ops emac_dt_mdio_phy_ops = {
  132. + .init = emac_mdio_init_phy,
  133. + .setup_aneg = emac_mdio_setup_aneg,
  134. + .setup_forced = emac_mdio_setup_forced,
  135. + .poll_link = emac_mdio_poll_link,
  136. + .read_link = emac_mdio_read_link,
  137. +};
  138. +
  139. +static void emac_dt_phy_mdio_cleanup(struct emac_instance *dev)
  140. +{
  141. + if (dev->mii_bus) {
  142. + if (dev->mii_bus->state == MDIOBUS_REGISTERED)
  143. + mdiobus_unregister(dev->mii_bus);
  144. + mdiobus_free(dev->mii_bus);
  145. + dev->mii_bus = NULL;
  146. + }
  147. + kfree(dev->phy.def);
  148. + dev->phy.def = NULL;
  149. +}
  150. +
  151. +static int emac_dt_mdio_probe(struct emac_instance *dev)
  152. +{
  153. + struct device_node *mii_np;
  154. + int res;
  155. +
  156. + mii_np = of_get_child_by_name(dev->ofdev->dev.of_node, "mdio");
  157. + if (!mii_np) {
  158. + dev_err(&dev->ndev->dev, "no mdio definition found.");
  159. + return -ENODEV;
  160. + }
  161. +
  162. + if (!of_device_is_available(mii_np)) {
  163. + res = 1;
  164. + goto err_put_node;
  165. + }
  166. +
  167. + dev->mii_bus = mdiobus_alloc();
  168. + if (!dev->mii_bus) {
  169. + res = -ENOMEM;
  170. + goto err_cleanup_mdio;
  171. + }
  172. +
  173. + dev->mii_bus->priv = dev->ndev;
  174. + dev->mii_bus->parent = dev->ndev->dev.parent;
  175. + dev->mii_bus->name = "emac_mdio";
  176. + dev->mii_bus->read = &emac_mii_bus_read;
  177. + dev->mii_bus->write = &emac_mii_bus_write;
  178. + dev->mii_bus->reset = &emac_mii_bus_reset;
  179. + snprintf(dev->mii_bus->id, MII_BUS_ID_SIZE, "%s", dev->mii_bus->name);
  180. +
  181. + res = of_mdiobus_register(dev->mii_bus, mii_np);
  182. + if (res) {
  183. + dev_err(&dev->ndev->dev, "cannot register MDIO bus %s (%d)",
  184. + dev->mii_bus->name, res);
  185. + goto err_cleanup_mdio;
  186. + }
  187. + of_node_put(mii_np);
  188. + return 0;
  189. +
  190. + err_cleanup_mdio:
  191. + emac_dt_phy_mdio_cleanup(dev);
  192. + err_put_node:
  193. + of_node_put(mii_np);
  194. + return res;
  195. +}
  196. +
  197. +static int emac_dt_phy_probe(struct emac_instance *dev,
  198. + struct device_node *phy_handle)
  199. +{
  200. + u32 phy_flags = 0;
  201. + int res;
  202. +
  203. + res = of_property_read_u32(phy_handle, "phy-flags", &phy_flags);
  204. + if (res < 0 && res != -EINVAL)
  205. + return res;
  206. +
  207. + dev->phy.def = kzalloc(sizeof(*dev->phy.def), GFP_KERNEL);
  208. + if (!dev->phy.def)
  209. + return -ENOMEM;
  210. +
  211. + dev->phy_dev = of_phy_connect(dev->ndev, phy_handle,
  212. + &emac_adjust_link, phy_flags,
  213. + PHY_INTERFACE_MODE_RGMII);
  214. + if (!dev->phy_dev) {
  215. + dev_err(&dev->ndev->dev, "failed to connect to PHY.");
  216. + kfree(dev->phy.dev);
  217. + dev->phy.dev = NULL;
  218. + return -ENODEV;
  219. + }
  220. +
  221. + dev->phy.def->phy_id = dev->phy_dev->drv->phy_id;
  222. + dev->phy.def->phy_id_mask = dev->phy_dev->drv->phy_id_mask;
  223. + dev->phy.def->name = dev->phy_dev->drv->name;
  224. + dev->phy.def->ops = &emac_dt_mdio_phy_ops;
  225. + dev->phy.features = dev->phy_dev->supported;
  226. + dev->phy.address = dev->phy_dev->addr;
  227. + dev->phy.mode = dev->phy_dev->interface;
  228. + return 0;
  229. +}
  230. +
  231. +static int emac_probe_dt_phy(struct emac_instance *dev)
  232. +{
  233. + struct device_node *np = dev->ofdev->dev.of_node;
  234. + struct device_node *phy_handle;
  235. + int res = 0;
  236. +
  237. + phy_handle = of_parse_phandle(np, "phy-handle", 0);
  238. +
  239. + if (phy_handle) {
  240. + res = emac_dt_mdio_probe(dev);
  241. + if (res)
  242. + goto out;
  243. +
  244. + res = emac_dt_phy_probe(dev, phy_handle);
  245. + if (res) {
  246. + emac_dt_phy_mdio_cleanup(dev);
  247. + goto out;
  248. + }
  249. +
  250. + return 1;
  251. + }
  252. +
  253. + out:
  254. + of_node_put(phy_handle);
  255. + /* if no phy device was specifie in the device tree, then we fallback
  256. + * to the old emac_phy.c probe code for compatibility reasons.
  257. + */
  258. + return res;
  259. +}
  260. +
  261. static int emac_init_phy(struct emac_instance *dev)
  262. {
  263. struct device_node *np = dev->ofdev->dev.of_node;
  264. @@ -2462,6 +2703,18 @@ static int emac_init_phy(struct emac_ins
  265. emac_configure(dev);
  266. + if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII)) {
  267. + int res = emac_probe_dt_phy(dev);
  268. +
  269. + if (res == 1)
  270. + goto init_phy;
  271. + if (res < 0)
  272. + dev_err(&dev->ndev->dev, "failed to attach dt phy (%d).",
  273. + res);
  274. +
  275. + /* continue with old code */
  276. + }
  277. +
  278. if (dev->phy_address != 0xffffffff)
  279. phy_map = ~(1 << dev->phy_address);
  280. @@ -2489,6 +2742,7 @@ static int emac_init_phy(struct emac_ins
  281. return -ENXIO;
  282. }
  283. + init_phy:
  284. /* Init PHY */
  285. if (dev->phy.def->ops->init)
  286. dev->phy.def->ops->init(&dev->phy);
  287. @@ -2907,6 +3161,8 @@ static int emac_probe(struct platform_de
  288. /* I have a bad feeling about this ... */
  289. err_detach_tah:
  290. + emac_dt_phy_mdio_cleanup(dev);
  291. +
  292. if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
  293. tah_detach(dev->tah_dev, dev->tah_port);
  294. err_detach_rgmii:
  295. @@ -2957,6 +3213,11 @@ static int emac_remove(struct platform_d
  296. if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
  297. zmii_detach(dev->zmii_dev, dev->zmii_port);
  298. + if (dev->phy_dev)
  299. + phy_disconnect(dev->phy_dev);
  300. +
  301. + emac_dt_phy_mdio_cleanup(dev);
  302. +
  303. busy_phy_map &= ~(1 << dev->phy.address);
  304. DBG(dev, "busy_phy_map now %#x" NL, busy_phy_map);
  305. --- a/drivers/net/ethernet/ibm/emac/core.h
  306. +++ b/drivers/net/ethernet/ibm/emac/core.h
  307. @@ -199,6 +199,10 @@ struct emac_instance {
  308. struct emac_instance *mdio_instance;
  309. struct mutex mdio_lock;
  310. + /* Device-tree based phy configuration */
  311. + struct mii_bus *mii_bus;
  312. + struct phy_device *phy_dev;
  313. +
  314. /* ZMII infos if any */
  315. u32 zmii_ph;
  316. u32 zmii_port;