xref: /freebsd/sys/dev/mii/axphy.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009 M. Warner Losh <imp@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * driver for internal phy in the AX88x9x chips.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/socket.h>
40 #include <sys/bus.h>
41 
42 #include <net/if.h>
43 #include <net/if_media.h>
44 
45 #include <dev/mii/mii.h>
46 #include <dev/mii/miivar.h>
47 #include "miidevs.h"
48 
49 #include "miibus_if.h"
50 
51 static int 	axphy_probe(device_t dev);
52 static int 	axphy_attach(device_t dev);
53 
54 static device_method_t axphy_methods[] = {
55 	/* device interface */
56 	DEVMETHOD(device_probe,		axphy_probe),
57 	DEVMETHOD(device_attach,	axphy_attach),
58 	DEVMETHOD(device_detach,	mii_phy_detach),
59 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
60 	DEVMETHOD_END
61 };
62 
63 static driver_t axphy_driver = {
64 	"axphy",
65 	axphy_methods,
66 	sizeof(struct mii_softc)
67 };
68 
69 DRIVER_MODULE(axphy, miibus, axphy_driver, 0, 0);
70 
71 static int	axphy_service(struct mii_softc *, struct mii_data *, int);
72 static void	axphy_status(struct mii_softc *);
73 
74 static const struct mii_phydesc axphys[] = {
75 	MII_PHY_DESC(xxASIX, AX88X9X),
76 	MII_PHY_END
77 };
78 
79 static const struct mii_phy_funcs axphy_funcs = {
80 	axphy_service,
81 	axphy_status,
82 	mii_phy_reset
83 };
84 
85 static int
86 axphy_probe(device_t dev)
87 {
88 
89 	return (mii_phy_dev_probe(dev, axphys, BUS_PROBE_DEFAULT));
90 }
91 
92 static int
93 axphy_attach(device_t dev)
94 {
95 	struct mii_softc *sc;
96 
97 	sc = device_get_softc(dev);
98 
99 	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE,
100 	    &axphy_funcs, 1);
101 	mii_phy_setmedia(sc);
102 
103 	return (0);
104 }
105 
106 static int
107 axphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
108 {
109 
110 	switch (cmd) {
111 	case MII_POLLSTAT:
112 		break;
113 
114 	case MII_MEDIACHG:
115 		mii_phy_setmedia(sc);
116 		break;
117 
118 	case MII_TICK:
119 		if (mii_phy_tick(sc) == EJUSTRETURN)
120 			return (0);
121 		break;
122 	}
123 
124 	/* Update the media status. */
125 	PHY_STATUS(sc);
126 
127 	/* Callback if something changed. */
128 	mii_phy_update(sc, cmd);
129 	return (0);
130 }
131 
132 static void
133 axphy_status(struct mii_softc *sc)
134 {
135 	struct mii_data *mii = sc->mii_pdata;
136 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
137 	int bmsr, bmcr;
138 
139 	mii->mii_media_status = IFM_AVALID;
140 	mii->mii_media_active = IFM_ETHER;
141 
142 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
143 	if (bmsr & BMSR_LINK)
144 		mii->mii_media_status |= IFM_ACTIVE;
145 
146 	bmcr = PHY_READ(sc, MII_BMCR);
147 	if (bmcr & BMCR_ISO) {
148 		mii->mii_media_active |= IFM_NONE;
149 		mii->mii_media_status = 0;
150 		return;
151 	}
152 
153 	if (bmcr & BMCR_LOOP)
154 		mii->mii_media_active |= IFM_LOOP;
155 
156 	if (bmcr & BMCR_AUTOEN) {
157 		if ((bmsr & BMSR_ACOMP) == 0) {
158 			mii->mii_media_active |= IFM_NONE;
159 			return;
160 		}
161 
162 #if 0
163 		scr = PHY_READ(sc, MII_AXPHY_SCR);
164 		if (scr & SCR_S100)
165 			mii->mii_media_active |= IFM_100_TX;
166 		else
167 			mii->mii_media_active |= IFM_10_T;
168 		if (scr & SCR_FDX)
169 			mii->mii_media_active |=
170 			    IFM_FDX | mii_phy_flowstatus(sc);
171 		else
172 			mii->mii_media_active |= IFM_HDX;
173 #endif
174 	} else
175 		mii->mii_media_active = ife->ifm_media;
176 }
177