Ye Li | 5fe419e | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2020 NXP |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <net.h> |
Patrick Delaunay | d33982d | 2021-07-20 20:09:51 +0200 | [diff] [blame^] | 9 | #include <asm-generic/gpio.h> |
Ye Li | 5fe419e | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 10 | #include <dm/device-internal.h> |
| 11 | #include <dm/uclass-internal.h> |
| 12 | #include <dm/lists.h> |
Patrick Delaunay | d33982d | 2021-07-20 20:09:51 +0200 | [diff] [blame^] | 13 | #include <linux/delay.h> |
Ye Li | 5fe419e | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 14 | |
| 15 | struct eth_phy_device_priv { |
| 16 | struct mii_dev *mdio_bus; |
Patrick Delaunay | d33982d | 2021-07-20 20:09:51 +0200 | [diff] [blame^] | 17 | struct gpio_desc reset_gpio; |
| 18 | u32 reset_assert_delay; |
| 19 | u32 reset_deassert_delay; |
Ye Li | 5fe419e | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | int eth_phy_binds_nodes(struct udevice *eth_dev) |
| 23 | { |
| 24 | ofnode mdio_node, phy_node; |
| 25 | const char *node_name; |
| 26 | int ret; |
| 27 | |
| 28 | mdio_node = dev_read_subnode(eth_dev, "mdio"); |
| 29 | if (!ofnode_valid(mdio_node)) { |
| 30 | debug("%s: %s mdio subnode not found!", __func__, |
| 31 | eth_dev->name); |
| 32 | return -ENXIO; |
| 33 | } |
| 34 | |
| 35 | ofnode_for_each_subnode(phy_node, mdio_node) { |
| 36 | node_name = ofnode_get_name(phy_node); |
| 37 | |
| 38 | debug("* Found child node: '%s'\n", node_name); |
| 39 | |
| 40 | ret = device_bind_driver_to_node(eth_dev, |
| 41 | "eth_phy_generic_drv", |
| 42 | node_name, phy_node, NULL); |
| 43 | if (ret) { |
| 44 | debug(" - Eth phy binding error: %d\n", ret); |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | debug(" - bound phy device: '%s'\n", node_name); |
| 49 | } |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | int eth_phy_set_mdio_bus(struct udevice *eth_dev, struct mii_dev *mdio_bus) |
| 55 | { |
| 56 | struct udevice *dev; |
| 57 | struct eth_phy_device_priv *uc_priv; |
| 58 | |
| 59 | for (uclass_first_device(UCLASS_ETH_PHY, &dev); dev; |
| 60 | uclass_next_device(&dev)) { |
| 61 | if (dev->parent == eth_dev) { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 62 | uc_priv = (struct eth_phy_device_priv *)(dev_get_uclass_priv(dev)); |
Ye Li | 5fe419e | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 63 | |
| 64 | if (!uc_priv->mdio_bus) |
| 65 | uc_priv->mdio_bus = mdio_bus; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | struct mii_dev *eth_phy_get_mdio_bus(struct udevice *eth_dev) |
| 73 | { |
| 74 | int ret; |
| 75 | struct udevice *phy_dev; |
| 76 | struct eth_phy_device_priv *uc_priv; |
| 77 | |
| 78 | /* Will probe the parent of phy device, then phy device */ |
| 79 | ret = uclass_get_device_by_phandle(UCLASS_ETH_PHY, eth_dev, |
| 80 | "phy-handle", &phy_dev); |
| 81 | if (!ret) { |
| 82 | if (eth_dev != phy_dev->parent) { |
| 83 | /* |
| 84 | * phy_dev is shared and controlled by |
| 85 | * other eth controller |
| 86 | */ |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 87 | uc_priv = (struct eth_phy_device_priv *)(dev_get_uclass_priv(phy_dev)); |
Ye Li | 5fe419e | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 88 | if (uc_priv->mdio_bus) |
| 89 | printf("Get shared mii bus on %s\n", eth_dev->name); |
| 90 | else |
| 91 | printf("Can't get shared mii bus on %s\n", eth_dev->name); |
| 92 | |
| 93 | return uc_priv->mdio_bus; |
| 94 | } |
| 95 | } else { |
| 96 | printf("FEC: can't find phy-handle\n"); |
| 97 | } |
| 98 | |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | int eth_phy_get_addr(struct udevice *dev) |
| 103 | { |
| 104 | struct ofnode_phandle_args phandle_args; |
| 105 | int reg; |
| 106 | |
| 107 | if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0, |
| 108 | &phandle_args)) { |
| 109 | debug("Failed to find phy-handle"); |
| 110 | return -ENODEV; |
| 111 | } |
| 112 | |
| 113 | reg = ofnode_read_u32_default(phandle_args.node, "reg", 0); |
| 114 | |
| 115 | return reg; |
| 116 | } |
| 117 | |
Patrick Delaunay | d33982d | 2021-07-20 20:09:51 +0200 | [diff] [blame^] | 118 | /* parsing generic properties of devicetree/bindings/net/ethernet-phy.yaml */ |
| 119 | static int eth_phy_of_to_plat(struct udevice *dev) |
| 120 | { |
| 121 | struct eth_phy_device_priv *uc_priv = dev_get_uclass_priv(dev); |
| 122 | int ret; |
| 123 | |
| 124 | if (!CONFIG_IS_ENABLED(DM_GPIO)) |
| 125 | return 0; |
| 126 | |
| 127 | /* search "reset-gpios" in phy node */ |
| 128 | ret = gpio_request_by_name(dev, "reset-gpios", 0, |
| 129 | &uc_priv->reset_gpio, |
| 130 | GPIOD_IS_OUT); |
| 131 | if (ret != -ENOENT) |
| 132 | return ret; |
| 133 | |
| 134 | uc_priv->reset_assert_delay = dev_read_u32_default(dev, "reset-assert-us", 0); |
| 135 | uc_priv->reset_deassert_delay = dev_read_u32_default(dev, "reset-deassert-us", 0); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | void eth_phy_reset(struct udevice *dev, int value) |
| 141 | { |
| 142 | struct eth_phy_device_priv *uc_priv = dev_get_uclass_priv(dev); |
| 143 | u32 delay; |
| 144 | |
| 145 | if (!CONFIG_IS_ENABLED(DM_GPIO)) |
| 146 | return; |
| 147 | |
| 148 | if (!dm_gpio_is_valid(&uc_priv->reset_gpio)) |
| 149 | return; |
| 150 | |
| 151 | dm_gpio_set_value(&uc_priv->reset_gpio, value); |
| 152 | |
| 153 | delay = value ? uc_priv->reset_assert_delay : uc_priv->reset_deassert_delay; |
| 154 | if (delay) |
| 155 | udelay(delay); |
| 156 | } |
| 157 | |
| 158 | static int eth_phy_pre_probe(struct udevice *dev) |
| 159 | { |
| 160 | /* Assert and deassert the reset signal */ |
| 161 | eth_phy_reset(dev, 1); |
| 162 | eth_phy_reset(dev, 0); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Ye Li | 5fe419e | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 167 | UCLASS_DRIVER(eth_phy_generic) = { |
| 168 | .id = UCLASS_ETH_PHY, |
| 169 | .name = "eth_phy_generic", |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 170 | .per_device_auto = sizeof(struct eth_phy_device_priv), |
Patrick Delaunay | d33982d | 2021-07-20 20:09:51 +0200 | [diff] [blame^] | 171 | .pre_probe = eth_phy_pre_probe, |
Ye Li | 5fe419e | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | U_BOOT_DRIVER(eth_phy_generic_drv) = { |
| 175 | .name = "eth_phy_generic_drv", |
| 176 | .id = UCLASS_ETH_PHY, |
Patrick Delaunay | d33982d | 2021-07-20 20:09:51 +0200 | [diff] [blame^] | 177 | .of_to_plat = eth_phy_of_to_plat, |
Ye Li | 5fe419e | 2020-05-03 22:41:14 +0800 | [diff] [blame] | 178 | }; |