Alex Marginean | c3452b5 | 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 | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Alex Marginean | c3452b5 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 11 | #include <miiphy.h> |
| 12 | #include <dm/device-internal.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <dm/device_compat.h> |
Alex Marginean | c3452b5 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 14 | #include <dm/uclass-internal.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <linux/compat.h> |
Alex Marginean | c3452b5 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 16 | |
Alex Marginean | 2f62455 | 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 | c3452b5 | 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 | 6b3abc0 | 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 */ |
| 43 | if (ofnode_valid(dev->node)) { |
| 44 | dt_name = ofnode_read_string(dev->node, "device-name"); |
| 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 | c3452b5 | 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 | 398e751 | 2019-07-30 14:49:56 -0500 | [diff] [blame] | 103 | strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1); |
Alex Marginean | c3452b5 | 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 | a5d32c3 | 2019-11-25 17:15:11 +0200 | [diff] [blame] | 121 | struct phy_device *dm_mdio_phy_connect(struct udevice *mdiodev, int phyaddr, |
Alex Marginean | c3452b5 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 122 | struct udevice *ethdev, |
| 123 | phy_interface_t interface) |
| 124 | { |
Alex Marginean | a5d32c3 | 2019-11-25 17:15:11 +0200 | [diff] [blame] | 125 | struct mdio_perdev_priv *pdata = dev_get_uclass_priv(mdiodev); |
Alex Marginean | c3452b5 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 126 | |
Alex Marginean | a5d32c3 | 2019-11-25 17:15:11 +0200 | [diff] [blame] | 127 | if (device_probe(mdiodev)) |
| 128 | return NULL; |
Alex Marginean | c3452b5 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 129 | |
Alex Marginean | a5d32c3 | 2019-11-25 17:15:11 +0200 | [diff] [blame] | 130 | return phy_connect(pdata->mii_bus, phyaddr, ethdev, interface); |
Alex Marginean | c3452b5 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 131 | } |
| 132 | |
Alex Marginean | 2f62455 | 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 | |
| 142 | for (i = 0; i < PHY_HANDLE_STR_CNT; i++) |
| 143 | if (!dev_read_phandle_with_args(ethdev, phy_handle_str[i], NULL, |
| 144 | 0, 0, &phandle)) |
| 145 | break; |
| 146 | |
| 147 | if (!ofnode_valid(phandle.node)) { |
Sean Anderson | 0851bd1 | 2020-09-15 10:44:53 -0400 | [diff] [blame] | 148 | dev_dbg(ethdev, "can't find PHY node\n"); |
Alex Marginean | 2f62455 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * reading 'reg' directly should be fine. This is a PHY node, the |
| 154 | * address is always size 1 and requires no translation |
| 155 | */ |
| 156 | if (ofnode_read_u32(phandle.node, "reg", &phy_addr)) { |
| 157 | dev_dbg(ethdev, "missing reg property in phy node\n"); |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | if (uclass_get_device_by_ofnode(UCLASS_MDIO, |
| 162 | ofnode_get_parent(phandle.node), |
| 163 | &mdiodev)) { |
Sean Anderson | 0851bd1 | 2020-09-15 10:44:53 -0400 | [diff] [blame] | 164 | dev_dbg(ethdev, "can't find MDIO bus for node %s\n", |
Alex Marginean | 2f62455 | 2019-11-25 17:15:12 +0200 | [diff] [blame] | 165 | ofnode_get_name(ofnode_get_parent(phandle.node))); |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | phy = dm_mdio_phy_connect(mdiodev, phy_addr, ethdev, interface); |
| 170 | |
| 171 | if (phy) |
| 172 | phy->node = phandle.node; |
| 173 | |
| 174 | return phy; |
| 175 | } |
| 176 | |
| 177 | /* Connect to a PHY linked in eth DT node */ |
| 178 | struct phy_device *dm_eth_phy_connect(struct udevice *ethdev) |
| 179 | { |
| 180 | const char *if_str; |
| 181 | phy_interface_t interface; |
| 182 | struct phy_device *phy; |
| 183 | int i; |
| 184 | |
| 185 | if (!ofnode_valid(ethdev->node)) { |
| 186 | debug("%s: supplied eth dev has no DT node!\n", ethdev->name); |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | interface = PHY_INTERFACE_MODE_NONE; |
| 191 | for (i = 0; i < PHY_MODE_STR_CNT; i++) { |
| 192 | if_str = ofnode_read_string(ethdev->node, phy_mode_str[i]); |
| 193 | if (if_str) { |
| 194 | interface = phy_get_interface_by_name(if_str); |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | if (interface < 0) |
| 199 | interface = PHY_INTERFACE_MODE_NONE; |
| 200 | if (interface == PHY_INTERFACE_MODE_NONE) |
| 201 | dev_dbg(ethdev, "can't find interface mode, default to NONE\n"); |
| 202 | |
| 203 | phy = dm_eth_connect_phy_handle(ethdev, interface); |
| 204 | |
| 205 | if (!phy) |
| 206 | return NULL; |
| 207 | |
| 208 | phy->interface = interface; |
| 209 | |
| 210 | return phy; |
| 211 | } |
| 212 | |
Alex Marginean | c3452b5 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 213 | UCLASS_DRIVER(mdio) = { |
| 214 | .id = UCLASS_MDIO, |
| 215 | .name = "mdio", |
| 216 | .post_bind = dm_mdio_post_bind, |
| 217 | .post_probe = dm_mdio_post_probe, |
| 218 | .pre_remove = dm_mdio_pre_remove, |
| 219 | .per_device_auto_alloc_size = sizeof(struct mdio_perdev_priv), |
| 220 | }; |