blob: 2f8454ca27da24538e6d1de4c74e82678562635a [file] [log] [blame]
Michal Simek488eec52022-02-23 15:45:42 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Xilinx ethernet phy reset driver
4 *
5 * Copyright (C) 2022 Xilinx, Inc.
6 */
7
Michal Simek488eec52022-02-23 15:45:42 +01008#include <dm/device_compat.h>
Marek Vasut01792852023-05-31 00:51:26 +02009#include <dm/device-internal.h>
10#include <dm/lists.h>
Michal Simek488eec52022-02-23 15:45:42 +010011#include <phy.h>
12#include <linux/delay.h>
13#include <asm/gpio.h>
14
15struct phy_device *phy_connect_phy_id(struct mii_dev *bus, struct udevice *dev,
Tom Rini6a25a7e2022-04-15 08:09:52 -040016 int phyaddr)
Michal Simek488eec52022-02-23 15:45:42 +010017{
18 struct phy_device *phydev;
19 struct ofnode_phandle_args phandle_args;
Marek Vasut01792852023-05-31 00:51:26 +020020 const char *node_name;
21 struct udevice *pdev;
Michal Simek488eec52022-02-23 15:45:42 +010022 u16 vendor, device;
Marek Vasutcf54eeb2024-03-18 15:57:01 +010023 ofnode node;
24 u32 id;
Michal Simek488eec52022-02-23 15:45:42 +010025 int ret;
26
27 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
28 &phandle_args))
29 return NULL;
30
31 if (!ofnode_valid(phandle_args.node))
32 return NULL;
33
34 node = phandle_args.node;
35
36 ret = ofnode_read_eth_phy_id(node, &vendor, &device);
37 if (ret) {
T Karthik Reddy13f3fe32022-03-30 11:07:54 +020038 debug("Failed to read eth PHY id, err: %d\n", ret);
Michal Simek488eec52022-02-23 15:45:42 +010039 return NULL;
40 }
41
Marek Vasutcf54eeb2024-03-18 15:57:01 +010042 ret = phy_gpio_reset(dev);
43 if (ret)
44 return NULL;
Michal Simek488eec52022-02-23 15:45:42 +010045
Marek Vasut51a4b6f2024-01-28 02:19:40 +010046 if (phyaddr == -1)
47 phyaddr = ofnode_read_u32_default(phandle_args.node, "reg", -1);
48
Michal Simek488eec52022-02-23 15:45:42 +010049 id = vendor << 16 | device;
Tom Rini6a25a7e2022-04-15 08:09:52 -040050 phydev = phy_device_create(bus, phyaddr, id, false);
Michal Simek488eec52022-02-23 15:45:42 +010051 if (phydev)
52 phydev->node = node;
53
Marek Vasut01792852023-05-31 00:51:26 +020054 if (IS_ENABLED(CONFIG_DM_ETH_PHY) && ofnode_valid(node)) {
55 node_name = ofnode_get_name(node);
56 ret = device_bind_driver_to_node(dev, "eth_phy_generic_drv",
57 node_name, node,
58 &pdev);
59 if (ret)
60 return NULL;
61
62 ret = device_probe(pdev);
63 if (ret)
64 return NULL;
65 }
66
Michal Simek488eec52022-02-23 15:45:42 +010067 return phydev;
68}