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