blob: 8864f99bb32fa73dc7b74b18087de239934b9f57 [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
8#include <common.h>
9#include <dm/device_compat.h>
10#include <phy.h>
11#include <linux/delay.h>
12#include <asm/gpio.h>
13
14struct phy_device *phy_connect_phy_id(struct mii_dev *bus, struct udevice *dev,
Tom Rini6a25a7e2022-04-15 08:09:52 -040015 int phyaddr)
Michal Simek488eec52022-02-23 15:45:42 +010016{
17 struct phy_device *phydev;
18 struct ofnode_phandle_args phandle_args;
19 struct gpio_desc gpio;
20 ofnode node;
21 u32 id, assert, deassert;
22 u16 vendor, device;
23 int ret;
24
25 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
26 &phandle_args))
27 return NULL;
28
29 if (!ofnode_valid(phandle_args.node))
30 return NULL;
31
32 node = phandle_args.node;
33
34 ret = ofnode_read_eth_phy_id(node, &vendor, &device);
35 if (ret) {
T Karthik Reddy13f3fe32022-03-30 11:07:54 +020036 debug("Failed to read eth PHY id, err: %d\n", ret);
Michal Simek488eec52022-02-23 15:45:42 +010037 return NULL;
38 }
39
T Karthik Reddy13f3fe32022-03-30 11:07:54 +020040 if (!IS_ENABLED(CONFIG_DM_ETH_PHY)) {
41 ret = gpio_request_by_name_nodev(node, "reset-gpios", 0, &gpio,
42 GPIOD_ACTIVE_LOW);
43 if (!ret) {
44 assert = ofnode_read_u32_default(node,
45 "reset-assert-us", 0);
46 deassert = ofnode_read_u32_default(node,
47 "reset-deassert-us",
48 0);
49 ret = dm_gpio_set_value(&gpio, 1);
50 if (ret) {
51 dev_err(dev,
52 "Failed assert gpio, err: %d\n", ret);
53 return NULL;
54 }
Michal Simek488eec52022-02-23 15:45:42 +010055
T Karthik Reddy13f3fe32022-03-30 11:07:54 +020056 udelay(assert);
Michal Simek488eec52022-02-23 15:45:42 +010057
T Karthik Reddy13f3fe32022-03-30 11:07:54 +020058 ret = dm_gpio_set_value(&gpio, 0);
59 if (ret) {
60 dev_err(dev,
61 "Failed deassert gpio, err: %d\n",
62 ret);
63 return NULL;
64 }
Michal Simek488eec52022-02-23 15:45:42 +010065
T Karthik Reddy13f3fe32022-03-30 11:07:54 +020066 udelay(deassert);
67 }
Michal Simek488eec52022-02-23 15:45:42 +010068 }
69
70 id = vendor << 16 | device;
Tom Rini6a25a7e2022-04-15 08:09:52 -040071 phydev = phy_device_create(bus, phyaddr, id, false);
Michal Simek488eec52022-02-23 15:45:42 +010072 if (phydev)
73 phydev->node = node;
74
75 return phydev;
76}