of_mdio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. int of_mdio_parse_addr(struct device *dev, const struct device_node *np)
  104. {
  105. u32 addr;
  106. int ret;
  107. ret = of_property_read_u32(np, "reg", &addr);
  108. if (ret < 0) {
  109. dev_err(dev, "%s has invalid PHY address\n", np->full_name);
  110. return ret;
  111. }
  112. /* A PHY must have a reg property in the range [0-31] */
  113. if (addr >= PHY_MAX_ADDR) {
  114. dev_err(dev, "%s PHY address %i is too large\n",
  115. np->full_name, addr);
  116. return -EINVAL;
  117. }
  118. return addr;
  119. }
  120. EXPORT_SYMBOL(of_mdio_parse_addr);
  121. /* The following is a list of PHY compatible strings which appear in
  122. * some DTBs. The compatible string is never matched against a PHY
  123. * driver, so is pointless. We only expect devices which are not PHYs
  124. * to have a compatible string, so they can be matched to an MDIO
  125. * driver. Encourage users to upgrade their DT blobs to remove these.
  126. */
  127. static const struct of_device_id whitelist_phys[] = {
  128. { .compatible = "brcm,40nm-ephy" },
  129. { .compatible = "broadcom,bcm5241" },
  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. /* Do not continue if the node is disabled */
  185. if (!of_device_is_available(np))
  186. return -ENODEV;
  187. /* Mask out all PHYs from auto probing. Instead the PHYs listed in
  188. * the device tree are populated after the bus has been registered */
  189. mdio->phy_mask = ~0;
  190. mdio->dev.of_node = np;
  191. /* Get bus level PHY reset GPIO details */
  192. mdio->reset_delay_us = DEFAULT_GPIO_RESET_DELAY;
  193. of_property_read_u32(np, "reset-delay-us", &mdio->reset_delay_us);
  194. mdio->num_reset_gpios = of_gpio_named_count(np, "reset-gpios");
  195. /* Register the MDIO bus */
  196. rc = mdiobus_register(mdio);
  197. if (rc)
  198. return rc;
  199. /* Loop over the child nodes and register a phy_device for each phy */
  200. for_each_available_child_of_node(np, child) {
  201. addr = of_mdio_parse_addr(&mdio->dev, child);
  202. if (addr < 0) {
  203. scanphys = true;
  204. continue;
  205. }
  206. if (of_mdiobus_child_is_phy(child))
  207. of_mdiobus_register_phy(mdio, child, addr);
  208. else
  209. of_mdiobus_register_device(mdio, child, addr);
  210. }
  211. if (!scanphys)
  212. return 0;
  213. /* auto scan for PHYs with empty reg property */
  214. for_each_available_child_of_node(np, child) {
  215. /* Skip PHYs with reg property set */
  216. if (of_find_property(child, "reg", NULL))
  217. continue;
  218. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  219. /* skip already registered PHYs */
  220. if (mdiobus_is_registered_device(mdio, addr))
  221. continue;
  222. /* be noisy to encourage people to set reg property */
  223. dev_info(&mdio->dev, "scan phy %s at address %i\n",
  224. child->name, addr);
  225. if (of_mdiobus_child_is_phy(child))
  226. of_mdiobus_register_phy(mdio, child, addr);
  227. }
  228. }
  229. return 0;
  230. }
  231. EXPORT_SYMBOL(of_mdiobus_register);
  232. /* Helper function for of_phy_find_device */
  233. static int of_phy_match(struct device *dev, void *phy_np)
  234. {
  235. return dev->of_node == phy_np;
  236. }
  237. /**
  238. * of_phy_find_device - Give a PHY node, find the phy_device
  239. * @phy_np: Pointer to the phy's device tree node
  240. *
  241. * If successful, returns a pointer to the phy_device with the embedded
  242. * struct device refcount incremented by one, or NULL on failure.
  243. */
  244. struct phy_device *of_phy_find_device(struct device_node *phy_np)
  245. {
  246. struct device *d;
  247. struct mdio_device *mdiodev;
  248. if (!phy_np)
  249. return NULL;
  250. d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
  251. if (d) {
  252. mdiodev = to_mdio_device(d);
  253. if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
  254. return to_phy_device(d);
  255. put_device(d);
  256. }
  257. return NULL;
  258. }
  259. EXPORT_SYMBOL(of_phy_find_device);
  260. /**
  261. * of_phy_connect - Connect to the phy described in the device tree
  262. * @dev: pointer to net_device claiming the phy
  263. * @phy_np: Pointer to device tree node for the PHY
  264. * @hndlr: Link state callback for the network device
  265. * @flags: flags to pass to the PHY
  266. * @iface: PHY data interface type
  267. *
  268. * If successful, returns a pointer to the phy_device with the embedded
  269. * struct device refcount incremented by one, or NULL on failure. The
  270. * refcount must be dropped by calling phy_disconnect() or phy_detach().
  271. */
  272. struct phy_device *of_phy_connect(struct net_device *dev,
  273. struct device_node *phy_np,
  274. void (*hndlr)(struct net_device *), u32 flags,
  275. phy_interface_t iface)
  276. {
  277. struct phy_device *phy = of_phy_find_device(phy_np);
  278. int ret;
  279. if (!phy)
  280. return NULL;
  281. phy->dev_flags = flags;
  282. ret = phy_connect_direct(dev, phy, hndlr, iface);
  283. /* refcount is held by phy_connect_direct() on success */
  284. put_device(&phy->mdio.dev);
  285. return ret ? NULL : phy;
  286. }
  287. EXPORT_SYMBOL(of_phy_connect);
  288. /**
  289. * of_phy_get_and_connect
  290. * - Get phy node and connect to the phy described in the device tree
  291. * @dev: pointer to net_device claiming the phy
  292. * @np: Pointer to device tree node for the net_device claiming the phy
  293. * @hndlr: Link state callback for the network device
  294. *
  295. * If successful, returns a pointer to the phy_device with the embedded
  296. * struct device refcount incremented by one, or NULL on failure. The
  297. * refcount must be dropped by calling phy_disconnect() or phy_detach().
  298. */
  299. struct phy_device *of_phy_get_and_connect(struct net_device *dev,
  300. struct device_node *np,
  301. void (*hndlr)(struct net_device *))
  302. {
  303. phy_interface_t iface;
  304. struct device_node *phy_np;
  305. struct phy_device *phy;
  306. iface = of_get_phy_mode(np);
  307. if (iface < 0)
  308. return NULL;
  309. phy_np = of_parse_phandle(np, "phy-handle", 0);
  310. if (!phy_np)
  311. return NULL;
  312. phy = of_phy_connect(dev, phy_np, hndlr, 0, iface);
  313. of_node_put(phy_np);
  314. return phy;
  315. }
  316. EXPORT_SYMBOL(of_phy_get_and_connect);
  317. /**
  318. * of_phy_attach - Attach to a PHY without starting the state machine
  319. * @dev: pointer to net_device claiming the phy
  320. * @phy_np: Node pointer for the PHY
  321. * @flags: flags to pass to the PHY
  322. * @iface: PHY data interface type
  323. *
  324. * If successful, returns a pointer to the phy_device with the embedded
  325. * struct device refcount incremented by one, or NULL on failure. The
  326. * refcount must be dropped by calling phy_disconnect() or phy_detach().
  327. */
  328. struct phy_device *of_phy_attach(struct net_device *dev,
  329. struct device_node *phy_np, u32 flags,
  330. phy_interface_t iface)
  331. {
  332. struct phy_device *phy = of_phy_find_device(phy_np);
  333. int ret;
  334. if (!phy)
  335. return NULL;
  336. ret = phy_attach_direct(dev, phy, flags, iface);
  337. /* refcount is held by phy_attach_direct() on success */
  338. put_device(&phy->mdio.dev);
  339. return ret ? NULL : phy;
  340. }
  341. EXPORT_SYMBOL(of_phy_attach);
  342. /*
  343. * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
  344. * support two DT bindings:
  345. * - the old DT binding, where 'fixed-link' was a property with 5
  346. * cells encoding various informations about the fixed PHY
  347. * - the new DT binding, where 'fixed-link' is a sub-node of the
  348. * Ethernet device.
  349. */
  350. bool of_phy_is_fixed_link(struct device_node *np)
  351. {
  352. struct device_node *dn;
  353. int len, err;
  354. const char *managed;
  355. /* New binding */
  356. dn = of_get_child_by_name(np, "fixed-link");
  357. if (dn) {
  358. of_node_put(dn);
  359. return true;
  360. }
  361. err = of_property_read_string(np, "managed", &managed);
  362. if (err == 0 && strcmp(managed, "auto") != 0)
  363. return true;
  364. /* Old binding */
  365. if (of_get_property(np, "fixed-link", &len) &&
  366. len == (5 * sizeof(__be32)))
  367. return true;
  368. return false;
  369. }
  370. EXPORT_SYMBOL(of_phy_is_fixed_link);
  371. int of_phy_register_fixed_link(struct device_node *np)
  372. {
  373. struct fixed_phy_status status = {};
  374. struct device_node *fixed_link_node;
  375. const __be32 *fixed_link_prop;
  376. int link_gpio;
  377. int len, err;
  378. struct phy_device *phy;
  379. const char *managed;
  380. err = of_property_read_string(np, "managed", &managed);
  381. if (err == 0) {
  382. if (strcmp(managed, "in-band-status") == 0) {
  383. /* status is zeroed, namely its .link member */
  384. phy = fixed_phy_register(PHY_POLL, &status, -1, np);
  385. return PTR_ERR_OR_ZERO(phy);
  386. }
  387. }
  388. /* New binding */
  389. fixed_link_node = of_get_child_by_name(np, "fixed-link");
  390. if (fixed_link_node) {
  391. status.link = 1;
  392. status.duplex = of_property_read_bool(fixed_link_node,
  393. "full-duplex");
  394. if (of_property_read_u32(fixed_link_node, "speed",
  395. &status.speed)) {
  396. of_node_put(fixed_link_node);
  397. return -EINVAL;
  398. }
  399. status.pause = of_property_read_bool(fixed_link_node, "pause");
  400. status.asym_pause = of_property_read_bool(fixed_link_node,
  401. "asym-pause");
  402. link_gpio = of_get_named_gpio_flags(fixed_link_node,
  403. "link-gpios", 0, NULL);
  404. of_node_put(fixed_link_node);
  405. if (link_gpio == -EPROBE_DEFER)
  406. return -EPROBE_DEFER;
  407. phy = fixed_phy_register(PHY_POLL, &status, link_gpio, np);
  408. return PTR_ERR_OR_ZERO(phy);
  409. }
  410. /* Old binding */
  411. fixed_link_prop = of_get_property(np, "fixed-link", &len);
  412. if (fixed_link_prop && len == (5 * sizeof(__be32))) {
  413. status.link = 1;
  414. status.duplex = be32_to_cpu(fixed_link_prop[1]);
  415. status.speed = be32_to_cpu(fixed_link_prop[2]);
  416. status.pause = be32_to_cpu(fixed_link_prop[3]);
  417. status.asym_pause = be32_to_cpu(fixed_link_prop[4]);
  418. phy = fixed_phy_register(PHY_POLL, &status, -1, np);
  419. return PTR_ERR_OR_ZERO(phy);
  420. }
  421. return -ENODEV;
  422. }
  423. EXPORT_SYMBOL(of_phy_register_fixed_link);
  424. void of_phy_deregister_fixed_link(struct device_node *np)
  425. {
  426. struct phy_device *phydev;
  427. phydev = of_phy_find_device(np);
  428. if (!phydev)
  429. return;
  430. fixed_phy_unregister(phydev);
  431. put_device(&phydev->mdio.dev); /* of_phy_find_device() */
  432. phy_device_free(phydev); /* fixed_phy_register() */
  433. }
  434. EXPORT_SYMBOL(of_phy_deregister_fixed_link);