blob: fe74ffc7f64e18f36a3e9f00e17845028764944c [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Framework and drivers for configuring and reading different PHYs
3 * Based on code in sungem_phy.c and gianfar_phy.c
4 *
5 * Author: Andy Fleming
6 *
7 * Copyright (c) 2004 Freescale Semiconductor, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#ifndef __PHY_H
17#define __PHY_H
18
19#include <linux/compiler.h>
20#include <linux/spinlock.h>
21#include <linux/ethtool.h>
22#include <linux/mdio.h>
23#include <linux/mii.h>
24#include <linux/module.h>
25#include <linux/timer.h>
26#include <linux/workqueue.h>
27#include <linux/mod_devicetable.h>
28
29#include <linux/atomic.h>
30
31#define PHY_DEFAULT_FEATURES (SUPPORTED_Autoneg | \
32 SUPPORTED_TP | \
33 SUPPORTED_MII)
34
35#define PHY_10BT_FEATURES (SUPPORTED_10baseT_Half | \
36 SUPPORTED_10baseT_Full)
37
38#define PHY_100BT_FEATURES (SUPPORTED_100baseT_Half | \
39 SUPPORTED_100baseT_Full)
40
41#define PHY_1000BT_FEATURES (SUPPORTED_1000baseT_Half | \
42 SUPPORTED_1000baseT_Full)
43
44#define PHY_BASIC_FEATURES (PHY_10BT_FEATURES | \
45 PHY_100BT_FEATURES | \
46 PHY_DEFAULT_FEATURES)
47
48#define PHY_GBIT_FEATURES (PHY_BASIC_FEATURES | \
49 PHY_1000BT_FEATURES)
50
51
52/*
53 * Set phydev->irq to PHY_POLL if interrupts are not supported,
54 * or not desired for this PHY. Set to PHY_IGNORE_INTERRUPT if
55 * the attached driver handles the interrupt
56 */
57#define PHY_POLL -1
58#define PHY_IGNORE_INTERRUPT -2
59
60#define PHY_HAS_INTERRUPT 0x00000001
61#define PHY_IS_INTERNAL 0x00000002
62#define MDIO_DEVICE_IS_PHY 0x80000000
63
64/* Interface Mode definitions */
65typedef enum {
66 PHY_INTERFACE_MODE_NA,
67 PHY_INTERFACE_MODE_INTERNAL,
68 PHY_INTERFACE_MODE_MII,
69 PHY_INTERFACE_MODE_GMII,
70 PHY_INTERFACE_MODE_SGMII,
71 PHY_INTERFACE_MODE_TBI,
72 PHY_INTERFACE_MODE_REVMII,
73 PHY_INTERFACE_MODE_RMII,
74 PHY_INTERFACE_MODE_RGMII,
75 PHY_INTERFACE_MODE_RGMII_ID,
76 PHY_INTERFACE_MODE_RGMII_RXID,
77 PHY_INTERFACE_MODE_RGMII_TXID,
78 PHY_INTERFACE_MODE_RTBI,
79 PHY_INTERFACE_MODE_SMII,
80 PHY_INTERFACE_MODE_XGMII,
81 PHY_INTERFACE_MODE_MOCA,
82 PHY_INTERFACE_MODE_QSGMII,
83 PHY_INTERFACE_MODE_TRGMII,
84 PHY_INTERFACE_MODE_1000BASEX,
85 PHY_INTERFACE_MODE_2500BASEX,
86 PHY_INTERFACE_MODE_RXAUI,
87 PHY_INTERFACE_MODE_XAUI,
88 /* 10GBASE-KR, XFI, SFI - single lane 10G Serdes */
89 PHY_INTERFACE_MODE_10GKR,
90 PHY_INTERFACE_MODE_MAX,
91} phy_interface_t;
92
93/**
94 * phy_supported_speeds - return all speeds currently supported by a phy device
95 * @phy: The phy device to return supported speeds of.
96 * @speeds: buffer to store supported speeds in.
97 * @size: size of speeds buffer.
98 *
99 * Description: Returns the number of supported speeds, and
100 * fills the speeds * buffer with the supported speeds. If speeds buffer is
101 * too small to contain * all currently supported speeds, will return as
102 * many speeds as can fit.
103 */
104unsigned int phy_supported_speeds(struct phy_device *phy,
105 unsigned int *speeds,
106 unsigned int size);
107
108/**
109 * It maps 'enum phy_interface_t' found in include/linux/phy.h
110 * into the device tree binding of 'phy-mode', so that Ethernet
111 * device driver can get phy interface from device tree.
112 */
113static inline const char *phy_modes(phy_interface_t interface)
114{
115 switch (interface) {
116 case PHY_INTERFACE_MODE_NA:
117 return "";
118 case PHY_INTERFACE_MODE_INTERNAL:
119 return "internal";
120 case PHY_INTERFACE_MODE_MII:
121 return "mii";
122 case PHY_INTERFACE_MODE_GMII:
123 return "gmii";
124 case PHY_INTERFACE_MODE_SGMII:
125 return "sgmii";
126 case PHY_INTERFACE_MODE_TBI:
127 return "tbi";
128 case PHY_INTERFACE_MODE_REVMII:
129 return "rev-mii";
130 case PHY_INTERFACE_MODE_RMII:
131 return "rmii";
132 case PHY_INTERFACE_MODE_RGMII:
133 return "rgmii";
134 case PHY_INTERFACE_MODE_RGMII_ID:
135 return "rgmii-id";
136 case PHY_INTERFACE_MODE_RGMII_RXID:
137 return "rgmii-rxid";
138 case PHY_INTERFACE_MODE_RGMII_TXID:
139 return "rgmii-txid";
140 case PHY_INTERFACE_MODE_RTBI:
141 return "rtbi";
142 case PHY_INTERFACE_MODE_SMII:
143 return "smii";
144 case PHY_INTERFACE_MODE_XGMII:
145 return "xgmii";
146 case PHY_INTERFACE_MODE_MOCA:
147 return "moca";
148 case PHY_INTERFACE_MODE_QSGMII:
149 return "qsgmii";
150 case PHY_INTERFACE_MODE_TRGMII:
151 return "trgmii";
152 case PHY_INTERFACE_MODE_1000BASEX:
153 return "1000base-x";
154 case PHY_INTERFACE_MODE_2500BASEX:
155 return "2500base-x";
156 case PHY_INTERFACE_MODE_RXAUI:
157 return "rxaui";
158 case PHY_INTERFACE_MODE_XAUI:
159 return "xaui";
160 case PHY_INTERFACE_MODE_10GKR:
161 return "10gbase-kr";
162 default:
163 return "unknown";
164 }
165}
166
167
168#define PHY_INIT_TIMEOUT 100000
169#define PHY_STATE_TIME 1
170#define PHY_FORCE_TIMEOUT 10
171#define PHY_AN_TIMEOUT 10
172
173#define PHY_MAX_ADDR 32
174
175/* Used when trying to connect to a specific phy (mii bus id:phy device id) */
176#define PHY_ID_FMT "%s:%02x"
177
178#define MII_BUS_ID_SIZE 61
179
180/* Or MII_ADDR_C45 into regnum for read/write on mii_bus to enable the 21 bit
181 IEEE 802.3ae clause 45 addressing mode used by 10GIGE phy chips. */
182#define MII_ADDR_C45 (1<<30)
183#define MII_DEVADDR_C45_SHIFT 16
184#define MII_REGADDR_C45_MASK GENMASK(15, 0)
185
186struct device;
187struct phylink;
188struct sk_buff;
189
190/*
191 * The Bus class for PHYs. Devices which provide access to
192 * PHYs should register using this structure
193 */
194struct mii_bus {
195 struct module *owner;
196 const char *name;
197 char id[MII_BUS_ID_SIZE];
198 void *priv;
199 int (*read)(struct mii_bus *bus, int addr, int regnum);
200 int (*write)(struct mii_bus *bus, int addr, int regnum, u16 val);
201 int (*reset)(struct mii_bus *bus);
202
203 /*
204 * A lock to ensure that only one thing can read/write
205 * the MDIO bus at a time
206 */
207 struct mutex mdio_lock;
208
209 struct device *parent;
210 enum {
211 MDIOBUS_ALLOCATED = 1,
212 MDIOBUS_REGISTERED,
213 MDIOBUS_UNREGISTERED,
214 MDIOBUS_RELEASED,
215 } state;
216 struct device dev;
217
218 /* list of all PHYs on bus */
219 struct mdio_device *mdio_map[PHY_MAX_ADDR];
220
221 /* PHY addresses to be ignored when probing */
222 u32 phy_mask;
223
224 /* PHY addresses to ignore the TA/read failure */
225 u32 phy_ignore_ta_mask;
226
227 /*
228 * An array of interrupts, each PHY's interrupt at the index
229 * matching its address
230 */
231 int irq[PHY_MAX_ADDR];
232
233 /* GPIO reset pulse width in microseconds */
234 int reset_delay_us;
235 /* RESET GPIO descriptor pointer */
236 struct gpio_desc *reset_gpiod;
237};
238#define to_mii_bus(d) container_of(d, struct mii_bus, dev)
239
240struct mii_bus *mdiobus_alloc_size(size_t);
241static inline struct mii_bus *mdiobus_alloc(void)
242{
243 return mdiobus_alloc_size(0);
244}
245
246int __mdiobus_register(struct mii_bus *bus, struct module *owner);
247#define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
248void mdiobus_unregister(struct mii_bus *bus);
249void mdiobus_free(struct mii_bus *bus);
250struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv);
251static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
252{
253 return devm_mdiobus_alloc_size(dev, 0);
254}
255
256void devm_mdiobus_free(struct device *dev, struct mii_bus *bus);
257struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
258
259#define PHY_INTERRUPT_DISABLED 0x0
260#define PHY_INTERRUPT_ENABLED 0x80000000
261
262/* PHY state machine states:
263 *
264 * DOWN: PHY device and driver are not ready for anything. probe
265 * should be called if and only if the PHY is in this state,
266 * given that the PHY device exists.
267 * - PHY driver probe function will, depending on the PHY, set
268 * the state to STARTING or READY
269 *
270 * STARTING: PHY device is coming up, and the ethernet driver is
271 * not ready. PHY drivers may set this in the probe function.
272 * If they do, they are responsible for making sure the state is
273 * eventually set to indicate whether the PHY is UP or READY,
274 * depending on the state when the PHY is done starting up.
275 * - PHY driver will set the state to READY
276 * - start will set the state to PENDING
277 *
278 * READY: PHY is ready to send and receive packets, but the
279 * controller is not. By default, PHYs which do not implement
280 * probe will be set to this state by phy_probe(). If the PHY
281 * driver knows the PHY is ready, and the PHY state is STARTING,
282 * then it sets this STATE.
283 * - start will set the state to UP
284 *
285 * PENDING: PHY device is coming up, but the ethernet driver is
286 * ready. phy_start will set this state if the PHY state is
287 * STARTING.
288 * - PHY driver will set the state to UP when the PHY is ready
289 *
290 * UP: The PHY and attached device are ready to do work.
291 * Interrupts should be started here.
292 * - timer moves to AN
293 *
294 * AN: The PHY is currently negotiating the link state. Link is
295 * therefore down for now. phy_timer will set this state when it
296 * detects the state is UP. config_aneg will set this state
297 * whenever called with phydev->autoneg set to AUTONEG_ENABLE.
298 * - If autonegotiation finishes, but there's no link, it sets
299 * the state to NOLINK.
300 * - If aneg finishes with link, it sets the state to RUNNING,
301 * and calls adjust_link
302 * - If autonegotiation did not finish after an arbitrary amount
303 * of time, autonegotiation should be tried again if the PHY
304 * supports "magic" autonegotiation (back to AN)
305 * - If it didn't finish, and no magic_aneg, move to FORCING.
306 *
307 * NOLINK: PHY is up, but not currently plugged in.
308 * - If the timer notes that the link comes back, we move to RUNNING
309 * - config_aneg moves to AN
310 * - phy_stop moves to HALTED
311 *
312 * FORCING: PHY is being configured with forced settings
313 * - if link is up, move to RUNNING
314 * - If link is down, we drop to the next highest setting, and
315 * retry (FORCING) after a timeout
316 * - phy_stop moves to HALTED
317 *
318 * RUNNING: PHY is currently up, running, and possibly sending
319 * and/or receiving packets
320 * - timer will set CHANGELINK if we're polling (this ensures the
321 * link state is polled every other cycle of this state machine,
322 * which makes it every other second)
323 * - irq will set CHANGELINK
324 * - config_aneg will set AN
325 * - phy_stop moves to HALTED
326 *
327 * CHANGELINK: PHY experienced a change in link state
328 * - timer moves to RUNNING if link
329 * - timer moves to NOLINK if the link is down
330 * - phy_stop moves to HALTED
331 *
332 * HALTED: PHY is up, but no polling or interrupts are done. Or
333 * PHY is in an error state.
334 *
335 * - phy_start moves to RESUMING
336 *
337 * RESUMING: PHY was halted, but now wants to run again.
338 * - If we are forcing, or aneg is done, timer moves to RUNNING
339 * - If aneg is not done, timer moves to AN
340 * - phy_stop moves to HALTED
341 */
342enum phy_state {
343 PHY_DOWN = 0,
344 PHY_STARTING,
345 PHY_READY,
346 PHY_PENDING,
347 PHY_UP,
348 PHY_AN,
349 PHY_RUNNING,
350 PHY_NOLINK,
351 PHY_FORCING,
352 PHY_CHANGELINK,
353 PHY_HALTED,
354 PHY_RESUMING
355};
356
357/**
358 * struct phy_c45_device_ids - 802.3-c45 Device Identifiers
359 * @devices_in_package: Bit vector of devices present.
360 * @device_ids: The device identifer for each present device.
361 */
362struct phy_c45_device_ids {
363 u32 devices_in_package;
364 u32 device_ids[8];
365};
366
367/* phy_device: An instance of a PHY
368 *
369 * drv: Pointer to the driver for this PHY instance
370 * phy_id: UID for this device found during discovery
371 * c45_ids: 802.3-c45 Device Identifers if is_c45.
372 * is_c45: Set to true if this phy uses clause 45 addressing.
373 * is_internal: Set to true if this phy is internal to a MAC.
374 * is_pseudo_fixed_link: Set to true if this phy is an Ethernet switch, etc.
375 * has_fixups: Set to true if this phy has fixups/quirks.
376 * suspended: Set to true if this phy has been suspended successfully.
377 * suspended_by_mdio_bus: Set to true if this phy was suspended by MDIO bus.
378 * sysfs_links: Internal boolean tracking sysfs symbolic links setup/removal.
379 * loopback_enabled: Set true if this phy has been loopbacked successfully.
380 * state: state of the PHY for management purposes
381 * dev_flags: Device-specific flags used by the PHY driver.
382 * link_timeout: The number of timer firings to wait before the
383 * giving up on the current attempt at acquiring a link
384 * irq: IRQ number of the PHY's interrupt (-1 if none)
385 * phy_timer: The timer for handling the state machine
386 * phy_queue: A work_queue for the phy_mac_interrupt
387 * attached_dev: The attached enet driver's device instance ptr
388 * adjust_link: Callback for the enet controller to respond to
389 * changes in the link state.
390 *
391 * speed, duplex, pause, supported, advertising, lp_advertising,
392 * and autoneg are used like in mii_if_info
393 *
394 * interrupts currently only supports enabled or disabled,
395 * but could be changed in the future to support enabling
396 * and disabling specific interrupts
397 *
398 * Contains some infrastructure for polling and interrupt
399 * handling, as well as handling shifts in PHY hardware state
400 */
401struct phy_device {
402 struct mdio_device mdio;
403
404 /* Information about the PHY type */
405 /* And management functions */
406 struct phy_driver *drv;
407
408 u32 phy_id;
409
410 struct phy_c45_device_ids c45_ids;
411 bool is_c45;
412 bool is_internal;
413 bool is_pseudo_fixed_link;
414 bool has_fixups;
415 bool suspended;
416 bool suspended_by_mdio_bus;
417 bool sysfs_links;
418 bool loopback_enabled;
419
420 enum phy_state state;
421
422 u32 dev_flags;
423
424 phy_interface_t interface;
425
426 /*
427 * forced speed & duplex (no autoneg)
428 * partner speed & duplex & pause (autoneg)
429 */
430 int speed;
431 int duplex;
432 int pause;
433 int asym_pause;
434
435 /* The most recently read link state */
436 int link;
437
438 /* Enabled Interrupts */
439 u32 interrupts;
440
441 /* Union of PHY and Attached devices' supported modes */
442 /* See mii.h for more info */
443 u32 supported;
444 u32 advertising;
445 u32 lp_advertising;
446
447 /* Energy efficient ethernet modes which should be prohibited */
448 u32 eee_broken_modes;
449
450 int autoneg;
451
452 int link_timeout;
453
454#ifdef CONFIG_LED_TRIGGER_PHY
455 struct phy_led_trigger *phy_led_triggers;
456 unsigned int phy_num_led_triggers;
457 struct phy_led_trigger *last_triggered;
458#endif
459
460 /*
461 * Interrupt number for this PHY
462 * -1 means no interrupt
463 */
464 int irq;
465
466 /* private data pointer */
467 /* For use by PHYs to maintain extra state */
468 void *priv;
469
470 /* Interrupt and Polling infrastructure */
471 struct work_struct phy_queue;
472 struct delayed_work state_queue;
473 atomic_t irq_disable;
474
475 struct mutex lock;
476
477 struct phylink *phylink;
478 struct net_device *attached_dev;
479
480 u8 mdix;
481 u8 mdix_ctrl;
482
483 void (*phy_link_change)(struct phy_device *, bool up, bool do_carrier);
484 void (*adjust_link)(struct net_device *dev);
485};
486#define to_phy_device(d) container_of(to_mdio_device(d), \
487 struct phy_device, mdio)
488
489/* struct phy_driver: Driver structure for a particular PHY type
490 *
491 * driver_data: static driver data
492 * phy_id: The result of reading the UID registers of this PHY
493 * type, and ANDing them with the phy_id_mask. This driver
494 * only works for PHYs with IDs which match this field
495 * name: The friendly name of this PHY type
496 * phy_id_mask: Defines the important bits of the phy_id
497 * features: A list of features (speed, duplex, etc) supported
498 * by this PHY
499 * flags: A bitfield defining certain other features this PHY
500 * supports (like interrupts)
501 *
502 * The drivers must implement config_aneg and read_status. All
503 * other functions are optional. Note that none of these
504 * functions should be called from interrupt time. The goal is
505 * for the bus read/write functions to be able to block when the
506 * bus transaction is happening, and be freed up by an interrupt
507 * (The MPC85xx has this ability, though it is not currently
508 * supported in the driver).
509 */
510struct phy_driver {
511 struct mdio_driver_common mdiodrv;
512 u32 phy_id;
513 char *name;
514 unsigned int phy_id_mask;
515 u32 features;
516 u32 flags;
517 const void *driver_data;
518
519 /*
520 * Called to issue a PHY software reset
521 */
522 int (*soft_reset)(struct phy_device *phydev);
523
524 /*
525 * Called to initialize the PHY,
526 * including after a reset
527 */
528 int (*config_init)(struct phy_device *phydev);
529
530 /*
531 * Called during discovery. Used to set
532 * up device-specific structures, if any
533 */
534 int (*probe)(struct phy_device *phydev);
535
536 /* PHY Power Management */
537 int (*suspend)(struct phy_device *phydev);
538 int (*resume)(struct phy_device *phydev);
539
540 /*
541 * Configures the advertisement and resets
542 * autonegotiation if phydev->autoneg is on,
543 * forces the speed to the current settings in phydev
544 * if phydev->autoneg is off
545 */
546 int (*config_aneg)(struct phy_device *phydev);
547
548 /* Determines the auto negotiation result */
549 int (*aneg_done)(struct phy_device *phydev);
550
551 /* Determines the negotiated speed and duplex */
552 int (*read_status)(struct phy_device *phydev);
553
554 /* Clears any pending interrupts */
555 int (*ack_interrupt)(struct phy_device *phydev);
556
557 /* Enables or disables interrupts */
558 int (*config_intr)(struct phy_device *phydev);
559
560 /*
561 * Checks if the PHY generated an interrupt.
562 * For multi-PHY devices with shared PHY interrupt pin
563 */
564 int (*did_interrupt)(struct phy_device *phydev);
565
566 /* Clears up any memory if needed */
567 void (*remove)(struct phy_device *phydev);
568
569 /* Returns true if this is a suitable driver for the given
570 * phydev. If NULL, matching is based on phy_id and
571 * phy_id_mask.
572 */
573 int (*match_phy_device)(struct phy_device *phydev);
574
575 /* Handles ethtool queries for hardware time stamping. */
576 int (*ts_info)(struct phy_device *phydev, struct ethtool_ts_info *ti);
577
578 /* Handles SIOCSHWTSTAMP ioctl for hardware time stamping. */
579 int (*hwtstamp)(struct phy_device *phydev, struct ifreq *ifr);
580
581 /*
582 * Requests a Rx timestamp for 'skb'. If the skb is accepted,
583 * the phy driver promises to deliver it using netif_rx() as
584 * soon as a timestamp becomes available. One of the
585 * PTP_CLASS_ values is passed in 'type'. The function must
586 * return true if the skb is accepted for delivery.
587 */
588 bool (*rxtstamp)(struct phy_device *dev, struct sk_buff *skb, int type);
589
590 /*
591 * Requests a Tx timestamp for 'skb'. The phy driver promises
592 * to deliver it using skb_complete_tx_timestamp() as soon as a
593 * timestamp becomes available. One of the PTP_CLASS_ values
594 * is passed in 'type'.
595 */
596 void (*txtstamp)(struct phy_device *dev, struct sk_buff *skb, int type);
597
598 /* Some devices (e.g. qnap TS-119P II) require PHY register changes to
599 * enable Wake on LAN, so set_wol is provided to be called in the
600 * ethernet driver's set_wol function. */
601 int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
602
603 /* See set_wol, but for checking whether Wake on LAN is enabled. */
604 void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
605
606 /*
607 * Called to inform a PHY device driver when the core is about to
608 * change the link state. This callback is supposed to be used as
609 * fixup hook for drivers that need to take action when the link
610 * state changes. Drivers are by no means allowed to mess with the
611 * PHY device structure in their implementations.
612 */
613 void (*link_change_notify)(struct phy_device *dev);
614
615 /*
616 * Phy specific driver override for reading a MMD register.
617 * This function is optional for PHY specific drivers. When
618 * not provided, the default MMD read function will be used
619 * by phy_read_mmd(), which will use either a direct read for
620 * Clause 45 PHYs or an indirect read for Clause 22 PHYs.
621 * devnum is the MMD device number within the PHY device,
622 * regnum is the register within the selected MMD device.
623 */
624 int (*read_mmd)(struct phy_device *dev, int devnum, u16 regnum);
625
626 /*
627 * Phy specific driver override for writing a MMD register.
628 * This function is optional for PHY specific drivers. When
629 * not provided, the default MMD write function will be used
630 * by phy_write_mmd(), which will use either a direct write for
631 * Clause 45 PHYs, or an indirect write for Clause 22 PHYs.
632 * devnum is the MMD device number within the PHY device,
633 * regnum is the register within the selected MMD device.
634 * val is the value to be written.
635 */
636 int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum,
637 u16 val);
638
639 /* Get the size and type of the eeprom contained within a plug-in
640 * module */
641 int (*module_info)(struct phy_device *dev,
642 struct ethtool_modinfo *modinfo);
643
644 /* Get the eeprom information from the plug-in module */
645 int (*module_eeprom)(struct phy_device *dev,
646 struct ethtool_eeprom *ee, u8 *data);
647
648 /* Get statistics from the phy using ethtool */
649 int (*get_sset_count)(struct phy_device *dev);
650 void (*get_strings)(struct phy_device *dev, u8 *data);
651 void (*get_stats)(struct phy_device *dev,
652 struct ethtool_stats *stats, u64 *data);
653
654 /* Get and Set PHY tunables */
655 int (*get_tunable)(struct phy_device *dev,
656 struct ethtool_tunable *tuna, void *data);
657 int (*set_tunable)(struct phy_device *dev,
658 struct ethtool_tunable *tuna,
659 const void *data);
660 int (*set_loopback)(struct phy_device *dev, bool enable);
661};
662#define to_phy_driver(d) container_of(to_mdio_common_driver(d), \
663 struct phy_driver, mdiodrv)
664
665#define PHY_ANY_ID "MATCH ANY PHY"
666#define PHY_ANY_UID 0xffffffff
667
668/* A Structure for boards to register fixups with the PHY Lib */
669struct phy_fixup {
670 struct list_head list;
671 char bus_id[MII_BUS_ID_SIZE + 3];
672 u32 phy_uid;
673 u32 phy_uid_mask;
674 int (*run)(struct phy_device *phydev);
675};
676
677const char *phy_speed_to_str(int speed);
678const char *phy_duplex_to_str(unsigned int duplex);
679
680/* A structure for mapping a particular speed and duplex
681 * combination to a particular SUPPORTED and ADVERTISED value
682 */
683struct phy_setting {
684 u32 speed;
685 u8 duplex;
686 u8 bit;
687};
688
689const struct phy_setting *
690phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
691 size_t maxbit, bool exact);
692size_t phy_speeds(unsigned int *speeds, size_t size,
693 unsigned long *mask, size_t maxbit);
694
695/**
696 * phy_read_mmd - Convenience function for reading a register
697 * from an MMD on a given PHY.
698 * @phydev: The phy_device struct
699 * @devad: The MMD to read from
700 * @regnum: The register on the MMD to read
701 *
702 * Same rules as for phy_read();
703 */
704int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
705
706/**
707 * phy_read - Convenience function for reading a given PHY register
708 * @phydev: the phy_device struct
709 * @regnum: register number to read
710 *
711 * NOTE: MUST NOT be called from interrupt context,
712 * because the bus read/write functions may wait for an interrupt
713 * to conclude the operation.
714 */
715static inline int phy_read(struct phy_device *phydev, u32 regnum)
716{
717 return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
718}
719
720/**
721 * phy_write - Convenience function for writing a given PHY register
722 * @phydev: the phy_device struct
723 * @regnum: register number to write
724 * @val: value to write to @regnum
725 *
726 * NOTE: MUST NOT be called from interrupt context,
727 * because the bus read/write functions may wait for an interrupt
728 * to conclude the operation.
729 */
730static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
731{
732 return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val);
733}
734
735/**
736 * phy_interrupt_is_valid - Convenience function for testing a given PHY irq
737 * @phydev: the phy_device struct
738 *
739 * NOTE: must be kept in sync with addition/removal of PHY_POLL and
740 * PHY_IGNORE_INTERRUPT
741 */
742static inline bool phy_interrupt_is_valid(struct phy_device *phydev)
743{
744 return phydev->irq != PHY_POLL && phydev->irq != PHY_IGNORE_INTERRUPT;
745}
746
747/**
748 * phy_is_internal - Convenience function for testing if a PHY is internal
749 * @phydev: the phy_device struct
750 */
751static inline bool phy_is_internal(struct phy_device *phydev)
752{
753 return phydev->is_internal;
754}
755
756/**
757 * phy_interface_mode_is_rgmii - Convenience function for testing if a
758 * PHY interface mode is RGMII (all variants)
759 * @mode: the phy_interface_t enum
760 */
761static inline bool phy_interface_mode_is_rgmii(phy_interface_t mode)
762{
763 return mode >= PHY_INTERFACE_MODE_RGMII &&
764 mode <= PHY_INTERFACE_MODE_RGMII_TXID;
765};
766
767/**
768 * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
769 * is RGMII (all variants)
770 * @phydev: the phy_device struct
771 */
772static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
773{
774 return phy_interface_mode_is_rgmii(phydev->interface);
775};
776
777/*
778 * phy_is_pseudo_fixed_link - Convenience function for testing if this
779 * PHY is the CPU port facing side of an Ethernet switch, or similar.
780 * @phydev: the phy_device struct
781 */
782static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
783{
784 return phydev->is_pseudo_fixed_link;
785}
786
787/**
788 * phy_write_mmd - Convenience function for writing a register
789 * on an MMD on a given PHY.
790 * @phydev: The phy_device struct
791 * @devad: The MMD to read from
792 * @regnum: The register on the MMD to read
793 * @val: value to write to @regnum
794 *
795 * Same rules as for phy_write();
796 */
797int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
798
799struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
800 bool is_c45,
801 struct phy_c45_device_ids *c45_ids);
802#if IS_ENABLED(CONFIG_PHYLIB)
803struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
804int phy_device_register(struct phy_device *phy);
805void phy_device_free(struct phy_device *phydev);
806#else
807static inline
808struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
809{
810 return NULL;
811}
812
813static inline int phy_device_register(struct phy_device *phy)
814{
815 return 0;
816}
817
818static inline void phy_device_free(struct phy_device *phydev) { }
819#endif /* CONFIG_PHYLIB */
820void phy_device_remove(struct phy_device *phydev);
821int phy_init_hw(struct phy_device *phydev);
822int phy_suspend(struct phy_device *phydev);
823int phy_resume(struct phy_device *phydev);
824int __phy_resume(struct phy_device *phydev);
825int phy_loopback(struct phy_device *phydev, bool enable);
826struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
827 phy_interface_t interface);
828struct phy_device *phy_find_first(struct mii_bus *bus);
829int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
830 u32 flags, phy_interface_t interface);
831int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
832 void (*handler)(struct net_device *),
833 phy_interface_t interface);
834struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
835 void (*handler)(struct net_device *),
836 phy_interface_t interface);
837void phy_disconnect(struct phy_device *phydev);
838void phy_detach(struct phy_device *phydev);
839void phy_start(struct phy_device *phydev);
840void phy_stop(struct phy_device *phydev);
841int phy_start_aneg(struct phy_device *phydev);
842int phy_aneg_done(struct phy_device *phydev);
843
844int phy_stop_interrupts(struct phy_device *phydev);
845int phy_restart_aneg(struct phy_device *phydev);
846
847static inline int phy_read_status(struct phy_device *phydev)
848{
849 if (!phydev->drv)
850 return -EIO;
851
852 return phydev->drv->read_status(phydev);
853}
854
855#define phydev_err(_phydev, format, args...) \
856 dev_err(&_phydev->mdio.dev, format, ##args)
857
858#define phydev_dbg(_phydev, format, args...) \
859 dev_dbg(&_phydev->mdio.dev, format, ##args)
860
861static inline const char *phydev_name(const struct phy_device *phydev)
862{
863 return dev_name(&phydev->mdio.dev);
864}
865
866void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
867 __printf(2, 3);
868void phy_attached_info(struct phy_device *phydev);
869
870/* Clause 22 PHY */
871int genphy_config_init(struct phy_device *phydev);
872int genphy_setup_forced(struct phy_device *phydev);
873int genphy_restart_aneg(struct phy_device *phydev);
874int genphy_config_aneg(struct phy_device *phydev);
875int genphy_aneg_done(struct phy_device *phydev);
876int genphy_update_link(struct phy_device *phydev);
877int genphy_read_status(struct phy_device *phydev);
878int genphy_suspend(struct phy_device *phydev);
879int genphy_resume(struct phy_device *phydev);
880int genphy_loopback(struct phy_device *phydev, bool enable);
881int genphy_soft_reset(struct phy_device *phydev);
882static inline int genphy_no_soft_reset(struct phy_device *phydev)
883{
884 return 0;
885}
886int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad,
887 u16 regnum);
888int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
889 u16 regnum, u16 val);
890
891/* Clause 45 PHY */
892int genphy_c45_restart_aneg(struct phy_device *phydev);
893int genphy_c45_aneg_done(struct phy_device *phydev);
894int genphy_c45_read_link(struct phy_device *phydev, u32 mmd_mask);
895int genphy_c45_read_lpa(struct phy_device *phydev);
896int genphy_c45_read_pma(struct phy_device *phydev);
897int genphy_c45_pma_setup_forced(struct phy_device *phydev);
898int genphy_c45_an_disable_aneg(struct phy_device *phydev);
899
900void phy_driver_unregister(struct phy_driver *drv);
901void phy_drivers_unregister(struct phy_driver *drv, int n);
902int phy_driver_register(struct phy_driver *new_driver, struct module *owner);
903int phy_drivers_register(struct phy_driver *new_driver, int n,
904 struct module *owner);
905void phy_state_machine(struct work_struct *work);
906void phy_change_work(struct work_struct *work);
907void phy_mac_interrupt(struct phy_device *phydev, int new_link);
908void phy_start_machine(struct phy_device *phydev);
909void phy_stop_machine(struct phy_device *phydev);
910void phy_trigger_machine(struct phy_device *phydev, bool sync);
911int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd);
912void phy_ethtool_ksettings_get(struct phy_device *phydev,
913 struct ethtool_link_ksettings *cmd);
914int phy_ethtool_ksettings_set(struct phy_device *phydev,
915 const struct ethtool_link_ksettings *cmd);
916int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
917int phy_start_interrupts(struct phy_device *phydev);
918void phy_print_status(struct phy_device *phydev);
919int phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
920
921int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
922 int (*run)(struct phy_device *));
923int phy_register_fixup_for_id(const char *bus_id,
924 int (*run)(struct phy_device *));
925int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
926 int (*run)(struct phy_device *));
927
928int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask);
929int phy_unregister_fixup_for_id(const char *bus_id);
930int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask);
931
932int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable);
933int phy_get_eee_err(struct phy_device *phydev);
934int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data);
935int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data);
936int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
937void phy_ethtool_get_wol(struct phy_device *phydev,
938 struct ethtool_wolinfo *wol);
939int phy_ethtool_get_link_ksettings(struct net_device *ndev,
940 struct ethtool_link_ksettings *cmd);
941int phy_ethtool_set_link_ksettings(struct net_device *ndev,
942 const struct ethtool_link_ksettings *cmd);
943int phy_ethtool_nway_reset(struct net_device *ndev);
944
945#if IS_ENABLED(CONFIG_PHYLIB)
946int __init mdio_bus_init(void);
947void mdio_bus_exit(void);
948#endif
949
950extern struct bus_type mdio_bus_type;
951
952struct mdio_board_info {
953 const char *bus_id;
954 char modalias[MDIO_NAME_SIZE];
955 int mdio_addr;
956 const void *platform_data;
957};
958
959#if IS_ENABLED(CONFIG_MDIO_DEVICE)
960int mdiobus_register_board_info(const struct mdio_board_info *info,
961 unsigned int n);
962#else
963static inline int mdiobus_register_board_info(const struct mdio_board_info *i,
964 unsigned int n)
965{
966 return 0;
967}
968#endif
969
970
971/**
972 * module_phy_driver() - Helper macro for registering PHY drivers
973 * @__phy_drivers: array of PHY drivers to register
974 *
975 * Helper macro for PHY drivers which do not do anything special in module
976 * init/exit. Each module may only use this macro once, and calling it
977 * replaces module_init() and module_exit().
978 */
979#define phy_module_driver(__phy_drivers, __count) \
980static int __init phy_module_init(void) \
981{ \
982 return phy_drivers_register(__phy_drivers, __count, THIS_MODULE); \
983} \
984module_init(phy_module_init); \
985static void __exit phy_module_exit(void) \
986{ \
987 phy_drivers_unregister(__phy_drivers, __count); \
988} \
989module_exit(phy_module_exit)
990
991#define module_phy_driver(__phy_drivers) \
992 phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers))
993
994#endif /* __PHY_H */