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