1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * (C) Copyright 2001-2015
3*4882a593Smuzhiyun * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4*4882a593Smuzhiyun * Joe Hershberger, National Instruments
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <common.h>
10*4882a593Smuzhiyun #include <dm.h>
11*4882a593Smuzhiyun #include <environment.h>
12*4882a593Smuzhiyun #include <net.h>
13*4882a593Smuzhiyun #include <dm/device-internal.h>
14*4882a593Smuzhiyun #include <dm/uclass-internal.h>
15*4882a593Smuzhiyun #include "eth_internal.h"
16*4882a593Smuzhiyun #include <eth_phy.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /**
21*4882a593Smuzhiyun * struct eth_device_priv - private structure for each Ethernet device
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun struct eth_device_priv {
26*4882a593Smuzhiyun enum eth_state_t state;
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /**
30*4882a593Smuzhiyun * struct eth_uclass_priv - The structure attached to the uclass itself
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * @current: The Ethernet device that the network functions are using
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun struct eth_uclass_priv {
35*4882a593Smuzhiyun struct udevice *current;
36*4882a593Smuzhiyun };
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /* eth_errno - This stores the most recent failure code from DM functions */
39*4882a593Smuzhiyun static int eth_errno;
40*4882a593Smuzhiyun
eth_get_uclass_priv(void)41*4882a593Smuzhiyun static struct eth_uclass_priv *eth_get_uclass_priv(void)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun struct uclass *uc;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun uclass_get(UCLASS_ETH, &uc);
46*4882a593Smuzhiyun assert(uc);
47*4882a593Smuzhiyun return uc->priv;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
eth_set_current_to_next(void)50*4882a593Smuzhiyun void eth_set_current_to_next(void)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun struct eth_uclass_priv *uc_priv;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun uc_priv = eth_get_uclass_priv();
55*4882a593Smuzhiyun if (uc_priv->current)
56*4882a593Smuzhiyun uclass_next_device(&uc_priv->current);
57*4882a593Smuzhiyun if (!uc_priv->current)
58*4882a593Smuzhiyun uclass_first_device(UCLASS_ETH, &uc_priv->current);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun * Typically this will simply return the active device.
63*4882a593Smuzhiyun * In the case where the most recent active device was unset, this will attempt
64*4882a593Smuzhiyun * to return the first device. If that device doesn't exist or fails to probe,
65*4882a593Smuzhiyun * this function will return NULL.
66*4882a593Smuzhiyun */
eth_get_dev(void)67*4882a593Smuzhiyun struct udevice *eth_get_dev(void)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun struct eth_uclass_priv *uc_priv;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun uc_priv = eth_get_uclass_priv();
72*4882a593Smuzhiyun if (!uc_priv->current)
73*4882a593Smuzhiyun eth_errno = uclass_first_device(UCLASS_ETH,
74*4882a593Smuzhiyun &uc_priv->current);
75*4882a593Smuzhiyun return uc_priv->current;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun * Typically this will just store a device pointer.
80*4882a593Smuzhiyun * In case it was not probed, we will attempt to do so.
81*4882a593Smuzhiyun * dev may be NULL to unset the active device.
82*4882a593Smuzhiyun */
eth_set_dev(struct udevice * dev)83*4882a593Smuzhiyun void eth_set_dev(struct udevice *dev)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun if (dev && !device_active(dev)) {
86*4882a593Smuzhiyun eth_errno = device_probe(dev);
87*4882a593Smuzhiyun if (eth_errno)
88*4882a593Smuzhiyun dev = NULL;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun eth_get_uclass_priv()->current = dev;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /*
95*4882a593Smuzhiyun * Find the udevice that either has the name passed in as devname or has an
96*4882a593Smuzhiyun * alias named devname.
97*4882a593Smuzhiyun */
eth_get_dev_by_name(const char * devname)98*4882a593Smuzhiyun struct udevice *eth_get_dev_by_name(const char *devname)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun int seq = -1;
101*4882a593Smuzhiyun char *endp = NULL;
102*4882a593Smuzhiyun const char *startp = NULL;
103*4882a593Smuzhiyun struct udevice *it;
104*4882a593Smuzhiyun struct uclass *uc;
105*4882a593Smuzhiyun int len = strlen("eth");
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /* Must be longer than 3 to be an alias */
108*4882a593Smuzhiyun if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
109*4882a593Smuzhiyun startp = devname + len;
110*4882a593Smuzhiyun seq = simple_strtoul(startp, &endp, 10);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun uclass_get(UCLASS_ETH, &uc);
114*4882a593Smuzhiyun uclass_foreach_dev(it, uc) {
115*4882a593Smuzhiyun /*
116*4882a593Smuzhiyun * We need the seq to be valid, so try to probe it.
117*4882a593Smuzhiyun * If the probe fails, the seq will not match since it will be
118*4882a593Smuzhiyun * -1 instead of what we are looking for.
119*4882a593Smuzhiyun * We don't care about errors from probe here. Either they won't
120*4882a593Smuzhiyun * match an alias or it will match a literal name and we'll pick
121*4882a593Smuzhiyun * up the error when we try to probe again in eth_set_dev().
122*4882a593Smuzhiyun */
123*4882a593Smuzhiyun if (device_probe(it))
124*4882a593Smuzhiyun continue;
125*4882a593Smuzhiyun /* Check for the name or the sequence number to match */
126*4882a593Smuzhiyun if (strcmp(it->name, devname) == 0 ||
127*4882a593Smuzhiyun (endp > startp && it->seq == seq))
128*4882a593Smuzhiyun return it;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun return NULL;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun
eth_get_ethaddr(void)134*4882a593Smuzhiyun unsigned char *eth_get_ethaddr(void)
135*4882a593Smuzhiyun {
136*4882a593Smuzhiyun struct eth_pdata *pdata;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun if (eth_get_dev()) {
139*4882a593Smuzhiyun pdata = eth_get_dev()->platdata;
140*4882a593Smuzhiyun return pdata->enetaddr;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun return NULL;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /* Set active state without calling start on the driver */
eth_init_state_only(void)147*4882a593Smuzhiyun int eth_init_state_only(void)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun struct udevice *current;
150*4882a593Smuzhiyun struct eth_device_priv *priv;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun current = eth_get_dev();
153*4882a593Smuzhiyun if (!current || !device_active(current))
154*4882a593Smuzhiyun return -EINVAL;
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun priv = current->uclass_priv;
157*4882a593Smuzhiyun priv->state = ETH_STATE_ACTIVE;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun return 0;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /* Set passive state without calling stop on the driver */
eth_halt_state_only(void)163*4882a593Smuzhiyun void eth_halt_state_only(void)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun struct udevice *current;
166*4882a593Smuzhiyun struct eth_device_priv *priv;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun current = eth_get_dev();
169*4882a593Smuzhiyun if (!current || !device_active(current))
170*4882a593Smuzhiyun return;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun priv = current->uclass_priv;
173*4882a593Smuzhiyun priv->state = ETH_STATE_PASSIVE;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
eth_get_dev_index(void)176*4882a593Smuzhiyun int eth_get_dev_index(void)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun if (eth_get_dev())
179*4882a593Smuzhiyun return eth_get_dev()->seq;
180*4882a593Smuzhiyun return -1;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
eth_write_hwaddr(struct udevice * dev)183*4882a593Smuzhiyun static int eth_write_hwaddr(struct udevice *dev)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun struct eth_pdata *pdata;
186*4882a593Smuzhiyun int ret = 0;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun if (!dev || !device_active(dev))
189*4882a593Smuzhiyun return -EINVAL;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /* seq is valid since the device is active */
192*4882a593Smuzhiyun if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
193*4882a593Smuzhiyun pdata = dev->platdata;
194*4882a593Smuzhiyun if (!is_valid_ethaddr(pdata->enetaddr)) {
195*4882a593Smuzhiyun printf("\nError: %s address %pM illegal value\n",
196*4882a593Smuzhiyun dev->name, pdata->enetaddr);
197*4882a593Smuzhiyun return -EINVAL;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /*
201*4882a593Smuzhiyun * Drivers are allowed to decide not to implement this at
202*4882a593Smuzhiyun * run-time. E.g. Some devices may use it and some may not.
203*4882a593Smuzhiyun */
204*4882a593Smuzhiyun ret = eth_get_ops(dev)->write_hwaddr(dev);
205*4882a593Smuzhiyun if (ret == -ENOSYS)
206*4882a593Smuzhiyun ret = 0;
207*4882a593Smuzhiyun if (ret)
208*4882a593Smuzhiyun printf("\nWarning: %s failed to set MAC address\n",
209*4882a593Smuzhiyun dev->name);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun return ret;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
on_ethaddr(const char * name,const char * value,enum env_op op,int flags)215*4882a593Smuzhiyun static int on_ethaddr(const char *name, const char *value, enum env_op op,
216*4882a593Smuzhiyun int flags)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun int index;
219*4882a593Smuzhiyun int retval;
220*4882a593Smuzhiyun struct udevice *dev;
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /* look for an index after "eth" */
223*4882a593Smuzhiyun index = simple_strtoul(name + 3, NULL, 10);
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
226*4882a593Smuzhiyun if (!retval) {
227*4882a593Smuzhiyun struct eth_pdata *pdata = dev->platdata;
228*4882a593Smuzhiyun switch (op) {
229*4882a593Smuzhiyun case env_op_create:
230*4882a593Smuzhiyun case env_op_overwrite:
231*4882a593Smuzhiyun eth_parse_enetaddr(value, pdata->enetaddr);
232*4882a593Smuzhiyun eth_write_hwaddr(dev);
233*4882a593Smuzhiyun break;
234*4882a593Smuzhiyun case env_op_delete:
235*4882a593Smuzhiyun memset(pdata->enetaddr, 0, ARP_HLEN);
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun return 0;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
242*4882a593Smuzhiyun
eth_init(void)243*4882a593Smuzhiyun int eth_init(void)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun char *ethact = env_get("ethact");
246*4882a593Smuzhiyun char *ethrotate = env_get("ethrotate");
247*4882a593Smuzhiyun struct udevice *current = NULL;
248*4882a593Smuzhiyun struct udevice *old_current;
249*4882a593Smuzhiyun int ret = -ENODEV;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /*
252*4882a593Smuzhiyun * When 'ethrotate' variable is set to 'no' and 'ethact' variable
253*4882a593Smuzhiyun * is already set to an ethernet device, we should stick to 'ethact'.
254*4882a593Smuzhiyun */
255*4882a593Smuzhiyun if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
256*4882a593Smuzhiyun if (ethact) {
257*4882a593Smuzhiyun current = eth_get_dev_by_name(ethact);
258*4882a593Smuzhiyun if (!current)
259*4882a593Smuzhiyun return -EINVAL;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun if (!current) {
264*4882a593Smuzhiyun current = eth_get_dev();
265*4882a593Smuzhiyun if (!current) {
266*4882a593Smuzhiyun printf("No ethernet found.\n");
267*4882a593Smuzhiyun return -ENODEV;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun old_current = current;
272*4882a593Smuzhiyun do {
273*4882a593Smuzhiyun if (current) {
274*4882a593Smuzhiyun debug("Trying %s\n", current->name);
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun if (device_active(current)) {
277*4882a593Smuzhiyun ret = eth_get_ops(current)->start(current);
278*4882a593Smuzhiyun if (ret >= 0) {
279*4882a593Smuzhiyun struct eth_device_priv *priv =
280*4882a593Smuzhiyun current->uclass_priv;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun priv->state = ETH_STATE_ACTIVE;
283*4882a593Smuzhiyun return 0;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun } else {
286*4882a593Smuzhiyun ret = eth_errno;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun debug("FAIL\n");
290*4882a593Smuzhiyun } else {
291*4882a593Smuzhiyun debug("PROBE FAIL\n");
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun /*
295*4882a593Smuzhiyun * If ethrotate is enabled, this will change "current",
296*4882a593Smuzhiyun * otherwise we will drop out of this while loop immediately
297*4882a593Smuzhiyun */
298*4882a593Smuzhiyun eth_try_another(0);
299*4882a593Smuzhiyun /* This will ensure the new "current" attempted to probe */
300*4882a593Smuzhiyun current = eth_get_dev();
301*4882a593Smuzhiyun } while (old_current != current);
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun return ret;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
eth_halt(void)306*4882a593Smuzhiyun void eth_halt(void)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun struct udevice *current;
309*4882a593Smuzhiyun struct eth_device_priv *priv;
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun current = eth_get_dev();
312*4882a593Smuzhiyun if (!current || !device_active(current))
313*4882a593Smuzhiyun return;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun eth_get_ops(current)->stop(current);
316*4882a593Smuzhiyun priv = current->uclass_priv;
317*4882a593Smuzhiyun priv->state = ETH_STATE_PASSIVE;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
eth_is_active(struct udevice * dev)320*4882a593Smuzhiyun int eth_is_active(struct udevice *dev)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun struct eth_device_priv *priv;
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun if (!dev || !device_active(dev))
325*4882a593Smuzhiyun return 0;
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun priv = dev_get_uclass_priv(dev);
328*4882a593Smuzhiyun return priv->state == ETH_STATE_ACTIVE;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun
eth_send(void * packet,int length)331*4882a593Smuzhiyun int eth_send(void *packet, int length)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun struct udevice *current;
334*4882a593Smuzhiyun int ret;
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun current = eth_get_dev();
337*4882a593Smuzhiyun if (!current)
338*4882a593Smuzhiyun return -ENODEV;
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun if (!device_active(current))
341*4882a593Smuzhiyun return -EINVAL;
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun ret = eth_get_ops(current)->send(current, packet, length);
344*4882a593Smuzhiyun if (ret < 0) {
345*4882a593Smuzhiyun /* We cannot completely return the error at present */
346*4882a593Smuzhiyun debug("%s: send() returned error %d\n", __func__, ret);
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun return ret;
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun
eth_rx(void)351*4882a593Smuzhiyun int eth_rx(void)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun struct udevice *current;
354*4882a593Smuzhiyun uchar *packet;
355*4882a593Smuzhiyun int flags;
356*4882a593Smuzhiyun int ret;
357*4882a593Smuzhiyun int i;
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun current = eth_get_dev();
360*4882a593Smuzhiyun if (!current)
361*4882a593Smuzhiyun return -ENODEV;
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun if (!device_active(current))
364*4882a593Smuzhiyun return -EINVAL;
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun /* Process up to 32 packets at one time */
367*4882a593Smuzhiyun flags = ETH_RECV_CHECK_DEVICE;
368*4882a593Smuzhiyun for (i = 0; i < 32; i++) {
369*4882a593Smuzhiyun ret = eth_get_ops(current)->recv(current, flags, &packet);
370*4882a593Smuzhiyun flags = 0;
371*4882a593Smuzhiyun if (ret > 0)
372*4882a593Smuzhiyun net_process_received_packet(packet, ret);
373*4882a593Smuzhiyun if (ret >= 0 && eth_get_ops(current)->free_pkt)
374*4882a593Smuzhiyun eth_get_ops(current)->free_pkt(current, packet, ret);
375*4882a593Smuzhiyun if (ret <= 0)
376*4882a593Smuzhiyun break;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun if (ret == -EAGAIN)
379*4882a593Smuzhiyun ret = 0;
380*4882a593Smuzhiyun if (ret < 0) {
381*4882a593Smuzhiyun /* We cannot completely return the error at present */
382*4882a593Smuzhiyun debug("%s: recv() returned error %d\n", __func__, ret);
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun return ret;
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun
eth_initialize(void)387*4882a593Smuzhiyun int eth_initialize(void)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun int num_devices = 0;
390*4882a593Smuzhiyun struct udevice *dev;
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun eth_common_init();
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun /*
395*4882a593Smuzhiyun * Devices need to write the hwaddr even if not started so that Linux
396*4882a593Smuzhiyun * will have access to the hwaddr that u-boot stored for the device.
397*4882a593Smuzhiyun * This is accomplished by attempting to probe each device and calling
398*4882a593Smuzhiyun * their write_hwaddr() operation.
399*4882a593Smuzhiyun */
400*4882a593Smuzhiyun uclass_first_device(UCLASS_ETH, &dev);
401*4882a593Smuzhiyun if (!dev) {
402*4882a593Smuzhiyun printf("No ethernet found.\n");
403*4882a593Smuzhiyun bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
404*4882a593Smuzhiyun } else {
405*4882a593Smuzhiyun char *ethprime = env_get("ethprime");
406*4882a593Smuzhiyun struct udevice *prime_dev = NULL;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun if (ethprime)
409*4882a593Smuzhiyun prime_dev = eth_get_dev_by_name(ethprime);
410*4882a593Smuzhiyun if (prime_dev) {
411*4882a593Smuzhiyun eth_set_dev(prime_dev);
412*4882a593Smuzhiyun eth_current_changed();
413*4882a593Smuzhiyun } else {
414*4882a593Smuzhiyun eth_set_dev(NULL);
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
418*4882a593Smuzhiyun do {
419*4882a593Smuzhiyun if (num_devices)
420*4882a593Smuzhiyun printf(", ");
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun printf("eth%d: %s", dev->seq, dev->name);
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun if (ethprime && dev == prime_dev)
425*4882a593Smuzhiyun printf(" [PRIME]");
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun eth_write_hwaddr(dev);
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun uclass_next_device(&dev);
430*4882a593Smuzhiyun num_devices++;
431*4882a593Smuzhiyun } while (dev);
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun putc('\n');
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun return num_devices;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun
eth_post_bind(struct udevice * dev)439*4882a593Smuzhiyun static int eth_post_bind(struct udevice *dev)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun if (strchr(dev->name, ' ')) {
442*4882a593Smuzhiyun printf("\nError: eth device name \"%s\" has a space!\n",
443*4882a593Smuzhiyun dev->name);
444*4882a593Smuzhiyun return -EINVAL;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun #ifdef CONFIG_DM_ETH_PHY
448*4882a593Smuzhiyun eth_phy_binds_nodes(dev);
449*4882a593Smuzhiyun #endif
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun return 0;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
eth_pre_unbind(struct udevice * dev)454*4882a593Smuzhiyun static int eth_pre_unbind(struct udevice *dev)
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun /* Don't hang onto a pointer that is going away */
457*4882a593Smuzhiyun if (dev == eth_get_uclass_priv()->current)
458*4882a593Smuzhiyun eth_set_dev(NULL);
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun return 0;
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun
eth_post_probe(struct udevice * dev)463*4882a593Smuzhiyun static int eth_post_probe(struct udevice *dev)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun struct eth_device_priv *priv = dev->uclass_priv;
466*4882a593Smuzhiyun struct eth_pdata *pdata = dev->platdata;
467*4882a593Smuzhiyun unsigned char env_enetaddr[ARP_HLEN];
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun #if defined(CONFIG_NEEDS_MANUAL_RELOC)
470*4882a593Smuzhiyun struct eth_ops *ops = eth_get_ops(dev);
471*4882a593Smuzhiyun static int reloc_done;
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun if (!reloc_done) {
474*4882a593Smuzhiyun if (ops->start)
475*4882a593Smuzhiyun ops->start += gd->reloc_off;
476*4882a593Smuzhiyun if (ops->send)
477*4882a593Smuzhiyun ops->send += gd->reloc_off;
478*4882a593Smuzhiyun if (ops->recv)
479*4882a593Smuzhiyun ops->recv += gd->reloc_off;
480*4882a593Smuzhiyun if (ops->free_pkt)
481*4882a593Smuzhiyun ops->free_pkt += gd->reloc_off;
482*4882a593Smuzhiyun if (ops->stop)
483*4882a593Smuzhiyun ops->stop += gd->reloc_off;
484*4882a593Smuzhiyun #ifdef CONFIG_MCAST_TFTP
485*4882a593Smuzhiyun if (ops->mcast)
486*4882a593Smuzhiyun ops->mcast += gd->reloc_off;
487*4882a593Smuzhiyun #endif
488*4882a593Smuzhiyun if (ops->write_hwaddr)
489*4882a593Smuzhiyun ops->write_hwaddr += gd->reloc_off;
490*4882a593Smuzhiyun if (ops->read_rom_hwaddr)
491*4882a593Smuzhiyun ops->read_rom_hwaddr += gd->reloc_off;
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun reloc_done++;
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun #endif
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun priv->state = ETH_STATE_INIT;
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun /* Check if the device has a MAC address in ROM */
500*4882a593Smuzhiyun if (eth_get_ops(dev)->read_rom_hwaddr)
501*4882a593Smuzhiyun eth_get_ops(dev)->read_rom_hwaddr(dev);
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
504*4882a593Smuzhiyun if (!is_zero_ethaddr(env_enetaddr)) {
505*4882a593Smuzhiyun if (!is_zero_ethaddr(pdata->enetaddr) &&
506*4882a593Smuzhiyun memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
507*4882a593Smuzhiyun printf("\nWarning: %s MAC addresses don't match:\n",
508*4882a593Smuzhiyun dev->name);
509*4882a593Smuzhiyun printf("Address in ROM is %pM\n",
510*4882a593Smuzhiyun pdata->enetaddr);
511*4882a593Smuzhiyun printf("Address in environment is %pM\n",
512*4882a593Smuzhiyun env_enetaddr);
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun /* Override the ROM MAC address */
516*4882a593Smuzhiyun memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
517*4882a593Smuzhiyun } else if (is_valid_ethaddr(pdata->enetaddr)) {
518*4882a593Smuzhiyun eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
519*4882a593Smuzhiyun printf("\nWarning: %s using MAC address from ROM\n",
520*4882a593Smuzhiyun dev->name);
521*4882a593Smuzhiyun } else if (is_zero_ethaddr(pdata->enetaddr) ||
522*4882a593Smuzhiyun !is_valid_ethaddr(pdata->enetaddr)) {
523*4882a593Smuzhiyun #ifdef CONFIG_NET_RANDOM_ETHADDR
524*4882a593Smuzhiyun net_random_ethaddr(pdata->enetaddr);
525*4882a593Smuzhiyun printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
526*4882a593Smuzhiyun dev->name, dev->seq, pdata->enetaddr);
527*4882a593Smuzhiyun #else
528*4882a593Smuzhiyun printf("\nError: %s address not set.\n",
529*4882a593Smuzhiyun dev->name);
530*4882a593Smuzhiyun return -EINVAL;
531*4882a593Smuzhiyun #endif
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun return 0;
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun
eth_pre_remove(struct udevice * dev)537*4882a593Smuzhiyun static int eth_pre_remove(struct udevice *dev)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun struct eth_pdata *pdata = dev->platdata;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun eth_get_ops(dev)->stop(dev);
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun /* clear the MAC address */
544*4882a593Smuzhiyun memset(pdata->enetaddr, 0, ARP_HLEN);
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun return 0;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun UCLASS_DRIVER(ethernet) = {
550*4882a593Smuzhiyun .name = "ethernet",
551*4882a593Smuzhiyun .id = UCLASS_ETH,
552*4882a593Smuzhiyun .post_bind = eth_post_bind,
553*4882a593Smuzhiyun .pre_unbind = eth_pre_unbind,
554*4882a593Smuzhiyun .post_probe = eth_post_probe,
555*4882a593Smuzhiyun .pre_remove = eth_pre_remove,
556*4882a593Smuzhiyun .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
557*4882a593Smuzhiyun .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
558*4882a593Smuzhiyun .flags = DM_UC_FLAG_SEQ_ALIAS,
559*4882a593Smuzhiyun };
560