Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2019 |
| 4 | * Alex Marginean, NXP |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 11 | #include <miiphy.h> |
| 12 | #include <dm/device-internal.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Bin Meng | e0518f0 | 2021-03-14 20:14:47 +0800 | [diff] [blame] | 14 | #include <dm/of_extra.h> |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 15 | #include <dm/uclass-internal.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <linux/compat.h> |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 17 | |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 18 | /* DT node properties for MAC-PHY interface */ |
| 19 | #define PHY_MODE_STR_CNT 2 |
| 20 | static const char *phy_mode_str[PHY_MODE_STR_CNT] = { "phy-mode", |
| 21 | "phy-connection-type" }; |
| 22 | /* DT node properties that reference a PHY node */ |
| 23 | #define PHY_HANDLE_STR_CNT 3 |
| 24 | const char *phy_handle_str[PHY_HANDLE_STR_CNT] = { "phy-handle", |
| 25 | "phy", |
| 26 | "phy-device" }; |
| 27 | |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 28 | void dm_mdio_probe_devices(void) |
| 29 | { |
| 30 | struct udevice *it; |
| 31 | struct uclass *uc; |
| 32 | |
| 33 | uclass_get(UCLASS_MDIO, &uc); |
| 34 | uclass_foreach_dev(it, uc) { |
| 35 | device_probe(it); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | static int dm_mdio_post_bind(struct udevice *dev) |
| 40 | { |
Alex Marginean | 2e859ad | 2019-07-25 12:33:17 +0300 | [diff] [blame] | 41 | const char *dt_name; |
| 42 | |
| 43 | /* set a custom name for the MDIO device, if present in DT */ |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 44 | if (dev_has_ofnode(dev)) { |
| 45 | dt_name = dev_read_string(dev, "device-name"); |
Alex Marginean | 2e859ad | 2019-07-25 12:33:17 +0300 | [diff] [blame] | 46 | if (dt_name) { |
| 47 | debug("renaming dev %s to %s\n", dev->name, dt_name); |
| 48 | device_set_name(dev, dt_name); |
| 49 | } |
| 50 | } |
| 51 | |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 52 | /* |
| 53 | * MDIO command doesn't like spaces in names, don't allow them to keep |
| 54 | * it happy |
| 55 | */ |
| 56 | if (strchr(dev->name, ' ')) { |
| 57 | debug("\nError: MDIO device name \"%s\" has a space!\n", |
| 58 | dev->name); |
| 59 | return -EINVAL; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Following read/write/reset functions are registered with legacy MII code. |
| 67 | * These are called for PHY operations by upper layers and we further call the |
| 68 | * DM MDIO driver functions. |
| 69 | */ |
| 70 | static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg) |
| 71 | { |
| 72 | struct udevice *dev = mii_bus->priv; |
| 73 | |
| 74 | return mdio_get_ops(dev)->read(dev, addr, devad, reg); |
| 75 | } |
| 76 | |
| 77 | static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg, |
| 78 | u16 val) |
| 79 | { |
| 80 | struct udevice *dev = mii_bus->priv; |
| 81 | |
| 82 | return mdio_get_ops(dev)->write(dev, addr, devad, reg, val); |
| 83 | } |
| 84 | |
| 85 | static int mdio_reset(struct mii_dev *mii_bus) |
| 86 | { |
| 87 | struct udevice *dev = mii_bus->priv; |
| 88 | |
| 89 | if (mdio_get_ops(dev)->reset) |
| 90 | return mdio_get_ops(dev)->reset(dev); |
| 91 | else |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | static int dm_mdio_post_probe(struct udevice *dev) |
| 96 | { |
| 97 | struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev); |
| 98 | |
| 99 | pdata->mii_bus = mdio_alloc(); |
| 100 | pdata->mii_bus->read = mdio_read; |
| 101 | pdata->mii_bus->write = mdio_write; |
| 102 | pdata->mii_bus->reset = mdio_reset; |
| 103 | pdata->mii_bus->priv = dev; |
Joe Hershberger | efb9429 | 2019-07-30 14:49:56 -0500 | [diff] [blame] | 104 | strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1); |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 105 | |
| 106 | return mdio_register(pdata->mii_bus); |
| 107 | } |
| 108 | |
| 109 | static int dm_mdio_pre_remove(struct udevice *dev) |
| 110 | { |
| 111 | struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev); |
| 112 | struct mdio_ops *ops = mdio_get_ops(dev); |
| 113 | |
| 114 | if (ops->reset) |
| 115 | ops->reset(dev); |
| 116 | mdio_unregister(pdata->mii_bus); |
| 117 | mdio_free(pdata->mii_bus); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Alex Marginean | 6b3089e | 2019-11-25 17:15:11 +0200 | [diff] [blame] | 122 | struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr, |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 123 | struct udevice *ethdev, |
| 124 | phy_interface_t interface) |
| 125 | { |
Alex Marginean | 6b3089e | 2019-11-25 17:15:11 +0200 | [diff] [blame] | 126 | struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev); |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 127 | |
Alex Marginean | 6b3089e | 2019-11-25 17:15:11 +0200 | [diff] [blame] | 128 | if (device_probe(mdiodev)) |
| 129 | return NULL; |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 130 | |
Alex Marginean | 6b3089e | 2019-11-25 17:15:11 +0200 | [diff] [blame] | 131 | return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface); |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 132 | } |
| 133 | |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 134 | static struct phy_device *dm_eth_connect_phy_handle(struct udevice *ethdev, |
| 135 | phy_interface_t interface) |
| 136 | { |
| 137 | u32 phy_addr; |
| 138 | struct udevice *mdiodev; |
| 139 | struct phy_device *phy; |
| 140 | struct ofnode_phandle_args phandle = {.node = ofnode_null()}; |
Vladimir Oltean | 6ca194a | 2021-03-14 20:14:48 +0800 | [diff] [blame] | 141 | ofnode phynode; |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 142 | int i; |
| 143 | |
Rasmus Villemoes | e505164 | 2020-10-05 15:15:16 +0200 | [diff] [blame] | 144 | if (CONFIG_IS_ENABLED(PHY_FIXED) && |
Vladimir Oltean | 6ca194a | 2021-03-14 20:14:48 +0800 | [diff] [blame] | 145 | ofnode_phy_is_fixed_link(dev_ofnode(ethdev), &phynode)) { |
| 146 | phy = phy_connect(NULL, 0, ethdev, interface); |
| 147 | phandle.node = phynode; |
Rasmus Villemoes | e505164 | 2020-10-05 15:15:16 +0200 | [diff] [blame] | 148 | goto out; |
| 149 | } |
| 150 | |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 151 | for (i = 0; i < PHY_HANDLE_STR_CNT; i++) |
| 152 | if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL, |
| 153 | 0, 0, &phandle)) |
| 154 | break; |
| 155 | |
| 156 | if (!ofnode_valid(phandle.node)) { |
Sean Anderson | 836cb5e | 2020-09-15 10:44:53 -0400 | [diff] [blame] | 157 | dev_dbg(ethdev, "can't find PHY node\n"); |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * reading 'reg' directly should be fine. This is a PHY node, the |
| 163 | * address is always size 1 and requires no translation |
| 164 | */ |
| 165 | if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) { |
| 166 | dev_dbg(ethdev, "missing reg property in phy node\n"); |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | if (uclass_get_device_by_ofnode(UCLASS_MDIO, |
| 171 | ofnode_get_parent(phandle.node), |
| 172 | &mdiodev)) { |
Sean Anderson | 836cb5e | 2020-09-15 10:44:53 -0400 | [diff] [blame] | 173 | dev_dbg(ethdev, "can't find MDIO bus for node %s\n", |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 174 | ofnode_get_name(ofnode_get_parent(phandle.node))); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface); |
| 179 | |
Rasmus Villemoes | e505164 | 2020-10-05 15:15:16 +0200 | [diff] [blame] | 180 | out: |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 181 | if (phy) |
| 182 | phy->node = phandle.node; |
| 183 | |
| 184 | return phy; |
| 185 | } |
| 186 | |
| 187 | /* Connect to a PHY linked in eth DT node */ |
| 188 | struct phy_device *dm_eth_phy_connect(struct udevice *ethdev) |
| 189 | { |
| 190 | const char *if_str; |
| 191 | phy_interface_t interface; |
| 192 | struct phy_device *phy; |
| 193 | int i; |
| 194 | |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 195 | if (!dev_has_ofnode(ethdev)) { |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 196 | debug("%s: supplied eth dev has no DT node!\n", ethdev->name); |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | interface = PHY_INTERFACE_MODE_NONE; |
| 201 | for (i = 0; i < PHY_MODE_STR_CNT; i++) { |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 202 | if_str = dev_read_string(ethdev, phy_mode_str[i]); |
Alex Marginean | cf64055 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 203 | if (if_str) { |
| 204 | interface = phy_get_interface_by_name(if_str); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | if (interface < 0) |
| 209 | interface = PHY_INTERFACE_MODE_NONE; |
| 210 | if (interface == PHY_INTERFACE_MODE_NONE) |
| 211 | dev_dbg(ethdev, "can't find interface mode, default to NONE\n"); |
| 212 | |
| 213 | phy = dm_eth_connect_phy_handle(ethdev, interface); |
| 214 | |
| 215 | if (!phy) |
| 216 | return NULL; |
| 217 | |
| 218 | phy->interface = interface; |
| 219 | |
| 220 | return phy; |
| 221 | } |
| 222 | |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 223 | UCLASS_DRIVER(mdio) = { |
| 224 | .id = UCLASS_MDIO, |
| 225 | .name = "mdio", |
| 226 | .post_bind = dm_mdio_post_bind, |
| 227 | .post_probe = dm_mdio_post_probe, |
| 228 | .pre_remove = dm_mdio_pre_remove, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 229 | .per_device_auto = sizeof(struct mdio_perdev_priv), |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 230 | }; |