Commit d42be546fc6a61da7e2ca15bf3e91cc46f3210e6

Authored by Mugunthan V N
Committed by Lokesh Vutla
1 parent 3ec018bb44

net: phy: on phy device create do not initialize link to 1

Currently when phy device is created the link variable is
initialized to 1 which denoted phy link is already up. On a power
reset there is no issue as phy status register link status will
not be set, so phy auto negotiate will be started. But when a cpu
reset is issued (ex: dra72x-evm) phy's link status bit is already
set which leads to assume that link is already setup in
genphy_update_link() initial check which results in ehternet not
working. So do not assume that link is already up and on phy
device create set link to zero. This is verified on dra72x-evm.

Reported-by: Franklin S Cooper Jr <fcooper@ti.com>
Signed-off-by: Mugunthan V N <mugunthanvnm@ti.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

drivers/net/phy/phy.c
1 /* 1 /*
2 * Generic PHY Management code 2 * Generic PHY Management code
3 * 3 *
4 * SPDX-License-Identifier: GPL-2.0+ 4 * SPDX-License-Identifier: GPL-2.0+
5 * 5 *
6 * Copyright 2011 Freescale Semiconductor, Inc. 6 * Copyright 2011 Freescale Semiconductor, Inc.
7 * author Andy Fleming 7 * author Andy Fleming
8 * 8 *
9 * Based loosely off of Linux's PHY Lib 9 * Based loosely off of Linux's PHY Lib
10 */ 10 */
11 11
12 #include <config.h> 12 #include <config.h>
13 #include <common.h> 13 #include <common.h>
14 #include <dm.h> 14 #include <dm.h>
15 #include <malloc.h> 15 #include <malloc.h>
16 #include <net.h> 16 #include <net.h>
17 #include <command.h> 17 #include <command.h>
18 #include <miiphy.h> 18 #include <miiphy.h>
19 #include <phy.h> 19 #include <phy.h>
20 #include <errno.h> 20 #include <errno.h>
21 #include <linux/err.h> 21 #include <linux/err.h>
22 #include <linux/compiler.h> 22 #include <linux/compiler.h>
23 23
24 DECLARE_GLOBAL_DATA_PTR; 24 DECLARE_GLOBAL_DATA_PTR;
25 25
26 /* Generic PHY support and helper functions */ 26 /* Generic PHY support and helper functions */
27 27
28 /** 28 /**
29 * genphy_config_advert - sanitize and advertise auto-negotation parameters 29 * genphy_config_advert - sanitize and advertise auto-negotation parameters
30 * @phydev: target phy_device struct 30 * @phydev: target phy_device struct
31 * 31 *
32 * Description: Writes MII_ADVERTISE with the appropriate values, 32 * Description: Writes MII_ADVERTISE with the appropriate values,
33 * after sanitizing the values to make sure we only advertise 33 * after sanitizing the values to make sure we only advertise
34 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement 34 * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
35 * hasn't changed, and > 0 if it has changed. 35 * hasn't changed, and > 0 if it has changed.
36 */ 36 */
37 static int genphy_config_advert(struct phy_device *phydev) 37 static int genphy_config_advert(struct phy_device *phydev)
38 { 38 {
39 u32 advertise; 39 u32 advertise;
40 int oldadv, adv; 40 int oldadv, adv;
41 int err, changed = 0; 41 int err, changed = 0;
42 42
43 /* Only allow advertising what 43 /* Only allow advertising what
44 * this PHY supports */ 44 * this PHY supports */
45 phydev->advertising &= phydev->supported; 45 phydev->advertising &= phydev->supported;
46 advertise = phydev->advertising; 46 advertise = phydev->advertising;
47 47
48 /* Setup standard advertisement */ 48 /* Setup standard advertisement */
49 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE); 49 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
50 50
51 if (adv < 0) 51 if (adv < 0)
52 return adv; 52 return adv;
53 53
54 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | 54 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
55 ADVERTISE_PAUSE_ASYM); 55 ADVERTISE_PAUSE_ASYM);
56 if (advertise & ADVERTISED_10baseT_Half) 56 if (advertise & ADVERTISED_10baseT_Half)
57 adv |= ADVERTISE_10HALF; 57 adv |= ADVERTISE_10HALF;
58 if (advertise & ADVERTISED_10baseT_Full) 58 if (advertise & ADVERTISED_10baseT_Full)
59 adv |= ADVERTISE_10FULL; 59 adv |= ADVERTISE_10FULL;
60 if (advertise & ADVERTISED_100baseT_Half) 60 if (advertise & ADVERTISED_100baseT_Half)
61 adv |= ADVERTISE_100HALF; 61 adv |= ADVERTISE_100HALF;
62 if (advertise & ADVERTISED_100baseT_Full) 62 if (advertise & ADVERTISED_100baseT_Full)
63 adv |= ADVERTISE_100FULL; 63 adv |= ADVERTISE_100FULL;
64 if (advertise & ADVERTISED_Pause) 64 if (advertise & ADVERTISED_Pause)
65 adv |= ADVERTISE_PAUSE_CAP; 65 adv |= ADVERTISE_PAUSE_CAP;
66 if (advertise & ADVERTISED_Asym_Pause) 66 if (advertise & ADVERTISED_Asym_Pause)
67 adv |= ADVERTISE_PAUSE_ASYM; 67 adv |= ADVERTISE_PAUSE_ASYM;
68 if (advertise & ADVERTISED_1000baseX_Half) 68 if (advertise & ADVERTISED_1000baseX_Half)
69 adv |= ADVERTISE_1000XHALF; 69 adv |= ADVERTISE_1000XHALF;
70 if (advertise & ADVERTISED_1000baseX_Full) 70 if (advertise & ADVERTISED_1000baseX_Full)
71 adv |= ADVERTISE_1000XFULL; 71 adv |= ADVERTISE_1000XFULL;
72 72
73 if (adv != oldadv) { 73 if (adv != oldadv) {
74 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv); 74 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE, adv);
75 75
76 if (err < 0) 76 if (err < 0)
77 return err; 77 return err;
78 changed = 1; 78 changed = 1;
79 } 79 }
80 80
81 /* Configure gigabit if it's supported */ 81 /* Configure gigabit if it's supported */
82 if (phydev->supported & (SUPPORTED_1000baseT_Half | 82 if (phydev->supported & (SUPPORTED_1000baseT_Half |
83 SUPPORTED_1000baseT_Full)) { 83 SUPPORTED_1000baseT_Full)) {
84 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000); 84 oldadv = adv = phy_read(phydev, MDIO_DEVAD_NONE, MII_CTRL1000);
85 85
86 if (adv < 0) 86 if (adv < 0)
87 return adv; 87 return adv;
88 88
89 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); 89 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
90 if (advertise & SUPPORTED_1000baseT_Half) 90 if (advertise & SUPPORTED_1000baseT_Half)
91 adv |= ADVERTISE_1000HALF; 91 adv |= ADVERTISE_1000HALF;
92 if (advertise & SUPPORTED_1000baseT_Full) 92 if (advertise & SUPPORTED_1000baseT_Full)
93 adv |= ADVERTISE_1000FULL; 93 adv |= ADVERTISE_1000FULL;
94 94
95 if (adv != oldadv) { 95 if (adv != oldadv) {
96 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000, 96 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_CTRL1000,
97 adv); 97 adv);
98 98
99 if (err < 0) 99 if (err < 0)
100 return err; 100 return err;
101 changed = 1; 101 changed = 1;
102 } 102 }
103 } 103 }
104 104
105 return changed; 105 return changed;
106 } 106 }
107 107
108 108
109 /** 109 /**
110 * genphy_setup_forced - configures/forces speed/duplex from @phydev 110 * genphy_setup_forced - configures/forces speed/duplex from @phydev
111 * @phydev: target phy_device struct 111 * @phydev: target phy_device struct
112 * 112 *
113 * Description: Configures MII_BMCR to force speed/duplex 113 * Description: Configures MII_BMCR to force speed/duplex
114 * to the values in phydev. Assumes that the values are valid. 114 * to the values in phydev. Assumes that the values are valid.
115 */ 115 */
116 static int genphy_setup_forced(struct phy_device *phydev) 116 static int genphy_setup_forced(struct phy_device *phydev)
117 { 117 {
118 int err; 118 int err;
119 int ctl = 0; 119 int ctl = 0;
120 120
121 phydev->pause = phydev->asym_pause = 0; 121 phydev->pause = phydev->asym_pause = 0;
122 122
123 if (SPEED_1000 == phydev->speed) 123 if (SPEED_1000 == phydev->speed)
124 ctl |= BMCR_SPEED1000; 124 ctl |= BMCR_SPEED1000;
125 else if (SPEED_100 == phydev->speed) 125 else if (SPEED_100 == phydev->speed)
126 ctl |= BMCR_SPEED100; 126 ctl |= BMCR_SPEED100;
127 127
128 if (DUPLEX_FULL == phydev->duplex) 128 if (DUPLEX_FULL == phydev->duplex)
129 ctl |= BMCR_FULLDPLX; 129 ctl |= BMCR_FULLDPLX;
130 130
131 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl); 131 err = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
132 132
133 return err; 133 return err;
134 } 134 }
135 135
136 136
137 /** 137 /**
138 * genphy_restart_aneg - Enable and Restart Autonegotiation 138 * genphy_restart_aneg - Enable and Restart Autonegotiation
139 * @phydev: target phy_device struct 139 * @phydev: target phy_device struct
140 */ 140 */
141 int genphy_restart_aneg(struct phy_device *phydev) 141 int genphy_restart_aneg(struct phy_device *phydev)
142 { 142 {
143 int ctl; 143 int ctl;
144 144
145 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); 145 ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
146 146
147 if (ctl < 0) 147 if (ctl < 0)
148 return ctl; 148 return ctl;
149 149
150 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART); 150 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
151 151
152 /* Don't isolate the PHY if we're negotiating */ 152 /* Don't isolate the PHY if we're negotiating */
153 ctl &= ~(BMCR_ISOLATE); 153 ctl &= ~(BMCR_ISOLATE);
154 154
155 ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl); 155 ctl = phy_write(phydev, MDIO_DEVAD_NONE, MII_BMCR, ctl);
156 156
157 return ctl; 157 return ctl;
158 } 158 }
159 159
160 160
161 /** 161 /**
162 * genphy_config_aneg - restart auto-negotiation or write BMCR 162 * genphy_config_aneg - restart auto-negotiation or write BMCR
163 * @phydev: target phy_device struct 163 * @phydev: target phy_device struct
164 * 164 *
165 * Description: If auto-negotiation is enabled, we configure the 165 * Description: If auto-negotiation is enabled, we configure the
166 * advertising, and then restart auto-negotiation. If it is not 166 * advertising, and then restart auto-negotiation. If it is not
167 * enabled, then we write the BMCR. 167 * enabled, then we write the BMCR.
168 */ 168 */
169 int genphy_config_aneg(struct phy_device *phydev) 169 int genphy_config_aneg(struct phy_device *phydev)
170 { 170 {
171 int result; 171 int result;
172 172
173 if (AUTONEG_ENABLE != phydev->autoneg) 173 if (AUTONEG_ENABLE != phydev->autoneg)
174 return genphy_setup_forced(phydev); 174 return genphy_setup_forced(phydev);
175 175
176 result = genphy_config_advert(phydev); 176 result = genphy_config_advert(phydev);
177 177
178 if (result < 0) /* error */ 178 if (result < 0) /* error */
179 return result; 179 return result;
180 180
181 if (result == 0) { 181 if (result == 0) {
182 /* Advertisment hasn't changed, but maybe aneg was never on to 182 /* Advertisment hasn't changed, but maybe aneg was never on to
183 * begin with? Or maybe phy was isolated? */ 183 * begin with? Or maybe phy was isolated? */
184 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); 184 int ctl = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
185 185
186 if (ctl < 0) 186 if (ctl < 0)
187 return ctl; 187 return ctl;
188 188
189 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) 189 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
190 result = 1; /* do restart aneg */ 190 result = 1; /* do restart aneg */
191 } 191 }
192 192
193 /* Only restart aneg if we are advertising something different 193 /* Only restart aneg if we are advertising something different
194 * than we were before. */ 194 * than we were before. */
195 if (result > 0) 195 if (result > 0)
196 result = genphy_restart_aneg(phydev); 196 result = genphy_restart_aneg(phydev);
197 197
198 return result; 198 return result;
199 } 199 }
200 200
201 /** 201 /**
202 * genphy_update_link - update link status in @phydev 202 * genphy_update_link - update link status in @phydev
203 * @phydev: target phy_device struct 203 * @phydev: target phy_device struct
204 * 204 *
205 * Description: Update the value in phydev->link to reflect the 205 * Description: Update the value in phydev->link to reflect the
206 * current link value. In order to do this, we need to read 206 * current link value. In order to do this, we need to read
207 * the status register twice, keeping the second value. 207 * the status register twice, keeping the second value.
208 */ 208 */
209 int genphy_update_link(struct phy_device *phydev) 209 int genphy_update_link(struct phy_device *phydev)
210 { 210 {
211 unsigned int mii_reg; 211 unsigned int mii_reg;
212 212
213 /* 213 /*
214 * Wait if the link is up, and autonegotiation is in progress 214 * Wait if the link is up, and autonegotiation is in progress
215 * (ie - we're capable and it's not done) 215 * (ie - we're capable and it's not done)
216 */ 216 */
217 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); 217 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
218 218
219 /* 219 /*
220 * If we already saw the link up, and it hasn't gone down, then 220 * If we already saw the link up, and it hasn't gone down, then
221 * we don't need to wait for autoneg again 221 * we don't need to wait for autoneg again
222 */ 222 */
223 if (phydev->link && mii_reg & BMSR_LSTATUS) 223 if (phydev->link && mii_reg & BMSR_LSTATUS)
224 return 0; 224 return 0;
225 225
226 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) { 226 if ((mii_reg & BMSR_ANEGCAPABLE) && !(mii_reg & BMSR_ANEGCOMPLETE)) {
227 int i = 0; 227 int i = 0;
228 228
229 printf("%s Waiting for PHY auto negotiation to complete", 229 printf("%s Waiting for PHY auto negotiation to complete",
230 phydev->dev->name); 230 phydev->dev->name);
231 while (!(mii_reg & BMSR_ANEGCOMPLETE)) { 231 while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
232 /* 232 /*
233 * Timeout reached ? 233 * Timeout reached ?
234 */ 234 */
235 if (i > PHY_ANEG_TIMEOUT) { 235 if (i > PHY_ANEG_TIMEOUT) {
236 printf(" TIMEOUT !\n"); 236 printf(" TIMEOUT !\n");
237 phydev->link = 0; 237 phydev->link = 0;
238 return 0; 238 return 0;
239 } 239 }
240 240
241 if (ctrlc()) { 241 if (ctrlc()) {
242 puts("user interrupt!\n"); 242 puts("user interrupt!\n");
243 phydev->link = 0; 243 phydev->link = 0;
244 return -EINTR; 244 return -EINTR;
245 } 245 }
246 246
247 if ((i++ % 500) == 0) 247 if ((i++ % 500) == 0)
248 printf("."); 248 printf(".");
249 249
250 udelay(1000); /* 1 ms */ 250 udelay(1000); /* 1 ms */
251 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); 251 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
252 } 252 }
253 printf(" done\n"); 253 printf(" done\n");
254 phydev->link = 1; 254 phydev->link = 1;
255 } else { 255 } else {
256 /* Read the link a second time to clear the latched state */ 256 /* Read the link a second time to clear the latched state */
257 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); 257 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
258 258
259 if (mii_reg & BMSR_LSTATUS) 259 if (mii_reg & BMSR_LSTATUS)
260 phydev->link = 1; 260 phydev->link = 1;
261 else 261 else
262 phydev->link = 0; 262 phydev->link = 0;
263 } 263 }
264 264
265 return 0; 265 return 0;
266 } 266 }
267 267
268 /* 268 /*
269 * Generic function which updates the speed and duplex. If 269 * Generic function which updates the speed and duplex. If
270 * autonegotiation is enabled, it uses the AND of the link 270 * autonegotiation is enabled, it uses the AND of the link
271 * partner's advertised capabilities and our advertised 271 * partner's advertised capabilities and our advertised
272 * capabilities. If autonegotiation is disabled, we use the 272 * capabilities. If autonegotiation is disabled, we use the
273 * appropriate bits in the control register. 273 * appropriate bits in the control register.
274 * 274 *
275 * Stolen from Linux's mii.c and phy_device.c 275 * Stolen from Linux's mii.c and phy_device.c
276 */ 276 */
277 int genphy_parse_link(struct phy_device *phydev) 277 int genphy_parse_link(struct phy_device *phydev)
278 { 278 {
279 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); 279 int mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
280 280
281 /* We're using autonegotiation */ 281 /* We're using autonegotiation */
282 if (phydev->supported & SUPPORTED_Autoneg) { 282 if (phydev->supported & SUPPORTED_Autoneg) {
283 u32 lpa = 0; 283 u32 lpa = 0;
284 int gblpa = 0; 284 int gblpa = 0;
285 u32 estatus = 0; 285 u32 estatus = 0;
286 286
287 /* Check for gigabit capability */ 287 /* Check for gigabit capability */
288 if (phydev->supported & (SUPPORTED_1000baseT_Full | 288 if (phydev->supported & (SUPPORTED_1000baseT_Full |
289 SUPPORTED_1000baseT_Half)) { 289 SUPPORTED_1000baseT_Half)) {
290 /* We want a list of states supported by 290 /* We want a list of states supported by
291 * both PHYs in the link 291 * both PHYs in the link
292 */ 292 */
293 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000); 293 gblpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_STAT1000);
294 if (gblpa < 0) { 294 if (gblpa < 0) {
295 debug("Could not read MII_STAT1000. Ignoring gigabit capability\n"); 295 debug("Could not read MII_STAT1000. Ignoring gigabit capability\n");
296 gblpa = 0; 296 gblpa = 0;
297 } 297 }
298 gblpa &= phy_read(phydev, 298 gblpa &= phy_read(phydev,
299 MDIO_DEVAD_NONE, MII_CTRL1000) << 2; 299 MDIO_DEVAD_NONE, MII_CTRL1000) << 2;
300 } 300 }
301 301
302 /* Set the baseline so we only have to set them 302 /* Set the baseline so we only have to set them
303 * if they're different 303 * if they're different
304 */ 304 */
305 phydev->speed = SPEED_10; 305 phydev->speed = SPEED_10;
306 phydev->duplex = DUPLEX_HALF; 306 phydev->duplex = DUPLEX_HALF;
307 307
308 /* Check the gigabit fields */ 308 /* Check the gigabit fields */
309 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) { 309 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
310 phydev->speed = SPEED_1000; 310 phydev->speed = SPEED_1000;
311 311
312 if (gblpa & PHY_1000BTSR_1000FD) 312 if (gblpa & PHY_1000BTSR_1000FD)
313 phydev->duplex = DUPLEX_FULL; 313 phydev->duplex = DUPLEX_FULL;
314 314
315 /* We're done! */ 315 /* We're done! */
316 return 0; 316 return 0;
317 } 317 }
318 318
319 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE); 319 lpa = phy_read(phydev, MDIO_DEVAD_NONE, MII_ADVERTISE);
320 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA); 320 lpa &= phy_read(phydev, MDIO_DEVAD_NONE, MII_LPA);
321 321
322 if (lpa & (LPA_100FULL | LPA_100HALF)) { 322 if (lpa & (LPA_100FULL | LPA_100HALF)) {
323 phydev->speed = SPEED_100; 323 phydev->speed = SPEED_100;
324 324
325 if (lpa & LPA_100FULL) 325 if (lpa & LPA_100FULL)
326 phydev->duplex = DUPLEX_FULL; 326 phydev->duplex = DUPLEX_FULL;
327 327
328 } else if (lpa & LPA_10FULL) 328 } else if (lpa & LPA_10FULL)
329 phydev->duplex = DUPLEX_FULL; 329 phydev->duplex = DUPLEX_FULL;
330 330
331 /* 331 /*
332 * Extended status may indicate that the PHY supports 332 * Extended status may indicate that the PHY supports
333 * 1000BASE-T/X even though the 1000BASE-T registers 333 * 1000BASE-T/X even though the 1000BASE-T registers
334 * are missing. In this case we can't tell whether the 334 * are missing. In this case we can't tell whether the
335 * peer also supports it, so we only check extended 335 * peer also supports it, so we only check extended
336 * status if the 1000BASE-T registers are actually 336 * status if the 1000BASE-T registers are actually
337 * missing. 337 * missing.
338 */ 338 */
339 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP)) 339 if ((mii_reg & BMSR_ESTATEN) && !(mii_reg & BMSR_ERCAP))
340 estatus = phy_read(phydev, MDIO_DEVAD_NONE, 340 estatus = phy_read(phydev, MDIO_DEVAD_NONE,
341 MII_ESTATUS); 341 MII_ESTATUS);
342 342
343 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF | 343 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_XHALF |
344 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) { 344 ESTATUS_1000_TFULL | ESTATUS_1000_THALF)) {
345 phydev->speed = SPEED_1000; 345 phydev->speed = SPEED_1000;
346 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL)) 346 if (estatus & (ESTATUS_1000_XFULL | ESTATUS_1000_TFULL))
347 phydev->duplex = DUPLEX_FULL; 347 phydev->duplex = DUPLEX_FULL;
348 } 348 }
349 349
350 } else { 350 } else {
351 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR); 351 u32 bmcr = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMCR);
352 352
353 phydev->speed = SPEED_10; 353 phydev->speed = SPEED_10;
354 phydev->duplex = DUPLEX_HALF; 354 phydev->duplex = DUPLEX_HALF;
355 355
356 if (bmcr & BMCR_FULLDPLX) 356 if (bmcr & BMCR_FULLDPLX)
357 phydev->duplex = DUPLEX_FULL; 357 phydev->duplex = DUPLEX_FULL;
358 358
359 if (bmcr & BMCR_SPEED1000) 359 if (bmcr & BMCR_SPEED1000)
360 phydev->speed = SPEED_1000; 360 phydev->speed = SPEED_1000;
361 else if (bmcr & BMCR_SPEED100) 361 else if (bmcr & BMCR_SPEED100)
362 phydev->speed = SPEED_100; 362 phydev->speed = SPEED_100;
363 } 363 }
364 364
365 return 0; 365 return 0;
366 } 366 }
367 367
368 int genphy_config(struct phy_device *phydev) 368 int genphy_config(struct phy_device *phydev)
369 { 369 {
370 int val; 370 int val;
371 u32 features; 371 u32 features;
372 372
373 /* For now, I'll claim that the generic driver supports 373 /* For now, I'll claim that the generic driver supports
374 * all possible port types */ 374 * all possible port types */
375 features = (SUPPORTED_TP | SUPPORTED_MII 375 features = (SUPPORTED_TP | SUPPORTED_MII
376 | SUPPORTED_AUI | SUPPORTED_FIBRE | 376 | SUPPORTED_AUI | SUPPORTED_FIBRE |
377 SUPPORTED_BNC); 377 SUPPORTED_BNC);
378 378
379 /* Do we support autonegotiation? */ 379 /* Do we support autonegotiation? */
380 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR); 380 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_BMSR);
381 381
382 if (val < 0) 382 if (val < 0)
383 return val; 383 return val;
384 384
385 if (val & BMSR_ANEGCAPABLE) 385 if (val & BMSR_ANEGCAPABLE)
386 features |= SUPPORTED_Autoneg; 386 features |= SUPPORTED_Autoneg;
387 387
388 if (val & BMSR_100FULL) 388 if (val & BMSR_100FULL)
389 features |= SUPPORTED_100baseT_Full; 389 features |= SUPPORTED_100baseT_Full;
390 if (val & BMSR_100HALF) 390 if (val & BMSR_100HALF)
391 features |= SUPPORTED_100baseT_Half; 391 features |= SUPPORTED_100baseT_Half;
392 if (val & BMSR_10FULL) 392 if (val & BMSR_10FULL)
393 features |= SUPPORTED_10baseT_Full; 393 features |= SUPPORTED_10baseT_Full;
394 if (val & BMSR_10HALF) 394 if (val & BMSR_10HALF)
395 features |= SUPPORTED_10baseT_Half; 395 features |= SUPPORTED_10baseT_Half;
396 396
397 if (val & BMSR_ESTATEN) { 397 if (val & BMSR_ESTATEN) {
398 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS); 398 val = phy_read(phydev, MDIO_DEVAD_NONE, MII_ESTATUS);
399 399
400 if (val < 0) 400 if (val < 0)
401 return val; 401 return val;
402 402
403 if (val & ESTATUS_1000_TFULL) 403 if (val & ESTATUS_1000_TFULL)
404 features |= SUPPORTED_1000baseT_Full; 404 features |= SUPPORTED_1000baseT_Full;
405 if (val & ESTATUS_1000_THALF) 405 if (val & ESTATUS_1000_THALF)
406 features |= SUPPORTED_1000baseT_Half; 406 features |= SUPPORTED_1000baseT_Half;
407 if (val & ESTATUS_1000_XFULL) 407 if (val & ESTATUS_1000_XFULL)
408 features |= SUPPORTED_1000baseX_Full; 408 features |= SUPPORTED_1000baseX_Full;
409 if (val & ESTATUS_1000_XHALF) 409 if (val & ESTATUS_1000_XHALF)
410 features |= SUPPORTED_1000baseX_Half; 410 features |= SUPPORTED_1000baseX_Half;
411 } 411 }
412 412
413 phydev->supported = features; 413 phydev->supported = features;
414 phydev->advertising = features; 414 phydev->advertising = features;
415 415
416 genphy_config_aneg(phydev); 416 genphy_config_aneg(phydev);
417 417
418 return 0; 418 return 0;
419 } 419 }
420 420
421 int genphy_startup(struct phy_device *phydev) 421 int genphy_startup(struct phy_device *phydev)
422 { 422 {
423 genphy_update_link(phydev); 423 genphy_update_link(phydev);
424 genphy_parse_link(phydev); 424 genphy_parse_link(phydev);
425 425
426 return 0; 426 return 0;
427 } 427 }
428 428
429 int genphy_shutdown(struct phy_device *phydev) 429 int genphy_shutdown(struct phy_device *phydev)
430 { 430 {
431 return 0; 431 return 0;
432 } 432 }
433 433
434 static struct phy_driver genphy_driver = { 434 static struct phy_driver genphy_driver = {
435 .uid = 0xffffffff, 435 .uid = 0xffffffff,
436 .mask = 0xffffffff, 436 .mask = 0xffffffff,
437 .name = "Generic PHY", 437 .name = "Generic PHY",
438 .features = 0, 438 .features = 0,
439 .config = genphy_config, 439 .config = genphy_config,
440 .startup = genphy_startup, 440 .startup = genphy_startup,
441 .shutdown = genphy_shutdown, 441 .shutdown = genphy_shutdown,
442 }; 442 };
443 443
444 static LIST_HEAD(phy_drivers); 444 static LIST_HEAD(phy_drivers);
445 445
446 int phy_init(void) 446 int phy_init(void)
447 { 447 {
448 #ifdef CONFIG_PHY_AQUANTIA 448 #ifdef CONFIG_PHY_AQUANTIA
449 phy_aquantia_init(); 449 phy_aquantia_init();
450 #endif 450 #endif
451 #ifdef CONFIG_PHY_ATHEROS 451 #ifdef CONFIG_PHY_ATHEROS
452 phy_atheros_init(); 452 phy_atheros_init();
453 #endif 453 #endif
454 #ifdef CONFIG_PHY_BROADCOM 454 #ifdef CONFIG_PHY_BROADCOM
455 phy_broadcom_init(); 455 phy_broadcom_init();
456 #endif 456 #endif
457 #ifdef CONFIG_PHY_CORTINA 457 #ifdef CONFIG_PHY_CORTINA
458 phy_cortina_init(); 458 phy_cortina_init();
459 #endif 459 #endif
460 #ifdef CONFIG_PHY_DAVICOM 460 #ifdef CONFIG_PHY_DAVICOM
461 phy_davicom_init(); 461 phy_davicom_init();
462 #endif 462 #endif
463 #ifdef CONFIG_PHY_ET1011C 463 #ifdef CONFIG_PHY_ET1011C
464 phy_et1011c_init(); 464 phy_et1011c_init();
465 #endif 465 #endif
466 #ifdef CONFIG_PHY_LXT 466 #ifdef CONFIG_PHY_LXT
467 phy_lxt_init(); 467 phy_lxt_init();
468 #endif 468 #endif
469 #ifdef CONFIG_PHY_MARVELL 469 #ifdef CONFIG_PHY_MARVELL
470 phy_marvell_init(); 470 phy_marvell_init();
471 #endif 471 #endif
472 #ifdef CONFIG_PHY_MICREL 472 #ifdef CONFIG_PHY_MICREL
473 phy_micrel_init(); 473 phy_micrel_init();
474 #endif 474 #endif
475 #ifdef CONFIG_PHY_NATSEMI 475 #ifdef CONFIG_PHY_NATSEMI
476 phy_natsemi_init(); 476 phy_natsemi_init();
477 #endif 477 #endif
478 #ifdef CONFIG_PHY_REALTEK 478 #ifdef CONFIG_PHY_REALTEK
479 phy_realtek_init(); 479 phy_realtek_init();
480 #endif 480 #endif
481 #ifdef CONFIG_PHY_SMSC 481 #ifdef CONFIG_PHY_SMSC
482 phy_smsc_init(); 482 phy_smsc_init();
483 #endif 483 #endif
484 #ifdef CONFIG_PHY_TERANETICS 484 #ifdef CONFIG_PHY_TERANETICS
485 phy_teranetics_init(); 485 phy_teranetics_init();
486 #endif 486 #endif
487 #ifdef CONFIG_PHY_VITESSE 487 #ifdef CONFIG_PHY_VITESSE
488 phy_vitesse_init(); 488 phy_vitesse_init();
489 #endif 489 #endif
490 490
491 return 0; 491 return 0;
492 } 492 }
493 493
494 int phy_register(struct phy_driver *drv) 494 int phy_register(struct phy_driver *drv)
495 { 495 {
496 INIT_LIST_HEAD(&drv->list); 496 INIT_LIST_HEAD(&drv->list);
497 list_add_tail(&drv->list, &phy_drivers); 497 list_add_tail(&drv->list, &phy_drivers);
498 498
499 #ifdef CONFIG_NEEDS_MANUAL_RELOC 499 #ifdef CONFIG_NEEDS_MANUAL_RELOC
500 if (drv->probe) 500 if (drv->probe)
501 drv->probe += gd->reloc_off; 501 drv->probe += gd->reloc_off;
502 if (drv->config) 502 if (drv->config)
503 drv->config += gd->reloc_off; 503 drv->config += gd->reloc_off;
504 if (drv->startup) 504 if (drv->startup)
505 drv->startup += gd->reloc_off; 505 drv->startup += gd->reloc_off;
506 if (drv->shutdown) 506 if (drv->shutdown)
507 drv->shutdown += gd->reloc_off; 507 drv->shutdown += gd->reloc_off;
508 if (drv->readext) 508 if (drv->readext)
509 drv->readext += gd->reloc_off; 509 drv->readext += gd->reloc_off;
510 if (drv->writeext) 510 if (drv->writeext)
511 drv->writeext += gd->reloc_off; 511 drv->writeext += gd->reloc_off;
512 #endif 512 #endif
513 return 0; 513 return 0;
514 } 514 }
515 515
516 static int phy_probe(struct phy_device *phydev) 516 static int phy_probe(struct phy_device *phydev)
517 { 517 {
518 int err = 0; 518 int err = 0;
519 519
520 phydev->advertising = phydev->supported = phydev->drv->features; 520 phydev->advertising = phydev->supported = phydev->drv->features;
521 phydev->mmds = phydev->drv->mmds; 521 phydev->mmds = phydev->drv->mmds;
522 522
523 if (phydev->drv->probe) 523 if (phydev->drv->probe)
524 err = phydev->drv->probe(phydev); 524 err = phydev->drv->probe(phydev);
525 525
526 return err; 526 return err;
527 } 527 }
528 528
529 static struct phy_driver *generic_for_interface(phy_interface_t interface) 529 static struct phy_driver *generic_for_interface(phy_interface_t interface)
530 { 530 {
531 #ifdef CONFIG_PHYLIB_10G 531 #ifdef CONFIG_PHYLIB_10G
532 if (is_10g_interface(interface)) 532 if (is_10g_interface(interface))
533 return &gen10g_driver; 533 return &gen10g_driver;
534 #endif 534 #endif
535 535
536 return &genphy_driver; 536 return &genphy_driver;
537 } 537 }
538 538
539 static struct phy_driver *get_phy_driver(struct phy_device *phydev, 539 static struct phy_driver *get_phy_driver(struct phy_device *phydev,
540 phy_interface_t interface) 540 phy_interface_t interface)
541 { 541 {
542 struct list_head *entry; 542 struct list_head *entry;
543 int phy_id = phydev->phy_id; 543 int phy_id = phydev->phy_id;
544 struct phy_driver *drv = NULL; 544 struct phy_driver *drv = NULL;
545 545
546 list_for_each(entry, &phy_drivers) { 546 list_for_each(entry, &phy_drivers) {
547 drv = list_entry(entry, struct phy_driver, list); 547 drv = list_entry(entry, struct phy_driver, list);
548 if ((drv->uid & drv->mask) == (phy_id & drv->mask)) 548 if ((drv->uid & drv->mask) == (phy_id & drv->mask))
549 return drv; 549 return drv;
550 } 550 }
551 551
552 /* If we made it here, there's no driver for this PHY */ 552 /* If we made it here, there's no driver for this PHY */
553 return generic_for_interface(interface); 553 return generic_for_interface(interface);
554 } 554 }
555 555
556 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr, 556 static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
557 int phy_id, 557 int phy_id,
558 phy_interface_t interface) 558 phy_interface_t interface)
559 { 559 {
560 struct phy_device *dev; 560 struct phy_device *dev;
561 561
562 /* We allocate the device, and initialize the 562 /* We allocate the device, and initialize the
563 * default values */ 563 * default values */
564 dev = malloc(sizeof(*dev)); 564 dev = malloc(sizeof(*dev));
565 if (!dev) { 565 if (!dev) {
566 printf("Failed to allocate PHY device for %s:%d\n", 566 printf("Failed to allocate PHY device for %s:%d\n",
567 bus->name, addr); 567 bus->name, addr);
568 return NULL; 568 return NULL;
569 } 569 }
570 570
571 memset(dev, 0, sizeof(*dev)); 571 memset(dev, 0, sizeof(*dev));
572 572
573 dev->duplex = -1; 573 dev->duplex = -1;
574 dev->link = 1; 574 dev->link = 0;
575 dev->interface = interface; 575 dev->interface = interface;
576 576
577 dev->autoneg = AUTONEG_ENABLE; 577 dev->autoneg = AUTONEG_ENABLE;
578 578
579 dev->addr = addr; 579 dev->addr = addr;
580 dev->phy_id = phy_id; 580 dev->phy_id = phy_id;
581 dev->bus = bus; 581 dev->bus = bus;
582 582
583 dev->drv = get_phy_driver(dev, interface); 583 dev->drv = get_phy_driver(dev, interface);
584 584
585 phy_probe(dev); 585 phy_probe(dev);
586 586
587 bus->phymap[addr] = dev; 587 bus->phymap[addr] = dev;
588 588
589 return dev; 589 return dev;
590 } 590 }
591 591
592 /** 592 /**
593 * get_phy_id - reads the specified addr for its ID. 593 * get_phy_id - reads the specified addr for its ID.
594 * @bus: the target MII bus 594 * @bus: the target MII bus
595 * @addr: PHY address on the MII bus 595 * @addr: PHY address on the MII bus
596 * @phy_id: where to store the ID retrieved. 596 * @phy_id: where to store the ID retrieved.
597 * 597 *
598 * Description: Reads the ID registers of the PHY at @addr on the 598 * Description: Reads the ID registers of the PHY at @addr on the
599 * @bus, stores it in @phy_id and returns zero on success. 599 * @bus, stores it in @phy_id and returns zero on success.
600 */ 600 */
601 int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id) 601 int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
602 { 602 {
603 int phy_reg; 603 int phy_reg;
604 604
605 /* Grab the bits from PHYIR1, and put them 605 /* Grab the bits from PHYIR1, and put them
606 * in the upper half */ 606 * in the upper half */
607 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1); 607 phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
608 608
609 if (phy_reg < 0) 609 if (phy_reg < 0)
610 return -EIO; 610 return -EIO;
611 611
612 *phy_id = (phy_reg & 0xffff) << 16; 612 *phy_id = (phy_reg & 0xffff) << 16;
613 613
614 /* Grab the bits from PHYIR2, and put them in the lower half */ 614 /* Grab the bits from PHYIR2, and put them in the lower half */
615 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2); 615 phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
616 616
617 if (phy_reg < 0) 617 if (phy_reg < 0)
618 return -EIO; 618 return -EIO;
619 619
620 *phy_id |= (phy_reg & 0xffff); 620 *phy_id |= (phy_reg & 0xffff);
621 621
622 return 0; 622 return 0;
623 } 623 }
624 624
625 static struct phy_device *create_phy_by_mask(struct mii_dev *bus, 625 static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
626 unsigned phy_mask, int devad, phy_interface_t interface) 626 unsigned phy_mask, int devad, phy_interface_t interface)
627 { 627 {
628 u32 phy_id = 0xffffffff; 628 u32 phy_id = 0xffffffff;
629 while (phy_mask) { 629 while (phy_mask) {
630 int addr = ffs(phy_mask) - 1; 630 int addr = ffs(phy_mask) - 1;
631 int r = get_phy_id(bus, addr, devad, &phy_id); 631 int r = get_phy_id(bus, addr, devad, &phy_id);
632 /* If the PHY ID is mostly f's, we didn't find anything */ 632 /* If the PHY ID is mostly f's, we didn't find anything */
633 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff) 633 if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
634 return phy_device_create(bus, addr, phy_id, interface); 634 return phy_device_create(bus, addr, phy_id, interface);
635 phy_mask &= ~(1 << addr); 635 phy_mask &= ~(1 << addr);
636 } 636 }
637 return NULL; 637 return NULL;
638 } 638 }
639 639
640 static struct phy_device *search_for_existing_phy(struct mii_dev *bus, 640 static struct phy_device *search_for_existing_phy(struct mii_dev *bus,
641 unsigned phy_mask, phy_interface_t interface) 641 unsigned phy_mask, phy_interface_t interface)
642 { 642 {
643 /* If we have one, return the existing device, with new interface */ 643 /* If we have one, return the existing device, with new interface */
644 while (phy_mask) { 644 while (phy_mask) {
645 int addr = ffs(phy_mask) - 1; 645 int addr = ffs(phy_mask) - 1;
646 if (bus->phymap[addr]) { 646 if (bus->phymap[addr]) {
647 bus->phymap[addr]->interface = interface; 647 bus->phymap[addr]->interface = interface;
648 return bus->phymap[addr]; 648 return bus->phymap[addr];
649 } 649 }
650 phy_mask &= ~(1 << addr); 650 phy_mask &= ~(1 << addr);
651 } 651 }
652 return NULL; 652 return NULL;
653 } 653 }
654 654
655 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus, 655 static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
656 unsigned phy_mask, phy_interface_t interface) 656 unsigned phy_mask, phy_interface_t interface)
657 { 657 {
658 int i; 658 int i;
659 struct phy_device *phydev; 659 struct phy_device *phydev;
660 660
661 phydev = search_for_existing_phy(bus, phy_mask, interface); 661 phydev = search_for_existing_phy(bus, phy_mask, interface);
662 if (phydev) 662 if (phydev)
663 return phydev; 663 return phydev;
664 /* Try Standard (ie Clause 22) access */ 664 /* Try Standard (ie Clause 22) access */
665 /* Otherwise we have to try Clause 45 */ 665 /* Otherwise we have to try Clause 45 */
666 for (i = 0; i < 5; i++) { 666 for (i = 0; i < 5; i++) {
667 phydev = create_phy_by_mask(bus, phy_mask, 667 phydev = create_phy_by_mask(bus, phy_mask,
668 i ? i : MDIO_DEVAD_NONE, interface); 668 i ? i : MDIO_DEVAD_NONE, interface);
669 if (IS_ERR(phydev)) 669 if (IS_ERR(phydev))
670 return NULL; 670 return NULL;
671 if (phydev) 671 if (phydev)
672 return phydev; 672 return phydev;
673 } 673 }
674 printf("Phy %d not found\n", ffs(phy_mask) - 1); 674 printf("Phy %d not found\n", ffs(phy_mask) - 1);
675 return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface); 675 return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
676 } 676 }
677 677
678 /** 678 /**
679 * get_phy_device - reads the specified PHY device and returns its @phy_device struct 679 * get_phy_device - reads the specified PHY device and returns its @phy_device struct
680 * @bus: the target MII bus 680 * @bus: the target MII bus
681 * @addr: PHY address on the MII bus 681 * @addr: PHY address on the MII bus
682 * 682 *
683 * Description: Reads the ID registers of the PHY at @addr on the 683 * Description: Reads the ID registers of the PHY at @addr on the
684 * @bus, then allocates and returns the phy_device to represent it. 684 * @bus, then allocates and returns the phy_device to represent it.
685 */ 685 */
686 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr, 686 static struct phy_device *get_phy_device(struct mii_dev *bus, int addr,
687 phy_interface_t interface) 687 phy_interface_t interface)
688 { 688 {
689 return get_phy_device_by_mask(bus, 1 << addr, interface); 689 return get_phy_device_by_mask(bus, 1 << addr, interface);
690 } 690 }
691 691
692 int phy_reset(struct phy_device *phydev) 692 int phy_reset(struct phy_device *phydev)
693 { 693 {
694 int reg; 694 int reg;
695 int timeout = 500; 695 int timeout = 500;
696 int devad = MDIO_DEVAD_NONE; 696 int devad = MDIO_DEVAD_NONE;
697 697
698 #ifdef CONFIG_PHYLIB_10G 698 #ifdef CONFIG_PHYLIB_10G
699 /* If it's 10G, we need to issue reset through one of the MMDs */ 699 /* If it's 10G, we need to issue reset through one of the MMDs */
700 if (is_10g_interface(phydev->interface)) { 700 if (is_10g_interface(phydev->interface)) {
701 if (!phydev->mmds) 701 if (!phydev->mmds)
702 gen10g_discover_mmds(phydev); 702 gen10g_discover_mmds(phydev);
703 703
704 devad = ffs(phydev->mmds) - 1; 704 devad = ffs(phydev->mmds) - 1;
705 } 705 }
706 #endif 706 #endif
707 707
708 reg = phy_read(phydev, devad, MII_BMCR); 708 reg = phy_read(phydev, devad, MII_BMCR);
709 if (reg < 0) { 709 if (reg < 0) {
710 debug("PHY status read failed\n"); 710 debug("PHY status read failed\n");
711 return -1; 711 return -1;
712 } 712 }
713 713
714 reg |= BMCR_RESET; 714 reg |= BMCR_RESET;
715 715
716 if (phy_write(phydev, devad, MII_BMCR, reg) < 0) { 716 if (phy_write(phydev, devad, MII_BMCR, reg) < 0) {
717 debug("PHY reset failed\n"); 717 debug("PHY reset failed\n");
718 return -1; 718 return -1;
719 } 719 }
720 720
721 #ifdef CONFIG_PHY_RESET_DELAY 721 #ifdef CONFIG_PHY_RESET_DELAY
722 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */ 722 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
723 #endif 723 #endif
724 /* 724 /*
725 * Poll the control register for the reset bit to go to 0 (it is 725 * Poll the control register for the reset bit to go to 0 (it is
726 * auto-clearing). This should happen within 0.5 seconds per the 726 * auto-clearing). This should happen within 0.5 seconds per the
727 * IEEE spec. 727 * IEEE spec.
728 */ 728 */
729 while ((reg & BMCR_RESET) && timeout--) { 729 while ((reg & BMCR_RESET) && timeout--) {
730 reg = phy_read(phydev, devad, MII_BMCR); 730 reg = phy_read(phydev, devad, MII_BMCR);
731 731
732 if (reg < 0) { 732 if (reg < 0) {
733 debug("PHY status read failed\n"); 733 debug("PHY status read failed\n");
734 return -1; 734 return -1;
735 } 735 }
736 udelay(1000); 736 udelay(1000);
737 } 737 }
738 738
739 if (reg & BMCR_RESET) { 739 if (reg & BMCR_RESET) {
740 puts("PHY reset timed out\n"); 740 puts("PHY reset timed out\n");
741 return -1; 741 return -1;
742 } 742 }
743 743
744 return 0; 744 return 0;
745 } 745 }
746 746
747 int miiphy_reset(const char *devname, unsigned char addr) 747 int miiphy_reset(const char *devname, unsigned char addr)
748 { 748 {
749 struct mii_dev *bus = miiphy_get_dev_by_name(devname); 749 struct mii_dev *bus = miiphy_get_dev_by_name(devname);
750 struct phy_device *phydev; 750 struct phy_device *phydev;
751 751
752 /* 752 /*
753 * miiphy_reset was only used on standard PHYs, so we'll fake it here. 753 * miiphy_reset was only used on standard PHYs, so we'll fake it here.
754 * If later code tries to connect with the right interface, this will 754 * If later code tries to connect with the right interface, this will
755 * be corrected by get_phy_device in phy_connect() 755 * be corrected by get_phy_device in phy_connect()
756 */ 756 */
757 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII); 757 phydev = get_phy_device(bus, addr, PHY_INTERFACE_MODE_MII);
758 758
759 return phy_reset(phydev); 759 return phy_reset(phydev);
760 } 760 }
761 761
762 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask, 762 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
763 phy_interface_t interface) 763 phy_interface_t interface)
764 { 764 {
765 /* Reset the bus */ 765 /* Reset the bus */
766 if (bus->reset) 766 if (bus->reset)
767 bus->reset(bus); 767 bus->reset(bus);
768 768
769 /* Wait 15ms to make sure the PHY has come out of hard reset */ 769 /* Wait 15ms to make sure the PHY has come out of hard reset */
770 udelay(15000); 770 udelay(15000);
771 return get_phy_device_by_mask(bus, phy_mask, interface); 771 return get_phy_device_by_mask(bus, phy_mask, interface);
772 } 772 }
773 773
774 #ifdef CONFIG_DM_ETH 774 #ifdef CONFIG_DM_ETH
775 void phy_connect_dev(struct phy_device *phydev, struct udevice *dev) 775 void phy_connect_dev(struct phy_device *phydev, struct udevice *dev)
776 #else 776 #else
777 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev) 777 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev)
778 #endif 778 #endif
779 { 779 {
780 /* Soft Reset the PHY */ 780 /* Soft Reset the PHY */
781 phy_reset(phydev); 781 phy_reset(phydev);
782 if (phydev->dev) { 782 if (phydev->dev) {
783 printf("%s:%d is connected to %s. Reconnecting to %s\n", 783 printf("%s:%d is connected to %s. Reconnecting to %s\n",
784 phydev->bus->name, phydev->addr, 784 phydev->bus->name, phydev->addr,
785 phydev->dev->name, dev->name); 785 phydev->dev->name, dev->name);
786 } 786 }
787 phydev->dev = dev; 787 phydev->dev = dev;
788 debug("%s connected to %s\n", dev->name, phydev->drv->name); 788 debug("%s connected to %s\n", dev->name, phydev->drv->name);
789 } 789 }
790 790
791 #ifdef CONFIG_DM_ETH 791 #ifdef CONFIG_DM_ETH
792 struct phy_device *phy_connect(struct mii_dev *bus, int addr, 792 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
793 struct udevice *dev, phy_interface_t interface) 793 struct udevice *dev, phy_interface_t interface)
794 #else 794 #else
795 struct phy_device *phy_connect(struct mii_dev *bus, int addr, 795 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
796 struct eth_device *dev, phy_interface_t interface) 796 struct eth_device *dev, phy_interface_t interface)
797 #endif 797 #endif
798 { 798 {
799 struct phy_device *phydev; 799 struct phy_device *phydev;
800 800
801 phydev = phy_find_by_mask(bus, 1 << addr, interface); 801 phydev = phy_find_by_mask(bus, 1 << addr, interface);
802 if (phydev) 802 if (phydev)
803 phy_connect_dev(phydev, dev); 803 phy_connect_dev(phydev, dev);
804 else 804 else
805 printf("Could not get PHY for %s: addr %d\n", bus->name, addr); 805 printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
806 return phydev; 806 return phydev;
807 } 807 }
808 808
809 /* 809 /*
810 * Start the PHY. Returns 0 on success, or a negative error code. 810 * Start the PHY. Returns 0 on success, or a negative error code.
811 */ 811 */
812 int phy_startup(struct phy_device *phydev) 812 int phy_startup(struct phy_device *phydev)
813 { 813 {
814 if (phydev->drv->startup) 814 if (phydev->drv->startup)
815 return phydev->drv->startup(phydev); 815 return phydev->drv->startup(phydev);
816 816
817 return 0; 817 return 0;
818 } 818 }
819 819
820 __weak int board_phy_config(struct phy_device *phydev) 820 __weak int board_phy_config(struct phy_device *phydev)
821 { 821 {
822 if (phydev->drv->config) 822 if (phydev->drv->config)
823 return phydev->drv->config(phydev); 823 return phydev->drv->config(phydev);
824 return 0; 824 return 0;
825 } 825 }
826 826
827 int phy_config(struct phy_device *phydev) 827 int phy_config(struct phy_device *phydev)
828 { 828 {
829 /* Invoke an optional board-specific helper */ 829 /* Invoke an optional board-specific helper */
830 board_phy_config(phydev); 830 board_phy_config(phydev);
831 831
832 return 0; 832 return 0;
833 } 833 }
834 834
835 int phy_shutdown(struct phy_device *phydev) 835 int phy_shutdown(struct phy_device *phydev)
836 { 836 {
837 if (phydev->drv->shutdown) 837 if (phydev->drv->shutdown)
838 phydev->drv->shutdown(phydev); 838 phydev->drv->shutdown(phydev);
839 839
840 return 0; 840 return 0;
841 } 841 }
842 842
843 int phy_get_interface_by_name(const char *str) 843 int phy_get_interface_by_name(const char *str)
844 { 844 {
845 int i; 845 int i;
846 846
847 for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) { 847 for (i = 0; i < PHY_INTERFACE_MODE_COUNT; i++) {
848 if (!strcmp(str, phy_interface_strings[i])) 848 if (!strcmp(str, phy_interface_strings[i]))
849 return i; 849 return i;
850 } 850 }
851 851
852 return -1; 852 return -1;
853 } 853 }
854 854