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