of_mdio.c 12 KB

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