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