etphy.c revision 1.1 1 1.1 jnemeth /* $NetBSD: etphy.c,v 1.1 2010/11/13 00:47:24 jnemeth Exp $ */
2 1.1 jnemeth /* $OpenBSD: etphy.c,v 1.4 2008/04/02 20:12:58 brad Exp $ */
3 1.1 jnemeth
4 1.1 jnemeth /*
5 1.1 jnemeth * Copyright (c) 2007 The DragonFly Project. All rights reserved.
6 1.1 jnemeth *
7 1.1 jnemeth * This code is derived from software contributed to The DragonFly Project
8 1.1 jnemeth * by Sepherosa Ziehau <sepherosa (at) gmail.com>
9 1.1 jnemeth *
10 1.1 jnemeth * Redistribution and use in source and binary forms, with or without
11 1.1 jnemeth * modification, are permitted provided that the following conditions
12 1.1 jnemeth * are met:
13 1.1 jnemeth *
14 1.1 jnemeth * 1. Redistributions of source code must retain the above copyright
15 1.1 jnemeth * notice, this list of conditions and the following disclaimer.
16 1.1 jnemeth * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jnemeth * notice, this list of conditions and the following disclaimer in
18 1.1 jnemeth * the documentation and/or other materials provided with the
19 1.1 jnemeth * distribution.
20 1.1 jnemeth * 3. Neither the name of The DragonFly Project nor the names of its
21 1.1 jnemeth * contributors may be used to endorse or promote products derived
22 1.1 jnemeth * from this software without specific, prior written permission.
23 1.1 jnemeth *
24 1.1 jnemeth * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 1.1 jnemeth * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 1.1 jnemeth * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 1.1 jnemeth * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 1.1 jnemeth * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 1.1 jnemeth * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 1.1 jnemeth * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 1.1 jnemeth * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 1.1 jnemeth * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 1.1 jnemeth * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 1.1 jnemeth * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 jnemeth * SUCH DAMAGE.
36 1.1 jnemeth *
37 1.1 jnemeth * $DragonFly: src/sys/dev/netif/mii_layer/truephy.c,v 1.1 2007/10/12 14:12:42 sephe Exp $
38 1.1 jnemeth */
39 1.1 jnemeth
40 1.1 jnemeth #include <sys/cdefs.h>
41 1.1 jnemeth __KERNEL_RCSID(0, "$NetBSD: etphy.c,v 1.1 2010/11/13 00:47:24 jnemeth Exp $");
42 1.1 jnemeth
43 1.1 jnemeth #include <sys/param.h>
44 1.1 jnemeth #include <sys/systm.h>
45 1.1 jnemeth #include <sys/kernel.h>
46 1.1 jnemeth #include <sys/device.h>
47 1.1 jnemeth #include <sys/socket.h>
48 1.1 jnemeth
49 1.1 jnemeth #include <net/if.h>
50 1.1 jnemeth #include <net/if_media.h>
51 1.1 jnemeth
52 1.1 jnemeth #include <dev/mii/mii.h>
53 1.1 jnemeth #include <dev/mii/miivar.h>
54 1.1 jnemeth #include <dev/mii/miidevs.h>
55 1.1 jnemeth
56 1.1 jnemeth #define ETPHY_INDEX 0x10 /* XXX reserved in DS */
57 1.1 jnemeth #define ETPHY_INDEX_MAGIC 0x402
58 1.1 jnemeth #define ETPHY_DATA 0x11 /* XXX reserved in DS */
59 1.1 jnemeth
60 1.1 jnemeth #define ETPHY_CTRL 0x12
61 1.1 jnemeth #define ETPHY_CTRL_DIAG 0x0004
62 1.1 jnemeth #define ETPHY_CTRL_RSV1 0x0002 /* XXX reserved */
63 1.1 jnemeth #define ETPHY_CTRL_RSV0 0x0001 /* XXX reserved */
64 1.1 jnemeth
65 1.1 jnemeth #define ETPHY_CONF 0x16
66 1.1 jnemeth #define ETPHY_CONF_TXFIFO_MASK 0x3000
67 1.1 jnemeth #define ETPHY_CONF_TXFIFO_8 0x0000
68 1.1 jnemeth #define ETPHY_CONF_TXFIFO_16 0x1000
69 1.1 jnemeth #define ETPHY_CONF_TXFIFO_24 0x2000
70 1.1 jnemeth #define ETPHY_CONF_TXFIFO_32 0x3000
71 1.1 jnemeth
72 1.1 jnemeth #define ETPHY_SR 0x1a
73 1.1 jnemeth #define ETPHY_SR_SPD_MASK 0x0300
74 1.1 jnemeth #define ETPHY_SR_SPD_1000T 0x0200
75 1.1 jnemeth #define ETPHY_SR_SPD_100TX 0x0100
76 1.1 jnemeth #define ETPHY_SR_SPD_10T 0x0000
77 1.1 jnemeth #define ETPHY_SR_FDX 0x0080
78 1.1 jnemeth
79 1.1 jnemeth
80 1.1 jnemeth int etphy_service(struct mii_softc *, struct mii_data *, int);
81 1.1 jnemeth void etphy_attach(device_t, device_t, void *);
82 1.1 jnemeth int etphy_match(device_t, cfdata_t, void *);
83 1.1 jnemeth void etphy_reset(struct mii_softc *);
84 1.1 jnemeth void etphy_status(struct mii_softc *);
85 1.1 jnemeth
86 1.1 jnemeth const struct mii_phy_funcs etphy_funcs = {
87 1.1 jnemeth etphy_service, etphy_status, etphy_reset,
88 1.1 jnemeth };
89 1.1 jnemeth
90 1.1 jnemeth static const struct mii_phydesc etphys[] = {
91 1.1 jnemeth { MII_OUI_AGERE, MII_MODEL_AGERE_ET1011,
92 1.1 jnemeth MII_STR_AGERE_ET1011 },
93 1.1 jnemeth { 0, 0,
94 1.1 jnemeth NULL },
95 1.1 jnemeth };
96 1.1 jnemeth
97 1.1 jnemeth CFATTACH_DECL_NEW(etphy, sizeof(struct mii_softc),
98 1.1 jnemeth etphy_match, etphy_attach, mii_phy_detach, mii_phy_activate);
99 1.1 jnemeth
100 1.1 jnemeth static const struct etphy_dsp {
101 1.1 jnemeth uint16_t index;
102 1.1 jnemeth uint16_t data;
103 1.1 jnemeth } etphy_dspcode[] = {
104 1.1 jnemeth { 0x880b, 0x0926 }, /* AfeIfCreg4B1000Msbs */
105 1.1 jnemeth { 0x880c, 0x0926 }, /* AfeIfCreg4B100Msbs */
106 1.1 jnemeth { 0x880d, 0x0926 }, /* AfeIfCreg4B10Msbs */
107 1.1 jnemeth
108 1.1 jnemeth { 0x880e, 0xb4d3 }, /* AfeIfCreg4B1000Lsbs */
109 1.1 jnemeth { 0x880f, 0xb4d3 }, /* AfeIfCreg4B100Lsbs */
110 1.1 jnemeth { 0x8810, 0xb4d3 }, /* AfeIfCreg4B10Lsbs */
111 1.1 jnemeth
112 1.1 jnemeth { 0x8805, 0xb03e }, /* AfeIfCreg3B1000Msbs */
113 1.1 jnemeth { 0x8806, 0xb03e }, /* AfeIfCreg3B100Msbs */
114 1.1 jnemeth { 0x8807, 0xff00 }, /* AfeIfCreg3B10Msbs */
115 1.1 jnemeth
116 1.1 jnemeth { 0x8808, 0xe090 }, /* AfeIfCreg3B1000Lsbs */
117 1.1 jnemeth { 0x8809, 0xe110 }, /* AfeIfCreg3B100Lsbs */
118 1.1 jnemeth { 0x880a, 0x0000 }, /* AfeIfCreg3B10Lsbs */
119 1.1 jnemeth
120 1.1 jnemeth { 0x300d, 1 }, /* DisableNorm */
121 1.1 jnemeth
122 1.1 jnemeth { 0x280c, 0x0180 }, /* LinkHoldEnd */
123 1.1 jnemeth
124 1.1 jnemeth { 0x1c21, 0x0002 }, /* AlphaM */
125 1.1 jnemeth
126 1.1 jnemeth { 0x3821, 6 }, /* FfeLkgTx0 */
127 1.1 jnemeth { 0x381d, 1 }, /* FfeLkg1g4 */
128 1.1 jnemeth { 0x381e, 1 }, /* FfeLkg1g5 */
129 1.1 jnemeth { 0x381f, 1 }, /* FfeLkg1g6 */
130 1.1 jnemeth { 0x3820, 1 }, /* FfeLkg1g7 */
131 1.1 jnemeth
132 1.1 jnemeth { 0x8402, 0x01f0 }, /* Btinact */
133 1.1 jnemeth { 0x800e, 20 }, /* LftrainTime */
134 1.1 jnemeth { 0x800f, 24 }, /* DvguardTime */
135 1.1 jnemeth { 0x8010, 46 } /* IdlguardTime */
136 1.1 jnemeth };
137 1.1 jnemeth
138 1.1 jnemeth int
139 1.1 jnemeth etphy_match(device_t parent, cfdata_t match, void *aux)
140 1.1 jnemeth {
141 1.1 jnemeth struct mii_attach_args *ma = aux;
142 1.1 jnemeth
143 1.1 jnemeth if (mii_phy_match(ma, etphys) != NULL)
144 1.1 jnemeth return 10;
145 1.1 jnemeth
146 1.1 jnemeth return 0;
147 1.1 jnemeth }
148 1.1 jnemeth
149 1.1 jnemeth void
150 1.1 jnemeth etphy_attach(device_t parent, device_t self, void *aux)
151 1.1 jnemeth {
152 1.1 jnemeth struct mii_softc *sc = device_private(self);
153 1.1 jnemeth struct mii_attach_args *ma = aux;
154 1.1 jnemeth struct mii_data *mii = ma->mii_data;
155 1.1 jnemeth const struct mii_phydesc *mpd;
156 1.1 jnemeth
157 1.1 jnemeth mpd = mii_phy_match(ma, etphys);
158 1.1 jnemeth aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
159 1.1 jnemeth
160 1.1 jnemeth sc->mii_dev = self;
161 1.1 jnemeth sc->mii_inst = mii->mii_instance;
162 1.1 jnemeth sc->mii_phy = ma->mii_phyno;
163 1.1 jnemeth sc->mii_funcs = &etphy_funcs;
164 1.1 jnemeth sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
165 1.1 jnemeth sc->mii_pdata = mii;
166 1.1 jnemeth sc->mii_flags = ma->mii_flags;
167 1.1 jnemeth
168 1.1 jnemeth sc->mii_flags |= MIIF_NOISOLATE | MIIF_NOLOOP;
169 1.1 jnemeth
170 1.1 jnemeth PHY_RESET(sc);
171 1.1 jnemeth
172 1.1 jnemeth sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
173 1.1 jnemeth if (sc->mii_capabilities & BMSR_EXTSTAT) {
174 1.1 jnemeth sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
175 1.1 jnemeth /* No 1000baseT half-duplex support */
176 1.1 jnemeth sc->mii_extcapabilities &= ~EXTSR_1000THDX;
177 1.1 jnemeth }
178 1.1 jnemeth aprint_normal_dev(self, "");
179 1.1 jnemeth if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
180 1.1 jnemeth aprint_error("no media present");
181 1.1 jnemeth else
182 1.1 jnemeth mii_phy_add_media(sc);
183 1.1 jnemeth aprint_normal("\n");
184 1.1 jnemeth
185 1.1 jnemeth if (!pmf_device_register(self, NULL, mii_phy_resume))
186 1.1 jnemeth aprint_error_dev(self, "couldn't establish power handler\n");
187 1.1 jnemeth }
188 1.1 jnemeth
189 1.1 jnemeth int
190 1.1 jnemeth etphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
191 1.1 jnemeth {
192 1.1 jnemeth struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
193 1.1 jnemeth int bmcr;
194 1.1 jnemeth
195 1.1 jnemeth switch (cmd) {
196 1.1 jnemeth case MII_POLLSTAT:
197 1.1 jnemeth /*
198 1.1 jnemeth * If we're not polling our PHY instance, just return.
199 1.1 jnemeth */
200 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst)
201 1.1 jnemeth return 0;
202 1.1 jnemeth break;
203 1.1 jnemeth
204 1.1 jnemeth case MII_MEDIACHG:
205 1.1 jnemeth /*
206 1.1 jnemeth * If the media indicates a different PHY instance,
207 1.1 jnemeth * isolate ourselves.
208 1.1 jnemeth */
209 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
210 1.1 jnemeth bmcr = PHY_READ(sc, MII_BMCR);
211 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
212 1.1 jnemeth return 0;
213 1.1 jnemeth }
214 1.1 jnemeth
215 1.1 jnemeth /*
216 1.1 jnemeth * If the interface is not up, don't do anything.
217 1.1 jnemeth */
218 1.1 jnemeth if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
219 1.1 jnemeth break;
220 1.1 jnemeth
221 1.1 jnemeth if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
222 1.1 jnemeth bmcr = PHY_READ(sc, MII_BMCR) & ~BMCR_AUTOEN;
223 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr);
224 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_PDOWN);
225 1.1 jnemeth }
226 1.1 jnemeth
227 1.1 jnemeth mii_phy_setmedia(sc);
228 1.1 jnemeth
229 1.1 jnemeth if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
230 1.1 jnemeth bmcr = PHY_READ(sc, MII_BMCR) & ~BMCR_PDOWN;
231 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr);
232 1.1 jnemeth
233 1.1 jnemeth if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {
234 1.1 jnemeth PHY_WRITE(sc, MII_BMCR,
235 1.1 jnemeth bmcr | BMCR_AUTOEN | BMCR_STARTNEG);
236 1.1 jnemeth }
237 1.1 jnemeth }
238 1.1 jnemeth break;
239 1.1 jnemeth
240 1.1 jnemeth case MII_TICK:
241 1.1 jnemeth /*
242 1.1 jnemeth * If we're not currently selected, just return.
243 1.1 jnemeth */
244 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst)
245 1.1 jnemeth return 0;
246 1.1 jnemeth
247 1.1 jnemeth if (mii_phy_tick(sc) == EJUSTRETURN)
248 1.1 jnemeth return 0;
249 1.1 jnemeth break;
250 1.1 jnemeth }
251 1.1 jnemeth
252 1.1 jnemeth /* Update the media status. */
253 1.1 jnemeth mii_phy_status(sc);
254 1.1 jnemeth
255 1.1 jnemeth /* Callback if something changed. */
256 1.1 jnemeth mii_phy_update(sc, cmd);
257 1.1 jnemeth return 0;
258 1.1 jnemeth }
259 1.1 jnemeth
260 1.1 jnemeth void
261 1.1 jnemeth etphy_reset(struct mii_softc *sc)
262 1.1 jnemeth {
263 1.1 jnemeth int i;
264 1.1 jnemeth
265 1.1 jnemeth for (i = 0; i < 2; ++i) {
266 1.1 jnemeth PHY_READ(sc, MII_PHYIDR1);
267 1.1 jnemeth PHY_READ(sc, MII_PHYIDR2);
268 1.1 jnemeth
269 1.1 jnemeth PHY_READ(sc, ETPHY_CTRL);
270 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL,
271 1.1 jnemeth ETPHY_CTRL_DIAG | ETPHY_CTRL_RSV1);
272 1.1 jnemeth
273 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, ETPHY_INDEX_MAGIC);
274 1.1 jnemeth PHY_READ(sc, ETPHY_DATA);
275 1.1 jnemeth
276 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL, ETPHY_CTRL_RSV1);
277 1.1 jnemeth }
278 1.1 jnemeth
279 1.1 jnemeth PHY_READ(sc, MII_BMCR);
280 1.1 jnemeth PHY_READ(sc, ETPHY_CTRL);
281 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_PDOWN | BMCR_S1000);
282 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL,
283 1.1 jnemeth ETPHY_CTRL_DIAG | ETPHY_CTRL_RSV1 | ETPHY_CTRL_RSV0);
284 1.1 jnemeth
285 1.1 jnemeth #define N(arr) (int)(sizeof(arr) / sizeof(arr[0]))
286 1.1 jnemeth
287 1.1 jnemeth for (i = 0; i < N(etphy_dspcode); ++i) {
288 1.1 jnemeth const struct etphy_dsp *dsp = &etphy_dspcode[i];
289 1.1 jnemeth
290 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, dsp->index);
291 1.1 jnemeth PHY_WRITE(sc, ETPHY_DATA, dsp->data);
292 1.1 jnemeth
293 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, dsp->index);
294 1.1 jnemeth PHY_READ(sc, ETPHY_DATA);
295 1.1 jnemeth }
296 1.1 jnemeth
297 1.1 jnemeth #undef N
298 1.1 jnemeth
299 1.1 jnemeth PHY_READ(sc, MII_BMCR);
300 1.1 jnemeth PHY_READ(sc, ETPHY_CTRL);
301 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_S1000);
302 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL, ETPHY_CTRL_RSV1);
303 1.1 jnemeth
304 1.1 jnemeth mii_phy_reset(sc);
305 1.1 jnemeth }
306 1.1 jnemeth
307 1.1 jnemeth void
308 1.1 jnemeth etphy_status(struct mii_softc *sc)
309 1.1 jnemeth {
310 1.1 jnemeth struct mii_data *mii = sc->mii_pdata;
311 1.1 jnemeth int bmsr, bmcr, sr;
312 1.1 jnemeth
313 1.1 jnemeth mii->mii_media_status = IFM_AVALID;
314 1.1 jnemeth mii->mii_media_active = IFM_ETHER;
315 1.1 jnemeth
316 1.1 jnemeth sr = PHY_READ(sc, ETPHY_SR);
317 1.1 jnemeth bmcr = PHY_READ(sc, MII_BMCR);
318 1.1 jnemeth
319 1.1 jnemeth bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
320 1.1 jnemeth if (bmsr & BMSR_LINK)
321 1.1 jnemeth mii->mii_media_status |= IFM_ACTIVE;
322 1.1 jnemeth
323 1.1 jnemeth if (bmcr & BMCR_AUTOEN) {
324 1.1 jnemeth if ((bmsr & BMSR_ACOMP) == 0) {
325 1.1 jnemeth mii->mii_media_active |= IFM_NONE;
326 1.1 jnemeth return;
327 1.1 jnemeth }
328 1.1 jnemeth }
329 1.1 jnemeth
330 1.1 jnemeth switch (sr & ETPHY_SR_SPD_MASK) {
331 1.1 jnemeth case ETPHY_SR_SPD_1000T:
332 1.1 jnemeth mii->mii_media_active |= IFM_1000_T;
333 1.1 jnemeth break;
334 1.1 jnemeth case ETPHY_SR_SPD_100TX:
335 1.1 jnemeth mii->mii_media_active |= IFM_100_TX;
336 1.1 jnemeth break;
337 1.1 jnemeth case ETPHY_SR_SPD_10T:
338 1.1 jnemeth mii->mii_media_active |= IFM_10_T;
339 1.1 jnemeth break;
340 1.1 jnemeth default:
341 1.1 jnemeth mii->mii_media_active |= IFM_NONE;
342 1.1 jnemeth return;
343 1.1 jnemeth }
344 1.1 jnemeth
345 1.1 jnemeth if (sr & ETPHY_SR_FDX)
346 1.1 jnemeth mii->mii_media_active |= IFM_FDX;
347 1.1 jnemeth else
348 1.1 jnemeth mii->mii_media_active |= IFM_HDX;
349 1.1 jnemeth }
350