^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * OF helpers for the MDIO (Ethernet PHY) API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2009 Secret Lab Technologies, Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This file provides helper functions for extracting PHY device information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * out of the OpenFirmware device tree and using it to populate an mii_bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/phy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/phy_fixed.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/of_irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/of_mdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/of_net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define DEFAULT_GPIO_RESET_DELAY 10 /* in microseconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* Extract the clause 22 phy ID from the compatible string of the form
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * ethernet-phy-idAAAA.BBBB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static int of_get_phy_id(struct device_node *device, u32 *phy_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) const char *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned int upper, lower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) of_property_for_each_string(device, "compatible", prop, cp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) *phy_id = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static struct mii_timestamper *of_find_mii_timestamper(struct device_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct of_phandle_args arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) err = of_parse_phandle_with_fixed_args(node, "timestamper", 1, 0, &arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) else if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (arg.args_count != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return register_mii_timestamper(arg.np, arg.args[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int of_mdiobus_phy_device_register(struct mii_bus *mdio, struct phy_device *phy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct device_node *child, u32 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) rc = of_irq_get(child, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (rc == -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (rc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) phy->irq = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) mdio->irq[addr] = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) phy->irq = mdio->irq[addr];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (of_property_read_bool(child, "broken-turn-around"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) mdio->phy_ignore_ta_mask |= 1 << addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) of_property_read_u32(child, "reset-assert-us",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) &phy->mdio.reset_assert_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) of_property_read_u32(child, "reset-deassert-us",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) &phy->mdio.reset_deassert_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /* Associate the OF node with the device structure so it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * can be looked up later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) of_node_get(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) phy->mdio.dev.of_node = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) phy->mdio.dev.fwnode = of_fwnode_handle(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* All data is now stored in the phy struct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * register it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) rc = phy_device_register(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) dev_dbg(&mdio->dev, "registered phy %pOFn at address %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) child, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) EXPORT_SYMBOL(of_mdiobus_phy_device_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int of_mdiobus_register_phy(struct mii_bus *mdio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct device_node *child, u32 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct mii_timestamper *mii_ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct phy_device *phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) bool is_c45;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) u32 phy_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) mii_ts = of_find_mii_timestamper(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (IS_ERR(mii_ts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return PTR_ERR(mii_ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) is_c45 = of_device_is_compatible(child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) "ethernet-phy-ieee802.3-c45");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (!is_c45 && !of_get_phy_id(child, &phy_id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) phy = phy_device_create(mdio, addr, phy_id, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) phy = get_phy_device(mdio, addr, is_c45);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (IS_ERR(phy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (mii_ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) unregister_mii_timestamper(mii_ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return PTR_ERR(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) rc = of_mdiobus_phy_device_register(mdio, phy, child, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (mii_ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) unregister_mii_timestamper(mii_ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) phy_device_free(phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* phy->mii_ts may already be defined by the PHY driver. A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * mii_timestamper probed via the device tree will still have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * precedence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (mii_ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) phy->mii_ts = mii_ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static int of_mdiobus_register_device(struct mii_bus *mdio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct device_node *child, u32 addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct mdio_device *mdiodev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) mdiodev = mdio_device_create(mdio, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (IS_ERR(mdiodev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return PTR_ERR(mdiodev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* Associate the OF node with the device structure so it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * can be looked up later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) of_node_get(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) mdiodev->dev.of_node = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) mdiodev->dev.fwnode = of_fwnode_handle(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* All data is now stored in the mdiodev struct; register it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) rc = mdio_device_register(mdiodev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) mdio_device_free(mdiodev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) dev_dbg(&mdio->dev, "registered mdio device %pOFn at address %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) child, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* The following is a list of PHY compatible strings which appear in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * some DTBs. The compatible string is never matched against a PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * driver, so is pointless. We only expect devices which are not PHYs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * to have a compatible string, so they can be matched to an MDIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * driver. Encourage users to upgrade their DT blobs to remove these.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static const struct of_device_id whitelist_phys[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) { .compatible = "brcm,40nm-ephy" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) { .compatible = "broadcom,bcm5241" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) { .compatible = "marvell,88E1111", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) { .compatible = "marvell,88e1116", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) { .compatible = "marvell,88e1118", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) { .compatible = "marvell,88e1145", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) { .compatible = "marvell,88e1149r", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) { .compatible = "marvell,88e1310", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) { .compatible = "marvell,88E1510", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) { .compatible = "marvell,88E1514", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) { .compatible = "moxa,moxart-rtl8201cp", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * Return true if the child node is for a phy. It must either:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * o Compatible string of "ethernet-phy-idX.X"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * o Compatible string of "ethernet-phy-ieee802.3-c45"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * o Compatible string of "ethernet-phy-ieee802.3-c22"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * o In the white list above (and issue a warning)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * o No compatibility string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * A device which is not a phy is expected to have a compatible string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * indicating what sort of device it is.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) bool of_mdiobus_child_is_phy(struct device_node *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) u32 phy_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (of_get_phy_id(child, &phy_id) != -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c45"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (of_device_is_compatible(child, "ethernet-phy-ieee802.3-c22"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (of_match_node(whitelist_phys, child)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) pr_warn(FW_WARN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) "%pOF: Whitelisted compatible string. Please remove\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (!of_find_property(child, "compatible", NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) EXPORT_SYMBOL(of_mdiobus_child_is_phy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * @mdio: pointer to mii_bus structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * @np: pointer to device_node of MDIO bus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * This function registers the mii_bus structure and registers a phy_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * for each child node of @np.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) bool scanphys = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) int addr, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (!np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return mdiobus_register(mdio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* Do not continue if the node is disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (!of_device_is_available(np))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /* Mask out all PHYs from auto probing. Instead the PHYs listed in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * the device tree are populated after the bus has been registered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) mdio->phy_mask = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) mdio->dev.of_node = np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) mdio->dev.fwnode = of_fwnode_handle(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /* Get bus level PHY reset GPIO details */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) mdio->reset_delay_us = DEFAULT_GPIO_RESET_DELAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) of_property_read_u32(np, "reset-delay-us", &mdio->reset_delay_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) mdio->reset_post_delay_us = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) of_property_read_u32(np, "reset-post-delay-us", &mdio->reset_post_delay_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /* Register the MDIO bus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) rc = mdiobus_register(mdio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) /* Loop over the child nodes and register a phy_device for each phy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) for_each_available_child_of_node(np, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) addr = of_mdio_parse_addr(&mdio->dev, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (addr < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) scanphys = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (of_mdiobus_child_is_phy(child))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) rc = of_mdiobus_register_phy(mdio, child, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) rc = of_mdiobus_register_device(mdio, child, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (rc == -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) dev_err(&mdio->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) "MDIO device at address %d is missing.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) else if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) goto unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (!scanphys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /* auto scan for PHYs with empty reg property */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) for_each_available_child_of_node(np, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) /* Skip PHYs with reg property set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (of_find_property(child, "reg", NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /* skip already registered PHYs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (mdiobus_is_registered_device(mdio, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* be noisy to encourage people to set reg property */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) dev_info(&mdio->dev, "scan phy %pOFn at address %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) child, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (of_mdiobus_child_is_phy(child)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /* -ENODEV is the return code that PHYLIB has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * standardized on to indicate that bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * scanning should continue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) rc = of_mdiobus_register_phy(mdio, child, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (rc != -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) goto unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) mdiobus_unregister(mdio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) EXPORT_SYMBOL(of_mdiobus_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * of_mdio_find_device - Given a device tree node, find the mdio_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * @np: pointer to the mdio_device's device tree node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * If successful, returns a pointer to the mdio_device with the embedded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * struct device refcount incremented by one, or NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * The caller should call put_device() on the mdio_device after its use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct mdio_device *of_mdio_find_device(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct device *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (!np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) d = bus_find_device_by_of_node(&mdio_bus_type, np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (!d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return to_mdio_device(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) EXPORT_SYMBOL(of_mdio_find_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * of_phy_find_device - Give a PHY node, find the phy_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * @phy_np: Pointer to the phy's device tree node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * If successful, returns a pointer to the phy_device with the embedded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * struct device refcount incremented by one, or NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct phy_device *of_phy_find_device(struct device_node *phy_np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct mdio_device *mdiodev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) mdiodev = of_mdio_find_device(phy_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (!mdiodev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return to_phy_device(&mdiodev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) put_device(&mdiodev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) EXPORT_SYMBOL(of_phy_find_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * of_phy_connect - Connect to the phy described in the device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * @dev: pointer to net_device claiming the phy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * @phy_np: Pointer to device tree node for the PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * @hndlr: Link state callback for the network device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * @flags: flags to pass to the PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * @iface: PHY data interface type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * If successful, returns a pointer to the phy_device with the embedded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * struct device refcount incremented by one, or NULL on failure. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * refcount must be dropped by calling phy_disconnect() or phy_detach().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct phy_device *of_phy_connect(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) struct device_node *phy_np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) void (*hndlr)(struct net_device *), u32 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) phy_interface_t iface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) struct phy_device *phy = of_phy_find_device(phy_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (!phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) phy->dev_flags |= flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ret = phy_connect_direct(dev, phy, hndlr, iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) /* refcount is held by phy_connect_direct() on success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) put_device(&phy->mdio.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return ret ? NULL : phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) EXPORT_SYMBOL(of_phy_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * of_phy_get_and_connect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * - Get phy node and connect to the phy described in the device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * @dev: pointer to net_device claiming the phy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * @np: Pointer to device tree node for the net_device claiming the phy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * @hndlr: Link state callback for the network device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * If successful, returns a pointer to the phy_device with the embedded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * struct device refcount incremented by one, or NULL on failure. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * refcount must be dropped by calling phy_disconnect() or phy_detach().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) struct phy_device *of_phy_get_and_connect(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) void (*hndlr)(struct net_device *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) phy_interface_t iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) struct device_node *phy_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) struct phy_device *phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) ret = of_get_phy_mode(np, &iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (of_phy_is_fixed_link(np)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) ret = of_phy_register_fixed_link(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) netdev_err(dev, "broken fixed-link specification\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) phy_np = of_node_get(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) phy_np = of_parse_phandle(np, "phy-handle", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) if (!phy_np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) phy = of_phy_connect(dev, phy_np, hndlr, 0, iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) of_node_put(phy_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) EXPORT_SYMBOL(of_phy_get_and_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * of_phy_attach - Attach to a PHY without starting the state machine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * @dev: pointer to net_device claiming the phy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * @phy_np: Node pointer for the PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * @flags: flags to pass to the PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * @iface: PHY data interface type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * If successful, returns a pointer to the phy_device with the embedded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * struct device refcount incremented by one, or NULL on failure. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * refcount must be dropped by calling phy_disconnect() or phy_detach().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) struct phy_device *of_phy_attach(struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct device_node *phy_np, u32 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) phy_interface_t iface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) struct phy_device *phy = of_phy_find_device(phy_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (!phy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) ret = phy_attach_direct(dev, phy, flags, iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) /* refcount is held by phy_attach_direct() on success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) put_device(&phy->mdio.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) return ret ? NULL : phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) EXPORT_SYMBOL(of_phy_attach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) * support two DT bindings:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) * - the old DT binding, where 'fixed-link' was a property with 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) * cells encoding various informations about the fixed PHY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) * - the new DT binding, where 'fixed-link' is a sub-node of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * Ethernet device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) bool of_phy_is_fixed_link(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) struct device_node *dn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) int len, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) const char *managed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) /* New binding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) dn = of_get_child_by_name(np, "fixed-link");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (dn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) of_node_put(dn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) err = of_property_read_string(np, "managed", &managed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) if (err == 0 && strcmp(managed, "auto") != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) /* Old binding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (of_get_property(np, "fixed-link", &len) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) len == (5 * sizeof(__be32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) EXPORT_SYMBOL(of_phy_is_fixed_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) int of_phy_register_fixed_link(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) struct fixed_phy_status status = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) struct device_node *fixed_link_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) u32 fixed_link_prop[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) const char *managed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (of_property_read_string(np, "managed", &managed) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) strcmp(managed, "in-band-status") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) /* status is zeroed, namely its .link member */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) goto register_phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) /* New binding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) fixed_link_node = of_get_child_by_name(np, "fixed-link");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) if (fixed_link_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) status.link = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) status.duplex = of_property_read_bool(fixed_link_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) "full-duplex");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) if (of_property_read_u32(fixed_link_node, "speed",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) &status.speed)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) of_node_put(fixed_link_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) status.pause = of_property_read_bool(fixed_link_node, "pause");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) status.asym_pause = of_property_read_bool(fixed_link_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) "asym-pause");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) of_node_put(fixed_link_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) goto register_phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /* Old binding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) if (of_property_read_u32_array(np, "fixed-link", fixed_link_prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) ARRAY_SIZE(fixed_link_prop)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) status.link = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) status.duplex = fixed_link_prop[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) status.speed = fixed_link_prop[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) status.pause = fixed_link_prop[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) status.asym_pause = fixed_link_prop[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) goto register_phy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) register_phy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) return PTR_ERR_OR_ZERO(fixed_phy_register(PHY_POLL, &status, np));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) EXPORT_SYMBOL(of_phy_register_fixed_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) void of_phy_deregister_fixed_link(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) struct phy_device *phydev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) phydev = of_phy_find_device(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) if (!phydev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) fixed_phy_unregister(phydev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) put_device(&phydev->mdio.dev); /* of_phy_find_device() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) phy_device_free(phydev); /* fixed_phy_register() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) EXPORT_SYMBOL(of_phy_deregister_fixed_link);