ikphy.c revision 1.10 1 /* $NetBSD: ikphy.c,v 1.10 2014/06/16 16:48:16 msaitoh Exp $ */
2
3 /*******************************************************************************
4 Copyright (c) 2001-2005, Intel Corporation
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 are met:
9
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12
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 3. Neither the name of the Intel Corporation nor the names of its
18 contributors may be used to endorse or promote products derived from
19 this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32 *******************************************************************************/
33 /*
34 * Copyright (c) 2006 Manuel Bouyer. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 */
56
57 /*
58 * driver for Intel's i82563 ethernet 10/100/1000 PHY
59 */
60
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: ikphy.c,v 1.10 2014/06/16 16:48:16 msaitoh Exp $");
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/device.h>
68 #include <sys/socket.h>
69 #include <sys/errno.h>
70
71 #include <net/if.h>
72 #include <net/if_media.h>
73
74 #include <dev/mii/mii.h>
75 #include <dev/mii/miivar.h>
76 #include <dev/mii/miidevs.h>
77
78 #include <dev/mii/ikphyreg.h>
79
80 static int ikphymatch(device_t, cfdata_t, void *);
81 static void ikphyattach(device_t, device_t, void *);
82
83 CFATTACH_DECL_NEW(ikphy, sizeof(struct mii_softc),
84 ikphymatch, ikphyattach, mii_phy_detach, mii_phy_activate);
85
86 static int ikphy_service(struct mii_softc *, struct mii_data *, int);
87 static void ikphy_status(struct mii_softc *);
88 static void ikphy_setmedia(struct mii_softc *);
89
90 static const struct mii_phy_funcs ikphy_funcs = {
91 ikphy_service, ikphy_status, mii_phy_reset,
92 };
93
94 static const struct mii_phydesc ikphys[] = {
95 { MII_OUI_xxMARVELL, MII_MODEL_xxMARVELL_I82563,
96 MII_STR_xxMARVELL_I82563 },
97
98 { 0, 0,
99 NULL },
100 };
101
102 static int
103 ikphymatch(device_t parent, cfdata_t match, void *aux)
104 {
105 struct mii_attach_args *ma = aux;
106
107 if (mii_phy_match(ma, ikphys) != NULL)
108 return (10);
109
110 return (0);
111 }
112
113 static void
114 ikphyattach(device_t parent, device_t self, void *aux)
115 {
116 struct mii_softc *sc = device_private(self);
117 struct mii_attach_args *ma = aux;
118 struct mii_data *mii = ma->mii_data;
119 const struct mii_phydesc *mpd;
120
121 mpd = mii_phy_match(ma, ikphys);
122 aprint_naive(": Media interface\n");
123 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
124
125 sc->mii_dev = self;
126 sc->mii_inst = mii->mii_instance;
127 sc->mii_phy = ma->mii_phyno;
128 sc->mii_funcs = &ikphy_funcs;
129 sc->mii_pdata = mii;
130 sc->mii_flags = ma->mii_flags;
131 sc->mii_anegticks = MII_ANEGTICKS;
132
133 PHY_RESET(sc);
134
135 sc->mii_capabilities =
136 PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
137 if (sc->mii_capabilities & BMSR_EXTSTAT)
138 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
139 aprint_normal_dev(self, "");
140 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
141 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
142 aprint_error("no media present");
143 else
144 mii_phy_add_media(sc);
145 aprint_normal("\n");
146 }
147
148 static int
149 ikphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
150 {
151 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
152 int reg;
153
154 switch (cmd) {
155 case MII_POLLSTAT:
156 /*
157 * If we're not polling our PHY instance, just return.
158 */
159 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
160 return (0);
161 break;
162
163 case MII_MEDIACHG:
164 /*
165 * If the media indicates a different PHY instance,
166 * isolate ourselves.
167 */
168 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
169 reg = PHY_READ(sc, MII_BMCR);
170 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
171 return (0);
172 }
173
174 /*
175 * If the interface is not up, don't do anything.
176 */
177 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
178 break;
179
180 ikphy_setmedia(sc);
181 break;
182
183 case MII_TICK:
184 /*
185 * If we're not currently selected, just return.
186 */
187 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
188 return (0);
189
190 if (mii_phy_tick(sc) == EJUSTRETURN)
191 return (0);
192 break;
193
194 case MII_DOWN:
195 mii_phy_down(sc);
196 return (0);
197 }
198
199 /* Update the media status. */
200 mii_phy_status(sc);
201
202 /* Callback if something changed. */
203 mii_phy_update(sc, cmd);
204 return (0);
205 }
206
207 static void
208 ikphy_setmedia(struct mii_softc *sc)
209 {
210 uint16_t phy_data;
211 struct mii_data *mii = sc->mii_pdata;
212 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
213
214 /* Enable CRS on TX for half-duplex operation. */
215 phy_data = PHY_READ(sc, GG82563_PHY_MAC_SPEC_CTRL);
216 phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
217 /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
218 phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
219 PHY_WRITE(sc, GG82563_PHY_MAC_SPEC_CTRL, phy_data);
220
221 /* set mdi/mid-x options */
222 phy_data = PHY_READ(sc, GG82563_PHY_SPEC_CTRL);
223 phy_data &= ~GG82563_PSCR_CROSSOVER_MODE_MASK;
224 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO)
225 phy_data |= GG82563_PSCR_CROSSOVER_MODE_AUTO;
226 else
227 phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDI;
228 /* set polarity correction */
229 phy_data &= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
230 PHY_WRITE(sc, GG82563_PHY_SPEC_CTRL, phy_data);
231
232 /* SW Reset the PHY so all changes take effect */
233 PHY_RESET(sc);
234
235 /* for the i80003 */
236 phy_data = PHY_READ(sc, GG82563_PHY_SPEC_CTRL_2);
237 phy_data &= ~GG82563_PSCR2_REVERSE_AUTO_NEG;
238 PHY_WRITE(sc, GG82563_PHY_SPEC_CTRL_2, phy_data);
239
240 /* Enable Electrical Idle on the PHY */
241 phy_data = PHY_READ(sc, GG82563_PHY_PWR_MGMT_CTRL);
242 phy_data |= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE;
243 PHY_WRITE(sc, GG82563_PHY_PWR_MGMT_CTRL, phy_data);
244
245 phy_data = PHY_READ(sc, GG82563_PHY_KMRN_MODE_CTRL);
246 phy_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
247 PHY_WRITE(sc, GG82563_PHY_KMRN_MODE_CTRL, phy_data);
248
249 /*
250 * Workaround: Disable padding in Kumeran interface in the MAC
251 * and in the PHY to avoid CRC errors.
252 */
253 phy_data = PHY_READ(sc, GG82563_PHY_INBAND_CTRL);
254 phy_data |= GG82563_ICR_DIS_PADDING;
255 PHY_WRITE(sc, GG82563_PHY_INBAND_CTRL, phy_data);
256
257 mii_phy_setmedia(sc);
258 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
259 /*
260 * when not in auto mode, we need to restart nego
261 * anyway, or a switch from a fixed mode to another
262 * fixed mode may not be seen by the switch.
263 */
264 PHY_WRITE(sc, MII_BMCR,
265 PHY_READ(sc, MII_BMCR) | BMCR_STARTNEG);
266 }
267 phy_data = PHY_READ(sc, GG82563_PHY_MAC_SPEC_CTRL);
268 phy_data &= ~GG82563_MSCR_TX_CLK_MASK;
269 switch(IFM_SUBTYPE(ife->ifm_media)) {
270 case IFM_10_T:
271 phy_data |= GG82563_MSCR_TX_CLK_10MBPS_2_5MHZ;
272 break;
273 case IFM_100_TX:
274 phy_data |= GG82563_MSCR_TX_CLK_100MBPS_25MHZ;
275 break;
276 case IFM_1000_T:
277 phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
278 break;
279 }
280 phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
281 PHY_WRITE(sc, GG82563_PHY_MAC_SPEC_CTRL, phy_data);
282 }
283
284 static void
285 ikphy_status(struct mii_softc *sc)
286 {
287 struct mii_data *mii = sc->mii_pdata;
288 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
289 int pssr, bmcr, gtsr, kmrn;
290
291 mii->mii_media_status = IFM_AVALID;
292 mii->mii_media_active = IFM_ETHER;
293
294 pssr = PHY_READ(sc, GG82563_PHY_SPEC_STATUS);
295
296 if (pssr & GG82563_PSSR_LINK)
297 mii->mii_media_status |= IFM_ACTIVE;
298
299 bmcr = PHY_READ(sc, MII_BMCR);
300 if (bmcr & BMCR_ISO) {
301 mii->mii_media_active |= IFM_NONE;
302 mii->mii_media_status = 0;
303 return;
304 }
305
306 if (bmcr & BMCR_LOOP)
307 mii->mii_media_active |= IFM_LOOP;
308
309 if (bmcr & BMCR_AUTOEN) {
310 /*
311 * The media status bits are only valid of autonegotiation
312 * has completed (or it's disabled).
313 */
314 if ((pssr & GG82563_PSSR_SPEED_DUPLEX_RESOLVED) == 0) {
315 /* Erg, still trying, I guess... */
316 mii->mii_media_active |= IFM_NONE;
317 return;
318 }
319
320 switch (pssr & GG82563_PSSR_SPEED_MASK) {
321 case GG82563_PSSR_SPEED_1000MBPS:
322 mii->mii_media_active |= IFM_1000_T;
323 gtsr = PHY_READ(sc, MII_100T2SR);
324 if (gtsr & GTSR_MS_RES)
325 mii->mii_media_active |= IFM_ETH_MASTER;
326 break;
327
328 case GG82563_PSSR_SPEED_100MBPS:
329 mii->mii_media_active |= IFM_100_TX;
330 break;
331
332 case GG82563_PSSR_SPEED_10MBPS:
333 mii->mii_media_active |= IFM_10_T;
334 break;
335
336 default:
337 mii->mii_media_active |= IFM_NONE;
338 mii->mii_media_status = 0;
339 return;
340 }
341
342 if (pssr & GG82563_PSSR_DUPLEX)
343 mii->mii_media_active |=
344 IFM_FDX | mii_phy_flowstatus(sc);
345 else
346 mii->mii_media_active |= IFM_HDX;
347 } else
348 mii->mii_media_active = ife->ifm_media;
349 kmrn = PHY_READ(sc, GG82563_PHY_KMRN_MODE_CTRL);
350 if (mii->mii_media_active & IFM_FDX)
351 kmrn &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
352 else
353 kmrn |= GG82563_KMCR_PASS_FALSE_CARRIER;
354 PHY_WRITE(sc, GG82563_PHY_KMRN_MODE_CTRL, kmrn);
355 }
356