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