of_mdio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * OF helpers for the MDIO (Ethernet PHY) API
  3. *
  4. * Copyright (c) 2009 Secret Lab Technologies, Ltd.
  5. *
  6. * This file is released under the GPLv2
  7. *
  8. * This file provides helper functions for extracting PHY device information
  9. * out of the OpenFirmware device tree and using it to populate an mii_bus.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/device.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/err.h>
  15. #include <linux/phy.h>
  16. #include <linux/phy_fixed.h>
  17. #include <linux/of.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_mdio.h>
  21. #include <linux/of_net.h>
  22. #include <linux/module.h>
  23. #define DEFAULT_GPIO_RESET_DELAY 10 /* in microseconds */
  24. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  25. MODULE_LICENSE("GPL");
  26. /* Extract the clause 22 phy ID from the compatible string of the form
  27. * ethernet-phy-idAAAA.BBBB */
  28. static int of_get_phy_id(struct device_node *device, u32 *phy_id)
  29. {
  30. struct property *prop;
  31. const char *cp;
  32. unsigned int upper, lower;
  33. of_property_for_each_string(device, "compatible", prop, cp) {
  34. if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) == 2) {
  35. *phy_id = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF);
  36. return 0;
  37. }
  38. }
  39. return -EINVAL;
  40. }
  41. static void of_mdiobus_register_phy(struct mii_bus *mdio,
  42. struct device_node *child, u32 addr)
  43. {
  44. struct phy_device *phy;
  45. bool is_c45;
  46. int rc;
  47. u32 phy_id;
  48. is_c45 = of_device_is_compatible(child,
  49. "ethernet-phy-ieee802.3-c45");
  50. if (!is_c45 && !of_get_phy_id(child, &phy_id))
  51. phy = phy_device_create(mdio, addr, phy_id, 0, NULL);
  52. else
  53. phy = get_phy_device(mdio, addr, is_c45);
  54. if (IS_ERR(phy))
  55. return;
  56. rc = irq_of_parse_and_map(child, 0);
  57. if (rc > 0) {
  58. phy->irq = rc;
  59. mdio->irq[addr] = rc;
  60. } else {
  61. phy->irq = mdio->irq[addr];
  62. }
  63. if (of_property_read_bool(child, "broken-turn-around"))
  64. mdio->phy_ignore_ta_mask |= 1 << addr;
  65. /* Associate the OF node with the device structure so it
  66. * can be looked up later */
  67. of_node_get(child);
  68. phy->mdio.dev.of_node = child;
  69. /* All data is now stored in the phy struct;
  70. * register it */
  71. rc = phy_device_register(phy);
  72. if (rc) {
  73. phy_device_free(phy);
  74. of_node_put(child);
  75. return;
  76. }
  77. dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
  78. child->name, addr);
  79. }
  80. static void of_mdiobus_register_device(struct mii_bus *mdio,
  81. struct device_node *child, u32 addr)
  82. {
  83. struct mdio_device *mdiodev;
  84. int rc;
  85. mdiodev = mdio_device_create(mdio, addr);
  86. if (IS_ERR(mdiodev))
  87. return;
  88. /* Associate the OF node with the device structure so it
  89. * can be looked up later.
  90. */
  91. of_node_get(child);
  92. mdiodev->dev.of_node = child;
  93. /* All data is now stored in the mdiodev struct; register it. */
  94. rc = mdio_device_register(mdiodev);
  95. if (rc) {
  96. mdio_device_free(mdiodev);
  97. of_node_put(child);
  98. return;
  99. }
  100. dev_dbg(&mdio->dev, "registered mdio device %s at address %i\n",
  101. child->name, addr);
  102. }
  103. /* The following is a list of PHY compatible strings which appear in
  104. * some DTBs. The compatible string is never matched against a PHY
  105. * driver, so is pointless. We only expect devices which are not PHYs
  106. * to have a compatible string, so they can be matched to an MDIO
  107. * driver. Encourage users to upgrade their DT blobs to remove these.
  108. */
  109. static const struct of_device_id whitelist_phys[] = {
  110. { .compatible = "brcm,40nm-ephy" },
  111. { .compatible = "broadcom,bcm5241" },
  112. { .compatible = "marvell,88E1111", },
  113. { .compatible = "marvell,88e1116", },
  114. { .compatible = "marvell,88e1118", },
  115. { .compatible = "marvell,88e1145", },
  116. { .compatible = "marvell,88e1149r", },
  117. { .compatible = "marvell,88e1310", },
  118. { .compatible = "marvell,88E1510", },
  119. { .compatible = "marvell,88E1514", },
  120. { .compatible = "moxa,moxart-rtl8201cp", },
  121. {}
  122. };
  123. /*
  124. * Return true if the child node is for a phy. It must either:
  125. * o Compatible string of "ethernet-phy-idX.X"
  126. * o Compatible string of "ethernet-phy-ieee802.3-c45"
  127. * o Compatible string of "ethernet-phy-ieee802.3-c22"
  128. * o In the white list above (and issue a warning)
  129. * o No compatibility string
  130. *
  131. * A device which is not a phy is expected to have a compatible string
  132. * indicating what sort of device it is.
  133. */
  134. static bool of_mdiobus_child_is_phy(struct device_node *child)
  135. {
  136. u32 phy_id;
  137. if (of_get_phy_id(child, &phy_id) != -EINVAL)
  138. return true;
  139. if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c45"))
  140. return true;
  141. if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c22"))
  142. return true;
  143. if (of_match_node(whitelist_phys, child)) {
  144. pr_warn(FW_WARN
  145. "%pOF: Whitelisted compatible string. Please remove\n",
  146. child);
  147. return true;
  148. }
  149. if (!of_find_property(child, "compatible", NULL))
  150. return true;
  151. return false;
  152. }
  153. /**
  154. * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
  155. * @mdio: pointer to mii_bus structure
  156. * @np: pointer to device_node of MDIO bus.
  157. *
  158. * This function registers the mii_bus structure and registers a phy_device
  159. * for each child node of @np.
  160. */
  161. int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
  162. {
  163. struct device_node *child;
  164. bool scanphys = false;
  165. int addr, rc;
  166. /* Do not continue if the node is disabled */
  167. if (!of_device_is_available(np))
  168. return -ENODEV;
  169. /* Mask out all PHYs from auto probing. Instead the PHYs listed in
  170. * the device tree are populated after the bus has been registered */
  171. mdio->phy_mask = ~0;
  172. mdio->dev.of_node = np;
  173. /* Get bus level PHY reset GPIO details */
  174. mdio->reset_delay_us = DEFAULT_GPIO_RESET_DELAY;
  175. of_property_read_u32(np, "reset-delay-us", &mdio->reset_delay_us);
  176. /* Register the MDIO bus */
  177. rc = mdiobus_register(mdio);
  178. if (rc)
  179. return rc;
  180. /* Loop over the child nodes and register a phy_device for each phy */
  181. for_each_available_child_of_node(np, child) {
  182. addr = of_mdio_parse_addr(&mdio->dev, child);
  183. if (addr < 0) {
  184. scanphys = true;
  185. continue;
  186. }
  187. if (of_mdiobus_child_is_phy(child))
  188. of_mdiobus_register_phy(mdio, child, addr);
  189. else
  190. of_mdiobus_register_device(mdio, child, addr);
  191. }
  192. if (!scanphys)
  193. return 0;
  194. /* auto scan for PHYs with empty reg property */
  195. for_each_available_child_of_node(np, child) {
  196. /* Skip PHYs with reg property set */
  197. if (of_find_property(child, "reg", NULL))
  198. continue;
  199. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  200. /* skip already registered PHYs */
  201. if (mdiobus_is_registered_device(mdio, addr))
  202. continue;
  203. /* be noisy to encourage people to set reg property */
  204. dev_info(&mdio->dev, "scan phy %s at address %i\n",
  205. child->name, addr);
  206. if (of_mdiobus_child_is_phy(child))
  207. of_mdiobus_register_phy(mdio, child, addr);
  208. }
  209. }
  210. return 0;
  211. }
  212. EXPORT_SYMBOL(of_mdiobus_register);
  213. /* Helper function for of_phy_find_device */
  214. static int of_phy_match(struct device *dev, void *phy_np)
  215. {
  216. return dev->of_node == phy_np;
  217. }
  218. /**
  219. * of_phy_find_device - Give a PHY node, find the phy_device
  220. * @phy_np: Pointer to the phy's device tree node
  221. *
  222. * If successful, returns a pointer to the phy_device with the embedded
  223. * struct device refcount incremented by one, or NULL on failure.
  224. */
  225. struct phy_device *of_phy_find_device(struct device_node *phy_np)
  226. {
  227. struct device *d;
  228. struct mdio_device *mdiodev;
  229. if (!phy_np)
  230. return NULL;
  231. d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
  232. if (d) {
  233. mdiodev = to_mdio_device(d);
  234. if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
  235. return to_phy_device(d);
  236. put_device(d);
  237. }
  238. return NULL;
  239. }
  240. EXPORT_SYMBOL(of_phy_find_device);
  241. /**
  242. * of_phy_connect - Connect to the phy described in the device tree
  243. * @dev: pointer to net_device claiming the phy
  244. * @phy_np: Pointer to device tree node for the PHY
  245. * @hndlr: Link state callback for the network device
  246. * @flags: flags to pass to the PHY
  247. * @iface: PHY data interface type
  248. *
  249. * If successful, returns a pointer to the phy_device with the embedded
  250. * struct device refcount incremented by one, or NULL on failure. The
  251. * refcount must be dropped by calling phy_disconnect() or phy_detach().
  252. */
  253. struct phy_device *of_phy_connect(struct net_device *dev,
  254. struct device_node *phy_np,
  255. void (*hndlr)(struct net_device *), u32 flags,
  256. phy_interface_t iface)
  257. {
  258. struct phy_device *phy = of_phy_find_device(phy_np);
  259. int ret;
  260. if (!phy)
  261. return NULL;
  262. phy->dev_flags = flags;
  263. ret = phy_connect_direct(dev, phy, hndlr, iface);
  264. /* refcount is held by phy_connect_direct() on success */
  265. put_device(&phy->mdio.dev);
  266. return ret ? NULL : phy;
  267. }
  268. EXPORT_SYMBOL(of_phy_connect);
  269. /**
  270. * of_phy_get_and_connect
  271. * - Get phy node and connect to the phy described in the device tree
  272. * @dev: pointer to net_device claiming the phy
  273. * @np: Pointer to device tree node for the net_device claiming the phy
  274. * @hndlr: Link state callback for the network device
  275. *
  276. * If successful, returns a pointer to the phy_device with the embedded
  277. * struct device refcount incremented by one, or NULL on failure. The
  278. * refcount must be dropped by calling phy_disconnect() or phy_detach().
  279. */
  280. struct phy_device *of_phy_get_and_connect(struct net_device *dev,
  281. struct device_node *np,
  282. void (*hndlr)(struct net_device *))
  283. {
  284. phy_interface_t iface;
  285. struct device_node *phy_np;
  286. struct phy_device *phy;
  287. iface = of_get_phy_mode(np);
  288. if (iface < 0)
  289. return NULL;
  290. phy_np = of_parse_phandle(np, "phy-handle", 0);
  291. if (!phy_np)
  292. return NULL;
  293. phy = of_phy_connect(dev, phy_np, hndlr, 0, iface);
  294. of_node_put(phy_np);
  295. return phy;
  296. }
  297. EXPORT_SYMBOL(of_phy_get_and_connect);
  298. /**
  299. * of_phy_attach - Attach to a PHY without starting the state machine
  300. * @dev: pointer to net_device claiming the phy
  301. * @phy_np: Node pointer for the PHY
  302. * @flags: flags to pass to the PHY
  303. * @iface: PHY data interface type
  304. *
  305. * If successful, returns a pointer to the phy_device with the embedded
  306. * struct device refcount incremented by one, or NULL on failure. The
  307. * refcount must be dropped by calling phy_disconnect() or phy_detach().
  308. */
  309. struct phy_device *of_phy_attach(struct net_device *dev,
  310. struct device_node *phy_np, u32 flags,
  311. phy_interface_t iface)
  312. {
  313. struct phy_device *phy = of_phy_find_device(phy_np);
  314. int ret;
  315. if (!phy)
  316. return NULL;
  317. ret = phy_attach_direct(dev, phy, flags, iface);
  318. /* refcount is held by phy_attach_direct() on success */
  319. put_device(&phy->mdio.dev);
  320. return ret ? NULL : phy;
  321. }
  322. EXPORT_SYMBOL(of_phy_attach);
  323. /*
  324. * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
  325. * support two DT bindings:
  326. * - the old DT binding, where 'fixed-link' was a property with 5
  327. * cells encoding various informations about the fixed PHY
  328. * - the new DT binding, where 'fixed-link' is a sub-node of the
  329. * Ethernet device.
  330. */
  331. bool of_phy_is_fixed_link(struct device_node *np)
  332. {
  333. struct device_node *dn;
  334. int len, err;
  335. const char *managed;
  336. /* New binding */
  337. dn = of_get_child_by_name(np, "fixed-link");
  338. if (dn) {
  339. of_node_put(dn);
  340. return true;
  341. }
  342. err = of_property_read_string(np, "managed", &managed);
  343. if (err == 0 && strcmp(managed, "auto") != 0)
  344. return true;
  345. /* Old binding */
  346. if (of_get_property(np, "fixed-link", &len) &&
  347. len == (5 * sizeof(__be32)))
  348. return true;
  349. return false;
  350. }
  351. EXPORT_SYMBOL(of_phy_is_fixed_link);
  352. int of_phy_register_fixed_link(struct device_node *np)
  353. {
  354. struct fixed_phy_status status = {};
  355. struct device_node *fixed_link_node;
  356. u32 fixed_link_prop[5];
  357. const char *managed;
  358. int link_gpio = -1;
  359. if (of_property_read_string(np, "managed", &managed) == 0 &&
  360. strcmp(managed, "in-band-status") == 0) {
  361. /* status is zeroed, namely its .link member */
  362. goto register_phy;
  363. }
  364. /* New binding */
  365. fixed_link_node = of_get_child_by_name(np, "fixed-link");
  366. if (fixed_link_node) {
  367. status.link = 1;
  368. status.duplex = of_property_read_bool(fixed_link_node,
  369. "full-duplex");
  370. if (of_property_read_u32(fixed_link_node, "speed",
  371. &status.speed)) {
  372. of_node_put(fixed_link_node);
  373. return -EINVAL;
  374. }
  375. status.pause = of_property_read_bool(fixed_link_node, "pause");
  376. status.asym_pause = of_property_read_bool(fixed_link_node,
  377. "asym-pause");
  378. link_gpio = of_get_named_gpio_flags(fixed_link_node,
  379. "link-gpios", 0, NULL);
  380. of_node_put(fixed_link_node);
  381. if (link_gpio == -EPROBE_DEFER)
  382. return -EPROBE_DEFER;
  383. goto register_phy;
  384. }
  385. /* Old binding */
  386. if (of_property_read_u32_array(np, "fixed-link", fixed_link_prop,
  387. ARRAY_SIZE(fixed_link_prop)) == 0) {
  388. status.link = 1;
  389. status.duplex = fixed_link_prop[1];
  390. status.speed = fixed_link_prop[2];
  391. status.pause = fixed_link_prop[3];
  392. status.asym_pause = fixed_link_prop[4];
  393. goto register_phy;
  394. }
  395. return -ENODEV;
  396. register_phy:
  397. return PTR_ERR_OR_ZERO(fixed_phy_register(PHY_POLL, &status, link_gpio,
  398. np));
  399. }
  400. EXPORT_SYMBOL(of_phy_register_fixed_link);
  401. void of_phy_deregister_fixed_link(struct device_node *np)
  402. {
  403. struct phy_device *phydev;
  404. phydev = of_phy_find_device(np);
  405. if (!phydev)
  406. return;
  407. fixed_phy_unregister(phydev);
  408. put_device(&phydev->mdio.dev); /* of_phy_find_device() */
  409. phy_device_free(phydev); /* fixed_phy_register() */
  410. }
  411. EXPORT_SYMBOL(of_phy_deregister_fixed_link);