1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright 2011 Freescale Semiconductor, Inc.
4 * Andy Fleming <afleming@gmail.com>
6 * This file pretty much stolen from Linux's mii.h/ethtool.h/phy.h
13 #include <linux/errno.h>
14 #include <linux/list.h>
15 #include <linux/mii.h>
16 #include <linux/ethtool.h>
17 #include <linux/mdio.h>
18 #include <phy_interface.h>
20 #define PHY_FIXED_ID 0xa5a55a5a
21 #define PHY_NCSI_ID 0xbeefcafe
24 * There is no actual id for this.
25 * This is just a dummy id for gmii2rgmmi converter.
27 #define PHY_GMII2RGMII_ID 0x5a5a5a5a
29 #define PHY_MAX_ADDR 32
31 #define PHY_FLAG_BROKEN_RESET (1 << 0) /* soft reset not supported */
33 #define PHY_DEFAULT_FEATURES (SUPPORTED_Autoneg | \
37 #define PHY_10BT_FEATURES (SUPPORTED_10baseT_Half | \
38 SUPPORTED_10baseT_Full)
40 #define PHY_100BT_FEATURES (SUPPORTED_100baseT_Half | \
41 SUPPORTED_100baseT_Full)
43 #define PHY_1000BT_FEATURES (SUPPORTED_1000baseT_Half | \
44 SUPPORTED_1000baseT_Full)
46 #define PHY_BASIC_FEATURES (PHY_10BT_FEATURES | \
47 PHY_100BT_FEATURES | \
50 #define PHY_GBIT_FEATURES (PHY_BASIC_FEATURES | \
53 #define PHY_10G_FEATURES (PHY_GBIT_FEATURES | \
54 SUPPORTED_10000baseT_Full)
56 #ifndef PHY_ANEG_TIMEOUT
57 #define PHY_ANEG_TIMEOUT 4000
63 #define MDIO_NAME_LEN 32
66 struct list_head link;
67 char name[MDIO_NAME_LEN];
69 int (*read)(struct mii_dev *bus, int addr, int devad, int reg);
70 int (*write)(struct mii_dev *bus, int addr, int devad, int reg,
72 int (*reset)(struct mii_dev *bus);
73 struct phy_device *phymap[PHY_MAX_ADDR];
77 /* struct phy_driver: a structure which defines PHY behavior
79 * uid will contain a number which represents the PHY. During
80 * startup, the driver will poll the PHY to find out what its
81 * UID--as defined by registers 2 and 3--is. The 32-bit result
82 * gotten from the PHY will be masked to
83 * discard any bits which may change based on revision numbers
84 * unimportant to functionality
95 /* Called to do any driver startup necessities */
96 /* Will be called during phy_connect */
97 int (*probe)(struct phy_device *phydev);
99 /* Called to configure the PHY, and modify the controller
100 * based on the results. Should be called after phy_connect */
101 int (*config)(struct phy_device *phydev);
103 /* Called when starting up the controller */
104 int (*startup)(struct phy_device *phydev);
106 /* Called when bringing down the controller */
107 int (*shutdown)(struct phy_device *phydev);
109 int (*readext)(struct phy_device *phydev, int addr, int devad, int reg);
110 int (*writeext)(struct phy_device *phydev, int addr, int devad, int reg,
113 /* Phy specific driver override for reading a MMD register */
114 int (*read_mmd)(struct phy_device *phydev, int devad, int reg);
116 /* Phy specific driver override for writing a MMD register */
117 int (*write_mmd)(struct phy_device *phydev, int devad, int reg,
120 struct list_head list;
122 /* driver private data */
127 /* Information about the PHY type */
128 /* And management functions */
130 struct phy_driver *drv;
137 struct eth_device *dev;
140 /* forced speed & duplex (no autoneg)
141 * partner speed & duplex & pause (autoneg)
146 /* The most recently read link state */
149 phy_interface_t interface;
172 static inline int phy_read(struct phy_device *phydev, int devad, int regnum)
174 struct mii_dev *bus = phydev->bus;
176 return bus->read(bus, phydev->addr, devad, regnum);
179 static inline int phy_write(struct phy_device *phydev, int devad, int regnum,
182 struct mii_dev *bus = phydev->bus;
184 return bus->write(bus, phydev->addr, devad, regnum, val);
187 static inline void phy_mmd_start_indirect(struct phy_device *phydev, int devad,
190 /* Write the desired MMD Devad */
191 phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_CTRL, devad);
193 /* Write the desired MMD register address */
194 phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA, regnum);
196 /* Select the Function : DATA with no post increment */
197 phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_CTRL,
198 (devad | MII_MMD_CTRL_NOINCR));
201 static inline int phy_read_mmd(struct phy_device *phydev, int devad,
204 struct phy_driver *drv = phydev->drv;
206 if (regnum > (u16)~0 || devad > 32)
209 /* driver-specific access */
211 return drv->read_mmd(phydev, devad, regnum);
213 /* direct C45 / C22 access */
214 if ((drv->features & PHY_10G_FEATURES) == PHY_10G_FEATURES ||
215 devad == MDIO_DEVAD_NONE || !devad)
216 return phy_read(phydev, devad, regnum);
218 /* indirect C22 access */
219 phy_mmd_start_indirect(phydev, devad, regnum);
221 /* Read the content of the MMD's selected register */
222 return phy_read(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA);
225 static inline int phy_write_mmd(struct phy_device *phydev, int devad,
228 struct phy_driver *drv = phydev->drv;
230 if (regnum > (u16)~0 || devad > 32)
233 /* driver-specific access */
235 return drv->write_mmd(phydev, devad, regnum, val);
237 /* direct C45 / C22 access */
238 if ((drv->features & PHY_10G_FEATURES) == PHY_10G_FEATURES ||
239 devad == MDIO_DEVAD_NONE || !devad)
240 return phy_write(phydev, devad, regnum, val);
242 /* indirect C22 access */
243 phy_mmd_start_indirect(phydev, devad, regnum);
245 /* Write the data into MMD's selected register */
246 return phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA, val);
249 #ifdef CONFIG_PHYLIB_10G
250 extern struct phy_driver gen10g_driver;
252 /* For now, XGMII is the only 10G interface */
253 static inline int is_10g_interface(phy_interface_t interface)
255 return interface == PHY_INTERFACE_MODE_XGMII;
261 * phy_init() - Initializes the PHY drivers
263 * This function registers all available PHY drivers
265 * @return 0 if OK, -ve on error
270 * phy_reset() - Resets the specified PHY
272 * Issues a reset of the PHY and waits for it to complete
274 * @phydev: PHY to reset
275 * @return 0 if OK, -ve on error
277 int phy_reset(struct phy_device *phydev);
280 * phy_find_by_mask() - Searches for a PHY on the specified MDIO bus
282 * The function checks the PHY addresses flagged in phy_mask and returns a
283 * phy_device pointer if it detects a PHY.
284 * This function should only be called if just one PHY is expected to be present
285 * in the set of addresses flagged in phy_mask. If multiple PHYs are present,
286 * it is undefined which of these PHYs is returned.
288 * @bus: MII/MDIO bus to scan
289 * @phy_mask: bitmap of PYH addresses to scan
290 * @interface: type of MAC-PHY interface
291 * @return pointer to phy_device if a PHY is found, or NULL otherwise
293 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
294 phy_interface_t interface);
299 * phy_connect_dev() - Associates the given pair of PHY and Ethernet devices
300 * @phydev: PHY device
301 * @dev: Ethernet device
303 void phy_connect_dev(struct phy_device *phydev, struct udevice *dev);
306 * phy_connect() - Creates a PHY device for the Ethernet interface
308 * Creates a PHY device for the PHY at the given address, if one doesn't exist
309 * already, and associates it with the Ethernet device.
310 * The function may be called with addr <= 0, in this case addr value is ignored
311 * and the bus is scanned to detect a PHY. Scanning should only be used if only
312 * one PHY is expected to be present on the MDIO bus, otherwise it is undefined
313 * which PHY is returned.
315 * @bus: MII/MDIO bus that hosts the PHY
316 * @addr: PHY address on MDIO bus
317 * @dev: Ethernet device to associate to the PHY
318 * @interface: type of MAC-PHY interface
319 * @return pointer to phy_device if a PHY is found, or NULL otherwise
321 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
323 phy_interface_t interface);
325 static inline ofnode phy_get_ofnode(struct phy_device *phydev)
327 if (ofnode_valid(phydev->node))
330 return dev_ofnode(phydev->dev);
335 * phy_connect_dev() - Associates the given pair of PHY and Ethernet devices
336 * @phydev: PHY device
337 * @dev: Ethernet device
339 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev);
342 * phy_connect() - Creates a PHY device for the Ethernet interface
344 * Creates a PHY device for the PHY at the given address, if one doesn't exist
345 * already, and associates it with the Ethernet device.
346 * The function may be called with addr <= 0, in this case addr value is ignored
347 * and the bus is scanned to detect a PHY. Scanning should only be used if only
348 * one PHY is expected to be present on the MDIO bus, otherwise it is undefined
349 * which PHY is returned.
351 * @bus: MII/MDIO bus that hosts the PHY
352 * @addr: PHY address on MDIO bus
353 * @dev: Ethernet device to associate to the PHY
354 * @interface: type of MAC-PHY interface
355 * @return pointer to phy_device if a PHY is found, or NULL otherwise
357 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
358 struct eth_device *dev,
359 phy_interface_t interface);
361 static inline ofnode phy_get_ofnode(struct phy_device *phydev)
363 return ofnode_null();
366 int phy_startup(struct phy_device *phydev);
367 int phy_config(struct phy_device *phydev);
368 int phy_shutdown(struct phy_device *phydev);
369 int phy_register(struct phy_driver *drv);
370 int phy_set_supported(struct phy_device *phydev, u32 max_speed);
371 int genphy_config_aneg(struct phy_device *phydev);
372 int genphy_restart_aneg(struct phy_device *phydev);
373 int genphy_update_link(struct phy_device *phydev);
374 int genphy_parse_link(struct phy_device *phydev);
375 int genphy_config(struct phy_device *phydev);
376 int genphy_startup(struct phy_device *phydev);
377 int genphy_shutdown(struct phy_device *phydev);
378 int gen10g_config(struct phy_device *phydev);
379 int gen10g_startup(struct phy_device *phydev);
380 int gen10g_shutdown(struct phy_device *phydev);
381 int gen10g_discover_mmds(struct phy_device *phydev);
383 int phy_b53_init(void);
384 int phy_mv88e61xx_init(void);
385 int phy_aquantia_init(void);
386 int phy_atheros_init(void);
387 int phy_broadcom_init(void);
388 int phy_cortina_init(void);
389 int phy_davicom_init(void);
390 int phy_et1011c_init(void);
391 int phy_lxt_init(void);
392 int phy_marvell_init(void);
393 int phy_micrel_ksz8xxx_init(void);
394 int phy_micrel_ksz90x1_init(void);
395 int phy_meson_gxl_init(void);
396 int phy_natsemi_init(void);
397 int phy_realtek_init(void);
398 int phy_smsc_init(void);
399 int phy_teranetics_init(void);
400 int phy_ti_init(void);
401 int phy_vitesse_init(void);
402 int phy_xilinx_init(void);
403 int phy_mscc_init(void);
404 int phy_fixed_init(void);
405 int phy_xilinx_gmii2rgmii_init(void);
407 int board_phy_config(struct phy_device *phydev);
408 int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id);
411 * phy_get_interface_by_name() - Look up a PHY interface name
413 * @str: PHY interface name, e.g. "mii"
414 * @return PHY_INTERFACE_MODE_... value, or -1 if not found
416 int phy_get_interface_by_name(const char *str);
419 * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
420 * is RGMII (all variants)
421 * @phydev: the phy_device struct
423 static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
425 return phydev->interface >= PHY_INTERFACE_MODE_RGMII &&
426 phydev->interface <= PHY_INTERFACE_MODE_RGMII_TXID;
430 * phy_interface_is_sgmii - Convenience function for testing if a PHY interface
431 * is SGMII (all variants)
432 * @phydev: the phy_device struct
434 static inline bool phy_interface_is_sgmii(struct phy_device *phydev)
436 return phydev->interface >= PHY_INTERFACE_MODE_SGMII &&
437 phydev->interface <= PHY_INTERFACE_MODE_QSGMII;
440 /* PHY UIDs for various PHYs that are referenced in external code */
441 #define PHY_UID_CS4340 0x13e51002
442 #define PHY_UID_CS4223 0x03e57003
443 #define PHY_UID_TN2020 0x00a19410
444 #define PHY_UID_IN112525_S03 0x02107440