linux/drivers/net/phy/phy_device.c
<<
>>
Prefs
   1/* Framework for finding and configuring PHYs.
   2 * Also contains generic PHY driver
   3 *
   4 * Author: Andy Fleming
   5 *
   6 * Copyright (c) 2004 Freescale Semiconductor, Inc.
   7 *
   8 * This program is free software; you can redistribute  it and/or modify it
   9 * under  the terms of  the GNU General  Public License as published by the
  10 * Free Software Foundation;  either version 2 of the  License, or (at your
  11 * option) any later version.
  12 *
  13 */
  14
  15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16
  17#include <linux/kernel.h>
  18#include <linux/string.h>
  19#include <linux/errno.h>
  20#include <linux/unistd.h>
  21#include <linux/slab.h>
  22#include <linux/interrupt.h>
  23#include <linux/init.h>
  24#include <linux/delay.h>
  25#include <linux/netdevice.h>
  26#include <linux/etherdevice.h>
  27#include <linux/skbuff.h>
  28#include <linux/mm.h>
  29#include <linux/module.h>
  30#include <linux/mii.h>
  31#include <linux/ethtool.h>
  32#include <linux/phy.h>
  33#include <linux/mdio.h>
  34#include <linux/io.h>
  35#include <linux/uaccess.h>
  36#include <linux/of.h>
  37
  38#include <asm/irq.h>
  39
  40MODULE_DESCRIPTION("PHY library");
  41MODULE_AUTHOR("Andy Fleming");
  42MODULE_LICENSE("GPL");
  43
  44void phy_device_free(struct phy_device *phydev)
  45{
  46        put_device(&phydev->mdio_dev);
  47}
  48EXPORT_SYMBOL(phy_device_free);
  49
  50static void phy_device_release(struct device *dev)
  51{
  52        kfree(to_phy_device(dev));
  53}
  54
  55enum genphy_driver {
  56        GENPHY_DRV_1G,
  57        GENPHY_DRV_10G,
  58        GENPHY_DRV_MAX
  59};
  60
  61static struct phy_driver genphy_driver[GENPHY_DRV_MAX];
  62
  63static LIST_HEAD(phy_fixup_list);
  64static DEFINE_MUTEX(phy_fixup_lock);
  65
  66#ifdef CONFIG_PM
  67static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
  68{
  69        struct device_driver *drv = phydev->mdio_dev.driver;
  70        struct phy_driver *phydrv = to_phy_driver(drv);
  71        struct net_device *netdev = phydev->attached_dev;
  72
  73        if (!drv || !phydrv->suspend)
  74                return false;
  75
  76        /* PHY not attached? May suspend if the PHY has not already been
  77         * suspended as part of a prior call to phy_disconnect() ->
  78         * phy_detach() -> phy_suspend() because the parent netdev might be the
  79         * MDIO bus driver and clock gated at this point.
  80         */
  81        if (!netdev)
  82                return !phydev->suspended;
  83
  84        /* Don't suspend PHY if the attached netdev parent may wakeup.
  85         * The parent may point to a PCI device, as in tg3 driver.
  86         */
  87        if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
  88                return false;
  89
  90        /* Also don't suspend PHY if the netdev itself may wakeup. This
  91         * is the case for devices w/o underlaying pwr. mgmt. aware bus,
  92         * e.g. SoC devices.
  93         */
  94        if (device_may_wakeup(&netdev->dev))
  95                return false;
  96
  97        return true;
  98}
  99
 100static int mdio_bus_phy_suspend(struct device *dev)
 101{
 102        struct phy_device *phydev = to_phy_device(dev);
 103
 104        /* We must stop the state machine manually, otherwise it stops out of
 105         * control, possibly with the phydev->lock held. Upon resume, netdev
 106         * may call phy routines that try to grab the same lock, and that may
 107         * lead to a deadlock.
 108         */
 109        if (phydev->attached_dev && phydev->adjust_link)
 110                phy_stop_machine(phydev);
 111
 112        if (!mdio_bus_phy_may_suspend(phydev))
 113                return 0;
 114
 115        return phy_suspend(phydev);
 116}
 117
 118static int mdio_bus_phy_resume(struct device *dev)
 119{
 120        struct phy_device *phydev = to_phy_device(dev);
 121        int ret;
 122
 123        if (!mdio_bus_phy_may_suspend(phydev))
 124                goto no_resume;
 125
 126        ret = phy_resume(phydev);
 127        if (ret < 0)
 128                return ret;
 129
 130no_resume:
 131        if (phydev->attached_dev && phydev->adjust_link)
 132                phy_start_machine(phydev);
 133
 134        return 0;
 135}
 136
 137static int mdio_bus_phy_restore(struct device *dev)
 138{
 139        struct phy_device *phydev = to_phy_device(dev);
 140        struct net_device *netdev = phydev->attached_dev;
 141        int ret;
 142
 143        if (!netdev)
 144                return 0;
 145
 146        ret = phy_init_hw(phydev);
 147        if (ret < 0)
 148                return ret;
 149
 150        if (phydev->attached_dev && phydev->adjust_link)
 151                phy_start_machine(phydev);
 152
 153        return 0;
 154}
 155
 156static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
 157        .suspend = mdio_bus_phy_suspend,
 158        .resume = mdio_bus_phy_resume,
 159        .freeze = mdio_bus_phy_suspend,
 160        .thaw = mdio_bus_phy_resume,
 161        .restore = mdio_bus_phy_restore,
 162};
 163
 164#define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
 165
 166#else
 167
 168#define MDIO_BUS_PHY_PM_OPS NULL
 169
 170#endif /* CONFIG_PM */
 171
 172/**
 173 * phy_register_fixup - creates a new phy_fixup and adds it to the list
 174 * @bus_id: A string which matches phydev->mdio_dev.bus_id (or PHY_ANY_ID)
 175 * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
 176 *      It can also be PHY_ANY_UID
 177 * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
 178 *      comparison
 179 * @run: The actual code to be run when a matching PHY is found
 180 */
 181int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
 182                       int (*run)(struct phy_device *))
 183{
 184        struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
 185
 186        if (!fixup)
 187                return -ENOMEM;
 188
 189        strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
 190        fixup->phy_uid = phy_uid;
 191        fixup->phy_uid_mask = phy_uid_mask;
 192        fixup->run = run;
 193
 194        mutex_lock(&phy_fixup_lock);
 195        list_add_tail(&fixup->list, &phy_fixup_list);
 196        mutex_unlock(&phy_fixup_lock);
 197
 198        return 0;
 199}
 200EXPORT_SYMBOL(phy_register_fixup);
 201
 202/* Registers a fixup to be run on any PHY with the UID in phy_uid */
 203int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
 204                               int (*run)(struct phy_device *))
 205{
 206        return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
 207}
 208EXPORT_SYMBOL(phy_register_fixup_for_uid);
 209
 210/* Registers a fixup to be run on the PHY with id string bus_id */
 211int phy_register_fixup_for_id(const char *bus_id,
 212                              int (*run)(struct phy_device *))
 213{
 214        return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
 215}
 216EXPORT_SYMBOL(phy_register_fixup_for_id);
 217
 218/* Returns 1 if fixup matches phydev in bus_id and phy_uid.
 219 * Fixups can be set to match any in one or more fields.
 220 */
 221static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
 222{
 223        if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
 224                if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
 225                        return 0;
 226
 227        if ((fixup->phy_uid & fixup->phy_uid_mask) !=
 228            (phydev->phy_id & fixup->phy_uid_mask))
 229                if (fixup->phy_uid != PHY_ANY_UID)
 230                        return 0;
 231
 232        return 1;
 233}
 234
 235/* Runs any matching fixups for this phydev */
 236static int phy_scan_fixups(struct phy_device *phydev)
 237{
 238        struct phy_fixup *fixup;
 239
 240        mutex_lock(&phy_fixup_lock);
 241        list_for_each_entry(fixup, &phy_fixup_list, list) {
 242                if (phy_needs_fixup(phydev, fixup)) {
 243                        int err = fixup->run(phydev);
 244
 245                        if (err < 0) {
 246                                mutex_unlock(&phy_fixup_lock);
 247                                return err;
 248                        }
 249                }
 250        }
 251        mutex_unlock(&phy_fixup_lock);
 252
 253        return 0;
 254}
 255
 256struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
 257                                     bool is_c45,
 258                                     struct phy_c45_device_ids *c45_ids)
 259{
 260        struct phy_device *dev;
 261
 262        /* We allocate the device, and initialize the default values */
 263        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 264        if (NULL == dev)
 265                return (struct phy_device *)PTR_ERR((void *)-ENOMEM);
 266
 267        dev->mdio_dev.release = phy_device_release;
 268        dev->mdio_dev.parent = &bus->dev;
 269        dev->mdio_dev.bus = &mdio_bus_type;
 270        dev->mdio_bus = bus;
 271        dev->mdio_pm_ops = MDIO_BUS_PHY_PM_OPS;
 272        dev->mdio_addr = addr;
 273        dev->mdio_flags = MDIO_DEVICE_FLAG_PHY;
 274
 275        dev->speed = 0;
 276        dev->duplex = -1;
 277        dev->pause = 0;
 278        dev->asym_pause = 0;
 279        dev->link = 1;
 280        dev->interface = PHY_INTERFACE_MODE_GMII;
 281
 282        dev->autoneg = AUTONEG_ENABLE;
 283
 284        dev->is_c45 = is_c45;
 285        dev->phy_id = phy_id;
 286        if (c45_ids)
 287                dev->c45_ids = *c45_ids;
 288        dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
 289        dev_set_name(&dev->mdio_dev, PHY_ID_FMT, bus->id, addr);
 290
 291        dev->state = PHY_DOWN;
 292
 293        mutex_init(&dev->lock);
 294        INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
 295        INIT_WORK(&dev->phy_queue, phy_change);
 296
 297        /* Request the appropriate module unconditionally; don't
 298         * bother trying to do so only if it isn't already loaded,
 299         * because that gets complicated. A hotplug event would have
 300         * done an unconditional modprobe anyway.
 301         * We don't do normal hotplug because it won't work for MDIO
 302         * -- because it relies on the device staying around for long
 303         * enough for the driver to get loaded. With MDIO, the NIC
 304         * driver will get bored and give up as soon as it finds that
 305         * there's no driver _already_ loaded.
 306         */
 307        request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
 308
 309        device_initialize(&dev->mdio_dev);
 310
 311        return dev;
 312}
 313EXPORT_SYMBOL(phy_device_create);
 314
 315/**
 316 * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
 317 * @bus: the target MII bus
 318 * @addr: PHY address on the MII bus
 319 * @phy_id: where to store the ID retrieved.
 320 * @c45_ids: where to store the c45 ID information.
 321 *
 322 *   If the PHY devices-in-package appears to be valid, it and the
 323 *   corresponding identifiers are stored in @c45_ids, zero is stored
 324 *   in @phy_id.  Otherwise 0xffffffff is stored in @phy_id.  Returns
 325 *   zero on success.
 326 *
 327 */
 328static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
 329                           struct phy_c45_device_ids *c45_ids) {
 330        int phy_reg;
 331        int i, reg_addr;
 332        const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
 333
 334        /* Find first non-zero Devices In package.  Device
 335         * zero is reserved, so don't probe it.
 336         */
 337        for (i = 1;
 338             i < num_ids && c45_ids->devices_in_package == 0;
 339             i++) {
 340                reg_addr = MII_ADDR_C45 | i << 16 | 6;
 341                phy_reg = mdiobus_read(bus, addr, reg_addr);
 342                if (phy_reg < 0)
 343                        return -EIO;
 344                c45_ids->devices_in_package = (phy_reg & 0xffff) << 16;
 345
 346                reg_addr = MII_ADDR_C45 | i << 16 | 5;
 347                phy_reg = mdiobus_read(bus, addr, reg_addr);
 348                if (phy_reg < 0)
 349                        return -EIO;
 350                c45_ids->devices_in_package |= (phy_reg & 0xffff);
 351
 352                /* If mostly Fs, there is no device there,
 353                 * let's get out of here.
 354                 */
 355                if ((c45_ids->devices_in_package & 0x1fffffff) == 0x1fffffff) {
 356                        *phy_id = 0xffffffff;
 357                        return 0;
 358                }
 359        }
 360
 361        /* Now probe Device Identifiers for each device present. */
 362        for (i = 1; i < num_ids; i++) {
 363                if (!(c45_ids->devices_in_package & (1 << i)))
 364                        continue;
 365
 366                reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
 367                phy_reg = mdiobus_read(bus, addr, reg_addr);
 368                if (phy_reg < 0)
 369                        return -EIO;
 370                c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
 371
 372                reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
 373                phy_reg = mdiobus_read(bus, addr, reg_addr);
 374                if (phy_reg < 0)
 375                        return -EIO;
 376                c45_ids->device_ids[i] |= (phy_reg & 0xffff);
 377        }
 378        *phy_id = 0;
 379        return 0;
 380}
 381
 382/**
 383 * get_phy_id - reads the specified addr for its ID.
 384 * @bus: the target MII bus
 385 * @addr: PHY address on the MII bus
 386 * @phy_id: where to store the ID retrieved.
 387 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
 388 * @c45_ids: where to store the c45 ID information.
 389 *
 390 * Description: In the case of a 802.3-c22 PHY, reads the ID registers
 391 *   of the PHY at @addr on the @bus, stores it in @phy_id and returns
 392 *   zero on success.
 393 *
 394 *   In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
 395 *   its return value is in turn returned.
 396 *
 397 */
 398static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
 399                      bool is_c45, struct phy_c45_device_ids *c45_ids)
 400{
 401        int phy_reg;
 402
 403        if (is_c45)
 404                return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
 405
 406        /* Grab the bits from PHYIR1, and put them in the upper half */
 407        phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
 408        if (phy_reg < 0) {
 409                /* if there is no device, return without an error so scanning
 410                 * the bus works properly
 411                 */
 412                if (phy_reg == -EIO || phy_reg == -ENODEV) {
 413                        *phy_id = 0xffffffff;
 414                        return 0;
 415                }
 416
 417                return -EIO;
 418        }
 419
 420        *phy_id = (phy_reg & 0xffff) << 16;
 421
 422        /* Grab the bits from PHYIR2, and put them in the lower half */
 423        phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
 424        if (phy_reg < 0)
 425                return -EIO;
 426
 427        *phy_id |= (phy_reg & 0xffff);
 428
 429        return 0;
 430}
 431
 432/**
 433 * get_phy_device - reads the specified PHY device and returns its @phy_device
 434 *                  struct
 435 * @bus: the target MII bus
 436 * @addr: PHY address on the MII bus
 437 * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
 438 *
 439 * Description: Reads the ID registers of the PHY at @addr on the
 440 *   @bus, then allocates and returns the phy_device to represent it.
 441 */
 442struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
 443{
 444        struct phy_c45_device_ids c45_ids = {0};
 445        u32 phy_id = 0;
 446        int r;
 447
 448        r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
 449        if (r)
 450                return ERR_PTR(r);
 451
 452        /* If the phy_id is mostly Fs, there is no device there */
 453        if ((phy_id & 0x1fffffff) == 0x1fffffff)
 454                return ERR_PTR(-ENODEV);
 455
 456        return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
 457}
 458EXPORT_SYMBOL(get_phy_device);
 459
 460/**
 461 * phy_device_register - Register the phy device on the MDIO bus
 462 * @phydev: phy_device structure to be added to the MDIO bus
 463 */
 464int phy_device_register(struct phy_device *phydev)
 465{
 466        int err;
 467
 468        err = mdiobus_register_device(phydev);
 469        if (err)
 470                return err;
 471
 472        /* Run all of the fixups for this PHY */
 473        err = phy_scan_fixups(phydev);
 474        if (err) {
 475                pr_err("PHY %d failed to initialize\n", phydev->mdio_addr);
 476                goto out;
 477        }
 478
 479        err = device_add(&phydev->mdio_dev);
 480        if (err) {
 481                pr_err("PHY %d failed to add\n", phydev->mdio_addr);
 482                goto out;
 483        }
 484
 485        return 0;
 486
 487 out:
 488        mdiobus_unregister_device(phydev);
 489        return err;
 490}
 491EXPORT_SYMBOL(phy_device_register);
 492
 493/**
 494 * phy_device_remove - Remove a previously registered phy device from the MDIO bus
 495 * @phydev: phy_device structure to remove
 496 *
 497 * This doesn't free the phy_device itself, it merely reverses the effects
 498 * of phy_device_register(). Use phy_device_free() to free the device
 499 * after calling this function.
 500 */
 501void phy_device_remove(struct phy_device *phydev)
 502{
 503        device_del(&phydev->mdio_dev);
 504        mdiobus_unregister_device(phydev);
 505}
 506EXPORT_SYMBOL(phy_device_remove);
 507
 508/**
 509 * phy_find_first - finds the first PHY device on the bus
 510 * @bus: the target MII bus
 511 */
 512struct phy_device *phy_find_first(struct mii_bus *bus)
 513{
 514        struct phy_device *phydev;
 515        int addr;
 516
 517        for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
 518                phydev = mdiobus_get_phy(bus, addr);
 519                if (phydev)
 520                        return phydev;
 521        }
 522        return NULL;
 523}
 524EXPORT_SYMBOL(phy_find_first);
 525
 526/**
 527 * phy_prepare_link - prepares the PHY layer to monitor link status
 528 * @phydev: target phy_device struct
 529 * @handler: callback function for link status change notifications
 530 *
 531 * Description: Tells the PHY infrastructure to handle the
 532 *   gory details on monitoring link status (whether through
 533 *   polling or an interrupt), and to call back to the
 534 *   connected device driver when the link status changes.
 535 *   If you want to monitor your own link state, don't call
 536 *   this function.
 537 */
 538static void phy_prepare_link(struct phy_device *phydev,
 539                             void (*handler)(struct net_device *))
 540{
 541        phydev->adjust_link = handler;
 542}
 543
 544/**
 545 * phy_connect_direct - connect an ethernet device to a specific phy_device
 546 * @dev: the network device to connect
 547 * @phydev: the pointer to the phy device
 548 * @handler: callback function for state change notifications
 549 * @interface: PHY device's interface
 550 */
 551int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
 552                       void (*handler)(struct net_device *),
 553                       phy_interface_t interface)
 554{
 555        int rc;
 556
 557        rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
 558        if (rc)
 559                return rc;
 560
 561        phy_prepare_link(phydev, handler);
 562        phy_start_machine(phydev);
 563        if (phydev->irq > 0)
 564                phy_start_interrupts(phydev);
 565
 566        return 0;
 567}
 568EXPORT_SYMBOL(phy_connect_direct);
 569
 570/**
 571 * phy_connect - connect an ethernet device to a PHY device
 572 * @dev: the network device to connect
 573 * @bus_id: the id string of the PHY device to connect
 574 * @handler: callback function for state change notifications
 575 * @interface: PHY device's interface
 576 *
 577 * Description: Convenience function for connecting ethernet
 578 *   devices to PHY devices.  The default behavior is for
 579 *   the PHY infrastructure to handle everything, and only notify
 580 *   the connected driver when the link status changes.  If you
 581 *   don't want, or can't use the provided functionality, you may
 582 *   choose to call only the subset of functions which provide
 583 *   the desired functionality.
 584 */
 585struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
 586                               void (*handler)(struct net_device *),
 587                               phy_interface_t interface)
 588{
 589        struct phy_device *phydev;
 590        struct device *d;
 591        int rc;
 592
 593        /* Search the list of PHY devices on the mdio bus for the
 594         * PHY with the requested name
 595         */
 596        d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
 597        if (!d) {
 598                pr_err("PHY %s not found\n", bus_id);
 599                return ERR_PTR(-ENODEV);
 600        }
 601        phydev = to_phy_device(d);
 602
 603        rc = phy_connect_direct(dev, phydev, handler, interface);
 604        if (rc)
 605                return ERR_PTR(rc);
 606
 607        return phydev;
 608}
 609EXPORT_SYMBOL(phy_connect);
 610
 611/**
 612 * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
 613 *                  device
 614 * @phydev: target phy_device struct
 615 */
 616void phy_disconnect(struct phy_device *phydev)
 617{
 618        if (phydev->irq > 0)
 619                phy_stop_interrupts(phydev);
 620
 621        phy_stop_machine(phydev);
 622
 623        phydev->adjust_link = NULL;
 624
 625        phy_detach(phydev);
 626}
 627EXPORT_SYMBOL(phy_disconnect);
 628
 629/**
 630 * phy_poll_reset - Safely wait until a PHY reset has properly completed
 631 * @phydev: The PHY device to poll
 632 *
 633 * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
 634 *   published in 2008, a PHY reset may take up to 0.5 seconds.  The MII BMCR
 635 *   register must be polled until the BMCR_RESET bit clears.
 636 *
 637 *   Furthermore, any attempts to write to PHY registers may have no effect
 638 *   or even generate MDIO bus errors until this is complete.
 639 *
 640 *   Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
 641 *   standard and do not fully reset after the BMCR_RESET bit is set, and may
 642 *   even *REQUIRE* a soft-reset to properly restart autonegotiation.  In an
 643 *   effort to support such broken PHYs, this function is separate from the
 644 *   standard phy_init_hw() which will zero all the other bits in the BMCR
 645 *   and reapply all driver-specific and board-specific fixups.
 646 */
 647static int phy_poll_reset(struct phy_device *phydev)
 648{
 649        /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
 650        unsigned int retries = 12;
 651        int ret;
 652
 653        do {
 654                msleep(50);
 655                ret = phy_read(phydev, MII_BMCR);
 656                if (ret < 0)
 657                        return ret;
 658        } while (ret & BMCR_RESET && --retries);
 659        if (ret & BMCR_RESET)
 660                return -ETIMEDOUT;
 661
 662        /* Some chips (smsc911x) may still need up to another 1ms after the
 663         * BMCR_RESET bit is cleared before they are usable.
 664         */
 665        msleep(1);
 666        return 0;
 667}
 668
 669int phy_init_hw(struct phy_device *phydev)
 670{
 671        int ret;
 672
 673        if (!phydev->drv || !phydev->drv->config_init)
 674                return 0;
 675
 676        ret = genphy_soft_reset(phydev);
 677        if (ret < 0)
 678                return ret;
 679
 680        ret = phy_scan_fixups(phydev);
 681        if (ret < 0)
 682                return ret;
 683
 684        return phydev->drv->config_init(phydev);
 685}
 686EXPORT_SYMBOL(phy_init_hw);
 687
 688void phy_attached_info(struct phy_device *phydev)
 689{
 690        phy_attached_print(phydev, NULL);
 691}
 692EXPORT_SYMBOL(phy_attached_info);
 693
 694#define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)"
 695void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
 696{
 697        if (!fmt) {
 698                dev_info(&phydev->mdio_dev, ATTACHED_FMT "\n",
 699                         phydev->drv->name, phydev_name(phydev),
 700                         phydev->irq);
 701        } else {
 702                va_list ap;
 703
 704                dev_info(&phydev->mdio_dev, ATTACHED_FMT,
 705                         phydev->drv->name, phydev_name(phydev),
 706                         phydev->irq);
 707
 708                va_start(ap, fmt);
 709                vprintk(fmt, ap);
 710                va_end(ap);
 711        }
 712}
 713EXPORT_SYMBOL(phy_attached_print);
 714
 715/**
 716 * phy_attach_direct - attach a network device to a given PHY device pointer
 717 * @dev: network device to attach
 718 * @phydev: Pointer to phy_device to attach
 719 * @flags: PHY device's dev_flags
 720 * @interface: PHY device's interface
 721 *
 722 * Description: Called by drivers to attach to a particular PHY
 723 *     device. The phy_device is found, and properly hooked up
 724 *     to the phy_driver.  If no driver is attached, then a
 725 *     generic driver is used.  The phy_device is given a ptr to
 726 *     the attaching device, and given a callback for link status
 727 *     change.  The phy_device is returned to the attaching driver.
 728 *     This function takes a reference on the phy device.
 729 */
 730int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
 731                      u32 flags, phy_interface_t interface)
 732{
 733        struct module *ndev_owner = dev->dev.parent->driver->owner;
 734        struct mii_bus *bus = phydev->mdio_bus;
 735        struct device *d = &phydev->mdio_dev;
 736        int err;
 737
 738        /* For Ethernet device drivers that register their own MDIO bus, we
 739         * will have bus->owner match ndev_mod, so we do not want to increment
 740         * our own module->refcnt here, otherwise we would not be able to
 741         * unload later on.
 742         */
 743        if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
 744                dev_err(&dev->dev, "failed to get the bus module\n");
 745                return -EIO;
 746        }
 747
 748        get_device(d);
 749
 750        /* Assume that if there is no driver, that it doesn't
 751         * exist, and we should use the genphy driver.
 752         */
 753        if (NULL == d->driver) {
 754                if (phydev->is_c45)
 755                        d->driver = &genphy_driver[GENPHY_DRV_10G].driver;
 756                else
 757                        d->driver = &genphy_driver[GENPHY_DRV_1G].driver;
 758
 759                err = d->driver->probe(d);
 760                if (err >= 0)
 761                        err = device_bind_driver(d);
 762
 763                if (err)
 764                        goto error;
 765        }
 766
 767        if (phydev->attached_dev) {
 768                dev_err(&dev->dev, "PHY already attached\n");
 769                err = -EBUSY;
 770                goto error;
 771        }
 772
 773        phydev->attached_dev = dev;
 774        dev->phydev = phydev;
 775
 776        phydev->dev_flags = flags;
 777
 778        phydev->interface = interface;
 779
 780        phydev->state = PHY_READY;
 781
 782        /* Initial carrier state is off as the phy is about to be
 783         * (re)initialized.
 784         */
 785        netif_carrier_off(phydev->attached_dev);
 786
 787        /* Do initial configuration here, now that
 788         * we have certain key parameters
 789         * (dev_flags and interface)
 790         */
 791        err = phy_init_hw(phydev);
 792        if (err)
 793                phy_detach(phydev);
 794
 795        phy_resume(phydev);
 796
 797        return err;
 798
 799error:
 800        put_device(d);
 801        if (ndev_owner != bus->owner)
 802                module_put(bus->owner);
 803        return err;
 804}
 805EXPORT_SYMBOL(phy_attach_direct);
 806
 807/**
 808 * phy_attach - attach a network device to a particular PHY device
 809 * @dev: network device to attach
 810 * @bus_id: Bus ID of PHY device to attach
 811 * @interface: PHY device's interface
 812 *
 813 * Description: Same as phy_attach_direct() except that a PHY bus_id
 814 *     string is passed instead of a pointer to a struct phy_device.
 815 */
 816struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
 817                              phy_interface_t interface)
 818{
 819        struct bus_type *bus = &mdio_bus_type;
 820        struct phy_device *phydev;
 821        struct device *d;
 822        int rc;
 823
 824        /* Search the list of PHY devices on the mdio bus for the
 825         * PHY with the requested name
 826         */
 827        d = bus_find_device_by_name(bus, NULL, bus_id);
 828        if (!d) {
 829                pr_err("PHY %s not found\n", bus_id);
 830                return ERR_PTR(-ENODEV);
 831        }
 832        phydev = to_phy_device(d);
 833
 834        rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
 835        if (rc)
 836                return ERR_PTR(rc);
 837
 838        return phydev;
 839}
 840EXPORT_SYMBOL(phy_attach);
 841
 842/**
 843 * phy_detach - detach a PHY device from its network device
 844 * @phydev: target phy_device struct
 845 *
 846 * This detaches the phy device from its network device and the phy
 847 * driver, and drops the reference count taken in phy_attach_direct().
 848 */
 849void phy_detach(struct phy_device *phydev)
 850{
 851        struct net_device *dev = phydev->attached_dev;
 852        struct module *ndev_owner = dev->dev.parent->driver->owner;
 853        struct mii_bus *bus;
 854        int i;
 855
 856        phydev->attached_dev->phydev = NULL;
 857        phydev->attached_dev = NULL;
 858        phy_suspend(phydev);
 859
 860        /* If the device had no specific driver before (i.e. - it
 861         * was using the generic driver), we unbind the device
 862         * from the generic driver so that there's a chance a
 863         * real driver could be loaded
 864         */
 865        for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) {
 866                if (phydev->mdio_dev.driver == &genphy_driver[i].driver) {
 867                        device_release_driver(&phydev->mdio_dev);
 868                        break;
 869                }
 870        }
 871
 872        /*
 873         * The phydev might go away on the put_device() below, so avoid
 874         * a use-after-free bug by reading the underlying bus first.
 875         */
 876        bus = phydev->mdio_bus;
 877
 878        put_device(&phydev->mdio_dev);
 879        if (ndev_owner != bus->owner)
 880                module_put(bus->owner);
 881}
 882EXPORT_SYMBOL(phy_detach);
 883
 884int phy_suspend(struct phy_device *phydev)
 885{
 886        struct phy_driver *phydrv = to_phy_driver(phydev->mdio_dev.driver);
 887        struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
 888        int ret = 0;
 889
 890        /* If the device has WOL enabled, we cannot suspend the PHY */
 891        phy_ethtool_get_wol(phydev, &wol);
 892        if (wol.wolopts)
 893                return -EBUSY;
 894
 895        if (phydev->drv && phydrv->suspend)
 896                ret = phydrv->suspend(phydev);
 897
 898        if (ret)
 899                return ret;
 900
 901        phydev->suspended = true;
 902
 903        return ret;
 904}
 905EXPORT_SYMBOL(phy_suspend);
 906
 907int __phy_resume(struct phy_device *phydev)
 908{
 909        struct phy_driver *phydrv = to_phy_driver(phydev->mdio_dev.driver);
 910        int ret = 0;
 911
 912        WARN_ON(!mutex_is_locked(&phydev->lock));
 913
 914        if (phydev->drv && phydrv->resume)
 915                ret = phydrv->resume(phydev);
 916
 917        if (ret)
 918                return ret;
 919
 920        phydev->suspended = false;
 921
 922        return ret;
 923}
 924EXPORT_SYMBOL(__phy_resume);
 925
 926int phy_resume(struct phy_device *phydev)
 927{
 928        int ret;
 929
 930        mutex_lock(&phydev->lock);
 931        ret = __phy_resume(phydev);
 932        mutex_unlock(&phydev->lock);
 933
 934        return ret;
 935}
 936EXPORT_SYMBOL(phy_resume);
 937
 938/* Generic PHY support and helper functions */
 939
 940/**
 941 * genphy_config_advert - sanitize and advertise auto-negotiation parameters
 942 * @phydev: target phy_device struct
 943 *
 944 * Description: Writes MII_ADVERTISE with the appropriate values,
 945 *   after sanitizing the values to make sure we only advertise
 946 *   what is supported.  Returns < 0 on error, 0 if the PHY's advertisement
 947 *   hasn't changed, and > 0 if it has changed.
 948 */
 949static int genphy_config_advert(struct phy_device *phydev)
 950{
 951        u32 advertise;
 952        int oldadv, adv;
 953        int err, changed = 0;
 954
 955        /* Only allow advertising what this PHY supports */
 956        phydev->advertising &= phydev->supported;
 957        advertise = phydev->advertising;
 958
 959        /* Setup standard advertisement */
 960        adv = phy_read(phydev, MII_ADVERTISE);
 961        if (adv < 0)
 962                return adv;
 963
 964        oldadv = adv;
 965        adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
 966                 ADVERTISE_PAUSE_ASYM);
 967        adv |= ethtool_adv_to_mii_adv_t(advertise);
 968
 969        if (adv != oldadv) {
 970                err = phy_write(phydev, MII_ADVERTISE, adv);
 971
 972                if (err < 0)
 973                        return err;
 974                changed = 1;
 975        }
 976
 977        /* Configure gigabit if it's supported */
 978        if (phydev->supported & (SUPPORTED_1000baseT_Half |
 979                                 SUPPORTED_1000baseT_Full)) {
 980                adv = phy_read(phydev, MII_CTRL1000);
 981                if (adv < 0)
 982                        return adv;
 983
 984                oldadv = adv;
 985                adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
 986                adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
 987
 988                if (adv != oldadv) {
 989                        err = phy_write(phydev, MII_CTRL1000, adv);
 990
 991                        if (err < 0)
 992                                return err;
 993                        changed = 1;
 994                }
 995        }
 996
 997        return changed;
 998}
 999
1000/**
1001 * genphy_config_eee_advert - disable unwanted eee mode advertisement
1002 * @phydev: target phy_device struct
1003 *
1004 * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
1005 *   efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
1006 *   changed, and 1 if it has changed.
1007 */
1008static int genphy_config_eee_advert(struct phy_device *phydev)
1009{
1010        int err;
1011
1012        /* Nothing to disable */
1013        if (!phydev->eee_broken_modes)
1014                return 0;
1015
1016        err = phy_modify_mmd_changed(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
1017                                     phydev->eee_broken_modes, 0);
1018        /* If the call failed, we assume that EEE is not supported */
1019        return err < 0 ? 0 : err;
1020}
1021
1022/**
1023 * genphy_setup_forced - configures/forces speed/duplex from @phydev
1024 * @phydev: target phy_device struct
1025 *
1026 * Description: Configures MII_BMCR to force speed/duplex
1027 *   to the values in phydev. Assumes that the values are valid.
1028 *   Please see phy_sanitize_settings().
1029 */
1030static int genphy_setup_forced(struct phy_device *phydev)
1031{
1032        u16 ctl = 0;
1033
1034        phydev->pause = 0;
1035        phydev->asym_pause = 0;
1036
1037        if (SPEED_1000 == phydev->speed)
1038                ctl |= BMCR_SPEED1000;
1039        else if (SPEED_100 == phydev->speed)
1040                ctl |= BMCR_SPEED100;
1041
1042        if (DUPLEX_FULL == phydev->duplex)
1043                ctl |= BMCR_FULLDPLX;
1044
1045        return phy_modify(phydev, MII_BMCR,
1046                          ~(BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN), ctl);
1047}
1048
1049
1050/**
1051 * genphy_restart_aneg - Enable and Restart Autonegotiation
1052 * @phydev: target phy_device struct
1053 */
1054int genphy_restart_aneg(struct phy_device *phydev)
1055{
1056        /* Don't isolate the PHY if we're negotiating */
1057        return phy_modify(phydev, MII_BMCR, BMCR_ISOLATE,
1058                          BMCR_ANENABLE | BMCR_ANRESTART);
1059}
1060EXPORT_SYMBOL(genphy_restart_aneg);
1061
1062/**
1063 * genphy_config_aneg - restart auto-negotiation or write BMCR
1064 * @phydev: target phy_device struct
1065 *
1066 * Description: If auto-negotiation is enabled, we configure the
1067 *   advertising, and then restart auto-negotiation.  If it is not
1068 *   enabled, then we write the BMCR.
1069 */
1070int genphy_config_aneg(struct phy_device *phydev)
1071{
1072        int err, changed;
1073
1074        changed = genphy_config_eee_advert(phydev);
1075
1076        if (AUTONEG_ENABLE != phydev->autoneg)
1077                return genphy_setup_forced(phydev);
1078
1079        err = genphy_config_advert(phydev);
1080        if (err < 0) /* error */
1081                return err;
1082
1083        changed |= err;
1084
1085        if (changed == 0) {
1086                /* Advertisement hasn't changed, but maybe aneg was never on to
1087                 * begin with?  Or maybe phy was isolated?
1088                 */
1089                int ctl = phy_read(phydev, MII_BMCR);
1090
1091                if (ctl < 0)
1092                        return ctl;
1093
1094                if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
1095                        changed = 1; /* do restart aneg */
1096        }
1097
1098        /* Only restart aneg if we are advertising something different
1099         * than we were before.
1100         */
1101        if (changed > 0)
1102                return genphy_restart_aneg(phydev);
1103
1104        return 0;
1105}
1106EXPORT_SYMBOL(genphy_config_aneg);
1107
1108/**
1109 * genphy_aneg_done - return auto-negotiation status
1110 * @phydev: target phy_device struct
1111 *
1112 * Description: Reads the status register and returns 0 either if
1113 *   auto-negotiation is incomplete, or if there was an error.
1114 *   Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
1115 */
1116int genphy_aneg_done(struct phy_device *phydev)
1117{
1118        int retval = phy_read(phydev, MII_BMSR);
1119
1120        return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
1121}
1122EXPORT_SYMBOL(genphy_aneg_done);
1123
1124static int gen10g_config_aneg(struct phy_device *phydev)
1125{
1126        return 0;
1127}
1128
1129/**
1130 * genphy_update_link - update link status in @phydev
1131 * @phydev: target phy_device struct
1132 *
1133 * Description: Update the value in phydev->link to reflect the
1134 *   current link value.  In order to do this, we need to read
1135 *   the status register twice, keeping the second value.
1136 */
1137int genphy_update_link(struct phy_device *phydev)
1138{
1139        int status;
1140
1141        /* Do a fake read */
1142        status = phy_read(phydev, MII_BMSR);
1143        if (status < 0)
1144                return status;
1145
1146        /* Read link and autonegotiation status */
1147        status = phy_read(phydev, MII_BMSR);
1148        if (status < 0)
1149                return status;
1150
1151        if ((status & BMSR_LSTATUS) == 0)
1152                phydev->link = 0;
1153        else
1154                phydev->link = 1;
1155
1156        return 0;
1157}
1158EXPORT_SYMBOL(genphy_update_link);
1159
1160/**
1161 * genphy_read_status - check the link status and update current link state
1162 * @phydev: target phy_device struct
1163 *
1164 * Description: Check the link, then figure out the current state
1165 *   by comparing what we advertise with what the link partner
1166 *   advertises.  Start by checking the gigabit possibilities,
1167 *   then move on to 10/100.
1168 */
1169int genphy_read_status(struct phy_device *phydev)
1170{
1171        int adv;
1172        int err;
1173        int lpa;
1174        int lpagb = 0;
1175
1176        /* Update the link, but return if there was an error */
1177        err = genphy_update_link(phydev);
1178        if (err)
1179                return err;
1180
1181        phydev->lp_advertising = 0;
1182
1183        if (AUTONEG_ENABLE == phydev->autoneg) {
1184                if (phydev->supported & (SUPPORTED_1000baseT_Half
1185                                        | SUPPORTED_1000baseT_Full)) {
1186                        lpagb = phy_read(phydev, MII_STAT1000);
1187                        if (lpagb < 0)
1188                                return lpagb;
1189
1190                        adv = phy_read(phydev, MII_CTRL1000);
1191                        if (adv < 0)
1192                                return adv;
1193
1194                        phydev->lp_advertising =
1195                                mii_stat1000_to_ethtool_lpa_t(lpagb);
1196                        lpagb &= adv << 2;
1197                }
1198
1199                lpa = phy_read(phydev, MII_LPA);
1200                if (lpa < 0)
1201                        return lpa;
1202
1203                phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
1204
1205                adv = phy_read(phydev, MII_ADVERTISE);
1206                if (adv < 0)
1207                        return adv;
1208
1209                lpa &= adv;
1210
1211                phydev->speed = SPEED_10;
1212                phydev->duplex = DUPLEX_HALF;
1213                phydev->pause = 0;
1214                phydev->asym_pause = 0;
1215
1216                if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
1217                        phydev->speed = SPEED_1000;
1218
1219                        if (lpagb & LPA_1000FULL)
1220                                phydev->duplex = DUPLEX_FULL;
1221                } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
1222                        phydev->speed = SPEED_100;
1223
1224                        if (lpa & LPA_100FULL)
1225                                phydev->duplex = DUPLEX_FULL;
1226                } else
1227                        if (lpa & LPA_10FULL)
1228                                phydev->duplex = DUPLEX_FULL;
1229
1230                if (phydev->duplex == DUPLEX_FULL) {
1231                        phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
1232                        phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
1233                }
1234        } else {
1235                int bmcr = phy_read(phydev, MII_BMCR);
1236
1237                if (bmcr < 0)
1238                        return bmcr;
1239
1240                if (bmcr & BMCR_FULLDPLX)
1241                        phydev->duplex = DUPLEX_FULL;
1242                else
1243                        phydev->duplex = DUPLEX_HALF;
1244
1245                if (bmcr & BMCR_SPEED1000)
1246                        phydev->speed = SPEED_1000;
1247                else if (bmcr & BMCR_SPEED100)
1248                        phydev->speed = SPEED_100;
1249                else
1250                        phydev->speed = SPEED_10;
1251
1252                phydev->pause = 0;
1253                phydev->asym_pause = 0;
1254        }
1255
1256        return 0;
1257}
1258EXPORT_SYMBOL(genphy_read_status);
1259
1260static int gen10g_read_status(struct phy_device *phydev)
1261{
1262        u32 mmd_mask = phydev->c45_ids.devices_in_package;
1263        int ret;
1264
1265        /* For now just lie and say it's 10G all the time */
1266        phydev->speed = SPEED_10000;
1267        phydev->duplex = DUPLEX_FULL;
1268
1269        /* Avoid reading the vendor MMDs */
1270        mmd_mask &= ~(BIT(MDIO_MMD_VEND1) | BIT(MDIO_MMD_VEND2));
1271
1272        ret = genphy_c45_read_link(phydev, mmd_mask);
1273
1274        phydev->link = ret > 0 ? 1 : 0;
1275
1276        return 0;
1277}
1278
1279/**
1280 * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
1281 * @phydev: target phy_device struct
1282 *
1283 * Description: Perform a software PHY reset using the standard
1284 * BMCR_RESET bit and poll for the reset bit to be cleared.
1285 *
1286 * Returns: 0 on success, < 0 on failure
1287 */
1288int genphy_soft_reset(struct phy_device *phydev)
1289{
1290        int ret;
1291
1292        ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
1293        if (ret < 0)
1294                return ret;
1295
1296        return phy_poll_reset(phydev);
1297}
1298EXPORT_SYMBOL(genphy_soft_reset);
1299
1300int genphy_config_init(struct phy_device *phydev)
1301{
1302        int val;
1303        u32 features;
1304
1305        /* For now, I'll claim that the generic driver supports
1306         * all possible port types
1307         */
1308        features = (SUPPORTED_TP | SUPPORTED_MII
1309                        | SUPPORTED_AUI | SUPPORTED_FIBRE |
1310                        SUPPORTED_BNC);
1311
1312        /* Do we support autonegotiation? */
1313        val = phy_read(phydev, MII_BMSR);
1314        if (val < 0)
1315                return val;
1316
1317        if (val & BMSR_ANEGCAPABLE)
1318                features |= SUPPORTED_Autoneg;
1319
1320        if (val & BMSR_100FULL)
1321                features |= SUPPORTED_100baseT_Full;
1322        if (val & BMSR_100HALF)
1323                features |= SUPPORTED_100baseT_Half;
1324        if (val & BMSR_10FULL)
1325                features |= SUPPORTED_10baseT_Full;
1326        if (val & BMSR_10HALF)
1327                features |= SUPPORTED_10baseT_Half;
1328
1329        if (val & BMSR_ESTATEN) {
1330                val = phy_read(phydev, MII_ESTATUS);
1331                if (val < 0)
1332                        return val;
1333
1334                if (val & ESTATUS_1000_TFULL)
1335                        features |= SUPPORTED_1000baseT_Full;
1336                if (val & ESTATUS_1000_THALF)
1337                        features |= SUPPORTED_1000baseT_Half;
1338        }
1339
1340        phydev->supported = features;
1341        phydev->advertising = features;
1342
1343        return 0;
1344}
1345EXPORT_SYMBOL(genphy_config_init);
1346
1347static int gen10g_config_init(struct phy_device *phydev)
1348{
1349        /* Temporarily just say we support everything */
1350        phydev->supported = SUPPORTED_10000baseT_Full;
1351        phydev->advertising = SUPPORTED_10000baseT_Full;
1352
1353        return 0;
1354}
1355
1356int genphy_suspend(struct phy_device *phydev)
1357{
1358        return phy_modify(phydev, MII_BMCR, 0, BMCR_PDOWN);
1359}
1360EXPORT_SYMBOL(genphy_suspend);
1361
1362static int gen10g_suspend(struct phy_device *phydev)
1363{
1364        return 0;
1365}
1366
1367int genphy_resume(struct phy_device *phydev)
1368{
1369        return phy_modify(phydev, MII_BMCR, BMCR_PDOWN, 0);
1370}
1371EXPORT_SYMBOL(genphy_resume);
1372
1373static int gen10g_resume(struct phy_device *phydev)
1374{
1375        return 0;
1376}
1377
1378static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
1379{
1380        phydev->supported &= ~(PHY_1000BT_FEATURES | PHY_100BT_FEATURES |
1381                               PHY_10BT_FEATURES);
1382
1383        switch (max_speed) {
1384        default:
1385                return -ENOTSUPP;
1386        case SPEED_1000:
1387                phydev->supported |= PHY_1000BT_FEATURES;
1388                /* fall through */
1389        case SPEED_100:
1390                phydev->supported |= PHY_100BT_FEATURES;
1391                /* fall through */
1392        case SPEED_10:
1393                phydev->supported |= PHY_10BT_FEATURES;
1394        }
1395
1396        return 0;
1397}
1398
1399int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
1400{
1401        int err;
1402
1403        err = __set_phy_supported(phydev, max_speed);
1404        if (err)
1405                return err;
1406
1407        phydev->advertising = phydev->supported;
1408
1409        return 0;
1410}
1411EXPORT_SYMBOL(phy_set_max_speed);
1412
1413static void of_set_phy_eee_broken(struct phy_device *phydev)
1414{
1415        struct device_node *node = phydev->mdio_dev.of_node;
1416        u32 broken;
1417
1418        if (!IS_ENABLED(CONFIG_OF_MDIO))
1419                return;
1420
1421        if (!node)
1422                return;
1423
1424        if (!of_property_read_u32(node, "eee-broken-modes", &broken))
1425                phydev->eee_broken_modes = broken;
1426}
1427
1428/**
1429 * phy_probe - probe and init a PHY device
1430 * @dev: device to probe and init
1431 *
1432 * Description: Take care of setting up the phy_device structure,
1433 *   set the state to READY (the driver's init function should
1434 *   set it to STARTING if needed).
1435 */
1436static int phy_probe(struct device *dev)
1437{
1438        struct phy_device *phydev = to_phy_device(dev);
1439        struct device_driver *drv = phydev->mdio_dev.driver;
1440        struct phy_driver *phydrv = to_phy_driver(drv);
1441        int err = 0;
1442
1443        phydev->drv = phydrv;
1444
1445        /* Disable the interrupt if the PHY doesn't support it
1446         * but the interrupt is still a valid one
1447         */
1448        if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
1449            phy_interrupt_is_valid(phydev))
1450                phydev->irq = PHY_POLL;
1451
1452        if (phydrv->flags & PHY_IS_INTERNAL)
1453                phydev->is_internal = true;
1454
1455        mutex_lock(&phydev->lock);
1456
1457        /* Start out supporting everything. Eventually,
1458         * a controller will attach, and may modify one
1459         * or both of these values
1460         */
1461        phydev->supported = phydrv->features;
1462        phydev->advertising = phydrv->features;
1463
1464        /* Get the EEE modes we want to prohibit. We will ask
1465         * the PHY stop advertising these mode later on
1466         */
1467        of_set_phy_eee_broken(phydev);
1468
1469        /* Set the state to READY by default */
1470        phydev->state = PHY_READY;
1471
1472        if (phydev->drv->probe)
1473                err = phydev->drv->probe(phydev);
1474
1475        mutex_unlock(&phydev->lock);
1476
1477        return err;
1478}
1479
1480static int phy_remove(struct device *dev)
1481{
1482        struct phy_device *phydev = to_phy_device(dev);
1483
1484        cancel_delayed_work_sync(&phydev->state_queue);
1485
1486        mutex_lock(&phydev->lock);
1487        phydev->state = PHY_DOWN;
1488        mutex_unlock(&phydev->lock);
1489
1490        if (phydev->drv && phydev->drv->remove)
1491                phydev->drv->remove(phydev);
1492        phydev->drv = NULL;
1493
1494        return 0;
1495}
1496
1497/**
1498 * phy_driver_register - register a phy_driver with the PHY layer
1499 * @new_driver: new phy_driver to register
1500 */
1501int phy_driver_register(struct phy_driver *new_driver)
1502{
1503        int retval;
1504
1505        new_driver->driver.name = new_driver->name;
1506        new_driver->driver.bus = &mdio_bus_type;
1507        new_driver->driver.probe = phy_probe;
1508        new_driver->driver.remove = phy_remove;
1509
1510        retval = driver_register(&new_driver->driver);
1511        if (retval) {
1512                pr_err("%s: Error %d in registering driver\n",
1513                       new_driver->name, retval);
1514
1515                return retval;
1516        }
1517
1518        pr_debug("%s: Registered new driver\n", new_driver->name);
1519
1520        return 0;
1521}
1522EXPORT_SYMBOL(phy_driver_register);
1523
1524int phy_drivers_register(struct phy_driver *new_driver, int n)
1525{
1526        int i, ret = 0;
1527
1528        for (i = 0; i < n; i++) {
1529                ret = phy_driver_register(new_driver + i);
1530                if (ret) {
1531                        while (i-- > 0)
1532                                phy_driver_unregister(new_driver + i);
1533                        break;
1534                }
1535        }
1536        return ret;
1537}
1538EXPORT_SYMBOL(phy_drivers_register);
1539
1540void phy_driver_unregister(struct phy_driver *drv)
1541{
1542        driver_unregister(&drv->driver);
1543}
1544EXPORT_SYMBOL(phy_driver_unregister);
1545
1546void phy_drivers_unregister(struct phy_driver *drv, int n)
1547{
1548        int i;
1549
1550        for (i = 0; i < n; i++)
1551                phy_driver_unregister(drv + i);
1552}
1553EXPORT_SYMBOL(phy_drivers_unregister);
1554
1555static struct phy_driver genphy_driver[] = {
1556{
1557        .phy_id         = 0xffffffff,
1558        .phy_id_mask    = 0xffffffff,
1559        .name           = "Generic PHY",
1560        .config_init    = genphy_config_init,
1561        .features       = 0,
1562        .config_aneg    = genphy_config_aneg,
1563        .aneg_done      = genphy_aneg_done,
1564        .read_status    = genphy_read_status,
1565        .suspend        = genphy_suspend,
1566        .resume         = genphy_resume,
1567        .driver         = { .owner = THIS_MODULE, },
1568}, {
1569        .phy_id         = 0xffffffff,
1570        .phy_id_mask    = 0xffffffff,
1571        .name           = "Generic 10G PHY",
1572        .config_init    = gen10g_config_init,
1573        .features       = 0,
1574        .config_aneg    = gen10g_config_aneg,
1575        .read_status    = gen10g_read_status,
1576        .suspend        = gen10g_suspend,
1577        .resume         = gen10g_resume,
1578        .driver         = {.owner = THIS_MODULE, },
1579} };
1580
1581static int __init phy_init(void)
1582{
1583        int rc;
1584
1585        rc = mdio_bus_init();
1586        if (rc)
1587                return rc;
1588
1589        rc = phy_drivers_register(genphy_driver,
1590                                  ARRAY_SIZE(genphy_driver));
1591        if (rc)
1592                mdio_bus_exit();
1593
1594        return rc;
1595}
1596
1597static void __exit phy_exit(void)
1598{
1599        phy_drivers_unregister(genphy_driver,
1600                               ARRAY_SIZE(genphy_driver));
1601        mdio_bus_exit();
1602}
1603
1604subsys_initcall(phy_init);
1605module_exit(phy_exit);
1606