xref: /freebsd/sys/dev/mii/miivar.h (revision 95ee2897)
1d0027533SBill Paul /*	$NetBSD: miivar.h,v 1.8 1999/04/23 04:24:32 thorpej Exp $	*/
2d0027533SBill Paul 
3d0027533SBill Paul /*-
4b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
5718cf2ccSPedro F. Giffuni  *
6d0027533SBill Paul  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
7d0027533SBill Paul  * All rights reserved.
8d0027533SBill Paul  *
9d0027533SBill Paul  * This code is derived from software contributed to The NetBSD Foundation
10d0027533SBill Paul  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
11d0027533SBill Paul  * NASA Ames Research Center.
12d0027533SBill Paul  *
13d0027533SBill Paul  * Redistribution and use in source and binary forms, with or without
14d0027533SBill Paul  * modification, are permitted provided that the following conditions
15d0027533SBill Paul  * are met:
16d0027533SBill Paul  * 1. Redistributions of source code must retain the above copyright
17d0027533SBill Paul  *    notice, this list of conditions and the following disclaimer.
18d0027533SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
19d0027533SBill Paul  *    notice, this list of conditions and the following disclaimer in the
20d0027533SBill Paul  *    documentation and/or other materials provided with the distribution.
21d0027533SBill Paul  *
22d0027533SBill Paul  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23d0027533SBill Paul  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24d0027533SBill Paul  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25d0027533SBill Paul  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26d0027533SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27d0027533SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28d0027533SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29d0027533SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30d0027533SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31d0027533SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32d0027533SBill Paul  * POSSIBILITY OF SUCH DAMAGE.
33d0027533SBill Paul  */
34d0027533SBill Paul 
35d0027533SBill Paul #ifndef _DEV_MII_MIIVAR_H_
36d0027533SBill Paul #define	_DEV_MII_MIIVAR_H_
37d0027533SBill Paul 
38d0027533SBill Paul #include <sys/queue.h>
3962d76917SMarcel Moolenaar #include <net/if_var.h>	/* XXX driver API temporary */
40d0027533SBill Paul 
419174eab4SKornel Duleba #include "opt_platform.h"
429174eab4SKornel Duleba 
439174eab4SKornel Duleba #ifdef FDT
449174eab4SKornel Duleba #include <dev/ofw/openfirm.h>
459174eab4SKornel Duleba #include <dev/ofw/ofw_bus.h>
469174eab4SKornel Duleba #include <dev/ofw/ofw_bus_subr.h>
479174eab4SKornel Duleba #endif
489174eab4SKornel Duleba 
49d0027533SBill Paul /*
503fcb7a53SMarius Strobl  * Media Independent Interface data structure defintions
51d0027533SBill Paul  */
52d0027533SBill Paul 
53d0027533SBill Paul struct mii_softc;
54d0027533SBill Paul 
55d0027533SBill Paul /*
56d0027533SBill Paul  * A network interface driver has one of these structures in its softc.
57d0027533SBill Paul  * It is the interface from the network interface driver to the MII
58d0027533SBill Paul  * layer.
59d0027533SBill Paul  */
60d0027533SBill Paul struct mii_data {
61d0027533SBill Paul 	struct ifmedia mii_media;	/* media information */
6262d76917SMarcel Moolenaar 	if_t mii_ifp;		/* pointer back to network interface */
63d0027533SBill Paul 
64d0027533SBill Paul 	/*
65d0027533SBill Paul 	 * For network interfaces with multiple PHYs, a list of all
66d0027533SBill Paul 	 * PHYs is required so they can all be notified when a media
67d0027533SBill Paul 	 * request is made.
68d0027533SBill Paul 	 */
69e3975643SJake Burkholder 	LIST_HEAD(mii_listhead, mii_softc) mii_phys;
703fcb7a53SMarius Strobl 	u_int mii_instance;
71d0027533SBill Paul 
72d0027533SBill Paul 	/*
73d0027533SBill Paul 	 * PHY driver fills this in with active media status.
74d0027533SBill Paul 	 */
753fcb7a53SMarius Strobl 	u_int mii_media_status;
763fcb7a53SMarius Strobl 	u_int mii_media_active;
77d0027533SBill Paul };
78d0027533SBill Paul typedef struct mii_data mii_data_t;
79d0027533SBill Paul 
80d0027533SBill Paul /*
813fcb7a53SMarius Strobl  * Functions provided by the PHY to perform various functions.
82d0027533SBill Paul  */
833fcb7a53SMarius Strobl struct mii_phy_funcs {
843fcb7a53SMarius Strobl 	int (*pf_service)(struct mii_softc *, struct mii_data *, int);
853fcb7a53SMarius Strobl 	void (*pf_status)(struct mii_softc *);
863fcb7a53SMarius Strobl 	void (*pf_reset)(struct mii_softc *);
873fcb7a53SMarius Strobl };
88d0027533SBill Paul 
89d0027533SBill Paul /*
90d0027533SBill Paul  * Requests that can be made to the downcall.
91d0027533SBill Paul  */
92d0027533SBill Paul #define	MII_TICK	1	/* once-per-second tick */
93d0027533SBill Paul #define	MII_MEDIACHG	2	/* user changed media; perform the switch */
94d0027533SBill Paul #define	MII_POLLSTAT	3	/* user requested media status; fill it in */
95d0027533SBill Paul 
96d0027533SBill Paul /*
97d0027533SBill Paul  * Each PHY driver's softc has one of these as the first member.
98d0027533SBill Paul  * XXX This would be better named "phy_softc", but this is the name
99d0027533SBill Paul  * XXX BSDI used, and we would like to have the same interface.
100d0027533SBill Paul  */
101d0027533SBill Paul struct mii_softc {
102d0027533SBill Paul 	device_t mii_dev;		/* generic device glue */
103d0027533SBill Paul 
104e3975643SJake Burkholder 	LIST_ENTRY(mii_softc) mii_list;	/* entry on parent's PHY list */
105d0027533SBill Paul 
1063fcb7a53SMarius Strobl 	uint32_t mii_mpd_oui;		/* the PHY's OUI (MII_OUI())*/
1073fcb7a53SMarius Strobl 	uint32_t mii_mpd_model;		/* the PHY's model (MII_MODEL())*/
1083fcb7a53SMarius Strobl 	uint32_t mii_mpd_rev;		/* the PHY's revision (MII_REV())*/
1093fcb7a53SMarius Strobl 	u_int mii_capmask;		/* capability mask for BMSR */
1103fcb7a53SMarius Strobl 	u_int mii_phy;			/* our MII address */
1113fcb7a53SMarius Strobl 	u_int mii_offset;		/* first PHY, second PHY, etc. */
1123fcb7a53SMarius Strobl 	u_int mii_inst;			/* instance for ifmedia */
113d0027533SBill Paul 
1143fcb7a53SMarius Strobl 	/* Our PHY functions. */
1153fcb7a53SMarius Strobl 	const struct mii_phy_funcs *mii_funcs;
1163fcb7a53SMarius Strobl 
117d0027533SBill Paul 	struct mii_data *mii_pdata;	/* pointer to parent's mii_data */
118d0027533SBill Paul 
1193fcb7a53SMarius Strobl 	u_int mii_flags;		/* misc. flags; see below */
1203fcb7a53SMarius Strobl 	u_int mii_capabilities;		/* capabilities from BMSR */
1213fcb7a53SMarius Strobl 	u_int mii_extcapabilities;	/* extended capabilities */
1223fcb7a53SMarius Strobl 	u_int mii_ticks;		/* MII_TICK counter */
1233fcb7a53SMarius Strobl 	u_int mii_anegticks;		/* ticks before retrying aneg */
1243fcb7a53SMarius Strobl 	u_int mii_media_active;		/* last active media */
1253fcb7a53SMarius Strobl 	u_int mii_media_status;		/* last active status */
126b38de28aSKornel Duleba 	u_int mii_maxspeed;		/* Max speed supported by this PHY */
127d0027533SBill Paul };
128d0027533SBill Paul typedef struct mii_softc mii_softc_t;
129d0027533SBill Paul 
130d0027533SBill Paul /* mii_flags */
131a55fb8a4SMarius Strobl #define	MIIF_INITDONE	0x00000001	/* has been initialized (mii_data) */
132a55fb8a4SMarius Strobl #define	MIIF_NOISOLATE	0x00000002	/* do not isolate the PHY */
1333fcb7a53SMarius Strobl #if 0
134a55fb8a4SMarius Strobl #define	MIIF_NOLOOP	0x00000004	/* no loopback capability */
1353fcb7a53SMarius Strobl #endif
136efd4fc3fSMarius Strobl #define	MIIF_DOINGAUTO	0x00000008	/* doing autonegotiation (mii_softc) */
137a55fb8a4SMarius Strobl #define	MIIF_AUTOTSLEEP	0x00000010	/* use tsleep(), not callout() */
138a55fb8a4SMarius Strobl #define	MIIF_HAVEFIBER	0x00000020	/* from parent: has fiber interface */
139a55fb8a4SMarius Strobl #define	MIIF_HAVE_GTCR	0x00000040	/* has 100base-T2/1000base-T CR */
140a55fb8a4SMarius Strobl #define	MIIF_IS_1000X	0x00000080	/* is a 1000BASE-X device */
141a55fb8a4SMarius Strobl #define	MIIF_DOPAUSE	0x00000100	/* advertise PAUSE capability */
142a55fb8a4SMarius Strobl #define	MIIF_IS_HPNA	0x00000200	/* is a HomePNA device */
143a55fb8a4SMarius Strobl #define	MIIF_FORCEANEG	0x00000400	/* force auto-negotiation */
144349eddbdSMike Karels #define	MIIF_RX_DELAY	0x00000800	/* add RX delay */
145349eddbdSMike Karels #define	MIIF_TX_DELAY	0x00001000	/* add TX delay */
146efd4fc3fSMarius Strobl #define	MIIF_NOMANPAUSE	0x00100000	/* no manual PAUSE selection */
1476882cb0cSMarius Strobl #define	MIIF_FORCEPAUSE	0x00200000	/* force PAUSE advertisement */
148a55fb8a4SMarius Strobl #define	MIIF_MACPRIV0	0x01000000	/* private to the MAC driver */
149a55fb8a4SMarius Strobl #define	MIIF_MACPRIV1	0x02000000	/* private to the MAC driver */
150a55fb8a4SMarius Strobl #define	MIIF_MACPRIV2	0x04000000	/* private to the MAC driver */
151a55fb8a4SMarius Strobl #define	MIIF_PHYPRIV0	0x10000000	/* private to the PHY driver */
152a55fb8a4SMarius Strobl #define	MIIF_PHYPRIV1	0x20000000	/* private to the PHY driver */
153a55fb8a4SMarius Strobl #define	MIIF_PHYPRIV2	0x40000000	/* private to the PHY driver */
154b7dee1dbSPoul-Henning Kamp 
1551c47c79eSOleg Bulyzhin /* Default mii_anegticks values */
1561c47c79eSOleg Bulyzhin #define	MII_ANEGTICKS		5
1571c47c79eSOleg Bulyzhin #define	MII_ANEGTICKS_GIGE	17
1581c47c79eSOleg Bulyzhin 
159b7dee1dbSPoul-Henning Kamp #define	MIIF_INHERIT_MASK	(MIIF_NOISOLATE|MIIF_NOLOOP|MIIF_AUTOTSLEEP)
160d0027533SBill Paul 
161d0027533SBill Paul /*
162a55fb8a4SMarius Strobl  * Special `locators' passed to mii_attach().  If one of these is not
163a55fb8a4SMarius Strobl  * an `any' value, we look for *that* PHY and configure it.  If both
1644a446e3eSMarius Strobl  * are not `any', that is an error, and mii_attach() will fail.
165a55fb8a4SMarius Strobl  */
166a55fb8a4SMarius Strobl #define	MII_OFFSET_ANY		-1
167a55fb8a4SMarius Strobl #define	MII_PHY_ANY		-1
168a55fb8a4SMarius Strobl 
169a55fb8a4SMarius Strobl /*
1700f75981aSIan Lepore  * Constants used to describe the type of attachment between MAC and PHY.
1710f75981aSIan Lepore  */
1720f75981aSIan Lepore enum mii_contype {
1730f75981aSIan Lepore 	MII_CONTYPE_UNKNOWN,	/* Must be have value 0. */
1740f75981aSIan Lepore 
1750f75981aSIan Lepore 	MII_CONTYPE_MII,
1760f75981aSIan Lepore 	MII_CONTYPE_GMII,
1770f75981aSIan Lepore 	MII_CONTYPE_SGMII,
1780f75981aSIan Lepore 	MII_CONTYPE_QSGMII,
1790f75981aSIan Lepore 	MII_CONTYPE_TBI,
1800f75981aSIan Lepore 	MII_CONTYPE_REVMII,	/* Reverse MII */
1810f75981aSIan Lepore 	MII_CONTYPE_RMII,
1820f75981aSIan Lepore 	MII_CONTYPE_RGMII,	/* Delays provided by MAC or PCB */
1830f75981aSIan Lepore 	MII_CONTYPE_RGMII_ID,	/* Rx and tx delays provided by PHY */
1840f75981aSIan Lepore 	MII_CONTYPE_RGMII_RXID,	/* Only rx delay provided by PHY */
1850f75981aSIan Lepore 	MII_CONTYPE_RGMII_TXID,	/* Only tx delay provided by PHY */
1860f75981aSIan Lepore 	MII_CONTYPE_RTBI,
1870f75981aSIan Lepore 	MII_CONTYPE_SMII,
1880f75981aSIan Lepore 	MII_CONTYPE_XGMII,
1890f75981aSIan Lepore 	MII_CONTYPE_TRGMII,
1900f75981aSIan Lepore 	MII_CONTYPE_2000BX,
1910f75981aSIan Lepore 	MII_CONTYPE_2500BX,
1920f75981aSIan Lepore 	MII_CONTYPE_RXAUI,
1930f75981aSIan Lepore 
1940f75981aSIan Lepore 	MII_CONTYPE_COUNT	/* Add new types before this line. */
1950f75981aSIan Lepore };
1960f75981aSIan Lepore typedef enum mii_contype mii_contype_t;
1970f75981aSIan Lepore 
1980f75981aSIan Lepore static inline bool
mii_contype_is_rgmii(mii_contype_t con)1990f75981aSIan Lepore mii_contype_is_rgmii(mii_contype_t con)
2000f75981aSIan Lepore {
2010f75981aSIan Lepore 
2020f75981aSIan Lepore 	return (con >= MII_CONTYPE_RGMII && con <= MII_CONTYPE_RGMII_TXID);
2030f75981aSIan Lepore }
2040f75981aSIan Lepore 
2050f75981aSIan Lepore /*
206d0027533SBill Paul  * Used to attach a PHY to a parent.
207d0027533SBill Paul  */
208d0027533SBill Paul struct mii_attach_args {
209d0027533SBill Paul 	struct mii_data *mii_data;	/* pointer to parent data */
2103fcb7a53SMarius Strobl 	u_int mii_phyno;		/* MII address */
2113fcb7a53SMarius Strobl 	u_int mii_offset;		/* first PHY, second PHY, etc. */
2123fcb7a53SMarius Strobl 	uint32_t mii_id1;		/* PHY ID register 1 */
2133fcb7a53SMarius Strobl 	uint32_t mii_id2;		/* PHY ID register 2 */
2143fcb7a53SMarius Strobl 	u_int mii_capmask;		/* capability mask for BMSR */
2159174eab4SKornel Duleba #ifdef FDT
2169174eab4SKornel Duleba 	struct ofw_bus_devinfo obd;
2179174eab4SKornel Duleba 	struct resource_list rl;
2189174eab4SKornel Duleba #endif
2199174eab4SKornel Duleba 
220d0027533SBill Paul };
221d0027533SBill Paul typedef struct mii_attach_args mii_attach_args_t;
222d0027533SBill Paul 
223b7dee1dbSPoul-Henning Kamp /*
224875525d5SPoul-Henning Kamp  * Used to match a PHY.
225875525d5SPoul-Henning Kamp  */
226875525d5SPoul-Henning Kamp struct mii_phydesc {
2273fcb7a53SMarius Strobl 	uint32_t mpd_oui;		/* the PHY's OUI */
2283fcb7a53SMarius Strobl 	uint32_t mpd_model;		/* the PHY's model */
229875525d5SPoul-Henning Kamp 	const char *mpd_name;		/* the PHY's name */
230875525d5SPoul-Henning Kamp };
231a1039f82SWarner Losh #define MII_PHY_DESC(a, b) { MII_OUI_ ## a, MII_MODEL_ ## a ## _ ## b, \
232a1039f82SWarner Losh 	MII_STR_ ## a ## _ ## b }
233a1039f82SWarner Losh #define MII_PHY_END	{ 0, 0, NULL }
234875525d5SPoul-Henning Kamp 
235664a31e4SPeter Wemm #ifdef _KERNEL
236d0027533SBill Paul 
237d0027533SBill Paul #define PHY_READ(p, r) \
238d0027533SBill Paul 	MIIBUS_READREG((p)->mii_dev, (p)->mii_phy, (r))
239d0027533SBill Paul 
240d0027533SBill Paul #define PHY_WRITE(p, r, v) \
241d0027533SBill Paul 	MIIBUS_WRITEREG((p)->mii_dev, (p)->mii_phy, (r), (v))
242d0027533SBill Paul 
2433fcb7a53SMarius Strobl #define	PHY_SERVICE(p, d, o) \
2443fcb7a53SMarius Strobl 	(*(p)->mii_funcs->pf_service)((p), (d), (o))
2453fcb7a53SMarius Strobl 
2463fcb7a53SMarius Strobl #define	PHY_STATUS(p) \
2473fcb7a53SMarius Strobl 	(*(p)->mii_funcs->pf_status)(p)
2483fcb7a53SMarius Strobl 
2493fcb7a53SMarius Strobl #define	PHY_RESET(p) \
2503fcb7a53SMarius Strobl 	(*(p)->mii_funcs->pf_reset)(p)
2513fcb7a53SMarius Strobl 
252a55fb8a4SMarius Strobl enum miibus_device_ivars {
253a55fb8a4SMarius Strobl 	MIIBUS_IVAR_FLAGS
254a55fb8a4SMarius Strobl };
255a55fb8a4SMarius Strobl 
256a55fb8a4SMarius Strobl /*
257a55fb8a4SMarius Strobl  * Simplified accessors for miibus
258a55fb8a4SMarius Strobl  */
259a55fb8a4SMarius Strobl #define	MIIBUS_ACCESSOR(var, ivar, type)				\
260a55fb8a4SMarius Strobl 	__BUS_ACCESSOR(miibus, var, MIIBUS, ivar, type)
261a55fb8a4SMarius Strobl 
2623fcb7a53SMarius Strobl MIIBUS_ACCESSOR(flags,		FLAGS,		u_int)
263a55fb8a4SMarius Strobl 
2649174eab4SKornel Duleba DECLARE_CLASS(miibus_driver);
2659174eab4SKornel Duleba 
2669174eab4SKornel Duleba #ifdef FDT
2679174eab4SKornel Duleba DECLARE_CLASS(miibus_fdt_driver);
2689174eab4SKornel Duleba #endif
2699174eab4SKornel Duleba 
270d0027533SBill Paul 
27162d76917SMarcel Moolenaar int	mii_attach(device_t, device_t *, if_t, ifm_change_cb_t,
272a55fb8a4SMarius Strobl 	    ifm_stat_cb_t, int, int, int, int);
273e51a25f8SAlfred Perlstein int	mii_mediachg(struct mii_data *);
274e51a25f8SAlfred Perlstein void	mii_tick(struct mii_data *);
275e51a25f8SAlfred Perlstein void	mii_pollstat(struct mii_data *);
27678c8c3dbSPoul-Henning Kamp void	mii_phy_add_media(struct mii_softc *);
277d0027533SBill Paul 
278fd94424cSPoul-Henning Kamp int	mii_phy_auto(struct mii_softc *);
279279fe8d1SPoul-Henning Kamp int	mii_phy_detach(device_t dev);
280efd4fc3fSMarius Strobl u_int	mii_phy_flowstatus(struct mii_softc *);
281e51a25f8SAlfred Perlstein void	mii_phy_reset(struct mii_softc *);
282b7dee1dbSPoul-Henning Kamp void	mii_phy_setmedia(struct mii_softc *sc);
283e51a25f8SAlfred Perlstein void	mii_phy_update(struct mii_softc *, int);
284e51a25f8SAlfred Perlstein int	mii_phy_tick(struct mii_softc *);
2857e310d2dSGleb Smirnoff int	mii_phy_mac_match(struct mii_softc *, const char *);
2867e310d2dSGleb Smirnoff int	mii_dev_mac_match(device_t, const char *);
2877e310d2dSGleb Smirnoff void	*mii_phy_mac_softc(struct mii_softc *);
2887e310d2dSGleb Smirnoff void	*mii_dev_mac_softc(device_t);
289d0027533SBill Paul 
29008576af0SWarner Losh const struct mii_phydesc * mii_phy_match(const struct mii_attach_args *ma,
29108576af0SWarner Losh     const struct mii_phydesc *mpd);
29208576af0SWarner Losh const struct mii_phydesc * mii_phy_match_gen(const struct mii_attach_args *ma,
29308576af0SWarner Losh     const struct mii_phydesc *mpd, size_t endlen);
294a35b9333SMarius Strobl int mii_phy_dev_probe(device_t dev, const struct mii_phydesc *mpd, int mrv);
2953fcb7a53SMarius Strobl void mii_phy_dev_attach(device_t dev, u_int flags,
2963fcb7a53SMarius Strobl     const struct mii_phy_funcs *mpf, int add_media);
297875525d5SPoul-Henning Kamp 
2989174eab4SKornel Duleba device_attach_t miibus_attach;
2999174eab4SKornel Duleba 
300e51a25f8SAlfred Perlstein void	ukphy_status(struct mii_softc *);
3013fcb7a53SMarius Strobl 
3023fcb7a53SMarius Strobl u_int	mii_oui(u_int, u_int);
3033fcb7a53SMarius Strobl #define	MII_OUI(id1, id2)	mii_oui(id1, id2)
3043fcb7a53SMarius Strobl #define	MII_MODEL(id2)		(((id2) & IDR2_MODEL) >> 4)
3053fcb7a53SMarius Strobl #define	MII_REV(id2)		((id2) & IDR2_REV)
3063fcb7a53SMarius Strobl 
307664a31e4SPeter Wemm #endif /* _KERNEL */
308d0027533SBill Paul 
309d0027533SBill Paul #endif /* _DEV_MII_MIIVAR_H_ */
310