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