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