xref: /freebsd/sys/dev/mii/truephy.c (revision 3157ba21)
1 /*-
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sepherosa Ziehau <sepherosa@gmail.com>
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sys/dev/netif/mii_layer/truephy.c,v 1.3 2008/02/10 07:29:27 sephe Exp $
35  * $FreeBSD$
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/socket.h>
42 #include <sys/errno.h>
43 #include <sys/module.h>
44 #include <sys/bus.h>
45 
46 #include <net/if.h>
47 #include <net/if_media.h>
48 #include <net/if_arp.h>
49 #include <net/ethernet.h>
50 #include <net/if_vlan_var.h>
51 
52 #include <dev/mii/mii.h>
53 #include <dev/mii/miivar.h>
54 #include "miidevs.h"
55 
56 #include <dev/mii/truephyreg.h>
57 
58 #include "miibus_if.h"
59 
60 #define	TRUEPHY_FRAMELEN(mtu)	\
61     (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + (mtu) + ETHER_CRC_LEN)
62 
63 static int	truephy_service(struct mii_softc *, struct mii_data *, int);
64 static int	truephy_attach(device_t);
65 static int	truephy_probe(device_t);
66 static void	truephy_reset(struct mii_softc *);
67 static void	truephy_status(struct mii_softc *);
68 
69 static device_method_t truephy_methods[] = {
70 	/* device interface */
71 	DEVMETHOD(device_probe,		truephy_probe),
72 	DEVMETHOD(device_attach,	truephy_attach),
73 	DEVMETHOD(device_detach,	mii_phy_detach),
74 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
75 	{ 0, 0 }
76 };
77 
78 static const struct mii_phydesc truephys[] = {
79 	MII_PHY_DESC(AGERE,	ET1011),
80 	MII_PHY_DESC(AGERE,	ET1011C),
81 	MII_PHY_END
82 };
83 
84 static devclass_t truephy_devclass;
85 
86 static driver_t truephy_driver = {
87 	"truephy",
88 	truephy_methods,
89 	sizeof(struct mii_softc)
90 };
91 
92 DRIVER_MODULE(truephy, miibus, truephy_driver, truephy_devclass, 0, 0);
93 
94 static const struct truephy_dsp {
95 	uint16_t	index;
96 	uint16_t	data;
97 } truephy_dspcode[] = {
98 	{ 0x880b,	0x0926 },	/* AfeIfCreg4B1000Msbs */
99 	{ 0x880c,	0x0926 },	/* AfeIfCreg4B100Msbs */
100 	{ 0x880d,	0x0926 },	/* AfeIfCreg4B10Msbs */
101 
102 	{ 0x880e,	0xb4d3 },	/* AfeIfCreg4B1000Lsbs */
103 	{ 0x880f,	0xb4d3 },	/* AfeIfCreg4B100Lsbs */
104 	{ 0x8810,	0xb4d3 },	/* AfeIfCreg4B10Lsbs */
105 
106 	{ 0x8805,	0xb03e },	/* AfeIfCreg3B1000Msbs */
107 	{ 0x8806,	0xb03e },	/* AfeIfCreg3B100Msbs */
108 	{ 0x8807,	0xff00 },	/* AfeIfCreg3B10Msbs */
109 
110 	{ 0x8808,	0xe090 },	/* AfeIfCreg3B1000Lsbs */
111 	{ 0x8809,	0xe110 },	/* AfeIfCreg3B100Lsbs */
112 	{ 0x880a,	0x0000 },	/* AfeIfCreg3B10Lsbs */
113 
114 	{ 0x300d,	1      },	/* DisableNorm */
115 
116 	{ 0x280c,	0x0180 },	/* LinkHoldEnd */
117 
118 	{ 0x1c21,	0x0002 },	/* AlphaM */
119 
120 	{ 0x3821,	6      },	/* FfeLkgTx0 */
121 	{ 0x381d,	1      },	/* FfeLkg1g4 */
122 	{ 0x381e,	1      },	/* FfeLkg1g5 */
123 	{ 0x381f,	1      },	/* FfeLkg1g6 */
124 	{ 0x3820,	1      },	/* FfeLkg1g7 */
125 
126 	{ 0x8402,	0x01f0 },	/* Btinact */
127 	{ 0x800e,	20     },	/* LftrainTime */
128 	{ 0x800f,	24     },	/* DvguardTime */
129 	{ 0x8010,	46     }	/* IdlguardTime */
130 };
131 
132 static int
133 truephy_probe(device_t dev)
134 {
135 
136 	return (mii_phy_dev_probe(dev, truephys, BUS_PROBE_DEFAULT));
137 }
138 
139 static int
140 truephy_attach(device_t dev)
141 {
142 	struct mii_softc *sc;
143 	struct mii_attach_args *ma;
144 	struct mii_data *mii;
145 
146 	sc = device_get_softc(dev);
147 	ma = device_get_ivars(dev);
148 
149 	sc->mii_phy = ma->mii_phyno;
150 	if (sc->mii_anegticks == 0)
151 		sc->mii_anegticks = MII_ANEGTICKS;
152 	sc->mii_dev = device_get_parent(dev);
153 	mii = device_get_softc(sc->mii_dev);
154 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
155 
156 	sc->mii_inst = mii->mii_instance;
157 	sc->mii_phy = ma->mii_phyno;
158 	sc->mii_service = truephy_service;
159 	sc->mii_pdata = mii;
160 
161 	sc->mii_flags |= MIIF_NOISOLATE | MIIF_NOLOOP;
162 
163 	mii->mii_instance++;
164 
165 	if (MII_MODEL(ma->mii_id2) == MII_MODEL_AGERE_ET1011)
166 		mii_phy_reset(sc);
167 	else
168 		truephy_reset(sc);
169 
170 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
171 	if (sc->mii_capabilities & BMSR_EXTSTAT) {
172 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
173 		/* No 1000baseT half-duplex support */
174 		sc->mii_extcapabilities &= ~EXTSR_1000THDX;
175 	}
176 
177 	device_printf(dev, " ");
178 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
179 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
180 		printf("no media present");
181 	else
182 		mii_phy_add_media(sc);
183 	printf("\n");
184 
185 	MIIBUS_MEDIAINIT(sc->mii_dev);
186 	return 0;
187 }
188 
189 static int
190 truephy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
191 {
192 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
193 	int bmcr;
194 
195 	switch (cmd) {
196 	case MII_POLLSTAT:
197 		/*
198 		 * If we're not polling our PHY instance, just return.
199 		 */
200 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
201 			return 0;
202 		break;
203 
204 	case MII_MEDIACHG:
205 		/*
206 		 * If the media indicates a different PHY instance,
207 		 * isolate ourselves.
208 		 */
209 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
210 			bmcr = PHY_READ(sc, MII_BMCR);
211 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
212 			return 0;
213 		}
214 
215 		/*
216 		 * If the interface is not up, don't do anything.
217 		 */
218 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
219 			break;
220 
221 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
222 			bmcr = PHY_READ(sc, MII_BMCR) & ~BMCR_AUTOEN;
223 			PHY_WRITE(sc, MII_BMCR, bmcr);
224 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_PDOWN);
225 		}
226 
227 		mii_phy_setmedia(sc);
228 
229 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
230 			bmcr = PHY_READ(sc, MII_BMCR) & ~BMCR_PDOWN;
231 			PHY_WRITE(sc, MII_BMCR, bmcr);
232 
233 			if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {
234 				PHY_WRITE(sc, MII_BMCR,
235 					  bmcr | BMCR_AUTOEN | BMCR_STARTNEG);
236 			}
237 		}
238 		break;
239 
240 	case MII_TICK:
241 		/*
242 		 * If we're not currently selected, just return.
243 		 */
244 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
245 			return 0;
246 
247 		if (mii_phy_tick(sc) == EJUSTRETURN)
248 			return 0;
249 		break;
250 	}
251 
252 	/* Update the media status. */
253 	truephy_status(sc);
254 
255 	/* Callback if something changed. */
256 	mii_phy_update(sc, cmd);
257 	return 0;
258 }
259 
260 static void
261 truephy_reset(struct mii_softc *sc)
262 {
263 	int i;
264 
265 	for (i = 0; i < 2; ++i) {
266 		PHY_READ(sc, MII_PHYIDR1);
267 		PHY_READ(sc, MII_PHYIDR2);
268 
269 		PHY_READ(sc, TRUEPHY_CTRL);
270 		PHY_WRITE(sc, TRUEPHY_CTRL,
271 			  TRUEPHY_CTRL_DIAG | TRUEPHY_CTRL_RSV1);
272 
273 		PHY_WRITE(sc, TRUEPHY_INDEX, TRUEPHY_INDEX_MAGIC);
274 		PHY_READ(sc, TRUEPHY_DATA);
275 
276 		PHY_WRITE(sc, TRUEPHY_CTRL, TRUEPHY_CTRL_RSV1);
277 	}
278 
279 	PHY_READ(sc, MII_BMCR);
280 	PHY_READ(sc, TRUEPHY_CTRL);
281 	PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_PDOWN | BMCR_S1000);
282 	PHY_WRITE(sc, TRUEPHY_CTRL,
283 		  TRUEPHY_CTRL_DIAG | TRUEPHY_CTRL_RSV1 | TRUEPHY_CTRL_RSV0);
284 
285 #define N(arr)	(int)(sizeof(arr) / sizeof(arr[0]))
286 
287 	for (i = 0; i < N(truephy_dspcode); ++i) {
288 		const struct truephy_dsp *dsp = &truephy_dspcode[i];
289 
290 		PHY_WRITE(sc, TRUEPHY_INDEX, dsp->index);
291 		PHY_WRITE(sc, TRUEPHY_DATA, dsp->data);
292 
293 		PHY_WRITE(sc, TRUEPHY_INDEX, dsp->index);
294 		PHY_READ(sc, TRUEPHY_DATA);
295 	}
296 
297 #undef N
298 
299 	PHY_READ(sc, MII_BMCR);
300 	PHY_READ(sc, TRUEPHY_CTRL);
301 	PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN |  BMCR_S1000);
302 	PHY_WRITE(sc, TRUEPHY_CTRL, TRUEPHY_CTRL_RSV1);
303 
304 	mii_phy_reset(sc);
305 
306 	if (TRUEPHY_FRAMELEN(sc->mii_pdata->mii_ifp->if_mtu) > 2048) {
307 		int conf;
308 
309 		conf = PHY_READ(sc, TRUEPHY_CONF);
310 		conf &= ~TRUEPHY_CONF_TXFIFO_MASK;
311 		conf |= TRUEPHY_CONF_TXFIFO_24;
312 		PHY_WRITE(sc, TRUEPHY_CONF, conf);
313 	}
314 }
315 
316 static void
317 truephy_status(struct mii_softc *sc)
318 {
319 	struct mii_data *mii = sc->mii_pdata;
320 	int bmsr, bmcr, sr;
321 
322 	mii->mii_media_status = IFM_AVALID;
323 	mii->mii_media_active = IFM_ETHER;
324 
325 	sr = PHY_READ(sc, TRUEPHY_SR);
326 	bmcr = PHY_READ(sc, MII_BMCR);
327 
328 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
329 	if (bmsr & BMSR_LINK)
330 		mii->mii_media_status |= IFM_ACTIVE;
331 
332 	if (bmcr & BMCR_AUTOEN) {
333 		if ((bmsr & BMSR_ACOMP) == 0) {
334 			mii->mii_media_active |= IFM_NONE;
335 			return;
336 		}
337 	}
338 
339 	switch (sr & TRUEPHY_SR_SPD_MASK) {
340 	case TRUEPHY_SR_SPD_1000T:
341 		mii->mii_media_active |= IFM_1000_T;
342 		break;
343 	case TRUEPHY_SR_SPD_100TX:
344 		mii->mii_media_active |= IFM_100_TX;
345 		break;
346 	case TRUEPHY_SR_SPD_10T:
347 		mii->mii_media_active |= IFM_10_T;
348 		break;
349 	default:
350 		/* XXX will this ever happen? */
351 		printf("invalid media SR %#x\n", sr);
352 		mii->mii_media_active |= IFM_NONE;
353 		return;
354 	}
355 
356 	if (sr & TRUEPHY_SR_FDX)
357 		mii->mii_media_active |= IFM_FDX;
358 	else
359 		mii->mii_media_active |= IFM_HDX;
360 }
361