of_mdio.c 13 KB

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