Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 1 | // 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 | |
| 14 | struct phy_device *phy_connect_phy_id(struct mii_dev *bus, struct udevice *dev, |
Tom Rini | 6a25a7e | 2022-04-15 08:09:52 -0400 | [diff] [blame] | 15 | int phyaddr) |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 16 | { |
| 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 Reddy | 13f3fe3 | 2022-03-30 11:07:54 +0200 | [diff] [blame] | 36 | debug("Failed to read eth PHY id, err: %d\n", ret); |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 37 | return NULL; |
| 38 | } |
| 39 | |
T Karthik Reddy | 13f3fe3 | 2022-03-30 11:07:54 +0200 | [diff] [blame] | 40 | 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 Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 55 | |
T Karthik Reddy | 13f3fe3 | 2022-03-30 11:07:54 +0200 | [diff] [blame] | 56 | udelay(assert); |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 57 | |
T Karthik Reddy | 13f3fe3 | 2022-03-30 11:07:54 +0200 | [diff] [blame] | 58 | 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 Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 65 | |
T Karthik Reddy | 13f3fe3 | 2022-03-30 11:07:54 +0200 | [diff] [blame] | 66 | udelay(deassert); |
| 67 | } |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | id = vendor << 16 | device; |
Tom Rini | 6a25a7e | 2022-04-15 08:09:52 -0400 | [diff] [blame] | 71 | phydev = phy_device_create(bus, phyaddr, id, false); |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 72 | if (phydev) |
| 73 | phydev->node = node; |
| 74 | |
| 75 | return phydev; |
| 76 | } |