Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * fwnode helpers for the MDIO (Ethernet PHY) API
0004  *
0005  * This file provides helper functions for extracting PHY device information
0006  * out of the fwnode and using it to populate an mii_bus.
0007  */
0008 
0009 #include <linux/acpi.h>
0010 #include <linux/fwnode_mdio.h>
0011 #include <linux/of.h>
0012 #include <linux/phy.h>
0013 
0014 MODULE_AUTHOR("Calvin Johnson <calvin.johnson@oss.nxp.com>");
0015 MODULE_LICENSE("GPL");
0016 
0017 static struct mii_timestamper *
0018 fwnode_find_mii_timestamper(struct fwnode_handle *fwnode)
0019 {
0020     struct of_phandle_args arg;
0021     int err;
0022 
0023     if (is_acpi_node(fwnode))
0024         return NULL;
0025 
0026     err = of_parse_phandle_with_fixed_args(to_of_node(fwnode),
0027                            "timestamper", 1, 0, &arg);
0028     if (err == -ENOENT)
0029         return NULL;
0030     else if (err)
0031         return ERR_PTR(err);
0032 
0033     if (arg.args_count != 1)
0034         return ERR_PTR(-EINVAL);
0035 
0036     return register_mii_timestamper(arg.np, arg.args[0]);
0037 }
0038 
0039 int fwnode_mdiobus_phy_device_register(struct mii_bus *mdio,
0040                        struct phy_device *phy,
0041                        struct fwnode_handle *child, u32 addr)
0042 {
0043     int rc;
0044 
0045     rc = fwnode_irq_get(child, 0);
0046     /* Don't wait forever if the IRQ provider doesn't become available,
0047      * just fall back to poll mode
0048      */
0049     if (rc == -EPROBE_DEFER)
0050         rc = driver_deferred_probe_check_state(&phy->mdio.dev);
0051     if (rc == -EPROBE_DEFER)
0052         return rc;
0053 
0054     if (rc > 0) {
0055         phy->irq = rc;
0056         mdio->irq[addr] = rc;
0057     } else {
0058         phy->irq = mdio->irq[addr];
0059     }
0060 
0061     if (fwnode_property_read_bool(child, "broken-turn-around"))
0062         mdio->phy_ignore_ta_mask |= 1 << addr;
0063 
0064     fwnode_property_read_u32(child, "reset-assert-us",
0065                  &phy->mdio.reset_assert_delay);
0066     fwnode_property_read_u32(child, "reset-deassert-us",
0067                  &phy->mdio.reset_deassert_delay);
0068 
0069     /* Associate the fwnode with the device structure so it
0070      * can be looked up later
0071      */
0072     fwnode_handle_get(child);
0073     device_set_node(&phy->mdio.dev, child);
0074 
0075     /* All data is now stored in the phy struct;
0076      * register it
0077      */
0078     rc = phy_device_register(phy);
0079     if (rc) {
0080         fwnode_handle_put(child);
0081         return rc;
0082     }
0083 
0084     dev_dbg(&mdio->dev, "registered phy %p fwnode at address %i\n",
0085         child, addr);
0086     return 0;
0087 }
0088 EXPORT_SYMBOL(fwnode_mdiobus_phy_device_register);
0089 
0090 int fwnode_mdiobus_register_phy(struct mii_bus *bus,
0091                 struct fwnode_handle *child, u32 addr)
0092 {
0093     struct mii_timestamper *mii_ts = NULL;
0094     struct phy_device *phy;
0095     bool is_c45 = false;
0096     u32 phy_id;
0097     int rc;
0098 
0099     mii_ts = fwnode_find_mii_timestamper(child);
0100     if (IS_ERR(mii_ts))
0101         return PTR_ERR(mii_ts);
0102 
0103     rc = fwnode_property_match_string(child, "compatible",
0104                       "ethernet-phy-ieee802.3-c45");
0105     if (rc >= 0)
0106         is_c45 = true;
0107 
0108     if (is_c45 || fwnode_get_phy_id(child, &phy_id))
0109         phy = get_phy_device(bus, addr, is_c45);
0110     else
0111         phy = phy_device_create(bus, addr, phy_id, 0, NULL);
0112     if (IS_ERR(phy)) {
0113         unregister_mii_timestamper(mii_ts);
0114         return PTR_ERR(phy);
0115     }
0116 
0117     if (is_acpi_node(child)) {
0118         phy->irq = bus->irq[addr];
0119 
0120         /* Associate the fwnode with the device structure so it
0121          * can be looked up later.
0122          */
0123         phy->mdio.dev.fwnode = child;
0124 
0125         /* All data is now stored in the phy struct, so register it */
0126         rc = phy_device_register(phy);
0127         if (rc) {
0128             phy_device_free(phy);
0129             fwnode_handle_put(phy->mdio.dev.fwnode);
0130             return rc;
0131         }
0132     } else if (is_of_node(child)) {
0133         rc = fwnode_mdiobus_phy_device_register(bus, phy, child, addr);
0134         if (rc) {
0135             unregister_mii_timestamper(mii_ts);
0136             phy_device_free(phy);
0137             return rc;
0138         }
0139     }
0140 
0141     /* phy->mii_ts may already be defined by the PHY driver. A
0142      * mii_timestamper probed via the device tree will still have
0143      * precedence.
0144      */
0145     if (mii_ts)
0146         phy->mii_ts = mii_ts;
0147     return 0;
0148 }
0149 EXPORT_SYMBOL(fwnode_mdiobus_register_phy);