ipgphy.c revision 1.4 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.4 2019/11/14 09:06:21 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 *, u_int);
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 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 case IFM_1000_T:
158 /*
159 * This device is required to do auto negotiation
160 * on 1000BASE-T.
161 */
162 (void)ipgphy_mii_phy_auto(sc, ife->ifm_media);
163 goto done;
164 break;
165
166 case IFM_100_TX:
167 speed = BMCR_S100;
168 break;
169
170 case IFM_10_T:
171 speed = BMCR_S10;
172 break;
173
174 default:
175 return EINVAL;
176 }
177
178 if ((ife->ifm_media & IFM_FDX) != 0)
179 speed |= BMCR_FDX;
180
181 PHY_WRITE(sc, MII_100T2CR, 0);
182 PHY_WRITE(sc, MII_BMCR, speed);
183 done:
184 break;
185
186 case MII_TICK:
187 /* If we're not currently selected, just return. */
188 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
189 return 0;
190
191 /* Is the interface even up? */
192 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
193 return 0;
194
195 /* Only used for autonegotiation. */
196 if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) &&
197 (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) {
198 sc->mii_ticks = 0;
199 break;
200 }
201
202 /*
203 * Check to see if we have link. If we do, we don't
204 * need to restart the autonegotiation process. Read
205 * the BMSR twice in case it's latched.
206 */
207 PHY_READ(sc, MII_BMSR, ®);
208 PHY_READ(sc, MII_BMSR, ®);
209 if (reg & BMSR_LINK) {
210 /*
211 * Reset autonegotiation timer to 0 in case the link
212 * goes down in the next tick.
213 */
214 sc->mii_ticks = 0;
215 /* See above. */
216 break;
217 }
218
219 /* Announce link loss right after it happens */
220 if (sc->mii_ticks++ == 0)
221 break;
222
223 /* Only retry autonegotiation every mii_anegticks seconds. */
224 if (sc->mii_ticks <= sc->mii_anegticks)
225 break;
226
227 sc->mii_ticks = 0;
228 ipgphy_mii_phy_auto(sc, ife->ifm_media);
229 break;
230 }
231
232 /* Update the media status. */
233 ipgphy_status(sc);
234
235 /* Callback if something changed. */
236 mii_phy_update(sc, cmd);
237 return 0;
238 }
239
240 static void
241 ipgphy_status(struct mii_softc *sc)
242 {
243 struct mii_data *mii = sc->mii_pdata;
244 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
245 uint16_t bmsr, bmcr, stat, gtsr;
246
247 /* For IP1000A, use generic way */
248 if (sc->mii_mpd_model == MII_MODEL_xxICPLUS_IP1000A) {
249 ukphy_status(sc);
250 return;
251 }
252
253 mii->mii_media_status = IFM_AVALID;
254 mii->mii_media_active = IFM_ETHER;
255
256 PHY_READ(sc, MII_BMSR, &bmsr);
257 PHY_READ(sc, MII_BMSR, &bmsr);
258 if (bmsr & BMSR_LINK)
259 mii->mii_media_status |= IFM_ACTIVE;
260
261 PHY_READ(sc, MII_BMCR, &bmcr);
262 if (bmcr & BMCR_LOOP)
263 mii->mii_media_active |= IFM_LOOP;
264
265 if (bmcr & BMCR_AUTOEN) {
266 if ((bmsr & BMSR_ACOMP) == 0) {
267 /* Erg, still trying, I guess... */
268 mii->mii_media_active |= IFM_NONE;
269 return;
270 }
271
272 PHY_READ(sc, IPGPHY_LSR, &stat);
273 switch (stat & IPGPHY_LSR_SPEED_MASK) {
274 case IPGPHY_LSR_SPEED_10:
275 mii->mii_media_active |= IFM_10_T;
276 break;
277 case IPGPHY_LSR_SPEED_100:
278 mii->mii_media_active |= IFM_100_TX;
279 break;
280 case IPGPHY_LSR_SPEED_1000:
281 mii->mii_media_active |= IFM_1000_T;
282 break;
283 default:
284 mii->mii_media_active |= IFM_NONE;
285 return;
286 }
287
288 if (stat & IPGPHY_LSR_FULL_DUPLEX)
289 mii->mii_media_active |= IFM_FDX;
290 else
291 mii->mii_media_active |= IFM_HDX;
292
293 if (mii->mii_media_active & IFM_FDX)
294 mii->mii_media_active |= mii_phy_flowstatus(sc);
295
296 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
297 PHY_READ(sc, MII_100T2SR, >sr);
298 if (gtsr & GTSR_MS_RES)
299 mii->mii_media_active |= IFM_ETH_MASTER;
300 }
301 } else
302 mii->mii_media_active = ife->ifm_media;
303 }
304
305 static int
306 ipgphy_mii_phy_auto(struct mii_softc *sc, u_int media)
307 {
308 uint16_t reg = 0;
309 u_int subtype = IFM_SUBTYPE(media);
310
311 /* XXX Is it requreid ? */
312 if (sc->mii_mpd_model == MII_MODEL_xxICPLUS_IP1001) {
313 PHY_READ(sc, MII_ANAR, ®);
314 reg &= ~(ANAR_PAUSE_SYM | ANAR_PAUSE_ASYM);
315 reg |= ANAR_NP;
316 }
317
318 if (subtype == IFM_AUTO)
319 reg |= ANAR_10 | ANAR_10_FD | ANAR_TX | ANAR_TX_FD;
320
321 if (sc->mii_flags & MIIF_DOPAUSE)
322 reg |= ANAR_PAUSE_SYM | ANAR_PAUSE_ASYM;
323
324 PHY_WRITE(sc, MII_ANAR, reg | ANAR_CSMA);
325
326 if (subtype == IFM_AUTO)
327 reg = GTCR_ADV_1000TFDX | GTCR_ADV_1000THDX;
328 else if (subtype == IFM_1000_T) {
329 if ((media & IFM_FDX) != 0)
330 reg = GTCR_ADV_1000TFDX;
331 else
332 reg = GTCR_ADV_1000THDX;
333 } else
334 reg = 0;
335
336 PHY_WRITE(sc, MII_100T2CR, reg);
337
338 PHY_WRITE(sc, MII_BMCR, BMCR_FDX | BMCR_AUTOEN | BMCR_STARTNEG);
339
340 return EJUSTRETURN;
341 }
342
343 static void
344 ipgphy_load_dspcode(struct mii_softc *sc)
345 {
346 PHY_WRITE(sc, 31, 0x0001);
347 PHY_WRITE(sc, 27, 0x01e0);
348 PHY_WRITE(sc, 31, 0x0002);
349 PHY_WRITE(sc, 27, 0xeb8e);
350 PHY_WRITE(sc, 31, 0x0000);
351 PHY_WRITE(sc, 30, 0x005e);
352 PHY_WRITE(sc, 9, 0x0700);
353
354 DELAY(50);
355 }
356
357 static void
358 ipgphy_reset(struct mii_softc *sc)
359 {
360 struct ifnet *ifp = sc->mii_pdata->mii_ifp;
361 uint16_t reg;
362
363 mii_phy_reset(sc);
364
365 /* Clear autoneg/full-duplex as we don't want it after reset */
366 PHY_READ(sc, MII_BMCR, ®);
367 reg &= ~(BMCR_AUTOEN | BMCR_FDX);
368 PHY_WRITE(sc, MII_BMCR, reg);
369
370 if (sc->mii_mpd_model == MII_MODEL_xxICPLUS_IP1000A &&
371 strcmp(ifp->if_xname, "stge") == 0) {
372 struct stge_softc *stge_sc = ifp->if_softc;
373 if (stge_sc->sc_rev >= 0x40 && stge_sc->sc_rev <= 0x4e)
374 ipgphy_load_dspcode(sc);
375 }
376 }
377