ipgphy.c revision 1.7 1 /* $NetBSD: ipgphy.c,v 1.7 2019/11/27 08:53:32 msaitoh Exp $ */
2 /* $OpenBSD: ipgphy.c,v 1.19 2015/07/19 06:28:12 yuo Exp $ */
3
4 /*-
5 * Copyright (c) 2006, Pyun YongHyeon <yongari (at) FreeBSD.org>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
13 * disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32 /*
33 * Driver for the IC Plus IP1000A/IP1001 10/100/1000 PHY.
34 */
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: ipgphy.c,v 1.7 2019/11/27 08:53:32 msaitoh Exp $");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/socket.h>
43 #include <sys/errno.h>
44
45 #include <net/if.h>
46 #include <net/if_media.h>
47
48 #include <dev/mii/mii.h>
49 #include <dev/mii/miivar.h>
50 #include <dev/mii/miidevs.h>
51
52 #include <dev/mii/ipgphyreg.h>
53
54 #include <dev/pci/if_stgereg.h>
55
56 static int ipgphy_match(device_t, cfdata_t, void *);
57 static void ipgphy_attach(device_t, device_t, void *);
58
59 CFATTACH_DECL_NEW(ipgphy, sizeof(struct mii_softc),
60 ipgphy_match, ipgphy_attach, mii_phy_detach, mii_phy_activate);
61
62 static int ipgphy_service(struct mii_softc *, struct mii_data *, int);
63 static void ipgphy_status(struct mii_softc *);
64 static int ipgphy_mii_phy_auto(struct mii_softc *, u_int);
65 static void ipgphy_load_dspcode(struct mii_softc *);
66 static void ipgphy_reset(struct mii_softc *);
67
68 static const struct mii_phy_funcs ipgphy_funcs = {
69 ipgphy_service, ipgphy_status, ipgphy_reset,
70 };
71
72 static const struct mii_phydesc ipgphys[] = {
73 MII_PHY_DESC(xxICPLUS, IP1000A),
74 MII_PHY_DESC(xxICPLUS, IP1001),
75 MII_PHY_END,
76 };
77
78 static int
79 ipgphy_match(device_t parent, cfdata_t match, void *aux)
80 {
81 struct mii_attach_args *ma = aux;
82
83 if (mii_phy_match(ma, ipgphys) != NULL)
84 return 10;
85
86 return 0;
87 }
88
89 static void
90 ipgphy_attach(device_t parent, device_t self, void *aux)
91 {
92 struct mii_softc *sc = device_private(self);
93 struct mii_attach_args *ma = aux;
94 struct mii_data *mii = ma->mii_data;
95 const struct mii_phydesc *mpd;
96
97 mpd = mii_phy_match(ma, ipgphys);
98 aprint_naive(": Media interface\n");
99 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
100
101 sc->mii_dev = self;
102 sc->mii_inst = mii->mii_instance;
103 sc->mii_phy = ma->mii_phyno;
104 sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
105 sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
106 sc->mii_mpd_rev = MII_REV(ma->mii_id2);
107 sc->mii_funcs = &ipgphy_funcs;
108 sc->mii_pdata = mii;
109 sc->mii_flags = ma->mii_flags;
110
111 sc->mii_flags |= MIIF_NOISOLATE;
112 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
113
114 PHY_RESET(sc);
115
116 PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
117 sc->mii_capabilities &= ma->mii_capmask;
118 //sc->mii_capabilities &= ~BMSR_ANEG;
119 if (sc->mii_capabilities & BMSR_EXTSTAT)
120 PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
121
122 aprint_normal_dev(self, "");
123 mii_phy_add_media(sc);
124 aprint_normal("\n");
125 }
126
127 static int
128 ipgphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
129 {
130 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
131 uint16_t reg, speed;
132
133 switch (cmd) {
134 case MII_POLLSTAT:
135 /* If we're not polling our PHY instance, just return. */
136 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
137 return 0;
138 break;
139
140 case MII_MEDIACHG:
141 /*
142 * If the media indicates a different PHY instance,
143 * isolate ourselves.
144 */
145 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
146 PHY_READ(sc, MII_BMCR, ®);
147 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
148 return 0;
149 }
150
151 /* If the interface is not up, don't do anything. */
152 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
153 break;
154
155 PHY_RESET(sc);
156
157 switch (IFM_SUBTYPE(ife->ifm_media)) {
158 case IFM_AUTO:
159 case IFM_1000_T:
160 /*
161 * This device is required to do auto negotiation
162 * on 1000BASE-T.
163 */
164 (void)ipgphy_mii_phy_auto(sc, ife->ifm_media);
165 goto done;
166 break;
167
168 case IFM_100_TX:
169 speed = BMCR_S100;
170 break;
171
172 case IFM_10_T:
173 speed = BMCR_S10;
174 break;
175
176 default:
177 return EINVAL;
178 }
179
180 if ((ife->ifm_media & IFM_FDX) != 0)
181 speed |= BMCR_FDX;
182
183 PHY_WRITE(sc, MII_100T2CR, 0);
184 PHY_WRITE(sc, MII_BMCR, speed);
185 done:
186 break;
187
188 case MII_TICK:
189 /* If we're not currently selected, just return. */
190 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
191 return 0;
192
193 /* Is the interface even up? */
194 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
195 return 0;
196
197 /* Only used for autonegotiation. */
198 if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) &&
199 (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) {
200 sc->mii_ticks = 0;
201 break;
202 }
203
204 /*
205 * Check to see if we have link. If we do, we don't
206 * need to restart the autonegotiation process. Read
207 * the BMSR twice in case it's latched.
208 */
209 PHY_READ(sc, MII_BMSR, ®);
210 PHY_READ(sc, MII_BMSR, ®);
211 if (reg & BMSR_LINK) {
212 /*
213 * Reset autonegotiation timer to 0 in case the link
214 * goes down in the next tick.
215 */
216 sc->mii_ticks = 0;
217 /* See above. */
218 break;
219 }
220
221 /* Announce link loss right after it happens */
222 if (sc->mii_ticks++ == 0)
223 break;
224
225 /* Only retry autonegotiation every mii_anegticks seconds. */
226 if (sc->mii_ticks <= sc->mii_anegticks)
227 break;
228
229 sc->mii_ticks = 0;
230 ipgphy_mii_phy_auto(sc, ife->ifm_media);
231 break;
232 }
233
234 /* Update the media status. */
235 ipgphy_status(sc);
236
237 /* Callback if something changed. */
238 mii_phy_update(sc, cmd);
239 return 0;
240 }
241
242 static void
243 ipgphy_status(struct mii_softc *sc)
244 {
245 struct mii_data *mii = sc->mii_pdata;
246 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
247 uint16_t bmsr, bmcr, stat, gtsr;
248
249 /* For IP1000A, use generic way */
250 if (sc->mii_mpd_model == MII_MODEL_xxICPLUS_IP1000A) {
251 ukphy_status(sc);
252 return;
253 }
254
255 mii->mii_media_status = IFM_AVALID;
256 mii->mii_media_active = IFM_ETHER;
257
258 PHY_READ(sc, MII_BMSR, &bmsr);
259 PHY_READ(sc, MII_BMSR, &bmsr);
260 if (bmsr & BMSR_LINK)
261 mii->mii_media_status |= IFM_ACTIVE;
262
263 PHY_READ(sc, MII_BMCR, &bmcr);
264 if (bmcr & BMCR_LOOP)
265 mii->mii_media_active |= IFM_LOOP;
266
267 if (bmcr & BMCR_AUTOEN) {
268 if ((bmsr & BMSR_ACOMP) == 0) {
269 /* Erg, still trying, I guess... */
270 mii->mii_media_active |= IFM_NONE;
271 return;
272 }
273
274 PHY_READ(sc, IPGPHY_LSR, &stat);
275 switch (stat & IPGPHY_LSR_SPEED_MASK) {
276 case IPGPHY_LSR_SPEED_10:
277 mii->mii_media_active |= IFM_10_T;
278 break;
279 case IPGPHY_LSR_SPEED_100:
280 mii->mii_media_active |= IFM_100_TX;
281 break;
282 case IPGPHY_LSR_SPEED_1000:
283 mii->mii_media_active |= IFM_1000_T;
284 break;
285 default:
286 mii->mii_media_active |= IFM_NONE;
287 return;
288 }
289
290 if (stat & IPGPHY_LSR_FULL_DUPLEX)
291 mii->mii_media_active |= IFM_FDX;
292 else
293 mii->mii_media_active |= IFM_HDX;
294
295 if (mii->mii_media_active & IFM_FDX)
296 mii->mii_media_active |= mii_phy_flowstatus(sc);
297
298 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
299 PHY_READ(sc, MII_100T2SR, >sr);
300 if (gtsr & GTSR_MS_RES)
301 mii->mii_media_active |= IFM_ETH_MASTER;
302 }
303 } else
304 mii->mii_media_active = ife->ifm_media;
305 }
306
307 static int
308 ipgphy_mii_phy_auto(struct mii_softc *sc, u_int media)
309 {
310 uint16_t reg = 0;
311 u_int subtype = IFM_SUBTYPE(media);
312
313 /* XXX Is it requreid ? */
314 if (sc->mii_mpd_model == MII_MODEL_xxICPLUS_IP1001) {
315 PHY_READ(sc, MII_ANAR, ®);
316 reg &= ~(ANAR_PAUSE_SYM | ANAR_PAUSE_ASYM);
317 reg |= ANAR_NP;
318 }
319
320 if (subtype == IFM_AUTO)
321 reg |= ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD;
322
323 if (sc->mii_flags & MIIF_DOPAUSE)
324 reg |= ANAR_PAUSE_SYM | ANAR_PAUSE_ASYM;
325
326 PHY_WRITE(sc, MII_ANAR, reg | ANAR_CSMA);
327
328 if (subtype == IFM_AUTO)
329 reg = GTCR_ADV_1000TFDX | GTCR_ADV_1000THDX;
330 else if (subtype == IFM_1000_T) {
331 if ((media & IFM_FDX) != 0)
332 reg = GTCR_ADV_1000TFDX;
333 else
334 reg = GTCR_ADV_1000THDX;
335 } else
336 reg = 0;
337
338 PHY_WRITE(sc, MII_100T2CR, reg);
339
340 PHY_WRITE(sc, MII_BMCR, BMCR_FDX | BMCR_AUTOEN | BMCR_STARTNEG);
341
342 return EJUSTRETURN;
343 }
344
345 static void
346 ipgphy_load_dspcode(struct mii_softc *sc)
347 {
348 PHY_WRITE(sc, 31, 0x0001);
349 PHY_WRITE(sc, 27, 0x01e0);
350 PHY_WRITE(sc, 31, 0x0002);
351 PHY_WRITE(sc, 27, 0xeb8e);
352 PHY_WRITE(sc, 31, 0x0000);
353 PHY_WRITE(sc, 30, 0x005e);
354 PHY_WRITE(sc, 9, 0x0700);
355
356 DELAY(50);
357 }
358
359 static void
360 ipgphy_reset(struct mii_softc *sc)
361 {
362 struct ifnet *ifp = sc->mii_pdata->mii_ifp;
363 uint16_t reg;
364
365 mii_phy_reset(sc);
366
367 /* Clear autoneg/full-duplex as we don't want it after reset */
368 PHY_READ(sc, MII_BMCR, ®);
369 reg &= ~(BMCR_AUTOEN | BMCR_FDX);
370 PHY_WRITE(sc, MII_BMCR, reg);
371
372 if (sc->mii_mpd_model == MII_MODEL_xxICPLUS_IP1000A &&
373 strcmp(ifp->if_xname, "stge") == 0) {
374 struct stge_softc *stge_sc = ifp->if_softc;
375 if (stge_sc->sc_rev >= 0x40 && stge_sc->sc_rev <= 0x4e)
376 ipgphy_load_dspcode(sc);
377 }
378 }
379