150120Swpaul/*	$NetBSD: miivar.h,v 1.8 1999/04/23 04:24:32 thorpej Exp $	*/
250120Swpaul
350120Swpaul/*-
450120Swpaul * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
550120Swpaul * All rights reserved.
650120Swpaul *
750120Swpaul * This code is derived from software contributed to The NetBSD Foundation
850120Swpaul * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
950120Swpaul * NASA Ames Research Center.
1050120Swpaul *
1150120Swpaul * Redistribution and use in source and binary forms, with or without
1250120Swpaul * modification, are permitted provided that the following conditions
1350120Swpaul * are met:
1450120Swpaul * 1. Redistributions of source code must retain the above copyright
1550120Swpaul *    notice, this list of conditions and the following disclaimer.
1650120Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1750120Swpaul *    notice, this list of conditions and the following disclaimer in the
1850120Swpaul *    documentation and/or other materials provided with the distribution.
1950120Swpaul *
2050120Swpaul * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2150120Swpaul * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2250120Swpaul * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2350120Swpaul * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2450120Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2550120Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2650120Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2750120Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2850120Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2950120Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3050120Swpaul * POSSIBILITY OF SUCH DAMAGE.
3150120Swpaul *
3250477Speter * $FreeBSD$
3350120Swpaul */
3450120Swpaul
3550120Swpaul#ifndef _DEV_MII_MIIVAR_H_
3650120Swpaul#define	_DEV_MII_MIIVAR_H_
3750120Swpaul
3850120Swpaul#include <sys/queue.h>
3950120Swpaul
4050120Swpaul/*
41221407Smarius * Media Independent Interface data structure defintions
4250120Swpaul */
4350120Swpaul
4450120Swpaulstruct mii_softc;
4550120Swpaul
4650120Swpaul/*
4750120Swpaul * Callbacks from MII layer into network interface device driver.
4850120Swpaul */
4992739Salfredtypedef	int (*mii_readreg_t)(struct device *, int, int);
5092739Salfredtypedef	void (*mii_writereg_t)(struct device *, int, int, int);
5192739Salfredtypedef	void (*mii_statchg_t)(struct device *);
5250120Swpaul
5350120Swpaul/*
5450120Swpaul * A network interface driver has one of these structures in its softc.
5550120Swpaul * It is the interface from the network interface driver to the MII
5650120Swpaul * layer.
5750120Swpaul */
5850120Swpaulstruct mii_data {
5950120Swpaul	struct ifmedia mii_media;	/* media information */
6050120Swpaul	struct ifnet *mii_ifp;		/* pointer back to network interface */
6150120Swpaul
6250120Swpaul	/*
6350120Swpaul	 * For network interfaces with multiple PHYs, a list of all
6450120Swpaul	 * PHYs is required so they can all be notified when a media
6550120Swpaul	 * request is made.
6650120Swpaul	 */
6760938Sjake	LIST_HEAD(mii_listhead, mii_softc) mii_phys;
68221407Smarius	u_int mii_instance;
6950120Swpaul
7050120Swpaul	/*
7150120Swpaul	 * PHY driver fills this in with active media status.
7250120Swpaul	 */
73221407Smarius	u_int mii_media_status;
74221407Smarius	u_int mii_media_active;
7550120Swpaul
7650120Swpaul	/*
7750120Swpaul	 * Calls from MII layer into network interface driver.
7850120Swpaul	 */
7950120Swpaul	mii_readreg_t mii_readreg;
8050120Swpaul	mii_writereg_t mii_writereg;
8150120Swpaul	mii_statchg_t mii_statchg;
8250120Swpaul};
8350120Swpaultypedef struct mii_data mii_data_t;
8450120Swpaul
8550120Swpaul/*
86221407Smarius * Functions provided by the PHY to perform various functions.
8750120Swpaul */
88221407Smariusstruct mii_phy_funcs {
89221407Smarius	int (*pf_service)(struct mii_softc *, struct mii_data *, int);
90221407Smarius	void (*pf_status)(struct mii_softc *);
91221407Smarius	void (*pf_reset)(struct mii_softc *);
92221407Smarius};
9350120Swpaul
9450120Swpaul/*
9550120Swpaul * Requests that can be made to the downcall.
9650120Swpaul */
9750120Swpaul#define	MII_TICK	1	/* once-per-second tick */
9850120Swpaul#define	MII_MEDIACHG	2	/* user changed media; perform the switch */
9950120Swpaul#define	MII_POLLSTAT	3	/* user requested media status; fill it in */
10050120Swpaul
10150120Swpaul/*
10250120Swpaul * Each PHY driver's softc has one of these as the first member.
10350120Swpaul * XXX This would be better named "phy_softc", but this is the name
10450120Swpaul * XXX BSDI used, and we would like to have the same interface.
10550120Swpaul */
10650120Swpaulstruct mii_softc {
10750120Swpaul	device_t mii_dev;		/* generic device glue */
108213878Smarius
10960938Sjake	LIST_ENTRY(mii_softc) mii_list;	/* entry on parent's PHY list */
11050120Swpaul
111221407Smarius	uint32_t mii_mpd_oui;		/* the PHY's OUI (MII_OUI())*/
112221407Smarius	uint32_t mii_mpd_model;		/* the PHY's model (MII_MODEL())*/
113221407Smarius	uint32_t mii_mpd_rev;		/* the PHY's revision (MII_REV())*/
114221407Smarius	u_int mii_capmask;		/* capability mask for BMSR */
115221407Smarius	u_int mii_phy;			/* our MII address */
116221407Smarius	u_int mii_offset;		/* first PHY, second PHY, etc. */
117221407Smarius	u_int mii_inst;			/* instance for ifmedia */
11850120Swpaul
119221407Smarius	/* Our PHY functions. */
120221407Smarius	const struct mii_phy_funcs *mii_funcs;
121221407Smarius
12250120Swpaul	struct mii_data *mii_pdata;	/* pointer to parent's mii_data */
12350120Swpaul
124221407Smarius	u_int mii_flags;		/* misc. flags; see below */
125221407Smarius	u_int mii_capabilities;		/* capabilities from BMSR */
126221407Smarius	u_int mii_extcapabilities;	/* extended capabilities */
127221407Smarius	u_int mii_ticks;		/* MII_TICK counter */
128221407Smarius	u_int mii_anegticks;		/* ticks before retrying aneg */
129221407Smarius	u_int mii_media_active;		/* last active media */
130221407Smarius	u_int mii_media_status;		/* last active status */
13150120Swpaul};
13250120Swpaultypedef struct mii_softc mii_softc_t;
13350120Swpaul
13450120Swpaul/* mii_flags */
135213878Smarius#define	MIIF_INITDONE	0x00000001	/* has been initialized (mii_data) */
136213878Smarius#define	MIIF_NOISOLATE	0x00000002	/* do not isolate the PHY */
137221407Smarius#if 0
138213878Smarius#define	MIIF_NOLOOP	0x00000004	/* no loopback capability */
139221407Smarius#endif
140215297Smarius#define	MIIF_DOINGAUTO	0x00000008	/* doing autonegotiation (mii_softc) */
141213878Smarius#define	MIIF_AUTOTSLEEP	0x00000010	/* use tsleep(), not callout() */
142213878Smarius#define	MIIF_HAVEFIBER	0x00000020	/* from parent: has fiber interface */
143213878Smarius#define	MIIF_HAVE_GTCR	0x00000040	/* has 100base-T2/1000base-T CR */
144213878Smarius#define	MIIF_IS_1000X	0x00000080	/* is a 1000BASE-X device */
145213878Smarius#define	MIIF_DOPAUSE	0x00000100	/* advertise PAUSE capability */
146213878Smarius#define	MIIF_IS_HPNA	0x00000200	/* is a HomePNA device */
147213878Smarius#define	MIIF_FORCEANEG	0x00000400	/* force auto-negotiation */
148215297Smarius#define	MIIF_NOMANPAUSE	0x00100000	/* no manual PAUSE selection */
149220938Smarius#define	MIIF_FORCEPAUSE	0x00200000	/* force PAUSE advertisement */
150213878Smarius#define	MIIF_MACPRIV0	0x01000000	/* private to the MAC driver */
151213878Smarius#define	MIIF_MACPRIV1	0x02000000	/* private to the MAC driver */
152213878Smarius#define	MIIF_MACPRIV2	0x04000000	/* private to the MAC driver */
153213878Smarius#define	MIIF_PHYPRIV0	0x10000000	/* private to the PHY driver */
154213878Smarius#define	MIIF_PHYPRIV1	0x20000000	/* private to the PHY driver */
155213878Smarius#define	MIIF_PHYPRIV2	0x40000000	/* private to the PHY driver */
15650120Swpaul
157158648Soleg/* Default mii_anegticks values */
158158648Soleg#define	MII_ANEGTICKS		5
159158648Soleg#define	MII_ANEGTICKS_GIGE	17
160158648Soleg
16195707Sphk#define	MIIF_INHERIT_MASK	(MIIF_NOISOLATE|MIIF_NOLOOP|MIIF_AUTOTSLEEP)
16295707Sphk
16350120Swpaul/*
164213878Smarius * Special `locators' passed to mii_attach().  If one of these is not
165213878Smarius * an `any' value, we look for *that* PHY and configure it.  If both
166214605Smarius * are not `any', that is an error, and mii_attach() will fail.
167213878Smarius */
168213878Smarius#define	MII_OFFSET_ANY		-1
169213878Smarius#define	MII_PHY_ANY		-1
170213878Smarius
171213878Smarius/*
17250120Swpaul * Used to attach a PHY to a parent.
17350120Swpaul */
17450120Swpaulstruct mii_attach_args {
17550120Swpaul	struct mii_data *mii_data;	/* pointer to parent data */
176221407Smarius	u_int mii_phyno;		/* MII address */
177221407Smarius	u_int mii_offset;		/* first PHY, second PHY, etc. */
178221407Smarius	uint32_t mii_id1;		/* PHY ID register 1 */
179221407Smarius	uint32_t mii_id2;		/* PHY ID register 2 */
180221407Smarius	u_int mii_capmask;		/* capability mask for BMSR */
18150120Swpaul};
18250120Swpaultypedef struct mii_attach_args mii_attach_args_t;
18350120Swpaul
18495707Sphk/*
18595724Sphk * Used to match a PHY.
18695724Sphk */
18795724Sphkstruct mii_phydesc {
188221407Smarius	uint32_t mpd_oui;		/* the PHY's OUI */
189221407Smarius	uint32_t mpd_model;		/* the PHY's model */
19095724Sphk	const char *mpd_name;		/* the PHY's name */
19195724Sphk};
192150755Simp#define MII_PHY_DESC(a, b) { MII_OUI_ ## a, MII_MODEL_ ## a ## _ ## b, \
193150755Simp	MII_STR_ ## a ## _ ## b }
194150755Simp#define MII_PHY_END	{ 0, 0, NULL }
19595724Sphk
19695724Sphk/*
19795707Sphk * An array of these structures map MII media types to BMCR/ANAR settings.
19895707Sphk */
19995707Sphkstruct mii_media {
200221407Smarius	u_int	mm_bmcr;		/* BMCR settings for this media */
201221407Smarius	u_int	mm_anar;		/* ANAR settings for this media */
202221407Smarius	u_int	mm_gtcr;		/* 100base-T2 or 1000base-T CR */
20395707Sphk};
20495707Sphk
20595707Sphk#define	MII_MEDIA_NONE		0
20695707Sphk#define	MII_MEDIA_10_T		1
20795707Sphk#define	MII_MEDIA_10_T_FDX	2
20895707Sphk#define	MII_MEDIA_100_T4	3
20995707Sphk#define	MII_MEDIA_100_TX	4
21095707Sphk#define	MII_MEDIA_100_TX_FDX	5
21195707Sphk#define	MII_MEDIA_1000_X	6
21295707Sphk#define	MII_MEDIA_1000_X_FDX	7
21395707Sphk#define	MII_MEDIA_1000_T	8
21495707Sphk#define	MII_MEDIA_1000_T_FDX	9
21595707Sphk#define	MII_NMEDIA		10
21695707Sphk
21755205Speter#ifdef _KERNEL
21850120Swpaul
21950120Swpaul#define PHY_READ(p, r) \
22050120Swpaul	MIIBUS_READREG((p)->mii_dev, (p)->mii_phy, (r))
22150120Swpaul
22250120Swpaul#define PHY_WRITE(p, r, v) \
22350120Swpaul	MIIBUS_WRITEREG((p)->mii_dev, (p)->mii_phy, (r), (v))
22450120Swpaul
225221407Smarius#define	PHY_SERVICE(p, d, o) \
226221407Smarius	(*(p)->mii_funcs->pf_service)((p), (d), (o))
227221407Smarius
228221407Smarius#define	PHY_STATUS(p) \
229221407Smarius	(*(p)->mii_funcs->pf_status)(p)
230221407Smarius
231221407Smarius#define	PHY_RESET(p) \
232221407Smarius	(*(p)->mii_funcs->pf_reset)(p)
233221407Smarius
234213878Smariusenum miibus_device_ivars {
235213878Smarius	MIIBUS_IVAR_FLAGS
236213878Smarius};
237213878Smarius
238213878Smarius/*
239213878Smarius * Simplified accessors for miibus
240213878Smarius */
241213878Smarius#define	MIIBUS_ACCESSOR(var, ivar, type)				\
242213878Smarius	__BUS_ACCESSOR(miibus, var, MIIBUS, ivar, type)
243213878Smarius
244221407SmariusMIIBUS_ACCESSOR(flags,		FLAGS,		u_int)
245213878Smarius
24650120Swpaulextern devclass_t	miibus_devclass;
24750120Swpaulextern driver_t		miibus_driver;
24850120Swpaul
249213878Smariusint	mii_attach(device_t, device_t *, struct ifnet *, ifm_change_cb_t,
250213878Smarius	    ifm_stat_cb_t, int, int, int, int);
25195722Sphkvoid	mii_down(struct mii_data *);
25292739Salfredint	mii_mediachg(struct mii_data *);
25392739Salfredvoid	mii_tick(struct mii_data *);
25492739Salfredvoid	mii_pollstat(struct mii_data *);
25595718Sphkvoid	mii_phy_add_media(struct mii_softc *);
25650120Swpaul
25796026Sphkint	mii_phy_auto(struct mii_softc *);
25895722Sphkint	mii_phy_detach(device_t dev);
25995722Sphkvoid	mii_phy_down(struct mii_softc *);
260215297Smariusu_int	mii_phy_flowstatus(struct mii_softc *);
26192739Salfredvoid	mii_phy_reset(struct mii_softc *);
26295707Sphkvoid	mii_phy_setmedia(struct mii_softc *sc);
26392739Salfredvoid	mii_phy_update(struct mii_softc *, int);
26492739Salfredint	mii_phy_tick(struct mii_softc *);
26550120Swpaul
266150756Simpconst struct mii_phydesc * mii_phy_match(const struct mii_attach_args *ma,
267150756Simp    const struct mii_phydesc *mpd);
268150756Simpconst struct mii_phydesc * mii_phy_match_gen(const struct mii_attach_args *ma,
269150756Simp    const struct mii_phydesc *mpd, size_t endlen);
270164827Smariusint mii_phy_dev_probe(device_t dev, const struct mii_phydesc *mpd, int mrv);
271221407Smariusvoid mii_phy_dev_attach(device_t dev, u_int flags,
272221407Smarius    const struct mii_phy_funcs *mpf, int add_media);
27395724Sphk
27492739Salfredvoid	ukphy_status(struct mii_softc *);
275221407Smarius
276221407Smariusu_int	mii_oui(u_int, u_int);
277221407Smarius#define	MII_OUI(id1, id2)	mii_oui(id1, id2)
278221407Smarius#define	MII_MODEL(id2)		(((id2) & IDR2_MODEL) >> 4)
279221407Smarius#define	MII_REV(id2)		((id2) & IDR2_REV)
280221407Smarius
28155205Speter#endif /* _KERNEL */
28250120Swpaul
28350120Swpaul#endif /* _DEV_MII_MIIVAR_H_ */
284