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