p2771-0000: Pass Ethernet MAC to the kernel
[oweals/u-boot.git] / board / nvidia / p2771-0000 / p2771-0000.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016, NVIDIA CORPORATION
4  */
5
6 #include <common.h>
7 #include <environment.h>
8 #include <i2c.h>
9 #include <linux/libfdt.h>
10 #include <asm/arch-tegra/cboot.h>
11 #include "../p2571/max77620_init.h"
12
13 void pin_mux_mmc(void)
14 {
15         struct udevice *dev;
16         uchar val;
17         int ret;
18
19         /* Turn on MAX77620 LDO3 to 3.3V for SD card power */
20         debug("%s: Set LDO3 for VDDIO_SDMMC_AP power to 3.3V\n", __func__);
21         ret = i2c_get_chip_for_busnum(0, MAX77620_I2C_ADDR_7BIT, 1, &dev);
22         if (ret) {
23                 printf("%s: Cannot find MAX77620 I2C chip\n", __func__);
24                 return;
25         }
26         /* 0xF2 for 3.3v, enabled: bit7:6 = 11 = enable, bit5:0 = voltage */
27         val = 0xF2;
28         ret = dm_i2c_write(dev, MAX77620_CNFG1_L3_REG, &val, 1);
29         if (ret) {
30                 printf("i2c_write 0 0x3c 0x27 failed: %d\n", ret);
31                 return;
32         }
33 }
34
35 #ifdef CONFIG_PCI_TEGRA
36 int tegra_pcie_board_init(void)
37 {
38         struct udevice *dev;
39         uchar val;
40         int ret;
41
42         /* Turn on MAX77620 LDO7 to 1.05V for PEX power */
43         debug("%s: Set LDO7 for PEX power to 1.05V\n", __func__);
44         ret = i2c_get_chip_for_busnum(0, MAX77620_I2C_ADDR_7BIT, 1, &dev);
45         if (ret) {
46                 printf("%s: Cannot find MAX77620 I2C chip\n", __func__);
47                 return -1;
48         }
49         /* 0xC5 for 1.05v, enabled: bit7:6 = 11 = enable, bit5:0 = voltage */
50         val = 0xC5;
51         ret = dm_i2c_write(dev, MAX77620_CNFG1_L7_REG, &val, 1);
52         if (ret)
53                 printf("i2c_write 0 0x3c 0x31 failed: %d\n", ret);
54
55         return 0;
56 }
57 #endif
58
59 int ft_board_setup(void *fdt, bd_t *bd)
60 {
61         const void *cboot_fdt = (const void *)cboot_boot_x0;
62         uint8_t mac[ETH_ALEN], local_mac[ETH_ALEN];
63         const char *path;
64         int offset, err;
65
66         err = cboot_get_ethaddr(cboot_fdt, local_mac);
67         if (err < 0)
68                 memset(local_mac, 0, ETH_ALEN);
69
70         path = fdt_get_alias(fdt, "ethernet");
71         if (!path)
72                 return 0;
73
74         debug("ethernet alias found: %s\n", path);
75
76         offset = fdt_path_offset(fdt, path);
77         if (offset < 0)
78                 return 0;
79
80         if (is_valid_ethaddr(local_mac)) {
81                 err = fdt_setprop(fdt, offset, "local-mac-address", local_mac,
82                                   ETH_ALEN);
83                 if (!err)
84                         debug("Local MAC address set: %pM\n", local_mac);
85         }
86
87         if (eth_env_get_enetaddr("ethaddr", mac)) {
88                 if (memcmp(local_mac, mac, ETH_ALEN) != 0) {
89                         err = fdt_setprop(fdt, offset, "mac-address", mac,
90                                           ETH_ALEN);
91                         if (!err)
92                                 debug("MAC address set: %pM\n", mac);
93                 }
94         }
95
96         return 0;
97 }