1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Framework and drivers for configuring and reading different PHYs
4 * Based on code in sungem_phy.c and (long-removed) gianfar_phy.c
5 *
6 * Author: Andy Fleming
7 *
8 * Copyright (c) 2004 Freescale Semiconductor, Inc.
9 */
10
11 #ifndef __PHY_H
12 #define __PHY_H
13
14 #include <linux/compiler.h>
15 #include <linux/spinlock.h>
16 #include <linux/ethtool.h>
17 #include <linux/linkmode.h>
18 #include <linux/mdio.h>
19 #include <linux/mii.h>
20 #include <linux/module.h>
21 #include <linux/timer.h>
22 #include <linux/workqueue.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/android_kabi.h>
25
26 #include <linux/atomic.h>
27
28 #define PHY_DEFAULT_FEATURES (SUPPORTED_Autoneg | \
29 SUPPORTED_TP | \
30 SUPPORTED_MII)
31
32 #define PHY_10BT_FEATURES (SUPPORTED_10baseT_Half | \
33 SUPPORTED_10baseT_Full)
34
35 #define PHY_100BT_FEATURES (SUPPORTED_100baseT_Half | \
36 SUPPORTED_100baseT_Full)
37
38 #define PHY_1000BT_FEATURES (SUPPORTED_1000baseT_Half | \
39 SUPPORTED_1000baseT_Full)
40
41 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
42 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
43 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
44 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
45 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
46 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
47 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
48 extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
49
50 #define PHY_BASIC_FEATURES ((unsigned long *)&phy_basic_features)
51 #define PHY_BASIC_T1_FEATURES ((unsigned long *)&phy_basic_t1_features)
52 #define PHY_GBIT_FEATURES ((unsigned long *)&phy_gbit_features)
53 #define PHY_GBIT_FIBRE_FEATURES ((unsigned long *)&phy_gbit_fibre_features)
54 #define PHY_GBIT_ALL_PORTS_FEATURES ((unsigned long *)&phy_gbit_all_ports_features)
55 #define PHY_10GBIT_FEATURES ((unsigned long *)&phy_10gbit_features)
56 #define PHY_10GBIT_FEC_FEATURES ((unsigned long *)&phy_10gbit_fec_features)
57 #define PHY_10GBIT_FULL_FEATURES ((unsigned long *)&phy_10gbit_full_features)
58
59 extern const int phy_basic_ports_array[3];
60 extern const int phy_fibre_port_array[1];
61 extern const int phy_all_ports_features_array[7];
62 extern const int phy_10_100_features_array[4];
63 extern const int phy_basic_t1_features_array[2];
64 extern const int phy_gbit_features_array[2];
65 extern const int phy_10gbit_features_array[1];
66
67 /*
68 * Set phydev->irq to PHY_POLL if interrupts are not supported,
69 * or not desired for this PHY. Set to PHY_IGNORE_INTERRUPT if
70 * the attached driver handles the interrupt
71 */
72 #define PHY_POLL -1
73 #define PHY_IGNORE_INTERRUPT -2
74
75 #define PHY_IS_INTERNAL 0x00000001
76 #define PHY_RST_AFTER_CLK_EN 0x00000002
77 #define MDIO_DEVICE_IS_PHY 0x80000000
78
79 /* Interface Mode definitions */
80 typedef enum {
81 PHY_INTERFACE_MODE_NA,
82 PHY_INTERFACE_MODE_INTERNAL,
83 PHY_INTERFACE_MODE_MII,
84 PHY_INTERFACE_MODE_GMII,
85 PHY_INTERFACE_MODE_SGMII,
86 PHY_INTERFACE_MODE_TBI,
87 PHY_INTERFACE_MODE_REVMII,
88 PHY_INTERFACE_MODE_RMII,
89 PHY_INTERFACE_MODE_RGMII,
90 PHY_INTERFACE_MODE_RGMII_ID,
91 PHY_INTERFACE_MODE_RGMII_RXID,
92 PHY_INTERFACE_MODE_RGMII_TXID,
93 PHY_INTERFACE_MODE_RTBI,
94 PHY_INTERFACE_MODE_SMII,
95 PHY_INTERFACE_MODE_XGMII,
96 PHY_INTERFACE_MODE_MOCA,
97 PHY_INTERFACE_MODE_QSGMII,
98 PHY_INTERFACE_MODE_TRGMII,
99 PHY_INTERFACE_MODE_1000BASEX,
100 PHY_INTERFACE_MODE_2500BASEX,
101 PHY_INTERFACE_MODE_RXAUI,
102 PHY_INTERFACE_MODE_XAUI,
103 /* 10GBASE-KR, XFI, SFI - single lane 10G Serdes */
104 PHY_INTERFACE_MODE_10GKR,
105 PHY_INTERFACE_MODE_USXGMII,
106 PHY_INTERFACE_MODE_MAX,
107 } phy_interface_t;
108
109 /**
110 * phy_supported_speeds - return all speeds currently supported by a phy device
111 * @phy: The phy device to return supported speeds of.
112 * @speeds: buffer to store supported speeds in.
113 * @size: size of speeds buffer.
114 *
115 * Description: Returns the number of supported speeds, and fills
116 * the speeds buffer with the supported speeds. If speeds buffer is
117 * too small to contain all currently supported speeds, will return as
118 * many speeds as can fit.
119 */
120 unsigned int phy_supported_speeds(struct phy_device *phy,
121 unsigned int *speeds,
122 unsigned int size);
123
124 /**
125 * phy_modes - map phy_interface_t enum to device tree binding of phy-mode
126 * @interface: enum phy_interface_t value
127 *
128 * Description: maps 'enum phy_interface_t' defined in this file
129 * into the device tree binding of 'phy-mode', so that Ethernet
130 * device driver can get phy interface from device tree.
131 */
phy_modes(phy_interface_t interface)132 static inline const char *phy_modes(phy_interface_t interface)
133 {
134 switch (interface) {
135 case PHY_INTERFACE_MODE_NA:
136 return "";
137 case PHY_INTERFACE_MODE_INTERNAL:
138 return "internal";
139 case PHY_INTERFACE_MODE_MII:
140 return "mii";
141 case PHY_INTERFACE_MODE_GMII:
142 return "gmii";
143 case PHY_INTERFACE_MODE_SGMII:
144 return "sgmii";
145 case PHY_INTERFACE_MODE_TBI:
146 return "tbi";
147 case PHY_INTERFACE_MODE_REVMII:
148 return "rev-mii";
149 case PHY_INTERFACE_MODE_RMII:
150 return "rmii";
151 case PHY_INTERFACE_MODE_RGMII:
152 return "rgmii";
153 case PHY_INTERFACE_MODE_RGMII_ID:
154 return "rgmii-id";
155 case PHY_INTERFACE_MODE_RGMII_RXID:
156 return "rgmii-rxid";
157 case PHY_INTERFACE_MODE_RGMII_TXID:
158 return "rgmii-txid";
159 case PHY_INTERFACE_MODE_RTBI:
160 return "rtbi";
161 case PHY_INTERFACE_MODE_SMII:
162 return "smii";
163 case PHY_INTERFACE_MODE_XGMII:
164 return "xgmii";
165 case PHY_INTERFACE_MODE_MOCA:
166 return "moca";
167 case PHY_INTERFACE_MODE_QSGMII:
168 return "qsgmii";
169 case PHY_INTERFACE_MODE_TRGMII:
170 return "trgmii";
171 case PHY_INTERFACE_MODE_1000BASEX:
172 return "1000base-x";
173 case PHY_INTERFACE_MODE_2500BASEX:
174 return "2500base-x";
175 case PHY_INTERFACE_MODE_RXAUI:
176 return "rxaui";
177 case PHY_INTERFACE_MODE_XAUI:
178 return "xaui";
179 case PHY_INTERFACE_MODE_10GKR:
180 return "10gbase-kr";
181 case PHY_INTERFACE_MODE_USXGMII:
182 return "usxgmii";
183 default:
184 return "unknown";
185 }
186 }
187
188
189 #define PHY_INIT_TIMEOUT 100000
190 #define PHY_FORCE_TIMEOUT 10
191
192 #define PHY_MAX_ADDR 32
193
194 /* Used when trying to connect to a specific phy (mii bus id:phy device id) */
195 #define PHY_ID_FMT "%s:%02x"
196
197 #define MII_BUS_ID_SIZE 61
198
199 /* Or MII_ADDR_C45 into regnum for read/write on mii_bus to enable the 21 bit
200 IEEE 802.3ae clause 45 addressing mode used by 10GIGE phy chips. */
201 #define MII_ADDR_C45 (1<<30)
202 #define MII_DEVADDR_C45_SHIFT 16
203 #define MII_REGADDR_C45_MASK GENMASK(15, 0)
204
205 struct device;
206 struct phylink;
207 struct sk_buff;
208
209 /*
210 * The Bus class for PHYs. Devices which provide access to
211 * PHYs should register using this structure
212 */
213 struct mii_bus {
214 struct module *owner;
215 const char *name;
216 char id[MII_BUS_ID_SIZE];
217 void *priv;
218 int (*read)(struct mii_bus *bus, int addr, int regnum);
219 int (*write)(struct mii_bus *bus, int addr, int regnum, u16 val);
220 int (*reset)(struct mii_bus *bus);
221
222 /*
223 * A lock to ensure that only one thing can read/write
224 * the MDIO bus at a time
225 */
226 struct mutex mdio_lock;
227
228 struct device *parent;
229 enum {
230 MDIOBUS_ALLOCATED = 1,
231 MDIOBUS_REGISTERED,
232 MDIOBUS_UNREGISTERED,
233 MDIOBUS_RELEASED,
234 } state;
235 struct device dev;
236
237 /* list of all PHYs on bus */
238 struct mdio_device *mdio_map[PHY_MAX_ADDR];
239
240 /* PHY addresses to be ignored when probing */
241 u32 phy_mask;
242
243 /* PHY addresses to ignore the TA/read failure */
244 u32 phy_ignore_ta_mask;
245
246 /*
247 * An array of interrupts, each PHY's interrupt at the index
248 * matching its address
249 */
250 int irq[PHY_MAX_ADDR];
251
252 /* GPIO reset pulse width in microseconds */
253 int reset_delay_us;
254 /* RESET GPIO descriptor pointer */
255 struct gpio_desc *reset_gpiod;
256 };
257 #define to_mii_bus(d) container_of(d, struct mii_bus, dev)
258
259 struct mii_bus *mdiobus_alloc_size(size_t);
mdiobus_alloc(void)260 static inline struct mii_bus *mdiobus_alloc(void)
261 {
262 return mdiobus_alloc_size(0);
263 }
264
265 int __mdiobus_register(struct mii_bus *bus, struct module *owner);
266 #define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
267 void mdiobus_unregister(struct mii_bus *bus);
268 void mdiobus_free(struct mii_bus *bus);
269 struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv);
devm_mdiobus_alloc(struct device * dev)270 static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
271 {
272 return devm_mdiobus_alloc_size(dev, 0);
273 }
274
275 void devm_mdiobus_free(struct device *dev, struct mii_bus *bus);
276 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
277
278 #define PHY_INTERRUPT_DISABLED false
279 #define PHY_INTERRUPT_ENABLED true
280
281 /* PHY state machine states:
282 *
283 * DOWN: PHY device and driver are not ready for anything. probe
284 * should be called if and only if the PHY is in this state,
285 * given that the PHY device exists.
286 * - PHY driver probe function will set the state to READY
287 *
288 * READY: PHY is ready to send and receive packets, but the
289 * controller is not. By default, PHYs which do not implement
290 * probe will be set to this state by phy_probe().
291 * - start will set the state to UP
292 *
293 * UP: The PHY and attached device are ready to do work.
294 * Interrupts should be started here.
295 * - timer moves to NOLINK or RUNNING
296 *
297 * NOLINK: PHY is up, but not currently plugged in.
298 * - irq or timer will set RUNNING if link comes back
299 * - phy_stop moves to HALTED
300 *
301 * RUNNING: PHY is currently up, running, and possibly sending
302 * and/or receiving packets
303 * - irq or timer will set NOLINK if link goes down
304 * - phy_stop moves to HALTED
305 *
306 * HALTED: PHY is up, but no polling or interrupts are done. Or
307 * PHY is in an error state.
308 * - phy_start moves to UP
309 */
310 enum phy_state {
311 PHY_DOWN = 0,
312 PHY_READY,
313 PHY_HALTED,
314 PHY_UP,
315 PHY_RUNNING,
316 PHY_NOLINK,
317 };
318
319 /**
320 * struct phy_c45_device_ids - 802.3-c45 Device Identifiers
321 * @devices_in_package: Bit vector of devices present.
322 * @device_ids: The device identifer for each present device.
323 */
324 struct phy_c45_device_ids {
325 u32 devices_in_package;
326 u32 device_ids[8];
327 };
328
329 /* phy_device: An instance of a PHY
330 *
331 * drv: Pointer to the driver for this PHY instance
332 * phy_id: UID for this device found during discovery
333 * c45_ids: 802.3-c45 Device Identifers if is_c45.
334 * is_c45: Set to true if this phy uses clause 45 addressing.
335 * is_internal: Set to true if this phy is internal to a MAC.
336 * is_pseudo_fixed_link: Set to true if this phy is an Ethernet switch, etc.
337 * is_gigabit_capable: Set to true if PHY supports 1000Mbps
338 * has_fixups: Set to true if this phy has fixups/quirks.
339 * suspended: Set to true if this phy has been suspended successfully.
340 * suspended_by_mdio_bus: Set to true if this phy was suspended by MDIO bus.
341 * sysfs_links: Internal boolean tracking sysfs symbolic links setup/removal.
342 * loopback_enabled: Set true if this phy has been loopbacked successfully.
343 * state: state of the PHY for management purposes
344 * dev_flags: Device-specific flags used by the PHY driver.
345 * irq: IRQ number of the PHY's interrupt (-1 if none)
346 * phy_timer: The timer for handling the state machine
347 * attached_dev: The attached enet driver's device instance ptr
348 * adjust_link: Callback for the enet controller to respond to
349 * changes in the link state.
350 *
351 * speed, duplex, pause, supported, advertising, lp_advertising,
352 * and autoneg are used like in mii_if_info
353 *
354 * interrupts currently only supports enabled or disabled,
355 * but could be changed in the future to support enabling
356 * and disabling specific interrupts
357 *
358 * Contains some infrastructure for polling and interrupt
359 * handling, as well as handling shifts in PHY hardware state
360 */
361 struct phy_device {
362 struct mdio_device mdio;
363
364 /* Information about the PHY type */
365 /* And management functions */
366 struct phy_driver *drv;
367
368 u32 phy_id;
369
370 struct phy_c45_device_ids c45_ids;
371 unsigned is_c45:1;
372 unsigned is_internal:1;
373 unsigned is_pseudo_fixed_link:1;
374 unsigned is_gigabit_capable:1;
375 unsigned has_fixups:1;
376 unsigned suspended:1;
377 unsigned suspended_by_mdio_bus:1;
378 unsigned sysfs_links:1;
379 unsigned loopback_enabled:1;
380
381 unsigned autoneg:1;
382 /* The most recently read link state */
383 unsigned link:1;
384 unsigned autoneg_complete:1;
385
386 /* Interrupts are enabled */
387 unsigned interrupts:1;
388
389 enum phy_state state;
390
391 u32 dev_flags;
392
393 phy_interface_t interface;
394
395 /*
396 * forced speed & duplex (no autoneg)
397 * partner speed & duplex & pause (autoneg)
398 */
399 int speed;
400 int duplex;
401 int pause;
402 int asym_pause;
403
404 /* Union of PHY and Attached devices' supported link modes */
405 /* See ethtool.h for more info */
406 __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
407 __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
408 __ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
409 /* used with phy_speed_down */
410 __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old);
411
412 /* Energy efficient ethernet modes which should be prohibited */
413 u32 eee_broken_modes;
414
415 #ifdef CONFIG_LED_TRIGGER_PHY
416 struct phy_led_trigger *phy_led_triggers;
417 unsigned int phy_num_led_triggers;
418 struct phy_led_trigger *last_triggered;
419
420 struct phy_led_trigger *led_link_trigger;
421 #endif
422
423 /*
424 * Interrupt number for this PHY
425 * -1 means no interrupt
426 */
427 int irq;
428
429 /* private data pointer */
430 /* For use by PHYs to maintain extra state */
431 void *priv;
432
433 /* Interrupt and Polling infrastructure */
434 struct delayed_work state_queue;
435
436 struct mutex lock;
437
438 struct phylink *phylink;
439 struct net_device *attached_dev;
440
441 u8 mdix;
442 u8 mdix_ctrl;
443
444 void (*phy_link_change)(struct phy_device *, bool up, bool do_carrier);
445 void (*adjust_link)(struct net_device *dev);
446
447 ANDROID_KABI_RESERVE(1);
448 ANDROID_KABI_RESERVE(2);
449 ANDROID_KABI_RESERVE(3);
450 ANDROID_KABI_RESERVE(4);
451 };
452 #define to_phy_device(d) container_of(to_mdio_device(d), \
453 struct phy_device, mdio)
454
455 /* struct phy_driver: Driver structure for a particular PHY type
456 *
457 * driver_data: static driver data
458 * phy_id: The result of reading the UID registers of this PHY
459 * type, and ANDing them with the phy_id_mask. This driver
460 * only works for PHYs with IDs which match this field
461 * name: The friendly name of this PHY type
462 * phy_id_mask: Defines the important bits of the phy_id
463 * features: A mandatory list of features (speed, duplex, etc)
464 * supported by this PHY
465 * flags: A bitfield defining certain other features this PHY
466 * supports (like interrupts)
467 *
468 * All functions are optional. If config_aneg or read_status
469 * are not implemented, the phy core uses the genphy versions.
470 * Note that none of these functions should be called from
471 * interrupt time. The goal is for the bus read/write functions
472 * to be able to block when the bus transaction is happening,
473 * and be freed up by an interrupt (The MPC85xx has this ability,
474 * though it is not currently supported in the driver).
475 */
476 struct phy_driver {
477 struct mdio_driver_common mdiodrv;
478 u32 phy_id;
479 char *name;
480 u32 phy_id_mask;
481 const unsigned long * const features;
482 u32 flags;
483 const void *driver_data;
484
485 /*
486 * Called to issue a PHY software reset
487 */
488 int (*soft_reset)(struct phy_device *phydev);
489
490 /*
491 * Called to initialize the PHY,
492 * including after a reset
493 */
494 int (*config_init)(struct phy_device *phydev);
495
496 /*
497 * Called during discovery. Used to set
498 * up device-specific structures, if any
499 */
500 int (*probe)(struct phy_device *phydev);
501
502 /*
503 * Probe the hardware to determine what abilities it has.
504 * Should only set phydev->supported.
505 */
506 int (*get_features)(struct phy_device *phydev);
507
508 /* PHY Power Management */
509 int (*suspend)(struct phy_device *phydev);
510 int (*resume)(struct phy_device *phydev);
511
512 /*
513 * Configures the advertisement and resets
514 * autonegotiation if phydev->autoneg is on,
515 * forces the speed to the current settings in phydev
516 * if phydev->autoneg is off
517 */
518 int (*config_aneg)(struct phy_device *phydev);
519
520 /* Determines the auto negotiation result */
521 int (*aneg_done)(struct phy_device *phydev);
522
523 /* Determines the negotiated speed and duplex */
524 int (*read_status)(struct phy_device *phydev);
525
526 /* Clears any pending interrupts */
527 int (*ack_interrupt)(struct phy_device *phydev);
528
529 /* Enables or disables interrupts */
530 int (*config_intr)(struct phy_device *phydev);
531
532 /*
533 * Checks if the PHY generated an interrupt.
534 * For multi-PHY devices with shared PHY interrupt pin
535 * Set interrupt bits have to be cleared.
536 */
537 int (*did_interrupt)(struct phy_device *phydev);
538
539 /* Override default interrupt handling */
540 int (*handle_interrupt)(struct phy_device *phydev);
541
542 /* Clears up any memory if needed */
543 void (*remove)(struct phy_device *phydev);
544
545 /* Returns true if this is a suitable driver for the given
546 * phydev. If NULL, matching is based on phy_id and
547 * phy_id_mask.
548 */
549 int (*match_phy_device)(struct phy_device *phydev);
550
551 /* Handles ethtool queries for hardware time stamping. */
552 int (*ts_info)(struct phy_device *phydev, struct ethtool_ts_info *ti);
553
554 /* Handles SIOCSHWTSTAMP ioctl for hardware time stamping. */
555 int (*hwtstamp)(struct phy_device *phydev, struct ifreq *ifr);
556
557 /*
558 * Requests a Rx timestamp for 'skb'. If the skb is accepted,
559 * the phy driver promises to deliver it using netif_rx() as
560 * soon as a timestamp becomes available. One of the
561 * PTP_CLASS_ values is passed in 'type'. The function must
562 * return true if the skb is accepted for delivery.
563 */
564 bool (*rxtstamp)(struct phy_device *dev, struct sk_buff *skb, int type);
565
566 /*
567 * Requests a Tx timestamp for 'skb'. The phy driver promises
568 * to deliver it using skb_complete_tx_timestamp() as soon as a
569 * timestamp becomes available. One of the PTP_CLASS_ values
570 * is passed in 'type'.
571 */
572 void (*txtstamp)(struct phy_device *dev, struct sk_buff *skb, int type);
573
574 /* Some devices (e.g. qnap TS-119P II) require PHY register changes to
575 * enable Wake on LAN, so set_wol is provided to be called in the
576 * ethernet driver's set_wol function. */
577 int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
578
579 /* See set_wol, but for checking whether Wake on LAN is enabled. */
580 void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
581
582 /*
583 * Called to inform a PHY device driver when the core is about to
584 * change the link state. This callback is supposed to be used as
585 * fixup hook for drivers that need to take action when the link
586 * state changes. Drivers are by no means allowed to mess with the
587 * PHY device structure in their implementations.
588 */
589 void (*link_change_notify)(struct phy_device *dev);
590
591 /*
592 * Phy specific driver override for reading a MMD register.
593 * This function is optional for PHY specific drivers. When
594 * not provided, the default MMD read function will be used
595 * by phy_read_mmd(), which will use either a direct read for
596 * Clause 45 PHYs or an indirect read for Clause 22 PHYs.
597 * devnum is the MMD device number within the PHY device,
598 * regnum is the register within the selected MMD device.
599 */
600 int (*read_mmd)(struct phy_device *dev, int devnum, u16 regnum);
601
602 /*
603 * Phy specific driver override for writing a MMD register.
604 * This function is optional for PHY specific drivers. When
605 * not provided, the default MMD write function will be used
606 * by phy_write_mmd(), which will use either a direct write for
607 * Clause 45 PHYs, or an indirect write for Clause 22 PHYs.
608 * devnum is the MMD device number within the PHY device,
609 * regnum is the register within the selected MMD device.
610 * val is the value to be written.
611 */
612 int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum,
613 u16 val);
614
615 int (*read_page)(struct phy_device *dev);
616 int (*write_page)(struct phy_device *dev, int page);
617
618 /* Get the size and type of the eeprom contained within a plug-in
619 * module */
620 int (*module_info)(struct phy_device *dev,
621 struct ethtool_modinfo *modinfo);
622
623 /* Get the eeprom information from the plug-in module */
624 int (*module_eeprom)(struct phy_device *dev,
625 struct ethtool_eeprom *ee, u8 *data);
626
627 /* Get statistics from the phy using ethtool */
628 int (*get_sset_count)(struct phy_device *dev);
629 void (*get_strings)(struct phy_device *dev, u8 *data);
630 void (*get_stats)(struct phy_device *dev,
631 struct ethtool_stats *stats, u64 *data);
632
633 /* Get and Set PHY tunables */
634 int (*get_tunable)(struct phy_device *dev,
635 struct ethtool_tunable *tuna, void *data);
636 int (*set_tunable)(struct phy_device *dev,
637 struct ethtool_tunable *tuna,
638 const void *data);
639 int (*set_loopback)(struct phy_device *dev, bool enable);
640
641 ANDROID_KABI_RESERVE(1);
642 ANDROID_KABI_RESERVE(2);
643
644 };
645 #define to_phy_driver(d) container_of(to_mdio_common_driver(d), \
646 struct phy_driver, mdiodrv)
647
648 #define PHY_ANY_ID "MATCH ANY PHY"
649 #define PHY_ANY_UID 0xffffffff
650
651 #define PHY_ID_MATCH_EXACT(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 0)
652 #define PHY_ID_MATCH_MODEL(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 4)
653 #define PHY_ID_MATCH_VENDOR(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 10)
654
655 /* A Structure for boards to register fixups with the PHY Lib */
656 struct phy_fixup {
657 struct list_head list;
658 char bus_id[MII_BUS_ID_SIZE + 3];
659 u32 phy_uid;
660 u32 phy_uid_mask;
661 int (*run)(struct phy_device *phydev);
662 };
663
664 const char *phy_speed_to_str(int speed);
665 const char *phy_duplex_to_str(unsigned int duplex);
666
667 /* A structure for mapping a particular speed and duplex
668 * combination to a particular SUPPORTED and ADVERTISED value
669 */
670 struct phy_setting {
671 u32 speed;
672 u8 duplex;
673 u8 bit;
674 };
675
676 const struct phy_setting *
677 phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
678 bool exact);
679 size_t phy_speeds(unsigned int *speeds, size_t size,
680 unsigned long *mask);
681 void of_set_phy_supported(struct phy_device *phydev);
682 void of_set_phy_eee_broken(struct phy_device *phydev);
683 int phy_speed_down_core(struct phy_device *phydev);
684
685 /**
686 * phy_is_started - Convenience function to check whether PHY is started
687 * @phydev: The phy_device struct
688 */
phy_is_started(struct phy_device * phydev)689 static inline bool phy_is_started(struct phy_device *phydev)
690 {
691 return phydev->state >= PHY_UP;
692 }
693
694 void phy_resolve_aneg_pause(struct phy_device *phydev);
695 void phy_resolve_aneg_linkmode(struct phy_device *phydev);
696
697 /**
698 * phy_read - Convenience function for reading a given PHY register
699 * @phydev: the phy_device struct
700 * @regnum: register number to read
701 *
702 * NOTE: MUST NOT be called from interrupt context,
703 * because the bus read/write functions may wait for an interrupt
704 * to conclude the operation.
705 */
phy_read(struct phy_device * phydev,u32 regnum)706 static inline int phy_read(struct phy_device *phydev, u32 regnum)
707 {
708 return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
709 }
710
711 /**
712 * __phy_read - convenience function for reading a given PHY register
713 * @phydev: the phy_device struct
714 * @regnum: register number to read
715 *
716 * The caller must have taken the MDIO bus lock.
717 */
__phy_read(struct phy_device * phydev,u32 regnum)718 static inline int __phy_read(struct phy_device *phydev, u32 regnum)
719 {
720 return __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
721 }
722
723 /**
724 * phy_write - Convenience function for writing a given PHY register
725 * @phydev: the phy_device struct
726 * @regnum: register number to write
727 * @val: value to write to @regnum
728 *
729 * NOTE: MUST NOT be called from interrupt context,
730 * because the bus read/write functions may wait for an interrupt
731 * to conclude the operation.
732 */
phy_write(struct phy_device * phydev,u32 regnum,u16 val)733 static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
734 {
735 return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val);
736 }
737
738 /**
739 * __phy_write - Convenience function for writing a given PHY register
740 * @phydev: the phy_device struct
741 * @regnum: register number to write
742 * @val: value to write to @regnum
743 *
744 * The caller must have taken the MDIO bus lock.
745 */
__phy_write(struct phy_device * phydev,u32 regnum,u16 val)746 static inline int __phy_write(struct phy_device *phydev, u32 regnum, u16 val)
747 {
748 return __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum,
749 val);
750 }
751
752 /**
753 * phy_read_mmd - Convenience function for reading a register
754 * from an MMD on a given PHY.
755 * @phydev: The phy_device struct
756 * @devad: The MMD to read from
757 * @regnum: The register on the MMD to read
758 *
759 * Same rules as for phy_read();
760 */
761 int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
762
763 /**
764 * __phy_read_mmd - Convenience function for reading a register
765 * from an MMD on a given PHY.
766 * @phydev: The phy_device struct
767 * @devad: The MMD to read from
768 * @regnum: The register on the MMD to read
769 *
770 * Same rules as for __phy_read();
771 */
772 int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
773
774 /**
775 * phy_write_mmd - Convenience function for writing a register
776 * on an MMD on a given PHY.
777 * @phydev: The phy_device struct
778 * @devad: The MMD to write to
779 * @regnum: The register on the MMD to read
780 * @val: value to write to @regnum
781 *
782 * Same rules as for phy_write();
783 */
784 int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
785
786 /**
787 * __phy_write_mmd - Convenience function for writing a register
788 * on an MMD on a given PHY.
789 * @phydev: The phy_device struct
790 * @devad: The MMD to write to
791 * @regnum: The register on the MMD to read
792 * @val: value to write to @regnum
793 *
794 * Same rules as for __phy_write();
795 */
796 int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
797
798 int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
799 u16 set);
800 int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
801 u16 set);
802 int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
803 int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
804
805 int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
806 u16 mask, u16 set);
807 int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
808 u16 mask, u16 set);
809 int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
810 u16 mask, u16 set);
811 int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
812 u16 mask, u16 set);
813
814 /**
815 * __phy_set_bits - Convenience function for setting bits in a PHY register
816 * @phydev: the phy_device struct
817 * @regnum: register number to write
818 * @val: bits to set
819 *
820 * The caller must have taken the MDIO bus lock.
821 */
__phy_set_bits(struct phy_device * phydev,u32 regnum,u16 val)822 static inline int __phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
823 {
824 return __phy_modify(phydev, regnum, 0, val);
825 }
826
827 /**
828 * __phy_clear_bits - Convenience function for clearing bits in a PHY register
829 * @phydev: the phy_device struct
830 * @regnum: register number to write
831 * @val: bits to clear
832 *
833 * The caller must have taken the MDIO bus lock.
834 */
__phy_clear_bits(struct phy_device * phydev,u32 regnum,u16 val)835 static inline int __phy_clear_bits(struct phy_device *phydev, u32 regnum,
836 u16 val)
837 {
838 return __phy_modify(phydev, regnum, val, 0);
839 }
840
841 /**
842 * phy_set_bits - Convenience function for setting bits in a PHY register
843 * @phydev: the phy_device struct
844 * @regnum: register number to write
845 * @val: bits to set
846 */
phy_set_bits(struct phy_device * phydev,u32 regnum,u16 val)847 static inline int phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
848 {
849 return phy_modify(phydev, regnum, 0, val);
850 }
851
852 /**
853 * phy_clear_bits - Convenience function for clearing bits in a PHY register
854 * @phydev: the phy_device struct
855 * @regnum: register number to write
856 * @val: bits to clear
857 */
phy_clear_bits(struct phy_device * phydev,u32 regnum,u16 val)858 static inline int phy_clear_bits(struct phy_device *phydev, u32 regnum, u16 val)
859 {
860 return phy_modify(phydev, regnum, val, 0);
861 }
862
863 /**
864 * __phy_set_bits_mmd - Convenience function for setting bits in a register
865 * on MMD
866 * @phydev: the phy_device struct
867 * @devad: the MMD containing register to modify
868 * @regnum: register number to modify
869 * @val: bits to set
870 *
871 * The caller must have taken the MDIO bus lock.
872 */
__phy_set_bits_mmd(struct phy_device * phydev,int devad,u32 regnum,u16 val)873 static inline int __phy_set_bits_mmd(struct phy_device *phydev, int devad,
874 u32 regnum, u16 val)
875 {
876 return __phy_modify_mmd(phydev, devad, regnum, 0, val);
877 }
878
879 /**
880 * __phy_clear_bits_mmd - Convenience function for clearing bits in a register
881 * on MMD
882 * @phydev: the phy_device struct
883 * @devad: the MMD containing register to modify
884 * @regnum: register number to modify
885 * @val: bits to clear
886 *
887 * The caller must have taken the MDIO bus lock.
888 */
__phy_clear_bits_mmd(struct phy_device * phydev,int devad,u32 regnum,u16 val)889 static inline int __phy_clear_bits_mmd(struct phy_device *phydev, int devad,
890 u32 regnum, u16 val)
891 {
892 return __phy_modify_mmd(phydev, devad, regnum, val, 0);
893 }
894
895 /**
896 * phy_set_bits_mmd - Convenience function for setting bits in a register
897 * on MMD
898 * @phydev: the phy_device struct
899 * @devad: the MMD containing register to modify
900 * @regnum: register number to modify
901 * @val: bits to set
902 */
phy_set_bits_mmd(struct phy_device * phydev,int devad,u32 regnum,u16 val)903 static inline int phy_set_bits_mmd(struct phy_device *phydev, int devad,
904 u32 regnum, u16 val)
905 {
906 return phy_modify_mmd(phydev, devad, regnum, 0, val);
907 }
908
909 /**
910 * phy_clear_bits_mmd - Convenience function for clearing bits in a register
911 * on MMD
912 * @phydev: the phy_device struct
913 * @devad: the MMD containing register to modify
914 * @regnum: register number to modify
915 * @val: bits to clear
916 */
phy_clear_bits_mmd(struct phy_device * phydev,int devad,u32 regnum,u16 val)917 static inline int phy_clear_bits_mmd(struct phy_device *phydev, int devad,
918 u32 regnum, u16 val)
919 {
920 return phy_modify_mmd(phydev, devad, regnum, val, 0);
921 }
922
923 /**
924 * phy_interrupt_is_valid - Convenience function for testing a given PHY irq
925 * @phydev: the phy_device struct
926 *
927 * NOTE: must be kept in sync with addition/removal of PHY_POLL and
928 * PHY_IGNORE_INTERRUPT
929 */
phy_interrupt_is_valid(struct phy_device * phydev)930 static inline bool phy_interrupt_is_valid(struct phy_device *phydev)
931 {
932 return phydev->irq != PHY_POLL && phydev->irq != PHY_IGNORE_INTERRUPT;
933 }
934
935 /**
936 * phy_polling_mode - Convenience function for testing whether polling is
937 * used to detect PHY status changes
938 * @phydev: the phy_device struct
939 */
phy_polling_mode(struct phy_device * phydev)940 static inline bool phy_polling_mode(struct phy_device *phydev)
941 {
942 return phydev->irq == PHY_POLL;
943 }
944
945 /**
946 * phy_is_internal - Convenience function for testing if a PHY is internal
947 * @phydev: the phy_device struct
948 */
phy_is_internal(struct phy_device * phydev)949 static inline bool phy_is_internal(struct phy_device *phydev)
950 {
951 return phydev->is_internal;
952 }
953
954 /**
955 * phy_interface_mode_is_rgmii - Convenience function for testing if a
956 * PHY interface mode is RGMII (all variants)
957 * @mode: the phy_interface_t enum
958 */
phy_interface_mode_is_rgmii(phy_interface_t mode)959 static inline bool phy_interface_mode_is_rgmii(phy_interface_t mode)
960 {
961 return mode >= PHY_INTERFACE_MODE_RGMII &&
962 mode <= PHY_INTERFACE_MODE_RGMII_TXID;
963 };
964
965 /**
966 * phy_interface_mode_is_8023z() - does the phy interface mode use 802.3z
967 * negotiation
968 * @mode: one of &enum phy_interface_t
969 *
970 * Returns true if the phy interface mode uses the 16-bit negotiation
971 * word as defined in 802.3z. (See 802.3-2015 37.2.1 Config_Reg encoding)
972 */
phy_interface_mode_is_8023z(phy_interface_t mode)973 static inline bool phy_interface_mode_is_8023z(phy_interface_t mode)
974 {
975 return mode == PHY_INTERFACE_MODE_1000BASEX ||
976 mode == PHY_INTERFACE_MODE_2500BASEX;
977 }
978
979 /**
980 * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
981 * is RGMII (all variants)
982 * @phydev: the phy_device struct
983 */
phy_interface_is_rgmii(struct phy_device * phydev)984 static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
985 {
986 return phy_interface_mode_is_rgmii(phydev->interface);
987 };
988
989 /*
990 * phy_is_pseudo_fixed_link - Convenience function for testing if this
991 * PHY is the CPU port facing side of an Ethernet switch, or similar.
992 * @phydev: the phy_device struct
993 */
phy_is_pseudo_fixed_link(struct phy_device * phydev)994 static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
995 {
996 return phydev->is_pseudo_fixed_link;
997 }
998
999 int phy_save_page(struct phy_device *phydev);
1000 int phy_select_page(struct phy_device *phydev, int page);
1001 int phy_restore_page(struct phy_device *phydev, int oldpage, int ret);
1002 int phy_read_paged(struct phy_device *phydev, int page, u32 regnum);
1003 int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val);
1004 int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
1005 u16 mask, u16 set);
1006 int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
1007 u16 mask, u16 set);
1008
1009 struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
1010 bool is_c45,
1011 struct phy_c45_device_ids *c45_ids);
1012 #if IS_ENABLED(CONFIG_PHYLIB)
1013 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
1014 int phy_device_register(struct phy_device *phy);
1015 void phy_device_free(struct phy_device *phydev);
1016 #else
1017 static inline
get_phy_device(struct mii_bus * bus,int addr,bool is_c45)1018 struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
1019 {
1020 return NULL;
1021 }
1022
phy_device_register(struct phy_device * phy)1023 static inline int phy_device_register(struct phy_device *phy)
1024 {
1025 return 0;
1026 }
1027
phy_device_free(struct phy_device * phydev)1028 static inline void phy_device_free(struct phy_device *phydev) { }
1029 #endif /* CONFIG_PHYLIB */
1030 void phy_device_remove(struct phy_device *phydev);
1031 int phy_init_hw(struct phy_device *phydev);
1032 int phy_suspend(struct phy_device *phydev);
1033 int phy_resume(struct phy_device *phydev);
1034 int __phy_resume(struct phy_device *phydev);
1035 int phy_loopback(struct phy_device *phydev, bool enable);
1036 struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
1037 phy_interface_t interface);
1038 struct phy_device *phy_find_first(struct mii_bus *bus);
1039 int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
1040 u32 flags, phy_interface_t interface);
1041 int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
1042 void (*handler)(struct net_device *),
1043 phy_interface_t interface);
1044 struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
1045 void (*handler)(struct net_device *),
1046 phy_interface_t interface);
1047 void phy_disconnect(struct phy_device *phydev);
1048 void phy_detach(struct phy_device *phydev);
1049 void phy_start(struct phy_device *phydev);
1050 void phy_stop(struct phy_device *phydev);
1051 int phy_start_aneg(struct phy_device *phydev);
1052 int phy_aneg_done(struct phy_device *phydev);
1053 int phy_speed_down(struct phy_device *phydev, bool sync);
1054 int phy_speed_up(struct phy_device *phydev);
1055
1056 int phy_restart_aneg(struct phy_device *phydev);
1057 int phy_reset_after_clk_enable(struct phy_device *phydev);
1058
phy_device_reset(struct phy_device * phydev,int value)1059 static inline void phy_device_reset(struct phy_device *phydev, int value)
1060 {
1061 mdio_device_reset(&phydev->mdio, value);
1062 }
1063
1064 #define phydev_err(_phydev, format, args...) \
1065 dev_err(&_phydev->mdio.dev, format, ##args)
1066
1067 #define phydev_info(_phydev, format, args...) \
1068 dev_info(&_phydev->mdio.dev, format, ##args)
1069
1070 #define phydev_warn(_phydev, format, args...) \
1071 dev_warn(&_phydev->mdio.dev, format, ##args)
1072
1073 #define phydev_dbg(_phydev, format, args...) \
1074 dev_dbg(&_phydev->mdio.dev, format, ##args)
1075
phydev_name(const struct phy_device * phydev)1076 static inline const char *phydev_name(const struct phy_device *phydev)
1077 {
1078 return dev_name(&phydev->mdio.dev);
1079 }
1080
1081 void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
1082 __printf(2, 3);
1083 void phy_attached_info(struct phy_device *phydev);
1084
1085 /* Clause 22 PHY */
1086 int genphy_read_abilities(struct phy_device *phydev);
1087 int genphy_setup_forced(struct phy_device *phydev);
1088 int genphy_restart_aneg(struct phy_device *phydev);
1089 int genphy_config_eee_advert(struct phy_device *phydev);
1090 int __genphy_config_aneg(struct phy_device *phydev, bool changed);
1091 int genphy_aneg_done(struct phy_device *phydev);
1092 int genphy_update_link(struct phy_device *phydev);
1093 int genphy_read_lpa(struct phy_device *phydev);
1094 int genphy_read_status(struct phy_device *phydev);
1095 int genphy_suspend(struct phy_device *phydev);
1096 int genphy_resume(struct phy_device *phydev);
1097 int genphy_loopback(struct phy_device *phydev, bool enable);
1098 int genphy_soft_reset(struct phy_device *phydev);
1099
genphy_config_aneg(struct phy_device * phydev)1100 static inline int genphy_config_aneg(struct phy_device *phydev)
1101 {
1102 return __genphy_config_aneg(phydev, false);
1103 }
1104
genphy_no_soft_reset(struct phy_device * phydev)1105 static inline int genphy_no_soft_reset(struct phy_device *phydev)
1106 {
1107 return 0;
1108 }
genphy_no_ack_interrupt(struct phy_device * phydev)1109 static inline int genphy_no_ack_interrupt(struct phy_device *phydev)
1110 {
1111 return 0;
1112 }
genphy_no_config_intr(struct phy_device * phydev)1113 static inline int genphy_no_config_intr(struct phy_device *phydev)
1114 {
1115 return 0;
1116 }
1117 int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad,
1118 u16 regnum);
1119 int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
1120 u16 regnum, u16 val);
1121
1122 /* Clause 45 PHY */
1123 int genphy_c45_restart_aneg(struct phy_device *phydev);
1124 int genphy_c45_check_and_restart_aneg(struct phy_device *phydev, bool restart);
1125 int genphy_c45_aneg_done(struct phy_device *phydev);
1126 int genphy_c45_read_link(struct phy_device *phydev);
1127 int genphy_c45_read_lpa(struct phy_device *phydev);
1128 int genphy_c45_read_pma(struct phy_device *phydev);
1129 int genphy_c45_pma_setup_forced(struct phy_device *phydev);
1130 int genphy_c45_an_config_aneg(struct phy_device *phydev);
1131 int genphy_c45_an_disable_aneg(struct phy_device *phydev);
1132 int genphy_c45_read_mdix(struct phy_device *phydev);
1133 int genphy_c45_pma_read_abilities(struct phy_device *phydev);
1134 int genphy_c45_read_status(struct phy_device *phydev);
1135 int genphy_c45_config_aneg(struct phy_device *phydev);
1136
1137 /* The gen10g_* functions are the old Clause 45 stub */
1138 int gen10g_config_aneg(struct phy_device *phydev);
1139
phy_read_status(struct phy_device * phydev)1140 static inline int phy_read_status(struct phy_device *phydev)
1141 {
1142 if (!phydev->drv)
1143 return -EIO;
1144
1145 if (phydev->drv->read_status)
1146 return phydev->drv->read_status(phydev);
1147 else
1148 return genphy_read_status(phydev);
1149 }
1150
1151 void phy_driver_unregister(struct phy_driver *drv);
1152 void phy_drivers_unregister(struct phy_driver *drv, int n);
1153 int phy_driver_register(struct phy_driver *new_driver, struct module *owner);
1154 int phy_drivers_register(struct phy_driver *new_driver, int n,
1155 struct module *owner);
1156 void phy_state_machine(struct work_struct *work);
1157 void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies);
1158 void phy_mac_interrupt(struct phy_device *phydev);
1159 void phy_start_machine(struct phy_device *phydev);
1160 void phy_stop_machine(struct phy_device *phydev);
1161 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd);
1162 void phy_ethtool_ksettings_get(struct phy_device *phydev,
1163 struct ethtool_link_ksettings *cmd);
1164 int phy_ethtool_ksettings_set(struct phy_device *phydev,
1165 const struct ethtool_link_ksettings *cmd);
1166 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
1167 void phy_request_interrupt(struct phy_device *phydev);
1168 void phy_free_interrupt(struct phy_device *phydev);
1169 void phy_print_status(struct phy_device *phydev);
1170 int phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
1171 void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode);
1172 void phy_advertise_supported(struct phy_device *phydev);
1173 void phy_support_sym_pause(struct phy_device *phydev);
1174 void phy_support_asym_pause(struct phy_device *phydev);
1175 void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
1176 bool autoneg);
1177 void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx);
1178 bool phy_validate_pause(struct phy_device *phydev,
1179 struct ethtool_pauseparam *pp);
1180
1181 int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
1182 int (*run)(struct phy_device *));
1183 int phy_register_fixup_for_id(const char *bus_id,
1184 int (*run)(struct phy_device *));
1185 int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
1186 int (*run)(struct phy_device *));
1187
1188 int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask);
1189 int phy_unregister_fixup_for_id(const char *bus_id);
1190 int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask);
1191
1192 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable);
1193 int phy_get_eee_err(struct phy_device *phydev);
1194 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data);
1195 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data);
1196 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
1197 void phy_ethtool_get_wol(struct phy_device *phydev,
1198 struct ethtool_wolinfo *wol);
1199 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1200 struct ethtool_link_ksettings *cmd);
1201 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1202 const struct ethtool_link_ksettings *cmd);
1203 int phy_ethtool_nway_reset(struct net_device *ndev);
1204
1205 #if IS_ENABLED(CONFIG_PHYLIB)
1206 int __init mdio_bus_init(void);
1207 void mdio_bus_exit(void);
1208 #endif
1209
1210 /* Inline function for use within net/core/ethtool.c (built-in) */
phy_ethtool_get_strings(struct phy_device * phydev,u8 * data)1211 static inline int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data)
1212 {
1213 if (!phydev->drv)
1214 return -EIO;
1215
1216 mutex_lock(&phydev->lock);
1217 phydev->drv->get_strings(phydev, data);
1218 mutex_unlock(&phydev->lock);
1219
1220 return 0;
1221 }
1222
phy_ethtool_get_sset_count(struct phy_device * phydev)1223 static inline int phy_ethtool_get_sset_count(struct phy_device *phydev)
1224 {
1225 int ret;
1226
1227 if (!phydev->drv)
1228 return -EIO;
1229
1230 if (phydev->drv->get_sset_count &&
1231 phydev->drv->get_strings &&
1232 phydev->drv->get_stats) {
1233 mutex_lock(&phydev->lock);
1234 ret = phydev->drv->get_sset_count(phydev);
1235 mutex_unlock(&phydev->lock);
1236
1237 return ret;
1238 }
1239
1240 return -EOPNOTSUPP;
1241 }
1242
phy_ethtool_get_stats(struct phy_device * phydev,struct ethtool_stats * stats,u64 * data)1243 static inline int phy_ethtool_get_stats(struct phy_device *phydev,
1244 struct ethtool_stats *stats, u64 *data)
1245 {
1246 if (!phydev->drv)
1247 return -EIO;
1248
1249 mutex_lock(&phydev->lock);
1250 phydev->drv->get_stats(phydev, stats, data);
1251 mutex_unlock(&phydev->lock);
1252
1253 return 0;
1254 }
1255
1256 extern struct bus_type mdio_bus_type;
1257
1258 struct mdio_board_info {
1259 const char *bus_id;
1260 char modalias[MDIO_NAME_SIZE];
1261 int mdio_addr;
1262 const void *platform_data;
1263 };
1264
1265 #if IS_ENABLED(CONFIG_MDIO_DEVICE)
1266 int mdiobus_register_board_info(const struct mdio_board_info *info,
1267 unsigned int n);
1268 #else
mdiobus_register_board_info(const struct mdio_board_info * i,unsigned int n)1269 static inline int mdiobus_register_board_info(const struct mdio_board_info *i,
1270 unsigned int n)
1271 {
1272 return 0;
1273 }
1274 #endif
1275
1276
1277 /**
1278 * module_phy_driver() - Helper macro for registering PHY drivers
1279 * @__phy_drivers: array of PHY drivers to register
1280 *
1281 * Helper macro for PHY drivers which do not do anything special in module
1282 * init/exit. Each module may only use this macro once, and calling it
1283 * replaces module_init() and module_exit().
1284 */
1285 #define phy_module_driver(__phy_drivers, __count) \
1286 static int __init phy_module_init(void) \
1287 { \
1288 return phy_drivers_register(__phy_drivers, __count, THIS_MODULE); \
1289 } \
1290 module_init(phy_module_init); \
1291 static void __exit phy_module_exit(void) \
1292 { \
1293 phy_drivers_unregister(__phy_drivers, __count); \
1294 } \
1295 module_exit(phy_module_exit)
1296
1297 #define module_phy_driver(__phy_drivers) \
1298 phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers))
1299
1300 bool phy_driver_is_genphy(struct phy_device *phydev);
1301 bool phy_driver_is_genphy_10g(struct phy_device *phydev);
1302
1303 #endif /* __PHY_H */
1304