xref: /freebsd/sys/dev/mii/axphy.c (revision e1e636193db45630c7881246d25902e57c43d24e)
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 /*
30  * driver for internal phy in the AX88x9x chips.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/socket.h>
38 #include <sys/bus.h>
39 
40 #include <net/if.h>
41 #include <net/if_media.h>
42 
43 #include <dev/mii/mii.h>
44 #include <dev/mii/miivar.h>
45 #include "miidevs.h"
46 
47 #include "miibus_if.h"
48 
49 static int 	axphy_probe(device_t dev);
50 static int 	axphy_attach(device_t dev);
51 
52 static device_method_t axphy_methods[] = {
53 	/* device interface */
54 	DEVMETHOD(device_probe,		axphy_probe),
55 	DEVMETHOD(device_attach,	axphy_attach),
56 	DEVMETHOD(device_detach,	mii_phy_detach),
57 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
58 	DEVMETHOD_END
59 };
60 
61 static driver_t axphy_driver = {
62 	"axphy",
63 	axphy_methods,
64 	sizeof(struct mii_softc)
65 };
66 
67 DRIVER_MODULE(axphy, miibus, axphy_driver, 0, 0);
68 
69 static int	axphy_service(struct mii_softc *, struct mii_data *, int);
70 static void	axphy_status(struct mii_softc *);
71 
72 static const struct mii_phydesc axphys[] = {
73 	MII_PHY_DESC(xxASIX, AX88X9X),
74 	MII_PHY_END
75 };
76 
77 static const struct mii_phy_funcs axphy_funcs = {
78 	axphy_service,
79 	axphy_status,
80 	mii_phy_reset
81 };
82 
83 static int
84 axphy_probe(device_t dev)
85 {
86 
87 	return (mii_phy_dev_probe(dev, axphys, BUS_PROBE_DEFAULT));
88 }
89 
90 static int
91 axphy_attach(device_t dev)
92 {
93 	struct mii_softc *sc;
94 
95 	sc = device_get_softc(dev);
96 
97 	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE,
98 	    &axphy_funcs, 1);
99 	mii_phy_setmedia(sc);
100 
101 	return (0);
102 }
103 
104 static int
105 axphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
106 {
107 
108 	switch (cmd) {
109 	case MII_POLLSTAT:
110 		break;
111 
112 	case MII_MEDIACHG:
113 		mii_phy_setmedia(sc);
114 		break;
115 
116 	case MII_TICK:
117 		if (mii_phy_tick(sc) == EJUSTRETURN)
118 			return (0);
119 		break;
120 	}
121 
122 	/* Update the media status. */
123 	PHY_STATUS(sc);
124 
125 	/* Callback if something changed. */
126 	mii_phy_update(sc, cmd);
127 	return (0);
128 }
129 
130 static void
131 axphy_status(struct mii_softc *sc)
132 {
133 	struct mii_data *mii = sc->mii_pdata;
134 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
135 	int bmsr, bmcr;
136 
137 	mii->mii_media_status = IFM_AVALID;
138 	mii->mii_media_active = IFM_ETHER;
139 
140 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
141 	if (bmsr & BMSR_LINK)
142 		mii->mii_media_status |= IFM_ACTIVE;
143 
144 	bmcr = PHY_READ(sc, MII_BMCR);
145 	if (bmcr & BMCR_ISO) {
146 		mii->mii_media_active |= IFM_NONE;
147 		mii->mii_media_status = 0;
148 		return;
149 	}
150 
151 	if (bmcr & BMCR_LOOP)
152 		mii->mii_media_active |= IFM_LOOP;
153 
154 	if (bmcr & BMCR_AUTOEN) {
155 		if ((bmsr & BMSR_ACOMP) == 0) {
156 			mii->mii_media_active |= IFM_NONE;
157 			return;
158 		}
159 
160 #if 0
161 		scr = PHY_READ(sc, MII_AXPHY_SCR);
162 		if (scr & SCR_S100)
163 			mii->mii_media_active |= IFM_100_TX;
164 		else
165 			mii->mii_media_active |= IFM_10_T;
166 		if (scr & SCR_FDX)
167 			mii->mii_media_active |=
168 			    IFM_FDX | mii_phy_flowstatus(sc);
169 		else
170 			mii->mii_media_active |= IFM_HDX;
171 #endif
172 	} else
173 		mii->mii_media_active = ife->ifm_media;
174 }
175