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 | |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 8 | #include <dm/device_compat.h> |
Marek Vasut | 0179285 | 2023-05-31 00:51:26 +0200 | [diff] [blame] | 9 | #include <dm/device-internal.h> |
| 10 | #include <dm/lists.h> |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 11 | #include <phy.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <asm/gpio.h> |
| 14 | |
| 15 | 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] | 16 | int phyaddr) |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 17 | { |
| 18 | struct phy_device *phydev; |
| 19 | struct ofnode_phandle_args phandle_args; |
Marek Vasut | 0179285 | 2023-05-31 00:51:26 +0200 | [diff] [blame] | 20 | const char *node_name; |
| 21 | struct udevice *pdev; |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 22 | u16 vendor, device; |
Marek Vasut | cf54eeb | 2024-03-18 15:57:01 +0100 | [diff] [blame] | 23 | ofnode node; |
| 24 | u32 id; |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 25 | 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 Reddy | 13f3fe3 | 2022-03-30 11:07:54 +0200 | [diff] [blame] | 38 | debug("Failed to read eth PHY id, err: %d\n", ret); |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 39 | return NULL; |
| 40 | } |
| 41 | |
Marek Vasut | cf54eeb | 2024-03-18 15:57:01 +0100 | [diff] [blame] | 42 | ret = phy_gpio_reset(dev); |
| 43 | if (ret) |
| 44 | return NULL; |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 45 | |
Marek Vasut | 51a4b6f | 2024-01-28 02:19:40 +0100 | [diff] [blame] | 46 | if (phyaddr == -1) |
| 47 | phyaddr = ofnode_read_u32_default(phandle_args.node, "reg", -1); |
| 48 | |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 49 | id = vendor << 16 | device; |
Tom Rini | 6a25a7e | 2022-04-15 08:09:52 -0400 | [diff] [blame] | 50 | phydev = phy_device_create(bus, phyaddr, id, false); |
Michal Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 51 | if (phydev) |
| 52 | phydev->node = node; |
| 53 | |
Marek Vasut | 0179285 | 2023-05-31 00:51:26 +0200 | [diff] [blame] | 54 | 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 Simek | 488eec5 | 2022-02-23 15:45:42 +0100 | [diff] [blame] | 67 | return phydev; |
| 68 | } |