xref: /openbsd/sys/dev/mii/atphy.c (revision 471aeecf)
1*471aeecfSnaddy /*	$OpenBSD: atphy.c,v 1.13 2022/04/06 18:59:29 naddy Exp $	*/
2c87dcaaaSbrad 
3c87dcaaaSbrad /*-
4c87dcaaaSbrad  * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org>
5c87dcaaaSbrad  * All rights reserved.
6c87dcaaaSbrad  *
7c87dcaaaSbrad  * Redistribution and use in source and binary forms, with or without
8c87dcaaaSbrad  * modification, are permitted provided that the following conditions
9c87dcaaaSbrad  * are met:
10c87dcaaaSbrad  * 1. Redistributions of source code must retain the above copyright
11c87dcaaaSbrad  *    notice unmodified, this list of conditions, and the following
12c87dcaaaSbrad  *    disclaimer.
13c87dcaaaSbrad  * 2. Redistributions in binary form must reproduce the above copyright
14c87dcaaaSbrad  *    notice, this list of conditions and the following disclaimer in the
15c87dcaaaSbrad  *    documentation and/or other materials provided with the distribution.
16c87dcaaaSbrad  *
17c87dcaaaSbrad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18c87dcaaaSbrad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19c87dcaaaSbrad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20c87dcaaaSbrad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21c87dcaaaSbrad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22c87dcaaaSbrad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23c87dcaaaSbrad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24c87dcaaaSbrad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25c87dcaaaSbrad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26c87dcaaaSbrad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27c87dcaaaSbrad  * SUCH DAMAGE.
28c87dcaaaSbrad  */
29c87dcaaaSbrad 
30c87dcaaaSbrad /*
31c87dcaaaSbrad  * Driver for the Attansic F1 10/100/1000 PHY.
32c87dcaaaSbrad  */
33c87dcaaaSbrad 
34c87dcaaaSbrad #include <sys/param.h>
35c87dcaaaSbrad #include <sys/systm.h>
36c87dcaaaSbrad #include <sys/device.h>
37c87dcaaaSbrad #include <sys/socket.h>
38c87dcaaaSbrad 
39c87dcaaaSbrad #include <net/if.h>
400deb6685Smpi #include <net/if_var.h>
41c87dcaaaSbrad #include <net/if_media.h>
42c87dcaaaSbrad 
43c87dcaaaSbrad #include <dev/mii/mii.h>
44c87dcaaaSbrad #include <dev/mii/miivar.h>
45c87dcaaaSbrad #include <dev/mii/miidevs.h>
46c87dcaaaSbrad 
47c87dcaaaSbrad /* Special Control Register */
48c87dcaaaSbrad #define ATPHY_SCR			0x10
49c87dcaaaSbrad #define ATPHY_SCR_JABBER_DISABLE	0x0001
50c87dcaaaSbrad #define ATPHY_SCR_POLARITY_REVERSAL	0x0002
51c87dcaaaSbrad #define ATPHY_SCR_SQE_TEST		0x0004
52c87dcaaaSbrad #define ATPHY_SCR_MAC_PDOWN		0x0008
53c87dcaaaSbrad #define ATPHY_SCR_CLK125_DISABLE	0x0010
54c87dcaaaSbrad #define ATPHY_SCR_MDI_MANUAL_MODE	0x0000
55c87dcaaaSbrad #define ATPHY_SCR_MDIX_MANUAL_MODE	0x0020
56c87dcaaaSbrad #define ATPHY_SCR_AUTO_X_1000T		0x0040
57c87dcaaaSbrad #define ATPHY_SCR_AUTO_X_MODE		0x0060
58c87dcaaaSbrad #define ATPHY_SCR_10BT_EXT_ENABLE	0x0080
59c87dcaaaSbrad #define ATPHY_SCR_MII_5BIT_ENABLE	0x0100
60c87dcaaaSbrad #define ATPHY_SCR_SCRAMBLER_DISABLE	0x0200
61c87dcaaaSbrad #define ATPHY_SCR_FORCE_LINK_GOOD	0x0400
62c87dcaaaSbrad #define ATPHY_SCR_ASSERT_CRS_ON_TX	0x0800
63c87dcaaaSbrad 
64c87dcaaaSbrad /* Special Status Register. */
65c87dcaaaSbrad #define ATPHY_SSR			0x11
66c87dcaaaSbrad #define ATPHY_SSR_SPD_DPLX_RESOLVED	0x0800
67c87dcaaaSbrad #define ATPHY_SSR_DUPLEX		0x2000
68c87dcaaaSbrad #define ATPHY_SSR_SPEED_MASK		0xC000
69c87dcaaaSbrad #define ATPHY_SSR_10MBS			0x0000
70c87dcaaaSbrad #define ATPHY_SSR_100MBS		0x4000
71c87dcaaaSbrad #define ATPHY_SSR_1000MBS		0x8000
72c87dcaaaSbrad 
73c87dcaaaSbrad int	atphy_service(struct mii_softc *, struct mii_data *, int);
74c87dcaaaSbrad void	atphy_attach(struct device *, struct device *, void *);
75c87dcaaaSbrad int	atphy_match(struct device *, void *, void *);
76c87dcaaaSbrad void	atphy_reset(struct mii_softc *);
77c87dcaaaSbrad void	atphy_status(struct mii_softc *);
78c87dcaaaSbrad int	atphy_mii_phy_auto(struct mii_softc *);
79c87dcaaaSbrad 
80c87dcaaaSbrad const struct mii_phy_funcs atphy_funcs = {
81c87dcaaaSbrad         atphy_service, atphy_status, atphy_reset,
82c87dcaaaSbrad };
83c87dcaaaSbrad 
84776b091eSkevlo static const struct mii_phydesc atphys[] = {
85c87dcaaaSbrad 	{ MII_OUI_ATHEROS,	MII_MODEL_ATHEROS_F1,
86c87dcaaaSbrad 	  MII_STR_ATHEROS_F1 },
87776b091eSkevlo 	{ MII_OUI_ATHEROS,	MII_MODEL_ATHEROS_F2,
88776b091eSkevlo 	  MII_STR_ATHEROS_F2 },
89c665afaeSkettenis 	{ MII_OUI_ATHEROS,	MII_MODEL_ATHEROS_AR8035,
90c665afaeSkettenis 	  MII_STR_ATHEROS_AR8035 },
91c87dcaaaSbrad 	{ 0,			0,
92c87dcaaaSbrad 	  NULL },
93c87dcaaaSbrad };
94c87dcaaaSbrad 
95*471aeecfSnaddy const struct cfattach atphy_ca = {
96c87dcaaaSbrad 	sizeof (struct mii_softc), atphy_match, atphy_attach,
97fa9fb3edSderaadt 	mii_phy_detach
98c87dcaaaSbrad };
99c87dcaaaSbrad 
100c87dcaaaSbrad struct cfdriver atphy_cd = {
101c87dcaaaSbrad 	NULL, "atphy", DV_DULL
102c87dcaaaSbrad };
103c87dcaaaSbrad 
104c87dcaaaSbrad int
atphy_match(struct device * parent,void * match,void * aux)105c87dcaaaSbrad atphy_match(struct device *parent, void *match, void *aux)
106c87dcaaaSbrad {
107c87dcaaaSbrad 	struct mii_attach_args *ma = aux;
108c87dcaaaSbrad 
109776b091eSkevlo 	if (mii_phy_match(ma, atphys) != NULL)
110c87dcaaaSbrad 		return (10);
111c87dcaaaSbrad 
112c87dcaaaSbrad 	return (0);
113c87dcaaaSbrad }
114c87dcaaaSbrad 
115c87dcaaaSbrad void
atphy_attach(struct device * parent,struct device * self,void * aux)116c87dcaaaSbrad atphy_attach(struct device *parent, struct device *self, void *aux)
117c87dcaaaSbrad {
118c87dcaaaSbrad 	struct mii_softc *sc = (struct mii_softc *)self;
119c87dcaaaSbrad 	struct mii_attach_args *ma = aux;
120c87dcaaaSbrad 	struct mii_data *mii = ma->mii_data;
121c87dcaaaSbrad 	const struct mii_phydesc *mpd;
122c87dcaaaSbrad 
123776b091eSkevlo 	mpd = mii_phy_match(ma, atphys);
124c87dcaaaSbrad 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
125c87dcaaaSbrad 
126c87dcaaaSbrad 	sc->mii_inst = mii->mii_instance;
127c87dcaaaSbrad 	sc->mii_phy = ma->mii_phyno;
128c87dcaaaSbrad 	sc->mii_funcs = &atphy_funcs;
129c5b6983dSkettenis 	sc->mii_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
130d30046b8Skevlo 	sc->mii_model = MII_MODEL(ma->mii_id2);
131c87dcaaaSbrad 	sc->mii_pdata = mii;
132c87dcaaaSbrad 	sc->mii_flags = ma->mii_flags;
133c87dcaaaSbrad 	sc->mii_anegticks = MII_ANEGTICKS_GIGE;
134c87dcaaaSbrad 
135c87dcaaaSbrad 	sc->mii_flags |= MIIF_NOLOOP;
136c87dcaaaSbrad 
137c87dcaaaSbrad 	PHY_RESET(sc);
138c87dcaaaSbrad 
139c87dcaaaSbrad 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
140c87dcaaaSbrad 	if (sc->mii_capabilities & BMSR_EXTSTAT)
141c87dcaaaSbrad 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
142c87dcaaaSbrad 
143c87dcaaaSbrad 	mii_phy_add_media(sc);
144c87dcaaaSbrad }
145c87dcaaaSbrad 
146c87dcaaaSbrad int
atphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)147c87dcaaaSbrad atphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
148c87dcaaaSbrad {
149c87dcaaaSbrad 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
150c87dcaaaSbrad 	uint16_t anar, bmcr, bmsr;
151c87dcaaaSbrad 
152c87dcaaaSbrad 	switch (cmd) {
153c87dcaaaSbrad 	case MII_POLLSTAT:
154c87dcaaaSbrad 		/*
155c87dcaaaSbrad 		 * If we're not polling our PHY instance, just return.
156c87dcaaaSbrad 		 */
157c87dcaaaSbrad 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
158c87dcaaaSbrad 			return (0);
159c87dcaaaSbrad 		break;
160c87dcaaaSbrad 
161c87dcaaaSbrad 	case MII_MEDIACHG:
162c87dcaaaSbrad 		/*
163c87dcaaaSbrad 		 * If the media indicates a different PHY instance,
164c87dcaaaSbrad 		 * isolate ourselves.
165c87dcaaaSbrad 		 */
166c87dcaaaSbrad 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
167c87dcaaaSbrad 			bmcr = PHY_READ(sc, MII_BMCR);
168c87dcaaaSbrad 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
169c87dcaaaSbrad 			return (0);
170c87dcaaaSbrad 		}
171c87dcaaaSbrad 
172c87dcaaaSbrad 		/*
173c87dcaaaSbrad 		 * If the interface is not up, don't do anything.
174c87dcaaaSbrad 		 */
175c87dcaaaSbrad 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
176c87dcaaaSbrad 			break;
177c87dcaaaSbrad 
178c87dcaaaSbrad 		bmcr = 0;
179c87dcaaaSbrad 		switch (IFM_SUBTYPE(ife->ifm_media)) {
180c87dcaaaSbrad 		case IFM_AUTO:
181c87dcaaaSbrad 		case IFM_1000_T:
182c87dcaaaSbrad 			atphy_mii_phy_auto(sc);
183c87dcaaaSbrad 			goto done;
184c87dcaaaSbrad 		case IFM_100_TX:
185c87dcaaaSbrad 			bmcr = BMCR_S100;
186c87dcaaaSbrad 			break;
187c87dcaaaSbrad 		case IFM_10_T:
188c87dcaaaSbrad 			bmcr = BMCR_S10;
189c87dcaaaSbrad 			break;
190c87dcaaaSbrad 		case IFM_NONE:
191c87dcaaaSbrad 			bmcr = PHY_READ(sc, MII_BMCR);
192c87dcaaaSbrad 			/*
193c87dcaaaSbrad 			 * XXX
194c87dcaaaSbrad 			 * Due to an unknown reason powering down PHY resulted
19534ba84b9Smartynas 			 * in unexpected results such as inaccessibility of
196c87dcaaaSbrad 			 * hardware of freshly rebooted system. Disable
1974b1a56afSjsg 			 * powering down PHY until I get more information for
1984b1a56afSjsg 			 * Attansic/Atheros PHY hardware.
199c87dcaaaSbrad 			 */
200c87dcaaaSbrad 			PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
201c87dcaaaSbrad 			goto done;
202c87dcaaaSbrad 		default:
203c87dcaaaSbrad 			return (EINVAL);
204c87dcaaaSbrad 		}
205c87dcaaaSbrad 
206c87dcaaaSbrad 		anar = mii_anar(ife->ifm_media);
207c87dcaaaSbrad 		if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) {
208c87dcaaaSbrad 			bmcr |= BMCR_FDX;
209c87dcaaaSbrad 			/* Enable pause. */
210c87dcaaaSbrad 			if (sc->mii_flags & MIIF_DOPAUSE)
211d6673823Skettenis 				anar |= ANAR_PAUSE_TOWARDS;
212c87dcaaaSbrad 		}
213c87dcaaaSbrad 
214c87dcaaaSbrad 		if ((sc->mii_extcapabilities & (EXTSR_1000TFDX |
215c87dcaaaSbrad 		    EXTSR_1000THDX)) != 0)
216c87dcaaaSbrad 			PHY_WRITE(sc, MII_100T2CR, 0);
217c87dcaaaSbrad 		PHY_WRITE(sc, MII_ANAR, anar);
218c87dcaaaSbrad 
219c87dcaaaSbrad 		/*
220c87dcaaaSbrad 		 * Reset the PHY so all changes take effect.
221c87dcaaaSbrad 		 */
222d4466d62Skevlo 		PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_RESET | BMCR_AUTOEN |
223d4466d62Skevlo 		    BMCR_STARTNEG);
224c87dcaaaSbrad done:
225c87dcaaaSbrad 		break;
226c87dcaaaSbrad 
227c87dcaaaSbrad 	case MII_TICK:
228c87dcaaaSbrad 		/*
229c87dcaaaSbrad 		 * If we're not currently selected, just return.
230c87dcaaaSbrad 		 */
231c87dcaaaSbrad 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
232c87dcaaaSbrad 			return (0);
233c87dcaaaSbrad 
234c87dcaaaSbrad 		/*
235c87dcaaaSbrad 		 * Is the interface even up?
236c87dcaaaSbrad 		 */
237c87dcaaaSbrad 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
238c87dcaaaSbrad 			return (0);
239c87dcaaaSbrad 
240c87dcaaaSbrad 		/*
241c87dcaaaSbrad 		 * Only used for autonegotiation.
242c87dcaaaSbrad 		 */
243c87dcaaaSbrad 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
244c87dcaaaSbrad 			sc->mii_ticks = 0;
245c87dcaaaSbrad 			break;
246c87dcaaaSbrad 		}
247c87dcaaaSbrad 
248c87dcaaaSbrad 		/*
249c87dcaaaSbrad 		 * Check for link.
250c87dcaaaSbrad 		 * Read the status register twice; BMSR_LINK is latch-low.
251c87dcaaaSbrad 		 */
252c87dcaaaSbrad 		bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
253c87dcaaaSbrad 		if (bmsr & BMSR_LINK) {
254c87dcaaaSbrad 			sc->mii_ticks = 0;
255c87dcaaaSbrad 			break;
256c87dcaaaSbrad 		}
257c87dcaaaSbrad 
258c87dcaaaSbrad 		/* Announce link loss right after it happens. */
259c87dcaaaSbrad 		if (sc->mii_ticks++ == 0)
260c87dcaaaSbrad 			break;
261c87dcaaaSbrad 
262c87dcaaaSbrad 		/*
263c87dcaaaSbrad 		 * Only retry autonegotiation every mii_anegticks seconds.
264c87dcaaaSbrad 		 */
265c87dcaaaSbrad 		if (sc->mii_ticks <= sc->mii_anegticks)
266d30046b8Skevlo 			return (0);
267c87dcaaaSbrad 
268c87dcaaaSbrad 		sc->mii_ticks = 0;
269c87dcaaaSbrad 		atphy_mii_phy_auto(sc);
270c87dcaaaSbrad 		break;
271c87dcaaaSbrad 	}
272c87dcaaaSbrad 
273c87dcaaaSbrad 	/* Update the media status. */
274c87dcaaaSbrad 	mii_phy_status(sc);
275c87dcaaaSbrad 
276c87dcaaaSbrad 	/* Callback if something changed. */
277c87dcaaaSbrad 	mii_phy_update(sc, cmd);
278c87dcaaaSbrad 	return (0);
279c87dcaaaSbrad }
280c87dcaaaSbrad 
281c87dcaaaSbrad void
atphy_status(struct mii_softc * sc)282c87dcaaaSbrad atphy_status(struct mii_softc *sc)
283c87dcaaaSbrad {
284c87dcaaaSbrad 	struct mii_data *mii = sc->mii_pdata;
285c87dcaaaSbrad 	uint32_t bmsr, bmcr, gsr, ssr;
286c87dcaaaSbrad 
287c87dcaaaSbrad 	mii->mii_media_status = IFM_AVALID;
288c87dcaaaSbrad 	mii->mii_media_active = IFM_ETHER;
289c87dcaaaSbrad 
290c87dcaaaSbrad 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
291c87dcaaaSbrad 	if (bmsr & BMSR_LINK)
292c87dcaaaSbrad 		mii->mii_media_status |= IFM_ACTIVE;
293c87dcaaaSbrad 
294c87dcaaaSbrad 	bmcr = PHY_READ(sc, MII_BMCR);
295c87dcaaaSbrad 	if (bmcr & BMCR_ISO) {
296c87dcaaaSbrad 		mii->mii_media_active |= IFM_NONE;
297c87dcaaaSbrad 		mii->mii_media_status = 0;
298c87dcaaaSbrad 		return;
299c87dcaaaSbrad 	}
300c87dcaaaSbrad 
301c87dcaaaSbrad 	if (bmcr & BMCR_LOOP)
302c87dcaaaSbrad 		mii->mii_media_active |= IFM_LOOP;
303c87dcaaaSbrad 
304c87dcaaaSbrad 	ssr = PHY_READ(sc, ATPHY_SSR);
305c87dcaaaSbrad 	if (!(ssr & ATPHY_SSR_SPD_DPLX_RESOLVED)) {
306c87dcaaaSbrad 		/* Erg, still trying, I guess... */
307c87dcaaaSbrad 		mii->mii_media_active |= IFM_NONE;
308c87dcaaaSbrad 		return;
309c87dcaaaSbrad 	}
310c87dcaaaSbrad 
311c87dcaaaSbrad 	switch (ssr & ATPHY_SSR_SPEED_MASK) {
312c87dcaaaSbrad 	case ATPHY_SSR_1000MBS:
313c87dcaaaSbrad 		mii->mii_media_active |= IFM_1000_T;
314c87dcaaaSbrad 		/*
315c87dcaaaSbrad 		 * atphy(4) has a valid link so reset mii_ticks.
316c87dcaaaSbrad 		 * Resetting mii_ticks is needed in order to
317c87dcaaaSbrad 		 * detect link loss after auto-negotiation.
318c87dcaaaSbrad 		 */
319c87dcaaaSbrad 		sc->mii_ticks = 0;
320c87dcaaaSbrad 		break;
321c87dcaaaSbrad 	case ATPHY_SSR_100MBS:
322c87dcaaaSbrad 		mii->mii_media_active |= IFM_100_TX;
323c87dcaaaSbrad 		sc->mii_ticks = 0;
324c87dcaaaSbrad 		break;
325c87dcaaaSbrad 	case ATPHY_SSR_10MBS:
326c87dcaaaSbrad 		mii->mii_media_active |= IFM_10_T;
327c87dcaaaSbrad 		sc->mii_ticks = 0;
328c87dcaaaSbrad 		break;
329c87dcaaaSbrad 	default:
330c87dcaaaSbrad 		mii->mii_media_active |= IFM_NONE;
331c87dcaaaSbrad 		return;
332c87dcaaaSbrad 	}
333c87dcaaaSbrad 
334c87dcaaaSbrad 	if (ssr & ATPHY_SSR_DUPLEX)
335c87dcaaaSbrad 		mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
336c87dcaaaSbrad 	else
337c87dcaaaSbrad 		mii->mii_media_active |= IFM_HDX;
338c87dcaaaSbrad 
339c87dcaaaSbrad 	gsr = PHY_READ(sc, MII_100T2SR);
340c87dcaaaSbrad 	if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) &&
341c87dcaaaSbrad 	    gsr & GTSR_MS_RES)
342c87dcaaaSbrad 		mii->mii_media_active |= IFM_ETH_MASTER;
343c87dcaaaSbrad }
344c87dcaaaSbrad 
345c87dcaaaSbrad void
atphy_reset(struct mii_softc * sc)346c87dcaaaSbrad atphy_reset(struct mii_softc *sc)
347c87dcaaaSbrad {
348c87dcaaaSbrad 	uint32_t reg;
349c87dcaaaSbrad 	int i;
350c87dcaaaSbrad 
351c87dcaaaSbrad 	/* Take PHY out of power down mode. */
352c87dcaaaSbrad 	PHY_WRITE(sc, 29, 0x29);
353c87dcaaaSbrad 	PHY_WRITE(sc, 30, 0);
354c87dcaaaSbrad 
355c87dcaaaSbrad 	reg = PHY_READ(sc, ATPHY_SCR);
356c87dcaaaSbrad 	/* Enable automatic crossover. */
357c87dcaaaSbrad 	reg |= ATPHY_SCR_AUTO_X_MODE;
358c87dcaaaSbrad 	/* Disable power down. */
359c87dcaaaSbrad 	reg &= ~ATPHY_SCR_MAC_PDOWN;
360c87dcaaaSbrad 	/* Enable CRS on Tx. */
361c87dcaaaSbrad 	reg |= ATPHY_SCR_ASSERT_CRS_ON_TX;
362c87dcaaaSbrad 	/* Auto correction for reversed cable polarity. */
363c87dcaaaSbrad 	reg |= ATPHY_SCR_POLARITY_REVERSAL;
364c87dcaaaSbrad 	PHY_WRITE(sc, ATPHY_SCR, reg);
365c87dcaaaSbrad 
366c87dcaaaSbrad 	/* Workaround F1 bug to reset phy. */
367c87dcaaaSbrad 	atphy_mii_phy_auto(sc);
368c87dcaaaSbrad 
369c87dcaaaSbrad 	for (i = 0; i < 1000; i++) {
370c87dcaaaSbrad 		DELAY(1);
371c87dcaaaSbrad 		if ((PHY_READ(sc, MII_BMCR) & BMCR_RESET) == 0)
372c87dcaaaSbrad 			break;
373c87dcaaaSbrad 	}
374c87dcaaaSbrad }
375c87dcaaaSbrad 
376c87dcaaaSbrad int
atphy_mii_phy_auto(struct mii_softc * sc)377c87dcaaaSbrad atphy_mii_phy_auto(struct mii_softc *sc)
378c87dcaaaSbrad {
379c87dcaaaSbrad 	uint16_t anar;
380c87dcaaaSbrad 
381c87dcaaaSbrad 	anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
382c87dcaaaSbrad 	if (sc->mii_flags & MIIF_DOPAUSE)
383d6673823Skettenis 		anar |= ANAR_PAUSE_TOWARDS;
384c87dcaaaSbrad 	PHY_WRITE(sc, MII_ANAR, anar);
385c87dcaaaSbrad 	if (sc->mii_extcapabilities & (EXTSR_1000TFDX | EXTSR_1000THDX))
386c87dcaaaSbrad 		PHY_WRITE(sc, MII_100T2CR, GTCR_ADV_1000TFDX |
387c87dcaaaSbrad 		    GTCR_ADV_1000THDX);
388d30046b8Skevlo 	else if (sc->mii_model == MII_MODEL_ATHEROS_F1) {
389d30046b8Skevlo 		/*
390d30046b8Skevlo 		 * AR8132 has 10/100 PHY and the PHY uses the same
391d30046b8Skevlo 		 * model number of F1 gigabit PHY.  The PHY has no
392d30046b8Skevlo 		 * ability to establish gigabit link so explicitly
393d30046b8Skevlo 		 * disable 1000baseT configuration for the PHY.
394d30046b8Skevlo 		 * Otherwise, there is a case that atphy(4) could
395d30046b8Skevlo 		 * not establish a link against gigabit link partner
396d30046b8Skevlo 		 * unless the link partner supports down-shifting.
397d30046b8Skevlo 		 */
398d30046b8Skevlo 		PHY_WRITE(sc, MII_100T2CR, 0);
399d30046b8Skevlo 	}
400c87dcaaaSbrad 	PHY_WRITE(sc, MII_BMCR, BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);
401c87dcaaaSbrad 
402c87dcaaaSbrad 	return (EJUSTRETURN);
403c87dcaaaSbrad }
404