blob: 8bdfb302f7283e3939c57c146d12eafcf92baf01 [file] [log] [blame]
Neil Armstrong5aeecbc2020-09-21 09:34:13 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 BayLibre, SAS
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <env_internal.h>
10#include <init.h>
11#include <net.h>
12#include <asm/io.h>
Marek Szyprowskiaa3c7912020-12-18 15:26:45 +010013#include <asm/arch/boot.h>
Neil Armstrong5aeecbc2020-09-21 09:34:13 +020014#include <asm/arch/eth.h>
Marek Szyprowskid10037e2020-12-17 08:26:42 +010015#include <asm/arch/sm.h>
Simon Glass401d1c42020-10-30 21:38:53 -060016#include <asm/global_data.h>
Neil Armstrongadbff642020-09-21 09:34:15 +020017#include <i2c.h>
18#include "khadas-mcu.h"
19
Marek Szyprowskiaa3c7912020-12-18 15:26:45 +010020int mmc_get_env_dev(void)
21{
Artem Lapkin786c5142021-05-27 15:35:28 +080022 switch (meson_get_boot_device()) {
23 case BOOT_DEVICE_EMMC:
Marek Szyprowskiaa3c7912020-12-18 15:26:45 +010024 return 2;
Artem Lapkin786c5142021-05-27 15:35:28 +080025 case BOOT_DEVICE_SD:
26 return 1;
27 default:
28 /* boot device is not EMMC|SD */
29 return -1;
30 }
Marek Szyprowskiaa3c7912020-12-18 15:26:45 +010031}
32
Neil Armstrongadbff642020-09-21 09:34:15 +020033/*
34 * The VIM3 on-board MCU can mux the PCIe/USB3.0 shared differential
35 * lines using a FUSB340TMX USB 3.1 SuperSpeed Data Switch between
36 * an USB3.0 Type A connector and a M.2 Key M slot.
37 * The PHY driving these differential lines is shared between
38 * the USB3.0 controller and the PCIe Controller, thus only
39 * a single controller can use it.
40 */
41int meson_ft_board_setup(void *blob, struct bd_info *bd)
42{
43 struct udevice *bus, *dev;
44 int node, i2c_node, ret;
45 unsigned int i2c_addr;
46 u32 *val;
47
48 /* Find I2C device */
49 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "khadas,mcu");
50 if (node < 0) {
51 printf("vim3: cannot find khadas,mcu node\n");
52 return 0;
53 }
54
55 /* Get addr */
56 val = (u32 *)fdt_getprop(gd->fdt_blob, node, "reg", NULL);
57 if (!val) {
58 printf("vim3: cannot find khadas,mcu node i2c addr\n");
59 return 0;
60 }
61 i2c_addr = fdt32_to_cpu(*val);
62
63 /* Get i2c device */
64 i2c_node = fdt_parent_offset(gd->fdt_blob, node);
65 if (node < 0) {
66 printf("vim3: cannot find khadas,mcu i2c node\n");
67 return 0;
68 }
69
70 ret = uclass_get_device_by_of_offset(UCLASS_I2C, i2c_node, &bus);
71 if (ret < 0) {
72 printf("vim3: cannot find i2c bus (%d)\n", ret);
73 return 0;
74 }
75
76 ret = i2c_get_chip(bus, i2c_addr, 1, &dev);
77 if (ret < 0) {
78 printf("vim3: cannot find i2c chip (%d)\n", ret);
79 return 0;
80 }
81
82 /* Read USB_PCIE_SWITCH_REG */
83 ret = dm_i2c_reg_read(dev, KHADAS_MCU_USB_PCIE_SWITCH_REG);
84 if (ret < 0) {
85 printf("vim3: failed to read i2c reg (%d)\n", ret);
86 return 0;
87 }
88 debug("MCU_USB_PCIE_SWITCH_REG: %d\n", ret);
89
90 /*
91 * If in PCIe mode, alter DT
92 * 0:Enable USB3.0,Disable PCIE, 1:Disable USB3.0, Enable PCIE
93 */
94 if (ret > 0) {
95 static char data[32] __aligned(4);
96 const void *ptmp;
97 int len;
98
99 /* Find USB node */
100 node = fdt_node_offset_by_compatible(blob, -1, "amlogic,meson-g12a-usb-ctrl");
101 if (node < 0) {
102 printf("vim3: cannot find amlogic,meson-g12a-usb-ctrl node\n");
103 return 0;
104 }
105
106 /* Update PHY names (mandatory to disable USB3.0) */
Matthias Schiffer61582872023-07-14 13:24:50 +0200107 len = strlcpy(data, "usb2-phy0", 32) + 1;
108 len += strlcpy(&data[len], "usb2-phy1", 32 - len) + 1;
Neil Armstrongadbff642020-09-21 09:34:15 +0200109 ret = fdt_setprop(blob, node, "phy-names", data, len);
110 if (ret < 0) {
111 printf("vim3: failed to update usb phy names property (%d)\n", ret);
112 return 0;
113 }
114
115 /* Update PHY list, by keeping the 2 first entries (optional) */
116 ptmp = fdt_getprop(blob, node, "phys", &len);
117 if (ptmp) {
118 memcpy(data, ptmp, min_t(unsigned int, 2 * sizeof(u32), len));
119
120 ret = fdt_setprop(blob, node, "phys", data,
121 min_t(unsigned int, 2 * sizeof(u32), len));
122 if (ret < 0)
123 printf("vim3: failed to update usb phys property (%d)\n", ret);
124 } else
125 printf("vim3: cannot find usb node phys property\n");
126
127 /* Find PCIe node */
128 node = fdt_node_offset_by_compatible(blob, -1, "amlogic,g12a-pcie");
129 if (node < 0) {
130 printf("vim3: cannot find amlogic,g12a-pcie node\n");
131 return 0;
132 }
133
134 /* Enable PCIe */
Matthias Schiffer61582872023-07-14 13:24:50 +0200135 len = strlcpy(data, "okay", 32) + 1;
Neil Armstrongadbff642020-09-21 09:34:15 +0200136 ret = fdt_setprop(blob, node, "status", data, len);
137 if (ret < 0) {
138 printf("vim3: failed to enable pcie node (%d)\n", ret);
139 return 0;
140 }
141
142 printf("vim3: successfully enabled PCIe\n");
143 }
144
145 return 0;
146}
Neil Armstrong5aeecbc2020-09-21 09:34:13 +0200147
Marek Szyprowskid10037e2020-12-17 08:26:42 +0100148#define EFUSE_MAC_OFFSET 0
Artem Lapkin6bfa3312021-01-12 19:42:12 +0800149#define EFUSE_MAC_SIZE 12
150#define MAC_ADDR_LEN 6
Marek Szyprowskid10037e2020-12-17 08:26:42 +0100151
Neil Armstrong5aeecbc2020-09-21 09:34:13 +0200152int misc_init_r(void)
153{
Artem Lapkin6bfa3312021-01-12 19:42:12 +0800154 u8 mac_addr[MAC_ADDR_LEN];
155 char efuse_mac_addr[EFUSE_MAC_SIZE], tmp[3];
Mattijs Korpershoek09c2deb2022-01-19 15:24:00 +0100156 char serial_string[EFUSE_MAC_SIZE + 1];
Marek Szyprowskid10037e2020-12-17 08:26:42 +0100157 ssize_t len;
158
Marek Szyprowskid10037e2020-12-17 08:26:42 +0100159 if (!eth_env_get_enetaddr("ethaddr", mac_addr)) {
160 len = meson_sm_read_efuse(EFUSE_MAC_OFFSET,
Artem Lapkin6bfa3312021-01-12 19:42:12 +0800161 efuse_mac_addr, EFUSE_MAC_SIZE);
Marek Szyprowskid10037e2020-12-17 08:26:42 +0100162 if (len != EFUSE_MAC_SIZE)
163 return 0;
164
Artem Lapkin6bfa3312021-01-12 19:42:12 +0800165 /* MAC is stored in ASCII format, 1bytes = 2characters */
166 for (int i = 0; i < 6; i++) {
167 tmp[0] = efuse_mac_addr[i * 2];
168 tmp[1] = efuse_mac_addr[i * 2 + 1];
169 tmp[2] = '\0';
Simon Glass7e5f4602021-07-24 09:03:29 -0600170 mac_addr[i] = hextoul(tmp, NULL);
Artem Lapkin6bfa3312021-01-12 19:42:12 +0800171 }
172
Marek Szyprowskid10037e2020-12-17 08:26:42 +0100173 if (is_valid_ethaddr(mac_addr))
174 eth_env_set_enetaddr("ethaddr", mac_addr);
175 else
176 meson_generate_serial_ethaddr();
Artem Lapkin6bfa3312021-01-12 19:42:12 +0800177
178 eth_env_get_enetaddr("ethaddr", mac_addr);
Marek Szyprowskid10037e2020-12-17 08:26:42 +0100179 }
180
Mattijs Korpershoek09c2deb2022-01-19 15:24:00 +0100181 if (!env_get("serial#")) {
182 eth_env_get_enetaddr("ethaddr", mac_addr);
183 sprintf(serial_string, "%02X%02X%02X%02X%02X%02X",
184 mac_addr[0], mac_addr[1], mac_addr[2],
185 mac_addr[3], mac_addr[4], mac_addr[5]);
186 env_set("serial#", serial_string);
187 }
188
Neil Armstrong5aeecbc2020-09-21 09:34:13 +0200189 return 0;
190}