of_mdio.c 14 KB

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