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> |
| 9 | #include <miiphy.h> |
| 10 | #include <dm/device-internal.h> |
| 11 | #include <dm/uclass-internal.h> |
| 12 | |
| 13 | void dm_mdio_probe_devices(void) |
| 14 | { |
| 15 | struct udevice *it; |
| 16 | struct uclass *uc; |
| 17 | |
| 18 | uclass_get(UCLASS_MDIO, &uc); |
| 19 | uclass_foreach_dev(it, uc) { |
| 20 | device_probe(it); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | static int dm_mdio_post_bind(struct udevice *dev) |
| 25 | { |
Alex Marginean | 2e859ad | 2019-07-25 12:33:17 +0300 | [diff] [blame] | 26 | const char *dt_name; |
| 27 | |
| 28 | /* set a custom name for the MDIO device, if present in DT */ |
| 29 | if (ofnode_valid(dev->node)) { |
| 30 | dt_name = ofnode_read_string(dev->node, "device-name"); |
| 31 | if (dt_name) { |
| 32 | debug("renaming dev %s to %s\n", dev->name, dt_name); |
| 33 | device_set_name(dev, dt_name); |
| 34 | } |
| 35 | } |
| 36 | |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 37 | /* |
| 38 | * MDIO command doesn't like spaces in names, don't allow them to keep |
| 39 | * it happy |
| 40 | */ |
| 41 | if (strchr(dev->name, ' ')) { |
| 42 | debug("\nError: MDIO device name \"%s\" has a space!\n", |
| 43 | dev->name); |
| 44 | return -EINVAL; |
| 45 | } |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Following read/write/reset functions are registered with legacy MII code. |
| 52 | * These are called for PHY operations by upper layers and we further call the |
| 53 | * DM MDIO driver functions. |
| 54 | */ |
| 55 | static int mdio_read(struct mii_dev *mii_bus, int addr, int devad, int reg) |
| 56 | { |
| 57 | struct udevice *dev = mii_bus->priv; |
| 58 | |
| 59 | return mdio_get_ops(dev)->read(dev, addr, devad, reg); |
| 60 | } |
| 61 | |
| 62 | static int mdio_write(struct mii_dev *mii_bus, int addr, int devad, int reg, |
| 63 | u16 val) |
| 64 | { |
| 65 | struct udevice *dev = mii_bus->priv; |
| 66 | |
| 67 | return mdio_get_ops(dev)->write(dev, addr, devad, reg, val); |
| 68 | } |
| 69 | |
| 70 | static int mdio_reset(struct mii_dev *mii_bus) |
| 71 | { |
| 72 | struct udevice *dev = mii_bus->priv; |
| 73 | |
| 74 | if (mdio_get_ops(dev)->reset) |
| 75 | return mdio_get_ops(dev)->reset(dev); |
| 76 | else |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int dm_mdio_post_probe(struct udevice *dev) |
| 81 | { |
| 82 | struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev); |
| 83 | |
| 84 | pdata->mii_bus = mdio_alloc(); |
| 85 | pdata->mii_bus->read = mdio_read; |
| 86 | pdata->mii_bus->write = mdio_write; |
| 87 | pdata->mii_bus->reset = mdio_reset; |
| 88 | pdata->mii_bus->priv = dev; |
Joe Hershberger | efb9429 | 2019-07-30 14:49:56 -0500 | [diff] [blame] | 89 | strncpy(pdata->mii_bus->name, dev->name, MDIO_NAME_LEN - 1); |
Alex Marginean | 1a5b098 | 2019-06-03 19:10:30 +0300 | [diff] [blame] | 90 | |
| 91 | return mdio_register(pdata->mii_bus); |
| 92 | } |
| 93 | |
| 94 | static int dm_mdio_pre_remove(struct udevice *dev) |
| 95 | { |
| 96 | struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev); |
| 97 | struct mdio_ops *ops = mdio_get_ops(dev); |
| 98 | |
| 99 | if (ops->reset) |
| 100 | ops->reset(dev); |
| 101 | mdio_unregister(pdata->mii_bus); |
| 102 | mdio_free(pdata->mii_bus); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | struct phy_device *dm_mdio_phy_connect(struct udevice *dev, int addr, |
| 108 | struct udevice *ethdev, |
| 109 | phy_interface_t interface) |
| 110 | { |
| 111 | struct mdio_perdev_priv *pdata = dev_get_uclass_priv(dev); |
| 112 | |
| 113 | if (device_probe(dev)) |
| 114 | return 0; |
| 115 | |
| 116 | return phy_connect(pdata->mii_bus, addr, ethdev, interface); |
| 117 | } |
| 118 | |
| 119 | UCLASS_DRIVER(mdio) = { |
| 120 | .id = UCLASS_MDIO, |
| 121 | .name = "mdio", |
| 122 | .post_bind = dm_mdio_post_bind, |
| 123 | .post_probe = dm_mdio_post_probe, |
| 124 | .pre_remove = dm_mdio_pre_remove, |
| 125 | .per_device_auto_alloc_size = sizeof(struct mdio_perdev_priv), |
| 126 | }; |