1 /* $OpenBSD: urlphy.c,v 1.18 2022/04/06 18:59:29 naddy 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_var.h>
45 #include <net/if_media.h>
46
47 #include <dev/mii/mii.h>
48 #include <dev/mii/miivar.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 const struct cfattach urlphy_ca = {
65 sizeof(struct mii_softc), urlphy_match, urlphy_attach, mii_phy_detach
66 };
67
68 struct cfdriver urlphy_cd = {
69 NULL, "urlphy", DV_DULL
70 };
71
72 int urlphy_service(struct mii_softc *, struct mii_data *, int);
73 void urlphy_status(struct mii_softc *);
74
75 const struct mii_phy_funcs urlphy_funcs = {
76 urlphy_service, urlphy_status, mii_phy_reset,
77 };
78
79 int
urlphy_match(struct device * parent,void * match,void * aux)80 urlphy_match(struct device *parent, void *match, void *aux)
81 {
82 struct mii_attach_args *ma = aux;
83
84 /*
85 * RTL8150 reports OUT == 0, MODEL == 0
86 */
87 if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 &&
88 MII_MODEL(ma->mii_id2) != 0)
89 return (0);
90
91 /*
92 * Make sure the parent is an 'url' device.
93 */
94 if (strcmp(parent->dv_cfdata->cf_driver->cd_name, "url") != 0)
95 return (0);
96
97 return (10);
98 }
99
100 void
urlphy_attach(struct device * parent,struct device * self,void * aux)101 urlphy_attach(struct device *parent, struct device *self, void *aux)
102 {
103 struct mii_softc *sc = (struct mii_softc *)self;
104 struct mii_attach_args *ma = aux;
105 struct mii_data *mii = ma->mii_data;
106
107 printf(": RTL internal phy\n");
108
109 DPRINTF(("%s: %s: enter\n", sc->mii_dev.dv_xname, __func__));
110
111 sc->mii_inst = mii->mii_instance;
112 sc->mii_phy = ma->mii_phyno;
113 sc->mii_funcs = &urlphy_funcs;
114 sc->mii_pdata = mii;
115 sc->mii_flags = ma->mii_flags;
116 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
117
118 sc->mii_flags |= MIIF_NOISOLATE | MIIF_NOLOOP;
119
120 if (mii->mii_instance != 0) {
121 printf("%s: ignoring this PHY, non-zero instance\n",
122 sc->mii_dev.dv_xname);
123 return;
124 }
125 PHY_RESET(sc);
126
127 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
128 if (sc->mii_capabilities & BMSR_MEDIAMASK)
129 mii_phy_add_media(sc);
130 }
131
132 int
urlphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)133 urlphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
134 {
135 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
136 int reg;
137
138 DPRINTF(("%s: %s: enter\n", sc->mii_dev.dv_xname, __func__));
139
140 if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
141 return (ENXIO);
142
143 switch (cmd) {
144 case MII_POLLSTAT:
145 /*
146 * If we're not polling our PHY instance, just return.
147 */
148 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
149 return (0);
150 break;
151
152 case MII_MEDIACHG:
153 /*
154 * If we're not currently selected, just return.
155 */
156 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
157 return (0);
158
159 /* If the interface is not up, don't do anything. */
160 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
161 break;
162
163 mii_phy_setmedia(sc);
164 break;
165
166 case MII_TICK:
167 /*
168 * If we're not currently selected, just return.
169 */
170 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
171 return (0);
172
173 /* Just bail now if the interface is down. */
174 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
175 return (0);
176
177 /*
178 * If we're not doing autonegotiation, we don't need to do
179 * any extra work here. However, we need to check the link
180 * status so we can generate an announcement if the status
181 * changes.
182 */
183 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
184 return (0);
185
186 /* Read the status register twice; MSR_LINK is latch-low. */
187 reg = PHY_READ(sc, URLPHY_MSR) | PHY_READ(sc, URLPHY_MSR);
188 if (reg & URLPHY_MSR_LINK) {
189 sc->mii_ticks = 0;
190 break;
191 }
192
193 /*
194 * Only retry autonegotiation every mii_anegticks seconds.
195 */
196 if (++sc->mii_ticks <= sc->mii_anegticks)
197 return (0);
198
199 sc->mii_ticks = 0;
200 PHY_RESET(sc);
201
202 if (mii_phy_auto(sc, 0) == EJUSTRETURN)
203 return (0);
204
205 break;
206
207 case MII_DOWN:
208 mii_phy_down(sc);
209 return (0);
210 }
211
212 /* Update the media status. */
213 mii_phy_status(sc);
214
215 /* Callback if something changed. */
216 mii_phy_update(sc, cmd);
217
218 return (0);
219 }
220
221 void
urlphy_status(struct mii_softc * sc)222 urlphy_status(struct mii_softc *sc)
223 {
224 struct mii_data *mii = sc->mii_pdata;
225 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
226 int msr, bmsr, bmcr;
227
228 DPRINTF(("%s: %s: enter\n", sc->mii_dev.dv_xname, __func__));
229
230 mii->mii_media_status = IFM_AVALID;
231 mii->mii_media_active = IFM_ETHER;
232
233 /*
234 * The link status bit is not exist in the BMSR register,
235 * so we need to read the MSR register to get link status.
236 */
237 msr = PHY_READ(sc, URLPHY_MSR) | PHY_READ(sc, URLPHY_MSR);
238 if (msr & URLPHY_MSR_LINK)
239 mii->mii_media_status |= IFM_ACTIVE;
240
241 DPRINTF(("%s: %s: link %s\n", sc->mii_dev.dv_xname, __func__,
242 mii->mii_media_status & IFM_ACTIVE ? "up" : "down"));
243
244 bmcr = PHY_READ(sc, MII_BMCR);
245 if (bmcr & BMCR_AUTOEN) {
246 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
247 if ((bmsr & BMSR_ACOMP) == 0) {
248 /* Erg, still trying, I guess... */
249 mii->mii_media_active |= IFM_NONE;
250 return;
251 }
252
253 if (msr & URLPHY_MSR_SPEED_100)
254 mii->mii_media_active |= IFM_100_TX;
255 else
256 mii->mii_media_active |= IFM_10_T;
257
258 if (msr & URLPHY_MSR_DUPLEX)
259 mii->mii_media_active |= IFM_FDX;
260 else
261 mii->mii_media_active |= IFM_HDX;
262 } else
263 mii->mii_media_active = ife->ifm_media;
264 }
265