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