xref: /openbsd/sys/dev/mii/urlphy.c (revision 57f47a24)
1 /*	$OpenBSD: urlphy.c,v 1.2 2002/05/07 19:50:01 nate Exp $ */
2 /*	$NetBSD: urlphy.c,v 1.1 2002/03/28 21:07:53 ichiro Exp $	*/
3 /*
4  * Copyright (c) 2001, 2002
5  *     Shingo WATANABE <nabe@nabechan.org>.  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, this list of conditions and the following 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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Shingo WATANABE.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 /*
37  * driver for Realtek RL8150L internal phy
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 
47 #include <net/if.h>
48 #include <net/if_media.h>
49 
50 #include <dev/mii/mii.h>
51 #include <dev/mii/miivar.h>
52 #include <dev/mii/miidevs.h>
53 #include <dev/mii/urlphyreg.h>
54 
55 #define	URLPHY_DEBUG	0
56 #ifdef URLPHY_DEBUG
57 #define DPRINTF(x)	if (urlphydebug) printf x
58 #define DPRINTFN(n,x)	if (urlphydebug>(n)) printf x
59 int urlphydebug = URLPHY_DEBUG;
60 #else
61 #define DPRINTF(x)
62 #define DPRINTFN(n,x)
63 #endif
64 
65 int urlphy_match(struct device *, void *, void *);
66 void urlphy_attach(struct device *, struct device *, void *);
67 
68 struct cfattach urlphy_ca = {
69 	sizeof(struct mii_softc), urlphy_match, urlphy_attach, mii_phy_detach,
70 	mii_phy_activate
71 };
72 
73 struct cfdriver urlphy_cd = {
74 	NULL, "urlphy", DV_DULL
75 };
76 
77 int urlphy_service(struct mii_softc *, struct mii_data *, int);
78 void urlphy_status(struct mii_softc *);
79 
80 int
81 urlphy_match(struct device *parent, void *match, void *aux)
82 {
83 	struct mii_attach_args *ma = aux;
84 
85 	/*
86 	 * RTL8150 reports OUT == 0, MODEL == 0
87 	 */
88 	if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 &&
89 	    MII_MODEL(ma->mii_id2) != 0)
90 		return (0);
91 
92 	/*
93 	 * Make sure the parent is an 'url' device.
94 	 */
95 	if (strcmp(parent->dv_cfdata->cf_driver->cd_name, "url") != 0)
96 		return(0);
97 
98 	return (10);
99 }
100 
101 void
102 urlphy_attach(struct device *parent, struct device *self, void *aux)
103 {
104 	struct mii_softc *sc = (struct mii_softc *)self;
105 	struct mii_attach_args *ma = aux;
106 	struct mii_data *mii = ma->mii_data;
107 
108 	DPRINTF(("%s: %s: enter\n", sc->mii_dev.dv_xname, __FUNCTION__));
109 
110 	sc->mii_inst = mii->mii_instance;
111 	sc->mii_phy = ma->mii_phyno;
112 	sc->mii_service = urlphy_service;
113 	sc->mii_status = urlphy_status;
114 	sc->mii_pdata = mii;
115 	sc->mii_flags = mii->mii_flags;
116 	sc->mii_anegticks = 10;
117 
118 	/* Don't do loopback on this PHY. */
119 	sc->mii_flags |= MIIF_NOLOOP;
120 	/* Don't do isolate on this PHY. */
121 	sc->mii_flags |= MIIF_NOISOLATE;
122 
123 	if (mii->mii_instance != 0) {
124 		printf("%s: ignoring this PHY, non-zero instance\n",
125 		       sc->mii_dev.dv_xname);
126 		return;
127 	}
128 	mii_phy_reset(sc);
129 
130 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
131 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
132 		mii_phy_add_media(sc);
133 }
134 
135 int
136 urlphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
137 {
138 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
139 	int reg;
140 
141 	DPRINTF(("%s: %s: enter\n", sc->mii_dev.dv_xname, __FUNCTION__));
142 
143 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
144 		return (ENXIO);
145 
146 	switch (cmd) {
147 	case MII_POLLSTAT:
148 		/*
149 		 * If we're not polling our PHY instance, just return.
150 		 */
151 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
152 			return (0);
153 		break;
154 
155 	case MII_MEDIACHG:
156 		/*
157 		 * If we're not currently selected, just return.
158 		 */
159 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
160 			return (0);
161 
162 		/* If the interface is not up, don't do anything. */
163 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
164 			break;
165 
166 		mii_phy_setmedia(sc);
167 		break;
168 
169 	case MII_TICK:
170 		/*
171 		 * If we're not currently selected, just return.
172 		 */
173 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
174 			return (0);
175 
176 		/* Just bail now if the interface is down. */
177 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
178 			return (0);
179 
180 		/*
181 		 * If we're not doing autonegotiation, we don't need to do
182 		 * any extra work here.  However, we need to check the link
183 		 * status so we can generate an announcement if the status
184 		 * changes.
185 		 */
186 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
187 			return (0);
188 
189 		/* Read the status register twice; MSR_LINK is latch-low. */
190 		reg = PHY_READ(sc, URLPHY_MSR) | PHY_READ(sc, URLPHY_MSR);
191 		if (reg & URLPHY_MSR_LINK)
192 			return (0);
193 
194 		/*
195 		 * Only retry autonegotiation every N seconds.
196 		 */
197 		KASSERT(sc->mii_anegticks != 0);
198 		if (++sc->mii_ticks != sc->mii_anegticks)
199 			return (0);
200 
201 		sc->mii_ticks = 0;
202 		mii_phy_reset(sc);
203 
204 		if (mii_phy_auto(sc, 0) == EJUSTRETURN)
205 			return (0);
206 
207 		break;
208 
209 	case MII_DOWN:
210 		mii_phy_down(sc);
211 		return (0);
212 	}
213 
214 	/* Update the media status. */
215 	mii_phy_status(sc);
216 
217 	/* Callback if something changed. */
218 	mii_phy_update(sc, cmd);
219 
220 	return (0);
221 }
222 
223 void
224 urlphy_status(struct mii_softc *sc)
225 {
226 	struct mii_data *mii = sc->mii_pdata;
227 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
228 	int msr, bmsr, bmcr;
229 
230 	DPRINTF(("%s: %s: enter\n", sc->mii_dev.dv_xname, __FUNCTION__));
231 
232 	mii->mii_media_status = IFM_AVALID;
233 	mii->mii_media_active = IFM_ETHER;
234 
235 	/*
236 	 * The link status bit is not exist in the BMSR register,
237 	 * so we need to read the MSR register to get link status.
238 	 */
239 	msr = PHY_READ(sc, URLPHY_MSR) | PHY_READ(sc, URLPHY_MSR);
240 	if (msr & URLPHY_MSR_LINK)
241 		mii->mii_media_status |= IFM_ACTIVE;
242 
243 	DPRINTF(("%s: %s: link %s\n", sc->mii_dev.dv_xname, __FUNCTION__,
244 		 mii->mii_media_status & IFM_ACTIVE ? "up" : "down"));
245 
246 	bmcr = PHY_READ(sc, MII_BMCR);
247 	if (bmcr & BMCR_AUTOEN) {
248 		bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
249 		if ((bmsr & BMSR_ACOMP) == 0) {
250 			/* Erg, still trying, I guess... */
251 			mii->mii_media_active |= IFM_NONE;
252 			return;
253 		}
254 
255 		if (msr & URLPHY_MSR_SPEED_100)
256 			mii->mii_media_active |= IFM_100_TX;
257 		else
258 			mii->mii_media_active |= IFM_10_T;
259 		if (msr & URLPHY_MSR_DUPLEX)
260 			mii->mii_media_active |= IFM_FDX;
261 	} else
262 		mii->mii_media_active = ife->ifm_media;
263 }
264