atphy.c revision 1.25 1 1.25 msaitoh /* $NetBSD: atphy.c,v 1.25 2019/11/20 08:50:59 msaitoh Exp $ */
2 1.1 cegger /* $OpenBSD: atphy.c,v 1.1 2008/09/25 20:47:16 brad Exp $ */
3 1.1 cegger
4 1.1 cegger /*-
5 1.1 cegger * Copyright (c) 2008, Pyun YongHyeon <yongari (at) FreeBSD.org>
6 1.1 cegger * All rights reserved.
7 1.1 cegger *
8 1.1 cegger * Redistribution and use in source and binary forms, with or without
9 1.1 cegger * modification, are permitted provided that the following conditions
10 1.1 cegger * are met:
11 1.1 cegger * 1. Redistributions of source code must retain the above copyright
12 1.1 cegger * notice unmodified, this list of conditions, and the following
13 1.1 cegger * disclaimer.
14 1.1 cegger * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 cegger * notice, this list of conditions and the following disclaimer in the
16 1.1 cegger * documentation and/or other materials provided with the distribution.
17 1.1 cegger *
18 1.1 cegger * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 1.1 cegger * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 cegger * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 cegger * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 1.1 cegger * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 cegger * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 cegger * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 cegger * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 cegger * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 cegger * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 cegger * SUCH DAMAGE.
29 1.1 cegger */
30 1.1 cegger
31 1.1 cegger /*
32 1.1 cegger * Driver for the Attansic F1 10/100/1000 PHY.
33 1.1 cegger */
34 1.1 cegger
35 1.2 cegger #include <sys/cdefs.h>
36 1.25 msaitoh __KERNEL_RCSID(0, "$NetBSD: atphy.c,v 1.25 2019/11/20 08:50:59 msaitoh Exp $");
37 1.2 cegger
38 1.1 cegger #include <sys/param.h>
39 1.1 cegger #include <sys/systm.h>
40 1.1 cegger #include <sys/kernel.h>
41 1.1 cegger #include <sys/device.h>
42 1.1 cegger #include <sys/socket.h>
43 1.1 cegger
44 1.1 cegger #include <net/if.h>
45 1.1 cegger #include <net/if_media.h>
46 1.1 cegger
47 1.1 cegger #include <dev/mii/mii.h>
48 1.1 cegger #include <dev/mii/miivar.h>
49 1.1 cegger #include <dev/mii/miidevs.h>
50 1.1 cegger
51 1.1 cegger /* Special Control Register */
52 1.1 cegger #define ATPHY_SCR 0x10
53 1.1 cegger #define ATPHY_SCR_JABBER_DISABLE 0x0001
54 1.1 cegger #define ATPHY_SCR_POLARITY_REVERSAL 0x0002
55 1.1 cegger #define ATPHY_SCR_SQE_TEST 0x0004
56 1.1 cegger #define ATPHY_SCR_MAC_PDOWN 0x0008
57 1.1 cegger #define ATPHY_SCR_CLK125_DISABLE 0x0010
58 1.1 cegger #define ATPHY_SCR_MDI_MANUAL_MODE 0x0000
59 1.1 cegger #define ATPHY_SCR_MDIX_MANUAL_MODE 0x0020
60 1.1 cegger #define ATPHY_SCR_AUTO_X_1000T 0x0040
61 1.1 cegger #define ATPHY_SCR_AUTO_X_MODE 0x0060
62 1.1 cegger #define ATPHY_SCR_10BT_EXT_ENABLE 0x0080
63 1.1 cegger #define ATPHY_SCR_MII_5BIT_ENABLE 0x0100
64 1.1 cegger #define ATPHY_SCR_SCRAMBLER_DISABLE 0x0200
65 1.1 cegger #define ATPHY_SCR_FORCE_LINK_GOOD 0x0400
66 1.1 cegger #define ATPHY_SCR_ASSERT_CRS_ON_TX 0x0800
67 1.1 cegger
68 1.1 cegger /* Special Status Register. */
69 1.1 cegger #define ATPHY_SSR 0x11
70 1.1 cegger #define ATPHY_SSR_SPD_DPLX_RESOLVED 0x0800
71 1.1 cegger #define ATPHY_SSR_DUPLEX 0x2000
72 1.1 cegger #define ATPHY_SSR_SPEED_MASK 0xC000
73 1.1 cegger #define ATPHY_SSR_10MBS 0x0000
74 1.1 cegger #define ATPHY_SSR_100MBS 0x4000
75 1.1 cegger #define ATPHY_SSR_1000MBS 0x8000
76 1.1 cegger
77 1.24 hkenken #define ATPHY_DEBUG_PORT_ADDR 0x1d
78 1.24 hkenken #define ATPHY_DEBUG_PORT_DATA 0x1e
79 1.24 hkenken #define ATPHY_RGMII_RX_CLK_DLY __BIT(15)
80 1.24 hkenken #define ATPHY_RGMII_TX_CLK_DLY __BIT(8)
81 1.24 hkenken
82 1.1 cegger static int atphy_match(device_t, cfdata_t, void *);
83 1.1 cegger static void atphy_attach(device_t, device_t, void *);
84 1.1 cegger
85 1.1 cegger static int atphy_service(struct mii_softc *, struct mii_data *, int);
86 1.1 cegger static void atphy_reset(struct mii_softc *);
87 1.1 cegger static void atphy_status(struct mii_softc *);
88 1.1 cegger static int atphy_mii_phy_auto(struct mii_softc *);
89 1.11 jmcneill static bool atphy_is_gige(const struct mii_phydesc *);
90 1.1 cegger
91 1.24 hkenken struct atphy_softc {
92 1.24 hkenken struct mii_softc mii_sc;
93 1.24 hkenken int mii_clk_25m;
94 1.24 hkenken bool rgmii_tx_internal_delay;
95 1.24 hkenken bool rgmii_rx_internal_delay;
96 1.24 hkenken };
97 1.24 hkenken
98 1.24 hkenken CFATTACH_DECL_NEW(atphy, sizeof(struct atphy_softc),
99 1.1 cegger atphy_match, atphy_attach, mii_phy_detach, mii_phy_activate);
100 1.1 cegger
101 1.1 cegger const struct mii_phy_funcs atphy_funcs = {
102 1.1 cegger atphy_service, atphy_status, atphy_reset,
103 1.1 cegger };
104 1.1 cegger
105 1.23 msaitoh static const struct mii_phydesc atphys[] = {
106 1.20 christos MII_PHY_DESC(ATHEROS, F1),
107 1.20 christos MII_PHY_DESC(ATTANSIC, L1),
108 1.20 christos MII_PHY_DESC(ATTANSIC, L2),
109 1.20 christos MII_PHY_DESC(ATTANSIC, AR8021),
110 1.20 christos MII_PHY_DESC(ATTANSIC, AR8035),
111 1.20 christos MII_PHY_END,
112 1.1 cegger };
113 1.1 cegger
114 1.24 hkenken static void
115 1.24 hkenken atphy_clk_25m(struct atphy_softc *asc)
116 1.24 hkenken {
117 1.24 hkenken struct mii_softc *sc = &asc->mii_sc;
118 1.24 hkenken struct {
119 1.24 hkenken uint32_t hz;
120 1.24 hkenken uint16_t data;
121 1.24 hkenken } select_clk[] = {
122 1.24 hkenken { 25000000, 0x0 },
123 1.24 hkenken { 50000000, 0x1 },
124 1.24 hkenken { 62500000, 0x2 },
125 1.24 hkenken { 125000000, 0x3 }
126 1.24 hkenken };
127 1.24 hkenken uint16_t data = 0;
128 1.24 hkenken uint16_t reg = 0;
129 1.24 hkenken
130 1.24 hkenken for (int i = 0; i < __arraycount(select_clk); i++) {
131 1.24 hkenken if (asc->mii_clk_25m <= select_clk[i].hz)
132 1.24 hkenken data = select_clk[i].data;
133 1.24 hkenken }
134 1.24 hkenken
135 1.24 hkenken PHY_WRITE(sc, 0x0d, 0x0007);
136 1.24 hkenken PHY_WRITE(sc, 0x0e, 0x8016);
137 1.24 hkenken PHY_WRITE(sc, 0x0d, 0x4007);
138 1.24 hkenken PHY_READ(sc, 0x0e, ®);
139 1.24 hkenken PHY_WRITE(sc, 0x0e, reg | __SHIFTIN(data, __BITS(4, 3)));
140 1.24 hkenken }
141 1.24 hkenken
142 1.24 hkenken
143 1.11 jmcneill static bool
144 1.11 jmcneill atphy_is_gige(const struct mii_phydesc *mpd)
145 1.11 jmcneill {
146 1.11 jmcneill switch (mpd->mpd_oui) {
147 1.11 jmcneill case MII_OUI_ATTANSIC:
148 1.11 jmcneill switch (mpd->mpd_model) {
149 1.11 jmcneill case MII_MODEL_ATTANSIC_L2:
150 1.11 jmcneill return false;
151 1.11 jmcneill }
152 1.11 jmcneill }
153 1.11 jmcneill
154 1.11 jmcneill return true;
155 1.11 jmcneill }
156 1.11 jmcneill
157 1.1 cegger static int
158 1.1 cegger atphy_match(device_t parent, cfdata_t match, void *aux)
159 1.1 cegger {
160 1.1 cegger struct mii_attach_args *ma = aux;
161 1.1 cegger
162 1.23 msaitoh if (mii_phy_match(ma, atphys) != NULL)
163 1.1 cegger return 10;
164 1.1 cegger
165 1.1 cegger return 0;
166 1.1 cegger }
167 1.1 cegger
168 1.1 cegger void
169 1.1 cegger atphy_attach(device_t parent, device_t self, void *aux)
170 1.1 cegger {
171 1.24 hkenken struct atphy_softc *asc = device_private(self);
172 1.24 hkenken prop_dictionary_t parent_prop = device_properties(parent);
173 1.24 hkenken prop_dictionary_t prop = device_properties(self);
174 1.24 hkenken struct mii_softc *sc = &asc->mii_sc;
175 1.1 cegger struct mii_attach_args *ma = aux;
176 1.1 cegger struct mii_data *mii = ma->mii_data;
177 1.1 cegger const struct mii_phydesc *mpd;
178 1.8 cegger uint16_t bmsr;
179 1.1 cegger
180 1.23 msaitoh mpd = mii_phy_match(ma, atphys);
181 1.1 cegger aprint_naive(": Media interface\n");
182 1.1 cegger aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
183 1.1 cegger
184 1.1 cegger sc->mii_dev = self;
185 1.1 cegger sc->mii_inst = mii->mii_instance;
186 1.1 cegger sc->mii_phy = ma->mii_phyno;
187 1.18 msaitoh sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
188 1.18 msaitoh sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
189 1.18 msaitoh sc->mii_mpd_rev = MII_REV(ma->mii_id2);
190 1.1 cegger sc->mii_funcs = &atphy_funcs;
191 1.1 cegger sc->mii_pdata = mii;
192 1.1 cegger sc->mii_flags = ma->mii_flags;
193 1.11 jmcneill if (atphy_is_gige(mpd))
194 1.11 jmcneill sc->mii_anegticks = MII_ANEGTICKS_GIGE;
195 1.11 jmcneill else
196 1.11 jmcneill sc->mii_anegticks = MII_ANEGTICKS;
197 1.1 cegger
198 1.1 cegger sc->mii_flags |= MIIF_NOLOOP;
199 1.1 cegger
200 1.24 hkenken prop_dictionary_get_bool(parent_prop, "tx_internal_delay", &asc->rgmii_tx_internal_delay);
201 1.24 hkenken prop_dictionary_get_bool(parent_prop, "rx_internal_delay", &asc->rgmii_rx_internal_delay);
202 1.24 hkenken
203 1.24 hkenken prop_dictionary_get_uint32(prop, "clk_25m", &asc->mii_clk_25m);
204 1.24 hkenken if (asc->mii_clk_25m != 0)
205 1.24 hkenken atphy_clk_25m(asc);
206 1.24 hkenken
207 1.1 cegger PHY_RESET(sc);
208 1.1 cegger
209 1.19 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
210 1.19 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
211 1.8 cegger sc->mii_capabilities = bmsr & ma->mii_capmask;
212 1.11 jmcneill if (atphy_is_gige(mpd) && (sc->mii_capabilities & BMSR_EXTSTAT))
213 1.19 msaitoh PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
214 1.1 cegger
215 1.4 cegger aprint_normal_dev(self, "");
216 1.1 cegger mii_phy_add_media(sc);
217 1.4 cegger aprint_normal("\n");
218 1.1 cegger }
219 1.1 cegger
220 1.1 cegger int
221 1.1 cegger atphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
222 1.1 cegger {
223 1.1 cegger struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
224 1.1 cegger uint16_t anar, bmcr, bmsr;
225 1.1 cegger
226 1.1 cegger switch (cmd) {
227 1.1 cegger case MII_POLLSTAT:
228 1.21 msaitoh /* If we're not polling our PHY instance, just return. */
229 1.1 cegger if (IFM_INST(ife->ifm_media) != sc->mii_inst)
230 1.1 cegger return 0;
231 1.1 cegger break;
232 1.1 cegger
233 1.1 cegger case MII_MEDIACHG:
234 1.1 cegger /*
235 1.1 cegger * If the media indicates a different PHY instance,
236 1.1 cegger * isolate ourselves.
237 1.1 cegger */
238 1.1 cegger if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
239 1.19 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
240 1.1 cegger PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
241 1.1 cegger return 0;
242 1.1 cegger }
243 1.1 cegger
244 1.21 msaitoh /* If the interface is not up, don't do anything. */
245 1.1 cegger if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
246 1.1 cegger break;
247 1.1 cegger
248 1.1 cegger bmcr = 0;
249 1.1 cegger switch (IFM_SUBTYPE(ife->ifm_media)) {
250 1.1 cegger case IFM_AUTO:
251 1.1 cegger case IFM_1000_T:
252 1.1 cegger atphy_mii_phy_auto(sc);
253 1.1 cegger goto done;
254 1.1 cegger case IFM_100_TX:
255 1.1 cegger bmcr = BMCR_S100;
256 1.1 cegger break;
257 1.1 cegger case IFM_10_T:
258 1.1 cegger bmcr = BMCR_S10;
259 1.1 cegger break;
260 1.1 cegger case IFM_NONE:
261 1.19 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
262 1.1 cegger /*
263 1.1 cegger * XXX
264 1.1 cegger * Due to an unknown reason powering down PHY resulted
265 1.6 cegger * in unexpected results such as inaccessibility of
266 1.1 cegger * hardware of freshly rebooted system. Disable
267 1.1 cegger * powering down PHY until I got more information for
268 1.1 cegger * Attansic/Atheros PHY hardwares.
269 1.1 cegger */
270 1.1 cegger PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
271 1.1 cegger goto done;
272 1.1 cegger default:
273 1.1 cegger return EINVAL;
274 1.1 cegger }
275 1.1 cegger
276 1.25 msaitoh anar = mii_anar(ife);
277 1.22 msaitoh if ((ife->ifm_media & IFM_FDX) != 0) {
278 1.1 cegger bmcr |= BMCR_FDX;
279 1.1 cegger /* Enable pause. */
280 1.1 cegger if (sc->mii_flags & MIIF_DOPAUSE)
281 1.15 msaitoh anar |= ANAR_PAUSE_TOWARDS;
282 1.1 cegger }
283 1.1 cegger
284 1.1 cegger if ((sc->mii_extcapabilities & (EXTSR_1000TFDX |
285 1.1 cegger EXTSR_1000THDX)) != 0)
286 1.1 cegger PHY_WRITE(sc, MII_100T2CR, 0);
287 1.1 cegger PHY_WRITE(sc, MII_ANAR, anar);
288 1.1 cegger
289 1.21 msaitoh /* Start autonegotiation. */
290 1.8 cegger PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_AUTOEN | BMCR_STARTNEG);
291 1.1 cegger done:
292 1.1 cegger break;
293 1.1 cegger
294 1.1 cegger case MII_TICK:
295 1.21 msaitoh /* If we're not currently selected, just return. */
296 1.1 cegger if (IFM_INST(ife->ifm_media) != sc->mii_inst)
297 1.1 cegger return 0;
298 1.1 cegger
299 1.21 msaitoh /* Is the interface even up? */
300 1.1 cegger if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
301 1.1 cegger return 0;
302 1.1 cegger
303 1.21 msaitoh /* Only used for autonegotiation. */
304 1.14 msaitoh if ((IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) &&
305 1.14 msaitoh (IFM_SUBTYPE(ife->ifm_media) != IFM_1000_T)) {
306 1.1 cegger sc->mii_ticks = 0;
307 1.1 cegger break;
308 1.1 cegger }
309 1.1 cegger
310 1.1 cegger /*
311 1.1 cegger * Check for link.
312 1.1 cegger * Read the status register twice; BMSR_LINK is latch-low.
313 1.1 cegger */
314 1.19 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
315 1.19 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
316 1.1 cegger if (bmsr & BMSR_LINK) {
317 1.1 cegger sc->mii_ticks = 0;
318 1.1 cegger break;
319 1.1 cegger }
320 1.1 cegger
321 1.1 cegger /* Announce link loss right after it happens. */
322 1.1 cegger if (sc->mii_ticks++ == 0)
323 1.1 cegger break;
324 1.1 cegger
325 1.21 msaitoh /* Only retry autonegotiation every mii_anegticks seconds. */
326 1.1 cegger if (sc->mii_ticks <= sc->mii_anegticks)
327 1.1 cegger break;
328 1.1 cegger
329 1.1 cegger atphy_mii_phy_auto(sc);
330 1.1 cegger break;
331 1.1 cegger }
332 1.1 cegger
333 1.1 cegger /* Update the media status. */
334 1.1 cegger mii_phy_status(sc);
335 1.1 cegger
336 1.1 cegger /* Callback if something changed. */
337 1.1 cegger mii_phy_update(sc, cmd);
338 1.1 cegger return 0;
339 1.1 cegger }
340 1.1 cegger
341 1.1 cegger static void
342 1.1 cegger atphy_status(struct mii_softc *sc)
343 1.1 cegger {
344 1.1 cegger struct mii_data *mii = sc->mii_pdata;
345 1.19 msaitoh uint16_t bmsr, bmcr, gsr, ssr;
346 1.1 cegger
347 1.1 cegger mii->mii_media_status = IFM_AVALID;
348 1.1 cegger mii->mii_media_active = IFM_ETHER;
349 1.1 cegger
350 1.19 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
351 1.19 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
352 1.1 cegger if (bmsr & BMSR_LINK)
353 1.1 cegger mii->mii_media_status |= IFM_ACTIVE;
354 1.1 cegger
355 1.19 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
356 1.1 cegger if (bmcr & BMCR_ISO) {
357 1.1 cegger mii->mii_media_active |= IFM_NONE;
358 1.1 cegger mii->mii_media_status = 0;
359 1.1 cegger return;
360 1.1 cegger }
361 1.1 cegger
362 1.1 cegger if (bmcr & BMCR_LOOP)
363 1.1 cegger mii->mii_media_active |= IFM_LOOP;
364 1.1 cegger
365 1.19 msaitoh PHY_READ(sc, ATPHY_SSR, &ssr);
366 1.1 cegger if (!(ssr & ATPHY_SSR_SPD_DPLX_RESOLVED)) {
367 1.1 cegger /* Erg, still trying, I guess... */
368 1.1 cegger mii->mii_media_active |= IFM_NONE;
369 1.1 cegger return;
370 1.1 cegger }
371 1.1 cegger
372 1.1 cegger switch (ssr & ATPHY_SSR_SPEED_MASK) {
373 1.1 cegger case ATPHY_SSR_1000MBS:
374 1.1 cegger mii->mii_media_active |= IFM_1000_T;
375 1.1 cegger /*
376 1.1 cegger * atphy(4) has a valid link so reset mii_ticks.
377 1.1 cegger * Resetting mii_ticks is needed in order to
378 1.1 cegger * detect link loss after auto-negotiation.
379 1.1 cegger */
380 1.1 cegger sc->mii_ticks = 0;
381 1.1 cegger break;
382 1.1 cegger case ATPHY_SSR_100MBS:
383 1.1 cegger mii->mii_media_active |= IFM_100_TX;
384 1.1 cegger sc->mii_ticks = 0;
385 1.1 cegger break;
386 1.1 cegger case ATPHY_SSR_10MBS:
387 1.1 cegger mii->mii_media_active |= IFM_10_T;
388 1.1 cegger sc->mii_ticks = 0;
389 1.1 cegger break;
390 1.1 cegger default:
391 1.1 cegger mii->mii_media_active |= IFM_NONE;
392 1.1 cegger return;
393 1.1 cegger }
394 1.1 cegger
395 1.1 cegger if (ssr & ATPHY_SSR_DUPLEX)
396 1.1 cegger mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
397 1.1 cegger else
398 1.1 cegger mii->mii_media_active |= IFM_HDX;
399 1.1 cegger
400 1.19 msaitoh PHY_READ(sc, MII_100T2SR, &gsr);
401 1.1 cegger if ((IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) &&
402 1.1 cegger gsr & GTSR_MS_RES)
403 1.1 cegger mii->mii_media_active |= IFM_ETH_MASTER;
404 1.1 cegger }
405 1.1 cegger
406 1.1 cegger static void
407 1.1 cegger atphy_reset(struct mii_softc *sc)
408 1.1 cegger {
409 1.24 hkenken struct atphy_softc *asc = (struct atphy_softc *)sc;
410 1.19 msaitoh uint16_t reg;
411 1.1 cegger int i;
412 1.1 cegger
413 1.1 cegger /* Take PHY out of power down mode. */
414 1.1 cegger PHY_WRITE(sc, 29, 0x29);
415 1.1 cegger PHY_WRITE(sc, 30, 0);
416 1.1 cegger
417 1.19 msaitoh PHY_READ(sc, ATPHY_SCR, ®);
418 1.1 cegger /* Enable automatic crossover. */
419 1.1 cegger reg |= ATPHY_SCR_AUTO_X_MODE;
420 1.1 cegger /* Disable power down. */
421 1.1 cegger reg &= ~ATPHY_SCR_MAC_PDOWN;
422 1.1 cegger /* Enable CRS on Tx. */
423 1.1 cegger reg |= ATPHY_SCR_ASSERT_CRS_ON_TX;
424 1.1 cegger /* Auto correction for reversed cable polarity. */
425 1.1 cegger reg |= ATPHY_SCR_POLARITY_REVERSAL;
426 1.1 cegger PHY_WRITE(sc, ATPHY_SCR, reg);
427 1.1 cegger
428 1.8 cegger atphy_mii_phy_auto(sc);
429 1.8 cegger
430 1.1 cegger /* Workaround F1 bug to reset phy. */
431 1.19 msaitoh PHY_READ(sc, MII_BMCR, ®);
432 1.19 msaitoh reg |= BMCR_RESET;
433 1.8 cegger PHY_WRITE(sc, MII_BMCR, reg);
434 1.1 cegger
435 1.1 cegger for (i = 0; i < 1000; i++) {
436 1.1 cegger DELAY(1);
437 1.19 msaitoh PHY_READ(sc, MII_BMCR, ®);
438 1.19 msaitoh if ((reg & BMCR_RESET) == 0)
439 1.1 cegger break;
440 1.1 cegger }
441 1.24 hkenken
442 1.24 hkenken if (asc->rgmii_tx_internal_delay) {
443 1.24 hkenken PHY_WRITE(sc, ATPHY_DEBUG_PORT_ADDR, 0x05);
444 1.24 hkenken PHY_WRITE(sc, ATPHY_DEBUG_PORT_DATA, ATPHY_RGMII_TX_CLK_DLY);
445 1.24 hkenken }
446 1.24 hkenken if (asc->rgmii_rx_internal_delay) {
447 1.24 hkenken PHY_WRITE(sc, ATPHY_DEBUG_PORT_ADDR, 0x00);
448 1.24 hkenken PHY_WRITE(sc, ATPHY_DEBUG_PORT_DATA, ATPHY_RGMII_RX_CLK_DLY);
449 1.24 hkenken }
450 1.1 cegger }
451 1.1 cegger
452 1.1 cegger static int
453 1.1 cegger atphy_mii_phy_auto(struct mii_softc *sc)
454 1.1 cegger {
455 1.1 cegger uint16_t anar;
456 1.1 cegger
457 1.13 msaitoh sc->mii_ticks = 0;
458 1.1 cegger anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
459 1.1 cegger if (sc->mii_flags & MIIF_DOPAUSE)
460 1.16 msaitoh anar |= ANAR_PAUSE_TOWARDS;
461 1.1 cegger PHY_WRITE(sc, MII_ANAR, anar);
462 1.1 cegger if (sc->mii_extcapabilities & (EXTSR_1000TFDX | EXTSR_1000THDX))
463 1.1 cegger PHY_WRITE(sc, MII_100T2CR, GTCR_ADV_1000TFDX |
464 1.1 cegger GTCR_ADV_1000THDX);
465 1.8 cegger PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
466 1.1 cegger
467 1.1 cegger return EJUSTRETURN;
468 1.1 cegger }
469