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