diff mbox series

[1/3] net: phy: Add new read ethernet phy id function

Message ID 16019efb3820a50330935fdaae191cec1f101b5c.1645627539.git.michal.simek@xilinx.com
State Accepted
Commit db681d4929cac3f5d0a8ce638e7e5306fe6038d2
Delegated to: Michal Simek
Headers show
Series net: phy: Add support for ethernet-phy-id | expand

Commit Message

Michal Simek Feb. 23, 2022, 2:45 p.m. UTC
Add new function to get ethernet phy id from compatible property
of the mdio phy node.

Signed-off-by: Michal Simek <michal.simek@xilinx.com>
Signed-off-by: T Karthik Reddy <t.karthik.reddy@xilinx.com>
---

 drivers/core/ofnode.c | 36 ++++++++++++++++++++++++++++++++++++
 include/dm/ofnode.h   | 13 +++++++++++++
 2 files changed, 49 insertions(+)
diff mbox series

Patch

diff --git a/drivers/core/ofnode.c b/drivers/core/ofnode.c
index 709bea272a6e..8042847f3c14 100644
--- a/drivers/core/ofnode.c
+++ b/drivers/core/ofnode.c
@@ -898,6 +898,42 @@  int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device)
 	return -ENOENT;
 }
 
+int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device)
+{
+	const char *list, *end;
+	int len;
+
+	list = ofnode_get_property(node, "compatible", &len);
+
+	if (!list)
+		return -ENOENT;
+
+	end = list + len;
+	while (list < end) {
+		len = strlen(list);
+
+		if (len >= strlen("ethernet-phy-idVVVV,DDDD")) {
+			char *s = strstr(list, "ethernet-phy-id");
+
+			/*
+			 * check if the string is something like
+			 * ethernet-phy-idVVVV,DDDD
+			 */
+			if (s && s[19] == '.') {
+				s += strlen("ethernet-phy-id");
+				*vendor = simple_strtol(s, NULL, 16);
+				s += 5;
+				*device = simple_strtol(s, NULL, 16);
+
+				return 0;
+			}
+		}
+		list += (len + 1);
+	}
+
+	return -ENOENT;
+}
+
 int ofnode_read_addr_cells(ofnode node)
 {
 	if (ofnode_is_np(node)) {
diff --git a/include/dm/ofnode.h b/include/dm/ofnode.h
index 0cb324c8b0c1..744dffe0a2dd 100644
--- a/include/dm/ofnode.h
+++ b/include/dm/ofnode.h
@@ -894,6 +894,19 @@  int ofnode_read_pci_addr(ofnode node, enum fdt_pci_space type,
  */
 int ofnode_read_pci_vendev(ofnode node, u16 *vendor, u16 *device);
 
+/**
+ * ofnode_read_eth_phy_id() - look up eth phy vendor and device id
+ *
+ * Look at the compatible property of a device node that represents a eth phy
+ * device and extract phy vendor id and device id from it.
+ *
+ * @param node		node to examine
+ * @param vendor	vendor id of the eth phy device
+ * @param device	device id of the eth phy device
+ * @return 0 if ok, negative on error
+ */
+int ofnode_read_eth_phy_id(ofnode node, u16 *vendor, u16 *device);
+
 /**
  * ofnode_read_addr_cells() - Get the number of address cells for a node
  *