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