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> |
Marek Vasut | 0179285 | 2023-05-31 00:51:26 +0200 | [diff] [blame] | 10 | #include <dm/device-internal.h> |
| 11 | #include <dm/lists.h> |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 12 | #include <phy.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <asm/gpio.h> |
| 15 | |
| 16 | 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] | 17 | int phyaddr) |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 18 | { |
| 19 | struct phy_device *phydev; |
| 20 | struct ofnode_phandle_args phandle_args; |
| 21 | struct gpio_desc gpio; |
Marek Vasut | 0179285 | 2023-05-31 00:51:26 +0200 | [diff] [blame] | 22 | const char *node_name; |
| 23 | struct udevice *pdev; |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 24 | 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 Reddy | 13f3fe3 | 2022-03-30 11:07:54 +0200 | [diff] [blame] | 40 | debug("Failed to read eth PHY id, err: %d\n", ret); |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 41 | return NULL; |
| 42 | } |
| 43 | |
T Karthik Reddy | 13f3fe3 | 2022-03-30 11:07:54 +0200 | [diff] [blame] | 44 | if (!IS_ENABLED(CONFIG_DM_ETH_PHY)) { |
| 45 | ret = gpio_request_by_name_nodev(node, "reset-gpios", 0, &gpio, |
Stefan Herbrechtsmeier | b99df75 | 2023-05-02 14:50:43 +0200 | [diff] [blame] | 46 | GPIOD_IS_OUT | GPIOD_ACTIVE_LOW); |
T Karthik Reddy | 13f3fe3 | 2022-03-30 11:07:54 +0200 | [diff] [blame] | 47 | 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 | } |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 59 | |
T Karthik Reddy | 13f3fe3 | 2022-03-30 11:07:54 +0200 | [diff] [blame] | 60 | udelay(assert); |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 61 | |
T Karthik Reddy | 13f3fe3 | 2022-03-30 11:07:54 +0200 | [diff] [blame] | 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 | } |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 69 | |
T Karthik Reddy | 13f3fe3 | 2022-03-30 11:07:54 +0200 | [diff] [blame] | 70 | udelay(deassert); |
| 71 | } |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | id = vendor << 16 | device; |
Tom Rini | 6a25a7e | 2022-04-15 08:09:52 -0400 | [diff] [blame] | 75 | phydev = phy_device_create(bus, phyaddr, id, false); |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 76 | if (phydev) |
| 77 | phydev->node = node; |
| 78 | |
Marek Vasut | 0179285 | 2023-05-31 00:51:26 +0200 | [diff] [blame] | 79 | 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 Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 92 | return phydev; |
| 93 | } |