blob: 877a51c3d007d51a3562c183fa69ee137bb319c0 [file] [log] [blame]
Michal Simeka744a282022-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
8#include <common.h>
9#include <dm/device_compat.h>
Marek Vasut68a4d152023-05-31 00:51:26 +020010#include <dm/device-internal.h>
11#include <dm/lists.h>
Michal Simeka744a282022-02-23 15:45:42 +010012#include <phy.h>
13#include <linux/delay.h>
14#include <asm/gpio.h>
15
16struct phy_device *phy_connect_phy_id(struct mii_dev *bus, struct udevice *dev,
Tom Rini7f418ea2022-04-15 08:09:52 -040017 int phyaddr)
Michal Simeka744a282022-02-23 15:45:42 +010018{
19 struct phy_device *phydev;
20 struct ofnode_phandle_args phandle_args;
21 struct gpio_desc gpio;
Marek Vasut68a4d152023-05-31 00:51:26 +020022 const char *node_name;
23 struct udevice *pdev;
Michal Simeka744a282022-02-23 15:45:42 +010024 ofnode node;
25 u32 id, assert, deassert;
26 u16 vendor, device;
27 int ret;
28
29 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
30 &phandle_args))
31 return NULL;
32
33 if (!ofnode_valid(phandle_args.node))
34 return NULL;
35
36 node = phandle_args.node;
37
38 ret = ofnode_read_eth_phy_id(node, &vendor, &device);
39 if (ret) {
T Karthik Reddyccc86562022-03-30 11:07:54 +020040 debug("Failed to read eth PHY id, err: %d\n", ret);
Michal Simeka744a282022-02-23 15:45:42 +010041 return NULL;
42 }
43
T Karthik Reddyccc86562022-03-30 11:07:54 +020044 if (!IS_ENABLED(CONFIG_DM_ETH_PHY)) {
45 ret = gpio_request_by_name_nodev(node, "reset-gpios", 0, &gpio,
Stefan Herbrechtsmeier1a3af262023-05-02 14:50:43 +020046 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
T Karthik Reddyccc86562022-03-30 11:07:54 +020047 if (!ret) {
48 assert = ofnode_read_u32_default(node,
49 "reset-assert-us", 0);
50 deassert = ofnode_read_u32_default(node,
51 "reset-deassert-us",
52 0);
53 ret = dm_gpio_set_value(&gpio, 1);
54 if (ret) {
55 dev_err(dev,
56 "Failed assert gpio, err: %d\n", ret);
57 return NULL;
58 }
59
60 udelay(assert);
61
62 ret = dm_gpio_set_value(&gpio, 0);
63 if (ret) {
64 dev_err(dev,
65 "Failed deassert gpio, err: %d\n",
66 ret);
67 return NULL;
68 }
69
70 udelay(deassert);
Michal Simeka744a282022-02-23 15:45:42 +010071 }
Michal Simeka744a282022-02-23 15:45:42 +010072 }
73
74 id = vendor << 16 | device;
Tom Rini7f418ea2022-04-15 08:09:52 -040075 phydev = phy_device_create(bus, phyaddr, id, false);
Michal Simeka744a282022-02-23 15:45:42 +010076 if (phydev)
77 phydev->node = node;
78
Marek Vasut68a4d152023-05-31 00:51:26 +020079 if (IS_ENABLED(CONFIG_DM_ETH_PHY) && ofnode_valid(node)) {
80 node_name = ofnode_get_name(node);
81 ret = device_bind_driver_to_node(dev, "eth_phy_generic_drv",
82 node_name, node,
83 &pdev);
84 if (ret)
85 return NULL;
86
87 ret = device_probe(pdev);
88 if (ret)
89 return NULL;
90 }
91
Michal Simeka744a282022-02-23 15:45:42 +010092 return phydev;
93}